use super::super::easing::Easing;
use super::super::error::{MotionError, MotionErrorCode};
use super::super::interpolate::scalar::lerp_scalar;
#[derive(Clone, Copy, Debug, PartialEq)]
#[non_exhaustive]
pub struct AnimValue(f32);
impl AnimValue {
#[must_use]
pub const fn new(value: f32) -> Self {
Self(value)
}
#[must_use]
pub const fn value(self) -> f32 {
self.0
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct TweenComponent {
from: AnimValue,
to: AnimValue,
duration_ms: f32,
start_ms: f32,
easing: Easing,
}
impl TweenComponent {
pub fn new(
from: f32,
to: f32,
start_ms: f32,
duration_ms: f32,
easing: Easing,
) -> Result<Self, MotionError> {
MotionError::require(
duration_ms.is_finite() && duration_ms > 0.0,
MotionErrorCode::PatchInvalid,
"duration_ms must be a finite positive number",
)?;
MotionError::require(
from.is_finite(),
MotionErrorCode::PatchInvalid,
"from must be a finite number",
)?;
MotionError::require(
to.is_finite(),
MotionErrorCode::PatchInvalid,
"to must be a finite number",
)?;
MotionError::require(
start_ms.is_finite(),
MotionErrorCode::PatchInvalid,
"start_ms must be a finite number",
)?;
Ok(Self {
from: AnimValue::new(from),
to: AnimValue::new(to),
duration_ms,
start_ms,
easing,
})
}
#[must_use]
pub const fn from_value(&self) -> f32 {
self.from.0
}
#[must_use]
pub const fn to_value(&self) -> f32 {
self.to.0
}
#[must_use]
pub const fn start_ms(&self) -> f32 {
self.start_ms
}
#[must_use]
pub const fn duration_ms(&self) -> f32 {
self.duration_ms
}
#[must_use]
pub const fn easing(&self) -> &Easing {
&self.easing
}
#[must_use]
pub fn value_at_wall_ms(&self, wall_ms: f32) -> f32 {
let local = (wall_ms - self.start_ms) / self.duration_ms;
self.value_at(local.clamp(0.0, 1.0))
}
#[must_use]
pub fn value_at(&self, mix: f32) -> f32 {
let eased = self.easing.value_at(mix);
lerp_scalar(self.from.0, self.to.0, eased)
}
}