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