Files
effectcraft/crates/ui-egui/src/panels/viewer_overlays.rs
T
Brandon ThomasandClaude Opus 5.5 468dc13cfe M0.13: viewer snapping, rulers, channels, snapshots, ROI
Composition viewer interaction parity with After Effects:
- Snapping (View > Snapping, the Tools bar checkbox, Cmd/Ctrl inverts while
  dragging): layer edges, centres and anchor points, mask and shape-path
  vertices, comp edges/centre, guides (Snap to Guides) and grid (Snap to
  Grid), with highlighted target box / line / cross feedback. Pure maths in
  engine::viewer (nearest target within tolerance, priority ties).
- Rulers (Cmd+R) with live pointer markers and a draggable zero point;
  guides dragged out of the rulers, moved in the viewer, dragged off to
  remove (view.moveGuide / view.removeGuide), lock respected; proportional
  grid.
- Show Channel (RGB/Red/Green/Blue/Alpha/RGB Straight, Colorize), exposure
  (+/- stops in linear light, reset), Take Snapshot (Shift+F5) with a stored
  render and Show Snapshot (F5 held / button held), Fast Previews (Off,
  Adaptive Resolution, Draft, Fast Draft, Wireframe), Resolution > Custom.
- Region of interest: drawn in the viewer; RenderOpts.roi renders only that
  rectangle (crop of the full render, tested), Crop Comp to ROI uses it.
- Bottom bar in AE 2026 order ("136.1 %", "(Full)").
- Shape Pen (SHP-1): Pen on a shape layer / nothing selected draws Shape n
  groups (Path 1, Stroke 1, Fill 1) via shape.newPath; the G slot cycles
  Add / Delete / Convert Vertex and Mask Feather; mask.* vertex commands
  work on shape paths; mask.insertVertex / mask.convertVertex; marquee
  vertex selection, Alt-drag/click converts, double-click free transform
  box (path.freeTransform on masks and shape paths).
- Motion paths (ANM-3): keyframe dots and spatial tangent handles, dragging
  keys (keys.set) and tangents (keys.setSpatialTangents), parented layers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-02 10:16:05 -04:00

529 lines
22 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! Composition viewer overlays and their drags: mask and shape paths (vertices, tangents,
//! segments for Add Vertex), position motion paths with keyframe dots and spatial Bezier
//! handles, the path free-transform box and the Mask Feather tool.
//!
//! Every edit is an engine command (`mask.*`, `path.freeTransform`, `keys.set`,
//! `keys.setSpatialTangents`, `prop.set`).
use effectcraft_engine::geom::{Mat3, vec2 as gv2};
use effectcraft_engine::keyframe::ShapePath;
use effectcraft_engine::project::{GroupKind, Layer, LayerId};
use effectcraft_engine::render::EvalCtx;
use effectcraft_engine::time::Tick;
use effectcraft_engine::{KeyRef, VertexRef};
use egui::{Color32, Pos2, Rect, Stroke, StrokeKind, vec2};
use serde_json::json;
use super::viewer::ViewerMap;
use crate::EffectcraftApp;
/// A vertex or tangent handle drawn this frame (masks and shape paths alike; `mask` is the path
/// group's uid).
#[derive(Clone, Copy)]
pub(crate) struct VertexHit {
pub layer: LayerId,
pub mask: u64,
pub index: usize,
pub pos: Pos2,
/// None = the vertex itself, Some(out?) = a tangent handle.
pub tangent: Option<bool>,
/// Path-space vertex position and path → comp matrix.
pub vertex: [f64; 2],
pub l2c: Mat3,
}
/// A path drawn this frame (for segment hits and the free-transform box).
#[derive(Clone)]
pub(crate) struct PathInfo {
pub layer: LayerId,
pub uid: u64,
/// Path space → comp.
pub m: Mat3,
pub sp: ShapePath,
pub is_mask: bool,
}
fn cubic(p0: [f64; 2], p1: [f64; 2], p2: [f64; 2], p3: [f64; 2], t: f64) -> [f64; 2] {
let u = 1.0 - t;
let f = |k: usize| u * u * u * p0[k] + 3.0 * u * u * t * p1[k] + 3.0 * u * t * t * p2[k] + t * t * t * p3[k];
[f(0), f(1)]
}
/// Control points of segment `i` (vertex i → next) in path space.
pub(crate) fn segment(sp: &ShapePath, i: usize) -> Option<[[f64; 2]; 4]> {
let n = sp.vertices.len();
let j = if i + 1 < n {
i + 1
} else if sp.closed && n > 1 {
0
} else {
return None;
};
let (a, d) = (sp.vertices[i], sp.vertices[j]);
let to = sp.out_tangents.get(i).copied().unwrap_or_default();
let ti = sp.in_tangents.get(j).copied().unwrap_or_default();
Some([a, [a[0] + to[0], a[1] + to[1]], [d[0] + ti[0], d[1] + ti[1]], d])
}
/// The path segment under a screen point: (layer, path uid, segment, t).
pub(crate) fn segment_at(paths: &[PathInfo], map: &ViewerMap, pos: Pos2, tol: f32) -> Option<(LayerId, u64, usize, f64)> {
let mut best: Option<(f32, (LayerId, u64, usize, f64))> = None;
for p in paths {
for i in 0..p.sp.vertices.len() {
let Some(c) = segment(&p.sp, i) else { continue };
for k in 1..48 {
let t = k as f64 / 48.0;
let q = cubic(c[0], c[1], c[2], c[3], t);
let cq = p.m.apply(gv2(q[0], q[1]));
let d = map.to_screen([cq.x, cq.y]).distance(pos);
if d < tol && best.as_ref().is_none_or(|b| d < b.0) {
best = Some((d, (p.layer, p.uid, i, t)));
}
}
}
}
best.map(|b| b.1)
}
/// A mask or shape path of a layer by group uid, evaluated at the context time.
pub(crate) fn path_of(ectx: &EvalCtx, layer: LayerId, uid: u64) -> Option<PathInfo> {
let l = ectx.comp.layer(layer)?;
let g = l.props.find_group(uid)?;
let sp = g.get("path").map(|pr| ectx.value(l, pr)).and_then(|v| v.as_path().cloned())?;
let (m, _) = super::viewer::l2c(ectx, l);
let is_mask = matches!(g.kind, GroupKind::Mask { .. });
let m = if is_mask { m } else { m * effectcraft_engine::render::shapes::item_matrix(ectx, l, uid).unwrap_or(Mat3::IDENTITY) };
Some(PathInfo { layer, uid, m, sp, is_mask })
}
fn flatten(sp: &ShapePath, m: &Mat3, map: &ViewerMap) -> Vec<Pos2> {
let k = effectcraft_engine::render::kurbo_path(sp);
let mut pts: Vec<Pos2> = vec![];
kurbo::flatten(k.iter(), 0.5, |el| match el {
kurbo::PathEl::MoveTo(q) | kurbo::PathEl::LineTo(q) => {
let c = m.apply(gv2(q.x, q.y));
pts.push(map.to_screen([c.x, c.y]));
}
_ => {}
});
if sp.closed && !pts.is_empty() {
pts.push(pts[0]);
}
pts
}
/// Draw the masks and (on shape layers) shape paths of a selected layer with their vertices;
/// selected vertices show their tangent handles. Collects vertex hits and paths.
#[allow(clippy::too_many_arguments)]
pub(crate) fn draw_paths(
app: &mut EffectcraftApp,
painter: &egui::Painter,
map: &ViewerMap,
ectx: &EvalCtx,
l: &Layer,
label_col: Color32,
sel: &[VertexRef],
pen: Option<(LayerId, u64)>,
hover: Option<Pos2>,
hits: &mut Vec<VertexHit>,
paths: &mut Vec<PathInfo>,
) {
let vs = app.session.prefs.general.path_point_size as f32 + 1.0;
let (m, _) = super::viewer::l2c(ectx, l);
let mut list: Vec<(u64, String, ShapePath, Mat3, Color32, bool)> = vec![];
if let Some(masks) = l.masks() {
for g in masks.groups() {
let GroupKind::Mask { color, locked, .. } = g.kind else { continue };
if locked && app.session.state.hide_locked_masks {
continue;
}
let Some(sp) = g.get("path").map(|pr| ectx.value(l, pr)).and_then(|v| v.as_path().cloned()) else { continue };
list.push((g.uid, g.name.clone(), sp, m, Color32::from_rgb(color[0], color[1], color[2]), true));
}
}
for (uid, sp) in effectcraft_engine::viewer::shape_paths(ectx, l) {
let im = effectcraft_engine::render::shapes::item_matrix(ectx, l, uid).unwrap_or(Mat3::IDENTITY);
let name = l.props.find_group(uid).map(|g| g.name.clone()).unwrap_or_default();
list.push((uid, name, sp, m * im, label_col, false));
}
for (uid, name, sp, pm, mc, is_mask) in list {
painter.add(egui::Shape::line(flatten(&sp, &pm, map), Stroke::new(1.0, mc)));
let scr = |p: [f64; 2]| {
let c = pm.apply(gv2(p[0], p[1]));
map.to_screen([c.x, c.y])
};
for (i, v) in sp.vertices.iter().enumerate() {
let s = scr(*v);
let selected = sel.iter().any(|x| x.layer == l.id && x.mask == uid && x.index == i);
if selected {
for (out, tg) in [(false, sp.in_tangents.get(i)), (true, sp.out_tangents.get(i))] {
let Some(tg) = tg.filter(|t| t[0] != 0.0 || t[1] != 0.0) else { continue };
let h = scr([v[0] + tg[0], v[1] + tg[1]]);
painter.line_segment([s, h], Stroke::new(1.0, mc));
painter.circle_filled(h, 3.0, mc);
hits.push(VertexHit { layer: l.id, mask: uid, index: i, pos: h, tangent: Some(out), vertex: *v, l2c: pm });
}
painter.rect_filled(Rect::from_center_size(s, vec2(vs, vs)), 0.0, mc);
} else {
painter.rect_filled(Rect::from_center_size(s, vec2(vs, vs)), 0.0, Color32::from_black_alpha(160));
painter.rect_stroke(Rect::from_center_size(s, vec2(vs, vs)), 0.0, Stroke::new(1.0, mc), StrokeKind::Inside);
}
let kind = if is_mask { "mask" } else { "shapePath" };
app.auto.add(&format!("viewer.{kind}.{uid}.vertex.{i}"), Rect::from_center_size(s, vec2(8.0, 8.0)), &name);
hits.push(VertexHit { layer: l.id, mask: uid, index: i, pos: s, tangent: None, vertex: *v, l2c: pm });
}
// Pen rubber band from the last vertex to the pointer.
if pen == Some((l.id, uid))
&& let (Some(last), Some(hp)) = (sp.vertices.last(), hover)
{
painter.line_segment([scr(*last), hp], Stroke::new(1.0, mc.gamma_multiply(0.6)));
}
paths.push(PathInfo { layer: l.id, uid, m: pm, sp, is_mask });
}
}
// ---------------------------------------------------------------- motion paths
/// A keyframe dot or spatial tangent handle on a motion path.
#[derive(Clone, Copy)]
pub(crate) struct KeyHit {
pub layer: LayerId,
pub prop: u64,
/// Layer time of the key.
pub time: Tick,
pub pos: Pos2,
/// None = the key, Some(out?) = a tangent handle.
pub tangent: Option<bool>,
pub value: [f64; 3],
/// Parent space → comp.
pub p2c: Mat3,
}
/// Parent space → comp matrix of a 2D layer at the context time.
pub(crate) fn parent_to_comp(ectx: &EvalCtx, l: &Layer) -> Mat3 {
match l.parent.and_then(|p| ectx.comp.layer(p)) {
Some(p) => {
let w = ectx.world_matrix(p).0;
Mat3([[w[0][0], w[0][1], w[0][3]], [w[1][0], w[1][1], w[1][3]], [0.0, 0.0, 1.0]])
}
None => Mat3::IDENTITY,
}
}
/// Draw the position motion path of a selected 2D layer: frame dots along the path, keyframe
/// squares (filled when selected) and spatial Bezier tangent handles. Works through parents.
pub(crate) fn motion_path(app: &mut EffectcraftApp, painter: &egui::Painter, map: &ViewerMap, ectx: &EvalCtx, l: &Layer, col: Color32, hits: &mut Vec<KeyHit>) {
let Some(pos) = l.transform().and_then(|tr| tr.get("position")) else { return };
if pos.keys.len() < 2 || l.is_3d() {
return;
}
let p2c = parent_to_comp(ectx, l);
let to_scr = |v: [f64; 3]| {
let c = p2c.apply(gv2(v[0], v[1]));
map.to_screen([c.x, c.y])
};
let (lt0, lt1) = (pos.keys[0].time, pos.keys[pos.keys.len() - 1].time);
let fd = ectx.comp.frame_duration();
let mut pts = vec![];
let mut lt = lt0;
while lt <= lt1 && pts.len() < 20_000 {
pts.push(to_scr(pos.value_at(lt).as_vec3()));
lt += fd;
}
pts.push(to_scr(pos.value_at(lt1).as_vec3()));
let faint = col.gamma_multiply(0.8);
painter.add(egui::Shape::line(pts.clone(), Stroke::new(1.0, faint)));
for q in &pts {
painter.circle_filled(*q, 1.2, faint);
}
let sel = &app.session.state.selected_keys;
for (i, k) in pos.keys.iter().enumerate() {
let v = k.value.as_vec3();
let s = to_scr(v);
let (tin, tout) = effectcraft_engine::keyframe::spatial_tangents(&pos.keys, i);
for (out, tg) in [(false, tin), (true, tout)] {
let has = (out && i + 1 < pos.keys.len()) || (!out && i > 0);
if !has || (tg[0] == 0.0 && tg[1] == 0.0) {
continue;
}
let h = to_scr([v[0] + tg[0], v[1] + tg[1], v[2] + tg[2]]);
painter.line_segment([s, h], Stroke::new(1.0, col));
painter.circle_filled(h, 3.0, col);
let side = if out { "out" } else { "in" };
app.auto.add(&format!("viewer.motionPath.{}.{i}.{side}", l.id.0), Rect::from_center_size(h, vec2(8.0, 8.0)), "Spatial tangent");
hits.push(KeyHit { layer: l.id, prop: pos.uid, time: k.time, pos: h, tangent: Some(out), value: v, p2c });
}
let r = Rect::from_center_size(s, vec2(7.0, 7.0));
if sel.contains(&KeyRef { layer: l.id, prop: pos.uid, time: k.time }) {
painter.rect_filled(r, 0.0, col);
} else {
painter.rect_filled(r, 0.0, Color32::from_black_alpha(140));
painter.rect_stroke(r, 0.0, Stroke::new(1.0, col), StrokeKind::Inside);
}
app.auto.add(&format!("viewer.motionPath.{}.{i}", l.id.0), r.expand(2.0), "Position keyframe");
hits.push(KeyHit { layer: l.id, prop: pos.uid, time: k.time, pos: s, tangent: None, value: v, p2c });
}
}
// ---------------------------------------------------------------- free transform
fn ft_id() -> egui::Id {
egui::Id::new("viewer-free-transform")
}
/// Enter Free Transform on a path (double-click a path with the Selection tool).
pub(crate) fn begin_free_transform(ctx: &egui::Context, layer: LayerId, uid: u64) {
ctx.data_mut(|d| d.insert_temp(ft_id(), (layer, uid)));
}
pub(crate) fn end_free_transform(ctx: &egui::Context) {
ctx.data_mut(|d| d.remove::<(LayerId, u64)>(ft_id()));
}
pub(crate) fn free_transform_target(ctx: &egui::Context) -> Option<(LayerId, u64)> {
ctx.data(|d| d.get_temp(ft_id()))
}
/// The free-transform box: path-space bounds of the transformed vertices, its handles on screen
/// (corners 0–3, edge midpoints 4–7) and the path → comp matrix.
pub(crate) struct FtBox {
pub layer: LayerId,
pub uid: u64,
pub b: [f64; 4],
pub m: Mat3,
pub handles: [Pos2; 8],
pub quad: [Pos2; 4],
}
impl FtBox {
/// Path-space position of handle `i`.
pub fn handle_local(&self, i: usize) -> [f64; 2] {
let [x0, y0, x1, y1] = self.b;
let (cx, cy) = ((x0 + x1) / 2.0, (y0 + y1) / 2.0);
[[x0, y0], [x1, y0], [x1, y1], [x0, y1], [cx, y0], [x1, cy], [cx, y1], [x0, cy]][i]
}
pub fn center(&self) -> [f64; 2] {
[(self.b[0] + self.b[2]) / 2.0, (self.b[1] + self.b[3]) / 2.0]
}
}
/// Draw the free-transform box of the target path (if any) and return it for hit tests.
pub(crate) fn draw_free_transform(
app: &mut EffectcraftApp,
ctx: &egui::Context,
painter: &egui::Painter,
map: &ViewerMap,
paths: &[PathInfo],
) -> Option<FtBox> {
let (layer, uid) = free_transform_target(ctx)?;
let Some(p) = paths.iter().find(|p| p.layer == layer && p.uid == uid) else {
end_free_transform(ctx);
return None;
};
let sel: Vec<usize> = app.session.state.selected_vertices.iter().filter(|v| v.layer == layer && v.mask == uid).map(|v| v.index).collect();
let idx: Vec<usize> = if sel.len() >= 2 { sel } else { (0..p.sp.vertices.len()).collect() };
let (mut lo, mut hi) = ([f64::MAX; 2], [f64::MIN; 2]);
for &i in &idx {
let Some(v) = p.sp.vertices.get(i) else { continue };
lo = [lo[0].min(v[0]), lo[1].min(v[1])];
hi = [hi[0].max(v[0]), hi[1].max(v[1])];
}
if lo[0] > hi[0] {
return None;
}
let b = [lo[0], lo[1], hi[0].max(lo[0] + 1e-3), hi[1].max(lo[1] + 1e-3)];
let mut f = FtBox { layer, uid, b, m: p.m, handles: [Pos2::ZERO; 8], quad: [Pos2::ZERO; 4] };
for i in 0..8 {
let q = f.handle_local(i);
let c = p.m.apply(gv2(q[0], q[1]));
f.handles[i] = map.to_screen([c.x, c.y]);
}
f.quad = [f.handles[0], f.handles[1], f.handles[2], f.handles[3]];
let col = app.tokens.accent;
painter.add(egui::Shape::closed_line(f.quad.to_vec(), Stroke::new(1.0, col)));
for (i, h) in f.handles.iter().enumerate() {
let r = Rect::from_center_size(*h, vec2(7.0, 7.0));
painter.rect_filled(r, 0.0, Color32::WHITE);
painter.rect_stroke(r, 0.0, Stroke::new(1.0, col), StrokeKind::Outside);
app.auto.add(&format!("viewer.freeTransform.handle.{i}"), r, "Free transform handle");
}
let c = f.center();
let cc = p.m.apply(gv2(c[0], c[1]));
let cs = map.to_screen([cc.x, cc.y]);
painter.circle_stroke(cs, 4.0, Stroke::new(1.0, col));
Some(f)
}
fn point_in_quad(p: Pos2, q: &[Pos2; 4]) -> bool {
let mut sign = 0.0f32;
for i in 0..4 {
let (a, b) = (q[i], q[(i + 1) % 4]);
let c = (b.x - a.x) * (p.y - a.y) - (b.y - a.y) * (p.x - a.x);
if c.abs() < 1e-6 {
continue;
}
if sign == 0.0 {
sign = c.signum();
} else if c.signum() != sign {
return false;
}
}
true
}
// ---------------------------------------------------------------- drags
/// Overlay drags owned by this module.
#[derive(Clone, Debug)]
pub(crate) enum Drag {
/// A motion-path keyframe: its value moves with the pointer (parent space).
Key { layer: LayerId, prop: u64, time: Tick, start: [f64; 3], press: [f64; 2], inv: Mat3 },
/// A spatial tangent handle of a motion-path key.
KeyTangent { layer: LayerId, prop: u64, time: Tick, out: bool, key: [f64; 3], inv: Mat3 },
/// Free transform: 0 move, 1 scale (anchor, axes), 2 rotate about `anchor`.
Ft { layer: LayerId, path: u64, mode: u8, anchor: [f64; 2], last: [f64; 2], inv: Mat3, axes: [bool; 2] },
/// Mask Feather tool: drag away from the path to feather the mask.
Feather { layer: LayerId, prop: u64, start: [f64; 2], press: Pos2 },
/// A guide dragged in the viewer.
Guide { index: usize, vertical: bool },
/// Drawing the region of interest.
Roi { start: [f64; 2] },
}
/// Start a motion-path drag at `press` (keys before tangents), selecting the key.
pub(crate) fn begin_key_drag(app: &mut EffectcraftApp, hits: &[KeyHit], press: Pos2, cpt: [f64; 2], shift: bool) -> Option<Drag> {
let h = hits
.iter()
.rev()
.filter(|h| h.tangent.is_some())
.find(|h| h.pos.distance(press) < 6.0)
.or_else(|| hits.iter().rev().find(|h| h.tangent.is_none() && h.pos.distance(press) < 6.0))?;
let inv = h.p2c.inverse().unwrap_or(Mat3::IDENTITY);
match h.tangent {
Some(out) => Some(Drag::KeyTangent { layer: h.layer, prop: h.prop, time: h.time, out, key: h.value, inv }),
None => {
let k = KeyRef { layer: h.layer, prop: h.prop, time: h.time };
if !app.session.state.selected_keys.contains(&k) || shift {
let _ = app.session.execute("keys.select", json!({"keys": [{"layer": h.layer.0, "prop": h.prop, "time": h.time.seconds()}], "add": shift}));
}
Some(Drag::Key { layer: h.layer, prop: h.prop, time: h.time, start: h.value, press: cpt, inv })
}
}
}
/// Start a free-transform drag on the box: a handle scales about the opposite one (corners both
/// axes, edges one), inside moves, just outside a corner rotates about the centre.
pub(crate) fn begin_ft_drag(f: &FtBox, press: Pos2, map: &ViewerMap) -> Option<Drag> {
let inv = f.m.inverse()?;
let c = map.to_comp(press);
let lp = inv.apply(gv2(c[0], c[1]));
let lp = [lp.x, lp.y];
if let Some(i) = (0..8).find(|i| f.handles[*i].distance(press) < 7.0) {
let opp = [2, 3, 0, 1, 6, 7, 4, 5][i];
let axes = match i {
4 | 6 => [false, true],
5 | 7 => [true, false],
_ => [true, true],
};
return Some(Drag::Ft { layer: f.layer, path: f.uid, mode: 1, anchor: f.handle_local(opp), last: lp, inv, axes });
}
if point_in_quad(press, &f.quad) {
return Some(Drag::Ft { layer: f.layer, path: f.uid, mode: 0, anchor: f.center(), last: lp, inv, axes: [true, true] });
}
if f.quad.iter().any(|q| q.distance(press) < 24.0) {
return Some(Drag::Ft { layer: f.layer, path: f.uid, mode: 2, anchor: f.center(), last: lp, inv, axes: [true, true] });
}
None
}
/// Mask Feather tool: start on a mask path.
pub(crate) fn begin_feather(app: &EffectcraftApp, ectx: &EvalCtx, paths: &[PathInfo], map: &ViewerMap, press: Pos2) -> Option<Drag> {
let (layer, uid, _, _) = segment_at(&paths.iter().filter(|p| p.is_mask).cloned().collect::<Vec<_>>(), map, press, 10.0)?;
let l = ectx.comp.layer(layer)?;
let pr = l.masks()?.find_group(uid)?.get("feather")?;
let _ = app;
Some(Drag::Feather { layer, prop: pr.uid, start: ectx.value(l, pr).as_vec2(), press })
}
/// Advance an overlay drag to the pointer (`cpt` comp, `pos` screen). `snap` corrects a comp
/// point for snapping. Returns the replacement drag state.
#[allow(clippy::too_many_arguments)]
pub(crate) fn update(
app: &mut EffectcraftApp,
d: &Drag,
cpt: [f64; 2],
pos: Pos2,
mods: egui::Modifiers,
merge: &str,
zoom: f32,
snap: &mut dyn FnMut(&EffectcraftApp, [f64; 2], LayerId) -> [f64; 2],
) -> Option<Drag> {
match d.clone() {
Drag::Key { layer, prop, time, start, press, inv } => {
// Snap the key's on-screen position like a layer drag.
let start_c = inv.inverse().map(|m| m.apply(gv2(start[0], start[1]))).map(|v| [v.x, v.y]).unwrap_or([start[0], start[1]]);
let mut target = [start_c[0] + cpt[0] - press[0], start_c[1] + cpt[1] - press[1]];
let s = snap(app, target, layer);
target = [target[0] + s[0], target[1] + s[1]];
let p = inv.apply(gv2(target[0], target[1]));
let v = json!([p.x, p.y, start[2]]);
let r = app.session.execute("keys.set", json!({"layer": layer.0, "prop": prop, "time": time.seconds(), "value": v, "merge": merge}));
if let Err(e) = r {
app.ui.status = e.to_string();
}
None
}
Drag::KeyTangent { layer, prop, time, out, key, inv } => {
let p = inv.apply(gv2(cpt[0], cpt[1]));
let tg = json!([p.x - key[0], p.y - key[1], 0.0]);
let mut q = json!({"layer": layer.0, "prop": prop, "time": time.seconds(), "break": mods.alt, "merge": merge});
q[if out { "out" } else { "in" }] = tg;
let _ = app.session.execute("keys.setSpatialTangents", q);
None
}
Drag::Ft { layer, path, mode, anchor, last, inv, axes } => {
let p = inv.apply(gv2(cpt[0], cpt[1]));
let cur = [p.x, p.y];
let mut q = json!({"layer": layer.0, "mask": path, "anchor": anchor, "merge": merge});
match mode {
0 => q["offset"] = json!([cur[0] - last[0], cur[1] - last[1]]),
1 => {
let f = |k: usize| {
let a = last[k] - anchor[k];
if axes[k] && a.abs() > 1e-6 { ((cur[k] - anchor[k]) / a).clamp(-50.0, 50.0) } else { 1.0 }
};
let (mut fx, mut fy) = (f(0), f(1));
if mods.shift && axes[0] && axes[1] {
let k = (fx + fy) / 2.0;
(fx, fy) = (k, k);
}
if fx.abs() < 1e-3 || fy.abs() < 1e-3 {
return None;
}
q["scale"] = json!([fx * 100.0, fy * 100.0]);
}
_ => {
let a0 = (last[1] - anchor[1]).atan2(last[0] - anchor[0]);
let a1 = (cur[1] - anchor[1]).atan2(cur[0] - anchor[0]);
q["rotation"] = json!((a1 - a0).to_degrees());
}
}
if cur != last {
let _ = app.session.execute("path.freeTransform", q);
}
Some(Drag::Ft { layer, path, mode, anchor, last: cur, inv, axes })
}
Drag::Feather { layer, prop, start, press } => {
let amt = (pos.distance(press) / zoom.max(1e-3)) as f64;
let v = json!([(start[0] + amt).max(0.0), (start[1] + amt).max(0.0)]);
let _ = app.session.execute("prop.set", json!({"layer": layer.0, "prop": prop, "value": v, "merge": merge}));
None
}
Drag::Guide { index, .. } => {
let _ = index;
None
}
Drag::Roi { .. } => None,
}
}