Skip to main content

ass_renderer/animation/
value.rs

1//! Animated property values and their interpolation results
2
3/// Animated property value
4#[derive(Debug, Clone)]
5pub enum AnimatedValue {
6    /// Integer value animation
7    Integer {
8        /// Starting value
9        from: i32,
10        /// Ending value
11        to: i32,
12    },
13    /// Float value animation
14    Float {
15        /// Starting value
16        from: f32,
17        /// Ending value
18        to: f32,
19    },
20    /// Color animation (RGBA)
21    Color {
22        /// Starting color [R, G, B, A]
23        from: [u8; 4],
24        /// Ending color [R, G, B, A]
25        to: [u8; 4],
26    },
27    /// Position animation
28    Position {
29        /// Starting position (x, y)
30        from: (f32, f32),
31        /// Ending position (x, y)
32        to: (f32, f32),
33    },
34    /// Scale animation
35    Scale {
36        /// Starting scale (x, y)
37        from: (f32, f32),
38        /// Ending scale (x, y)
39        to: (f32, f32),
40    },
41}
42
43impl AnimatedValue {
44    /// Interpolate value at given progress
45    pub fn interpolate(&self, progress: f32) -> AnimatedResult {
46        let t = progress.clamp(0.0, 1.0);
47
48        match self {
49            Self::Integer { from, to } => {
50                let value = *from + ((to - from) as f32 * t) as i32;
51                AnimatedResult::Integer(value)
52            }
53            Self::Float { from, to } => {
54                let value = from + (to - from) * t;
55                AnimatedResult::Float(value)
56            }
57            Self::Color { from, to } => {
58                let r = from[0] as f32 + (to[0] as f32 - from[0] as f32) * t;
59                let g = from[1] as f32 + (to[1] as f32 - from[1] as f32) * t;
60                let b = from[2] as f32 + (to[2] as f32 - from[2] as f32) * t;
61                let a = from[3] as f32 + (to[3] as f32 - from[3] as f32) * t;
62                AnimatedResult::Color([r as u8, g as u8, b as u8, a as u8])
63            }
64            Self::Position { from, to } => {
65                let x = from.0 + (to.0 - from.0) * t;
66                let y = from.1 + (to.1 - from.1) * t;
67                AnimatedResult::Position((x, y))
68            }
69            Self::Scale { from, to } => {
70                let x = from.0 + (to.0 - from.0) * t;
71                let y = from.1 + (to.1 - from.1) * t;
72                AnimatedResult::Scale((x, y))
73            }
74        }
75    }
76}
77
78/// Result of animation interpolation
79#[derive(Debug, Clone)]
80pub enum AnimatedResult {
81    /// Integer result value
82    Integer(i32),
83    /// Float result value
84    Float(f32),
85    /// Color result value [R, G, B, A]
86    Color([u8; 4]),
87    /// Position result value (x, y)
88    Position((f32, f32)),
89    /// Scale result value (x, y)
90    Scale((f32, f32)),
91}