mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-21 18:04:03 +01:00
Merge pull request #8907 from dotCooCoo/fix/tangent-circle-canceled-preview
Path Precision Tools: the Tangent Circle Tool works out its circle again after a canceled live edit
This commit is contained in:
3 files changed
+114
-19
No files matched your search
@@ -12,8 +12,10 @@
|
||||
//! three points can draw, a note says so. The circle is a new ellipse with the current paint, drawn
|
||||
//! with `shape.circleThroughPoints` in one undo step, so Align New Objects to Pixel Grid leaves it
|
||||
//! in place; the first segment stays picked until the circle is drawn, also when drawing fails or the
|
||||
//! press on the second segment is dropped. Escape, an edit or Undo outside the tool, or another
|
||||
//! document forgets the first segment.
|
||||
//! press on the second segment is dropped. Escape, an edit or Undo outside the tool, another
|
||||
//! document, or a change to the first segment that leaves no undo step (a live preview, or one that
|
||||
//! was canceled) forgets the first segment. The circles are looked for again whenever either
|
||||
//! segment's shape differs from the one they were found on.
|
||||
|
||||
use kurbo::{CubicBez, Shape};
|
||||
use serde_json::{Value, json};
|
||||
@@ -55,7 +57,8 @@ struct Pick {
|
||||
/// A press on the second segment, kept while the button is down.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
struct Press {
|
||||
/// The segments a circle may touch.
|
||||
/// The segment pressed on, and the segments a circle may touch.
|
||||
pick: Pick,
|
||||
second: Vec<CubicBez>,
|
||||
/// Where the press was.
|
||||
at: Point,
|
||||
@@ -78,12 +81,13 @@ pub struct TangentCircleTool {
|
||||
found: Option<SegmentCircle>,
|
||||
looked: bool,
|
||||
pointer: Point,
|
||||
/// The circles the last second segment gave (its path, subpath and segment, and the radius
|
||||
/// asked for), so that moving along it only picks among them.
|
||||
/// The circles the last second segment gave (its path, subpath and segment, the radius asked
|
||||
/// for, and the shapes of the first segment and of the segments a circle may touch), so that
|
||||
/// moving along it only picks among them.
|
||||
cache: Option<(CacheKey, Vec<SegmentCircle>)>,
|
||||
}
|
||||
|
||||
type CacheKey = (NodeId, usize, usize, Option<u64>);
|
||||
type CacheKey = (NodeId, usize, usize, Option<u64>, CubicBez, Vec<CubicBez>);
|
||||
|
||||
impl Default for TangentCircleTool {
|
||||
fn default() -> Self {
|
||||
@@ -102,6 +106,17 @@ fn pick_at(cx: &ToolContext, p: Point) -> Option<Pick> {
|
||||
Some(Pick { id, subpath, segment, t, p: curve.eval(t), curve })
|
||||
}
|
||||
|
||||
/// The document's segment at `pick` as it is now: none when its path, subpath or segment is gone.
|
||||
fn segment_now(cx: &ToolContext, pick: &Pick) -> Option<CubicBez> {
|
||||
let sp = cx.doc.node(pick.id)?.path_data()?.subpaths.get(pick.subpath)?;
|
||||
(pick.segment < sp.segment_count()).then(|| sp.segment(pick.segment))
|
||||
}
|
||||
|
||||
/// Whether the document's segment at `pick` still has the shape it had when it was picked.
|
||||
fn unchanged(cx: &ToolContext, pick: &Pick) -> bool {
|
||||
segment_now(cx, pick) == Some(pick.curve)
|
||||
}
|
||||
|
||||
/// The segments a circle may touch when `second` is picked: it and the segments on either side of
|
||||
/// it, without `first`. None when `second` is the first segment again.
|
||||
fn second_set(cx: &ToolContext, first: &Pick, second: &Pick) -> Vec<CubicBez> {
|
||||
@@ -192,24 +207,35 @@ impl TangentCircleTool {
|
||||
}
|
||||
|
||||
/// The circles `pick` gives as the second segment, with Shift's `radius`: worked out again only
|
||||
/// when the segment or the radius changes.
|
||||
/// when the segment, the radius or the shape of either segment changes.
|
||||
fn circles_on(&mut self, cx: &ToolContext, pick: &Pick, radius: Option<f64>) -> Vec<SegmentCircle> {
|
||||
let key = (pick.id, pick.subpath, pick.segment, radius.map(f64::to_bits));
|
||||
let Some(first) = &self.first else { return vec![] };
|
||||
let second = second_set(cx, first, pick);
|
||||
let key = (pick.id, pick.subpath, pick.segment, radius.map(f64::to_bits), first.curve, second);
|
||||
if let Some((k, found)) = &self.cache
|
||||
&& *k == key
|
||||
{
|
||||
return found.clone();
|
||||
}
|
||||
let Some(first) = &self.first else { return vec![] };
|
||||
let found = circles(first, &second_set(cx, first, pick), radius);
|
||||
let found = circles(first, &key.5, radius);
|
||||
self.cache = Some((key, found.clone()));
|
||||
found
|
||||
}
|
||||
|
||||
/// Forget the first segment when the document's no longer has the shape it was picked with: a
|
||||
/// live preview, or one that was canceled, changes it without an undo step, so the tool is not
|
||||
/// told ([`Tool::history_moved`]).
|
||||
fn forget_if_changed(&mut self, cx: &ToolContext) {
|
||||
if self.first.as_ref().is_some_and(|f| !unchanged(cx, f)) {
|
||||
self.forget();
|
||||
}
|
||||
}
|
||||
|
||||
/// Look for the circle the pointer at `p` would give over `hover`, before any press.
|
||||
fn look(&mut self, cx: &ToolContext, p: Point, mods: Mods) {
|
||||
self.found = None;
|
||||
self.looked = false;
|
||||
self.forget_if_changed(cx);
|
||||
let Some(hover) = self.hover.clone().filter(|_| self.first.is_some()) else { return };
|
||||
let found = self.circles_on(cx, &hover, mods.shift.then_some(self.radius));
|
||||
self.found = nearest_to(found, p, cx.tol(DRAW_PX));
|
||||
@@ -219,15 +245,25 @@ impl TangentCircleTool {
|
||||
/// The pointer at `ev` during a press on the second segment: once it is far enough from the
|
||||
/// press, the radius follows it (Cmd/Ctrl: a tenth of its movement; Shift: round steps).
|
||||
fn drag_to(&mut self, cx: &ToolContext, ev: &PointerEvent) {
|
||||
if self.press.is_none() {
|
||||
return;
|
||||
}
|
||||
// A change without an undo step since the press: a changed first segment is forgotten (the
|
||||
// press then draws nothing), and the circle follows the segments it may touch as they are now.
|
||||
self.forget_if_changed(cx);
|
||||
let radius = self.radius;
|
||||
let (Some(first), Some(press)) = (&self.first, &mut self.press) else { return };
|
||||
// A pressed segment that is gone leaves nothing to touch.
|
||||
let second = if segment_now(cx, &press.pick).is_some() { second_set(cx, first, &press.pick) } else { vec![] };
|
||||
let moved = second != press.second;
|
||||
press.second = second;
|
||||
let reach = ev.pos.distance(press.at);
|
||||
let gear = if ev.mods.cmd { SLOW } else { 1.0 };
|
||||
let r = match press.radius {
|
||||
None if reach <= cx.tol(DRAG_PX) => {
|
||||
// Shift pressed or let go before the drag starts: the click's circle, with or
|
||||
// without the tool's radius.
|
||||
if ev.mods.shift != press.shift {
|
||||
// Shift pressed or let go before the drag starts, or the segments changed: the
|
||||
// click's circle, with or without the tool's radius.
|
||||
if moved || ev.mods.shift != press.shift {
|
||||
press.shift = ev.mods.shift;
|
||||
let found = circles(first, &press.second, press.shift.then_some(radius));
|
||||
self.found = nearest_to(found, press.at, cx.tol(DRAW_PX));
|
||||
@@ -273,6 +309,7 @@ impl Tool for TangentCircleTool {
|
||||
}
|
||||
PointerKind::Down => {
|
||||
let Some(pick) = pick_at(cx, ev.pos) else { return vec![] };
|
||||
self.forget_if_changed(cx);
|
||||
let Some(first) = self.first.clone() else {
|
||||
self.first = Some(pick);
|
||||
self.picking = true;
|
||||
@@ -286,7 +323,7 @@ impl Tool for TangentCircleTool {
|
||||
self.found = nearest_to(found, ev.pos, cx.tol(DRAW_PX));
|
||||
self.looked = true;
|
||||
self.hover = None;
|
||||
self.press = Some(Press { second, at: ev.pos, shift: ev.mods.shift, reach: 0.0, radius: None });
|
||||
self.press = Some(Press { pick, second, at: ev.pos, shift: ev.mods.shift, reach: 0.0, radius: None });
|
||||
vec![]
|
||||
}
|
||||
PointerKind::Drag => {
|
||||
@@ -431,10 +468,14 @@ mod tests {
|
||||
/// Two open lines, (100, 100)–(300, 100) and (100, 140)–(300, 140), and an L from (100, 300)
|
||||
/// over to (300, 300) and down to (300, 400).
|
||||
fn doc() -> Document {
|
||||
doc_at(100.0, 140.0)
|
||||
}
|
||||
|
||||
/// [`doc`] with the two open lines at heights `a` and `b`, with the same ids.
|
||||
fn doc_at(a: f64, b: f64) -> Document {
|
||||
let mut d = Document::new(500.0, 500.0);
|
||||
let l = d.layers[0].id;
|
||||
let lines: [&[(f64, f64)]; 3] =
|
||||
[&[(100.0, 100.0), (300.0, 100.0)], &[(100.0, 140.0), (300.0, 140.0)], &[(100.0, 300.0), (300.0, 300.0), (300.0, 400.0)]];
|
||||
let lines: [&[(f64, f64)]; 3] = [&[(100.0, a), (300.0, a)], &[(100.0, b), (300.0, b)], &[(100.0, 300.0), (300.0, 300.0), (300.0, 400.0)]];
|
||||
for pts in lines {
|
||||
let id = d.alloc_id();
|
||||
let pts: Vec<Point> = pts.iter().map(|(x, y)| Point::new(*x, *y)).collect();
|
||||
@@ -676,6 +717,58 @@ mod tests {
|
||||
assert!(t.first.is_some());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_change_without_an_undo_step_is_not_drawn_from_the_old_shapes() {
|
||||
let (sel, p) = (Selection::default(), paint());
|
||||
let (d, previewed, first_previewed) = (doc(), doc_at(100.0, 180.0), doc_at(80.0, 140.0));
|
||||
let (cx, during, first_during) = (cx(&d, &sel, &p), cx(&previewed, &sel, &p), cx(&first_previewed, &sel, &p));
|
||||
let mut t = TangentCircleTool::default();
|
||||
// A live preview moves the second line down to 180, and the pointer over it shows a 40 pt
|
||||
// circle. The preview is canceled, which leaves no undo step: the click draws the 20 pt circle
|
||||
// touching the line where it is again, not the one found during the preview.
|
||||
t.pointer(&cx, &ev(PointerKind::Down, 150.0, 101.0));
|
||||
t.pointer(&cx, &ev(PointerKind::Up, 150.0, 101.0));
|
||||
t.pointer(&during, &ev(PointerKind::Move, 200.0, 179.0));
|
||||
assert!(t.overlays(&during).iter().any(|o| matches!(o, Overlay::Measure { text, .. } if text.starts_with("R: 40.00"))));
|
||||
t.pointer(&cx, &ev(PointerKind::Down, 200.0, 139.0));
|
||||
let released = up(&mut t, &cx, &ev(PointerKind::Up, 200.0, 139.0));
|
||||
assert!(close(drawn(&released), (130.0, 100.0, 40.0, 40.0)), "{released:?}");
|
||||
// The same for a press made during the preview and released after it was canceled.
|
||||
t.pointer(&cx, &ev(PointerKind::Down, 150.0, 101.0));
|
||||
t.pointer(&cx, &ev(PointerKind::Up, 150.0, 101.0));
|
||||
t.pointer(&during, &ev(PointerKind::Down, 200.0, 179.0));
|
||||
let released = up(&mut t, &cx, &ev(PointerKind::Up, 200.0, 179.0));
|
||||
assert!(close(drawn(&released), (130.0, 100.0, 40.0, 40.0)), "{released:?}");
|
||||
// A first segment picked during a preview that moved it, canceled since: forgotten, with no
|
||||
// circle shown from where it was.
|
||||
t.pointer(&first_during, &ev(PointerKind::Down, 150.0, 81.0));
|
||||
t.pointer(&first_during, &ev(PointerKind::Up, 150.0, 81.0));
|
||||
assert!(t.first.is_some());
|
||||
t.pointer(&cx, &ev(PointerKind::Move, 200.0, 139.0));
|
||||
assert!(t.first.is_none() && t.found.is_none() && !t.looked);
|
||||
// A press on a first segment that the preview changed since the press draws nothing.
|
||||
t.pointer(&first_during, &ev(PointerKind::Down, 150.0, 81.0));
|
||||
t.pointer(&first_during, &ev(PointerKind::Up, 150.0, 81.0));
|
||||
t.pointer(&first_during, &ev(PointerKind::Down, 200.0, 139.0));
|
||||
assert!(t.busy());
|
||||
assert!(t.pointer(&cx, &ev(PointerKind::Up, 200.0, 139.0)).is_empty());
|
||||
assert!(t.first.is_none() && !t.busy());
|
||||
// A press on the L's second segment, which a preview then removes: nothing to touch, so the
|
||||
// release draws no circle against the segment left beside it.
|
||||
t.pointer(&cx, &ev(PointerKind::Down, 150.0, 101.0));
|
||||
t.pointer(&cx, &ev(PointerKind::Up, 150.0, 101.0));
|
||||
t.pointer(&cx, &ev(PointerKind::Down, 300.0, 350.0));
|
||||
let l = t.press.as_ref().map(|p| (p.pick.id, p.pick.segment));
|
||||
assert!(matches!(l, Some((_, 1))), "{l:?}");
|
||||
let mut cut = doc();
|
||||
if let Some(path) = l.and_then(|(id, _)| cut.node_mut(id)).and_then(|n| n.path_data_mut()) {
|
||||
path.subpaths[0] = SubPath::polyline(&[Point::new(100.0, 300.0), Point::new(300.0, 300.0)], false);
|
||||
}
|
||||
let cut_cx = crate::testutil::cx(&cut, &sel, &p);
|
||||
assert!(t.pointer(&cut_cx, &ev(PointerKind::Up, 300.0, 350.0)).is_empty(), "nothing drawn");
|
||||
assert!(t.first.is_some(), "the first segment stays for another try");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_circle_the_three_points_cannot_draw_keeps_the_first_segment() {
|
||||
// A circle the geometry found, moved to (x, 0) and given radius `r`.
|
||||
|
||||
+4
-2
@@ -14500,8 +14500,10 @@ Tools:
|
||||
undo step. A circle too small or too large for its three points to give it back in floating point counts as no
|
||||
circle. Shift pressed or let go during a click counts at the release. With no circle the note shows, a click draws
|
||||
nothing and the first segment stays picked; it also stays when drawing the circle fails or the press on the second
|
||||
segment is canceled (a canceled touch). Escape, an edit, Undo or Redo outside the tool, or another document forgets
|
||||
the first segment.
|
||||
segment is canceled (a canceled touch). Escape, an edit, Undo or Redo outside the tool, another document, or a
|
||||
change to the first segment that makes no undo step (a live preview, or one that was canceled) forgets the first
|
||||
segment. The circle is looked for again whenever either segment no longer has the shape it was found on, so a
|
||||
canceled preview does not leave a circle worked out on the previewed paths.
|
||||
Option (`tool.setOption`, or a double-click on the tool): `radius` (pt, 0.01 to 1000000, 36). `tool.setOption`
|
||||
gives an error for a value outside that range or one that is not a number, and for any other option.
|
||||
- **Trim at Crossings Tool** (`trimAtCrossings`): removes stretches of paths between the places where they are crossed,
|
||||
|
||||
@@ -2991,7 +2991,7 @@ A VectorCraft addition, not a feature of the reference app, so no area score mov
|
||||
|
||||
## Path Precision Tools add-on: Tangent Circle Tool (2026-11-17)
|
||||
|
||||
A VectorCraft addition, so no area score moves. The Tangent Circle Tool (`tangentCircle`; Object › Plug-ins › Path Precision Tools and the All Tools drawer) draws a circle on two clicked path segments: tangent to the first at the point clicked (its center on the normal there) and touching the second segment or one beside it on its subpath, the touch nearest the pointer winning. Shift gives the circle the tool's radius, and a drag from the second segment sets the radius to the drag's length (Shift: steps of 1, 2 or 5 times a power of 10 pt, at least 4 screen pixels; Cmd/Ctrl: a tenth of the movement); with a radius the circle may touch the first segment anywhere along it. Before the click the circle shows with its center, radii and radius, or a note says there is none. The circles are found by sampling the distance to the second segments along the first segment's normal (or along the first segment, for a radius) and taking the places where it equals the radius (samples, bisection where it crosses, and a search where it only touches). Left out are circles whose nearest point of the second segments is not on the circle (within a millionth of the radius: where the first segment turns back on itself the distance jumps past the radius without a touch), circles that cross the first segment, circles that touch both segments at one point, circles without a finite radius above 0, and circles too small or too large for three of their points to give them back in floating point; a circle found twice (the same center, touching the first segment at the same point) is kept once. The circle is a new ellipse with the current paint, one undo step; the first segment stays picked until it is drawn, also when drawing fails or the press is canceled. Tests: `tangent_circle::tests` in `vectorcraft-pathops` (parallel and crossing lines, a curved second segment, a corner's fillet, lines twice the radius apart, a touch between the places tried, the ends of a line with its handles on its anchors, a circle that crosses the first segment, a first segment that turns back, no circle), `addons::tangent_circle::tests` in `vectorcraft-tools` (two clicks, Shift and the radius option, a drag with Shift and Cmd/Ctrl, no circle and Escape, a canceled press, a circle its three points cannot draw), the session test for a failed draw in `vectorcraft-engine` and the Path Precision Tools menu, options, drawer and catalog tests in `vectorcraft-ui-egui`. Commands: `docs/mcp.md` › Path Precision Tools add-on.
|
||||
A VectorCraft addition, so no area score moves. The Tangent Circle Tool (`tangentCircle`; Object › Plug-ins › Path Precision Tools and the All Tools drawer) draws a circle on two clicked path segments: tangent to the first at the point clicked (its center on the normal there) and touching the second segment or one beside it on its subpath, the touch nearest the pointer winning. Shift gives the circle the tool's radius, and a drag from the second segment sets the radius to the drag's length (Shift: steps of 1, 2 or 5 times a power of 10 pt, at least 4 screen pixels; Cmd/Ctrl: a tenth of the movement); with a radius the circle may touch the first segment anywhere along it. Before the click the circle shows with its center, radii and radius, or a note says there is none. The circles are found by sampling the distance to the second segments along the first segment's normal (or along the first segment, for a radius) and taking the places where it equals the radius (samples, bisection where it crosses, and a search where it only touches). Left out are circles whose nearest point of the second segments is not on the circle (within a millionth of the radius: where the first segment turns back on itself the distance jumps past the radius without a touch), circles that cross the first segment, circles that touch both segments at one point, circles without a finite radius above 0, and circles too small or too large for three of their points to give them back in floating point; a circle found twice (the same center, touching the first segment at the same point) is kept once. The circle is a new ellipse with the current paint, one undo step; the first segment stays picked until it is drawn, also when drawing fails or the press is canceled, and the circles are looked for again when a live preview, or one that was canceled, changes either segment without an undo step (a changed first segment is forgotten). Tests: `tangent_circle::tests` in `vectorcraft-pathops` (parallel and crossing lines, a curved second segment, a corner's fillet, lines twice the radius apart, a touch between the places tried, the ends of a line with its handles on its anchors, a circle that crosses the first segment, a first segment that turns back, no circle), `addons::tangent_circle::tests` in `vectorcraft-tools` (two clicks, Shift and the radius option, a drag with Shift and Cmd/Ctrl, no circle and Escape, a canceled press, a canceled preview, a circle its three points cannot draw), the session test for a failed draw in `vectorcraft-engine` and the Path Precision Tools menu, options, drawer and catalog tests in `vectorcraft-ui-egui`. Commands: `docs/mcp.md` › Path Precision Tools add-on.
|
||||
|
||||
## Path Precision Tools add-on: Trim at Crossings (2026-11-13)
|
||||
|
||||
|
||||
Reference in new issue
Block a user