From 82003901c765592421b7898f7d0293b51c053c90 Mon Sep 17 00:00:00 2001 From: James Ryans Date: Sat, 16 Nov 2024 10:18:56 +0700 Subject: [PATCH 1/5] place new layer from basic tools within an artboard bounds Signed-off-by: James Ryans --- .../portfolio/document/document_message_handler.rs | 13 +++++++++++++ .../src/messages/tool/tool_messages/ellipse_tool.rs | 2 +- editor/src/messages/tool/tool_messages/line_tool.rs | 3 ++- editor/src/messages/tool/tool_messages/pen_tool.rs | 2 +- .../src/messages/tool/tool_messages/polygon_tool.rs | 2 +- .../messages/tool/tool_messages/rectangle_tool.rs | 2 +- 6 files changed, 19 insertions(+), 5 deletions(-) diff --git a/editor/src/messages/portfolio/document/document_message_handler.rs b/editor/src/messages/portfolio/document/document_message_handler.rs index 502438fc3b..59dbfae8c3 100644 --- a/editor/src/messages/portfolio/document/document_message_handler.rs +++ b/editor/src/messages/portfolio/document/document_message_handler.rs @@ -1652,6 +1652,19 @@ impl DocumentMessageHandler { } } + /// Finds the artboard that bounds the point in viewport space and be the container of any newly added layers. + pub fn new_layer_bounding_artboard(&self, point: DVec2) -> LayerNodeIdentifier { + self.network_interface + .all_artboards() + .iter() + .find(|&artboard| { + let bounds = self.metadata().bounding_box_viewport(*artboard).unwrap(); + bounds[0].x <= point.x && point.x <= bounds[1].x && bounds[0].y <= point.y && point.y <= bounds[1].y + }) + .copied() + .unwrap_or(LayerNodeIdentifier::ROOT_PARENT) + } + /// Finds the parent folder which, based on the current selections, should be the container of any newly added layers. pub fn new_layer_parent(&self, include_self: bool) -> LayerNodeIdentifier { self.network_interface diff --git a/editor/src/messages/tool/tool_messages/ellipse_tool.rs b/editor/src/messages/tool/tool_messages/ellipse_tool.rs index 04a9778f2a..b6352de97a 100644 --- a/editor/src/messages/tool/tool_messages/ellipse_tool.rs +++ b/editor/src/messages/tool/tool_messages/ellipse_tool.rs @@ -205,7 +205,7 @@ impl Fsm for EllipseToolFsmState { let node = node_type.node_template_input_override([None, Some(NodeInput::value(TaggedValue::F64(0.5), false)), Some(NodeInput::value(TaggedValue::F64(0.5), false))]); let nodes = vec![(NodeId(0), node)]; - let layer = graph_modification_utils::new_custom(NodeId::new(), nodes, document.new_layer_parent(true), responses); + let layer = graph_modification_utils::new_custom(NodeId::new(), nodes, document.new_layer_bounding_artboard(shape_data.viewport_drag_start(document)), responses); responses.add(Message::StartBuffer); responses.add(GraphOperationMessage::TransformSet { layer, diff --git a/editor/src/messages/tool/tool_messages/line_tool.rs b/editor/src/messages/tool/tool_messages/line_tool.rs index 70a777e863..62b9fcdd50 100644 --- a/editor/src/messages/tool/tool_messages/line_tool.rs +++ b/editor/src/messages/tool/tool_messages/line_tool.rs @@ -186,7 +186,8 @@ impl Fsm for LineToolFsmState { ]); let nodes = vec![(NodeId(0), node)]; - let layer = graph_modification_utils::new_custom(NodeId::new(), nodes, document.new_layer_parent(false), responses); + let viewport = document.metadata().document_to_viewport.transform_point2(snapped.snapped_point_document); + let layer = graph_modification_utils::new_custom(NodeId::new(), nodes, document.new_layer_bounding_artboard(viewport), responses); responses.add(Message::StartBuffer); responses.add(GraphOperationMessage::TransformSet { layer, diff --git a/editor/src/messages/tool/tool_messages/pen_tool.rs b/editor/src/messages/tool/tool_messages/pen_tool.rs index 7e4c1fd814..e8d7faf3a4 100644 --- a/editor/src/messages/tool/tool_messages/pen_tool.rs +++ b/editor/src/messages/tool/tool_messages/pen_tool.rs @@ -492,7 +492,7 @@ impl PenToolData { let node_type = resolve_document_node_type("Path").expect("Path node does not exist"); let nodes = vec![(NodeId(0), node_type.default_node_template())]; - let parent = document.new_layer_parent(true); + let parent = document.new_layer_bounding_artboard(viewport); let layer = graph_modification_utils::new_custom(NodeId::new(), nodes, parent, responses); tool_options.fill.apply_fill(layer, responses); tool_options.stroke.apply_stroke(tool_options.line_weight, layer, responses); diff --git a/editor/src/messages/tool/tool_messages/polygon_tool.rs b/editor/src/messages/tool/tool_messages/polygon_tool.rs index aa45b56637..d59951cde3 100644 --- a/editor/src/messages/tool/tool_messages/polygon_tool.rs +++ b/editor/src/messages/tool/tool_messages/polygon_tool.rs @@ -264,7 +264,7 @@ impl Fsm for PolygonToolFsmState { let nodes = vec![(NodeId(0), node)]; - let layer = graph_modification_utils::new_custom(NodeId::new(), nodes, document.new_layer_parent(false), responses); + let layer = graph_modification_utils::new_custom(NodeId::new(), nodes, document.new_layer_bounding_artboard(polygon_data.viewport_drag_start(document)), responses); responses.add(Message::StartBuffer); responses.add(GraphOperationMessage::TransformSet { layer, diff --git a/editor/src/messages/tool/tool_messages/rectangle_tool.rs b/editor/src/messages/tool/tool_messages/rectangle_tool.rs index 82eb3ecdca..686d1351f1 100644 --- a/editor/src/messages/tool/tool_messages/rectangle_tool.rs +++ b/editor/src/messages/tool/tool_messages/rectangle_tool.rs @@ -211,7 +211,7 @@ impl Fsm for RectangleToolFsmState { let node = node_type.node_template_input_override([None, Some(NodeInput::value(TaggedValue::F64(1.), false)), Some(NodeInput::value(TaggedValue::F64(1.), false))]); let nodes = vec![(NodeId(0), node)]; - let layer = graph_modification_utils::new_custom(NodeId::new(), nodes, document.new_layer_parent(true), responses); + let layer = graph_modification_utils::new_custom(NodeId::new(), nodes, document.new_layer_bounding_artboard(shape_data.viewport_drag_start(document)), responses); responses.add(Message::StartBuffer); responses.add(GraphOperationMessage::TransformSet { layer, From 79b676c6d23b42406ed352c30db57879309d8635 Mon Sep 17 00:00:00 2001 From: James Ryans Date: Sun, 17 Nov 2024 01:08:43 +0700 Subject: [PATCH 2/5] add for Text Tool Signed-off-by: James Ryans --- editor/src/messages/tool/tool_messages/text_tool.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/editor/src/messages/tool/tool_messages/text_tool.rs b/editor/src/messages/tool/tool_messages/text_tool.rs index 78d90a7b3c..c085bc4896 100644 --- a/editor/src/messages/tool/tool_messages/text_tool.rs +++ b/editor/src/messages/tool/tool_messages/text_tool.rs @@ -328,7 +328,7 @@ impl TextToolData { size: editing_text.font_size, line_height_ratio: editing_text.line_height_ratio, character_spacing: editing_text.character_spacing, - parent: document.new_layer_parent(true), + parent: document.new_layer_bounding_artboard(input.mouse.position), insert_index: 0, }); responses.add(Message::StartBuffer); From 235f8c36c73381adfd8cffa2cfd400197c4d1f22 Mon Sep 17 00:00:00 2001 From: James Ryans Date: Mon, 25 Nov 2024 21:42:39 +0700 Subject: [PATCH 3/5] use click_xray function Signed-off-by: James Ryans --- .../portfolio/document/document_message_handler.rs | 13 ++++--------- .../src/messages/tool/tool_messages/ellipse_tool.rs | 2 +- editor/src/messages/tool/tool_messages/line_tool.rs | 3 +-- editor/src/messages/tool/tool_messages/pen_tool.rs | 2 +- .../src/messages/tool/tool_messages/polygon_tool.rs | 2 +- .../messages/tool/tool_messages/rectangle_tool.rs | 2 +- editor/src/messages/tool/tool_messages/text_tool.rs | 2 +- 7 files changed, 10 insertions(+), 16 deletions(-) diff --git a/editor/src/messages/portfolio/document/document_message_handler.rs b/editor/src/messages/portfolio/document/document_message_handler.rs index 59dbfae8c3..9131341cee 100644 --- a/editor/src/messages/portfolio/document/document_message_handler.rs +++ b/editor/src/messages/portfolio/document/document_message_handler.rs @@ -1653,15 +1653,10 @@ impl DocumentMessageHandler { } /// Finds the artboard that bounds the point in viewport space and be the container of any newly added layers. - pub fn new_layer_bounding_artboard(&self, point: DVec2) -> LayerNodeIdentifier { - self.network_interface - .all_artboards() - .iter() - .find(|&artboard| { - let bounds = self.metadata().bounding_box_viewport(*artboard).unwrap(); - bounds[0].x <= point.x && point.x <= bounds[1].x && bounds[0].y <= point.y && point.y <= bounds[1].y - }) - .copied() + pub fn new_layer_bounding_artboard(&self, ipp: &InputPreprocessorMessageHandler) -> LayerNodeIdentifier { + self.click_xray(ipp) + .filter(|layer| self.network_interface.is_artboard(&layer.to_node(), &[])) + .next() .unwrap_or(LayerNodeIdentifier::ROOT_PARENT) } diff --git a/editor/src/messages/tool/tool_messages/ellipse_tool.rs b/editor/src/messages/tool/tool_messages/ellipse_tool.rs index b6352de97a..801563a9c0 100644 --- a/editor/src/messages/tool/tool_messages/ellipse_tool.rs +++ b/editor/src/messages/tool/tool_messages/ellipse_tool.rs @@ -205,7 +205,7 @@ impl Fsm for EllipseToolFsmState { let node = node_type.node_template_input_override([None, Some(NodeInput::value(TaggedValue::F64(0.5), false)), Some(NodeInput::value(TaggedValue::F64(0.5), false))]); let nodes = vec![(NodeId(0), node)]; - let layer = graph_modification_utils::new_custom(NodeId::new(), nodes, document.new_layer_bounding_artboard(shape_data.viewport_drag_start(document)), responses); + let layer = graph_modification_utils::new_custom(NodeId::new(), nodes, document.new_layer_bounding_artboard(input), responses); responses.add(Message::StartBuffer); responses.add(GraphOperationMessage::TransformSet { layer, diff --git a/editor/src/messages/tool/tool_messages/line_tool.rs b/editor/src/messages/tool/tool_messages/line_tool.rs index 62b9fcdd50..437820d676 100644 --- a/editor/src/messages/tool/tool_messages/line_tool.rs +++ b/editor/src/messages/tool/tool_messages/line_tool.rs @@ -186,8 +186,7 @@ impl Fsm for LineToolFsmState { ]); let nodes = vec![(NodeId(0), node)]; - let viewport = document.metadata().document_to_viewport.transform_point2(snapped.snapped_point_document); - let layer = graph_modification_utils::new_custom(NodeId::new(), nodes, document.new_layer_bounding_artboard(viewport), responses); + let layer = graph_modification_utils::new_custom(NodeId::new(), nodes, document.new_layer_bounding_artboard(input), responses); responses.add(Message::StartBuffer); responses.add(GraphOperationMessage::TransformSet { layer, diff --git a/editor/src/messages/tool/tool_messages/pen_tool.rs b/editor/src/messages/tool/tool_messages/pen_tool.rs index e8d7faf3a4..e35ed2b0a9 100644 --- a/editor/src/messages/tool/tool_messages/pen_tool.rs +++ b/editor/src/messages/tool/tool_messages/pen_tool.rs @@ -492,7 +492,7 @@ impl PenToolData { let node_type = resolve_document_node_type("Path").expect("Path node does not exist"); let nodes = vec![(NodeId(0), node_type.default_node_template())]; - let parent = document.new_layer_bounding_artboard(viewport); + let parent = document.new_layer_bounding_artboard(input); let layer = graph_modification_utils::new_custom(NodeId::new(), nodes, parent, responses); tool_options.fill.apply_fill(layer, responses); tool_options.stroke.apply_stroke(tool_options.line_weight, layer, responses); diff --git a/editor/src/messages/tool/tool_messages/polygon_tool.rs b/editor/src/messages/tool/tool_messages/polygon_tool.rs index d59951cde3..e35f5ab990 100644 --- a/editor/src/messages/tool/tool_messages/polygon_tool.rs +++ b/editor/src/messages/tool/tool_messages/polygon_tool.rs @@ -264,7 +264,7 @@ impl Fsm for PolygonToolFsmState { let nodes = vec![(NodeId(0), node)]; - let layer = graph_modification_utils::new_custom(NodeId::new(), nodes, document.new_layer_bounding_artboard(polygon_data.viewport_drag_start(document)), responses); + let layer = graph_modification_utils::new_custom(NodeId::new(), nodes, document.new_layer_bounding_artboard(input), responses); responses.add(Message::StartBuffer); responses.add(GraphOperationMessage::TransformSet { layer, diff --git a/editor/src/messages/tool/tool_messages/rectangle_tool.rs b/editor/src/messages/tool/tool_messages/rectangle_tool.rs index 686d1351f1..fd36a0fceb 100644 --- a/editor/src/messages/tool/tool_messages/rectangle_tool.rs +++ b/editor/src/messages/tool/tool_messages/rectangle_tool.rs @@ -211,7 +211,7 @@ impl Fsm for RectangleToolFsmState { let node = node_type.node_template_input_override([None, Some(NodeInput::value(TaggedValue::F64(1.), false)), Some(NodeInput::value(TaggedValue::F64(1.), false))]); let nodes = vec![(NodeId(0), node)]; - let layer = graph_modification_utils::new_custom(NodeId::new(), nodes, document.new_layer_bounding_artboard(shape_data.viewport_drag_start(document)), responses); + let layer = graph_modification_utils::new_custom(NodeId::new(), nodes, document.new_layer_bounding_artboard(input), responses); responses.add(Message::StartBuffer); responses.add(GraphOperationMessage::TransformSet { layer, diff --git a/editor/src/messages/tool/tool_messages/text_tool.rs b/editor/src/messages/tool/tool_messages/text_tool.rs index c085bc4896..9d5978e8aa 100644 --- a/editor/src/messages/tool/tool_messages/text_tool.rs +++ b/editor/src/messages/tool/tool_messages/text_tool.rs @@ -328,7 +328,7 @@ impl TextToolData { size: editing_text.font_size, line_height_ratio: editing_text.line_height_ratio, character_spacing: editing_text.character_spacing, - parent: document.new_layer_bounding_artboard(input.mouse.position), + parent: document.new_layer_bounding_artboard(input), insert_index: 0, }); responses.add(Message::StartBuffer); From d6441d8d22d1e5b88033b037f8d9dacb62e91829 Mon Sep 17 00:00:00 2001 From: James Ryans Date: Mon, 25 Nov 2024 21:53:03 +0700 Subject: [PATCH 4/5] support for freehand tool Signed-off-by: James Ryans --- editor/src/messages/tool/tool_messages/freehand_tool.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/editor/src/messages/tool/tool_messages/freehand_tool.rs b/editor/src/messages/tool/tool_messages/freehand_tool.rs index 76d48ab170..7f31bcec3e 100644 --- a/editor/src/messages/tool/tool_messages/freehand_tool.rs +++ b/editor/src/messages/tool/tool_messages/freehand_tool.rs @@ -238,7 +238,7 @@ impl Fsm for FreehandToolFsmState { responses.add(DocumentMessage::DeselectAllLayers); - let parent = document.new_layer_parent(true); + let parent = document.new_layer_bounding_artboard(input); let node_type = resolve_document_node_type("Path").expect("Path node does not exist"); let node = node_type.default_node_template(); From 3bcefafe426f84381a435a7b239d3f9a9f995e8e Mon Sep 17 00:00:00 2001 From: James Ryans Date: Mon, 25 Nov 2024 22:00:26 +0700 Subject: [PATCH 5/5] support spline tool Signed-off-by: James Ryans --- editor/src/messages/tool/tool_messages/spline_tool.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/editor/src/messages/tool/tool_messages/spline_tool.rs b/editor/src/messages/tool/tool_messages/spline_tool.rs index 05dfb09abf..87acb17196 100644 --- a/editor/src/messages/tool/tool_messages/spline_tool.rs +++ b/editor/src/messages/tool/tool_messages/spline_tool.rs @@ -203,7 +203,7 @@ impl Fsm for SplineToolFsmState { responses.add(DocumentMessage::StartTransaction); responses.add(DocumentMessage::DeselectAllLayers); - let parent = document.new_layer_parent(true); + let parent = document.new_layer_bounding_artboard(input); tool_data.weight = tool_options.line_weight;