Skip to content

Commit 3ec7143

Browse files
committed
Fix pen tool (except overlays)
1 parent 7b76b8c commit 3ec7143

File tree

3 files changed

+59
-18
lines changed

3 files changed

+59
-18
lines changed

editor/src/messages/tool/common_functionality/graph_modification_utils.rs

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::messages::prelude::*;
44

55
use bezier_rs::{ManipulatorGroup, Subpath};
66
use document_legacy::{LayerId, Operation};
7-
use graph_craft::document::NodeNetwork;
7+
use graph_craft::document::{value::TaggedValue, DocumentNode, NodeId, NodeInput, NodeNetwork};
88
use graphene_core::uuid::ManipulatorGroupId;
99

1010
use glam::DAffine2;
@@ -43,3 +43,55 @@ pub fn set_manipulator_mirror_angle(manipulator_groups: &Vec<ManipulatorGroup<Ma
4343
});
4444
}
4545
}
46+
47+
/// An immutable reference to a layer within the document node graph for easy access.
48+
pub struct NodeGraphLayer<'a> {
49+
node_graph: &'a NodeNetwork,
50+
outwards_links: HashMap<NodeId, Vec<NodeId>>,
51+
layer_node: NodeId,
52+
}
53+
54+
impl<'a> NodeGraphLayer<'a> {
55+
/// Get the layer node from the document
56+
pub fn new(layer: &[LayerId], document: &'a document_legacy::document::Document) -> Option<Self> {
57+
let node_graph = &document.document_network;
58+
let outwards_links = document.document_network.collect_outwards_links();
59+
60+
let Some(mut layer_node) = layer.last().copied() else {
61+
error!("Tried to modify root layer");
62+
return None;
63+
};
64+
while node_graph.nodes.get(&layer_node)?.name != "Layer" {
65+
layer_node = outwards_links.get(&layer_node)?.first().copied()?;
66+
}
67+
Some(Self {
68+
node_graph,
69+
outwards_links,
70+
layer_node,
71+
})
72+
}
73+
74+
/// Return an iterator up the primary flow of the layer
75+
pub fn primary_layer_flow(&self) -> impl Iterator<Item = (&'a DocumentNode, u64)> {
76+
self.node_graph.primary_flow_from_opt(Some(self.layer_node))
77+
}
78+
79+
/// Find a specific input of a node within the layer's primary flow
80+
pub fn find_input(&self, node_name: &str, index: usize) -> Option<&'a TaggedValue> {
81+
for (node, _id) in self.primary_layer_flow() {
82+
if node.name == node_name {
83+
let subpaths_input = node.inputs.get(index)?;
84+
let NodeInput::Value {
85+
tagged_value,
86+
..
87+
} = subpaths_input
88+
else {
89+
continue;
90+
};
91+
92+
return Some(&tagged_value);
93+
}
94+
}
95+
None
96+
}
97+
}

editor/src/messages/tool/common_functionality/overlay_renderer.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ impl OverlayRenderer {
5050
let layer_id = layer_path.last().unwrap();
5151
self.layer_overlay_visibility(document, layer_path.clone(), true, responses);
5252

53+
// How we get vector data from graph?
5354
if let Some(vector_data) = layer.as_vector_data() {
5455
let outline_cache = self.shape_overlay_cache.get(layer_id);
5556
trace!("Overlay: Outline cache {:?}", &outline_cache);

editor/src/messages/tool/tool_messages/pen_tool.rs

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use crate::messages::prelude::*;
1010
use crate::messages::tool::common_functionality::color_selector::{ToolColorOptions, ToolColorType};
1111
use crate::messages::tool::common_functionality::graph_modification_utils;
1212
use crate::messages::tool::common_functionality::overlay_renderer::OverlayRenderer;
13+
use crate::messages::tool::tool_messages::pen_tool::graph_modification_utils::NodeGraphLayer;
1314

1415
use crate::messages::tool::common_functionality::snapping::SnapManager;
1516
use crate::messages::tool::utility_types::{EventToMessageMap, Fsm, ToolActionHandlerData, ToolMetadata, ToolTransition, ToolType};
@@ -18,7 +19,6 @@ use crate::messages::tool::utility_types::{HintData, HintGroup, HintInfo};
1819
use bezier_rs::Subpath;
1920
use document_legacy::LayerId;
2021
use graph_craft::document::value::TaggedValue;
21-
use graph_craft::document::NodeInput;
2222
use graphene_core::uuid::ManipulatorGroupId;
2323
use graphene_core::vector::style::{Fill, Stroke};
2424
use graphene_core::vector::{ManipulatorPointId, SelectedType};
@@ -794,21 +794,9 @@ fn should_extend(document: &DocumentMessageHandler, pos: DVec2, tolerance: f64)
794794
}
795795

796796
fn get_subpaths<'a>(layer_path: &[LayerId], document: &'a DocumentMessageHandler) -> Option<&'a Vec<Subpath<ManipulatorGroupId>>> {
797-
let layer = document.document_legacy.layer(layer_path).ok().and_then(|layer| layer.as_layer().ok())?;
798-
let network = &layer.network;
799-
for (node, _node_id) in network.primary_flow() {
800-
if node.name == "Path Generator" {
801-
let subpaths_input = node.inputs.get(0)?;
802-
let NodeInput::Value {
803-
tagged_value: TaggedValue::Subpaths(subpaths),
804-
..
805-
} = subpaths_input
806-
else {
807-
continue;
808-
};
809-
810-
return Some(subpaths);
811-
}
797+
if let TaggedValue::Subpaths(subpaths) = NodeGraphLayer::new(layer_path, &document.document_legacy)?.find_input("Path Generator", 0)? {
798+
Some(subpaths)
799+
} else {
800+
None
812801
}
813-
None
814802
}

0 commit comments

Comments
 (0)