forked from libretro/RetroArch
-
Notifications
You must be signed in to change notification settings - Fork 2
/
menu_animation.c
678 lines (584 loc) · 17.1 KB
/
menu_animation.c
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
/* RetroArch - A frontend for libretro.
* Copyright (C) 2014-2017 - Jean-André Santoni
* Copyright (C) 2011-2017 - Daniel De Matteis
*
* RetroArch is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include <math.h>
#include <string.h>
#include <compat/strl.h>
#include <encodings/utf.h>
#include <retro_miscellaneous.h>
#include <features/features_cpu.h>
#include "menu_animation.h"
#include "../configuration.h"
#include "../performance_counters.h"
#define IDEAL_DELTA_TIME (1.0 / 60.0 * 1000000.0)
struct tween
{
bool alive;
float duration;
float running_since;
float initial_value;
float target_value;
float *subject;
int tag;
easing_cb easing;
tween_cb cb;
};
struct menu_animation
{
struct tween *list;
size_t capacity;
size_t size;
size_t first_dead;
};
typedef struct menu_animation menu_animation_t;
static menu_animation_t anim;
static retro_time_t cur_time = 0;
static retro_time_t old_time = 0;
static float delta_time = 0.0f;
static bool animation_is_active = false;
/* from https://github.com/kikito/tween.lua/blob/master/tween.lua */
static float easing_linear(float t, float b, float c, float d)
{
return c * t / d + b;
}
static float easing_in_out_quad(float t, float b, float c, float d)
{
t = t / d * 2;
if (t < 1)
return c / 2 * pow(t, 2) + b;
return -c / 2 * ((t - 1) * (t - 3) - 1) + b;
}
static float easing_in_quad(float t, float b, float c, float d)
{
return c * pow(t / d, 2) + b;
}
static float easing_out_quad(float t, float b, float c, float d)
{
t = t / d;
return -c * t * (t - 2) + b;
}
static float easing_out_in_quad(float t, float b, float c, float d)
{
if (t < d / 2)
return easing_out_quad(t * 2, b, c / 2, d);
return easing_in_quad((t * 2) - d, b + c / 2, c / 2, d);
}
static float easing_in_cubic(float t, float b, float c, float d)
{
return c * pow(t / d, 3) + b;
}
static float easing_out_cubic(float t, float b, float c, float d)
{
return c * (pow(t / d - 1, 3) + 1) + b;
}
static float easing_in_out_cubic(float t, float b, float c, float d)
{
t = t / d * 2;
if (t < 1)
return c / 2 * t * t * t + b;
t = t - 2;
return c / 2 * (t * t * t + 2) + b;
}
static float easing_out_in_cubic(float t, float b, float c, float d)
{
if (t < d / 2)
return easing_out_cubic(t * 2, b, c / 2, d);
return easing_in_cubic((t * 2) - d, b + c / 2, c / 2, d);
}
static float easing_in_quart(float t, float b, float c, float d)
{
return c * pow(t / d, 4) + b;
}
static float easing_out_quart(float t, float b, float c, float d)
{
return -c * (pow(t / d - 1, 4) - 1) + b;
}
static float easing_in_out_quart(float t, float b, float c, float d)
{
t = t / d * 2;
if (t < 1)
return c / 2 * pow(t, 4) + b;
return -c / 2 * (pow(t - 2, 4) - 2) + b;
}
static float easing_out_in_quart(float t, float b, float c, float d)
{
if (t < d / 2)
return easing_out_quart(t * 2, b, c / 2, d);
return easing_in_quart((t * 2) - d, b + c / 2, c / 2, d);
}
static float easing_in_quint(float t, float b, float c, float d)
{
return c * pow(t / d, 5) + b;
}
static float easing_out_quint(float t, float b, float c, float d)
{
return c * (pow(t / d - 1, 5) + 1) + b;
}
static float easing_in_out_quint(float t, float b, float c, float d)
{
t = t / d * 2;
if (t < 1)
return c / 2 * pow(t, 5) + b;
return c / 2 * (pow(t - 2, 5) + 2) + b;
}
static float easing_out_in_quint(float t, float b, float c, float d)
{
if (t < d / 2)
return easing_out_quint(t * 2, b, c / 2, d);
return easing_in_quint((t * 2) - d, b + c / 2, c / 2, d);
}
static float easing_in_sine(float t, float b, float c, float d)
{
return -c * cos(t / d * (M_PI / 2)) + c + b;
}
static float easing_out_sine(float t, float b, float c, float d)
{
return c * sin(t / d * (M_PI / 2)) + b;
}
static float easing_in_out_sine(float t, float b, float c, float d)
{
return -c / 2 * (cos(M_PI * t / d) - 1) + b;
}
static float easing_out_in_sine(float t, float b, float c, float d)
{
if (t < d / 2)
return easing_out_sine(t * 2, b, c / 2, d);
return easing_in_sine((t * 2) -d, b + c / 2, c / 2, d);
}
static float easing_in_expo(float t, float b, float c, float d)
{
if (t == 0)
return b;
return c * powf(2, 10 * (t / d - 1)) + b - c * 0.001;
}
static float easing_out_expo(float t, float b, float c, float d)
{
if (t == d)
return b + c;
return c * 1.001 * (-powf(2, -10 * t / d) + 1) + b;
}
static float easing_in_out_expo(float t, float b, float c, float d)
{
if (t == 0)
return b;
if (t == d)
return b + c;
t = t / d * 2;
if (t < 1)
return c / 2 * powf(2, 10 * (t - 1)) + b - c * 0.0005;
return c / 2 * 1.0005 * (-powf(2, -10 * (t - 1)) + 2) + b;
}
static float easing_out_in_expo(float t, float b, float c, float d)
{
if (t < d / 2)
return easing_out_expo(t * 2, b, c / 2, d);
return easing_in_expo((t * 2) - d, b + c / 2, c / 2, d);
}
static float easing_in_circ(float t, float b, float c, float d)
{
return(-c * (sqrt(1 - powf(t / d, 2)) - 1) + b);
}
static float easing_out_circ(float t, float b, float c, float d)
{
return(c * sqrt(1 - powf(t / d - 1, 2)) + b);
}
static float easing_in_out_circ(float t, float b, float c, float d)
{
t = t / d * 2;
if (t < 1)
return -c / 2 * (sqrt(1 - t * t) - 1) + b;
t = t - 2;
return c / 2 * (sqrt(1 - t * t) + 1) + b;
}
static float easing_out_in_circ(float t, float b, float c, float d)
{
if (t < d / 2)
return easing_out_circ(t * 2, b, c / 2, d);
return easing_in_circ((t * 2) - d, b + c / 2, c / 2, d);
}
static float easing_out_bounce(float t, float b, float c, float d)
{
t = t / d;
if (t < 1 / 2.75)
return c * (7.5625 * t * t) + b;
if (t < 2 / 2.75)
{
t = t - (1.5 / 2.75);
return c * (7.5625 * t * t + 0.75) + b;
}
else if (t < 2.5 / 2.75)
{
t = t - (2.25 / 2.75);
return c * (7.5625 * t * t + 0.9375) + b;
}
t = t - (2.625 / 2.75);
return c * (7.5625 * t * t + 0.984375) + b;
}
static float easing_in_bounce(float t, float b, float c, float d)
{
return c - easing_out_bounce(d - t, 0, c, d) + b;
}
static float easing_in_out_bounce(float t, float b, float c, float d)
{
if (t < d / 2)
return easing_in_bounce(t * 2, 0, c, d) * 0.5 + b;
return easing_out_bounce(t * 2 - d, 0, c, d) * 0.5 + c * .5 + b;
}
static float easing_out_in_bounce(float t, float b, float c, float d)
{
if (t < d / 2)
return easing_out_bounce(t * 2, b, c / 2, d);
return easing_in_bounce((t * 2) - d, b + c / 2, c / 2, d);
}
static void menu_animation_ticker_generic(uint64_t idx,
size_t max_width, size_t *offset, size_t *width)
{
int ticker_period = (int)(2 * (*width - max_width) + 4);
int phase = idx % ticker_period;
int phase_left_stop = 2;
int phase_left_moving = (int)(phase_left_stop + (*width - max_width));
int phase_right_stop = phase_left_moving + 2;
int left_offset = phase - phase_left_stop;
int right_offset = (int)((*width - max_width) - (phase - phase_right_stop));
if (phase < phase_left_stop)
*offset = 0;
else if (phase < phase_left_moving)
*offset = left_offset;
else if (phase < phase_right_stop)
*offset = *width - max_width;
else
*offset = right_offset;
*width = max_width;
}
bool menu_animation_push(menu_animation_ctx_entry_t *entry)
{
struct tween t;
struct tween *target = NULL;
t.alive = true;
t.duration = entry->duration;
t.running_since = 0;
t.initial_value = *entry->subject;
t.target_value = entry->target_value;
t.subject = entry->subject;
t.tag = entry->tag;
t.cb = entry->cb;
t.easing = NULL;
switch (entry->easing_enum)
{
case EASING_LINEAR:
t.easing = &easing_linear;
break;
/* Quad */
case EASING_IN_QUAD:
t.easing = &easing_in_quad;
break;
case EASING_OUT_QUAD:
t.easing = &easing_out_quad;
break;
case EASING_IN_OUT_QUAD:
t.easing = &easing_in_out_quad;
break;
case EASING_OUT_IN_QUAD:
t.easing = &easing_out_in_quad;
break;
/* Cubic */
case EASING_IN_CUBIC:
t.easing = &easing_in_cubic;
break;
case EASING_OUT_CUBIC:
t.easing = &easing_out_cubic;
break;
case EASING_IN_OUT_CUBIC:
t.easing = &easing_in_out_cubic;
break;
case EASING_OUT_IN_CUBIC:
t.easing = &easing_out_in_cubic;
break;
/* Quart */
case EASING_IN_QUART:
t.easing = &easing_in_quart;
break;
case EASING_OUT_QUART:
t.easing = &easing_out_quart;
break;
case EASING_IN_OUT_QUART:
t.easing = &easing_in_out_quart;
break;
case EASING_OUT_IN_QUART:
t.easing = &easing_out_in_quart;
break;
/* Quint */
case EASING_IN_QUINT:
t.easing = &easing_in_quint;
break;
case EASING_OUT_QUINT:
t.easing = &easing_out_quint;
break;
case EASING_IN_OUT_QUINT:
t.easing = &easing_in_out_quint;
break;
case EASING_OUT_IN_QUINT:
t.easing = &easing_out_in_quint;
break;
/* Sine */
case EASING_IN_SINE:
t.easing = &easing_in_sine;
break;
case EASING_OUT_SINE:
t.easing = &easing_out_sine;
break;
case EASING_IN_OUT_SINE:
t.easing = &easing_in_out_sine;
break;
case EASING_OUT_IN_SINE:
t.easing = &easing_out_in_sine;
break;
/* Expo */
case EASING_IN_EXPO:
t.easing = &easing_in_expo;
break;
case EASING_OUT_EXPO:
t.easing = &easing_out_expo;
break;
case EASING_IN_OUT_EXPO:
t.easing = &easing_in_out_expo;
break;
case EASING_OUT_IN_EXPO:
t.easing = &easing_out_in_expo;
break;
/* Circ */
case EASING_IN_CIRC:
t.easing = &easing_in_circ;
break;
case EASING_OUT_CIRC:
t.easing = &easing_out_circ;
break;
case EASING_IN_OUT_CIRC:
t.easing = &easing_in_out_circ;
break;
case EASING_OUT_IN_CIRC:
t.easing = &easing_out_in_circ;
break;
/* Bounce */
case EASING_IN_BOUNCE:
t.easing = &easing_in_bounce;
break;
case EASING_OUT_BOUNCE:
t.easing = &easing_out_bounce;
break;
case EASING_IN_OUT_BOUNCE:
t.easing = &easing_in_out_bounce;
break;
case EASING_OUT_IN_BOUNCE:
t.easing = &easing_out_in_bounce;
break;
default:
break;
}
/* ignore born dead tweens */
if (!t.easing || t.duration == 0 || t.initial_value == t.target_value)
return false;
if (anim.first_dead < anim.size && !anim.list[anim.first_dead].alive)
target = &anim.list[anim.first_dead++];
else
{
if (anim.size >= anim.capacity)
{
anim.capacity++;
anim.list = (struct tween*)realloc(anim.list,
anim.capacity * sizeof(struct tween));
}
target = &anim.list[anim.size++];
}
*target = t;
return true;
}
bool menu_animation_update(float delta_time)
{
unsigned i;
unsigned active_tweens = 0;
for(i = 0; i < anim.size; i++)
{
struct tween *tween = &anim.list[i];
if (!tween || !tween->alive)
continue;
tween->running_since += delta_time;
*tween->subject = tween->easing(
tween->running_since,
tween->initial_value,
tween->target_value - tween->initial_value,
tween->duration);
if (tween->running_since >= tween->duration)
{
*tween->subject = tween->target_value;
tween->alive = false;
if (i < anim.first_dead)
anim.first_dead = i;
if (tween->cb)
tween->cb();
}
if (tween->running_since < tween->duration)
active_tweens += 1;
}
if (!active_tweens)
{
anim.size = 0;
anim.first_dead = 0;
return false;
}
animation_is_active = true;
return true;
}
bool menu_animation_ticker(const menu_animation_ctx_ticker_t *ticker)
{
size_t str_len = utf8len(ticker->str);
size_t offset = 0;
if ((size_t)str_len <= ticker->len)
{
utf8cpy(ticker->s,
PATH_MAX_LENGTH,
ticker->str,
ticker->len);
return true;
}
if (!ticker->selected)
{
utf8cpy(ticker->s, PATH_MAX_LENGTH, ticker->str, ticker->len - 3);
strlcat(ticker->s, "...", PATH_MAX_LENGTH);
return true;
}
if (str_len > ticker->len)
menu_animation_ticker_generic(
ticker->idx,
ticker->len,
&offset,
&str_len);
utf8cpy(
ticker->s,
PATH_MAX_LENGTH,
utf8skip(ticker->str, offset),
str_len);
animation_is_active = true;
return true;
}
bool menu_animation_get_ideal_delta_time(menu_animation_ctx_delta_t *delta)
{
if (!delta)
return false;
delta->ideal = delta->current / IDEAL_DELTA_TIME;
return true;
}
void menu_animation_update_time(bool timedate_enable)
{
static retro_time_t
last_clock_update = 0;
cur_time = cpu_features_get_time_usec();
delta_time = cur_time - old_time;
if (delta_time >= IDEAL_DELTA_TIME* 4)
delta_time = IDEAL_DELTA_TIME * 4;
if (delta_time <= IDEAL_DELTA_TIME / 4)
delta_time = IDEAL_DELTA_TIME / 4;
old_time = cur_time;
if (((cur_time - last_clock_update) > 1000000)
&& timedate_enable)
{
animation_is_active = true;
last_clock_update = cur_time;
}
}
bool menu_animation_is_active(void)
{
return animation_is_active;
}
bool menu_animation_ctl(enum menu_animation_ctl_state state, void *data)
{
switch (state)
{
case MENU_ANIMATION_CTL_DEINIT:
{
size_t i;
for (i = 0; i < anim.size; i++)
{
if (anim.list[i].subject)
anim.list[i].subject = NULL;
}
free(anim.list);
memset(&anim, 0, sizeof(menu_animation_t));
}
cur_time = 0;
old_time = 0;
delta_time = 0.0f;
break;
case MENU_ANIMATION_CTL_CLEAR_ACTIVE:
animation_is_active = false;
break;
case MENU_ANIMATION_CTL_SET_ACTIVE:
animation_is_active = true;
break;
case MENU_ANIMATION_CTL_DELTA_TIME:
{
float *ptr = (float*)data;
if (!ptr)
return false;
*ptr = delta_time;
}
break;
case MENU_ANIMATION_CTL_KILL_BY_TAG:
{
unsigned i;
menu_animation_ctx_tag_t *tag = (menu_animation_ctx_tag_t*)data;
if (!tag || tag->id == -1)
return false;
for (i = 0; i < anim.size; ++i)
{
if (anim.list[i].tag != tag->id)
continue;
anim.list[i].alive = false;
anim.list[i].subject = NULL;
if (i < anim.first_dead)
anim.first_dead = i;
}
}
break;
case MENU_ANIMATION_CTL_KILL_BY_SUBJECT:
{
unsigned i, j, killed = 0;
menu_animation_ctx_subject_t *subject =
(menu_animation_ctx_subject_t*)data;
float **sub = (float**)subject->data;
for (i = 0; i < anim.size; ++i)
{
if (!anim.list[i].alive)
continue;
for (j = 0; j < subject->count; ++j)
{
if (anim.list[i].subject != sub[j])
continue;
anim.list[i].alive = false;
anim.list[i].subject = NULL;
if (i < anim.first_dead)
anim.first_dead = i;
killed++;
break;
}
}
}
break;
case MENU_ANIMATION_CTL_NONE:
default:
break;
}
return true;
}