use crate::*;
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Animation {
Tween(Tween),
Spring(Spring),
}
impl Default for Animation {
fn default() -> Self {
Self::Tween(Tween::default())
}
}
impl From<Tween> for Animation {
fn from(tween: Tween) -> Self {
Self::Tween(tween)
}
}
impl From<Spring> for Animation {
fn from(spring: Spring) -> Self {
Self::Spring(spring)
}
}
builder! {
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct AnimationOptions {
new(),
#[into]
initial: Option<f32> = None,
restart: bool = false,
#[into]
velocity: Option<f32> = None,
delay: f32 = 0.0,
}
}
builder! {
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct Tween {
new(
duration: f32,
easing: Easing,
),
}
}
builder! {
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Spring {
new(
duration: f32,
bounce: f32,
),
rest_distance: f32 = 0.001,
rest_speed: f32 = 0.001,
}
}
impl Spring {
pub const fn rest(mut self, distance: f32, speed: f32) -> Self {
self.rest_distance = distance;
self.rest_speed = speed;
self
}
}
builder! {
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Motion {
new(#[into] animation: Animation),
properties: MotionProps = MotionProps::NONE,
restart: bool = false,
delay: f32 = 0.0,
#[into]
lift: Option<Id> = None,
}
}
impl Motion {
pub fn resize(mut self) -> Self {
self.properties |= MotionProps::RESIZE;
self
}
pub fn x(mut self) -> Self {
self.properties |= MotionProps::X;
self
}
pub fn y(mut self) -> Self {
self.properties |= MotionProps::Y;
self
}
pub fn width(mut self) -> Self {
self.properties |= MotionProps::WIDTH;
self
}
pub fn height(mut self) -> Self {
self.properties |= MotionProps::HEIGHT;
self
}
pub fn position(mut self) -> Self {
self.properties |= MotionProps::POSITION;
self
}
pub fn size(mut self) -> Self {
self.properties |= MotionProps::SIZE;
self
}
pub fn layout(mut self) -> Self {
self.properties |= MotionProps::LAYOUT;
self
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct MotionProps(u8);
impl MotionProps {
pub const NONE: Self = Self(0);
pub const X: Self = Self(1 << 0);
pub const Y: Self = Self(1 << 1);
pub const WIDTH: Self = Self(1 << 2);
pub const HEIGHT: Self = Self(1 << 3);
pub const RESIZE: Self = Self(1 << 4);
pub const POSITION: Self = Self(Self::X.0 | Self::Y.0);
pub const SIZE: Self = Self(Self::WIDTH.0 | Self::HEIGHT.0);
pub const LAYOUT: Self = Self(Self::POSITION.0 | Self::SIZE.0);
pub const fn intersects(self, other: Self) -> bool {
self.0 & other.0 != 0
}
pub const fn intersection(self, other: Self) -> Self {
Self(self.0 & other.0)
}
pub const fn is_empty(self) -> bool {
self.0 == 0
}
}
impl BitOr for MotionProps {
type Output = Self;
fn bitor(self, other: Self) -> Self {
Self(self.0 | other.0)
}
}
impl BitOrAssign for MotionProps {
fn bitor_assign(&mut self, other: Self) {
self.0 |= other.0;
}
}
#[derive(Clone, Copy, Debug)]
pub enum Easing {
Bezier([f32; 4]),
Function(fn(f32) -> (f32, f32)),
}
impl Easing {
pub const LINEAR: Self = Self::Bezier([0.0, 0.0, 1.0, 1.0]);
pub const EASE_IN_QUAD: Self = Self::Bezier([1.0 / 3.0, 0.0, 2.0 / 3.0, 1.0 / 3.0]);
pub const EASE_OUT_QUAD: Self = Self::Bezier([1.0 / 3.0, 2.0 / 3.0, 2.0 / 3.0, 1.0]);
pub const EASE: Self = Self::Bezier([0.25, 0.1, 0.25, 1.0]);
pub const EASE_IN: Self = Self::Bezier([0.42, 0.0, 1.0, 1.0]);
pub const EASE_OUT: Self = Self::Bezier([0.0, 0.0, 0.58, 1.0]);
pub const EASE_IN_OUT: Self = Self::Bezier([0.42, 0.0, 0.58, 1.0]);
pub const EASE_IN_CUBIC: Self = Self::Bezier([0.32, 0.0, 0.67, 0.0]);
pub const EASE_OUT_CUBIC: Self = Self::Bezier([0.33, 1.0, 0.68, 1.0]);
pub const EASE_IN_OUT_CUBIC: Self = Self::Bezier([0.65, 0.0, 0.35, 1.0]);
pub const EASE_OUT_EXPO: Self = Self::Bezier([0.16, 1.0, 0.3, 1.0]);
#[inline]
pub fn sample(self, value: f32) -> (f32, f32) {
const MAX_SLOPE: f64 = 1000.0;
let [x1, y1, x2, y2] = match self {
Self::Bezier(points) => points,
Self::Function(sample) => return sample(value),
};
if x1 == y1 && x2 == y2 {
return (value, 1.0);
}
let [y1, y2] = [y1, y2].map(f64::from);
let value = value as f64;
let cy = 3.0 * y1;
let by = 3.0 * (y2 - y1) - cy;
let ay = 1.0 - cy - by;
if x1 == 1.0 / 3.0 && x2 == 2.0 / 3.0 {
let amount = if value <= 0.0 {
0.0
} else if value >= 1.0 {
1.0
} else {
((ay * value + by) * value + cy) * value
};
let slope = ((3.0 * ay * value + 2.0 * by) * value + cy).clamp(-MAX_SLOPE, MAX_SLOPE);
return (amount as f32, slope as f32);
}
let [x1, x2] = [x1, x2].map(f64::from);
let cx = 3.0 * x1;
let bx = 3.0 * (x2 - x1) - cx;
let ax = 1.0 - cx - bx;
let mut low = 0.0;
let mut high = 1.0;
let mut t = value.clamp(0.0, 1.0);
for _ in 0..32 {
let error = ((ax * t + bx) * t + cx) * t - value;
let derivative = (3.0 * ax * t + 2.0 * bx) * t + cx;
if error.abs() <= derivative.abs() * 1e-10 {
break;
}
if error < 0.0 {
low = t;
} else {
high = t;
}
let next = t - error / derivative;
t = if next > low && next < high {
next
} else {
(low + high) * 0.5
};
}
let dx = (3.0 * ax * t + 2.0 * bx) * t + cx;
let dy = (3.0 * ay * t + 2.0 * by) * t + cy;
let slope = if dx > 0.0 {
dy / dx
} else if t == 0.0 && y1 == 0.0 && x2 > 0.0 {
y2 / x2
} else if t == 1.0 && y2 == 1.0 && x1 < 1.0 {
(1.0 - y1) / (1.0 - x1)
} else if dy == 0.0 {
0.0
} else {
dy.signum() * MAX_SLOPE
}
.clamp(-MAX_SLOPE, MAX_SLOPE);
let amount = if value <= 0.0 {
0.0
} else if value >= 1.0 {
1.0
} else {
((ay * t + by) * t + cy) * t
};
(amount as f32, slope as f32)
}
}
impl PartialEq for Easing {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Self::Bezier(a), Self::Bezier(b)) => a == b,
(Self::Function(a), Self::Function(b)) => core::ptr::fn_addr_eq(*a, *b),
_ => false,
}
}
}
impl Default for Easing {
fn default() -> Self {
Self::LINEAR
}
}
pub struct AnimationState {
pub call: Call,
pub playback: Playback,
pub seen: bool,
}
impl AnimationState {
pub fn update(animations: &mut Vec<Self>, mut call: Call, initial: f32) -> &mut Self {
let index = loop {
match animations.binary_search_by_key(&call, |animation| animation.call) {
Ok(index) if animations[index].seen => call.repeat += 1,
Ok(index) => break index,
Err(index) => {
animations.insert(
index,
Self {
call,
playback: Playback::Target(Animator::new([initial], Animation::default())),
seen: false,
},
);
break index;
}
}
};
let animation = &mut animations[index];
animation.seen = true;
animation
}
pub fn is_active(&self) -> bool {
match &self.playback {
Playback::Target(animator) => animator.is_active(),
Playback::Loop { .. } => true,
}
}
}
pub enum Playback {
Target(Animator<f32, 1>),
Loop { tween: Tween, time: f64, value: f32 },
}
pub struct MotionState<T: Scalar> {
pub id: Id,
pub animator: Animator<T, 4>,
pub properties: MotionProps,
pub node: NodeId,
pub initialized: bool,
pub seen: bool,
pub flying: bool,
}
impl<T: Scalar> MotionState<T> {
pub fn new(id: Id, node: NodeId) -> Self {
Self {
id,
animator: Animator::new([T::ZERO; 4], Animation::default()),
properties: MotionProps::NONE,
node,
initialized: false,
seen: true,
flying: false,
}
}
pub fn is_active(&self) -> bool {
self.animator.is_active()
}
pub fn advance(
&mut self,
target: Rect<T>,
config: Motion,
now: Duration,
resized: bool,
commit: bool,
) -> ([T; 4], MotionProps) {
let targets = [target.x, target.y, target.width, target.height];
let options = AnimationOptions {
initial: None,
restart: config.restart,
velocity: None,
delay: config.delay,
};
if !self.initialized || (resized && !config.properties.intersects(MotionProps::RESIZE)) {
if commit {
self.animator = Animator::new(targets, config.animation);
self.initialized = true;
}
return (targets, MotionProps::NONE);
}
let properties = [MotionProps::X, MotionProps::Y, MotionProps::WIDTH, MotionProps::HEIGHT];
let (mut values, active) = self.animator.advance(
targets,
properties.map(|p| config.properties.intersects(p)),
config.animation,
options,
now,
commit,
);
for (i, value) in values.iter_mut().enumerate().skip(2) {
if *value < T::ZERO {
*value = T::ZERO;
if commit {
self.animator.clamp(i, T::ZERO, T::UNBOUNDED, now);
}
}
}
let mut animated = MotionProps::NONE;
for (i, property) in properties.into_iter().enumerate() {
if active[i] {
animated |= property;
}
}
(values, animated)
}
}