mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-25 17:00:29 +00:00
Merge pull request #8460 from dotCooCoo/feat/transform-rect-ellipse-angle
Transform: Rectangle and Ellipse Properties show the shape's angle
This commit is contained in:
@@ -305,7 +305,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Live Shape Properties",
|
||||
[],
|
||||
None,
|
||||
"{id?, ids?, radius?: pt (or % of each corner's maximum when `relativeRadii` is true), kind?: \"round\"|\"invertedRound\"|\"chamfer\", relativeRadii?: bool (Relative rounding, as in the reference app: store radii as fractions of each corner's maximum so they scale with the shape), corners?: [i…], items?: [{id, corners?: [i…]}] (several objects, each with its own corners, in place of ids and corners: the canvas's corner widgets across a selection), sides?: n, polygonAngle?: degrees (counter-clockwise from its first vertex straight up), polygonRadius?: pt (centre to vertex), sideLength?: pt (sets the radius), makeSidesEqual?: true (a polygon scaled unevenly: drops the uneven scale and shear, keeping its centre, angle and mean radius), pieStart?, pieEnd?: degrees (an ellipse's pie, counter-clockwise from 3 o'clock; 0 to 360 is the whole ellipse), invertPie?: true (swaps them: the other part of the ellipse), constrainPie?: bool (Constrain Pie Angle: with one of pieStart and pieEnd, not both and not with invertPie, the other angle moves by as much, so each ellipse keeps its span), lineLength?: pt (a live line's length, above 0; its first end stays), lineAngle?: degrees (a live line's angle, counter-clockwise from 3 o'clock, from its first end to its second; it turns about its midpoint)} (Live Corners on any path: radius and kind set its corners (anchors that aren't smooth, where two sides, straight or curved (#1144), meet at an angle): `corners` (anchor indices of the path with its corners uncut, counting every subpath's anchors in order: a rectangle's 0 top-left, 1 top-right, 2 bottom-right, 3 bottom-left; a polygon's from the first vertex clockwise), else the corners with a Direct-Selected anchor, else every corner; each radius is drawn no larger than half the corner's shorter side allows; a path that isn't a live shape keeps its uncut outline so its corners stay editable, and is plain again once none is cut; sides: a polygon's, which keep the radius they shared; the polygon params apply in that order: sides, makeSidesEqual, polygonAngle, then sideLength or else polygonRadius; a line's lineLength applies before its lineAngle)",
|
||||
"{id?, ids?, radius?: pt (or % of each corner's maximum when `relativeRadii` is true), kind?: \"round\"|\"invertedRound\"|\"chamfer\", relativeRadii?: bool (Relative rounding, as in the reference app: store radii as fractions of each corner's maximum so they scale with the shape), corners?: [i…], items?: [{id, corners?: [i…]}] (several objects, each with its own corners, in place of ids and corners: the canvas's corner widgets across a selection), sides?: n, polygonAngle?: degrees (counter-clockwise from its first vertex straight up), polygonRadius?: pt (centre to vertex), sideLength?: pt (sets the radius), makeSidesEqual?: true (a polygon scaled unevenly: drops the uneven scale and shear, keeping its centre, angle and mean radius), pieStart?, pieEnd?: degrees (an ellipse's pie, counter-clockwise from 3 o'clock; 0 to 360 is the whole ellipse), invertPie?: true (swaps them: the other part of the ellipse), constrainPie?: bool (Constrain Pie Angle: with one of pieStart and pieEnd, not both and not with invertPie, the other angle moves by as much, so each ellipse keeps its span), lineLength?: pt (a live line's length, above 0; its first end stays), lineAngle?: degrees (a live line's angle, counter-clockwise from 3 o'clock, from its first end to its second; it turns about its midpoint), rectangleAngle?: degrees (a live rectangle's, counterclockwise from level), ellipseAngle?: degrees (a live ellipse's, counterclockwise from level; its pie turns with it)} (rectangleAngle, ellipseAngle and polygonAngle turn each shape of that kind about its own center and leave other objects as they are; a pixel-aligned shape then goes back on the pixel grid, as after other edits, which can move its center or change its size; an angle that is not a number is an error, and `null` counts as an omitted key; a shape that a turn would take past the coordinate limit (±4,000,000 pt) is an error that changes nothing; Live Corners on any path: radius and kind set its corners (anchors that aren't smooth, where two sides, straight or curved (#1144), meet at an angle): `corners` (anchor indices of the path with its corners uncut, counting every subpath's anchors in order: a rectangle's 0 top-left, 1 top-right, 2 bottom-right, 3 bottom-left; a polygon's from the first vertex clockwise), else the corners with a Direct-Selected anchor, else every corner; each radius is drawn no larger than half the corner's shorter side allows; a path that isn't a live shape keeps its uncut outline so its corners stay editable, and is plain again once none is cut; sides: a polygon's, which keep the radius they shared; the polygon params apply in that order: sides, makeSidesEqual, polygonAngle, then sideLength or else polygonRadius; a line's lineLength applies before its lineAngle)",
|
||||
has_selection,
|
||||
set_live_shape
|
||||
),
|
||||
|
||||
@@ -202,6 +202,16 @@ pub(super) fn set_live_shape(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
// A polygon's Polygon Properties: its angle, radius or side length, Make Sides Equal.
|
||||
let (polygon_angle, polygon_radius, side) = (angle("polygonAngle")?, length("polygonRadius")?, length("sideLength")?);
|
||||
let equal = p.get("makeSidesEqual").and_then(Value::as_bool) == Some(true);
|
||||
// A rectangle's Rectangle Angle and an ellipse's Ellipse Angle, as `polygonAngle` turns a polygon.
|
||||
let (rectangle_angle, ellipse_angle) = (angle("rectangleAngle")?, angle("ellipseAngle")?);
|
||||
// Whether a shape at `now` already stands at `a`: a turn the rounding of an earlier one left
|
||||
// (89.999999999°, or 359.9999999999° against 0°) counts as no change.
|
||||
let at_angle = |now: Option<f64>, a: f64| {
|
||||
now.is_some_and(|n| {
|
||||
let d = (n - a).rem_euclid(360.0);
|
||||
d.min(360.0 - d) < 1e-9
|
||||
})
|
||||
};
|
||||
// A line's Line Properties: its length (its first end stays) and its angle (about its midpoint).
|
||||
// A line's length can reach across the whole canvas, past `MAX_COORD`: its ends are checked below.
|
||||
let line_length = number("lineLength", |x| x > 0.0, "a length in points above 0")?;
|
||||
@@ -249,12 +259,15 @@ pub(super) fn set_live_shape(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
Some(l @ LiveShape::Polygon { sides: now, .. })
|
||||
if !(sides.is_none_or(|n| n.clamp(3, 1000) == u64::from(*now))
|
||||
&& (!equal || l.polygon_sides_equal())
|
||||
&& polygon_angle.is_none_or(|a| l.polygon_angle() == Some(a))
|
||||
&& polygon_angle.is_none_or(|a| at_angle(l.polygon_angle(), a))
|
||||
&& side.and_then(|len| l.polygon_radius_for_side(len)).or(polygon_radius).is_none_or(|r| l.polygon_radius() == Some(r))) =>
|
||||
{
|
||||
return false;
|
||||
}
|
||||
Some(LiveShape::Ellipse { pie, .. }) if pie_of(*pie) != *pie => return false,
|
||||
Some(l @ LiveShape::Ellipse { pie, .. }) if pie_of(*pie) != *pie || ellipse_angle.is_some_and(|a| !at_angle(l.shape_angle(), a)) => {
|
||||
return false;
|
||||
}
|
||||
Some(l @ LiveShape::Rectangle { .. }) if rectangle_angle.is_some_and(|a| !at_angle(l.shape_angle(), a)) => return false,
|
||||
_ => {}
|
||||
}
|
||||
if relative_radii.is_some_and(|on| live.as_deref().map(|l| l.radii_relative()) != Some(on)) {
|
||||
@@ -298,9 +311,18 @@ pub(super) fn set_live_shape(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
}
|
||||
*path = l.to_path();
|
||||
}
|
||||
if let Some(l @ LiveShape::Ellipse { .. }) = live.as_deref_mut()
|
||||
&& (pie_start.is_some() || pie_end.is_some() || invert)
|
||||
if let Some(a) = rectangle_angle
|
||||
&& let Some(l @ LiveShape::Rectangle { .. }) = live.as_deref_mut()
|
||||
{
|
||||
l.set_shape_angle(a);
|
||||
*path = l.to_path();
|
||||
}
|
||||
if let Some(l @ LiveShape::Ellipse { .. }) = live.as_deref_mut()
|
||||
&& (pie_start.is_some() || pie_end.is_some() || invert || ellipse_angle.is_some())
|
||||
{
|
||||
if let Some(a) = ellipse_angle {
|
||||
l.set_shape_angle(a);
|
||||
}
|
||||
if let LiveShape::Ellipse { pie, .. } = l {
|
||||
*pie = pie_of(*pie);
|
||||
}
|
||||
@@ -339,6 +361,11 @@ pub(super) fn set_live_shape(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
for id in &ids {
|
||||
super::pixelgrid::resnap(d, *id);
|
||||
}
|
||||
// A shape named in `ids` and not selected is not checked after the edit: one that a turn or
|
||||
// a new radius would take past the coordinate limit is an error here, and nothing changes.
|
||||
if !crate::sanity::objects_in_range(d, &ids) {
|
||||
return Err(bad("object.setLiveShape", format!("coordinate out of range (limit ±{MAX_COORD} pt)")));
|
||||
}
|
||||
Ok(())
|
||||
})?;
|
||||
ok()
|
||||
|
||||
@@ -585,6 +585,106 @@ fn a_polygon_takes_its_angle_radius_side_length_and_equal_sides() {
|
||||
}
|
||||
}
|
||||
|
||||
/// Rectangle Angle and Ellipse Angle: `rectangleAngle` and `ellipseAngle` turn each selected live
|
||||
/// rectangle or ellipse about its own center, keeping its size, corners and pie, as one undo step.
|
||||
/// The angle it already has records no step, each key leaves the other kind as it is, and a value
|
||||
/// that is not a number is an error.
|
||||
#[test]
|
||||
fn a_rectangle_and_an_ellipse_take_their_angle() {
|
||||
let (mut s, rect) = session();
|
||||
run(&mut s, json!({"radius": 8}));
|
||||
let ellipse = NodeId(s.execute("shape.ellipse", &json!({"x": 200, "y": 200, "width": 120, "height": 80})).unwrap()["id"].as_u64().unwrap());
|
||||
run(&mut s, json!({"pieStart": 30, "pieEnd": 120}));
|
||||
let live = |s: &Session, id: NodeId| {
|
||||
let n = s.doc().unwrap().doc.node(id).unwrap();
|
||||
(n.live_shape().unwrap().clone(), n.geometric_bounds().unwrap())
|
||||
};
|
||||
let steps = |s: &Session| s.doc().unwrap().history.undo.len();
|
||||
let close = |a: f64, b: f64| (a - b).abs() < 1e-6;
|
||||
let angle = |s: &Session, id: NodeId| live(s, id).0.shape_angle().unwrap();
|
||||
// The ellipse's center: the middle of its untransformed box, wherever its pie leaves the path.
|
||||
let ellipse_center = |s: &Session| match live(s, ellipse).0 {
|
||||
LiveShape::Ellipse { w, h, xf, .. } => xf * vectorcraft_geom::Point::new(w / 2.0, h / 2.0),
|
||||
l => panic!("{l:?}"),
|
||||
};
|
||||
let center = ellipse_center(&s);
|
||||
|
||||
s.execute("select.set", &json!({"ids": [rect.0]})).unwrap();
|
||||
let n = steps(&s);
|
||||
run(&mut s, json!({"rectangleAngle": 90}));
|
||||
let (l, b) = live(&s, rect);
|
||||
assert!(close(angle(&s, rect), 90.0), "{l:?}");
|
||||
// 100 × 60 at (10, 10) turned about (60, 40): 60 wide, 100 tall, same center.
|
||||
assert!(close(b.width(), 60.0) && close(b.height(), 100.0) && close(b.center().x, 60.0) && close(b.center().y, 40.0), "{b:?}");
|
||||
let LiveShape::Rectangle { w, h, radii, .. } = &l else { panic!("{l:?}") };
|
||||
assert!(close(*w, 100.0) && close(*h, 60.0) && *radii == [8.0; 4], "{w} {h} {radii:?}");
|
||||
assert_eq!(steps(&s), n + 1, "one undo step");
|
||||
for same in [json!({"rectangleAngle": 90}), json!({"rectangleAngle": 450}), json!({"rectangleAngle": -270}), json!({"ellipseAngle": 10})] {
|
||||
run(&mut s, same.clone());
|
||||
assert_eq!(steps(&s), n + 1, "{same} records no undo step");
|
||||
}
|
||||
// A turn the field gives back rounded is the angle the shape has: typing it again records no step.
|
||||
run(&mut s, json!({"rectangleAngle": 33.3}));
|
||||
let now = angle(&s, rect);
|
||||
run(&mut s, json!({"rectangleAngle": now}));
|
||||
run(&mut s, json!({"rectangleAngle": 33.3}));
|
||||
assert_eq!(steps(&s), n + 2);
|
||||
for bad in [json!({"rectangleAngle": "left"}), json!({"ellipseAngle": "up"}), json!({"rectangleAngle": true})] {
|
||||
assert!(s.execute("object.setLiveShape", &bad).is_err(), "{bad}");
|
||||
assert_eq!(steps(&s), n + 2, "{bad} changes nothing");
|
||||
}
|
||||
|
||||
// The ellipse turns about its center and keeps its pie.
|
||||
s.execute("select.set", &json!({"ids": [ellipse.0]})).unwrap();
|
||||
run(&mut s, json!({"ellipseAngle": 90}));
|
||||
let (l, b) = live(&s, ellipse);
|
||||
assert!(close(angle(&s, ellipse), 90.0), "{l:?}");
|
||||
let LiveShape::Ellipse { pie, .. } = &l else { panic!("{l:?}") };
|
||||
assert_eq!(*pie, (30.0, 120.0));
|
||||
let now = ellipse_center(&s);
|
||||
assert!(close(now.x, center.x) && close(now.y, center.y), "{center:?} → {now:?}");
|
||||
assert!(b.width() < b.height(), "120 × 80 turned a quarter turn: {b:?}");
|
||||
|
||||
// Both selected: each key turns its own kind only, each about its own center.
|
||||
s.execute("select.set", &json!({"ids": [rect.0, ellipse.0]})).unwrap();
|
||||
run(&mut s, json!({"rectangleAngle": 45}));
|
||||
assert!(close(angle(&s, rect), 45.0) && close(angle(&s, ellipse), 90.0));
|
||||
let b = live(&s, rect).1;
|
||||
assert!(close(b.center().x, 60.0) && close(b.center().y, 40.0), "{b:?}");
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
assert!(close(angle(&s, rect), 33.3));
|
||||
|
||||
// A reflected rectangle takes the angle too, about its center.
|
||||
s.execute("select.set", &json!({"ids": [rect.0]})).unwrap();
|
||||
s.execute("object.reflect", &json!({"axis": "vertical"})).unwrap();
|
||||
let before = live(&s, rect).1.center();
|
||||
run(&mut s, json!({"rectangleAngle": 20}));
|
||||
let after = live(&s, rect).1.center();
|
||||
assert!(close(angle(&s, rect), 20.0) && close(before.x, after.x) && close(before.y, after.y), "{before:?} → {after:?}");
|
||||
}
|
||||
|
||||
/// A shape named in `ids` that a turn would take past the coordinate limit (±4,000,000 pt) is an
|
||||
/// error that changes nothing, whether or not it is selected.
|
||||
#[test]
|
||||
fn a_turn_past_the_coordinate_limit_is_an_error() {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({"width": 400, "height": 400})).unwrap();
|
||||
let mut add = |cmd: &str, p: Value| s.execute(cmd, &p).unwrap()["id"].as_u64().unwrap();
|
||||
// Each reaches within 10 pt of the limit and past it when turned.
|
||||
let rect = add("shape.rectangle", json!({"x": 3_999_890, "y": 100, "width": 100, "height": 100}));
|
||||
let ellipse = add("shape.ellipse", json!({"x": 3_999_950, "y": 100, "width": 40, "height": 100}));
|
||||
let polygon = add("shape.polygon", json!({"cx": 3_999_955, "cy": 200, "radius": 50, "sides": 6}));
|
||||
for (id, key, angle) in [(rect, "rectangleAngle", 45), (ellipse, "ellipseAngle", 90), (polygon, "polygonAngle", 90)] {
|
||||
for selected in [false, true] {
|
||||
s.execute("select.set", &json!({"ids": if selected { vec![id] } else { vec![] }})).unwrap();
|
||||
let (doc, steps) = (s.doc().unwrap().doc.clone(), s.doc().unwrap().history.undo.len());
|
||||
let e = s.execute("object.setLiveShape", &json!({"ids": [id], key: angle})).unwrap_err();
|
||||
assert!(e.to_string().contains("out of range"), "{key}, selected {selected}: {e}");
|
||||
assert!(*s.doc().unwrap().doc == *doc && s.doc().unwrap().history.undo.len() == steps, "{key}, selected {selected}: changed");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A live line's Line Properties: Line Length keeps its first end in place, Line Angle (degrees
|
||||
/// counterclockwise, any finite value brought into 0 to 360) turns it about its midpoint, the
|
||||
/// length applies before the angle, every selected line takes them in one undo step, and a value
|
||||
|
||||
Reference in New Issue
Block a user