use ranim_core::animation::{AnimationCell, Eval};
pub trait FuncRequirement: Clone {}
impl<T: Clone> FuncRequirement for T {}
pub trait FuncAnim: FuncRequirement + 'static {
fn func(&mut self, f: impl Fn(&Self, f64) -> Self + 'static) -> AnimationCell<Self>;
}
impl<T: FuncRequirement + 'static> FuncAnim for T {
fn func(&mut self, f: impl Fn(&Self, f64) -> Self + 'static) -> AnimationCell<Self> {
Func::new(self.clone(), f)
.into_animation_cell()
.apply_to(self)
}
}
pub struct Func<T: FuncRequirement> {
src: T,
#[allow(clippy::type_complexity)]
f: Box<dyn Fn(&T, f64) -> T>,
}
impl<T: FuncRequirement> Func<T> {
pub fn new(target: T, f: impl Fn(&T, f64) -> T + 'static) -> Self {
Self {
src: target,
f: Box::new(f),
}
}
}
impl<T: FuncRequirement> Eval<T> for Func<T> {
fn eval_alpha(&self, alpha: f64) -> T {
(self.f)(&self.src, alpha)
}
}