Skip to content

Spacing type radio button for repeat and circular repeat node #2674

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use graphene_std::transform::{Footprint, ReferencePoint};
use graphene_std::vector::VectorDataTable;
use graphene_std::vector::misc::ArcType;
use graphene_std::vector::misc::{BooleanOperation, GridType};
use graphene_std::vector::style::{Fill, FillChoice, FillType, GradientStops};
use graphene_std::vector::style::{CircularSpacing, Fill, FillChoice, FillType, GradientStops, Spacing};
use graphene_std::{GraphicGroupTable, NodeInputDecleration, RasterFrame};

pub(crate) fn string_properties(text: &str) -> Vec<LayoutGroup> {
Expand Down Expand Up @@ -122,6 +122,9 @@ pub(crate) fn property_from_type(
index: usize,
ty: &Type,
number_options: (Option<f64>, Option<f64>, Option<(f64, f64)>),
unit: Option<&str>,
display_decimal_places: Option<u32>,
step: Option<f64>,
context: &mut NodePropertiesContext,
) -> Result<Vec<LayoutGroup>, Vec<LayoutGroup>> {
let Some(network) = context.network_interface.nested_network(context.selection_network_path) else {
Expand All @@ -138,6 +141,15 @@ pub(crate) fn property_from_type(

let (mut number_min, mut number_max, range) = number_options;
let mut number_input = NumberInput::default();
if let Some(unit) = unit {
number_input = number_input.unit(unit);
}
if let Some(display_decimal_places) = display_decimal_places {
number_input = number_input.display_decimal_places(display_decimal_places);
}
if let Some(step) = step {
number_input = number_input.step(step);
}
if let Some((range_start, range_end)) = range {
number_min = Some(range_start);
number_max = Some(range_end);
Expand Down Expand Up @@ -226,6 +238,8 @@ pub(crate) fn property_from_type(
Some(x) if x == TypeId::of::<LineJoin>() => enum_choice::<LineJoin>().for_socket(default_info).property_row(),
Some(x) if x == TypeId::of::<ArcType>() => enum_choice::<ArcType>().for_socket(default_info).property_row(),
Some(x) if x == TypeId::of::<BooleanOperation>() => enum_choice::<BooleanOperation>().for_socket(default_info).property_row(),
Some(x) if x == TypeId::of::<Spacing>() => enum_choice::<Spacing>().for_socket(default_info).property_row(),
Some(x) if x == TypeId::of::<CircularSpacing>() => enum_choice::<CircularSpacing>().for_socket(default_info).property_row(),
Some(x) if x == TypeId::of::<CentroidType>() => enum_choice::<CentroidType>().for_socket(default_info).property_row(),
Some(x) if x == TypeId::of::<LuminanceCalculation>() => enum_choice::<LuminanceCalculation>().for_socket(default_info).property_row(),
// =====
Expand All @@ -250,8 +264,8 @@ pub(crate) fn property_from_type(
}
}
Type::Generic(_) => vec![TextLabel::new("Generic type (not supported)").widget_holder()].into(),
Type::Fn(_, out) => return property_from_type(node_id, index, out, number_options, context),
Type::Future(out) => return property_from_type(node_id, index, out, number_options, context),
Type::Fn(_, out) => return property_from_type(node_id, index, out, number_options, unit, display_decimal_places, step, context),
Type::Future(out) => return property_from_type(node_id, index, out, number_options, unit, display_decimal_places, step, context),
};

extra_widgets.push(widgets);
Expand Down Expand Up @@ -1387,6 +1401,9 @@ pub(crate) fn generate_node_properties(node_id: NodeId, context: &mut NodeProper
};

let mut number_options = (None, None, None);
let mut display_decimal_places = None;
let mut step = None;
let mut unit_suffix = None;
let input_type = match implementation {
DocumentNodeImplementation::ProtoNode(proto_node_identifier) => 'early_return: {
if let Some(field) = graphene_core::registry::NODE_METADATA
Expand All @@ -1395,6 +1412,9 @@ pub(crate) fn generate_node_properties(node_id: NodeId, context: &mut NodeProper
.get(&proto_node_identifier.name.clone().into_owned())
.and_then(|metadata| metadata.fields.get(input_index))
{
display_decimal_places = field.number_display_decimal_places;
unit_suffix = field.unit;
step = field.number_step;
number_options = (field.number_min, field.number_max, field.number_mode_range);
if let Some(ref default) = field.default_type {
break 'early_return default.clone();
Expand All @@ -1409,7 +1429,7 @@ pub(crate) fn generate_node_properties(node_id: NodeId, context: &mut NodeProper
let mut input_types = implementations
.keys()
.filter_map(|item| item.inputs.get(input_index))
.filter(|ty| property_from_type(node_id, input_index, ty, number_options, context).is_ok())
.filter(|ty| property_from_type(node_id, input_index, ty, number_options, unit_suffix, display_decimal_places, step, context).is_ok())
.collect::<Vec<_>>();
input_types.sort_by_key(|ty| ty.type_name());
let input_type = input_types.first().cloned();
Expand All @@ -1423,7 +1443,7 @@ pub(crate) fn generate_node_properties(node_id: NodeId, context: &mut NodeProper
_ => context.network_interface.input_type(&InputConnector::node(node_id, input_index), context.selection_network_path).0,
};

property_from_type(node_id, input_index, &input_type, number_options, context).unwrap_or_else(|value| value)
property_from_type(node_id, input_index, &input_type, number_options, unit_suffix, display_decimal_places, step, context).unwrap_or_else(|value| value)
});

layout.extend(row);
Expand Down
3 changes: 3 additions & 0 deletions node-graph/gcore/src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ pub struct FieldMetadata {
pub number_min: Option<f64>,
pub number_max: Option<f64>,
pub number_mode_range: Option<(f64, f64)>,
pub number_display_decimal_places: Option<u32>,
pub number_step: Option<f64>,
pub unit: Option<&'static str>,
}

pub trait ChoiceTypeStatic: Sized + Copy + crate::vector::misc::AsU32 + Send + Sync {
Expand Down
18 changes: 18 additions & 0 deletions node-graph/gcore/src/vector/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -922,3 +922,21 @@ pub enum ViewMode {
/// Render with normal coloration at the document resolution, showing the pixels when the current viewport resolution is higher
Pixels,
}

#[derive(Debug, Default, PartialEq, Eq, Clone, Copy, Hash, serde::Serialize, serde::Deserialize, DynAny, specta::Type, node_macro::ChoiceType)]
#[widget(Radio)]
pub enum Spacing {
#[default]
Span,
Envelope,
Pitch,
Gap,
}

#[derive(Debug, Default, PartialEq, Eq, Clone, Copy, Hash, serde::Serialize, serde::Deserialize, DynAny, specta::Type, node_macro::ChoiceType)]
#[widget(Radio)]
pub enum CircularSpacing {
#[default]
Span,
Pitch,
}
75 changes: 67 additions & 8 deletions node-graph/gcore/src/vector/vector_nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::renderer::GraphicElementRendered;
use crate::transform::{Footprint, ReferencePoint, Transform, TransformMut};
use crate::vector::PointDomain;
use crate::vector::misc::dvec2_to_point;
use crate::vector::style::{LineCap, LineJoin};
use crate::vector::style::{CircularSpacing, LineCap, LineJoin, Spacing};
use crate::{CloneVarArgs, Color, Context, Ctx, ExtractAll, GraphicElement, GraphicGroupTable, OwnedContextImpl};
use bezier_rs::{Join, ManipulatorGroup, Subpath, SubpathTValue};
use core::f64::consts::PI;
Expand Down Expand Up @@ -210,6 +210,7 @@ async fn repeat<I: 'n + Send>(
direction: PixelSize,
angle: Angle,
#[default(4)] instances: IntegerCount,
spacing: Spacing,
) -> GraphicGroupTable
where
Instances<I>: GraphicElementRendered,
Expand All @@ -225,11 +226,47 @@ where
};

let center = (bounding_box[0] + bounding_box[1]) / 2.;
let exact_size = (bounding_box[1] - bounding_box[0]).abs();

for index in 0..instances {
let angle = index as f64 * angle / total;
let translation = index as f64 * direction / total;
let transform = DAffine2::from_translation(center) * DAffine2::from_angle(angle) * DAffine2::from_translation(translation) * DAffine2::from_translation(-center);
let mut translation = index as f64 * direction / total;
let mut size = index as f64 * exact_size / total;

let transform = match spacing {
Spacing::Span => DAffine2::from_translation(center) * DAffine2::from_translation(translation) * DAffine2::from_angle(angle) * DAffine2::from_translation(-center),
Spacing::Envelope => {
if direction.x < -exact_size.x {
size.x -= size.x * 2.;
} else if direction.x <= exact_size.x {
size.x = 0.;
translation.x = 0.;
}
if direction.y < -exact_size.y {
size.y -= size.y * 2.;
} else if direction.y <= exact_size.y {
size.y = 0.;
translation.y = 0.;
}
if size == DVec2::ZERO {
DAffine2::from_translation(center) * DAffine2::from_angle(angle) * DAffine2::from_translation(-center)
} else {
DAffine2::from_translation(size).inverse()
* DAffine2::from_translation(center)
* DAffine2::from_translation(translation)
* DAffine2::from_angle(angle)
* DAffine2::from_translation(-center)
}
}
Spacing::Pitch => DAffine2::from_translation(center) * DAffine2::from_translation(index as f64 * direction) * DAffine2::from_angle(angle) * DAffine2::from_translation(-center),
Spacing::Gap => {
DAffine2::from_translation(center)
* DAffine2::from_translation(index as f64 * exact_size)
* DAffine2::from_translation(index as f64 * direction)
* DAffine2::from_angle(angle)
* DAffine2::from_translation(-center)
}
};

result_table.push(Instance {
instance: instance.to_graphic_element().clone(),
Expand All @@ -248,8 +285,12 @@ async fn circular_repeat<I: 'n + Send>(
// TODO: Implement other GraphicElementRendered types.
#[implementations(GraphicGroupTable, VectorDataTable, ImageFrameTable<Color>)] instance: Instances<I>,
angle_offset: Angle,
#[default(180.)]
#[unit("°")]
angle_pitch: f64,
#[default(5)] radius: f64,
#[default(5)] instances: IntegerCount,
spacing: CircularSpacing,
) -> GraphicGroupTable
where
Instances<I>: GraphicElementRendered,
Expand All @@ -264,10 +305,19 @@ where

let center = (bounding_box[0] + bounding_box[1]) / 2.;
let base_transform = DVec2::new(0., radius) - center;
let circle = angle_pitch.to_radians();

for index in 0..instances {
let rotation = DAffine2::from_angle((std::f64::consts::TAU / instances as f64) * index as f64 + angle_offset.to_radians());
let transform = DAffine2::from_translation(center) * rotation * DAffine2::from_translation(base_transform);
let transform = match spacing {
CircularSpacing::Span => {
let rotation = DAffine2::from_angle((circle / instances as f64) * index as f64 + angle_offset.to_radians());
DAffine2::from_translation(center) * rotation * DAffine2::from_translation(base_transform)
}
CircularSpacing::Pitch => {
let rotation = DAffine2::from_angle(circle * index as f64 + angle_offset.to_radians());
DAffine2::from_translation(center) * rotation * DAffine2::from_translation(base_transform)
}
};

result_table.push(Instance {
instance: instance.to_graphic_element().clone(),
Expand Down Expand Up @@ -1817,7 +1867,7 @@ mod test {
async fn repeat() {
let direction = DVec2::X * 1.5;
let instances = 3;
let repeated = super::repeat(Footprint::default(), vector_node(Subpath::new_rect(DVec2::ZERO, DVec2::ONE)), direction, 0., instances).await;
let repeated = super::repeat(Footprint::default(), vector_node(Subpath::new_rect(DVec2::ZERO, DVec2::ONE)), direction, 0., instances, Spacing::Span).await;
let vector_data = super::flatten_vector_elements(Footprint::default(), repeated).await;
let vector_data = vector_data.instance_ref_iter().next().unwrap().instance;
assert_eq!(vector_data.region_bezier_paths().count(), 3);
Expand All @@ -1829,7 +1879,7 @@ mod test {
async fn repeat_transform_position() {
let direction = DVec2::new(12., 10.);
let instances = 8;
let repeated = super::repeat(Footprint::default(), vector_node(Subpath::new_rect(DVec2::ZERO, DVec2::ONE)), direction, 0., instances).await;
let repeated = super::repeat(Footprint::default(), vector_node(Subpath::new_rect(DVec2::ZERO, DVec2::ONE)), direction, 0., instances, Spacing::Span).await;
let vector_data = super::flatten_vector_elements(Footprint::default(), repeated).await;
let vector_data = vector_data.instance_ref_iter().next().unwrap().instance;
assert_eq!(vector_data.region_bezier_paths().count(), 8);
Expand All @@ -1839,7 +1889,16 @@ mod test {
}
#[tokio::test]
async fn circular_repeat() {
let repeated = super::circular_repeat(Footprint::default(), vector_node(Subpath::new_rect(DVec2::NEG_ONE, DVec2::ONE)), 45., 4., 8).await;
let repeated = super::circular_repeat(
Footprint::default(),
vector_node(Subpath::new_rect(DVec2::NEG_ONE, DVec2::ONE)),
45.,
360.,
4.,
8,
CircularSpacing::Span,
)
.await;
let vector_data = super::flatten_vector_elements(Footprint::default(), repeated).await;
let vector_data = vector_data.instance_ref_iter().next().unwrap().instance;
assert_eq!(vector_data.region_bezier_paths().count(), 8);
Expand Down
2 changes: 2 additions & 0 deletions node-graph/graph-craft/src/document/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,8 @@ tagged_value! {
FillType(graphene_core::vector::style::FillType),
FillChoice(graphene_core::vector::style::FillChoice),
GradientType(graphene_core::vector::style::GradientType),
Spacing(graphene_core::vector::style::Spacing),
CircularSpacing(graphene_core::vector::style::CircularSpacing),
ReferencePoint(graphene_core::transform::ReferencePoint),
CentroidType(graphene_core::vector::misc::CentroidType),
BooleanOperation(graphene_core::vector::misc::BooleanOperation),
Expand Down
38 changes: 38 additions & 0 deletions node-graph/node-macro/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,41 @@ pub(crate) fn generate_node_code(parsed: &ParsedNodeFn) -> syn::Result<TokenStre
_ => quote!(None),
})
.collect();
let number_display_decimal_places: Vec<_> = fields
.iter()
.map(|field| match field {
ParsedField::Regular {
number_display_decimal_places: Some(decimal_places),
..
}
| ParsedField::Node {
number_display_decimal_places: Some(decimal_places),
..
} => {
quote!(Some(#decimal_places))
}
_ => quote!(None),
})
.collect();
let number_step: Vec<_> = fields
.iter()
.map(|field| match field {
ParsedField::Regular { number_step: Some(step), .. } | ParsedField::Node { number_step: Some(step), .. } => {
quote!(Some(#step))
}
_ => quote!(None),
})
.collect();

let unit_suffix: Vec<_> = fields
.iter()
.map(|field| match field {
ParsedField::Regular { unit: Some(unit), .. } | ParsedField::Node { unit: Some(unit), .. } => {
quote!(Some(#unit))
}
_ => quote!(None),
})
.collect();

let exposed: Vec<_> = fields
.iter()
Expand Down Expand Up @@ -375,6 +410,9 @@ pub(crate) fn generate_node_code(parsed: &ParsedNodeFn) -> syn::Result<TokenStre
number_min: #number_min_values,
number_max: #number_max_values,
number_mode_range: #number_mode_range_values,
number_display_decimal_places: #number_display_decimal_places,
number_step: #number_step,
unit: #unit_suffix,
},
)*
],
Expand Down
Loading