Skip to main content

euv_chart/animation/
impl.rs

1use super::*;
2
3/// Inherent implementation of [`Animator`].
4impl Animator {
5    /// Returns an animator with the default settings.
6    pub fn new() -> Self {
7        Self {
8            from: Vec::new(),
9            to: Vec::new(),
10            current: Vec::new(),
11            tween: Tween::create(0.0, 1.0, 0.0),
12            duration: DEFAULT_ANIMATION_DURATION,
13            delay: 0.0,
14            initial: None,
15        }
16    }
17
18    /// Returns the duration of one animation, in milliseconds.
19    ///
20    /// # Returns
21    ///
22    /// - `f64` - A 64-bit float.
23    pub fn duration(&self) -> f64 {
24        self.get_duration()
25    }
26
27    /// Sets the duration of one animation, in milliseconds.
28    ///
29    /// A duration of zero turns the animation off, which is what a
30    /// server-side render or a benchmark wants. The new duration
31    /// applies to the animation that starts next.
32    ///
33    /// # Arguments
34    ///
35    /// - `f64` - A 64-bit float (`f64`).
36    ///
37    /// # Returns
38    ///
39    /// - `&mut Self` - A `mut Self` value.
40    pub fn set_duration(&mut self, duration: f64) -> &mut Self {
41        *self.get_mut_duration() = duration.max(0.0);
42        self
43    }
44
45    /// Returns the delay before the animation starts, in milliseconds.
46    ///
47    /// # Returns
48    ///
49    /// - `f64` - A 64-bit float.
50    pub fn delay(&self) -> f64 {
51        self.get_delay()
52    }
53
54    /// Sets the delay before the animation starts, in milliseconds.
55    ///
56    /// # Arguments
57    ///
58    /// - `f64` - A 64-bit float (`f64`).
59    ///
60    /// # Returns
61    ///
62    /// - `&mut Self` - A `mut Self` value.
63    pub fn set_delay(&mut self, delay: f64) -> &mut Self {
64        let value: f64 = delay.max(0.0);
65        *self.get_mut_delay() = value;
66        self.get_mut_tween()
67            .set_delay(value / MILLISECONDS_PER_SECOND);
68        self
69    }
70
71    /// Returns the easing of the animation.
72    ///
73    /// # Returns
74    ///
75    /// - `Easing` - A `Easing` value.
76    pub fn easing(&self) -> Easing {
77        self.get_tween().get_easing()
78    }
79
80    /// Sets the easing of the animation.
81    ///
82    /// # Arguments
83    ///
84    /// - `Easing` - A `Easing` parameter.
85    ///
86    /// # Returns
87    ///
88    /// - `&mut Self` - A `mut Self` value.
89    pub fn set_easing(&mut self, easing: Easing) -> &mut Self {
90        self.get_mut_tween().set_easing(easing);
91        self
92    }
93
94    /// Returns `true` when an animation is running.
95    ///
96    /// # Returns
97    ///
98    /// - `bool` - A boolean.
99    pub fn is_running(&self) -> bool {
100        !self.get_tween().is_finished()
101    }
102
103    /// Pauses the animation.
104    ///
105    /// The values stay where they are until the animation is resumed.
106    ///
107    /// # Returns
108    ///
109    /// - `&mut Self` - A `mut Self` value.
110    pub fn pause(&mut self) -> &mut Self {
111        self.get_mut_tween().pause();
112        self
113    }
114
115    /// Resumes a paused animation.
116    ///
117    /// # Returns
118    ///
119    /// - `&mut Self` - A `mut Self` value.
120    pub fn resume(&mut self) -> &mut Self {
121        self.get_mut_tween().resume();
122        self
123    }
124
125    /// Starts the animation over.
126    ///
127    /// The values go back to where the animation started, and the
128    /// animation runs again from there.
129    ///
130    /// # Returns
131    ///
132    /// - `&mut Self` - A `mut Self` value.
133    pub fn reset(&mut self) -> &mut Self {
134        self.get_mut_tween().reset();
135        let from: Vec<Vec<f64>> = self.get_from().clone();
136        self.set_current(from);
137        self
138    }
139
140    /// Remembers the option the chart started with.
141    ///
142    /// # Arguments
143    ///
144    /// - `&ChartOption` - Shared reference to a `ChartOption`.
145    ///
146    /// # Returns
147    ///
148    /// - `&mut Self` - A `mut Self` value.
149    pub fn remember_initial(&mut self, option: &ChartOption) -> &mut Self {
150        self.set_initial(Some(option.clone()));
151        self
152    }
153
154    /// Starts an animation towards the given option.
155    ///
156    /// The values the chart shows now become the starting point of the
157    /// animation; a series that is new grows from zero.
158    ///
159    /// # Arguments
160    ///
161    /// - `&ChartOption` - Shared reference to a `ChartOption`.
162    ///
163    /// # Returns
164    ///
165    /// - `&mut Self` - A `mut Self` value.
166    pub fn begin(&mut self, option: &ChartOption) -> &mut Self {
167        let target: Vec<Vec<f64>> = collect_values(option);
168        if self.get_duration() <= 0.0 {
169            self.set_current(target.clone());
170            self.set_to(target.clone());
171            self.set_from(target);
172            // A tween of no duration is over before it starts.
173            let mut tween: Tween<f64> = Tween::create(0.0, 1.0, 0.0);
174            tween.set_state(TweenState::Finished);
175            self.set_tween(tween);
176            return self;
177        }
178        // A series that was not there before starts at zero, which is
179        // what makes a new series grow out of the axis.
180        let from: Vec<Vec<f64>> = align_values(self.get_current(), &target);
181        self.set_from(from.clone());
182        self.set_to(target.clone());
183        self.set_current(from);
184        // The tween is the clock: it is rebuilt for every animation,
185        // because its duration is fixed when it is created.
186        let tween: Tween<f64> =
187            Tween::create(0.0, 1.0, self.get_duration() / MILLISECONDS_PER_SECOND)
188                .with_easing(self.get_tween().get_easing())
189                .with_delay(self.get_delay() / MILLISECONDS_PER_SECOND);
190        self.set_tween(tween);
191        self
192    }
193
194    /// Advances the animation by the given number of milliseconds.
195    ///
196    /// Returns `true` when the animation is still running.
197    ///
198    /// # Arguments
199    ///
200    /// - `f64` - A 64-bit float (`f64`).
201    ///
202    /// # Returns
203    ///
204    /// - `bool` - A boolean.
205    pub fn advance(&mut self, delta: f64) -> bool {
206        if self.get_tween().is_finished() {
207            return false;
208        }
209        self.get_mut_tween()
210            .update(delta.max(0.0) / MILLISECONDS_PER_SECOND);
211        let progress: f64 = self.get_tween().value();
212        let current: Vec<Vec<f64>> = interpolate(self.get_from(), self.get_to(), progress);
213        self.set_current(current);
214        !self.get_tween().is_finished()
215    }
216
217    /// Writes the animated values into the option.
218    ///
219    /// The option's series data is replaced by the animated values, so
220    /// that the layout — and therefore the picture — follows the
221    /// animation.
222    ///
223    /// # Arguments
224    ///
225    /// - `&mut ChartOption` - Mutable reference to a `ChartOption` (mutated in place).
226    pub fn apply(&self, option: &mut ChartOption) {
227        if self.get_current().is_empty() {
228            return;
229        }
230        for (index, series) in option.series.iter_mut().enumerate() {
231            let Some(values) = self.get_current().get(index) else {
232                continue;
233            };
234            for (data_index, data) in series.data.iter_mut().enumerate() {
235                let Some(value) = values.get(data_index) else {
236                    continue;
237                };
238                write_value(data, *value);
239            }
240        }
241    }
242}
243
244/// Default construction for [`Animator`].
245impl Default for Animator {
246    /// Constructs a default [`Animator`] value.
247    fn default() -> Self {
248        Self::new()
249    }
250}