use super::*;
impl Easing {
#[must_use]
pub fn eval(&self, t: f32) -> f32 {
let u: f32 = t.clamp(0.0, 1.0);
match self {
Self::Linear => u,
Self::EaseOutCubic => {
let inv: f32 = 1.0 - u;
1.0 - inv * inv * inv
}
Self::EaseInOutCubic => {
if u < 0.5 {
4.0 * u * u * u
} else {
let k: f32 = -2.0 * u + 2.0;
1.0 - (k * k * k) * 0.5
}
}
Self::EaseOutBack => {
let c1: f32 = 1.4;
let c3: f32 = c1 + 1.0;
let k: f32 = u - 1.0;
1.0 + c3 * k * k * k + c1 * k * k
}
Self::EaseOutQuint => {
let inv: f32 = 1.0 - u;
let inv2: f32 = inv * inv;
1.0 - inv2 * inv2 * inv
}
Self::EaseInCubic => u * u * u,
Self::SpringGentle => {
if u <= 0.0 {
0.0
} else if u >= 1.0 {
1.0
} else {
let raw: f32 = 1.0 - (-6.2 * u).exp() * (9.0 * u).cos();
let end: f32 = 1.0 - (-6.2_f32).exp() * 9.0_f32.cos();
raw / end.max(0.001)
}
}
Self::SpringSnappy => {
if u <= 0.0 {
0.0
} else if u >= 1.0 {
1.0
} else {
let raw: f32 = 1.0 - (-8.4 * u).exp() * (12.5 * u).cos();
let end: f32 = 1.0 - (-8.4_f32).exp() * 12.5_f32.cos();
raw / end.max(0.001)
}
}
}
}
}
impl NodeTransition {
#[must_use]
pub fn for_node(node: NodeId) -> Self {
Self {
node,
elapsed: 0.0,
delay: 0.0,
duration: 0.20,
easing: Easing::EaseOutCubic,
from_opacity: 1.0,
to_opacity: 1.0,
from_tx: 0.0,
to_tx: 0.0,
from_ty: 0.0,
to_ty: 0.0,
from_scale: 1.0,
to_scale: 1.0,
hide_on_complete: false,
}
}
#[must_use]
pub fn with_delay(mut self, delay_secs: f32) -> Self {
self.set_delay(delay_secs.max(0.0));
self
}
#[must_use]
pub fn with_duration(mut self, duration_secs: f32) -> Self {
self.set_duration(duration_secs.max(0.016));
self
}
#[must_use]
pub fn with_easing(mut self, easing: Easing) -> Self {
self.set_easing(easing);
self
}
#[must_use]
pub fn with_opacity(mut self, from: f32, to: f32) -> Self {
self.set_from_opacity(from.clamp(0.0, 1.0));
self.set_to_opacity(to.clamp(0.0, 1.0));
self
}
#[must_use]
pub fn with_offset(mut self, from: Point, to: Point) -> Self {
self.set_from_tx(from.get_x());
self.set_from_ty(from.get_y());
self.set_to_tx(to.get_x());
self.set_to_ty(to.get_y());
self
}
#[must_use]
pub fn with_scale(mut self, from: f32, to: f32) -> Self {
self.set_from_scale(from.clamp(0.05, 4.0));
self.set_to_scale(to.clamp(0.05, 4.0));
self
}
#[must_use]
pub fn with_hide_on_complete(mut self, hide: bool) -> Self {
self.set_hide_on_complete(hide);
self
}
#[must_use]
pub fn route_enter(node: NodeId) -> Self {
Self {
node,
elapsed: 0.0,
delay: 0.0,
duration: 0.24,
easing: Easing::EaseOutQuint,
from_opacity: 0.0,
to_opacity: 1.0,
from_tx: 0.0,
to_tx: 0.0,
from_ty: 14.0,
to_ty: 0.0,
from_scale: 0.992,
to_scale: 1.0,
hide_on_complete: false,
}
}
#[must_use]
pub fn fade(node: NodeId, from: f32, to: f32, duration: f32, hide_on_complete: bool) -> Self {
Self {
node,
elapsed: 0.0,
delay: 0.0,
duration: duration.max(0.016),
easing: Easing::EaseOutCubic,
from_opacity: from,
to_opacity: to,
from_tx: 0.0,
to_tx: 0.0,
from_ty: 0.0,
to_ty: 0.0,
from_scale: 1.0,
to_scale: 1.0,
hide_on_complete,
}
}
#[must_use]
pub fn slide_fade(
node: NodeId,
from_offset: Point,
to_offset: Point,
from_opacity: f32,
to_opacity: f32,
duration: f32,
hide_on_complete: bool,
) -> Self {
Self {
node,
elapsed: 0.0,
delay: 0.0,
duration: duration.max(0.016),
easing: Easing::EaseOutCubic,
from_opacity,
to_opacity,
from_tx: from_offset.get_x(),
to_tx: to_offset.get_x(),
from_ty: from_offset.get_y(),
to_ty: to_offset.get_y(),
from_scale: 1.0,
to_scale: 1.0,
hide_on_complete,
}
}
#[must_use]
pub fn modal_enter(node: NodeId) -> Self {
Self {
node,
elapsed: 0.0,
delay: 0.0,
duration: 0.28,
easing: Easing::SpringSnappy,
from_opacity: 0.0,
to_opacity: 1.0,
from_tx: 0.0,
to_tx: 0.0,
from_ty: 18.0,
to_ty: 0.0,
from_scale: 0.93,
to_scale: 1.0,
hide_on_complete: false,
}
}
#[must_use]
pub fn modal_exit(node: NodeId) -> Self {
Self {
node,
elapsed: 0.0,
delay: 0.0,
duration: 0.18,
easing: Easing::EaseOutCubic,
from_opacity: 1.0,
to_opacity: 0.0,
from_tx: 0.0,
to_tx: 0.0,
from_ty: 0.0,
to_ty: 10.0,
from_scale: 1.0,
to_scale: 0.96,
hide_on_complete: false,
}
}
#[must_use]
pub fn sample(&self) -> (f32, Transform2D) {
let active_elapsed: f32 = (self.get_elapsed() - self.get_delay()).max(0.0);
let raw_t: f32 = (active_elapsed / self.get_duration().max(0.001)).clamp(0.0, 1.0);
let k: f32 = self.get_easing().eval(raw_t);
let op: f32 = (self.get_from_opacity()
+ (self.get_to_opacity() - self.get_from_opacity()) * k)
.clamp(0.0, 1.0);
let tx: f32 = self.get_from_tx() + (self.get_to_tx() - self.get_from_tx()) * k;
let ty: f32 = self.get_from_ty() + (self.get_to_ty() - self.get_from_ty()) * k;
let sc: f32 = (self.get_from_scale() + (self.get_to_scale() - self.get_from_scale()) * k)
.clamp(0.05, 4.0);
(op, Transform2D::new(tx, ty, sc, sc, 0.0))
}
#[must_use]
pub fn is_complete(&self) -> bool {
self.get_elapsed() >= self.get_delay() + self.get_duration()
}
}