@@ -177,26 +177,13 @@ impl NodeNetworkInterface {
177
177
let mut last_chain_node_distance = 0u32 ;
178
178
// Iterate upstream from the layer, and get the number of nodes distance to the last node with Position::Chain
179
179
for ( index, node_id) in self
180
- . upstream_flow_back_from_nodes ( vec ! [ * node_id] , network_path, FlowType :: HorizontalFlow )
180
+ . upstream_flow_back_from_nodes ( vec ! [ * node_id] , network_path, FlowType :: HorizontalPrimaryOutputFlow )
181
181
. skip ( 1 )
182
182
. enumerate ( )
183
183
. collect :: < Vec < _ > > ( )
184
184
{
185
- let Some ( network_metadata) = self . network_metadata ( network_path) else {
186
- log:: error!( "Could not get nested network_metadata in chain_width" ) ;
187
- return 0 ;
188
- } ;
189
185
// Check if the node is positioned as a chain
190
- let is_chain = network_metadata
191
- . persistent_metadata
192
- . node_metadata
193
- . get ( & node_id)
194
- . map ( |node_metadata| & node_metadata. persistent_metadata . node_type_metadata )
195
- . is_some_and ( |node_type_metadata| match node_type_metadata {
196
- NodeTypePersistentMetadata :: Node ( node_persistent_metadata) => matches ! ( node_persistent_metadata. position, NodePosition :: Chain ) ,
197
- _ => false ,
198
- } ) ;
199
- if is_chain {
186
+ if self . is_chain ( & node_id, network_path) {
200
187
last_chain_node_distance = ( index as u32 ) + 1 ;
201
188
} else {
202
189
return last_chain_node_distance * 7 + 1 ;
@@ -326,7 +313,7 @@ impl NodeNetworkInterface {
326
313
/// Creates a copy for each node by disconnecting nodes which are not connected to other copied nodes.
327
314
/// Returns an iterator of all persistent metadata for a node and their ids
328
315
pub fn copy_nodes < ' a > ( & ' a mut self , new_ids : & ' a HashMap < NodeId , NodeId > , network_path : & ' a [ NodeId ] ) -> impl Iterator < Item = ( NodeId , NodeTemplate ) > + ' a {
329
- new_ids
316
+ let mut new_nodes = new_ids
330
317
. iter ( )
331
318
. filter_map ( |( node_id, & new) | {
332
319
self . create_node_template ( node_id, network_path) . and_then ( |mut node_template| {
@@ -354,7 +341,7 @@ impl NodeNetworkInterface {
354
341
} ;
355
342
}
356
343
357
- // Ensure a chain node has a selected downstream layer, and set absolute nodes to a chain if there is a downstream layer
344
+ // If a chain node does not have a selected downstream layer, then set the position to absolute
358
345
let downstream_layer = self . downstream_layer ( node_id, network_path) ;
359
346
if downstream_layer. map_or ( true , |downstream_layer| new_ids. keys ( ) . all ( |key| * key != downstream_layer. to_node ( ) ) ) {
360
347
let Some ( position) = self . position ( node_id, network_path) else {
@@ -364,20 +351,6 @@ impl NodeNetworkInterface {
364
351
node_template. persistent_node_metadata . node_type_metadata = NodeTypePersistentMetadata :: Node ( NodePersistentMetadata {
365
352
position : NodePosition :: Absolute ( position) ,
366
353
} ) ;
367
- } else if !self . is_layer ( node_id, network_path) {
368
- if let Some ( downstream_layer) = downstream_layer {
369
- if self
370
- . upstream_flow_back_from_nodes ( vec ! [ downstream_layer. to_node( ) ] , network_path, FlowType :: HorizontalFlow )
371
- . skip ( 1 )
372
- . take_while ( |node_id| !self . is_layer ( node_id, network_path) )
373
- . any ( |upstream_node| upstream_node == * node_id)
374
- {
375
- match & mut node_template. persistent_node_metadata . node_type_metadata {
376
- NodeTypePersistentMetadata :: Node ( node_metadata) => node_metadata. position = NodePosition :: Chain ,
377
- NodeTypePersistentMetadata :: Layer ( _) => log:: error!( "Node is not be a layer" ) ,
378
- } ;
379
- }
380
- }
381
354
}
382
355
383
356
// Shift all absolute nodes 2 to the right and 2 down
@@ -395,12 +368,25 @@ impl NodeNetworkInterface {
395
368
}
396
369
}
397
370
398
- Some ( ( new, node_id, node_template) )
371
+ Some ( ( new, * node_id, node_template) )
399
372
} )
400
373
} )
401
- . collect :: < Vec < _ > > ( )
402
- . into_iter ( )
403
- . map ( move |( new, node_id, node) | ( new, self . map_ids ( node, node_id, new_ids, network_path) ) )
374
+ . collect :: < Vec < _ > > ( ) ;
375
+
376
+ for old_id in new_nodes. iter ( ) . map ( |( _, old_id, _) | * old_id) . collect :: < Vec < _ > > ( ) {
377
+ // Try set all selected nodes upstream of a layer to be chain nodes
378
+ if self . is_layer ( & old_id, network_path) {
379
+ for valid_upstream_chain_node in self . valid_upstream_chain_nodes ( & InputConnector :: node ( old_id, 1 ) , network_path) {
380
+ if let Some ( node_template) = new_nodes. iter_mut ( ) . find_map ( |( _, old_id, template) | ( * old_id == valid_upstream_chain_node) . then_some ( template) ) {
381
+ match & mut node_template. persistent_node_metadata . node_type_metadata {
382
+ NodeTypePersistentMetadata :: Node ( node_metadata) => node_metadata. position = NodePosition :: Chain ,
383
+ NodeTypePersistentMetadata :: Layer ( _) => log:: error!( "Node cannot be a layer" ) ,
384
+ } ;
385
+ }
386
+ }
387
+ }
388
+ }
389
+ new_nodes. into_iter ( ) . map ( move |( new, node_id, node) | ( new, self . map_ids ( node, & node_id, new_ids, network_path) ) )
404
390
}
405
391
406
392
/// Create a node template from an existing node.
@@ -2060,7 +2046,6 @@ impl NodeNetworkInterface {
2060
2046
log:: error!( "Could not get nested network in load_outward_wires" ) ;
2061
2047
return ;
2062
2048
} ;
2063
- log:: debug!( "network: {network:?}" ) ;
2064
2049
// Initialize all output connectors for nodes
2065
2050
for ( node_id, _) in network. nodes . iter ( ) {
2066
2051
let number_of_outputs = self . number_of_outputs ( node_id, network_path) ;
@@ -2072,12 +2057,9 @@ impl NodeNetworkInterface {
2072
2057
for import_index in 0 ..self . number_of_imports ( network_path) {
2073
2058
outward_wires. insert ( OutputConnector :: Import ( import_index) , Vec :: new ( ) ) ;
2074
2059
}
2075
- log:: debug!( "Initialized outward_wires: {:?}" , outward_wires) ;
2076
2060
// Collect wires between all nodes and the Imports
2077
2061
for ( current_node_id, node) in network. nodes . iter ( ) {
2078
- log:: debug!( "node: {node:?}, id: {current_node_id:?}" ) ;
2079
2062
for ( input_index, input) in node. inputs . iter ( ) . enumerate ( ) {
2080
- log:: debug!( "input: {input:?}" ) ;
2081
2063
if let NodeInput :: Node { node_id, output_index, .. } = input {
2082
2064
// If this errors then there is an input to a node that does not exist
2083
2065
let outward_wires_entry = outward_wires. get_mut ( & OutputConnector :: node ( * node_id, * output_index) ) . expect ( & format ! (
@@ -2095,7 +2077,6 @@ impl NodeNetworkInterface {
2095
2077
}
2096
2078
}
2097
2079
for ( export_index, export) in network. exports . iter ( ) . enumerate ( ) {
2098
- log:: debug!( "export: {export:?}" ) ;
2099
2080
if let NodeInput :: Node { node_id, output_index, .. } = export {
2100
2081
let outward_wires_entry = outward_wires. get_mut ( & OutputConnector :: node ( * node_id, * output_index) ) . expect ( & format ! (
2101
2082
"Output connector {:?} should be initialized for each node input from exports" ,
@@ -4103,74 +4084,84 @@ impl NodeNetworkInterface {
4103
4084
self . unload_all_nodes_bounding_box ( network_path) ;
4104
4085
}
4105
4086
4106
- /// Input connector is the input to the layer
4107
- pub fn try_set_upstream_to_chain ( & mut self , input_connector : & InputConnector , network_path : & [ NodeId ] ) {
4108
- // If the new input is to a non layer node on the same y position as the input connector, or the input connector is the side input of a layer, then set it to chain position
4109
- if let InputConnector :: Node {
4087
+ fn valid_upstream_chain_nodes ( & mut self , input_connector : & InputConnector , network_path : & [ NodeId ] ) -> Vec < NodeId > {
4088
+ let InputConnector :: Node {
4110
4089
node_id : input_connector_node_id,
4111
4090
input_index,
4112
4091
} = input_connector
4113
- {
4114
- let mut set_position_to_chain = false ;
4115
- if self . is_layer ( input_connector_node_id, network_path) && * input_index == 1 || self . is_chain ( input_connector_node_id, network_path) && * input_index == 0 {
4116
- let mut downstream_id = * input_connector_node_id;
4117
- for upstream_node in self
4118
- . upstream_flow_back_from_nodes ( vec ! [ * input_connector_node_id] , network_path, FlowType :: HorizontalFlow )
4119
- . skip ( 1 )
4120
- . collect :: < Vec < _ > > ( )
4121
- {
4122
- if self . is_layer ( & upstream_node, network_path) {
4123
- break ;
4124
- }
4125
- if !self . has_primary_output ( & upstream_node, network_path) {
4126
- break ;
4127
- }
4128
- let Some ( outward_wires) = self . outward_wires ( network_path) . and_then ( |outward_wires| outward_wires. get ( & OutputConnector :: node ( upstream_node, 0 ) ) ) else {
4129
- log:: error!( "Could not get outward wires in try_set_upstream_to_chain" ) ;
4130
- break ;
4131
- } ;
4132
- if outward_wires. len ( ) != 1 {
4133
- break ;
4134
- }
4135
- let downstream_position = self . position ( & downstream_id, network_path) ;
4136
- let upstream_node_position = self . position ( & upstream_node, network_path) ;
4137
- if let ( Some ( input_connector_position) , Some ( new_upstream_node_position) ) = ( downstream_position, upstream_node_position) {
4138
- if input_connector_position. y == new_upstream_node_position. y
4139
- && new_upstream_node_position. x >= input_connector_position. x - 9
4140
- && new_upstream_node_position. x <= input_connector_position. x
4141
- {
4142
- set_position_to_chain = true ;
4143
- self . set_chain_position ( & upstream_node, network_path) ;
4144
- } else {
4145
- break ;
4146
- }
4092
+ else {
4093
+ return Vec :: new ( ) ;
4094
+ } ;
4095
+ let mut set_position_to_chain = Vec :: new ( ) ;
4096
+ if self . is_layer ( input_connector_node_id, network_path) && * input_index == 1 || self . is_chain ( input_connector_node_id, network_path) && * input_index == 0 {
4097
+ let mut downstream_id = * input_connector_node_id;
4098
+ for upstream_node in self
4099
+ . upstream_flow_back_from_nodes ( vec ! [ * input_connector_node_id] , network_path, FlowType :: HorizontalFlow )
4100
+ . skip ( 1 )
4101
+ . collect :: < Vec < _ > > ( )
4102
+ {
4103
+ if self . is_layer ( & upstream_node, network_path) {
4104
+ break ;
4105
+ }
4106
+ if !self . has_primary_output ( & upstream_node, network_path) {
4107
+ break ;
4108
+ }
4109
+ let Some ( outward_wires) = self . outward_wires ( network_path) . and_then ( |outward_wires| outward_wires. get ( & OutputConnector :: node ( upstream_node, 0 ) ) ) else {
4110
+ log:: error!( "Could not get outward wires in try_set_upstream_to_chain" ) ;
4111
+ break ;
4112
+ } ;
4113
+ if outward_wires. len ( ) != 1 {
4114
+ break ;
4115
+ }
4116
+ let downstream_position = self . position ( & downstream_id, network_path) ;
4117
+ let upstream_node_position = self . position ( & upstream_node, network_path) ;
4118
+ if let ( Some ( input_connector_position) , Some ( new_upstream_node_position) ) = ( downstream_position, upstream_node_position) {
4119
+ if input_connector_position. y == new_upstream_node_position. y
4120
+ && new_upstream_node_position. x >= input_connector_position. x - 9
4121
+ && new_upstream_node_position. x <= input_connector_position. x
4122
+ {
4123
+ set_position_to_chain. push ( upstream_node) ;
4147
4124
} else {
4148
4125
break ;
4149
4126
}
4150
- downstream_id = upstream_node;
4127
+ } else {
4128
+ break ;
4151
4129
}
4130
+ downstream_id = upstream_node;
4152
4131
}
4153
- // Reload click target of the layer which used to encapsulate the node
4154
- if set_position_to_chain {
4155
- let mut downstream_layer = Some ( * input_connector_node_id) ;
4156
- while let Some ( downstream_layer_id) = downstream_layer {
4157
- if downstream_layer_id == * input_connector_node_id || !self . is_layer ( & downstream_layer_id, network_path) {
4158
- let Some ( outward_wires) = self . outward_wires ( network_path) else {
4159
- log:: error!( "Could not get outward wires in try_set_upstream_to_chain" ) ;
4160
- downstream_layer = None ;
4161
- break ;
4162
- } ;
4163
- downstream_layer = outward_wires
4164
- . get ( & OutputConnector :: node ( downstream_layer_id, 0 ) )
4165
- . and_then ( |outward_wires| if outward_wires. len ( ) == 1 { outward_wires[ 0 ] . node_id ( ) } else { None } ) ;
4166
- } else {
4132
+ }
4133
+ set_position_to_chain
4134
+ }
4135
+
4136
+ /// Input connector is the input to the layer
4137
+ pub fn try_set_upstream_to_chain ( & mut self , input_connector : & InputConnector , network_path : & [ NodeId ] ) {
4138
+ // If the new input is to a non layer node on the same y position as the input connector, or the input connector is the side input of a layer, then set it to chain position
4139
+
4140
+ let valid_upstream_chain_nodes = self . valid_upstream_chain_nodes ( input_connector, network_path) ;
4141
+
4142
+ for node_id in & valid_upstream_chain_nodes {
4143
+ self . set_chain_position ( & node_id, network_path) ;
4144
+ }
4145
+ // Reload click target of the layer which used to encapsulate the node
4146
+ if !valid_upstream_chain_nodes. is_empty ( ) {
4147
+ let mut downstream_layer = Some ( input_connector. node_id ( ) . unwrap ( ) ) ;
4148
+ while let Some ( downstream_layer_id) = downstream_layer {
4149
+ if downstream_layer_id == input_connector. node_id ( ) . unwrap ( ) || !self . is_layer ( & downstream_layer_id, network_path) {
4150
+ let Some ( outward_wires) = self . outward_wires ( network_path) else {
4151
+ log:: error!( "Could not get outward wires in try_set_upstream_to_chain" ) ;
4152
+ downstream_layer = None ;
4167
4153
break ;
4168
- }
4169
- }
4170
- if let Some ( downstream_layer) = downstream_layer {
4171
- self . unload_node_click_targets ( & downstream_layer, network_path) ;
4154
+ } ;
4155
+ downstream_layer = outward_wires
4156
+ . get ( & OutputConnector :: node ( downstream_layer_id, 0 ) )
4157
+ . and_then ( |outward_wires| if outward_wires. len ( ) == 1 { outward_wires[ 0 ] . node_id ( ) } else { None } ) ;
4158
+ } else {
4159
+ break ;
4172
4160
}
4173
4161
}
4162
+ if let Some ( downstream_layer) = downstream_layer {
4163
+ self . unload_node_click_targets ( & downstream_layer, network_path) ;
4164
+ }
4174
4165
}
4175
4166
}
4176
4167
@@ -4987,6 +4978,8 @@ pub enum FlowType {
4987
4978
PrimaryFlow ,
4988
4979
/// Iterate over the secondary input (inclusive) for layer nodes and primary input for non layer nodes.
4989
4980
HorizontalFlow ,
4981
+ /// Same as horizontal flow, but only iterates over connections to primary outputs
4982
+ HorizontalPrimaryOutputFlow ,
4990
4983
/// Upstream flow starting from the either the node (inclusive) or secondary input of the layer (not inclusive).
4991
4984
LayerChildrenUpstreamFlow ,
4992
4985
}
@@ -5009,15 +5002,19 @@ impl<'a> Iterator for FlowIter<'a> {
5009
5002
let node_id = self . stack . pop ( ) ?;
5010
5003
5011
5004
if let ( Some ( document_node) , Some ( node_metadata) ) = ( self . network . nodes . get ( & node_id) , self . network_metadata . persistent_metadata . node_metadata . get ( & node_id) ) {
5012
- let skip = if self . flow_type == FlowType :: HorizontalFlow && node_metadata. persistent_metadata . is_layer ( ) {
5005
+ let skip = if matches ! ( self . flow_type, FlowType :: HorizontalFlow | FlowType :: HorizontalPrimaryOutputFlow ) && node_metadata. persistent_metadata . is_layer ( ) {
5013
5006
1
5014
5007
} else {
5015
5008
0
5016
5009
} ;
5017
5010
let take = if self . flow_type == FlowType :: UpstreamFlow { usize:: MAX } else { 1 } ;
5018
5011
let inputs = document_node. inputs . iter ( ) . skip ( skip) . take ( take) ;
5019
5012
5020
- let node_ids = inputs. filter_map ( |input| if let NodeInput :: Node { node_id, .. } = input { Some ( node_id) } else { None } ) ;
5013
+ let node_ids = inputs. filter_map ( |input| match input {
5014
+ NodeInput :: Node { output_index, .. } if self . flow_type == FlowType :: HorizontalPrimaryOutputFlow && * output_index != 0 => None ,
5015
+ NodeInput :: Node { node_id, .. } => Some ( node_id) ,
5016
+ _ => None ,
5017
+ } ) ;
5021
5018
5022
5019
self . stack . extend ( node_ids) ;
5023
5020
0 commit comments