use lgwks_deps::libm;
use super::error::{MotionError, MotionErrorCode};
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub enum Easing {
Named(NamedCurve),
CubicBezier {
p1x: f32,
p1y: f32,
p2x: f32,
p2y: f32,
},
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum NamedCurve {
Instant,
Linear,
QuadIn,
QuadOut,
QuadInOut,
CubicIn,
CubicOut,
CubicInOut,
QuartIn,
QuartOut,
QuartInOut,
QuintIn,
QuintOut,
QuintInOut,
SineIn,
SineOut,
SineInOut,
ExpoIn,
ExpoOut,
ExpoInOut,
CircIn,
CircOut,
CircInOut,
BackIn,
BackOut,
BackInOut,
}
impl Easing {
#[must_use]
pub const fn named(curve: NamedCurve) -> Self {
Self::Named(curve)
}
pub fn cubic_bezier(p1x: f32, p1y: f32, p2x: f32, p2y: f32) -> Result<Self, MotionError> {
let x_in_domain = (0.0..=1.0).contains(&p1x) && (0.0..=1.0).contains(&p2x);
MotionError::require(
x_in_domain,
MotionErrorCode::EasingDomainError,
"cubic-bezier x control points must lie in [0, 1]",
)?;
Ok(Self::CubicBezier { p1x, p1y, p2x, p2y })
}
#[must_use]
pub fn value_at(&self, time_normalized: f32) -> f32 {
let clamped = time_normalized.clamp(0.0, 1.0);
match *self {
Self::Named(curve) => eval_named(curve, clamped),
Self::CubicBezier { p1x, p1y, p2x, p2y } => {
eval_cubic_bezier(p1x, p1y, p2x, p2y, clamped)
}
}
}
}
fn eval_named(curve: NamedCurve, time: f32) -> f32 {
const BACK_OVERSHOOT: f32 = 1.70158;
match curve {
NamedCurve::Instant => {
if time < 1.0 {
0.0
} else {
1.0
}
}
NamedCurve::Linear => time,
NamedCurve::QuadIn => time * time,
NamedCurve::QuadOut => 1.0 - (1.0 - time) * (1.0 - time),
NamedCurve::QuadInOut => smooth_power_in_out(time, 2.0),
NamedCurve::CubicIn => time * time * time,
NamedCurve::CubicOut => 1.0 - (1.0 - time) * (1.0 - time) * (1.0 - time),
NamedCurve::CubicInOut => smooth_power_in_out(time, 3.0),
NamedCurve::QuartIn => time * time * time * time,
NamedCurve::QuartOut => 1.0 - pow4(1.0 - time),
NamedCurve::QuartInOut => smooth_power_in_out(time, 4.0),
NamedCurve::QuintIn => time * time * time * time * time,
NamedCurve::QuintOut => 1.0 - pow5(1.0 - time),
NamedCurve::QuintInOut => smooth_power_in_out(time, 5.0),
NamedCurve::SineIn => 1.0 - libm::cosf(time * core::f32::consts::FRAC_PI_2),
NamedCurve::SineOut => libm::sinf(time * core::f32::consts::FRAC_PI_2),
NamedCurve::SineInOut => -(libm::cosf(core::f32::consts::PI * time) - 1.0) / 2.0,
NamedCurve::ExpoIn => {
if time <= 0.0 {
0.0
} else {
libm::powf(2.0, 10.0 * time - 10.0)
}
}
NamedCurve::ExpoOut => {
if time >= 1.0 {
1.0
} else {
1.0 - libm::powf(2.0, -10.0 * time)
}
}
NamedCurve::ExpoInOut => smooth_expo_in_out(time),
NamedCurve::CircIn => 1.0 - libm::sqrtf(1.0 - time * time),
NamedCurve::CircOut => libm::sqrtf(1.0 - (time - 1.0) * (time - 1.0)),
NamedCurve::CircInOut => smooth_circ_in_out(time),
NamedCurve::BackIn => time * time * ((BACK_OVERSHOOT + 1.0) * time - BACK_OVERSHOOT),
NamedCurve::BackOut => {
let shifted = time - 1.0;
shifted * shifted * ((BACK_OVERSHOOT + 1.0) * shifted + BACK_OVERSHOOT) + 1.0
}
NamedCurve::BackInOut => smooth_back_in_out(time, BACK_OVERSHOOT),
}
}
fn smooth_power_in_out(time: f32, power: f32) -> f32 {
if time < 0.5 {
libm::powf(2.0 * time, power) / 2.0
} else {
1.0 - libm::powf(-2.0 * time + 2.0, power) / 2.0
}
}
fn smooth_expo_in_out(time: f32) -> f32 {
if time <= 0.0 {
return 0.0;
}
if time >= 1.0 {
return 1.0;
}
if time < 0.5 {
libm::powf(2.0, 20.0 * time - 10.0) / 2.0
} else {
(2.0 - libm::powf(2.0, -20.0 * time + 10.0)) / 2.0
}
}
fn smooth_circ_in_out(time: f32) -> f32 {
if time < 0.5 {
(1.0 - libm::sqrtf(1.0 - 4.0 * time * time)) / 2.0
} else {
(libm::sqrtf(1.0 - (-2.0 * time + 2.0) * (-2.0 * time + 2.0)) + 1.0) / 2.0
}
}
fn smooth_back_in_out(time: f32, overshoot: f32) -> f32 {
let overshoot_scaled = overshoot * 1.525;
if time < 0.5 {
let scaled = 2.0 * time;
scaled * scaled * ((overshoot_scaled + 1.0) * scaled - overshoot_scaled) / 2.0
} else {
let scaled = 2.0 * time - 2.0;
(scaled * scaled * ((overshoot_scaled + 1.0) * scaled + overshoot_scaled) + 2.0) / 2.0
}
}
fn pow4(value: f32) -> f32 {
value * value * value * value
}
fn pow5(value: f32) -> f32 {
value * value * value * value * value
}
fn eval_cubic_bezier(p1x: f32, p1y: f32, p2x: f32, p2y: f32, time: f32) -> f32 {
let mut parameter = time;
for _ in 0..8 {
let position = bezier_value(p1x, p2x, parameter);
let slope = bezier_derivative(p1x, p2x, parameter);
if slope.abs() < 1e-6 {
break;
}
parameter -= (position - time) / slope;
}
bezier_value(p1y, p2y, parameter.clamp(0.0, 1.0))
}
fn bezier_value(p1: f32, p2: f32, time: f32) -> f32 {
let mt = 1.0 - time;
3.0 * mt * mt * time * p1 + 3.0 * mt * time * time * p2 + time * time * time
}
fn bezier_derivative(p1: f32, p2: f32, time: f32) -> f32 {
let mt = 1.0 - time;
3.0 * mt * mt * p1 + 6.0 * mt * time * (p2 - p1) + 3.0 * time * time * (1.0 - p2)
}