Skip to content

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

Timeline: timeline.move reports the clips an overwrite move covers

Peet Guerciodfaf31bfec74
diff --git a/crates/engine/src/commands.rs b/crates/engine/src/commands.rs
index 015754659..f7684b409 100644
--- a/crates/engine/src/commands.rs
+++ b/crates/engine/src/commands.rs
@@ -250,6 +250,9 @@ pub(crate) fn item_p(p: &Value, k: &str) -> Option<ItemId> {
/// the same time offset on their own tracks, so picture and sound stay in sync; a partner on a
/// locked track, or one whose position would not change, stays as it is. Nothing moves before the sequence start: when a clip would,
/// every clip of the call lands later by the same amount, so the spacing asked for is kept.
+/// `overwritten` lists every clip the move covered without being asked to move it: its length
+/// before and after (ticks) and the clips what is left of it now lives on as (none = removed; a
+/// clip a moved clip lands inside is split in two).
fn move_clips(s: &mut Session, p: &Value) -> Result<Value> {
const CMD: &str = "timeline.move";
let mut listed: Vec<(ClipId, TrackId, Tick)> = Vec::new();
@@ -292,8 +295,31 @@ fn move_clips(s: &mut Session, p: &Value) -> Result<Value> {
moves.push((clip, track, start));
}
let ins = bool_p(p, "insert").unwrap_or(false);
+ // every clip on the sequence now: id -> (track, start, end, project item)
+ let spots = |s: &Session| -> Vec<(u64, TrackId, Tick, Tick, ItemId)> {
+ let q = s.active_sequence();
+ q.into_iter().flat_map(|q| q.all_tracks()).flat_map(|t| t.items.iter().map(move |i| (i.id.0, t.id, i.start, i.end(), i.item))).collect()
+ };
+ let before = spots(s);
s.edit_sequence(if ins { "Move (Insert)" } else { "Move" }, |q, ctx, _| Ok(edit::move_items(q, &moves, ins, ctx)?))?;
- Ok(json!({"moved": moves.iter().map(|m| m.0.0).collect::<Vec<_>>()}))
+ // An overwrite shortens, splits or removes whatever already sits where a clip lands, and a
+ // shortened clip may live on under a new id: name each clip the move changed without being
+ // asked to, with what is left of it, so the caller hears about it instead of finding out later.
+ let after = spots(s);
+ let moved = |c: u64| moves.iter().any(|m| m.0.0 == c);
+ let new = |c: u64| !before.iter().any(|b| b.0 == c);
+ let mut overwritten = Vec::new();
+ for &(c, track, start, end, item) in before.iter().filter(|b| !moved(b.0)) {
+ let kept = after.iter().find(|a| a.0 == c);
+ if ins || kept.is_some_and(|k| (k.2, k.3) == (start, end)) {
+ continue; // untouched (an insert only shifts clips, it never covers one)
+ }
+ let pieces: Vec<_> =
+ after.iter().filter(|a| a.0 == c || (new(a.0) && !moved(a.0) && a.1 == track && a.4 == item && a.2 >= start && a.3 <= end)).collect();
+ let now: i64 = pieces.iter().map(|a| (a.3 - a.2).0).sum();
+ overwritten.push(json!({"clip": c, "was": (end - start).0, "now": now, "pieces": pieces.iter().map(|a| a.0).collect::<Vec<_>>()}));
+ }
+ Ok(json!({"moved": moves.iter().map(|m| m.0.0).collect::<Vec<_>>(), "overwritten": overwritten}))
}
/// Expand a clip selection with linked partners (when linked selection is on).
diff --git a/crates/engine/src/timeline_move_tests.rs b/crates/engine/src/timeline_move_tests.rs
index e9f7f4bb0..e80c2f972 100644
--- a/crates/engine/src/timeline_move_tests.rs
+++ b/crates/engine/src/timeline_move_tests.rs
@@ -202,3 +202,48 @@ fn hostile_times_are_refused_or_clamped_never_a_panic() {
assert!(s.execute("timeline.move", json!({"moves": [{"clip": 987_654_321u64, "track": v_track.0, "time": 0}]})).is_err(), "a clip that does not exist");
assert_eq!(*s.project, *before);
}
+
+/// Moving one narration line onto a spot another line still held shortened that line and said
+/// nothing; the result now names every clip the move changed without being asked to.
+#[test]
+fn a_move_that_lands_on_another_clip_reports_what_it_cut() {
+ let mut s = demo();
+ let (_, _, a_track, a1) = pair_in_an_empty_sequence(&mut s, 0.0);
+ let later = a1.end().0 as f64 / filmcraft_time::TICKS_PER_SECOND as f64 + 2.0;
+ let item = a1.item.0;
+ s.execute("timeline.place", json!({"item": item, "seconds": later})).unwrap();
+ let a2 = s.active_sequence().unwrap().track(a_track).unwrap().items[1].clone();
+ let line = |c: ClipId, t: Tick| json!({"moves": [{"clip": c.0, "track": a_track.0, "time": t.0}], "linked": false});
+ // onto empty space: nothing else changes, nothing is reported
+ let r = s.execute("timeline.move", line(a1.id, a2.end() + Tick(1000))).unwrap();
+ assert_eq!(r["overwritten"], json!([]));
+ s.execute("edit.undo", json!({})).unwrap();
+ // over the head of the next line: that line is shortened (living on under a new id), and the result says so
+ let half = Tick(a1.duration.0 / 2);
+ let r = s.execute("timeline.move", line(a1.id, a2.start - half)).unwrap();
+ let rest = s.active_sequence().unwrap().track(a_track).unwrap().items.last().unwrap().clone();
+ assert_eq!((rest.end(), rest.duration), (a2.end(), a2.duration - half), "the overwrite itself is unchanged");
+ assert_eq!(r["overwritten"], json!([{"clip": a2.id.0, "was": a2.duration.0, "now": rest.duration.0, "pieces": [rest.id.0]}]));
+ s.execute("edit.undo", json!({})).unwrap();
+ // over its tail: shortened in place, same id
+ let r = s.execute("timeline.move", line(a2.id, a1.end() - half)).unwrap();
+ assert_eq!(r["overwritten"], json!([{"clip": a1.id.0, "was": a1.duration.0, "now": (a1.duration - half).0, "pieces": [a1.id.0]}]));
+ s.execute("edit.undo", json!({})).unwrap();
+ // squarely onto it: the line under it is gone, and the result names it
+ let r = s.execute("timeline.move", line(a1.id, a2.start)).unwrap();
+ assert!(s.active_sequence().unwrap().find_item(a2.id).is_none());
+ assert_eq!(r["overwritten"], json!([{"clip": a2.id.0, "was": a2.duration.0, "now": 0, "pieces": []}]));
+ s.execute("edit.undo", json!({})).unwrap();
+ // inside a longer line: it is split in two around the moved one
+ s.execute("timeline.trim", json!({"clip": a1.id.0, "edge": "out", "delta": -(a1.duration.0 / 2)})).unwrap();
+ let short = s.active_sequence().unwrap().find_item(a1.id).unwrap().1.duration;
+ let r = s.execute("timeline.move", line(a1.id, a2.start + Tick(a2.duration.0 / 4))).unwrap();
+ let o = &r["overwritten"][0];
+ assert_eq!(r["overwritten"].as_array().unwrap().len(), 1, "{r}");
+ assert_eq!((o["clip"].as_u64(), o["now"].as_i64()), (Some(a2.id.0), Some((a2.duration - short).0)), "{r}");
+ assert_eq!(o["pieces"].as_array().unwrap().len(), 2, "{r}");
+ // an insert-mode move shifts clips along and covers nothing
+ s.execute("edit.undo", json!({})).unwrap();
+ let r = s.execute("timeline.move", json!({"moves": [{"clip": a1.id.0, "track": a_track.0, "time": a2.start.0}], "insert": true, "linked": false})).unwrap();
+ assert_eq!(r["overwritten"], json!([]));
+}