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
/**************************************************************************/
/*  animation_blend_tree.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_blend_tree.h"

#include "scene/resources/animation.h"

void AnimationNodeAnimation::set_animation(const StringName &p_name) {
	animation = p_name;
}

StringName AnimationNodeAnimation::get_animation() const {
	return animation;
}

Vector<String> (*AnimationNodeAnimation::get_editable_animation_list)() = nullptr;

void AnimationNodeAnimation::get_parameter_list(List<PropertyInfo> *r_list) const {
	AnimationNode::get_parameter_list(r_list);
	r_list->push_back(PropertyInfo(Variant::BOOL, backward, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE));
}

Variant AnimationNodeAnimation::get_parameter_default_value(const StringName &p_parameter) const {
	Variant ret = AnimationNode::get_parameter_default_value(p_parameter);
	if (ret != Variant()) {
		return ret;
	}
	if (p_parameter == backward) {
		return false;
	}
	return 0;
}

AnimationNode::NodeTimeInfo AnimationNodeAnimation::get_node_time_info() const {
	NodeTimeInfo nti;
	if (!process_state->tree->has_animation(animation)) {
		return nti;
	}

	if (use_custom_timeline) {
		nti.length = timeline_length;
		nti.loop_mode = loop_mode;
	} else {
		Ref<Animation> anim = process_state->tree->get_animation(animation);
		nti.length = (double)anim->get_length();
		nti.loop_mode = anim->get_loop_mode();
	}
	nti.position = get_parameter(current_position);

	return nti;
}

void AnimationNodeAnimation::_validate_property(PropertyInfo &p_property) const {
	if (p_property.name == "animation" && get_editable_animation_list) {
		Vector<String> names = get_editable_animation_list();
		String anims;
		for (int i = 0; i < names.size(); i++) {
			if (i > 0) {
				anims += ",";
			}
			anims += String(names[i]);
		}
		if (!anims.is_empty()) {
			p_property.hint = PROPERTY_HINT_ENUM;
			p_property.hint_string = anims;
		}
	}

	if (!use_custom_timeline) {
		if (p_property.name == "timeline_length" || p_property.name == "start_offset" || p_property.name == "loop_mode" || p_property.name == "stretch_time_scale") {
			p_property.usage = PROPERTY_USAGE_NONE;
		}
	}
}

AnimationNode::NodeTimeInfo AnimationNodeAnimation::process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only) {
	process_state->is_testing = p_test_only;

	AnimationMixer::PlaybackInfo pi = p_playback_info;
	if (p_playback_info.seeked) {
		if (p_playback_info.is_external_seeking) {
			pi.delta = get_node_time_info().position - p_playback_info.time;
		}
	} else {
		pi.time = get_node_time_info().position + (get_parameter(backward) ? -p_playback_info.delta : p_playback_info.delta);
	}

	NodeTimeInfo nti = _process(pi, p_test_only);

	if (!p_test_only) {
		set_node_time_info(nti);
	}

	return nti;
}

AnimationNode::NodeTimeInfo AnimationNodeAnimation::_process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only) {
	if (!process_state->tree->has_animation(animation)) {
		AnimationNodeBlendTree *tree = Object::cast_to<AnimationNodeBlendTree>(node_state.parent);
		if (tree) {
			String node_name = tree->get_node_name(Ref<AnimationNodeAnimation>(this));
			make_invalid(vformat(RTR("On BlendTree node '%s', animation not found: '%s'"), node_name, animation));

		} else {
			make_invalid(vformat(RTR("Animation not found: '%s'"), animation));
		}

		return NodeTimeInfo();
	}

	Ref<Animation> anim = process_state->tree->get_animation(animation);
	double anim_size = (double)anim->get_length();

	NodeTimeInfo cur_nti = get_node_time_info();
	double cur_len = cur_nti.length;
	double cur_time = p_playback_info.time;
	double cur_delta = p_playback_info.delta;
	bool cur_backward = get_parameter(backward);

	Animation::LoopMode cur_loop_mode = cur_nti.loop_mode;
	double prev_time = cur_nti.position;

	Animation::LoopedFlag looped_flag = Animation::LOOPED_FLAG_NONE;
	bool node_backward = play_mode == PLAY_MODE_BACKWARD;

	bool p_seek = p_playback_info.seeked;
	bool p_is_external_seeking = p_playback_info.is_external_seeking;

	// 1. Progress for AnimationNode.
	bool will_end = Animation::is_greater_or_equal_approx(cur_time + cur_delta, cur_len);
	bool is_started = p_seek && !p_is_external_seeking && Math::is_zero_approx(cur_time);

	// 1. Progress for AnimationNode.
	if (is_started && advance_on_start) {
		cur_time = cur_delta;
	}
	if (cur_loop_mode != Animation::LOOP_NONE) {
		if (cur_loop_mode == Animation::LOOP_LINEAR) {
			if (!Math::is_zero_approx(cur_len)) {
				cur_time = Math::fposmod(cur_time, cur_len);
			}
			cur_backward = false;
		} else {
			if (!Math::is_zero_approx(cur_len)) {
				if (Animation::is_greater_or_equal_approx(prev_time, 0) && Animation::is_less_approx(cur_time, 0)) {
					cur_backward = !cur_backward;
				} else if (Animation::is_less_or_equal_approx(prev_time, cur_len) && Animation::is_greater_approx(cur_time, cur_len)) {
					cur_backward = !cur_backward;
				}
				cur_time = Math::pingpong(cur_time, cur_len);
			}
		}
	} else {
		if (Animation::is_less_approx(cur_time, 0)) {
			cur_delta += cur_time;
			cur_time = 0;
		} else if (Animation::is_greater_approx(cur_time, cur_len)) {
			cur_delta += cur_time - cur_len;
			cur_time = cur_len;
		}
		cur_backward = false;
		// If ended, don't progress AnimationNode. So set delta to 0.
		if (!Math::is_zero_approx(cur_delta)) {
			if (play_mode == PLAY_MODE_FORWARD) {
				if (Animation::is_greater_or_equal_approx(prev_time, cur_len)) {
					cur_delta = 0;
				}
			} else {
				if (Animation::is_less_or_equal_approx(prev_time, 0)) {
					cur_delta = 0;
				}
			}
		}
	}

	// 2. For return, store "AnimationNode" time info here, not "Animation" time info as below.
	NodeTimeInfo nti;
	nti.length = cur_len;
	nti.position = cur_time;
	nti.delta = cur_delta;
	nti.loop_mode = cur_loop_mode;
	nti.will_end = will_end;

	// 3. Progress for Animation.
	double prev_playback_time = prev_time + start_offset;
	double cur_playback_time = cur_time + start_offset;
	if (stretch_time_scale) {
		double mlt = anim_size / cur_len;
		prev_playback_time *= mlt;
		cur_playback_time *= mlt;
		cur_delta *= mlt;
	}
	if (cur_loop_mode == Animation::LOOP_LINEAR) {
		if (!Math::is_zero_approx(anim_size)) {
			prev_playback_time = Math::fposmod(prev_playback_time, anim_size);
			cur_playback_time = Math::fposmod(cur_playback_time, anim_size);
			if (Animation::is_greater_or_equal_approx(prev_playback_time, 0) && Animation::is_less_approx(cur_playback_time, 0)) {
				looped_flag = node_backward ? Animation::LOOPED_FLAG_END : Animation::LOOPED_FLAG_START;
			}
			if (Animation::is_less_or_equal_approx(prev_playback_time, anim_size) && Animation::is_greater_approx(cur_playback_time, anim_size)) {
				looped_flag = node_backward ? Animation::LOOPED_FLAG_START : Animation::LOOPED_FLAG_END;
			}
		}
	} else if (cur_loop_mode == Animation::LOOP_PINGPONG) {
		if (!Math::is_zero_approx(anim_size)) {
			if (Animation::is_greater_or_equal_approx(Math::fposmod(cur_playback_time, anim_size * 2.0), anim_size)) {
				cur_delta = -cur_delta; // Needed for retrieving discrete keys correctly.
			}
			prev_playback_time = Math::pingpong(prev_playback_time, anim_size);
			cur_playback_time = Math::pingpong(cur_playback_time, anim_size);
			if (Animation::is_greater_or_equal_approx(prev_playback_time, 0) && Animation::is_less_approx(cur_playback_time, 0)) {
				looped_flag = node_backward ? Animation::LOOPED_FLAG_END : Animation::LOOPED_FLAG_START;
			}
			if (Animation::is_less_or_equal_approx(prev_playback_time, anim_size) && Animation::is_greater_approx(cur_playback_time, anim_size)) {
				looped_flag = node_backward ? Animation::LOOPED_FLAG_START : Animation::LOOPED_FLAG_END;
			}
		}
	} else {
		if (Animation::is_less_approx(cur_playback_time, 0)) {
			cur_playback_time = 0;
		} else if (Animation::is_greater_approx(cur_playback_time, anim_size)) {
			cur_playback_time = anim_size;
		}

		// Emit start & finish signal. Internally, the detections are the same for backward.
		// We should use call_deferred since the track keys are still being processed.
		if (process_state->tree && !p_test_only) {
			// AnimationTree uses seek to 0 "internally" to process the first key of the animation, which is used as the start detection.
			if (is_started) {
				process_state->tree->call_deferred(SNAME("emit_signal"), SceneStringName(animation_started), animation);
			}
			// Finished.
			if (Animation::is_less_approx(prev_playback_time, anim_size) && Animation::is_greater_or_equal_approx(cur_playback_time, anim_size)) {
				cur_playback_time = anim_size;
				process_state->tree->call_deferred(SNAME("emit_signal"), SceneStringName(animation_finished), animation);
			}
		}
	}

	if (!p_test_only) {
		AnimationMixer::PlaybackInfo pi = p_playback_info;
		pi.start = 0.0;
		pi.end = cur_len;
		if (play_mode == PLAY_MODE_FORWARD) {
			pi.time = cur_playback_time;
			pi.delta = cur_delta;
		} else {
			pi.time = anim_size - cur_playback_time;
			pi.delta = -cur_delta;
		}
		pi.weight = 1.0;
		pi.looped_flag = looped_flag;
		blend_animation(animation, pi);
	}

	set_parameter(backward, cur_backward);

	return nti;
}

String AnimationNodeAnimation::get_caption() const {
	return "Animation";
}

void AnimationNodeAnimation::set_play_mode(PlayMode p_play_mode) {
	play_mode = p_play_mode;
}

AnimationNodeAnimation::PlayMode AnimationNodeAnimation::get_play_mode() const {
	return play_mode;
}

void AnimationNodeAnimation::set_backward(bool p_backward) {
	set_parameter(backward, p_backward);
}

bool AnimationNodeAnimation::is_backward() const {
	return get_parameter(backward);
}

void AnimationNodeAnimation::set_advance_on_start(bool p_advance_on_start) {
	advance_on_start = p_advance_on_start;
}

bool AnimationNodeAnimation::is_advance_on_start() const {
	return advance_on_start;
}

void AnimationNodeAnimation::set_use_custom_timeline(bool p_use_custom_timeline) {
	use_custom_timeline = p_use_custom_timeline;
	notify_property_list_changed();
}

bool AnimationNodeAnimation::is_using_custom_timeline() const {
	return use_custom_timeline;
}

void AnimationNodeAnimation::set_timeline_length(double p_length) {
	timeline_length = p_length;
}

double AnimationNodeAnimation::get_timeline_length() const {
	return timeline_length;
}

void AnimationNodeAnimation::set_stretch_time_scale(bool p_strech_time_scale) {
	stretch_time_scale = p_strech_time_scale;
	notify_property_list_changed();
}

bool AnimationNodeAnimation::is_stretching_time_scale() const {
	return stretch_time_scale;
}

void AnimationNodeAnimation::set_start_offset(double p_offset) {
	start_offset = p_offset;
}

double AnimationNodeAnimation::get_start_offset() const {
	return start_offset;
}

void AnimationNodeAnimation::set_loop_mode(Animation::LoopMode p_loop_mode) {
	loop_mode = p_loop_mode;
}

Animation::LoopMode AnimationNodeAnimation::get_loop_mode() const {
	return loop_mode;
}

void AnimationNodeAnimation::_bind_methods() {
	ClassDB::bind_method(D_METHOD("set_animation", "name"), &AnimationNodeAnimation::set_animation);
	ClassDB::bind_method(D_METHOD("get_animation"), &AnimationNodeAnimation::get_animation);

	ClassDB::bind_method(D_METHOD("set_play_mode", "mode"), &AnimationNodeAnimation::set_play_mode);
	ClassDB::bind_method(D_METHOD("get_play_mode"), &AnimationNodeAnimation::get_play_mode);

	ClassDB::bind_method(D_METHOD("set_advance_on_start", "advance_on_start"), &AnimationNodeAnimation::set_advance_on_start);
	ClassDB::bind_method(D_METHOD("is_advance_on_start"), &AnimationNodeAnimation::is_advance_on_start);

	ClassDB::bind_method(D_METHOD("set_use_custom_timeline", "use_custom_timeline"), &AnimationNodeAnimation::set_use_custom_timeline);
	ClassDB::bind_method(D_METHOD("is_using_custom_timeline"), &AnimationNodeAnimation::is_using_custom_timeline);

	ClassDB::bind_method(D_METHOD("set_timeline_length", "timeline_length"), &AnimationNodeAnimation::set_timeline_length);
	ClassDB::bind_method(D_METHOD("get_timeline_length"), &AnimationNodeAnimation::get_timeline_length);

	ClassDB::bind_method(D_METHOD("set_stretch_time_scale", "stretch_time_scale"), &AnimationNodeAnimation::set_stretch_time_scale);
	ClassDB::bind_method(D_METHOD("is_stretching_time_scale"), &AnimationNodeAnimation::is_stretching_time_scale);

	ClassDB::bind_method(D_METHOD("set_start_offset", "start_offset"), &AnimationNodeAnimation::set_start_offset);
	ClassDB::bind_method(D_METHOD("get_start_offset"), &AnimationNodeAnimation::get_start_offset);

	ClassDB::bind_method(D_METHOD("set_loop_mode", "loop_mode"), &AnimationNodeAnimation::set_loop_mode);
	ClassDB::bind_method(D_METHOD("get_loop_mode"), &AnimationNodeAnimation::get_loop_mode);

	ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "animation"), "set_animation", "get_animation");
	ADD_PROPERTY(PropertyInfo(Variant::INT, "play_mode", PROPERTY_HINT_ENUM, "Forward,Backward"), "set_play_mode", "get_play_mode");
	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "advance_on_start"), "set_advance_on_start", "is_advance_on_start");
	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_custom_timeline"), "set_use_custom_timeline", "is_using_custom_timeline");
	ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "timeline_length", PROPERTY_HINT_RANGE, "0.001,60,0.001,or_greater,or_less,hide_slider,suffix:s"), "set_timeline_length", "get_timeline_length");
	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "stretch_time_scale"), "set_stretch_time_scale", "is_stretching_time_scale");
	ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "start_offset", PROPERTY_HINT_RANGE, "-60,60,0.001,or_greater,or_less,hide_slider,suffix:s"), "set_start_offset", "get_start_offset");
	ADD_PROPERTY(PropertyInfo(Variant::INT, "loop_mode", PROPERTY_HINT_ENUM, "None,Linear,Ping-Pong"), "set_loop_mode", "get_loop_mode");

	BIND_ENUM_CONSTANT(PLAY_MODE_FORWARD);
	BIND_ENUM_CONSTANT(PLAY_MODE_BACKWARD);
}

AnimationNodeAnimation::AnimationNodeAnimation() {
}

////////////////////////////////////////////////////////

void AnimationNodeSync::_bind_methods() {
	ClassDB::bind_method(D_METHOD("set_use_sync", "enable"), &AnimationNodeSync::set_use_sync);
	ClassDB::bind_method(D_METHOD("is_using_sync"), &AnimationNodeSync::is_using_sync);

	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "sync"), "set_use_sync", "is_using_sync");
}

void AnimationNodeSync::set_use_sync(bool p_sync) {
	sync = p_sync;
}

bool AnimationNodeSync::is_using_sync() const {
	return sync;
}

AnimationNodeSync::AnimationNodeSync() {
}

////////////////////////////////////////////////////////
void AnimationNodeOneShot::get_parameter_list(List<PropertyInfo> *r_list) const {
	AnimationNode::get_parameter_list(r_list);
	r_list->push_back(PropertyInfo(Variant::BOOL, active, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_READ_ONLY));
	r_list->push_back(PropertyInfo(Variant::BOOL, internal_active, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_READ_ONLY));
	r_list->push_back(PropertyInfo(Variant::INT, request, PROPERTY_HINT_ENUM, ",Fire,Abort,Fade Out"));
	r_list->push_back(PropertyInfo(Variant::FLOAT, fade_in_remaining, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE));
	r_list->push_back(PropertyInfo(Variant::FLOAT, fade_out_remaining, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE));
	r_list->push_back(PropertyInfo(Variant::FLOAT, time_to_restart, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE));
}

Variant AnimationNodeOneShot::get_parameter_default_value(const StringName &p_parameter) const {
	Variant ret = AnimationNode::get_parameter_default_value(p_parameter);
	if (ret != Variant()) {
		return ret;
	}

	if (p_parameter == request) {
		return ONE_SHOT_REQUEST_NONE;
	} else if (p_parameter == active || p_parameter == internal_active) {
		return false;
	} else if (p_parameter == time_to_restart) {
		return -1;
	} else {
		return 0.0;
	}
}

bool AnimationNodeOneShot::is_parameter_read_only(const StringName &p_parameter) const {
	if (AnimationNode::is_parameter_read_only(p_parameter)) {
		return true;
	}

	if (p_parameter == active || p_parameter == internal_active) {
		return true;
	}
	return false;
}

void AnimationNodeOneShot::set_fade_in_time(double p_time) {
	fade_in = p_time;
}

double AnimationNodeOneShot::get_fade_in_time() const {
	return fade_in;
}

void AnimationNodeOneShot::set_fade_out_time(double p_time) {
	fade_out = p_time;
}

double AnimationNodeOneShot::get_fade_out_time() const {
	return fade_out;
}

void AnimationNodeOneShot::set_fade_in_curve(const Ref<Curve> &p_curve) {
	fade_in_curve = p_curve;
}

Ref<Curve> AnimationNodeOneShot::get_fade_in_curve() const {
	return fade_in_curve;
}

void AnimationNodeOneShot::set_fade_out_curve(const Ref<Curve> &p_curve) {
	fade_out_curve = p_curve;
}

Ref<Curve> AnimationNodeOneShot::get_fade_out_curve() const {
	return fade_out_curve;
}

void AnimationNodeOneShot::set_auto_restart_enabled(bool p_enabled) {
	auto_restart = p_enabled;
}

void AnimationNodeOneShot::set_auto_restart_delay(double p_time) {
	auto_restart_delay = p_time;
}

void AnimationNodeOneShot::set_auto_restart_random_delay(double p_time) {
	auto_restart_random_delay = p_time;
}

bool AnimationNodeOneShot::is_auto_restart_enabled() const {
	return auto_restart;
}

double AnimationNodeOneShot::get_auto_restart_delay() const {
	return auto_restart_delay;
}

double AnimationNodeOneShot::get_auto_restart_random_delay() const {
	return auto_restart_random_delay;
}

void AnimationNodeOneShot::set_mix_mode(MixMode p_mix) {
	mix = p_mix;
}

AnimationNodeOneShot::MixMode AnimationNodeOneShot::get_mix_mode() const {
	return mix;
}

void AnimationNodeOneShot::set_break_loop_at_end(bool p_enable) {
	break_loop_at_end = p_enable;
}

bool AnimationNodeOneShot::is_loop_broken_at_end() const {
	return break_loop_at_end;
}

String AnimationNodeOneShot::get_caption() const {
	return "OneShot";
}

bool AnimationNodeOneShot::has_filter() const {
	return true;
}

AnimationNode::NodeTimeInfo AnimationNodeOneShot::_process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only) {
	OneShotRequest cur_request = static_cast<OneShotRequest>((int)get_parameter(request));
	bool cur_active = get_parameter(active);
	bool cur_internal_active = get_parameter(internal_active);
	NodeTimeInfo cur_nti = get_node_time_info();
	double cur_time_to_restart = get_parameter(time_to_restart);
	double cur_fade_in_remaining = get_parameter(fade_in_remaining);
	double cur_fade_out_remaining = get_parameter(fade_out_remaining);

	set_parameter(request, ONE_SHOT_REQUEST_NONE);

	bool is_shooting = true;
	bool clear_remaining_fade = false;
	bool is_fading_out = cur_active == true && cur_internal_active == false;

	double p_time = p_playback_info.time;
	double p_delta = p_playback_info.delta;
	double abs_delta = Math::abs(p_delta);
	bool p_seek = p_playback_info.seeked;
	bool p_is_external_seeking = p_playback_info.is_external_seeking;

	if (Math::is_zero_approx(p_time) && p_seek && !p_is_external_seeking) {
		clear_remaining_fade = true; // Reset occurs.
	}

	bool do_start = cur_request == ONE_SHOT_REQUEST_FIRE;
	if (cur_request == ONE_SHOT_REQUEST_ABORT) {
		set_parameter(internal_active, false);
		set_parameter(active, false);
		set_parameter(time_to_restart, -1);
		is_shooting = false;
	} else if (cur_request == ONE_SHOT_REQUEST_FADE_OUT && !is_fading_out) { // If fading, keep current fade.
		if (cur_active) {
			// Request fading.
			is_fading_out = true;
			cur_fade_out_remaining = fade_out;
			cur_fade_in_remaining = 0;
		} else {
			// Shot is ended, do nothing.
			is_shooting = false;
		}
		set_parameter(internal_active, false);
		set_parameter(time_to_restart, -1);
	} else if (!do_start && !cur_active) {
		if (Animation::is_greater_or_equal_approx(cur_time_to_restart, 0) && !p_seek) {
			cur_time_to_restart -= abs_delta;
			if (Animation::is_less_approx(cur_time_to_restart, 0)) {
				do_start = true; // Restart.
			}
			set_parameter(time_to_restart, cur_time_to_restart);
		}
		if (!do_start) {
			is_shooting = false;
		}
	}

	bool os_seek = p_seek;

	if (clear_remaining_fade) {
		os_seek = false;
		cur_fade_out_remaining = 0;
		set_parameter(fade_out_remaining, 0);
		if (is_fading_out) {
			is_fading_out = false;
			set_parameter(internal_active, false);
			set_parameter(active, false);
		}
	}

	if (!is_shooting) {
		AnimationMixer::PlaybackInfo pi = p_playback_info;
		pi.weight = 1.0;
		return blend_input(0, pi, FILTER_IGNORE, sync, p_test_only);
	}

	if (do_start) {
		os_seek = true;
		if (!cur_internal_active) {
			cur_fade_in_remaining = fade_in; // If already active, don't fade-in again.
		}
		cur_internal_active = true;
		set_parameter(request, ONE_SHOT_REQUEST_NONE);
		set_parameter(internal_active, true);
		set_parameter(active, true);
	}

	real_t blend = 1.0;
	bool use_blend = sync;

	if (Animation::is_greater_approx(cur_fade_in_remaining, 0)) {
		if (Animation::is_greater_approx(fade_in, 0)) {
			use_blend = true;
			blend = (fade_in - cur_fade_in_remaining) / fade_in;
			if (fade_in_curve.is_valid()) {
				blend = fade_in_curve->sample(blend);
			}
		} else {
			blend = 0; // Should not happen.
		}
	}

	if (is_fading_out) {
		use_blend = true;
		if (Animation::is_greater_approx(fade_out, 0)) {
			blend = cur_fade_out_remaining / fade_out;
			if (fade_out_curve.is_valid()) {
				blend = 1.0 - fade_out_curve->sample(1.0 - blend);
			}
		} else {
			blend = 0;
		}
	}

	AnimationMixer::PlaybackInfo pi = p_playback_info;
	NodeTimeInfo main_nti;
	if (mix == MIX_MODE_ADD) {
		pi.weight = 1.0;
		main_nti = blend_input(0, pi, FILTER_IGNORE, sync, p_test_only);
	} else {
		pi.seeked &= use_blend;
		pi.weight = 1.0 - blend;
		main_nti = blend_input(0, pi, FILTER_BLEND, sync, p_test_only); // Unlike below, processing this edge is a corner case.
	}

	pi = p_playback_info;
	if (do_start) {
		pi.time = 0;
	} else if (os_seek) {
		pi.time = cur_nti.position;
	}
	pi.seeked = os_seek;
	pi.weight = Math::is_zero_approx(blend) ? CMP_EPSILON : blend;

	NodeTimeInfo os_nti = blend_input(1, pi, FILTER_PASS, true, p_test_only); // Blend values must be more than CMP_EPSILON to process discrete keys in edge.

	if (Animation::is_less_or_equal_approx(cur_fade_in_remaining, 0) && !do_start && !is_fading_out && Animation::is_less_or_equal_approx(os_nti.get_remain(break_loop_at_end), fade_out)) {
		is_fading_out = true;
		cur_fade_out_remaining = os_nti.get_remain(break_loop_at_end);
		cur_fade_in_remaining = 0;
		set_parameter(internal_active, false);
	}

	if (!p_seek) {
		if (Animation::is_less_or_equal_approx(os_nti.get_remain(break_loop_at_end), 0) || (is_fading_out && Animation::is_less_or_equal_approx(cur_fade_out_remaining, 0))) {
			set_parameter(internal_active, false);
			set_parameter(active, false);
			if (auto_restart) {
				double restart_sec = auto_restart_delay + Math::randd() * auto_restart_random_delay;
				set_parameter(time_to_restart, restart_sec);
			}
		}
		double d = Math::abs(os_nti.delta);
		if (!do_start) {
			cur_fade_in_remaining = MAX(0, cur_fade_in_remaining - d); // Don't consider seeked delta by restart.
		}
		cur_fade_out_remaining = MAX(0, cur_fade_out_remaining - d);
	}

	set_parameter(fade_in_remaining, cur_fade_in_remaining);
	set_parameter(fade_out_remaining, cur_fade_out_remaining);

	return cur_internal_active ? os_nti : main_nti;
}

void AnimationNodeOneShot::_bind_methods() {
	ClassDB::bind_method(D_METHOD("set_fadein_time", "time"), &AnimationNodeOneShot::set_fade_in_time);
	ClassDB::bind_method(D_METHOD("get_fadein_time"), &AnimationNodeOneShot::get_fade_in_time);

	ClassDB::bind_method(D_METHOD("set_fadein_curve", "curve"), &AnimationNodeOneShot::set_fade_in_curve);
	ClassDB::bind_method(D_METHOD("get_fadein_curve"), &AnimationNodeOneShot::get_fade_in_curve);

	ClassDB::bind_method(D_METHOD("set_fadeout_time", "time"), &AnimationNodeOneShot::set_fade_out_time);
	ClassDB::bind_method(D_METHOD("get_fadeout_time"), &AnimationNodeOneShot::get_fade_out_time);

	ClassDB::bind_method(D_METHOD("set_fadeout_curve", "curve"), &AnimationNodeOneShot::set_fade_out_curve);
	ClassDB::bind_method(D_METHOD("get_fadeout_curve"), &AnimationNodeOneShot::get_fade_out_curve);

	ClassDB::bind_method(D_METHOD("set_break_loop_at_end", "enable"), &AnimationNodeOneShot::set_break_loop_at_end);
	ClassDB::bind_method(D_METHOD("is_loop_broken_at_end"), &AnimationNodeOneShot::is_loop_broken_at_end);

	ClassDB::bind_method(D_METHOD("set_autorestart", "active"), &AnimationNodeOneShot::set_auto_restart_enabled);
	ClassDB::bind_method(D_METHOD("has_autorestart"), &AnimationNodeOneShot::is_auto_restart_enabled);

	ClassDB::bind_method(D_METHOD("set_autorestart_delay", "time"), &AnimationNodeOneShot::set_auto_restart_delay);
	ClassDB::bind_method(D_METHOD("get_autorestart_delay"), &AnimationNodeOneShot::get_auto_restart_delay);

	ClassDB::bind_method(D_METHOD("set_autorestart_random_delay", "time"), &AnimationNodeOneShot::set_auto_restart_random_delay);
	ClassDB::bind_method(D_METHOD("get_autorestart_random_delay"), &AnimationNodeOneShot::get_auto_restart_random_delay);

	ClassDB::bind_method(D_METHOD("set_mix_mode", "mode"), &AnimationNodeOneShot::set_mix_mode);
	ClassDB::bind_method(D_METHOD("get_mix_mode"), &AnimationNodeOneShot::get_mix_mode);

	ADD_PROPERTY(PropertyInfo(Variant::INT, "mix_mode", PROPERTY_HINT_ENUM, "Blend,Add"), "set_mix_mode", "get_mix_mode");

	ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "fadein_time", PROPERTY_HINT_RANGE, "0,60,0.01,or_greater,suffix:s"), "set_fadein_time", "get_fadein_time");
	ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "fadein_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_fadein_curve", "get_fadein_curve");
	ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "fadeout_time", PROPERTY_HINT_RANGE, "0,60,0.01,or_greater,suffix:s"), "set_fadeout_time", "get_fadeout_time");
	ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "fadeout_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_fadeout_curve", "get_fadeout_curve");
	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "break_loop_at_end"), "set_break_loop_at_end", "is_loop_broken_at_end");

	ADD_GROUP("Auto Restart", "autorestart_");
	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "autorestart"), "set_autorestart", "has_autorestart");

	ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "autorestart_delay", PROPERTY_HINT_RANGE, "0,60,0.01,or_greater,suffix:s"), "set_autorestart_delay", "get_autorestart_delay");
	ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "autorestart_random_delay", PROPERTY_HINT_RANGE, "0,60,0.01,or_greater,suffix:s"), "set_autorestart_random_delay", "get_autorestart_random_delay");

	BIND_ENUM_CONSTANT(ONE_SHOT_REQUEST_NONE);
	BIND_ENUM_CONSTANT(ONE_SHOT_REQUEST_FIRE);
	BIND_ENUM_CONSTANT(ONE_SHOT_REQUEST_ABORT);
	BIND_ENUM_CONSTANT(ONE_SHOT_REQUEST_FADE_OUT);

	BIND_ENUM_CONSTANT(MIX_MODE_BLEND);
	BIND_ENUM_CONSTANT(MIX_MODE_ADD);
}

AnimationNodeOneShot::AnimationNodeOneShot() {
	add_input("in");
	add_input("shot");
}

////////////////////////////////////////////////

void AnimationNodeAdd2::get_parameter_list(List<PropertyInfo> *r_list) const {
	AnimationNode::get_parameter_list(r_list);
	r_list->push_back(PropertyInfo(Variant::FLOAT, add_amount, PROPERTY_HINT_RANGE, "0,1,0.01,or_less,or_greater"));
}

Variant AnimationNodeAdd2::get_parameter_default_value(const StringName &p_parameter) const {
	Variant ret = AnimationNode::get_parameter_default_value(p_parameter);
	if (ret != Variant()) {
		return ret;
	}

	return 0;
}

String AnimationNodeAdd2::get_caption() const {
	return "Add2";
}

bool AnimationNodeAdd2::has_filter() const {
	return true;
}

AnimationNode::NodeTimeInfo AnimationNodeAdd2::_process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only) {
	double amount = get_parameter(add_amount);

	AnimationMixer::PlaybackInfo pi = p_playback_info;
	pi.weight = 1.0;
	NodeTimeInfo nti = blend_input(0, pi, FILTER_IGNORE, sync, p_test_only);
	pi.weight = amount;
	blend_input(1, pi, FILTER_PASS, sync, p_test_only);

	return nti;
}

AnimationNodeAdd2::AnimationNodeAdd2() {
	add_input("in");
	add_input("add");
}

////////////////////////////////////////////////

void AnimationNodeAdd3::get_parameter_list(List<PropertyInfo> *r_list) const {
	AnimationNode::get_parameter_list(r_list);
	r_list->push_back(PropertyInfo(Variant::FLOAT, add_amount, PROPERTY_HINT_RANGE, "-1,1,0.01,or_less,or_greater"));
}

Variant AnimationNodeAdd3::get_parameter_default_value(const StringName &p_parameter) const {
	Variant ret = AnimationNode::get_parameter_default_value(p_parameter);
	if (ret != Variant()) {
		return ret;
	}

	return 0;
}

String AnimationNodeAdd3::get_caption() const {
	return "Add3";
}

bool AnimationNodeAdd3::has_filter() const {
	return true;
}

AnimationNode::NodeTimeInfo AnimationNodeAdd3::_process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only) {
	double amount = get_parameter(add_amount);

	AnimationMixer::PlaybackInfo pi = p_playback_info;
	pi.weight = MAX(0, -amount);
	blend_input(0, pi, FILTER_PASS, sync, p_test_only);
	pi.weight = 1.0;
	NodeTimeInfo nti = blend_input(1, pi, FILTER_IGNORE, sync, p_test_only);
	pi.weight = MAX(0, amount);
	blend_input(2, pi, FILTER_PASS, sync, p_test_only);

	return nti;
}

AnimationNodeAdd3::AnimationNodeAdd3() {
	add_input("-add");
	add_input("in");
	add_input("+add");
}

/////////////////////////////////////////////

void AnimationNodeBlend2::get_parameter_list(List<PropertyInfo> *r_list) const {
	AnimationNode::get_parameter_list(r_list);
	r_list->push_back(PropertyInfo(Variant::FLOAT, blend_amount, PROPERTY_HINT_RANGE, "0,1,0.01,or_less,or_greater"));
}

Variant AnimationNodeBlend2::get_parameter_default_value(const StringName &p_parameter) const {
	Variant ret = AnimationNode::get_parameter_default_value(p_parameter);
	if (ret != Variant()) {
		return ret;
	}

	return 0; // For blend amount.
}

String AnimationNodeBlend2::get_caption() const {
	return "Blend2";
}

AnimationNode::NodeTimeInfo AnimationNodeBlend2::_process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only) {
	double amount = get_parameter(blend_amount);

	AnimationMixer::PlaybackInfo pi = p_playback_info;
	pi.weight = 1.0 - amount;
	NodeTimeInfo nti0 = blend_input(0, pi, FILTER_BLEND, sync, p_test_only);
	pi.weight = amount;
	NodeTimeInfo nti1 = blend_input(1, pi, FILTER_PASS, sync, p_test_only);

	return amount > 0.5 ? nti1 : nti0; // Hacky but good enough.
}

bool AnimationNodeBlend2::has_filter() const {
	return true;
}

AnimationNodeBlend2::AnimationNodeBlend2() {
	add_input("in");
	add_input("blend");
}

//////////////////////////////////////

void AnimationNodeBlend3::get_parameter_list(List<PropertyInfo> *r_list) const {
	AnimationNode::get_parameter_list(r_list);
	r_list->push_back(PropertyInfo(Variant::FLOAT, blend_amount, PROPERTY_HINT_RANGE, "-1,1,0.01,or_less,or_greater"));
}

Variant AnimationNodeBlend3::get_parameter_default_value(const StringName &p_parameter) const {
	Variant ret = AnimationNode::get_parameter_default_value(p_parameter);
	if (ret != Variant()) {
		return ret;
	}

	return 0; // For blend amount.
}

String AnimationNodeBlend3::get_caption() const {
	return "Blend3";
}

AnimationNode::NodeTimeInfo AnimationNodeBlend3::_process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only) {
	double amount = get_parameter(blend_amount);

	AnimationMixer::PlaybackInfo pi = p_playback_info;
	pi.weight = MAX(0, -amount);
	NodeTimeInfo nti0 = blend_input(0, pi, FILTER_IGNORE, sync, p_test_only);
	pi.weight = 1.0 - ABS(amount);
	NodeTimeInfo nti1 = blend_input(1, pi, FILTER_IGNORE, sync, p_test_only);
	pi.weight = MAX(0, amount);
	NodeTimeInfo nti2 = blend_input(2, pi, FILTER_IGNORE, sync, p_test_only);

	return amount > 0.5 ? nti2 : (amount < -0.5 ? nti0 : nti1); // Hacky but good enough.
}

AnimationNodeBlend3::AnimationNodeBlend3() {
	add_input("-blend");
	add_input("in");
	add_input("+blend");
}

////////////////////////////////////////////////

void AnimationNodeSub2::get_parameter_list(List<PropertyInfo> *r_list) const {
	AnimationNode::get_parameter_list(r_list);
	r_list->push_back(PropertyInfo(Variant::FLOAT, sub_amount, PROPERTY_HINT_RANGE, "0,1,0.01,or_less,or_greater"));
}

Variant AnimationNodeSub2::get_parameter_default_value(const StringName &p_parameter) const {
	Variant ret = AnimationNode::get_parameter_default_value(p_parameter);
	if (ret != Variant()) {
		return ret;
	}

	return 0;
}

String AnimationNodeSub2::get_caption() const {
	return "Sub2";
}

bool AnimationNodeSub2::has_filter() const {
	return true;
}

AnimationNode::NodeTimeInfo AnimationNodeSub2::_process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only) {
	double amount = get_parameter(sub_amount);

	AnimationMixer::PlaybackInfo pi = p_playback_info;
	// Out = Sub.Transform3D^(-1) * In.Transform3D
	pi.weight = -amount;
	blend_input(1, pi, FILTER_PASS, sync, p_test_only);
	pi.weight = 1.0;

	return blend_input(0, pi, FILTER_IGNORE, sync, p_test_only);
}

AnimationNodeSub2::AnimationNodeSub2() {
	add_input("in");
	add_input("sub");
}

/////////////////////////////////

void AnimationNodeTimeScale::get_parameter_list(List<PropertyInfo> *r_list) const {
	AnimationNode::get_parameter_list(r_list);
	r_list->push_back(PropertyInfo(Variant::FLOAT, scale, PROPERTY_HINT_RANGE, "-32,32,0.01,or_less,or_greater"));
}

Variant AnimationNodeTimeScale::get_parameter_default_value(const StringName &p_parameter) const {
	Variant ret = AnimationNode::get_parameter_default_value(p_parameter);
	if (ret != Variant()) {
		return ret;
	}

	return 1.0; // Initial timescale.
}

String AnimationNodeTimeScale::get_caption() const {
	return "TimeScale";
}

AnimationNode::NodeTimeInfo AnimationNodeTimeScale::_process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only) {
	double cur_scale = get_parameter(scale);

	AnimationMixer::PlaybackInfo pi = p_playback_info;
	pi.weight = 1.0;
	if (!pi.seeked) {
		pi.delta *= cur_scale;
	}

	return blend_input(0, pi, FILTER_IGNORE, true, p_test_only);
}

AnimationNodeTimeScale::AnimationNodeTimeScale() {
	add_input("in");
}

////////////////////////////////////

void AnimationNodeTimeSeek::get_parameter_list(List<PropertyInfo> *r_list) const {
	AnimationNode::get_parameter_list(r_list);
	r_list->push_back(PropertyInfo(Variant::FLOAT, seek_pos_request, PROPERTY_HINT_RANGE, "-1,3600,0.01,or_greater")); // It will be reset to -1 after seeking the position immediately.
}

Variant AnimationNodeTimeSeek::get_parameter_default_value(const StringName &p_parameter) const {
	Variant ret = AnimationNode::get_parameter_default_value(p_parameter);
	if (ret != Variant()) {
		return ret;
	}

	return -1.0; // Initial seek request.
}

String AnimationNodeTimeSeek::get_caption() const {
	return "TimeSeek";
}

void AnimationNodeTimeSeek::set_explicit_elapse(bool p_enable) {
	explicit_elapse = p_enable;
}

bool AnimationNodeTimeSeek::is_explicit_elapse() const {
	return explicit_elapse;
}

AnimationNode::NodeTimeInfo AnimationNodeTimeSeek::_process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only) {
	double cur_seek_pos = get_parameter(seek_pos_request);

	AnimationMixer::PlaybackInfo pi = p_playback_info;
	pi.weight = 1.0;
	if (Animation::is_greater_or_equal_approx(cur_seek_pos, 0)) {
		pi.time = cur_seek_pos;
		pi.seeked = true;
		pi.is_external_seeking = explicit_elapse;
		set_parameter(seek_pos_request, -1.0); // Reset.
	}

	return blend_input(0, pi, FILTER_IGNORE, true, p_test_only);
}

AnimationNodeTimeSeek::AnimationNodeTimeSeek() {
	add_input("in");
}

void AnimationNodeTimeSeek::_bind_methods() {
	ClassDB::bind_method(D_METHOD("set_explicit_elapse", "enable"), &AnimationNodeTimeSeek::set_explicit_elapse);
	ClassDB::bind_method(D_METHOD("is_explicit_elapse"), &AnimationNodeTimeSeek::is_explicit_elapse);
	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "explicit_elapse"), "set_explicit_elapse", "is_explicit_elapse");
}

/////////////////////////////////////////////////

bool AnimationNodeTransition::_set(const StringName &p_path, const Variant &p_value) {
	String path = p_path;

	if (!path.begins_with("input_")) {
		return false;
	}

	int which = path.get_slicec('/', 0).get_slicec('_', 1).to_int();
	String what = path.get_slicec('/', 1);

	if (which == get_input_count() && what == "name") {
		if (add_input(p_value)) {
			return true;
		}
		return false;
	}

	ERR_FAIL_INDEX_V(which, get_input_count(), false);

	if (what == "name") {
		set_input_name(which, p_value);
	} else if (what == "auto_advance") {
		set_input_as_auto_advance(which, p_value);
	} else if (what == "break_loop_at_end") {
		set_input_break_loop_at_end(which, p_value);
	} else if (what == "reset") {
		set_input_reset(which, p_value);
	} else {
		return false;
	}

	return true;
}

bool AnimationNodeTransition::_get(const StringName &p_path, Variant &r_ret) const {
	String path = p_path;

	if (!path.begins_with("input_")) {
		return false;
	}

	int which = path.get_slicec('/', 0).get_slicec('_', 1).to_int();
	String what = path.get_slicec('/', 1);

	ERR_FAIL_INDEX_V(which, get_input_count(), false);

	if (what == "name") {
		r_ret = get_input_name(which);
	} else if (what == "auto_advance") {
		r_ret = is_input_set_as_auto_advance(which);
	} else if (what == "break_loop_at_end") {
		r_ret = is_input_loop_broken_at_end(which);
	} else if (what == "reset") {
		r_ret = is_input_reset(which);
	} else {
		return false;
	}

	return true;
}

void AnimationNodeTransition::get_parameter_list(List<PropertyInfo> *r_list) const {
	AnimationNode::get_parameter_list(r_list);
	String anims;
	for (int i = 0; i < get_input_count(); i++) {
		if (i > 0) {
			anims += ",";
		}
		anims += inputs[i].name;
	}

	r_list->push_back(PropertyInfo(Variant::STRING, current_state, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_READ_ONLY)); // For interface.
	r_list->push_back(PropertyInfo(Variant::STRING, transition_request, PROPERTY_HINT_ENUM, anims)); // For transition request. It will be cleared after setting the value immediately.
	r_list->push_back(PropertyInfo(Variant::INT, current_index, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_READ_ONLY)); // To avoid finding the index every frame, use this internally.
	r_list->push_back(PropertyInfo(Variant::INT, prev_index, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE));
	r_list->push_back(PropertyInfo(Variant::FLOAT, prev_xfading, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE));
}

Variant AnimationNodeTransition::get_parameter_default_value(const StringName &p_parameter) const {
	Variant ret = AnimationNode::get_parameter_default_value(p_parameter);
	if (ret != Variant()) {
		return ret;
	}

	if (p_parameter == prev_xfading) {
		return 0.0;
	} else if (p_parameter == prev_index || p_parameter == current_index) {
		return -1;
	} else {
		return String();
	}
}

bool AnimationNodeTransition::is_parameter_read_only(const StringName &p_parameter) const {
	if (AnimationNode::is_parameter_read_only(p_parameter)) {
		return true;
	}

	if (p_parameter == current_state || p_parameter == current_index) {
		return true;
	}
	return false;
}

String AnimationNodeTransition::get_caption() const {
	return "Transition";
}

void AnimationNodeTransition::set_input_count(int p_inputs) {
	for (int i = get_input_count(); i < p_inputs; i++) {
		add_input("state_" + itos(i));
	}
	while (get_input_count() > p_inputs) {
		remove_input(get_input_count() - 1);
	}

	pending_update = true;

	emit_signal(SNAME("tree_changed")); // For updating connect activity map.
	notify_property_list_changed();
}

bool AnimationNodeTransition::add_input(const String &p_name) {
	if (AnimationNode::add_input(p_name)) {
		input_data.push_back(InputData());
		return true;
	}
	return false;
}

void AnimationNodeTransition::remove_input(int p_index) {
	input_data.remove_at(p_index);
	AnimationNode::remove_input(p_index);
}

bool AnimationNodeTransition::set_input_name(int p_input, const String &p_name) {
	pending_update = true;
	if (!AnimationNode::set_input_name(p_input, p_name)) {
		return false;
	}
	emit_signal(SNAME("tree_changed")); // For updating enum options.
	return true;
}

void AnimationNodeTransition::set_input_as_auto_advance(int p_input, bool p_enable) {
	ERR_FAIL_INDEX(p_input, get_input_count());
	input_data.write[p_input].auto_advance = p_enable;
}

bool AnimationNodeTransition::is_input_set_as_auto_advance(int p_input) const {
	ERR_FAIL_INDEX_V(p_input, get_input_count(), false);
	return input_data[p_input].auto_advance;
}

void AnimationNodeTransition::set_input_break_loop_at_end(int p_input, bool p_enable) {
	ERR_FAIL_INDEX(p_input, get_input_count());
	input_data.write[p_input].break_loop_at_end = p_enable;
}

bool AnimationNodeTransition::is_input_loop_broken_at_end(int p_input) const {
	ERR_FAIL_INDEX_V(p_input, get_input_count(), false);
	return input_data[p_input].break_loop_at_end;
}

void AnimationNodeTransition::set_input_reset(int p_input, bool p_enable) {
	ERR_FAIL_INDEX(p_input, get_input_count());
	input_data.write[p_input].reset = p_enable;
}

bool AnimationNodeTransition::is_input_reset(int p_input) const {
	ERR_FAIL_INDEX_V(p_input, get_input_count(), true);
	return input_data[p_input].reset;
}

void AnimationNodeTransition::set_xfade_time(double p_fade) {
	xfade_time = p_fade;
}

double AnimationNodeTransition::get_xfade_time() const {
	return xfade_time;
}

void AnimationNodeTransition::set_xfade_curve(const Ref<Curve> &p_curve) {
	xfade_curve = p_curve;
}

Ref<Curve> AnimationNodeTransition::get_xfade_curve() const {
	return xfade_curve;
}

void AnimationNodeTransition::set_allow_transition_to_self(bool p_enable) {
	allow_transition_to_self = p_enable;
}

bool AnimationNodeTransition::is_allow_transition_to_self() const {
	return allow_transition_to_self;
}

AnimationNode::NodeTimeInfo AnimationNodeTransition::_process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only) {
	String cur_transition_request = get_parameter(transition_request);
	int cur_current_index = get_parameter(current_index);
	int cur_prev_index = get_parameter(prev_index);

	NodeTimeInfo cur_nti = get_node_time_info();<--- cur_nti is initialized
	double cur_prev_xfading = get_parameter(prev_xfading);

	bool switched = false;
	bool restart = false;
	bool clear_remaining_fade = false;

	if (pending_update) {
		if (cur_current_index < 0 || cur_current_index >= get_input_count()) {
			set_parameter(prev_index, -1);
			if (get_input_count() > 0) {
				set_parameter(current_index, 0);
				set_parameter(current_state, get_input_name(0));
			} else {
				set_parameter(current_index, -1);
				set_parameter(current_state, StringName());
			}
		} else {
			set_parameter(current_state, get_input_name(cur_current_index));
		}
		pending_update = false;
	}

	double p_time = p_playback_info.time;
	bool p_seek = p_playback_info.seeked;
	bool p_is_external_seeking = p_playback_info.is_external_seeking;

	if (Math::is_zero_approx(p_time) && p_seek && !p_is_external_seeking) {
		clear_remaining_fade = true; // Reset occurs.
	}

	if (!cur_transition_request.is_empty()) {
		int new_idx = find_input(cur_transition_request);
		if (new_idx >= 0) {
			if (cur_current_index == new_idx) {
				if (allow_transition_to_self) {
					// Transition to same state.
					restart = input_data[cur_current_index].reset;
					clear_remaining_fade = true;
				}
			} else {
				switched = true;
				cur_prev_index = cur_current_index;
				set_parameter(prev_index, cur_current_index);
				cur_current_index = new_idx;
				set_parameter(current_index, cur_current_index);
				set_parameter(current_state, cur_transition_request);
			}
		} else {
			ERR_PRINT("No such input: '" + cur_transition_request + "'");
		}
		cur_transition_request = String();
		set_parameter(transition_request, cur_transition_request);
	}

	if (clear_remaining_fade) {
		cur_prev_xfading = 0;
		set_parameter(prev_xfading, 0);
		cur_prev_index = -1;
		set_parameter(prev_index, -1);
	}

	AnimationMixer::PlaybackInfo pi = p_playback_info;

	// Special case for restart.
	if (restart) {
		pi.time = 0;
		pi.seeked = true;
		pi.weight = 1.0;
		return blend_input(cur_current_index, pi, FILTER_IGNORE, true, p_test_only);
	}

	if (switched) {
		cur_prev_xfading = xfade_time;
	}

	if (cur_current_index < 0 || cur_current_index >= get_input_count() || cur_prev_index >= get_input_count()) {
		return NodeTimeInfo();
	}

	if (sync) {
		pi.weight = 0;
		for (int i = 0; i < get_input_count(); i++) {
			if (i != cur_current_index && i != cur_prev_index) {
				blend_input(i, pi, FILTER_IGNORE, true, p_test_only);
			}
		}
	}

	if (cur_prev_index < 0) { // Process current animation, check for transition.
		pi.weight = 1.0;
		cur_nti = blend_input(cur_current_index, pi, FILTER_IGNORE, true, p_test_only);<--- cur_nti is overwritten
		if (input_data[cur_current_index].auto_advance && Animation::is_less_or_equal_approx(cur_nti.get_remain(input_data[cur_current_index].break_loop_at_end), xfade_time)) {
			set_parameter(transition_request, get_input_name((cur_current_index + 1) % get_input_count()));
		}
	} else { // Cross-fading from prev to current.

		real_t blend = 0.0;
		real_t blend_inv = 1.0;
		bool use_blend = sync;
		if (xfade_time > 0) {
			use_blend = true;
			blend = cur_prev_xfading / xfade_time;
			if (xfade_curve.is_valid()) {
				blend = xfade_curve->sample(blend);
			}
			blend_inv = 1.0 - blend;
			blend = Math::is_zero_approx(blend) ? CMP_EPSILON : blend;
			blend_inv = Math::is_zero_approx(blend_inv) ? CMP_EPSILON : blend_inv;
		}

		// Blend values must be more than CMP_EPSILON to process discrete keys in edge.
		pi.weight = blend_inv;
		if (input_data[cur_current_index].reset && !p_seek && switched) { // Just switched, seek to start of current.
			pi.time = 0;
			pi.seeked = true;
		}
		cur_nti = blend_input(cur_current_index, pi, FILTER_IGNORE, true, p_test_only);

		pi = p_playback_info;
		pi.seeked &= use_blend;
		pi.weight = blend;
		blend_input(cur_prev_index, pi, FILTER_IGNORE, true, p_test_only);
		if (!p_seek) {
			if (Animation::is_less_or_equal_approx(cur_prev_xfading, 0)) {
				set_parameter(prev_index, -1);
			}
			cur_prev_xfading -= Math::abs(p_playback_info.delta);
		}
	}

	set_parameter(prev_xfading, cur_prev_xfading);

	return cur_nti;
}

void AnimationNodeTransition::_get_property_list(List<PropertyInfo> *p_list) const {
	for (int i = 0; i < get_input_count(); i++) {
		p_list->push_back(PropertyInfo(Variant::STRING, "input_" + itos(i) + "/name", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_INTERNAL));
		p_list->push_back(PropertyInfo(Variant::BOOL, "input_" + itos(i) + "/auto_advance", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_INTERNAL));
		p_list->push_back(PropertyInfo(Variant::BOOL, "input_" + itos(i) + "/break_loop_at_end", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_INTERNAL));
		p_list->push_back(PropertyInfo(Variant::BOOL, "input_" + itos(i) + "/reset", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_INTERNAL));
	}
}

void AnimationNodeTransition::_bind_methods() {
	ClassDB::bind_method(D_METHOD("set_input_count", "input_count"), &AnimationNodeTransition::set_input_count);

	ClassDB::bind_method(D_METHOD("set_input_as_auto_advance", "input", "enable"), &AnimationNodeTransition::set_input_as_auto_advance);
	ClassDB::bind_method(D_METHOD("is_input_set_as_auto_advance", "input"), &AnimationNodeTransition::is_input_set_as_auto_advance);

	ClassDB::bind_method(D_METHOD("set_input_break_loop_at_end", "input", "enable"), &AnimationNodeTransition::set_input_break_loop_at_end);
	ClassDB::bind_method(D_METHOD("is_input_loop_broken_at_end", "input"), &AnimationNodeTransition::is_input_loop_broken_at_end);

	ClassDB::bind_method(D_METHOD("set_input_reset", "input", "enable"), &AnimationNodeTransition::set_input_reset);
	ClassDB::bind_method(D_METHOD("is_input_reset", "input"), &AnimationNodeTransition::is_input_reset);

	ClassDB::bind_method(D_METHOD("set_xfade_time", "time"), &AnimationNodeTransition::set_xfade_time);
	ClassDB::bind_method(D_METHOD("get_xfade_time"), &AnimationNodeTransition::get_xfade_time);

	ClassDB::bind_method(D_METHOD("set_xfade_curve", "curve"), &AnimationNodeTransition::set_xfade_curve);
	ClassDB::bind_method(D_METHOD("get_xfade_curve"), &AnimationNodeTransition::get_xfade_curve);

	ClassDB::bind_method(D_METHOD("set_allow_transition_to_self", "enable"), &AnimationNodeTransition::set_allow_transition_to_self);
	ClassDB::bind_method(D_METHOD("is_allow_transition_to_self"), &AnimationNodeTransition::is_allow_transition_to_self);

	ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "xfade_time", PROPERTY_HINT_RANGE, "0,120,0.01,suffix:s"), "set_xfade_time", "get_xfade_time");
	ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "xfade_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_xfade_curve", "get_xfade_curve");
	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_transition_to_self"), "set_allow_transition_to_self", "is_allow_transition_to_self");
	ADD_PROPERTY(PropertyInfo(Variant::INT, "input_count", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_ARRAY | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED, "Inputs,input_"), "set_input_count", "get_input_count");
}

AnimationNodeTransition::AnimationNodeTransition() {
}

/////////////////////

String AnimationNodeOutput::get_caption() const {
	return "Output";
}

AnimationNode::NodeTimeInfo AnimationNodeOutput::_process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only) {
	AnimationMixer::PlaybackInfo pi = p_playback_info;
	pi.weight = 1.0;
	return blend_input(0, pi, FILTER_IGNORE, true, p_test_only);
}

AnimationNodeOutput::AnimationNodeOutput() {
	add_input("output");
}

///////////////////////////////////////////////////////
void AnimationNodeBlendTree::add_node(const StringName &p_name, Ref<AnimationNode> p_node, const Vector2 &p_position) {
	ERR_FAIL_COND(nodes.has(p_name));
	ERR_FAIL_COND(p_node.is_null());
	ERR_FAIL_COND(p_name == SceneStringName(output));
	ERR_FAIL_COND(String(p_name).contains_char('/'));

	Node n;
	n.node = p_node;
	n.position = p_position;
	n.connections.resize(n.node->get_input_count());
	nodes[p_name] = n;

	emit_changed();
	emit_signal(SNAME("tree_changed"));

	p_node->connect(SNAME("tree_changed"), callable_mp(this, &AnimationNodeBlendTree::_tree_changed), CONNECT_REFERENCE_COUNTED);
	p_node->connect(SNAME("animation_node_renamed"), callable_mp(this, &AnimationNodeBlendTree::_animation_node_renamed), CONNECT_REFERENCE_COUNTED);
	p_node->connect(SNAME("animation_node_removed"), callable_mp(this, &AnimationNodeBlendTree::_animation_node_removed), CONNECT_REFERENCE_COUNTED);
	p_node->connect_changed(callable_mp(this, &AnimationNodeBlendTree::_node_changed).bind(p_name), CONNECT_REFERENCE_COUNTED);
}

Ref<AnimationNode> AnimationNodeBlendTree::get_node(const StringName &p_name) const {
	ERR_FAIL_COND_V(!nodes.has(p_name), Ref<AnimationNode>());

	return nodes[p_name].node;
}

StringName AnimationNodeBlendTree::get_node_name(const Ref<AnimationNode> &p_node) const {
	for (const KeyValue<StringName, Node> &E : nodes) {
		if (E.value.node == p_node) {
			return E.key;
		}
	}

	ERR_FAIL_V(StringName());
}

void AnimationNodeBlendTree::set_node_position(const StringName &p_node, const Vector2 &p_position) {
	ERR_FAIL_COND(!nodes.has(p_node));
	nodes[p_node].position = p_position;
}

Vector2 AnimationNodeBlendTree::get_node_position(const StringName &p_node) const {
	ERR_FAIL_COND_V(!nodes.has(p_node), Vector2());
	return nodes[p_node].position;
}

void AnimationNodeBlendTree::get_child_nodes(List<ChildNode> *r_child_nodes) {
	Vector<StringName> ns;

	for (const KeyValue<StringName, Node> &E : nodes) {
		ns.push_back(E.key);
	}

	for (int i = 0; i < ns.size(); i++) {
		ChildNode cn;
		cn.name = ns[i];
		cn.node = nodes[cn.name].node;
		r_child_nodes->push_back(cn);
	}
}

bool AnimationNodeBlendTree::has_node(const StringName &p_name) const {
	return nodes.has(p_name);
}

Vector<StringName> AnimationNodeBlendTree::get_node_connection_array(const StringName &p_name) const {
	ERR_FAIL_COND_V(!nodes.has(p_name), Vector<StringName>());
	return nodes[p_name].connections;
}

void AnimationNodeBlendTree::remove_node(const StringName &p_name) {
	ERR_FAIL_COND(!nodes.has(p_name));
	ERR_FAIL_COND(p_name == SceneStringName(output)); //can't delete output

	{
		Ref<AnimationNode> node = nodes[p_name].node;
		node->disconnect(SNAME("tree_changed"), callable_mp(this, &AnimationNodeBlendTree::_tree_changed));
		node->disconnect(SNAME("animation_node_renamed"), callable_mp(this, &AnimationNodeBlendTree::_animation_node_renamed));
		node->disconnect(SNAME("animation_node_removed"), callable_mp(this, &AnimationNodeBlendTree::_animation_node_removed));
		node->disconnect_changed(callable_mp(this, &AnimationNodeBlendTree::_node_changed));
	}

	nodes.erase(p_name);

	// Erase connections to name.
	for (KeyValue<StringName, Node> &E : nodes) {
		for (int i = 0; i < E.value.connections.size(); i++) {
			if (E.value.connections[i] == p_name) {
				E.value.connections.write[i] = StringName();
			}
		}
	}

	emit_signal(SNAME("animation_node_removed"), get_instance_id(), p_name);
	emit_changed();
	emit_signal(SNAME("tree_changed"));
}

void AnimationNodeBlendTree::rename_node(const StringName &p_name, const StringName &p_new_name) {
	ERR_FAIL_COND(!nodes.has(p_name));
	ERR_FAIL_COND(nodes.has(p_new_name));
	ERR_FAIL_COND(p_name == SceneStringName(output));
	ERR_FAIL_COND(p_new_name == SceneStringName(output));

	nodes[p_name].node->disconnect_changed(callable_mp(this, &AnimationNodeBlendTree::_node_changed));

	nodes[p_new_name] = nodes[p_name];
	nodes.erase(p_name);

	// Rename connections.
	for (KeyValue<StringName, Node> &E : nodes) {
		for (int i = 0; i < E.value.connections.size(); i++) {
			if (E.value.connections[i] == p_name) {
				E.value.connections.write[i] = p_new_name;
			}
		}
	}
	// Connection must be done with new name.
	nodes[p_new_name].node->connect_changed(callable_mp(this, &AnimationNodeBlendTree::_node_changed).bind(p_new_name), CONNECT_REFERENCE_COUNTED);

	emit_signal(SNAME("animation_node_renamed"), get_instance_id(), p_name, p_new_name);
	emit_signal(SNAME("tree_changed"));
}

void AnimationNodeBlendTree::connect_node(const StringName &p_input_node, int p_input_index, const StringName &p_output_node) {
	ERR_FAIL_COND(!nodes.has(p_output_node));
	ERR_FAIL_COND(!nodes.has(p_input_node));
	ERR_FAIL_COND(p_output_node == SceneStringName(output));
	ERR_FAIL_COND(p_input_node == p_output_node);

	Ref<AnimationNode> input = nodes[p_input_node].node;
	ERR_FAIL_INDEX(p_input_index, nodes[p_input_node].connections.size());

	for (KeyValue<StringName, Node> &E : nodes) {
		for (int i = 0; i < E.value.connections.size(); i++) {
			StringName output = E.value.connections[i];
			ERR_FAIL_COND(output == p_output_node);
		}
	}

	nodes[p_input_node].connections.write[p_input_index] = p_output_node;

	emit_changed();
}

void AnimationNodeBlendTree::disconnect_node(const StringName &p_node, int p_input_index) {
	ERR_FAIL_COND(!nodes.has(p_node));

	Ref<AnimationNode> input = nodes[p_node].node;
	ERR_FAIL_INDEX(p_input_index, nodes[p_node].connections.size());

	nodes[p_node].connections.write[p_input_index] = StringName();
}

AnimationNodeBlendTree::ConnectionError AnimationNodeBlendTree::can_connect_node(const StringName &p_input_node, int p_input_index, const StringName &p_output_node) const {
	if (!nodes.has(p_output_node) || p_output_node == SceneStringName(output)) {
		return CONNECTION_ERROR_NO_OUTPUT;
	}

	if (!nodes.has(p_input_node)) {
		return CONNECTION_ERROR_NO_INPUT;
	}

	if (p_input_node == p_output_node) {
		return CONNECTION_ERROR_SAME_NODE;
	}

	Ref<AnimationNode> input = nodes[p_input_node].node;

	if (p_input_index < 0 || p_input_index >= nodes[p_input_node].connections.size()) {
		return CONNECTION_ERROR_NO_INPUT_INDEX;
	}

	if (nodes[p_input_node].connections[p_input_index] != StringName()) {
		return CONNECTION_ERROR_CONNECTION_EXISTS;
	}

	for (const KeyValue<StringName, Node> &E : nodes) {
		for (int i = 0; i < E.value.connections.size(); i++) {
			const StringName output = E.value.connections[i];
			if (output == p_output_node) {
				return CONNECTION_ERROR_CONNECTION_EXISTS;
			}
		}
	}
	return CONNECTION_OK;
}

void AnimationNodeBlendTree::get_node_connections(List<NodeConnection> *r_connections) const {
	for (const KeyValue<StringName, Node> &E : nodes) {
		for (int i = 0; i < E.value.connections.size(); i++) {
			const StringName output = E.value.connections[i];
			if (output != StringName()) {
				NodeConnection nc;
				nc.input_node = E.key;
				nc.input_index = i;
				nc.output_node = output;
				r_connections->push_back(nc);
			}
		}
	}
}

String AnimationNodeBlendTree::get_caption() const {
	return "BlendTree";
}

AnimationNode::NodeTimeInfo AnimationNodeBlendTree::_process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only) {
	Ref<AnimationNodeOutput> output = nodes[SceneStringName(output)].node;
	node_state.connections = nodes[SceneStringName(output)].connections;
	ERR_FAIL_COND_V(output.is_null(), NodeTimeInfo());

	AnimationMixer::PlaybackInfo pi = p_playback_info;
	pi.weight = 1.0;

	return _blend_node(output, "output", this, pi, FILTER_IGNORE, true, p_test_only, nullptr);
}

void AnimationNodeBlendTree::get_node_list(List<StringName> *r_list) {
	for (const KeyValue<StringName, Node> &E : nodes) {
		r_list->push_back(E.key);
	}
}

void AnimationNodeBlendTree::set_graph_offset(const Vector2 &p_graph_offset) {
	graph_offset = p_graph_offset;
}

Vector2 AnimationNodeBlendTree::get_graph_offset() const {
	return graph_offset;
}

Ref<AnimationNode> AnimationNodeBlendTree::get_child_by_name(const StringName &p_name) const {
	return get_node(p_name);
}

bool AnimationNodeBlendTree::_set(const StringName &p_name, const Variant &p_value) {
	String prop_name = p_name;
	if (prop_name.begins_with("nodes/")) {
		String node_name = prop_name.get_slicec('/', 1);
		String what = prop_name.get_slicec('/', 2);

		if (what == "node") {
			Ref<AnimationNode> anode = p_value;
			if (anode.is_valid()) {
				add_node(node_name, p_value);
			}
			return true;
		}

		if (what == "position") {
			if (nodes.has(node_name)) {
				nodes[node_name].position = p_value;
			}
			return true;
		}
	} else if (prop_name == "node_connections") {
		Array conns = p_value;
		ERR_FAIL_COND_V(conns.size() % 3 != 0, false);

		for (int i = 0; i < conns.size(); i += 3) {
			connect_node(conns[i], conns[i + 1], conns[i + 2]);
		}
		return true;
	}

	return false;
}

bool AnimationNodeBlendTree::_get(const StringName &p_name, Variant &r_ret) const {
	String prop_name = p_name;
	if (prop_name.begins_with("nodes/")) {
		String node_name = prop_name.get_slicec('/', 1);
		String what = prop_name.get_slicec('/', 2);

		if (what == "node") {
			if (nodes.has(node_name)) {
				r_ret = nodes[node_name].node;
				return true;
			}
		}

		if (what == "position") {
			if (nodes.has(node_name)) {
				r_ret = nodes[node_name].position;
				return true;
			}
		}
	} else if (prop_name == "node_connections") {
		List<NodeConnection> nc;
		get_node_connections(&nc);
		Array conns;
		conns.resize(nc.size() * 3);

		int idx = 0;
		for (const NodeConnection &E : nc) {
			conns[idx * 3 + 0] = E.input_node;
			conns[idx * 3 + 1] = E.input_index;
			conns[idx * 3 + 2] = E.output_node;
			idx++;
		}

		r_ret = conns;
		return true;
	}

	return false;
}

void AnimationNodeBlendTree::_get_property_list(List<PropertyInfo> *p_list) const {
	List<StringName> names;
	for (const KeyValue<StringName, Node> &E : nodes) {
		names.push_back(E.key);
	}

	for (const StringName &E : names) {
		String prop_name = E;
		if (prop_name != "output") {
			p_list->push_back(PropertyInfo(Variant::OBJECT, "nodes/" + prop_name + "/node", PROPERTY_HINT_RESOURCE_TYPE, "AnimationNode", PROPERTY_USAGE_NO_EDITOR));
		}
		p_list->push_back(PropertyInfo(Variant::VECTOR2, "nodes/" + prop_name + "/position", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
	}

	p_list->push_back(PropertyInfo(Variant::ARRAY, "node_connections", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
}

void AnimationNodeBlendTree::_tree_changed() {
	AnimationRootNode::_tree_changed();
}

void AnimationNodeBlendTree::_animation_node_renamed(const ObjectID &p_oid, const String &p_old_name, const String &p_new_name) {
	AnimationRootNode::_animation_node_renamed(p_oid, p_old_name, p_new_name);
}

void AnimationNodeBlendTree::_animation_node_removed(const ObjectID &p_oid, const StringName &p_node) {
	AnimationRootNode::_animation_node_removed(p_oid, p_node);
}

void AnimationNodeBlendTree::reset_state() {
	graph_offset = Vector2();
	nodes.clear();
	_initialize_node_tree();
	emit_changed();
	emit_signal(SNAME("tree_changed"));
}

void AnimationNodeBlendTree::_node_changed(const StringName &p_node) {
	ERR_FAIL_COND(!nodes.has(p_node));
	nodes[p_node].connections.resize(nodes[p_node].node->get_input_count());
	emit_signal(SNAME("node_changed"), p_node);
}

#ifdef TOOLS_ENABLED
void AnimationNodeBlendTree::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const {
	const String pf = p_function;
	bool add_node_options = false;
	if (p_idx == 0) {
		add_node_options = (pf == "get_node" || pf == "has_node" || pf == "rename_node" || pf == "remove_node" || pf == "set_node_position" || pf == "get_node_position" || pf == "connect_node" || pf == "disconnect_node");
	} else if (p_idx == 2) {
		add_node_options = (pf == "connect_node" || pf == "disconnect_node");
	}
	if (add_node_options) {
		for (const KeyValue<StringName, Node> &E : nodes) {
			r_options->push_back(String(E.key).quote());
		}
	}
	AnimationRootNode::get_argument_options(p_function, p_idx, r_options);
}
#endif

void AnimationNodeBlendTree::_bind_methods() {
	ClassDB::bind_method(D_METHOD("add_node", "name", "node", "position"), &AnimationNodeBlendTree::add_node, DEFVAL(Vector2()));
	ClassDB::bind_method(D_METHOD("get_node", "name"), &AnimationNodeBlendTree::get_node);
	ClassDB::bind_method(D_METHOD("remove_node", "name"), &AnimationNodeBlendTree::remove_node);
	ClassDB::bind_method(D_METHOD("rename_node", "name", "new_name"), &AnimationNodeBlendTree::rename_node);
	ClassDB::bind_method(D_METHOD("has_node", "name"), &AnimationNodeBlendTree::has_node);
	ClassDB::bind_method(D_METHOD("connect_node", "input_node", "input_index", "output_node"), &AnimationNodeBlendTree::connect_node);
	ClassDB::bind_method(D_METHOD("disconnect_node", "input_node", "input_index"), &AnimationNodeBlendTree::disconnect_node);

	ClassDB::bind_method(D_METHOD("set_node_position", "name", "position"), &AnimationNodeBlendTree::set_node_position);
	ClassDB::bind_method(D_METHOD("get_node_position", "name"), &AnimationNodeBlendTree::get_node_position);

	ClassDB::bind_method(D_METHOD("set_graph_offset", "offset"), &AnimationNodeBlendTree::set_graph_offset);
	ClassDB::bind_method(D_METHOD("get_graph_offset"), &AnimationNodeBlendTree::get_graph_offset);

	ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "graph_offset", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_graph_offset", "get_graph_offset");

	BIND_CONSTANT(CONNECTION_OK);
	BIND_CONSTANT(CONNECTION_ERROR_NO_INPUT);
	BIND_CONSTANT(CONNECTION_ERROR_NO_INPUT_INDEX);
	BIND_CONSTANT(CONNECTION_ERROR_NO_OUTPUT);
	BIND_CONSTANT(CONNECTION_ERROR_SAME_NODE);
	BIND_CONSTANT(CONNECTION_ERROR_CONNECTION_EXISTS);

	ADD_SIGNAL(MethodInfo(SNAME("node_changed"), PropertyInfo(Variant::STRING_NAME, "node_name")));
}

void AnimationNodeBlendTree::_initialize_node_tree() {
	Ref<AnimationNodeOutput> output;
	output.instantiate();
	Node n;
	n.node = output;
	n.position = Vector2(300, 150);
	n.connections.resize(1);
	nodes["output"] = n;
}

AnimationNodeBlendTree::AnimationNodeBlendTree() {
	_initialize_node_tree();
}

AnimationNodeBlendTree::~AnimationNodeBlendTree() {
}