use std::time::Duration;
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Tween {
pub begin: f32,
pub end: f32,
pub duration: Duration,
pub func: Easing,
pub yoyo: bool,
pub value: f32,
pub time: Duration,
pub overflow: Duration,
pub reversed: bool,
}
impl Tween {
pub fn new(begin: f32, end: f32, duration: Duration, func: Easing, yoyo: bool) -> Self {
Self {
begin,
end,
duration,
func,
yoyo,
value: begin,
time: Duration::ZERO,
overflow: Duration::ZERO,
reversed: false,
}
}
pub fn set_time(&mut self, current: Duration) {
if current >= self.duration {
self.overflow = current - self.duration;
self.time = self.duration;
self.value = self.end;
} else if current.is_zero() {
self.overflow = Duration::ZERO;
self.time = Duration::ZERO;
self.value = self.begin;
} else {
self.overflow = Duration::ZERO;
self.time = current;
self.value = self.func.apply(
self.time.as_secs_f32(),
self.begin,
self.change(),
self.duration.as_secs_f32(),
);
}
}
pub fn change(&self) -> f32 {
self.end - self.begin
}
pub fn is_finished(&self) -> bool {
if self.reversed {
self.time.is_zero()
} else {
self.time >= self.duration
}
}
pub fn update(&mut self, dt: Duration) {
let prev = self.time;
if self.reversed {
match prev.checked_sub(dt) {
Some(t) => self.set_time(t),
None => {
self.set_time(Duration::ZERO);
self.overflow = dt - prev;
}
}
} else {
self.set_time(prev + dt);
}
while self.yoyo && !self.duration.is_zero() && self.is_finished() {
let over = self.overflow;
self.reversed = !self.reversed;
if self.reversed {
if over <= self.duration {
self.set_time(self.duration - over);
} else {
self.set_time(Duration::ZERO);
self.overflow = over - self.duration;
}
} else {
self.set_time(over);
}
}
}
}
use std::f32::consts::PI;
const BACK_S: f32 = 1.70158;
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Easing {
Linear,
InQuad,
OutQuad,
InOutQuad,
OutInQuad,
InCubic,
OutCubic,
InOutCubic,
OutInCubic,
InQuart,
OutQuart,
InOutQuart,
OutInQuart,
InQuint,
OutQuint,
InOutQuint,
OutInQuint,
InSine,
OutSine,
InOutSine,
OutInSine,
InExpo,
OutExpo,
InOutExpo,
OutInExpo,
InCirc,
OutCirc,
InOutCirc,
OutInCirc,
InElastic,
OutElastic,
InOutElastic,
OutInElastic,
InBack,
OutBack,
InOutBack,
OutInBack,
InBounce,
OutBounce,
InOutBounce,
OutInBounce,
}
impl Easing {
pub(crate) fn apply(self, t: f32, b: f32, c: f32, d: f32) -> f32 {
use Easing::*;
match self {
Linear => linear(t, b, c, d),
InQuad => in_quad(t, b, c, d),
OutQuad => out_quad(t, b, c, d),
InOutQuad => in_out_quad(t, b, c, d),
OutInQuad => out_in_quad(t, b, c, d),
InCubic => in_cubic(t, b, c, d),
OutCubic => out_cubic(t, b, c, d),
InOutCubic => in_out_cubic(t, b, c, d),
OutInCubic => out_in_cubic(t, b, c, d),
InQuart => in_quart(t, b, c, d),
OutQuart => out_quart(t, b, c, d),
InOutQuart => in_out_quart(t, b, c, d),
OutInQuart => out_in_quart(t, b, c, d),
InQuint => in_quint(t, b, c, d),
OutQuint => out_quint(t, b, c, d),
InOutQuint => in_out_quint(t, b, c, d),
OutInQuint => out_in_quint(t, b, c, d),
InSine => in_sine(t, b, c, d),
OutSine => out_sine(t, b, c, d),
InOutSine => in_out_sine(t, b, c, d),
OutInSine => out_in_sine(t, b, c, d),
InExpo => in_expo(t, b, c, d),
OutExpo => out_expo(t, b, c, d),
InOutExpo => in_out_expo(t, b, c, d),
OutInExpo => out_in_expo(t, b, c, d),
InCirc => in_circ(t, b, c, d),
OutCirc => out_circ(t, b, c, d),
InOutCirc => in_out_circ(t, b, c, d),
OutInCirc => out_in_circ(t, b, c, d),
InElastic => in_elastic(t, b, c, d),
OutElastic => out_elastic(t, b, c, d),
InOutElastic => in_out_elastic(t, b, c, d),
OutInElastic => out_in_elastic(t, b, c, d),
InBack => in_back(t, b, c, d),
OutBack => out_back(t, b, c, d),
InOutBack => in_out_back(t, b, c, d),
OutInBack => out_in_back(t, b, c, d),
InBounce => in_bounce(t, b, c, d),
OutBounce => out_bounce(t, b, c, d),
InOutBounce => in_out_bounce(t, b, c, d),
OutInBounce => out_in_bounce(t, b, c, d),
}
}
}
fn linear(t: f32, b: f32, c: f32, d: f32) -> f32 {
c * t / d + b
}
fn in_quad(t: f32, b: f32, c: f32, d: f32) -> f32 {
c * (t / d).powi(2) + b
}
fn out_quad(t: f32, b: f32, c: f32, d: f32) -> f32 {
let t = t / d;
-c * t * (t - 2.0) + b
}
fn in_out_quad(t: f32, b: f32, c: f32, d: f32) -> f32 {
let t = t / d * 2.0;
if t < 1.0 {
return c / 2.0 * t.powi(2) + b;
}
-c / 2.0 * ((t - 1.0) * (t - 3.0) - 1.0) + b
}
fn out_in_quad(t: f32, b: f32, c: f32, d: f32) -> f32 {
if t < d / 2.0 {
return out_quad(t * 2.0, b, c / 2.0, d);
}
in_quad((t * 2.0) - d, b + c / 2.0, c / 2.0, d)
}
fn in_cubic(t: f32, b: f32, c: f32, d: f32) -> f32 {
c * (t / d).powi(3) + b
}
fn out_cubic(t: f32, b: f32, c: f32, d: f32) -> f32 {
c * ((t / d - 1.0).powi(3) + 1.0) + b
}
fn in_out_cubic(t: f32, b: f32, c: f32, d: f32) -> f32 {
let mut t = t / d * 2.0;
if t < 1.0 {
return c / 2.0 * t * t * t + b;
}
t -= 2.0;
c / 2.0 * (t * t * t + 2.0) + b
}
fn out_in_cubic(t: f32, b: f32, c: f32, d: f32) -> f32 {
if t < d / 2.0 {
return out_cubic(t * 2.0, b, c / 2.0, d);
}
in_cubic((t * 2.0) - d, b + c / 2.0, c / 2.0, d)
}
fn in_quart(t: f32, b: f32, c: f32, d: f32) -> f32 {
c * (t / d).powi(4) + b
}
fn out_quart(t: f32, b: f32, c: f32, d: f32) -> f32 {
-c * ((t / d - 1.0).powi(4) - 1.0) + b
}
fn in_out_quart(t: f32, b: f32, c: f32, d: f32) -> f32 {
let t = t / d * 2.0;
if t < 1.0 {
return c / 2.0 * t.powi(4) + b;
}
-c / 2.0 * ((t - 2.0).powi(4) - 2.0) + b
}
fn out_in_quart(t: f32, b: f32, c: f32, d: f32) -> f32 {
if t < d / 2.0 {
return out_quart(t * 2.0, b, c / 2.0, d);
}
in_quart((t * 2.0) - d, b + c / 2.0, c / 2.0, d)
}
fn in_quint(t: f32, b: f32, c: f32, d: f32) -> f32 {
c * (t / d).powi(5) + b
}
fn out_quint(t: f32, b: f32, c: f32, d: f32) -> f32 {
c * ((t / d - 1.0).powi(5) + 1.0) + b
}
fn in_out_quint(t: f32, b: f32, c: f32, d: f32) -> f32 {
let t = t / d * 2.0;
if t < 1.0 {
return c / 2.0 * t.powi(5) + b;
}
c / 2.0 * ((t - 2.0).powi(5) + 2.0) + b
}
fn out_in_quint(t: f32, b: f32, c: f32, d: f32) -> f32 {
if t < d / 2.0 {
return out_quint(t * 2.0, b, c / 2.0, d);
}
in_quint((t * 2.0) - d, b + c / 2.0, c / 2.0, d)
}
fn in_sine(t: f32, b: f32, c: f32, d: f32) -> f32 {
-c * (t / d * (PI / 2.0)).cos() + c + b
}
fn out_sine(t: f32, b: f32, c: f32, d: f32) -> f32 {
c * (t / d * (PI / 2.0)).sin() + b
}
fn in_out_sine(t: f32, b: f32, c: f32, d: f32) -> f32 {
-c / 2.0 * ((PI * t / d).cos() - 1.0) + b
}
fn out_in_sine(t: f32, b: f32, c: f32, d: f32) -> f32 {
if t < d / 2.0 {
return out_sine(t * 2.0, b, c / 2.0, d);
}
in_sine((t * 2.0) - d, b + c / 2.0, c / 2.0, d)
}
fn in_expo(t: f32, b: f32, c: f32, d: f32) -> f32 {
if t == 0.0 {
return b;
}
c * 2.0_f32.powf(10.0 * (t / d - 1.0)) + b - c * 0.001
}
fn out_expo(t: f32, b: f32, c: f32, d: f32) -> f32 {
if t == d {
return b + c;
}
c * 1.001 * (-(2.0_f32.powf(-10.0 * t / d)) + 1.0) + b
}
fn in_out_expo(t: f32, b: f32, c: f32, d: f32) -> f32 {
if t == 0.0 {
return b;
}
if t == d {
return b + c;
}
let t = t / d * 2.0;
if t < 1.0 {
return c / 2.0 * 2.0_f32.powf(10.0 * (t - 1.0)) + b - c * 0.0005;
}
c / 2.0 * 1.0005 * (-(2.0_f32.powf(-10.0 * (t - 1.0))) + 2.0) + b
}
fn out_in_expo(t: f32, b: f32, c: f32, d: f32) -> f32 {
if t < d / 2.0 {
return out_expo(t * 2.0, b, c / 2.0, d);
}
in_expo((t * 2.0) - d, b + c / 2.0, c / 2.0, d)
}
fn in_circ(t: f32, b: f32, c: f32, d: f32) -> f32 {
-c * ((1.0 - (t / d).powi(2)).sqrt() - 1.0) + b
}
fn out_circ(t: f32, b: f32, c: f32, d: f32) -> f32 {
c * (1.0 - (t / d - 1.0).powi(2)).sqrt() + b
}
fn in_out_circ(t: f32, b: f32, c: f32, d: f32) -> f32 {
let mut t = t / d * 2.0;
if t < 1.0 {
return -c / 2.0 * ((1.0 - t * t).sqrt() - 1.0) + b;
}
t -= 2.0;
c / 2.0 * ((1.0 - t * t).sqrt() + 1.0) + b
}
fn out_in_circ(t: f32, b: f32, c: f32, d: f32) -> f32 {
if t < d / 2.0 {
return out_circ(t * 2.0, b, c / 2.0, d);
}
in_circ((t * 2.0) - d, b + c / 2.0, c / 2.0, d)
}
fn in_elastic(t: f32, b: f32, c: f32, d: f32) -> f32 {
if t == 0.0 {
return b;
}
let mut t = t / d;
if t == 1.0 {
return b + c;
}
let (p, a, s) = calculate_pas(c, d);
t -= 1.0;
-(a * 2.0_f32.powf(10.0 * t) * ((t * d - s) * (2.0 * PI) / p).sin()) + b
}
fn out_elastic(t: f32, b: f32, c: f32, d: f32) -> f32 {
if t == 0.0 {
return b;
}
let t = t / d;
if t == 1.0 {
return b + c;
}
let (p, a, s) = calculate_pas(c, d);
a * 2.0_f32.powf(-10.0 * t) * ((t * d - s) * (2.0 * PI) / p).sin() + c + b
}
fn in_out_elastic(t: f32, b: f32, c: f32, d: f32) -> f32 {
if t == 0.0 {
return b;
}
let mut t = t / d * 2.0;
if t == 2.0 {
return b + c;
}
let (p, a, s) = calculate_pas(c, d);
t -= 1.0;
if t < 0.0 {
return -0.5 * (a * 2.0_f32.powf(10.0 * t) * ((t * d - s) * (2.0 * PI) / p).sin()) + b;
}
a * 2.0_f32.powf(-10.0 * t) * ((t * d - s) * (2.0 * PI) / p).sin() * 0.5 + c + b
}
fn out_in_elastic(t: f32, b: f32, c: f32, d: f32) -> f32 {
if t < d / 2.0 {
return out_elastic(t * 2.0, b, c / 2.0, d);
}
in_elastic((t * 2.0) - d, b + c / 2.0, c / 2.0, d)
}
fn in_back(t: f32, b: f32, c: f32, d: f32) -> f32 {
let t = t / d;
c * t * t * ((BACK_S + 1.0) * t - BACK_S) + b
}
fn out_back(t: f32, b: f32, c: f32, d: f32) -> f32 {
let t = t / d - 1.0;
c * (t * t * ((BACK_S + 1.0) * t + BACK_S) + 1.0) + b
}
fn in_out_back(t: f32, b: f32, c: f32, d: f32) -> f32 {
let s = BACK_S * 1.525;
let mut t = t / d * 2.0;
if t < 1.0 {
return c / 2.0 * (t * t * ((s + 1.0) * t - s)) + b;
}
t -= 2.0;
c / 2.0 * (t * t * ((s + 1.0) * t + s) + 2.0) + b
}
fn out_in_back(t: f32, b: f32, c: f32, d: f32) -> f32 {
if t < (d / 2.0) {
return out_back(t * 2.0, b, c / 2.0, d);
}
in_back((t * 2.0) - d, b + c / 2.0, c / 2.0, d)
}
fn out_bounce(t: f32, b: f32, c: f32, d: f32) -> f32 {
let mut t = t / d;
if t < 1.0 / 2.75 {
return c * (7.5625 * t * t) + b;
}
if t < 2.0 / 2.75 {
t -= 1.5 / 2.75;
return c * (7.5625 * t * t + 0.75) + b;
} else if t < 2.5 / 2.75 {
t -= 2.25 / 2.75;
return c * (7.5625 * t * t + 0.9375) + b;
}
t -= 2.625 / 2.75;
c * (7.5625 * t * t + 0.984375) + b
}
fn in_bounce(t: f32, b: f32, c: f32, d: f32) -> f32 {
c - out_bounce(d - t, 0.0, c, d) + b
}
fn in_out_bounce(t: f32, b: f32, c: f32, d: f32) -> f32 {
if t < d / 2.0 {
return in_bounce(t * 2.0, 0.0, c, d) * 0.5 + b;
}
out_bounce(t * 2.0 - d, 0.0, c, d) * 0.5 + c * 0.5 + b
}
fn out_in_bounce(t: f32, b: f32, c: f32, d: f32) -> f32 {
if t < d / 2.0 {
return out_bounce(t * 2.0, b, c / 2.0, d);
}
in_bounce((t * 2.0) - d, b + c / 2.0, c / 2.0, d)
}
fn calculate_pas(c: f32, d: f32) -> (f32, f32, f32) {
let p = d * 0.3;
(p, c, p / 4.0)
}