Symbols: the Symbolism tools take their new options, and OK checks them before any change

tool.setOption now accepts usePressure, method, showBrush, the Symbol
Sprayer options, proportional and affectsDensity for the Symbolism
tools, so the Symbolism Tools Options dialog can save them. The dialog
checks every option, the number ranges included, before
symbol.setDensity re-spaces the selected symbol sets.

- Size: Average gives a sprayed instance the mean size under the brush,
  or the symbol's own size with none there, without the size jitter.
- A stroke that moves an instance away and back again does not count
  it, and adds no undo step.
- symbol.adjust reads an instance's own values for Average only.
This commit is contained in:
dotCooCoo committed 2026-11-18 15:10:38 -08:00
1 parent 247bfeb11d
commit 3ef0e5e588
4 files changed
+47 -29

No files matched your search

+9 -4
View File
@@ -1036,8 +1036,9 @@ pub(super) fn symbol_spray(s: &mut Session, p: &Value) -> Result<Value> {
let (_, _, js) = jitter(i + 7, *c);
let near = near.as_ref();
// User Defined: the symbol's own size, turned the way the stroke runs, opaque, stained
// with the fill color and given the style, each in full.
let k = near.filter(|_| modes.size).map_or(scale, |m| m.scale) * (1.0 + js * 0.1);
// with the fill color and given the style, each in full. Size: Average is the mean size
// under the brush, or the symbol's own size with none there, without the size jitter.
let k = if modes.size { near.map_or(scale, |m| m.scale) } else { scale * (1.0 + js * 0.1) };
let angle = if modes.spin { near.map_or(0.0, |m| m.angle) } else { along.map_or(0.0, |v| v.atan2()) };
let turn = Affine::translate(c.to_vec2()) * Affine::rotate(angle) * Affine::translate(-c.to_vec2());
let id = d.alloc_id();
@@ -1155,7 +1156,7 @@ pub(super) fn symbol_adjust(s: &mut Session, p: &Value) -> Result<Value> {
let first = pts.first().map_or(Point::ZERO, |(q, _)| *q);
let share = |id: NodeId| if method == Method::Random { jitter(id.0 as usize, first).0 + 1.0 } else { 1.0 };
// No undo step when no instance changes (none under the brush, or Average with each at the mean).
let count = crate::cmd::colorcmds::edit_if_changed(s, label, |_, d| {
let count = crate::cmd::colorcmds::edit_if_changed(s, label, |orig, d| {
let mut touched = std::collections::BTreeSet::new();
for (j, (q, pressure)) in pts.iter().enumerate() {
let delta = if j > 0 { *q - pts[j - 1].0 } else { Vec2::ZERO };
@@ -1181,7 +1182,9 @@ pub(super) fn symbol_adjust(s: &mut Session, p: &Value) -> Result<Value> {
}
// Average needs the instance's own values, so it leaves an instance without a size
// as it is; the other methods change it as before.
let avg = match (method, &target, d.node(*id).and_then(|n| look(d, n))) {
// The instance's own values are read for Average only.
let own = (method == Method::Average).then(|| d.node(*id).and_then(|n| look(d, n))).flatten();
let avg = match (method, &target, own) {
(Method::Average, Some(t), Some(cur)) => Some((t, cur)),
// No mean under the brush (every instance there without a size), or none of
// its own.
@@ -1303,6 +1306,8 @@ pub(super) fn symbol_adjust(s: &mut Session, p: &Value) -> Result<Value> {
}
}
}
// An instance the stroke moved away and back again is not counted.
touched.retain(|id| d.node(*id) != orig.node(*id));
Ok((touched.len(), !touched.is_empty()))
})?;
Ok(json!({ "count": count }))
+17 -4
View File
@@ -404,10 +404,10 @@ fn the_sprayer_gives_new_instances_the_average_or_the_user_defined_values() {
s.execute("symbol.spray", &p).unwrap();
*members(s, set).last().unwrap()
};
// Average (the default): the new instance, sprayed beside it, looks like it.
// Average (the default): the new instance, sprayed beside it, looks like it, at its size.
let near = spray(&mut s, json!([[215, 150]]), json!({}));
let (w, o, f) = look(&s, near);
assert!((w / width - 1.0).abs() < 0.11, "{w} of {width}");
assert!((w / width - 1.0).abs() < 1e-9, "{w} of {width}");
assert_eq!((o, f), (opacity, stain.clone()));
// The averages of two instances that look alike are theirs.
let third = spray(&mut s, json!([[208, 165]]), json!({"attributes": {"size": "average", "screen": null}}));
@@ -415,10 +415,10 @@ fn the_sprayer_gives_new_instances_the_average_or_the_user_defined_values() {
// Style: Average replaces a User Defined stain with the fill of the instances under the brush.
let mixed = spray(&mut s, json!([[205, 160]]), json!({"attributes": {"stain": "userDefined"}, "color": "#0000ff"}));
assert_eq!(look(&s, mixed).2.map(|(c, _)| c), stain.clone().map(|(c, _)| c));
// Outside the brush's radius of every instance, the plain values.
// Outside the brush's radius of every instance, the plain values: the symbol's own size.
let far = spray(&mut s, json!([[330, 250]]), json!({}));
let (w, o, f) = look(&s, far);
assert!((w / 20.0 - 1.0).abs() < 0.11 && o == 1.0 && f.is_none(), "{w} {o} {f:?}");
assert!((w / 20.0 - 1.0).abs() < 1e-9 && o == 1.0 && f.is_none(), "{w} {o} {f:?}");
// User Defined: the symbol's size and opaque; Style: Average still gives the fill under the brush.
let user = json!({"size": "userDefined", "screen": "userDefined", "stain": "userDefined", "spin": "userDefined"});
let plain = spray(&mut s, json!([[212, 140]]), json!({"attributes": user, "color": "#0000ff"}));
@@ -703,3 +703,16 @@ fn a_stroke_that_changes_no_instance_adds_no_undo_step() {
assert!(state(&s) == before, "{tool} changed the document");
}
}
/// An instance a stroke moves away and back to where it was is not counted, and the stroke adds
/// no undo step: the Shifter's drag out and back moves it 8 pt right, then 8 pt left.
#[test]
fn an_instance_moved_away_and_back_is_not_counted() {
let mut s = session();
let id = dot(&mut s, 200.0, 150.0);
let before = state(&s);
let r = adjust(&mut s, json!({"tool": "shift", "points": [[196, 166], [212, 166], [196, 166]], "radius": 40, "intensity": 10}));
assert_eq!(bounds(&s, id).center(), Point::new(200.0, 150.0));
assert_eq!(r["count"], 0);
assert!(state(&s) == before, "the stroke added an undo step");
}
+13 -17
View File
@@ -73,27 +73,19 @@ pub struct SymbolismTool {
}
/// Checks the Symbolism Tools Options in `values` before `tool.setOption` sets any, when `tool` is
/// a symbolism tool: `method` that is not one of [`METHODS`], a Symbol Sprayer option that is not
/// one of [`SPRAY_MODES`], and `usePressure`, `showBrush`, `proportional` or `affectsDensity` that
/// is not true or false are errors. Ok for every other tool and every other key. Saved settings go
/// to the tool's `set_option` instead, which skips a value it cannot read.
/// a symbolism tool, with the tool's own checks ([`Tool::check_option`]): a number out of its
/// range, `method` that is not one of [`METHODS`], a Symbol Sprayer option that is not one of
/// [`SPRAY_MODES`], and `usePressure`, `showBrush`, `proportional` or `affectsDensity` that is not
/// true or false are errors. Ok for every other tool, and for a key the tool does not report
/// (`tool.setOption` refuses that one as an unknown option). Saved settings go to the tool's
/// `set_option` instead, which skips a value it cannot read.
pub fn check_options(tool: &str, values: &serde_json::Map<String, Value>) -> Result<(), String> {
if !tool.starts_with("symbol") || create(tool).is_none() {
return Ok(());
}
let must_be = |key: &str, names: &[&str]| format!("`{key}` must be one of {}", names.join(", "));
for (key, v) in values {
let error = match key.as_str() {
"usePressure" | "showBrush" | "proportional" | "affectsDensity" => (!v.is_boolean()).then(|| format!("`{key}` must be true or false")),
"method" => one_of(v, &METHODS).is_none().then(|| must_be(key, &METHODS)),
k if SPRAY_ATTRIBUTES.iter().any(|(a, _)| *a == k) => one_of(v, &SPRAY_MODES).is_none().then(|| must_be(key, &SPRAY_MODES)),
_ => None,
};
if let Some(e) = error {
return Err(e);
}
}
Ok(())
let tool = SymbolismTool::new(tool);
let known = tool.options();
values.iter().filter(|(key, _)| known.get(key.as_str()).is_some()).try_for_each(|(key, v)| tool.check_option(key, v))
}
/// `v` as one of `allowed`, when it is one.
@@ -347,10 +339,14 @@ impl Tool for SymbolismTool {
}
fn check_option(&self, key: &str, v: &Value) -> Result<(), String> {
use crate::option_check as c;
let must_be = |names: &[&str]| format!("`{key}` must be one of {}", names.join(", "));
match key {
"diameter" => c::number(key, v, 2.0, 2000.0),
"intensity" | "density" => c::number(key, v, 1.0, 10.0),
"symbol" | "style" => c::string_or_null(key, v),
"usePressure" | "showBrush" | "proportional" | "affectsDensity" => c::boolean(key, v),
"method" => one_of(v, &METHODS).map(drop).ok_or_else(|| must_be(&METHODS)),
k if SPRAY_ATTRIBUTES.iter().any(|(a, _)| *a == k) => one_of(v, &SPRAY_MODES).map(drop).ok_or_else(|| must_be(&SPRAY_MODES)),
_ => Err(c::unknown(key)),
}
}
@@ -122,8 +122,9 @@ fn selected_sets(app: &VectorcraftApp) -> Vec<u64> {
.collect()
}
/// OK: set the brush the Symbolism tools share (the tool keeps it within its ranges), after giving
/// a new Symbol Set Density to the selected symbol sets.
/// OK: set the brush the Symbolism tools share, after giving a new Symbol Set Density to the
/// selected symbol sets. Every option is checked first, so a value the tools refuse (a number out
/// of its range included) leaves the symbol sets and the options as they are.
fn confirm(app: &mut VectorcraftApp, d: &Dialog) -> DialogResult {
let mut values: Map<String, Value> =
NUMBERS.iter().filter(|k| d.fields.contains_key(**k)).map(|k| (k.to_string(), json!(d.f64(k, 0.0)))).collect();
@@ -300,8 +301,11 @@ mod tests {
d.fields.insert("usePressure".into(), json!(1));
assert!(super::super::confirm(&mut app).is_err());
assert_eq!((width(&app), undo(&app)), (was, steps));
// A tool that is not a Symbolism tool, or a density that is not a number.
for (k, v) in [("tool", json!("pen")), ("density", json!("three")), ("densityWas", json!(null))] {
// A tool that is not a Symbolism tool, a density that is not a number, or a number out of
// its range.
for (k, v) in
[("tool", json!("pen")), ("density", json!("three")), ("densityWas", json!(null)), ("diameter", json!(1)), ("intensity", json!(11))]
{
app.run("tool.options", json!({"tool": "symbolSprayer"})).unwrap();
let d = app.ui.dialog.as_mut().unwrap();
d.fields.insert("density".into(), json!(3));