use crate::utils::easing::Easing;
use std::time::Duration;
use super::{Interpolatable, Timer};
#[derive(Clone, Debug)]
pub struct AnimatedValue<T: Interpolatable> {
current: T,
from: T,
to: T,
timer: Timer,
easing: Easing,
}
impl<T: Interpolatable> AnimatedValue<T> {
pub fn new(initial: T, duration: Duration) -> Self {
Self {
current: initial.clone(),
from: initial.clone(),
to: initial,
timer: Timer::new(duration),
easing: Easing::OutQuad,
}
}
pub fn easing(mut self, easing: Easing) -> Self {
self.easing = easing;
self
}
pub fn animate_to(&mut self, target: T) {
self.from = self.current.clone();
self.to = target;
self.timer.restart();
}
pub fn set(&mut self, value: T) {
self.current = value.clone();
self.from = value.clone();
self.to = value;
self.timer.reset();
}
pub fn value(&mut self) -> &T {
if self.timer.is_running() || !self.timer.is_finished() {
let t = self.timer.progress_eased(self.easing);
self.current = self.from.lerp(&self.to, t);
}
&self.current
}
pub fn is_complete(&self) -> bool {
self.timer.is_finished()
}
pub fn target(&self) -> &T {
&self.to
}
}