Files
designcraft/crates/tools/src/anchors.rs
T
aiongg 78340e6588 Canvas: tools show their own cursors (Direct Selection, Pen states)
Tool cursors are glyphs drawn in code (black ink, white halo): the filled
arrow for Selection, the hollow arrow for Direct Selection, the pen nib
with an add, delete, close or convert badge, and a curved double arrow
for rotating. Linux and Windows show them as OS cursor bitmaps,
rasterized per pixels-per-point and cached; macOS paints them at the
pointer, since it sizes cursor bitmaps in points; the web build keeps
the system cursors. Text, hand, zoom and resize cursors stay system
cursors.

The Pen tool reports what a click does: over the first anchor of the
path being drawn it closes the path, and between paths a click on a
selected path's anchor deletes it and a click on its outline adds one.
The Add, Delete and Convert Direction Point tools show the matching pen
badges.

The glyph geometry and the rasterizer are adapted from VectorCraft's
code-drawn tool cursors (same organisation, original work).

Fixes #438
2026-10-14 18:11:06 +02:00

116 lines
4.5 KiB
Rust

//! Add Anchor Point (=), Delete Anchor Point (-) and Convert Direction Point (Shift+C): they act on
//! the selected paths, or the path under the pointer when nothing is selected.
use designcraft_doc::ItemId;
use designcraft_geom::Point;
use serde_json::json;
use crate::select::anchor_at_in;
use crate::{Action, Cursor, Mods, PointerEvent, PointerKind, Tool, ToolContext};
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum Kind {
Add,
/// Scissors: cut the path at the click.
Scissors,
Delete,
Convert,
}
pub struct AnchorTool {
kind: Kind,
/// Convert: the anchor pressed on (id, subpath, anchor), the press point, whether it dragged.
press: Option<(u64, usize, usize, Point, bool)>,
}
impl AnchorTool {
pub fn new(kind: Kind) -> Self {
Self { kind, press: None }
}
}
/// The paths the tool works on.
fn targets(cx: &ToolContext, p: Point) -> Vec<ItemId> {
if cx.selection.items.is_empty() { cx.hit(p).map(|(_, id)| vec![id]).unwrap_or_default() } else { cx.selection.items.clone() }
}
/// The first of `ids` whose outline passes within 5 screen pixels of canvas point `p`.
pub(crate) fn segment_at(cx: &ToolContext, ids: &[ItemId], p: Point) -> Option<ItemId> {
ids.iter().copied().find(|&id| {
let (Some(it), Some(xf)) = (cx.doc.item(id), cx.item_canvas_xf(id)) else { return false };
let inner = (xf * it.xf).inverse() * p;
let scale = (xf * it.xf).inverse().determinant().abs().sqrt();
it.path.nearest(inner).is_some_and(|n| n.4 <= cx.tol(5.0) * scale)
})
}
impl Tool for AnchorTool {
fn id(&self) -> &'static str {
match self.kind {
Kind::Add => "addAnchor",
Kind::Scissors => "scissors",
Kind::Delete => "deleteAnchor",
Kind::Convert => "convertDirection",
}
}
fn pointer(&mut self, cx: &ToolContext, ev: &PointerEvent) -> Vec<Action> {
let p = ev.pos;
match (self.kind, ev.kind) {
(Kind::Add | Kind::Scissors, PointerKind::Down) => {
let Some((_, sp)) = cx.layout.spread_at(p) else { return vec![] };
let Some(id) = segment_at(cx, &targets(cx, p), p) else { return vec![] };
vec![if self.kind == Kind::Scissors {
Action::Exec("path.split".into(), json!({"id": id.0, "at": [sp.x, sp.y]}))
} else {
Action::Exec("path.addAnchor".into(), json!({"id": id.0, "at": [sp.x, sp.y], "tolerance": cx.tol(5.0)}))
}]
}
(Kind::Delete, PointerKind::Down) => match anchor_at_in(cx, &targets(cx, p), p) {
Some((id, si, ai, None)) => vec![Action::Exec("path.deleteAnchor".into(), json!({"id": id, "subpath": si, "anchor": ai}))],
_ => vec![],
},
(Kind::Convert, PointerKind::Down) => {
if let Some((id, si, ai, None)) = anchor_at_in(cx, &targets(cx, p), p) {
self.press = Some((id, si, ai, p, false));
}
vec![]
}
(Kind::Convert, PointerKind::Drag) => {
let Some((id, si, ai, start, dragged)) = self.press else { return vec![] };
if !dragged && (p - start).hypot() < cx.tol(3.0) {
return vec![];
}
let Some((_, sp)) = cx.layout.spread_at(p) else { return vec![] };
let mut out = vec![];
if !dragged {
out.push(Action::Begin("Convert Direction Point".into()));
self.press = Some((id, si, ai, start, true));
}
out.push(Action::Preview("path.convertAnchor".into(), json!({"id": id, "subpath": si, "anchor": ai, "to": [sp.x, sp.y]})));
out
}
(Kind::Convert, PointerKind::Up) => match self.press.take() {
Some((_, _, _, _, true)) => vec![Action::Commit],
// A click toggles smooth ↔ corner.
Some((id, si, ai, _, false)) => vec![Action::Exec("path.convertAnchor".into(), json!({"id": id, "subpath": si, "anchor": ai}))],
None => vec![],
},
_ => vec![],
}
}
fn cursor(&self, _cx: &ToolContext, _p: Point, _m: Mods) -> Cursor {
match self.kind {
Kind::Add => Cursor::PenAdd,
Kind::Scissors => Cursor::Crosshair,
Kind::Delete => Cursor::PenDelete,
Kind::Convert => Cursor::PenConvert,
}
}
fn busy(&self) -> bool {
self.press.is_some_and(|p| p.4)
}
}