Skip to content

EffectCraft: An open-source motion graphics and compositing tool written by scratch in pure Rust

Merge pull request #950 from Themolx/trim-selected-layers

Brandon Thomas8e6c3e0e6869
diff --cc crates/engine/src/tests_timeline.rs
index 53ebd52cd,d6faa352b..009af9e52
--- a/crates/engine/src/tests_timeline.rs
+++ b/crates/engine/src/tests_timeline.rs
@@@ -986,94 -884,45 +986,137 @@@ fn property_edits_apply_to_every_select
assert_eq!(prop(&s, b, "transform/rotation").value.as_f64(), 0.0);
}
+/// #770: Proportional Scrubbing (After Effects 26.2, Ctrl+Alt / Cmd+Opt scrub). The change is
+/// spread over the selected layers by selection order (not stacking order): the first keeps its
+/// value, the last takes all of it, the one between half; whichever layer is scrubbed. One undo
+/// step; a keyframed property gets a key at the current time, the unchanged first layer none.
+#[test]
+fn proportional_scrub_spreads_the_change_by_selection_order() {
+ let (mut s, a) = setup();
+ let b = s.execute("layer.newSolid", json!({"color": "#ff0000", "width": 50, "height": 50})).unwrap()["layer"].as_u64().unwrap();
+ let c = s.execute("layer.newSolid", json!({"color": "#00ff00", "width": 50, "height": 50})).unwrap()["layer"].as_u64().unwrap();
+ s.execute("prop.set", json!({"layer": b, "path": "transform/rotation", "value": 20})).unwrap();
+ // Selection order c, a, b.
+ s.execute("layer.select", json!({"layers": [c]})).unwrap();
+ s.execute("layer.select", json!({"layers": [a], "add": true})).unwrap();
+ s.execute("layer.select", json!({"layers": [b], "toggle": true})).unwrap();
+ let rot = |s: &Session, l: u64| prop(s, l, "transform/rotation").value.as_f64();
+ let uid = prop(&s, a, "transform/rotation").uid;
+
+ // Scrub layer a (the middle one) by +10 a frame, three frames: the UI sends a's shown value
+ // plus the drag.
+ let undo = s.history.undo.len();
+ for _ in 0..3 {
+ let v = rot(&s, a) + 10.0;
+ s.execute("prop.set", json!({"layer": a, "prop": uid, "value": v, "selected": true, "offset": true, "proportional": true, "merge": "scrub-r"}))
+ .unwrap();
+ }
+ assert_eq!(rot(&s, c), 0.0, "the first selected layer keeps its value");
+ assert_eq!(rot(&s, a), 15.0, "the middle one moves by half");
+ assert_eq!(rot(&s, b), 50.0, "the last one moves by the whole change");
+ assert_eq!(s.history.undo.len(), undo + 1, "one undo step for the scrub");
+ s.execute("edit.undo", json!({})).unwrap();
+ assert_eq!((rot(&s, c), rot(&s, a), rot(&s, b)), (0.0, 0.0, 20.0));
+
+ // Vectors and keyframes: Position animated on every layer, scrubbed on the last layer at 1 s.
+ for l in [a, b, c] {
+ s.execute("prop.set", json!({"layer": l, "path": "transform/position", "value": [100, 100], "time": 0.0})).unwrap();
+ s.execute("prop.toggleAnimation", json!({"layer": l, "path": "transform/position"})).unwrap();
+ }
+ s.execute("time.set", json!({"time": 1.0})).unwrap();
+ let pos_uid = prop(&s, b, "transform/position").uid;
+ s.execute("prop.set", json!({"layer": b, "prop": pos_uid, "value": [140, 80], "selected": true, "offset": true, "proportional": true})).unwrap();
+ let at1 = |s: &Session, l: u64| {
+ let p = prop(s, l, "transform/position");
+ (p.keys.len(), p.value_at(effectcraft_time::Tick::from_seconds_f64(1.0)).components()[..2].to_vec())
+ };
+ assert_eq!(at1(&s, c), (1, vec![100.0, 100.0]), "no key on the first selected layer");
+ assert_eq!(at1(&s, a), (2, vec![120.0, 90.0]));
+ assert_eq!(at1(&s, b), (2, vec![140.0, 80.0]));
+
+ // Without `proportional` a scrub still moves them all by the same change.
+ s.execute("prop.set", json!({"layer": a, "prop": uid, "value": 5, "selected": true, "offset": true})).unwrap();
+ assert_eq!((rot(&s, c), rot(&s, a), rot(&s, b)), (5.0, 5.0, 25.0));
+}
+
+#[test]
+fn proportional_share_is_linear_in_selection_order() {
+ use crate::commands::prop::proportional_share;
+ assert_eq!((0..5).map(|i| proportional_share(i, 5)).collect::<Vec<_>>(), [0.0, 0.25, 0.5, 0.75, 1.0]);
+ // One layer (or a bad count) takes the whole change; an index past the end the last share.
+ assert_eq!(proportional_share(0, 1), 1.0);
+ assert_eq!(proportional_share(0, 0), 1.0);
+ assert_eq!(proportional_share(usize::MAX, 3), 1.0);
+}
+
+#[test]
+fn add_item_keeps_footage_longer_than_the_comp_at_full_length() {
+ let mut s = Session::default();
+ s.execute("comp.new", json!({"name": "T", "width": 400, "height": 300, "frameRate": 30, "duration": 4})).unwrap();
+ let f = effectcraft_project::Footage {
+ width: 100,
+ height: 100,
+ kind: effectcraft_project::FootageKind::Video,
+ has_video: true,
+ duration: effectcraft_time::Tick::from_seconds_f64(10.0),
+ ..Default::default()
+ };
+ let item = std::sync::Arc::make_mut(&mut s.project).add_item("long.mp4", effectcraft_color::Label::None, None, effectcraft_project::ItemKind::Footage(f));
+ let l = s.execute("layer.addItem", json!({"item": item.0})).unwrap()["layer"].as_u64().unwrap();
+ let layer = s.active_comp().unwrap().layer(effectcraft_project::LayerId(l)).unwrap();
+ assert_eq!(layer.out_point.seconds(), 10.0);
+}
+
+/// View > Go to Time's dialog sends its Frame field as a float; `time.set` used to reject it
+/// with "need `time`, `frame` or `timecode`".
+#[test]
+fn go_to_time_accepts_a_float_frame_from_the_dialog() {
+ let (mut s, _) = setup();
+ let r = s.execute("time.set", json!({"frame": 12.0})).unwrap();
+ assert_eq!(r["frame"], json!(12));
+ let r = s.execute("time.set", json!({"frame": 7})).unwrap();
+ assert_eq!(r["frame"], json!(7));
+}
++
+ /// Dragging a trim edge with several layers selected trims every selected layer by the same
+ /// amount from its own in/out point (as in AE), clamped to the comp and to one frame of layer;
+ /// a whole merged drag is one undo step.
+ #[test]
+ fn relative_trim_moves_every_selected_layer_by_the_same_delta() {
+ let (mut s, a) = setup();
+ let b = s.execute("layer.newSolid", json!({"color": "#ff0000", "width": 100, "height": 100})).unwrap()["layer"].as_u64().unwrap();
+ s.execute("layer.timing", json!({"layers": [a], "in": 0.5, "out": 2.0})).unwrap();
+ s.execute("layer.timing", json!({"layers": [b], "in": 1.0, "out": 3.5})).unwrap();
+ let io = |s: &Session, l: u64| {
+ let l = s.active_comp().unwrap().layer(effectcraft_project::LayerId(l)).unwrap();
+ (l.in_point.seconds(), l.out_point.seconds())
+ };
+ let near = |x: f64, y: f64| (x - y).abs() < 1e-9;
+ let undo_len = s.history.undo.len();
+ // In-point drag in frame steps, merged into one edit.
+ for _ in 0..3 {
+ s.execute("layer.timing", json!({"layers": [a, b], "inDelta": 0.1, "merge": "bar-trim"})).unwrap();
+ }
+ assert_eq!(s.history.undo.len(), undo_len + 1);
+ let ((ai, ao), (bi, bo)) = (io(&s, a), io(&s, b));
+ assert!(near(ai, 0.8) && near(ao, 2.0) && near(bi, 1.3) && near(bo, 3.5), "{ai} {ao} {bi} {bo}");
+ s.execute("edit.undo", json!({})).unwrap();
+ assert!(near(io(&s, a).0, 0.5) && near(io(&s, b).0, 1.0));
+ s.history.merge_key = None;
+ // Out-point drag: b hits the comp end (4 s) while a keeps going.
+ s.execute("layer.timing", json!({"layers": [a, b], "outDelta": 1.0})).unwrap();
+ assert!(near(io(&s, a).1, 3.0) && near(io(&s, b).1, 4.0), "{:?} {:?}", io(&s, a), io(&s, b));
+ // In-point drag left: a stops at the comp start.
+ s.execute("layer.timing", json!({"layers": [a, b], "inDelta": -0.8})).unwrap();
+ assert!(near(io(&s, a).0, 0.0) && near(io(&s, b).0, 0.2), "{:?} {:?}", io(&s, a), io(&s, b));
+ // Trimming past the other edge keeps one frame of layer.
+ s.execute("layer.timing", json!({"layers": [a, b], "inDelta": 10.0})).unwrap();
+ let fd = 1.0 / 30.0;
+ assert!(near(io(&s, a).0, 3.0 - fd) && near(io(&s, b).0, 4.0 - fd), "{:?} {:?}", io(&s, a), io(&s, b));
+ s.execute("layer.timing", json!({"layers": [a, b], "outDelta": -10.0})).unwrap();
+ assert!(near(io(&s, a).1, 3.0) && near(io(&s, b).1, 4.0), "{:?} {:?}", io(&s, a), io(&s, b));
+ // A point already outside the comp trims from where it is instead of jumping onto the edge.
+ s.execute("layer.timing", json!({"layers": [a], "in": -1.0})).unwrap();
+ s.execute("layer.timing", json!({"layers": [a], "inDelta": 0.1})).unwrap();
+ assert!(near(io(&s, a).0, -0.9), "{:?}", io(&s, a));
+ }