mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-18 07:49:26 +00:00
Brushes: Options of Selected Object keeps a path's null modes and largest width, and its document
- A calligraphic path's `modes: null` (modes that follow its values) stays when another value changes, instead of falling back to the brush's modes. - An art `width` without a `width_max` keeps the largest width of each path whose width follows the pen, so editing Width no longer moves the other end. - OK changes nothing when another document has become the active one since the dialog opened, as Delete Brush does.
This commit is contained in:
@@ -804,10 +804,16 @@ impl BrushKind {
|
||||
}
|
||||
}
|
||||
|
||||
/// The values of [`Self::stroke_option_keys`], as a JSON object.
|
||||
/// The values of [`Self::stroke_option_keys`], as a JSON object. Calligraphic modes left to the
|
||||
/// values are a `null` `modes`, so a stroke that sets them so keeps that setting.
|
||||
fn option_values(&self) -> serde_json::Value {
|
||||
let v = match self {
|
||||
BrushKind::Calligraphic(c) => serde_json::to_value(c),
|
||||
BrushKind::Calligraphic(c) => serde_json::to_value(c).map(|mut v| {
|
||||
if let Some(o) = v.as_object_mut() {
|
||||
o.entry("modes").or_insert(serde_json::Value::Null);
|
||||
}
|
||||
v
|
||||
}),
|
||||
BrushKind::Scatter(s) => serde_json::to_value(ScatterOptions {
|
||||
size: s.size,
|
||||
spacing: s.spacing,
|
||||
|
||||
@@ -2118,3 +2118,20 @@ fn null_stroke_option_items_keep_the_items_the_stroke_has() {
|
||||
let same = b.stroke_options_that_differ(&map(json!({"modes": [null, null, null, null]}))).unwrap();
|
||||
assert!(same.is_empty(), "{same:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_null_calligraphic_modes_setting_stays_when_another_value_changes() {
|
||||
let map = |v: serde_json::Value| v.as_object().cloned().unwrap_or_default();
|
||||
// A brush whose modes are its own, and a stroke whose modes follow its values instead.
|
||||
let mut b = calli(0.0, 100.0, 3.0);
|
||||
if let BrushKind::Calligraphic(c) = &mut b.kind {
|
||||
c.modes = Some([Variation::Pressure; 3]);
|
||||
}
|
||||
let got = b.stroke_options_that_differ(&map(json!({"modes": null, "angle": 30.0}))).unwrap();
|
||||
assert_eq!(serde_json::Value::Object(got), json!({"modes": null, "angle": 30.0}));
|
||||
let BrushKind::Calligraphic(c) = b.with_stroke_options(&map(json!({"modes": null, "angle": 30.0}))).unwrap().kind else { panic!("calligraphic") };
|
||||
assert_eq!(c.modes(), [Variation::Fixed; 3], "the stroke draws with the modes its values give");
|
||||
// A brush whose modes follow its values: a null `modes` is the brush's own and is left out.
|
||||
let plain = calli(0.0, 100.0, 3.0).stroke_options_that_differ(&map(json!({"modes": null}))).unwrap();
|
||||
assert!(plain.is_empty(), "{plain:?}");
|
||||
}
|
||||
|
||||
@@ -574,6 +574,7 @@ pub(super) fn brush_object_options(s: &mut Session, p: &Value) -> Result<Value>
|
||||
merged.insert(k.clone(), next);
|
||||
}
|
||||
keep_drawn_modes(b, &was, ¶ms, &mut merged);
|
||||
keep_largest_width(b, &was, ¶ms, &mut merged);
|
||||
b.stroke_options_that_differ(&merged).map_err(|e| bad(C, e))?
|
||||
};
|
||||
options.get_or_insert_with(|| next.clone());
|
||||
@@ -636,6 +637,30 @@ fn keep_drawn_modes(
|
||||
merged.insert("modes".into(), Value::Array(kept));
|
||||
}
|
||||
|
||||
/// Keep the largest width a stroke of art brush `b` draws with (with its own settings `was`) when
|
||||
/// `params` sets `width` and no `width_max`. Without a `width_max` of its own the largest width is
|
||||
/// the width, so without this a changed width would also change the largest width.
|
||||
fn keep_largest_width(
|
||||
b: &Brush,
|
||||
was: &serde_json::Map<String, Value>,
|
||||
params: &serde_json::Map<String, Value>,
|
||||
merged: &mut serde_json::Map<String, Value>,
|
||||
) {
|
||||
if params.get("width").is_none_or(Value::is_null) || params.get("width_max").is_some_and(|m| !m.is_null()) {
|
||||
return;
|
||||
}
|
||||
let art = |o: &serde_json::Map<String, Value>| match b.with_stroke_options(o).ok().map(|b| b.kind) {
|
||||
Some(BrushKind::Art(a)) => Some(a),
|
||||
_ => None,
|
||||
};
|
||||
let (Some(before), Some(after)) = (art(was), art(merged)) else { return };
|
||||
let follows_pen = |a: &brush::ArtBrush| a.width_mode.is_some_and(|m| m.follows_pen());
|
||||
// Only a width that followed the pen has a largest width, and only one that still does uses it.
|
||||
if before.width_max.is_none() && follows_pen(&before) && follows_pen(&after) && after.width != before.width {
|
||||
merged.insert("width_max".into(), json!(before.width));
|
||||
}
|
||||
}
|
||||
|
||||
/// The name `brush.placeArt` gives each pattern brush tile it places, by tile, in the order it
|
||||
/// places them; `brush.replaceArt` reads a group of that name as that tile.
|
||||
const TILE_NAMES: [&str; 5] = ["Side Tile", "Outer Corner Tile", "Inner Corner Tile", "Start Tile", "End Tile"];
|
||||
|
||||
@@ -207,6 +207,41 @@ fn options_of_selected_object_keep_the_modes_each_path_draws_with() {
|
||||
assert_eq!((confetti_size_mode(&s, a), confetti_size_mode(&s, b)), (Variation::Fixed, Variation::Fixed));
|
||||
}
|
||||
|
||||
/// The widths (%) the first stroke of `id`, painted with Arrow, draws with at the lightest and the
|
||||
/// heaviest pressure.
|
||||
fn arrow_width_range(s: &Session, id: NodeId) -> (f64, f64) {
|
||||
let lib = vectorcraft_brush::library(&s.doc().unwrap().doc);
|
||||
let b = lib.iter().find(|b| b.name == "Arrow").unwrap();
|
||||
match b.with_stroke_options(own_options(s, id).as_object().unwrap()).unwrap().kind {
|
||||
vectorcraft_brush::BrushKind::Art(a) => a.width_range(),
|
||||
k => panic!("not an art brush: {k:?}"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn options_of_selected_object_keep_each_paths_largest_width_when_the_width_changes() {
|
||||
let mut s = session();
|
||||
let a = line(&mut s);
|
||||
let b = line(&mut s);
|
||||
s.execute("brush.apply", &json!({"name": "Arrow", "ids": [a.0, b.0]})).unwrap();
|
||||
// Both widths follow the pressure; the first path's largest width is its width, the second
|
||||
// path's is its own.
|
||||
s.execute("brush.objectOptions", &json!({"ids": [a.0], "params": {"width": 100, "width_mode": "pressure"}})).unwrap();
|
||||
s.execute("brush.objectOptions", &json!({"ids": [b.0], "params": {"width": 100, "width_mode": "pressure", "width_max": 150}})).unwrap();
|
||||
assert_eq!((arrow_width_range(&s, a), arrow_width_range(&s, b)), ((100.0, 100.0), (100.0, 150.0)));
|
||||
// The width the path has already changes nothing.
|
||||
let steps = undo_steps(&s);
|
||||
assert_eq!(s.execute("brush.objectOptions", &json!({"ids": [a.0], "params": {"width": 100}})).unwrap()["changed"], json!([]));
|
||||
assert_eq!(own_options(&s, a), json!({"width_mode": "pressure"}));
|
||||
assert_eq!(undo_steps(&s), steps);
|
||||
// A new smallest width leaves each path's largest width as it was.
|
||||
s.execute("brush.objectOptions", &json!({"ids": [a.0, b.0], "params": {"width": 50}})).unwrap();
|
||||
assert_eq!((arrow_width_range(&s, a), arrow_width_range(&s, b)), ((50.0, 100.0), (50.0, 150.0)));
|
||||
// A largest width the params give is set on each path.
|
||||
s.execute("brush.objectOptions", &json!({"ids": [a.0, b.0], "params": {"width": 40, "width_max": 120}})).unwrap();
|
||||
assert_eq!((arrow_width_range(&s, a), arrow_width_range(&s, b)), ((40.0, 120.0), (40.0, 120.0)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn options_of_selected_object_go_when_a_freehand_stroke_changes_the_brush() {
|
||||
let mut s = session();
|
||||
|
||||
@@ -20,9 +20,10 @@
|
||||
//!
|
||||
//! The panel menu's Options of Selected Object opens the dialog on the brush of the first selected
|
||||
//! brushed path, with that path's own settings ([`open_object`]), with `__object`, `__targets` (the
|
||||
//! brushed paths) and `__opened` (the values it opened with). It has no name, and of the Art and
|
||||
//! Pattern brush options only those a path can set for itself. OK runs `brush.objectOptions` with
|
||||
//! the values changed since it opened: the paths change and the brush stays as it is.
|
||||
//! brushed paths), `__opened` (the values it opened with) and `__doc` (the uid of the document). It
|
||||
//! has no name, and of the Art and Pattern brush options only those a path can set for itself. OK
|
||||
//! runs `brush.objectOptions` with the values changed since it opened: the paths change and the
|
||||
//! brush stays as it is. OK after another document has become the active one changes nothing.
|
||||
|
||||
use egui::{Color32, Sense, Stroke, pos2, vec2};
|
||||
use serde_json::{Map, Value, json};
|
||||
@@ -55,6 +56,9 @@ const TARGETS: &str = "__targets";
|
||||
/// The field of such a dialog holding the `brush.objectOptions` params it opened with.
|
||||
const OPENED: &str = "__opened";
|
||||
|
||||
/// The field of such a dialog holding the uid of the document it opened in.
|
||||
const DOC: &str = "__doc";
|
||||
|
||||
/// Calligraphic values: (field, label, suffix, decimals), in [`Calligraphic::RANGES`] order.
|
||||
const CALLI: [(&str, &str, &str, usize); 3] = [("angle", "Angle:", "°", 0), ("roundness", "Roundness:", "%", 0), ("size", "Size:", " pt", 1)];
|
||||
|
||||
@@ -216,7 +220,8 @@ pub fn open_object(app: &mut VectorcraftApp) -> Result<Value, String> {
|
||||
let r = app.run("brush.objectOptions", json!({}))?;
|
||||
let targets = r["ids"].clone();
|
||||
let first = targets.get(0).and_then(Value::as_u64).map(NodeId).ok_or("no path painted with a brush is selected")?;
|
||||
let lib = vectorcraft_brush::library(&app.session.active().ok_or("no document open")?.doc);
|
||||
let st = app.session.active().ok_or("no document open")?;
|
||||
let (lib, doc) = (vectorcraft_brush::library(&st.doc), st.uid);
|
||||
let b = stroke_brush(app, &lib, first).ok_or("no path painted with a brush is selected")?;
|
||||
let own: Map<String, Value> = serde_json::from_value(r["options"].clone()).unwrap_or_default();
|
||||
let edited = b.with_stroke_options(&own)?;
|
||||
@@ -225,6 +230,7 @@ pub fn open_object(app: &mut VectorcraftApp) -> Result<Value, String> {
|
||||
f.insert("__brush".into(), json!(b.name));
|
||||
f.insert(OBJECT.into(), json!(true));
|
||||
f.insert(TARGETS.into(), targets);
|
||||
f.insert(DOC.into(), json!(doc));
|
||||
let mut d = Dialog { kind: KIND.into(), fields: f };
|
||||
let opened = object_params(&d, b.kind.stroke_option_keys());
|
||||
d.fields.insert(OPENED.into(), Value::Object(opened));
|
||||
@@ -256,8 +262,13 @@ fn changed_part(was: Option<&Value>, now: Value) -> Option<Value> {
|
||||
}
|
||||
|
||||
/// OK of Options of Selected Object: `brush.objectOptions` on the paths it opened for, with the
|
||||
/// values changed since it opened ([`changed_part`]). The dialog closes when they are set.
|
||||
/// values changed since it opened ([`changed_part`]). The dialog closes when they are set. When
|
||||
/// another document has become the active one, it closes as Cancel does and changes nothing.
|
||||
fn confirm_object(app: &mut VectorcraftApp, d: &Dialog) -> Result<Value, String> {
|
||||
if d.fields.get(DOC).and_then(Value::as_u64) != app.session.active().map(|doc| doc.uid) {
|
||||
app.ui.dialog = None;
|
||||
return Ok(json!({}));
|
||||
}
|
||||
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();
|
||||
|
||||
@@ -999,3 +999,23 @@ fn options_of_selected_object_show_what_a_path_can_set_for_each_type() {
|
||||
app.run("select.none", json!({})).unwrap();
|
||||
assert!(!object_available(&app));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn options_of_selected_object_change_nothing_after_another_document_became_active() {
|
||||
let mut app = app();
|
||||
let a = brushed_line(&mut app, 50.0, "Arrow");
|
||||
select(&mut app, &[a]);
|
||||
app.run("ui.brushObjectOptions", json!({})).unwrap();
|
||||
set(&mut app, json!({"width": 50}));
|
||||
// A second document with a brushed path of the same id becomes the active one.
|
||||
app.run("file.new", json!({"width": 300, "height": 200})).unwrap();
|
||||
app.run("document.activate", json!({"index": 1})).unwrap();
|
||||
let other = brushed_line(&mut app, 50.0, "Arrow");
|
||||
assert_eq!(other, a);
|
||||
// OK closes the dialog as Cancel does.
|
||||
super::super::confirm(&mut app).unwrap();
|
||||
assert!(app.ui.dialog.is_none());
|
||||
assert_eq!(own_options(&app, other), json!({}), "the second document's path keeps the brush's settings");
|
||||
app.run("document.activate", json!({"index": 0})).unwrap();
|
||||
assert_eq!(own_options(&app, a), json!({}), "the first document's path keeps the brush's settings");
|
||||
}
|
||||
|
||||
+3
-1
@@ -3468,7 +3468,9 @@ already walks Image Trace. Short agent entry points:
|
||||
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. A mode that `params` does not set (no `modes`, or
|
||||
a `null` item) stays the one each path draws with, so a changed calligraphic variation or scatter range does not
|
||||
change it. `reset: true` clears a path's settings. The command
|
||||
change it. An art `width` without a `width_max` keeps the largest width of each path whose width follows the pen,
|
||||
and a calligraphic path's `modes: null` (modes that follow its values) stays when another value changes. The dialog
|
||||
changes nothing when another document has become the active one. `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