#[derive(Debug, Clone)]
pub enum AnimatedValue {
Integer {
from: i32,
to: i32,
},
Float {
from: f32,
to: f32,
},
Color {
from: [u8; 4],
to: [u8; 4],
},
Position {
from: (f32, f32),
to: (f32, f32),
},
Scale {
from: (f32, f32),
to: (f32, f32),
},
}
impl AnimatedValue {
pub fn interpolate(&self, progress: f32) -> AnimatedResult {
let t = progress.clamp(0.0, 1.0);
match self {
Self::Integer { from, to } => {
let value = *from + ((to - from) as f32 * t) as i32;
AnimatedResult::Integer(value)
}
Self::Float { from, to } => {
let value = from + (to - from) * t;
AnimatedResult::Float(value)
}
Self::Color { from, to } => {
let r = from[0] as f32 + (to[0] as f32 - from[0] as f32) * t;
let g = from[1] as f32 + (to[1] as f32 - from[1] as f32) * t;
let b = from[2] as f32 + (to[2] as f32 - from[2] as f32) * t;
let a = from[3] as f32 + (to[3] as f32 - from[3] as f32) * t;
AnimatedResult::Color([r as u8, g as u8, b as u8, a as u8])
}
Self::Position { from, to } => {
let x = from.0 + (to.0 - from.0) * t;
let y = from.1 + (to.1 - from.1) * t;
AnimatedResult::Position((x, y))
}
Self::Scale { from, to } => {
let x = from.0 + (to.0 - from.0) * t;
let y = from.1 + (to.1 - from.1) * t;
AnimatedResult::Scale((x, y))
}
}
}
}
#[derive(Debug, Clone)]
pub enum AnimatedResult {
Integer(i32),
Float(f32),
Color([u8; 4]),
Position((f32, f32)),
Scale((f32, f32)),
}