mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-19 12:38:18 +00:00
Merge pull request #8670 from storytold/train/carry8122
Ellipse: change anchor count with arrow keys while drawing (#8122)
This commit is contained in:
@@ -0,0 +1,197 @@
|
||||
//! Ellipse arrow keys edit the anchor count of the shape actually being dragged.
|
||||
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
|
||||
|
||||
use serde_json::{Value, json};
|
||||
use vectorcraft_doc::{LiveShape, Node, NodeId};
|
||||
use vectorcraft_engine::{Session, ViewInfo};
|
||||
use vectorcraft_geom::{AnchorKind, Rect};
|
||||
use vectorcraft_tools::{Mods, PointerEvent, PointerKind, ToolKey};
|
||||
|
||||
fn run(s: &mut Session, command: &str, params: Value) -> Value {
|
||||
s.execute(command, ¶ms).unwrap()
|
||||
}
|
||||
|
||||
fn view() -> ViewInfo {
|
||||
ViewInfo { smart_guides: false, snap_to_point: false, snap_to_grid: false, snap_to_pixel: false, ..ViewInfo::default() }
|
||||
}
|
||||
|
||||
fn fixture() -> (Session, NodeId) {
|
||||
let mut s = Session::new();
|
||||
run(&mut s, "file.new", json!({"width": 700, "height": 500}));
|
||||
let other = NodeId(run(&mut s, "shape.rectangle", json!({"x": 400, "y": 300, "width": 30, "height": 20}))["id"].as_u64().unwrap());
|
||||
run(&mut s, "select.none", json!({}));
|
||||
s.select_tool("ellipse", view()).unwrap();
|
||||
(s, other)
|
||||
}
|
||||
|
||||
fn pointer(s: &mut Session, view: ViewInfo, kind: PointerKind, x: f64, y: f64, mods: Mods) {
|
||||
s.pointer(&PointerEvent::new(kind, x, y).with_mods(mods), view).unwrap();
|
||||
}
|
||||
|
||||
fn begin(s: &mut Session, view: ViewInfo, mods: Mods) {
|
||||
run(s, "select.none", json!({}));
|
||||
pointer(s, view, PointerKind::Down, 100.0, 100.0, mods);
|
||||
pointer(s, view, PointerKind::Drag, 220.0, 180.0, mods);
|
||||
}
|
||||
|
||||
fn key(s: &mut Session, view: ViewInfo, key: ToolKey) {
|
||||
s.tool_key(key, Mods::default(), view).unwrap();
|
||||
}
|
||||
|
||||
fn drawn(s: &Session) -> Node {
|
||||
let st = s.doc().unwrap();
|
||||
assert_eq!(st.selection.objects.len(), 1, "one drawn ellipse is selected");
|
||||
st.doc.node(st.selection.objects[0]).unwrap().clone()
|
||||
}
|
||||
|
||||
fn count(s: &Session) -> usize {
|
||||
drawn(s).path_data().unwrap().anchor_count()
|
||||
}
|
||||
|
||||
fn bounds_are(node: &Node, expected: Rect) {
|
||||
let got = node.geometric_bounds().unwrap();
|
||||
for (a, b) in [(got.x0, expected.x0), (got.y0, expected.y0), (got.x1, expected.x1), (got.y1, expected.y1)] {
|
||||
assert!((a - b).abs() < 1e-6, "bounds {got:?}, expected {expected:?}");
|
||||
}
|
||||
}
|
||||
|
||||
fn document(s: &Session) -> Value {
|
||||
json!(s.doc().unwrap().doc)
|
||||
}
|
||||
|
||||
fn depth(s: &Session) -> usize {
|
||||
s.doc().unwrap().history.undo.len()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ellipse_drag_anchors_baseline_default_four_is_the_live_ellipse() {
|
||||
let (mut s, _) = fixture();
|
||||
let before = depth(&s);
|
||||
begin(&mut s, view(), Mods::default());
|
||||
let preview = drawn(&s);
|
||||
assert_eq!(preview.path_data().unwrap().anchor_count(), 4);
|
||||
assert!(matches!(preview.live_shape(), Some(LiveShape::Ellipse { .. })));
|
||||
bounds_are(&preview, Rect::new(100.0, 100.0, 220.0, 180.0));
|
||||
pointer(&mut s, view(), PointerKind::Up, 220.0, 180.0, Mods::default());
|
||||
assert_eq!(depth(&s), before + 1);
|
||||
assert!(matches!(drawn(&s).live_shape(), Some(LiveShape::Ellipse { .. })));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ellipse_drag_anchors_baseline_up_changes_preview_and_commits_once() {
|
||||
let (mut s, other) = fixture();
|
||||
let before = document(&s);
|
||||
let other_before = json!(s.doc().unwrap().doc.node(other));
|
||||
let history = depth(&s);
|
||||
begin(&mut s, view(), Mods::default());
|
||||
assert_eq!(count(&s), 4, "normal drag before the key");
|
||||
key(&mut s, view(), ToolKey::Up);
|
||||
assert_eq!(count(&s), 5, "Up changes the shape currently previewed");
|
||||
let preview = drawn(&s);
|
||||
assert!(preview.live_shape().is_none(), "a non-four count is the existing plain path");
|
||||
assert!(preview.path_data().unwrap().subpaths.iter().all(|sp| sp.closed));
|
||||
assert!(preview.path_data().unwrap().subpaths.iter().flat_map(|sp| &sp.anchors).all(|a| a.kind == AnchorKind::Smooth));
|
||||
bounds_are(&preview, Rect::new(100.0, 100.0, 220.0, 180.0));
|
||||
pointer(&mut s, view(), PointerKind::Up, 220.0, 180.0, Mods::default());
|
||||
assert_eq!(count(&s), 5);
|
||||
bounds_are(&drawn(&s), Rect::new(100.0, 100.0, 220.0, 180.0));
|
||||
assert_eq!(drawn(&s).appearance, preview.appearance);
|
||||
assert_eq!(json!(s.doc().unwrap().doc.node(other)), other_before);
|
||||
assert_eq!(depth(&s), history + 1, "keys update one preview, not separate edits");
|
||||
let after = document(&s);
|
||||
run(&mut s, "edit.undo", json!({}));
|
||||
assert_eq!(document(&s), before);
|
||||
run(&mut s, "edit.redo", json!({}));
|
||||
assert_eq!(document(&s), after);
|
||||
let idle = document(&s);
|
||||
key(&mut s, view(), ToolKey::Up);
|
||||
assert_eq!(document(&s), idle, "an idle tool key does not change existing art");
|
||||
assert_eq!(s.tool_options()["anchors"], 5);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ellipse_drag_anchors_limits_remembered_count_and_cancel_baseline() {
|
||||
for (initial, pressed, expected) in [(2, ToolKey::Down, 2), (999, ToolKey::Up, 1000)] {
|
||||
let (mut s, _) = fixture();
|
||||
s.set_tool_option("anchors", &json!(initial));
|
||||
begin(&mut s, view(), Mods::default());
|
||||
assert_eq!(count(&s), initial, "the remembered click count starts the drag");
|
||||
key(&mut s, view(), pressed);
|
||||
key(&mut s, view(), pressed);
|
||||
assert_eq!(count(&s), expected, "one step per key, clamped to the existing domain");
|
||||
bounds_are(&drawn(&s), Rect::new(100.0, 100.0, 220.0, 180.0));
|
||||
pointer(&mut s, view(), PointerKind::Up, 220.0, 180.0, Mods::default());
|
||||
s.select_tool("selection", view()).unwrap();
|
||||
s.select_tool("ellipse", view()).unwrap();
|
||||
assert_eq!(s.tool_options()["anchors"], expected);
|
||||
}
|
||||
let (mut s, _) = fixture();
|
||||
s.set_tool_option("anchors", &json!(6));
|
||||
let before = document(&s);
|
||||
let history = depth(&s);
|
||||
begin(&mut s, view(), Mods::default());
|
||||
key(&mut s, view(), ToolKey::Up);
|
||||
assert_eq!(count(&s), 7);
|
||||
key(&mut s, view(), ToolKey::Escape);
|
||||
assert_eq!(document(&s), before);
|
||||
assert_eq!(depth(&s), history);
|
||||
assert_eq!(s.tool_options()["anchors"], 6, "Cancel restores the count before the drag");
|
||||
begin(&mut s, view(), Mods::default());
|
||||
key(&mut s, view(), ToolKey::Up);
|
||||
s.set_tool_option("anchors", &json!(9));
|
||||
key(&mut s, view(), ToolKey::Up);
|
||||
assert_eq!(count(&s), 10);
|
||||
key(&mut s, view(), ToolKey::Escape);
|
||||
assert_eq!(document(&s), before);
|
||||
assert_eq!(depth(&s), history);
|
||||
assert_eq!(s.tool_options()["anchors"], 9, "an explicit setting updates the Cancel baseline");
|
||||
s.select_tool("selection", view()).unwrap();
|
||||
s.select_tool("ellipse", view()).unwrap();
|
||||
assert_eq!(s.tool_options()["anchors"], 9, "the restored option survives switching tools");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ellipse_drag_anchors_keys_keep_shift_alt_space_and_turned_axes() {
|
||||
for angle in [0.0, 90.0] {
|
||||
let (mut s, _) = fixture();
|
||||
s.prefs.constrain_angle = angle;
|
||||
let mods = Mods { shift: true, alt: true, ..Mods::default() };
|
||||
pointer(&mut s, view(), PointerKind::Down, 100.0, 100.0, mods);
|
||||
pointer(&mut s, view(), PointerKind::Drag, 180.0, 160.0, mods);
|
||||
bounds_are(&drawn(&s), Rect::new(20.0, 20.0, 180.0, 180.0));
|
||||
key(&mut s, view(), ToolKey::Up);
|
||||
assert_eq!(count(&s), 5);
|
||||
bounds_are(&drawn(&s), Rect::new(20.0, 20.0, 180.0, 180.0));
|
||||
let moving = Mods { space: true, ..mods };
|
||||
pointer(&mut s, view(), PointerKind::Drag, 200.0, 180.0, moving);
|
||||
bounds_are(&drawn(&s), Rect::new(40.0, 40.0, 200.0, 200.0));
|
||||
key(&mut s, view(), ToolKey::Up);
|
||||
assert_eq!(count(&s), 6);
|
||||
bounds_are(&drawn(&s), Rect::new(40.0, 40.0, 200.0, 200.0));
|
||||
pointer(&mut s, view(), PointerKind::Up, 200.0, 180.0, moving);
|
||||
assert_eq!(count(&s), 6);
|
||||
bounds_are(&drawn(&s), Rect::new(40.0, 40.0, 200.0, 200.0));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ellipse_drag_anchors_key_keeps_the_tangent_snapped_box() {
|
||||
let (mut s, _) = fixture();
|
||||
let line = NodeId(run(&mut s, "shape.line", json!({"x1": 40, "y1": 200, "x2": 260, "y2": 200}))["id"].as_u64().unwrap());
|
||||
let line_before = json!(s.doc().unwrap().doc.node(line));
|
||||
run(&mut s, "select.none", json!({}));
|
||||
s.prefs.snap_tangent = true;
|
||||
let snap = ViewInfo { smart_guides: true, snap_to_point: false, ..view() };
|
||||
let mods = Mods { shift: true, alt: true, ..Mods::default() };
|
||||
pointer(&mut s, snap, PointerKind::Down, 100.0, 100.0, mods);
|
||||
pointer(&mut s, snap, PointerKind::Drag, 198.0, 130.0, mods);
|
||||
// The pointer alone gives radius98; the horizontal line pulls the shown radius to100.
|
||||
bounds_are(&drawn(&s), Rect::new(0.0, 0.0, 200.0, 200.0));
|
||||
key(&mut s, snap, ToolKey::Up);
|
||||
assert_eq!(count(&s), 5);
|
||||
bounds_are(&drawn(&s), Rect::new(0.0, 0.0, 200.0, 200.0));
|
||||
pointer(&mut s, snap, PointerKind::Up, 198.0, 130.0, mods);
|
||||
assert_eq!(count(&s), 5);
|
||||
bounds_are(&drawn(&s), Rect::new(0.0, 0.0, 200.0, 200.0));
|
||||
assert_eq!(json!(s.doc().unwrap().doc.node(line)), line_before);
|
||||
}
|
||||
@@ -113,8 +113,7 @@ pub struct ShapeTool {
|
||||
began: bool,
|
||||
pub sides: u32,
|
||||
pub points: u32,
|
||||
/// The anchors an ellipse made by a click has (the Ellipse dialog opens on it); 4 is a live
|
||||
/// ellipse.
|
||||
/// The anchors an ellipse has (the Ellipse dialog opens on it); 4 is a live ellipse.
|
||||
pub anchors: u32,
|
||||
pub corner_radius: f64,
|
||||
/// Star inner radius as a fraction of the outer radius.
|
||||
@@ -180,6 +179,7 @@ fn circle_through_points(points: &[Point]) -> (String, Value) {
|
||||
struct DragOptions {
|
||||
sides: u32,
|
||||
points: u32,
|
||||
anchors: u32,
|
||||
corner_radius: f64,
|
||||
star_ratio: f64,
|
||||
}
|
||||
@@ -286,13 +286,14 @@ impl ShapeTool {
|
||||
}
|
||||
|
||||
fn drag_options(&self) -> DragOptions {
|
||||
DragOptions { sides: self.sides, points: self.points, corner_radius: self.corner_radius, star_ratio: self.star_ratio }
|
||||
DragOptions { sides: self.sides, points: self.points, anchors: self.anchors, corner_radius: self.corner_radius, star_ratio: self.star_ratio }
|
||||
}
|
||||
|
||||
/// Put back the options a canceled drag changed.
|
||||
fn restore_drag_options(&mut self, o: DragOptions) {
|
||||
self.sides = o.sides;
|
||||
self.points = o.points;
|
||||
self.anchors = o.anchors;
|
||||
self.corner_radius = o.corner_radius;
|
||||
self.star_ratio = o.star_ratio;
|
||||
}
|
||||
@@ -607,6 +608,8 @@ impl ShapeTool {
|
||||
let mut v = box_params(start, p, m, base);
|
||||
if self.id == "roundedRectangle" {
|
||||
v["radius"] = json!(self.corner_radius);
|
||||
} else if self.id == "ellipse" && self.anchors != 4 {
|
||||
v["anchors"] = json!(self.anchors);
|
||||
}
|
||||
(cmd.into(), v)
|
||||
}
|
||||
@@ -891,6 +894,14 @@ impl Tool for ShapeTool {
|
||||
self.points = self.points.saturating_sub(1).max(3);
|
||||
true
|
||||
}
|
||||
("ellipse", ToolKey::Up) => {
|
||||
self.anchors = self.anchors.saturating_add(1).min(1000);
|
||||
true
|
||||
}
|
||||
("ellipse", ToolKey::Down) => {
|
||||
self.anchors = self.anchors.saturating_sub(1).max(2);
|
||||
true
|
||||
}
|
||||
("roundedRectangle", ToolKey::Up) => {
|
||||
self.corner_radius = (self.corner_radius + 1.0).min(MAX_CORNER_RADIUS);
|
||||
true
|
||||
@@ -902,7 +913,9 @@ impl Tool for ShapeTool {
|
||||
_ => false,
|
||||
};
|
||||
if changed {
|
||||
let (c, v) = self.command_at(s, self.last, self.mods, cx.constrain_angle);
|
||||
// A count-only key keeps the corner a tangent-snapped circle already showed.
|
||||
let p = self.tangent_corner.map_or(self.last, |d| s + d);
|
||||
let (c, v) = self.command_at(s, p, self.mods, cx.constrain_angle);
|
||||
vec![self.preview(c, v)]
|
||||
} else if key == ToolKey::Escape {
|
||||
self.start = None;
|
||||
@@ -1103,6 +1116,7 @@ impl Tool for ShapeTool {
|
||||
match key {
|
||||
"sides" => b.sides = self.sides,
|
||||
"points" => b.points = self.points,
|
||||
"anchors" => b.anchors = self.anchors,
|
||||
"cornerRadius" => b.corner_radius = self.corner_radius,
|
||||
"starRatio" => b.star_ratio = self.star_ratio,
|
||||
_ => {}
|
||||
|
||||
@@ -151,6 +151,8 @@ macOS cursor delivery (#2292) is narrowed to a native host adapter using the exi
|
||||
|
||||
- **Polygon/Star count persistence:** Exact-size placement saves the chosen count for later drags and reopening the existing canvas dialog. Regression coverage includes both tools, repeated drags, switching tools and Cancel. The remaining G1 scope and estimates are unchanged.
|
||||
|
||||
- **Ellipse drag anchor count (#7840):** Up/Down changes the remembered Anchors count from 2 to 1000 in the current preview box. The existing click-dialog count starts later drags; Escape restores the earlier count while an explicit `tool.setOption` update is kept. Regression target: `crates/engine/tests/ellipse_drag_anchors.rs`. Broader G1 scope and estimates unchanged.
|
||||
|
||||
- **Artboard Options shares existing presets and orientation controls and can fit artwork without moving it; artwork fitting includes all drawn document art across artboards and off-board; artwork bounds are cached by active document and revision, and selection uses the shared bounds cache across artboards:** Shared existing preset definitions are reused; no external preset assets are added. Presets preserve each selected artboard's orientation, and orientation choices apply even when the active board already matches. Fit operations draft bounds until OK, affect only the active artboard and leave artwork in place. Regression coverage checks draft/Cancel, selected-set application, active-only fitting and Undo. Broader scope and estimates unchanged.
|
||||
|
||||
- **Implemented, regression-tested and contributor-confirmed:** status messages and readouts use stable responsive allocations sized for localized labels and ordinary coordinates. Original-file acceptance confirmed 2026-10-19; broader G1 scope and estimates unchanged. Sample widths are cached by language, document unit and pixels per point rather than remeasured every frame.
|
||||
|
||||
+2
-1
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
+3
-2
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user