mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-05 13:32:45 -05:00
A star drawn with the Star tool, shape.star or addon.shapes.star was a plain path, so its point count could not change after it was drawn. - LiveShape::Star keeps the outer and inner radii, the point count (2 to 1000, clamped when read from a file) and Live Corners' radii and kinds. Its corners keep the radius they shared through a new count. - object.setLiveShape takes `points` (2 to 1000) and `ratio` (inner radius over outer radius, above 0 and up to 1), one undo step each and none when nothing changes. A new layout drops the star's selected anchors and segments; `points` with `radius` or `kind` is an error. - The side widget shows on a live star and sets 3 to 20 points. - Star Properties in the Transform panel and the Properties panel's Transform section have a Points field with a 3 to 20 slider and a Ratio field. - Shape Tools' configure keeps a whole star live. Fixes #4339
586 lines
28 KiB
Rust
586 lines
28 KiB
Rust
//! The side widget: a small diamond beside the right side of a selected live polygon's bounding
|
|
//! box (<https://helpx.adobe.com/illustrator/desktop/draw-shapes-and-paths/modify-live-shapes/add-or-remove-sides-from-polygons.html>).
|
|
//! Dragging it down adds sides and dragging it up removes them, one side per [`STEP_PX`] screen
|
|
//! pixels along the box's own vertical axis, from 3 to 11 sides. A polygon with more than 11
|
|
//! sides keeps its count until the drag goes up a step, which gives it 11. The drag sends
|
|
//! `object.setLiveShape`'s `sides` in one undo step, and none when the count ends where it began.
|
|
//! The Selection tool and the shape tools that show the box hit the widget, and the canvas draws it
|
|
//! from the same geometry.
|
|
//!
|
|
//! A live star shows the same widget
|
|
//! (<https://helpx.adobe.com/illustrator/desktop/draw-shapes-and-paths/modify-live-shapes/add-or-remove-sides-from-stars.html>):
|
|
//! its drag sends `points`, from 3 to 20, with the same rule for a count past either end.
|
|
|
|
use serde_json::json;
|
|
use vectorcraft_doc::{Document, LiveShape, NodeId, NodeKind, OrientedBox, Selection};
|
|
use vectorcraft_geom::Point;
|
|
|
|
use crate::select::shown_box;
|
|
use crate::{Action, Overlay, PointerEvent, ToolContext};
|
|
|
|
/// The widget's center sits this far (screen px) outside the middle of the box's right side, clear
|
|
/// of its handle.
|
|
const OFFSET_PX: f64 = 16.0;
|
|
/// Half the widget's diagonal (screen px), as the canvas draws it.
|
|
pub const RADIUS_PX: f32 = 4.0;
|
|
/// How near (screen px) the pointer must be to hit it.
|
|
const HIT_PX: f64 = 5.0;
|
|
/// How far (screen px) the pointer moves along the box's vertical axis per side.
|
|
pub const STEP_PX: f64 = 12.0;
|
|
/// The fewest sides a drag gives.
|
|
pub const MIN_SIDES: u32 = 3;
|
|
/// The most sides a drag gives.
|
|
pub const MAX_SIDES: u32 = 11;
|
|
/// The fewest points a drag gives a star.
|
|
pub const MIN_POINTS: u32 = 3;
|
|
/// The most points a drag gives a star.
|
|
pub const MAX_POINTS: u32 = 20;
|
|
|
|
/// The side widget of a selection that is exactly one editable live polygon or star.
|
|
#[derive(Clone, Copy, Debug, PartialEq)]
|
|
pub struct SideWidget {
|
|
pub id: NodeId,
|
|
/// Center in document coordinates.
|
|
pub at: Point,
|
|
/// The polygon's side count, or the star's point count.
|
|
pub sides: u32,
|
|
/// The shape is a star: the drag sets its points (3 to 20) instead of a polygon's sides.
|
|
pub star: bool,
|
|
}
|
|
|
|
impl SideWidget {
|
|
/// The widget beside `bx`, the selection's bounding box, at `zoom` (screen px per document
|
|
/// point). None unless the selection is one whole editable live polygon or star, with no
|
|
/// anchors or segments of it selected.
|
|
pub fn of(doc: &Document, selection: &Selection, bx: &OrientedBox, zoom: f64) -> Option<Self> {
|
|
let [id] = selection.objects[..] else { return None };
|
|
if !selection.anchors.is_empty() || selection.segments.contains_key(&id) || !zoom.is_finite() || zoom <= 0.0 {
|
|
return None;
|
|
}
|
|
let NodeKind::Path { live, .. } = &doc.node(id)?.kind else { return None };
|
|
let (sides, star) = match live.as_deref() {
|
|
Some(LiveShape::Polygon { sides, .. }) => (*sides, false),
|
|
Some(LiveShape::Star { points, .. }) => (*points, true),
|
|
_ => return None,
|
|
};
|
|
// After the kind: on a locked artboard it measures the art of the layer object holding `id`.
|
|
if !doc.is_editable(id) {
|
|
return None;
|
|
}
|
|
let r = bx.rect;
|
|
let at = bx.to_doc() * Point::new(r.x1 + OFFSET_PX / zoom, r.center().y);
|
|
(at.x.is_finite() && at.y.is_finite()).then_some(Self { id, at, sides, star })
|
|
}
|
|
|
|
/// The widget the tool can drag (its bounding box and the shape widgets showing).
|
|
pub fn for_tool(cx: &ToolContext) -> Option<(Self, OrientedBox)> {
|
|
// One object or none: a larger selection's box is not measured.
|
|
if !cx.shape_widgets || cx.selection.objects.len() != 1 {
|
|
return None;
|
|
}
|
|
let bx = shown_box(cx)?;
|
|
Some((Self::of(cx.doc, cx.selection, &bx, cx.zoom)?, bx))
|
|
}
|
|
|
|
/// The widget of `cx` and its box if the widget is under `p`.
|
|
fn at(cx: &ToolContext, p: Point) -> Option<(Self, OrientedBox)> {
|
|
Self::for_tool(cx).filter(|(w, _)| w.at.distance(p) <= cx.tol(HIT_PX))
|
|
}
|
|
}
|
|
|
|
/// Is `p` over the side widget?
|
|
pub fn over_widget(cx: &ToolContext, p: Point) -> bool {
|
|
SideWidget::at(cx, p).is_some()
|
|
}
|
|
|
|
/// The side count a drag of `steps` (down positive) gives a polygon of `from` sides: within 3 to 11,
|
|
/// and `from` itself while a polygon of more than 11 sides has not been dragged up.
|
|
pub fn sides_after(from: u32, steps: i64) -> u32 {
|
|
count_after(from, steps, MIN_SIDES, MAX_SIDES)
|
|
}
|
|
|
|
/// The point count a drag of `steps` (down positive) gives a star of `from` points: within 3 to 20,
|
|
/// and `from` itself while a star of more than 20 points has not been dragged up, or a star of
|
|
/// fewer than 3 has not been dragged down.
|
|
pub fn points_after(from: u32, steps: i64) -> u32 {
|
|
count_after(from, steps, MIN_POINTS, MAX_POINTS)
|
|
}
|
|
|
|
/// A count a drag of `steps` gives from `from`, within `min` to `max`. A count past `max` stays
|
|
/// until the drag goes up a step, which gives `max`; one below `min` stays until it goes down a
|
|
/// step, which gives `min`.
|
|
fn count_after(from: u32, steps: i64, min: u32, max: u32) -> u32 {
|
|
let (lo, hi) = (i64::from(min), i64::from(max));
|
|
let to = if from > max {
|
|
if steps >= 0 {
|
|
return from;
|
|
}
|
|
(hi + 1).saturating_add(steps)
|
|
} else if from < min {
|
|
if steps <= 0 {
|
|
return from;
|
|
}
|
|
(lo - 1).saturating_add(steps)
|
|
} else {
|
|
i64::from(from).saturating_add(steps)
|
|
};
|
|
to.clamp(lo, hi) as u32
|
|
}
|
|
|
|
/// Dragging the side widget.
|
|
#[derive(Clone, Debug)]
|
|
pub struct SideDrag {
|
|
widget: SideWidget,
|
|
bx: OrientedBox,
|
|
start: Point,
|
|
/// The pointer has left the click.
|
|
moved: bool,
|
|
/// The undo step has begun: the count has differed from the polygon's.
|
|
began: bool,
|
|
/// The count last sent (the polygon's until the drag changes it).
|
|
sides: u32,
|
|
at: Point,
|
|
}
|
|
|
|
impl SideDrag {
|
|
/// The drag has left the click.
|
|
pub fn adjusting(&self) -> bool {
|
|
self.moved
|
|
}
|
|
|
|
/// Start a drag when the press `ev` is on the side widget of the selection.
|
|
pub fn hit(cx: &ToolContext, ev: &PointerEvent) -> Option<Self> {
|
|
let (widget, bx) = SideWidget::at(cx, ev.pos)?;
|
|
Some(Self { widget, bx, start: ev.pos, moved: false, began: false, sides: widget.sides, at: ev.pos })
|
|
}
|
|
|
|
/// A pointer position that is not finite leaves the count and the readout as they are. A drag
|
|
/// too long to measure counts as the most steps in its direction.
|
|
pub fn drag(&mut self, cx: &ToolContext, p: Point) -> Vec<Action> {
|
|
if !p.x.is_finite() || !p.y.is_finite() {
|
|
return vec![];
|
|
}
|
|
self.at = p;
|
|
if !self.moved {
|
|
if p.distance(self.start) < cx.tol(3.0) {
|
|
return vec![];
|
|
}
|
|
self.moved = true;
|
|
}
|
|
let down = (self.bx.to_local(p).y - self.bx.to_local(self.start).y) * cx.zoom;
|
|
let steps = (down / STEP_PX).trunc();
|
|
if steps.is_nan() {
|
|
return vec![];
|
|
}
|
|
// The cast saturates: an infinite count of steps becomes i64::MAX or i64::MIN.
|
|
let sides = if self.widget.star { points_after(self.widget.sides, steps as i64) } else { sides_after(self.widget.sides, steps as i64) };
|
|
if sides == self.sides {
|
|
return vec![];
|
|
}
|
|
let mut out = vec![];
|
|
if !self.began {
|
|
self.began = true;
|
|
out.push(Action::Begin("Live Shape".into()));
|
|
}
|
|
self.sides = sides;
|
|
let key = if self.widget.star { "points" } else { "sides" };
|
|
out.push(Action::Preview("object.setLiveShape".into(), json!({ "id": self.widget.id.0, key: sides })));
|
|
out
|
|
}
|
|
|
|
/// The release: one undo step when the count changed, and nothing kept when it came back.
|
|
pub fn finish(self) -> Vec<Action> {
|
|
match (self.began, self.sides == self.widget.sides) {
|
|
(false, _) => vec![],
|
|
(true, true) => vec![Action::Cancel],
|
|
(true, false) => vec![Action::Commit],
|
|
}
|
|
}
|
|
|
|
/// Escape: the polygon keeps the count it had.
|
|
pub fn cancel(self) -> Vec<Action> {
|
|
if self.began { vec![Action::Cancel] } else { vec![] }
|
|
}
|
|
|
|
/// The side count next to the pointer.
|
|
pub fn overlays(&self, cx: &ToolContext) -> Vec<Overlay> {
|
|
if !self.moved || !cx.measurement_labels {
|
|
return vec![];
|
|
}
|
|
let label = if self.widget.star { "Points" } else { "Sides" };
|
|
vec![Overlay::Measure { p: self.at, text: format!("{label}: {}", self.sides) }]
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use crate::select::SelectionTool;
|
|
use crate::testutil::*;
|
|
use crate::{Cursor, Mods, PointerKind, Tool, ToolKey};
|
|
use vectorcraft_doc::{Appearance, Node};
|
|
use vectorcraft_geom::Affine;
|
|
|
|
/// A document holding a live polygon of `sides` sides and radius 100 placed by `xf`, and a
|
|
/// selection of it.
|
|
fn polygon_doc(sides: u32, xf: Affine) -> (Document, NodeId, Selection) {
|
|
shape_doc(LiveShape::Polygon { radius: 100.0, sides, xf, radii: vec![], kinds: vec![], radii_relative: false })
|
|
}
|
|
|
|
/// A document holding a live star of `points` points, radii 100 and 50, placed by `xf`, and a
|
|
/// selection of it.
|
|
fn star_doc(points: u32, xf: Affine) -> (Document, NodeId, Selection) {
|
|
shape_doc(LiveShape::Star { r1: 100.0, r2: 50.0, points, xf, radii: vec![], kinds: vec![], radii_relative: false })
|
|
}
|
|
|
|
/// A document holding the live shape `live`, and a selection of it.
|
|
fn shape_doc(live: LiveShape) -> (Document, NodeId, Selection) {
|
|
let mut d = Document::new(800.0, 800.0);
|
|
let l = d.layers[0].id;
|
|
let id = d.alloc_id();
|
|
let mut n = Node::path(id, live.to_path(), Appearance::default_art());
|
|
if let NodeKind::Path { live: slot, .. } = &mut n.kind {
|
|
*slot = Some(Box::new(live));
|
|
}
|
|
d.insert(Some(l), 0, n).unwrap();
|
|
let mut s = Selection::default();
|
|
s.add(id);
|
|
(d, id, s)
|
|
}
|
|
|
|
fn ev(kind: PointerKind, p: Point) -> PointerEvent {
|
|
PointerEvent::new(kind, p.x, p.y)
|
|
}
|
|
|
|
fn sides_of(a: &[Action]) -> Vec<u64> {
|
|
a.iter().filter_map(|a| if let Action::Preview(_, v) = a { v["sides"].as_u64() } else { None }).collect()
|
|
}
|
|
|
|
#[test]
|
|
fn the_counts_stay_within_three_and_eleven() {
|
|
assert_eq!(sides_after(5, 1), 6);
|
|
assert_eq!(sides_after(5, -1), 4);
|
|
assert_eq!(sides_after(5, 100), 11);
|
|
assert_eq!(sides_after(5, -100), 3);
|
|
assert_eq!(sides_after(3, -1), 3);
|
|
assert_eq!(sides_after(11, 1), 11);
|
|
// Over 11: unchanged until the drag goes up, which gives 11, then 10.
|
|
assert_eq!(sides_after(40, 0), 40);
|
|
assert_eq!(sides_after(40, 5), 40);
|
|
assert_eq!(sides_after(40, -1), 11);
|
|
assert_eq!(sides_after(40, -2), 10);
|
|
assert_eq!(sides_after(1000, -100), 3);
|
|
}
|
|
|
|
#[test]
|
|
fn a_star_s_counts_stay_within_three_and_twenty() {
|
|
assert_eq!(points_after(5, 1), 6);
|
|
assert_eq!(points_after(5, 100), 20);
|
|
assert_eq!(points_after(5, -100), 3);
|
|
assert_eq!(points_after(20, i64::MAX), 20);
|
|
// Over 20: unchanged until the drag goes up, which gives 20.
|
|
assert_eq!(points_after(40, 3), 40);
|
|
assert_eq!(points_after(40, -1), 20);
|
|
// Under 3 (a two-point star): unchanged until the drag goes down, which gives 3.
|
|
assert_eq!(points_after(2, 0), 2);
|
|
assert_eq!(points_after(2, -4), 2);
|
|
assert_eq!(points_after(2, 1), 3);
|
|
assert_eq!(points_after(2, 2), 4);
|
|
assert_eq!(points_after(2, i64::MIN), 2);
|
|
}
|
|
|
|
/// A live star shows the widget, and dragging it down a step gives the star a point, in one
|
|
/// undo step, with the count beside the pointer.
|
|
#[test]
|
|
fn dragging_a_star_s_widget_down_adds_a_point() {
|
|
let (d, id, s) = star_doc(5, Affine::translate((300.0, 300.0)));
|
|
let p = paint();
|
|
let c = cx(&d, &s, &p);
|
|
let (w, _) = SideWidget::for_tool(&c).expect("a widget on a live star");
|
|
assert_eq!((w.id, w.sides, w.star), (id, 5, true));
|
|
let mut t = SelectionTool::default();
|
|
t.pointer(&c, &ev(PointerKind::Down, w.at));
|
|
let to = w.at + vectorcraft_geom::Vec2::new(0.0, STEP_PX + 1.0);
|
|
let out = t.pointer(&c, &ev(PointerKind::Drag, to));
|
|
assert_eq!(out[0], Action::Begin("Live Shape".into()));
|
|
assert!(matches!(&out[1], Action::Preview(cmd, v) if cmd == "object.setLiveShape" && *v == json!({ "id": id.0, "points": 6 })), "{out:?}");
|
|
assert!(t.overlays(&c).iter().any(|o| matches!(o, Overlay::Measure { text, .. } if text == "Points: 6")));
|
|
assert_eq!(t.pointer(&c, &ev(PointerKind::Up, to)), vec![Action::Commit]);
|
|
}
|
|
|
|
#[test]
|
|
fn the_longest_drags_stay_within_three_and_eleven() {
|
|
assert_eq!(sides_after(5, i64::MAX), 11);
|
|
assert_eq!(sides_after(5, i64::MIN), 3);
|
|
assert_eq!(sides_after(3, i64::MAX), 11);
|
|
assert_eq!(sides_after(11, i64::MAX), 11);
|
|
assert_eq!(sides_after(11, i64::MIN), 3);
|
|
assert_eq!(sides_after(40, i64::MAX), 40);
|
|
assert_eq!(sides_after(40, i64::MIN), 3);
|
|
}
|
|
|
|
/// A drag far past the document, even one too long to measure, stops at 11 down and 3 up; a
|
|
/// pointer position that is not finite keeps the count it had.
|
|
#[test]
|
|
fn a_huge_or_invalid_drag_stays_within_three_and_eleven() {
|
|
let (d, _, s) = polygon_doc(5, Affine::translate((300.0, 300.0)));
|
|
let p = paint();
|
|
// At zoom 1 the step count is finite and past i64; at zoom 4 the distance itself is infinite.
|
|
for zoom in [1.0, 4.0] {
|
|
let c = ToolContext { zoom, ..cx(&d, &s, &p) };
|
|
let (w, _) = SideWidget::for_tool(&c).unwrap();
|
|
let far = |y: f64| Point::new(w.at.x, y);
|
|
let mut t = SelectionTool::default();
|
|
t.pointer(&c, &ev(PointerKind::Down, w.at));
|
|
assert_eq!(sides_of(&t.pointer(&c, &ev(PointerKind::Drag, far(1e308)))), [11], "zoom {zoom}");
|
|
assert_eq!(sides_of(&t.pointer(&c, &ev(PointerKind::Drag, far(-1e308)))), [3], "zoom {zoom}");
|
|
assert_eq!(t.pointer(&c, &ev(PointerKind::Up, far(-1e308))), vec![Action::Commit]);
|
|
}
|
|
let c = cx(&d, &s, &p);
|
|
let (w, _) = SideWidget::for_tool(&c).unwrap();
|
|
let mut t = SelectionTool::default();
|
|
t.pointer(&c, &ev(PointerKind::Down, w.at));
|
|
let shown = w.at + vectorcraft_geom::Vec2::new(0.0, 24.0);
|
|
assert_eq!(sides_of(&t.pointer(&c, &ev(PointerKind::Drag, shown))), [7]);
|
|
for bad in [Point::new(f64::NAN, w.at.y), Point::new(w.at.x, f64::INFINITY), Point::new(w.at.x, f64::NEG_INFINITY)] {
|
|
assert!(t.pointer(&c, &ev(PointerKind::Drag, bad)).is_empty(), "{bad:?}");
|
|
}
|
|
assert!(t.overlays(&c).iter().any(|o| matches!(o, Overlay::Measure { p, text } if *p == shown && text == "Sides: 7")));
|
|
assert_eq!(t.pointer(&c, &ev(PointerKind::Up, shown)), vec![Action::Commit]);
|
|
}
|
|
|
|
/// The widget sits 16 px right of the middle of the box's right side, at any zoom; only one
|
|
/// whole editable live polygon shows it.
|
|
#[test]
|
|
fn one_whole_live_polygon_shows_the_widget() {
|
|
let (d, id, s) = polygon_doc(6, Affine::translate((300.0, 300.0)));
|
|
let p = paint();
|
|
let c = cx(&d, &s, &p);
|
|
let (w, bx) = SideWidget::for_tool(&c).expect("a widget");
|
|
assert_eq!((w.id, w.sides), (id, 6));
|
|
let want = |zoom: f64| Point::new(bx.rect.x1 + 16.0 / zoom, bx.rect.center().y);
|
|
assert!(w.at.distance(want(1.0)) < 1e-9, "{w:?} {bx:?}");
|
|
assert!(SideWidget::of(&d, &s, &bx, 4.0).unwrap().at.distance(want(4.0)) < 1e-9);
|
|
assert!(over_widget(&c, w.at) && !over_widget(&c, w.at + vectorcraft_geom::Vec2::new(0.0, 8.0)));
|
|
// Hidden with the bounding box or the shape widgets hidden, with an anchor or segment
|
|
// selected, or with no zoom.
|
|
assert!(SideWidget::for_tool(&ToolContext { show_bbox: false, ..cx(&d, &s, &p) }).is_none());
|
|
let hidden = ToolContext { shape_widgets: false, ..cx(&d, &s, &p) };
|
|
assert!(SideWidget::for_tool(&hidden).is_none() && !over_widget(&hidden, w.at));
|
|
assert!(SideDrag::hit(&hidden, &PointerEvent::new(PointerKind::Down, w.at.x, w.at.y)).is_none());
|
|
let mut part = s.clone();
|
|
part.segments.insert(id, Default::default());
|
|
assert!(SideWidget::of(&d, &part, &bx, 1.0).is_none());
|
|
assert!(SideWidget::of(&d, &s, &bx, 0.0).is_none() && SideWidget::of(&d, &s, &bx, f64::NAN).is_none());
|
|
// A locked polygon, two polygons, nothing or a plain path: none.
|
|
let mut locked = d.clone();
|
|
locked.node_mut(id).unwrap().locked = true;
|
|
assert!(SideWidget::of(&locked, &s, &bx, 1.0).is_none());
|
|
let mut two = d.clone();
|
|
let other = two.alloc_id();
|
|
let mut n = two.node(id).unwrap().clone();
|
|
n.id = other;
|
|
let l = two.layers[0].id;
|
|
two.insert(Some(l), 0, n).unwrap();
|
|
let mut both = s.clone();
|
|
both.add(other);
|
|
assert!(SideWidget::of(&two, &both, &bx, 1.0).is_none());
|
|
assert!(SideWidget::of(&d, &Selection::default(), &bx, 1.0).is_none());
|
|
let (d, rect) = doc_with_rect();
|
|
let mut s = Selection::default();
|
|
s.add(rect);
|
|
let bx = OrientedBox::aligned(vectorcraft_geom::Rect::new(0.0, 0.0, 10.0, 10.0));
|
|
assert!(SideWidget::of(&d, &s, &bx, 1.0).is_none());
|
|
}
|
|
|
|
/// With the Selection tool, dragging the widget down adds a side per 12 screen px and up removes
|
|
/// them, within 3 to 11, in one undo step with the count shown next to the pointer.
|
|
#[test]
|
|
fn dragging_the_widget_changes_the_sides() {
|
|
let (d, id, s) = polygon_doc(5, Affine::translate((300.0, 300.0)));
|
|
let p = paint();
|
|
for zoom in [1.0, 2.0] {
|
|
let c = ToolContext { zoom, ..cx(&d, &s, &p) };
|
|
let (w, _) = SideWidget::for_tool(&c).unwrap();
|
|
let at = |px: f64| w.at + vectorcraft_geom::Vec2::new(0.0, px / zoom);
|
|
let mut t = SelectionTool::default();
|
|
assert_eq!(t.cursor(&c, w.at, Mods::default()), Cursor::ArrowHollow);
|
|
assert!(t.pointer(&c, &ev(PointerKind::Down, w.at)).is_empty());
|
|
// Past the click but short of a step: nothing yet.
|
|
assert!(t.pointer(&c, &ev(PointerKind::Drag, at(6.0))).is_empty());
|
|
assert!(t.transforming());
|
|
let a = t.pointer(&c, &ev(PointerKind::Drag, at(12.0)));
|
|
assert_eq!(a[0], Action::Begin("Live Shape".into()));
|
|
assert_eq!(a[1], Action::Preview("object.setLiveShape".into(), json!({ "id": id.0, "sides": 6 })));
|
|
assert!(t.overlays(&c).iter().any(|o| matches!(o, Overlay::Measure { text, .. } if text == "Sides: 6")));
|
|
// The same count again sends nothing; far down stops at 11, far up at 3.
|
|
assert!(t.pointer(&c, &ev(PointerKind::Drag, at(20.0))).is_empty());
|
|
assert_eq!(sides_of(&t.pointer(&c, &ev(PointerKind::Drag, at(500.0)))), [11]);
|
|
assert_eq!(sides_of(&t.pointer(&c, &ev(PointerKind::Drag, at(-500.0)))), [3]);
|
|
assert_eq!(t.pointer(&c, &ev(PointerKind::Up, at(-500.0))), vec![Action::Commit]);
|
|
assert!(!t.busy() && !t.transforming());
|
|
}
|
|
// A click sends nothing, and the readout follows Measurement Labels.
|
|
let c = cx(&d, &s, &p);
|
|
let (w, _) = SideWidget::for_tool(&c).unwrap();
|
|
let mut t = SelectionTool::default();
|
|
assert!(t.pointer(&c, &ev(PointerKind::Down, w.at)).is_empty());
|
|
assert!(t.pointer(&c, &ev(PointerKind::Up, w.at)).is_empty());
|
|
t.pointer(&c, &ev(PointerKind::Down, w.at));
|
|
t.pointer(&c, &ev(PointerKind::Drag, w.at + vectorcraft_geom::Vec2::new(0.0, 13.0)));
|
|
let quiet = ToolContext { measurement_labels: false, ..cx(&d, &s, &p) };
|
|
assert!(!t.overlays(&quiet).iter().any(|o| matches!(o, Overlay::Measure { .. })));
|
|
// Back at the start: the release keeps nothing.
|
|
assert_eq!(sides_of(&t.pointer(&c, &ev(PointerKind::Drag, w.at))), [5]);
|
|
assert_eq!(t.pointer(&c, &ev(PointerKind::Up, w.at)), vec![Action::Cancel]);
|
|
}
|
|
|
|
/// On a turned polygon the drag is read along the box's own vertical axis.
|
|
#[test]
|
|
fn a_turned_polygon_reads_the_drag_along_its_box() {
|
|
let xf = Affine::translate((400.0, 400.0)) * Affine::rotate(std::f64::consts::FRAC_PI_2);
|
|
let (d, _, s) = polygon_doc(5, xf);
|
|
let p = paint();
|
|
let c = cx(&d, &s, &p);
|
|
let (w, bx) = SideWidget::for_tool(&c).unwrap();
|
|
let down = bx.to_doc() * Point::new(0.0, 1.0) - bx.to_doc() * Point::ZERO;
|
|
let mut t = SelectionTool::default();
|
|
t.pointer(&c, &ev(PointerKind::Down, w.at));
|
|
assert_eq!(sides_of(&t.pointer(&c, &ev(PointerKind::Drag, w.at + down * 25.0))), [7], "{bx:?}");
|
|
assert_eq!(sides_of(&t.pointer(&c, &ev(PointerKind::Drag, w.at - down * 13.0))), [4]);
|
|
}
|
|
|
|
/// A double-click on the widget with the Selection tool does nothing: in isolation mode it does
|
|
/// not leave isolation, though no art is under the widget.
|
|
#[test]
|
|
fn a_double_click_on_the_widget_does_nothing() {
|
|
let live = LiveShape::Polygon {
|
|
radius: 100.0,
|
|
sides: 5,
|
|
xf: Affine::translate((300.0, 300.0)),
|
|
radii: vec![],
|
|
kinds: vec![],
|
|
radii_relative: false,
|
|
};
|
|
let mut d = Document::new(800.0, 800.0);
|
|
let l = d.layers[0].id;
|
|
let id = d.alloc_id();
|
|
let mut n = Node::path(id, live.to_path(), Appearance::default_art());
|
|
if let NodeKind::Path { live: slot, .. } = &mut n.kind {
|
|
*slot = Some(Box::new(live));
|
|
}
|
|
let group = d.alloc_id();
|
|
d.insert(Some(l), 0, Node::group(group, vec![std::sync::Arc::new(n)])).unwrap();
|
|
let mut s = Selection::default();
|
|
s.add(id);
|
|
let p = paint();
|
|
let c = ToolContext { isolation: Some(group), ..cx(&d, &s, &p) };
|
|
let (w, _) = SideWidget::for_tool(&c).unwrap();
|
|
assert!(vectorcraft_doc::hit::hit_test(&d, w.at, c.hit_options()).is_none());
|
|
let mut t = SelectionTool::default();
|
|
assert!(t.pointer(&c, &ev(PointerKind::DoubleClick, w.at)).is_empty());
|
|
// Away from the widget and the art, the double-click leaves isolation.
|
|
let away = w.at + vectorcraft_geom::Vec2::new(40.0, 0.0);
|
|
assert_eq!(t.pointer(&c, &ev(PointerKind::DoubleClick, away)), vec![Action::Exec("object.exitIsolation".into(), json!({}))]);
|
|
}
|
|
|
|
/// A polygon of more than 11 sides keeps its count on a press and a drag down; the first step
|
|
/// up gives 11.
|
|
#[test]
|
|
fn a_polygon_over_eleven_sides_does_not_jump() {
|
|
let (d, _, s) = polygon_doc(40, Affine::translate((300.0, 300.0)));
|
|
let p = paint();
|
|
let c = cx(&d, &s, &p);
|
|
let (w, _) = SideWidget::for_tool(&c).unwrap();
|
|
let mut t = SelectionTool::default();
|
|
assert!(t.pointer(&c, &ev(PointerKind::Down, w.at)).is_empty());
|
|
assert!(t.pointer(&c, &ev(PointerKind::Drag, w.at + vectorcraft_geom::Vec2::new(0.0, 40.0))).is_empty());
|
|
assert!(t.pointer(&c, &ev(PointerKind::Up, w.at + vectorcraft_geom::Vec2::new(0.0, 40.0))).is_empty());
|
|
t.pointer(&c, &ev(PointerKind::Down, w.at));
|
|
let a = t.pointer(&c, &ev(PointerKind::Drag, w.at - vectorcraft_geom::Vec2::new(0.0, 12.0)));
|
|
assert_eq!(a[0], Action::Begin("Live Shape".into()));
|
|
assert_eq!(sides_of(&a), [11]);
|
|
assert!(t.overlays(&c).iter().any(|o| matches!(o, Overlay::Measure { text, .. } if text == "Sides: 11")));
|
|
assert_eq!(sides_of(&t.pointer(&c, &ev(PointerKind::Drag, w.at - vectorcraft_geom::Vec2::new(0.0, 24.0)))), [10]);
|
|
}
|
|
|
|
/// Escape during a drag puts the count back; the release after it sends nothing.
|
|
#[test]
|
|
fn escape_cancels_a_side_widget_drag() {
|
|
let (d, _, s) = polygon_doc(5, Affine::translate((300.0, 300.0)));
|
|
let p = paint();
|
|
let c = cx(&d, &s, &p);
|
|
let (w, _) = SideWidget::for_tool(&c).unwrap();
|
|
let mut t = SelectionTool::default();
|
|
t.pointer(&c, &ev(PointerKind::Down, w.at));
|
|
t.pointer(&c, &ev(PointerKind::Drag, w.at + vectorcraft_geom::Vec2::new(0.0, 30.0)));
|
|
assert_eq!(t.key(&c, ToolKey::Escape, Mods::default()), vec![Action::Cancel]);
|
|
assert!(!t.busy() && !t.transforming());
|
|
assert!(!t.overlays(&c).iter().any(|o| matches!(o, Overlay::Measure { .. })));
|
|
assert!(t.pointer(&c, &ev(PointerKind::Up, w.at)).is_empty());
|
|
}
|
|
|
|
/// Switching tools during a drag puts the count back, as Escape does; a press that changed
|
|
/// nothing sends nothing.
|
|
#[test]
|
|
fn switching_tools_cancels_a_side_widget_drag() {
|
|
let (d, _, s) = polygon_doc(5, Affine::translate((300.0, 300.0)));
|
|
let p = paint();
|
|
let c = cx(&d, &s, &p);
|
|
let (w, _) = SideWidget::for_tool(&c).unwrap();
|
|
let mut t = SelectionTool::default();
|
|
t.pointer(&c, &ev(PointerKind::Down, w.at));
|
|
assert_eq!(sides_of(&t.pointer(&c, &ev(PointerKind::Drag, w.at + vectorcraft_geom::Vec2::new(0.0, 30.0)))), [7]);
|
|
assert_eq!(t.deactivate(&c), vec![Action::Cancel]);
|
|
assert!(!t.busy() && !t.transforming());
|
|
t.pointer(&c, &ev(PointerKind::Down, w.at));
|
|
assert!(t.pointer(&c, &ev(PointerKind::Drag, w.at + vectorcraft_geom::Vec2::new(0.0, 6.0))).is_empty());
|
|
assert!(t.deactivate(&c).is_empty());
|
|
assert!(!t.busy() && !t.transforming());
|
|
}
|
|
|
|
/// The release is read where it happens, with or without a drag event there first: back at the
|
|
/// start the polygon keeps its count with no undo step, and further down it takes the new count.
|
|
#[test]
|
|
fn the_release_position_sets_the_sides() {
|
|
let (d, id, s) = polygon_doc(5, Affine::translate((300.0, 300.0)));
|
|
let p = paint();
|
|
let c = cx(&d, &s, &p);
|
|
let (w, _) = SideWidget::for_tool(&c).unwrap();
|
|
let at = |px: f64| w.at + vectorcraft_geom::Vec2::new(0.0, px);
|
|
let mut t = SelectionTool::default();
|
|
t.pointer(&c, &ev(PointerKind::Down, w.at));
|
|
assert_eq!(sides_of(&t.pointer(&c, &ev(PointerKind::Drag, at(12.0)))), [6]);
|
|
assert_eq!(
|
|
t.pointer(&c, &ev(PointerKind::Up, w.at)),
|
|
vec![Action::Preview("object.setLiveShape".into(), json!({ "id": id.0, "sides": 5 })), Action::Cancel]
|
|
);
|
|
assert!(!t.busy() && !t.transforming());
|
|
t.pointer(&c, &ev(PointerKind::Down, w.at));
|
|
assert_eq!(sides_of(&t.pointer(&c, &ev(PointerKind::Drag, at(12.0)))), [6]);
|
|
assert_eq!(
|
|
t.pointer(&c, &ev(PointerKind::Up, at(24.0))),
|
|
vec![Action::Preview("object.setLiveShape".into(), json!({ "id": id.0, "sides": 7 })), Action::Commit]
|
|
);
|
|
}
|
|
|
|
/// A double-click on the widget with the Selection tool does nothing to the artwork under it.
|
|
#[test]
|
|
fn a_double_click_on_the_widget_leaves_the_artwork_under_it_alone() {
|
|
let (mut d, _, s) = polygon_doc(5, Affine::translate((300.0, 300.0)));
|
|
let p = paint();
|
|
let (w, _) = SideWidget::for_tool(&cx(&d, &s, &p)).unwrap();
|
|
let l = d.layers[0].id;
|
|
let under = d.alloc_id();
|
|
let at = d.layers[0].children().map_or(0, |c| c.len());
|
|
let r = vectorcraft_geom::Rect::new(w.at.x - 20.0, w.at.y - 20.0, w.at.x + 20.0, w.at.y + 20.0);
|
|
d.insert(Some(l), at, Node::path(under, vectorcraft_geom::shapes::rectangle(r), Appearance::default_art())).unwrap();
|
|
let c = cx(&d, &s, &p);
|
|
assert!(over_widget(&c, w.at));
|
|
let mut t = SelectionTool::default();
|
|
assert!(t.pointer(&c, &ev(PointerKind::DoubleClick, w.at)).is_empty());
|
|
// Beside the widget, the double-click reaches the artwork.
|
|
assert!(!t.pointer(&c, &ev(PointerKind::DoubleClick, w.at + vectorcraft_geom::Vec2::new(0.0, 15.0))).is_empty());
|
|
}
|
|
}
|