FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust
graphics: keep duration separate from timeline placement
storybored3793971b2666fd3
diff --git a/crates/engine/src/graphics.rs b/crates/engine/src/graphics.rsindex 2e19a349b..fea4922a9 100644--- a/crates/engine/src/graphics.rs+++ b/crates/engine/src/graphics.rs@@ -98,7 +98,12 @@ pub(crate) fn graphic_source(p: &mut filmcraft_project::Project, w: u32, h: u32,/// topmost clip at the playhead that is free for the duration (a track is added if needed).fn new_graphic_clip(s: &mut Session, layer: filmcraft_project::EffectInstance, name: &str, p: &Value) -> Result<ClipId> {let seconds = f64_p(p, "seconds").unwrap_or(5.0).max(0.01);- place_video_clip(s, name, p, "New Graphic", vec![layer], move |pr, (w, h, rate)| {+ // Here `seconds` is duration, not the generic time parser's placement alias.+ let mut placement = p.clone();+ if let Some(params) = placement.as_object_mut() {+ params.remove("seconds");+ }+ place_video_clip(s, name, &placement, "New Graphic", vec![layer], move |pr, (w, h, rate)| {let src = graphic_source(pr, w, h, rate);(src, rate.snap_nearest(Tick::from_seconds_f64(seconds)).max(rate.frame_duration()))})diff --git a/crates/engine/src/graphics_tests.rs b/crates/engine/src/graphics_tests.rsindex 803cb7b36..0c7cc4cb7 100644--- a/crates/engine/src/graphics_tests.rs+++ b/crates/engine/src/graphics_tests.rs@@ -24,6 +24,47 @@ fn text_of(l: &filmcraft_project::graphic::LayerSpec) -> String {}}+#[test]+fn graphic_duration_does_not_set_its_timeline_position() {+ for command in ["graphics.newText", "graphics.newShape"] {+ for playhead_frame in [0, 72] {+ for seconds in [1, 2] {+ let mut s = Session::default();+ s.execute("file.newSequence", json!({"name": "Placement", "fps": 24, "width": 128, "height": 128, "video": 1, "audio": 1})).unwrap();+ s.execute("playhead.set", json!({"frame": playhead_frame})).unwrap();+ let start = s.playhead();+ let r = s.execute(command, json!({"seconds": seconds})).unwrap();+ let clip = ClipId(r["clip"].as_u64().unwrap());+ let q = s.active_sequence().unwrap();+ let (_, it) = q.find_item(clip).unwrap();+ assert_eq!(it.start, start, "{command}, duration {seconds}, playhead {playhead_frame}");+ assert_eq!(it.duration, Tick::from_seconds_f64(f64::from(seconds)));+ s.execute("edit.undo", json!({})).unwrap();+ assert!(s.active_sequence().unwrap().find_item(clip).is_none());+ s.execute("edit.redo", json!({})).unwrap();+ assert_eq!(s.active_sequence().unwrap().find_item(clip).unwrap().1.start, start);+ }+ }+ }+}++#[test]+fn graphic_duration_keeps_explicit_time_frame_and_timecode_placement() {+ for command in ["graphics.newText", "graphics.newShape"] {+ for mut params in [json!({"time": 0}), json!({"frame": 0}), json!({"timecode": "00:00:00:00"})] {+ let mut s = Session::default();+ s.execute("file.newSequence", json!({"name": "Placement", "fps": 24, "width": 128, "height": 128, "video": 1, "audio": 1})).unwrap();+ s.execute("playhead.set", json!({"frame": 72})).unwrap();+ params["seconds"] = json!(2);+ let r = s.execute(command, params).unwrap();+ let clip = ClipId(r["clip"].as_u64().unwrap());+ let (_, it) = s.active_sequence().unwrap().find_item(clip).unwrap();+ assert_eq!(it.start, Tick::ZERO, "{command}");+ assert_eq!(it.duration, Tick::from_seconds_f64(2.0));+ }+ }+}+#[test]fn new_text_makes_a_graphic_clip_above_the_footage() {let mut s = demo();