Use Preview Bounds: clicks on a layer with a 3D effect select its members

The art that clicks and marquees test leaves 3D effects unprojected again.
The faces of a layer's 3D effect carry the layer's id, which a click
cannot select, so a click on a member of such a layer selected nothing.
Use Preview Bounds still measures the projected art.
This commit is contained in:
dotCooCoo
2026-11-16 00:24:24 -08:00
parent b10ceca2aa
commit 6a493e0fee
2 changed files with 61 additions and 16 deletions
+32 -16
View File
@@ -13,7 +13,8 @@
//! before its effects, which the renderer clips by, and those strokes as drawn. Fills and
//! strokes that paint nothing (hidden, or painted None) are left out. Extrude & Bevel, Revolve,
//! Inflate and Rotate are measured by the same projected vector art the renderer draws, with
//! any geometry effects after projection applied to that art. [`bounds_of`],
//! any geometry effects after projection applied to that art; [`drawn_art`], which clicks and
//! marquees use, leaves 3D effects unprojected. [`bounds_of`],
//! [`oriented_bounds`] and [`anchor_bounds`] measure the visual bounds of that art, strokes
//! included, where [`Document::bounds_of`], [`Document::oriented_bounds`] and
//! [`Selection::anchor_bounds`] measure the objects themselves. Raster effects (shadows, glows,
@@ -193,8 +194,8 @@ pub fn laid_out_ink(layout: &vectorcraft_text::TextLayout, t: &vectorcraft_doc::
.fold(layout.ink_bounds(), |acc, (_, bar)| union_opt(acc, Some(bar.bounding_box())))
}
/// The visual bounds of `n` as drawn: those of its [`drawn_art`], else
/// [`Node::visual_bounds_with`].
/// The visual bounds of `n` as drawn: those of its [`drawn_art`] with its 3D effects projected,
/// else [`Node::visual_bounds_with`].
pub fn visual_bounds(symbols: &SymbolBoxes, n: &Node) -> Option<Rect> {
Drawn::new(symbols).visual_bounds(n)
}
@@ -205,32 +206,46 @@ pub fn bounds_in(symbols: &SymbolBoxes, n: &Node, a: Affine) -> Option<Rect> {
Drawn::new(symbols).bounds_in(n, a)
}
/// `n` as plain art with geometry and projected 3D effects applied, at every depth, symbol instances
/// measured by `symbols`. Raster effects stay on the art. A group, layer or intertwine without a
/// clip is rebuilt with the drawn art of each member that has one. A clip group or layer becomes a
/// group of its clipping path without strokes and the drawn art of the path's strokes, when they
/// have one. `None` when no supported geometry or 3D effect in `n` changes what it draws.
/// `n` as plain art with the geometry effects in it applied, at every depth, symbol instances
/// measured by `symbols`, for clicks and marquees. Raster effects stay on the art. A group, layer or
/// intertwine without a clip is rebuilt with the drawn art of each member that has one. A clip group
/// or layer becomes a group of its clipping path without strokes and the drawn art of the path's
/// strokes, when they have one. 3D effects stay on the art unprojected, so each object in it keeps
/// its own id. `None` when no geometry effect in `n` changes what it draws.
pub fn drawn_art(symbols: &SymbolBoxes, n: &Node) -> Option<Node> {
Drawn::new(symbols).drawn_art(n)
Drawn::without_3d(symbols).drawn_art(n)
}
/// Whether [`drawn_art`] gives `n` art of its own, or would give one to an object in it. A symbol
/// instance in `n` counts when its symbol's art has geometry or 3D effects.
/// Whether `n` is measured by art of its own with Use Preview Bounds, or an object in it is: a
/// geometry or 3D effect in it changes what it draws. A symbol instance in `n` counts when its
/// symbol's art has geometry or 3D effects.
pub fn draws_geometry(symbols: &SymbolBoxes, n: &Node) -> bool {
Drawn::new(symbols).draws_geometry(n)
}
/// One measurement: whether each symbol checked so far draws geometry or 3D art, by symbol name,
/// and how many more instances it measures with those effects in the current object ([`MAX_INSTANCES`]).
/// With `three_d` false, 3D effects are left on the art unprojected ([`drawn_art`]).
struct Drawn<'s, 'd> {
symbols: &'s SymbolBoxes<'d>,
draws: RefCell<HashMap<String, bool>>,
left: Cell<usize>,
three_d: bool,
}
impl<'s, 'd> Drawn<'s, 'd> {
fn new(symbols: &'s SymbolBoxes<'d>) -> Self {
Drawn { symbols, draws: RefCell::default(), left: Cell::new(MAX_INSTANCES) }
Drawn { symbols, draws: RefCell::default(), left: Cell::new(MAX_INSTANCES), three_d: true }
}
/// A measurement that projects no 3D effect, for clicks and marquees ([`drawn_art`]).
fn without_3d(symbols: &'s SymbolBoxes<'d>) -> Self {
Drawn { three_d: false, ..Self::new(symbols) }
}
/// Whether `n` has a 3D effect of its own that this measurement projects.
fn projects(&self, n: &Node) -> bool {
self.three_d && (has_revolve(n) || has_solid(n))
}
/// Sets the count of instances left back to [`MAX_INSTANCES`], for the next object.
@@ -275,8 +290,7 @@ impl<'s, 'd> Drawn<'s, 'd> {
let mut found = false;
n.walk(&mut |c| {
found = found
|| has_revolve(c)
|| has_solid(c)
|| self.projects(c)
|| match &c.kind {
NodeKind::SymbolInstance { symbol, .. } => !geometry_effects(c).is_empty() || self.symbol_draws(symbol),
_ => reshapes(c),
@@ -307,12 +321,14 @@ impl<'s, 'd> Drawn<'s, 'd> {
fn own_art(&self, n: &Node) -> Option<Node> {
let doc = Some(self.symbols.document());
// A symbol instance with a 3D effect of its own counts toward MAX_INSTANCES before its art is built.
let counted = matches!(n.kind, NodeKind::SymbolInstance { .. }) && (has_revolve(n) || has_solid(n));
let counted = matches!(n.kind, NodeKind::SymbolInstance { .. }) && self.projects(n);
if counted {
let left = self.left.get().checked_sub(1)?;
self.left.set(left);
}
if let Some(art) = revolve_art_in(doc, n).or_else(|| solid_art_in(doc, n)) {
if self.three_d
&& let Some(art) = revolve_art_in(doc, n).or_else(|| solid_art_in(doc, n))
{
// The builders retain later geometry on the projected group. Measure it after that
// geometry too; item-level effects are handled by the ordinary member traversal.
return Some(self.own_art(&art).unwrap_or(art));
+29
View File
@@ -352,6 +352,35 @@ fn preview_3d_oriented_box_follows_projected_art() {
}
}
/// A click on a member of a layer with a 3D effect selects that member, with Use Preview Bounds on
/// or off, and the layer is still measured by its projected art.
#[test]
fn a_click_selects_the_member_of_a_layer_with_a_3d_effect() {
for effect in PREVIEW_3D {
for on in [false, true] {
let mut s = session();
let id = rect(&mut s, 100.0, 80.0, 0.0);
run(&mut s, "paint.setFill", json!({"color": "#0000ff"}));
let layer = s.doc().unwrap().doc.layers[0].id;
run(&mut s, "effect.apply", json!({"ids": [layer.0], "effect": effect, "params": params_3d(effect), "item": null}));
preview(&mut s, on);
run(&mut s, "select.none", json!({}));
let v = ViewInfo::default();
s.select_tool("selection", v).unwrap();
for k in [PointerKind::Down, PointerKind::Up] {
s.pointer(&PointerEvent::new(k, 140.0, 125.0), v).unwrap();
}
assert_eq!(s.active().unwrap().selection.objects, [id], "{effect}, preview={on}");
let d = &s.doc().unwrap().doc;
let art =
vectorcraft_render::effects::revolve_art(&d.layers[0]).or_else(|| vectorcraft_render::effects::solid_art(&d.layers[0])).unwrap();
let expected = vectorcraft_render::effects::preview::visual_bounds(&d.symbol_boxes(), &art).unwrap();
let measured = vectorcraft_render::effects::preview::bounds_of(d, &[layer], true).unwrap();
assert!(near(measured, expected, 1e-6), "{effect}: {measured:?} vs {expected:?}");
}
}
}
#[test]
fn a_square_with_offset_path_is_measured_by_the_offset_shape() {
let mut s = session();