iced_core/
animation.rs

1//! Animate your applications.
2use crate::time::{Duration, Instant};
3
4pub use lilt::{Easing, FloatRepresentable as Float, Interpolable};
5
6/// The animation of some particular state.
7///
8/// It tracks state changes and allows projecting interpolated values
9/// through time.
10#[derive(Debug, Clone)]
11pub struct Animation<T>
12where
13    T: Clone + Copy + PartialEq + Float,
14{
15    raw: lilt::Animated<T, Instant>,
16    duration: Duration, // TODO: Expose duration getter in `lilt`
17}
18
19impl<T> Animation<T>
20where
21    T: Clone + Copy + PartialEq + Float,
22{
23    /// Creates a new [`Animation`] with the given initial state.
24    pub fn new(state: T) -> Self {
25        Self {
26            raw: lilt::Animated::new(state),
27            duration: Duration::from_millis(100),
28        }
29    }
30
31    /// Sets the [`Easing`] function of the [`Animation`].
32    ///
33    /// See the [Easing Functions Cheat Sheet](https://easings.net) for
34    /// details!
35    pub fn easing(mut self, easing: Easing) -> Self {
36        self.raw = self.raw.easing(easing);
37        self
38    }
39
40    /// Sets the duration of the [`Animation`] to 100ms.
41    pub fn very_quick(self) -> Self {
42        self.duration(Duration::from_millis(100))
43    }
44
45    /// Sets the duration of the [`Animation`] to 200ms.
46    pub fn quick(self) -> Self {
47        self.duration(Duration::from_millis(200))
48    }
49
50    /// Sets the duration of the [`Animation`] to 400ms.
51    pub fn slow(self) -> Self {
52        self.duration(Duration::from_millis(400))
53    }
54
55    /// Sets the duration of the [`Animation`] to 500ms.
56    pub fn very_slow(self) -> Self {
57        self.duration(Duration::from_millis(500))
58    }
59
60    /// Sets the duration of the [`Animation`] to the given value.
61    pub fn duration(mut self, duration: Duration) -> Self {
62        self.raw = self.raw.duration(duration.as_secs_f32() * 1_000.0);
63        self.duration = duration;
64        self
65    }
66
67    /// Sets a delay for the [`Animation`].
68    pub fn delay(mut self, duration: Duration) -> Self {
69        self.raw = self.raw.delay(duration.as_secs_f64() as f32 * 1000.0);
70        self
71    }
72
73    /// Makes the [`Animation`] repeat a given amount of times.
74    ///
75    /// Providing 1 repetition plays the animation twice in total.
76    pub fn repeat(mut self, repetitions: u32) -> Self {
77        self.raw = self.raw.repeat(repetitions);
78        self
79    }
80
81    /// Makes the [`Animation`] repeat forever.
82    pub fn repeat_forever(mut self) -> Self {
83        self.raw = self.raw.repeat_forever();
84        self
85    }
86
87    /// Makes the [`Animation`] automatically reverse when repeating.
88    pub fn auto_reverse(mut self) -> Self {
89        self.raw = self.raw.auto_reverse();
90        self
91    }
92
93    /// Transitions the [`Animation`] from its current state to the given new state.
94    pub fn go(mut self, new_state: T) -> Self {
95        self.go_mut(new_state);
96        self
97    }
98
99    /// Transitions the [`Animation`] from its current state to the given new state, by reference.
100    pub fn go_mut(&mut self, new_state: T) {
101        self.raw.transition(new_state, Instant::now());
102    }
103
104    /// Returns true if the [`Animation`] is currently in progress.
105    ///
106    /// An [`Animation`] is in progress when it is transitioning to a different state.
107    pub fn is_animating(&self, at: Instant) -> bool {
108        self.raw.in_progress(at)
109    }
110
111    /// Projects the [`Animation`] into an interpolated value at the given [`Instant`]; using the
112    /// closure provided to calculate the different keyframes of interpolated values.
113    ///
114    /// If the [`Animation`] state is a `bool`, you can use the simpler [`interpolate`] method.
115    ///
116    /// [`interpolate`]: Animation::interpolate
117    pub fn interpolate_with<I>(&self, f: impl Fn(T) -> I, at: Instant) -> I
118    where
119        I: Interpolable,
120    {
121        self.raw.animate(f, at)
122    }
123
124    /// Retuns the current state of the [`Animation`].
125    pub fn value(&self) -> T {
126        self.raw.value
127    }
128}
129
130impl Animation<bool> {
131    /// Projects the [`Animation`] into an interpolated value at the given [`Instant`]; using the
132    /// `start` and `end` values as the origin and destination keyframes.
133    pub fn interpolate<I>(&self, start: I, end: I, at: Instant) -> I
134    where
135        I: Interpolable + Clone,
136    {
137        self.raw.animate_bool(start, end, at)
138    }
139
140    /// Returns the remaining [`Duration`] of the [`Animation`].
141    pub fn remaining(&self, at: Instant) -> Duration {
142        Duration::from_secs_f32(self.interpolate(
143            self.duration.as_secs_f32(),
144            0.0,
145            at,
146        ))
147    }
148}