Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement extending, joining, and creating new subpaths with the Spline tool #2203

Merged
merged 21 commits into from
Jan 25, 2025
Merged
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
83aa914
visualize spline end points using overlays
indierusty Jan 16, 2025
b74c587
implement for spline tool to extend path by draging end points
indierusty Jan 17, 2025
345e7a7
allow holding Shift to begin drawing a new spline subpath in the same…
indierusty Jan 17, 2025
4d38634
implement spline tool to join two endpoints
indierusty Jan 18, 2025
8889a64
fix naming
indierusty Jan 18, 2025
ccd1552
refactor spline tool
indierusty Jan 19, 2025
7ded22e
impl spline tool snapping and overlays
indierusty Jan 19, 2025
8aba576
fix joining path and refactor
indierusty Jan 20, 2025
3d0764d
improve join_path comment
indierusty Jan 20, 2025
11298ea
fix snapping overlays flickering by ignoring snapping in current layer
indierusty Jan 21, 2025
44cf06e
fix inserting single point on aborting spline tool
indierusty Jan 21, 2025
1f68f93
add snapping for endpoint even when regular snapping is disabled
indierusty Jan 21, 2025
0bc30dd
Merge branch 'master' into spline-tool-editing
Keavon Jan 24, 2025
75cc2e4
fix extending
indierusty Jan 25, 2025
c525dcd
Merge branch 'spline-tool-editing' of github.com:indierusty/Graphite …
indierusty Jan 25, 2025
acd4ec4
Merge branch 'master' into spline-tool-editing
Keavon Jan 25, 2025
662a0bb
fix inserting new point instead of extending and Add hint for Shift t…
indierusty Jan 25, 2025
49334a4
Merge branch 'master' into spline-tool-editing
Keavon Jan 25, 2025
9076dc0
fix grammatical errors and code style
indierusty Jan 25, 2025
31b5ad1
Merge branch 'master' into spline-tool-editing
Keavon Jan 25, 2025
e18eacc
Code review
Keavon Jan 25, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 17 additions & 16 deletions editor/src/messages/tool/tool_messages/spline_tool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use crate::messages::tool::common_functionality::auto_panning::AutoPanning;
use crate::messages::tool::common_functionality::color_selector::{ToolColorOptions, ToolColorType};
use crate::messages::tool::common_functionality::graph_modification_utils;
use crate::messages::tool::common_functionality::snapping::{SnapCandidatePoint, SnapData, SnapManager, SnapTypeConfiguration, SnappedPoint};

use crate::messages::tool::common_functionality::utility_functions::{closest_point, should_extend};

use graph_craft::document::{NodeId, NodeInput};
Expand Down Expand Up @@ -185,7 +184,7 @@ impl ToolTransition for SplineTool {

#[derive(Clone, Debug, Default)]
struct SplineToolData {
/// list of points inserted.
/// List of points inserted.
points: Vec<(PointId, DVec2)>,
/// Point to be inserted.
next_point: DVec2,
Expand All @@ -209,7 +208,7 @@ impl SplineToolData {
self.points = Vec::new();
}

/// get snapped point but ignoring current layer
/// Get the snapped point while ignoring current layer
fn snapped_point(&mut self, document: &DocumentMessageHandler, input: &InputPreprocessorMessageHandler) -> SnappedPoint {
let point = SnapCandidatePoint::handle(document.metadata().document_to_viewport.inverse().transform_point2(input.mouse.position));
let ignore = if let Some(layer) = self.layer { vec![layer] } else { vec![] };
Expand All @@ -232,7 +231,9 @@ impl Fsm for SplineToolFsmState {
..
} = tool_action_data;

let ToolMessage::Spline(event) = event else { return self };
let ToolMessage::Spline(event) = event else {
return self;
Keavon marked this conversation as resolved.
Show resolved Hide resolved
};
match (self, event) {
(_, SplineToolMessage::CanvasTransformed) => self,
(_, SplineToolMessage::Overlays(mut overlay_context)) => {
Expand All @@ -255,7 +256,6 @@ impl Fsm for SplineToolFsmState {
if let Some((layer, point, position)) = should_extend(document, viewport, SNAP_POINT_TOLERANCE, selected_nodes.selected_layers(document.metadata())) {
tool_data.layer = Some(layer);
tool_data.points.push((point, position));
// update next point to preview current mouse pos instead of pointing last mouse pos when DragStop event occured.
tool_data.next_point = position;
tool_data.extend = true;

Expand Down Expand Up @@ -299,7 +299,7 @@ impl Fsm for SplineToolFsmState {
SplineToolFsmState::Drawing
}
(SplineToolFsmState::Drawing, SplineToolMessage::DragStop) => {
// if extending ignore first DragStop event to avoid inserting new point.
// The first DragStop event will be ignored to prevent insertion of new point.
if tool_data.extend {
tool_data.extend = false;
return SplineToolFsmState::Drawing;
Expand All @@ -325,7 +325,7 @@ impl Fsm for SplineToolFsmState {
let ignore = |cp: PointId| tool_data.preview_point.is_some_and(|pp| pp == cp) || tool_data.points.last().is_some_and(|(ep, _)| *ep == cp);
let join_point = closest_point(document, input.mouse.position, PATH_JOIN_THRESHOLD, vec![layer].into_iter(), ignore);

// endpoints snapping
// Endpoints snapping
if let Some((_, _, point)) = join_point {
tool_data.next_point = point;
tool_data.snap_manager.clear_indicator();
Expand Down Expand Up @@ -408,25 +408,26 @@ impl Fsm for SplineToolFsmState {

/// Return `true` only if new segment is inserted to connect two end points in the selected layer otherwise `false`.
fn join_path(document: &DocumentMessageHandler, mouse_pos: DVec2, tool_data: &mut SplineToolData, responses: &mut VecDeque<Message>) -> bool {
let Some((endpoint, _)) = tool_data.points.last().map(|p| *p) else {
let Some((end_point, _)) = tool_data.points.last().map(|p| *p) else {
Keavon marked this conversation as resolved.
Show resolved Hide resolved
return false;
};
// use preview_point to get current dragging position.

let preview_point = tool_data.preview_point;
let selected_nodes = document.network_interface.selected_nodes(&[]).unwrap();
let selected_layers = selected_nodes.selected_layers(document.metadata());
// get the closest point to mouse position which is not preview_point or end_point.
let Some((layer, point, _)) = closest_point(document, mouse_pos, PATH_JOIN_THRESHOLD, selected_layers, |cp| {
preview_point.is_some_and(|pp| pp == cp) || cp == endpoint
}) else {

// Get the closest point to mouse position which is not preview_point or end_point.
let closest_point = closest_point(document, mouse_pos, PATH_JOIN_THRESHOLD, selected_layers, |cp| {
preview_point.is_some_and(|pp| pp == cp) || cp == end_point
});
let Some((layer, join_point, _)) = closest_point else {
return false;
};

// NOTE: deleting preview point before joining two endponts because
// last point inserted could be preview point and segment which is after the endpoint
// Last end point inserted was the preview point and segment therefore we delete it before joining the end_point & join_point.
delete_preview(tool_data, responses);

let points = [endpoint, point];
let points = [end_point, join_point];
let id = SegmentId::generate();
let modification_type = VectorModificationType::InsertSegment { id, points, handles: [None, None] };
responses.add(GraphOperationMessage::Vector { layer, modification_type });
Expand Down
Loading