use std::{fmt, time::Duration};
use serde::{Deserialize, Serialize};
#[derive(Debug, Copy, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", rename_all_fields = "camelCase")]
pub enum Animation {
Default {},
Curve { curve: AnimationCurve, duration_seconds: Option<f64> },
}
#[derive(Debug, Copy, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", rename_all_fields = "camelCase")]
pub enum AnimationCurve {
Linear,
EaseIn,
EaseOut,
EaseInOut,
}
impl Animation {
pub const LINEAR: Self = Self::Curve { curve: AnimationCurve::Linear, duration_seconds: None };
pub const EASE_IN: Self = Self::Curve { curve: AnimationCurve::EaseIn, duration_seconds: None };
pub const EASE_OUT: Self = Self::Curve { curve: AnimationCurve::EaseOut, duration_seconds: None };
pub const EASE_IN_OUT: Self = Self::Curve { curve: AnimationCurve::EaseInOut, duration_seconds: None };
pub fn curve(curve: AnimationCurve, duration: Option<Duration>) -> Self {
Self::Curve { curve, duration_seconds: duration.map(|d| d.as_secs_f64()) }
}
pub fn linear(duration: Duration) -> Self {
Self::curve(AnimationCurve::Linear, Some(duration))
}
pub fn ease_in(duration: Duration) -> Self {
Self::curve(AnimationCurve::EaseIn, Some(duration))
}
pub fn ease_out(duration: Duration) -> Self {
Self::curve(AnimationCurve::EaseOut, Some(duration))
}
pub fn ease_in_out(duration: Duration) -> Self {
Self::curve(AnimationCurve::EaseInOut, Some(duration))
}
}
impl Default for Animation {
fn default() -> Self {
Self::Default {}
}
}
impl fmt::Display for Animation {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Default {} => write!(f, "Default")?,
Self::Curve { curve, duration_seconds } => {
write!(f, "{}", curve)?;
if let Some(duration_seconds) = duration_seconds {
write!(f, " ({}s)", duration_seconds)?;
}
},
}
Ok(())
}
}
impl fmt::Display for AnimationCurve {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
AnimationCurve::Linear => write!(f, "Linear"),
AnimationCurve::EaseIn => write!(f, "Ease in"),
AnimationCurve::EaseOut => write!(f, "Ease out"),
AnimationCurve::EaseInOut => write!(f, "Ease in/out"),
}
}
}