#![doc = include_str!(".crate-docs.md")]
use std::fmt::Debug;
use std::sync::Arc;
use easings::Linear;
pub mod easings;
#[derive(Debug, Clone, PartialEq)]
pub struct EasingFunction(EasingKind);
impl EasingFunction {
pub fn from_fn(func: fn(f32) -> f32) -> Self {
Self(EasingKind::Fn(func))
}
pub fn new<Easing>(easing: Easing) -> Self
where
Easing: crate::Easing + Debug + Clone,
{
Self(EasingKind::Custom(Arc::new(easing)))
}
}
impl Easing for EasingFunction {
fn ease(&self, progress: f32) -> f32 {
self.0.ease(progress)
}
}
impl Default for EasingFunction {
fn default() -> Self {
Self::from(Linear)
}
}
#[derive(Debug, Clone)]
enum EasingKind {
Fn(fn(f32) -> f32),
Custom(Arc<dyn Easing>),
}
impl Easing for EasingKind {
fn ease(&self, progress: f32) -> f32 {
match self {
Self::Fn(func) => func(progress),
Self::Custom(func) => func.ease(progress),
}
}
}
impl PartialEq for EasingKind {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Self::Fn(l0), Self::Fn(r0)) => l0 == r0,
(Self::Custom(l0), Self::Custom(r0)) => std::ptr::eq(&**l0, &**r0),
_ => false,
}
}
}
pub trait Easing: Debug + Send + Sync + 'static {
fn ease(&self, progress: f32) -> f32;
}
impl<T> Easing for T
where
T: Fn(f32) -> f32 + Debug + Send + Sync + 'static,
{
fn ease(&self, progress: f32) -> f32 {
self(progress)
}
}