use std::ops::{Add, Mul};
use crate::{
core::Duration,
vlm::{Quat, Vec3},
};
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum AnimInterpolation {
Step,
#[default]
Linear,
CubicSpline,
}
mod sealed {
pub trait Sealed {}
impl Sealed for f32 {}
impl Sealed for crate::vlm::Vec3 {}
impl Sealed for crate::vlm::Quat {}
}
pub trait AnimCurveValue:
Copy + Default + Add<Output = Self> + Mul<f32, Output = Self> + sealed::Sealed
{
fn curve_zero() -> Self;
fn curve_is_finite(self) -> bool;
fn curve_is_valid(self) -> bool;
fn curve_normalize(self) -> Self;
fn curve_lerp(self, other: Self, weight: f32) -> Self;
}
impl AnimCurveValue for f32 {
fn curve_zero() -> Self {
0.0
}
fn curve_is_finite(self) -> bool {
self.is_finite()
}
fn curve_is_valid(self) -> bool {
self.is_finite()
}
fn curve_normalize(self) -> Self {
self
}
fn curve_lerp(self, other: Self, weight: f32) -> Self {
self * (1.0 - weight) + other * weight
}
}
impl AnimCurveValue for Vec3 {
fn curve_zero() -> Self {
Self::default()
}
fn curve_is_finite(self) -> bool {
self.is_finite()
}
fn curve_is_valid(self) -> bool {
self.is_finite()
}
fn curve_normalize(self) -> Self {
self
}
fn curve_lerp(self, other: Self, weight: f32) -> Self {
self.lerp(other, weight)
}
}
impl AnimCurveValue for Quat {
fn curve_zero() -> Self {
Self {
x: 0.0,
y: 0.0,
z: 0.0,
w: 0.0,
}
}
fn curve_is_finite(self) -> bool {
self.is_finite()
}
fn curve_is_valid(self) -> bool {
self.is_finite()
&& self
.x
.abs()
.max(self.y.abs())
.max(self.z.abs())
.max(self.w.abs())
> 0.0
}
fn curve_normalize(self) -> Self {
self.try_normalized().unwrap_or(self)
}
fn curve_lerp(self, other: Self, weight: f32) -> Self {
self.try_slerp(other, weight).unwrap_or(self)
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Keyframe<T: AnimCurveValue> {
pub time: Duration,
pub value: T,
pub in_tangent: T,
pub out_tangent: T,
}
impl<T: AnimCurveValue> Default for Keyframe<T> {
fn default() -> Self {
Self {
time: Duration::ZERO,
value: T::default(),
in_tangent: T::curve_zero(),
out_tangent: T::curve_zero(),
}
}
}