Skip to content

FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust

Timeline: a Rolling Edit drag stops at the roll's limits

dotCooCood371ee3aca0f
diff --git a/crates/engine/src/commands.rs b/crates/engine/src/commands.rs
index 813185a12..a9c49940d 100644
--- a/crates/engine/src/commands.rs
+++ b/crates/engine/src/commands.rs
@@ -635,6 +635,32 @@ pub fn trim_delta(s: &Session, clip: ClipId, edge: Edge, mode: TrimMode, delta:
clamp_trim_linked(q, &with_links(s, &[clip]), edge, mode, delta, &ctx)
}
+/// The delta a roll of every cut in `pairs` can apply together: `delta` clamped by each pair's
+/// media and the frame left in each clip, each tried on a copy of `q`.
+fn clamp_roll_linked(q: &filmcraft_project::Sequence, pairs: &[(ClipId, ClipId)], delta: Tick, ctx: &mut edit::EditCtx) -> Result<Tick> {
+ let mut d = delta;
+ for &(pl, pr) in pairs {
+ let mut probe = q.clone();
+ let x = edit::roll(&mut probe, pl, pr, d, ctx)?;
+ if x.abs() < d.abs() {
+ d = x;
+ }
+ }
+ Ok(d)
+}
+
+/// The delta `timeline.roll` would apply for `delta`, without editing anything: the Timeline's
+/// Rolling Edit drag shows this, so the cut stops where the roll on release stops instead of
+/// following the pointer past the media.
+pub fn roll_delta(s: &Session, left: ClipId, right: ClipId, delta: Tick) -> Result<Tick> {
+ let q = s.active_sequence().ok_or(EngineError::NoSequence)?;
+ let durations = |id: ItemId| crate::media_duration(&s.project, &s.media, id);
+ let starts = |id: ItemId| crate::media_start(&s.project, id);
+ let mut next = s.project.next_id;
+ let mut ctx = edit::EditCtx { next_id: &mut next, media_duration: &durations, media_start: &starts, min_duration: q.settings.frame_rate.frame_duration() };
+ clamp_roll_linked(q, &linked_roll_pairs(q, left, right, s.state.linked_selection), delta, &mut ctx)
+}
+
/// Sequence settings matching a media clip (New Sequence From Clip, New Sequence from an item):
/// its frame size, rate and pixel aspect ratio (Interpret Footage's, else the file's), so a
/// 1440 x 1080 clip with 4:3 pixels makes a 1440 x 1080 sequence that displays 16:9.
@@ -2854,14 +2880,7 @@ fn build() -> Vec<CommandSpec> {
let (x, n, linked) = s.edit_sequence("Rolling Edit", |q, ctx, _| {
let pairs = linked_roll_pairs(q, l, r, follow);
let linked = pairs.len() > 1;
- let mut d = d;
- for &(pl, pr) in &pairs {
- let mut probe = q.clone();
- let x = edit::roll(&mut probe, pl, pr, d, ctx)?;
- if x.abs() < d.abs() {
- d = x;
- }
- }
+ let d = clamp_roll_linked(q, &pairs, d, ctx)?;
if d != Tick::ZERO {
for &(pl, pr) in &pairs {
edit::roll(q, pl, pr, d, ctx)?;
diff --git a/crates/engine/src/roll_linked_tests.rs b/crates/engine/src/roll_linked_tests.rs
index dd3f2e4b2..6e1edefeb 100644
--- a/crates/engine/src/roll_linked_tests.rs
+++ b/crates/engine/src/roll_linked_tests.rs
@@ -45,6 +45,31 @@ fn roll_moves_linked_audio_cut() {
assert_eq!(range(&s, a1), (f(60), f(96)));
}
+/// The delta the Timeline previews while a cut is rolled is the one `timeline.roll` applies on
+/// release: rolled far to the left, the cut stops where the right clip's media starts, 12 frames
+/// before its In point.
+#[test]
+fn roll_delta_stops_at_the_start_of_the_media() {
+ let mut s = Session::default();
+ s.execute("file.openDemoProject", json!({})).unwrap();
+ s.execute("file.newSequence", json!({"name": "Roll", "video": 1, "audio": 1})).unwrap();
+ let rate = s.sequence_rate();
+ let f = |n: i64| rate.tick_of(n);
+ let item = s.project.items.values().find(|i| i.has_video() && i.has_audio() && i.as_media().is_some()).map(|i| i.id).unwrap();
+ for (frame, source_in) in [(0, 48), (48, 12)] {
+ s.execute("timeline.place", json!({"item": item.0, "frame": frame, "sourceIn": f(source_in).0, "duration": f(48).0})).unwrap();
+ }
+ let q = s.active_sequence().unwrap();
+ let (left, right) = (q.video_tracks[0].items[0].id, q.video_tracks[0].items[1].id);
+ let far = Tick(-3600 * filmcraft_time::TICKS_PER_SECOND);
+ let shown = crate::commands::roll_delta(&s, left, right, far).unwrap();
+ assert_eq!(shown, -f(12), "clamped to the media before the right clip's In point");
+ assert_eq!(crate::commands::roll_delta(&s, left, right, -f(5)).unwrap(), -f(5), "a roll within the media is not clamped");
+ let r = s.execute("timeline.roll", json!({"left": left.0, "right": right.0, "delta": far.0})).unwrap();
+ assert_eq!(r["delta"], json!(shown.0), "the preview and the roll agree");
+ assert_eq!(range(&s, right), (f(36), f(96)));
+}
+
/// Linked selection off (or unlinked clips) rolls only the given pair and says so.
#[test]
fn roll_without_linked_selection_leaves_audio() {
diff --git a/crates/ui-egui/src/panels/timeline.rs b/crates/ui-egui/src/panels/timeline.rs
index 3d59a0c6c..1f206d735 100644
--- a/crates/ui-egui/src/panels/timeline.rs
+++ b/crates/ui-egui/src/panels/timeline.rs
@@ -2691,7 +2691,10 @@ fn interact(app: &mut FilmcraftApp, ui: &mut egui::Ui, seq: &Sequence, layout: &
}),
Drag::Roll { left, right, from, .. } => seq.find_item(left).map(|(_, l)| {
let target = snap(app, seq, layout, rate.snap_nearest(l.end() + (t_here - from)), &[left, right]);
- Drag::Roll { left, right, delta: target - l.end(), from }
+ // stop where the roll on release stops: the media, a frame left in each clip and linked
+ // cuts
+ let delta = filmcraft_engine::commands::roll_delta(&app.session, left, right, target - l.end()).unwrap_or(target - l.end());
+ Drag::Roll { left, right, delta, from }
}),
Drag::Slip { clip, .. } => {
let start = resp.interact_pointer_pos().map(|_| ()).and(ctx.input(|i| i.pointer.press_origin()));
diff --git a/crates/ui-egui/tests/it/timeline_trim_ui.rs b/crates/ui-egui/tests/it/timeline_trim_ui.rs
index d6243d548..bb9f4584f 100644
--- a/crates/ui-egui/tests/it/timeline_trim_ui.rs
+++ b/crates/ui-egui/tests/it/timeline_trim_ui.rs
@@ -365,6 +365,39 @@ fn an_edge_dragged_past_the_media_stops_at_its_end() {
assert_eq!(d.span(last).1, e0 + limit.0, "released where it was shown");
}
+/// With the Rolling Edit tool, a cut dragged far past the end of the right clip stops while the
+/// drag is still in progress, at the same place the roll lands on release.
+#[test]
+fn a_cut_rolled_past_its_limit_stops_there() {
+ use filmcraft_ui_egui::panels::timeline::Drag;
+ let mut d = Driver::demo();
+ let (city, dunes) = (d.v1_clip(CITY), d.v1_clip(DUNES));
+ d.ok("ui.menu.invoke", json!({"id": "tool.rolling"}));
+ let (cut, x1, y) = d.edges(dunes);
+ let (_, e0) = d.span(city);
+ let far = filmcraft_time::Tick(3600 * filmcraft_time::TICKS_PER_SECOND);
+ let limit = filmcraft_engine::commands::roll_delta(&d.app().session, ClipId(city), ClipId(dunes), far).expect("limit");
+ assert!(filmcraft_time::Tick::ZERO < limit && limit < far, "the roll has a limit: {limit:?}");
+ let (from, to) = (pos2(cut + 3.0, y), pos2(x1 + 100.0, y));
+ let push = |d: &mut Driver, e: egui::Event| d.harness.input_mut().events.push(e);
+ push(&mut d, egui::Event::PointerMoved(from));
+ d.frames(1);
+ push(&mut d, egui::Event::PointerButton { pos: from, button: egui::PointerButton::Primary, pressed: true, modifiers: Default::default() });
+ d.frames(1);
+ for i in 1..=60 {
+ push(&mut d, egui::Event::PointerMoved(from + (to - from) * (i as f32 / 60.0)));
+ d.frames(1);
+ }
+ match &d.app().tl.drag {
+ Some(Drag::Roll { delta, .. }) => assert_eq!(*delta, limit, "the rolled cut stops at the roll's limit"),
+ other => panic!("not rolling: {other:?}"),
+ }
+ push(&mut d, egui::Event::PointerButton { pos: to, button: egui::PointerButton::Primary, pressed: false, modifiers: Default::default() });
+ d.frames(2);
+ assert_eq!(d.span(city).1, e0 + limit.0, "released where it was shown");
+ assert_eq!(d.span(dunes).0, e0 + limit.0);
+}
+
/// Dragging a picture in the Trim Monitor must not deadlock: the shift bookkeeping used to call
/// `drag_delta()` inside `data_mut()`, and both take egui's non-reentrant context write-lock, so
/// the drag froze the whole app on its first recognised frame (the delta is read before locking