mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-19 17:05:55 +01:00
Symbols: Random styling and Symbol Set Density follow the brush and the set
- With Method Random the Symbol Styler scales each instance's share of the style by the brush strength, so the brush edge and a light pen pressure style an instance less. - symbol.setDensity gives every selected set the new density, also a set whose instances do not move (one instance, or each locked), so a later change packs it from that density. No undo step is added when every set already has it.
This commit is contained in:
1 parent
1340719244
commit
0bbddf5cc4
5 files changed
+66
-16
No files matched your search
@@ -359,7 +359,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Symbol Set Density",
|
||||
[],
|
||||
None,
|
||||
"{from: 1..10, to: 1..10, ids?|id?} space the instances of the selected symbol sets (or those in `ids`) as Symbol Set Density `to` packs them, from the density each set was created at or last given (`from` for a set without one, such as one grouped by hand): each instance moves along the line from the middle of its set's instances, to (1.15 − 0.1·to) / (1.15 − 0.1·from) of its distance from it (each factor at least 0.1), as one undo step, and the sets moved then have density `to`; a higher density packs them closer, and a locked or hidden instance stays where it is. No undo step is added when no instance moves (a set already at `to`, or sets of one instance). A `from` or `to` left out, not a number or outside 1..10, an id naming no object, or no symbol set among the objects is an error → {ids: the sets changed, count: the instances moved}",
|
||||
"{from: 1..10, to: 1..10, ids?|id?} space the instances of the selected symbol sets (or those in `ids`) as Symbol Set Density `to` packs them, from the density each set was created at or last given (`from` for a set without one, such as one grouped by hand): each instance moves along the line from the middle of its set's instances, to (1.15 − 0.1·to) / (1.15 − 0.1·from) of its distance from it (each factor at least 0.1), as one undo step, and every set then has density `to`, also one whose instances do not move (one instance, or each locked); a higher density packs them closer, and a locked or hidden instance stays where it is. No undo step is added when no instance moves and every set already has density `to`. A `from` or `to` left out, not a number or outside 1..10, an id naming no object, or no symbol set among the objects is an error → {ids: the sets changed, count: the instances moved}",
|
||||
has_doc,
|
||||
symbol_set_density
|
||||
),
|
||||
|
||||
@@ -281,7 +281,8 @@ impl Grid {
|
||||
/// Symbol Set Density `to` packs them, from the density each set was created at or last given
|
||||
/// (`from` for a set without one): each instance moves along the line from the middle of its set's
|
||||
/// instances, to [`spacing`]`(to) / `[`spacing`]` of the set's density` of its distance from it.
|
||||
/// The sets moved then have density `to`. One undo step, and none when no instance moves.
|
||||
/// Every selected set then has density `to`, also one whose instances do not move. One undo step,
|
||||
/// and none when no instance moves and every set already has density `to`.
|
||||
pub(super) fn symbol_set_density(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
const C: &str = "symbol.setDensity";
|
||||
let density = |key: &str| match p.get(key) {
|
||||
@@ -311,10 +312,17 @@ pub(super) fn symbol_set_density(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let mut changed: Vec<NodeId> = vec![];
|
||||
for set in &sets {
|
||||
// A set packs from the density it was created at or last given, else from `from`.
|
||||
let ratio = spacing(to) / spacing(set_density_of(d, *set).unwrap_or(from));
|
||||
let own = set_density_of(d, *set);
|
||||
let ratio = spacing(to) / spacing(own.unwrap_or(from));
|
||||
// A set takes density `to` even when none of its instances moves (one instance, or each
|
||||
// locked or on top of the others), so a later change packs it from `to`.
|
||||
let restamp = own != Some(to);
|
||||
let centers: Vec<(NodeId, Point)> =
|
||||
d.node(*set).and_then(|n| n.children()).into_iter().flatten().filter_map(|c| Some((c.id, instance_center(c)?))).collect();
|
||||
if centers.len() < 2 {
|
||||
if restamp {
|
||||
changed.push(*set);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
let middle = (centers.iter().fold(Vec2::ZERO, |sum, (_, c)| sum + c.to_vec2()) / centers.len() as f64).to_point();
|
||||
@@ -323,12 +331,12 @@ pub(super) fn symbol_set_density(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
moves.extend(
|
||||
centers.iter().filter(|(id, _)| editable(*id)).map(|(id, c)| (*id, (*c - middle) * (ratio - 1.0))).filter(|(_, v)| v.hypot() > 1e-9),
|
||||
);
|
||||
if moves.len() > before {
|
||||
if moves.len() > before || restamp {
|
||||
changed.push(*set);
|
||||
}
|
||||
}
|
||||
drop(editable);
|
||||
if !moves.is_empty() {
|
||||
if !changed.is_empty() {
|
||||
s.edit("Symbol Set Density", |d, _| {
|
||||
for (id, v) in &moves {
|
||||
if let Some(n) = d.node_mut(*id) {
|
||||
|
||||
@@ -1202,7 +1202,9 @@ pub(super) fn symbol_adjust(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
None => {
|
||||
let cur = n.appearance.fill().filter(|f| f.paint == paint).map(|f| f.opacity).unwrap_or(0.0);
|
||||
let step = match (tool.as_str(), method) {
|
||||
("style", Method::Random) => (share / 2.0) as f32,
|
||||
// Random: the instance's share of the style, scaled by the
|
||||
// brush strength there.
|
||||
("style", Method::Random) => (share / 2.0 * w.min(1.0)) as f32,
|
||||
("style", _) => 1.0,
|
||||
_ => (0.15 * w * share) as f32,
|
||||
};
|
||||
|
||||
@@ -191,6 +191,24 @@ fn random_changes_each_instance_by_its_own_share() {
|
||||
assert_eq!(grown(Some("random")), random);
|
||||
}
|
||||
|
||||
/// With Method Random the Symbol Styler gives each instance its share of the style scaled by the
|
||||
/// brush strength: at Intensity 1 a tenth of what it gives at Intensity 10.
|
||||
#[test]
|
||||
fn random_style_follows_the_brush_strength() {
|
||||
let styled = |intensity: u32| {
|
||||
let mut s = session();
|
||||
let ids = [(200.0, 150.0), (201.0, 150.0), (200.0, 151.0), (199.0, 150.0), (200.0, 149.0)].map(|(x, y)| dot(&mut s, x, y));
|
||||
let p = json!({"tool": "style", "points": [[200, 150]], "radius": 1000, "intensity": intensity, "method": "random", "color": "#ff0000"});
|
||||
adjust(&mut s, p);
|
||||
ids.map(|id| f64::from(node(&s, id).appearance.fill().map_or(0.0, |f| f.opacity)))
|
||||
};
|
||||
let (full, low) = (styled(10), styled(1));
|
||||
assert!(full.iter().any(|o| *o > 0.05), "{full:?}");
|
||||
for (f, l) in full.iter().zip(&low) {
|
||||
assert!((l - f * 0.1).abs() < 1e-3, "{low:?} against {full:?}");
|
||||
}
|
||||
}
|
||||
|
||||
/// A `method` that is none of the three is an error and changes nothing; the Symbol Shifter takes
|
||||
/// one and shifts as it does without.
|
||||
#[test]
|
||||
@@ -456,15 +474,11 @@ fn set_density_spaces_the_instances_of_a_symbol_set() {
|
||||
let r = s.execute("symbol.setDensity", &json!({"from": 9, "to": 5, "ids": [set.0, set.0]})).unwrap();
|
||||
assert_eq!(r, json!({"ids": [set.0], "count": 8}));
|
||||
assert!(centers(&s).iter().zip(&was).all(|(a, b)| a.distance(*b) < 1e-9));
|
||||
// The density the set has (5, a `from` of 7 notwithstanding), or a set of one instance: no
|
||||
// instance moves and no undo step is added.
|
||||
// The density the set has (5, a `from` of 7 notwithstanding): no instance moves and no undo
|
||||
// step is added.
|
||||
let steps = undo_len(&s);
|
||||
assert_eq!(s.execute("symbol.setDensity", &json!({"from": 7, "to": 5})).unwrap(), json!({"ids": [], "count": 0}));
|
||||
s.select(|_, sel| sel.clear()).unwrap();
|
||||
let one = NodeId(s.execute("symbol.spray", &json!({"name": "Dot", "points": [[300, 40]]})).unwrap()["id"].as_u64().unwrap());
|
||||
let steps_with_one = undo_len(&s);
|
||||
assert_eq!(s.execute("symbol.setDensity", &json!({"from": 1, "to": 10, "ids": [one.0]})).unwrap(), json!({"ids": [], "count": 0}));
|
||||
assert_eq!((steps_with_one, undo_len(&s)), (steps + 1, steps + 1));
|
||||
assert_eq!(undo_len(&s), steps);
|
||||
// Undo puts a spaced set back.
|
||||
s.execute("symbol.setDensity", &json!({"from": 5, "to": 1, "id": set.0})).unwrap();
|
||||
assert!(centers(&s)[0].distance(was[0]) > 1.0);
|
||||
@@ -508,6 +522,31 @@ fn set_density_starts_from_the_density_a_set_was_sprayed_at() {
|
||||
assert!((center(&s, a).x - (150.0 - 50.0 * 0.25 / 0.65)).abs() < 1e-9, "{:?}", center(&s, a));
|
||||
}
|
||||
|
||||
/// A set of one instance takes the new density without moving, as one undo step, so a later change
|
||||
/// packs it from that density; setting the density it already has adds no undo step.
|
||||
#[test]
|
||||
fn set_density_gives_a_set_of_one_instance_the_new_density() {
|
||||
let mut s = session();
|
||||
let set = NodeId(s.execute("symbol.spray", &json!({"name": "Dot", "points": [[100, 150]], "radius": 4})).unwrap()["id"].as_u64().unwrap());
|
||||
let steps = undo_len(&s);
|
||||
assert_eq!(s.execute("symbol.setDensity", &json!({"from": 5, "to": 9, "ids": [set.0]})).unwrap(), json!({"ids": [set.0], "count": 0}));
|
||||
assert_eq!(undo_len(&s), steps + 1);
|
||||
assert_eq!(s.execute("symbol.setDensity", &json!({"from": 5, "to": 9, "ids": [set.0]})).unwrap(), json!({"ids": [], "count": 0}));
|
||||
assert_eq!(undo_len(&s), steps + 1);
|
||||
// A second instance, then from the set's 9 to 5: 0.65 / 0.25 of each distance from the middle.
|
||||
s.select(|_, sel| sel.set([set])).unwrap();
|
||||
s.execute("symbol.spray", &json!({"name": "Dot", "points": [[200, 150]], "radius": 4, "density": 9})).unwrap();
|
||||
let ids = members(&s, set);
|
||||
assert_eq!(ids.len(), 2);
|
||||
let was: Vec<Point> = ids.iter().map(|id| center(&s, *id)).collect();
|
||||
let middle = Point::new((was[0].x + was[1].x) / 2.0, (was[0].y + was[1].y) / 2.0);
|
||||
s.execute("symbol.setDensity", &json!({"from": 5, "to": 5, "ids": [set.0]})).unwrap();
|
||||
for (id, before) in ids.iter().zip(&was) {
|
||||
let want = middle + (*before - middle) * (0.65 / 0.25);
|
||||
assert!(center(&s, *id).distance(want) < 1e-9, "{before:?} -> {:?}", center(&s, *id));
|
||||
}
|
||||
}
|
||||
|
||||
/// A locked instance of the set stays where it is, and the others move.
|
||||
#[test]
|
||||
fn set_density_leaves_a_locked_instance_in_place() {
|
||||
|
||||
+4
-3
@@ -3324,9 +3324,10 @@ already walks Image Trace. Short agent entry points:
|
||||
style. `attributes` left out means `average` for all six.
|
||||
- `symbol.setDensity` moves each instance of a set along the line from the middle of the set's instances, to
|
||||
(1.15 − 0.1·`to`) / (1.15 − 0.1·`from`) of its distance from it (each factor at least 0.1), as one undo step, and
|
||||
adds none when no instance moves. `from` is the density each set was created at or last given, and the
|
||||
`from` passed applies only to a set without one (a group named "Symbol Set" by hand); the sets moved then have
|
||||
density `to`. A locked or hidden instance stays where it is. It returns `{ids, count}`: the sets changed and the
|
||||
adds none when no instance moves and every set already has density `to`. `from` is the density each set was
|
||||
created at or last given, and the `from` passed applies only to a set without one (a group named "Symbol Set" by
|
||||
hand); every set then has density `to`, also one whose instances do not move (one instance, or each locked). A
|
||||
locked or hidden instance stays where it is. It returns `{ids, count}`: the sets changed and the
|
||||
instances moved.
|
||||
- A `method` or an attribute outside these values, a point's third number that isn't a number (in `symbol.spray`
|
||||
too, which does not use it), a `from` or `to` of `symbol.setDensity` outside 1..10, or no symbol set among its
|
||||
|
||||
Reference in new issue
Block a user