三国卡牌客户端基础资源仓库
yyl
5 天以前 cec146fc3fe287928e075c79ece20a20a9b16b20
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
/******************************************************************************
 * Spine Runtimes License Agreement
 * Last updated July 28, 2023. Replaces all prior versions.
 *
 * Copyright (c) 2013-2023, Esoteric Software LLC
 *
 * Integration of the Spine Runtimes into software or otherwise creating
 * derivative works of the Spine Runtimes is permitted under the terms and
 * conditions of Section 2 of the Spine Editor License Agreement:
 * http://esotericsoftware.com/spine-editor-license
 *
 * Otherwise, it is permitted to integrate the Spine Runtimes into software or
 * otherwise create derivative works of the Spine Runtimes (collectively,
 * "Products"), provided that each user of the Products must obtain their own
 * Spine Editor license and redistribution of the Products in any form must
 * include this license and copyright notice.
 *
 * THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES,
 * BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THE
 * SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *****************************************************************************/
 
using System;
using System.Collections.Generic;
 
namespace Spine {
 
    /// <summary>
    /// Stores a list of timelines to animate a skeleton's pose over time.</summary>
    public class Animation {
        internal String name;
        internal ExposedList<Timeline> timelines;
        internal HashSet<string> timelineIds;
        internal float duration;
 
        public Animation (string name, ExposedList<Timeline> timelines, float duration) {
            if (name == null) throw new ArgumentNullException("name", "name cannot be null.");
 
            this.name = name;
            SetTimelines(timelines);
            this.duration = duration;
        }
 
        public ExposedList<Timeline> Timelines {
            get { return timelines; }
            set { SetTimelines(value); }
        }
 
        public void SetTimelines (ExposedList<Timeline> timelines) {
            if (timelines == null) throw new ArgumentNullException("timelines", "timelines cannot be null.");
            this.timelines = timelines;
            // Note: avoiding reallocations by adding all hash set entries at
            // once (EnsureCapacity() is only available in newer .Net versions).
            int idCount = 0;
            int timelinesCount = timelines.Count;
            Timeline[] timelinesItems = timelines.Items;
            for (int t = 0; t < timelinesCount; ++t)
                idCount += timelinesItems[t].PropertyIds.Length;
            string[] propertyIds = new string[idCount];
            int currentId = 0;
            for (int t = 0; t < timelinesCount; ++t) {
                string[] ids = timelinesItems[t].PropertyIds;
                for (int i = 0, idsLength = ids.Length; i < idsLength; ++i)
                    propertyIds[currentId++] = ids[i];
            }
            this.timelineIds = new HashSet<string>(propertyIds);
        }
 
        /// <summary>The duration of the animation in seconds, which is usually the highest time of all frames in the timeline. The duration is
        /// used to know when it has completed and when it should loop back to the start.</summary>
        public float Duration { get { return duration; } set { duration = value; } }
 
        /// <summary>The animation's name, which is unique across all animations in the skeleton.</summary>
        public string Name { get { return name; } }
 
        /// <summary>Returns true if this animation contains a timeline with any of the specified property IDs.</summary>
        public bool HasTimeline (string[] propertyIds) {
            foreach (string id in propertyIds)
                if (timelineIds.Contains(id)) return true;
            return false;
        }
 
        /// <summary>Applies the animation's timelines to the specified skeleton.</summary>
        /// <seealso cref="Timeline.Apply(Skeleton, float, float, ExposedList, float, MixBlend, MixDirection)"/>
        /// <param name="skeleton">The skeleton the animation is being applied to. This provides access to the bones, slots, and other skeleton
        ///                    components the timelines may change.</param>
        /// <param name="lastTime">The last time in seconds this animation was applied. Some timelines trigger only at specific times rather
        ///                    than every frame. Pass -1 the first time an animation is applied to ensure frame 0 is triggered.</param>
        /// <param name="time"> The time in seconds the skeleton is being posed for. Most timelines find the frame before and the frame after
        ///                    this time and interpolate between the frame values. If beyond the <see cref="Duration"/> and <c>loop</c> is
        ///                    true then the animation will repeat, else the last frame will be applied.</param>
        /// <param name="loop">If true, the animation repeats after the <see cref="Duration"/>.</param>
        /// <param name="events">If any events are fired, they are added to this list. Can be null to ignore fired events or if no timelines
        ///                    fire events.</param>
        /// <param name="alpha"> 0 applies the current or setup values (depending on <c>blend</c>). 1 applies the timeline values. Between
        ///                    0 and 1 applies values between the current or setup values and the timeline values. By adjusting
        ///                    <c>alpha</c> over time, an animation can be mixed in or out. <c>alpha</c> can also be useful to apply
        ///                    animations on top of each other (layering).</param>
        /// <param name="blend">Controls how mixing is applied when <c>alpha</c> &lt; 1.</param>
        /// <param name="direction">Indicates whether the timelines are mixing in or out. Used by timelines which perform instant transitions,
        ///                    such as <see cref="DrawOrderTimeline"/> or <see cref="AttachmentTimeline"/>.</param>
        public void Apply (Skeleton skeleton, float lastTime, float time, bool loop, ExposedList<Event> events, float alpha,
                            MixBlend blend, MixDirection direction) {
            if (skeleton == null) throw new ArgumentNullException("skeleton", "skeleton cannot be null.");
 
            if (loop && duration != 0) {
                time %= duration;
                if (lastTime > 0) lastTime %= duration;
            }
 
            Timeline[] timelines = this.timelines.Items;
            for (int i = 0, n = this.timelines.Count; i < n; i++)
                timelines[i].Apply(skeleton, lastTime, time, events, alpha, blend, direction);
        }
 
        override public string ToString () {
            return name;
        }
    }
 
    /// <summary>
    /// Controls how timeline values are mixed with setup pose values or current pose values when a timeline is applied with
    /// <c>alpha</c> &lt; 1.</summary>
    /// <seealso cref="Timeline.Apply(Skeleton, float, float, ExposedList, float, MixBlend, MixDirection)"/>
    public enum MixBlend {
        /// <summary> Transitions from the setup value to the timeline value (the current value is not used). Before the first frame, the
        ///           setup value is set.</summary>
        Setup,
 
        /// <summary>
        /// <para>
        /// Transitions from the current value to the timeline value. Before the first frame, transitions from the current value to
        /// the setup value. Timelines which perform instant transitions, such as <see cref="DrawOrderTimeline"/> or
        /// <see cref="AttachmentTimeline"/>, use the setup value before the first frame.</para>
        /// <para>
        /// <c>First</c> is intended for the first animations applied, not for animations layered on top of those.</para>
        /// </summary>
        First,
 
        /// <summary>
        /// <para>
        /// Transitions from the current value to the timeline value. No change is made before the first frame (the current value is
        /// kept until the first frame).</para>
        /// <para>
        /// <c>Replace</c> is intended for animations layered on top of others, not for the first animations applied.</para>
        /// </summary>
        Replace,
 
        /// <summary>
        /// <para>
        /// Transitions from the current value to the current value plus the timeline value. No change is made before the first frame
        /// (the current value is kept until the first frame).</para>
        /// <para>
        /// <c>Add</c> is intended for animations layered on top of others, not for the first animations applied. Properties
        /// set by additive animations must be set manually or by another animation before applying the additive animations, else the
        /// property values will increase each time the additive animations are applied.
        /// </para>
        /// </summary>
        Add
    }
 
    /// <summary>
    /// Indicates whether a timeline's <c>alpha</c> is mixing out over time toward 0 (the setup or current pose value) or
    /// mixing in toward 1 (the timeline's value). Some timelines use this to decide how values are applied.</summary>
    /// <seealso cref="Timeline.Apply(Skeleton, float, float, ExposedList, float, MixBlend, MixDirection)"/>
    public enum MixDirection {
        In,
        Out
    }
 
    public enum Property {
        Rotate = 0, X, Y, ScaleX, ScaleY, ShearX, ShearY, Inherit, //
        RGB, Alpha, RGB2, //
        Attachment, Deform, //
        Event, DrawOrder, //
        IkConstraint, TransformConstraint, //
        PathConstraintPosition, PathConstraintSpacing, PathConstraintMix, //
        PhysicsConstraintInertia, PhysicsConstraintStrength, PhysicsConstraintDamping, PhysicsConstraintMass, //
        PhysicsConstraintWind, PhysicsConstraintGravity, PhysicsConstraintMix, PhysicsConstraintReset, //
        Sequence
    }
 
    /// <summary>
    /// The base class for all timelines.</summary>
    public abstract class Timeline {
        private readonly string[] propertyIds;
        internal readonly float[] frames;
 
        /// <param name="propertyIds">Unique identifiers for the properties the timeline modifies.</param>
        public Timeline (int frameCount, params string[] propertyIds) {
            if (propertyIds == null) throw new System.ArgumentNullException("propertyIds", "propertyIds cannot be null.");
            this.propertyIds = propertyIds;
            frames = new float[frameCount * FrameEntries];
        }
 
        /// <summary>Uniquely encodes both the type of this timeline and the skeleton properties that it affects.</summary>
        public string[] PropertyIds {
            get { return propertyIds; }
        }
 
        /// <summary>The time in seconds and any other values for each frame.</summary>
        public float[] Frames {
            get { return frames; }
        }
 
        /// <summary>The number of entries stored per frame.</summary>
        public virtual int FrameEntries {
            get { return 1; }
        }
 
        /// <summary>The number of frames for this timeline.</summary>
        public virtual int FrameCount {
            get { return frames.Length / FrameEntries; }
        }
 
        public float Duration {
            get {
                return frames[frames.Length - FrameEntries];
            }
        }
 
        /// <summary>Applies this timeline to the skeleton.</summary>
        /// <param name="skeleton">The skeleton the timeline is being applied to. This provides access to the bones, slots, and other
        ///                    skeleton components the timeline may change.</param>
        /// <param name="lastTime">The time this timeline was last applied. Timelines such as <see cref="EventTimeline"/> trigger only
        ///                    at specific times rather than every frame. In that case, the timeline triggers everything between
        ///                    <c>lastTime</c> (exclusive) and <c>time</c> (inclusive). Pass -1 the first time an animation is
        ///                     applied to ensure frame 0 is triggered.</param>
        /// <param name="time">The time in seconds that the skeleton is being posed for. Most timelines find the frame before and the frame
        ///                    after this time and interpolate between the frame values.If beyond the last frame, the last frame will be
        ///                    applied.</param>
        /// <param name="events">If any events are fired, they are added to this list. Can be null to ignore fired events or if the timeline
        ///                    does not fire events.</param>
        /// <param name="alpha">0 applies the current or setup value (depending on <c>blend</c>). 1 applies the timeline value.
        ///                    Between 0 and 1 applies a value between the current or setup value and the timeline value.By adjusting
        ///                    <c>alpha</c> over time, an animation can be mixed in or out. <c>alpha</c> can also be useful to
        ///                    apply animations on top of each other (layering).</param>
        /// <param name="blend">Controls how mixing is applied when <c>alpha</c> &lt; 1.</param>
        /// <param name="direction">Indicates whether the timeline is mixing in or out. Used by timelines which perform instant transitions,
        ///                   such as <see cref="DrawOrderTimeline"/> or <see cref="AttachmentTimeline"/>, and other such as <see cref="ScaleTimeline"/>.</param>
        public abstract void Apply (Skeleton skeleton, float lastTime, float time, ExposedList<Event> events, float alpha,
            MixBlend blend, MixDirection direction);
 
        /// <summary>Search using a stride of 1.</summary>
        /// <param name="time">Must be >= the first value in <c>frames</c>.</param>
        /// <returns>The index of the first value <= <c>time</c>.</returns>
        internal static int Search (float[] frames, float time) {
            int n = frames.Length;
            for (int i = 1; i < n; i++)
                if (frames[i] > time) return i - 1;
            return n - 1;
        }
 
        /// <summary>Search using the specified stride.</summary>
        /// <param name="time">Must be >= the first value in <c>frames</c>.</param>
        /// <returns>The index of the first value <= <c>time</c>.</returns>
        internal static int Search (float[] frames, float time, int step) {
            int n = frames.Length;
            for (int i = step; i < n; i += step)
                if (frames[i] > time) return i - step;
            return n - step;
        }
    }
 
    /// <summary>An interface for timelines which change the property of a bone.</summary>
    public interface IBoneTimeline {
        /// <summary>The index of the bone in <see cref="Skeleton.Bones"/> that will be changed when this timeline is applied.</summary>
        int BoneIndex { get; }
    }
 
    /// <summary>An interface for timelines which change the property of a slot.</summary>
    public interface ISlotTimeline {
        /// <summary>The index of the slot in <see cref="Skeleton.Slots"/> that will be changed when this timeline is applied.</summary>
        int SlotIndex { get; }
    }
 
    /// <summary>The base class for timelines that interpolate between frame values using stepped, linear, or a Bezier curve.</summary>
    public abstract class CurveTimeline : Timeline {
        public const int LINEAR = 0, STEPPED = 1, BEZIER = 2, BEZIER_SIZE = 18;
 
        internal float[] curves;
        /// <summary>The number of key frames for this timeline.</summary>
 
        /// <param name="bezierCount">The maximum number of Bezier curves. See <see cref="Shrink(int)"/>.</param>
        /// <param name="propertyIds">Unique identifiers for the properties the timeline modifies.</param>
        public CurveTimeline (int frameCount, int bezierCount, params string[] propertyIds)
            : base(frameCount, propertyIds) {
            curves = new float[frameCount + bezierCount * BEZIER_SIZE];
            curves[frameCount - 1] = STEPPED;
        }
 
        /// <summary>Sets the specified frame to linear interpolation.</summary>
        /// <param name="frame">Between 0 and <c>frameCount - 1</c>, inclusive.</param>
        public void SetLinear (int frame) {
            curves[frame] = LINEAR;
        }
 
        /// <summary>Sets the specified frame to stepped interpolation.</summary>
        /// <param name="frame">Between 0 and <c>frameCount - 1</c>, inclusive.</param>
        public void SetStepped (int frame) {
            curves[frame] = STEPPED;
        }
 
        /// <summary>Returns the interpolation type for the specified frame.</summary>
        /// <param name="frame">Between 0 and <c>frameCount - 1</c>, inclusive.</param>
        /// <returns><see cref="LINEAR"/>, <see cref="STEPPED"/> or <see cref="BEZIER"/> + the index of the Bezier segments.</returns>
        public float GetCurveType (int frame) {
            return (int)curves[frame];
        }
 
        /// <summary>Shrinks the storage for Bezier curves, for use when <c>bezierCount</c> (specified in the constructor) was larger
        /// than the actual number of Bezier curves.</summary>
        public void Shrink (int bezierCount) {
            int size = FrameCount + bezierCount * BEZIER_SIZE;
            if (curves.Length > size) {
                float[] newCurves = new float[size];
                Array.Copy(curves, 0, newCurves, 0, size);
                curves = newCurves;
            }
        }
 
        /// <summary>
        /// Stores the segments for the specified Bezier curve. For timelines that modify multiple values, there may be more than
        /// one curve per frame.</summary>
        /// <param name="bezier">The ordinal of this Bezier curve for this timeline, between 0 and <c>bezierCount - 1</c> (specified
        ///                    in the constructor), inclusive.</param>
        /// <param name="frame">Between 0 and <c>frameCount - 1</c>, inclusive.</param>
        /// <param name="value">The index of the value for the frame this curve is used for.</param>
        /// <param name="time1">The time for the first key.</param>
        /// <param name="value1">The value for the first key.</param>
        /// <param name="cx1">The time for the first Bezier handle.</param>
        /// <param name="cy1">The value for the first Bezier handle.</param>
        /// <param name="cx2">The time of the second Bezier handle.</param>
        /// <param name="cy2">The value for the second Bezier handle.</param>
        /// <param name="time2">The time for the second key.</param>
        /// <param name="value2">The value for the second key.</param>
        public void SetBezier (int bezier, int frame, int value, float time1, float value1, float cx1, float cy1, float cx2,
            float cy2, float time2, float value2) {
 
            float[] curves = this.curves;
            int i = FrameCount + bezier * BEZIER_SIZE;
            if (value == 0) curves[frame] = BEZIER + i;
            float tmpx = (time1 - cx1 * 2 + cx2) * 0.03f, tmpy = (value1 - cy1 * 2 + cy2) * 0.03f;
            float dddx = ((cx1 - cx2) * 3 - time1 + time2) * 0.006f, dddy = ((cy1 - cy2) * 3 - value1 + value2) * 0.006f;
            float ddx = tmpx * 2 + dddx, ddy = tmpy * 2 + dddy;
            float dx = (cx1 - time1) * 0.3f + tmpx + dddx * 0.16666667f, dy = (cy1 - value1) * 0.3f + tmpy + dddy * 0.16666667f;
            float x = time1 + dx, y = value1 + dy;
            for (int n = i + BEZIER_SIZE; i < n; i += 2) {
                curves[i] = x;
                curves[i + 1] = y;
                dx += ddx;
                dy += ddy;
                ddx += dddx;
                ddy += dddy;
                x += dx;
                y += dy;
            }
        }
 
        /// <summary>
        /// Returns the Bezier interpolated value for the specified time.</summary>
        /// <param name="frameIndex">The index into <see cref="Frames"/> for the values of the frame before <c>time</c>.</param>
        /// <param name="valueOffset">The offset from <c>frameIndex</c> to the value this curve is used for.</param>
        /// <param name="i">The index of the Bezier segments. See <see cref="GetCurveType(int)"/>.</param>
        public float GetBezierValue (float time, int frameIndex, int valueOffset, int i) {
            float[] curves = this.curves;
            if (curves[i] > time) {
                float x = frames[frameIndex], y = frames[frameIndex + valueOffset];
                return y + (time - x) / (curves[i] - x) * (curves[i + 1] - y);
            }
            int n = i + BEZIER_SIZE;
            for (i += 2; i < n; i += 2) {
                if (curves[i] >= time) {
                    float x = curves[i - 2], y = curves[i - 1];
                    return y + (time - x) / (curves[i] - x) * (curves[i + 1] - y);
                }
            }
            frameIndex += FrameEntries;
            { // scope added to prevent compile error "float x and y declared in enclosing scope"
                float x = curves[n - 2], y = curves[n - 1];
                return y + (time - x) / (frames[frameIndex] - x) * (frames[frameIndex + valueOffset] - y);
            }
        }
    }
 
    /// <summary>The base class for a <see cref="CurveTimeline"/> that sets one property.</summary>
    public abstract class CurveTimeline1 : CurveTimeline {
        public const int ENTRIES = 2;
        internal const int VALUE = 1;
 
        /// <param name="bezierCount">The maximum number of Bezier curves. See <see cref="Shrink(int)"/>.</param>
        /// <param name="propertyIds">Unique identifiers for the properties the timeline modifies.</param>
        public CurveTimeline1 (int frameCount, int bezierCount, string propertyId)
            : base(frameCount, bezierCount, propertyId) {
        }
 
        public override int FrameEntries {
            get { return ENTRIES; }
        }
 
        /// <summary>Sets the time and value for the specified frame.</summary>
        /// <param name="frame">Between 0 and <c>frameCount</c>, inclusive.</param>
        /// <param name="time">The frame time in seconds</param>
        public void SetFrame (int frame, float time, float value) {
            frame <<= 1;
            frames[frame] = time;
            frames[frame + VALUE] = value;
        }
 
        /// <summary>Returns the interpolated value for the specified time.</summary>
        public float GetCurveValue (float time) {
            float[] frames = this.frames;
            int i = frames.Length - 2;
            for (int ii = 2; ii <= i; ii += 2) {
                if (frames[ii] > time) {
                    i = ii - 2;
                    break;
                }
            }
 
            int curveType = (int)curves[i >> 1];
            switch (curveType) {
            case LINEAR:
                float before = frames[i], value = frames[i + VALUE];
                return value + (time - before) / (frames[i + ENTRIES] - before) * (frames[i + ENTRIES + VALUE] - value);
            case STEPPED:
                return frames[i + VALUE];
            }
            return GetBezierValue(time, i, VALUE, curveType - BEZIER);
        }
 
        public float GetRelativeValue (float time, float alpha, MixBlend blend, float current, float setup) {
            if (time < frames[0]) {
                switch (blend) {
                case MixBlend.Setup:
                    return setup;
                case MixBlend.First:
                    return current + (setup - current) * alpha;
                }
                return current;
            }
            float value = GetCurveValue(time);
            switch (blend) {
            case MixBlend.Setup:
                return setup + value * alpha;
            case MixBlend.First:
            case MixBlend.Replace:
                value += setup - current;
                break;
            }
            return current + value * alpha;
        }
 
        public float GetAbsoluteValue (float time, float alpha, MixBlend blend, float current, float setup) {
            if (time < frames[0]) {
                switch (blend) {
                case MixBlend.Setup:
                    return setup;
                case MixBlend.First:
                    return current + (setup - current) * alpha;
                }
                return current;
            }
            float value = GetCurveValue(time);
            if (blend == MixBlend.Setup) return setup + (value - setup) * alpha;
            return current + (value - current) * alpha;
        }
 
        public float GetAbsoluteValue (float time, float alpha, MixBlend blend, float current, float setup, float value) {
            if (time < frames[0]) {
                switch (blend) {
                case MixBlend.Setup:
                    return setup;
                case MixBlend.First:
                    return current + (setup - current) * alpha;
                }
                return current;
            }
            if (blend == MixBlend.Setup) return setup + (value - setup) * alpha;
            return current + (value - current) * alpha;
        }
 
        public float GetScaleValue (float time, float alpha, MixBlend blend, MixDirection direction, float current, float setup) {
            float[] frames = this.frames;
            if (time < frames[0]) {
                switch (blend) {
                case MixBlend.Setup:
                    return setup;
                case MixBlend.First:
                    return current + (setup - current) * alpha;
                }
                return current;
            }
            float value = GetCurveValue(time) * setup;
            if (alpha == 1) {
                if (blend == MixBlend.Add) return current + value - setup;
                return value;
            }
            // Mixing out uses sign of setup or current pose, else use sign of key.
            if (direction == MixDirection.Out) {
                switch (blend) {
                case MixBlend.Setup:
                    return setup + (Math.Abs(value) * Math.Sign(setup) - setup) * alpha;
                case MixBlend.First:
                case MixBlend.Replace:
                    return current + (Math.Abs(value) * Math.Sign(current) - current) * alpha;
                }
            } else {
                float s;
                switch (blend) {
                case MixBlend.Setup:
                    s = Math.Abs(setup) * Math.Sign(value);
                    return s + (value - s) * alpha;
                case MixBlend.First:
                case MixBlend.Replace:
                    s = Math.Abs(current) * Math.Sign(value);
                    return s + (value - s) * alpha;
                }
            }
            return current + (value - setup) * alpha;
        }
    }
 
    /// <summary>The base class for a <see cref="CurveTimeline"/> which sets two properties.</summary>
    public abstract class CurveTimeline2 : CurveTimeline {
        public const int ENTRIES = 3;
        internal const int VALUE1 = 1, VALUE2 = 2;
 
        /// <param name="bezierCount">The maximum number of Bezier curves. See <see cref="Shrink(int)"/>.</param>
        /// <param name="propertyIds">Unique identifiers for the properties the timeline modifies.</param>
        public CurveTimeline2 (int frameCount, int bezierCount, string propertyId1, string propertyId2)
            : base(frameCount, bezierCount, propertyId1, propertyId2) {
        }
 
        public override int FrameEntries {
            get { return ENTRIES; }
        }
 
        /// <summary>Sets the time and values for the specified frame.</summary>
        /// <param name="frame">Between 0 and <c>frameCount</c>, inclusive.</param>
        /// <param name="time">The frame time in seconds.</param>
        public void SetFrame (int frame, float time, float value1, float value2) {
            frame *= ENTRIES;
            frames[frame] = time;
            frames[frame + VALUE1] = value1;
            frames[frame + VALUE2] = value2;
        }
    }
 
    /// <summary>Changes a bone's local <see cref="Bone.Rotation"/>.</summary>
    public class RotateTimeline : CurveTimeline1, IBoneTimeline {
        readonly int boneIndex;
 
        public RotateTimeline (int frameCount, int bezierCount, int boneIndex)
            : base(frameCount, bezierCount, (int)Property.Rotate + "|" + boneIndex) {
            this.boneIndex = boneIndex;
        }
 
        public int BoneIndex {
            get {
                return boneIndex;
            }
        }
 
        override public void Apply (Skeleton skeleton, float lastTime, float time, ExposedList<Event> firedEvents, float alpha, MixBlend blend,
                                    MixDirection direction) {
            Bone bone = skeleton.bones.Items[boneIndex];
            if (bone.active) bone.rotation = GetRelativeValue(time, alpha, blend, bone.rotation, bone.data.rotation);
        }
    }
 
    /// <summary>Changes a bone's local <see cref"Bone.X"/> and <see cref"Bone.Y"/>.</summary>
    public class TranslateTimeline : CurveTimeline2, IBoneTimeline {
        readonly int boneIndex;
 
        public TranslateTimeline (int frameCount, int bezierCount, int boneIndex)
            : base(frameCount, bezierCount, //
                (int)Property.X + "|" + boneIndex, //
                (int)Property.Y + "|" + boneIndex) {
            this.boneIndex = boneIndex;
        }
 
        public int BoneIndex {
            get {
                return boneIndex;
            }
        }
 
        override public void Apply (Skeleton skeleton, float lastTime, float time, ExposedList<Event> firedEvents, float alpha, MixBlend blend,
                                    MixDirection direction) {
            Bone bone = skeleton.bones.Items[boneIndex];
            if (!bone.active) return;
 
            float[] frames = this.frames;
            if (time < frames[0]) {
                switch (blend) {
                case MixBlend.Setup:
                    bone.x = bone.data.x;
                    bone.y = bone.data.y;
                    return;
                case MixBlend.First:
                    bone.x += (bone.data.x - bone.x) * alpha;
                    bone.y += (bone.data.y - bone.y) * alpha;
                    return;
                }
                return;
            }
 
            float x, y;
            GetCurveValue(out x, out y, time); // note: reference implementation has code inlined
 
            switch (blend) {
            case MixBlend.Setup:
                bone.x = bone.data.x + x * alpha;
                bone.y = bone.data.y + y * alpha;
                break;
            case MixBlend.First:
            case MixBlend.Replace:
                bone.x += (bone.data.x + x - bone.x) * alpha;
                bone.y += (bone.data.y + y - bone.y) * alpha;
                break;
            case MixBlend.Add:
                bone.x += x * alpha;
                bone.y += y * alpha;
                break;
            }
        }
 
        public void GetCurveValue (out float x, out float y, float time) {
            int i = Search(frames, time, ENTRIES), curveType = (int)curves[i / ENTRIES];
            switch (curveType) {
            case LINEAR:
                float before = frames[i];
                x = frames[i + VALUE1];
                y = frames[i + VALUE2];
                float t = (time - before) / (frames[i + ENTRIES] - before);
                x += (frames[i + ENTRIES + VALUE1] - x) * t;
                y += (frames[i + ENTRIES + VALUE2] - y) * t;
                break;
            case STEPPED:
                x = frames[i + VALUE1];
                y = frames[i + VALUE2];
                break;
            default:
                x = GetBezierValue(time, i, VALUE1, curveType - BEZIER);
                y = GetBezierValue(time, i, VALUE2, curveType + BEZIER_SIZE - BEZIER);
                break;
            }
        }
    }
 
    /// <summary>Changes a bone's local <see cref"Bone.X"/>.</summary>
    public class TranslateXTimeline : CurveTimeline1, IBoneTimeline {
        readonly int boneIndex;
 
        public TranslateXTimeline (int frameCount, int bezierCount, int boneIndex)
            : base(frameCount, bezierCount, (int)Property.X + "|" + boneIndex) {
            this.boneIndex = boneIndex;
        }
 
        public int BoneIndex {
            get {
                return boneIndex;
            }
        }
 
        override public void Apply (Skeleton skeleton, float lastTime, float time, ExposedList<Event> firedEvents, float alpha, MixBlend blend,
                                    MixDirection direction) {
            Bone bone = skeleton.bones.Items[boneIndex];
            if (bone.active) bone.x = GetRelativeValue(time, alpha, blend, bone.x, bone.data.x);
        }
    }
 
    /// <summary>Changes a bone's local <see cref"Bone.Y"/>.</summary>
    public class TranslateYTimeline : CurveTimeline1, IBoneTimeline {
        readonly int boneIndex;
 
        public TranslateYTimeline (int frameCount, int bezierCount, int boneIndex)
            : base(frameCount, bezierCount, (int)Property.Y + "|" + boneIndex) {
            this.boneIndex = boneIndex;
        }
 
        public int BoneIndex {
            get {
                return boneIndex;
            }
        }
 
        override public void Apply (Skeleton skeleton, float lastTime, float time, ExposedList<Event> firedEvents, float alpha, MixBlend blend,
                                    MixDirection direction) {
            Bone bone = skeleton.bones.Items[boneIndex];
            if (bone.active) bone.y = GetRelativeValue(time, alpha, blend, bone.y, bone.data.y);
        }
    }
 
    /// <summary>Changes a bone's local <see cref="Bone.ScaleX"/> and <see cref="Bone.ScaleY"/>.</summary>
    public class ScaleTimeline : CurveTimeline2, IBoneTimeline {
        readonly int boneIndex;
 
        public ScaleTimeline (int frameCount, int bezierCount, int boneIndex)
            : base(frameCount, bezierCount, //
                (int)Property.ScaleX + "|" + boneIndex, //
                (int)Property.ScaleY + "|" + boneIndex) {
            this.boneIndex = boneIndex;
        }
 
        public int BoneIndex {
            get {
                return boneIndex;
            }
        }
 
        override public void Apply (Skeleton skeleton, float lastTime, float time, ExposedList<Event> firedEvents, float alpha, MixBlend blend,
                                    MixDirection direction) {
            Bone bone = skeleton.bones.Items[boneIndex];
            if (!bone.active) return;
 
            float[] frames = this.frames;
            if (time < frames[0]) {
                switch (blend) {
                case MixBlend.Setup:
                    bone.scaleX = bone.data.scaleX;
                    bone.scaleY = bone.data.scaleY;
                    return;
                case MixBlend.First:
                    bone.scaleX += (bone.data.scaleX - bone.scaleX) * alpha;
                    bone.scaleY += (bone.data.scaleY - bone.scaleY) * alpha;
                    return;
                }
                return;
            }
 
            float x, y;
            int i = Search(frames, time, ENTRIES), curveType = (int)curves[i / ENTRIES];
            switch (curveType) {
            case LINEAR:
                float before = frames[i];
                x = frames[i + VALUE1];
                y = frames[i + VALUE2];
                float t = (time - before) / (frames[i + ENTRIES] - before);
                x += (frames[i + ENTRIES + VALUE1] - x) * t;
                y += (frames[i + ENTRIES + VALUE2] - y) * t;
                break;
            case STEPPED:
                x = frames[i + VALUE1];
                y = frames[i + VALUE2];
                break;
            default:
                x = GetBezierValue(time, i, VALUE1, curveType - BEZIER);
                y = GetBezierValue(time, i, VALUE2, curveType + BEZIER_SIZE - BEZIER);
                break;
            }
            x *= bone.data.scaleX;
            y *= bone.data.scaleY;
 
            if (alpha == 1) {
                if (blend == MixBlend.Add) {
                    bone.scaleX += x - bone.data.scaleX;
                    bone.scaleY += y - bone.data.scaleY;
                } else {
                    bone.scaleX = x;
                    bone.scaleY = y;
                }
            } else {
                // Mixing out uses sign of setup or current pose, else use sign of key.
                float bx, by;
                if (direction == MixDirection.Out) {
                    switch (blend) {
                    case MixBlend.Setup:
                        bx = bone.data.scaleX;
                        by = bone.data.scaleY;
                        bone.scaleX = bx + (Math.Abs(x) * Math.Sign(bx) - bx) * alpha;
                        bone.scaleY = by + (Math.Abs(y) * Math.Sign(by) - by) * alpha;
                        break;
                    case MixBlend.First:
                    case MixBlend.Replace:
                        bx = bone.scaleX;
                        by = bone.scaleY;
                        bone.scaleX = bx + (Math.Abs(x) * Math.Sign(bx) - bx) * alpha;
                        bone.scaleY = by + (Math.Abs(y) * Math.Sign(by) - by) * alpha;
                        break;
                    case MixBlend.Add:
                        bone.scaleX += (x - bone.data.scaleX) * alpha;
                        bone.scaleY += (y - bone.data.scaleY) * alpha;
                        break;
                    }
                } else {
                    switch (blend) {
                    case MixBlend.Setup:
                        bx = Math.Abs(bone.data.scaleX) * Math.Sign(x);
                        by = Math.Abs(bone.data.scaleY) * Math.Sign(y);
                        bone.scaleX = bx + (x - bx) * alpha;
                        bone.scaleY = by + (y - by) * alpha;
                        break;
                    case MixBlend.First:
                    case MixBlend.Replace:
                        bx = Math.Abs(bone.scaleX) * Math.Sign(x);
                        by = Math.Abs(bone.scaleY) * Math.Sign(y);
                        bone.scaleX = bx + (x - bx) * alpha;
                        bone.scaleY = by + (y - by) * alpha;
                        break;
                    case MixBlend.Add:
                        bone.scaleX += (x - bone.data.scaleX) * alpha;
                        bone.scaleY += (y - bone.data.scaleY) * alpha;
                        break;
                    }
                }
            }
        }
    }
 
    /// <summary>Changes a bone's local <see cref="Bone.ScaleX"/>.</summary>
    public class ScaleXTimeline : CurveTimeline1, IBoneTimeline {
        readonly int boneIndex;
 
        public ScaleXTimeline (int frameCount, int bezierCount, int boneIndex)
            : base(frameCount, bezierCount, (int)Property.ScaleX + "|" + boneIndex) {
            this.boneIndex = boneIndex;
        }
 
        public int BoneIndex {
            get {
                return boneIndex;
            }
        }
 
        override public void Apply (Skeleton skeleton, float lastTime, float time, ExposedList<Event> firedEvents, float alpha, MixBlend blend,
                                    MixDirection direction) {
            Bone bone = skeleton.bones.Items[boneIndex];
            if (bone.active) bone.scaleX = GetScaleValue(time, alpha, blend, direction, bone.scaleX, bone.data.scaleX);
        }
    }
 
    /// <summary>Changes a bone's local <see cref="Bone.ScaleY"/>.</summary>
    public class ScaleYTimeline : CurveTimeline1, IBoneTimeline {
        readonly int boneIndex;
 
        public ScaleYTimeline (int frameCount, int bezierCount, int boneIndex)
            : base(frameCount, bezierCount, (int)Property.ScaleY + "|" + boneIndex) {
            this.boneIndex = boneIndex;
        }
 
        public int BoneIndex {
            get {
                return boneIndex;
            }
        }
 
        override public void Apply (Skeleton skeleton, float lastTime, float time, ExposedList<Event> firedEvents, float alpha, MixBlend blend,
                                    MixDirection direction) {
            Bone bone = skeleton.bones.Items[boneIndex];
            if (bone.active) bone.scaleY = GetScaleValue(time, alpha, blend, direction, bone.scaleY, bone.data.scaleY);
        }
    }
 
    /// <summary>Changes a bone's local <see cref="Bone.ShearX"/> and <see cref="Bone.ShearY"/>.</summary>
    public class ShearTimeline : CurveTimeline2, IBoneTimeline {
        readonly int boneIndex;
 
        public ShearTimeline (int frameCount, int bezierCount, int boneIndex)
            : base(frameCount, bezierCount, //
                (int)Property.ShearX + "|" + boneIndex, //
                (int)Property.ShearY + "|" + boneIndex) {
            this.boneIndex = boneIndex;
        }
 
        public int BoneIndex {
            get {
                return boneIndex;
            }
        }
 
        override public void Apply (Skeleton skeleton, float lastTime, float time, ExposedList<Event> firedEvents, float alpha, MixBlend blend,
                                    MixDirection direction) {
            Bone bone = skeleton.bones.Items[boneIndex];
            if (!bone.active) return;
 
            float[] frames = this.frames;
            if (time < frames[0]) { // Time is before first frame.
                switch (blend) {
                case MixBlend.Setup:
                    bone.shearX = bone.data.shearX;
                    bone.shearY = bone.data.shearY;
                    return;
                case MixBlend.First:
                    bone.shearX += (bone.data.shearX - bone.shearX) * alpha;
                    bone.shearY += (bone.data.shearY - bone.shearY) * alpha;
                    return;
                }
                return;
            }
 
            float x, y;
            int i = Search(frames, time, ENTRIES), curveType = (int)curves[i / ENTRIES];
            switch (curveType) {
            case LINEAR:
                float before = frames[i];
                x = frames[i + VALUE1];
                y = frames[i + VALUE2];
                float t = (time - before) / (frames[i + ENTRIES] - before);
                x += (frames[i + ENTRIES + VALUE1] - x) * t;
                y += (frames[i + ENTRIES + VALUE2] - y) * t;
                break;
            case STEPPED:
                x = frames[i + VALUE1];
                y = frames[i + VALUE2];
                break;
            default:
                x = GetBezierValue(time, i, VALUE1, curveType - BEZIER);
                y = GetBezierValue(time, i, VALUE2, curveType + BEZIER_SIZE - BEZIER);
                break;
            }
 
            switch (blend) {
            case MixBlend.Setup:
                bone.shearX = bone.data.shearX + x * alpha;
                bone.shearY = bone.data.shearY + y * alpha;
                break;
            case MixBlend.First:
            case MixBlend.Replace:
                bone.shearX += (bone.data.shearX + x - bone.shearX) * alpha;
                bone.shearY += (bone.data.shearY + y - bone.shearY) * alpha;
                break;
            case MixBlend.Add:
                bone.shearX += x * alpha;
                bone.shearY += y * alpha;
                break;
            }
        }
    }
 
    /// <summary>Changes a bone's local <see cref="Bone.ShearX"/>.</summary>
    public class ShearXTimeline : CurveTimeline1, IBoneTimeline {
        readonly int boneIndex;
 
        public ShearXTimeline (int frameCount, int bezierCount, int boneIndex)
            : base(frameCount, bezierCount, (int)Property.ShearX + "|" + boneIndex) {
            this.boneIndex = boneIndex;
        }
 
        public int BoneIndex {
            get {
                return boneIndex;
            }
        }
 
        override public void Apply (Skeleton skeleton, float lastTime, float time, ExposedList<Event> firedEvents, float alpha, MixBlend blend,
                                    MixDirection direction) {
            Bone bone = skeleton.bones.Items[boneIndex];
            if (bone.active) bone.shearX = GetRelativeValue(time, alpha, blend, bone.shearX, bone.data.shearX);
        }
    }
 
    /// <summary>Changes a bone's local <see cref="Bone.ShearY"/>.</summary>
    public class ShearYTimeline : CurveTimeline1, IBoneTimeline {
        readonly int boneIndex;
 
        public ShearYTimeline (int frameCount, int bezierCount, int boneIndex)
            : base(frameCount, bezierCount, (int)Property.ShearY + "|" + boneIndex) {
            this.boneIndex = boneIndex;
        }
 
        public int BoneIndex {
            get {
                return boneIndex;
            }
        }
 
        override public void Apply (Skeleton skeleton, float lastTime, float time, ExposedList<Event> firedEvents, float alpha, MixBlend blend,
                                    MixDirection direction) {
            Bone bone = skeleton.bones.Items[boneIndex];
            if (bone.active) bone.shearY = GetRelativeValue(time, alpha, blend, bone.shearY, bone.data.shearY);
        }
    }
 
    /// <summary>Changes a bone's <see cref="Bone.Inherit"/>.</summary>
 
    public class InheritTimeline : Timeline, IBoneTimeline {
        public const int ENTRIES = 2;
        public const int INHERIT = 1;
 
        readonly int boneIndex;
 
        public InheritTimeline (int frameCount, int boneIndex)
            : base(frameCount, (int)Property.Inherit + "|" + boneIndex) {
            this.boneIndex = boneIndex;
        }
 
        public int BoneIndex {
            get {
                return boneIndex;
            }
        }
 
        public override int FrameEntries {
            get { return ENTRIES; }
        }
 
        /// <summary>Sets the transform mode for the specified frame.</summary>
        /// <param name="frame">Between 0 and <c>frameCount</c>, inclusive.</param>
        /// <param name="time">The frame time in seconds.</param>
        public void SetFrame (int frame, float time, Inherit inherit) {
            frame *= ENTRIES;
            frames[frame] = time;
            frames[frame + INHERIT] = (int)inherit;
        }
 
        override public void Apply (Skeleton skeleton, float lastTime, float time, ExposedList<Event> firedEvents, float alpha, MixBlend blend,
                                    MixDirection direction) {
 
            Bone bone = skeleton.bones.Items[boneIndex];
            if (!bone.active) return;
 
            if (direction == MixDirection.Out) {
                if (blend == MixBlend.Setup) bone.inherit = bone.data.inherit;
                return;
            }
 
            float[] frames = this.frames;
            if (time < frames[0]) {
                if (blend == MixBlend.Setup || blend == MixBlend.First) bone.inherit = bone.data.inherit;
                return;
            }
            bone.inherit = InheritEnum.Values[(int)frames[Search(frames, time, ENTRIES) + INHERIT]];
        }
    }
 
    /// <summary>Changes a slot's <see cref="Slot.Color"/>.</summary>
    public class RGBATimeline : CurveTimeline, ISlotTimeline {
        public const int ENTRIES = 5;
        protected const int R = 1, G = 2, B = 3, A = 4;
 
        readonly int slotIndex;
 
        public RGBATimeline (int frameCount, int bezierCount, int slotIndex)
            : base(frameCount, bezierCount, //
                (int)Property.RGB + "|" + slotIndex, //
                (int)Property.Alpha + "|" + slotIndex) {
            this.slotIndex = slotIndex;
        }
        public override int FrameEntries {
            get { return ENTRIES; }
        }
 
        public int SlotIndex {
            get {
                return slotIndex;
            }
        }
 
        /// <summary>Sets the time and color for the specified frame.</summary>
        /// <param name="frame">Between 0 and <c>frameCount</c>, inclusive.</param>
        /// <param name="time">The frame time in seconds.</param>
        public void SetFrame (int frame, float time, float r, float g, float b, float a) {
            frame *= ENTRIES;
            frames[frame] = time;
            frames[frame + R] = r;
            frames[frame + G] = g;
            frames[frame + B] = b;
            frames[frame + A] = a;
        }
 
        override public void Apply (Skeleton skeleton, float lastTime, float time, ExposedList<Event> firedEvents, float alpha, MixBlend blend,
                                    MixDirection direction) {
            Slot slot = skeleton.slots.Items[slotIndex];
            if (!slot.bone.active) return;
 
            float[] frames = this.frames;
            if (time < frames[0]) {
                SlotData setup = slot.data;
                switch (blend) {
                case MixBlend.Setup:
                    slot.r = setup.r;
                    slot.g = setup.g;
                    slot.b = setup.b;
                    slot.a = setup.a;
                    return;
                case MixBlend.First:
                    slot.r += (setup.r - slot.r) * alpha;
                    slot.g += (setup.g - slot.g) * alpha;
                    slot.b += (setup.b - slot.b) * alpha;
                    slot.a += (setup.a - slot.a) * alpha;
                    slot.ClampColor();
                    return;
                }
                return;
            }
 
            float r, g, b, a;
            int i = Search(frames, time, ENTRIES), curveType = (int)curves[i / ENTRIES];
            switch (curveType) {
            case LINEAR:
                float before = frames[i];
                r = frames[i + R];
                g = frames[i + G];
                b = frames[i + B];
                a = frames[i + A];
                float t = (time - before) / (frames[i + ENTRIES] - before);
                r += (frames[i + ENTRIES + R] - r) * t;
                g += (frames[i + ENTRIES + G] - g) * t;
                b += (frames[i + ENTRIES + B] - b) * t;
                a += (frames[i + ENTRIES + A] - a) * t;
                break;
            case STEPPED:
                r = frames[i + R];
                g = frames[i + G];
                b = frames[i + B];
                a = frames[i + A];
                break;
            default:
                r = GetBezierValue(time, i, R, curveType - BEZIER);
                g = GetBezierValue(time, i, G, curveType + BEZIER_SIZE - BEZIER);
                b = GetBezierValue(time, i, B, curveType + BEZIER_SIZE * 2 - BEZIER);
                a = GetBezierValue(time, i, A, curveType + BEZIER_SIZE * 3 - BEZIER);
                break;
            }
 
            if (alpha == 1) {
                slot.r = r;
                slot.g = g;
                slot.b = b;
                slot.a = a;
            } else {
                float br, bg, bb, ba;
                if (blend == MixBlend.Setup) {
                    br = slot.data.r;
                    bg = slot.data.g;
                    bb = slot.data.b;
                    ba = slot.data.a;
                } else {
                    br = slot.r;
                    bg = slot.g;
                    bb = slot.b;
                    ba = slot.a;
                }
                slot.r = br + (r - br) * alpha;
                slot.g = bg + (g - bg) * alpha;
                slot.b = bb + (b - bb) * alpha;
                slot.a = ba + (a - ba) * alpha;
            }
            slot.ClampColor();
        }
    }
 
    /// <summary>Changes the RGB for a slot's <see cref="Slot.Color"/>.</summary>
    public class RGBTimeline : CurveTimeline, ISlotTimeline {
        public const int ENTRIES = 4;
        protected const int R = 1, G = 2, B = 3;
 
        readonly int slotIndex;
 
        public RGBTimeline (int frameCount, int bezierCount, int slotIndex)
            : base(frameCount, bezierCount, //
                (int)Property.RGB + "|" + slotIndex) {
            this.slotIndex = slotIndex;
        }
 
        public override int FrameEntries {
            get { return ENTRIES; }
        }
 
        public int SlotIndex {
            get {
                return slotIndex;
            }
        }
 
        /// <summary>Sets the time and color for the specified frame.</summary>
        /// <param name="frame">Between 0 and <c>frameCount</c>, inclusive.</param>
        /// <param name="time">The frame time in seconds.</param>
        public void SetFrame (int frame, float time, float r, float g, float b) {
            frame <<= 2;
            frames[frame] = time;
            frames[frame + R] = r;
            frames[frame + G] = g;
            frames[frame + B] = b;
        }
 
        override public void Apply (Skeleton skeleton, float lastTime, float time, ExposedList<Event> firedEvents, float alpha, MixBlend blend,
                                    MixDirection direction) {
            Slot slot = skeleton.slots.Items[slotIndex];
            if (!slot.bone.active) return;
 
            float[] frames = this.frames;
            if (time < frames[0]) {
                SlotData setup = slot.data;
                switch (blend) {
                case MixBlend.Setup:
                    slot.r = setup.r;
                    slot.g = setup.g;
                    slot.b = setup.b;
                    return;
                case MixBlend.First:
                    slot.r += (setup.r - slot.r) * alpha;
                    slot.g += (setup.g - slot.g) * alpha;
                    slot.b += (setup.b - slot.b) * alpha;
                    slot.ClampColor();
                    return;
                }
                return;
            }
 
            float r, g, b;
            int i = Search(frames, time, ENTRIES), curveType = (int)curves[i >> 2];
            switch (curveType) {
            case LINEAR:
                float before = frames[i];
                r = frames[i + R];
                g = frames[i + G];
                b = frames[i + B];
                float t = (time - before) / (frames[i + ENTRIES] - before);
                r += (frames[i + ENTRIES + R] - r) * t;
                g += (frames[i + ENTRIES + G] - g) * t;
                b += (frames[i + ENTRIES + B] - b) * t;
                break;
            case STEPPED:
                r = frames[i + R];
                g = frames[i + G];
                b = frames[i + B];
                break;
            default:
                r = GetBezierValue(time, i, R, curveType - BEZIER);
                g = GetBezierValue(time, i, G, curveType + BEZIER_SIZE - BEZIER);
                b = GetBezierValue(time, i, B, curveType + BEZIER_SIZE * 2 - BEZIER);
                break;
            }
 
            if (alpha == 1) {
                slot.r = r;
                slot.g = g;
                slot.b = b;
            } else {
                float br, bg, bb;
                if (blend == MixBlend.Setup) {
                    SlotData setup = slot.data;
                    br = setup.r;
                    bg = setup.g;
                    bb = setup.b;
                } else {
                    br = slot.r;
                    bg = slot.g;
                    bb = slot.b;
                }
                slot.r = br + (r - br) * alpha;
                slot.g = bg + (g - bg) * alpha;
                slot.b = bb + (b - bb) * alpha;
            }
            slot.ClampColor();
        }
    }
 
    /// <summary>Changes the alpha for a slot's <see cref="Slot.Color"/>.</summary>
    public class AlphaTimeline : CurveTimeline1, ISlotTimeline {
        readonly int slotIndex;
 
        public AlphaTimeline (int frameCount, int bezierCount, int slotIndex)
            : base(frameCount, bezierCount, (int)Property.Alpha + "|" + slotIndex) {
            this.slotIndex = slotIndex;
        }
 
        public int SlotIndex {
            get {
                return slotIndex;
            }
        }
 
        override public void Apply (Skeleton skeleton, float lastTime, float time, ExposedList<Event> firedEvents, float alpha, MixBlend blend,
                                    MixDirection direction) {
            Slot slot = skeleton.slots.Items[slotIndex];
            if (!slot.bone.active) return;
 
            float[] frames = this.frames;
            if (time < frames[0]) {
                SlotData setup = slot.data;
                switch (blend) {
                case MixBlend.Setup:
                    slot.a = setup.a;
                    return;
                case MixBlend.First:
                    slot.a += (setup.a - slot.a) * alpha;
                    slot.ClampColor();
                    return;
                }
                return;
            }
 
            float a = GetCurveValue(time);
            if (alpha == 1)
                slot.a = a;
            else {
                if (blend == MixBlend.Setup) slot.a = slot.data.a;
                slot.a += (a - slot.a) * alpha;
            }
            slot.ClampColor();
        }
    }
 
    /// <summary>Changes a slot's <see cref="Slot.Color"/> and <see cref="Slot.DarkColor"/> for two color tinting.</summary>
    public class RGBA2Timeline : CurveTimeline, ISlotTimeline {
        public const int ENTRIES = 8;
        protected const int R = 1, G = 2, B = 3, A = 4, R2 = 5, G2 = 6, B2 = 7;
 
        readonly int slotIndex;
 
        public RGBA2Timeline (int frameCount, int bezierCount, int slotIndex)
            : base(frameCount, bezierCount, //
                (int)Property.RGB + "|" + slotIndex, //
                (int)Property.Alpha + "|" + slotIndex, //
                (int)Property.RGB2 + "|" + slotIndex) {
            this.slotIndex = slotIndex;
        }
 
        public override int FrameEntries {
            get {
                return ENTRIES;
            }
        }
 
        /// <summary>
        /// The index of the slot in <see cref="Skeleton.Slots"/> that will be changed when this timeline is applied. The
        /// <see cref="Slot"/> must have a dark color available.</summary>
        public int SlotIndex {
            get {
                return slotIndex;
            }
        }
 
        /// <summary>Sets the time, light color, and dark color for the specified frame.</summary>
        /// <param name="frame">Between 0 and <c>frameCount</c>, inclusive.</param>
        /// <param name="time">The frame time in seconds.</param>
        public void SetFrame (int frame, float time, float r, float g, float b, float a, float r2, float g2, float b2) {
            frame <<= 3;
            frames[frame] = time;
            frames[frame + R] = r;
            frames[frame + G] = g;
            frames[frame + B] = b;
            frames[frame + A] = a;
            frames[frame + R2] = r2;
            frames[frame + G2] = g2;
            frames[frame + B2] = b2;
        }
 
        override public void Apply (Skeleton skeleton, float lastTime, float time, ExposedList<Event> firedEvents, float alpha, MixBlend blend,
                                    MixDirection direction) {
            Slot slot = skeleton.slots.Items[slotIndex];
            if (!slot.bone.active) return;
 
            float[] frames = this.frames;
            if (time < frames[0]) {
                SlotData setup = slot.data;
                switch (blend) {
                case MixBlend.Setup:
                    slot.r = setup.r;
                    slot.g = setup.g;
                    slot.b = setup.b;
                    slot.a = setup.a;
                    slot.ClampColor();
                    slot.r2 = setup.r2;
                    slot.g2 = setup.g2;
                    slot.b2 = setup.b2;
                    slot.ClampSecondColor();
                    return;
                case MixBlend.First:
                    slot.r += (slot.r - setup.r) * alpha;
                    slot.g += (slot.g - setup.g) * alpha;
                    slot.b += (slot.b - setup.b) * alpha;
                    slot.a += (slot.a - setup.a) * alpha;
                    slot.ClampColor();
                    slot.r2 += (slot.r2 - setup.r2) * alpha;
                    slot.g2 += (slot.g2 - setup.g2) * alpha;
                    slot.b2 += (slot.b2 - setup.b2) * alpha;
                    slot.ClampSecondColor();
                    return;
                }
                return;
            }
 
            float r, g, b, a, r2, g2, b2;
            int i = Search(frames, time, ENTRIES), curveType = (int)curves[i >> 3];
            switch (curveType) {
            case LINEAR:
                float before = frames[i];
                r = frames[i + R];
                g = frames[i + G];
                b = frames[i + B];
                a = frames[i + A];
                r2 = frames[i + R2];
                g2 = frames[i + G2];
                b2 = frames[i + B2];
                float t = (time - before) / (frames[i + ENTRIES] - before);
                r += (frames[i + ENTRIES + R] - r) * t;
                g += (frames[i + ENTRIES + G] - g) * t;
                b += (frames[i + ENTRIES + B] - b) * t;
                a += (frames[i + ENTRIES + A] - a) * t;
                r2 += (frames[i + ENTRIES + R2] - r2) * t;
                g2 += (frames[i + ENTRIES + G2] - g2) * t;
                b2 += (frames[i + ENTRIES + B2] - b2) * t;
                break;
            case STEPPED:
                r = frames[i + R];
                g = frames[i + G];
                b = frames[i + B];
                a = frames[i + A];
                r2 = frames[i + R2];
                g2 = frames[i + G2];
                b2 = frames[i + B2];
                break;
            default:
                r = GetBezierValue(time, i, R, curveType - BEZIER);
                g = GetBezierValue(time, i, G, curveType + BEZIER_SIZE - BEZIER);
                b = GetBezierValue(time, i, B, curveType + BEZIER_SIZE * 2 - BEZIER);
                a = GetBezierValue(time, i, A, curveType + BEZIER_SIZE * 3 - BEZIER);
                r2 = GetBezierValue(time, i, R2, curveType + BEZIER_SIZE * 4 - BEZIER);
                g2 = GetBezierValue(time, i, G2, curveType + BEZIER_SIZE * 5 - BEZIER);
                b2 = GetBezierValue(time, i, B2, curveType + BEZIER_SIZE * 6 - BEZIER);
                break;
            }
 
            if (alpha == 1) {
                slot.r = r;
                slot.g = g;
                slot.b = b;
                slot.a = a;
                slot.r2 = r2;
                slot.g2 = g2;
                slot.b2 = b2;
            } else {
                float br, bg, bb, ba, br2, bg2, bb2;
                if (blend == MixBlend.Setup) {
                    br = slot.data.r;
                    bg = slot.data.g;
                    bb = slot.data.b;
                    ba = slot.data.a;
                    br2 = slot.data.r2;
                    bg2 = slot.data.g2;
                    bb2 = slot.data.b2;
                } else {
                    br = slot.r;
                    bg = slot.g;
                    bb = slot.b;
                    ba = slot.a;
                    br2 = slot.r2;
                    bg2 = slot.g2;
                    bb2 = slot.b2;
                }
                slot.r = br + (r - br) * alpha;
                slot.g = bg + (g - bg) * alpha;
                slot.b = bb + (b - bb) * alpha;
                slot.a = ba + (a - ba) * alpha;
                slot.r2 = br2 + (r2 - br2) * alpha;
                slot.g2 = bg2 + (g2 - bg2) * alpha;
                slot.b2 = bb2 + (b2 - bb2) * alpha;
            }
            slot.ClampColor();
            slot.ClampSecondColor();
        }
    }
 
    /// <summary>Changes the RGB for a slot's <see cref="Slot.Color"/> and <see cref="Slot.DarkColor"/> for two color tinting.</summary>
    public class RGB2Timeline : CurveTimeline, ISlotTimeline {
        public const int ENTRIES = 7;
        protected const int R = 1, G = 2, B = 3, R2 = 4, G2 = 5, B2 = 6;
 
        readonly int slotIndex;
 
        public RGB2Timeline (int frameCount, int bezierCount, int slotIndex)
            : base(frameCount, bezierCount, //
                (int)Property.RGB + "|" + slotIndex, //
                (int)Property.RGB2 + "|" + slotIndex) {
            this.slotIndex = slotIndex;
        }
 
        public override int FrameEntries {
            get {
                return ENTRIES;
            }
        }
 
        /// <summary>
        /// The index of the slot in <see cref="Skeleton.Slots"/> that will be changed when this timeline is applied. The
        /// <see cref="Slot"/> must have a dark color available.</summary>
        public int SlotIndex {
            get {
                return slotIndex;
            }
        }
 
        /// <summary>Sets the time, light color, and dark color for the specified frame.</summary>
        /// <param name="frame">Between 0 and <c>frameCount</c>, inclusive.</param>
        /// <param name="time">The frame time in seconds.</param>
        public void SetFrame (int frame, float time, float r, float g, float b, float r2, float g2, float b2) {
            frame *= ENTRIES;
            frames[frame] = time;
            frames[frame + R] = r;
            frames[frame + G] = g;
            frames[frame + B] = b;
            frames[frame + R2] = r2;
            frames[frame + G2] = g2;
            frames[frame + B2] = b2;
        }
 
        override public void Apply (Skeleton skeleton, float lastTime, float time, ExposedList<Event> firedEvents, float alpha, MixBlend blend,
                                    MixDirection direction) {
            Slot slot = skeleton.slots.Items[slotIndex];
            if (!slot.bone.active) return;
 
            float[] frames = this.frames;
            if (time < frames[0]) {
                SlotData setup = slot.data;
                switch (blend) {
                case MixBlend.Setup:
                    slot.r = setup.r;
                    slot.g = setup.g;
                    slot.b = setup.b;
                    slot.ClampColor();
                    slot.r2 = setup.r2;
                    slot.g2 = setup.g2;
                    slot.b2 = setup.b2;
                    slot.ClampSecondColor();
                    return;
                case MixBlend.First:
                    slot.r += (slot.r - setup.r) * alpha;
                    slot.g += (slot.g - setup.g) * alpha;
                    slot.b += (slot.b - setup.b) * alpha;
                    slot.ClampColor();
                    slot.r2 += (slot.r2 - setup.r2) * alpha;
                    slot.g2 += (slot.g2 - setup.g2) * alpha;
                    slot.b2 += (slot.b2 - setup.b2) * alpha;
                    slot.ClampSecondColor();
                    return;
                }
                return;
            }
 
            float r, g, b, r2, g2, b2;
            int i = Search(frames, time, ENTRIES), curveType = (int)curves[i / ENTRIES];
            switch (curveType) {
            case LINEAR:
                float before = frames[i];
                r = frames[i + R];
                g = frames[i + G];
                b = frames[i + B];
                r2 = frames[i + R2];
                g2 = frames[i + G2];
                b2 = frames[i + B2];
                float t = (time - before) / (frames[i + ENTRIES] - before);
                r += (frames[i + ENTRIES + R] - r) * t;
                g += (frames[i + ENTRIES + G] - g) * t;
                b += (frames[i + ENTRIES + B] - b) * t;
                r2 += (frames[i + ENTRIES + R2] - r2) * t;
                g2 += (frames[i + ENTRIES + G2] - g2) * t;
                b2 += (frames[i + ENTRIES + B2] - b2) * t;
                break;
            case STEPPED:
                r = frames[i + R];
                g = frames[i + G];
                b = frames[i + B];
                r2 = frames[i + R2];
                g2 = frames[i + G2];
                b2 = frames[i + B2];
                break;
            default:
                r = GetBezierValue(time, i, R, curveType - BEZIER);
                g = GetBezierValue(time, i, G, curveType + BEZIER_SIZE - BEZIER);
                b = GetBezierValue(time, i, B, curveType + BEZIER_SIZE * 2 - BEZIER);
                r2 = GetBezierValue(time, i, R2, curveType + BEZIER_SIZE * 3 - BEZIER);
                g2 = GetBezierValue(time, i, G2, curveType + BEZIER_SIZE * 4 - BEZIER);
                b2 = GetBezierValue(time, i, B2, curveType + BEZIER_SIZE * 5 - BEZIER);
                break;
            }
 
            if (alpha == 1) {
                slot.r = r;
                slot.g = g;
                slot.b = b;
                slot.r2 = r2;
                slot.g2 = g2;
                slot.b2 = b2;
            } else {
                float br, bg, bb, br2, bg2, bb2;
                if (blend == MixBlend.Setup) {
                    SlotData setup = slot.data;
                    br = setup.r;
                    bg = setup.g;
                    bb = setup.b;
                    br2 = setup.r2;
                    bg2 = setup.g2;
                    bb2 = setup.b2;
                } else {
                    br = slot.r;
                    bg = slot.g;
                    bb = slot.b;
                    br2 = slot.r2;
                    bg2 = slot.g2;
                    bb2 = slot.b2;
                }
                slot.r = br + (r - br) * alpha;
                slot.g = bg + (g - bg) * alpha;
                slot.b = bb + (b - bb) * alpha;
                slot.r2 = br2 + (r2 - br2) * alpha;
                slot.g2 = bg2 + (g2 - bg2) * alpha;
                slot.b2 = bb2 + (b2 - bb2) * alpha;
            }
            slot.ClampColor();
            slot.ClampSecondColor();
        }
    }
 
    /// <summary>Changes a slot's <see cref="Slot.Attachment"/>.</summary>
    public class AttachmentTimeline : Timeline, ISlotTimeline {
        readonly int slotIndex;
        readonly string[] attachmentNames;
 
        public AttachmentTimeline (int frameCount, int slotIndex)
            : base(frameCount, (int)Property.Attachment + "|" + slotIndex) {
            this.slotIndex = slotIndex;
            attachmentNames = new String[frameCount];
        }
 
        public int SlotIndex {
            get {
                return slotIndex;
            }
        }
 
        /// <summary>The attachment name for each frame. May contain null values to clear the attachment. </summary>
        public string[] AttachmentNames {
            get {
                return attachmentNames;
            }
        }
 
        /// <summary>Sets the time and attachment name for the specified frame.</summary>
        /// <param name="frame">Between 0 and <c>frameCount</c>, inclusive.</param>
        /// <param name="time">The frame time in seconds.</param>
        public void SetFrame (int frame, float time, String attachmentName) {
            frames[frame] = time;
            attachmentNames[frame] = attachmentName;
        }
 
        public override void Apply (Skeleton skeleton, float lastTime, float time, ExposedList<Event> firedEvents, float alpha, MixBlend blend,
                            MixDirection direction) {
            Slot slot = skeleton.slots.Items[slotIndex];
            if (!slot.bone.active) return;
 
            if (direction == MixDirection.Out) {
                if (blend == MixBlend.Setup) SetAttachment(skeleton, slot, slot.data.attachmentName);
                return;
            }
 
            float[] frames = this.frames;
            if (time < frames[0]) {
                if (blend == MixBlend.Setup || blend == MixBlend.First) SetAttachment(skeleton, slot, slot.data.attachmentName);
                return;
            }
 
            SetAttachment(skeleton, slot, attachmentNames[Search(frames, time)]);
        }
 
        private void SetAttachment (Skeleton skeleton, Slot slot, string attachmentName) {
            slot.Attachment = attachmentName == null ? null : skeleton.GetAttachment(slotIndex, attachmentName);
        }
    }
 
    /// <summary>Changes a slot's <see cref="Slot.Deform"/> to deform a <see cref="VertexAttachment"/>.</summary>
    public class DeformTimeline : CurveTimeline, ISlotTimeline {
        readonly int slotIndex;
        readonly VertexAttachment attachment;
        internal float[][] vertices;
 
        public DeformTimeline (int frameCount, int bezierCount, int slotIndex, VertexAttachment attachment)
            : base(frameCount, bezierCount, (int)Property.Deform + "|" + slotIndex + "|" + attachment.Id) {
            this.slotIndex = slotIndex;
            this.attachment = attachment;
            vertices = new float[frameCount][];
        }
 
        public int SlotIndex {
            get {
                return slotIndex;
            }
        }
        /// <summary>The attachment that will be deformed.</summary>
        /// <seealso cref="VertexAttachment.TimelineAttachment"/>
        public VertexAttachment Attachment {
            get {
                return attachment;
            }
        }
 
        /// <summary>The vertices for each frame.</summary>
        public float[][] Vertices {
            get {
                return vertices;
            }
        }
 
        /// <summary>Sets the time and vertices for the specified frame.</summary>
        /// <param name="frame">Between 0 and <c>frameCount</c>, inclusive.</param>
        /// <param name="time">The frame time in seconds.</param>
        /// <param name="vertices">Vertex positions for an unweighted VertexAttachment, or deform offsets if it has weights.</param>
        public void SetFrame (int frame, float time, float[] vertices) {
            frames[frame] = time;
            this.vertices[frame] = vertices;
        }
 
        /// <param name="value1">Ignored (0 is used for a deform timeline).</param>
        /// <param name="value2">Ignored (1 is used for a deform timeline).</param>
        public void setBezier (int bezier, int frame, int value, float time1, float value1, float cx1, float cy1, float cx2,
            float cy2, float time2, float value2) {
            float[] curves = this.curves;
            int i = FrameCount + bezier * BEZIER_SIZE;
            if (value == 0) curves[frame] = BEZIER + i;
            float tmpx = (time1 - cx1 * 2 + cx2) * 0.03f, tmpy = cy2 * 0.03f - cy1 * 0.06f;
            float dddx = ((cx1 - cx2) * 3 - time1 + time2) * 0.006f, dddy = (cy1 - cy2 + 0.33333333f) * 0.018f;
            float ddx = tmpx * 2 + dddx, ddy = tmpy * 2 + dddy;
            float dx = (cx1 - time1) * 0.3f + tmpx + dddx * 0.16666667f, dy = cy1 * 0.3f + tmpy + dddy * 0.16666667f;
            float x = time1 + dx, y = dy;
            for (int n = i + BEZIER_SIZE; i < n; i += 2) {
                curves[i] = x;
                curves[i + 1] = y;
                dx += ddx;
                dy += ddy;
                ddx += dddx;
                ddy += dddy;
                x += dx;
                y += dy;
            }
        }
 
        /// <summary>Returns the interpolated percentage for the specified time.</summary>
        /// <param name="frame">The frame before <c>time</c>.</param>
        private float GetCurvePercent (float time, int frame) {
            float[] curves = this.curves;
            int i = (int)curves[frame];
            switch (i) {
            case LINEAR:
                float x = frames[frame];
                return (time - x) / (frames[frame + FrameEntries] - x);
            case STEPPED:
                return 0;
            }
            i -= BEZIER;
            if (curves[i] > time) {
                float x = frames[frame];
                return curves[i + 1] * (time - x) / (curves[i] - x);
            }
            int n = i + BEZIER_SIZE;
            for (i += 2; i < n; i += 2) {
                if (curves[i] >= time) {
                    float x = curves[i - 2], y = curves[i - 1];
                    return y + (time - x) / (curves[i] - x) * (curves[i + 1] - y);
                }
            }
            { // scope added to prevent compile error "float x and y declared in enclosing scope"
                float x = curves[n - 2], y = curves[n - 1];
                return y + (1 - y) * (time - x) / (frames[frame + FrameEntries] - x);
            }
        }
 
        override public void Apply (Skeleton skeleton, float lastTime, float time, ExposedList<Event> firedEvents, float alpha, MixBlend blend,
                                    MixDirection direction) {
 
            Slot slot = skeleton.slots.Items[slotIndex];
            if (!slot.bone.active) return;
            VertexAttachment vertexAttachment = slot.attachment as VertexAttachment;
            if (vertexAttachment == null || vertexAttachment.TimelineAttachment != attachment) return;
 
            ExposedList<float> deformArray = slot.deform;
            if (deformArray.Count == 0) blend = MixBlend.Setup;
 
            float[][] vertices = this.vertices;
            int vertexCount = vertices[0].Length;
 
            float[] deform;
 
            float[] frames = this.frames;
            if (time < frames[0]) {
                switch (blend) {
                case MixBlend.Setup:
                    deformArray.Clear();
                    return;
                case MixBlend.First:
                    if (alpha == 1) {
                        deformArray.Clear();
                        return;
                    }
 
                    // Ensure size and preemptively set count.
                    if (deformArray.Capacity < vertexCount) deformArray.Capacity = vertexCount;
                    deformArray.Count = vertexCount;
                    deform = deformArray.Items;
 
                    if (vertexAttachment.bones == null) {
                        // Unweighted vertex positions.
                        float[] setupVertices = vertexAttachment.vertices;
                        for (int i = 0; i < vertexCount; i++)
                            deform[i] += (setupVertices[i] - deform[i]) * alpha;
                    } else {
                        // Weighted deform offsets.
                        alpha = 1 - alpha;
                        for (int i = 0; i < vertexCount; i++)
                            deform[i] *= alpha;
                    }
                    return;
                }
                return;
            }
 
            // Ensure size and preemptively set count.
            if (deformArray.Capacity < vertexCount) deformArray.Capacity = vertexCount;
            deformArray.Count = vertexCount;
            deform = deformArray.Items;
 
            if (time >= frames[frames.Length - 1]) { // Time is after last frame.
                float[] lastVertices = vertices[frames.Length - 1];
                if (alpha == 1) {
                    if (blend == MixBlend.Add) {
                        if (vertexAttachment.bones == null) {
                            // Unweighted vertex positions, no alpha.
                            float[] setupVertices = vertexAttachment.vertices;
                            for (int i = 0; i < vertexCount; i++)
                                deform[i] += lastVertices[i] - setupVertices[i];
                        } else {
                            // Weighted deform offsets, no alpha.
                            for (int i = 0; i < vertexCount; i++)
                                deform[i] += lastVertices[i];
                        }
                    } else {
                        // Vertex positions or deform offsets, no alpha.
                        Array.Copy(lastVertices, 0, deform, 0, vertexCount);
                    }
                } else {
                    switch (blend) {
                    case MixBlend.Setup: {
                        if (vertexAttachment.bones == null) {
                            // Unweighted vertex positions, with alpha.
                            float[] setupVertices = vertexAttachment.vertices;
                            for (int i = 0; i < vertexCount; i++) {
                                float setup = setupVertices[i];
                                deform[i] = setup + (lastVertices[i] - setup) * alpha;
                            }
                        } else {
                            // Weighted deform offsets, with alpha.
                            for (int i = 0; i < vertexCount; i++)
                                deform[i] = lastVertices[i] * alpha;
                        }
                        break;
                    }
                    case MixBlend.First:
                    case MixBlend.Replace:
                        // Vertex positions or deform offsets, with alpha.
                        for (int i = 0; i < vertexCount; i++)
                            deform[i] += (lastVertices[i] - deform[i]) * alpha;
                        break;
                    case MixBlend.Add:
                        if (vertexAttachment.bones == null) {
                            // Unweighted vertex positions, no alpha.
                            float[] setupVertices = vertexAttachment.vertices;
                            for (int i = 0; i < vertexCount; i++)
                                deform[i] += (lastVertices[i] - setupVertices[i]) * alpha;
                        } else {
                            // Weighted deform offsets, alpha.
                            for (int i = 0; i < vertexCount; i++)
                                deform[i] += lastVertices[i] * alpha;
                        }
                        break;
                    }
                }
                return;
            }
 
            int frame = Search(frames, time);
            float percent = GetCurvePercent(time, frame);
            float[] prevVertices = vertices[frame];
            float[] nextVertices = vertices[frame + 1];
 
            if (alpha == 1) {
                if (blend == MixBlend.Add) {
                    if (vertexAttachment.bones == null) {
                        // Unweighted vertex positions, no alpha.
                        float[] setupVertices = vertexAttachment.vertices;
                        for (int i = 0; i < vertexCount; i++) {
                            float prev = prevVertices[i];
                            deform[i] += prev + (nextVertices[i] - prev) * percent - setupVertices[i];
                        }
                    } else {
                        // Weighted deform offsets, no alpha.
                        for (int i = 0; i < vertexCount; i++) {
                            float prev = prevVertices[i];
                            deform[i] += prev + (nextVertices[i] - prev) * percent;
                        }
                    }
                } else {
                    // Vertex positions or deform offsets, no alpha.
                    for (int i = 0; i < vertexCount; i++) {
                        float prev = prevVertices[i];
                        deform[i] = prev + (nextVertices[i] - prev) * percent;
                    }
                }
            } else {
                switch (blend) {
                case MixBlend.Setup: {
                    if (vertexAttachment.bones == null) {
                        // Unweighted vertex positions, with alpha.
                        float[] setupVertices = vertexAttachment.vertices;
                        for (int i = 0; i < vertexCount; i++) {
                            float prev = prevVertices[i], setup = setupVertices[i];
                            deform[i] = setup + (prev + (nextVertices[i] - prev) * percent - setup) * alpha;
                        }
                    } else {
                        // Weighted deform offsets, with alpha.
                        for (int i = 0; i < vertexCount; i++) {
                            float prev = prevVertices[i];
                            deform[i] = (prev + (nextVertices[i] - prev) * percent) * alpha;
                        }
                    }
                    break;
                }
                case MixBlend.First:
                case MixBlend.Replace: {
                    // Vertex positions or deform offsets, with alpha.
                    for (int i = 0; i < vertexCount; i++) {
                        float prev = prevVertices[i];
                        deform[i] += (prev + (nextVertices[i] - prev) * percent - deform[i]) * alpha;
                    }
                    break;
                }
                case MixBlend.Add:
                    if (vertexAttachment.bones == null) {
                        // Unweighted vertex positions, with alpha.
                        float[] setupVertices = vertexAttachment.vertices;
                        for (int i = 0; i < vertexCount; i++) {
                            float prev = prevVertices[i];
                            deform[i] += (prev + (nextVertices[i] - prev) * percent - setupVertices[i]) * alpha;
                        }
                    } else {
                        // Weighted deform offsets, with alpha.
                        for (int i = 0; i < vertexCount; i++) {
                            float prev = prevVertices[i];
                            deform[i] += (prev + (nextVertices[i] - prev) * percent) * alpha;
                        }
                    }
                    break;
                }
            }
        }
    }
 
    /// <summary>Fires an <see cref="Event"/> when specific animation times are reached.</summary>
    public class EventTimeline : Timeline {
        readonly static string[] propertyIds = { ((int)Property.Event).ToString() };
        readonly Event[] events;
 
        public EventTimeline (int frameCount)
            : base(frameCount, propertyIds) {
            events = new Event[frameCount];
        }
 
        /// <summary>The event for each frame.</summary>
        public Event[] Events {
            get {
                return events;
            }
        }
 
        /// <summary>Sets the time and event for the specified frame.</summary>
        /// <param name="frame">Between 0 and <c>frameCount</c>, inclusive.</param>
        public void SetFrame (int frame, Event e) {
            frames[frame] = e.time;
            events[frame] = e;
        }
 
        /// <summary>Fires events for frames &gt; <c>lastTime</c> and &lt;= <c>time</c>.</summary>
        public override void Apply (Skeleton skeleton, float lastTime, float time, ExposedList<Event> firedEvents, float alpha,
            MixBlend blend, MixDirection direction) {
 
            if (firedEvents == null) return;
 
            float[] frames = this.frames;
            int frameCount = frames.Length;
 
            if (lastTime > time) { // Apply after lastTime for looped animations.
                Apply(skeleton, lastTime, int.MaxValue, firedEvents, alpha, blend, direction);
                lastTime = -1f;
            } else if (lastTime >= frames[frameCount - 1]) // Last time is after last frame.
                return;
            if (time < frames[0]) return;
 
            int i;
            if (lastTime < frames[0])
                i = 0;
            else {
                i = Search(frames, lastTime) + 1;
                float frameTime = frames[i];
                while (i > 0) { // Fire multiple events with the same frame.
                    if (frames[i - 1] != frameTime) break;
                    i--;
                }
            }
            for (; i < frameCount && time >= frames[i]; i++)
                firedEvents.Add(events[i]);
        }
    }
 
    /// <summary>Changes a skeleton's <see cref="Skeleton.DrawOrder"/>.</summary>
    public class DrawOrderTimeline : Timeline {
        static readonly string[] propertyIds = { ((int)Property.DrawOrder).ToString() };
 
        readonly int[][] drawOrders;
 
        public DrawOrderTimeline (int frameCount)
            : base(frameCount, propertyIds) {
            drawOrders = new int[frameCount][];
        }
 
        /// <summary>The draw order for each frame. </summary>
        /// <seealso cref="Timeline.SetFrame(int, float, int[])"/>.
        public int[][] DrawOrders {
            get {
                return drawOrders;
            }
        }
 
        /// <summary>Sets the time and draw order for the specified frame.</summary>
        /// <param name="frame">Between 0 and <c>frameCount</c>, inclusive.</param>
        /// <param name="time">The frame time in seconds.</param>
        /// <param name="drawOrder">For each slot in <see cref="Skeleton.Slots"/>, the index of the slot in the new draw order. May be null to use
        ///                     setup pose draw order.</param>
        public void SetFrame (int frame, float time, int[] drawOrder) {
            frames[frame] = time;
            drawOrders[frame] = drawOrder;
        }
 
        public override void Apply (Skeleton skeleton, float lastTime, float time, ExposedList<Event> firedEvents, float alpha, MixBlend blend,
                            MixDirection direction) {
 
            if (direction == MixDirection.Out) {
                if (blend == MixBlend.Setup) Array.Copy(skeleton.slots.Items, 0, skeleton.drawOrder.Items, 0, skeleton.slots.Count);
                return;
            }
 
            float[] frames = this.frames;
            if (time < frames[0]) {
                if (blend == MixBlend.Setup || blend == MixBlend.First) Array.Copy(skeleton.slots.Items, 0, skeleton.drawOrder.Items, 0, skeleton.slots.Count);
                return;
            }
 
            int[] drawOrderToSetupIndex = drawOrders[Search(frames, time)];
            if (drawOrderToSetupIndex == null)
                Array.Copy(skeleton.slots.Items, 0, skeleton.drawOrder.Items, 0, skeleton.slots.Count);
            else {
                Slot[] slots = skeleton.slots.Items;
                Slot[] drawOrder = skeleton.drawOrder.Items;
                for (int i = 0, n = drawOrderToSetupIndex.Length; i < n; i++)
                    drawOrder[i] = slots[drawOrderToSetupIndex[i]];
            }
        }
    }
 
    /// <summary>Changes an IK constraint's <see cref="IkConstraint.Mix"/>, <see cref="IkConstraint.Softness"/>,
    /// <see cref="IkConstraint.BendDirection"/>, <see cref="IkConstraint.Stretch"/>, and <see cref="IkConstraint.Compress"/>.</summary>
    public class IkConstraintTimeline : CurveTimeline {
        public const int ENTRIES = 6;
        private const int MIX = 1, SOFTNESS = 2, BEND_DIRECTION = 3, COMPRESS = 4, STRETCH = 5;
 
        readonly int constraintIndex;
 
        public IkConstraintTimeline (int frameCount, int bezierCount, int ikConstraintIndex)
            : base(frameCount, bezierCount, (int)Property.IkConstraint + "|" + ikConstraintIndex) {
            this.constraintIndex = ikConstraintIndex;
        }
 
        public override int FrameEntries {
            get {
                return ENTRIES;
            }
        }
 
        /// <summary>The index of the IK constraint in <see cref="Skeleton.IkConstraints"/> that will be changed when this timeline is
        /// applied.</summary>
        public int IkConstraintIndex {
            get {
                return constraintIndex;
            }
        }
 
        /// <summary>Sets the time, mix, softness, bend direction, compress, and stretch for the specified frame.</summary>
        /// <param name="frame">Between 0 and <c>frameCount</c>, inclusive.</param>
        /// <param name="time">The frame time in seconds.</param>
        /// <param name="bendDirection">1 or -1.</param>
        public void SetFrame (int frame, float time, float mix, float softness, int bendDirection, bool compress,
            bool stretch) {
            frame *= ENTRIES;
            frames[frame] = time;
            frames[frame + MIX] = mix;
            frames[frame + SOFTNESS] = softness;
            frames[frame + BEND_DIRECTION] = bendDirection;
            frames[frame + COMPRESS] = compress ? 1 : 0;
            frames[frame + STRETCH] = stretch ? 1 : 0;
        }
 
        override public void Apply (Skeleton skeleton, float lastTime, float time, ExposedList<Event> firedEvents, float alpha, MixBlend blend,
                                    MixDirection direction) {
            IkConstraint constraint = skeleton.ikConstraints.Items[constraintIndex];
            if (!constraint.active) return;
 
            float[] frames = this.frames;
            if (time < frames[0]) {
                switch (blend) {
                case MixBlend.Setup:
                    constraint.mix = constraint.data.mix;
                    constraint.softness = constraint.data.softness;
                    constraint.bendDirection = constraint.data.bendDirection;
                    constraint.compress = constraint.data.compress;
                    constraint.stretch = constraint.data.stretch;
                    return;
                case MixBlend.First:
                    constraint.mix += (constraint.data.mix - constraint.mix) * alpha;
                    constraint.softness += (constraint.data.softness - constraint.softness) * alpha;
                    constraint.bendDirection = constraint.data.bendDirection;
                    constraint.compress = constraint.data.compress;
                    constraint.stretch = constraint.data.stretch;
                    return;
                }
                return;
            }
 
            float mix, softness;
            int i = Search(frames, time, ENTRIES), curveType = (int)curves[i / ENTRIES];
            switch (curveType) {
            case LINEAR:
                float before = frames[i];
                mix = frames[i + MIX];
                softness = frames[i + SOFTNESS];
                float t = (time - before) / (frames[i + ENTRIES] - before);
                mix += (frames[i + ENTRIES + MIX] - mix) * t;
                softness += (frames[i + ENTRIES + SOFTNESS] - softness) * t;
                break;
            case STEPPED:
                mix = frames[i + MIX];
                softness = frames[i + SOFTNESS];
                break;
            default:
                mix = GetBezierValue(time, i, MIX, curveType - BEZIER);
                softness = GetBezierValue(time, i, SOFTNESS, curveType + BEZIER_SIZE - BEZIER);
                break;
            }
 
            if (blend == MixBlend.Setup) {
                constraint.mix = constraint.data.mix + (mix - constraint.data.mix) * alpha;
                constraint.softness = constraint.data.softness + (softness - constraint.data.softness) * alpha;
                if (direction == MixDirection.Out) {
                    constraint.bendDirection = constraint.data.bendDirection;
                    constraint.compress = constraint.data.compress;
                    constraint.stretch = constraint.data.stretch;
                } else {
                    constraint.bendDirection = (int)frames[i + BEND_DIRECTION];
                    constraint.compress = frames[i + COMPRESS] != 0;
                    constraint.stretch = frames[i + STRETCH] != 0;
                }
            } else {
                constraint.mix += (mix - constraint.mix) * alpha;
                constraint.softness += (softness - constraint.softness) * alpha;
                if (direction == MixDirection.In) {
                    constraint.bendDirection = (int)frames[i + BEND_DIRECTION];
                    constraint.compress = frames[i + COMPRESS] != 0;
                    constraint.stretch = frames[i + STRETCH] != 0;
                }
            }
        }
    }
 
    ///    <summary>Changes a transform constraint's mixes.</summary>
    public class TransformConstraintTimeline : CurveTimeline {
        public const int ENTRIES = 7;
        private const int ROTATE = 1, X = 2, Y = 3, SCALEX = 4, SCALEY = 5, SHEARY = 6;
 
        readonly int constraintIndex;
 
        public TransformConstraintTimeline (int frameCount, int bezierCount, int transformConstraintIndex)
            : base(frameCount, bezierCount, (int)Property.TransformConstraint + "|" + transformConstraintIndex) {
            constraintIndex = transformConstraintIndex;
        }
 
        public override int FrameEntries {
            get {
                return ENTRIES;
            }
        }
 
        /// <summary>The index of the transform constraint in <see cref="Skeleton.TransformConstraints"/> that will be changed when this
        /// timeline is applied.</summary>
        public int TransformConstraintIndex {
            get {
                return constraintIndex;
            }
        }
 
        /// <summary>Sets the time, rotate mix, translate mix, scale mix, and shear mix for the specified frame.</summary>
        /// <param name="frame">Between 0 and <c>frameCount</c>, inclusive.</param>
        /// <param name="time">The frame time in seconds.</param>
        public void SetFrame (int frame, float time, float mixRotate, float mixX, float mixY, float mixScaleX, float mixScaleY,
            float mixShearY) {
            frame *= ENTRIES;
            frames[frame] = time;
            frames[frame + ROTATE] = mixRotate;
            frames[frame + X] = mixX;
            frames[frame + Y] = mixY;
            frames[frame + SCALEX] = mixScaleX;
            frames[frame + SCALEY] = mixScaleY;
            frames[frame + SHEARY] = mixShearY;
        }
 
        override public void Apply (Skeleton skeleton, float lastTime, float time, ExposedList<Event> firedEvents, float alpha, MixBlend blend,
                                    MixDirection direction) {
            TransformConstraint constraint = skeleton.transformConstraints.Items[constraintIndex];
            if (!constraint.active) return;
 
            float[] frames = this.frames;
            if (time < frames[0]) {
                TransformConstraintData data = constraint.data;
                switch (blend) {
                case MixBlend.Setup:
                    constraint.mixRotate = data.mixRotate;
                    constraint.mixX = data.mixX;
                    constraint.mixY = data.mixY;
                    constraint.mixScaleX = data.mixScaleX;
                    constraint.mixScaleY = data.mixScaleY;
                    constraint.mixShearY = data.mixShearY;
                    return;
                case MixBlend.First:
                    constraint.mixRotate += (data.mixRotate - constraint.mixRotate) * alpha;
                    constraint.mixX += (data.mixX - constraint.mixX) * alpha;
                    constraint.mixY += (data.mixY - constraint.mixY) * alpha;
                    constraint.mixScaleX += (data.mixScaleX - constraint.mixScaleX) * alpha;
                    constraint.mixScaleY += (data.mixScaleY - constraint.mixScaleY) * alpha;
                    constraint.mixShearY += (data.mixShearY - constraint.mixShearY) * alpha;
                    return;
                }
                return;
            }
 
            float rotate, x, y, scaleX, scaleY, shearY;
            GetCurveValue(out rotate, out x, out y, out scaleX, out scaleY, out shearY, time);
 
            if (blend == MixBlend.Setup) {
                TransformConstraintData data = constraint.data;
                constraint.mixRotate = data.mixRotate + (rotate - data.mixRotate) * alpha;
                constraint.mixX = data.mixX + (x - data.mixX) * alpha;
                constraint.mixY = data.mixY + (y - data.mixY) * alpha;
                constraint.mixScaleX = data.mixScaleX + (scaleX - data.mixScaleX) * alpha;
                constraint.mixScaleY = data.mixScaleY + (scaleY - data.mixScaleY) * alpha;
                constraint.mixShearY = data.mixShearY + (shearY - data.mixShearY) * alpha;
            } else {
                constraint.mixRotate += (rotate - constraint.mixRotate) * alpha;
                constraint.mixX += (x - constraint.mixX) * alpha;
                constraint.mixY += (y - constraint.mixY) * alpha;
                constraint.mixScaleX += (scaleX - constraint.mixScaleX) * alpha;
                constraint.mixScaleY += (scaleY - constraint.mixScaleY) * alpha;
                constraint.mixShearY += (shearY - constraint.mixShearY) * alpha;
            }
        }
 
        public void GetCurveValue (out float rotate, out float x, out float y,
            out float scaleX, out float scaleY, out float shearY, float time) {
 
            float[] frames = this.frames;
            int i = Search(frames, time, ENTRIES), curveType = (int)curves[i / ENTRIES];
            switch (curveType) {
            case LINEAR:
                float before = frames[i];
                rotate = frames[i + ROTATE];
                x = frames[i + X];
                y = frames[i + Y];
                scaleX = frames[i + SCALEX];
                scaleY = frames[i + SCALEY];
                shearY = frames[i + SHEARY];
                float t = (time - before) / (frames[i + ENTRIES] - before);
                rotate += (frames[i + ENTRIES + ROTATE] - rotate) * t;
                x += (frames[i + ENTRIES + X] - x) * t;
                y += (frames[i + ENTRIES + Y] - y) * t;
                scaleX += (frames[i + ENTRIES + SCALEX] - scaleX) * t;
                scaleY += (frames[i + ENTRIES + SCALEY] - scaleY) * t;
                shearY += (frames[i + ENTRIES + SHEARY] - shearY) * t;
                break;
            case STEPPED:
                rotate = frames[i + ROTATE];
                x = frames[i + X];
                y = frames[i + Y];
                scaleX = frames[i + SCALEX];
                scaleY = frames[i + SCALEY];
                shearY = frames[i + SHEARY];
                break;
            default:
                rotate = GetBezierValue(time, i, ROTATE, curveType - BEZIER);
                x = GetBezierValue(time, i, X, curveType + BEZIER_SIZE - BEZIER);
                y = GetBezierValue(time, i, Y, curveType + BEZIER_SIZE * 2 - BEZIER);
                scaleX = GetBezierValue(time, i, SCALEX, curveType + BEZIER_SIZE * 3 - BEZIER);
                scaleY = GetBezierValue(time, i, SCALEY, curveType + BEZIER_SIZE * 4 - BEZIER);
                shearY = GetBezierValue(time, i, SHEARY, curveType + BEZIER_SIZE * 5 - BEZIER);
                break;
            }
        }
    }
 
    /// <summary>Changes a path constraint's <see cref="PathConstraint.Position"/>.</summary>
    public class PathConstraintPositionTimeline : CurveTimeline1 {
        readonly int constraintIndex;
 
        public PathConstraintPositionTimeline (int frameCount, int bezierCount, int pathConstraintIndex)
            : base(frameCount, bezierCount, (int)Property.PathConstraintPosition + "|" + pathConstraintIndex) {
            this.constraintIndex = pathConstraintIndex;
        }
 
        /// <summary>The index of the path constraint slot in <see cref="Skeleton.PathConstraints"/> that will be changed when this timeline
        /// is applied.</summary>
        public int PathConstraintIndex {
            get {
                return constraintIndex;
            }
        }
 
        override public void Apply (Skeleton skeleton, float lastTime, float time, ExposedList<Event> firedEvents, float alpha, MixBlend blend,
                                    MixDirection direction) {
            PathConstraint constraint = skeleton.pathConstraints.Items[constraintIndex];
            if (constraint.active)
                constraint.position = GetAbsoluteValue(time, alpha, blend, constraint.position, constraint.data.position);
        }
    }
 
    /// <summary>Changes a path constraint's <see cref="PathConstraint.Spacing"/>.</summary>
    public class PathConstraintSpacingTimeline : CurveTimeline1 {
        readonly int constraintIndex;
 
        public PathConstraintSpacingTimeline (int frameCount, int bezierCount, int pathConstraintIndex)
            : base(frameCount, bezierCount, (int)Property.PathConstraintSpacing + "|" + pathConstraintIndex) {
            constraintIndex = pathConstraintIndex;
        }
 
        /// <summary>The index of the path constraint in <see cref="Skeleton.PathConstraints"/> that will be changed when this timeline
        /// is applied.</summary>
        public int PathConstraintIndex {
            get {
                return constraintIndex;
            }
        }
 
        override public void Apply (Skeleton skeleton, float lastTime, float time, ExposedList<Event> events, float alpha, MixBlend blend,
                                    MixDirection direction) {
 
            PathConstraint constraint = skeleton.pathConstraints.Items[constraintIndex];
            if (constraint.active)
                constraint.spacing = GetAbsoluteValue(time, alpha, blend, constraint.spacing, constraint.data.spacing);
        }
    }
 
    /// <summary>Changes a path constraint's <see cref="PathConstraint.MixRotate"/>, <see cref="PathConstraint.MixX"/>, and
    /// <see cref="PathConstraint.MixY"/>.</summary>
    public class PathConstraintMixTimeline : CurveTimeline {
        public const int ENTRIES = 4;
        private const int ROTATE = 1, X = 2, Y = 3;
 
        readonly int constraintIndex;
 
        public PathConstraintMixTimeline (int frameCount, int bezierCount, int pathConstraintIndex)
            : base(frameCount, bezierCount, (int)Property.PathConstraintMix + "|" + pathConstraintIndex) {
            constraintIndex = pathConstraintIndex;
        }
 
        public override int FrameEntries {
            get { return ENTRIES; }
        }
 
        /// <summary>The index of the path constraint slot in <see cref="Skeleton.PathConstraints"/> that will be changed when this timeline
        /// is applied.</summary>
        public int PathConstraintIndex {
            get {
                return constraintIndex;
            }
        }
 
        /// <summary>Sets the time and color for the specified frame.</summary>
        /// <param name="frame">Between 0 and <c>frameCount</c>, inclusive.</param>
        /// <param name="time">The frame time in seconds.</param>
        public void SetFrame (int frame, float time, float mixRotate, float mixX, float mixY) {
            frame <<= 2;
            frames[frame] = time;
            frames[frame + ROTATE] = mixRotate;
            frames[frame + X] = mixX;
            frames[frame + Y] = mixY;
        }
 
        override public void Apply (Skeleton skeleton, float lastTime, float time, ExposedList<Event> firedEvents, float alpha, MixBlend blend,
                                    MixDirection direction) {
            PathConstraint constraint = skeleton.pathConstraints.Items[constraintIndex];
            if (!constraint.active) return;
 
            float[] frames = this.frames;
            if (time < frames[0]) {
                switch (blend) {
                case MixBlend.Setup:
                    constraint.mixRotate = constraint.data.mixRotate;
                    constraint.mixX = constraint.data.mixX;
                    constraint.mixY = constraint.data.mixY;
                    return;
                case MixBlend.First:
                    constraint.mixRotate += (constraint.data.mixRotate - constraint.mixRotate) * alpha;
                    constraint.mixX += (constraint.data.mixX - constraint.mixX) * alpha;
                    constraint.mixY += (constraint.data.mixY - constraint.mixY) * alpha;
                    return;
                }
                return;
            }
 
            float rotate, x, y;
            int i = Search(frames, time, ENTRIES), curveType = (int)curves[i >> 2];
            switch (curveType) {
            case LINEAR:
                float before = frames[i];
                rotate = frames[i + ROTATE];
                x = frames[i + X];
                y = frames[i + Y];
                float t = (time - before) / (frames[i + ENTRIES] - before);
                rotate += (frames[i + ENTRIES + ROTATE] - rotate) * t;
                x += (frames[i + ENTRIES + X] - x) * t;
                y += (frames[i + ENTRIES + Y] - y) * t;
                break;
            case STEPPED:
                rotate = frames[i + ROTATE];
                x = frames[i + X];
                y = frames[i + Y];
                break;
            default:
                rotate = GetBezierValue(time, i, ROTATE, curveType - BEZIER);
                x = GetBezierValue(time, i, X, curveType + BEZIER_SIZE - BEZIER);
                y = GetBezierValue(time, i, Y, curveType + BEZIER_SIZE * 2 - BEZIER);
                break;
            }
 
            if (blend == MixBlend.Setup) {
                PathConstraintData data = constraint.data;
                constraint.mixRotate = data.mixRotate + (rotate - data.mixRotate) * alpha;
                constraint.mixX = data.mixX + (x - data.mixX) * alpha;
                constraint.mixY = data.mixY + (y - data.mixY) * alpha;
            } else {
                constraint.mixRotate += (rotate - constraint.mixRotate) * alpha;
                constraint.mixX += (x - constraint.mixX) * alpha;
                constraint.mixY += (y - constraint.mixY) * alpha;
            }
        }
    }
 
    /// <summary>The base class for most <see cref="PhysicsConstraint"/> timelines.</summary>
    public abstract class PhysicsConstraintTimeline : CurveTimeline1 {
        readonly int constraintIndex;
 
        /// <param name="physicsConstraintIndex">-1 for all physics constraints in the skeleton.</param>
        public PhysicsConstraintTimeline (int frameCount, int bezierCount, int physicsConstraintIndex, Property property)
            : base(frameCount, bezierCount, (int)property + "|" + physicsConstraintIndex) {
 
            constraintIndex = physicsConstraintIndex;
        }
 
        /// <summary>The index of the physics constraint in <see cref="Skeleton.PhysicsConstraints"/> that will be changed when this timeline
        /// is applied, or -1 if all physics constraints in the skeleton will be changed.</summary>
        public int PhysicsConstraintIndex {
            get {
                return constraintIndex;
            }
        }
 
        override public void Apply (Skeleton skeleton, float lastTime, float time, ExposedList<Event> firedEvents, float alpha, MixBlend blend,
                                    MixDirection direction) {
            PhysicsConstraint constraint;
            if (constraintIndex == -1) {
                float value = time >= frames[0] ? GetCurveValue(time) : 0;
 
                PhysicsConstraint[] constraints = skeleton.physicsConstraints.Items;
                for (int i = 0, n = skeleton.physicsConstraints.Count; i < n; i++) {
                    constraint = (PhysicsConstraint)constraints[i];
                    if (constraint.active && Global(constraint.data))
                        Set(constraint, GetAbsoluteValue(time, alpha, blend, Get(constraint), Setup(constraint), value));
                }
            } else {
                constraint = skeleton.physicsConstraints.Items[constraintIndex];
                if (constraint.active) Set(constraint, GetAbsoluteValue(time, alpha, blend, Get(constraint), Setup(constraint)));
            }
        }
 
        abstract protected float Setup (PhysicsConstraint constraint);
 
        abstract protected float Get (PhysicsConstraint constraint);
 
        abstract protected void Set (PhysicsConstraint constraint, float value);
 
        abstract protected bool Global (PhysicsConstraintData constraint);
    }
 
    /// <summary>Changes a physics constraint's <see cref="PhysicsConstraint.Inertia"/>.</summary>
    public class PhysicsConstraintInertiaTimeline : PhysicsConstraintTimeline {
        public PhysicsConstraintInertiaTimeline (int frameCount, int bezierCount, int physicsConstraintIndex)
            : base(frameCount, bezierCount, physicsConstraintIndex, Property.PhysicsConstraintInertia) {
        }
 
        override protected float Setup (PhysicsConstraint constraint) {
            return constraint.data.inertia;
        }
 
        override protected float Get (PhysicsConstraint constraint) {
            return constraint.inertia;
        }
 
        override protected void Set (PhysicsConstraint constraint, float value) {
            constraint.inertia = value;
        }
 
        override protected bool Global (PhysicsConstraintData constraint) {
            return constraint.inertiaGlobal;
        }
    }
 
    /// <summary>Changes a physics constraint's <see cref="PhysicsConstraint.Strength"/>.</summary>
    public class PhysicsConstraintStrengthTimeline : PhysicsConstraintTimeline {
        public PhysicsConstraintStrengthTimeline (int frameCount, int bezierCount, int physicsConstraintIndex)
            : base(frameCount, bezierCount, physicsConstraintIndex, Property.PhysicsConstraintStrength) {
        }
 
        override protected float Setup (PhysicsConstraint constraint) {
            return constraint.data.strength;
        }
 
        override protected float Get (PhysicsConstraint constraint) {
            return constraint.strength;
        }
 
        override protected void Set (PhysicsConstraint constraint, float value) {
            constraint.strength = value;
        }
 
        override protected bool Global (PhysicsConstraintData constraint) {
            return constraint.strengthGlobal;
        }
    }
 
    /// <summary>Changes a physics constraint's <see cref="PhysicsConstraint.Damping"/>.</summary>
    public class PhysicsConstraintDampingTimeline : PhysicsConstraintTimeline {
        public PhysicsConstraintDampingTimeline (int frameCount, int bezierCount, int physicsConstraintIndex)
            : base(frameCount, bezierCount, physicsConstraintIndex, Property.PhysicsConstraintDamping) {
        }
 
        override protected float Setup (PhysicsConstraint constraint) {
            return constraint.data.damping;
        }
 
        override protected float Get (PhysicsConstraint constraint) {
            return constraint.damping;
        }
 
        override protected void Set (PhysicsConstraint constraint, float value) {
            constraint.damping = value;
        }
 
        override protected bool Global (PhysicsConstraintData constraint) {
            return constraint.dampingGlobal;
        }
    }
 
    /// <summary>Changes a physics constraint's <see cref="PhysicsConstraint.MassInverse"/>. The timeline values are not inverted.</summary>
    public class PhysicsConstraintMassTimeline : PhysicsConstraintTimeline {
        public PhysicsConstraintMassTimeline (int frameCount, int bezierCount, int physicsConstraintIndex)
            : base(frameCount, bezierCount, physicsConstraintIndex, Property.PhysicsConstraintMass) {
        }
 
        override protected float Setup (PhysicsConstraint constraint) {
            return 1 / constraint.data.massInverse;
        }
 
        override protected float Get (PhysicsConstraint constraint) {
            return 1 / constraint.massInverse;
        }
 
        override protected void Set (PhysicsConstraint constraint, float value) {
            constraint.massInverse = 1 / value;
        }
 
        override protected bool Global (PhysicsConstraintData constraint) {
            return constraint.massGlobal;
        }
    }
 
    /// <summary>Changes a physics constraint's <see cref="PhysicsConstraint.Wind"/>.</summary>
    public class PhysicsConstraintWindTimeline : PhysicsConstraintTimeline {
        public PhysicsConstraintWindTimeline (int frameCount, int bezierCount, int physicsConstraintIndex)
            : base(frameCount, bezierCount, physicsConstraintIndex, Property.PhysicsConstraintWind) {
        }
 
        override protected float Setup (PhysicsConstraint constraint) {
            return constraint.data.wind;
        }
 
        override protected float Get (PhysicsConstraint constraint) {
            return constraint.wind;
        }
 
        override protected void Set (PhysicsConstraint constraint, float value) {
            constraint.wind = value;
        }
 
        override protected bool Global (PhysicsConstraintData constraint) {
            return constraint.windGlobal;
        }
    }
 
    /// <summary>Changes a physics constraint's <see cref="PhysicsConstraint.Gravity"/>.</summary>
    public class PhysicsConstraintGravityTimeline : PhysicsConstraintTimeline {
        public PhysicsConstraintGravityTimeline (int frameCount, int bezierCount, int physicsConstraintIndex)
            : base(frameCount, bezierCount, physicsConstraintIndex, Property.PhysicsConstraintGravity) {
        }
 
        override protected float Setup (PhysicsConstraint constraint) {
            return constraint.data.gravity;
        }
 
        override protected float Get (PhysicsConstraint constraint) {
            return constraint.gravity;
        }
 
        override protected void Set (PhysicsConstraint constraint, float value) {
            constraint.gravity = value;
        }
 
        override protected bool Global (PhysicsConstraintData constraint) {
            return constraint.gravityGlobal;
        }
    }
 
    /// <summary>Changes a physics constraint's <see cref="PhysicsConstraint.Mix"/>.</summary>
    public class PhysicsConstraintMixTimeline : PhysicsConstraintTimeline {
        public PhysicsConstraintMixTimeline (int frameCount, int bezierCount, int physicsConstraintIndex)
            : base(frameCount, bezierCount, physicsConstraintIndex, Property.PhysicsConstraintMix) {
        }
 
        override protected float Setup (PhysicsConstraint constraint) {
            return constraint.data.mix;
        }
 
        override protected float Get (PhysicsConstraint constraint) {
            return constraint.mix;
        }
 
        override protected void Set (PhysicsConstraint constraint, float value) {
            constraint.mix = value;
        }
 
        override protected bool Global (PhysicsConstraintData constraint) {
            return constraint.mixGlobal;
        }
    }
 
    /// <summary>Resets a physics constraint when specific animation times are reached.</summary>
    public class PhysicsConstraintResetTimeline : Timeline {
        static readonly string[] propertyIds = { ((int)Property.PhysicsConstraintReset).ToString() };
 
        readonly int constraintIndex;
 
        /// <param name="physicsConstraintIndex">-1 for all physics constraints in the skeleton.</param>
        public PhysicsConstraintResetTimeline (int frameCount, int physicsConstraintIndex)
            : base(frameCount, propertyIds) {
            constraintIndex = physicsConstraintIndex;
        }
 
        /// <summary>The index of the physics constraint in <see cref="Skeleton.PhysicsConstraints"/> that will be reset when this timeline is
        /// applied, or -1 if all physics constraints in the skeleton will be reset.</summary>
        public int PhysicsConstraintIndex {
            get {
                return constraintIndex;
            }
        }
 
        override public int FrameCount {
            get { return frames.Length; }
        }
 
        /// <summary>Sets the time for the specified frame.</summary>
        /// <param name="frame">Between 0 and <c>frameCount</c>, inclusive.</param>
        public void SetFrame (int frame, float time) {
            frames[frame] = time;
        }
 
        /// <summary>Resets the physics constraint when frames > <c>lastTime</c> and <= <c>time</c>.</summary>
        override public void Apply (Skeleton skeleton, float lastTime, float time, ExposedList<Event> firedEvents, float alpha, MixBlend blend,
                                    MixDirection direction) {
 
            PhysicsConstraint constraint = null;
            if (constraintIndex != -1) {
                constraint = skeleton.physicsConstraints.Items[constraintIndex];
                if (!constraint.active) return;
            }
 
            float[] frames = this.frames;
 
            if (lastTime > time) { // Apply after lastTime for looped animations.
                Apply(skeleton, lastTime, int.MaxValue, null, alpha, blend, direction);
                lastTime = -1f;
            } else if (lastTime >= frames[frames.Length - 1]) // Last time is after last frame.
                return;
            if (time < frames[0]) return;
 
            if (lastTime < frames[0] || time >= frames[Search(frames, lastTime) + 1]) {
                if (constraint != null)
                    constraint.Reset();
                else {
                    PhysicsConstraint[] constraints = skeleton.physicsConstraints.Items;
                    for (int i = 0, n = skeleton.physicsConstraints.Count; i < n; i++) {
                        constraint = (PhysicsConstraint)constraints[i];
                        if (constraint.active) constraint.Reset();
                    }
                }
            }
        }
    }
 
 
    /// <summary>Changes a slot's <see cref="Slot.SequenceIndex"/> for an attachment's <see cref="Sequence"/>.</summary>
    public class SequenceTimeline : Timeline, ISlotTimeline {
        public const int ENTRIES = 3;
        private const int MODE = 1, DELAY = 2;
 
        readonly int slotIndex;
        readonly IHasTextureRegion attachment;
 
        public SequenceTimeline (int frameCount, int slotIndex, Attachment attachment)
            : base(frameCount, (int)Property.Sequence + "|" + slotIndex + "|" + ((IHasTextureRegion)attachment).Sequence.Id) {
            this.slotIndex = slotIndex;
            this.attachment = (IHasTextureRegion)attachment;
        }
 
        public override int FrameEntries {
            get { return ENTRIES; }
        }
 
        public int SlotIndex {
            get {
                return slotIndex;
            }
        }
        public Attachment Attachment {
            get {
                return (Attachment)attachment;
            }
        }
 
        /// <summary>Sets the time, mode, index, and frame time for the specified frame.</summary>
        /// <param name="frame">Between 0 and <c>frameCount</c>, inclusive.</param>
        /// <param name="time">Seconds between frames.</param>
        public void SetFrame (int frame, float time, SequenceMode mode, int index, float delay) {
            frame *= ENTRIES;
            frames[frame] = time;
            frames[frame + MODE] = (int)mode | (index << 4);
            frames[frame + DELAY] = delay;
        }
 
        override public void Apply (Skeleton skeleton, float lastTime, float time, ExposedList<Event> firedEvents, float alpha, MixBlend blend,
            MixDirection direction) {
 
            Slot slot = skeleton.slots.Items[slotIndex];
            if (!slot.bone.active) return;
            Attachment slotAttachment = slot.attachment;
            if (slotAttachment != attachment) {
                VertexAttachment vertexAttachment = slotAttachment as VertexAttachment;
                if ((vertexAttachment == null)
                    || vertexAttachment.TimelineAttachment != attachment) return;
            }
            Sequence sequence = ((IHasTextureRegion)slotAttachment).Sequence;
            if (sequence == null) return;
 
            if (direction == MixDirection.Out) {
                if (blend == MixBlend.Setup) slot.SequenceIndex = -1;
                return;
            }
 
            float[] frames = this.frames;
            if (time < frames[0]) {
                if (blend == MixBlend.Setup || blend == MixBlend.First) slot.SequenceIndex = -1;
                return;
            }
 
            int i = Search(frames, time, ENTRIES);
            float before = frames[i];
            int modeAndIndex = (int)frames[i + MODE];
            float delay = frames[i + DELAY];
 
            int index = modeAndIndex >> 4, count = sequence.Regions.Length;
            SequenceMode mode = (SequenceMode)(modeAndIndex & 0xf);
            if (mode != SequenceMode.Hold) {
                index += (int)((time - before) / delay + 0.0001f);
                switch (mode) {
                case SequenceMode.Once:
                    index = Math.Min(count - 1, index);
                    break;
                case SequenceMode.Loop:
                    index %= count;
                    break;
                case SequenceMode.Pingpong: {
                    int n = (count << 1) - 2;
                    index = n == 0 ? 0 : index % n;
                    if (index >= count) index = n - index;
                    break;
                }
                case SequenceMode.OnceReverse:
                    index = Math.Max(count - 1 - index, 0);
                    break;
                case SequenceMode.LoopReverse:
                    index = count - 1 - (index % count);
                    break;
                case SequenceMode.PingpongReverse: {
                    int n = (count << 1) - 2;
                    index = n == 0 ? 0 : (index + count - 1) % n;
                    if (index >= count) index = n - index;
                    break;
                } // end case
                }
            }
            slot.SequenceIndex = index;
        }
    }
}