forked from godotengine/godot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
label_transitions.h
801 lines (724 loc) · 28.2 KB
/
label_transitions.h
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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
/**************************************************************************/
/* label_transitions.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#ifndef LABEL_TRANSITIONS_H
#define LABEL_TRANSITIONS_H
#include "scene/resources/font.h"
#include <memory>
#include <vector>
// How to use:
// -----------
// t = current time (in any unit measure, but same unit as duration)
// b = starting value to interpolate
// c = the total change in value of b that needs to occur
// d = total time it should take to complete (duration)
// Linear Easing functions
static real_t ease_linear_none(real_t t, real_t b, real_t c, real_t d) {
return (c * t / d + b);
}
static real_t ease_linear_in(real_t t, real_t b, real_t c, real_t d) {
return (c * t / d + b);
}
static real_t ease_linear_out(real_t t, real_t b, real_t c, real_t d) {
return (c * t / d + b);
}
static real_t ease_linear_inout(real_t t, real_t b, real_t c, real_t d) {
return (c * t / d + b);
}
// Sine Easing functions
static real_t ease_sine_in(real_t t, real_t b, real_t c, real_t d) {
return (-c * Math::cos(t / d * (Math_PI / 2.0f)) + c + b);
}
static real_t ease_sine_out(real_t t, real_t b, real_t c, real_t d) {
return (c * Math::sin(t / d * (Math_PI / 2.0f)) + b);
}
static real_t ease_sine_inout(real_t t, real_t b, real_t c, real_t d) {
return (-c / 2.0f * (Math::cos(Math_PI * t / d) - 1.0f) + b);
}
// Circular Easing functions
static real_t ease_circ_in(real_t t, real_t b, real_t c, real_t d) {
t /= d;
return (-c * (Math::sqrt(1.0f - t * t) - 1.0f) + b);
}
static real_t ease_circ_out(real_t t, real_t b, real_t c, real_t d) {
t = t / d - 1.0f;
return (c * Math::sqrt(1.0f - t * t) + b);
}
static real_t ease_circ_inout(real_t t, real_t b, real_t c, real_t d) {
t /= d / 2.0f;
if (t < 1.0f)
return (-c / 2.0f * (Math::sqrt(1.0f - t * t) - 1.0f) + b);
t -= 2.0f;
return (c / 2.0f * (Math::sqrt(1.0f - t * t) + 1.0f) + b);
}
// Cubic Easing functions
static real_t ease_cubic_in(real_t t, real_t b, real_t c, real_t d) {
t /= d;
return (c * t * t * t + b);
}
static real_t ease_cubic_out(real_t t, real_t b, real_t c, real_t d) {
t = t / d - 1.0f;
return (c * (t * t * t + 1.0f) + b);
}
static real_t ease_cubic_inout(real_t t, real_t b, real_t c, real_t d) {
t /= d / 2.0f;
if (t < 1.0f)
return (c / 2.0f * t * t * t + b);
t -= 2.0f;
return (c / 2.0f * (t * t * t + 2.0f) + b);
}
// Quadratic Easing functions
static real_t ease_quad_in(real_t t, real_t b, real_t c, real_t d) {
t /= d;
return (c * t * t + b);
}
static real_t ease_quad_out(real_t t, real_t b, real_t c, real_t d) {
t /= d;
return (-c * t * (t - 2.0f) + b);
}
static real_t ease_quad_inout(real_t t, real_t b, real_t c, real_t d) {
if ((t /= d / 2) < 1)
return (((c / 2) * (t * t)) + b);
return (-c / 2.0f * (((t - 1.0f) * (t - 3.0f)) - 1.0f) + b);
}
// Exponential Easing functions
static real_t ease_expo_in(real_t t, real_t b, real_t c, real_t d) {
return (t == 0.0f) ? b : (c * Math::pow(2.0f, 10.0f * (t / d - 1.0f)) + b);
}
static real_t ease_expo_out(real_t t, real_t b, real_t c, real_t d) {
return (t == d) ? (b + c) : (c * (-Math::pow(2.0f, -10.0f * t / d) + 1.0f) + b);
}
static real_t ease_expo_inout(real_t t, real_t b, real_t c, real_t d) {
if (t == 0.0f)
return b;
if (t == d)
return (b + c);
if ((t /= d / 2.0f) < 1.0f)
return (c / 2.0f * Math::pow(2.0f, 10.0f * (t - 1.0f)) + b);
return (c / 2.0f * (-Math::pow(2.0f, -10.0f * (t - 1.0f)) + 2.0f) + b);
}
// Back Easing functions
static real_t ease_back_in(real_t t, real_t b, real_t c, real_t d) {
const real_t s = 1.70158f;
const real_t postFix = t /= d;
return (c * (postFix)*t * ((s + 1.0f) * t - s) + b);
}
static real_t ease_back_out(real_t t, real_t b, real_t c, real_t d) {
const real_t s = 1.70158f;
t = t / d - 1.0f;
return (c * (t * t * ((s + 1.0f) * t + s) + 1.0f) + b);
}
static real_t ease_back_inout(real_t t, real_t b, real_t c, real_t d) {
const real_t s = 1.70158f * 1.525f;
if ((t /= d / 2.0f) < 1.0f)
return (c / 2.0f * (t * t * ((s + 1.0f) * t - s)) + b);
const real_t postFix = t -= 2.0f;
return (c / 2.0f * ((postFix)*t * ((s + 1.0f) * t + s) + 2.0f) + b);
}
// Bounce Easing functions
static real_t ease_bounce_out(real_t t, real_t b, real_t c, real_t d) {
if ((t /= d) < (1.0f / 2.75f)) {
return (c * (7.5625f * t * t) + b);
} else if (t < (2.0f / 2.75f)) {
const real_t postFix = t -= (1.5f / 2.75f);
return (c * (7.5625f * (postFix)*t + 0.75f) + b);
} else if (t < (2.5 / 2.75)) {
const real_t postFix = t -= (2.25f / 2.75f);
return (c * (7.5625f * (postFix)*t + 0.9375f) + b);
} else {
const real_t postFix = t -= (2.625f / 2.75f);
return (c * (7.5625f * (postFix)*t + 0.984375f) + b);
}
}
static real_t ease_bounce_in(real_t t, real_t b, real_t c, real_t d) {
return (c - ease_bounce_out(d - t, 0.0f, c, d) + b);
}
static real_t ease_bounce_inout(real_t t, real_t b, real_t c, real_t d) {
if (t < d / 2.0f)
return (ease_bounce_in(t * 2.0f, 0.0f, c, d) * 0.5f + b);
else
return (ease_bounce_out(t * 2.0f - d, 0.0f, c, d) * 0.5f + c * 0.5f + b);
}
// Elastic Easing functions
static real_t ease_elastic_in(real_t t, real_t b, real_t c, real_t d) {
if (t == 0.0f)
return b;
if ((t /= d) == 1.0f)
return (b + c);
const real_t p = d * 0.3f;
const real_t a = c;
const real_t s = p / 4.0f;
const real_t postFix = a * Math::pow(2.0f, 10.0f * (t -= 1.0f));
return (-(postFix * Math::sin((t * d - s) * (2.0f * Math_PI) / p)) + b);
}
static real_t ease_elastic_out(real_t t, real_t b, real_t c, real_t d) {
if (t == 0.0f)
return b;
if ((t /= d) == 1.0f)
return (b + c);
const real_t p = d * 0.3f;
const real_t a = c;
const real_t s = p / 4.0f;
return (a * Math::pow(2.0f, -10.0f * t) * Math::sin((t * d - s) * (2.0f * Math_PI) / p) + c + b);
}
static real_t ease_elastic_inout(real_t t, real_t b, real_t c, real_t d) {
if (t == 0.0f)
return b;
if ((t /= d / 2.0f) == 2.0f)
return (b + c);
const real_t p = d * (0.3f * 1.5f);
const real_t a = c;
const real_t s = p / 4.0f;
if (t < 1.0f) {
const real_t postFix = a * Math::pow(2.0f, 10.0f * (t -= 1.0f));
return -0.5f * (postFix * Math::sin((t * d - s) * (2.0f * Math_PI) / p)) + b;
}
const real_t postFix = a * Math::pow(2.0f, -10.0f * (t -= 1.0f));
return (postFix * Math::sin((t * d - s) * (2.0f * Math_PI) / p) * 0.5f + c + b);
}
// End easing functions.
#define EASE_FUNC \
"EaseLinearNone," \
"EaseLinearIn," \
"EaseLinearOut," \
"EaseLinearInOut," \
"EaseSineIn," \
"EaseSineOut," \
"EaseSineInOut," \
"EaseCircIn," \
"EaseCircOut," \
"EaseCircInOut," \
"EaseCubicIn," \
"EaseCubicOut," \
"EaseCubicInOut," \
"EaseQuadIn," \
"EaseQuadOut," \
"EaseQuadInOut," \
"EaseExpoIn," \
"EaseExpoOut," \
"EaseExpoInOut," \
"EaseBackIn," \
"EaseBackOut," \
"EaseBackInOut," \
"EaseBounceIn," \
"EaseBounceOut," \
"EaseBounceInOut," \
"EaseElasticIn," \
"EaseElasticOut," \
"EaseElasticInOut"
#define DefineEaseFunc(F) \
ease_##F##_in, \
ease_##F##_out, \
ease_##F##_inout
typedef real_t (*ease_func_t)(real_t t, real_t b, real_t c, real_t d);
static ease_func_t ease_func_table[] = {
ease_linear_none,
DefineEaseFunc(linear),
DefineEaseFunc(sine),
DefineEaseFunc(circ),
DefineEaseFunc(cubic),
DefineEaseFunc(quad),
DefineEaseFunc(expo),
DefineEaseFunc(back),
DefineEaseFunc(bounce),
DefineEaseFunc(elastic)
};
#undef DefineEaseFunc
static inline void _dump_xform(const CharTransform &xform) {
print_line("CharTransform:");
print_line(" dest_rect: " + xform.dest_rect);
print_line(" tex_clip: " + xform.tex_clip);
}
struct Label::AnimationController {
virtual bool init_transition(Label *p_owner, real_t p_duration, ease_func_t p_ease, WordCache *p_cache_in, WordCache *p_cache_out) = 0;
virtual bool update(real_t p_dt, ease_func_t p_ease) = 0; // -> true: transition done
virtual const std::vector<WordCache *> get_draw_cache() = 0;
virtual const CharTransform *get_char_xform(const WordCache *p_cache, int p_pos) = 0;
virtual bool is_done() const = 0;
virtual bool is_valid() const = 0;
virtual ~AnimationController() {}
bool _same_char(WordCache &cache1, WordCache &cache2, int position) {
if (position >= cache1.cache_text.length() || position >= cache2.cache_text.length())
return false;
return cache1.cache_text[position] == cache2.cache_text[position];
}
};
struct Label::AnimationNone : public Label::AnimationController {
virtual bool init_transition(Label *p_owner, real_t p_duration, ease_func_t p_ease, WordCache *p_cache_in, WordCache *p_cache_out) { return false; }
virtual bool update(real_t p_dt, ease_func_t p_ease) { return true; }
virtual const std::vector<WordCache *> get_draw_cache() { return std::vector<WordCache *>(); }
virtual const CharTransform *get_char_xform(const WordCache *p_cache, int p_pos) { return nullptr; }
virtual bool is_done() const { return true; }
virtual bool is_valid() const { return false; }
};
struct Label::GenericDualTransformController : public Label::AnimationController {
enum AnimationOrient {
ANIMATION_WHEEL_UP,
ANIMATION_WHEEL_DOWN,
ANIMATION_REVEAL_UP,
ANIMATION_REVEAL_DOWN,
ANIMATION_SLIDE_UP,
ANIMATION_SLIDE_DOWN,
};
CharTransform xform_out;
CharTransform xform_in;
real_t current = 0;
real_t duration = 0;
bool active = false;
bool change_new_chars_only = true;
Label *owner = nullptr;
AnimationOrient orientation = ANIMATION_WHEEL_UP;
WordCache cache_in;
WordCache cache_out;
void _update_xform(ease_func_t ease) {
xform_in.progress = xform_out.progress = 1.0 - Math::abs(current / duration);
const real_t t = ease(current, 0, 1, duration);
switch (orientation) {
case ANIMATION_WHEEL_UP: {
xform_out.dest_rect.size.y = 1.0 - t; // 1 .. 0
xform_in.dest_rect.position.y = 1.0 - t; // 1 .. 0
} break;
case ANIMATION_WHEEL_DOWN: {
xform_out.dest_rect.position.y = t; // 0 .. 1
xform_in.dest_rect.size.y = t; // 0 .. 1
} break;
case ANIMATION_REVEAL_UP: {
xform_out.dest_rect.size.y = 1.0 - t; // 1 .. 0
xform_out.tex_clip.position.y = t; // 0 .. 1
xform_in.dest_rect.position.y = 1.0 - t; // 1 .. 0
xform_in.tex_clip.size.y = t; // 0 .. 1
} break;
case ANIMATION_REVEAL_DOWN: {
xform_out.dest_rect.position.y = t; // 0 .. 1
xform_out.tex_clip.position.y = t; // 0 .. 1
xform_in.dest_rect.size.y = t; // 0 .. 1
xform_in.tex_clip.position.y = t; // 1 .. 0
} break;
case ANIMATION_SLIDE_UP: {
xform_out.dest_rect.size.y = 1.0 - t; // 1 .. 0
xform_out.tex_clip.position.y = t; // 0 .. 1
xform_in.dest_rect.position.y = 1.0 - t; // 1 .. 0
xform_in.tex_clip.size.y = t; // 0 .. 1
} break;
case ANIMATION_SLIDE_DOWN: {
xform_out.dest_rect.position.y = t; // 0 .. 1
xform_out.tex_clip.size.y = 1.0 - t; // 1 .. 0
xform_in.dest_rect.size.y = t; // 0 .. 1
xform_in.tex_clip.position.y = 1.0 - t; // 1 .. 0
} break;
default:
ERR_PRINT("Unknown orientation type - no transform performed");
}
}
virtual bool init_transition(Label *p_owner, real_t p_duration, ease_func_t p_ease, WordCache *p_cache_in, WordCache *p_cache_out) {
ERR_FAIL_COND_V(p_cache_in == 0, false);
ERR_FAIL_COND_V(p_cache_out == 0, false);
ERR_FAIL_COND_V(p_duration <= 0, false);
xform_in = CharTransform();
xform_out = CharTransform();
cache_in = *p_cache_in;
cache_out = *p_cache_out;
duration = p_duration;
current = 0; // transition from 0 .. duration
active = cache_in.words || cache_out.words;
_update_xform(p_ease); // initial xform value
return true;
}
virtual bool update(real_t p_dt, ease_func_t p_ease) {
if (!active) {
return true;
}
if (current > duration) {
active = false;
return true;
}
_update_xform(p_ease);
current += p_dt;
return false;
}
virtual const std::vector<WordCache *> get_draw_cache() { return std::vector<WordCache *>{ &cache_in, &cache_out }; }
virtual const CharTransform *get_char_xform(const WordCache *p_cache, int p_pos) {
if (!change_new_chars_only || !_same_char(cache_in, cache_out, p_pos)) {
if (p_cache == &cache_in)
return &xform_in;
if (p_cache == &cache_out)
return &xform_out;
}
return nullptr;
}
virtual bool is_done() const { return !active; }
virtual bool is_valid() const {
if (!owner)
return false;
else
return (cache_in.line_count != cache_out.line_count) && (owner->get_valign() == VALIGN_CENTER || owner->get_align() == ALIGN_CENTER);
}
GenericDualTransformController(AnimationOrient p_orientation, bool p_change_new_chars_only) {
orientation = p_orientation;
change_new_chars_only = p_change_new_chars_only;
}
};
struct Label::GenericSingleTransformController : public Label::AnimationController {
enum AnimationOrient {
ANIMATION_ROTATE_H,
ANIMATION_ROTATE_V,
};
CharTransform xform;
real_t current = 0;
real_t duration = 0;
bool active = false;
Label *owner = nullptr;
AnimationOrient orientation = ANIMATION_ROTATE_H;
WordCache cache_in;
WordCache cache_out;
void _update_xform(ease_func_t ease) {
xform.progress = 1.0 - Math::abs(current / duration);
switch (orientation) {
case ANIMATION_ROTATE_H: {
xform.dest_rect.position.x = 0.5 - ease(current, 0, 0.5, current < 0 ? -duration : duration); // 0 .. 0,5 .. 0
xform.dest_rect.size.x = 1 - xform.dest_rect.position.x / (1 - xform.dest_rect.position.x); // 1 .. 0 .. 1
} break;
case ANIMATION_ROTATE_V: {
xform.dest_rect.position.y = 0.5 - ease(current, 0, 0.5, current < 0 ? -duration : duration); // 0 .. 0,5 .. 0
xform.dest_rect.size.y = 1 - xform.dest_rect.position.y / (1 - xform.dest_rect.position.y); // 1 .. 0 .. 1
} break;
default:
ERR_PRINT("Unknown orientation type - no transform performed");
}
}
virtual bool init_transition(Label *p_owner, real_t p_duration, ease_func_t p_ease, WordCache *p_cache_in, WordCache *p_cache_out) {
ERR_FAIL_COND_V(p_cache_out == 0, false);
ERR_FAIL_COND_V(p_cache_in == 0, false);
ERR_FAIL_COND_V(p_duration <= 0, false);
const real_t duration_by_2 = p_duration / 2;
xform = CharTransform();
xform.vertical_align = orientation == ANIMATION_ROTATE_V;
cache_in = *p_cache_in;
cache_out = *p_cache_out;
duration = duration_by_2;
// transition from range: -duration/2 .. 0 .. duration/2
if (!cache_out.words) // nothing to hide - jump to showing new text
current = 0;
else
current = -duration_by_2;
active = cache_in.words || cache_out.words;
_update_xform(p_ease); // initial xform value
return true;
}
virtual bool update(real_t p_dt, ease_func_t p_ease) {
if (!active) {
return true;
}
if (current > duration || !get_draw_cache().front()->words) {
active = false;
return true;
}
_update_xform(p_ease);
#if 0
_dump_xform(xform);
print_line(vformat(" current:%f", current));
print_line(" text:"+get_draw_cache()[0]->cache_text);
#endif
current += p_dt;
return false;
}
virtual const std::vector<WordCache *> get_draw_cache() {
if (current >= 0) {
return std::vector<WordCache *>{ &cache_in }; // new text
} else {
return std::vector<WordCache *>{ &cache_out }; // old text
}
}
virtual const CharTransform *get_char_xform(const WordCache *p_cache, int p_pos) { return &xform; }
virtual bool is_done() const { return !active; }
virtual bool is_valid() const { return !!owner; }
GenericSingleTransformController(AnimationOrient p_orientation) {
orientation = p_orientation;
}
};
struct Label::GenericMulti1TransformController : public Label::AnimationController {
enum AnimationOrient {
ANIMATION_ROTATE_H,
ANIMATION_ROTATE_V
};
struct _xform {
CharTransform xform;
real_t current = 0;
real_t delay = 0;
};
Vector<_xform> trans_info;
real_t duration = 0;
bool active = false;
Label *owner = nullptr;
AnimationOrient orientation = ANIMATION_ROTATE_H;
WordCache cache_in;
WordCache cache_out;
void _update_xform(_xform &info, real_t dt, ease_func_t ease) {
info.xform.progress = 1.0 - Math::abs(info.current / duration);
const real_t t = ease(info.current, 0, 0.5, info.current < 0 ? -duration : duration);
switch (orientation) {
case ANIMATION_ROTATE_H: {
info.xform.dest_rect.position.x = 0.5 - t; // 0 .. 0,5 .. 0
info.xform.dest_rect.size.x = 1 - info.xform.dest_rect.position.x / (1 - info.xform.dest_rect.position.x); // 1 .. 0 .. 1
} break;
case ANIMATION_ROTATE_V: {
info.xform.dest_rect.position.y = 0.5 - t; // 0 .. 0,5 .. 0
info.xform.dest_rect.size.y = 1 - info.xform.dest_rect.position.y / (1 - info.xform.dest_rect.position.y); // 1 .. 0 .. 1
} break;
default:
ERR_PRINT("Unknown orientation type - no transform performed");
}
info.current += dt;
}
bool _update_xform(real_t dt, ease_func_t ease) {
bool status = false;
for (int f = 0; f < trans_info.size(); ++f) {
_xform &info = trans_info.write[f];
if (info.delay <= 0) {
if (info.current < duration) {
_update_xform(info, dt, ease);
status = true;
}
} else {
info.delay -= dt;
status = true;
}
}
return status;
}
virtual bool init_transition(Label *p_owner, real_t p_duration, ease_func_t p_ease, WordCache *p_cache_in, WordCache *p_cache_out) {
ERR_FAIL_COND_V(p_cache_out == 0, false);
ERR_FAIL_COND_V(p_cache_in == 0, false);
ERR_FAIL_COND_V(p_duration <= 0, false);
const real_t duration_by_2 = p_duration / 2;
const int xforms_size = MAX(p_cache_in->cache_text.length(), p_cache_out->cache_text.length());
trans_info.clear();
trans_info.resize(xforms_size);
cache_in = *p_cache_in;
cache_out = *p_cache_out;
duration = duration_by_2;
for (int f = 0; f < xforms_size; ++f) {
_xform &info = trans_info.write[f];
info.xform.vertical_align = orientation == ANIMATION_ROTATE_V;
info.delay = f * duration * 0.2;
// transition from range: -duration/2 .. 0 .. duration/2
if (!cache_out.words) // nothing to hide - jump to showing new text
info.current = 0;
else
info.current = -duration_by_2;
_update_xform(info, 0, p_ease); // initial xform value
}
active = cache_in.words || cache_out.words;
return true;
}
virtual bool update(real_t p_dt, ease_func_t p_ease) {
if (!active) {
return true;
}
if ((active = _update_xform(p_dt, p_ease))) {
return false;
}
cache_in = WordCache();
cache_out = WordCache();
return true;
}
virtual const std::vector<WordCache *> get_draw_cache() {
return std::vector<WordCache *>{ &cache_out, &cache_in };
}
virtual const CharTransform *get_char_xform(const WordCache *p_cache, int p_pos) {
ERR_FAIL_INDEX_V(p_pos, trans_info.size(), nullptr);
static CharTransform _hidden(true);
const _xform &info = trans_info[p_pos];
if (info.current > 0) {
if (p_cache == &cache_out)
return &_hidden;
if (p_cache == &cache_in)
return &info.xform;
} else {
if (p_cache == &cache_out)
return &info.xform;
if (p_cache == &cache_in)
return &_hidden;
}
return nullptr;
}
virtual bool is_done() const { return !active; }
virtual bool is_valid() const { return !!owner; }
GenericMulti1TransformController(AnimationOrient p_orientation) {
orientation = p_orientation;
}
};
struct Label::GenericMulti2TransformController : public Label::AnimationController {
enum AnimationOrient {
ANIMATION_SLIDE_UP,
ANIMATION_SLIDE_DOWN,
};
struct _xform {
CharTransform xform_in, xform_out;
real_t current = 0;
real_t delay = 0;
};
Vector<_xform> trans_info;
real_t duration = 0;
bool active = false;
Label *owner = nullptr;
AnimationOrient orientation = ANIMATION_SLIDE_UP;
WordCache cache_in;
WordCache cache_out;
void _update_xform(_xform &info, real_t dt, ease_func_t ease) {
info.xform_out.progress = info.xform_in.progress = 1.0 - Math::abs(info.current / duration);
const real_t t = ease(info.current, 0, 1, duration);
switch (orientation) {
case ANIMATION_SLIDE_UP: {
info.xform_out.dest_rect.size.y = 1.0 - t; // 1 .. 0
info.xform_out.tex_clip.position.y = t; // 0 .. 1
info.xform_in.dest_rect.position.y = 1.0 - t; // 1 .. 0
info.xform_in.tex_clip.size.y = t; // 0 .. 1
} break;
case ANIMATION_SLIDE_DOWN: {
info.xform_out.dest_rect.position.y = t; // 0 .. 1
info.xform_out.tex_clip.size.y = 1.0 - t; // 1 .. 0
info.xform_in.dest_rect.size.y = t; // 0 .. 1
info.xform_in.tex_clip.position.y = 1.0 - t; // 1 .. 0
} break;
default:
ERR_PRINT("Unknown orientation type - no transform performed");
}
info.current += dt;
}
bool _update_xform(real_t dt, ease_func_t ease) {
bool status = false;
for (int f = 0; f < trans_info.size(); ++f) {
_xform &info = trans_info.write[f];
if (info.delay <= 0) {
if (info.current < duration) {
_update_xform(info, dt, ease);
status = true;
}
} else {
info.delay -= dt;
status = true;
}
}
return status;
}
virtual bool init_transition(Label *p_owner, real_t p_duration, ease_func_t p_ease, WordCache *p_cache_in, WordCache *p_cache_out) {
ERR_FAIL_COND_V(p_cache_out == 0, false);
ERR_FAIL_COND_V(p_cache_in == 0, false);
ERR_FAIL_COND_V(p_duration <= 0, false);
const int xforms_size = MAX(p_cache_in->cache_text.length(), p_cache_out->cache_text.length());
trans_info.clear();
trans_info.resize(xforms_size);
cache_in = *p_cache_in;
cache_out = *p_cache_out;
duration = p_duration;
for (int f = 0; f < xforms_size; ++f) {
_xform &info = trans_info.write[f];
info.delay = f * p_duration * 0.2;
info.current = 0;
_update_xform(info, 0, p_ease); // initial xform value
}
active = cache_in.words || cache_out.words;
return true;
}
virtual bool update(real_t p_dt, ease_func_t p_ease) {
if (!active) {
return true;
}
if ((active = _update_xform(p_dt, p_ease))) {
return false;
}
cache_in = WordCache();
cache_out = WordCache();
return true;
}
virtual const std::vector<WordCache *> get_draw_cache() {
return std::vector<WordCache *>{ &cache_out, &cache_in };
}
virtual const CharTransform *get_char_xform(const WordCache *p_cache, int p_pos) {
ERR_FAIL_INDEX_V(p_pos, trans_info.size(), nullptr);
const _xform &info = trans_info[p_pos];
if (p_cache == &cache_in)
return &info.xform_in;
if (p_cache == &cache_out)
return &info.xform_out;
return nullptr;
}
virtual bool is_done() const { return !active; }
virtual bool is_valid() const { return !!owner; }
GenericMulti2TransformController(AnimationOrient p_orientation) {
orientation = p_orientation;
}
};
std::unique_ptr<Label::AnimationController> AnimationControllerFactory(Label::TransitionEffect p_effect) {
switch (p_effect) {
case Label::TRANSITIONEFFECT_NONE:
return std::unique_ptr<Label::AnimationController>(new Label::AnimationNone());
case Label::TRANSITIONEFFECT_SLIDE_UP:
return std::unique_ptr<Label::AnimationController>(new Label::GenericDualTransformController(Label::GenericDualTransformController::ANIMATION_SLIDE_UP, false));
case Label::TRANSITIONEFFECT_SLIDE_DOWN:
return std::unique_ptr<Label::AnimationController>(new Label::GenericDualTransformController(Label::GenericDualTransformController::ANIMATION_SLIDE_DOWN, false));
case Label::TRANSITIONEFFECT_SLIDE_UP_NEW:
return std::unique_ptr<Label::AnimationController>(new Label::GenericDualTransformController(Label::GenericDualTransformController::ANIMATION_SLIDE_UP, true));
case Label::TRANSITIONEFFECT_SLIDE_DOWN_NEW:
return std::unique_ptr<Label::AnimationController>(new Label::GenericDualTransformController(Label::GenericDualTransformController::ANIMATION_SLIDE_DOWN, true));
case Label::TRANSITIONEFFECT_WHEEL_UP:
return std::unique_ptr<Label::AnimationController>(new Label::GenericDualTransformController(Label::GenericDualTransformController::ANIMATION_WHEEL_UP, false));
case Label::TRANSITIONEFFECT_WHEEL_DOWN:
return std::unique_ptr<Label::AnimationController>(new Label::GenericDualTransformController(Label::GenericDualTransformController::ANIMATION_WHEEL_DOWN, false));
case Label::TRANSITIONEFFECT_WHEEL_UP_NEW:
return std::unique_ptr<Label::AnimationController>(new Label::GenericDualTransformController(Label::GenericDualTransformController::ANIMATION_WHEEL_UP, true));
case Label::TRANSITIONEFFECT_WHEEL_DOWN_NEW:
return std::unique_ptr<Label::AnimationController>(new Label::GenericDualTransformController(Label::GenericDualTransformController::ANIMATION_WHEEL_DOWN, true));
case Label::TRANSITIONEFFECT_REVEAL_UP:
return std::unique_ptr<Label::AnimationController>(new Label::GenericDualTransformController(Label::GenericDualTransformController::ANIMATION_REVEAL_UP, false));
case Label::TRANSITIONEFFECT_REVEAL_DOWN:
return std::unique_ptr<Label::AnimationController>(new Label::GenericDualTransformController(Label::GenericDualTransformController::ANIMATION_REVEAL_DOWN, false));
case Label::TRANSITIONEFFECT_REVEAL_UP_NEW:
return std::unique_ptr<Label::AnimationController>(new Label::GenericDualTransformController(Label::GenericDualTransformController::ANIMATION_REVEAL_UP, true));
case Label::TRANSITIONEFFECT_REVEAL_DOWN_NEW:
return std::unique_ptr<Label::AnimationController>(new Label::GenericDualTransformController(Label::GenericDualTransformController::ANIMATION_REVEAL_DOWN, true));
case Label::TRANSITIONEFFECT_ROTATE_V:
return std::unique_ptr<Label::AnimationController>(new Label::GenericSingleTransformController(Label::GenericSingleTransformController::ANIMATION_ROTATE_V));
case Label::TRANSITIONEFFECT_ROTATE_H:
return std::unique_ptr<Label::AnimationController>(new Label::GenericSingleTransformController(Label::GenericSingleTransformController::ANIMATION_ROTATE_H));
case Label::TRANSITIONEFFECT_ROTATE_V_SEQ:
return std::unique_ptr<Label::AnimationController>(new Label::GenericMulti1TransformController(Label::GenericMulti1TransformController::ANIMATION_ROTATE_V));
case Label::TRANSITIONEFFECT_ROTATE_H_SEQ:
return std::unique_ptr<Label::AnimationController>(new Label::GenericMulti1TransformController(Label::GenericMulti1TransformController::ANIMATION_ROTATE_H));
case Label::TRANSITIONEFFECT_SLIDE_UP_SEQ:
return std::unique_ptr<Label::AnimationController>(new Label::GenericMulti2TransformController(Label::GenericMulti2TransformController::ANIMATION_SLIDE_UP));
case Label::TRANSITIONEFFECT_SLIDE_DOWN_SEQ:
return std::unique_ptr<Label::AnimationController>(new Label::GenericMulti2TransformController(Label::GenericMulti2TransformController::ANIMATION_SLIDE_DOWN));
default:
ERR_PRINT("Unknown transition - defaults to None");
}
return std::unique_ptr<Label::AnimationController>(new Label::AnimationNone());
};
#endif /* LABEL_TRANSITIONS_H */