diff --git a/crates/doc/src/hit.rs b/crates/doc/src/hit.rs index 6320be059d..6507112c24 100644 --- a/crates/doc/src/hit.rs +++ b/crates/doc/src/hit.rs @@ -131,6 +131,20 @@ pub fn set_instance_row_art(f: InstanceRowArt) { let _ = INSTANCE_ROW_ART.set(f); } +/// Does a symbol instance's active fill or stroke carry a visible geometry effect, which the art it +/// places takes on each of its fills and strokes ([`set_instance_row_art`])? Effects are classified +/// above this crate, so the engine registers one with [`set_instance_reshapes`]. +pub type InstanceReshapes = fn(&Node) -> bool; + +static INSTANCE_RESHAPES: OnceLock = OnceLock::new(); + +/// Register how Direct Selection tells whether a symbol instance's fill or stroke effects reshape +/// its art (the first registration stays). +pub fn set_instance_reshapes(f: InstanceReshapes) { + // Already set: the same engine, registered by another session. + let _ = INSTANCE_RESHAPES.set(f); +} + /// The shapes that [`ImagePaint`] gives for one object. type Painted = Vec<(BezPath, FillRule, AppearanceItem)>; @@ -1603,17 +1617,18 @@ fn painted_hit(painted: &Painted, p: Point, opt: HitOptions) -> Option }) } -/// Is `n` a symbol instance whose active fill or stroke has geometry effects that reshape the art -/// it draws ([`image_paint`])? Callers ask [`own_effects_art`] first, which takes an instance whose +/// Is `n` a symbol instance whose active fill or stroke has a visible geometry effect, which +/// reshapes the art it draws ([`set_instance_reshapes`])? This holds whatever that art is, clip +/// groups and type included. Callers ask [`own_effects_art`] first, which takes an instance whose /// own effects change what it draws; an instance whose own effects add only pixels (a drop shadow) /// is tested here. Never with `outline`, where the canvas draws no effects, nor while a -/// [`DrawnOff`] guard lives. -fn reshapes_instance_art(n: &Node, symbols: &SymbolBoxes, outline: bool) -> bool { +/// [`DrawnOff`] guard lives, nor while nothing is registered. +fn reshapes_instance_art(n: &Node, outline: bool) -> bool { !outline && !DRAWN_OFF.get() && matches!(n.kind, NodeKind::SymbolInstance { .. }) && n.appearance.items.iter().any(|i| !i.effects().is_empty()) - && !image_paint(Some(symbols.document()), n).is_empty() + && INSTANCE_RESHAPES.get().is_some_and(|f| f(n)) } /// How many objects `n` holds with its opacity masks' art ([`Node::count_with_masks`]), what @@ -1905,7 +1920,7 @@ impl FindPart<'_> { let _art_only = ArtOnly::set(); return hit_any(&art, self.p, opt, self.symbols); } - if reshapes_instance_art(&placed, self.symbols, opt.outline) { + if reshapes_instance_art(&placed, opt.outline) { return drawn_instance_hit(&placed, self.p, opt, self.symbols); } } @@ -2235,7 +2250,7 @@ fn outline_within(n: &Node, symbols: &SymbolBoxes, limit: Option, outline: return; } if let NodeKind::SymbolInstance { symbol, .. } = &n.kind - && reshapes_instance_art(n, symbols, outline) + && reshapes_instance_art(n, outline) { if symbols.art(symbol).is_some() && let Some(art) = InstanceArt::nested(symbols.document(), n) diff --git a/crates/doc/src/hit/tests.rs b/crates/doc/src/hit/tests.rs index 7c704511c3..ea38de43b8 100644 --- a/crates/doc/src/hit/tests.rs +++ b/crates/doc/src/hit/tests.rs @@ -3041,8 +3041,14 @@ fn a_click_counts_the_members_of_an_intertwine_group_in_symbol_art_toward_one_li assert_eq!(with_art_limit(100, || at(140.0)).0, Some(id), "on the squares' paths"); } +/// Whether a symbol instance's fill or stroke effects reshape its art, for these tests: when +/// [`painted_far_away`] gives it shapes. +fn reshapes_when_painted(n: &Node) -> bool { + !painted_far_away(None, n).is_empty() +} + /// An instance of the symbol named `symbol`, not moved, whose fill has a geometry effect: -/// [`painted_far_away`] gives it shapes when the name starts with "Painted", so a part pick and a +/// [`reshapes_when_painted`] holds for it when the name starts with "Painted", so a part pick and a /// part outline walk its art as drawn with its fill's effects ([`InstanceArt::drawn`]). fn painted_instance_of(d: &mut Document, symbol: &str) -> Arc { let mut n = Node::new(d.alloc_id(), NodeKind::SymbolInstance { symbol: symbol.into(), xf: vectorcraft_geom::Affine::IDENTITY }); @@ -3075,6 +3081,7 @@ fn painted_chain(d: &mut Document) -> NodeId { #[test] fn a_parts_outline_walks_nested_reshaped_symbol_instances_within_one_limit() { set_image_paint(painted_far_away); + set_instance_reshapes(reshapes_when_painted); let mut d = Document::new(600.0, 400.0); let id = painted_chain(&mut d); let n = d.node(id).unwrap(); @@ -3093,6 +3100,7 @@ fn a_parts_outline_walks_nested_reshaped_symbol_instances_within_one_limit() { #[test] fn a_part_pick_walks_nested_reshaped_symbol_instances_within_one_limit() { set_image_paint(painted_far_away); + set_instance_reshapes(reshapes_when_painted); let mut d = Document::new(600.0, 400.0); let id = painted_chain(&mut d); let opt = HitOptions::default(); diff --git a/crates/engine/src/session/documents.rs b/crates/engine/src/session/documents.rs index 0272e8fb76..1685adc40f 100644 --- a/crates/engine/src/session/documents.rs +++ b/crates/engine/src/session/documents.rs @@ -27,6 +27,7 @@ impl Session { // Direct Selection picks and outlines the parts of a dynamic symbol's instance where the // effects of the instance's fill and stroke draw them. vectorcraft_doc::hit::set_instance_row_art(vectorcraft_render::effects::add_instance_row_effects_to_parts); + vectorcraft_doc::hit::set_instance_reshapes(vectorcraft_render::effects::has_item_geometry); // Clicks inside a clip group fall in the region its clipping path's effects give it. vectorcraft_doc::hit::set_clip_region(vectorcraft_render::effects::reshaped_clip_outline); // Clicks and marquees find paths, compound paths, groups and layers where their geometry diff --git a/crates/engine/src/tests/dynamic_symbols.rs b/crates/engine/src/tests/dynamic_symbols.rs index 6f4973cd15..fc1305a0a7 100644 --- a/crates/engine/src/tests/dynamic_symbols.rs +++ b/crates/engine/src/tests/dynamic_symbols.rs @@ -741,6 +741,32 @@ fn a_picked_clipping_path_is_outlined_by_its_own_geometry() { assert!((b.x0 - 10.0).abs() < 0.5 && (b.x1 - 50.0).abs() < 0.5, "the clipping path's own edge: {b:?}"); } +/// A symbol instance in a dynamic symbol's art whose own art is a clip group takes the instance's +/// fill effect on the clip group's members, which the canvas draws moved: Direct Selection picks it +/// where its member is drawn, not where the member's own shape is, and outlines it there. +#[test] +fn a_nested_instance_of_a_clip_group_is_picked_where_the_fill_effect_draws_its_art() { + use vectorcraft_doc::hit::HitOptions; + let opt = HitOptions { tol: 1.0, ..Default::default() }; + let mut s = session(); + let content = square(&mut s, 10.0); + let clip = id(&s.execute("shape.rectangle", &json!({"x": 0, "y": 0, "width": 200, "height": 60})).unwrap()); + s.execute("object.clippingMask.make", &json!({"ids": [content.0, clip.0]})).unwrap(); + let group = top(&s); + assert!(node(&s, group).clips()); + let inner = id(&s.execute("symbol.new", &json!({"name": "Inner", "ids": [group.0]})).unwrap()); + let b = square(&mut s, 160.0); + let inst = id(&s.execute("symbol.new", &json!({"name": "Outer", "ids": [inner.0, b.0], "type": "dynamic"})).unwrap()); + row_effect(&mut s, inst, true, "distort.transform", json!({"moveH": 100})); + // The member is drawn at x 110..150, inside the clipping path. + assert_eq!(pixel(&s, 130, 30), BLUE, "the canvas draws the member moved"); + assert_ne!(pixel(&s, 30, 30), BLUE, "and not in its own place"); + assert_eq!(part_at(&s, inst, (130.0, 30.0), opt), Some(vec![0]), "the nested instance where its member is drawn"); + assert_eq!(part_at(&s, inst, (30.0, 30.0), opt), None, "the member's own place"); + let o = part_outline_box(&s, inst, &[0]); + assert!((o.x0 - 110.0).abs() < 0.5 && (o.x1 - 150.0).abs() < 0.5, "the outline follows the moved member: {o:?}"); +} + #[test] fn a_drag_of_a_picked_object_moves_nothing_and_says_why() { let mut s = session();