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
|
/*************************************************************************/
/* tween.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
/* */
/* 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 "tween.h"
#include "core/method_bind_ext.gen.inc"
void Tween::_add_pending_command(StringName p_key, const Variant &p_arg1, const Variant &p_arg2, const Variant &p_arg3, const Variant &p_arg4, const Variant &p_arg5, const Variant &p_arg6, const Variant &p_arg7, const Variant &p_arg8, const Variant &p_arg9, const Variant &p_arg10) {
// Add a new pending command and reference it
pending_commands.push_back(PendingCommand());
PendingCommand &cmd = pending_commands.back()->get();
// Update the command with the target key
cmd.key = p_key;
// Determine command argument count
int &count = cmd.args;
if (p_arg10.get_type() != Variant::NIL)
count = 10;
else if (p_arg9.get_type() != Variant::NIL)
count = 9;
else if (p_arg8.get_type() != Variant::NIL)
count = 8;
else if (p_arg7.get_type() != Variant::NIL)
count = 7;
else if (p_arg6.get_type() != Variant::NIL)
count = 6;
else if (p_arg5.get_type() != Variant::NIL)
count = 5;
else if (p_arg4.get_type() != Variant::NIL)
count = 4;
else if (p_arg3.get_type() != Variant::NIL)
count = 3;
else if (p_arg2.get_type() != Variant::NIL)
count = 2;
else if (p_arg1.get_type() != Variant::NIL)
count = 1;
// Add the specified arguments to the command
// TODO: Make this a switch statement?
if (count > 0)
cmd.arg[0] = p_arg1;
if (count > 1)
cmd.arg[1] = p_arg2;
if (count > 2)
cmd.arg[2] = p_arg3;
if (count > 3)
cmd.arg[3] = p_arg4;
if (count > 4)
cmd.arg[4] = p_arg5;
if (count > 5)
cmd.arg[5] = p_arg6;
if (count > 6)
cmd.arg[6] = p_arg7;
if (count > 7)
cmd.arg[7] = p_arg8;
if (count > 8)
cmd.arg[8] = p_arg9;
if (count > 9)
cmd.arg[9] = p_arg10;
}
void Tween::_process_pending_commands() {
// For each pending command...
for (List<PendingCommand>::Element *E = pending_commands.front(); E; E = E->next()) {
// Get the command
PendingCommand &cmd = E->get();
Variant::CallError err;
// Grab all of the arguments for the command
Variant *arg[10] = {
&cmd.arg[0],
&cmd.arg[1],
&cmd.arg[2],
&cmd.arg[3],
&cmd.arg[4],
&cmd.arg[5],
&cmd.arg[6],
&cmd.arg[7],
&cmd.arg[8],
&cmd.arg[9],
};
// Execute the command (and retrieve any errors)
this->call(cmd.key, (const Variant **)arg, cmd.args, err);
}
// Clear the pending commands
pending_commands.clear();
}
bool Tween::_set(const StringName &p_name, const Variant &p_value) {
// Set the correct attribute based on the given name
String name = p_name;
if (name == "playback/speed" || name == "speed") { // Backwards compatibility
set_speed_scale(p_value);
} else if (name == "playback/active") {
set_active(p_value);
} else if (name == "playback/repeat") {
set_repeat(p_value);
}
return true;
}
bool Tween::_get(const StringName &p_name, Variant &r_ret) const {
// Get the correct attribute based on the given name
String name = p_name;
if (name == "playback/speed") { // Backwards compatibility
r_ret = speed_scale;
} else if (name == "playback/active") {
r_ret = is_active();
} else if (name == "playback/repeat") {
r_ret = is_repeat();
}
return true;
}
void Tween::_get_property_list(List<PropertyInfo> *p_list) const {
// Add the property info for the Tween object
p_list->push_back(PropertyInfo(Variant::BOOL, "playback/active", PROPERTY_HINT_NONE, ""));
p_list->push_back(PropertyInfo(Variant::BOOL, "playback/repeat", PROPERTY_HINT_NONE, ""));
p_list->push_back(PropertyInfo(Variant::REAL, "playback/speed", PROPERTY_HINT_RANGE, "-64,64,0.01"));
}
void Tween::_notification(int p_what) {
// What notification did we receive?
switch (p_what) {
case NOTIFICATION_ENTER_TREE: {
// Are we not already active?
if (!is_active()) {
// 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);
}
} break;
case NOTIFICATION_READY: {
// Do nothing
} break;
case NOTIFICATION_INTERNAL_PROCESS: {
// Are we processing during physics time?
if (tween_process_mode == TWEEN_PROCESS_PHYSICS)
// Do nothing since we aren't aligned with physics when we should be
break;
// Should we update?
if (is_active())
// Update the tweens
_tween_process(get_process_delta_time());
} break;
case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
// Are we processing during 'regular' time?
if (tween_process_mode == TWEEN_PROCESS_IDLE)
// Do nothing since we whould only process during idle time
break;
// Should we update?
if (is_active())
// Update the tweens
_tween_process(get_physics_process_delta_time());
} break;
case NOTIFICATION_EXIT_TREE: {
// We've left the tree. Stop all tweens
stop_all();
} break;
}
}
void Tween::_bind_methods() {
// Bind getters and setters
ClassDB::bind_method(D_METHOD("is_active"), &Tween::is_active);
ClassDB::bind_method(D_METHOD("set_active", "active"), &Tween::set_active);
ClassDB::bind_method(D_METHOD("is_repeat"), &Tween::is_repeat);
ClassDB::bind_method(D_METHOD("set_repeat", "repeat"), &Tween::set_repeat);
ClassDB::bind_method(D_METHOD("set_speed_scale", "speed"), &Tween::set_speed_scale);
ClassDB::bind_method(D_METHOD("get_speed_scale"), &Tween::get_speed_scale);
ClassDB::bind_method(D_METHOD("set_tween_process_mode", "mode"), &Tween::set_tween_process_mode);
ClassDB::bind_method(D_METHOD("get_tween_process_mode"), &Tween::get_tween_process_mode);
// Bind the various Tween control methods
ClassDB::bind_method(D_METHOD("start"), &Tween::start);
ClassDB::bind_method(D_METHOD("reset", "object", "key"), &Tween::reset, DEFVAL(""));
ClassDB::bind_method(D_METHOD("reset_all"), &Tween::reset_all);
ClassDB::bind_method(D_METHOD("stop", "object", "key"), &Tween::stop, DEFVAL(""));
ClassDB::bind_method(D_METHOD("stop_all"), &Tween::stop_all);
ClassDB::bind_method(D_METHOD("resume", "object", "key"), &Tween::resume, DEFVAL(""));
ClassDB::bind_method(D_METHOD("resume_all"), &Tween::resume_all);
ClassDB::bind_method(D_METHOD("remove", "object", "key"), &Tween::remove, DEFVAL(""));
ClassDB::bind_method(D_METHOD("_remove_by_uid", "uid"), &Tween::_remove_by_uid);
ClassDB::bind_method(D_METHOD("remove_all"), &Tween::remove_all);
ClassDB::bind_method(D_METHOD("seek", "time"), &Tween::seek);
ClassDB::bind_method(D_METHOD("tell"), &Tween::tell);
ClassDB::bind_method(D_METHOD("get_runtime"), &Tween::get_runtime);
// Bind interpolation and follow methods
ClassDB::bind_method(D_METHOD("interpolate_property", "object", "property", "initial_val", "final_val", "duration", "trans_type", "ease_type", "delay"), &Tween::interpolate_property, DEFVAL(0));
ClassDB::bind_method(D_METHOD("interpolate_method", "object", "method", "initial_val", "final_val", "duration", "trans_type", "ease_type", "delay"), &Tween::interpolate_method, DEFVAL(0));
ClassDB::bind_method(D_METHOD("interpolate_callback", "object", "duration", "callback", "arg1", "arg2", "arg3", "arg4", "arg5"), &Tween::interpolate_callback, DEFVAL(Variant()), DEFVAL(Variant()), DEFVAL(Variant()), DEFVAL(Variant()), DEFVAL(Variant()));
ClassDB::bind_method(D_METHOD("interpolate_deferred_callback", "object", "duration", "callback", "arg1", "arg2", "arg3", "arg4", "arg5"), &Tween::interpolate_deferred_callback, DEFVAL(Variant()), DEFVAL(Variant()), DEFVAL(Variant()), DEFVAL(Variant()), DEFVAL(Variant()));
ClassDB::bind_method(D_METHOD("follow_property", "object", "property", "initial_val", "target", "target_property", "duration", "trans_type", "ease_type", "delay"), &Tween::follow_property, DEFVAL(0));
ClassDB::bind_method(D_METHOD("follow_method", "object", "method", "initial_val", "target", "target_method", "duration", "trans_type", "ease_type", "delay"), &Tween::follow_method, DEFVAL(0));
ClassDB::bind_method(D_METHOD("targeting_property", "object", "property", "initial", "initial_val", "final_val", "duration", "trans_type", "ease_type", "delay"), &Tween::targeting_property, DEFVAL(0));
ClassDB::bind_method(D_METHOD("targeting_method", "object", "method", "initial", "initial_method", "final_val", "duration", "trans_type", "ease_type", "delay"), &Tween::targeting_method, DEFVAL(0));
// Add the Tween signals
ADD_SIGNAL(MethodInfo("tween_started", PropertyInfo(Variant::OBJECT, "object"), PropertyInfo(Variant::NODE_PATH, "key")));
ADD_SIGNAL(MethodInfo("tween_step", PropertyInfo(Variant::OBJECT, "object"), PropertyInfo(Variant::NODE_PATH, "key"), PropertyInfo(Variant::REAL, "elapsed"), PropertyInfo(Variant::OBJECT, "value")));
ADD_SIGNAL(MethodInfo("tween_completed", PropertyInfo(Variant::OBJECT, "object"), PropertyInfo(Variant::NODE_PATH, "key")));
ADD_SIGNAL(MethodInfo("tween_all_completed"));
// Add the properties and tie them to the getters and setters
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "repeat"), "set_repeat", "is_repeat");
ADD_PROPERTY(PropertyInfo(Variant::INT, "playback_process_mode", PROPERTY_HINT_ENUM, "Physics,Idle"), "set_tween_process_mode", "get_tween_process_mode");
ADD_PROPERTY(PropertyInfo(Variant::REAL, "playback_speed", PROPERTY_HINT_RANGE, "-64,64,0.01"), "set_speed_scale", "get_speed_scale");
// Bind Idle vs Physics process
BIND_ENUM_CONSTANT(TWEEN_PROCESS_PHYSICS);
BIND_ENUM_CONSTANT(TWEEN_PROCESS_IDLE);
// Bind the Transition type constants
BIND_ENUM_CONSTANT(TRANS_LINEAR);
BIND_ENUM_CONSTANT(TRANS_SINE);
BIND_ENUM_CONSTANT(TRANS_QUINT);
BIND_ENUM_CONSTANT(TRANS_QUART);
BIND_ENUM_CONSTANT(TRANS_QUAD);
BIND_ENUM_CONSTANT(TRANS_EXPO);
BIND_ENUM_CONSTANT(TRANS_ELASTIC);
BIND_ENUM_CONSTANT(TRANS_CUBIC);
BIND_ENUM_CONSTANT(TRANS_CIRC);
BIND_ENUM_CONSTANT(TRANS_BOUNCE);
BIND_ENUM_CONSTANT(TRANS_BACK);
// Bind the easing constants
BIND_ENUM_CONSTANT(EASE_IN);
BIND_ENUM_CONSTANT(EASE_OUT);
BIND_ENUM_CONSTANT(EASE_IN_OUT);
BIND_ENUM_CONSTANT(EASE_OUT_IN);
}
Variant &Tween::_get_initial_val(InterpolateData &p_data) {
// What type of data are we interpolating?
switch (p_data.type) {
case INTER_PROPERTY:
case INTER_METHOD:
case FOLLOW_PROPERTY:
case FOLLOW_METHOD:
// Simply use the given initial value
return p_data.initial_val;
case TARGETING_PROPERTY:
case TARGETING_METHOD: {
// Get the object that is being targeted
Object *object = ObjectDB::get_instance(p_data.target_id);
ERR_FAIL_COND_V(object == NULL, p_data.initial_val);
// Are we targeting a property or a method?
static Variant initial_val;
if (p_data.type == TARGETING_PROPERTY) {
// Get the property from the target object
bool valid = false;
initial_val = object->get_indexed(p_data.target_key, &valid);
ERR_FAIL_COND_V(!valid, p_data.initial_val);
} else {
// Call the method and get the initial value from it
Variant::CallError error;
initial_val = object->call(p_data.target_key[0], NULL, 0, error);
ERR_FAIL_COND_V(error.error != Variant::CallError::CALL_OK, p_data.initial_val);
}
return initial_val;
}
case INTER_CALLBACK:
// Callback does not have a special initial value
break;
}
// If we've made it here, just return the delta value as the initial value
return p_data.delta_val;
}
Variant &Tween::_get_delta_val(InterpolateData &p_data) {
// What kind of data are we interpolating?
switch (p_data.type) {
case INTER_PROPERTY:
case INTER_METHOD:
// Simply return the given delta value
return p_data.delta_val;
case FOLLOW_PROPERTY:
case FOLLOW_METHOD: {
// We're following an object, so grab that instance
Object *target = ObjectDB::get_instance(p_data.target_id);
ERR_FAIL_COND_V(target == NULL, p_data.initial_val);
// We want to figure out the final value
Variant final_val;
if (p_data.type == FOLLOW_PROPERTY) {
// Read the property as-is
bool valid = false;
final_val = target->get_indexed(p_data.target_key, &valid);
ERR_FAIL_COND_V(!valid, p_data.initial_val);
} else {
// We're looking at a method. Call the method on the target object
Variant::CallError error;
final_val = target->call(p_data.target_key[0], NULL, 0, error);
ERR_FAIL_COND_V(error.error != Variant::CallError::CALL_OK, p_data.initial_val);
}
// If we're looking at an INT value, instead convert it to a REAL
// This is better for interpolation
if (final_val.get_type() == Variant::INT) final_val = final_val.operator real_t();
// Calculate the delta based on the initial value and the final value
_calc_delta_val(p_data.initial_val, final_val, p_data.delta_val);
return p_data.delta_val;
}
case TARGETING_PROPERTY:
case TARGETING_METHOD: {
// Grab the initial value from the data to calculate delta
Variant initial_val = _get_initial_val(p_data);
// If we're looking at an INT value, instead convert it to a REAL
// This is better for interpolation
if (initial_val.get_type() == Variant::INT) initial_val = initial_val.operator real_t();
// Calculate the delta based on the initial value and the final value
_calc_delta_val(initial_val, p_data.final_val, p_data.delta_val);
return p_data.delta_val;
}
case INTER_CALLBACK:
// Callbacks have no special delta
break;
}
// If we've made it here, use the initial value as the delta
return p_data.initial_val;
}
Variant Tween::_run_equation(InterpolateData &p_data) {
// Get the initial and delta values from the data
Variant &initial_val = _get_initial_val(p_data);
Variant &delta_val = _get_delta_val(p_data);
Variant result;
#define APPLY_EQUATION(element) \
r.element = _run_equation(p_data.trans_type, p_data.ease_type, p_data.elapsed - p_data.delay, i.element, d.element, p_data.duration);
// What type of data are we interpolating?
switch (initial_val.get_type()) {
case Variant::BOOL:
// Run the boolean specific equation (checking if it is at least 0.5)
result = (_run_equation(p_data.trans_type, p_data.ease_type, p_data.elapsed - p_data.delay, initial_val, delta_val, p_data.duration)) >= 0.5;
break;
case Variant::INT:
// Run the integer specific equation
result = (int)_run_equation(p_data.trans_type, p_data.ease_type, p_data.elapsed - p_data.delay, (int)initial_val, (int)delta_val, p_data.duration);
break;
case Variant::REAL:
// Run the REAL specific equation
result = _run_equation(p_data.trans_type, p_data.ease_type, p_data.elapsed - p_data.delay, (real_t)initial_val, (real_t)delta_val, p_data.duration);
break;
case Variant::VECTOR2: {
// Get vectors for initial and delta values
Vector2 i = initial_val;
Vector2 d = delta_val;
Vector2 r;
// Execute the equation and mutate the r vector
// This uses the custom APPLY_EQUATION macro defined above
APPLY_EQUATION(x);
APPLY_EQUATION(y);
result = r;
} break;
case Variant::VECTOR3: {
// Get vectors for initial and delta values
Vector3 i = initial_val;
Vector3 d = delta_val;
Vector3 r;
// Execute the equation and mutate the r vector
// This uses the custom APPLY_EQUATION macro defined above
APPLY_EQUATION(x);
APPLY_EQUATION(y);
APPLY_EQUATION(z);
result = r;
} break;
case Variant::BASIS: {
// Get the basis for initial and delta values
Basis i = initial_val;
Basis d = delta_val;
Basis r;
// Execute the equation on all the basis and mutate the r basis
// This uses the custom APPLY_EQUATION macro defined above
APPLY_EQUATION(elements[0][0]);
APPLY_EQUATION(elements[0][1]);
APPLY_EQUATION(elements[0][2]);
APPLY_EQUATION(elements[1][0]);
APPLY_EQUATION(elements[1][1]);
APPLY_EQUATION(elements[1][2]);
APPLY_EQUATION(elements[2][0]);
APPLY_EQUATION(elements[2][1]);
APPLY_EQUATION(elements[2][2]);
result = r;
} break;
case Variant::TRANSFORM2D: {
// Get the transforms for initial and delta values
Transform2D i = initial_val;
Transform2D d = delta_val;
Transform2D r;
// Execute the equation on the transforms and mutate the r transform
// This uses the custom APPLY_EQUATION macro defined above
APPLY_EQUATION(elements[0][0]);
APPLY_EQUATION(elements[0][1]);
APPLY_EQUATION(elements[1][0]);
APPLY_EQUATION(elements[1][1]);
APPLY_EQUATION(elements[2][0]);
APPLY_EQUATION(elements[2][1]);
result = r;
} break;
case Variant::QUAT: {
// Get the quaternian for the initial and delta values
Quat i = initial_val;
Quat d = delta_val;
Quat r;
// Execute the equation on the quaternian values and mutate the r quaternian
// This uses the custom APPLY_EQUATION macro defined above
APPLY_EQUATION(x);
APPLY_EQUATION(y);
APPLY_EQUATION(z);
APPLY_EQUATION(w);
result = r;
} break;
case Variant::AABB: {
// Get the AABB's for the initial and delta values
AABB i = initial_val;
AABB d = delta_val;
AABB r;
// Execute the equation for the position and size of the AABB's and mutate the r AABB
// This uses the custom APPLY_EQUATION macro defined above
APPLY_EQUATION(position.x);
APPLY_EQUATION(position.y);
APPLY_EQUATION(position.z);
APPLY_EQUATION(size.x);
APPLY_EQUATION(size.y);
APPLY_EQUATION(size.z);
result = r;
} break;
case Variant::TRANSFORM: {
// Get the transforms for the initial and delta values
Transform i = initial_val;
Transform d = delta_val;
Transform r;
// Execute the equation for each of the transforms and their origin and mutate the r transform
// This uses the custom APPLY_EQUATION macro defined above
APPLY_EQUATION(basis.elements[0][0]);
APPLY_EQUATION(basis.elements[0][1]);
APPLY_EQUATION(basis.elements[0][2]);
APPLY_EQUATION(basis.elements[1][0]);
APPLY_EQUATION(basis.elements[1][1]);
APPLY_EQUATION(basis.elements[1][2]);
APPLY_EQUATION(basis.elements[2][0]);
APPLY_EQUATION(basis.elements[2][1]);
APPLY_EQUATION(basis.elements[2][2]);
APPLY_EQUATION(origin.x);
APPLY_EQUATION(origin.y);
APPLY_EQUATION(origin.z);
result = r;
} break;
case Variant::COLOR: {
// Get the Color for initial and delta value
Color i = initial_val;
Color d = delta_val;
Color r;
// Apply the equation on the Color RGBA, and mutate the r color
// This uses the custom APPLY_EQUATION macro defined above
APPLY_EQUATION(r);
APPLY_EQUATION(g);
APPLY_EQUATION(b);
APPLY_EQUATION(a);
result = r;
} break;
default: {
// If unknown, just return the initial value
result = initial_val;
} break;
};
#undef APPLY_EQUATION
// Return the result that was computed
return result;
}
bool Tween::_apply_tween_value(InterpolateData &p_data, Variant &value) {
// Get the object we want to apply the new value to
Object *object = ObjectDB::get_instance(p_data.id);
ERR_FAIL_COND_V(object == NULL, false);
// What kind of data are we mutating?
switch (p_data.type) {
case INTER_PROPERTY:
case FOLLOW_PROPERTY:
case TARGETING_PROPERTY: {
// Simply set the property on the object
bool valid = false;
object->set_indexed(p_data.key, value, &valid);
return valid;
}
case INTER_METHOD:
case FOLLOW_METHOD:
case TARGETING_METHOD: {
// We want to call the method on the target object
Variant::CallError error;
// Do we have a non-nil value passed in?
if (value.get_type() != Variant::NIL) {
// Pass it as an argument to the function call
Variant *arg[1] = { &value };
object->call(p_data.key[0], (const Variant **)arg, 1, error);
} else {
// Don't pass any argument
object->call(p_data.key[0], NULL, 0, error);
}
// Did we get an error from the function call?
if (error.error == Variant::CallError::CALL_OK)
return true;
return false;
}
case INTER_CALLBACK:
// Nothing to apply for a callback
break;
};
// No issues found!
return true;
}
void Tween::_tween_process(float p_delta) {
// Process all of the pending commands
_process_pending_commands();
// If the scale is 0, make no progress on the tweens
if (speed_scale == 0)
return;
// Update the delta and whether we are pending an update
p_delta *= speed_scale;
pending_update++;
// Are we repeating the interpolations?
if (repeat) {
// For each interpolation...
bool repeats_finished = true;
for (List<InterpolateData>::Element *E = interpolates.front(); E; E = E->next()) {
// Get the data from it
InterpolateData &data = E->get();
// Is not finished?
if (!data.finish) {
// We aren't finished yet, no need to check the rest
repeats_finished = false;
break;
}
}
// If we are all finished, we can reset all of the tweens
if (repeats_finished)
reset_all();
}
// Are all of the tweens complete?
bool all_finished = true;
// For each tween we wish to interpolate...
for (List<InterpolateData>::Element *E = interpolates.front(); E; E = E->next()) {
// Get the data from it
InterpolateData &data = E->get();
// Track if we hit one that isn't finished yet
all_finished = all_finished && data.finish;
// Is the data not active or already finished? No need to go any further
if (!data.active || data.finish)
continue;
// Get the target object for this interpolation
Object *object = ObjectDB::get_instance(data.id);
if (object == NULL)
continue;
// Are we still delaying this tween?
bool prev_delaying = data.elapsed <= data.delay;
data.elapsed += p_delta;
if (data.elapsed < data.delay)
continue;
else if (prev_delaying) {
// We can apply the tween's value to the data and emit that the tween has started
_apply_tween_value(data, data.initial_val);
emit_signal("tween_started", object, NodePath(Vector<StringName>(), data.key, false));
}
// Are we at the end of the tween?
if (data.elapsed > (data.delay + data.duration)) {
// Set the elapsed time to the end and mark this one as finished
data.elapsed = data.delay + data.duration;
data.finish = true;
}
// Are we interpolating a callback?
if (data.type == INTER_CALLBACK) {
// Is the tween completed?
if (data.finish) {
// Are we calling this callback deferred or immediately?
if (data.call_deferred) {
// Run the deferred function callback, applying the correct number of arguments
switch (data.args) {
case 0:
object->call_deferred(data.key[0]);
break;
case 1:
object->call_deferred(data.key[0], data.arg[0]);
break;
case 2:
object->call_deferred(data.key[0], data.arg[0], data.arg[1]);
break;
case 3:
object->call_deferred(data.key[0], data.arg[0], data.arg[1], data.arg[2]);
break;
case 4:
object->call_deferred(data.key[0], data.arg[0], data.arg[1], data.arg[2], data.arg[3]);
break;
case 5:
object->call_deferred(data.key[0], data.arg[0], data.arg[1], data.arg[2], data.arg[3], data.arg[4]);
break;
}
} else {
// Call the function directly with the arguments
Variant::CallError error;
Variant *arg[5] = {
&data.arg[0],
&data.arg[1],
&data.arg[2],
&data.arg[3],
&data.arg[4],
};
object->call(data.key[0], (const Variant **)arg, data.args, error);
}
}
} else {
// We can apply the value directly
Variant result = _run_equation(data);
_apply_tween_value(data, result);
// Emit that the tween has taken a step
emit_signal("tween_step", object, NodePath(Vector<StringName>(), data.key, false), data.elapsed, result);
}
// Is the tween now finished?
if (data.finish) {
// Set it to the final value directly
_apply_tween_value(data, data.final_val);
// Mark the tween as completed and emit the signal
data.elapsed = 0;
emit_signal("tween_completed", object, NodePath(Vector<StringName>(), data.key, false));
// If we are not repeating the tween, remove it
if (!repeat)
call_deferred("_remove_by_uid", data.uid);
} else if (!repeat) {
// Check whether all tweens are finished
all_finished = all_finished && data.finish;
}
}
// One less update left to go
pending_update--;
// If all tweens are completed, we no longer need to be active
if (all_finished) {
set_active(false);
emit_signal("tween_all_completed");
}
}
void Tween::set_tween_process_mode(TweenProcessMode p_mode) {
tween_process_mode = p_mode;
}
Tween::TweenProcessMode Tween::get_tween_process_mode() const {
return tween_process_mode;
}
bool Tween::is_active() const {
return is_processing_internal() || is_physics_processing_internal();
}
void Tween::set_active(bool p_active) {
// Do nothing if it's the same active mode that we currently are
if (is_active() == p_active)
return;
// Depending on physics or idle, set processing
switch (tween_process_mode) {
case TWEEN_PROCESS_IDLE: set_process_internal(p_active); break;
case TWEEN_PROCESS_PHYSICS: set_physics_process_internal(p_active); break;
}
}
bool Tween::is_repeat() const {
return repeat;
}
void Tween::set_repeat(bool p_repeat) {
repeat = p_repeat;
}
void Tween::set_speed_scale(float p_speed) {
speed_scale = p_speed;
}
float Tween::get_speed_scale() const {
return speed_scale;
}
bool Tween::start() {
// Are there any pending updates?
if (pending_update != 0) {
// Start the tweens after deferring
call_deferred("start");
return true;
}
// We want to be activated
set_active(true);
return true;
}
bool Tween::reset(Object *p_object, StringName p_key) {
// Find all interpolations that use the same object and target string
pending_update++;
for (List<InterpolateData>::Element *E = interpolates.front(); E; E = E->next()) {
// Get the target object
InterpolateData &data = E->get();
Object *object = ObjectDB::get_instance(data.id);
if (object == NULL)
continue;
// Do we have the correct object and key?
if (object == p_object && (data.concatenated_key == p_key || p_key == "")) {
// Reset the tween to the initial state
data.elapsed = 0;
data.finish = false;
// Also apply the initial state if there isn't a delay
if (data.delay == 0)
_apply_tween_value(data, data.initial_val);
}
}
pending_update--;
return true;
}
bool Tween::reset_all() {
// Go through all interpolations
pending_update++;
for (List<InterpolateData>::Element *E = interpolates.front(); E; E = E->next()) {
// Get the target data and set it back to the initial state
InterpolateData &data = E->get();
data.elapsed = 0;
data.finish = false;
// If there isn't a delay, apply the value to the object
if (data.delay == 0)
_apply_tween_value(data, data.initial_val);
}
pending_update--;
return true;
}
bool Tween::stop(Object *p_object, StringName p_key) {
// Find the tween that has the given target object and string key
pending_update++;
for (List<InterpolateData>::Element *E = interpolates.front(); E; E = E->next()) {
// Get the object the tween is targeting
InterpolateData &data = E->get();
Object *object = ObjectDB::get_instance(data.id);
if (object == NULL)
continue;
// Is this the correct object and does it have the given key?
if (object == p_object && (data.concatenated_key == p_key || p_key == ""))
// Disable the tween
data.active = false;
}
pending_update--;
return true;
}
bool Tween::stop_all() {
// We no longer need to be active since all tweens have been stopped
set_active(false);
// For each interpolation...
pending_update++;
for (List<InterpolateData>::Element *E = interpolates.front(); E; E = E->next()) {
// Simply set it inactive
InterpolateData &data = E->get();
data.active = false;
}
pending_update--;
return true;
}
bool Tween::resume(Object *p_object, StringName p_key) {
// We need to be activated
// TODO: What if no tween is found??
set_active(true);
// Find the tween that uses the given target object and string key
pending_update++;
for (List<InterpolateData>::Element *E = interpolates.front(); E; E = E->next()) {
// Grab the object
InterpolateData &data = E->get();
Object *object = ObjectDB::get_instance(data.id);
if (object == NULL)
continue;
// If the object and string key match, activate it
if (object == p_object && (data.concatenated_key == p_key || p_key == ""))
data.active = true;
}
pending_update--;
return true;
}
bool Tween::resume_all() {
// Set ourselves active so we can process tweens
// TODO: What if there are no tweens? We get set to active for no reason!
set_active(true);
// For each interpolation...
pending_update++;
for (List<InterpolateData>::Element *E = interpolates.front(); E; E = E->next()) {
// Simply grab it and set it to active
InterpolateData &data = E->get();
data.active = true;
}
pending_update--;
return true;
}
bool Tween::remove(Object *p_object, StringName p_key) {
// If we are still updating, call this function again later
if (pending_update != 0) {
call_deferred("remove", p_object, p_key);
return true;
}
// For each interpolation...
List<List<InterpolateData>::Element *> for_removal;
for (List<InterpolateData>::Element *E = interpolates.front(); E; E = E->next()) {
// Get the target object
InterpolateData &data = E->get();
Object *object = ObjectDB::get_instance(data.id);
if (object == NULL)
continue;
// If the target object and string key match, queue it for removal
if (object == p_object && (data.concatenated_key == p_key || p_key == "")) {
for_removal.push_back(E);
}
}
// For each interpolation we wish to remove...
for (List<List<InterpolateData>::Element *>::Element *E = for_removal.front(); E; E = E->next()) {
// Erase it
interpolates.erase(E->get());
}
return true;
}
void Tween::_remove_by_uid(int uid) {
// If we are still updating, call this function again later
if (pending_update != 0) {
call_deferred("_remove_by_uid", uid);
return;
}
// Find the interpolation that matches the given UID
for (List<InterpolateData>::Element *E = interpolates.front(); E; E = E->next()) {
if (uid == E->get().uid) {
// It matches, erase it and stop looking
E->erase();
break;
}
}
}
void Tween::_push_interpolate_data(InterpolateData &p_data) {
pending_update++;
// Add the new interpolation
p_data.uid = ++uid;
interpolates.push_back(p_data);
pending_update--;
}
bool Tween::remove_all() {
// If we are still updating, call this function again later
if (pending_update != 0) {
call_deferred("remove_all");
return true;
}
// We no longer need to be active
set_active(false);
// Clear out all interpolations and reset the uid
interpolates.clear();
uid = 0;
return true;
}
bool Tween::seek(real_t p_time) {
// Go through each interpolation...
pending_update++;
for (List<InterpolateData>::Element *E = interpolates.front(); E; E = E->next()) {
// Get the target data
InterpolateData &data = E->get();
// Update the elapsed data to be set to the target time
data.elapsed = p_time;
// Are we at the end?
if (data.elapsed < data.delay) {
// There is still time left to go
data.finish = false;
continue;
} else if (data.elapsed >= (data.delay + data.duration)) {
// We are past the end of it, set the elapsed time to the end and mark as finished
data.elapsed = (data.delay + data.duration);
data.finish = true;
} else {
// We are not finished with this interpolation yet
data.finish = false;
}
// If we are a callback, do nothing special
if (data.type == INTER_CALLBACK) {
continue;
}
// Run the equation on the data and apply the value
Variant result = _run_equation(data);
_apply_tween_value(data, result);
}
pending_update--;
return true;
}
real_t Tween::tell() const {
// We want to grab the position of the furthest along tween
pending_update++;
real_t pos = 0;
// For each interpolation...
for (const List<InterpolateData>::Element *E = interpolates.front(); E; E = E->next()) {
// Get the data and figure out if it's position is further along than the previous ones
const InterpolateData &data = E->get();
if (data.elapsed > pos)
// Save it if so
pos = data.elapsed;
}
pending_update--;
return pos;
}
real_t Tween::get_runtime() const {
// If the tween isn't moving, it'll last forever
if (speed_scale == 0) {
return INFINITY;
}
pending_update++;
// For each interpolation...
real_t runtime = 0;
for (const List<InterpolateData>::Element *E = interpolates.front(); E; E = E->next()) {
// Get the tween data and see if it's runtime is greater than the previous tweens
const InterpolateData &data = E->get();
real_t t = data.delay + data.duration;
if (t > runtime)
// This is the longest running tween
runtime = t;
}
pending_update--;
// Adjust the runtime for the current speed scale
return runtime / speed_scale;
}
bool Tween::_calc_delta_val(const Variant &p_initial_val, const Variant &p_final_val, Variant &p_delta_val) {
// Get the initial, final, and delta values
const Variant &initial_val = p_initial_val;
const Variant &final_val = p_final_val;
Variant &delta_val = p_delta_val;
// What kind of data are we interpolating?
switch (initial_val.get_type()) {
case Variant::BOOL:
// We'll treat booleans just like integers
case Variant::INT:
// Compute the integer delta
delta_val = (int)final_val - (int)initial_val;
break;
case Variant::REAL:
// Convert to REAL and find the delta
delta_val = (real_t)final_val - (real_t)initial_val;
break;
case Variant::VECTOR2:
// Convert to Vectors and find the delta
delta_val = final_val.operator Vector2() - initial_val.operator Vector2();
break;
case Variant::VECTOR3:
// Convert to Vectors and find the delta
delta_val = final_val.operator Vector3() - initial_val.operator Vector3();
break;
case Variant::BASIS: {
// Build a new basis which is the delta between the initial and final values
Basis i = initial_val;
Basis f = final_val;
delta_val = Basis(f.elements[0][0] - i.elements[0][0],
f.elements[0][1] - i.elements[0][1],
f.elements[0][2] - i.elements[0][2],
f.elements[1][0] - i.elements[1][0],
f.elements[1][1] - i.elements[1][1],
f.elements[1][2] - i.elements[1][2],
f.elements[2][0] - i.elements[2][0],
f.elements[2][1] - i.elements[2][1],
f.elements[2][2] - i.elements[2][2]);
} break;
case Variant::TRANSFORM2D: {
// Build a new transform which is the difference between the initial and final values
Transform2D i = initial_val;
Transform2D f = final_val;
Transform2D d = Transform2D();
d[0][0] = f.elements[0][0] - i.elements[0][0];
d[0][1] = f.elements[0][1] - i.elements[0][1];
d[1][0] = f.elements[1][0] - i.elements[1][0];
d[1][1] = f.elements[1][1] - i.elements[1][1];
d[2][0] = f.elements[2][0] - i.elements[2][0];
d[2][1] = f.elements[2][1] - i.elements[2][1];
delta_val = d;
} break;
case Variant::QUAT:
// Convert to quaternianls and find the delta
delta_val = final_val.operator Quat() - initial_val.operator Quat();
break;
case Variant::AABB: {
// Build a new AABB and use the new position and sizes to make a delta
AABB i = initial_val;
AABB f = final_val;
delta_val = AABB(f.position - i.position, f.size - i.size);
} break;
case Variant::TRANSFORM: {
// Build a new transform which is the difference between the initial and final values
Transform i = initial_val;
Transform f = final_val;
Transform d;
d.set(f.basis.elements[0][0] - i.basis.elements[0][0],
f.basis.elements[0][1] - i.basis.elements[0][1],
f.basis.elements[0][2] - i.basis.elements[0][2],
f.basis.elements[1][0] - i.basis.elements[1][0],
f.basis.elements[1][1] - i.basis.elements[1][1],
f.basis.elements[1][2] - i.basis.elements[1][2],
f.basis.elements[2][0] - i.basis.elements[2][0],
f.basis.elements[2][1] - i.basis.elements[2][1],
f.basis.elements[2][2] - i.basis.elements[2][2],
f.origin.x - i.origin.x,
f.origin.y - i.origin.y,
f.origin.z - i.origin.z);
delta_val = d;
} break;
case Variant::COLOR: {
// Make a new color which is the difference between each the color's RGBA attributes
Color i = initial_val;
Color f = final_val;
delta_val = Color(f.r - i.r, f.g - i.g, f.b - i.b, f.a - i.a);
} break;
default:
// TODO: Should move away from a 'magic string'?
ERR_PRINT("Invalid param type, except(int/real/vector2/vector/matrix/matrix32/quat/aabb/transform/color)");
return false;
};
return true;
}
bool Tween::_build_interpolation(InterpolateType p_interpolation_type, Object *p_object, NodePath *p_property, StringName *p_method, Variant p_initial_val, Variant p_final_val, real_t p_duration, TransitionType p_trans_type, EaseType p_ease_type, real_t p_delay) {
// TODO: Add initialization+implementation for remaining interpolation types
// TODO: Fix this method's organization to take advantage of the type
// Make a new interpolation data
InterpolateData data;
data.active = true;
data.type = p_interpolation_type;
data.finish = false;
data.elapsed = 0;
// Validate and apply interpolation data
// Give it the object
ERR_EXPLAIN("Invalid object provided to Tween!");
ERR_FAIL_COND_V(p_object == NULL, false); // Is the object real
ERR_FAIL_COND_V(!ObjectDB::instance_validate(p_object), false); // Is the object a valid instance?
data.id = p_object->get_instance_id();
// Validate the initial and final values
ERR_EXPLAIN("Initial value type does not match final value type!"); // TODO: Print both types to make debugging easier
ERR_FAIL_COND_V(p_initial_val.get_type() != p_final_val.get_type(), false); // Do the initial and final value types match?
data.initial_val = p_initial_val;
data.final_val = p_final_val;
// Check the Duration
ERR_EXPLAIN("Only non-negative duration values allowed in Tweens!");
ERR_FAIL_COND_V(p_duration < 0, false); // Is the tween duration non-negative
data.duration = p_duration;
// Tween Delay
ERR_EXPLAIN("Only non-negative delay values allowed in Tweens!");
ERR_FAIL_COND_V(p_delay < 0, false); // Is the delay non-negative?
data.delay = p_delay;
// Transition type
ERR_EXPLAIN("Invalid transition type provided to Tween");
ERR_FAIL_COND_V(p_trans_type < 0 || p_trans_type >= TRANS_COUNT, false); // Is the transition type valid
data.trans_type = p_trans_type;
// Easing type
ERR_EXPLAIN("Invalid easing type provided to Tween");
ERR_FAIL_COND_V(p_ease_type < 0 || p_ease_type >= EASE_COUNT, false); // Is the easing type valid
data.ease_type = p_ease_type;
// Is the property defined?
if (p_property) {
// Check that the object actually contains the given property
bool prop_valid = false;
p_object->get_indexed(p_property->get_subnames(), &prop_valid);
ERR_EXPLAIN("Tween target object has no property named: " + p_property->get_concatenated_subnames());
ERR_FAIL_COND_V(!prop_valid, false);
data.key = p_property->get_subnames();
data.concatenated_key = p_property->get_concatenated_subnames();
}
// Is the method defined?
if (p_method) {
// Does the object even have the requested method?
ERR_EXPLAIN("Tween target object has no method named: " + *p_method); // TODO: Fix this error message
ERR_FAIL_COND_V(!p_object->has_method(*p_method), false);
data.key.push_back(*p_method);
data.concatenated_key = *p_method;
}
// Is there not a valid delta?
if (!_calc_delta_val(data.initial_val, data.final_val, data.delta_val))
return false;
// Add this interpolation to the total
_push_interpolate_data(data);
return true;
}
bool Tween::interpolate_property(Object *p_object, NodePath p_property, Variant p_initial_val, Variant p_final_val, real_t p_duration, TransitionType p_trans_type, EaseType p_ease_type, real_t p_delay) {
// If we are busy updating, call this function again later
if (pending_update != 0) {
_add_pending_command("interpolate_property", p_object, p_property, p_initial_val, p_final_val, p_duration, p_trans_type, p_ease_type, p_delay);
return true;
}
// Get the property from the node path
p_property = p_property.get_as_property_path();
// If no initial value given, grab the initial value from the object
// TODO: Is this documented? This is very useful and removes a lot of clutter from tweens!
if (p_initial_val.get_type() == Variant::NIL) p_initial_val = p_object->get_indexed(p_property.get_subnames());
// Convert any integers into REALs as they are better for interpolation
if (p_initial_val.get_type() == Variant::INT) p_initial_val = p_initial_val.operator real_t();
if (p_final_val.get_type() == Variant::INT) p_final_val = p_final_val.operator real_t();
// Build the interpolation data
bool result = _build_interpolation(INTER_PROPERTY, p_object, &p_property, NULL, p_initial_val, p_final_val, p_duration, p_trans_type, p_ease_type, p_delay);
return result;
}
bool Tween::interpolate_method(Object *p_object, StringName p_method, Variant p_initial_val, Variant p_final_val, real_t p_duration, TransitionType p_trans_type, EaseType p_ease_type, real_t p_delay) {
// If we are busy updating, call this function again later
if (pending_update != 0) {
_add_pending_command("interpolate_method", p_object, p_method, p_initial_val, p_final_val, p_duration, p_trans_type, p_ease_type, p_delay);
return true;
}
// Convert any integers into REALs as they are better for interpolation
if (p_initial_val.get_type() == Variant::INT) p_initial_val = p_initial_val.operator real_t();
if (p_final_val.get_type() == Variant::INT) p_final_val = p_final_val.operator real_t();
// Build the interpolation data
bool result = _build_interpolation(INTER_METHOD, p_object, NULL, &p_method, p_initial_val, p_final_val, p_duration, p_trans_type, p_ease_type, p_delay);
return result;
}
bool Tween::interpolate_callback(Object *p_object, real_t p_duration, String p_callback, VARIANT_ARG_DECLARE) {
// If we are already updating, call this function again later
if (pending_update != 0) {
_add_pending_command("interpolate_callback", p_object, p_duration, p_callback, p_arg1, p_arg2, p_arg3, p_arg4, p_arg5);
return true;
}
// Check that the target object is valid
ERR_FAIL_COND_V(p_object == NULL, false);
ERR_FAIL_COND_V(!ObjectDB::instance_validate(p_object), false);
// Duration cannot be negative
ERR_FAIL_COND_V(p_duration < 0, false);
// Check whether the object even has the callback
ERR_EXPLAIN("Object has no callback named: %s" + p_callback);
ERR_FAIL_COND_V(!p_object->has_method(p_callback), false);
// Build a new InterpolationData
InterpolateData data;
data.active = true;
data.type = INTER_CALLBACK;
data.finish = false;
data.call_deferred = false;
data.elapsed = 0;
// Give the data it's configuration
data.id = p_object->get_instance_id();
data.key.push_back(p_callback);
data.concatenated_key = p_callback;
data.duration = p_duration;
data.delay = 0;
// Add arguments to the interpolation
int args = 0;
if (p_arg5.get_type() != Variant::NIL)
args = 5;
else if (p_arg4.get_type() != Variant::NIL)
args = 4;
else if (p_arg3.get_type() != Variant::NIL)
args = 3;
else if (p_arg2.get_type() != Variant::NIL)
args = 2;
else if (p_arg1.get_type() != Variant::NIL)
args = 1;
else
args = 0;
data.args = args;
data.arg[0] = p_arg1;
data.arg[1] = p_arg2;
data.arg[2] = p_arg3;
data.arg[3] = p_arg4;
data.arg[4] = p_arg5;
// Add the new interpolation
_push_interpolate_data(data);
return true;
}
bool Tween::interpolate_deferred_callback(Object *p_object, real_t p_duration, String p_callback, VARIANT_ARG_DECLARE) {
// If we are already updating, call this function again later
if (pending_update != 0) {
_add_pending_command("interpolate_deferred_callback", p_object, p_duration, p_callback, p_arg1, p_arg2, p_arg3, p_arg4, p_arg5);
return true;
}
// Check that the target object is valid
ERR_FAIL_COND_V(p_object == NULL, false);
ERR_FAIL_COND_V(!ObjectDB::instance_validate(p_object), false);
// No negative durations allowed
ERR_FAIL_COND_V(p_duration < 0, false);
// Confirm the callback exists on the object
ERR_EXPLAIN("Object has no callback named: %s" + p_callback);
ERR_FAIL_COND_V(!p_object->has_method(p_callback), false);
// Create a new InterpolateData for the callback
InterpolateData data;
data.active = true;
data.type = INTER_CALLBACK;
data.finish = false;
data.call_deferred = true;
data.elapsed = 0;
// Give the data it's configuration
data.id = p_object->get_instance_id();
data.key.push_back(p_callback);
data.concatenated_key = p_callback;
data.duration = p_duration;
data.delay = 0;
// Collect arguments for the callback
int args = 0;
if (p_arg5.get_type() != Variant::NIL)
args = 5;
else if (p_arg4.get_type() != Variant::NIL)
args = 4;
else if (p_arg3.get_type() != Variant::NIL)
args = 3;
else if (p_arg2.get_type() != Variant::NIL)
args = 2;
else if (p_arg1.get_type() != Variant::NIL)
args = 1;
else
args = 0;
data.args = args;
data.arg[0] = p_arg1;
data.arg[1] = p_arg2;
data.arg[2] = p_arg3;
data.arg[3] = p_arg4;
data.arg[4] = p_arg5;
// Add the new interpolation
_push_interpolate_data(data);
return true;
}
bool Tween::follow_property(Object *p_object, NodePath p_property, Variant p_initial_val, Object *p_target, NodePath p_target_property, real_t p_duration, TransitionType p_trans_type, EaseType p_ease_type, real_t p_delay) {
// If we are already updating, call this function again later
if (pending_update != 0) {
_add_pending_command("follow_property", p_object, p_property, p_initial_val, p_target, p_target_property, p_duration, p_trans_type, p_ease_type, p_delay);
return true;
}
// Get the two properties from their paths
p_property = p_property.get_as_property_path();
p_target_property = p_target_property.get_as_property_path();
// If no initial value is given, grab it from the source object
// TODO: Is this documented? It's really helpful for decluttering tweens
if (p_initial_val.get_type() == Variant::NIL) p_initial_val = p_object->get_indexed(p_property.get_subnames());
// Convert initial INT values to REAL as they are better for interpolation
if (p_initial_val.get_type() == Variant::INT) p_initial_val = p_initial_val.operator real_t();
// Confirm the source and target objects are valid
ERR_FAIL_COND_V(p_object == NULL, false);
ERR_FAIL_COND_V(!ObjectDB::instance_validate(p_object), false);
ERR_FAIL_COND_V(p_target == NULL, false);
ERR_FAIL_COND_V(!ObjectDB::instance_validate(p_target), false);
// No negative durations
ERR_FAIL_COND_V(p_duration < 0, false);
// Ensure transition and easing types are valid
ERR_FAIL_COND_V(p_trans_type < 0 || p_trans_type >= TRANS_COUNT, false);
ERR_FAIL_COND_V(p_ease_type < 0 || p_ease_type >= EASE_COUNT, false);
// No negative delays
ERR_FAIL_COND_V(p_delay < 0, false);
// Confirm the source and target objects have the desired properties
bool prop_valid = false;
p_object->get_indexed(p_property.get_subnames(), &prop_valid);
ERR_FAIL_COND_V(!prop_valid, false);
bool target_prop_valid = false;
Variant target_val = p_target->get_indexed(p_target_property.get_subnames(), &target_prop_valid);
ERR_FAIL_COND_V(!target_prop_valid, false);
// Convert target INT to REAL since it is better for interpolation
if (target_val.get_type() == Variant::INT) target_val = target_val.operator real_t();
// Verify that the target value and initial value are the same type
ERR_FAIL_COND_V(target_val.get_type() != p_initial_val.get_type(), false);
// Create a new InterpolateData
InterpolateData data;
data.active = true;
data.type = FOLLOW_PROPERTY;
data.finish = false;
data.elapsed = 0;
// Give the InterpolateData it's configuration
data.id = p_object->get_instance_id();
data.key = p_property.get_subnames();
data.concatenated_key = p_property.get_concatenated_subnames();
data.initial_val = p_initial_val;
data.target_id = p_target->get_instance_id();
data.target_key = p_target_property.get_subnames();
data.duration = p_duration;
data.trans_type = p_trans_type;
data.ease_type = p_ease_type;
data.delay = p_delay;
// Add the interpolation
_push_interpolate_data(data);
return true;
}
bool Tween::follow_method(Object *p_object, StringName p_method, Variant p_initial_val, Object *p_target, StringName p_target_method, real_t p_duration, TransitionType p_trans_type, EaseType p_ease_type, real_t p_delay) {
// If we are currently updating, call this function again later
if (pending_update != 0) {
_add_pending_command("follow_method", p_object, p_method, p_initial_val, p_target, p_target_method, p_duration, p_trans_type, p_ease_type, p_delay);
return true;
}
// Convert initial INT values to REAL as they are better for interpolation
if (p_initial_val.get_type() == Variant::INT) p_initial_val = p_initial_val.operator real_t();
// Verify the source and target objects are valid
ERR_FAIL_COND_V(p_object == NULL, false);
ERR_FAIL_COND_V(!ObjectDB::instance_validate(p_object), false);
ERR_FAIL_COND_V(p_target == NULL, false);
ERR_FAIL_COND_V(!ObjectDB::instance_validate(p_target), false);
// No negative durations
ERR_FAIL_COND_V(p_duration < 0, false);
// Ensure that the transition and ease types are valid
ERR_FAIL_COND_V(p_trans_type < 0 || p_trans_type >= TRANS_COUNT, false);
ERR_FAIL_COND_V(p_ease_type < 0 || p_ease_type >= EASE_COUNT, false);
// No negative delays
ERR_FAIL_COND_V(p_delay < 0, false);
// Confirm both objects have the target methods
ERR_EXPLAIN("Object has no method named: %s" + p_method);
ERR_FAIL_COND_V(!p_object->has_method(p_method), false);
ERR_EXPLAIN("Target has no method named: %s" + p_target_method);
ERR_FAIL_COND_V(!p_target->has_method(p_target_method), false);
// Call the method to get the target value
Variant::CallError error;
Variant target_val = p_target->call(p_target_method, NULL, 0, error);
ERR_FAIL_COND_V(error.error != Variant::CallError::CALL_OK, false);
// Convert target INT values to REAL as they are better for interpolation
if (target_val.get_type() == Variant::INT) target_val = target_val.operator real_t();
ERR_FAIL_COND_V(target_val.get_type() != p_initial_val.get_type(), false);
// Make the new InterpolateData for the method follow
InterpolateData data;
data.active = true;
data.type = FOLLOW_METHOD;
data.finish = false;
data.elapsed = 0;
// Give the data it's configuration
data.id = p_object->get_instance_id();
data.key.push_back(p_method);
data.concatenated_key = p_method;
data.initial_val = p_initial_val;
data.target_id = p_target->get_instance_id();
data.target_key.push_back(p_target_method);
data.duration = p_duration;
data.trans_type = p_trans_type;
data.ease_type = p_ease_type;
data.delay = p_delay;
// Add the new interpolation
_push_interpolate_data(data);
return true;
}
bool Tween::targeting_property(Object *p_object, NodePath p_property, Object *p_initial, NodePath p_initial_property, Variant p_final_val, real_t p_duration, TransitionType p_trans_type, EaseType p_ease_type, real_t p_delay) {
// If we are currently updating, call this function again later
if (pending_update != 0) {
_add_pending_command("targeting_property", p_object, p_property, p_initial, p_initial_property, p_final_val, p_duration, p_trans_type, p_ease_type, p_delay);
return true;
}
// Grab the target property and the target property
p_property = p_property.get_as_property_path();
p_initial_property = p_initial_property.get_as_property_path();
// Convert the initial INT values to REAL as they are better for Interpolation
if (p_final_val.get_type() == Variant::INT) p_final_val = p_final_val.operator real_t();
// Verify both objects are valid
ERR_FAIL_COND_V(p_object == NULL, false);
ERR_FAIL_COND_V(!ObjectDB::instance_validate(p_object), false);
ERR_FAIL_COND_V(p_initial == NULL, false);
ERR_FAIL_COND_V(!ObjectDB::instance_validate(p_initial), false);
// No negative durations
ERR_FAIL_COND_V(p_duration < 0, false);
// Ensure transition and easing types are valid
ERR_FAIL_COND_V(p_trans_type < 0 || p_trans_type >= TRANS_COUNT, false);
ERR_FAIL_COND_V(p_ease_type < 0 || p_ease_type >= EASE_COUNT, false);
// No negative delays
ERR_FAIL_COND_V(p_delay < 0, false);
// Ensure the initial and target properties exist on their objects
bool prop_valid = false;
p_object->get_indexed(p_property.get_subnames(), &prop_valid);
ERR_FAIL_COND_V(!prop_valid, false);
bool initial_prop_valid = false;
Variant initial_val = p_initial->get_indexed(p_initial_property.get_subnames(), &initial_prop_valid);
ERR_FAIL_COND_V(!initial_prop_valid, false);
// Convert the initial INT value to REAL as it is better for interpolation
if (initial_val.get_type() == Variant::INT) initial_val = initial_val.operator real_t();
ERR_FAIL_COND_V(initial_val.get_type() != p_final_val.get_type(), false);
// Build the InterpolateData object
InterpolateData data;
data.active = true;
data.type = TARGETING_PROPERTY;
data.finish = false;
data.elapsed = 0;
// Give the data it's configuration
data.id = p_object->get_instance_id();
data.key = p_property.get_subnames();
data.concatenated_key = p_property.get_concatenated_subnames();
data.target_id = p_initial->get_instance_id();
data.target_key = p_initial_property.get_subnames();
data.initial_val = initial_val;
data.final_val = p_final_val;
data.duration = p_duration;
data.trans_type = p_trans_type;
data.ease_type = p_ease_type;
data.delay = p_delay;
// Ensure there is a valid delta
if (!_calc_delta_val(data.initial_val, data.final_val, data.delta_val))
return false;
// Add the interpolation
_push_interpolate_data(data);
return true;
}
bool Tween::targeting_method(Object *p_object, StringName p_method, Object *p_initial, StringName p_initial_method, Variant p_final_val, real_t p_duration, TransitionType p_trans_type, EaseType p_ease_type, real_t p_delay) {
// If we are currently updating, call this function again later
if (pending_update != 0) {
_add_pending_command("targeting_method", p_object, p_method, p_initial, p_initial_method, p_final_val, p_duration, p_trans_type, p_ease_type, p_delay);
return true;
}
// Convert final INT values to REAL as they are better for interpolation
if (p_final_val.get_type() == Variant::INT) p_final_val = p_final_val.operator real_t();
// Make sure the given objects are valid
ERR_FAIL_COND_V(p_object == NULL, false);
ERR_FAIL_COND_V(!ObjectDB::instance_validate(p_object), false);
ERR_FAIL_COND_V(p_initial == NULL, false);
ERR_FAIL_COND_V(!ObjectDB::instance_validate(p_initial), false);
// No negative durations
ERR_FAIL_COND_V(p_duration < 0, false);
// Ensure transition and easing types are valid
ERR_FAIL_COND_V(p_trans_type < 0 || p_trans_type >= TRANS_COUNT, false);
ERR_FAIL_COND_V(p_ease_type < 0 || p_ease_type >= EASE_COUNT, false);
// No negative delays
ERR_FAIL_COND_V(p_delay < 0, false);
// Make sure both objects have the given method
ERR_EXPLAIN("Object has no method named: %s" + p_method);
ERR_FAIL_COND_V(!p_object->has_method(p_method), false);
ERR_EXPLAIN("Initial Object has no method named: %s" + p_initial_method);
ERR_FAIL_COND_V(!p_initial->has_method(p_initial_method), false);
// Call the method to get the initial value
Variant::CallError error;
Variant initial_val = p_initial->call(p_initial_method, NULL, 0, error);
ERR_FAIL_COND_V(error.error != Variant::CallError::CALL_OK, false);
// Convert initial INT values to REAL as they aer better for interpolation
if (initial_val.get_type() == Variant::INT) initial_val = initial_val.operator real_t();
ERR_FAIL_COND_V(initial_val.get_type() != p_final_val.get_type(), false);
// Build the new InterpolateData object
InterpolateData data;
data.active = true;
data.type = TARGETING_METHOD;
data.finish = false;
data.elapsed = 0;
// Configure the data
data.id = p_object->get_instance_id();
data.key.push_back(p_method);
data.concatenated_key = p_method;
data.target_id = p_initial->get_instance_id();
data.target_key.push_back(p_initial_method);
data.initial_val = initial_val;
data.final_val = p_final_val;
data.duration = p_duration;
data.trans_type = p_trans_type;
data.ease_type = p_ease_type;
data.delay = p_delay;
// Ensure there is a valid delta
if (!_calc_delta_val(data.initial_val, data.final_val, data.delta_val))
return false;
// Add the interpolation
_push_interpolate_data(data);
return true;
}
Tween::Tween() {
// Initialize tween attributes
tween_process_mode = TWEEN_PROCESS_IDLE;
repeat = false;
speed_scale = 1;
pending_update = 0;
uid = 0;
}
Tween::~Tween() {
}
|