From 667ff774ad3b0582cbf00a0c4578b9348189acc9 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Tue, 17 Nov 2026 22:47:24 -0800 Subject: [PATCH] Shapes: the Curvature Circle tool passes over the circles it drew MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit After a click, the circle the tool drew is selected and on top, so the next hover or click near the point it touches snapped to that circle instead of the path. The pointer now passes over the circles the tool drew until an edit, Undo or Redo is made outside the tool. - An anchor marked smooth is a corner when its handles turn by 90° or more; the params text and docs/mcp.md now say so instead of "turn back the way they came". - Tests cover a second click after a draw, in the tool and through the session, and a marked-smooth anchor with square handles. --- crates/engine/src/cmd/create.rs | 2 +- crates/engine/src/tests/curvature_circle.rs | 22 ++++++++++++ crates/geom/src/curvature.rs | 14 +++++--- crates/tools/src/curvature_circle.rs | 40 +++++++++++++++------ crates/tools/src/curvature_circle/tests.rs | 33 +++++++++++++---- crates/tools/src/draw2.rs | 1 - docs/mcp.md | 7 ++-- 7 files changed, 93 insertions(+), 26 deletions(-) diff --git a/crates/engine/src/cmd/create.rs b/crates/engine/src/cmd/create.rs index c6bd053d09..2159a183dc 100644 --- a/crates/engine/src/cmd/create.rs +++ b/crates/engine/src/cmd/create.rs @@ -73,7 +73,7 @@ pub fn specs() -> Vec { "Curvature Circle", [], None, - "{id, subpath?=0, segment, t: 0..1, convex?=false} → {id, cx, cy, radius, nominal}: a circle with the current paint that touches path `id` at `t` on the segment and bends as the path bends there (its circle of curvature), on the side the path bends toward, or on the other side with convex; a segment that is the usual cubic of a circular arc of 90° or less gives a circle of that arc's own radius (nominal: true), on the concave side the arc's own circle; not pixel-aligned even while Align New Objects to Pixel Grid is on; a straight stretch, a cusp, a corner anchor (t 0 or 1 where two segments meet at an angle of more than half a degree and the anchor is not marked smooth, or where they turn back the way they came; segments of no length between them are passed over), a guide, or a circle reaching past the canvas is an error, and nothing is drawn", + "{id, subpath?=0, segment, t: 0..1, convex?=false} → {id, cx, cy, radius, nominal}: a circle with the current paint that touches path `id` at `t` on the segment and bends as the path bends there (its circle of curvature), on the side the path bends toward, or on the other side with convex; a segment that is the usual cubic of a circular arc of 90° or less gives a circle of that arc's own radius (nominal: true), on the concave side the arc's own circle; not pixel-aligned even while Align New Objects to Pixel Grid is on; a straight stretch, a cusp, a corner anchor (t 0 or 1 where two segments meet at an angle of more than half a degree and the anchor is not marked smooth, or where they turn by 90° or more; segments of no length between them are passed over), a guide, or a circle reaching past the canvas is an error, and nothing is drawn", has_doc, curvature_circle ), diff --git a/crates/engine/src/tests/curvature_circle.rs b/crates/engine/src/tests/curvature_circle.rs index e3dd8eb3ac..59d3892e58 100644 --- a/crates/engine/src/tests/curvature_circle.rs +++ b/crates/engine/src/tests/curvature_circle.rs @@ -200,3 +200,25 @@ fn the_curvature_circle_tool_draws_the_circle_where_it_clicks() { click(&mut s, Mods::default(), 20.0, 20.0); assert_eq!((undo_len(&s), object_count(&s)), (undo + 2, count + 2)); } + +/// After a click at a parabola's vertex, a click at (280, 236), 2.4 pt from the circle just drawn +/// and about 3.1 pt from the parabola, passes over that circle and draws the parabola's circle +/// there; after an Undo, the pointer reaches the circle again. +#[test] +fn the_next_click_passes_over_the_circle_the_tool_drew() { + let mut s = session(); + parabola(&mut s); + s.select_tool("curvatureCircle", view()).unwrap(); + click(&mut s, Mods::default(), 200.0, 200.0); + let first = *s.doc().unwrap().selection.objects.first().unwrap(); + assert!(bounds_are(&s, first, Rect::new(100.0, 200.0, 300.0, 400.0))); + click(&mut s, Mods::default(), 280.0, 236.0); + let second = *s.doc().unwrap().selection.objects.first().unwrap(); + assert_ne!(second, first); + let r = s.doc().unwrap().doc.bounds_of(&[second], false).unwrap().width() / 2.0; + assert!(r > 150.0, "the parabola's circle at x = 280 is larger than at its vertex: {r}"); + s.execute("edit.undo", &json!({})).unwrap(); + let count = object_count(&s); + click(&mut s, Mods::default(), 280.0, 241.0); + assert_eq!(object_count(&s), count + 1, "the circle drawn first is within reach again"); +} diff --git a/crates/geom/src/curvature.rs b/crates/geom/src/curvature.rs index 90d0aa23f6..de5e68e54b 100644 --- a/crates/geom/src/curvature.rs +++ b/crates/geom/src/curvature.rs @@ -80,8 +80,8 @@ pub fn curvature_circle(c: CubicBez, t: f64, convex: bool) -> Result Result { let n = sp.segment_count(); if seg >= n { @@ -119,7 +119,7 @@ fn neighbor(sp: &SubPath, seg: usize, forward: bool) -> Option<(CubicBez, bool)> } /// Is the join at anchor `anchor` smooth: marked smooth (with no segment of no length passed -/// over) with the directions `a` into it and `b` out of it not pointing back, or `a` and `b` the +/// over) with the directions `a` into it and `b` out of it less than 90° apart, or `a` and `b` the /// same within [`SMOOTH_TURN`]? fn smooth(sp: &SubPath, anchor: usize, skipped: bool, a: Vec2, b: Vec2) -> bool { let marked = !skipped && sp.anchors.get(anchor).is_some_and(|an| an.kind == AnchorKind::Smooth && an.has_in() && an.has_out()); @@ -349,8 +349,8 @@ mod tests { } /// An anchor whose handles turn by a thousandth of a radian, as rounded coordinates leave them, - /// is smooth; one marked smooth is smooth unless its handles point back the way they came; a - /// turn of 2° is a corner. + /// is smooth; one marked smooth is smooth unless its handles turn by 90° or more; a turn of 2° + /// is a corner. #[test] fn a_nearly_smooth_or_marked_smooth_anchor_has_a_circle() { let mid = |out_x: f64, kind: AnchorKind| { @@ -370,6 +370,10 @@ mod tests { back.anchors.get_mut(1).unwrap().h_out = Point::new(100.0, -40.0); assert_eq!(curvature_circle_on(&back, 0, 1.0, false), Err(NoCircle::Corner)); assert_eq!(curvature_circle_on(&back, 1, 0.0, false), Err(NoCircle::Corner)); + // Marked smooth, with the out handle square to the in handle: a turn of 90°. + let mut square = mid(100.0, AnchorKind::Smooth); + square.anchors.get_mut(1).unwrap().h_out = Point::new(150.0, 0.0); + assert_eq!(curvature_circle_on(&square, 0, 1.0, false), Err(NoCircle::Corner)); } /// An arc's cubic is taken only up to a quarter turn, with both ends on one circle and handles diff --git a/crates/tools/src/curvature_circle.rs b/crates/tools/src/curvature_circle.rs index e27fa5a885..6a0ffe05ea 100644 --- a/crates/tools/src/curvature_circle.rs +++ b/crates/tools/src/curvature_circle.rs @@ -11,7 +11,10 @@ //! the side the path bends toward; with Shift held at the press, on the other side. On a straight //! stretch, at a cusp, at a corner, or where the circle would reach past the canvas, the pointer //! shows "Straight segment", "Cusp", "Corner" or "Circle too large" instead, and a click draws -//! nothing. +//! nothing. The pointer passes over the circles the tool drew until an edit, Undo or Redo is made +//! outside the tool. + +use std::collections::HashSet; use serde_json::json; use vectorcraft_doc::{MAX_COORD, NodeId}; @@ -55,14 +58,19 @@ pub struct CurvatureCircleTool { /// The press in progress, where the pointer was pressed: what a release draws. press: Option<(Hit, bool)>, from: Point, + /// The circles the tool drew since the history last moved outside it. + drawn: HashSet, } -/// The nearest point within reach of `p` on the paths a click can pick, guides left out; of paths -/// at the same distance, a selected one, then the one on top. -fn hit_at(cx: &ToolContext, p: Point) -> Option { +/// The nearest point within reach of `p` on the paths a click can pick, guides and the paths in +/// `skip` left out; of paths at the same distance, a selected one, then the one on top. +fn hit_at(cx: &ToolContext, p: Point, skip: &HashSet) -> Option { let tol = cx.tol(SNAP_PX).max(cx.snap_tol()); let mut best: Option<(Hit, f64)> = None; for id in crate::draw2::candidate_paths(cx) { + if skip.contains(&id) { + continue; + } let Some(pd) = cx.doc.node(id).filter(|n| !n.is_guide()).and_then(|n| n.path_data()) else { continue }; // A path whose control points' box lies out of reach holds no point within reach. if pd.control_bounds().is_none_or(|b| !b.inflate(tol, tol).contains(p)) { @@ -112,11 +120,11 @@ impl Tool for CurvatureCircleTool { } match ev.kind { PointerKind::Move => { - self.hover = hit_at(cx, ev.pos).map(|h| (h, ev.mods.shift)); + self.hover = hit_at(cx, ev.pos, &self.drawn).map(|h| (h, ev.mods.shift)); vec![] } PointerKind::Down => { - self.press = hit_at(cx, ev.pos).map(|h| (h, ev.mods.shift)); + self.press = hit_at(cx, ev.pos, &self.drawn).map(|h| (h, ev.mods.shift)); self.hover = self.press; self.from = ev.pos; vec![] @@ -131,13 +139,13 @@ impl Tool for CurvatureCircleTool { } PointerKind::Up => { let press = self.press.take(); - self.hover = hit_at(cx, ev.pos).map(|h| (h, ev.mods.shift)); + self.hover = hit_at(cx, ev.pos, &self.drawn).map(|h| (h, ev.mods.shift)); let Some((hit, convex)) = press else { return vec![] }; if ev.pos.distance(self.from) >= cx.tol(DRAG_PX) || !matches!(found(cx, hit, convex), Some(Found::Circle(_))) { return vec![]; } let p = json!({"id": hit.id.0, "subpath": hit.subpath, "segment": hit.segment, "t": hit.t, "convex": convex}); - vec![Action::Exec("shape.curvatureCircle".into(), p)] + vec![Action::Exec("shape.curvatureCircle".into(), p), Action::Notify("created".into())] } PointerKind::DoubleClick | PointerKind::TripleClick => vec![], } @@ -167,9 +175,20 @@ impl Tool for CurvatureCircleTool { true } + /// The command selects the circle it drew; the tool adds it to the circles the pointer passes + /// over. + fn notify(&mut self, cx: &ToolContext, what: &str) { + if what == "created" + && let [id] = cx.selection.objects[..] + { + self.drawn.insert(id); + } + } + fn deactivate(&mut self, _cx: &ToolContext) -> Vec { self.hover = None; self.press = None; + self.drawn.clear(); vec![] } @@ -180,10 +199,11 @@ impl Tool for CurvatureCircleTool { } /// An edit, Undo or Redo made outside the tool, or another document, may move or remove the - /// path under the pointer. + /// path under the pointer, and the pointer reaches the circles the tool drew again. fn history_moved(&mut self) { self.hover = None; self.press = None; + self.drawn.clear(); } fn overlays(&self, cx: &ToolContext) -> Vec { @@ -194,7 +214,7 @@ impl Tool for CurvatureCircleTool { Some(Found::Circle(c)) => { let r = c.radius; let path = shapes::ellipse(Rect::new(c.center.x - r, c.center.y - r, c.center.x + r, c.center.y + r)).to_bezpath(); - // Drawn solid when longer than DASHED_PX on screen. + // The outline is drawn solid when it is longer than DASHED_PX on screen. let dashed = std::f64::consts::TAU * r <= cx.tol(DASHED_PX); o.push(Overlay::Path { path, color, width: 1.0, dashed }); o.push(Overlay::Line { a: c.center, b: c.at, color, dashed: false }); diff --git a/crates/tools/src/curvature_circle/tests.rs b/crates/tools/src/curvature_circle/tests.rs index 8881842516..d417af58dc 100644 --- a/crates/tools/src/curvature_circle/tests.rs +++ b/crates/tools/src/curvature_circle/tests.rs @@ -30,10 +30,11 @@ fn click(t: &mut CurvatureCircleTool, cx: &ToolContext, p: (f64, f64), mods: Mod acts } -/// The one command `acts` runs: its params. +/// The one command `acts` runs, followed by the tool's notice that it drew: the command's params. fn params(acts: &[Action]) -> serde_json::Value { - let [Action::Exec(c, v)] = acts else { panic!("one command: {acts:?}") }; + let [Action::Exec(c, v), Action::Notify(what)] = acts else { panic!("one command: {acts:?}") }; assert_eq!(c, "shape.curvatureCircle"); + assert_eq!(what, "created"); v.clone() } @@ -133,14 +134,14 @@ fn the_pointer_reaches_a_path_within_5_screen_points() { assert_eq!(click(&mut t, &cx, (200.0, 200.0), Mods::default()), vec![]); assert!(t.overlays(&cx).is_empty()); assert_eq!(click(&mut t, &cx, (306.0, 200.0), Mods::default()), vec![]); - assert_eq!(click(&mut t, &cx, (304.0, 200.0), Mods::default()).len(), 1); + assert_eq!(click(&mut t, &cx, (304.0, 200.0), Mods::default()).len(), 2); let zoomed = ToolContext { zoom: 2.0, ..cx }; assert_eq!(click(&mut t, &zoomed, (303.0, 200.0), Mods::default()), vec![]); - assert_eq!(click(&mut t, &zoomed, (302.0, 200.0), Mods::default()).len(), 1); + assert_eq!(click(&mut t, &zoomed, (302.0, 200.0), Mods::default()).len(), 2); // A larger snapping tolerance reaches farther, with Smart Guides off or on. for smart_guides in [false, true] { let wide = ToolContext { snapping_tolerance: 10.0, smart_guides, ..cx }; - assert_eq!(click(&mut t, &wide, (309.0, 200.0), Mods::default()).len(), 1, "{smart_guides}"); + assert_eq!(click(&mut t, &wide, (309.0, 200.0), Mods::default()).len(), 2, "{smart_guides}"); assert_eq!(click(&mut t, &wide, (311.0, 200.0), Mods::default()), vec![], "{smart_guides}"); } } @@ -177,6 +178,26 @@ fn a_selected_guide_is_left_out() { assert!(t.overlays(&cx).is_empty()); } +/// After a click on a circle drawn with the Ellipse tool, the next hover and click pass over the +/// circle the tool drew (selected and on top of it) and use the circle under it again, until an +/// edit made outside the tool. +#[test] +fn the_next_click_passes_over_the_circle_just_drawn() { + let d = doc_with(&[circle(), circle()]); + let ids: Vec = d.layers[0].children().unwrap().iter().rev().map(|n| n.id).collect(); + let (drawn, under) = (ids[0], ids[1]); + let s = Selection { objects: vec![drawn], ..Selection::default() }; + let p = paint(); + let cx = ToolContext { smart_guides: false, ..cx(&d, &s, &p) }; + let mut t = CurvatureCircleTool::default(); + t.notify(&cx, "created"); + t.pointer(&cx, &ev(PointerKind::Move, (302.0, 200.0), Mods::default())); + assert_eq!(t.hover.map(|(h, _)| h.id), Some(under)); + assert_eq!(params(&click(&mut t, &cx, (302.0, 200.0), Mods::default()))["id"], json!(under.0)); + t.history_moved(); + assert_eq!(params(&click(&mut t, &cx, (302.0, 200.0), Mods::default()))["id"], json!(drawn.0)); +} + /// A press that a second finger or a canceled touch drops draws nothing on the release, and an /// edit made outside the tool drops what the pointer was over. #[test] @@ -247,7 +268,7 @@ fn a_drag_draws_nothing() { t.pointer(&cx, &ev(PointerKind::Up, (250.0, 200.0), Mods::default())); assert!(t.overlays(&cx).is_empty()); assert_eq!(press(&mut t, (300.0, 202.0)), vec![]); - assert_eq!(press(&mut t, (300.0, 201.0)).len(), 1); + assert_eq!(press(&mut t, (300.0, 201.0)).len(), 2); // A release that far away with no drag before it is not a click either. t.pointer(&cx, &ev(PointerKind::Down, (300.0, 200.0), Mods::default())); assert_eq!(t.pointer(&cx, &ev(PointerKind::Up, (300.0, 230.0), Mods::default())), vec![]); diff --git a/crates/tools/src/draw2.rs b/crates/tools/src/draw2.rs index ef634c7335..16cedd95cf 100644 --- a/crates/tools/src/draw2.rs +++ b/crates/tools/src/draw2.rs @@ -374,7 +374,6 @@ pub(crate) fn candidate_paths(cx: &ToolContext) -> Vec { } }); rest.reverse(); - // Skips ids already listed. let mut seen: std::collections::HashSet = v.iter().copied().collect(); for id in rest { if seen.insert(id) { diff --git a/docs/mcp.md b/docs/mcp.md index 74cf29f4e7..9939e58cb9 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -6226,8 +6226,8 @@ result is `{id, cx, cy, radius, nominal}`, where `nominal` is true for such an a The circle is not pixel-aligned, even while [Align New Objects to Pixel Grid](#align-new-objects-to-pixel-grid) is on. The command returns an error and draws nothing on a straight segment or the turning point of an S curve, at a cusp, at an anchor whose handle is drawn back onto it, at a corner anchor (`t` 0 or 1 where two segments meet at an angle -of more than half a degree and the anchor is not marked smooth, or where they turn back the way they came; segments -of no length between them are passed over), when the circle would reach past the canvas (4,000,000 pt from the +of more than half a degree and the anchor is not marked smooth, or where they turn by 90° or more; segments of no +length between them are passed over), when the circle would reach past the canvas (4,000,000 pt from the origin), when `t` is not from 0 to 1, when `id` is a guide, and when the path, subpath or segment does not exist. The Curvature Circle tool, after the Circle tool in the Ellipse tool's toolbar group, draws with this command. The @@ -6238,7 +6238,8 @@ it touches and, with Measurement Labels on, its radius by the pointer, followed radius. A click draws the circle, and a press dragged 2 screen points or more, or canceled with Escape, draws nothing; Shift held at the press puts the circle on the other side of the path. On a straight segment, at a cusp, at a corner anchor, or where the circle would reach past the canvas, the pointer shows "Straight segment", "Cusp", "Corner" or -"Circle too large", and a click draws nothing. +"Circle too large", and a click draws nothing. The pointer passes over the circles the tool drew until an edit, Undo +or Redo is made outside the tool. ```json {"name":"run_command","arguments":{"command":"shape.curvatureCircle","params":{"id":12,"segment":0,"t":0.5}}}