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 (`240ms EaseOutQuint`, fade + 14px 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.22,
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: 12.0,
243            to_ty: 0.0,
244            from_scale: 1.0,
245            to_scale: 1.0,
246            hide_on_complete: false,
247        }
248    }
249
250    /// Creates a directional horizontal page-slide entrance transition (`260ms EaseOutQuint`, opaque crisp slide).
251    ///
252    /// # Arguments
253    ///
254    /// - `NodeId` - Incoming page container node.
255    /// - `f32` - Starting horizontal translation offset in logical pixels (`+px` enters from right, `-px` enters from left).
256    ///
257    /// # Returns
258    ///
259    /// - `Self` - Directional horizontal route entrance transition.
260    #[must_use]
261    pub fn route_slide_enter(node: NodeId, start_tx: f32) -> Self {
262        Self {
263            node,
264            elapsed: 0.0,
265            delay: 0.0,
266            duration: 0.26,
267            easing: Easing::EaseOutQuint,
268            from_opacity: 1.0,
269            to_opacity: 1.0,
270            from_tx: start_tx,
271            to_tx: 0.0,
272            from_ty: 0.0,
273            to_ty: 0.0,
274            from_scale: 1.0,
275            to_scale: 1.0,
276            hide_on_complete: false,
277        }
278    }
279
280    /// Creates a directional horizontal page-slide exit transition (`220ms EaseOutQuint`) hiding the node on completion.
281    ///
282    /// # Arguments
283    ///
284    /// - `NodeId` - Outgoing page container node.
285    /// - `f32` - Ending horizontal translation offset in logical pixels (`-px` exits left, `+px` exits right).
286    ///
287    /// # Returns
288    ///
289    /// - `Self` - Directional horizontal route exit transition.
290    #[must_use]
291    pub fn route_slide_exit(node: NodeId, end_tx: f32) -> Self {
292        Self {
293            node,
294            elapsed: 0.0,
295            delay: 0.0,
296            duration: 0.22,
297            easing: Easing::EaseOutQuint,
298            from_opacity: 1.0,
299            to_opacity: 0.25,
300            from_tx: 0.0,
301            to_tx: end_tx,
302            from_ty: 0.0,
303            to_ty: 0.0,
304            from_scale: 1.0,
305            to_scale: 1.0,
306            hide_on_complete: true,
307        }
308    }
309
310    /// Creates a pure opacity fade transition with optional `Display::None` on completion.
311    ///
312    /// # Arguments
313    ///
314    /// - `NodeId` - Target node handle.
315    /// - `f32` - Starting opacity.
316    /// - `f32` - Ending opacity.
317    /// - `f32` - Duration in seconds.
318    /// - `bool` - Whether to hide the node when the transition finishes.
319    ///
320    /// # Returns
321    ///
322    /// - `Self` - Fade transition.
323    #[must_use]
324    pub fn fade(node: NodeId, from: f32, to: f32, duration: f32, hide_on_complete: bool) -> Self {
325        Self {
326            node,
327            elapsed: 0.0,
328            delay: 0.0,
329            duration: duration.max(0.016),
330            easing: Easing::EaseOutCubic,
331            from_opacity: from,
332            to_opacity: to,
333            from_tx: 0.0,
334            to_tx: 0.0,
335            from_ty: 0.0,
336            to_ty: 0.0,
337            from_scale: 1.0,
338            to_scale: 1.0,
339            hide_on_complete,
340        }
341    }
342
343    /// Creates a slide-and-fade transition for drawers, sheets, popovers, dropdowns, and toasts.
344    ///
345    /// # Arguments
346    ///
347    /// - `NodeId` - Target node handle.
348    /// - `Point` - Starting `(tx, ty)` offset in logical pixels.
349    /// - `Point` - Ending `(tx, ty)` offset in logical pixels.
350    /// - `f32` - Starting opacity.
351    /// - `f32` - Ending opacity.
352    /// - `f32` - Duration in seconds.
353    /// - `bool` - Whether to hide the node upon completion.
354    ///
355    /// # Returns
356    ///
357    /// - `Self` - Slide + fade transition.
358    #[must_use]
359    pub fn slide_fade(
360        node: NodeId,
361        from_offset: Point,
362        to_offset: Point,
363        from_opacity: f32,
364        to_opacity: f32,
365        duration: f32,
366        hide_on_complete: bool,
367    ) -> Self {
368        Self {
369            node,
370            elapsed: 0.0,
371            delay: 0.0,
372            duration: duration.max(0.016),
373            easing: Easing::EaseOutCubic,
374            from_opacity,
375            to_opacity,
376            from_tx: from_offset.get_x(),
377            to_tx: to_offset.get_x(),
378            from_ty: from_offset.get_y(),
379            to_ty: to_offset.get_y(),
380            from_scale: 1.0,
381            to_scale: 1.0,
382            hide_on_complete,
383        }
384    }
385
386    /// Creates a subtle scale-and-rise modal dialog entrance transition (`200ms EaseOutCubic`).
387    ///
388    /// # Arguments
389    ///
390    /// - `NodeId` - Target dialog panel node.
391    ///
392    /// # Returns
393    ///
394    /// - `Self` - Modal dialog entrance transition.
395    #[must_use]
396    pub fn modal_enter(node: NodeId) -> Self {
397        Self {
398            node,
399            elapsed: 0.0,
400            delay: 0.0,
401            duration: 0.28,
402            easing: Easing::SpringSnappy,
403            from_opacity: 0.0,
404            to_opacity: 1.0,
405            from_tx: 0.0,
406            to_tx: 0.0,
407            from_ty: 18.0,
408            to_ty: 0.0,
409            from_scale: 0.93,
410            to_scale: 1.0,
411            hide_on_complete: false,
412        }
413    }
414
415    /// Creates a modal dialog exit transition (`180ms EaseOutCubic`).
416    ///
417    /// # Arguments
418    ///
419    /// - `NodeId` - Target dialog panel node.
420    ///
421    /// # Returns
422    ///
423    /// - `Self` - Modal dialog exit transition.
424    #[must_use]
425    pub fn modal_exit(node: NodeId) -> Self {
426        Self {
427            node,
428            elapsed: 0.0,
429            delay: 0.0,
430            duration: 0.18,
431            easing: Easing::EaseOutCubic,
432            from_opacity: 1.0,
433            to_opacity: 0.0,
434            from_tx: 0.0,
435            to_tx: 0.0,
436            from_ty: 0.0,
437            to_ty: 10.0,
438            from_scale: 1.0,
439            to_scale: 0.96,
440            hide_on_complete: false,
441        }
442    }
443
444    /// Evaluates interpolated `(opacity, Transform2D)` at the current `elapsed` timestamp.
445    ///
446    /// # Arguments
447    ///
448    /// - `&Self` - Transition reference.
449    ///
450    /// # Returns
451    ///
452    /// - `(f32, Transform2D)` - Current interpolated opacity and 2D transform.
453    #[must_use]
454    pub fn sample(&self) -> (f32, Transform2D) {
455        let active_elapsed: f32 = (self.get_elapsed() - self.get_delay()).max(0.0);
456        let raw_t: f32 = (active_elapsed / self.get_duration().max(0.001)).clamp(0.0, 1.0);
457        let k: f32 = self.get_easing().eval(raw_t);
458        let op: f32 = (self.get_from_opacity()
459            + (self.get_to_opacity() - self.get_from_opacity()) * k)
460            .clamp(0.0, 1.0);
461        let tx: f32 = self.get_from_tx() + (self.get_to_tx() - self.get_from_tx()) * k;
462        let ty: f32 = self.get_from_ty() + (self.get_to_ty() - self.get_from_ty()) * k;
463        let sc: f32 = (self.get_from_scale() + (self.get_to_scale() - self.get_from_scale()) * k)
464            .clamp(0.05, 4.0);
465        (op, Transform2D::new(tx, ty, sc, sc, 0.0))
466    }
467
468    /// Returns `true` when the transition has reached or exceeded its total `delay + duration`.
469    ///
470    /// # Arguments
471    ///
472    /// - `&Self` - Transition reference.
473    ///
474    /// # Returns
475    ///
476    /// - `bool` - Whether this transition is finished.
477    #[must_use]
478    pub fn is_complete(&self) -> bool {
479        self.get_elapsed() >= self.get_delay() + self.get_duration()
480    }
481}