ass_renderer/animation/
value.rs1#[derive(Debug, Clone)]
5pub enum AnimatedValue {
6 Integer {
8 from: i32,
10 to: i32,
12 },
13 Float {
15 from: f32,
17 to: f32,
19 },
20 Color {
22 from: [u8; 4],
24 to: [u8; 4],
26 },
27 Position {
29 from: (f32, f32),
31 to: (f32, f32),
33 },
34 Scale {
36 from: (f32, f32),
38 to: (f32, f32),
40 },
41}
42
43impl AnimatedValue {
44 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#[derive(Debug, Clone)]
80pub enum AnimatedResult {
81 Integer(i32),
83 Float(f32),
85 Color([u8; 4]),
87 Position((f32, f32)),
89 Scale((f32, f32)),
91}