Lock editing on tween in-between frames

Block geometry/clip edits on frames that fall strictly inside a tween span,
where an edit would silently mutate the bracketing keyframe instead:

- Shape tweens: gate vector editing (vertices, curves, DCEL hits) and all
  geometry tools in stage.rs behind VectorLayer::is_tween_inbetween.
- Motion tweens: block selecting/dragging/transforming clip instances whose
  transform is mid-tween, via AnimationData::is_object_tweened_at.

Also: inserting a keyframe mid-tween now captures the interpolated geometry
shown at that frame (not the left keyframe's) and inherits the shape tween,
so the new keyframe continues morphing toward the right keyframe.
This commit is contained in:
Skyler Lehmkuhl
2026-06-21 16:01:47 -04:00
parent a1acecf396
commit 5fd04b5dd5
3 changed files with 215 additions and 26 deletions
@@ -322,6 +322,20 @@ impl AnimationCurve {
}
}
/// True when `time` lies strictly between two keyframes — an in-between frame of a
/// tween (not on a keyframe, not in the pre/post-extrapolation tails).
pub fn is_tween_inbetween(&self, time: f64, tol: f64) -> bool {
if self.keyframes.len() < 2 {
return false;
}
let first = self.keyframes.first().unwrap().time;
let last = self.keyframes.last().unwrap().time;
if time <= first + tol || time >= last - tol {
return false;
}
!self.keyframes.iter().any(|kf| (kf.time - time).abs() <= tol)
}
/// Extrapolate before the first keyframe
fn extrapolate_pre(&self, time: f64, first_kf: &Keyframe) -> f64 {
match self.pre_extrapolation {
@@ -516,6 +530,17 @@ impl AnimationData {
self.curves.remove(target)
}
/// True when the object (e.g. a clip instance) is mid motion-tween at `time` — any of
/// its curves has `time` strictly between two keyframes. Used to lock out editing on
/// in-between frames (editing there would silently insert a keyframe and disturb the tween).
pub fn is_object_tweened_at(&self, id: uuid::Uuid, time: f64) -> bool {
const TOL: f64 = 0.001;
self.curves.iter().any(|(target, curve)| {
matches!(target, AnimationTarget::Object { id: oid, .. } if *oid == id)
&& curve.is_tween_inbetween(time, TOL)
})
}
/// Evaluate a property at a given time
pub fn eval(&self, target: &AnimationTarget, time: f64, default: f64) -> f64 {
self.curves
@@ -545,3 +570,51 @@ impl AnimationData {
(t, opacity)
}
}
#[cfg(test)]
mod tween_lock_tests {
use super::*;
#[test]
fn curve_in_between_detection() {
let mut c = AnimationCurve::new(
AnimationTarget::Object { id: uuid::Uuid::nil(), property: TransformProperty::X },
0.0,
);
c.set_keyframe(Keyframe::linear(0.0, 0.0));
c.set_keyframe(Keyframe::linear(10.0, 100.0));
assert!(c.is_tween_inbetween(5.0, 0.001), "strictly between keyframes");
assert!(!c.is_tween_inbetween(0.0, 0.001), "on a keyframe");
assert!(!c.is_tween_inbetween(10.0, 0.001), "on a keyframe");
assert!(!c.is_tween_inbetween(15.0, 0.001), "past the last keyframe (extrapolation tail)");
}
#[test]
fn single_keyframe_is_never_in_between() {
let mut c = AnimationCurve::new(
AnimationTarget::Object { id: uuid::Uuid::nil(), property: TransformProperty::X },
0.0,
);
c.set_keyframe(Keyframe::linear(10.0, 100.0));
assert!(!c.is_tween_inbetween(5.0, 0.001));
assert!(!c.is_tween_inbetween(20.0, 0.001));
}
#[test]
fn object_tweened_when_any_curve_is_in_between() {
let id = uuid::Uuid::new_v4();
let mut data = AnimationData::new();
let mut cx = AnimationCurve::new(
AnimationTarget::Object { id, property: TransformProperty::X },
0.0,
);
cx.set_keyframe(Keyframe::linear(0.0, 0.0));
cx.set_keyframe(Keyframe::linear(10.0, 100.0));
data.set_curve(cx);
assert!(data.is_object_tweened_at(id, 5.0));
assert!(!data.is_object_tweened_at(id, 0.0));
assert!(!data.is_object_tweened_at(uuid::Uuid::new_v4(), 5.0), "different object");
}
}
@@ -493,30 +493,53 @@ impl VectorLayer {
&mut self.keyframes[insert_idx]
}
/// Insert a new keyframe at time by cloning the DCEL from the active keyframe.
/// Insert a new keyframe at time, taking the geometry the layer shows there.
/// If a keyframe already exists at the exact time, does nothing and returns it.
///
/// Inside a shape-tween span this captures the *interpolated* geometry at `time` (not
/// the left keyframe's), and inherits the span's `tween_after` so the new keyframe keeps
/// tweening toward the next one — i.e. splitting a tween in two leaves the motion intact.
pub fn insert_keyframe_from_current(&mut self, time: f64) -> &mut ShapeKeyframe {
let tolerance = 0.001;
if let Some(idx) = self.keyframe_index_at_exact(time, tolerance) {
return &mut self.keyframes[idx];
}
// Clone graph and clip instance IDs from the active keyframe
let (cloned_graph, cloned_clip_ids) = self
// Geometry shown at `time` (interpolated if mid-tween, else the held keyframe).
let cloned_graph = self
.tweened_graph_at(time)
.map(|g| g.into_owned())
.unwrap_or_else(VectorGraph::new);
// Inherit tween + clip instances from the active (left) keyframe.
let (tween_after, cloned_clip_ids) = self
.keyframe_at(time)
.map(|kf| {
(kf.graph.clone(), kf.clip_instance_ids.clone())
})
.unwrap_or_else(|| (VectorGraph::new(), Vec::new()));
.map(|kf| (kf.tween_after, kf.clip_instance_ids.clone()))
.unwrap_or((TweenType::None, Vec::new()));
let insert_idx = self.keyframes.partition_point(|kf| kf.time < time);
let mut kf = ShapeKeyframe::new(time);
kf.graph = cloned_graph;
kf.tween_after = tween_after;
kf.clip_instance_ids = cloned_clip_ids;
self.keyframes.insert(insert_idx, kf);
&mut self.keyframes[insert_idx]
}
/// True when `time` falls strictly inside a shape-tween span — i.e. an in-between frame
/// (the left keyframe has `tween_after == Shape` and `time` is not on either keyframe).
/// Editing such a frame would silently modify the left keyframe, so the editor blocks it.
pub fn is_tween_inbetween(&self, time: f64) -> bool {
let tol = 0.001;
let idx = self.keyframes.partition_point(|kf| kf.time <= time);
if idx == 0 || idx >= self.keyframes.len() {
return false;
}
let (a, b) = (&self.keyframes[idx - 1], &self.keyframes[idx]);
a.tween_after == TweenType::Shape
&& (time - a.time).abs() > tol
&& (b.time - time).abs() > tol
}
/// Remove a keyframe at the exact time (within tolerance).
/// Returns the removed keyframe if found.
pub(crate) fn remove_keyframe_at(&mut self, time: f64, tolerance: f64) -> Option<ShapeKeyframe> {
@@ -1136,6 +1159,53 @@ mod tests {
assert!((g.vertices[0].position.x - 0.0).abs() < 1e-6);
}
#[test]
fn inserting_keyframe_mid_tween_captures_interpolated_geometry_and_inherits_tween() {
use crate::vector_graph::{Direction, FillRule, ShapeColor};
use kurbo::{CubicBez, Point};
let mk = |x: f64| {
let mut g = VectorGraph::new();
let v0 = g.alloc_vertex(Point::new(x, 0.0));
let v1 = g.alloc_vertex(Point::new(x + 10.0, 0.0));
let c = CubicBez::new(
Point::new(x, 0.0),
Point::new(x + 3.0, 0.0),
Point::new(x + 7.0, 0.0),
Point::new(x + 10.0, 0.0),
);
g.alloc_edge(c, v0, v1, None, Some(ShapeColor::rgb(0, 0, 0)));
g.alloc_fill(vec![(crate::vector_graph::EdgeId(0), Direction::Forward)],
ShapeColor::rgb(255, 0, 0), FillRule::NonZero);
g
};
let mut layer = VectorLayer::new("L");
layer.keyframes.clear();
let mut kf_a = ShapeKeyframe::new(0.0);
kf_a.graph = mk(0.0);
kf_a.tween_after = TweenType::Shape;
let mut kf_b = ShapeKeyframe::new(10.0);
kf_b.graph = mk(100.0);
layer.keyframes.push(kf_a);
layer.keyframes.push(kf_b);
// Insert keyframe C at the midpoint of the A→B shape tween.
layer.insert_keyframe_from_current(5.0);
let c = layer.keyframe_at(5.0).expect("keyframe C exists");
// C took the interpolated geometry (x=50), not A's geometry (x=0).
assert!((c.graph.vertices[0].position.x - 50.0).abs() < 1e-6,
"C should hold interpolated geometry, got {}", c.graph.vertices[0].position.x);
// C inherits the shape tween so it still morphs toward B.
assert_eq!(c.tween_after, TweenType::Shape, "C should inherit the shape tween");
// The frame between C and B is still an in-between (tween continues past C).
assert!(layer.is_tween_inbetween(7.0));
// C itself is a keyframe, not an in-between.
assert!(!layer.is_tween_inbetween(5.0));
}
#[test]
fn test_layer_creation() {
let layer = Layer::new(LayerType::Vector, "Test Layer");