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    /// at the given time.
95    pub fn go(mut self, new_state: T, at: Instant) -> Self {
96        self.go_mut(new_state, at);
97        self
98    }
99
100    /// Transitions the [`Animation`] from its current state to the given new state
101    /// at the given time, by reference.
102    pub fn go_mut(&mut self, new_state: T, at: Instant) {
103        self.raw.transition(new_state, at);
104    }
105
106    /// Returns true if the [`Animation`] is currently in progress.
107    ///
108    /// An [`Animation`] is in progress when it is transitioning to a different state.
109    pub fn is_animating(&self, at: Instant) -> bool {
110        self.raw.in_progress(at)
111    }
112
113    /// Projects the [`Animation`] into an interpolated value at the given [`Instant`]; using the
114    /// closure provided to calculate the different keyframes of interpolated values.
115    ///
116    /// If the [`Animation`] state is a `bool`, you can use the simpler [`interpolate`] method.
117    ///
118    /// [`interpolate`]: Animation::interpolate
119    pub fn interpolate_with<I>(&self, f: impl Fn(T) -> I, at: Instant) -> I
120    where
121        I: Interpolable,
122    {
123        self.raw.animate(f, at)
124    }
125
126    /// Retuns the current state of the [`Animation`].
127    pub fn value(&self) -> T {
128        self.raw.value
129    }
130}
131
132impl Animation<bool> {
133    /// Projects the [`Animation`] into an interpolated value at the given [`Instant`]; using the
134    /// `start` and `end` values as the origin and destination keyframes.
135    pub fn interpolate<I>(&self, start: I, end: I, at: Instant) -> I
136    where
137        I: Interpolable + Clone,
138    {
139        self.raw.animate_bool(start, end, at)
140    }
141
142    /// Returns the remaining [`Duration`] of the [`Animation`].
143    pub fn remaining(&self, at: Instant) -> Duration {
144        Duration::from_secs_f32(self.interpolate(
145            self.duration.as_secs_f32(),
146            0.0,
147            at,
148        ))
149    }
150}