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