From 97616e80199c74faa07fe008d93323f8bbe53a70 Mon Sep 17 00:00:00 2001 From: James Lindsay <78500760+0HyperCube@users.noreply.github.com> Date: Mon, 8 Jul 2024 01:23:44 +0100 Subject: [PATCH] Fix Pen and Freehand tool path extension (#1809) Improve path tool extend --- .../tool/common_functionality/utility_functions.rs | 2 +- editor/src/messages/tool/tool_messages/freehand_tool.rs | 8 +++++--- editor/src/messages/tool/tool_messages/pen_tool.rs | 8 +++++--- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/editor/src/messages/tool/common_functionality/utility_functions.rs b/editor/src/messages/tool/common_functionality/utility_functions.rs index 51c1dd8e..dbec0b3d 100644 --- a/editor/src/messages/tool/common_functionality/utility_functions.rs +++ b/editor/src/messages/tool/common_functionality/utility_functions.rs @@ -5,7 +5,7 @@ use graphene_std::vector::PointId; use glam::DVec2; -/// Determines if a path should be extended. Returns the path and if it is extending from the start, if applicable. +/// Determines if a path should be extended. Goal in viewport space. Returns the path and if it is extending from the start, if applicable. pub fn should_extend(document: &DocumentMessageHandler, goal: DVec2, tolerance: f64) -> Option<(LayerNodeIdentifier, PointId, DVec2)> { let mut best = None; let mut best_distance_squared = tolerance * tolerance; diff --git a/editor/src/messages/tool/tool_messages/freehand_tool.rs b/editor/src/messages/tool/tool_messages/freehand_tool.rs index c0d72818..b3e44e4e 100644 --- a/editor/src/messages/tool/tool_messages/freehand_tool.rs +++ b/editor/src/messages/tool/tool_messages/freehand_tool.rs @@ -175,7 +175,6 @@ impl ToolTransition for FreehandTool { #[derive(Clone, Debug, Default)] struct FreehandToolData { - extend_from_start: bool, end_point: Option<(DVec2, PointId)>, dragged: bool, weight: f64, @@ -208,12 +207,13 @@ impl Fsm for FreehandToolFsmState { responses.add(DocumentMessage::StartTransaction); tool_data.dragged = false; - tool_data.extend_from_start = false; + tool_data.end_point = None; tool_data.weight = tool_options.line_weight; // Extend an endpoint of the selected path - if let Some((layer, _, position)) = should_extend(document, input.mouse.position, crate::consts::SNAP_POINT_TOLERANCE) { + if let Some((layer, point, position)) = should_extend(document, input.mouse.position, crate::consts::SNAP_POINT_TOLERANCE) { tool_data.layer = Some(layer); + tool_data.end_point = Some((position, point)); extend_path_with_next_segment(tool_data, position, responses); @@ -260,6 +260,7 @@ impl Fsm for FreehandToolFsmState { responses.add(DocumentMessage::DocumentHistoryBackward); } + tool_data.end_point = None; tool_data.layer = None; FreehandToolFsmState::Ready @@ -267,6 +268,7 @@ impl Fsm for FreehandToolFsmState { (FreehandToolFsmState::Drawing, FreehandToolMessage::Abort) => { responses.add(DocumentMessage::AbortTransaction); tool_data.layer = None; + tool_data.end_point = None; FreehandToolFsmState::Ready } diff --git a/editor/src/messages/tool/tool_messages/pen_tool.rs b/editor/src/messages/tool/tool_messages/pen_tool.rs index 9c17ac7d..315ec4a2 100644 --- a/editor/src/messages/tool/tool_messages/pen_tool.rs +++ b/editor/src/messages/tool/tool_messages/pen_tool.rs @@ -508,8 +508,12 @@ impl Fsm for PenToolFsmState { (PenToolFsmState::Ready, PenToolMessage::DragStart) => { responses.add(DocumentMessage::StartTransaction); + let point = SnapCandidatePoint::handle(document.metadata.document_to_viewport.inverse().transform_point2(input.mouse.position)); + let snapped = tool_data.snap_manager.free_snap(&SnapData::new(document, input), &point, None, false); + let viewport = document.metadata.document_to_viewport.transform_point2(snapped.snapped_point_document); + // Perform extension of an existing path - if let Some((layer, point, position)) = should_extend(document, input.mouse.position, crate::consts::SNAP_POINT_TOLERANCE) { + if let Some((layer, point, position)) = should_extend(document, viewport, crate::consts::SNAP_POINT_TOLERANCE) { tool_data.add_point(LastPoint { id: point, pos: position, @@ -535,8 +539,6 @@ impl Fsm for PenToolFsmState { // Generate first point let id = PointId::generate(); let transform = document.metadata().transform_to_document(parent); - let point = SnapCandidatePoint::handle(document.metadata.document_to_viewport.inverse().transform_point2(input.mouse.position)); - let snapped = tool_data.snap_manager.free_snap(&SnapData::new(document, input), &point, None, false); let pos = transform.inverse().transform_point2(snapped.snapped_point_document); let modification_type = VectorModificationType::InsertPoint { id, position: pos }; responses.add(GraphOperationMessage::Vector { layer, modification_type });