mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-26 17:15:56 +00:00
Fill Area: in isolation mode only the isolated group's paths bound areas
With a group or layer isolated, the tool's hover and click and shapeBuilder.copyRegion take only the paths inside it, and the new path goes inside it, directly above the object in it that holds the front-most path along the area. Before, a click on art outside the isolated group added a path there. No area is found while the isolated object is a path or compound path, a group that makes, moves or combines its own art (Live Paint, compound shapes, Shaper groups, graphs, Image Trace results, dimensions, flares, Objects on Path), a layer or group with a live effect, a clipping mask, or an object inside one of these or inside an envelope. An isolated object that no longer exists counts as no isolation, for the tool as for the command.
This commit is contained in:
@@ -73,7 +73,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Fill Area",
|
||||
[],
|
||||
None,
|
||||
"{point: [x,y], ids?|id?: [id…] (the objects or layers whose paths bound areas; default: every layer, nothing needs to be selected), gap?: pt (0–72; 0: Gap Detection off; out of range is an error), openFilledAsClosed?: true (as shapeBuilder.merge), fill?, stroke?: \"#rrggbb\" | {color|swatch|gradient|none} (default: the current fill and stroke, with the current stroke weight)} add a new closed path of the area under point that the visible, unlocked paths enclose (the region of shapeBuilder.regions holding it: areas a shape already fills and areas open paths bound count too), directly above the front-most object along the area (its outline touches the area's outline, or it covers the area; when a group, Live Paint group, Shaper group or clipping group holds that path, above the outermost group in its layer, never inside it), and select it; the paths around the area are not changed; a point in no enclosed area is an error and changes nothing (no undo step); when the paths together have more than 65,536 segments, only an area the paths near the point enclose is found → {id, above (the object the new path went above)}",
|
||||
"{point: [x,y], ids?|id?: [id…] (the objects or layers whose paths bound areas; default: every layer, nothing needs to be selected), gap?: pt (0–72; 0: Gap Detection off; out of range is an error), openFilledAsClosed?: true (as shapeBuilder.merge), fill?, stroke?: \"#rrggbb\" | {color|swatch|gradient|none} (default: the current fill and stroke, with the current stroke weight)} add a new closed path of the area under point that the visible, unlocked paths enclose (the region of shapeBuilder.regions holding it: areas a shape already fills and areas open paths bound count too), directly above the front-most object along the area (its outline touches the area's outline, or it covers the area; when a group, Live Paint group, Shaper group or clipping group holds that path, above the outermost group in its layer, never inside it), and select it; in isolation mode only the paths of the isolated layer or group bound areas (with ids, only the paths of the listed objects that are in it), and the new path goes inside it, directly above the object in it that holds that path; no area is found while the isolated object is not a layer or group (a path or compound path), is a Live Paint group, compound shape, Shaper group, graph, Image Trace result, dimension, flare or Objects on Path group, is a layer or group with a live effect or a clipping mask, or lies inside one of these or inside an envelope; the paths around the area are not changed; a point in no enclosed area is an error and changes nothing (no undo step); when the paths together have more than 65,536 segments, only an area the paths near the point enclose is found → {id, above (the object the new path went above)}",
|
||||
has_doc,
|
||||
sb_copy_region
|
||||
),
|
||||
|
||||
@@ -195,18 +195,22 @@ pub(super) fn sb_copy_region(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let stroke = paint_param(s, p, "stroke", C)?.unwrap_or_else(|| s.paint.stroke.clone());
|
||||
let look = s.new_art_look(fill, stroke, s.paint.stroke_width);
|
||||
let area = {
|
||||
let d = &s.doc()?.doc;
|
||||
let roots = match named_ids(p)? {
|
||||
let st = s.doc()?;
|
||||
let d = &st.doc;
|
||||
let named = match named_ids(p)? {
|
||||
Some(ids) => {
|
||||
if let Some(bad_id) = ids.iter().find(|i| d.node(**i).is_none()) {
|
||||
return Err(EngineError::NoNode(*bad_id));
|
||||
}
|
||||
// An object listed with a layer or group that holds it counts once.
|
||||
outermost(d, ids)
|
||||
Some(outermost(d, ids))
|
||||
}
|
||||
None => b::every_layer(d),
|
||||
None => None,
|
||||
};
|
||||
b::AreaFinder::new(d, &roots, gap, open_filled_as_closed).find(pt)
|
||||
// In isolation mode only the isolated layer or group's paths bound areas, and the new path
|
||||
// goes inside it.
|
||||
b::fill_area_roots(d, st.isolation, named.as_deref())
|
||||
.and_then(|(roots, scope)| b::AreaFinder::new(d, &roots, scope, gap, open_filled_as_closed).find(pt))
|
||||
};
|
||||
let Some(area) = area else { return Err(EngineError::Other("Fill Area: no enclosed area under the pointer".into())) };
|
||||
let id = s.edit("Fill Area", |d, sel| {
|
||||
|
||||
@@ -189,6 +189,137 @@ fn fill_area_stacks_above_the_group_that_holds_the_paths() {
|
||||
assert_eq!(members(&s, clip), before);
|
||||
}
|
||||
|
||||
/// In isolation mode only the isolated group's paths bound areas, for the command and the tool: a
|
||||
/// click in an area that art outside the group encloses adds nothing, and the new path goes inside
|
||||
/// the group, above the path along the area.
|
||||
#[test]
|
||||
fn fill_area_stays_inside_the_isolated_group() {
|
||||
let mut s = session();
|
||||
let outside =
|
||||
[line(&mut s, (200.0, 0.0), (300.0, 100.0)), line(&mut s, (300.0, 0.0), (200.0, 100.0)), line(&mut s, (190.0, 80.0), (310.0, 80.0))];
|
||||
let lines = triangle(&mut s);
|
||||
let ids: Vec<u64> = lines.iter().map(|l| l.0).collect();
|
||||
s.execute("select.set", &json!({"ids": ids})).unwrap();
|
||||
let g = id_of(&s.execute("object.group", &json!({})).unwrap());
|
||||
s.execute("object.isolate", &json!({"id": g.0})).unwrap();
|
||||
deselect(&mut s);
|
||||
let members = |s: &Session| -> Vec<NodeId> { node(s, g).children().unwrap().iter().map(|n| n.id).collect() };
|
||||
let (undo, kids) = (undo_len(&s), stack(&s));
|
||||
assert!(s.execute("shapeBuilder.copyRegion", &json!({"point": [250, 70]})).is_err(), "art outside the isolated group");
|
||||
let named: Vec<u64> = outside.iter().map(|l| l.0).collect();
|
||||
assert!(s.execute("shapeBuilder.copyRegion", &json!({"point": [250, 70], "ids": named})).is_err(), "named art outside it");
|
||||
assert_eq!((undo_len(&s), stack(&s), members(&s)), (undo, kids.clone(), lines.to_vec()));
|
||||
|
||||
let v = ViewInfo::default();
|
||||
s.select_tool("fillArea", v).unwrap();
|
||||
s.pointer(&PointerEvent::new(PointerKind::Move, 250.0, 70.0), v).unwrap();
|
||||
assert!(
|
||||
!s.overlays(v).iter().any(|o| matches!(o, Overlay::Path { color: [0xff, 0x2a, 0x2a], .. })),
|
||||
"no area outside the isolated group is outlined"
|
||||
);
|
||||
s.pointer(&PointerEvent::new(PointerKind::Down, 250.0, 70.0), v).unwrap();
|
||||
s.pointer(&PointerEvent::new(PointerKind::Up, 250.0, 70.0), v).unwrap();
|
||||
assert_eq!((undo_len(&s), stack(&s), members(&s)), (undo, kids.clone(), lines.to_vec()));
|
||||
|
||||
s.pointer(&PointerEvent::new(PointerKind::Move, 50.0, 70.0), v).unwrap();
|
||||
let red = s.overlays(v).into_iter().filter(|o| matches!(o, Overlay::Path { color: [0xff, 0x2a, 0x2a], .. })).count();
|
||||
assert_eq!(red, 1, "the isolated group's area is outlined");
|
||||
s.pointer(&PointerEvent::new(PointerKind::Down, 50.0, 70.0), v).unwrap();
|
||||
s.pointer(&PointerEvent::new(PointerKind::Up, 50.0, 70.0), v).unwrap();
|
||||
let new = s.doc().unwrap().selection.objects[0];
|
||||
assert_eq!(s.doc().unwrap().doc.parent_of(new), Some(g));
|
||||
assert_eq!(members(&s), [lines.as_slice(), &[new]].concat());
|
||||
assert_eq!(stack(&s), kids);
|
||||
assert!((area(&s, new) - 900.0).abs() < 1.0, "{}", area(&s, new));
|
||||
|
||||
// An isolated object that no longer exists counts as no isolation, for the tool as for the
|
||||
// command: the area outside the group fills.
|
||||
s.doc_mut().unwrap().isolation = Some(NodeId(999_999));
|
||||
s.pointer(&PointerEvent::new(PointerKind::Move, 250.0, 70.0), v).unwrap();
|
||||
let red = s.overlays(v).into_iter().filter(|o| matches!(o, Overlay::Path { color: [0xff, 0x2a, 0x2a], .. })).count();
|
||||
assert_eq!(red, 1, "the area outside the group is outlined");
|
||||
s.pointer(&PointerEvent::new(PointerKind::Down, 250.0, 70.0), v).unwrap();
|
||||
s.pointer(&PointerEvent::new(PointerKind::Up, 250.0, 70.0), v).unwrap();
|
||||
let new = s.doc().unwrap().selection.objects[0];
|
||||
assert_eq!(s.doc().unwrap().doc.parent_of(new), s.doc().unwrap().doc.default_layer());
|
||||
}
|
||||
|
||||
/// While a compound path is isolated, Fill Area finds no area, for the command and the tool, and
|
||||
/// changes nothing: the compound path cannot hold the new path. The same goes for an isolated
|
||||
/// Objects on Path group, a group with a Transform effect and a group that is a clipping mask.
|
||||
#[test]
|
||||
fn fill_area_finds_nothing_while_a_compound_path_is_isolated() {
|
||||
let mut s = session();
|
||||
let outer = rect(&mut s, 0.0, 0.0, 100.0, 100.0);
|
||||
let inner = rect(&mut s, 25.0, 25.0, 50.0, 50.0);
|
||||
s.execute("select.set", &json!({"ids": [outer.0, inner.0]})).unwrap();
|
||||
let c = id_of(&s.execute("object.compoundPath.make", &json!({})).unwrap());
|
||||
deselect(&mut s);
|
||||
assert!(s.execute("shapeBuilder.copyRegion", &json!({"point": [10, 10]})).is_ok(), "the ring fills outside isolation");
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
s.doc_mut().unwrap().isolation = Some(c);
|
||||
let (undo, kids) = (undo_len(&s), stack(&s));
|
||||
assert!(s.execute("shapeBuilder.copyRegion", &json!({"point": [10, 10]})).is_err());
|
||||
let v = ViewInfo::default();
|
||||
s.select_tool("fillArea", v).unwrap();
|
||||
s.pointer(&PointerEvent::new(PointerKind::Move, 10.0, 10.0), v).unwrap();
|
||||
assert!(!s.overlays(v).iter().any(|o| matches!(o, Overlay::Path { color: [0xff, 0x2a, 0x2a], .. })));
|
||||
s.pointer(&PointerEvent::new(PointerKind::Down, 10.0, 10.0), v).unwrap();
|
||||
s.pointer(&PointerEvent::new(PointerKind::Up, 10.0, 10.0), v).unwrap();
|
||||
assert_eq!((undo_len(&s), stack(&s)), (undo, kids));
|
||||
|
||||
// An Objects on Path group moves its members along the path, so a new path in it would not
|
||||
// stay where the area is.
|
||||
let mut s = session();
|
||||
let a = rect(&mut s, 0.0, 0.0, 40.0, 40.0);
|
||||
let b = rect(&mut s, 100.0, 0.0, 40.0, 40.0);
|
||||
let path = line(&mut s, (0.0, 200.0), (300.0, 200.0));
|
||||
s.execute("select.set", &json!({"ids": [a.0, b.0]})).unwrap();
|
||||
let g = id_of(&s.execute("object.objectsOnPath.attach", &json!({"path": path.0})).unwrap());
|
||||
deselect(&mut s);
|
||||
let c = s.doc().unwrap().doc.node(a).unwrap().geometric_bounds().unwrap().center();
|
||||
assert!(s.execute("shapeBuilder.copyRegion", &json!({"point": [c.x, c.y]})).is_ok(), "the square fills outside isolation");
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
s.doc_mut().unwrap().isolation = Some(g);
|
||||
let (undo, kids) = (undo_len(&s), stack(&s));
|
||||
assert!(s.execute("shapeBuilder.copyRegion", &json!({"point": [c.x, c.y]})).is_err());
|
||||
assert_eq!((undo_len(&s), stack(&s)), (undo, kids));
|
||||
|
||||
// A group with a Transform effect moves what is in it, so a new path in it would not be drawn
|
||||
// where the area is.
|
||||
let mut s = session();
|
||||
let lines = triangle(&mut s);
|
||||
let ids: Vec<u64> = lines.iter().map(|l| l.0).collect();
|
||||
s.execute("select.set", &json!({"ids": ids})).unwrap();
|
||||
let g = id_of(&s.execute("object.group", &json!({})).unwrap());
|
||||
s.execute("effect.apply", &json!({"effect": "distort.transform", "params": {"moveH": 100}, "ids": [g.0], "item": null})).unwrap();
|
||||
deselect(&mut s);
|
||||
s.doc_mut().unwrap().isolation = Some(g);
|
||||
let (undo, kids) = (undo_len(&s), stack(&s));
|
||||
assert!(s.execute("shapeBuilder.copyRegion", &json!({"point": [50, 70]})).is_err());
|
||||
assert_eq!((undo_len(&s), stack(&s), node(&s, g).children().unwrap().len()), (undo, kids, 3));
|
||||
|
||||
// A group that is a clipping mask clips and is not painted, so it takes no new path.
|
||||
let mut s = session();
|
||||
let back = rect(&mut s, -20.0, -20.0, 140.0, 140.0);
|
||||
let edge = rect(&mut s, 0.0, 0.0, 100.0, 100.0);
|
||||
s.execute("select.set", &json!({"ids": [back.0, edge.0]})).unwrap();
|
||||
let clip = id_of(&s.execute("object.clippingMask.make", &json!({})).unwrap());
|
||||
s.execute("select.set", &json!({"ids": [edge.0]})).unwrap();
|
||||
let mask = id_of(&s.execute("object.group", &json!({})).unwrap());
|
||||
// A filled square drawn in the mask group bounds an area of its own.
|
||||
s.doc_mut().unwrap().isolation = Some(mask);
|
||||
let square = rect(&mut s, 10.0, 10.0, 80.0, 80.0);
|
||||
assert_eq!(s.doc().unwrap().doc.parent_of(square), Some(mask));
|
||||
deselect(&mut s);
|
||||
let first = node(&s, clip).children().unwrap()[0].id;
|
||||
assert_eq!(first, mask, "the group is the clipping mask");
|
||||
let (undo, kids) = (undo_len(&s), stack(&s));
|
||||
let members = node(&s, mask).children().unwrap().len();
|
||||
assert!(s.execute("shapeBuilder.copyRegion", &json!({"point": [50, 70]})).is_err());
|
||||
assert_eq!((undo_len(&s), stack(&s), node(&s, mask).children().unwrap().len()), (undo, kids, members));
|
||||
}
|
||||
|
||||
/// The new path goes directly above the front-most object along the area, not above art that
|
||||
/// lies elsewhere, and paths on other layers bound areas too.
|
||||
#[test]
|
||||
|
||||
Reference in New Issue
Block a user