From d61b4d91a27de5a4e34afcc941ba16d63472d385 Mon Sep 17 00:00:00 2001 From: TheNachoBIT Date: Sat, 28 Sep 2024 10:56:48 -0300 Subject: [PATCH] Only allow for types that contain the Tween trait --- src/lib.rs | 44 ++++++++++++++++++++++++++++++++++++++----- src/timeline.rs | 50 ++++++++++++++++++++++++++++++++----------------- src/value.rs | 7 +++++++ 3 files changed, 79 insertions(+), 22 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index f7de37d..3158b68 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -330,19 +330,53 @@ fn tline_set_by_timestamp() { } #[test] -fn tline_new_sequence() { +fn tline_new_integer_sequences() { let mut timeline = Timeline::new(Framerate::Fixed(24.0)); - let mut sequence_one: &mut Sequence = timeline.new_sequence().unwrap(); + let mut sequence_one: &mut Sequence = timeline.new_sequence().unwrap(); assert!(sequence_one - .add_keyframe_at_timestamp(Keyframe { value: 3 }, &tcode_hmsf!(00:00:05:00)) + .add_keyframe_at_timestamp(Keyframe { value: 3.0 }, &tcode_hmsf!(00:00:05:00)) .is_some()); - let mut sequence_two: &mut Sequence = timeline.new_sequence().unwrap(); + let mut sequence_two: &mut Sequence = timeline.new_sequence().unwrap(); assert!(sequence_two - .add_keyframe_at_timestamp(Keyframe { value: 6 }, &tcode_hmsf!(00:00:10:00)) + .add_keyframe_at_timestamp(Keyframe { value: 6.0 }, &tcode_hmsf!(00:00:10:00)) + .is_some()); +} + +#[test] +fn tline_new_kurbo_sequences() { + use kurbo::Vec2; + + let mut timeline = Timeline::new(Framerate::Fixed(24.0)); + + let mut sequence: &mut Sequence = timeline.new_sequence().unwrap(); + + sequence.add_keyframes_at_timestamp(vec![ + ( + Keyframe { + value: Vec2::new(0.0, 1.0), + }, + &tcode_hmsf!(00:00:01:00), + ), + ( + Keyframe { + value: Vec2::new(1.0, 1.0), + }, + &tcode_hmsf!(00:00:02:00), + ), + ( + Keyframe { + value: Vec2::new(1.0, 2.0), + }, + &tcode_hmsf!(00:00:03:00), + ), + ]); + + assert!(sequence + .get_keyframe_at_timestamp(&tcode_hmsf!(00:00:02:00)) .is_some()); } diff --git a/src/timeline.rs b/src/timeline.rs index 0c36624..52dc10c 100644 --- a/src/timeline.rs +++ b/src/timeline.rs @@ -368,7 +368,7 @@ impl Timeline { } } - pub fn new_sequence(&mut self) -> Option<&mut Sequence> { + pub fn new_sequence(&mut self) -> Option<&mut Sequence> { if self.sequences.get::>>().is_none() { self.sequences.insert(Vec::>::new()); } @@ -409,17 +409,17 @@ impl Timeline { } #[derive(Debug)] -pub struct Sequence { +pub struct Sequence { tree: BTreeMap>, } -impl Default for Sequence { +impl Default for Sequence { fn default() -> Self { Self::new() } } -impl Sequence { +impl Sequence { pub fn new() -> Self { Self { tree: BTreeMap::new(), @@ -474,6 +474,8 @@ impl Sequence { key: Keyframe, time: &Timecode, ) -> Option<&mut Keyframe> { + // TODO: Make it so it returns 'None' instead of panicking. + let hour: &mut HourLeaf = self.get_or_create_hour_with_timestamp(time).unwrap(); let minute: &mut MinuteLeaf = hour.get_or_create_minute_with_timestamp(time).unwrap(); let second: &mut SecondLeaf = minute.get_or_create_second_with_timestamp(time).unwrap(); @@ -482,6 +484,20 @@ impl Sequence { frame.add_keyframe_at_timestamp(time, key) } + /// # Panics + /// + /// TODO! + pub fn get_keyframe_at_timestamp(&mut self, time: &Timecode) -> Option<&mut Keyframe> { + // TODO: Make it so it returns 'None' instead of panicking. + + let hour: &mut HourLeaf = self.get_hour_with_timestamp(time).unwrap(); + let minute: &mut MinuteLeaf = hour.get_minute_with_timestamp(time).unwrap(); + let second: &mut SecondLeaf = minute.get_second_with_timestamp(time).unwrap(); + let frame: &mut FrameLeaf = second.get_frame_with_timestamp(time).unwrap(); + + frame.get_keyframe_at_timestamp(time) + } + /// # Panics /// /// TODO! @@ -493,17 +509,17 @@ impl Sequence { } #[derive(Debug)] -pub struct HourLeaf { +pub struct HourLeaf { minutes: BTreeMap>, } -impl Default for HourLeaf { +impl Default for HourLeaf { fn default() -> Self { Self::new() } } -impl HourLeaf { +impl HourLeaf { pub fn new() -> Self { Self { minutes: BTreeMap::new(), @@ -555,17 +571,17 @@ impl HourLeaf { } #[derive(Debug)] -pub struct MinuteLeaf { +pub struct MinuteLeaf { seconds: BTreeMap>, } -impl Default for MinuteLeaf { +impl Default for MinuteLeaf { fn default() -> Self { Self::new() } } -impl MinuteLeaf { +impl MinuteLeaf { pub fn new() -> Self { Self { seconds: BTreeMap::new(), @@ -617,17 +633,17 @@ impl MinuteLeaf { } #[derive(Debug)] -pub struct SecondLeaf { +pub struct SecondLeaf { frames: BTreeMap>, } -impl Default for SecondLeaf { +impl Default for SecondLeaf { fn default() -> Self { Self::new() } } -impl SecondLeaf { +impl SecondLeaf { pub fn new() -> Self { Self { frames: BTreeMap::new(), @@ -676,17 +692,17 @@ impl SecondLeaf { } #[derive(Debug)] -pub struct FrameLeaf { +pub struct FrameLeaf { nanos: BTreeMap>, } -impl Default for FrameLeaf { +impl Default for FrameLeaf { fn default() -> Self { Self::new() } } -impl FrameLeaf { +impl FrameLeaf { pub fn new() -> Self { Self { nanos: BTreeMap::new(), @@ -724,6 +740,6 @@ impl FrameLeaf { } #[derive(Debug, Default)] -pub struct Keyframe { +pub struct Keyframe { pub value: T, } diff --git a/src/value.rs b/src/value.rs index 394e58e..3f65735 100644 --- a/src/value.rs +++ b/src/value.rs @@ -178,6 +178,13 @@ impl Tween for f64 { } } +impl Tween for f32 { + fn tween(&self, other: &Self, t: f64, _easing: &Easing) -> Self { + // Same TODO as f64 + keyframe::ease(keyframe::functions::Linear, *self, *other, t) + } +} + impl Tween for kurbo::Point { fn tween(&self, other: &Self, t: f64, easing: &Easing) -> Self { Self::new(