FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust
Timeline: a transition stays within its clips after a regular trim
dotCooCoof1d0c2ddf412
diff --git a/crates/edit/src/lib.rs b/crates/edit/src/lib.rsindex 2f2588d6c..c4bb898d5 100644--- a/crates/edit/src/lib.rs+++ b/crates/edit/src/lib.rs@@ -234,6 +234,34 @@ fn fades_follow_edges(track: &mut Track, clip: ClipId) {}}+/// Keep each transition between `clip` and its neighbor within the two clips after a regular trim+/// or a non-ripple speed change made `clip` shorter: the transition is shortened when the clips are+/// now shorter than it, moved back inside them, and its stored alignment follows where that leaves it+/// on the cut (as `transitions::set_span` stores it: Custom Start has no variant).+fn transitions_stay_within_clips(track: &mut Track, clip: ClipId, frame: Tick) {+ let spans: Vec<Option<(Tick, Tick, Tick)>> = track+ .transitions+ .iter()+ .map(|tr| match (tr.from.and_then(|c| track.item(c)), tr.to.and_then(|c| track.item(c))) {+ (Some(f), Some(t)) if (f.id == clip || t.id == clip) && f.start < t.end() => Some((f.start, t.start, t.end())),+ _ => None,+ })+ .collect();+ for (tr, span) in track.transitions.iter_mut().zip(spans) {+ let Some((lo, cut, hi)) = span else { continue };+ if tr.start >= lo && tr.end() <= hi {+ continue;+ }+ tr.duration = tr.duration.min(hi - lo);+ tr.start = tr.start.clamp(lo, hi - tr.duration);+ tr.align = match transitions::alignment(tr, cut, frame) {+ transitions::Alignment::Start => filmcraft_project::TransitionAlign::StartAtCut,+ transitions::Alignment::End => filmcraft_project::TransitionAlign::EndAtCut,+ transitions::Alignment::Center | transitions::Alignment::Custom => filmcraft_project::TransitionAlign::CenterAtCut,+ };+ }+}+fn place(track: &mut Track, item: TrackItem) {let idx = track.items.partition_point(|i| i.start <= item.start);track.items.insert(idx, item);@@ -765,8 +793,12 @@ pub fn trim(seq: &mut Sequence, clip: ClipId, edge: Edge, mode: TrimMode, delta:}}transitions_follow_cuts(seq, &mut work);- } else if let Ok(track) = track_mut(&mut work, tid) {- fades_follow_edges(track, clip);+ } else {+ let frame = work.settings.frame_rate.frame_duration();+ if let Ok(track) = track_mut(&mut work, tid) {+ fades_follow_edges(track, clip);+ transitions_stay_within_clips(track, clip, frame);+ }}work.check().map_err(EditError::Other)?;*seq = work;@@ -1130,7 +1162,12 @@ pub fn set_speed_group(seq: &mut Sequence, clips: &[ClipId], speed: f64, reverseit.speed = speed;it.reverse = reverse;it.duration = new_dur;- fades_follow_edges(work.track_mut(tid).ok_or(EditError::NoTrack(tid))?, *clip);+ let frame = work.settings.frame_rate.frame_duration();+ let track = work.track_mut(tid).ok_or(EditError::NoTrack(tid))?;+ fades_follow_edges(track, *clip);+ if !ripple {+ transitions_stay_within_clips(track, *clip, frame);+ }origins.push((tid, old_end, delta));}let main_from = origins.first().map(|o| o.1).unwrap_or_default();diff --git a/crates/edit/src/tests.rs b/crates/edit/src/tests.rsindex 9b42c106a..9a99193ed 100644--- a/crates/edit/src/tests.rs+++ b/crates/edit/src/tests.rs@@ -527,6 +527,42 @@ fn fades_follow_a_non_ripple_speed_change() {fx.seq.check().unwrap();}+/// A regular trim or a non-ripple speed change that shortens one of the two clips of a transition+/// keeps the transition within them, and its stored alignment follows where it now sits on the cut.+#[test]+fn a_transition_stays_within_its_clips_after_a_regular_trim_or_speed_change() {+ for (case, expected) in [("Out trim of the second clip", 7), ("In trim of the first clip", 9), ("speed change of the second clip", 7)] {+ let mut fx = Fx::new();+ let v1 = fx.v(0);+ let a = fx.put(v1, 0, 10, 10);+ let b = fx.put(v1, 10, 10, 10);+ fx.seq.track_mut(v1).unwrap().transitions = vec![Transition {+ id: TransitionId(1),+ effect: filmcraft_project::find_effect("cross_dissolve").unwrap().instance(),+ start: f(8),+ duration: f(4),+ from: Some(a),+ to: Some(b),+ align: filmcraft_project::TransitionAlign::EndAtCut,+ reverse: false,+ }];+ let mut n = fx.next;+ match case {+ "Out trim of the second clip" => {+ trim(&mut fx.seq, b, Edge::Out, TrimMode::Regular, -f(9), &mut Fx::ctx(&mut n)).unwrap();+ }+ "In trim of the first clip" => {+ trim(&mut fx.seq, a, Edge::In, TrimMode::Regular, f(9), &mut Fx::ctx(&mut n)).unwrap();+ }+ _ => set_speed(&mut fx.seq, b, 10.0, false, false, &mut Fx::ctx(&mut n)).unwrap(),+ }+ let tr = &fx.seq.track(v1).unwrap().transitions[0];+ assert_eq!((tr.start, tr.duration), (f(expected), f(4)), "{case}");+ assert_eq!(tr.align, filmcraft_project::TransitionAlign::CenterAtCut, "{case}");+ fx.seq.check().unwrap();+ }+}+#[test]fn transitions_travel_with_moved_clips() {// V1: a [0,10) fading in, b [10,20), c [20,30); b→c crossfade, c fading out