1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
use crate::BoneKeyframe;
use std::cmp::Ordering;
use std::ops::Deref;
use std::slice::IterMut;

mod deserialize;

/// Keyframes sorted in ascending frame order
#[derive(Debug, PartialEq, Serialize, Default, Clone)]
pub struct SortedKeyframes(Vec<BoneKeyframe>);

impl SortedKeyframes {
    /// Create a new SortedKeyframes.
    ///
    /// The passed in keyframes will get be sorted.
    pub fn new(keyframes: Vec<BoneKeyframe>) -> Self {
        let mut keys = SortedKeyframes(keyframes);

        keys.sort_ascending();

        keys
    }

    fn sort_ascending(&mut self) {
        self.0.sort_by(|a, b| a.frame().cmp(&b.frame()));
    }
}

impl SortedKeyframes {
    pub(crate) fn iter_mut(&mut self) -> IterMut<'_, BoneKeyframe> {
        self.0.iter_mut()
    }

    pub(crate) fn push(&mut self, bone_keyframe: BoneKeyframe) {
        self.0.push(bone_keyframe)
    }

    pub(crate) fn sort_by<F>(&mut self, compare: F)
    where
        F: FnMut(&BoneKeyframe, &BoneKeyframe) -> Ordering,
    {
        self.0.sort_by(compare);
    }
}

impl Deref for SortedKeyframes {
    type Target = Vec<BoneKeyframe>;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}