This commit is contained in:
Adam Gerhant 2025-07-06 21:36:58 -07:00 committed by GitHub
commit c8dde23f0a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
15 changed files with 366 additions and 46 deletions

View file

@ -84,11 +84,11 @@ fn static_nodes() -> Vec<DocumentNodeDefinition> {
let custom = vec![
// TODO: Auto-generate this from its proto node macro
DocumentNodeDefinition {
identifier: "Identity",
identifier: "Pass Through",
category: "General",
node_template: NodeTemplate {
document_node: DocumentNode {
implementation: DocumentNodeImplementation::proto("graphene_core::ops::IdentityNode"),
implementation: DocumentNodeImplementation::proto("graphene_std::any::IdentityNode"),
inputs: vec![NodeInput::value(TaggedValue::None, true)],
..Default::default()
},
@ -99,7 +99,26 @@ fn static_nodes() -> Vec<DocumentNodeDefinition> {
},
},
description: Cow::Borrowed("Passes-through the input value without changing it. This is useful for rerouting wires for organization purposes."),
properties: Some("identity_properties"),
properties: Some("pass_through_properties"),
},
DocumentNodeDefinition {
identifier: "Value",
category: "General",
node_template: NodeTemplate {
document_node: DocumentNode {
implementation: DocumentNodeImplementation::proto("graphene_std::any::IdentityNode"),
manual_composition: Some(generic!(T)),
inputs: vec![NodeInput::value(TaggedValue::None, false)],
..Default::default()
},
persistent_node_metadata: DocumentNodePersistentMetadata {
input_metadata: vec![("", "Value").into()],
output_names: vec!["Out".to_string()],
..Default::default()
},
},
description: Cow::Borrowed("Returns the value stored in its input"),
properties: Some("value_properties"),
},
// TODO: Auto-generate this from its proto node macro
DocumentNodeDefinition {
@ -2142,13 +2161,14 @@ fn static_node_properties() -> NodeProperties {
map.insert("grid_properties".to_string(), Box::new(node_properties::grid_properties));
map.insert("sample_polyline_properties".to_string(), Box::new(node_properties::sample_polyline_properties));
map.insert(
"identity_properties".to_string(),
Box::new(|_node_id, _context| node_properties::string_properties("The identity node passes its data through.")),
"pass_through_properties".to_string(),
Box::new(|_node_id, _context| node_properties::string_properties("The Pass Through node can be used to organize wires.")),
);
map.insert(
"monitor_properties".to_string(),
Box::new(|_node_id, _context| node_properties::string_properties("The Monitor node is used by the editor to access the data flowing through it.")),
);
map.insert("value_properties".to_string(), Box::new(node_properties::value_properties));
map
}

View file

@ -147,6 +147,10 @@ pub enum NodeGraphMessage {
node_id: NodeId,
alias: String,
},
SetReference {
node_id: NodeId,
reference: Option<String>,
},
SetToNodeOrLayer {
node_id: NodeId,
is_layer: bool,

View file

@ -498,7 +498,10 @@ impl<'a> MessageHandler<NodeGraphMessage, NodeGraphHandlerData<'a>> for NodeGrap
log::error!("Could not get center of selected_nodes");
return;
};
let center_of_selected_nodes_grid_space = IVec2::new((center_of_selected_nodes.x / 24. + 0.5).floor() as i32, (center_of_selected_nodes.y / 24. + 0.5).floor() as i32);
let center_of_selected_nodes_grid_space = IVec2::new(
(center_of_selected_nodes.x / GRID_SIZE as f64 + 0.5).floor() as i32,
(center_of_selected_nodes.y / GRID_SIZE as f64 + 0.5).floor() as i32,
);
default_node_template.persistent_node_metadata.node_type_metadata = NodeTypePersistentMetadata::node(center_of_selected_nodes_grid_space - IVec2::new(3, 1));
responses.add(DocumentMessage::AddTransaction);
responses.add(NodeGraphMessage::InsertNode {
@ -603,7 +606,7 @@ impl<'a> MessageHandler<NodeGraphMessage, NodeGraphHandlerData<'a>> for NodeGrap
log::error!("Could not get network metadata in PointerDown");
return;
};
self.disconnecting = None;
let click = ipp.mouse.position;
let node_graph_point = network_metadata.persistent_metadata.navigation_metadata.node_graph_to_viewport.inverse().transform_point2(click);
@ -938,6 +941,24 @@ impl<'a> MessageHandler<NodeGraphMessage, NodeGraphHandlerData<'a>> for NodeGrap
};
responses.add(FrontendMessage::UpdateWirePathInProgress { wire_path: Some(wire_path) });
}
}
// Dragging from an exposed value input
else if self.disconnecting.is_some() {
let to_connector = network_interface.input_connector_from_click(ipp.mouse.position, selection_network_path);
if let Some(to_connector) = &to_connector {
let Some(input_position) = network_interface.input_position(to_connector, selection_network_path) else {
log::error!("Could not get input position for connector: {to_connector:?}");
return;
};
self.wire_in_progress_to_connector = Some(input_position);
}
// Not hovering over a node input or node output, create the value node if alt is pressed
else if ipp.keyboard.get(Key::Alt as usize) {
self.preview_on_mouse_up = None;
self.create_value_node(network_interface, point, breadcrumb_network_path, responses);
} else {
//TODO: Start creating wire
}
} else if let Some((drag_start, dragged)) = &mut self.drag_start {
if drag_start.start_x != point.x || drag_start.start_y != point.y {
*dragged = true;
@ -957,7 +978,10 @@ impl<'a> MessageHandler<NodeGraphMessage, NodeGraphHandlerData<'a>> for NodeGrap
}
}
let mut graph_delta = IVec2::new(((point.x - drag_start.start_x) / 24.).round() as i32, ((point.y - drag_start.start_y) / 24.).round() as i32);
let mut graph_delta = IVec2::new(
((point.x - drag_start.start_x) / GRID_SIZE as f64).round() as i32,
((point.y - drag_start.start_y) / GRID_SIZE as f64).round() as i32,
);
let previous_round_x = drag_start.round_x;
let previous_round_y = drag_start.round_y;
@ -1344,6 +1368,7 @@ impl<'a> MessageHandler<NodeGraphMessage, NodeGraphHandlerData<'a>> for NodeGrap
let document_bbox: [DVec2; 2] = viewport_bbox.map(|p| network_metadata.persistent_metadata.navigation_metadata.node_graph_to_viewport.inverse().transform_point2(p));
let mut nodes = Vec::new();
for node_id in &self.frontend_nodes {
let Some(node_bbox) = network_interface.node_bounding_box(node_id, breadcrumb_network_path) else {
log::error!("Could not get bbox for node: {:?}", node_id);
@ -1355,6 +1380,17 @@ impl<'a> MessageHandler<NodeGraphMessage, NodeGraphHandlerData<'a>> for NodeGrap
}
}
// Always send nodes with errors
for error in &self.node_graph_errors {
let Some((id, path)) = error.node_path.split_last() else {
log::error!("Could not get node path in error: {:?}", error);
continue;
};
if breadcrumb_network_path == path {
nodes.push(*id);
}
}
responses.add(FrontendMessage::UpdateVisibleNodes { nodes });
}
NodeGraphMessage::SendGraph => {
@ -1393,7 +1429,7 @@ impl<'a> MessageHandler<NodeGraphMessage, NodeGraphHandlerData<'a>> for NodeGrap
input,
});
responses.add(PropertiesPanelMessage::Refresh);
if !(network_interface.reference(&node_id, selection_network_path).is_none() || input_index == 0) && network_interface.connected_to_output(&node_id, selection_network_path) {
if network_interface.connected_to_output(&node_id, selection_network_path) {
responses.add(NodeGraphMessage::RunDocumentGraph);
}
}
@ -1475,6 +1511,9 @@ impl<'a> MessageHandler<NodeGraphMessage, NodeGraphHandlerData<'a>> for NodeGrap
responses.add(NodeGraphMessage::SendWires);
}
NodeGraphMessage::SetReference { node_id, reference } => {
network_interface.set_reference(&node_id, breadcrumb_network_path, reference);
}
NodeGraphMessage::SetToNodeOrLayer { node_id, is_layer } => {
if is_layer && !network_interface.is_eligible_to_be_layer(&node_id, selection_network_path) {
return;
@ -2154,6 +2193,69 @@ impl NodeGraphMessageHandler {
}
}
fn create_value_node(&mut self, network_interface: &mut NodeNetworkInterface, point: DVec2, breadcrumb_network_path: &[NodeId], responses: &mut VecDeque<Message>) {
let Some(disconnecting) = self.disconnecting.take() else {
log::error!("To connector must be initialized to create a value node");
return;
};
let Some(mut position) = self.wire_in_progress_to_connector.take() else {
log::error!("To connector must be initialized to create a value node");
return;
};
// Offset node insertion 3 grid spaces left and 1 grid space up so the center of the node is dragged
position = position - DVec2::new(GRID_SIZE as f64 * 3., GRID_SIZE as f64);
// Offset to account for division rounding error and place the selected node to the top left of the input
if position.x < 0. {
position.x = position.x - 1.;
}
if position.y < 0. {
position.y = position.y - 1.;
}
let Some(mut input) = network_interface.take_input(&disconnecting, breadcrumb_network_path) else {
return;
};
match &mut input {
NodeInput::Value { exposed, .. } => *exposed = false,
_ => return,
}
let drag_start = DragStart {
start_x: point.x,
start_y: point.y,
round_x: 0,
round_y: 0,
};
self.drag_start = Some((drag_start, false));
self.node_has_moved_in_drag = false;
self.update_node_graph_hints(responses);
let node_id = NodeId::new();
responses.add(NodeGraphMessage::CreateNodeFromContextMenu {
node_id: Some(node_id),
node_type: "Value".to_string(),
xy: Some(((position.x / GRID_SIZE as f64) as i32, (position.y / GRID_SIZE as f64) as i32)),
add_transaction: false,
});
responses.add(NodeGraphMessage::SetInput {
input_connector: InputConnector::node(node_id, 0),
input,
});
responses.add(NodeGraphMessage::CreateWire {
output_connector: OutputConnector::Node { node_id, output_index: 0 },
input_connector: disconnecting,
});
responses.add(NodeGraphMessage::SelectedNodesSet { nodes: vec![node_id] });
// Update the frontend that the node is disconnected
responses.add(NodeGraphMessage::RunDocumentGraph);
responses.add(NodeGraphMessage::SendGraph);
}
fn collect_wires(&mut self, network_interface: &mut NodeNetworkInterface, graph_wire_style: GraphWireStyle, breadcrumb_network_path: &[NodeId]) -> Vec<WirePathUpdate> {
let mut added_wires = network_interface
.node_graph_input_connectors(breadcrumb_network_path)

View file

@ -9,7 +9,7 @@ use choice::enum_choice;
use dyn_any::DynAny;
use glam::{DAffine2, DVec2, IVec2, UVec2};
use graph_craft::Type;
use graph_craft::document::value::TaggedValue;
use graph_craft::document::value::{TaggedValue, TaggedValueChoice};
use graph_craft::document::{DocumentNode, DocumentNodeImplementation, NodeId, NodeInput};
use graphene_std::animation::RealTimeMode;
use graphene_std::extract_xy::XY;
@ -89,7 +89,7 @@ pub fn start_widgets(parameter_widgets_info: ParameterWidgetsInfo) -> Vec<Widget
description,
input_type,
blank_assist,
exposeable,
exposable: exposeable,
} = parameter_widgets_info;
let Some(document_node) = document_node else {
@ -122,6 +122,7 @@ pub(crate) fn property_from_type(
unit: Option<&str>,
display_decimal_places: Option<u32>,
step: Option<f64>,
exposable: bool,
context: &mut NodePropertiesContext,
) -> Result<Vec<LayoutGroup>, Vec<LayoutGroup>> {
let (mut number_min, mut number_max, range) = number_options;
@ -144,7 +145,8 @@ pub(crate) fn property_from_type(
let min = |x: f64| number_min.unwrap_or(x);
let max = |x: f64| number_max.unwrap_or(x);
let default_info = ParameterWidgetsInfo::new(node_id, index, true, context);
let mut default_info = ParameterWidgetsInfo::new(node_id, index, true, context);
default_info.exposable = exposable;
let mut extra_widgets = vec![];
let widgets = match ty {
@ -252,8 +254,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, 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),
Type::Fn(_, out) => return property_from_type(node_id, index, out, number_options, unit, display_decimal_places, step, exposable, context),
Type::Future(out) => return property_from_type(node_id, index, out, number_options, unit, display_decimal_places, step, exposable, context),
};
extra_widgets.push(widgets);
@ -1044,7 +1046,7 @@ pub(crate) fn channel_mixer_properties(node_id: NodeId, context: &mut NodeProper
let is_monochrome = bool_widget(ParameterWidgetsInfo::new(node_id, MonochromeInput::INDEX, true, context), CheckboxInput::default());
let mut parameter_info = ParameterWidgetsInfo::new(node_id, OutputChannelInput::INDEX, true, context);
parameter_info.exposeable = false;
parameter_info.exposable = false;
let output_channel = enum_choice::<RedGreenBlue>().for_socket(parameter_info).property_row();
let document_node = match get_document_node(node_id, context) {
@ -1101,7 +1103,7 @@ pub(crate) fn selective_color_properties(node_id: NodeId, context: &mut NodeProp
use graphene_std::raster::selective_color::*;
let mut default_info = ParameterWidgetsInfo::new(node_id, ColorsInput::INDEX, true, context);
default_info.exposeable = false;
default_info.exposable = false;
let colors = enum_choice::<SelectiveColorChoice>().for_socket(default_info).property_row();
let document_node = match get_document_node(node_id, context) {
@ -1456,7 +1458,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, unit_suffix, display_decimal_places, step, context).is_ok())
.filter(|ty| property_from_type(node_id, input_index, ty, number_options, unit_suffix, display_decimal_places, step, true, context).is_ok())
.collect::<Vec<_>>();
input_types.sort_by_key(|ty| ty.type_name());
let input_type = input_types.first().cloned();
@ -1470,7 +1472,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, unit_suffix, display_decimal_places, step, context).unwrap_or_else(|value| value)
property_from_type(node_id, input_index, &input_type, number_options, unit_suffix, display_decimal_places, step, true, context).unwrap_or_else(|value| value)
});
layout.extend(row);
@ -1835,6 +1837,77 @@ pub fn math_properties(node_id: NodeId, context: &mut NodePropertiesContext) ->
]
}
pub fn value_properties(node_id: NodeId, context: &mut NodePropertiesContext) -> Vec<LayoutGroup> {
let Some(document_node) = context.network_interface.document_node(&node_id, context.selection_network_path) else {
log::warn!("Value properties failed to be built because its document node is invalid.");
return vec![];
};
let Some(input) = document_node.inputs.get(0) else {
log::warn!("Secondary value input could not be found on value properties");
return vec![];
};
let mut select_value_widgets = Vec::new();
select_value_widgets.push(TextLabel::new("Value type: ").tooltip("Select the type the value node should output").widget_holder());
let Some(input_value) = input.as_non_exposed_value() else {
log::error!("Primary value node input should be a hidden value input");
return Vec::new();
};
let input_type = input_value.ty();
let Some(choice) = TaggedValueChoice::from_tagged_value(input_value) else {
log::error!("Tagged value in value node should always have a choice. input: {:?}", input);
return Vec::new();
};
// let committer = || {|_| {
// let messages = vec![
// DocumentMessage::AddTransaction.into(),
// NodeGraphMessage::RunDocumentGraph.into(),
// ];
// Message::Batched(messages.into_boxed_slice()).into()
// };
let updater = || {
move |v: &TaggedValueChoice| {
let value = v.to_tagged_value();
let messages = vec![NodeGraphMessage::SetInputValue { node_id, input_index: 0, value }.into(), NodeGraphMessage::SendGraph.into()];
Message::Batched(messages.into_boxed_slice()).into()
}
};
let value_dropdown = enum_choice::<TaggedValueChoice>().dropdown_menu(choice, updater, || commit_value);
select_value_widgets.extend_from_slice(&[Separator::new(SeparatorType::Unrelated).widget_holder(), value_dropdown]);
let mut type_widgets = match property_from_type(node_id, 0, &input_type, (None, None, None), None, None, None, false, context) {
Ok(type_widgets) => type_widgets,
Err(type_widgets) => type_widgets,
};
if type_widgets.len() <= 0 {
log::error!("Could not generate type widgets for value node");
return Vec::new();
}
let LayoutGroup::Row { widgets: mut type_widgets } = type_widgets.remove(0) else {
log::error!("Could not get autogenerated widgets for value node");
return Vec::new();
};
if type_widgets.len() <= 2 {
log::error!("Could not generate type widgets for value node");
return Vec::new();
}
//Remove the name and blank assist
type_widgets.remove(0);
type_widgets.remove(0);
vec![LayoutGroup::Row { widgets: select_value_widgets }, LayoutGroup::Row { widgets: type_widgets }]
}
pub struct ParameterWidgetsInfo<'a> {
document_node: Option<&'a DocumentNode>,
node_id: NodeId,
@ -1843,7 +1916,7 @@ pub struct ParameterWidgetsInfo<'a> {
description: String,
input_type: FrontendGraphDataType,
blank_assist: bool,
exposeable: bool,
exposable: bool,
}
impl<'a> ParameterWidgetsInfo<'a> {
@ -1860,7 +1933,7 @@ impl<'a> ParameterWidgetsInfo<'a> {
description,
input_type,
blank_assist,
exposeable: true,
exposable: true,
}
}
}
@ -1916,7 +1989,7 @@ pub mod choice {
todo!()
}
fn dropdown_menu<U, C>(&self, current: E, updater_factory: impl Fn() -> U, committer_factory: impl Fn() -> C) -> WidgetHolder
pub fn dropdown_menu<U, C>(&self, current: E, updater_factory: impl Fn() -> U, committer_factory: impl Fn() -> C) -> WidgetHolder
where
U: Fn(&E) -> Message + 'static + Send + Sync,
C: Fn(&()) -> Message + 'static + Send + Sync,

View file

@ -480,6 +480,24 @@ impl NodeNetworkInterface {
}
}
pub fn take_input(&mut self, input_connector: &InputConnector, network_path: &[NodeId]) -> Option<NodeInput> {
let Some(network) = self.network_mut(network_path) else {
log::error!("Could not get network in input_from_connector");
return None;
};
let input = match input_connector {
InputConnector::Node { node_id, input_index } => {
let Some(node) = network.nodes.get_mut(node_id) else {
log::error!("Could not get node {node_id} in input_from_connector");
return None;
};
node.inputs.get_mut(*input_index)
}
InputConnector::Export(export_index) => network.exports.get_mut(*export_index),
};
input.map(|input| std::mem::replace(input, NodeInput::value(TaggedValue::None, true)))
}
/// Try and get the [`Type`] for any [`InputConnector`] based on the `self.resolved_types`.
fn node_type_from_compiled(&mut self, input_connector: &InputConnector, network_path: &[NodeId]) -> Option<(Type, TypeSource)> {
let (node_id, input_index) = match *input_connector {
@ -4211,13 +4229,23 @@ impl NodeNetworkInterface {
// Side effects
match (&old_input, &new_input) {
// If a node input is exposed or hidden reload the click targets and update the bounding box for all nodes
(NodeInput::Value { exposed: new_exposed, .. }, NodeInput::Value { exposed: old_exposed, .. }) => {
(NodeInput::Value { exposed: new_exposed, .. }, NodeInput::Value { exposed: old_exposed, tagged_value }) => {
if let InputConnector::Node { node_id, .. } = input_connector {
if new_exposed != old_exposed {
self.unload_upstream_node_click_targets(vec![*node_id], network_path);
self.unload_all_nodes_bounding_box(network_path);
}
}
// Update the name of the value node
if let InputConnector::Node { node_id, .. } = input_connector {
let Some(reference) = self.reference(node_id, network_path) else {
log::error!("Could not get reference for {:?}", node_id);
return;
};
if reference.as_deref() == Some("Value") {
self.set_display_name(node_id, format!("{:?} Value", tagged_value.ty().nested_type()), network_path);
}
}
}
(_, NodeInput::Node { node_id: upstream_node_id, .. }) => {
// Load structure if the change is to the document network and to the first or second

View file

@ -140,7 +140,7 @@ const REPLACEMENTS: &[(&str, &str)] = &[
("graphene_std::raster::NoisePatternNode", "graphene_raster_nodes::std_nodes::NoisePatternNode"),
("graphene_std::raster::MandelbrotNode", "graphene_raster_nodes::std_nodes::MandelbrotNode"),
// text
("graphene_core::text::TextGeneratorNode", "graphene_core::text::TextNode"),
("graphene_core::text::TextGeneratorNode", "graphene_std::text::TextNode"),
// transform
("graphene_core::transform::SetTransformNode", "graphene_core::transform_nodes::ReplaceTransformNode"),
("graphene_core::transform::ReplaceTransformNode", "graphene_core::transform_nodes::ReplaceTransformNode"),
@ -159,7 +159,7 @@ const REPLACEMENTS: &[(&str, &str)] = &[
("graphene_core::vector::generator_nodes::StarGenerator", "graphene_core::vector::generator_nodes::StarNode"),
("graphene_std::executor::BlendGpuImageNode", "graphene_std::gpu_nodes::BlendGpuImageNode"),
("graphene_std::raster::SampleNode", "graphene_std::raster::SampleImageNode"),
("graphene_core::transform::CullNode", "graphene_core::ops::IdentityNode"),
("graphene_core::transform::CullNode", "graphene_std::any::IdentityNode"),
("graphene_std::raster::MaskImageNode", "graphene_std::raster::MaskNode"),
("graphene_core::vector::FlattenVectorElementsNode", "graphene_core::vector::FlattenPathNode"),
("graphene_std::vector::BooleanOperationNode", "graphene_path_bool::BooleanOperationNode"),

View file

@ -1,13 +1,6 @@
use crate::Node;
use std::marker::PhantomData;
// TODO: Rename to "Passthrough"
/// Passes-through the input value without changing it. This is useful for rerouting wires for organization purposes.
#[node_macro::node(skip_impl)]
fn identity<'i, T: 'i + Send>(value: T) -> T {
value
}
// Type
// TODO: Document this
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]

View file

@ -460,7 +460,7 @@ pub enum DocumentNodeImplementation {
impl Default for DocumentNodeImplementation {
fn default() -> Self {
Self::ProtoNode(ProtoNodeIdentifier::new("graphene_core::ops::IdentityNode"))
Self::ProtoNode(ProtoNodeIdentifier::new("graphene_std::any::IdentityNode"))
}
}
@ -920,7 +920,7 @@ impl NodeNetwork {
return;
};
// If the node is hidden, replace it with an identity node
let identity_node = DocumentNodeImplementation::ProtoNode("graphene_core::ops::IdentityNode".into());
let identity_node = DocumentNodeImplementation::ProtoNode("graphene_std::any::IdentityNode".into());
if !node.visible && node.implementation != identity_node {
node.implementation = identity_node;
@ -1096,7 +1096,7 @@ impl NodeNetwork {
fn remove_id_node(&mut self, id: NodeId) -> Result<(), String> {
let node = self.nodes.get(&id).ok_or_else(|| format!("Node with id {id} does not exist"))?.clone();
if let DocumentNodeImplementation::ProtoNode(ident) = &node.implementation {
if ident.name == "graphene_core::ops::IdentityNode" {
if ident.name == "graphene_std::any::IdentityNode" {
assert_eq!(node.inputs.len(), 1, "Id node has more than one input");
if let NodeInput::Node { node_id, output_index, .. } = node.inputs[0] {
let node_input_output_index = output_index;
@ -1143,13 +1143,13 @@ impl NodeNetwork {
Ok(())
}
/// Strips out any [`graphene_core::ops::IdentityNode`]s that are unnecessary.
/// Strips out any [`graphene_std::any::IdentityNode`]s that are unnecessary.
pub fn remove_redundant_id_nodes(&mut self) {
let id_nodes = self
.nodes
.iter()
.filter(|(_, node)| {
matches!(&node.implementation, DocumentNodeImplementation::ProtoNode(ident) if ident == &ProtoNodeIdentifier::new("graphene_core::ops::IdentityNode"))
matches!(&node.implementation, DocumentNodeImplementation::ProtoNode(ident) if ident == &ProtoNodeIdentifier::new("graphene_std::any::IdentityNode"))
&& node.inputs.len() == 1
&& matches!(node.inputs[0], NodeInput::Node { .. })
})
@ -1338,7 +1338,7 @@ mod test {
fn extract_node() {
let id_node = DocumentNode {
inputs: vec![],
implementation: DocumentNodeImplementation::ProtoNode("graphene_core::ops::IdentityNode".into()),
implementation: DocumentNodeImplementation::ProtoNode("graphene_std::any::IdentityNode".into()),
..Default::default()
};
// TODO: Extend test cases to test nested network
@ -1540,7 +1540,7 @@ mod test {
NodeId(1),
DocumentNode {
inputs: vec![NodeInput::network(concrete!(u32), 0)],
implementation: DocumentNodeImplementation::ProtoNode(ProtoNodeIdentifier::new("graphene_core::ops::IdentityNode")),
implementation: DocumentNodeImplementation::ProtoNode(ProtoNodeIdentifier::new("graphene_std::any::IdentityNode")),
..Default::default()
},
),
@ -1548,7 +1548,7 @@ mod test {
NodeId(2),
DocumentNode {
inputs: vec![NodeInput::network(concrete!(u32), 1)],
implementation: DocumentNodeImplementation::ProtoNode(ProtoNodeIdentifier::new("graphene_core::ops::IdentityNode")),
implementation: DocumentNodeImplementation::ProtoNode(ProtoNodeIdentifier::new("graphene_std::any::IdentityNode")),
..Default::default()
},
),
@ -1575,7 +1575,7 @@ mod test {
NodeId(2),
DocumentNode {
inputs: vec![result_node_input],
implementation: DocumentNodeImplementation::ProtoNode(ProtoNodeIdentifier::new("graphene_core::ops::IdentityNode")),
implementation: DocumentNodeImplementation::ProtoNode(ProtoNodeIdentifier::new("graphene_std::any::IdentityNode")),
..Default::default()
},
),

View file

@ -8,11 +8,13 @@ use graphene_application_io::SurfaceFrame;
use graphene_brush::brush_cache::BrushCache;
use graphene_brush::brush_stroke::BrushStroke;
use graphene_core::raster_types::CPU;
use graphene_core::registry::{ChoiceTypeStatic, ChoiceWidgetHint, VariantMetadata};
use graphene_core::transform::ReferencePoint;
use graphene_core::uuid::NodeId;
use graphene_core::vector::style::Fill;
use graphene_core::{Color, MemoHash, Node, Type};
use graphene_core::{AsU32, Color, MemoHash, Node, Type};
use graphene_svg_renderer::RenderMetadata;
use std::borrow::Cow;
use std::fmt::Display;
use std::hash::Hash;
use std::marker::PhantomData;
@ -36,6 +38,65 @@ macro_rules! tagged_value {
EditorApi(Arc<WasmEditorApi>)
}
#[derive(Clone, Copy, Debug)]
#[repr(u32)]
pub enum TaggedValueChoice {
None,
$($identifier,)*
}
impl TaggedValueChoice {
pub fn to_tagged_value(&self) -> TaggedValue {
match self {
TaggedValueChoice::None => TaggedValue::None,
$(TaggedValueChoice::$identifier => TaggedValue::$identifier(Default::default()),)*
}
}
pub fn from_tagged_value(value: &TaggedValue) -> Option<Self> {
match value {
TaggedValue::None => Some(TaggedValueChoice::None),
$( TaggedValue::$identifier(_) => Some(TaggedValueChoice::$identifier), )*
_ => None
}
}
}
impl ChoiceTypeStatic for TaggedValueChoice {
const WIDGET_HINT: ChoiceWidgetHint = ChoiceWidgetHint::Dropdown; // or your preferred hint
const DESCRIPTION: Option<&'static str> = Some("Select a value");
fn list() -> &'static [&'static [(Self, VariantMetadata)]] {
const COUNT: usize = 0 $( + one!($identifier) )*;
// Define static array of (choice, metadata) tuples
static VALUES: [(TaggedValueChoice, VariantMetadata); 1 + COUNT] = [
(TaggedValueChoice::None, VariantMetadata {
name: Cow::Borrowed(stringify!(None)),
label: Cow::Borrowed(stringify!(None)),
docstring: None,
icon: None,
}),
$(
(TaggedValueChoice::$identifier, VariantMetadata {
name: Cow::Borrowed(stringify!($identifier)),
label: Cow::Borrowed(stringify!($identifier)),
docstring: None,
icon: None,
}),
)*
];
// Static reference to the slice of VALUES
static LIST: [&'static [(TaggedValueChoice, VariantMetadata)]; 1] = [
&VALUES,
];
&LIST
}
}
// We must manually implement hashing because some values are floats and so do not reproducibly hash (see FakeHash below)
#[allow(clippy::derived_hash_with_manual_eq)]
impl Hash for TaggedValue {
@ -155,6 +216,12 @@ macro_rules! tagged_value {
};
}
macro_rules! one {
($anything:tt) => {
1
};
}
tagged_value! {
// ===============
// PRIMITIVE TYPES
@ -387,6 +454,12 @@ impl Display for TaggedValue {
}
}
impl AsU32 for TaggedValueChoice {
fn as_u32(&self) -> u32 {
*self as u32
}
}
pub struct UpcastNode {
value: MemoHash<TaggedValue>,
}

View file

@ -143,7 +143,7 @@ pub struct ProtoNode {
impl Default for ProtoNode {
fn default() -> Self {
Self {
identifier: ProtoNodeIdentifier::new("graphene_core::ops::IdentityNode"),
identifier: ProtoNodeIdentifier::new("graphene_std::any::IdentityNode"),
construction_args: ConstructionArgs::Value(value::TaggedValue::U32(0).into()),
input: ProtoNodeInput::None,
original_location: OriginalLocation::default(),

View file

@ -46,3 +46,20 @@ pub fn input_node<O: StaticType>(n: SharedNodeContainer) -> DowncastBothNode<(),
pub fn downcast_node<I: StaticType, O: StaticType>(n: SharedNodeContainer) -> DowncastBothNode<I, O> {
DowncastBothNode::new(n)
}
pub struct IdentityNode {
value: SharedNodeContainer,
}
impl<'i> Node<'i, Any<'i>> for IdentityNode {
type Output = DynFuture<'i, Any<'i>>;
fn eval(&'i self, input: Any<'i>) -> Self::Output {
Box::pin(async move { self.value.eval(input).await })
}
}
impl IdentityNode {
pub const fn new(value: SharedNodeContainer) -> Self {
IdentityNode { value }
}
}

View file

@ -20,7 +20,7 @@ mod tests {
NodeId(0),
DocumentNode {
inputs: vec![NodeInput::network(concrete!(u32), 0)],
implementation: DocumentNodeImplementation::ProtoNode(ProtoNodeIdentifier::new("graphene_core::ops::IdentityNode")),
implementation: DocumentNodeImplementation::ProtoNode(ProtoNodeIdentifier::new("graphene_std::any::IdentityNode")),
..Default::default()
},
),

View file

@ -14,7 +14,7 @@ use graphene_std::Context;
use graphene_std::GraphicElement;
#[cfg(feature = "gpu")]
use graphene_std::any::DowncastBothNode;
use graphene_std::any::{ComposeTypeErased, DynAnyNode, IntoTypeErasedNode};
use graphene_std::any::{ComposeTypeErased, DynAnyNode, IdentityNode, IntoTypeErasedNode};
use graphene_std::application_io::{ImageTexture, SurfaceFrame};
#[cfg(feature = "gpu")]
use graphene_std::wasm_application_io::{WasmEditorApi, WasmSurfaceHandle};
@ -113,6 +113,16 @@ fn node_registry() -> HashMap<ProtoNodeIdentifier, HashMap<NodeIOTypes, NodeCons
vec![Type::Fn(Box::new(generic!(T)), Box::new(generic!(V))), Type::Fn(Box::new(generic!(V)), Box::new(generic!(U)))],
),
),
(
ProtoNodeIdentifier::new("graphene_std::any::IdentityNode"),
|args| {
Box::pin(async move {
let node = IdentityNode::new(args[0].clone());
node.into_type_erased()
})
},
NodeIOTypes::new(generic!(T), generic!(U), vec![Type::Fn(Box::new(concrete!(Context)), Box::new(generic!(U)))]),
),
#[cfg(feature = "gpu")]
async_node!(graphene_core::memo::MemoNode<_, _>, input: Context, fn_params: [Context => WgpuSurface]),
#[cfg(feature = "gpu")]

View file

@ -68,7 +68,7 @@ pub fn wrap_network_in_scope(mut network: NodeNetwork, editor_api: Arc<WasmEdito
inner_network,
render_node,
DocumentNode {
implementation: DocumentNodeImplementation::proto("graphene_core::ops::IdentityNode"),
implementation: DocumentNodeImplementation::proto("graphene_std::any::IdentityNode"),
inputs: vec![NodeInput::value(TaggedValue::EditorApi(editor_api), false)],
..Default::default()
},

View file

@ -49,7 +49,7 @@ pub fn generate_node_substitutions() -> HashMap<String, DocumentNode> {
let input_count = inputs.len();
let network_inputs = (0..input_count).map(|i| NodeInput::node(NodeId(i as u64), 0)).collect();
let identity_node = ProtoNodeIdentifier::new("graphene_core::ops::IdentityNode");
let identity_node = ProtoNodeIdentifier::new("graphene_std::any::IdentityNode");
let into_node_registry = &interpreted_executor::node_registry::NODE_REGISTRY;