Skip to content

Commit a512730

Browse files
committed
Prototype document network level into node insertion
1 parent 41288d7 commit a512730

File tree

2 files changed

+113
-2
lines changed

2 files changed

+113
-2
lines changed

editor/src/messages/portfolio/document/node_graph/document_node_definitions.rs

+107-2
Original file line numberDiff line numberDiff line change
@@ -2689,13 +2689,19 @@ fn static_nodes() -> Vec<DocumentNodeDefinition> {
26892689
let Some(implementations) = &node_registry.get(&id) else { continue };
26902690
let valid_inputs: HashSet<_> = implementations.iter().map(|(_, node_io)| node_io.call_argument.clone()).collect();
26912691
let first_node_io = implementations.first().map(|(_, node_io)| node_io).unwrap_or(const { &NodeIOTypes::empty() });
2692+
let mut node_io_types = vec![HashSet::new(); fields.len()];
2693+
for (_, node_io) in implementations.iter() {
2694+
for (i, ty) in node_io.inputs.iter().enumerate() {
2695+
node_io_types[i].insert(ty.clone());
2696+
}
2697+
}
26922698
let mut input_type = &first_node_io.call_argument;
26932699
if valid_inputs.len() > 1 {
26942700
input_type = &const { generic!(D) };
26952701
}
26962702
let output_type = &first_node_io.return_value;
26972703

2698-
let inputs = fields
2704+
let inputs: Vec<_> = fields
26992705
.iter()
27002706
.zip(first_node_io.inputs.iter())
27012707
.enumerate()
@@ -2715,14 +2721,89 @@ fn static_nodes() -> Vec<DocumentNodeDefinition> {
27152721
NodeInput::value(TaggedValue::None, true)
27162722
})
27172723
.collect();
2724+
let input_count = inputs.len();
2725+
let network_inputs = (0..input_count).map(|i| NodeInput::node(NodeId(i as u64), 0)).collect();
2726+
let identity_node = ProtoNodeIdentifier::new("graphene_core::ops::IdentityNode");
2727+
let into_node_registry = &interpreted_executor::node_registry::NODE_REGISTRY;
2728+
let mut nodes: HashMap<_, _, _> = node_io_types
2729+
.iter()
2730+
.enumerate()
2731+
.map(|(i, inputs)| {
2732+
(
2733+
NodeId(i as u64),
2734+
match inputs.len() {
2735+
1 => {
2736+
let input = inputs.iter().next().unwrap();
2737+
let input_ty = input.nested_type();
2738+
let into_node_identifier = ProtoNodeIdentifier {
2739+
name: format!("graphene_core::ops::IntoNode<{}>", input_ty.clone()).into(),
2740+
};
2741+
let proto_node = if into_node_registry.iter().any(|(ident, _)| {
2742+
let ident = ident.name.as_ref();
2743+
// log::debug!("checking: {} against {}", ident, into_node_identifier.name.as_ref());
2744+
ident == into_node_identifier.name.as_ref()
2745+
}) {
2746+
// log::debug!("placing {}", into_node_identifier.name.as_ref());
2747+
into_node_identifier
2748+
} else {
2749+
identity_node.clone()
2750+
};
2751+
DocumentNode {
2752+
inputs: vec![NodeInput::network(input.clone(), i)],
2753+
// manual_composition: Some(fn_input.clone()),
2754+
implementation: DocumentNodeImplementation::ProtoNode(proto_node),
2755+
visible: true,
2756+
..Default::default()
2757+
}
2758+
}
2759+
_ => DocumentNode {
2760+
inputs: vec![NodeInput::network(generic!(X), i)],
2761+
implementation: DocumentNodeImplementation::ProtoNode(identity_node.clone()),
2762+
visible: true,
2763+
..Default::default()
2764+
},
2765+
},
2766+
)
2767+
})
2768+
.collect();
2769+
2770+
let document_node = DocumentNode {
2771+
inputs: network_inputs,
2772+
manual_composition: Some(input_type.clone()),
2773+
implementation: DocumentNodeImplementation::ProtoNode(id.clone().into()),
2774+
visible: true,
2775+
skip_deduplication: false,
2776+
..Default::default()
2777+
};
2778+
let mut node_names: HashMap<NodeId, String> = nodes
2779+
.iter()
2780+
.map(|(id, node)| (*id, node.implementation.get_proto_node().unwrap().name.rsplit_once("::").unwrap().1.to_string()))
2781+
.collect();
2782+
nodes.insert(NodeId(input_count as u64), document_node);
2783+
node_names.insert(NodeId(input_count as u64), display_name.to_string());
2784+
let node_type_metadata = |id: NodeId| {
2785+
NodeTypePersistentMetadata::Node(NodePersistentMetadata::new(NodePosition::Absolute(if id.0 == input_count as u64 {
2786+
IVec2::default()
2787+
} else {
2788+
IVec2 { x: -10, y: id.0 as i32 }
2789+
})))
2790+
};
27182791

27192792
let node = DocumentNodeDefinition {
27202793
identifier: display_name,
27212794
node_template: NodeTemplate {
27222795
document_node: DocumentNode {
27232796
inputs,
27242797
manual_composition: Some(input_type.clone()),
2725-
implementation: DocumentNodeImplementation::ProtoNode(id.clone().into()),
2798+
implementation: DocumentNodeImplementation::Network(NodeNetwork {
2799+
exports: vec![NodeInput::Node {
2800+
node_id: NodeId(input_count as u64),
2801+
output_index: 0,
2802+
lambda: false,
2803+
}],
2804+
nodes,
2805+
scope_injections: Default::default(),
2806+
}),
27262807
visible: true,
27272808
skip_deduplication: false,
27282809
..Default::default()
@@ -2742,6 +2823,30 @@ fn static_nodes() -> Vec<DocumentNodeDefinition> {
27422823
has_primary_output: true,
27432824
locked: false,
27442825

2826+
network_metadata: Some(NodeNetworkMetadata {
2827+
persistent_metadata: NodeNetworkPersistentMetadata {
2828+
node_metadata: node_names
2829+
.into_iter()
2830+
.map(|(id, display_name)| {
2831+
let node_type_metadata = node_type_metadata(id);
2832+
(
2833+
id,
2834+
DocumentNodeMetadata {
2835+
persistent_metadata: DocumentNodePersistentMetadata {
2836+
display_name,
2837+
node_type_metadata,
2838+
..Default::default()
2839+
},
2840+
..Default::default()
2841+
},
2842+
)
2843+
})
2844+
.collect(),
2845+
..Default::default()
2846+
},
2847+
..Default::default()
2848+
}),
2849+
27452850
..Default::default()
27462851
},
27472852
},

editor/src/messages/portfolio/document/utility_types/network_interface.rs

+6
Original file line numberDiff line numberDiff line change
@@ -6438,6 +6438,12 @@ pub struct NodePersistentMetadata {
64386438
position: NodePosition,
64396439
}
64406440

6441+
impl NodePersistentMetadata {
6442+
pub fn new(position: NodePosition) -> Self {
6443+
Self { position }
6444+
}
6445+
}
6446+
64416447
/// A layer can either be position as Absolute or in a Stack
64426448
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
64436449
pub enum LayerPosition {

0 commit comments

Comments
 (0)