Skip to content

Commit

Permalink
Only allow for types that contain the Tween trait
Browse files Browse the repository at this point in the history
  • Loading branch information
TheNachoBIT committed Sep 28, 2024
1 parent 6ac2594 commit d61b4d9
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 22 deletions.
44 changes: 39 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<i64> = timeline.new_sequence().unwrap();
let mut sequence_one: &mut Sequence<f64> = 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<i32> = timeline.new_sequence().unwrap();
let mut sequence_two: &mut Sequence<f32> = 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<Vec2> = 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());
}

Expand Down
50 changes: 33 additions & 17 deletions src/timeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ impl Timeline {
}
}

pub fn new_sequence<T: 'static>(&mut self) -> Option<&mut Sequence<T>> {
pub fn new_sequence<T: Tween + 'static>(&mut self) -> Option<&mut Sequence<T>> {
if self.sequences.get::<Vec<Sequence<T>>>().is_none() {
self.sequences.insert(Vec::<Sequence<T>>::new());
}
Expand Down Expand Up @@ -409,17 +409,17 @@ impl Timeline {
}

#[derive(Debug)]
pub struct Sequence<T> {
pub struct Sequence<T: Tween> {
tree: BTreeMap<isize, HourLeaf<T>>,
}

impl<T> Default for Sequence<T> {
impl<T: Tween> Default for Sequence<T> {
fn default() -> Self {
Self::new()
}
}

impl<T> Sequence<T> {
impl<T: Tween> Sequence<T> {
pub fn new() -> Self {
Self {
tree: BTreeMap::new(),
Expand Down Expand Up @@ -474,6 +474,8 @@ impl<T> Sequence<T> {
key: Keyframe<T>,
time: &Timecode,
) -> Option<&mut Keyframe<T>> {
// TODO: Make it so it returns 'None' instead of panicking.

let hour: &mut HourLeaf<T> = self.get_or_create_hour_with_timestamp(time).unwrap();
let minute: &mut MinuteLeaf<T> = hour.get_or_create_minute_with_timestamp(time).unwrap();
let second: &mut SecondLeaf<T> = minute.get_or_create_second_with_timestamp(time).unwrap();
Expand All @@ -482,6 +484,20 @@ impl<T> Sequence<T> {
frame.add_keyframe_at_timestamp(time, key)
}

/// # Panics
///
/// TODO!
pub fn get_keyframe_at_timestamp(&mut self, time: &Timecode) -> Option<&mut Keyframe<T>> {
// TODO: Make it so it returns 'None' instead of panicking.

let hour: &mut HourLeaf<T> = self.get_hour_with_timestamp(time).unwrap();
let minute: &mut MinuteLeaf<T> = hour.get_minute_with_timestamp(time).unwrap();
let second: &mut SecondLeaf<T> = minute.get_second_with_timestamp(time).unwrap();
let frame: &mut FrameLeaf<T> = second.get_frame_with_timestamp(time).unwrap();

frame.get_keyframe_at_timestamp(time)
}

/// # Panics
///
/// TODO!
Expand All @@ -493,17 +509,17 @@ impl<T> Sequence<T> {
}

#[derive(Debug)]
pub struct HourLeaf<T> {
pub struct HourLeaf<T: Tween> {
minutes: BTreeMap<isize, MinuteLeaf<T>>,
}

impl<T> Default for HourLeaf<T> {
impl<T: Tween> Default for HourLeaf<T> {
fn default() -> Self {
Self::new()
}
}

impl<T> HourLeaf<T> {
impl<T: Tween> HourLeaf<T> {
pub fn new() -> Self {
Self {
minutes: BTreeMap::new(),
Expand Down Expand Up @@ -555,17 +571,17 @@ impl<T> HourLeaf<T> {
}

#[derive(Debug)]
pub struct MinuteLeaf<T> {
pub struct MinuteLeaf<T: Tween> {
seconds: BTreeMap<isize, SecondLeaf<T>>,
}

impl<T> Default for MinuteLeaf<T> {
impl<T: Tween> Default for MinuteLeaf<T> {
fn default() -> Self {
Self::new()
}
}

impl<T> MinuteLeaf<T> {
impl<T: Tween> MinuteLeaf<T> {
pub fn new() -> Self {
Self {
seconds: BTreeMap::new(),
Expand Down Expand Up @@ -617,17 +633,17 @@ impl<T> MinuteLeaf<T> {
}

#[derive(Debug)]
pub struct SecondLeaf<T> {
pub struct SecondLeaf<T: Tween> {
frames: BTreeMap<isize, FrameLeaf<T>>,
}

impl<T> Default for SecondLeaf<T> {
impl<T: Tween> Default for SecondLeaf<T> {
fn default() -> Self {
Self::new()
}
}

impl<T> SecondLeaf<T> {
impl<T: Tween> SecondLeaf<T> {
pub fn new() -> Self {
Self {
frames: BTreeMap::new(),
Expand Down Expand Up @@ -676,17 +692,17 @@ impl<T> SecondLeaf<T> {
}

#[derive(Debug)]
pub struct FrameLeaf<T> {
pub struct FrameLeaf<T: Tween> {
nanos: BTreeMap<isize, Keyframe<T>>,
}

impl<T> Default for FrameLeaf<T> {
impl<T: Tween> Default for FrameLeaf<T> {
fn default() -> Self {
Self::new()
}
}

impl<T> FrameLeaf<T> {
impl<T: Tween> FrameLeaf<T> {
pub fn new() -> Self {
Self {
nanos: BTreeMap::new(),
Expand Down Expand Up @@ -724,6 +740,6 @@ impl<T> FrameLeaf<T> {
}

#[derive(Debug, Default)]
pub struct Keyframe<T> {
pub struct Keyframe<T: Tween> {
pub value: T,
}
7 changes: 7 additions & 0 deletions src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit d61b4d9

Please sign in to comment.