mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-18 07:49:26 +00:00
Brushes: Options of Selected Object keeps each path's values the dialog did not change
With several brushed paths selected, editing one end of a range in the dialog sent the whole range, so every path took the first path's other end. The dialog now sends only the items and colorization fields that changed, and brush.objectOptions reads a null item of an array as each path's own item, else the brush's. A null mode keeps the mode the path has with its other values, and modes that change nothing are not stored.
This commit is contained in:
+42
-4
@@ -865,12 +865,26 @@ impl BrushKind {
|
||||
}
|
||||
}
|
||||
|
||||
/// Stroke option value `new` with each `null` item of an array taken from the same place in
|
||||
/// `current`, an array of the same length: a stroke option that changes some items of a range or a
|
||||
/// list of modes and keeps the others ([`Brush::with_stroke_options`]). Any other `new` is given as
|
||||
/// it is.
|
||||
pub fn keep_items(new: &serde_json::Value, current: &serde_json::Value) -> serde_json::Value {
|
||||
match (new, current) {
|
||||
(serde_json::Value::Array(n), serde_json::Value::Array(c)) if n.len() == c.len() => {
|
||||
serde_json::Value::Array(n.iter().zip(c).map(|(n, c)| if n.is_null() { c.clone() } else { n.clone() }).collect())
|
||||
}
|
||||
_ => new.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
impl Brush {
|
||||
/// The brush as a stroke whose own settings are `options` draws it: each field of
|
||||
/// [`BrushKind::stroke_option_keys`] in `options` replaces the brush's value (a `colorization`
|
||||
/// object changes only the fields it gives), and the values are kept within their ranges
|
||||
/// ([`Brush::sanitize`]). Fields the type does not take are passed over. An error when a value
|
||||
/// is of the wrong kind.
|
||||
/// ([`Brush::sanitize`]). A `null` item of an array keeps the brush's item there
|
||||
/// ([`keep_items`]). Fields the type does not take are passed over. An error when a value is of
|
||||
/// the wrong kind.
|
||||
pub fn with_stroke_options(&self, options: &serde_json::Map<String, serde_json::Value>) -> Result<Brush, String> {
|
||||
let keys = self.kind.stroke_option_keys();
|
||||
let mut values = self.kind.option_values();
|
||||
@@ -885,8 +899,11 @@ impl Brush {
|
||||
inner.insert(f.clone(), fv.clone());
|
||||
}
|
||||
}
|
||||
// Set below, once the values the modes may follow are in.
|
||||
_ if k == "modes" => {}
|
||||
_ => {
|
||||
o.insert(k.clone(), v.clone());
|
||||
let current = o.get(k).cloned().unwrap_or_default();
|
||||
o.insert(k.clone(), keep_items(v, ¤t));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -899,7 +916,28 @@ impl Brush {
|
||||
c.insert("key".into(), serde_json::to_value(key).map_err(|e| e.to_string())?);
|
||||
}
|
||||
let mut b = self.clone();
|
||||
b.kind.set_option_values(values).map_err(|e| e.to_string())?;
|
||||
b.kind.set_option_values(values.clone()).map_err(|e| e.to_string())?;
|
||||
if let Some(m) = options.get("modes")
|
||||
&& keys.contains(&"modes")
|
||||
{
|
||||
// A null item keeps the mode the brush with the other settings has: modes it leaves to
|
||||
// its values follow the stroke's values, as they are kept within their ranges. Modes
|
||||
// equal to those are left as the brush has them, so they keep following the values.
|
||||
b.sanitize();
|
||||
let current = match &b.kind {
|
||||
BrushKind::Calligraphic(c) => serde_json::to_value(c.modes()).unwrap_or_default(),
|
||||
BrushKind::Scatter(s) => serde_json::to_value(s.modes()).unwrap_or_default(),
|
||||
_ => serde_json::Value::Null,
|
||||
};
|
||||
let modes = keep_items(m, ¤t);
|
||||
if modes != current {
|
||||
if let Some(o) = values.as_object_mut() {
|
||||
o.insert("modes".into(), modes);
|
||||
}
|
||||
b = self.clone();
|
||||
b.kind.set_option_values(values).map_err(|e| e.to_string())?;
|
||||
}
|
||||
}
|
||||
b.sanitize();
|
||||
Ok(b)
|
||||
}
|
||||
|
||||
@@ -2098,3 +2098,23 @@ fn stroke_options_are_checked_and_kept_within_their_ranges() {
|
||||
let odd = stroke_pieces(&doc, &brush, &path, &with_options(json!({"width": [1, 2]})));
|
||||
assert!(near(bounds(&odd).height(), 4.0, 1e-6));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn null_stroke_option_items_keep_the_items_the_stroke_has() {
|
||||
let map = |v: serde_json::Value| v.as_object().cloned().unwrap_or_default();
|
||||
// A scatter brush at fixed sizes, with no modes of its own: they follow its values.
|
||||
let b = Brush { name: "s".into(), kind: BrushKind::Scatter(Scatter { size: (100.0, 100.0), ..Default::default() }) };
|
||||
let BrushKind::Scatter(s) = b.with_stroke_options(&map(json!({"size": [null, 150]}))).unwrap().kind else { panic!("scatter") };
|
||||
assert_eq!(s.size, (100.0, 150.0), "the smallest size stays the brush's");
|
||||
// A stroke whose sizes vary keeps that random size mode when it sets only the spacing mode.
|
||||
let e = b.with_stroke_options(&map(json!({"size": [50, 80], "modes": [null, "pressure", null, null]}))).unwrap();
|
||||
let BrushKind::Scatter(s) = e.kind else { panic!("scatter") };
|
||||
assert_eq!(s.modes(), [Variation::Random, Variation::Pressure, Variation::Fixed, Variation::Fixed]);
|
||||
// The kept modes follow the values as they are kept within their ranges.
|
||||
let c = calli(0.0, 100.0, 3.0).with_stroke_options(&map(json!({"variation": [-2, 0, 0], "modes": [null, "pressure", null]}))).unwrap();
|
||||
let BrushKind::Calligraphic(c) = c.kind else { panic!("calligraphic") };
|
||||
assert_eq!(c.modes(), [Variation::Fixed, Variation::Pressure, Variation::Fixed]);
|
||||
// Modes that change nothing are no setting of the stroke's: they keep following its values.
|
||||
let same = b.stroke_options_that_differ(&map(json!({"modes": [null, null, null, null]}))).unwrap();
|
||||
assert!(same.is_empty(), "{same:?}");
|
||||
}
|
||||
|
||||
@@ -171,7 +171,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Options of Selected Object…",
|
||||
[],
|
||||
None,
|
||||
"{ids?|id?, params?: {…the fields brush.options takes that a path can set for itself: calligraphic angle, roundness, size, variation, modes; scatter size, spacing, scatter, rotation, modes, rotation_relative_to_path, colorization; art width, width_mode, width_max, flip_along, flip_across, overlap, colorization; pattern scale, spacing, fit, flip_along, flip_across, colorization (values outside their ranges are clamped; a colorization object changes only the fields it gives)}, reset?: bool} set the brush settings of the brushed strokes of the selected paths on those strokes only, leaving the brush and its other strokes as they are (the Brushes panel's Options of Selected Object); params add to each stroke's own settings, a setting equal to the brush's own value is dropped, and reset: true clears them so the strokes draw the brush again; without params or reset nothing changes; one undo step, none when nothing changes. The brushes' own edits (brush.options) still reach the settings a stroke does not set, applying a brush (brush.apply, or stroke.setAdvanced with another brush) or removing it clears them, and a renamed brush keeps them. Refused with nothing painted with a brush selected, for paths painted with brushes of different types, for a bristle brush, for a field the type does not take and for a value of the wrong kind → {ids: the brushed paths, changed: the paths whose settings changed, options: the first stroke's own settings}",
|
||||
"{ids?|id?, params?: {…the fields brush.options takes that a path can set for itself: calligraphic angle, roundness, size, variation, modes; scatter size, spacing, scatter, rotation, modes, rotation_relative_to_path, colorization; art width, width_mode, width_max, flip_along, flip_across, overlap, colorization; pattern scale, spacing, fit, flip_along, flip_across, colorization (values outside their ranges are clamped; a colorization object changes only the fields it gives; a null item of an array such as size: [40, null] keeps each stroke's own item, else the brush's)}, reset?: bool} set the brush settings of the brushed strokes of the selected paths on those strokes only, leaving the brush and its other strokes as they are (the Brushes panel's Options of Selected Object); params add to each stroke's own settings, a setting equal to the brush's own value is dropped, and reset: true clears them so the strokes draw the brush again; without params or reset nothing changes; one undo step, none when nothing changes. The brushes' own edits (brush.options) still reach the settings a stroke does not set, applying a brush (brush.apply, or stroke.setAdvanced with another brush) or removing it clears them, and a renamed brush keeps them. Refused with nothing painted with a brush selected, for paths painted with brushes of different types, for a bristle brush, for a field the type does not take and for a value of the wrong kind → {ids: the brushed paths, changed: the paths whose settings changed, options: the first stroke's own settings}",
|
||||
has_doc,
|
||||
brush_object_options
|
||||
),
|
||||
|
||||
@@ -558,7 +558,20 @@ pub(super) fn brush_object_options(s: &mut Session, p: &Value) -> Result<Value>
|
||||
let keys = b.kind.stroke_option_keys();
|
||||
let mut merged: serde_json::Map<String, Value> =
|
||||
own.iter().filter(|(k, _)| keys.contains(&k.as_str())).map(|(k, v)| (k.clone(), v.clone())).collect();
|
||||
merged.extend(params.clone());
|
||||
// A colorization object changes only the fields it gives, and a null item of an
|
||||
// array keeps the stroke's own item (else the brush's).
|
||||
for (k, v) in ¶ms {
|
||||
let next = match (merged.get(k), v) {
|
||||
(Some(Value::Object(mine)), Value::Object(fields)) if k == "colorization" => {
|
||||
let mut c = mine.clone();
|
||||
c.extend(fields.clone());
|
||||
Value::Object(c)
|
||||
}
|
||||
(Some(mine), _) => brush::keep_items(v, mine),
|
||||
(None, _) => v.clone(),
|
||||
};
|
||||
merged.insert(k.clone(), next);
|
||||
}
|
||||
b.stroke_options_that_differ(&merged).map_err(|e| bad(C, e))?
|
||||
};
|
||||
options.get_or_insert_with(|| next.clone());
|
||||
|
||||
@@ -152,6 +152,29 @@ fn options_of_selected_object_change_one_path_and_leave_the_brush() {
|
||||
assert_eq!(undo_steps(&s), steps);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn options_of_selected_object_null_items_keep_each_paths_own() {
|
||||
let mut s = session();
|
||||
let a = line(&mut s);
|
||||
let b = line(&mut s);
|
||||
s.execute("brush.apply", &json!({"name": "Confetti", "ids": [a.0, b.0]})).unwrap();
|
||||
let brush_size = s.execute("brush.get", &json!({"name": "Confetti"})).unwrap()["size"].clone();
|
||||
s.execute("brush.objectOptions", &json!({"ids": [a.0], "params": {"size": [50, 80]}})).unwrap();
|
||||
s.execute("brush.objectOptions", &json!({"ids": [b.0], "params": {"size": [60, 120]}})).unwrap();
|
||||
let steps = undo_steps(&s);
|
||||
// Only the smallest size changes: each path keeps its own largest size.
|
||||
let r = s.execute("brush.objectOptions", &json!({"ids": [a.0, b.0], "params": {"size": [40, null]}})).unwrap();
|
||||
assert_eq!(r["changed"], json!([a.0, b.0]));
|
||||
assert_eq!(own_options(&s, a)["size"], json!([40.0, 80.0]));
|
||||
assert_eq!(own_options(&s, b)["size"], json!([40.0, 120.0]));
|
||||
assert_eq!(undo_steps(&s), steps + 1, "one undo step for both paths");
|
||||
// A path without its own value keeps the brush's item.
|
||||
let c = line(&mut s);
|
||||
s.execute("brush.apply", &json!({"name": "Confetti", "ids": [c.0]})).unwrap();
|
||||
s.execute("brush.objectOptions", &json!({"ids": [c.0], "params": {"size": [null, 150]}})).unwrap();
|
||||
assert_eq!(own_options(&s, c)["size"], json!([brush_size[0], 150.0]), "brush size {brush_size}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn options_of_selected_object_keys_convert_with_the_document() {
|
||||
let mut s = session();
|
||||
|
||||
@@ -241,13 +241,28 @@ fn object_params(d: &Dialog, keys: &[&str]) -> Map<String, Value> {
|
||||
}
|
||||
}
|
||||
|
||||
/// What of option value `now` changed from `was`, the value it opened with: `None` when nothing
|
||||
/// did; of a range or a list of modes, the items that changed with `null` for the others, and of a
|
||||
/// colorization, the fields that changed. The paths keep their own values for the rest.
|
||||
fn changed_part(was: Option<&Value>, now: Value) -> Option<Value> {
|
||||
match (was, now) {
|
||||
(Some(w), n) if *w == n => None,
|
||||
(Some(Value::Array(w)), Value::Array(n)) if w.len() == n.len() => {
|
||||
Some(Value::Array(w.iter().zip(n).map(|(w, n)| if *w == n { Value::Null } else { n }).collect()))
|
||||
}
|
||||
(Some(Value::Object(w)), Value::Object(n)) => Some(Value::Object(n.into_iter().filter(|(f, v)| w.get(f) != Some(v)).collect())),
|
||||
(_, n) => Some(n),
|
||||
}
|
||||
}
|
||||
|
||||
/// OK of Options of Selected Object: `brush.objectOptions` on the paths it opened for, with the
|
||||
/// values changed since it opened. The dialog closes when they are set.
|
||||
/// values changed since it opened ([`changed_part`]). The dialog closes when they are set.
|
||||
fn confirm_object(app: &mut VectorcraftApp, d: &Dialog) -> Result<Value, String> {
|
||||
let keys: Vec<String> = d.fields.get("__keys").and_then(|v| serde_json::from_value(v.clone()).ok()).unwrap_or_default();
|
||||
let keys: Vec<&str> = keys.iter().map(String::as_str).collect();
|
||||
let opened = d.fields.get(OPENED).and_then(Value::as_object).cloned().unwrap_or_default();
|
||||
let changed: Map<String, Value> = object_params(d, &keys).into_iter().filter(|(k, v)| opened.get(k) != Some(v)).collect();
|
||||
let changed: Map<String, Value> =
|
||||
object_params(d, &keys).into_iter().filter_map(|(k, v)| changed_part(opened.get(&k), v).map(|v| (k, v))).collect();
|
||||
let r = app.run("brush.objectOptions", json!({ "ids": d.fields.get(TARGETS).cloned().unwrap_or_default(), "params": changed }));
|
||||
if r.is_ok() {
|
||||
app.ui.dialog = None;
|
||||
|
||||
@@ -908,6 +908,23 @@ fn options_of_selected_object_set_one_paths_art_brush_settings() {
|
||||
assert_eq!(own_options(&app, a), json!({"width": 50.0}));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn options_of_selected_object_change_only_the_values_edited_on_each_path() {
|
||||
let mut app = app();
|
||||
let a = brushed_line(&mut app, 50.0, "Confetti");
|
||||
let b = brushed_line(&mut app, 100.0, "Confetti");
|
||||
app.run("brush.objectOptions", json!({"ids": [a], "params": {"size": [50, 80]}})).unwrap();
|
||||
app.run("brush.objectOptions", json!({"ids": [b], "params": {"size": [60, 120]}})).unwrap();
|
||||
select(&mut app, &[a, b]);
|
||||
app.run("ui.brushObjectOptions", json!({})).unwrap();
|
||||
// The dialog shows the first path's values; only the smallest size is edited.
|
||||
assert_eq!(app.ui.dialog.as_ref().unwrap().f64("sizeMax", 0.0), 80.0);
|
||||
set(&mut app, json!({"sizeMin": 40}));
|
||||
super::super::confirm(&mut app).unwrap();
|
||||
assert_eq!(own_options(&app, a)["size"], json!([40.0, 80.0]));
|
||||
assert_eq!(own_options(&app, b)["size"], json!([40.0, 120.0]), "the second path keeps its own largest size");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn options_of_selected_object_show_what_a_path_can_set_for_each_type() {
|
||||
let mut app = app();
|
||||
|
||||
@@ -920,7 +920,7 @@ pub const UI_COMMANDS: &[(&str, &str, &str, &str)] = &[
|
||||
"ui.brushObjectOptions",
|
||||
"Options of Selected Object…",
|
||||
"",
|
||||
"{} open the Brushes panel menu's Options of Selected Object (dialog `brushOptions` with `__object`): the Brush Options of the brush that the first selected brushed path paints with, at that path's own settings, without the name field; Art shows width, widthMax/widthMode, flipAlong, flipAcross and overlap, and Pattern scale, spacing, flipAlong, flipAcross and fit, each with colorization; OK runs brush.objectOptions on the brushed paths selected when it opened, with the values changed since it opened, and the brush stays as it is. An error with nothing painted with a brush selected, for paths painted with brushes of different types and for a bristle brush",
|
||||
"{} open the Brushes panel menu's Options of Selected Object (dialog `brushOptions` with `__object`): the Brush Options of the brush that the first selected brushed path paints with, at that path's own settings, without the name field; Art shows width, widthMax/widthMode, flipAlong, flipAcross and overlap, and Pattern scale, spacing, flipAlong, flipAcross and fit, each with colorization; OK runs brush.objectOptions on the brushed paths selected when it opened, with the values changed since it opened (one end of a range or one colorization field changed leaves each path's other values as they are), and the brush stays as it is. An error with nothing painted with a brush selected, for paths painted with brushes of different types and for a bristle brush",
|
||||
),
|
||||
(
|
||||
"ui.graphicStyleOptions",
|
||||
|
||||
+3
-1
@@ -3424,7 +3424,9 @@ already walks Image Trace. Short agent entry points:
|
||||
`rotation_relative_to_path` and `colorization`; art `width`, `width_mode`, `width_max`, `flip_along`, `flip_across`,
|
||||
`overlap` and `colorization`; pattern `scale`, `spacing`, `fit`, `flip_along`, `flip_across` and `colorization`.
|
||||
Values outside their ranges are clamped as in `brush.options`, a `colorization` object changes only the fields it
|
||||
gives, and a setting equal to the brush's own value is dropped. `reset: true` clears a path's settings. The command
|
||||
gives, a `null` item of an array (`size: [40, null]`) keeps each path's own item, else the brush's, and a setting
|
||||
equal to the brush's own value is dropped. The dialog sends only the items and fields changed in it, so editing one
|
||||
end of a range leaves each selected path's other end as it was. `reset: true` clears a path's settings. The command
|
||||
refuses `params` together with `reset`, a selection with no brushed path, paths whose brushes are of different
|
||||
types, a bristle brush and a field the type does not take. It records one undo step, and none when nothing changes.
|
||||
`brush.options` edits still reach the settings a path does not set, `brush.apply`, a brush change in
|
||||
|
||||
Reference in New Issue
Block a user