mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-29 21:49:17 +00:00
Merge pull request #9791 from dotCooCoo/fix/direct-move-edge-snap
Direct Selection: a path held by its edge lands that point on other paths
This commit is contained in:
+24
-12
@@ -66,7 +66,7 @@ use crate::guides::{HandleSnap, Leave, PointSnap, Targets};
|
||||
use crate::meshedit::{MeshEdit, grid_of};
|
||||
use crate::pathtype::{self, BracketDrag, near_bracket};
|
||||
use crate::rulerguide::GuideEdit;
|
||||
use crate::select::{MoveSnap, hit_center, hold_point, is_area_type, is_path_type, matrix_json, release_guide_at, selection_bounds};
|
||||
use crate::select::{MoveSnap, held_on_path, hit_center, hold_point, is_area_type, is_path_type, matrix_json, release_guide_at, selection_bounds};
|
||||
use crate::{Action, Cursor, Mods, Overlay, PixelOp, PointerEvent, PointerKind, Tool, ToolContext, ToolKey};
|
||||
|
||||
/// An ordinary path's handle drag keeps its owner across previews, which each start at Begin.
|
||||
@@ -103,6 +103,9 @@ enum State {
|
||||
start: Point,
|
||||
grab: Option<Point>,
|
||||
hold: Point,
|
||||
/// `hold` is a point of the path's stroke, outline or edge: it also lands on the other
|
||||
/// art's paths, as with the Selection tool ([`MoveSnap::by_edge`]).
|
||||
edge: bool,
|
||||
began: bool,
|
||||
/// Group Selection: Shift-click on a selected object toggles it off on release.
|
||||
deselect: Option<NodeId>,
|
||||
@@ -1117,7 +1120,9 @@ impl Tool for DirectSelectionTool {
|
||||
));
|
||||
out
|
||||
}
|
||||
(PointerKind::Drag, State::MoveObject { start, grab, hold, began, .. }) => self.drag_move_object(cx, ev, p, (start, grab, hold, began)),
|
||||
(PointerKind::Drag, State::MoveObject { start, grab, hold, edge, began, .. }) => {
|
||||
self.drag_move_object(cx, ev, p, (start, grab, hold, edge, began))
|
||||
}
|
||||
(PointerKind::Drag, State::TypeArea { items, start, began }) => {
|
||||
let mut out = vec![];
|
||||
let on_path = items.first().and_then(|(id, _)| cx.doc.node(*id)).is_some_and(is_path_type);
|
||||
@@ -1455,7 +1460,7 @@ impl DirectSelectionTool {
|
||||
return None;
|
||||
}
|
||||
selected_hidden_at(cx.doc, p, cx.selection_hit_options(), &cx.selection.objects)?;
|
||||
self.state = State::MoveObject { start: p, grab: None, hold: p, began: false, deselect: None };
|
||||
self.state = State::MoveObject { start: p, grab: None, hold: p, edge: false, began: false, deselect: None };
|
||||
Some(vec![])
|
||||
}
|
||||
fn press_group(&mut self, cx: &ToolContext, ev: &PointerEvent, p: Point, tol: f64) -> Vec<Action> {
|
||||
@@ -1495,7 +1500,7 @@ impl DirectSelectionTool {
|
||||
out.push(Action::Exec("select.set".into(), json!({"ids": [target.0]})));
|
||||
}
|
||||
}
|
||||
self.state = State::MoveObject { start: p, grab: None, hold: hold_point(cx, &h, p), began: false, deselect };
|
||||
self.state = Self::move_object(cx, &h, p, deselect);
|
||||
out
|
||||
}
|
||||
fn double_click(&mut self, cx: &ToolContext, ev: &PointerEvent, p: Point) -> Vec<Action> {
|
||||
@@ -1533,7 +1538,7 @@ impl DirectSelectionTool {
|
||||
// Alt picks the whole path (and Alt-drag copies it), as on a segment. On a path
|
||||
// already selected whole, an Alt-click adds the next enclosing group on release.
|
||||
if ev.mods.alt {
|
||||
self.state = State::MoveObject { start: p, grab: Some(grab), hold: grab, began: false, deselect: None };
|
||||
self.state = State::MoveObject { start: p, grab: Some(grab), hold: grab, edge: false, began: false, deselect: None };
|
||||
let whole = cx.selection.contains(id) && cx.selection.partial(id).is_none();
|
||||
if whole && !ev.mods.shift {
|
||||
self.click_group = cx.doc.ancestry(id).and_then(|a| next_group(cx, &a));
|
||||
@@ -1546,7 +1551,7 @@ impl DirectSelectionTool {
|
||||
// Paths all selected whole move as objects, held by this anchor, so Alt can turn
|
||||
// the copy on and off during the drag as with the Selection tool.
|
||||
self.state = if already && cx.selection.anchors.is_empty() {
|
||||
State::MoveObject { start: p, grab: Some(grab), hold: grab, began: false, deselect: None }
|
||||
State::MoveObject { start: p, grab: Some(grab), hold: grab, edge: false, began: false, deselect: None }
|
||||
} else {
|
||||
State::MoveAnchors { start: p, grab, began: false, segment: false }
|
||||
};
|
||||
@@ -1564,7 +1569,7 @@ impl DirectSelectionTool {
|
||||
/// Press on the centre point of object `id`: it selects the object whole (Shift toggles it),
|
||||
/// as a press on its fill does, and a drag moves it held by that point (#4891).
|
||||
fn press_center(&mut self, cx: &ToolContext, ev: &PointerEvent, p: Point, id: NodeId, center: Point) -> Vec<Action> {
|
||||
self.state = State::MoveObject { start: p, grab: None, hold: center, began: false, deselect: None };
|
||||
self.state = State::MoveObject { start: p, grab: None, hold: center, edge: false, began: false, deselect: None };
|
||||
self.frame.clear();
|
||||
if ev.mods.shift {
|
||||
return vec![Action::Exec("select.toggle".into(), json!({"id": id.0}))];
|
||||
@@ -1592,7 +1597,7 @@ impl DirectSelectionTool {
|
||||
// The engine refuses any other object with a status message, and the selection stays,
|
||||
// so a drag from the press does not move it.
|
||||
if symbol_overrides::part(&sym.art, &path).is_some_and(symbol_overrides::takes_part_paint) {
|
||||
self.state = State::MoveObject { start: p, grab: None, hold: hold_point(cx, &h, p), began: false, deselect: None };
|
||||
self.state = Self::move_object(cx, &h, p, None);
|
||||
}
|
||||
if cx.selection.symbol_part.as_ref().is_some_and(|sp| sp.instance == h.leaf && sp.path == path) {
|
||||
return vec![];
|
||||
@@ -1648,7 +1653,7 @@ impl DirectSelectionTool {
|
||||
// Clicking the fill selects the whole leaf path (all anchors). With Alt, a click
|
||||
// on a path already selected whole adds the next enclosing group on release,
|
||||
// as Group Selection's click does; each further Alt-click climbs one level.
|
||||
self.state = State::MoveObject { start: p, grab: None, hold: hold_point(cx, &h, p), began: false, deselect: None };
|
||||
self.state = Self::move_object(cx, &h, p, None);
|
||||
if ev.mods.alt && whole && !ev.mods.shift {
|
||||
self.click_group = next_group(cx, &h.ancestry);
|
||||
}
|
||||
@@ -1756,12 +1761,19 @@ impl DirectSelectionTool {
|
||||
out.push(Action::Preview("path.moveAnchors".into(), json!({"dx": d.x, "dy": d.y})));
|
||||
out
|
||||
}
|
||||
/// A press at `p` on the object hit (`h`) that moves the selection, held where the press took
|
||||
/// hold of it ([`hold_point`]): a point of the path's stroke, outline or edge also lands on
|
||||
/// the other art's paths (#8568).
|
||||
fn move_object(cx: &ToolContext, h: &Hit, p: Point, deselect: Option<NodeId>) -> State {
|
||||
let (hold, edge) = held_on_path(cx, h, p).map_or((p, false), |q| (q, true));
|
||||
State::MoveObject { start: p, grab: None, hold, edge, began: false, deselect }
|
||||
}
|
||||
fn drag_move_object(
|
||||
&mut self,
|
||||
cx: &ToolContext,
|
||||
ev: &PointerEvent,
|
||||
p: Point,
|
||||
(start, grab, hold, began): (Point, Option<Point>, Point, bool),
|
||||
(start, grab, hold, edge, began): (Point, Option<Point>, Point, bool, bool),
|
||||
) -> Vec<Action> {
|
||||
let mut out = vec![];
|
||||
if !began {
|
||||
@@ -1778,7 +1790,7 @@ impl DirectSelectionTool {
|
||||
grid.then(|| MoveSnap::grid_only(vectorcraft_effects::preview::anchor_bounds_with(cx.selection, cx.doc, cx.bounds_measure())));
|
||||
let copy = ev.mods.alt && self.partial_snap.is_none();
|
||||
out.push(Action::Begin(if copy { "Copy".into() } else { "Move".into() }));
|
||||
self.state = State::MoveObject { start, grab, hold, began: true, deselect: None };
|
||||
self.state = State::MoveObject { start, grab, hold, edge, began: true, deselect: None };
|
||||
if grab.is_some() {
|
||||
// Alt can go down or up at any time: gather both while nothing has moved.
|
||||
self.anchor_snap = Some(PointSnap::moving(cx, || Targets::for_anchor_drag(cx.doc, cx.selection)));
|
||||
@@ -1787,7 +1799,7 @@ impl DirectSelectionTool {
|
||||
let back = cx.smart_guides && !cx.snap_to_grid && cx.snap_to_last_location;
|
||||
self.last_location = back.then(|| (Targets::last_location(cx), selection_bounds(cx)));
|
||||
} else {
|
||||
self.move_snap = Some(MoveSnap::new(cx));
|
||||
self.move_snap = Some(if edge { MoveSnap::by_edge(cx) } else { MoveSnap::new(cx) });
|
||||
}
|
||||
}
|
||||
let mut d = move_delta(start, p, ev.mods.shift, cx.constrain_angle);
|
||||
|
||||
@@ -2235,6 +2235,50 @@ fn a_drag_from_a_fill_skips_the_anchors_of_a_path_in_a_selected_group() {
|
||||
assert_eq!((v, labels), (json!({"dx": 248.0, "dy": 251.0}), Vec::<String>::new()));
|
||||
}
|
||||
|
||||
/// #8568: Direct Selection and Group Selection move a path selected whole held by a point of its
|
||||
/// edge (a press on the fill 3 points inside the bottom edge holds (150, 200); with Group
|
||||
/// Selection also a press on that edge), and with Smart Guides that point lands on another path
|
||||
/// ("path"), as with the Selection tool. A press farther inside holds no edge, and the pressed
|
||||
/// point does not land on the path.
|
||||
#[test]
|
||||
fn a_whole_path_held_by_its_edge_lands_on_another_path() {
|
||||
let (a, b) = (Point::new(300.0, 400.0), Point::new(500.0, 300.0));
|
||||
let (mut d, id) = doc_with_rect();
|
||||
let l = d.layers[0].id;
|
||||
let n = d.alloc_id();
|
||||
let line = PathData::single(vectorcraft_geom::SubPath::polyline(&[a, b], false));
|
||||
d.insert(Some(l), 1, Node::path(n, line, vectorcraft_doc::Appearance::default_art())).unwrap();
|
||||
let off_line = |q: Point| ((b - a).cross(q - a) / (b - a).hypot()).abs();
|
||||
let p = paint();
|
||||
let mut whole = Selection::default();
|
||||
whole.add(id);
|
||||
let c = cx(&d, &whole, &p);
|
||||
let moved = |v: &Value| Vec2::new(v["matrix"][4].as_f64().unwrap_or(f64::NAN), v["matrix"][5].as_f64().unwrap_or(f64::NAN));
|
||||
for group in [false, true] {
|
||||
let presses: &[(f64, f64)] = if group { &[(150.0, 197.0), (150.0, 200.0)] } else { &[(150.0, 197.0)] };
|
||||
for &(x, y) in presses {
|
||||
let mut t = DirectSelectionTool::new(group);
|
||||
// The held point (150, 200) is dropped half a point beside the line.
|
||||
let (v, labels) = drag(&mut t, &c, (x, y), (x + 280.0, y + 135.5), Mods::default());
|
||||
let edge = Point::new(150.0, 200.0) + moved(&v);
|
||||
assert!(off_line(edge) < 1e-9, "group {group}, press ({x}, {y}): the edge lands at {edge:?}, {} off the line", off_line(edge));
|
||||
assert_eq!(labels, vec!["path".to_string()], "group {group}, press ({x}, {y})");
|
||||
}
|
||||
if group {
|
||||
// In Outline view, Group Selection holds the path by its outline.
|
||||
let c = ToolContext { outline: true, ..cx(&d, &whole, &p) };
|
||||
let mut t = DirectSelectionTool::new(group);
|
||||
let (v, labels) = drag(&mut t, &c, (150.0, 200.0), (430.0, 335.5), Mods::default());
|
||||
let edge = Point::new(150.0, 200.0) + moved(&v);
|
||||
assert!(off_line(edge) < 1e-9 && labels == ["path"], "outline: the edge lands at {edge:?} {labels:?}");
|
||||
}
|
||||
let mut t = DirectSelectionTool::new(group);
|
||||
let (v, _) = drag(&mut t, &c, (150.0, 190.0), (430.0, 334.5), Mods::default());
|
||||
let pressed = Point::new(150.0, 190.0) + moved(&v);
|
||||
assert!(off_line(pressed) > 0.4, "group {group}: the pressed point lands at {pressed:?}");
|
||||
}
|
||||
}
|
||||
|
||||
/// A dragged anchor lands on another path's anchor or segment, lines up with the other anchors
|
||||
/// of its own path, and with several selected the one pressed on snaps, the others following.
|
||||
#[test]
|
||||
|
||||
@@ -817,7 +817,7 @@ pub(crate) fn hold_point(cx: &ToolContext, h: &Hit, p: Point) -> Point {
|
||||
}
|
||||
|
||||
/// [`hold_point`]'s point of the path hit, None when the press holds no point of it.
|
||||
fn held_on_path(cx: &ToolContext, h: &Hit, p: Point) -> Option<Point> {
|
||||
pub(crate) fn held_on_path(cx: &ToolContext, h: &Hit, p: Point) -> Option<Point> {
|
||||
let reach = if matches!(h.kind, HitKind::Stroke | HitKind::Outline) { f64::INFINITY } else { cx.tol(HOVER_REACH) };
|
||||
cx.doc.node(h.leaf).and_then(|n| n.path_data()).and_then(|pd| hold_on_path(pd, p, reach))
|
||||
}
|
||||
|
||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user