diff --git a/crates/tools/src/xform/transform.rs b/crates/tools/src/xform/transform.rs index 3bd8df6cec..ea74d548bb 100644 --- a/crates/tools/src/xform/transform.rs +++ b/crates/tools/src/xform/transform.rs @@ -14,6 +14,11 @@ //! With the Reflect tool, a click also sets the first point of an axis, and a second click //! reflects the selection across the line through both points (Alt on it reflects a copy); a //! drag from there draws the axis from that first point. +//! +//! A drag that starts on the reference point moves the point instead of transforming the +//! selection (#11020): the pointer shows a move cursor over it, the dragged point snaps as a drawn +//! point does (the selection's own anchors and center included), and the release sets the point +//! there without changing the document. use std::collections::{BTreeMap, BTreeSet}; @@ -93,9 +98,19 @@ struct Drag { /// The document revision at the press: a press whose document or selection changes before it /// becomes a drag is dropped, so its reference point is never applied to other objects. revision: (u64, u64), + /// The press was on the reference point: a drag moves the point and transforms nothing. + pivot: bool, } +/// How near (in screen pixels) a press must be to the reference point to drag the point itself. +const GRAB_REFERENCE_POINT: f64 = 6.0; + impl Drag { + /// A drag that transforms the selection has begun, so ending it early cancels an interaction. + fn transforming(&self) -> bool { + self.began && !self.pivot + } + /// The angle Scale and Reflect act along, for a drag that copies (`copy`) or not, and that /// transforms only pattern tiles (`patterns_only`, the backquote key) or not. fn angle_for(&self, copy: bool, patterns_only: bool) -> f64 { @@ -365,6 +380,11 @@ impl TransformTool { fn dialog(&self, o: Point) -> Action { Action::Dialog(self.kind.id().into(), self.kind.dialog_fields(o)) } + + /// Is `p` on the reference point, close enough for a press there to drag the point? + fn on_reference_point(&self, cx: &ToolContext, p: Point) -> bool { + self.reference_point(cx).is_some_and(|r| p.distance(r) <= cx.tol(GRAB_REFERENCE_POINT)) + } } impl Tool for TransformTool { @@ -391,6 +411,7 @@ impl Tool for TransformTool { // transforms a copy about the reference point as it was. With a Reflect axis waiting // for its second point, a drag draws the axis from its first point. self.axis_to = None; + let pivot = self.on_reference_point(cx, p); let origin = self.axis_first(cx).or_else(|| self.reference_point(cx)).unwrap_or(bounds.center()); let anchors = vectorcraft_effects::preview::anchor_bounds_with(cx.selection, cx.doc, cx.bounds_measure()).is_some(); let angle = selection_box(cx).map_or(0.0, |b| b.angle); @@ -408,6 +429,7 @@ impl Tool for TransformTool { moved: Vec2::ZERO, last: Affine::IDENTITY, revision: cx.revision, + pivot, }); vec![] } @@ -425,12 +447,26 @@ impl Tool for TransformTool { return out; } d.began = true; - // The selection moves, so only the other objects are targets. The Scale tool's - // drag scales; the others move the art about the reference point. - let targets = || Targets::collect(cx.doc, &cx.selection.objects, None); - self.snap = - Some(if self.kind == TransformKind::Scale { PointSnap::scaling(cx, targets) } else { PointSnap::moving(cx, targets) }); - out.push(Action::Begin(self.kind.label().into())); + if d.pivot { + // The selection stays where it is, so its own anchors and center are + // targets for the point too. + self.snap = Some(PointSnap::new(cx, || Targets::collect(cx.doc, &[], None))); + } else { + // The selection moves, so only the other objects are targets. The Scale tool's + // drag scales; the others move the art about the reference point. + let targets = || Targets::collect(cx.doc, &cx.selection.objects, None); + self.snap = + Some(if self.kind == TransformKind::Scale { PointSnap::scaling(cx, targets) } else { PointSnap::moving(cx, targets) }); + out.push(Action::Begin(self.kind.label().into())); + } + } + if d.pivot { + // Dragging the reference point moves it and records nothing in the document. + let (p, guides) = self.snap.as_ref().map_or((p, vec![]), |s| s.snap(cx, p)); + self.hover = guides; + d.pointer = p; + self.drag = Some(d); + return out; } let moved = p - d.start; let (p, guides) = self.snap.as_ref().map_or((p, vec![]), |s| s.snap(cx, p)); @@ -449,7 +485,15 @@ impl Tool for TransformTool { } PointerKind::Up => { let Some(d) = self.end_drag() else { return vec![] }; - if d.began { + if d.began && d.pivot { + // The reference point dragged to a new place, for the selection as it is. A + // Reflect axis waiting from the old point is dropped, as a new point drops it. + if d.revision == cx.revision { + self.origin = Some((d.pointer, selected_ids(cx))); + self.drop_axis(); + } + vec![] + } else if d.began { // The Alt and backquote states at release decide whether the result is a copy // or moves only pattern tiles, and with anchors selected, whether it acts // along the box or the page. @@ -530,10 +574,11 @@ impl Tool for TransformTool { fn key(&mut self, cx: &ToolContext, key: ToolKey, _mods: Mods) -> Vec { match key { // Escape cancels a drag, and drops the Reflect axis the drag was drawing from too. + // Escape while dragging the reference point leaves the point where it was. ToolKey::Escape if self.busy() => { - self.end_drag(); + let transforming = self.end_drag().is_some_and(|d| d.transforming()); self.drop_axis(); - vec![Action::Cancel] + if transforming { vec![Action::Cancel] } else { vec![] } } // Escape drops a Reflect axis waiting for its second point, and a press not yet // released with it, so the release sets no point; the reference point stays. @@ -560,12 +605,13 @@ impl Tool for TransformTool { /// dropped too, as with Escape. fn drop_press(&mut self, _cx: &ToolContext) -> Vec { self.drop_axis(); - if self.end_drag().is_some_and(|d| d.began) { vec![Action::Cancel] } else { vec![] } + if self.end_drag().is_some_and(|d| d.transforming()) { vec![Action::Cancel] } else { vec![] } } fn overlays(&self, cx: &ToolContext) -> Vec { let mut o = vec![]; let reference = match self.drag { + Some(d) if d.began && d.pivot => Some(d.pointer), Some(d) => Some(d.origin), None => self.reference_point(cx), }; @@ -592,10 +638,18 @@ impl Tool for TransformTool { o } - fn cursor(&self, cx: &ToolContext, _p: Point, _m: Mods) -> Cursor { + fn cursor(&self, cx: &ToolContext, p: Point, _m: Mods) -> Cursor { if !self.has_target(cx) { return Cursor::NotAllowed; } + // Over the reference point, and while dragging it: a drag there moves the point. + let moving_point = match self.drag { + Some(d) => d.pivot, + None => self.on_reference_point(cx, p), + }; + if moving_point { + return Cursor::Move; + } match self.kind { TransformKind::Rotate if self.busy() => Cursor::Rotate, _ => Cursor::Crosshair, @@ -637,7 +691,7 @@ impl Tool for TransformTool { fn deactivate(&mut self, _cx: &ToolContext) -> Vec { self.drop_axis(); - if self.end_drag().is_some_and(|d| d.began) { vec![Action::Cancel] } else { vec![] } + if self.end_drag().is_some_and(|d| d.transforming()) { vec![Action::Cancel] } else { vec![] } } } @@ -773,6 +827,85 @@ mod tests { assert!((m * Point::new(200.0, 200.0)).distance(Point::new(300.0, 250.0)) < 1e-9); } + /// A drag that starts on the reference point moves the point (#11020): the pointer shows the + /// move cursor there, the drag changes nothing in the document, the point snaps onto the + /// selection's anchor, and the next drag elsewhere transforms about the moved point as one + /// undo step. + #[test] + fn dragging_the_reference_point_moves_it_and_the_next_drag_turns_about_it() { + let (d, id) = doc_with_rect(); + let mut s = Selection::default(); + s.add(id); + let p = paint(); + let cx = cx(&d, &s, &p); + let none = Mods::default(); + for kind in [TransformKind::Rotate, TransformKind::Reflect, TransformKind::Scale, TransformKind::Shear] { + let mut t = TransformTool::new(kind); + assert_eq!(t.cursor(&cx, Point::new(151.0, 149.0), none), Cursor::Move, "{kind:?}"); + assert_ne!(t.cursor(&cx, Point::new(250.0, 150.0), none), Cursor::Move, "{kind:?}"); + assert!(t.pointer(&cx, &ev(PointerKind::Down, 151.0, 149.0)).is_empty(), "{kind:?}"); + // Near the bottom-right corner: the point snaps onto it, and nothing is transformed. + assert!(t.pointer(&cx, &ev(PointerKind::Drag, 198.0, 203.0)).is_empty(), "{kind:?}"); + assert_eq!(t.cursor(&cx, Point::new(198.0, 203.0), none), Cursor::Move, "{kind:?}"); + let target = target_overlays(Point::new(200.0, 200.0), cx.tol(8.0)); + assert!(target.iter().all(|o| t.overlays(&cx).contains(o)), "{kind:?}: {:?}", t.overlays(&cx)); + assert!(t.pointer(&cx, &ev(PointerKind::Up, 198.0, 203.0)).is_empty(), "{kind:?}"); + assert_eq!(t.reference_point(&cx), Some(Point::new(200.0, 200.0)), "{kind:?}"); + } + // A Rotate drag from the top-right corner now turns about the bottom-right one. + let mut t = TransformTool::new(TransformKind::Rotate); + t.pointer(&cx, &ev(PointerKind::Down, 150.0, 150.0)); + t.pointer(&cx, &ev(PointerKind::Drag, 200.0, 200.0)); + t.pointer(&cx, &ev(PointerKind::Up, 200.0, 200.0)); + assert!(t.pointer(&cx, &ev(PointerKind::Down, 200.0, 100.0)).is_empty()); + let a = t.pointer(&cx, &ev(PointerKind::Drag, 100.0, 200.0)); + assert_eq!(a[0], Action::Begin("Rotate".into())); + let m = matrix(&a[1]); + assert!((m * Point::new(200.0, 200.0)).distance(Point::new(200.0, 200.0)) < 1e-9, "{m:?}"); + assert!((m * Point::new(200.0, 100.0)).distance(Point::new(100.0, 200.0)) < 1e-9, "{m:?}"); + let up = t.pointer(&cx, &ev(PointerKind::Up, 100.0, 200.0)); + assert!(matches!(up.as_slice(), [Action::Preview(..), Action::Commit]), "{up:?}"); + } + + /// With snapping off the dragged reference point lands where the pointer leaves it. Escape, or + /// a press the engine drops, ends the drag with the point where it was and nothing to cancel, + /// and a Reflect axis waiting from the old point is dropped when the point moves. + #[test] + fn a_dragged_reference_point_lands_unsnapped_and_escape_keeps_the_old_one() { + let (d, id) = doc_with_rect(); + let mut s = Selection::default(); + s.add(id); + let p = paint(); + let mut c = cx(&d, &s, &p); + (c.smart_guides, c.snap_to_point) = (false, false); + let none = Mods::default(); + let center = Point::new(150.0, 150.0); + let mut t = TransformTool::new(TransformKind::Rotate); + t.pointer(&c, &ev(PointerKind::Down, 150.0, 150.0)); + assert!(t.pointer(&c, &ev(PointerKind::Drag, 173.0, 121.0)).is_empty()); + assert!(t.key(&c, ToolKey::Escape, none).is_empty()); + assert!(t.pointer(&c, &ev(PointerKind::Up, 173.0, 121.0)).is_empty()); + assert_eq!(t.reference_point(&c), Some(center)); + t.pointer(&c, &ev(PointerKind::Down, 150.0, 150.0)); + t.pointer(&c, &ev(PointerKind::Drag, 173.0, 121.0)); + assert!(t.drop_press(&c).is_empty()); + assert!(t.pointer(&c, &ev(PointerKind::Up, 173.0, 121.0)).is_empty()); + assert_eq!(t.reference_point(&c), Some(center)); + t.pointer(&c, &ev(PointerKind::Down, 150.0, 150.0)); + t.pointer(&c, &ev(PointerKind::Drag, 173.0, 121.0)); + assert!(t.pointer(&c, &ev(PointerKind::Up, 173.0, 121.0)).is_empty()); + assert_eq!(t.reference_point(&c), Some(Point::new(173.0, 121.0))); + + let mut t = TransformTool::new(TransformKind::Reflect); + assert!(click(&mut t, &c, 100.0, 100.0, none).is_empty()); + assert!(t.ends_on_escape(&c)); + t.pointer(&c, &ev(PointerKind::Down, 100.0, 100.0)); + assert!(t.pointer(&c, &ev(PointerKind::Drag, 120.0, 180.0)).is_empty()); + assert!(t.pointer(&c, &ev(PointerKind::Up, 120.0, 180.0)).is_empty()); + assert!(!t.ends_on_escape(&c)); + assert_eq!(t.reference_point(&c), Some(Point::new(120.0, 180.0))); + } + #[test] fn scale_shift_is_uniform() { let t = TransformTool::new(TransformKind::Scale); diff --git a/docs/ui-parity.md b/docs/ui-parity.md index 30076872df..7a60beba1c 100644 --- a/docs/ui-parity.md +++ b/docs/ui-parity.md @@ -347,7 +347,7 @@ Every parity table on this page has a Headless column: the Summary table (12 are | Vector Halftone Preview keeps the selection box and hides selection marks on the temporary dots | done | #6369; Preview off and Cancel restore the original marks. | unknown | | Free Transform, Scale and Reflect along a rotated box | done | The Scale and Reflect tools, dialogs and commands and the Transform and Properties panels' flips act along the turned box until Reset Bounding Box; `basis: "page"` keeps the page's axes | unknown | | Free Transform widget: constrain, free distort, perspective distort; the key-held variants | done | | unknown | -| Transform tools: click sets the reference point (snaps to anchors/centres), Alt-click opens the dialog, Alt-release copies | done | with anchors selected with Direct Selection, the tools, the dialogs and Move transform those anchors alone, about the centre of their box by default (#1408; the default point and whole-object copies are our choices) | unknown | +| Transform tools: click sets the reference point (snaps to anchors/centres), Alt-click opens the dialog, Alt-release copies | done | with anchors selected with Direct Selection, the tools, the dialogs and Move transform those anchors alone, about the centre of their box by default (#1408; the default point and whole-object copies are our choices). A drag that starts on the reference point moves the point, with the move cursor over it, snapping as a drawn point does, and changes nothing in the document (#11020; VectorCraft's own) | unknown | | Reflect tool: a second click reflects across the line through both clicked points; Alt on it reflects a copy | done | [the help page](https://helpx.adobe.com/illustrator/desktop/manage-objects/arrange-objects/reflect-or-flip-objects.html); one undo step; a second click whose snapped point is within 3 screen pixels of the first one's, Escape or a changed selection leaves nothing reflected (our choices) | unknown | | Move, Rotate, Scale, Reflect and Shear dialogs: live Preview; Up/Down step angles and percentages by 1, Shift by 10 | done | #1412; Preview starts off (our choice: the public documentation gives no default), Move's as it was last confirmed; OK is one undo step and Transform Again repeats it | unknown | | Transform Again, Transform Each, Scale Strokes & Effects, Scale Corners | done | | unknown |