Skip to content

Commit 8ea25ac

Browse files
committed
fix:
- replace remaining hardcoded input indices with named constants - resolve the errors caused by the accuracy issue
1 parent d4ae26e commit 8ea25ac

File tree

13 files changed

+179
-92
lines changed

13 files changed

+179
-92
lines changed

editor/src/messages/portfolio/document/document_message_handler.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1900,7 +1900,12 @@ impl DocumentMessageHandler {
19001900

19011901
// If there's already a boolean operation on the selected layer, update it with the new operation
19021902
if let (Some(upstream_boolean_op), Some(only_selected_layer)) = (upstream_boolean_op, only_selected_layer) {
1903-
network_interface.set_input(&InputConnector::node(upstream_boolean_op, 1), NodeInput::value(TaggedValue::BooleanOperation(operation), false), &[]);
1903+
const BOOLEAN_OPERATION_INDEX: usize = 1;
1904+
network_interface.set_input(
1905+
&InputConnector::node(upstream_boolean_op, BOOLEAN_OPERATION_INDEX),
1906+
NodeInput::value(TaggedValue::BooleanOperation(operation), false),
1907+
&[],
1908+
);
19041909

19051910
responses.add(NodeGraphMessage::RunDocumentGraph);
19061911

@@ -2828,8 +2833,9 @@ impl DocumentMessageHandler {
28282833
.popover_layout({
28292834
// Showing only compatible types for the layer based on the output type of the node upstream from its horizontal input
28302835
let compatible_type = selected_layer.and_then(|layer| {
2836+
const LAYER_SECONDARY_INPUT_INDEX: usize = 1;
28312837
self.network_interface
2832-
.upstream_output_connector(&InputConnector::node(layer.to_node(), 1), &[])
2838+
.upstream_output_connector(&InputConnector::node(layer.to_node(), LAYER_SECONDARY_INPUT_INDEX), &[])
28332839
.and_then(|upstream_output| self.network_interface.output_type(&upstream_output, &[]).add_node_string())
28342840
});
28352841

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

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ impl MessageHandler<GraphOperationMessage, GraphOperationMessageContext<'_>> for
101101
}
102102
}
103103
GraphOperationMessage::SetUpstreamToChain { layer } => {
104-
let Some(OutputConnector::Node { node_id: first_chain_node, .. }) = network_interface.upstream_output_connector(&InputConnector::node(layer.to_node(), 1), &[]) else {
104+
const LAYER_SECONDARY_INPUT_INDEX: usize = 1;
105+
let Some(OutputConnector::Node { node_id: first_chain_node, .. }) = network_interface.upstream_output_connector(&InputConnector::node(layer.to_node(), LAYER_SECONDARY_INPUT_INDEX), &[]) else {
105106
return;
106107
};
107108

@@ -145,15 +146,18 @@ impl MessageHandler<GraphOperationMessage, GraphOperationMessageContext<'_>> for
145146

146147
// Set the bottom input of the artboard back to artboard
147148
let bottom_input = NodeInput::value(TaggedValue::Artboard(Table::new()), true);
148-
network_interface.set_input(&InputConnector::node(artboard_layer.to_node(), 0), bottom_input, &[]);
149+
const ARTBOARD_BASE_INDEX: usize = 0;
150+
network_interface.set_input(&InputConnector::node(artboard_layer.to_node(), ARTBOARD_BASE_INDEX), bottom_input, &[]);
149151
} else {
150152
// We have some non layers (e.g. just a rectangle node). We disconnect the bottom input and connect it to the left input.
151-
network_interface.disconnect_input(&InputConnector::node(artboard_layer.to_node(), 0), &[]);
152-
network_interface.set_input(&InputConnector::node(artboard_layer.to_node(), 1), primary_input, &[]);
153+
const ARTBOARD_BASE_INDEX: usize = 0;
154+
const ARTBOARD_CONTENT_INDEX: usize = 1;
155+
network_interface.disconnect_input(&InputConnector::node(artboard_layer.to_node(), ARTBOARD_BASE_INDEX), &[]);
156+
network_interface.set_input(&InputConnector::node(artboard_layer.to_node(), ARTBOARD_CONTENT_INDEX), primary_input, &[]);
153157

154158
// Set the bottom input of the artboard back to artboard
155159
let bottom_input = NodeInput::value(TaggedValue::Artboard(Table::new()), true);
156-
network_interface.set_input(&InputConnector::node(artboard_layer.to_node(), 0), bottom_input, &[]);
160+
network_interface.set_input(&InputConnector::node(artboard_layer.to_node(), ARTBOARD_BASE_INDEX), bottom_input, &[]);
157161
}
158162
}
159163
responses.add_front(NodeGraphMessage::SelectedNodesSet { nodes: vec![id] });
@@ -193,8 +197,9 @@ impl MessageHandler<GraphOperationMessage, GraphOperationMessageContext<'_>> for
193197
let first_new_node_id = new_ids[&NodeId(0)];
194198
responses.add(NodeGraphMessage::AddNodes { nodes, new_ids });
195199

200+
const LAYER_SECONDARY_INPUT_INDEX: usize = 1;
196201
responses.add(NodeGraphMessage::SetInput {
197-
input_connector: InputConnector::node(layer.to_node(), 1),
202+
input_connector: InputConnector::node(layer.to_node(), LAYER_SECONDARY_INPUT_INDEX),
198203
input: NodeInput::node(first_new_node_id, 0),
199204
});
200205
}
@@ -251,10 +256,11 @@ impl MessageHandler<GraphOperationMessage, GraphOperationMessageContext<'_>> for
251256
return;
252257
};
253258

254-
artboard_data.insert(
255-
artboard.to_node(),
256-
ArtboardInfo {
257-
input_node: NodeInput::node(document_node.inputs[1].as_node().unwrap_or_default(), 0),
259+
const ARTBOARD_CONTENT_INDEX: usize = 1;
260+
artboard_data.insert(
261+
artboard.to_node(),
262+
ArtboardInfo {
263+
input_node: NodeInput::node(document_node.inputs[ARTBOARD_CONTENT_INDEX].as_node().unwrap_or_default(), 0),
258264
output_nodes: network_interface
259265
.outward_wires(&[])
260266
.and_then(|outward_wires| outward_wires.get(&OutputConnector::node(artboard.to_node(), 0)))
@@ -281,8 +287,9 @@ impl MessageHandler<GraphOperationMessage, GraphOperationMessageContext<'_>> for
281287
// Go through all artboards and connect them to the merge nodes
282288
for artboard in &artboard_data {
283289
// Modify downstream connections
284-
responses.add(NodeGraphMessage::SetInput {
285-
input_connector: InputConnector::node(artboard.1.merge_node, 1),
290+
const MERGE_CONTENT_INDEX: usize = 1;
291+
responses.add(NodeGraphMessage::SetInput {
292+
input_connector: InputConnector::node(artboard.1.merge_node, MERGE_CONTENT_INDEX),
286293
input: NodeInput::node(artboard.1.input_node.as_node().unwrap_or_default(), 0),
287294
});
288295

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

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ impl<'a> ModifyInputsContext<'a> {
7171
let mut post_node_input_connector = if parent == LayerNodeIdentifier::ROOT_PARENT {
7272
InputConnector::Export(0)
7373
} else {
74-
InputConnector::node(parent.to_node(), 1)
74+
const LAYER_SECONDARY_INPUT_INDEX: usize = 1;
75+
InputConnector::node(parent.to_node(), LAYER_SECONDARY_INPUT_INDEX)
7576
};
7677
// Skip layers based on skip_layer_nodes, which inserts the new layer at a certain index of the layer stack.
7778
let mut current_index = 0;
@@ -320,7 +321,8 @@ impl<'a> ModifyInputsContext<'a> {
320321
// If inserting a 'Path' node, insert a 'Flatten Path' node if the type is `Graphic`.
321322
// TODO: Allow the 'Path' node to operate on table data by utilizing the reference (index or ID?) for each row.
322323
if node_definition.identifier == "Path" {
323-
let layer_input_type = self.network_interface.input_type(&InputConnector::node(output_layer.to_node(), 1), &[]);
324+
const LAYER_SECONDARY_INPUT_INDEX: usize = 1;
325+
let layer_input_type = self.network_interface.input_type(&InputConnector::node(output_layer.to_node(), LAYER_SECONDARY_INPUT_INDEX), &[]);
324326
if layer_input_type.compiled_nested_type() == Some(&concrete!(Table<Graphic>)) {
325327
let Some(flatten_path_definition) = resolve_proto_node_type(graphene_std::vector_nodes::flatten_path::IDENTIFIER) else {
326328
log::error!("Flatten Path does not exist in ModifyInputsContext::existing_node_id");
@@ -505,10 +507,18 @@ impl<'a> ModifyInputsContext<'a> {
505507
let Some(brush_node_id) = self.existing_network_node_id("Brush", true) else {
506508
return;
507509
};
508-
self.set_input_with_refresh(InputConnector::node(brush_node_id, 1), NodeInput::value(TaggedValue::BrushStrokes(strokes), false), false);
510+
const BRUSH_STROKES_INDEX: usize = 1;
511+
self.set_input_with_refresh(
512+
InputConnector::node(brush_node_id, BRUSH_STROKES_INDEX),
513+
NodeInput::value(TaggedValue::BrushStrokes(strokes), false),
514+
false,
515+
);
509516
}
510517

511518
pub fn resize_artboard(&mut self, location: IVec2, dimensions: IVec2) {
519+
const ARTBOARD_LOCATION_INDEX: usize = 2;
520+
const ARTBOARD_DIMENSIONS_INDEX: usize = 3;
521+
512522
let Some(artboard_node_id) = self.existing_network_node_id("Artboard", true) else {
513523
return;
514524
};
@@ -524,8 +534,16 @@ impl<'a> ModifyInputsContext<'a> {
524534
dimensions.y *= -1;
525535
location.y -= dimensions.y;
526536
}
527-
self.set_input_with_refresh(InputConnector::node(artboard_node_id, 2), NodeInput::value(TaggedValue::DVec2(location.into()), false), false);
528-
self.set_input_with_refresh(InputConnector::node(artboard_node_id, 3), NodeInput::value(TaggedValue::DVec2(dimensions.into()), false), false);
537+
self.set_input_with_refresh(
538+
InputConnector::node(artboard_node_id, ARTBOARD_LOCATION_INDEX),
539+
NodeInput::value(TaggedValue::DVec2(location.into()), false),
540+
false,
541+
);
542+
self.set_input_with_refresh(
543+
InputConnector::node(artboard_node_id, ARTBOARD_DIMENSIONS_INDEX),
544+
NodeInput::value(TaggedValue::DVec2(dimensions.into()), false),
545+
false,
546+
);
529547
}
530548

531549
/// Set the input, refresh the properties panel, and run the document graph if skip_rerender is false

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1324,7 +1324,8 @@ impl<'a> MessageHandler<NodeGraphMessage, NodeGraphMessageContext<'a>> for NodeG
13241324
.cloned()
13251325
.collect::<Vec<_>>()
13261326
{
1327-
network_interface.try_set_upstream_to_chain(&InputConnector::node(layer, 1), selection_network_path);
1327+
const LAYER_SECONDARY_INPUT_INDEX: usize = 1;
1328+
network_interface.try_set_upstream_to_chain(&InputConnector::node(layer, LAYER_SECONDARY_INPUT_INDEX), selection_network_path);
13281329
}
13291330
responses.add(NodeGraphMessage::SendGraph);
13301331

@@ -2503,9 +2504,10 @@ impl NodeGraphMessageHandler {
25032504
.icon(Some("Node".to_string()))
25042505
.tooltip_description("Add an operation to the end of this layer's chain of nodes.")
25052506
.popover_layout({
2507+
const LAYER_SECONDARY_INPUT_INDEX: usize = 1;
25062508
let compatible_type = context
25072509
.network_interface
2508-
.upstream_output_connector(&InputConnector::node(layer, 1), &[])
2510+
.upstream_output_connector(&InputConnector::node(layer, LAYER_SECONDARY_INPUT_INDEX), &[])
25092511
.and_then(|upstream_output| context.network_interface.output_type(&upstream_output, &[]).add_node_string());
25102512

25112513
let mut node_chooser = NodeCatalog::new();

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

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,8 @@ impl NodeNetworkInterface {
419419
for old_id in new_nodes.iter().map(|(_, old_id, _)| *old_id).collect::<Vec<_>>() {
420420
// Try set all selected nodes upstream of a layer to be chain nodes
421421
if self.is_layer(&old_id, network_path) {
422-
for valid_upstream_chain_node in self.valid_upstream_chain_nodes(&InputConnector::node(old_id, 1), network_path) {
422+
const LAYER_SECONDARY_INPUT_INDEX: usize = 1;
423+
for valid_upstream_chain_node in self.valid_upstream_chain_nodes(&InputConnector::node(old_id, LAYER_SECONDARY_INPUT_INDEX), network_path) {
423424
if let Some(node_template) = new_nodes.iter_mut().find_map(|(_, old_id, template)| (*old_id == valid_upstream_chain_node).then_some(template)) {
424425
match &mut node_template.persistent_node_metadata.node_type_metadata {
425426
NodeTypePersistentMetadata::Node(node_metadata) => node_metadata.position = NodePosition::Chain,
@@ -1195,7 +1196,8 @@ impl NodeNetworkInterface {
11951196
.find(|ancestor| *ancestor != LayerNodeIdentifier::ROOT_PARENT && self.is_artboard(&ancestor.to_node(), &[]))
11961197
{
11971198
let artboard = self.document_node(&artboard_node_identifier.to_node(), &[]);
1198-
let clip_input = artboard.unwrap().inputs.get(5).unwrap();
1199+
const ARTBOARD_CLIP_INDEX: usize = 5;
1200+
let clip_input = artboard.unwrap().inputs.get(ARTBOARD_CLIP_INDEX).unwrap();
11991201
if let NodeInput::Value { tagged_value, .. } = clip_input
12001202
&& tagged_value.clone().deref() == &TaggedValue::Bool(true)
12011203
{
@@ -1312,7 +1314,8 @@ impl NodeNetworkInterface {
13121314
.iter()
13131315
.filter_map(move |node_id| {
13141316
if self.is_layer(node_id, network_path) {
1315-
network.nodes.get(node_id).and_then(|node| node.inputs.get(1)).and_then(|input| input.as_node())
1317+
const LAYER_SECONDARY_INPUT_INDEX: usize = 1;
1318+
network.nodes.get(node_id).and_then(|node| node.inputs.get(LAYER_SECONDARY_INPUT_INDEX)).and_then(|input| input.as_node())
13161319
} else {
13171320
Some(*node_id)
13181321
}
@@ -3097,7 +3100,8 @@ impl NodeNetworkInterface {
30973100
let mut modified = vector.clone();
30983101

30993102
let path_node = self.document_network().nodes.get(&path_node);
3100-
let modification_input = path_node.and_then(|node: &DocumentNode| node.inputs.get(1)).and_then(|input| input.as_value());
3103+
const PATH_MODIFICATION_INDEX: usize = 1;
3104+
let modification_input = path_node.and_then(|node: &DocumentNode| node.inputs.get(PATH_MODIFICATION_INDEX)).and_then(|input| input.as_value());
31013105
if let Some(TaggedValue::VectorModification(modification)) = modification_input {
31023106
modification.apply(&mut modified);
31033107
}
@@ -4604,7 +4608,8 @@ impl NodeNetworkInterface {
46044608

46054609
// Try build the chain
46064610
if is_layer {
4607-
self.try_set_upstream_to_chain(&InputConnector::node(*node_id, 1), network_path);
4611+
const LAYER_SECONDARY_INPUT_INDEX: usize = 1;
4612+
self.try_set_upstream_to_chain(&InputConnector::node(*node_id, LAYER_SECONDARY_INPUT_INDEX), network_path);
46084613
} else {
46094614
self.try_set_node_to_chain(node_id, network_path);
46104615
}
@@ -5655,16 +5660,17 @@ impl NodeNetworkInterface {
56555660

56565661
// Moves a node and to the start of a layer chain (feeding into the secondary input of the layer)
56575662
pub fn move_node_to_chain_start(&mut self, node_id: &NodeId, parent: LayerNodeIdentifier, network_path: &[NodeId]) {
5658-
let Some(current_input) = self.input_from_connector(&InputConnector::node(parent.to_node(), 1), network_path) else {
5663+
const LAYER_SECONDARY_INPUT_INDEX: usize = 1;
5664+
let Some(current_input) = self.input_from_connector(&InputConnector::node(parent.to_node(), LAYER_SECONDARY_INPUT_INDEX), network_path) else {
56595665
log::error!("Could not get input for node {node_id}");
56605666
return;
56615667
};
56625668
if matches!(current_input, NodeInput::Value { .. }) {
5663-
self.create_wire(&OutputConnector::node(*node_id, 0), &InputConnector::node(parent.to_node(), 1), network_path);
5669+
self.create_wire(&OutputConnector::node(*node_id, 0), &InputConnector::node(parent.to_node(), LAYER_SECONDARY_INPUT_INDEX), network_path);
56645670
self.set_chain_position(node_id, network_path);
56655671
} else {
56665672
// Insert the node in the gap and set the upstream to a chain
5667-
self.insert_node_between(node_id, &InputConnector::node(parent.to_node(), 1), 0, network_path);
5673+
self.insert_node_between(node_id, &InputConnector::node(parent.to_node(), LAYER_SECONDARY_INPUT_INDEX), 0, network_path);
56685674
self.force_set_upstream_to_chain(node_id, network_path);
56695675
}
56705676
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ use crate::messages::portfolio::document::node_graph::utility_types::FrontendGra
1212
use crate::messages::portfolio::document::utility_types::network_interface::{InputConnector, NodeNetworkInterface, OutputConnector};
1313

1414
// This file contains utility methods for interfacing with the resolved types returned from the compiler
15+
16+
/// Index of the `editor_api` parameter in the render node (second parameter after the primary context input).
17+
const RENDER_NODE_EDITOR_API_INDEX: usize = 1;
1518
#[derive(Debug, Default)]
1619
pub struct ResolvedDocumentNodeTypes {
1720
pub types: HashMap<Vec<NodeId>, NodeTypes>,
@@ -317,7 +320,7 @@ impl NodeNetworkInterface {
317320
log::error!("Protonode {render_node:?} not found in registry");
318321
return Vec::new();
319322
};
320-
implementations.keys().map(|types| types.inputs[1].clone()).collect()
323+
implementations.keys().map(|types| types.inputs[RENDER_NODE_EDITOR_API_INDEX].clone()).collect()
321324
}
322325
}
323326
}

editor/src/messages/tool/common_functionality/gizmos/shape_gizmos/point_radius_handle.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use crate::messages::tool::common_functionality::shapes::shape_utility::{extract
1414
use glam::DVec2;
1515
use graph_craft::document::NodeInput;
1616
use graph_craft::document::value::TaggedValue;
17+
use graphene_std::vector::generator_nodes::star::*;
1718
use std::collections::VecDeque;
1819
use std::f64::consts::{FRAC_1_SQRT_2, FRAC_PI_4, PI, SQRT_2};
1920

@@ -337,13 +338,13 @@ impl PointRadiusHandle {
337338
return snap_radii;
338339
};
339340

340-
let (Some(&TaggedValue::F64(radius_1)), Some(&TaggedValue::F64(radius_2))) = (node_inputs[2].as_value(), node_inputs[3].as_value()) else {
341+
let (Some(&TaggedValue::F64(radius_1)), Some(&TaggedValue::F64(radius_2))) = (node_inputs[Radius1Input::INDEX].as_value(), node_inputs[Radius2Input::INDEX].as_value()) else {
341342
return snap_radii;
342343
};
343344

344345
let other_radius = if radius_index == 3 { radius_1 } else { radius_2 };
345346

346-
let Some(&TaggedValue::U32(sides)) = node_inputs[1].as_value() else {
347+
let Some(&TaggedValue::U32(sides)) = node_inputs[SidesInput::<u32>::INDEX].as_value() else {
347348
return snap_radii;
348349
};
349350

@@ -456,7 +457,7 @@ impl PointRadiusHandle {
456457
return;
457458
};
458459

459-
let (Some(&TaggedValue::F64(radius_1)), Some(&TaggedValue::F64(radius_2))) = (node_inputs[2].as_value(), node_inputs[3].as_value()) else {
460+
let (Some(&TaggedValue::F64(radius_1)), Some(&TaggedValue::F64(radius_2))) = (node_inputs[Radius1Input::INDEX].as_value(), node_inputs[Radius2Input::INDEX].as_value()) else {
460461
return;
461462
};
462463

0 commit comments

Comments
 (0)