/
Animation.java
2663 lines (2327 loc) · 93 KB
/
Animation.java
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
/******************************************************************************
* 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.
*****************************************************************************/
package com.esotericsoftware.spine;
import static com.esotericsoftware.spine.Animation.MixBlend.*;
import static com.esotericsoftware.spine.Animation.MixDirection.*;
import static com.esotericsoftware.spine.utils.SpineUtils.*;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.FloatArray;
import com.badlogic.gdx.utils.Null;
import com.badlogic.gdx.utils.ObjectSet;
import com.esotericsoftware.spine.BoneData.Inherit;
import com.esotericsoftware.spine.attachments.Attachment;
import com.esotericsoftware.spine.attachments.HasTextureRegion;
import com.esotericsoftware.spine.attachments.Sequence;
import com.esotericsoftware.spine.attachments.Sequence.SequenceMode;
import com.esotericsoftware.spine.attachments.VertexAttachment;
/** Stores a list of timelines to animate a skeleton's pose over time. */
public class Animation {
final String name;
Array<Timeline> timelines;
final ObjectSet<String> timelineIds;
float duration;
public Animation (String name, Array<Timeline> timelines, float duration) {
if (name == null) throw new IllegalArgumentException("name cannot be null.");
this.name = name;
this.duration = duration;
timelineIds = new ObjectSet(timelines.size);
setTimelines(timelines);
}
/** If the returned array or the timelines it contains are modified, {@link #setTimelines(Array)} must be called. */
public Array<Timeline> getTimelines () {
return timelines;
}
public void setTimelines (Array<Timeline> timelines) {
if (timelines == null) throw new IllegalArgumentException("timelines cannot be null.");
this.timelines = timelines;
int n = timelines.size;
timelineIds.clear(n);
Object[] items = timelines.items;
for (int i = 0; i < n; i++)
timelineIds.addAll(((Timeline)items[i]).getPropertyIds());
}
/** Returns true if this animation contains a timeline with any of the specified property IDs. */
public boolean hasTimeline (String[] propertyIds) {
for (String id : propertyIds)
if (timelineIds.contains(id)) return true;
return false;
}
/** 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. */
public float getDuration () {
return duration;
}
public void setDuration (float duration) {
this.duration = duration;
}
/** Applies the animation's timelines to the specified skeleton.
* <p>
* See Timeline {@link Timeline#apply(Skeleton, float, float, Array, float, MixBlend, MixDirection)}.
* @param 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 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 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 {@link #getDuration()} and <code>loop</code> is
* true then the animation will repeat, else the last frame will be applied.
* @param loop If true, the animation repeats after the {@link #getDuration()}.
* @param 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 alpha 0 applies the current or setup values (depending on <code>blend</code>). 1 applies the timeline values. Between
* 0 and 1 applies values between the current or setup values and the timeline values. By adjusting
* <code>alpha</code> over time, an animation can be mixed in or out. <code>alpha</code> can also be useful to apply
* animations on top of each other (layering).
* @param blend Controls how mixing is applied when <code>alpha</code> < 1.
* @param direction Indicates whether the timelines are mixing in or out. Used by timelines which perform instant transitions,
* such as {@link DrawOrderTimeline} or {@link AttachmentTimeline}. */
public void apply (Skeleton skeleton, float lastTime, float time, boolean loop, @Null Array<Event> events, float alpha,
MixBlend blend, MixDirection direction) {
if (skeleton == null) throw new IllegalArgumentException("skeleton cannot be null.");
if (loop && duration != 0) {
time %= duration;
if (lastTime > 0) lastTime %= duration;
}
Object[] timelines = this.timelines.items;
for (int i = 0, n = this.timelines.size; i < n; i++)
((Timeline)timelines[i]).apply(skeleton, lastTime, time, events, alpha, blend, direction);
}
/** The animation's name, which is unique across all animations in the skeleton. */
public String getName () {
return name;
}
public String toString () {
return name;
}
/** Controls how timeline values are mixed with setup pose values or current pose values when a timeline is applied with
* <code>alpha</code> < 1.
* <p>
* See Timeline {@link Timeline#apply(Skeleton, float, float, Array, float, MixBlend, MixDirection)}. */
static public enum MixBlend {
/** Transitions from the setup value to the timeline value (the current value is not used). Before the first frame, the
* setup value is set. */
setup,
/** 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 {@link DrawOrderTimeline} or
* {@link AttachmentTimeline}, use the setup value before the first frame.
* <p>
* <code>first</code> is intended for the first animations applied, not for animations layered on top of those. */
first,
/** 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).
* <p>
* <code>replace</code> is intended for animations layered on top of others, not for the first animations applied. */
replace,
/** 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).
* <p>
* <code>add</code> 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. */
add
}
/** Indicates whether a timeline's <code>alpha</code> 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.
* <p>
* See Timeline {@link Timeline#apply(Skeleton, float, float, Array, float, MixBlend, MixDirection)}. */
static public enum MixDirection {
in, out
}
static private enum Property {
rotate, 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
}
/** The base class for all timelines. */
static public abstract class Timeline {
private final String[] propertyIds;
final float[] frames;
/** @param propertyIds Unique identifiers for the properties the timeline modifies. */
public Timeline (int frameCount, String... propertyIds) {
if (propertyIds == null) throw new IllegalArgumentException("propertyIds cannot be null.");
this.propertyIds = propertyIds;
frames = new float[frameCount * getFrameEntries()];
}
/** Uniquely encodes both the type of this timeline and the skeleton properties that it affects. */
public String[] getPropertyIds () {
return propertyIds;
}
/** The time in seconds and any other values for each frame. */
public float[] getFrames () {
return frames;
}
/** The number of entries stored per frame. */
public int getFrameEntries () {
return 1;
}
/** The number of frames for this timeline. */
public int getFrameCount () {
return frames.length / getFrameEntries();
}
public float getDuration () {
return frames[frames.length - getFrameEntries()];
}
/** Applies this timeline to the skeleton.
* @param skeleton The skeleton to which the timeline is being applied. This provides access to the bones, slots, and other
* skeleton components that the timeline may change.
* @param lastTime The last time in seconds this timeline was applied. Timelines such as {@link EventTimeline} trigger only
* at specific times rather than every frame. In that case, the timeline triggers everything between
* <code>lastTime</code> (exclusive) and <code>time</code> (inclusive). Pass -1 the first time an animation is
* applied to ensure frame 0 is triggered.
* @param 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 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 alpha 0 applies the current or setup value (depending on <code>blend</code>). 1 applies the timeline value.
* Between 0 and 1 applies a value between the current or setup value and the timeline value. By adjusting
* <code>alpha</code> over time, an animation can be mixed in or out. <code>alpha</code> can also be useful to
* apply animations on top of each other (layering).
* @param blend Controls how mixing is applied when <code>alpha</code> < 1.
* @param direction Indicates whether the timeline is mixing in or out. Used by timelines which perform instant transitions,
* such as {@link DrawOrderTimeline} or {@link AttachmentTimeline}, and others such as {@link ScaleTimeline}. */
abstract public void apply (Skeleton skeleton, float lastTime, float time, @Null Array<Event> events, float alpha,
MixBlend blend, MixDirection direction);
/** Linear search using a stride of 1.
* @param time Must be >= the first value in <code>frames</code>.
* @return The index of the first value <= <code>time</code>. */
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;
}
/** Linear search using the specified stride.
* @param time Must be >= the first value in <code>frames</code>.
* @return The index of the first value <= <code>time</code>. */
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;
}
}
/** An interface for timelines which change the property of a bone. */
static public interface BoneTimeline {
/** The index of the bone in {@link Skeleton#getBones()} that will be changed when this timeline is applied. */
public int getBoneIndex ();
}
/** An interface for timelines which change the property of a slot. */
static public interface SlotTimeline {
/** The index of the slot in {@link Skeleton#getSlots()} that will be changed when this timeline is applied. */
public int getSlotIndex ();
}
/** The base class for timelines that interpolate between frame values using stepped, linear, or a Bezier curve. */
static public abstract class CurveTimeline extends Timeline {
static public final int LINEAR = 0, STEPPED = 1, BEZIER = 2, BEZIER_SIZE = 18;
float[] curves;
/** @param bezierCount The maximum number of Bezier curves. See {@link #shrink(int)}.
* @param propertyIds Unique identifiers for the properties the timeline modifies. */
public CurveTimeline (int frameCount, int bezierCount, String... propertyIds) {
super(frameCount, propertyIds);
curves = new float[frameCount + bezierCount * BEZIER_SIZE];
curves[frameCount - 1] = STEPPED;
}
/** Sets the specified frame to linear interpolation.
* @param frame Between 0 and <code>frameCount - 1</code>, inclusive. */
public void setLinear (int frame) {
curves[frame] = LINEAR;
}
/** Sets the specified frame to stepped interpolation.
* @param frame Between 0 and <code>frameCount - 1</code>, inclusive. */
public void setStepped (int frame) {
curves[frame] = STEPPED;
}
/** Returns the interpolation type for the specified frame.
* @param frame Between 0 and <code>frameCount - 1</code>, inclusive.
* @return {@link #LINEAR}, {@link #STEPPED}, or {@link #BEZIER} + the index of the Bezier segments. */
public int getCurveType (int frame) {
return (int)curves[frame];
}
/** Shrinks the storage for Bezier curves, for use when <code>bezierCount</code> (specified in the constructor) was larger
* than the actual number of Bezier curves. */
public void shrink (int bezierCount) {
int size = getFrameCount() + bezierCount * BEZIER_SIZE;
if (curves.length > size) {
float[] newCurves = new float[size];
arraycopy(curves, 0, newCurves, 0, size);
curves = newCurves;
}
}
/** Stores the segments for the specified Bezier curve. For timelines that modify multiple values, there may be more than
* one curve per frame.
* @param bezier The ordinal of this Bezier curve for this timeline, between 0 and <code>bezierCount - 1</code> (specified
* in the constructor), inclusive.
* @param frame Between 0 and <code>frameCount - 1</code>, inclusive.
* @param value The index of the value for the frame this curve is used for.
* @param time1 The time for the first key.
* @param value1 The value for the first key.
* @param cx1 The time for the first Bezier handle.
* @param cy1 The value for the first Bezier handle.
* @param cx2 The time of the second Bezier handle.
* @param cy2 The value for the second Bezier handle.
* @param time2 The time for the second key.
* @param value2 The value for the second key. */
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 = getFrameCount() + 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;
}
}
/** Returns the Bezier interpolated value for the specified time.
* @param frameIndex The index into {@link #getFrames()} for the values of the frame before <code>time</code>.
* @param valueOffset The offset from <code>frameIndex</code> to the value this curve is used for.
* @param i The index of the Bezier segments. See {@link #getCurveType(int)}. */
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 += getFrameEntries();
float x = curves[n - 2], y = curves[n - 1];
return y + (time - x) / (frames[frameIndex] - x) * (frames[frameIndex + valueOffset] - y);
}
}
/** The base class for a {@link CurveTimeline} that sets one property. */
static public abstract class CurveTimeline1 extends CurveTimeline {
static public final int ENTRIES = 2;
static final int VALUE = 1;
/** @param bezierCount The maximum number of Bezier curves. See {@link #shrink(int)}.
* @param propertyId Unique identifier for the property the timeline modifies. */
public CurveTimeline1 (int frameCount, int bezierCount, String propertyId) {
super(frameCount, bezierCount, propertyId);
}
public int getFrameEntries () {
return ENTRIES;
}
/** Sets the time and value for the specified frame.
* @param frame Between 0 and <code>frameCount</code>, inclusive.
* @param time The frame time in seconds. */
public void setFrame (int frame, float time, float value) {
frame <<= 1;
frames[frame] = time;
frames[frame + VALUE] = value;
}
/** Returns the interpolated value for the specified time. */
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 setup:
return setup;
case first:
return current + (setup - current) * alpha;
}
return current;
}
float value = getCurveValue(time);
switch (blend) {
case setup:
return setup + value * alpha;
case first:
case replace:
value += setup - current;
}
return current + value * alpha;
}
public float getAbsoluteValue (float time, float alpha, MixBlend blend, float current, float setup) {
if (time < frames[0]) {
switch (blend) {
case setup:
return setup;
case 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 setup:
return setup;
case 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 setup:
return setup;
case first:
return current + (setup - current) * alpha;
}
return current;
}
float value = getCurveValue(time) * setup;
if (alpha == 1) {
if (blend == add) return current + value - setup;
return value;
}
// Mixing out uses sign of setup or current pose, else use sign of key.
if (direction == out) {
switch (blend) {
case setup:
return setup + (Math.abs(value) * Math.signum(setup) - setup) * alpha;
case first:
case replace:
return current + (Math.abs(value) * Math.signum(current) - current) * alpha;
}
} else {
float s;
switch (blend) {
case setup:
s = Math.abs(setup) * Math.signum(value);
return s + (value - s) * alpha;
case first:
case replace:
s = Math.abs(current) * Math.signum(value);
return s + (value - s) * alpha;
}
}
return current + (value - setup) * alpha;
}
}
/** The base class for a {@link CurveTimeline} which sets two properties. */
static public abstract class CurveTimeline2 extends CurveTimeline {
static public final int ENTRIES = 3;
static final int VALUE1 = 1, VALUE2 = 2;
/** @param bezierCount The maximum number of Bezier curves. See {@link #shrink(int)}.
* @param propertyId1 Unique identifier for the first property the timeline modifies.
* @param propertyId2 Unique identifier for the second property the timeline modifies. */
public CurveTimeline2 (int frameCount, int bezierCount, String propertyId1, String propertyId2) {
super(frameCount, bezierCount, propertyId1, propertyId2);
}
public int getFrameEntries () {
return ENTRIES;
}
/** Sets the time and values for the specified frame.
* @param frame Between 0 and <code>frameCount</code>, inclusive.
* @param time The frame time in seconds. */
public void setFrame (int frame, float time, float value1, float value2) {
frame *= ENTRIES;
frames[frame] = time;
frames[frame + VALUE1] = value1;
frames[frame + VALUE2] = value2;
}
}
/** Changes a bone's local {@link Bone#getRotation()}. */
static public class RotateTimeline extends CurveTimeline1 implements BoneTimeline {
final int boneIndex;
public RotateTimeline (int frameCount, int bezierCount, int boneIndex) {
super(frameCount, bezierCount, Property.rotate.ordinal() + "|" + boneIndex);
this.boneIndex = boneIndex;
}
public int getBoneIndex () {
return boneIndex;
}
public void apply (Skeleton skeleton, float lastTime, float time, @Null Array<Event> events, float alpha, MixBlend blend,
MixDirection direction) {
Bone bone = skeleton.bones.get(boneIndex);
if (bone.active) bone.rotation = getRelativeValue(time, alpha, blend, bone.rotation, bone.data.rotation);
}
}
/** Changes a bone's local {@link Bone#getX()} and {@link Bone#getY()}. */
static public class TranslateTimeline extends CurveTimeline2 implements BoneTimeline {
final int boneIndex;
public TranslateTimeline (int frameCount, int bezierCount, int boneIndex) {
super(frameCount, bezierCount, //
Property.x.ordinal() + "|" + boneIndex, //
Property.y.ordinal() + "|" + boneIndex);
this.boneIndex = boneIndex;
}
public int getBoneIndex () {
return boneIndex;
}
public void apply (Skeleton skeleton, float lastTime, float time, @Null Array<Event> events, float alpha, MixBlend blend,
MixDirection direction) {
Bone bone = skeleton.bones.get(boneIndex);
if (!bone.active) return;
float[] frames = this.frames;
if (time < frames[0]) {
switch (blend) {
case setup:
bone.x = bone.data.x;
bone.y = bone.data.y;
return;
case first:
bone.x += (bone.data.x - bone.x) * alpha;
bone.y += (bone.data.y - bone.y) * alpha;
}
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);
}
switch (blend) {
case setup:
bone.x = bone.data.x + x * alpha;
bone.y = bone.data.y + y * alpha;
break;
case first:
case replace:
bone.x += (bone.data.x + x - bone.x) * alpha;
bone.y += (bone.data.y + y - bone.y) * alpha;
break;
case add:
bone.x += x * alpha;
bone.y += y * alpha;
}
}
}
/** Changes a bone's local {@link Bone#getX()}. */
static public class TranslateXTimeline extends CurveTimeline1 implements BoneTimeline {
final int boneIndex;
public TranslateXTimeline (int frameCount, int bezierCount, int boneIndex) {
super(frameCount, bezierCount, Property.x.ordinal() + "|" + boneIndex);
this.boneIndex = boneIndex;
}
public int getBoneIndex () {
return boneIndex;
}
public void apply (Skeleton skeleton, float lastTime, float time, @Null Array<Event> events, float alpha, MixBlend blend,
MixDirection direction) {
Bone bone = skeleton.bones.get(boneIndex);
if (bone.active) bone.x = getRelativeValue(time, alpha, blend, bone.x, bone.data.x);
}
}
/** Changes a bone's local {@link Bone#getY()}. */
static public class TranslateYTimeline extends CurveTimeline1 implements BoneTimeline {
final int boneIndex;
public TranslateYTimeline (int frameCount, int bezierCount, int boneIndex) {
super(frameCount, bezierCount, Property.y.ordinal() + "|" + boneIndex);
this.boneIndex = boneIndex;
}
public int getBoneIndex () {
return boneIndex;
}
public void apply (Skeleton skeleton, float lastTime, float time, @Null Array<Event> events, float alpha, MixBlend blend,
MixDirection direction) {
Bone bone = skeleton.bones.get(boneIndex);
if (bone.active) bone.y = getRelativeValue(time, alpha, blend, bone.y, bone.data.y);
}
}
/** Changes a bone's local {@link Bone#getScaleX()} and {@link Bone#getScaleY()}. */
static public class ScaleTimeline extends CurveTimeline2 implements BoneTimeline {
final int boneIndex;
public ScaleTimeline (int frameCount, int bezierCount, int boneIndex) {
super(frameCount, bezierCount, //
Property.scaleX.ordinal() + "|" + boneIndex, //
Property.scaleY.ordinal() + "|" + boneIndex);
this.boneIndex = boneIndex;
}
public int getBoneIndex () {
return boneIndex;
}
public void apply (Skeleton skeleton, float lastTime, float time, @Null Array<Event> events, float alpha, MixBlend blend,
MixDirection direction) {
Bone bone = skeleton.bones.get(boneIndex);
if (!bone.active) return;
float[] frames = this.frames;
if (time < frames[0]) {
switch (blend) {
case setup:
bone.scaleX = bone.data.scaleX;
bone.scaleY = bone.data.scaleY;
return;
case first:
bone.scaleX += (bone.data.scaleX - bone.scaleX) * alpha;
bone.scaleY += (bone.data.scaleY - bone.scaleY) * alpha;
}
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);
}
x *= bone.data.scaleX;
y *= bone.data.scaleY;
if (alpha == 1) {
if (blend == 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 == out) {
switch (blend) {
case setup:
bx = bone.data.scaleX;
by = bone.data.scaleY;
bone.scaleX = bx + (Math.abs(x) * Math.signum(bx) - bx) * alpha;
bone.scaleY = by + (Math.abs(y) * Math.signum(by) - by) * alpha;
break;
case first:
case replace:
bx = bone.scaleX;
by = bone.scaleY;
bone.scaleX = bx + (Math.abs(x) * Math.signum(bx) - bx) * alpha;
bone.scaleY = by + (Math.abs(y) * Math.signum(by) - by) * alpha;
break;
case add:
bone.scaleX += (x - bone.data.scaleX) * alpha;
bone.scaleY += (y - bone.data.scaleY) * alpha;
}
} else {
switch (blend) {
case setup:
bx = Math.abs(bone.data.scaleX) * Math.signum(x);
by = Math.abs(bone.data.scaleY) * Math.signum(y);
bone.scaleX = bx + (x - bx) * alpha;
bone.scaleY = by + (y - by) * alpha;
break;
case first:
case replace:
bx = Math.abs(bone.scaleX) * Math.signum(x);
by = Math.abs(bone.scaleY) * Math.signum(y);
bone.scaleX = bx + (x - bx) * alpha;
bone.scaleY = by + (y - by) * alpha;
break;
case add:
bone.scaleX += (x - bone.data.scaleX) * alpha;
bone.scaleY += (y - bone.data.scaleY) * alpha;
}
}
}
}
}
/** Changes a bone's local {@link Bone#getScaleX()}. */
static public class ScaleXTimeline extends CurveTimeline1 implements BoneTimeline {
final int boneIndex;
public ScaleXTimeline (int frameCount, int bezierCount, int boneIndex) {
super(frameCount, bezierCount, Property.scaleX.ordinal() + "|" + boneIndex);
this.boneIndex = boneIndex;
}
public int getBoneIndex () {
return boneIndex;
}
public void apply (Skeleton skeleton, float lastTime, float time, @Null Array<Event> events, float alpha, MixBlend blend,
MixDirection direction) {
Bone bone = skeleton.bones.get(boneIndex);
if (bone.active) bone.scaleX = getScaleValue(time, alpha, blend, direction, bone.scaleX, bone.data.scaleX);
}
}
/** Changes a bone's local {@link Bone#getScaleY()}. */
static public class ScaleYTimeline extends CurveTimeline1 implements BoneTimeline {
final int boneIndex;
public ScaleYTimeline (int frameCount, int bezierCount, int boneIndex) {
super(frameCount, bezierCount, Property.scaleY.ordinal() + "|" + boneIndex);
this.boneIndex = boneIndex;
}
public int getBoneIndex () {
return boneIndex;
}
public void apply (Skeleton skeleton, float lastTime, float time, @Null Array<Event> events, float alpha, MixBlend blend,
MixDirection direction) {
Bone bone = skeleton.bones.get(boneIndex);
if (bone.active) bone.scaleY = getScaleValue(time, alpha, blend, direction, bone.scaleY, bone.data.scaleY);
}
}
/** Changes a bone's local {@link Bone#getShearX()} and {@link Bone#getShearY()}. */
static public class ShearTimeline extends CurveTimeline2 implements BoneTimeline {
final int boneIndex;
public ShearTimeline (int frameCount, int bezierCount, int boneIndex) {
super(frameCount, bezierCount, //
Property.shearX.ordinal() + "|" + boneIndex, //
Property.shearY.ordinal() + "|" + boneIndex);
this.boneIndex = boneIndex;
}
public int getBoneIndex () {
return boneIndex;
}
public void apply (Skeleton skeleton, float lastTime, float time, @Null Array<Event> events, float alpha, MixBlend blend,
MixDirection direction) {
Bone bone = skeleton.bones.get(boneIndex);
if (!bone.active) return;
float[] frames = this.frames;
if (time < frames[0]) {
switch (blend) {
case setup:
bone.shearX = bone.data.shearX;
bone.shearY = bone.data.shearY;
return;
case first:
bone.shearX += (bone.data.shearX - bone.shearX) * alpha;
bone.shearY += (bone.data.shearY - bone.shearY) * alpha;
}
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);
}
switch (blend) {
case setup:
bone.shearX = bone.data.shearX + x * alpha;
bone.shearY = bone.data.shearY + y * alpha;
break;
case first:
case replace:
bone.shearX += (bone.data.shearX + x - bone.shearX) * alpha;
bone.shearY += (bone.data.shearY + y - bone.shearY) * alpha;
break;
case add:
bone.shearX += x * alpha;
bone.shearY += y * alpha;
}
}
}
/** Changes a bone's local {@link Bone#getShearX()}. */
static public class ShearXTimeline extends CurveTimeline1 implements BoneTimeline {
final int boneIndex;
public ShearXTimeline (int frameCount, int bezierCount, int boneIndex) {
super(frameCount, bezierCount, Property.shearX.ordinal() + "|" + boneIndex);
this.boneIndex = boneIndex;
}
public int getBoneIndex () {
return boneIndex;
}
public void apply (Skeleton skeleton, float lastTime, float time, @Null Array<Event> events, float alpha, MixBlend blend,
MixDirection direction) {
Bone bone = skeleton.bones.get(boneIndex);
if (bone.active) bone.shearX = getRelativeValue(time, alpha, blend, bone.shearX, bone.data.shearX);
}
}
/** Changes a bone's local {@link Bone#getShearY()}. */
static public class ShearYTimeline extends CurveTimeline1 implements BoneTimeline {
final int boneIndex;
public ShearYTimeline (int frameCount, int bezierCount, int boneIndex) {
super(frameCount, bezierCount, Property.shearY.ordinal() + "|" + boneIndex);
this.boneIndex = boneIndex;
}
public int getBoneIndex () {
return boneIndex;
}
public void apply (Skeleton skeleton, float lastTime, float time, @Null Array<Event> events, float alpha, MixBlend blend,
MixDirection direction) {
Bone bone = skeleton.bones.get(boneIndex);
if (bone.active) bone.shearY = getRelativeValue(time, alpha, blend, bone.shearY, bone.data.shearY);
}
}
/** Changes a bone's {@link Bone#getInherit()}. */
static public class InheritTimeline extends Timeline implements BoneTimeline {
static public final int ENTRIES = 2;
static private final int INHERIT = 1;
final int boneIndex;
public InheritTimeline (int frameCount, int boneIndex) {
super(frameCount, Property.inherit.ordinal() + "|" + boneIndex);
this.boneIndex = boneIndex;
}
public int getBoneIndex () {
return boneIndex;
}
public int getFrameEntries () {
return ENTRIES;
}
/** Sets the transform mode for the specified frame.
* @param frame Between 0 and <code>frameCount</code>, inclusive.
* @param time The frame time in seconds. */
public void setFrame (int frame, float time, Inherit inherit) {
frame *= ENTRIES;
frames[frame] = time;
frames[frame + INHERIT] = inherit.ordinal();
}
public void apply (Skeleton skeleton, float lastTime, float time, @Null Array<Event> events, float alpha, MixBlend blend,
MixDirection direction) {
Bone bone = skeleton.bones.get(boneIndex);
if (!bone.active) return;
float[] frames = this.frames;
if (time < frames[0]) {
if (blend == setup || blend == first) bone.inherit = bone.data.inherit;
return;
}
bone.inherit = Inherit.values[(int)frames[search(frames, time, ENTRIES) + INHERIT]];
}
}
/** Changes a slot's {@link Slot#getColor()}. */
static public class RGBATimeline extends CurveTimeline implements SlotTimeline {
static public final int ENTRIES = 5;
static private final int R = 1, G = 2, B = 3, A = 4;
final int slotIndex;
public RGBATimeline (int frameCount, int bezierCount, int slotIndex) {
super(frameCount, bezierCount, //