use keyframe::EasingFunction; #[derive(Clone)] pub struct Animation { time: f32, duration: f32, from: f32, to: f32, function: F, direction: bool, } impl Animation { pub fn new(function: F, duration: f32, from: f32, to: f32) -> Self { Self { time: duration, duration, from, to, function, direction: false, } } pub fn update(&mut self, dt: f32) { self.time += dt; } pub fn is_active(&self) -> bool { self.time < self.duration } pub fn get(&self) -> f32 { if self.is_active() { let x = self.time / self.duration; let x = if self.direction { x } else { 1.0 - x }; let lerp = self.function.y(x as f64) as f32; (1.0 - lerp) * self.from + lerp * self.to } else if self.direction { self.to } else { self.from } } pub fn ease_to(&mut self, to: f32) { self.from = self.get(); self.to = to; self.time = 0.0; self.direction = true; } pub fn ease_in(&mut self) { if !self.direction { self.ease_toggle(); } } pub fn ease_out(&mut self) { if self.direction { self.ease_toggle(); } } pub fn ease_toggle(&mut self) { if self.is_active() { self.time = self.duration - self.time; } else { self.time = 0.0; } self.direction = !self.direction; } }