mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-26 22:27:08 +00:00
Width Point Edit: OK and Delete act only on the stroke and the paired points it opened on
- OK and Delete on several width points are refused when a selected point became, or stopped being, one half of a discontinuous point since the dialog opened, for example after a point was added at its place. Before, Delete removed only the selected half and left the new one, and OK edited only the selected half. - OK and Delete are refused when `ui.dialog.set` changed `id`, `index`, `outIndex` or `indices` after the dialog opened. Before, a changed `id` sent the edit or the removal to another stroke whose width points matched.
This commit is contained in:
@@ -32,7 +32,9 @@
|
||||
//! `outSide2`, `outTotalWidth` and `outLinked`.
|
||||
//!
|
||||
//! OK and Delete on existing points, one or several, are refused when those points changed since
|
||||
//! the dialog opened or another document is active.
|
||||
//! the dialog opened (their place, their widths, or which of them pair as a discontinuous point) or
|
||||
//! another document is active. OK and Delete are also refused when `id`, `index`, `outIndex` or
|
||||
//! `indices` no longer holds the value it had when the dialog opened.
|
||||
|
||||
use egui::vec2;
|
||||
use serde_json::{Value, json};
|
||||
@@ -65,6 +67,10 @@ const DOC: &str = "__doc";
|
||||
/// The place and side widths `[t, left, right]` (points) of the existing point the one-point dialog
|
||||
/// opened on, and of the outgoing half of a discontinuous point.
|
||||
const AT: &str = "__at";
|
||||
/// The values of [`TARGET_KEYS`] when the dialog opened.
|
||||
const TARGET: &str = "__target";
|
||||
/// The fields that name the stroke and the points OK and Delete act on.
|
||||
const TARGET_KEYS: [&str; 4] = ["id", "index", "outIndex", "indices"];
|
||||
|
||||
/// The fields of each set of side widths of several points: Side 1, Side 2, Total Width and the
|
||||
/// link, for the incoming set and the outgoing set.
|
||||
@@ -296,6 +302,14 @@ fn pairs(points: &[(f64, f64, f64)]) -> Vec<Option<usize>> {
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// The sets of side widths that edit width point `i`, `[incoming, outgoing]`, with the points
|
||||
/// paired as `pairs` pairs them: an outgoing half is not edited by the incoming set, and an
|
||||
/// incoming half not by the outgoing set.
|
||||
fn sets(pairs: &[Option<usize>], i: usize) -> [bool; 2] {
|
||||
let outgoing = i.checked_sub(1).is_some_and(|a| pairs[a] == Some(i));
|
||||
[!outgoing, pairs[i].is_none()]
|
||||
}
|
||||
|
||||
/// The width points `indices` of `id` names, each discontinuous point's two halves together.
|
||||
fn target(app: &VectorcraftApp, id: u64, indices: &Value) -> Result<Target, String> {
|
||||
const NONE: &str = "no such width point";
|
||||
@@ -326,7 +340,7 @@ fn target(app: &VectorcraftApp, id: u64, indices: &Value) -> Result<Target, Stri
|
||||
let half = st.width / 2.0;
|
||||
let entry = |i: usize| {
|
||||
let (t, l, r) = points[i];
|
||||
Entry { index: i, t, left: l * half, right: r * half, sets: [!outgoing(i), pairs[i].is_none()] }
|
||||
Entry { index: i, t, left: l * half, right: r * half, sets: sets(&pairs, i) }
|
||||
};
|
||||
Ok(Target::Several(picked.into_iter().map(entry).collect()))
|
||||
}
|
||||
@@ -345,9 +359,15 @@ fn open_several(app: &mut VectorcraftApp, id: u64, es: &[Entry], adjust: bool) {
|
||||
d.fields.insert(keys[3].into(), json!(linked));
|
||||
}
|
||||
store(&mut d, es, None);
|
||||
d.fields.insert(TARGET.into(), targets(&d));
|
||||
app.ui.dialog = Some(d);
|
||||
}
|
||||
|
||||
/// The values of `d`'s [`TARGET_KEYS`] (null for one it does not hold).
|
||||
fn targets(d: &Dialog) -> Value {
|
||||
json!(TARGET_KEYS.map(|key| d.fields.get(key).cloned().unwrap_or(Value::Null)))
|
||||
}
|
||||
|
||||
/// Open Width Point Edit for `index`, or a prospective point at fraction `t` without an index
|
||||
/// (`{id, index?, t?, adjustAdjoining?}`), or for several existing points (`{id, indices,
|
||||
/// adjustAdjoining?}`). Opening only samples the stroke: confirmation creates the point.
|
||||
@@ -409,7 +429,9 @@ pub fn open(app: &mut VectorcraftApp, p: &Value) -> Result<Value, String> {
|
||||
fields[key] = value;
|
||||
}
|
||||
}
|
||||
app.ui.dialog = Some(Dialog::new(KIND, fields));
|
||||
let mut d = Dialog::new(KIND, fields);
|
||||
d.fields.insert(TARGET.into(), targets(&d));
|
||||
app.ui.dialog = Some(d);
|
||||
Ok(Value::Null)
|
||||
}
|
||||
|
||||
@@ -547,6 +569,12 @@ fn same_document(app: &VectorcraftApp, d: &Dialog) -> Result<(), String> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether the dialog still names the stroke and points it opened on: `ui.dialog.set` can set
|
||||
/// `id`, `index`, `outIndex` and `indices`, and OK and Delete refuse any other value.
|
||||
fn same_target(d: &Dialog) -> Result<(), String> {
|
||||
if d.fields.get(TARGET) == Some(&targets(d)) { Ok(()) } else { Err("no such width point".into()) }
|
||||
}
|
||||
|
||||
/// `id`'s width profile and the stroke's half weight, when the points `open` are still where and as
|
||||
/// wide as when the dialog opened, exactly.
|
||||
fn opened_profile(app: &VectorcraftApp, id: u64, open: &[Entry]) -> Result<(WidthProfile, f64), String> {
|
||||
@@ -567,6 +595,12 @@ fn opened_profile(app: &VectorcraftApp, id: u64, open: &[Entry]) -> Result<(Widt
|
||||
return Err(NONE.into());
|
||||
}
|
||||
}
|
||||
// Each point is still a continuous point or the same half of a discontinuous one: a point added
|
||||
// at the place of one would leave it changing only one half.
|
||||
let pairs = pairs(&profile.points);
|
||||
if open.iter().any(|o| o.sets != sets(&pairs, o.index)) {
|
||||
return Err(NONE.into());
|
||||
}
|
||||
Ok((profile, half))
|
||||
}
|
||||
|
||||
@@ -636,6 +670,7 @@ fn confirm_several(app: &mut VectorcraftApp, d: &Dialog) -> Result<Value, String
|
||||
let id = d.fields.get("id").and_then(Value::as_u64).ok_or("no such width point")?;
|
||||
let open = entries(d, OPEN);
|
||||
same_document(app, d)?;
|
||||
same_target(d)?;
|
||||
opened_profile(app, id, &open)?;
|
||||
if d.bool("discard") {
|
||||
let indices: Vec<usize> = open.iter().map(|e| e.index).collect();
|
||||
@@ -677,8 +712,9 @@ fn confirm(app: &mut VectorcraftApp, d: &Dialog) -> Result<Value, String> {
|
||||
app.ui.dialog = None;
|
||||
return Ok(Value::Null);
|
||||
}
|
||||
// OK and Delete act only on the document and the points the dialog opened on.
|
||||
// OK and Delete act only on the document, the stroke and the points the dialog opened on.
|
||||
same_document(app, d)?;
|
||||
same_target(d)?;
|
||||
if let Some(index) = index.as_u64().and_then(|i| usize::try_from(i).ok()) {
|
||||
unchanged_point(app, d, index, out.as_ref().and_then(Value::as_u64).and_then(|i| usize::try_from(i).ok()))?;
|
||||
}
|
||||
|
||||
@@ -1047,3 +1047,70 @@ fn one_point_refuses_a_change_since_the_dialog_opened() {
|
||||
assert_eq!(app.session.doc().unwrap().doc, paired, "{field}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn several_points_refuse_a_change_in_which_points_pair() {
|
||||
// A point added at a selected point's place, after it, pairs with it; a third point at a
|
||||
// discontinuous point's place unpairs its halves. The selected points keep their index, place
|
||||
// and widths.
|
||||
let cases = [
|
||||
(
|
||||
three_app as fn() -> (VectorcraftApp, u64),
|
||||
json!([1, 3]),
|
||||
json!([[0, 1, 1], [0.25, 0.8, 0.4], [0.5, 0.8, 0.8], [0.75, 0.4, 1.2], [0.75, 0.3, 0.3], [1, 1, 1]]),
|
||||
),
|
||||
(pair_app, json!([0, 1]), json!([[0, 1, 1], [0.5, 0.8, 0.8], [0.5, 0.4, 1.2], [0.5, 0.3, 0.3], [1, 1, 1]])),
|
||||
];
|
||||
for (make, indices, points) in cases {
|
||||
for field in ["side2", "discard"] {
|
||||
let (mut app, id) = make();
|
||||
app.run("ui.widthPointEdit", json!({"id": id, "indices": indices})).unwrap();
|
||||
assert!(app.ui.dialog.as_ref().unwrap().fields.contains_key(OPEN), "{indices}");
|
||||
app.run("stroke.widthProfile.set", json!({"ids": [id], "points": points})).unwrap();
|
||||
let changed = app.session.doc().unwrap().doc.clone();
|
||||
let d = app.ui.dialog.as_mut().unwrap();
|
||||
d.fields.insert(field.into(), if field == "discard" { json!(true) } else { json!(5) });
|
||||
assert!(super::super::confirm(&mut app).is_err(), "{indices} {field}");
|
||||
assert_eq!(app.session.doc().unwrap().doc, changed, "{indices} {field}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ok_and_delete_refuse_another_stroke_or_point_set_into_the_dialog() {
|
||||
let set = |app: &mut VectorcraftApp, field: &str, value: Value| {
|
||||
let (req, _) = crate::control::ControlRequest::new("ui.dialog.set", json!({"field": field, "value": value}));
|
||||
let crate::control::Outcome::Done(r) = crate::control::handle(app, &egui::Context::default(), &req) else {
|
||||
panic!("expected an immediate reply")
|
||||
};
|
||||
assert_eq!(r["ok"], json!(true), "{field}: {r}");
|
||||
};
|
||||
// `field` set to `value` on the dialog opened with `opened`, then `action` and OK.
|
||||
let cases = [
|
||||
(json!({"indices": [1, 3]}), "id", None, "side1"),
|
||||
(json!({"indices": [1, 3]}), "id", None, "discard"),
|
||||
(json!({"indices": [1, 3]}), "indices", Some(json!([1, 2])), "side1"),
|
||||
(json!({"index": 2}), "id", None, "side1"),
|
||||
(json!({"index": 2}), "id", None, "discard"),
|
||||
(json!({"index": 2}), "index", Some(json!(1)), "discard"),
|
||||
(json!({"t": 0.4}), "id", None, "side1"),
|
||||
];
|
||||
for (opened, field, value, action) in cases {
|
||||
let (mut app, id) = three_app();
|
||||
// Another stroke in the document with the same weight and width points.
|
||||
let other = app.run("shape.line", json!({"x1": 100, "y1": 250, "x2": 300, "y2": 250})).unwrap()["id"].as_u64().unwrap();
|
||||
app.run("stroke.set", json!({"ids": [other], "weight": 10})).unwrap();
|
||||
let profile = json!([[0, 1, 1], [0.25, 0.8, 0.4], [0.5, 0.8, 0.8], [0.75, 0.4, 1.2], [1, 1, 1]]);
|
||||
app.run("stroke.widthProfile.set", json!({"ids": [other], "points": profile})).unwrap();
|
||||
let before = app.session.doc().unwrap().doc.clone();
|
||||
let mut p = opened.clone();
|
||||
p["id"] = json!(id);
|
||||
app.run("ui.widthPointEdit", p).unwrap();
|
||||
set(&mut app, field, value.unwrap_or(json!(other)));
|
||||
let d = app.ui.dialog.as_mut().unwrap();
|
||||
d.fields.insert(action.into(), if action == "discard" { json!(true) } else { json!(5) });
|
||||
assert!(super::super::confirm(&mut app).is_err(), "{opened} {field} {action}");
|
||||
assert_eq!(app.session.doc().unwrap().doc, before, "{opened} {field} {action}");
|
||||
assert!(app.ui.dialog.is_some(), "{opened} {field} {action}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -656,7 +656,9 @@ as it was; `totalWidth` scales each point's own two sides to that total; a null
|
||||
`stroke.widthPoint.remove {id, indices}`. With `adjustAdjoining`, each changed point changes the nearest unselected
|
||||
point at another place before and after it in proportion to its change since the dialog opened; a point next to two
|
||||
changed points takes both changes. These rules for several points are VectorCraft's own. Confirm and Delete on existing
|
||||
points, one or several, are refused when those points changed since the dialog opened or another document is active.
|
||||
points, one or several, are refused when those points changed since the dialog opened (their place, their widths, or
|
||||
which of them pair as a discontinuous point) or another document is active. Confirm and Delete are also refused when
|
||||
`ui.dialog.set` changed `id`, `index`, `outIndex` or `indices` from the value it had when the dialog opened.
|
||||
|
||||
Corners: double-clicking a Live Corners widget with the Selection or Direct Selection tool, or `ui.corners {id?,
|
||||
corners?}`, opens the `corners` dialog for a path's corners (the Direct-Selected ones, else every corner; fields `id`,
|
||||
|
||||
+3
-2
@@ -8611,8 +8611,9 @@ turn it off to keep each point's other side), and Total Width scales each point'
|
||||
one `stroke.widthProfile.set` (one undo step, none when nothing changed), and Delete removes them all; with a
|
||||
discontinuous point among them, Incoming and Outgoing edit its two halves and both edit the continuous points.
|
||||
Malformed or out-of-range `indices`, or more than one of `index`, `indices` and `t`, are errors that open no dialog. OK
|
||||
and Delete on existing points are refused when those points changed since the dialog opened or another document is
|
||||
active.
|
||||
and Delete on existing points are refused when those points changed since the dialog opened (their place, their widths,
|
||||
or which of them pair as a discontinuous point) or another document is active. OK and Delete are also refused when
|
||||
`id`, `index`, `outIndex` or `indices` no longer holds the value it had when the dialog opened.
|
||||
`document.inspect` reports a path's `strokeOptions.widthPoints` (`[t, left, right]`, factors of half the weight; null
|
||||
for a uniform stroke). `stroke.widthPoint.remove {id, index | indices}` deletes points; `stroke.widthProfile.set {ids,
|
||||
points}` replaces them all, as dragging several Shift-selected points with the Width tool does in one undo step.
|
||||
|
||||
Reference in New Issue
Block a user