57 lines
1.3 KiB
Rust
57 lines
1.3 KiB
Rust
|
use keyframe::EasingFunction;
|
||
|
|
||
|
pub struct Animation {
|
||
|
time: f32,
|
||
|
duration: f32,
|
||
|
from: f32,
|
||
|
to: f32,
|
||
|
function: Option<Box<dyn EasingFunction>>,
|
||
|
direction: bool,
|
||
|
}
|
||
|
|
||
|
impl Animation {
|
||
|
pub fn new(start: f32) -> Self {
|
||
|
Self {
|
||
|
time: 1.0,
|
||
|
duration: 0.0,
|
||
|
from: 0.0,
|
||
|
to: start,
|
||
|
function: None,
|
||
|
direction: true,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
pub fn update(&mut self, dt: f32) {
|
||
|
self.time += dt;
|
||
|
}
|
||
|
|
||
|
pub fn is_active(&self) -> bool {
|
||
|
self.function.is_none() || self.time >= self.duration
|
||
|
}
|
||
|
|
||
|
pub fn get(&self) -> f32 {
|
||
|
if self.time >= self.duration {
|
||
|
self.to
|
||
|
} else if let Some(function) = self.function.as_ref() {
|
||
|
let (from, to) = if self.direction {
|
||
|
(self.from, self.to)
|
||
|
} else {
|
||
|
(self.to, self.from)
|
||
|
};
|
||
|
|
||
|
let lerp = function.y((self.time / self.duration) as f64) as f32;
|
||
|
(1.0 - lerp) * from + lerp * to
|
||
|
} else {
|
||
|
self.to
|
||
|
}
|
||
|
}
|
||
|
|
||
|
pub fn ease_to(&mut self, function: Box<dyn EasingFunction>, duration: f32, to: f32) {
|
||
|
self.from = self.get();
|
||
|
self.to = to;
|
||
|
self.time = 0.0;
|
||
|
self.duration = duration;
|
||
|
self.function = Some(function);
|
||
|
}
|
||
|
}
|