Skip to main content

euv_core/scene/animation/
impl.rs

1use super::*;
2
3/// Easing evaluation functions for `Easing`.
4impl Easing {
5    /// Evaluates the easing curve for normalized progress `t` in `[0.0, 1.0]`.
6    ///
7    /// # Arguments
8    ///
9    /// - `&Self` - Easing curve variant.
10    /// - `f32` - Linear progress clamped to `[0.0, 1.0]`.
11    ///
12    /// # Returns
13    ///
14    /// - `f32` - Eased interpolation factor.
15    #[must_use]
16    pub fn eval(&self, t: f32) -> f32 {
17        let u: f32 = t.clamp(0.0, 1.0);
18        match self {
19            Self::Linear => u,
20            Self::EaseOutCubic => {
21                let inv: f32 = 1.0 - u;
22                1.0 - inv * inv * inv
23            }
24            Self::EaseInOutCubic => {
25                if u < 0.5 {
26                    4.0 * u * u * u
27                } else {
28                    let k: f32 = -2.0 * u + 2.0;
29                    1.0 - (k * k * k) * 0.5
30                }
31            }
32            Self::EaseOutBack => {
33                let c1: f32 = 1.4;
34                let c3: f32 = c1 + 1.0;
35                let k: f32 = u - 1.0;
36                1.0 + c3 * k * k * k + c1 * k * k
37            }
38            Self::EaseOutQuint => {
39                let inv: f32 = 1.0 - u;
40                let inv2: f32 = inv * inv;
41                1.0 - inv2 * inv2 * inv
42            }
43            Self::EaseInCubic => u * u * u,
44            Self::SpringGentle => {
45                if u <= 0.0 {
46                    0.0
47                } else if u >= 1.0 {
48                    1.0
49                } else {
50                    let raw: f32 = 1.0 - (-6.2 * u).exp() * (9.0 * u).cos();
51                    let end: f32 = 1.0 - (-6.2_f32).exp() * 9.0_f32.cos();
52                    raw / end.max(0.001)
53                }
54            }
55            Self::SpringSnappy => {
56                if u <= 0.0 {
57                    0.0
58                } else if u >= 1.0 {
59                    1.0
60                } else {
61                    let raw: f32 = 1.0 - (-8.4 * u).exp() * (12.5 * u).cos();
62                    let end: f32 = 1.0 - (-8.4_f32).exp() * 12.5_f32.cos();
63                    raw / end.max(0.001)
64                }
65            }
66        }
67    }
68}
69
70/// Standard transition presets and sampling helpers for `NodeTransition`.
71impl NodeTransition {
72    /// Creates a fluent base transition targeting `node` (`200ms EaseOutCubic`, identity transform/opacity).
73    ///
74    /// # Arguments
75    ///
76    /// - `NodeId` - Target scene node handle.
77    ///
78    /// # Returns
79    ///
80    /// - `Self` - Configurable `NodeTransition` ready for chained setters.
81    #[must_use]
82    pub fn for_node(node: NodeId) -> Self {
83        Self {
84            node,
85            elapsed: 0.0,
86            delay: 0.0,
87            duration: 0.20,
88            easing: Easing::EaseOutCubic,
89            from_opacity: 1.0,
90            to_opacity: 1.0,
91            from_tx: 0.0,
92            to_tx: 0.0,
93            from_ty: 0.0,
94            to_ty: 0.0,
95            from_scale: 1.0,
96            to_scale: 1.0,
97            hide_on_complete: false,
98        }
99    }
100
101    /// Sets an initial hold delay in seconds before interpolation begins.
102    ///
103    /// # Arguments
104    ///
105    /// - `Self` - Source transition.
106    /// - `f32` - Delay duration in seconds (clamped to `>= 0.0`).
107    ///
108    /// # Returns
109    ///
110    /// - `Self` - Updated transition with start delay.
111    #[must_use]
112    pub fn with_delay(mut self, delay_secs: f32) -> Self {
113        self.set_delay(delay_secs.max(0.0));
114        self
115    }
116
117    /// Sets transition duration in seconds.
118    ///
119    /// # Arguments
120    ///
121    /// - `Self` - Source transition.
122    /// - `f32` - Duration in seconds (minimum `16ms`).
123    ///
124    /// # Returns
125    ///
126    /// - `Self` - Updated transition.
127    #[must_use]
128    pub fn with_duration(mut self, duration_secs: f32) -> Self {
129        self.set_duration(duration_secs.max(0.016));
130        self
131    }
132
133    /// Sets the interpolation easing curve.
134    ///
135    /// # Arguments
136    ///
137    /// - `Self` - Source transition.
138    /// - `Easing` - Easing curve variant.
139    ///
140    /// # Returns
141    ///
142    /// - `Self` - Updated transition.
143    #[must_use]
144    pub fn with_easing(mut self, easing: Easing) -> Self {
145        self.set_easing(easing);
146        self
147    }
148
149    /// Configures start and end opacity values in `[0.0, 1.0]`.
150    ///
151    /// # Arguments
152    ///
153    /// - `Self` - Source transition.
154    /// - `f32` - Starting opacity.
155    /// - `f32` - Ending opacity.
156    ///
157    /// # Returns
158    ///
159    /// - `Self` - Updated transition.
160    #[must_use]
161    pub fn with_opacity(mut self, from: f32, to: f32) -> Self {
162        self.set_from_opacity(from.clamp(0.0, 1.0));
163        self.set_to_opacity(to.clamp(0.0, 1.0));
164        self
165    }
166
167    /// Configures start and end 2D translation offsets in logical pixels.
168    ///
169    /// # Arguments
170    ///
171    /// - `Self` - Source transition.
172    /// - `Point` - Starting `(tx, ty)` offset.
173    /// - `Point` - Ending `(tx, ty)` offset.
174    ///
175    /// # Returns
176    ///
177    /// - `Self` - Updated transition.
178    #[must_use]
179    pub fn with_offset(mut self, from: Point, to: Point) -> Self {
180        self.set_from_tx(from.get_x());
181        self.set_from_ty(from.get_y());
182        self.set_to_tx(to.get_x());
183        self.set_to_ty(to.get_y());
184        self
185    }
186
187    /// Configures uniform start and end scale multipliers.
188    ///
189    /// # Arguments
190    ///
191    /// - `Self` - Source transition.
192    /// - `f32` - Starting scale factor.
193    /// - `f32` - Ending scale factor.
194    ///
195    /// # Returns
196    ///
197    /// - `Self` - Updated transition.
198    #[must_use]
199    pub fn with_scale(mut self, from: f32, to: f32) -> Self {
200        self.set_from_scale(from.clamp(0.05, 4.0));
201        self.set_to_scale(to.clamp(0.05, 4.0));
202        self
203    }
204
205    /// Configures whether the node switches to `Display::None` when the transition completes.
206    ///
207    /// # Arguments
208    ///
209    /// - `Self` - Source transition.
210    /// - `bool` - Hide-on-complete flag.
211    ///
212    /// # Returns
213    ///
214    /// - `Self` - Updated transition.
215    #[must_use]
216    pub fn with_hide_on_complete(mut self, hide: bool) -> Self {
217        self.set_hide_on_complete(hide);
218        self
219    }
220
221    /// Creates a standard route-switch entrance transition (`220ms EaseOutCubic`, fade + 12px upward glide).
222    ///
223    /// # Arguments
224    ///
225    /// - `NodeId` - Target page container node.
226    ///
227    /// # Returns
228    ///
229    /// - `Self` - Route entrance transition.
230    #[must_use]
231    pub fn route_enter(node: NodeId) -> Self {
232        Self {
233            node,
234            elapsed: 0.0,
235            delay: 0.0,
236            duration: 0.24,
237            easing: Easing::EaseOutQuint,
238            from_opacity: 0.0,
239            to_opacity: 1.0,
240            from_tx: 0.0,
241            to_tx: 0.0,
242            from_ty: 14.0,
243            to_ty: 0.0,
244            from_scale: 0.992,
245            to_scale: 1.0,
246            hide_on_complete: false,
247        }
248    }
249
250    /// Creates a pure opacity fade transition with optional `Display::None` on completion.
251    ///
252    /// # Arguments
253    ///
254    /// - `NodeId` - Target node handle.
255    /// - `f32` - Starting opacity.
256    /// - `f32` - Ending opacity.
257    /// - `f32` - Duration in seconds.
258    /// - `bool` - Whether to hide the node when the transition finishes.
259    ///
260    /// # Returns
261    ///
262    /// - `Self` - Fade transition.
263    #[must_use]
264    pub fn fade(node: NodeId, from: f32, to: f32, duration: f32, hide_on_complete: bool) -> Self {
265        Self {
266            node,
267            elapsed: 0.0,
268            delay: 0.0,
269            duration: duration.max(0.016),
270            easing: Easing::EaseOutCubic,
271            from_opacity: from,
272            to_opacity: to,
273            from_tx: 0.0,
274            to_tx: 0.0,
275            from_ty: 0.0,
276            to_ty: 0.0,
277            from_scale: 1.0,
278            to_scale: 1.0,
279            hide_on_complete,
280        }
281    }
282
283    /// Creates a slide-and-fade transition for drawers, sheets, popovers, dropdowns, and toasts.
284    ///
285    /// # Arguments
286    ///
287    /// - `NodeId` - Target node handle.
288    /// - `Point` - Starting `(tx, ty)` offset in logical pixels.
289    /// - `Point` - Ending `(tx, ty)` offset in logical pixels.
290    /// - `f32` - Starting opacity.
291    /// - `f32` - Ending opacity.
292    /// - `f32` - Duration in seconds.
293    /// - `bool` - Whether to hide the node upon completion.
294    ///
295    /// # Returns
296    ///
297    /// - `Self` - Slide + fade transition.
298    #[must_use]
299    pub fn slide_fade(
300        node: NodeId,
301        from_offset: Point,
302        to_offset: Point,
303        from_opacity: f32,
304        to_opacity: f32,
305        duration: f32,
306        hide_on_complete: bool,
307    ) -> Self {
308        Self {
309            node,
310            elapsed: 0.0,
311            delay: 0.0,
312            duration: duration.max(0.016),
313            easing: Easing::EaseOutCubic,
314            from_opacity,
315            to_opacity,
316            from_tx: from_offset.get_x(),
317            to_tx: to_offset.get_x(),
318            from_ty: from_offset.get_y(),
319            to_ty: to_offset.get_y(),
320            from_scale: 1.0,
321            to_scale: 1.0,
322            hide_on_complete,
323        }
324    }
325
326    /// Creates a subtle scale-and-rise modal dialog entrance transition (`200ms EaseOutCubic`).
327    ///
328    /// # Arguments
329    ///
330    /// - `NodeId` - Target dialog panel node.
331    ///
332    /// # Returns
333    ///
334    /// - `Self` - Modal dialog entrance transition.
335    #[must_use]
336    pub fn modal_enter(node: NodeId) -> Self {
337        Self {
338            node,
339            elapsed: 0.0,
340            delay: 0.0,
341            duration: 0.28,
342            easing: Easing::SpringSnappy,
343            from_opacity: 0.0,
344            to_opacity: 1.0,
345            from_tx: 0.0,
346            to_tx: 0.0,
347            from_ty: 18.0,
348            to_ty: 0.0,
349            from_scale: 0.93,
350            to_scale: 1.0,
351            hide_on_complete: false,
352        }
353    }
354
355    /// Creates a modal dialog exit transition (`180ms EaseOutCubic`).
356    ///
357    /// # Arguments
358    ///
359    /// - `NodeId` - Target dialog panel node.
360    ///
361    /// # Returns
362    ///
363    /// - `Self` - Modal dialog exit transition.
364    #[must_use]
365    pub fn modal_exit(node: NodeId) -> Self {
366        Self {
367            node,
368            elapsed: 0.0,
369            delay: 0.0,
370            duration: 0.18,
371            easing: Easing::EaseOutCubic,
372            from_opacity: 1.0,
373            to_opacity: 0.0,
374            from_tx: 0.0,
375            to_tx: 0.0,
376            from_ty: 0.0,
377            to_ty: 10.0,
378            from_scale: 1.0,
379            to_scale: 0.96,
380            hide_on_complete: false,
381        }
382    }
383
384    /// Evaluates interpolated `(opacity, Transform2D)` at the current `elapsed` timestamp.
385    ///
386    /// # Arguments
387    ///
388    /// - `&Self` - Transition reference.
389    ///
390    /// # Returns
391    ///
392    /// - `(f32, Transform2D)` - Current interpolated opacity and 2D transform.
393    #[must_use]
394    pub fn sample(&self) -> (f32, Transform2D) {
395        let active_elapsed: f32 = (self.get_elapsed() - self.get_delay()).max(0.0);
396        let raw_t: f32 = (active_elapsed / self.get_duration().max(0.001)).clamp(0.0, 1.0);
397        let k: f32 = self.get_easing().eval(raw_t);
398        let op: f32 = (self.get_from_opacity()
399            + (self.get_to_opacity() - self.get_from_opacity()) * k)
400            .clamp(0.0, 1.0);
401        let tx: f32 = self.get_from_tx() + (self.get_to_tx() - self.get_from_tx()) * k;
402        let ty: f32 = self.get_from_ty() + (self.get_to_ty() - self.get_from_ty()) * k;
403        let sc: f32 = (self.get_from_scale() + (self.get_to_scale() - self.get_from_scale()) * k)
404            .clamp(0.05, 4.0);
405        (op, Transform2D::new(tx, ty, sc, sc, 0.0))
406    }
407
408    /// Returns `true` when the transition has reached or exceeded its total `delay + duration`.
409    ///
410    /// # Arguments
411    ///
412    /// - `&Self` - Transition reference.
413    ///
414    /// # Returns
415    ///
416    /// - `bool` - Whether this transition is finished.
417    #[must_use]
418    pub fn is_complete(&self) -> bool {
419        self.get_elapsed() >= self.get_delay() + self.get_duration()
420    }
421}