Skip to content

DesignCraft: An open-source page and layout design editor written by scratch in pure Rust

M4: the Pen's drag preview on a continued end point runs from the anchor it is joined to (#1438)

Trevor Brown5b48a6fcf7f0
diff --git a/crates/tools/src/pen.rs b/crates/tools/src/pen.rs
index 2404cb762..753e6d650 100644
--- a/crates/tools/src/pen.rs
+++ b/crates/tools/src/pen.rs
@@ -33,6 +33,9 @@ pub struct PenTool {
/// Continuing from the subpath's first anchor: `anchors` run in drawing order, the reverse
/// of the path's, and new anchors go before its first one.
from_start: bool,
+ /// The anchor the continued end point is joined to (drawing order): the segment a drag of the
+ /// end point's direction lines reshapes runs from it, not from the path's other end.
+ joined: Option<(Point, Point, Point)>,
}
/// An end point of an open path under canvas point `p`: (item, subpath, whether it is the last
@@ -120,6 +123,7 @@ impl PenTool {
self.editing_end = false;
self.end_moved = false;
self.from_start = false;
+ self.joined = None;
vec![]
}
@@ -137,6 +141,13 @@ impl PenTool {
// From the first anchor the anchors run backwards (handles swapped); new ones are added
// before the first, so the path keeps its direction (and its arrowheads their ends).
let (other, end) = if at_end { (at(first, false), at(last, false)) } else { (at(last, true), at(first, true)) };
+ // The anchor next to the end point, in drawing order too (the other end when there are two).
+ let joined = if at_end {
+ sp.anchors.len().checked_sub(2).and_then(|i| sp.anchors.get(i)).map(|a| at(a, false))
+ } else {
+ sp.anchors.get(1).map(|a| at(a, true))
+ };
+ self.joined = joined;
self.path = Some(id.0);
self.subpath = Some(si);
self.spread = Some(loc.spread);
@@ -322,9 +333,12 @@ impl Tool for PenTool {
&& let Some(a) = self.anchors.last()
{
// The direction line being dragged out, and the segment it shapes: from the previous
- // anchor's out handle to the new anchor's in handle, live under the pointer (#1438).
+ // anchor's out handle to the new anchor's in handle, live under the pointer (#1438). A
+ // continued end point being reshaped is joined to its neighbour in the path, not to
+ // the other end the anchors start with.
out.push(Overlay::Line { a: xf * a.1, b: xf * a.2, color: blue, dashed: false });
- if let Some(prev) = self.anchors.len().checked_sub(2).and_then(|i| self.anchors.get(i)) {
+ let prev = if self.editing_end { self.joined } else { self.anchors.len().checked_sub(2).and_then(|i| self.anchors.get(i)).copied() };
+ if let Some(prev) = prev {
let mut path = BezPath::new();
path.move_to(xf * prev.0);
path.curve_to(xf * prev.2, xf * a.1, xf * a.0);
diff --git a/crates/tools/src/tests.rs b/crates/tools/src/tests.rs
index 177f114b5..c1eecd86a 100644
--- a/crates/tools/src/tests.rs
+++ b/crates/tools/src/tests.rs
@@ -1906,6 +1906,52 @@ fn pen_over_a_selected_open_paths_end_point_continues_it() {
assert!(t.busy(), "the path is being continued");
}
+/// Continuing a path from an end point and dragging its direction lines previews the segment the
+/// drag reshapes: from the anchor joined to that end point, not from the path's other end, from
+/// either end (#1438).
+#[test]
+fn pen_previews_the_segment_joined_to_a_continued_end_point() {
+ use designcraft_geom::{BezPath, PathEl};
+ let mut d = Document::new(&NewDocument::default());
+ let lid = d.default_layer();
+ let id = ItemId(d.alloc());
+ let anchors = vec![
+ designcraft_geom::Anchor::corner(Point::new(100.0, 100.0)),
+ designcraft_geom::Anchor::corner(Point::new(200.0, 100.0)),
+ designcraft_geom::Anchor::corner(Point::new(200.0, 200.0)),
+ ];
+ let mut it = Item::new(id, lid, Shape::Path, designcraft_geom::PathData::single(designcraft_geom::SubPath::new(anchors, false)));
+ it.stroke = Stroke::default();
+ d.insert_item(SpreadRef::Doc(0), it, None).unwrap();
+ let (s, c, l) = (Selection::items(vec![id]), Cache::new(), CanvasLayout::new(&d, false));
+ let cx = ctx(&d, &s, &c, &l);
+ let xf = l.xf(SpreadRef::Doc(0));
+ let near = |a: Point, b: Point| (a - b).hypot() < 1e-6;
+ let solid = |t: &dyn Tool| -> Vec<BezPath> {
+ t.overlays(&cx)
+ .into_iter()
+ .filter_map(|o| match o {
+ Overlay::Path { path, dashed: false, .. } => Some(path),
+ _ => None,
+ })
+ .collect()
+ };
+ for (end, joined, far) in [((200.0, 200.0), (200.0, 100.0), (100.0, 100.0)), ((100.0, 100.0), (200.0, 100.0), (200.0, 200.0))] {
+ let mut t = create("pen");
+ let p = xf * Point::new(end.0, end.1);
+ t.pointer(&cx, &PointerEvent::new(PointerKind::Down, p.x, p.y));
+ t.pointer(&cx, &PointerEvent::new(PointerKind::Drag, p.x + 30.0, p.y + 30.0));
+ let [path] = &solid(&*t)[..] else { panic!("one segment preview from {end:?}: {:?}", t.overlays(&cx)) };
+ match path.elements() {
+ [PathEl::MoveTo(a), PathEl::CurveTo(_, _, b)] => {
+ assert!(near(*a, xf * Point::new(joined.0, joined.1)), "from the anchor joined to {end:?}, not the other end {far:?}: {a:?}");
+ assert!(near(*b, p), "to the end point being reshaped: {b:?}");
+ }
+ other => panic!("{other:?}"),
+ }
+ }
+}
+
#[test]
fn the_selection_tool_drags_a_path_type_bracket_along_its_path() {
let mut d = Document::new(&NewDocument::default());