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
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
|
/**************************************************************************/
/* animation_player.cpp */
/**************************************************************************/
/* 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. */
/**************************************************************************/
#include "animation_player.h"
#include "core/config/engine.h"
#include "core/object/message_queue.h"
#include "scene/scene_string_names.h"
#include "servers/audio/audio_stream.h"
#ifdef TOOLS_ENABLED
#include "editor/editor_node.h"
#include "editor/editor_undo_redo_manager.h"
#include "scene/2d/skeleton_2d.h"
void AnimatedValuesBackup::update_skeletons() {
for (int i = 0; i < entries.size(); i++) {
if (entries[i].bone_idx != -1) {
// 3D bone
Object::cast_to<Skeleton3D>(entries[i].object)->notification(Skeleton3D::NOTIFICATION_UPDATE_SKELETON);
} else {
Bone2D *bone = Object::cast_to<Bone2D>(entries[i].object);
if (bone && bone->skeleton) {
// 2D bone
bone->skeleton->_update_transform();
}
}
}
}
void AnimatedValuesBackup::restore() const {
for (int i = 0; i < entries.size(); i++) {
const AnimatedValuesBackup::Entry *entry = &entries[i];
if (entry->bone_idx == -1) {
entry->object->set_indexed(entry->subpath, entry->value);
} else {
Array arr = entry->value;
if (arr.size() == 3) {
Object::cast_to<Skeleton3D>(entry->object)->set_bone_pose_position(entry->bone_idx, arr[0]);
Object::cast_to<Skeleton3D>(entry->object)->set_bone_pose_rotation(entry->bone_idx, arr[1]);
Object::cast_to<Skeleton3D>(entry->object)->set_bone_pose_scale(entry->bone_idx, arr[2]);
}
}
}
}
void AnimatedValuesBackup::_bind_methods() {
ClassDB::bind_method(D_METHOD("restore"), &AnimatedValuesBackup::restore);
}
#endif
bool AnimationPlayer::_set(const StringName &p_name, const Variant &p_value) {
String name = p_name;
if (name.begins_with("playback/play")) { // bw compatibility
set_current_animation(p_value);
} else if (name.begins_with("anims/")) {
// Backwards compatibility with 3.x, add them to "default" library.
String which = name.get_slicec('/', 1);
Ref<Animation> anim = p_value;
Ref<AnimationLibrary> al;
if (!has_animation_library(StringName())) {
al.instantiate();
add_animation_library(StringName(), al);
} else {
al = get_animation_library(StringName());
}
al->add_animation(which, anim);
} else if (name.begins_with("libraries")) {
Dictionary d = p_value;
while (animation_libraries.size()) {
remove_animation_library(animation_libraries[0].name);
}
List<Variant> keys;
d.get_key_list(&keys);
for (const Variant &K : keys) {
StringName lib_name = K;
Ref<AnimationLibrary> lib = d[lib_name];
add_animation_library(lib_name, lib);
}
emit_signal("animation_libraries_updated");
} else if (name.begins_with("next/")) {
String which = name.get_slicec('/', 1);
animation_set_next(which, p_value);
} else if (p_name == SceneStringNames::get_singleton()->blend_times) {
Array array = p_value;
int len = array.size();
ERR_FAIL_COND_V(len % 3, false);
for (int i = 0; i < len / 3; i++) {
StringName from = array[i * 3 + 0];
StringName to = array[i * 3 + 1];
float time = array[i * 3 + 2];
set_blend_time(from, to, time);
}
} else {
return false;
}
return true;
}
bool AnimationPlayer::_get(const StringName &p_name, Variant &r_ret) const {
String name = p_name;
if (name == "playback/play") { // bw compatibility
r_ret = get_current_animation();
} else if (name.begins_with("libraries")) {
Dictionary d;
for (const AnimationLibraryData &lib : animation_libraries) {
d[lib.name] = lib.library;
}
r_ret = d;
} else if (name.begins_with("next/")) {
String which = name.get_slicec('/', 1);
r_ret = animation_get_next(which);
} else if (name == "blend_times") {
Vector<BlendKey> keys;
for (const KeyValue<BlendKey, double> &E : blend_times) {
keys.ordered_insert(E.key);
}
Array array;
for (int i = 0; i < keys.size(); i++) {
array.push_back(keys[i].from);
array.push_back(keys[i].to);
array.push_back(blend_times.get(keys[i]));
}
r_ret = array;
} else {
return false;
}
return true;
}
void AnimationPlayer::_validate_property(PropertyInfo &p_property) const {
if (p_property.name == "current_animation") {
List<String> names;
for (const KeyValue<StringName, AnimationData> &E : animation_set) {
names.push_back(E.key);
}
names.sort();
names.push_front("[stop]");
String hint;
for (List<String>::Element *E = names.front(); E; E = E->next()) {
if (E != names.front()) {
hint += ",";
}
hint += E->get();
}
p_property.hint_string = hint;
}
}
void AnimationPlayer::_get_property_list(List<PropertyInfo> *p_list) const {
List<PropertyInfo> anim_names;
anim_names.push_back(PropertyInfo(Variant::DICTIONARY, "libraries"));
for (const KeyValue<StringName, AnimationData> &E : animation_set) {
if (E.value.next != StringName()) {
anim_names.push_back(PropertyInfo(Variant::STRING, "next/" + String(E.key), PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL));
}
}
anim_names.sort();
for (const PropertyInfo &E : anim_names) {
p_list->push_back(E);
}
p_list->push_back(PropertyInfo(Variant::ARRAY, "blend_times", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL));
}
void AnimationPlayer::advance(double p_time) {
_animation_process(p_time);
}
void AnimationPlayer::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_ENTER_TREE: {
if (!processing) {
//make sure that a previous process state was not saved
//only process if "processing" is set
set_physics_process_internal(false);
set_process_internal(false);
}
clear_caches();
} break;
case NOTIFICATION_READY: {
if (!Engine::get_singleton()->is_editor_hint() && animation_set.has(autoplay)) {
play(autoplay);
_animation_process(0);
}
} break;
case NOTIFICATION_INTERNAL_PROCESS: {
if (process_callback == ANIMATION_PROCESS_PHYSICS) {
break;
}
if (processing) {
_animation_process(get_process_delta_time());
}
} break;
case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
if (process_callback == ANIMATION_PROCESS_IDLE) {
break;
}
if (processing) {
_animation_process(get_physics_process_delta_time());
}
} break;
case NOTIFICATION_EXIT_TREE: {
clear_caches();
} break;
}
}
void AnimationPlayer::_ensure_node_caches(AnimationData *p_anim, Node *p_root_override) {
// Already cached?
if (p_anim->node_cache.size() == p_anim->animation->get_track_count()) {
return;
}
Node *parent = p_root_override ? p_root_override : get_node(root);
ERR_FAIL_COND(!parent);
Animation *a = p_anim->animation.operator->();
p_anim->node_cache.resize(a->get_track_count());
setup_pass++;
for (int i = 0; i < a->get_track_count(); i++) {
p_anim->node_cache.write[i] = nullptr;
if (!a->track_is_enabled(i)) {
continue;
}
Ref<Resource> resource;
Vector<StringName> leftover_path;
Node *child = parent->get_node_and_resource(a->track_get_path(i), resource, leftover_path);
ERR_CONTINUE_MSG(!child, "On Animation: '" + p_anim->name + "', couldn't resolve track: '" + String(a->track_get_path(i)) + "'."); // couldn't find the child node
ObjectID id = resource.is_valid() ? resource->get_instance_id() : child->get_instance_id();
int bone_idx = -1;
int blend_shape_idx = -1;
#ifndef _3D_DISABLED
if (a->track_get_path(i).get_subname_count() == 1 && Object::cast_to<Skeleton3D>(child)) {
Skeleton3D *sk = Object::cast_to<Skeleton3D>(child);
bone_idx = sk->find_bone(a->track_get_path(i).get_subname(0));
if (bone_idx == -1) {
continue;
}
}
if (a->track_get_type(i) == Animation::TYPE_BLEND_SHAPE) {
MeshInstance3D *mi_3d = Object::cast_to<MeshInstance3D>(child);
if (!mi_3d) {
continue;
}
if (a->track_get_path(i).get_subname_count() != 1) {
continue;
}
blend_shape_idx = mi_3d->find_blend_shape_by_name(a->track_get_path(i).get_subname(0));
if (blend_shape_idx == -1) {
continue;
}
}
#endif // _3D_DISABLED
if (!child->is_connected("tree_exiting", callable_mp(this, &AnimationPlayer::_node_removed))) {
child->connect("tree_exiting", callable_mp(this, &AnimationPlayer::_node_removed).bind(child), CONNECT_ONE_SHOT);
}
TrackNodeCacheKey key;
key.id = id;
key.bone_idx = bone_idx;
key.blend_shape_idx = blend_shape_idx;
if (!node_cache_map.has(key)) {
node_cache_map[key] = TrackNodeCache();
}
TrackNodeCache *node_cache = &node_cache_map[key];
p_anim->node_cache.write[i] = node_cache;
node_cache->path = a->track_get_path(i);
node_cache->node = child;
node_cache->resource = resource;
node_cache->node_2d = Object::cast_to<Node2D>(child);
#ifndef _3D_DISABLED
if (a->track_get_type(i) == Animation::TYPE_POSITION_3D || a->track_get_type(i) == Animation::TYPE_ROTATION_3D || a->track_get_type(i) == Animation::TYPE_SCALE_3D) {
// special cases and caches for transform tracks
if (node_cache->last_setup_pass != setup_pass) {
node_cache->loc_used = false;
node_cache->rot_used = false;
node_cache->scale_used = false;
}
// cache node_3d
node_cache->node_3d = Object::cast_to<Node3D>(child);
// cache skeleton
node_cache->skeleton = Object::cast_to<Skeleton3D>(child);
if (node_cache->skeleton) {
if (a->track_get_path(i).get_subname_count() == 1) {
StringName bone_name = a->track_get_path(i).get_subname(0);
node_cache->bone_idx = node_cache->skeleton->find_bone(bone_name);
if (node_cache->bone_idx < 0) {
// broken track (nonexistent bone)
node_cache->skeleton = nullptr;
node_cache->node_3d = nullptr;
ERR_CONTINUE(node_cache->bone_idx < 0);
}
Transform3D rest = node_cache->skeleton->get_bone_rest(bone_idx);
node_cache->init_loc = rest.origin;
node_cache->init_rot = rest.basis.get_rotation_quaternion();
node_cache->init_scale = rest.basis.get_scale();
} else {
// Not a skeleton, the node can be accessed with the node_3d member.
node_cache->skeleton = nullptr;
}
}
switch (a->track_get_type(i)) {
case Animation::TYPE_POSITION_3D: {
node_cache->loc_used = true;
} break;
case Animation::TYPE_ROTATION_3D: {
node_cache->rot_used = true;
} break;
case Animation::TYPE_SCALE_3D: {
node_cache->scale_used = true;
} break;
default: {
}
}
}
if (a->track_get_type(i) == Animation::TYPE_BLEND_SHAPE) {
// special cases and caches for transform tracks
node_cache->node_blend_shape = Object::cast_to<MeshInstance3D>(child);
node_cache->blend_shape_idx = blend_shape_idx;
}
#endif // _3D_DISABLED
if (a->track_get_type(i) == Animation::TYPE_VALUE) {
if (!node_cache->property_anim.has(a->track_get_path(i).get_concatenated_subnames())) {
TrackNodeCache::PropertyAnim pa;
pa.subpath = leftover_path;
pa.object = resource.is_valid() ? (Object *)resource.ptr() : (Object *)child;
pa.special = SP_NONE;
pa.owner = p_anim->node_cache[i];
if (false && node_cache->node_2d) {
if (leftover_path.size() == 1 && leftover_path[0] == SceneStringNames::get_singleton()->transform_pos) {
pa.special = SP_NODE2D_POS;
} else if (leftover_path.size() == 1 && leftover_path[0] == SceneStringNames::get_singleton()->transform_rot) {
pa.special = SP_NODE2D_ROT;
} else if (leftover_path.size() == 1 && leftover_path[0] == SceneStringNames::get_singleton()->transform_scale) {
pa.special = SP_NODE2D_SCALE;
}
}
node_cache->property_anim[a->track_get_path(i).get_concatenated_subnames()] = pa;
}
}
if (a->track_get_type(i) == Animation::TYPE_BEZIER && leftover_path.size()) {
if (!node_cache->bezier_anim.has(a->track_get_path(i).get_concatenated_subnames())) {
TrackNodeCache::BezierAnim ba;
ba.bezier_property = leftover_path;
ba.object = resource.is_valid() ? (Object *)resource.ptr() : (Object *)child;
ba.owner = p_anim->node_cache[i];
node_cache->bezier_anim[a->track_get_path(i).get_concatenated_subnames()] = ba;
}
}
node_cache->last_setup_pass = setup_pass;
}
}
static void _call_object(Object *p_object, const StringName &p_method, const Vector<Variant> &p_params, bool p_deferred) {
// Separate function to use alloca() more efficiently
const Variant **argptrs = (const Variant **)alloca(sizeof(const Variant **) * p_params.size());
const Variant *args = p_params.ptr();
uint32_t argcount = p_params.size();
for (uint32_t i = 0; i < argcount; i++) {
argptrs[i] = &args[i];
}
if (p_deferred) {
MessageQueue::get_singleton()->push_callp(p_object, p_method, argptrs, argcount);
} else {
Callable::CallError ce;
p_object->callp(p_method, argptrs, argcount, ce);
}
}
Variant AnimationPlayer::_post_process_key_value(const Ref<Animation> &p_anim, int p_track, Variant p_value, const Object *p_object, int p_object_idx) {
switch (p_anim->track_get_type(p_track)) {
#ifndef _3D_DISABLED
case Animation::TYPE_POSITION_3D: {
if (p_object_idx >= 0) {
const Skeleton3D *skel = Object::cast_to<Skeleton3D>(p_object);
return Vector3(p_value) * skel->get_motion_scale();
}
return p_value;
} break;
#endif // _3D_DISABLED
default: {
} break;
}
return p_value;
}
void AnimationPlayer::_animation_process_animation(AnimationData *p_anim, double p_prev_time, double p_time, double p_delta, float p_interp, bool p_is_current, bool p_seeked, bool p_started, Animation::LoopedFlag p_looped_flag) {
_ensure_node_caches(p_anim);
ERR_FAIL_COND(p_anim->node_cache.size() != p_anim->animation->get_track_count());
Animation *a = p_anim->animation.operator->();
bool can_call = is_inside_tree() && !Engine::get_singleton()->is_editor_hint();
bool backward = signbit(p_delta);
for (int i = 0; i < a->get_track_count(); i++) {
// If an animation changes this animation (or it animates itself)
// we need to recreate our animation cache
if (p_anim->node_cache.size() != a->get_track_count()) {
_ensure_node_caches(p_anim);
}
TrackNodeCache *nc = p_anim->node_cache[i];
if (!nc) {
continue; // no node cache for this track, skip it
}
if (!a->track_is_enabled(i)) {
continue; // do nothing if the track is disabled
}
if (a->track_get_key_count(i) == 0) {
continue; // do nothing if track is empty
}
switch (a->track_get_type(i)) {
case Animation::TYPE_POSITION_3D: {
#ifndef _3D_DISABLED
if (!nc->node_3d) {
continue;
}
Vector3 loc;
Error err = a->position_track_interpolate(i, p_time, &loc);
//ERR_CONTINUE(err!=OK); //used for testing, should be removed
if (err != OK) {
continue;
}
loc = _post_process_key_value(a, i, loc, nc->node_3d, nc->bone_idx);
if (nc->accum_pass != accum_pass) {
ERR_CONTINUE(cache_update_size >= NODE_CACHE_UPDATE_MAX);
cache_update[cache_update_size++] = nc;
nc->accum_pass = accum_pass;
nc->loc_accum = loc;
nc->rot_accum = nc->init_rot;
nc->scale_accum = nc->init_scale;
} else {
nc->loc_accum = nc->loc_accum.lerp(loc, p_interp);
}
#endif // _3D_DISABLED
} break;
case Animation::TYPE_ROTATION_3D: {
#ifndef _3D_DISABLED
if (!nc->node_3d) {
continue;
}
Quaternion rot;
Error err = a->rotation_track_interpolate(i, p_time, &rot);
//ERR_CONTINUE(err!=OK); //used for testing, should be removed
if (err != OK) {
continue;
}
rot = _post_process_key_value(a, i, rot, nc->node_3d, nc->bone_idx);
if (nc->accum_pass != accum_pass) {
ERR_CONTINUE(cache_update_size >= NODE_CACHE_UPDATE_MAX);
cache_update[cache_update_size++] = nc;
nc->accum_pass = accum_pass;
nc->loc_accum = nc->init_loc;
nc->rot_accum = rot;
nc->scale_accum = nc->init_scale;
} else {
nc->rot_accum = nc->rot_accum.slerp(rot, p_interp);
}
#endif // _3D_DISABLED
} break;
case Animation::TYPE_SCALE_3D: {
#ifndef _3D_DISABLED
if (!nc->node_3d) {
continue;
}
Vector3 scale;
Error err = a->scale_track_interpolate(i, p_time, &scale);
//ERR_CONTINUE(err!=OK); //used for testing, should be removed
if (err != OK) {
continue;
}
scale = _post_process_key_value(a, i, scale, nc->node_3d, nc->bone_idx);
if (nc->accum_pass != accum_pass) {
ERR_CONTINUE(cache_update_size >= NODE_CACHE_UPDATE_MAX);
cache_update[cache_update_size++] = nc;
nc->accum_pass = accum_pass;
nc->loc_accum = nc->init_loc;
nc->rot_accum = nc->init_rot;
nc->scale_accum = scale;
} else {
nc->scale_accum = nc->scale_accum.lerp(scale, p_interp);
}
#endif // _3D_DISABLED
} break;
case Animation::TYPE_BLEND_SHAPE: {
#ifndef _3D_DISABLED
if (!nc->node_blend_shape) {
continue;
}
float blend;
Error err = a->blend_shape_track_interpolate(i, p_time, &blend);
//ERR_CONTINUE(err!=OK); //used for testing, should be removed
if (err != OK) {
continue;
}
blend = _post_process_key_value(a, i, blend, nc->node_blend_shape, nc->blend_shape_idx);
if (nc->accum_pass != accum_pass) {
ERR_CONTINUE(cache_update_size >= NODE_CACHE_UPDATE_MAX);
nc->accum_pass = accum_pass;
cache_update[cache_update_size++] = nc;
nc->blend_shape_accum = blend;
} else {
nc->blend_shape_accum = Math::lerp(nc->blend_shape_accum, blend, p_interp);
}
#endif // _3D_DISABLED
} break;
case Animation::TYPE_VALUE: {
if (!nc->node) {
continue;
}
//StringName property=a->track_get_path(i).get_property();
HashMap<StringName, TrackNodeCache::PropertyAnim>::Iterator E = nc->property_anim.find(a->track_get_path(i).get_concatenated_subnames());
ERR_CONTINUE(!E); //should it continue, or create a new one?
TrackNodeCache::PropertyAnim *pa = &E->value;
Animation::UpdateMode update_mode = a->value_track_get_update_mode(i);
if (update_mode == Animation::UPDATE_CAPTURE) {
if (p_started || pa->capture == Variant()) {
pa->capture = pa->object->get_indexed(pa->subpath);
}
int key_count = a->track_get_key_count(i);
if (key_count == 0) {
continue; //eeh not worth it
}
double first_key_time = a->track_get_key_time(i, 0);
double transition = 1.0;
int first_key = 0;
if (first_key_time == 0.0) {
//ignore, use for transition
if (key_count == 1) {
continue; //with one key we can't do anything
}
transition = (double)a->track_get_key_transition(i, 0);
first_key_time = a->track_get_key_time(i, 1);
first_key = 1;
}
if (p_time < first_key_time) {
double c = Math::ease(p_time / first_key_time, transition);
Variant first_value = a->track_get_key_value(i, first_key);
first_value = _post_process_key_value(a, i, first_value, nc->node);
Variant interp_value = Animation::interpolate_variant(pa->capture, first_value, c);
if (pa->accum_pass != accum_pass) {
ERR_CONTINUE(cache_update_prop_size >= NODE_CACHE_UPDATE_MAX);
cache_update_prop[cache_update_prop_size++] = pa;
pa->value_accum = interp_value;
pa->accum_pass = accum_pass;
} else {
pa->value_accum = Animation::interpolate_variant(pa->value_accum, interp_value, p_interp);
}
continue; //handled
}
}
if (update_mode == Animation::UPDATE_CONTINUOUS || update_mode == Animation::UPDATE_CAPTURE) {
Variant value = a->value_track_interpolate(i, p_time);
if (value == Variant()) {
continue;
}
value = _post_process_key_value(a, i, value, nc->node);
if (pa->accum_pass != accum_pass) {
ERR_CONTINUE(cache_update_prop_size >= NODE_CACHE_UPDATE_MAX);
cache_update_prop[cache_update_prop_size++] = pa;
pa->value_accum = value;
pa->accum_pass = accum_pass;
} else {
pa->value_accum = Animation::interpolate_variant(pa->value_accum, value, p_interp);
}
} else {
List<int> indices;
if (p_seeked) {
int found_key = a->track_find_key(i, p_time);
if (found_key >= 0) {
indices.push_back(found_key);
}
} else {
if (p_started) {
int first_key = a->track_find_key(i, p_prev_time, Animation::FIND_MODE_EXACT);
if (first_key >= 0) {
indices.push_back(first_key);
}
}
a->track_get_key_indices_in_range(i, p_time, p_delta, &indices, p_looped_flag);
}
for (int &F : indices) {
Variant value = a->track_get_key_value(i, F);
value = _post_process_key_value(a, i, value, nc->node);
switch (pa->special) {
case SP_NONE: {
bool valid;
pa->object->set_indexed(pa->subpath, value, &valid); //you are not speshul
#ifdef DEBUG_ENABLED
if (!valid) {
ERR_PRINT("Failed setting track value '" + String(pa->owner->path) + "'. Check if the property exists or the type of key is valid. Animation '" + a->get_name() + "' at node '" + get_path() + "'.");
}
#endif
} break;
case SP_NODE2D_POS: {
#ifdef DEBUG_ENABLED
if (value.get_type() != Variant::VECTOR2) {
ERR_PRINT("Position key at time " + rtos(p_time) + " in Animation Track '" + String(pa->owner->path) + "' not of type Vector2(). Animation '" + a->get_name() + "' at node '" + get_path() + "'.");
}
#endif
static_cast<Node2D *>(pa->object)->set_position(value);
} break;
case SP_NODE2D_ROT: {
#ifdef DEBUG_ENABLED
if (value.is_num()) {
ERR_PRINT("Rotation key at time " + rtos(p_time) + " in Animation Track '" + String(pa->owner->path) + "' not numerical. Animation '" + a->get_name() + "' at node '" + get_path() + "'.");
}
#endif
static_cast<Node2D *>(pa->object)->set_rotation((double)value);
} break;
case SP_NODE2D_SCALE: {
#ifdef DEBUG_ENABLED
if (value.get_type() != Variant::VECTOR2) {
ERR_PRINT("Scale key at time " + rtos(p_time) + " in Animation Track '" + String(pa->owner->path) + "' not of type Vector2()." + a->get_name() + "' at node '" + get_path() + "'.");
}
#endif
static_cast<Node2D *>(pa->object)->set_scale(value);
} break;
}
}
}
} break;
case Animation::TYPE_METHOD: {
if (!nc->node || is_stopping) {
continue;
}
if (!p_is_current) {
break;
}
List<int> indices;
if (p_seeked) {
int found_key = a->track_find_key(i, p_time);
if (found_key >= 0) {
indices.push_back(found_key);
}
} else {
if (p_started) {
int first_key = a->track_find_key(i, p_prev_time, Animation::FIND_MODE_EXACT);
if (first_key >= 0) {
indices.push_back(first_key);
}
}
a->track_get_key_indices_in_range(i, p_time, p_delta, &indices, p_looped_flag);
}
for (int &E : indices) {
StringName method = a->method_track_get_name(i, E);
Vector<Variant> params = a->method_track_get_params(i, E);
#ifdef DEBUG_ENABLED
if (!nc->node->has_method(method)) {
ERR_PRINT("Invalid method call '" + method + "'. '" + a->get_name() + "' at node '" + get_path() + "'.");
}
#endif
if (can_call) {
_call_object(nc->node, method, params, method_call_mode == ANIMATION_METHOD_CALL_DEFERRED);
}
}
} break;
case Animation::TYPE_BEZIER: {
if (!nc->node) {
continue;
}
HashMap<StringName, TrackNodeCache::BezierAnim>::Iterator E = nc->bezier_anim.find(a->track_get_path(i).get_concatenated_subnames());
ERR_CONTINUE(!E); //should it continue, or create a new one?
TrackNodeCache::BezierAnim *ba = &E->value;
real_t bezier = a->bezier_track_interpolate(i, p_time);
bezier = _post_process_key_value(a, i, bezier, nc->node);
if (ba->accum_pass != accum_pass) {
ERR_CONTINUE(cache_update_bezier_size >= NODE_CACHE_UPDATE_MAX);
cache_update_bezier[cache_update_bezier_size++] = ba;
ba->bezier_accum = bezier;
ba->accum_pass = accum_pass;
} else {
ba->bezier_accum = Math::lerp(ba->bezier_accum, (float)bezier, p_interp);
}
} break;
case Animation::TYPE_AUDIO: {
if (!nc->node || is_stopping) {
continue;
}
if (p_seeked) {
//find whatever should be playing
int idx = a->track_find_key(i, p_time);
if (idx < 0) {
continue;
}
Ref<AudioStream> stream = a->audio_track_get_key_stream(i, idx);
if (!stream.is_valid()) {
nc->node->call(SNAME("stop"));
nc->audio_playing = false;
playing_caches.erase(nc);
} else {
float start_ofs = a->audio_track_get_key_start_offset(i, idx);
start_ofs += p_time - a->track_get_key_time(i, idx);
float end_ofs = a->audio_track_get_key_end_offset(i, idx);
float len = stream->get_length();
if (start_ofs > len - end_ofs) {
nc->node->call(SNAME("stop"));
nc->audio_playing = false;
playing_caches.erase(nc);
continue;
}
nc->node->call(SNAME("set_stream"), stream);
nc->node->call(SNAME("play"), start_ofs);
nc->audio_playing = true;
playing_caches.insert(nc);
if (len && end_ofs > 0) { //force an end at a time
nc->audio_len = len - start_ofs - end_ofs;
} else {
nc->audio_len = 0;
}
nc->audio_start = p_time;
}
} else {
//find stuff to play
List<int> to_play;
if (p_started) {
int first_key = a->track_find_key(i, p_prev_time, Animation::FIND_MODE_EXACT);
if (first_key >= 0) {
to_play.push_back(first_key);
}
}
a->track_get_key_indices_in_range(i, p_time, p_delta, &to_play, p_looped_flag);
if (to_play.size()) {
int idx = to_play.back()->get();
Ref<AudioStream> stream = a->audio_track_get_key_stream(i, idx);
if (!stream.is_valid()) {
nc->node->call(SNAME("stop"));
nc->audio_playing = false;
playing_caches.erase(nc);
} else {
float start_ofs = a->audio_track_get_key_start_offset(i, idx);
float end_ofs = a->audio_track_get_key_end_offset(i, idx);
float len = stream->get_length();
nc->node->call(SNAME("set_stream"), stream);
nc->node->call(SNAME("play"), start_ofs);
nc->audio_playing = true;
playing_caches.insert(nc);
if (len && end_ofs > 0) { //force an end at a time
nc->audio_len = len - start_ofs - end_ofs;
} else {
nc->audio_len = 0;
}
nc->audio_start = p_time;
}
} else if (nc->audio_playing) {
bool loop = a->get_loop_mode() != Animation::LOOP_NONE;
bool stop = false;
if (!loop) {
if ((p_time < nc->audio_start && !backward) || (p_time > nc->audio_start && backward)) {
stop = true;
}
} else if (nc->audio_len > 0) {
float len = nc->audio_start > p_time ? (a->get_length() - nc->audio_start) + p_time : p_time - nc->audio_start;
if (len > nc->audio_len) {
stop = true;
}
}
if (stop) {
//time to stop
nc->node->call(SNAME("stop"));
nc->audio_playing = false;
playing_caches.erase(nc);
}
}
}
} break;
case Animation::TYPE_ANIMATION: {
if (is_stopping) {
continue;
}
AnimationPlayer *player = Object::cast_to<AnimationPlayer>(nc->node);
if (!player) {
continue;
}
if (p_seeked) {
//seek
int idx = a->track_find_key(i, p_time);
if (idx < 0) {
continue;
}
double pos = a->track_get_key_time(i, idx);
StringName anim_name = a->animation_track_get_key_animation(i, idx);
if (String(anim_name) == "[stop]" || !player->has_animation(anim_name)) {
continue;
}
Ref<Animation> anim = player->get_animation(anim_name);
double at_anim_pos = 0.0;
switch (anim->get_loop_mode()) {
case Animation::LOOP_NONE: {
at_anim_pos = MIN((double)anim->get_length(), p_time - pos); //seek to end
} break;
case Animation::LOOP_LINEAR: {
at_anim_pos = Math::fposmod(p_time - pos, (double)anim->get_length()); //seek to loop
} break;
case Animation::LOOP_PINGPONG: {
at_anim_pos = Math::pingpong(p_time - pos, (double)anim->get_length());
} break;
default:
break;
}
if (player->is_playing()) {
player->seek(at_anim_pos);
player->play(anim_name);
nc->animation_playing = true;
playing_caches.insert(nc);
} else {
player->set_assigned_animation(anim_name);
player->seek(at_anim_pos, true);
}
} else {
//find stuff to play
List<int> to_play;
if (p_started) {
int first_key = a->track_find_key(i, p_prev_time, Animation::FIND_MODE_EXACT);
if (first_key >= 0) {
to_play.push_back(first_key);
}
}
a->track_get_key_indices_in_range(i, p_time, p_delta, &to_play, p_looped_flag);
if (to_play.size()) {
int idx = to_play.back()->get();
StringName anim_name = a->animation_track_get_key_animation(i, idx);
if (String(anim_name) == "[stop]" || !player->has_animation(anim_name)) {
if (playing_caches.has(nc)) {
playing_caches.erase(nc);
player->stop();
nc->animation_playing = false;
}
} else {
player->seek(0.0);
player->play(anim_name);
nc->animation_playing = true;
playing_caches.insert(nc);
}
}
}
} break;
}
}
}
void AnimationPlayer::_animation_process_data(PlaybackData &cd, double p_delta, float p_blend, bool p_seeked, bool p_started) {
double delta = p_delta * speed_scale * cd.speed_scale;
double next_pos = cd.pos + delta;
bool backwards = signbit(delta); // Negative zero means playing backwards too.
real_t len = cd.from->animation->get_length();
Animation::LoopedFlag looped_flag = Animation::LOOPED_FLAG_NONE;
switch (cd.from->animation->get_loop_mode()) {
case Animation::LOOP_NONE: {
if (next_pos < 0) {
next_pos = 0;
} else if (next_pos > len) {
next_pos = len;
}
delta = next_pos - cd.pos; // Fix delta (after determination of backwards because negative zero is lost here).
} break;
case Animation::LOOP_LINEAR: {
if (next_pos < 0 && cd.pos >= 0) {
looped_flag = Animation::LOOPED_FLAG_START;
}
if (next_pos > len && cd.pos <= len) {
looped_flag = Animation::LOOPED_FLAG_END;
}
next_pos = Math::fposmod(next_pos, (double)len);
} break;
case Animation::LOOP_PINGPONG: {
if (next_pos < 0 && cd.pos >= 0) {
cd.speed_scale *= -1.0;
looped_flag = Animation::LOOPED_FLAG_START;
}
if (next_pos > len && cd.pos <= len) {
cd.speed_scale *= -1.0;
looped_flag = Animation::LOOPED_FLAG_END;
}
next_pos = Math::pingpong(next_pos, (double)len);
} break;
default:
break;
}
double prev_pos = cd.pos; // The animation may be changed during process, so it is safer that the state is changed before process.
cd.pos = next_pos;
AnimationData *prev_from = cd.from;
_animation_process_animation(cd.from, prev_pos, cd.pos, delta, p_blend, &cd == &playback.current, p_seeked, p_started, looped_flag);
// End detection.
if (cd.from->animation->get_loop_mode() == Animation::LOOP_NONE) {
if (prev_from != playback.current.from) {
return; // Animation has been changed in the process (may be caused by method track), abort process.
}
if (!backwards && prev_pos <= len && next_pos == len) {
// Playback finished.
end_reached = true;
end_notify = prev_pos < len; // Notify only if not already at the end.
}
if (backwards && prev_pos >= 0 && next_pos == 0) {
// Playback finished.
end_reached = true;
end_notify = prev_pos > 0; // Notify only if not already at the beginning.
}
}
}
void AnimationPlayer::_animation_process2(double p_delta, bool p_started) {
Playback &c = playback;
accum_pass++;
bool seeked = c.seeked; // The animation may be changed during process, so it is safer that the state is changed before process.
if (p_delta != 0) {
c.seeked = false;
}
float blend = 1.0; // First animation we play at 100% blend
List<Blend>::Element *next = NULL;
for (List<Blend>::Element *E = c.blend.front(); E; E = next) {
Blend &b = E->get();
// Note: There may be issues if an animation event triggers an animation change while this blend is active,
// so it is best to use "deferred" calls instead of "immediate" for animation events that can trigger new animations.
_animation_process_data(b.data, p_delta, blend, false, false);
blend = 1.0 - b.blend_left / b.blend_time; // This is how much to blend the NEXT animation
b.blend_left -= Math::absf(speed_scale * p_delta);
next = E->next();
if (b.blend_left < 0) {
// If the blend of this has finished, we need to remove ALL the previous blends
List<Blend>::Element *prev;
while (E) {
prev = E->prev();
c.blend.erase(E);
E = prev;
}
}
}
_animation_process_data(c.current, p_delta, blend, seeked, p_started);
}
void AnimationPlayer::_animation_update_transforms() {
{
Transform3D t;
for (int i = 0; i < cache_update_size; i++) {
TrackNodeCache *nc = cache_update[i];
ERR_CONTINUE(nc->accum_pass != accum_pass);
#ifndef _3D_DISABLED
if (nc->skeleton && nc->bone_idx >= 0) {
if (nc->loc_used) {
nc->skeleton->set_bone_pose_position(nc->bone_idx, nc->loc_accum);
}
if (nc->rot_used) {
nc->skeleton->set_bone_pose_rotation(nc->bone_idx, nc->rot_accum);
}
if (nc->scale_used) {
nc->skeleton->set_bone_pose_scale(nc->bone_idx, nc->scale_accum);
}
} else if (nc->node_blend_shape) {
nc->node_blend_shape->set_blend_shape_value(nc->blend_shape_idx, nc->blend_shape_accum);
} else if (nc->node_3d) {
if (nc->loc_used) {
nc->node_3d->set_position(nc->loc_accum);
}
if (nc->rot_used) {
nc->node_3d->set_rotation(nc->rot_accum.get_euler());
}
if (nc->scale_used) {
nc->node_3d->set_scale(nc->scale_accum);
}
}
#endif // _3D_DISABLED
}
}
for (int i = 0; i < cache_update_prop_size; i++) {
TrackNodeCache::PropertyAnim *pa = cache_update_prop[i];
ERR_CONTINUE(pa->accum_pass != accum_pass);
switch (pa->special) {
case SP_NONE: {
bool valid;
pa->object->set_indexed(pa->subpath, pa->value_accum, &valid); //you are not speshul
#ifdef DEBUG_ENABLED
if (!valid) {
// Get subpath as string for printing the error
// Cannot use `String::join(Vector<String>)` because this is a vector of StringName
String key_debug;
if (pa->subpath.size() > 0) {
key_debug = pa->subpath[0];
for (int subpath_index = 1; subpath_index < pa->subpath.size(); ++subpath_index) {
key_debug += ".";
key_debug += pa->subpath[subpath_index];
}
}
ERR_PRINT("Failed setting key '" + key_debug +
"' at time " + rtos(playback.current.pos) +
" in Animation '" + get_current_animation() +
"' at Node '" + get_path() +
"', Track '" + String(pa->owner->path) +
"'. Check if the property exists or the type of key is right for the property.");
}
#endif
} break;
case SP_NODE2D_POS: {
#ifdef DEBUG_ENABLED
if (pa->value_accum.get_type() != Variant::VECTOR2) {
ERR_PRINT("Position key at time " + rtos(playback.current.pos) + " in Animation '" + get_current_animation() + "' at Node '" + get_path() + "', Track '" + String(pa->owner->path) + "' not of type Vector2()");
}
#endif
static_cast<Node2D *>(pa->object)->set_position(pa->value_accum);
} break;
case SP_NODE2D_ROT: {
#ifdef DEBUG_ENABLED
if (pa->value_accum.is_num()) {
ERR_PRINT("Rotation key at time " + rtos(playback.current.pos) + " in Animation '" + get_current_animation() + "' at Node '" + get_path() + "', Track '" + String(pa->owner->path) + "' not numerical");
}
#endif
static_cast<Node2D *>(pa->object)->set_rotation(Math::deg_to_rad((double)pa->value_accum));
} break;
case SP_NODE2D_SCALE: {
#ifdef DEBUG_ENABLED
if (pa->value_accum.get_type() != Variant::VECTOR2) {
ERR_PRINT("Scale key at time " + rtos(playback.current.pos) + " in Animation '" + get_current_animation() + "' at Node '" + get_path() + "', Track '" + String(pa->owner->path) + "' not of type Vector2()");
}
#endif
static_cast<Node2D *>(pa->object)->set_scale(pa->value_accum);
} break;
}
}
for (int i = 0; i < cache_update_bezier_size; i++) {
TrackNodeCache::BezierAnim *ba = cache_update_bezier[i];
ERR_CONTINUE(ba->accum_pass != accum_pass);
ba->object->set_indexed(ba->bezier_property, ba->bezier_accum);
}
}
void AnimationPlayer::_animation_process(double p_delta) {
if (playback.current.from) {
end_reached = false;
end_notify = false;
bool started = playback.started; // The animation may be changed during process, so it is safer that the state is changed before process.
if (playback.started) {
playback.started = false;
}
cache_update_size = 0;
cache_update_prop_size = 0;
cache_update_bezier_size = 0;
AnimationData *prev_from = playback.current.from;
_animation_process2(p_delta, started);
if (prev_from != playback.current.from) {
return; // Animation has been changed in the process (may be caused by method track), abort process.
}
_animation_update_transforms();
if (end_reached) {
if (queued.size()) {
String old = playback.assigned;
play(queued.front()->get());
String new_name = playback.assigned;
queued.pop_front();
if (end_notify) {
emit_signal(SceneStringNames::get_singleton()->animation_changed, old, new_name);
}
} else {
playing = false;
_set_process(false);
if (end_notify) {
emit_signal(SceneStringNames::get_singleton()->animation_finished, playback.assigned);
if (movie_quit_on_finish && OS::get_singleton()->has_feature("movie")) {
print_line(vformat("Movie Maker mode is enabled. Quitting on animation finish as requested by: %s", get_path()));
get_tree()->quit();
}
}
}
end_reached = false;
}
} else {
_set_process(false);
}
}
void AnimationPlayer::_animation_set_cache_update() {
// Relatively fast function to update all animations.
animation_set_update_pass++;
bool clear_cache_needed = false;
// Update changed and add otherwise
for (const AnimationLibraryData &lib : animation_libraries) {
for (const KeyValue<StringName, Ref<Animation>> &K : lib.library->animations) {
StringName key = lib.name == StringName() ? K.key : StringName(String(lib.name) + "/" + String(K.key));
if (!animation_set.has(key)) {
AnimationData ad;
ad.animation = K.value;
ad.animation_library = lib.name;
ad.name = key;
ad.last_update = animation_set_update_pass;
animation_set.insert(ad.name, ad);
} else {
AnimationData &ad = animation_set[key];
if (ad.last_update != animation_set_update_pass) {
// Was not updated, update. If the animation is duplicated, the second one will be ignored.
if (ad.animation != K.value || ad.animation_library != lib.name) {
// Animation changed, update and clear caches.
clear_cache_needed = true;
ad.animation = K.value;
ad.animation_library = lib.name;
}
ad.last_update = animation_set_update_pass;
}
}
}
}
// Check removed
List<StringName> to_erase;
for (const KeyValue<StringName, AnimationData> &E : animation_set) {
if (E.value.last_update != animation_set_update_pass) {
// Was not updated, must be erased
to_erase.push_back(E.key);
clear_cache_needed = true;
}
}
while (to_erase.size()) {
animation_set.erase(to_erase.front()->get());
to_erase.pop_front();
}
if (clear_cache_needed) {
// If something was modified or removed, caches need to be cleared
clear_caches();
}
emit_signal(SNAME("animation_list_changed"));
}
void AnimationPlayer::_animation_added(const StringName &p_name, const StringName &p_library) {
_animation_set_cache_update();
}
void AnimationPlayer::_animation_removed(const StringName &p_name, const StringName &p_library) {
StringName name = p_library == StringName() ? p_name : StringName(String(p_library) + "/" + String(p_name));
if (!animation_set.has(name)) {
return; // No need to update because not the one from the library being used.
}
_animation_set_cache_update();
// Erase blends if needed
List<BlendKey> to_erase;
for (const KeyValue<BlendKey, double> &E : blend_times) {
BlendKey bk = E.key;
if (bk.from == name || bk.to == name) {
to_erase.push_back(bk);
}
}
while (to_erase.size()) {
blend_times.erase(to_erase.front()->get());
to_erase.pop_front();
}
}
void AnimationPlayer::_rename_animation(const StringName &p_from_name, const StringName &p_to_name) {
// Rename autoplay or blends if needed.
List<BlendKey> to_erase;
HashMap<BlendKey, double, BlendKey> to_insert;
for (const KeyValue<BlendKey, double> &E : blend_times) {
BlendKey bk = E.key;
BlendKey new_bk = bk;
bool erase = false;
if (bk.from == p_from_name) {
new_bk.from = p_to_name;
erase = true;
}
if (bk.to == p_from_name) {
new_bk.to = p_to_name;
erase = true;
}
if (erase) {
to_erase.push_back(bk);
to_insert[new_bk] = E.value;
}
}
while (to_erase.size()) {
blend_times.erase(to_erase.front()->get());
to_erase.pop_front();
}
while (to_insert.size()) {
blend_times[to_insert.begin()->key] = to_insert.begin()->value;
to_insert.remove(to_insert.begin());
}
if (autoplay == p_from_name) {
autoplay = p_to_name;
}
}
void AnimationPlayer::_animation_renamed(const StringName &p_name, const StringName &p_to_name, const StringName &p_library) {
StringName from_name = p_library == StringName() ? p_name : StringName(String(p_library) + "/" + String(p_name));
StringName to_name = p_library == StringName() ? p_to_name : StringName(String(p_library) + "/" + String(p_to_name));
if (!animation_set.has(from_name)) {
return; // No need to update because not the one from the library being used.
}
_animation_set_cache_update();
_rename_animation(from_name, to_name);
}
Error AnimationPlayer::add_animation_library(const StringName &p_name, const Ref<AnimationLibrary> &p_animation_library) {
ERR_FAIL_COND_V(p_animation_library.is_null(), ERR_INVALID_PARAMETER);
#ifdef DEBUG_ENABLED
ERR_FAIL_COND_V_MSG(String(p_name).contains("/") || String(p_name).contains(":") || String(p_name).contains(",") || String(p_name).contains("["), ERR_INVALID_PARAMETER, "Invalid animation name: " + String(p_name) + ".");
#endif
int insert_pos = 0;
for (const AnimationLibraryData &lib : animation_libraries) {
ERR_FAIL_COND_V_MSG(lib.name == p_name, ERR_ALREADY_EXISTS, "Can't add animation library twice with name: " + String(p_name));
ERR_FAIL_COND_V_MSG(lib.library == p_animation_library, ERR_ALREADY_EXISTS, "Can't add animation library twice (adding as '" + p_name.operator String() + "', exists as '" + lib.name.operator String() + "'.");
if (lib.name.operator String() >= p_name.operator String()) {
break;
}
insert_pos++;
}
AnimationLibraryData ald;
ald.name = p_name;
ald.library = p_animation_library;
animation_libraries.insert(insert_pos, ald);
ald.library->connect(SNAME("animation_added"), callable_mp(this, &AnimationPlayer::_animation_added).bind(p_name));
ald.library->connect(SNAME("animation_removed"), callable_mp(this, &AnimationPlayer::_animation_removed).bind(p_name));
ald.library->connect(SNAME("animation_renamed"), callable_mp(this, &AnimationPlayer::_animation_renamed).bind(p_name));
ald.library->connect(SNAME("animation_changed"), callable_mp(this, &AnimationPlayer::_animation_changed));
_animation_set_cache_update();
notify_property_list_changed();
return OK;
}
void AnimationPlayer::remove_animation_library(const StringName &p_name) {
int at_pos = -1;
for (uint32_t i = 0; i < animation_libraries.size(); i++) {
if (animation_libraries[i].name == p_name) {
at_pos = i;
break;
}
}
ERR_FAIL_COND(at_pos == -1);
animation_libraries[at_pos].library->disconnect(SNAME("animation_added"), callable_mp(this, &AnimationPlayer::_animation_added));
animation_libraries[at_pos].library->disconnect(SNAME("animation_removed"), callable_mp(this, &AnimationPlayer::_animation_removed));
animation_libraries[at_pos].library->disconnect(SNAME("animation_renamed"), callable_mp(this, &AnimationPlayer::_animation_renamed));
animation_libraries[at_pos].library->disconnect(SNAME("animation_changed"), callable_mp(this, &AnimationPlayer::_animation_changed));
stop();
animation_libraries.remove_at(at_pos);
_animation_set_cache_update();
notify_property_list_changed();
}
void AnimationPlayer::rename_animation_library(const StringName &p_name, const StringName &p_new_name) {
if (p_name == p_new_name) {
return;
}
#ifdef DEBUG_ENABLED
ERR_FAIL_COND_MSG(String(p_new_name).contains("/") || String(p_new_name).contains(":") || String(p_new_name).contains(",") || String(p_new_name).contains("["), "Invalid animation library name: " + String(p_new_name) + ".");
#endif
bool found = false;
for (AnimationLibraryData &lib : animation_libraries) {
ERR_FAIL_COND_MSG(lib.name == p_new_name, "Can't rename animation library to another existing name: " + String(p_new_name));
if (lib.name == p_name) {
found = true;
lib.name = p_new_name;
// rename connections
lib.library->disconnect(SNAME("animation_added"), callable_mp(this, &AnimationPlayer::_animation_added));
lib.library->disconnect(SNAME("animation_removed"), callable_mp(this, &AnimationPlayer::_animation_removed));
lib.library->disconnect(SNAME("animation_renamed"), callable_mp(this, &AnimationPlayer::_animation_renamed));
lib.library->connect(SNAME("animation_added"), callable_mp(this, &AnimationPlayer::_animation_added).bind(p_new_name));
lib.library->connect(SNAME("animation_removed"), callable_mp(this, &AnimationPlayer::_animation_removed).bind(p_new_name));
lib.library->connect(SNAME("animation_renamed"), callable_mp(this, &AnimationPlayer::_animation_renamed).bind(p_new_name));
for (const KeyValue<StringName, Ref<Animation>> &K : lib.library->animations) {
StringName old_name = p_name == StringName() ? K.key : StringName(String(p_name) + "/" + String(K.key));
StringName new_name = p_new_name == StringName() ? K.key : StringName(String(p_new_name) + "/" + String(K.key));
_rename_animation(old_name, new_name);
}
}
}
ERR_FAIL_COND(!found);
stop();
animation_libraries.sort(); // Must keep alphabetical order.
_animation_set_cache_update(); // Update cache.
notify_property_list_changed();
}
bool AnimationPlayer::has_animation_library(const StringName &p_name) const {
for (const AnimationLibraryData &lib : animation_libraries) {
if (lib.name == p_name) {
return true;
}
}
return false;
}
Ref<AnimationLibrary> AnimationPlayer::get_animation_library(const StringName &p_name) const {
for (const AnimationLibraryData &lib : animation_libraries) {
if (lib.name == p_name) {
return lib.library;
}
}
ERR_FAIL_V(Ref<AnimationLibrary>());
}
TypedArray<StringName> AnimationPlayer::_get_animation_library_list() const {
TypedArray<StringName> ret;
for (const AnimationLibraryData &lib : animation_libraries) {
ret.push_back(lib.name);
}
return ret;
}
void AnimationPlayer::get_animation_library_list(List<StringName> *p_libraries) const {
for (const AnimationLibraryData &lib : animation_libraries) {
p_libraries->push_back(lib.name);
}
}
bool AnimationPlayer::has_animation(const StringName &p_name) const {
return animation_set.has(p_name);
}
Ref<Animation> AnimationPlayer::get_animation(const StringName &p_name) const {
ERR_FAIL_COND_V_MSG(!animation_set.has(p_name), Ref<Animation>(), vformat("Animation not found: \"%s\".", p_name));
const AnimationData &anim_data = animation_set[p_name];
return anim_data.animation;
}
void AnimationPlayer::get_animation_list(List<StringName> *p_animations) const {
List<String> anims;
for (const KeyValue<StringName, AnimationData> &E : animation_set) {
anims.push_back(E.key);
}
anims.sort();
for (const String &E : anims) {
p_animations->push_back(E);
}
}
void AnimationPlayer::set_blend_time(const StringName &p_animation1, const StringName &p_animation2, double p_time) {
ERR_FAIL_COND_MSG(!animation_set.has(p_animation1), vformat("Animation not found: %s.", p_animation1));
ERR_FAIL_COND_MSG(!animation_set.has(p_animation2), vformat("Animation not found: %s.", p_animation2));
ERR_FAIL_COND_MSG(p_time < 0, "Blend time cannot be smaller than 0.");
BlendKey bk;
bk.from = p_animation1;
bk.to = p_animation2;
if (p_time == 0) {
blend_times.erase(bk);
} else {
blend_times[bk] = p_time;
}
}
double AnimationPlayer::get_blend_time(const StringName &p_animation1, const StringName &p_animation2) const {
BlendKey bk;
bk.from = p_animation1;
bk.to = p_animation2;
if (blend_times.has(bk)) {
return blend_times[bk];
} else {
return 0;
}
}
void AnimationPlayer::queue(const StringName &p_name) {
if (!is_playing()) {
play(p_name);
} else {
queued.push_back(p_name);
}
}
Vector<String> AnimationPlayer::get_queue() {
Vector<String> ret;
for (const StringName &E : queued) {
ret.push_back(E);
}
return ret;
}
void AnimationPlayer::clear_queue() {
queued.clear();
}
void AnimationPlayer::play_backwards(const StringName &p_name, double p_custom_blend) {
play(p_name, p_custom_blend, -1, true);
}
void AnimationPlayer::play(const StringName &p_name, double p_custom_blend, float p_custom_scale, bool p_from_end) {
StringName name = p_name;
if (String(name) == "") {
name = playback.assigned;
}
ERR_FAIL_COND_MSG(!animation_set.has(name), vformat("Animation not found: %s.", name));
Playback &c = playback;
if (c.current.from) {
double blend_time = 0.0;
// find if it can blend
BlendKey bk;
bk.from = c.current.from->name;
bk.to = name;
if (p_custom_blend >= 0) {
blend_time = p_custom_blend;
} else if (blend_times.has(bk)) {
blend_time = blend_times[bk];
} else {
bk.from = "*";
if (blend_times.has(bk)) {
blend_time = blend_times[bk];
} else {
bk.from = c.current.from->name;
bk.to = "*";
if (blend_times.has(bk)) {
blend_time = blend_times[bk];
}
}
}
if (p_custom_blend < 0 && blend_time == 0 && default_blend_time) {
blend_time = default_blend_time;
}
if (blend_time > 0) {
Blend b;
b.data = c.current;
b.blend_time = b.blend_left = blend_time;
c.blend.push_back(b);
} else {
c.blend.clear();
}
}
if (get_current_animation() != p_name) {
_stop_playing_caches(false);
}
c.current.from = &animation_set[name];
if (c.assigned != name) { // reset
c.current.pos = p_from_end ? c.current.from->animation->get_length() : 0;
} else {
if (p_from_end && c.current.pos == 0) {
// Animation reset BUT played backwards, set position to the end
c.current.pos = c.current.from->animation->get_length();
} else if (!p_from_end && c.current.pos == c.current.from->animation->get_length()) {
// Animation resumed but already ended, set position to the beginning
c.current.pos = 0;
}
}
c.current.speed_scale = p_custom_scale;
c.assigned = name;
c.seeked = false;
c.started = true;
if (!end_reached) {
queued.clear();
}
_set_process(true); // always process when starting an animation
playing = true;
emit_signal(SceneStringNames::get_singleton()->animation_started, c.assigned);
if (is_inside_tree() && Engine::get_singleton()->is_editor_hint()) {
return; // no next in this case
}
StringName next = animation_get_next(p_name);
if (next != StringName() && animation_set.has(next)) {
queue(next);
}
}
bool AnimationPlayer::is_playing() const {
return playing;
}
void AnimationPlayer::set_current_animation(const String &p_anim) {
if (p_anim == "[stop]" || p_anim.is_empty()) {
stop();
} else if (!is_playing() || playback.assigned != p_anim) {
play(p_anim);
} else {
// Same animation, do not replay from start
}
}
String AnimationPlayer::get_current_animation() const {
return (is_playing() ? playback.assigned : "");
}
void AnimationPlayer::set_assigned_animation(const String &p_anim) {
if (is_playing()) {
play(p_anim);
} else {
ERR_FAIL_COND_MSG(!animation_set.has(p_anim), vformat("Animation not found: %s.", p_anim));
playback.current.pos = 0;
playback.current.from = &animation_set[p_anim];
playback.assigned = p_anim;
}
}
String AnimationPlayer::get_assigned_animation() const {
return playback.assigned;
}
void AnimationPlayer::pause() {
_stop_internal(false, false);
}
void AnimationPlayer::stop(bool p_keep_state) {
_stop_internal(true, p_keep_state);
}
void AnimationPlayer::set_speed_scale(float p_speed) {
speed_scale = p_speed;
}
float AnimationPlayer::get_speed_scale() const {
return speed_scale;
}
float AnimationPlayer::get_playing_speed() const {
if (!playing) {
return 0;
}
return speed_scale * playback.current.speed_scale;
}
void AnimationPlayer::seek(double p_time, bool p_update) {
if (!playback.current.from) {
if (playback.assigned) {
ERR_FAIL_COND_MSG(!animation_set.has(playback.assigned), vformat("Animation not found: %s.", playback.assigned));
playback.current.from = &animation_set[playback.assigned];
}
ERR_FAIL_COND(!playback.current.from);
}
playback.current.pos = p_time;
playback.seeked = true;
if (p_update) {
_animation_process(0);
}
}
void AnimationPlayer::seek_delta(double p_time, double p_delta) {
if (!playback.current.from) {
if (playback.assigned) {
ERR_FAIL_COND_MSG(!animation_set.has(playback.assigned), vformat("Animation not found: %s.", playback.assigned));
playback.current.from = &animation_set[playback.assigned];
}
ERR_FAIL_COND(!playback.current.from);
}
playback.current.pos = p_time - p_delta;
if (speed_scale != 0.0) {
p_delta /= speed_scale;
}
_animation_process(p_delta);
//playback.current.pos=p_time;
}
bool AnimationPlayer::is_valid() const {
return (playback.current.from);
}
double AnimationPlayer::get_current_animation_position() const {
ERR_FAIL_COND_V_MSG(!playback.current.from, 0, "AnimationPlayer has no current animation");
return playback.current.pos;
}
double AnimationPlayer::get_current_animation_length() const {
ERR_FAIL_COND_V_MSG(!playback.current.from, 0, "AnimationPlayer has no current animation");
return playback.current.from->animation->get_length();
}
void AnimationPlayer::_animation_changed(const StringName &p_name) {
clear_caches();
if (is_playing()) {
playback.seeked = true; //need to restart stuff, like audio
}
}
void AnimationPlayer::_stop_playing_caches(bool p_reset) {
for (TrackNodeCache *E : playing_caches) {
if (E->node && E->audio_playing) {
E->node->call(SNAME("stop"));
}
if (E->node && E->animation_playing) {
AnimationPlayer *player = Object::cast_to<AnimationPlayer>(E->node);
if (!player) {
continue;
}
if (p_reset) {
player->stop();
} else {
player->pause();
}
}
}
playing_caches.clear();
}
void AnimationPlayer::_node_removed(Node *p_node) {
clear_caches(); // nodes contained here are being removed, clear the caches
}
void AnimationPlayer::clear_caches() {
_stop_playing_caches(true);
node_cache_map.clear();
for (KeyValue<StringName, AnimationData> &E : animation_set) {
E.value.node_cache.clear();
}
cache_update_size = 0;
cache_update_prop_size = 0;
cache_update_bezier_size = 0;
emit_signal(SNAME("caches_cleared"));
}
void AnimationPlayer::set_active(bool p_active) {
if (active == p_active) {
return;
}
active = p_active;
_set_process(processing, true);
}
bool AnimationPlayer::is_active() const {
return active;
}
StringName AnimationPlayer::find_animation(const Ref<Animation> &p_animation) const {
for (const KeyValue<StringName, AnimationData> &E : animation_set) {
if (E.value.animation == p_animation) {
return E.key;
}
}
return StringName();
}
StringName AnimationPlayer::find_animation_library(const Ref<Animation> &p_animation) const {
for (const KeyValue<StringName, AnimationData> &E : animation_set) {
if (E.value.animation == p_animation) {
return E.value.animation_library;
}
}
return StringName();
}
void AnimationPlayer::set_autoplay(const String &p_name) {
if (is_inside_tree() && !Engine::get_singleton()->is_editor_hint()) {
WARN_PRINT("Setting autoplay after the node has been added to the scene has no effect.");
}
autoplay = p_name;
}
String AnimationPlayer::get_autoplay() const {
return autoplay;
}
void AnimationPlayer::set_reset_on_save_enabled(bool p_enabled) {
reset_on_save = p_enabled;
}
bool AnimationPlayer::is_reset_on_save_enabled() const {
return reset_on_save;
}
void AnimationPlayer::set_process_callback(AnimationProcessCallback p_mode) {
if (process_callback == p_mode) {
return;
}
bool pr = processing;
if (pr) {
_set_process(false);
}
process_callback = p_mode;
if (pr) {
_set_process(true);
}
}
AnimationPlayer::AnimationProcessCallback AnimationPlayer::get_process_callback() const {
return process_callback;
}
void AnimationPlayer::set_method_call_mode(AnimationMethodCallMode p_mode) {
method_call_mode = p_mode;
}
AnimationPlayer::AnimationMethodCallMode AnimationPlayer::get_method_call_mode() const {
return method_call_mode;
}
void AnimationPlayer::set_movie_quit_on_finish_enabled(bool p_enabled) {
movie_quit_on_finish = p_enabled;
}
bool AnimationPlayer::is_movie_quit_on_finish_enabled() const {
return movie_quit_on_finish;
}
void AnimationPlayer::_set_process(bool p_process, bool p_force) {
if (processing == p_process && !p_force) {
return;
}
switch (process_callback) {
case ANIMATION_PROCESS_PHYSICS:
set_physics_process_internal(p_process && active);
break;
case ANIMATION_PROCESS_IDLE:
set_process_internal(p_process && active);
break;
case ANIMATION_PROCESS_MANUAL:
break;
}
processing = p_process;
}
void AnimationPlayer::_stop_internal(bool p_reset, bool p_keep_state) {
_stop_playing_caches(p_reset);
Playback &c = playback;
c.blend.clear();
if (p_reset) {
if (p_keep_state) {
c.current.pos = 0;
} else {
is_stopping = true;
seek(0, true);
is_stopping = false;
}
c.current.from = nullptr;
c.current.speed_scale = 1;
}
_set_process(false);
queued.clear();
playing = false;
}
void AnimationPlayer::animation_set_next(const StringName &p_animation, const StringName &p_next) {
ERR_FAIL_COND_MSG(!animation_set.has(p_animation), vformat("Animation not found: %s.", p_animation));
animation_set[p_animation].next = p_next;
}
StringName AnimationPlayer::animation_get_next(const StringName &p_animation) const {
if (!animation_set.has(p_animation)) {
return StringName();
}
return animation_set[p_animation].next;
}
void AnimationPlayer::set_default_blend_time(double p_default) {
default_blend_time = p_default;
}
double AnimationPlayer::get_default_blend_time() const {
return default_blend_time;
}
void AnimationPlayer::set_root(const NodePath &p_root) {
root = p_root;
clear_caches();
}
NodePath AnimationPlayer::get_root() const {
return root;
}
void AnimationPlayer::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const {
String pf = p_function;
if (p_idx == 0 && (p_function == "play" || p_function == "play_backwards" || p_function == "has_animation" || p_function == "queue")) {
List<StringName> al;
get_animation_list(&al);
for (const StringName &name : al) {
r_options->push_back(String(name).quote());
}
}
Node::get_argument_options(p_function, p_idx, r_options);
}
#ifdef TOOLS_ENABLED
Ref<AnimatedValuesBackup> AnimationPlayer::backup_animated_values(Node *p_root_override) {
Ref<AnimatedValuesBackup> backup;
if (!playback.current.from) {
return backup;
}
_ensure_node_caches(playback.current.from, p_root_override);
backup.instantiate();
for (int i = 0; i < playback.current.from->node_cache.size(); i++) {
TrackNodeCache *nc = playback.current.from->node_cache[i];
if (!nc) {
continue;
}
if (nc->skeleton) {
if (nc->bone_idx == -1) {
continue;
}
AnimatedValuesBackup::Entry entry;
entry.object = nc->skeleton;
entry.bone_idx = nc->bone_idx;
Array arr;
arr.resize(3);
arr[0] = nc->skeleton->get_bone_pose_position(nc->bone_idx);
arr[1] = nc->skeleton->get_bone_pose_rotation(nc->bone_idx);
arr[2] = nc->skeleton->get_bone_pose_scale(nc->bone_idx);
entry.value = nc;
backup->entries.push_back(entry);
} else {
if (nc->node_3d) {
AnimatedValuesBackup::Entry entry;
entry.object = nc->node_3d;
entry.subpath.push_back("transform");
entry.value = nc->node_3d->get_transform();
entry.bone_idx = -1;
backup->entries.push_back(entry);
} else {
for (const KeyValue<StringName, TrackNodeCache::PropertyAnim> &E : nc->property_anim) {
AnimatedValuesBackup::Entry entry;
entry.object = E.value.object;
entry.subpath = E.value.subpath;
bool valid;
entry.value = E.value.object->get_indexed(E.value.subpath, &valid);
entry.bone_idx = -1;
if (valid) {
backup->entries.push_back(entry);
}
}
}
}
}
return backup;
}
Ref<AnimatedValuesBackup> AnimationPlayer::apply_reset(bool p_user_initiated) {
ERR_FAIL_COND_V(!can_apply_reset(), Ref<AnimatedValuesBackup>());
Ref<Animation> reset_anim = animation_set[SceneStringNames::get_singleton()->RESET].animation;
ERR_FAIL_COND_V(reset_anim.is_null(), Ref<AnimatedValuesBackup>());
Node *root_node = get_node_or_null(root);
ERR_FAIL_COND_V(!root_node, Ref<AnimatedValuesBackup>());
AnimationPlayer *aux_player = memnew(AnimationPlayer);
EditorNode::get_singleton()->add_child(aux_player);
Ref<AnimationLibrary> al;
al.instantiate();
al->add_animation(SceneStringNames::get_singleton()->RESET, reset_anim);
aux_player->add_animation_library("", al);
aux_player->set_assigned_animation(SceneStringNames::get_singleton()->RESET);
// Forcing the use of the original root because the scene where original player belongs may be not the active one
Ref<AnimatedValuesBackup> old_values = aux_player->backup_animated_values(get_node(get_root()));
aux_player->seek(0.0f, true);
aux_player->queue_free();
if (p_user_initiated) {
Ref<AnimatedValuesBackup> new_values = aux_player->backup_animated_values();
old_values->restore();
EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
ur->create_action(TTR("Animation Apply Reset"));
ur->add_do_method(new_values.ptr(), "restore");
ur->add_undo_method(old_values.ptr(), "restore");
ur->commit_action();
}
return old_values;
}
bool AnimationPlayer::can_apply_reset() const {
return has_animation(SceneStringNames::get_singleton()->RESET) && playback.assigned != SceneStringNames::get_singleton()->RESET;
}
#endif // TOOLS_ENABLED
void AnimationPlayer::_bind_methods() {
ClassDB::bind_method(D_METHOD("add_animation_library", "name", "library"), &AnimationPlayer::add_animation_library);
ClassDB::bind_method(D_METHOD("remove_animation_library", "name"), &AnimationPlayer::remove_animation_library);
ClassDB::bind_method(D_METHOD("rename_animation_library", "name", "newname"), &AnimationPlayer::rename_animation_library);
ClassDB::bind_method(D_METHOD("has_animation_library", "name"), &AnimationPlayer::has_animation_library);
ClassDB::bind_method(D_METHOD("get_animation_library", "name"), &AnimationPlayer::get_animation_library);
ClassDB::bind_method(D_METHOD("get_animation_library_list"), &AnimationPlayer::_get_animation_library_list);
ClassDB::bind_method(D_METHOD("has_animation", "name"), &AnimationPlayer::has_animation);
ClassDB::bind_method(D_METHOD("get_animation", "name"), &AnimationPlayer::get_animation);
ClassDB::bind_method(D_METHOD("get_animation_list"), &AnimationPlayer::_get_animation_list);
ClassDB::bind_method(D_METHOD("animation_set_next", "anim_from", "anim_to"), &AnimationPlayer::animation_set_next);
ClassDB::bind_method(D_METHOD("animation_get_next", "anim_from"), &AnimationPlayer::animation_get_next);
ClassDB::bind_method(D_METHOD("set_blend_time", "anim_from", "anim_to", "sec"), &AnimationPlayer::set_blend_time);
ClassDB::bind_method(D_METHOD("get_blend_time", "anim_from", "anim_to"), &AnimationPlayer::get_blend_time);
ClassDB::bind_method(D_METHOD("set_default_blend_time", "sec"), &AnimationPlayer::set_default_blend_time);
ClassDB::bind_method(D_METHOD("get_default_blend_time"), &AnimationPlayer::get_default_blend_time);
ClassDB::bind_method(D_METHOD("play", "name", "custom_blend", "custom_speed", "from_end"), &AnimationPlayer::play, DEFVAL(""), DEFVAL(-1), DEFVAL(1.0), DEFVAL(false));
ClassDB::bind_method(D_METHOD("play_backwards", "name", "custom_blend"), &AnimationPlayer::play_backwards, DEFVAL(""), DEFVAL(-1));
ClassDB::bind_method(D_METHOD("pause"), &AnimationPlayer::pause);
ClassDB::bind_method(D_METHOD("stop", "keep_state"), &AnimationPlayer::stop, DEFVAL(false));
ClassDB::bind_method(D_METHOD("is_playing"), &AnimationPlayer::is_playing);
ClassDB::bind_method(D_METHOD("set_current_animation", "anim"), &AnimationPlayer::set_current_animation);
ClassDB::bind_method(D_METHOD("get_current_animation"), &AnimationPlayer::get_current_animation);
ClassDB::bind_method(D_METHOD("set_assigned_animation", "anim"), &AnimationPlayer::set_assigned_animation);
ClassDB::bind_method(D_METHOD("get_assigned_animation"), &AnimationPlayer::get_assigned_animation);
ClassDB::bind_method(D_METHOD("queue", "name"), &AnimationPlayer::queue);
ClassDB::bind_method(D_METHOD("get_queue"), &AnimationPlayer::get_queue);
ClassDB::bind_method(D_METHOD("clear_queue"), &AnimationPlayer::clear_queue);
ClassDB::bind_method(D_METHOD("set_active", "active"), &AnimationPlayer::set_active);
ClassDB::bind_method(D_METHOD("is_active"), &AnimationPlayer::is_active);
ClassDB::bind_method(D_METHOD("set_speed_scale", "speed"), &AnimationPlayer::set_speed_scale);
ClassDB::bind_method(D_METHOD("get_speed_scale"), &AnimationPlayer::get_speed_scale);
ClassDB::bind_method(D_METHOD("get_playing_speed"), &AnimationPlayer::get_playing_speed);
ClassDB::bind_method(D_METHOD("set_autoplay", "name"), &AnimationPlayer::set_autoplay);
ClassDB::bind_method(D_METHOD("get_autoplay"), &AnimationPlayer::get_autoplay);
ClassDB::bind_method(D_METHOD("set_reset_on_save_enabled", "enabled"), &AnimationPlayer::set_reset_on_save_enabled);
ClassDB::bind_method(D_METHOD("is_reset_on_save_enabled"), &AnimationPlayer::is_reset_on_save_enabled);
ClassDB::bind_method(D_METHOD("set_root", "path"), &AnimationPlayer::set_root);
ClassDB::bind_method(D_METHOD("get_root"), &AnimationPlayer::get_root);
ClassDB::bind_method(D_METHOD("find_animation", "animation"), &AnimationPlayer::find_animation);
ClassDB::bind_method(D_METHOD("find_animation_library", "animation"), &AnimationPlayer::find_animation_library);
ClassDB::bind_method(D_METHOD("clear_caches"), &AnimationPlayer::clear_caches);
ClassDB::bind_method(D_METHOD("set_process_callback", "mode"), &AnimationPlayer::set_process_callback);
ClassDB::bind_method(D_METHOD("get_process_callback"), &AnimationPlayer::get_process_callback);
ClassDB::bind_method(D_METHOD("set_method_call_mode", "mode"), &AnimationPlayer::set_method_call_mode);
ClassDB::bind_method(D_METHOD("get_method_call_mode"), &AnimationPlayer::get_method_call_mode);
ClassDB::bind_method(D_METHOD("set_movie_quit_on_finish_enabled", "enabled"), &AnimationPlayer::set_movie_quit_on_finish_enabled);
ClassDB::bind_method(D_METHOD("is_movie_quit_on_finish_enabled"), &AnimationPlayer::is_movie_quit_on_finish_enabled);
ClassDB::bind_method(D_METHOD("get_current_animation_position"), &AnimationPlayer::get_current_animation_position);
ClassDB::bind_method(D_METHOD("get_current_animation_length"), &AnimationPlayer::get_current_animation_length);
ClassDB::bind_method(D_METHOD("seek", "seconds", "update"), &AnimationPlayer::seek, DEFVAL(false));
ClassDB::bind_method(D_METHOD("advance", "delta"), &AnimationPlayer::advance);
ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "root_node"), "set_root", "get_root");
ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "current_animation", PROPERTY_HINT_ENUM, "", PROPERTY_USAGE_EDITOR), "set_current_animation", "get_current_animation");
ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "assigned_animation", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_assigned_animation", "get_assigned_animation");
ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "autoplay", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_autoplay", "get_autoplay");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "reset_on_save", PROPERTY_HINT_NONE, ""), "set_reset_on_save_enabled", "is_reset_on_save_enabled");
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "current_animation_length", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "", "get_current_animation_length");
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "current_animation_position", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "", "get_current_animation_position");
ADD_GROUP("Playback Options", "playback_");
ADD_PROPERTY(PropertyInfo(Variant::INT, "playback_process_mode", PROPERTY_HINT_ENUM, "Physics,Idle,Manual"), "set_process_callback", "get_process_callback");
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "playback_default_blend_time", PROPERTY_HINT_RANGE, "0,4096,0.01,suffix:s"), "set_default_blend_time", "get_default_blend_time");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "playback_active", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_active", "is_active");
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "playback_speed", PROPERTY_HINT_RANGE, "-64,64,0.01"), "set_speed_scale", "get_speed_scale");
ADD_PROPERTY(PropertyInfo(Variant::INT, "method_call_mode", PROPERTY_HINT_ENUM, "Deferred,Immediate"), "set_method_call_mode", "get_method_call_mode");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "movie_quit_on_finish"), "set_movie_quit_on_finish_enabled", "is_movie_quit_on_finish_enabled");
ADD_SIGNAL(MethodInfo("animation_finished", PropertyInfo(Variant::STRING_NAME, "anim_name")));
ADD_SIGNAL(MethodInfo("animation_changed", PropertyInfo(Variant::STRING_NAME, "old_name"), PropertyInfo(Variant::STRING_NAME, "new_name")));
ADD_SIGNAL(MethodInfo("animation_started", PropertyInfo(Variant::STRING_NAME, "anim_name")));
ADD_SIGNAL(MethodInfo("animation_list_changed"));
ADD_SIGNAL(MethodInfo("animation_libraries_updated"));
ADD_SIGNAL(MethodInfo("caches_cleared"));
BIND_ENUM_CONSTANT(ANIMATION_PROCESS_PHYSICS);
BIND_ENUM_CONSTANT(ANIMATION_PROCESS_IDLE);
BIND_ENUM_CONSTANT(ANIMATION_PROCESS_MANUAL);
BIND_ENUM_CONSTANT(ANIMATION_METHOD_CALL_DEFERRED);
BIND_ENUM_CONSTANT(ANIMATION_METHOD_CALL_IMMEDIATE);
}
AnimationPlayer::AnimationPlayer() {
root = SceneStringNames::get_singleton()->path_pp;
}
AnimationPlayer::~AnimationPlayer() {
}
|