tuigui/animations/
cubic_bezier.rs

1use super::functions;
2
3#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
4/// Cubic bezier curve animation
5pub struct CubicBezier {
6    pub original_control: crate::Transform,
7    pub end: crate::Transform,
8    pub end_control: crate::Transform,
9    pub last_for_seconds: f64,
10}
11
12impl CubicBezier {
13    pub fn new(
14        original_control: crate::Transform,
15        end: crate::Transform,
16        end_control: crate::Transform,
17        last_for_seconds: f64,
18    ) -> Self {
19        Self {
20            original_control,
21            end,
22            end_control,
23            last_for_seconds,
24        }
25    }
26}
27
28impl crate::Animation for CubicBezier {
29    fn animated_transform(&mut self, original_transform: crate::Transform, duration_seconds: f64) -> crate::Transform {
30        let t: f64;
31
32        if self.last_for_seconds == 0.0 {
33            t = 0.0;
34        } else {
35            if duration_seconds <= self.last_for_seconds {
36                t = (self.last_for_seconds - duration_seconds) / self.last_for_seconds;
37            } else {
38                t = 0.0;
39            }
40        }
41
42        return self.end.apply_cubic_bezier(
43            self.end_control,
44            original_transform,
45            self.original_control,
46            t,
47            functions::cubic_bezier,
48        );
49    }
50
51    fn is_done(&self, duration_seconds: f64) -> bool {
52        return duration_seconds >= self.last_for_seconds;
53    }
54}