97 lines
2.1 KiB
Rust
97 lines
2.1 KiB
Rust
use keyframe::EasingFunction;
|
|
|
|
#[derive(Clone)]
|
|
pub struct Animation<F> {
|
|
time: f32,
|
|
duration: f32,
|
|
in_delay: f32,
|
|
out_delay: f32,
|
|
from: f32,
|
|
to: f32,
|
|
function: F,
|
|
direction: bool,
|
|
}
|
|
|
|
impl<F: EasingFunction> Animation<F> {
|
|
pub fn new(function: F, duration: f32, from: f32, to: f32) -> Self {
|
|
Self {
|
|
time: duration,
|
|
duration,
|
|
from,
|
|
to,
|
|
in_delay: 0.0,
|
|
out_delay: 0.0,
|
|
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() {
|
|
if self.time <= 0.0 {
|
|
if self.direction {
|
|
self.from
|
|
} else {
|
|
self.to
|
|
}
|
|
} else {
|
|
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 if self.direction {
|
|
self.time = -self.out_delay;
|
|
} else {
|
|
self.time = -self.in_delay;
|
|
}
|
|
|
|
self.direction = !self.direction;
|
|
}
|
|
|
|
pub fn set_in_delay(&mut self, delay: f32) {
|
|
self.in_delay = delay;
|
|
}
|
|
|
|
pub fn set_out_delay(&mut self, delay: f32) {
|
|
self.out_delay = delay;
|
|
}
|
|
}
|