mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-26 19:36:16 +00:00
Merge pull request #9089 from dotCooCoo/fix/corners-relative-per-corner
Corners: a relative radius is a percentage of each corner's own maximum
This commit is contained in:
@@ -1525,7 +1525,7 @@ pub const UI_COMMANDS: &[(&str, &str, &str, &str)] = &[
|
||||
"ui.corners",
|
||||
"Corners…",
|
||||
"",
|
||||
"{id?, corners?: [i…]} open Corners for path `id` (default: the selected one) and its corners (anchor indices of the path with its corners uncut, as object.setLiveShape takes them: a rectangle's 0 top-left, 1 top-right, 2 bottom-right, 3 bottom-left; default: the Direct-Selected corners, else every corner) (dialog `corners`: kind (round|invertedRound|chamfer), radius (pt); double-clicking a corner widget with Direct Selection opens it too): OK runs object.setLiveShape",
|
||||
"{id?, corners?: [i…]} open Corners for path `id` (default: the selected one) and its corners (anchor indices of the path with its corners uncut, as object.setLiveShape takes them: a rectangle's 0 top-left, 1 top-right, 2 bottom-right, 3 bottom-left; default: the Direct-Selected corners, else every corner) (dialog `corners`: kind (round|invertedRound|chamfer), radii (relative|absolute), radius (pt, or with relative radii a percentage of each corner's own maximum); double-clicking a corner widget with Direct Selection opens it too): OK runs object.setLiveShape, as one command.batch undo step when relative radii give the corners different lengths",
|
||||
),
|
||||
(
|
||||
"ui.simplifyAuto",
|
||||
|
||||
@@ -115,21 +115,51 @@ fn ok(app: &mut VectorcraftApp, result: &ParamResult) -> Result<(), String> {
|
||||
if let Some(k) = f.text("kind") {
|
||||
p["kind"] = json!(k);
|
||||
}
|
||||
if let Some(mut r) = f.number("radius") {
|
||||
if relative {
|
||||
let id = NodeId(json_field(f, "id").as_u64().unwrap_or(0));
|
||||
let st = app.session.active().ok_or("no document")?;
|
||||
if let Some(lc) = st.doc.node(id).and_then(LiveCorners::of) {
|
||||
let corners: BTreeSet<usize> = serde_json::from_value(json_field(f, "corners")).unwrap_or_default();
|
||||
if let Some(k) = corners.iter().next() {
|
||||
let max = lc.corner(*k).map(|c| c.max_radius()).unwrap_or(0.0);
|
||||
r = (r / 100.0).clamp(0.0, 1.0) * max;
|
||||
}
|
||||
let Some(r) = f.number("radius") else {
|
||||
return app.run("object.setLiveShape", p).map(|_| ());
|
||||
};
|
||||
if !relative {
|
||||
p["radius"] = json!(r);
|
||||
return app.run("object.setLiveShape", p).map(|_| ());
|
||||
}
|
||||
// Relative: the percentage is of each corner's own maximum, so corners with different
|
||||
// maximums (a star's points and its inner corners) get different lengths. Corners of the
|
||||
// same length share one `object.setLiveShape`; several run as one undo step.
|
||||
let id = NodeId(json_field(f, "id").as_u64().unwrap_or(0));
|
||||
let st = app.session.active().ok_or("no document")?;
|
||||
let mut lengths: Vec<(f64, Vec<usize>)> = vec![];
|
||||
if let Some(lc) = st.doc.node(id).and_then(LiveCorners::of) {
|
||||
let corners: BTreeSet<usize> = serde_json::from_value(json_field(f, "corners")).unwrap_or_default();
|
||||
for k in corners {
|
||||
let len = (r / 100.0).clamp(0.0, 1.0) * lc.corner(k).map(|c| c.max_radius()).unwrap_or(0.0);
|
||||
match lengths.iter_mut().find(|(l, _)| (*l - len).abs() < 1e-9) {
|
||||
Some((_, ks)) => ks.push(k),
|
||||
None => lengths.push((len, vec![k])),
|
||||
}
|
||||
}
|
||||
p["radius"] = json!(r);
|
||||
}
|
||||
app.run("object.setLiveShape", p).map(|_| ())
|
||||
match &lengths[..] {
|
||||
[] => {
|
||||
p["radius"] = json!(r);
|
||||
app.run("object.setLiveShape", p).map(|_| ())
|
||||
}
|
||||
[(len, _)] => {
|
||||
p["radius"] = json!(len);
|
||||
app.run("object.setLiveShape", p).map(|_| ())
|
||||
}
|
||||
_ => {
|
||||
let commands: Vec<Value> = lengths
|
||||
.iter()
|
||||
.map(|(len, ks)| {
|
||||
let mut step = p.clone();
|
||||
step["corners"] = json!(ks);
|
||||
step["radius"] = json!(len);
|
||||
json!({"command": "object.setLiveShape", "params": step})
|
||||
})
|
||||
.collect();
|
||||
app.run("command.batch", json!({"label": "Live Shape", "commands": commands})).map(|_| ())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -277,4 +307,38 @@ mod tests {
|
||||
assert!(matches!(&n.kind, NodeKind::Path { live: Some(l), .. } if matches!(**l, LiveShape::Path { .. })));
|
||||
assert_eq!(crate::panels::corner_radius(&app, &n), Some(5.0));
|
||||
}
|
||||
|
||||
/// Relative rounding gives each corner the percentage of its own maximum: a star's point and
|
||||
/// the inner corner beside it differ, and OK rounds both as one undo step.
|
||||
#[test]
|
||||
fn relative_radius_is_a_percentage_of_each_corners_own_maximum() {
|
||||
let (mut app, _) = app();
|
||||
let id = app.run("shape.star", json!({"cx": 200, "cy": 200, "radius1": 60, "radius2": 30})).unwrap()["id"].as_u64().unwrap();
|
||||
app.run("object.setLiveShape", json!({"id": id, "radius": 1})).unwrap();
|
||||
let lc = |app: &VectorcraftApp| {
|
||||
let n = app.session.active().unwrap().doc.node(NodeId(id)).unwrap().clone();
|
||||
let c = LiveCorners::of(&n).unwrap();
|
||||
(c.radii_relative(), c.radius(0), c.radius(1), c.corner(0).unwrap().max_radius(), c.corner(1).unwrap().max_radius())
|
||||
};
|
||||
let (_, _, _, max0, max1) = lc(&app);
|
||||
assert!((max0 - max1).abs() > 1.0, "the corners' maximums differ: {max0} and {max1}");
|
||||
let steps = |app: &VectorcraftApp| app.session.doc().unwrap().history.undo.len();
|
||||
let n = steps(&app);
|
||||
app.run("ui.corners", json!({"id": id, "corners": [0, 1]})).unwrap();
|
||||
set(&app, "radii", json!("relative"));
|
||||
set(&app, "radius", json!(50));
|
||||
confirm(&mut app);
|
||||
assert!(!is_open(&app));
|
||||
let (relative, f0, f1, _, _) = lc(&app);
|
||||
assert!(relative);
|
||||
assert!((f0 - 0.5).abs() < 1e-9 && (f1 - 0.5).abs() < 1e-9, "stored fractions: {f0} and {f1}");
|
||||
assert_eq!(steps(&app), n + 1, "one undo step");
|
||||
// The same values again change nothing and record no step.
|
||||
app.run("ui.corners", json!({"id": id, "corners": [0, 1]})).unwrap();
|
||||
set(&app, "radius", json!(50));
|
||||
confirm(&mut app);
|
||||
assert_eq!(steps(&app), n + 1);
|
||||
app.run("edit.undo", json!({})).unwrap();
|
||||
assert!(!lc(&app).0, "undo brings back absolute radii");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1526,7 +1526,7 @@ pub const UI_COMMANDS: &[(&str, &str, &str, &str)] = &[
|
||||
"ui.corners",
|
||||
"Corners…",
|
||||
"",
|
||||
"{id?, corners?: [i…]} open Corners for path `id` (default: the selected one) and its corners (anchor indices of the path with its corners uncut, as object.setLiveShape takes them: a rectangle's 0 top-left, 1 top-right, 2 bottom-right, 3 bottom-left; default: the Direct-Selected corners, else every corner) (dialog `corners`: kind (round|invertedRound|chamfer), radius (pt); double-clicking a corner widget with Direct Selection opens it too): OK runs object.setLiveShape",
|
||||
"{id?, corners?: [i…]} open Corners for path `id` (default: the selected one) and its corners (anchor indices of the path with its corners uncut, as object.setLiveShape takes them: a rectangle's 0 top-left, 1 top-right, 2 bottom-right, 3 bottom-left; default: the Direct-Selected corners, else every corner) (dialog `corners`: kind (round|invertedRound|chamfer), radii (relative|absolute), radius (pt, or with relative radii a percentage of each corner's own maximum); double-clicking a corner widget with Direct Selection opens it too): OK runs object.setLiveShape, as one command.batch undo step when relative radii give the corners different lengths",
|
||||
),
|
||||
(
|
||||
"ui.simplifyAuto",
|
||||
|
||||
+2
-1
@@ -8228,7 +8228,8 @@ the Direct Selection tool those of any path. `rectanglesOnly: true` sets them on
|
||||
alone and leaves the other objects as they are (the Rectangle Properties corner fields and menus send it with the
|
||||
selection, so an action that records them acts on the rectangles selected when it plays). Several objects each with their own corners are one call:
|
||||
`items: [{id, corners?}]` in place of `ids` and `corners`. Alt-clicking one cycles their kind. Double-clicking one with Direct Selection opens Corners (`ui.corners {id?, corners?}`,
|
||||
dialog `corners`: `kind`, `radius`; OK runs `object.setLiveShape`). Double-clicking a selected live shape's corner
|
||||
dialog `corners`: `kind`, `radii`, `radius`, a percentage of each corner's own maximum while `radii` is `relative`; OK runs
|
||||
`object.setLiveShape`, as one undo step for corners whose maximums differ). Double-clicking a selected live shape's corner
|
||||
widget with the Selection tool shows and selects the Transform panel, as the Rectangle, Rounded Rectangle, Ellipse,
|
||||
Polygon and Star tools do. With the headless backend, the `pointer_gesture` reply carries `{"panel": "transform"}` in its
|
||||
`requests`; the desktop app runs `window.panel.activate {"panel": "transform"}` instead, and its reply has no `requests`. The tool, the selection and the document stay as they are, and
|
||||
|
||||
Reference in New Issue
Block a user