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
53
54
use super::functions;

#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
/// Cubic bezier curve animation
pub struct CubicBezier {
    pub original_control: crate::Transform,
    pub end: crate::Transform,
    pub end_control: crate::Transform,
    pub last_for_seconds: f64,
}

impl CubicBezier {
    pub fn new(
        original_control: crate::Transform,
        end: crate::Transform,
        end_control: crate::Transform,
        last_for_seconds: f64,
    ) -> Self {
        Self {
            original_control,
            end,
            end_control,
            last_for_seconds,
        }
    }
}

impl crate::Animation for CubicBezier {
    fn animated_transform(&mut self, original_transform: crate::Transform, duration_seconds: f64) -> crate::Transform {
        let t: f64;

        if self.last_for_seconds == 0.0 {
            t = 0.0;
        } else {
            if duration_seconds <= self.last_for_seconds {
                t = (self.last_for_seconds - duration_seconds) / self.last_for_seconds;
            } else {
                t = 0.0;
            }
        }

        return self.end.apply_cubic_bezier(
            self.end_control,
            original_transform,
            self.original_control,
            t,
            functions::cubic_bezier,
        );
    }

    fn is_done(&self, duration_seconds: f64) -> bool {
        return duration_seconds >= self.last_for_seconds;
    }
}