use crate::animated::DEFAULT_DURATION;
use super::Curve;
use std::time::Duration;
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Easing {
pub curve: Curve,
pub duration: Duration,
pub reversible: bool,
}
impl Default for Easing {
fn default() -> Self {
Self {
curve: Curve::default(),
duration: DEFAULT_DURATION,
reversible: false,
}
}
}
impl Easing {
pub const LINEAR: Self = Self {
curve: Curve::Linear,
duration: DEFAULT_DURATION,
reversible: false,
};
pub const EASE: Self = Self {
curve: Curve::Ease,
duration: DEFAULT_DURATION,
reversible: false,
};
pub const EASE_IN: Self = Self {
curve: Curve::EaseIn,
duration: DEFAULT_DURATION,
reversible: false,
};
pub const EASE_OUT: Self = Self {
curve: Curve::EaseOut,
duration: DEFAULT_DURATION,
reversible: false,
};
pub const EASE_IN_OUT: Self = Self {
curve: Curve::EaseInOut,
duration: DEFAULT_DURATION,
reversible: false,
};
pub fn new(curve: Curve) -> Self {
Self {
curve,
duration: DEFAULT_DURATION,
reversible: false,
}
}
pub fn with_curve(mut self, curve: Curve) -> Self {
self.curve = curve;
self
}
pub fn with_duration(mut self, duration: Duration) -> Self {
self.duration = duration;
self
}
pub fn very_quick(self) -> Self {
self.with_duration(Duration::from_millis(100))
}
pub fn quick(self) -> Self {
self.with_duration(Duration::from_millis(200))
}
pub fn slow(self) -> Self {
self.with_duration(Duration::from_millis(400))
}
pub fn very_slow(self) -> Self {
self.with_duration(Duration::from_millis(500))
}
pub fn reversible(mut self, reversible: bool) -> Self {
self.reversible = reversible;
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn const_easings() {
assert_eq!(Easing::LINEAR.curve, Curve::Linear);
assert_eq!(Easing::EASE.curve, Curve::Ease);
assert_eq!(Easing::EASE_IN.curve, Curve::EaseIn);
assert_eq!(Easing::EASE_OUT.curve, Curve::EaseOut);
assert_eq!(Easing::EASE_IN_OUT.curve, Curve::EaseInOut);
}
#[test]
fn default() {
let easing = Easing::default();
assert_eq!(easing.curve, Curve::default());
assert_eq!(easing.duration, DEFAULT_DURATION);
assert!(!easing.reversible);
}
#[test]
fn initialization() {
let easing = Easing::new(Curve::EaseInOut)
.with_duration(Duration::from_millis(300))
.reversible(true);
assert_eq!(easing.curve, Curve::EaseInOut);
assert_eq!(easing.duration, Duration::from_millis(300));
assert!(easing.reversible);
}
}