@@ -150,20 +150,25 @@ impl<'a> MessageHandler<NodeGraphMessage, NodeGraphHandlerData<'a>> for NodeGrap
150150 continue ;
151151 } ;
152152
153- let node = document_network. nodes . get ( & node_id) . expect ( "node should always exist" ) ;
153+ let Some ( node) = document_network. nodes . get ( & node_id) else {
154+ continue ;
155+ } ;
154156 let child_id = node. inputs . get ( 1 ) . and_then ( |input| if let NodeInput :: Node { node_id, .. } = input { Some ( node_id) } else { None } ) ;
155157 let Some ( child_id) = child_id else {
156158 continue ;
157159 } ;
158160
159161 let outward_links = document_network. collect_outwards_links ( ) ;
160- for ( _node, upstream_id) in document_network. upstream_flow_back_from_nodes ( vec ! [ * child_id] , graph_craft:: document:: FlowType :: UpstreamFlow ) {
162+
163+ for ( _, upstream_id) in document_network. upstream_flow_back_from_nodes ( vec ! [ * child_id] , graph_craft:: document:: FlowType :: UpstreamFlow ) {
161164 // TODO: move into a document_network function .is_sole_dependent. This function does a downstream traversal starting from the current node,
162- // and only traverses for nodes that are not in the delete_nodes set. If all downstream nodes converge to some node in the delete_nodes set,
163- // then it is a sole dependent. If the output node is eventually reached, then it is not a sole dependent. This means disconnected branches
164- // that do not feed into the delete_nodes set or the output node will be deleted.
165+ // TODO: and only traverses for nodes that are not in the delete_nodes set. If all downstream nodes converge to some node in the delete_nodes set,
166+ // TODO: then it is a sole dependent. If the output node is eventually reached, then it is not a sole dependent. This means disconnected branches
167+ // TODO: that do not feed into the delete_nodes set or the output node will be deleted.
168+
165169 let mut stack = vec ! [ upstream_id] ;
166170 let mut can_delete = true ;
171+
167172 // TODO: Add iteration limit to force break in case of infinite while loop
168173 while let Some ( current_node) = stack. pop ( ) {
169174 if let Some ( downstream_nodes) = outward_links. get ( & current_node) {
@@ -176,7 +181,9 @@ impl<'a> MessageHandler<NodeGraphMessage, NodeGraphHandlerData<'a>> for NodeGrap
176181 // Continue traversing over the downstream sibling, which happens if the current node is a sibling to a node in node_ids
177182 else {
178183 for deleted_node_id in & node_ids {
179- let output_node: & DocumentNode = document_network. nodes . get ( & deleted_node_id) . expect ( "node should always exist" ) ;
184+ let Some ( output_node) = document_network. nodes . get ( & deleted_node_id) else {
185+ continue ;
186+ } ;
180187 let Some ( input) = output_node. inputs . get ( 0 ) else {
181188 continue ;
182189 } ;
@@ -191,26 +198,24 @@ impl<'a> MessageHandler<NodeGraphMessage, NodeGraphHandlerData<'a>> for NodeGrap
191198 }
192199 }
193200 }
201+
194202 if can_delete {
195203 delete_nodes. insert ( upstream_id) ;
196204 }
197205 }
198206 }
207+
199208 for delete_node_id in delete_nodes {
200- let delete_node = document_network. nodes . get ( & delete_node_id) . expect ( "node should always exist" ) ;
209+ let Some ( delete_node) = document_network. nodes . get ( & delete_node_id) else {
210+ continue ;
211+ } ;
212+
201213 if delete_node. is_layer {
202214 // Delete node from document metadata
203215 let layer_node = LayerNodeIdentifier :: new ( delete_node_id, document_network) ;
204216 layer_node. delete ( document_metadata) ;
205- // Shift the position of the upstream sibling nodes for a deleted layer node. This ends up causing more positioning issues than it solves.
206- // let shift = IVec2::new(0, -3);
207- // let node_ids = document_network.upstream_flow_back_from_nodes(vec![delete_node_id], false).map(|(_, id)| id).collect::<Vec<_>>();
208-
209- // for node_id in node_ids {
210- // let Some(node) = document_network.nodes.get_mut(&node_id) else { continue };
211- // node.metadata.position += shift;
212- // }
213217 }
218+
214219 self . remove_node ( document_network, selected_nodes, delete_node_id, responses, reconnect) ;
215220 }
216221
@@ -220,20 +225,18 @@ impl<'a> MessageHandler<NodeGraphMessage, NodeGraphHandlerData<'a>> for NodeGrap
220225 responses. add ( NodeGraphMessage :: RunDocumentGraph ) ;
221226 }
222227 }
223- // No need to call this since the metadata is already updated
224- // load_network_structure(document_network, document_metadata, selected_nodes, collapsed);
225- }
226228
227- // Deletes selected_nodes. If reconnect is true, then all children nodes (secondary input) of the selected nodes are deleted and the siblings(primary input/output) are reconnected.
228- // If reconnect is false, then only the selected nodes are deleted and not reconnected.
229+ // There is no need to call `load_network_structure()` since the metadata is already updated
230+ }
231+ // Deletes selected_nodes. If `reconnect` is true, then all children nodes (secondary input) of the selected nodes are deleted and the siblings (primary input/output) are reconnected.
232+ // If `reconnect` is false, then only the selected nodes are deleted and not reconnected.
229233 NodeGraphMessage :: DeleteSelectedNodes { reconnect } => {
230234 responses. add ( DocumentMessage :: StartTransaction ) ;
231235 responses. add ( NodeGraphMessage :: DeleteNodes {
232236 node_ids : selected_nodes. selected_nodes ( ) . copied ( ) . collect ( ) ,
233237 reconnect,
234238 } ) ;
235239 }
236-
237240 NodeGraphMessage :: DisconnectNodes { node_id, input_index } => {
238241 let Some ( network) = document_network. nested_network ( & self . network ) else {
239242 warn ! ( "No network" ) ;
@@ -247,14 +250,16 @@ impl<'a> MessageHandler<NodeGraphMessage, NodeGraphHandlerData<'a>> for NodeGrap
247250 warn ! ( "Node {} not in library" , node. name) ;
248251 return ;
249252 } ;
250-
251253 let Some ( ( input_index, existing_input) ) = node. inputs . iter ( ) . enumerate ( ) . filter ( |( _, input) | input. is_exposed ( ) ) . nth ( input_index) else {
252254 return ;
253255 } ;
256+
254257 let mut input = node_type. inputs [ input_index] . default . clone ( ) ;
255258 if let NodeInput :: Value { exposed, .. } = & mut input {
256259 * exposed = existing_input. is_exposed ( ) ;
257260 }
261+
262+ responses. add ( DocumentMessage :: StartTransaction ) ;
258263 responses. add ( NodeGraphMessage :: SetNodeInput { node_id, input_index, input } ) ;
259264
260265 if network. connected_to_output ( node_id) {
@@ -343,7 +348,6 @@ impl<'a> MessageHandler<NodeGraphMessage, NodeGraphHandlerData<'a>> for NodeGrap
343348 }
344349
345350 responses. add ( NodeGraphMessage :: SetNodeInput { node_id, input_index, input } ) ;
346-
347351 responses. add ( NodeGraphMessage :: EnforceLayerHasNoMultiParams { node_id } ) ;
348352 responses. add ( PropertiesPanelMessage :: Refresh ) ;
349353 responses. add ( NodeGraphMessage :: SendGraph ) ;
@@ -382,6 +386,7 @@ impl<'a> MessageHandler<NodeGraphMessage, NodeGraphHandlerData<'a>> for NodeGrap
382386 error ! ( "Failed to find input index {insert_node_input_index} on node {insert_node_id:#?}" ) ;
383387 return ;
384388 } ;
389+
385390 responses. add ( DocumentMessage :: StartTransaction ) ;
386391
387392 let post_input = NodeInput :: node ( insert_node_id, insert_node_output_index) ;
@@ -414,6 +419,7 @@ impl<'a> MessageHandler<NodeGraphMessage, NodeGraphHandlerData<'a>> for NodeGrap
414419 node. metadata . position += IVec2 :: new ( displacement_x, displacement_y)
415420 }
416421 }
422+
417423 // Since document structure doesn't change, just update the nodes
418424 if graph_view_overlay_open {
419425 let links = Self :: collect_links ( network) ;
@@ -662,19 +668,20 @@ impl<'a> MessageHandler<NodeGraphMessage, NodeGraphHandlerData<'a>> for NodeGrap
662668 self . update_selection_action_buttons ( document_network, document_metadata, selected_nodes, responses) ;
663669 }
664670 NodeGraphMessage :: ToggleSelectedLayers => {
665- if let Some ( network) = document_network. nested_network_mut ( & self . network ) {
666- for node_id in selected_nodes. selected_nodes ( ) {
667- if let Some ( node) = network. nodes . get_mut ( & node_id) {
668- if node. has_primary_output {
669- responses. add ( NodeGraphMessage :: SetToNodeOrLayer {
670- node_id : * node_id,
671- is_layer : !node. is_layer ,
672- } ) ;
673- }
674- if network. connected_to_output ( * node_id) {
675- responses. add ( NodeGraphMessage :: RunDocumentGraph ) ;
676- }
677- }
671+ let Some ( network) = document_network. nested_network_mut ( & self . network ) else { return } ;
672+
673+ for node_id in selected_nodes. selected_nodes ( ) {
674+ let Some ( node) = network. nodes . get_mut ( & node_id) else { continue } ;
675+
676+ if node. has_primary_output {
677+ responses. add ( NodeGraphMessage :: SetToNodeOrLayer {
678+ node_id : * node_id,
679+ is_layer : !node. is_layer ,
680+ } ) ;
681+ }
682+
683+ if network. connected_to_output ( * node_id) {
684+ responses. add ( NodeGraphMessage :: RunDocumentGraph ) ;
678685 }
679686 }
680687 }
@@ -683,13 +690,13 @@ impl<'a> MessageHandler<NodeGraphMessage, NodeGraphHandlerData<'a>> for NodeGrap
683690 return ;
684691 }
685692
686- if let Some ( network) = document_network. nested_network_mut ( & self . network ) {
687- if let Some ( node) = network. nodes . get_mut ( & node_id) {
688- node. is_layer = is_layer;
689- }
690- responses. add ( NodeGraphMessage :: RunDocumentGraph ) ;
691- responses. add ( DocumentMessage :: DocumentStructureChanged ) ;
693+ let Some ( network) = document_network. nested_network_mut ( & self . network ) else { return } ;
694+
695+ if let Some ( node) = network. nodes . get_mut ( & node_id) {
696+ node. is_layer = is_layer;
692697 }
698+ responses. add ( NodeGraphMessage :: RunDocumentGraph ) ;
699+ responses. add ( DocumentMessage :: DocumentStructureChanged ) ;
693700 }
694701 NodeGraphMessage :: SetName { node_id, name } => {
695702 responses. add ( DocumentMessage :: StartTransaction ) ;
@@ -1136,27 +1143,17 @@ impl NodeGraphMessageHandler {
11361143 }
11371144
11381145 pub fn eligible_to_be_layer ( & self , document_network : & NodeNetwork , node_id : NodeId ) -> bool {
1139- let Some ( network) = document_network. nested_network ( & self . network ) else {
1140- return false ;
1141- } ;
1142- let Some ( node) = network. nodes . get ( & node_id) else {
1143- return false ;
1144- } ;
1146+ let Some ( network) = document_network. nested_network ( & self . network ) else { return false } ;
1147+ let Some ( node) = network. nodes . get ( & node_id) else { return false } ;
1148+ let Some ( definition) = resolve_document_node_type ( & node. name ) else { return false } ;
11451149
1146- let exposed_value_count = node
1147- . inputs
1148- . iter ( )
1149- . filter ( |input| if let NodeInput :: Value { tagged_value : _, exposed } = input { * exposed } else { false } )
1150- . count ( ) ;
1150+ let exposed_value_count = node. inputs . iter ( ) . filter ( |input| if let NodeInput :: Value { exposed, .. } = input { * exposed } else { false } ) . count ( ) ;
11511151 let node_input_count = node. inputs . iter ( ) . filter ( |input| if let NodeInput :: Node { .. } = input { true } else { false } ) . count ( ) ;
11521152 let input_count = node_input_count + exposed_value_count;
1153- let Some ( definition) = resolve_document_node_type ( & node. name ) else { return false } ;
11541153 let output_count = definition. outputs . len ( ) ;
1154+
11551155 // TODO: Eventually allow nodes at the bottom of a stack to be layers, where `input_count` is 0
1156- if !node. has_primary_output || output_count != 1 || input_count == 0 || input_count > 2 {
1157- return false ;
1158- }
1159- true
1156+ node. has_primary_output && output_count == 1 && ( input_count == 1 || input_count == 2 )
11601157 }
11611158
11621159 fn untitled_layer_label ( node : & DocumentNode ) -> String {
0 commit comments