Viewer: the Snapping menu has After Effects' two options

The Tools bar's Snapping options menu (#265) offered per-feature
toggles (Edges, Corners, Centers, Anchor Points, Mask and Shape Path
Points) that After Effects' Snapping menu doesn't have. It now has the
two After Effects has:

- Snap Edges Extended (as before).
- Snap to Features in Collapsed Compositions and Text Layers: the
  visible layers inside a precomp layer with Collapse Transformations
  on are snap targets too, mapped into the outer comp (points always,
  edge lines while the precomp layer is unrotated); a snap highlights
  the precomp layer. Nested collapsed precomps are followed eight deep.

Every layer feature snaps again (corners, edge midpoints, centre,
anchor point, mask and shape path points); the snapping engine itself
is unchanged. `view.snappingOptions` takes `edgesExtended` and
`collapsedFeatures` (and `toggle` of either); the old keys are refused.

Test: collapsed_precomp_features_snap (fails before: no such option),
snapping_options_command_sets_and_toggles (the per-feature keys are
refused), snapping_options_pick_the_targets (AE behaviour kept: edges
extended or not, hidden layers, guides only), and the Snapping menu UI
test (only the two options are listed).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
bflatastic
2026-10-09 21:03:12 +07:00
co-authored by Claude Opus 5.5
parent b9d1f79f7d
commit 6ce58040fd
7 changed files with 162 additions and 109 deletions
+98 -68
View File
@@ -67,48 +67,34 @@ impl SnapTarget {
}
}
/// Tools bar ▸ Snapping options: which layer features snap (the dragged layer's and the
/// targets), and Snap Edges Extended (layer edges snap along their whole line, beyond the
/// layer's bounds). Everything is on by default.
/// Tools bar ▸ Snapping options, the two After Effects' Snapping menu has: Snap Edges Extended
/// (layer edges snap along their whole line, beyond the layer's bounds) and Snap to Features in
/// Collapsed Compositions and Text Layers (the layers inside a precomp layer with Collapse
/// Transformations on are targets too). Both are on by default. Every layer feature snaps
/// (corners, edge midpoints, centre, anchor point, mask and shape path points), as in After
/// Effects.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default, rename_all = "camelCase")]
pub struct SnapFeatures {
pub edges_extended: bool,
/// Edges and edge midpoints of layers, and the comp's edges.
pub edges: bool,
pub corners: bool,
/// Layer centres and the comp's centre.
pub centers: bool,
pub anchor_points: bool,
/// Mask and shape path points.
pub paths: bool,
pub collapsed_features: bool,
}
impl Default for SnapFeatures {
fn default() -> Self {
SnapFeatures { edges_extended: true, edges: true, corners: true, centers: true, anchor_points: true, paths: true }
SnapFeatures { edges_extended: true, collapsed_features: true }
}
}
impl SnapFeatures {
/// The options in the Tools bar's Snapping menu order: (parameter key, label).
pub const OPTIONS: [(&'static str, &'static str); 6] = [
("edgesExtended", "Snap Edges Extended"),
("edges", "Edges"),
("corners", "Corners"),
("centers", "Centers"),
("anchorPoints", "Anchor Points"),
("paths", "Mask and Shape Path Points"),
];
pub const OPTIONS: [(&'static str, &'static str); 2] =
[("edgesExtended", "Snap Edges Extended"), ("collapsedFeatures", "Snap to Features in Collapsed Compositions and Text Layers")];
/// The option called `key` (an [`OPTIONS`](Self::OPTIONS) key).
pub fn option_mut(&mut self, key: &str) -> Option<&mut bool> {
Some(match key {
"edgesExtended" => &mut self.edges_extended,
"edges" => &mut self.edges,
"corners" => &mut self.corners,
"centers" => &mut self.centers,
"anchorPoints" => &mut self.anchor_points,
"paths" => &mut self.paths,
"collapsedFeatures" => &mut self.collapsed_features,
_ => return None,
})
}
@@ -187,15 +173,13 @@ pub fn snap(sources: &[[f64; 2]], targets: &[SnapTarget], tol: f64) -> Option<Sn
Some(Snap { delta, hits, at })
}
/// Snap features of a layer in comp space that `f` turns on: corners, edge midpoints, centre,
/// anchor point and mask / shape path vertices (no box for layers without bounds).
pub fn layer_features(ctx: &EvalCtx, layer: &Layer, f: SnapFeatures) -> Vec<[f64; 2]> {
/// Snap features of a layer in comp space: corners, edge midpoints, centre, anchor point and
/// mask / shape path vertices (no box for layers without bounds).
pub fn layer_features(ctx: &EvalCtx, layer: &Layer) -> Vec<[f64; 2]> {
let (m, _) = ctx.layer_to_comp(layer);
let mut out = layer_box(ctx, layer, &m).map(|b| b.features(f)).unwrap_or_default();
out.extend(anchor_feature(ctx, layer, &m).filter(|_| f.anchor_points));
if f.paths {
out.extend(layer_vertices(ctx, layer));
}
let mut out = layer_box(ctx, layer, &m).map(|b| b.points()).unwrap_or_default();
out.extend(anchor_feature(ctx, layer, &m));
out.extend(layer_vertices(ctx, layer));
out
}
@@ -208,19 +192,9 @@ struct BoxPoints {
}
impl BoxPoints {
/// The points `f` turns on.
fn features(&self, f: SnapFeatures) -> Vec<[f64; 2]> {
let mut out = vec![];
if f.corners {
out.extend(self.corners);
}
if f.edges {
out.extend(self.mids);
}
if f.centers {
out.push(self.center);
}
out
/// Corners, edge midpoints and centre.
fn points(&self) -> Vec<[f64; 2]> {
self.corners.iter().chain(&self.mids).chain([&self.center]).copied().collect()
}
}
@@ -301,8 +275,8 @@ pub struct SnapOptions {
}
/// Snap targets of a comp at the context time: other visible layers' features and vertices and
/// the comp's edges and centre (with `layers`, as `features` allows), and guides and grid lines
/// (when on). Layers in `exclude` (the dragged ones) are skipped.
/// the comp's edges and centre (with `layers`, as the Snapping options allow), and guides and
/// grid lines (when on). Layers in `exclude` (the dragged ones) are skipped.
pub fn targets(ctx: &EvalCtx, exclude: &[LayerId], opts: SnapOptions) -> Vec<SnapTarget> {
let comp = ctx.comp;
let (w, h) = (comp.width as f64, comp.height as f64);
@@ -315,17 +289,10 @@ pub fn targets(ctx: &EvalCtx, exclude: &[LayerId], opts: SnapOptions) -> Vec<Sna
out.extend(x.iter().map(|x| SnapTarget::vline(*x, PRI_COMP, SnapSource::Comp).within(ys)));
out.extend(y.iter().map(|y| SnapTarget::hline(*y, PRI_COMP, SnapSource::Comp).within(xs)));
};
if f.edges {
lines(&[0.0, w], &[0.0, h]);
}
if f.centers {
lines(&[w / 2.0], &[h / 2.0]);
out.push(SnapTarget::point([w / 2.0, h / 2.0], PRI_COMP, SnapSource::Comp));
}
// Visible layers: eye on, and soloed while any layer is.
let any_solo = comp.layers.iter().any(|l| l.switches.solo && l.source.is_av() && l.is_active_at(ctx.time));
let visible = |l: &&Layer| l.is_active_at(ctx.time) && l.switches.video && (!any_solo || l.switches.solo) && !exclude.contains(&l.id);
for l in comp.layers.iter().filter(visible) {
lines(&[0.0, w], &[0.0, h]);
lines(&[w / 2.0], &[h / 2.0]);
out.push(SnapTarget::point([w / 2.0, h / 2.0], PRI_COMP, SnapSource::Comp));
for l in visible_layers(ctx, exclude) {
out.extend(layer_targets(ctx, l, true, f));
}
}
@@ -354,11 +321,27 @@ pub fn targets(ctx: &EvalCtx, exclude: &[LayerId], opts: SnapOptions) -> Vec<Sna
out
}
/// A 2D layer's snap targets as `f` allows: its box points, its edges as lines while unrotated
/// (across the comp with Snap Edges Extended, along the layer otherwise), its anchor point
/// unless `anchor` is false (Pan Behind drags it, the box stays put) and its mask and shape path
/// vertices. None for cameras, lights and 3D layers.
/// How deep Snap to Features in Collapsed Compositions looks into nested collapsed precomps.
const MAX_COLLAPSED_DEPTH: usize = 8;
/// Visible layers of a comp at the context time: active, eye on, and soloed while any layer is.
fn visible_layers<'a>(ctx: &EvalCtx<'a>, exclude: &'a [LayerId]) -> impl Iterator<Item = &'a Layer> {
let comp = ctx.comp;
let time = ctx.time;
let any_solo = comp.layers.iter().any(|l| l.switches.solo && l.source.is_av() && l.is_active_at(time));
comp.layers.iter().filter(move |l| l.is_active_at(time) && l.switches.video && (!any_solo || l.switches.solo) && !exclude.contains(&l.id))
}
/// A 2D layer's snap targets: its box points, its edges as lines while unrotated (across the
/// comp with Snap Edges Extended, along the layer otherwise), its anchor point unless `anchor` is
/// false (Pan Behind drags it, the box stays put), its mask and shape path vertices and, with
/// Snap to Features in Collapsed Compositions, the targets of the visible layers inside a
/// collapsed precomp layer. None for cameras, lights and 3D layers.
pub fn layer_targets(ctx: &EvalCtx, l: &Layer, anchor: bool, f: SnapFeatures) -> Vec<SnapTarget> {
layer_targets_at(ctx, l, anchor, f, 0)
}
fn layer_targets_at(ctx: &EvalCtx, l: &Layer, anchor: bool, f: SnapFeatures, depth: usize) -> Vec<SnapTarget> {
if l.is_camera() || l.is_light() || l.is_3d() {
return vec![];
}
@@ -367,7 +350,7 @@ pub fn layer_targets(ctx: &EvalCtx, l: &Layer, anchor: bool, f: SnapFeatures) ->
let mut out = vec![];
if let Some(b) = layer_box(ctx, l, &m) {
let [nw, ne, _, sw] = b.corners;
if f.edges && (nw[1] - ne[1]).abs() < 1e-6 && (nw[0] - sw[0]).abs() < 1e-6 {
if (nw[1] - ne[1]).abs() < 1e-6 && (nw[0] - sw[0]).abs() < 1e-6 {
let (xs, ys) = if f.edges_extended { (None, None) } else { (Some([nw[0], ne[0]]), Some([nw[1], sw[1]])) };
for x in [nw[0], ne[0]] {
out.push(SnapTarget::vline(x, PRI_LAYER, src).within(ys));
@@ -376,11 +359,58 @@ pub fn layer_targets(ctx: &EvalCtx, l: &Layer, anchor: bool, f: SnapFeatures) ->
out.push(SnapTarget::hline(y, PRI_LAYER, src).within(xs));
}
}
out.extend(b.features(f).into_iter().map(|p| SnapTarget::point(p, PRI_LAYER, src)));
out.extend(b.points().into_iter().map(|p| SnapTarget::point(p, PRI_LAYER, src)));
}
out.extend(anchor_feature(ctx, l, &m).filter(|_| anchor && f.anchor_points).map(|p| SnapTarget::point(p, PRI_LAYER, src)));
if f.paths {
out.extend(layer_vertices(ctx, l).into_iter().map(|p| SnapTarget::point(p, PRI_VERTEX, SnapSource::Vertex(l.id))));
out.extend(anchor_feature(ctx, l, &m).filter(|_| anchor).map(|p| SnapTarget::point(p, PRI_LAYER, src)));
out.extend(layer_vertices(ctx, l).into_iter().map(|p| SnapTarget::point(p, PRI_VERTEX, SnapSource::Vertex(l.id))));
if f.collapsed_features
&& l.switches.collapse
&& depth < MAX_COLLAPSED_DEPTH
&& let effectcraft_project::LayerSource::Comp { item } = l.source
&& let Some(nc) = ctx.project.comp(item)
{
out.extend(collapsed_targets(ctx, l, item, nc, &m, f, depth));
}
out
}
/// The targets of the layers inside collapsed precomp layer `l` (showing comp `nc`), in the
/// outer comp (`m`: `l`'s layer → comp). They highlight `l` when they snap. Lines stay lines
/// only while `l` is unrotated; otherwise only the points snap.
fn collapsed_targets(
ctx: &EvalCtx,
l: &Layer,
item: effectcraft_project::ItemId,
nc: &effectcraft_project::Comp,
m: &Mat3,
f: SnapFeatures,
depth: usize,
) -> Vec<SnapTarget> {
let nctx = EvalCtx { comp_id: item, comp: nc, time: ctx.nested_time(l), ..*ctx };
let axis_aligned = m.0[0][1].abs() < 1e-9 && m.0[1][0].abs() < 1e-9;
let (sx, ox, sy, oy) = (m.0[0][0], m.0[0][2], m.0[1][1], m.0[1][2]);
let mut out = vec![];
for nl in visible_layers(&nctx, &[]) {
for t in layer_targets_at(&nctx, nl, true, f, depth + 1) {
let source = match t.source {
SnapSource::Vertex(_) => SnapSource::Vertex(l.id),
_ => SnapSource::Layer(l.id),
};
let mapped = match t.kind {
SnapKind::Point => {
let q = m.apply(vec2(t.pos[0], t.pos[1]));
Some(SnapTarget::point([q.x, q.y], t.priority, source))
}
SnapKind::VLine if axis_aligned => {
Some(SnapTarget::vline(sx * t.pos[0] + ox, t.priority, source).within(t.span.map(|[a, b]| [sy * a + oy, sy * b + oy])))
}
SnapKind::HLine if axis_aligned => {
Some(SnapTarget::hline(sy * t.pos[1] + oy, t.priority, source).within(t.span.map(|[a, b]| [sx * a + ox, sx * b + ox])))
}
_ => None,
};
out.extend(mapped);
}
}
out
}