Skip to content

Commit 8d57e3d

Browse files
committed
Change some indices
1 parent 8b61dd5 commit 8d57e3d

File tree

3 files changed

+29
-16
lines changed

3 files changed

+29
-16
lines changed

editor/src/messages/portfolio/document/graph_operation/graph_operation_message_handler.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ impl MessageHandler<GraphOperationMessage, GraphOperationMessageData<'_>> for Gr
105105
}
106106
GraphOperationMessage::NewArtboard { id, artboard } => {
107107
let mut modify_inputs = ModifyInputsContext::new(document_network, document_metadata, node_graph, responses);
108-
if let Some(layer) = modify_inputs.create_layer(id, modify_inputs.document_network.original_outputs()[0].node_id, 0, 0) {
108+
if let Some(layer) = modify_inputs.create_layer(id, modify_inputs.document_network.original_outputs()[0].node_id, 0) {
109109
modify_inputs.insert_artboard(artboard, layer);
110110
}
111111
load_network_structure(document_network, document_metadata, selected_nodes, collapsed);
@@ -128,7 +128,7 @@ impl MessageHandler<GraphOperationMessage, GraphOperationMessageData<'_>> for Gr
128128
insert_index,
129129
alias,
130130
} => {
131-
trace!("Inserting new layer {id} as a child of {parent:?} at index {insert_index}");
131+
info!("Inserting new layer {id} as a child of {parent:?} at index {insert_index}");
132132

133133
let mut modify_inputs = ModifyInputsContext::new(document_network, document_metadata, node_graph, responses);
134134

@@ -160,15 +160,19 @@ impl MessageHandler<GraphOperationMessage, GraphOperationMessageData<'_>> for Gr
160160

161161
// Insert node into network
162162
modify_inputs.document_network.nodes.insert(node_id, document_node);
163+
info!("Inserting nodes");
163164
}
164165

165166
if let Some(layer_node) = modify_inputs.document_network.nodes.get_mut(&layer) {
166167
if let Some(&input) = new_ids.get(&NodeId(0)) {
167-
layer_node.inputs[1] = NodeInput::node(input, 0)
168+
layer_node.inputs[1] = NodeInput::node(input, 0);
169+
info!("Linking node");
168170
}
169171
}
170172

171173
modify_inputs.responses.add(NodeGraphMessage::RunDocumentGraph);
174+
} else {
175+
error!("Create failed");
172176
}
173177

174178
load_network_structure(document_network, document_metadata, selected_nodes, collapsed);

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

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -114,40 +114,48 @@ impl<'a> ModifyInputsContext<'a> {
114114
Some(new_id)
115115
}
116116

117-
pub fn skip_artboards(&self, output: &mut NodeOutput) -> Option<(NodeId, usize)> {
118-
while let NodeInput::Node { node_id, output_index, .. } = &self.document_network.nodes.get(&output.node_id)?.primary_input()? {
117+
pub fn skip_artboards(&self, output: &mut NodeId) -> Option<NodeId> {
118+
while let NodeInput::Node { node_id, .. } = &self.document_network.nodes.get(output)?.primary_input()? {
119119
let sibling_node = self.document_network.nodes.get(node_id)?;
120120
if sibling_node.name != "Artboard" {
121-
return Some((*node_id, *output_index));
121+
return Some(*node_id);
122122
}
123-
*output = NodeOutput::new(*node_id, *output_index)
123+
*output = *node_id;
124124
}
125125
None
126126
}
127127

128-
pub fn create_layer(&mut self, new_id: NodeId, output_node_id: NodeId, input_index: usize, skip_layer_nodes: usize) -> Option<NodeId> {
128+
pub fn create_layer(&mut self, new_id: NodeId, output_node_id: NodeId, skip_layer_nodes: usize) -> Option<NodeId> {
129129
assert!(!self.document_network.nodes.contains_key(&new_id), "Creating already existing layer");
130130

131-
let mut output = NodeOutput::new(output_node_id, input_index);
131+
let mut output = output_node_id;
132+
let mut output_in_index = 0;
132133
let mut sibling_layer = None;
133134
let mut shift = IVec2::new(0, 3);
134135
// Locate the node output of the first sibling layer to the new layer
135-
if let Some((node_id, output_index)) = self.skip_artboards(&mut output) {
136+
let skipped_artboards = self.skip_artboards(&mut output);
137+
// Connect to input 1 of the parent layer.
138+
output_in_index = self.document_network.nodes.get(&output).map_or(0, |node| if node.is_layer() { 1 } else { 0 });
139+
if let Some(node_id) = skipped_artboards {
136140
let sibling_node = self.document_network.nodes.get(&node_id)?;
137141
if sibling_node.is_layer() {
138142
// There is already a layer node
139143
sibling_layer = Some(NodeOutput::new(node_id, 0));
140144
} else {
145+
info!("Insert between");
141146
// The user has connected another node to the output. Insert a layer node between the output and the node.
142147
let node = resolve_document_node_type("Layer").expect("Layer node").default_document_node();
143-
let node_id = self.insert_between(NodeId(generate_uuid()), NodeOutput::new(node_id, output_index), output, node, 1, 0, IVec2::new(-8, 0))?;
148+
let index = self.document_network.nodes.get(&output).map_or(0, |node| if node.is_layer() { 1 } else { 0 });
149+
let post = NodeOutput::new(output, index);
150+
let node_id = self.insert_between(NodeId(generate_uuid()), NodeOutput::new(node_id, 0), post, node, 1, 0, IVec2::new(-8, 0))?;
144151
sibling_layer = Some(NodeOutput::new(node_id, 0));
145152
}
146153

147154
// Skip some layer nodes
148155
for _ in 0..skip_layer_nodes {
149156
if let Some(old_sibling) = &sibling_layer {
150-
output = NodeOutput::new(old_sibling.node_id, 0);
157+
output = old_sibling.node_id;
158+
output_in_index = 0;
151159
sibling_layer = self.document_network.nodes.get(&old_sibling.node_id)?.inputs[0].as_node().map(|node| NodeOutput::new(node, 0));
152160
shift = IVec2::new(0, 3);
153161
}
@@ -160,10 +168,11 @@ impl<'a> ModifyInputsContext<'a> {
160168

161169
// Create node
162170
let layer_node = resolve_document_node_type("Layer").expect("Layer node").default_document_node();
171+
163172
let new_id = if let Some(sibling_layer) = sibling_layer {
164-
self.insert_between(new_id, sibling_layer, output, layer_node, 0, 0, shift)
173+
self.insert_between(new_id, sibling_layer, NodeOutput::new(output, output_in_index), layer_node, 0, 0, shift)
165174
} else {
166-
self.insert_node_before(new_id, output.node_id, 0, layer_node, shift)
175+
self.insert_node_before(new_id, output, output_in_index, layer_node, shift)
167176
};
168177

169178
// Update the document metadata structure
@@ -188,7 +197,7 @@ impl<'a> ModifyInputsContext<'a> {
188197
} else {
189198
parent.to_node()
190199
};
191-
self.create_layer(new_id, output_node_id, 0, skip_layer_nodes)
200+
self.create_layer(new_id, output_node_id, skip_layer_nodes)
192201
}
193202

194203
pub fn insert_artboard(&mut self, artboard: Artboard, layer: NodeId) -> Option<NodeId> {

editor/src/messages/portfolio/portfolio_message_handler.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ impl MessageHandler<PortfolioMessage, PortfolioMessageData<'_>> for PortfolioMes
186186
let node = layer.to_node();
187187
let previous_alias = active_document.network().nodes.get(&node).map(|node| node.alias.clone()).unwrap_or_default();
188188

189-
let Some(node) = active_document.network().nodes.get(&node).and_then(|node| node.inputs.first()).and_then(|input| input.as_node()) else {
189+
let Some(node) = active_document.network().nodes.get(&node).and_then(|node| node.primary_input()).and_then(|input| input.as_node()) else {
190190
continue;
191191
};
192192

0 commit comments

Comments
 (0)