Files
vectorcraft/crates/tools/src/xform.rs
T
dotCooCoo 763be536f5 Add Polygonal Lasso as an optional tool
Polygonal Lasso selects anchor points inside a loop of straight
segments. Each click sets a vertex, and a segment from the last vertex
follows the pointer. A double-click, Enter or a click on the first
vertex closes the loop. Backspace or Delete removes the last vertex,
Escape drops the loop, and Shift keeps the next segment to 45-degree
steps from the Constrain Angle. Shift held at the first click adds to
the selection and Alt subtracts from it, as with the Lasso. A loop of
fewer than three vertices selects nothing and records no undo step.

The tool is listed under its own heading in All Tools and under
Object › Plug-ins › Polygonal Lasso, with no default toolbar slot or
shortcut. On close it uses the Lasso's test for which anchor and mesh
points are inside (even-odd), now shared as lasso_select; the Lasso
itself behaves as before.

Touch mode now counts Polygonal Lasso and Enclosing Lasso as selection
tools, as it counts the Lasso, so the two-finger tap switches from them
to the drawing tool chosen last.

Fixes #4736
2026-11-15 19:17:09 -08:00

91 lines
3.4 KiB
Rust

//! Transform and utility tools (rotate/reflect/scale/shear, free transform, eyedropper, gradient,
//! artboard, magic wand, lasso, measure).
//!
//! Like every tool they only emit actions: transforms preview `object.transform` / `object.distort`
//! on the interaction snapshot, the utility tools execute selection / paint / artboard commands.
mod artboard;
pub(crate) mod eyedropper;
mod free;
mod freeform;
mod gradient;
mod measure;
mod transform;
mod wand;
use vectorcraft_doc::{Document, NodeId, NodeKind};
use vectorcraft_geom::{BezPath, Point, Rect, Shape};
use crate::{Overlay, Tool};
pub use artboard::ArtboardTool;
pub use eyedropper::EyedropperTool;
pub use free::{DistortMode, FreeTransformTool};
pub use gradient::GradientTool;
pub(crate) use measure::end_point as measure_end_point;
pub use measure::{MeasureTool, measure};
pub use transform::{TransformKind, TransformTool};
pub(crate) use wand::lasso_select;
pub use wand::{LassoTool, MagicWandTool};
/// Create a transform/utility tool by id (None = not one of ours).
pub fn create(id: &str) -> Option<Box<dyn Tool>> {
Some(match id {
"rotate" => Box::new(TransformTool::new(TransformKind::Rotate)),
"reflect" => Box::new(TransformTool::new(TransformKind::Reflect)),
"scale" => Box::new(TransformTool::new(TransformKind::Scale)),
"shear" => Box::new(TransformTool::new(TransformKind::Shear)),
"freeTransform" => Box::new(FreeTransformTool::default()),
"eyedropper" => Box::new(EyedropperTool::default()),
"gradient" => Box::new(GradientTool::default()),
"artboard" => Box::new(ArtboardTool::default()),
"magicWand" => Box::new(MagicWandTool),
"lasso" => Box::new(LassoTool::default()),
"measure" => Box::new(MeasureTool::default()),
_ => return None,
})
}
/// Cyan used for the reference point target and tool annotations.
pub const CYAN: [u8; 3] = [0x00, 0xa8, 0xff];
/// Illustrator's selection blue for bounding boxes.
pub const BLUE: [u8; 3] = [0x4a, 0x7c, 0xff];
/// The reference-point target (circle + crosshair) drawn at `p`, `r` in document units.
pub(crate) fn target_overlays(p: Point, r: f64) -> Vec<Overlay> {
let circle = vectorcraft_geom::kurbo::Circle::new(p, r * 0.6).to_path(0.05);
vec![
Overlay::Path { path: circle, color: CYAN, width: 1.0, dashed: false },
Overlay::Line { a: Point::new(p.x - r, p.y), b: Point::new(p.x + r, p.y), color: CYAN, dashed: false },
Overlay::Line { a: Point::new(p.x, p.y - r), b: Point::new(p.x, p.y + r), color: CYAN, dashed: false },
]
}
/// Closed polygon path through `pts`.
pub(crate) fn polygon(pts: &[Point], closed: bool) -> BezPath {
let mut bp = BezPath::new();
for (i, p) in pts.iter().enumerate() {
if i == 0 {
bp.move_to(*p);
} else {
bp.line_to(*p);
}
}
if closed && pts.len() > 2 {
bp.close_path();
}
bp
}
pub(crate) fn rect_corners(r: Rect) -> [Point; 4] {
[Point::new(r.x0, r.y0), Point::new(r.x1, r.y0), Point::new(r.x1, r.y1), Point::new(r.x0, r.y1)]
}
/// The node that owns the appearance for a hit leaf (a compound path's child → the compound).
pub fn paint_owner(doc: &Document, leaf: NodeId) -> NodeId {
match doc.parent_of(leaf) {
Some(p) if doc.node(p).is_some_and(|n| matches!(n.kind, NodeKind::Compound { .. })) => p,
_ => leaf,
}
}