Add animation delay

This commit is contained in:
mars 2022-07-09 18:23:13 -06:00
parent 1a9b1fd9c3
commit 5d1c12ffe2
1 changed files with 19 additions and 5 deletions

View File

@ -4,6 +4,7 @@ use keyframe::EasingFunction;
pub struct Animation<F> {
time: f32,
duration: f32,
delay: f32,
from: f32,
to: f32,
function: F,
@ -17,6 +18,7 @@ impl<F: EasingFunction> Animation<F> {
duration,
from,
to,
delay: 0.0,
function,
direction: false,
}
@ -32,10 +34,18 @@ impl<F: EasingFunction> Animation<F> {
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
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 {
@ -66,9 +76,13 @@ impl<F: EasingFunction> Animation<F> {
if self.is_active() {
self.time = self.duration - self.time;
} else {
self.time = 0.0;
self.time = -self.delay;
}
self.direction = !self.direction;
}
pub fn set_delay(&mut self, delay: f32) {
self.delay = delay;
}
}