23
23
import com .badlogic .gdx .math .Matrix4 ;
24
24
import com .badlogic .gdx .math .Vector2 ;
25
25
import com .badlogic .gdx .utils .Array ;
26
+ import com .badlogic .gdx .utils .GdxRuntimeException ;
26
27
import com .badlogic .gdx .utils .Json ;
27
28
import com .badlogic .gdx .utils .JsonValue ;
28
29
import com .bladecoder .engine .actions .ActionCallback ;
@@ -68,7 +69,7 @@ public class SpineRenderer extends AnimationRenderer {
68
69
69
70
private SkeletonRenderer <SpriteBatch > renderer ;
70
71
private SkeletonBounds bounds ;
71
-
72
+
72
73
private float width = super .getWidth (), height = super .getHeight ();
73
74
74
75
private float lastAnimationTime = 0 ;
@@ -101,8 +102,8 @@ public void complete(int trackIndex, int loopCount) {
101
102
&& (currentCount == Tween .INFINITY || currentCount > loopCount )) {
102
103
103
104
// FIX for latest spine rt not setting setup pose when looping.
104
- SkeletonCacheEntry cs = (SkeletonCacheEntry )currentSource ;
105
-
105
+ SkeletonCacheEntry cs = (SkeletonCacheEntry ) currentSource ;
106
+
106
107
cs .skeleton .setToSetupPose ();
107
108
cs .skeleton .setFlipX (flipX );
108
109
complete = true ;
@@ -169,9 +170,19 @@ public void start(int arg0) {
169
170
170
171
@ Override
171
172
public String [] getInternalAnimations (AnimationDesc anim ) {
172
- retrieveSource (anim .source , ((SpineAnimationDesc ) anim ).atlas );
173
+ try {
174
+ retrieveSource (anim .source , ((SpineAnimationDesc ) anim ).atlas );
175
+ } catch (GdxRuntimeException e ) {
176
+ sourceCache .remove (anim .source );
177
+ Array <String > dependencies = EngineAssetManager .getInstance ().getDependencies (getFileName (anim .source ));
178
+ if (dependencies .size > 0 )
179
+ dependencies .removeIndex (dependencies .size - 1 );
180
+ return new String [0 ];
181
+ }
173
182
174
- Array <Animation > animations = ((SkeletonCacheEntry )sourceCache .get (anim .source )).skeleton .getData ().getAnimations ();
183
+ Array <Animation > animations = ((SkeletonCacheEntry ) sourceCache .get (anim .source )).skeleton .getData ()
184
+ .getAnimations ();
185
+
175
186
String [] result = new String [animations .size ];
176
187
177
188
for (int i = 0 ; i < animations .size ; i ++) {
@@ -188,7 +199,7 @@ public void update(float delta) {
188
199
return ;
189
200
}
190
201
191
- if (currentSource != null && ((SkeletonCacheEntry )currentSource ).skeleton != null ) {
202
+ if (currentSource != null && ((SkeletonCacheEntry ) currentSource ).skeleton != null ) {
192
203
float d = delta ;
193
204
194
205
if (currentAnimationType == Tween .Type .REVERSE ) {
@@ -210,7 +221,7 @@ public void update(float delta) {
210
221
211
222
private void updateAnimation (float time ) {
212
223
SkeletonCacheEntry cs = (SkeletonCacheEntry ) currentSource ;
213
-
224
+
214
225
cs .animation .update (time );
215
226
cs .animation .apply (cs .skeleton );
216
227
cs .skeleton .updateWorldTransform ();
@@ -222,18 +233,18 @@ private void updateAnimation(float time) {
222
233
public void draw (SpriteBatch batch , float x , float y , float scale , float rotation , Color tint ) {
223
234
224
235
SkeletonCacheEntry cs = (SkeletonCacheEntry ) currentSource ;
225
-
236
+
226
237
if (cs != null && cs .skeleton != null ) {
227
238
Matrix4 tm = batch .getTransformMatrix ();
228
239
tmp .set (tm );
229
240
230
241
float originX = cs .skeleton .getRootBone ().getX ();
231
242
float originY = cs .skeleton .getRootBone ().getY ();
232
- tm .translate (x ,y , 0 ).rotate (0 , 0 , 1 , rotation ).scale (scale , scale , 1 ).translate (originX , originY , 0 );
233
-
234
- // cs.skeleton.setX(x / scale);
235
- // cs.skeleton.setY(y / scale);
236
-
243
+ tm .translate (x , y , 0 ).rotate (0 , 0 , 1 , rotation ).scale (scale , scale , 1 ).translate (originX , originY , 0 );
244
+
245
+ // cs.skeleton.setX(x / scale);
246
+ // cs.skeleton.setY(y / scale);
247
+
237
248
batch .setTransformMatrix (tm );
238
249
239
250
if (tint != null )
@@ -247,8 +258,9 @@ public void draw(SpriteBatch batch, float x, float y, float scale, float rotatio
247
258
} else {
248
259
float dx = getAlignDx (getWidth (), orgAlign );
249
260
float dy = getAlignDy (getHeight (), orgAlign );
250
-
251
- RectangleRenderer .draw (batch , x + dx * scale , y + dy * scale , getWidth () * scale , getHeight () * scale , Color .RED );
261
+
262
+ RectangleRenderer .draw (batch , x + dx * scale , y + dy * scale , getWidth () * scale , getHeight () * scale ,
263
+ Color .RED );
252
264
}
253
265
}
254
266
@@ -339,7 +351,7 @@ public void startAnimation(String id, Tween.Type repeatType, int count, ActionCa
339
351
340
352
if (currentAnimationType == Tween .Type .REVERSE ) {
341
353
// get animation duration
342
- Array <Animation > animations = ((SkeletonCacheEntry )currentSource ).skeleton .getData ().getAnimations ();
354
+ Array <Animation > animations = ((SkeletonCacheEntry ) currentSource ).skeleton .getData ().getAnimations ();
343
355
344
356
for (Animation a : animations ) {
345
357
if (a .getName ().equals (currentAnimation .id )) {
@@ -380,7 +392,7 @@ public void computeBbox() {
380
392
if (bbox != null && (bbox .getVertices () == null || bbox .getVertices ().length != 8 )) {
381
393
bbox .setVertices (new float [8 ]);
382
394
}
383
-
395
+
384
396
SkeletonCacheEntry cs = (SkeletonCacheEntry ) currentSource ;
385
397
386
398
if (cs == null || cs .skeleton == null ) {
@@ -542,9 +554,15 @@ private AnimationDesc getAnimation(String id) {
542
554
543
555
return fa ;
544
556
}
557
+
558
+
559
+ private String getFileName (String source ) {
560
+ return EngineAssetManager .SPINE_DIR + source + EngineAssetManager .SPINE_EXT ;
561
+ }
545
562
546
563
private void loadSource (String source , String atlas ) {
547
- SkeletonCacheEntry entry = (SkeletonCacheEntry )sourceCache .get (source );
564
+ EngineLogger .debug ("Loading: " + source );
565
+ SkeletonCacheEntry entry = (SkeletonCacheEntry ) sourceCache .get (source );
548
566
549
567
if (entry == null ) {
550
568
entry = new SkeletonCacheEntry ();
@@ -553,34 +571,35 @@ private void loadSource(String source, String atlas) {
553
571
}
554
572
555
573
if (entry .refCounter == 0 ) {
556
-
557
- if (EngineAssetManager .getInstance ().getLoader (SkeletonData .class ) == null ) {
574
+
575
+ if (EngineAssetManager .getInstance ().getLoader (SkeletonData .class ) == null ) {
558
576
EngineAssetManager .getInstance ().setLoader (SkeletonData .class ,
559
577
new SkeletonDataLoader (EngineAssetManager .getInstance ().getFileHandleResolver ()));
560
578
}
561
-
579
+
562
580
SkeletonDataLoaderParameter parameter = new SkeletonDataLoaderParameter (
563
581
EngineAssetManager .ATLASES_DIR + entry .atlas + EngineAssetManager .ATLAS_EXT ,
564
582
EngineAssetManager .getInstance ().getScale ());
565
- EngineAssetManager .getInstance ().load (EngineAssetManager . SPINE_DIR + source + EngineAssetManager . SPINE_EXT ,
583
+ EngineAssetManager .getInstance ().load (getFileName ( source ) ,
566
584
SkeletonData .class , parameter );
567
585
}
568
586
569
587
entry .refCounter ++;
570
588
}
571
589
572
590
private void retrieveSource (String source , String atlas ) {
573
- SkeletonCacheEntry entry = (SkeletonCacheEntry )sourceCache .get (source );
591
+ EngineLogger .debug ("Retrieving: " + source );
592
+ SkeletonCacheEntry entry = (SkeletonCacheEntry ) sourceCache .get (source );
574
593
575
594
if (entry == null || entry .refCounter < 1 ) {
576
595
loadSource (source , atlas );
577
596
EngineAssetManager .getInstance ().finishLoading ();
578
- entry = (SkeletonCacheEntry )sourceCache .get (source );
597
+ entry = (SkeletonCacheEntry ) sourceCache .get (source );
579
598
}
580
599
581
600
if (entry .skeleton == null ) {
582
601
SkeletonData skeletonData = EngineAssetManager .getInstance ()
583
- .get (EngineAssetManager . SPINE_DIR + source + EngineAssetManager . SPINE_EXT , SkeletonData .class );
602
+ .get (getFileName ( source ) , SkeletonData .class );
584
603
585
604
entry .skeleton = new Skeleton (skeletonData );
586
605
@@ -596,10 +615,12 @@ private void retrieveSource(String source, String atlas) {
596
615
}
597
616
598
617
private void disposeSource (String source ) {
599
- SkeletonCacheEntry entry = (SkeletonCacheEntry )sourceCache .get (source );
618
+ EngineLogger .debug ("Disposing: " + source );
619
+ SkeletonCacheEntry entry = (SkeletonCacheEntry ) sourceCache .get (source );
600
620
601
621
if (entry .refCounter == 1 ) {
602
- EngineAssetManager .getInstance ().disposeAtlas (source );
622
+ EngineAssetManager .getInstance ()
623
+ .unload (EngineAssetManager .SPINE_DIR + source + EngineAssetManager .SPINE_EXT );
603
624
entry .animation = null ;
604
625
entry .skeleton = null ;
605
626
}
@@ -632,11 +653,11 @@ public void retrieveAssets() {
632
653
633
654
for (String key : sourceCache .keySet ()) {
634
655
if (sourceCache .get (key ).refCounter > 0 )
635
- retrieveSource (key , ((SkeletonCacheEntry )sourceCache .get (key )).atlas );
656
+ retrieveSource (key , ((SkeletonCacheEntry ) sourceCache .get (key )).atlas );
636
657
}
637
658
638
659
if (currentAnimation != null ) {
639
- SkeletonCacheEntry entry = (SkeletonCacheEntry )sourceCache .get (currentAnimation .source );
660
+ SkeletonCacheEntry entry = (SkeletonCacheEntry ) sourceCache .get (currentAnimation .source );
640
661
currentSource = entry ;
641
662
642
663
// Stop events to avoid event trigger
@@ -670,7 +691,7 @@ public void dispose() {
670
691
@ Override
671
692
public void write (Json json ) {
672
693
super .write (json );
673
-
694
+
674
695
if (SerializationHelper .getInstance ().getMode () == Mode .MODEL ) {
675
696
676
697
} else {
@@ -690,11 +711,11 @@ public void write(Json json) {
690
711
@ Override
691
712
public void read (Json json , JsonValue jsonData ) {
692
713
super .read (json , jsonData );
693
-
714
+
694
715
if (SerializationHelper .getInstance ().getMode () == Mode .MODEL ) {
695
716
fanims = json .readValue ("fanims" , HashMap .class , SpineAnimationDesc .class , jsonData );
696
717
} else {
697
-
718
+
698
719
String animationCbSer = json .readValue ("cb" , String .class , jsonData );
699
720
animationCb = ActionCallbackSerialization .find (animationCbSer );
700
721
0 commit comments