ranim_anims/
fading.rs

1use ranim_core::{
2    animation::{AnimationCell, Eval},
3    traits::{Interpolatable, Opacity},
4    utils::rate_functions::smooth,
5};
6
7// MARK: Require Trait
8/// The requirement of [`FadeIn`] and [`FadeOut`]
9pub trait FadingRequirement: Opacity + Interpolatable + Clone {}
10impl<T: Opacity + Interpolatable + Clone> FadingRequirement for T {}
11
12// MARK: Anim Trait
13/// The methods to create animations for `T` that satisfies [`FadingRequirement`]
14pub trait FadingAnim: FadingRequirement + Sized + 'static {
15    /// Create a [`FadeIn`] anim.
16    fn fade_in(&mut self) -> AnimationCell<Self>;
17    /// Create a [`FadeOut`] anim.
18    fn fade_out(&mut self) -> AnimationCell<Self>;
19}
20
21impl<T: FadingRequirement + Sized + 'static> FadingAnim for T {
22    fn fade_in(&mut self) -> AnimationCell<Self> {
23        FadeIn::new(self.clone())
24            .into_animation_cell()
25            .with_rate_func(smooth)
26            .apply_to(self)
27    }
28    fn fade_out(&mut self) -> AnimationCell<Self> {
29        FadeOut::new(self.clone())
30            .into_animation_cell()
31            .with_rate_func(smooth)
32            .apply_to(self)
33    }
34}
35
36// MARK: Impl
37
38/// Fade-in animation.
39///
40/// Because some Items may not be completly opaque, so
41/// this is implemented by setting the opacity to 0.0 as
42/// initial state, then interpolate between them.
43pub struct FadeIn<T: FadingRequirement> {
44    src: T,
45    dst: T,
46}
47
48impl<T: FadingRequirement> FadeIn<T> {
49    /// Constructor
50    pub fn new(target: T) -> Self {
51        let mut src = target.clone();
52        let dst = target.clone();
53        src.set_opacity(0.0);
54        Self { src, dst }
55    }
56}
57
58impl<T: FadingRequirement> Eval<T> for FadeIn<T> {
59    fn eval_alpha(&self, alpha: f64) -> T {
60        self.src.lerp(&self.dst, alpha)
61    }
62}
63
64/// Fade-out animation.
65///
66/// Because some Items may not be completly opaque, so
67/// this is implemented by setting the opacity to 0.0 as
68/// target state, then interpolate between them.
69pub struct FadeOut<T: FadingRequirement> {
70    src: T,
71    dst: T,
72}
73
74impl<T: FadingRequirement> FadeOut<T> {
75    /// Constructor
76    pub fn new(target: T) -> Self {
77        let src = target.clone();
78        let mut dst = target.clone();
79        dst.set_opacity(0.0);
80        Self { src, dst }
81    }
82}
83
84impl<T: FadingRequirement> Eval<T> for FadeOut<T> {
85    fn eval_alpha(&self, alpha: f64) -> T {
86        self.src.lerp(&self.dst, alpha)
87    }
88}