Skip to content

Commit 9cd3ae0

Browse files
committed
change method name: startWalkFA -> walk
added compute bounds
1 parent 229d64e commit 9cd3ae0

File tree

6 files changed

+95
-79
lines changed

6 files changed

+95
-79
lines changed

blade-engine-spine-plugin/src/main/java/com/bladecoder/engine/spine/SpineRenderer.java

Lines changed: 90 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import java.util.HashMap;
1919

2020
import com.bladecoder.engine.model.ActorRenderer;
21-
2221
import com.badlogic.gdx.graphics.Color;
2322
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
2423
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
@@ -38,13 +37,16 @@
3837
import com.esotericsoftware.spine.Animation;
3938
import com.esotericsoftware.spine.AnimationState;
4039
import com.esotericsoftware.spine.AnimationState.AnimationStateListener;
40+
import com.esotericsoftware.spine.attachments.Attachment;
41+
import com.esotericsoftware.spine.attachments.RegionAttachment;
4142
import com.esotericsoftware.spine.AnimationStateData;
4243
import com.esotericsoftware.spine.Event;
4344
import com.esotericsoftware.spine.Skeleton;
4445
import com.esotericsoftware.spine.SkeletonBinary;
4546
import com.esotericsoftware.spine.SkeletonBounds;
4647
import com.esotericsoftware.spine.SkeletonData;
4748
import com.esotericsoftware.spine.SkeletonRenderer;
49+
import com.esotericsoftware.spine.Slot;
4850

4951
public class SpineRenderer implements ActorRenderer {
5052

@@ -66,6 +68,7 @@ public class SpineRenderer implements ActorRenderer {
6668

6769
private SkeletonRenderer renderer;
6870
private SkeletonBounds bounds;
71+
private float width = 200, height = 200;
6972

7073
private final HashMap<String, SkeletonCacheEntry> sourceCache = new HashMap<String, SkeletonCacheEntry>();
7174

@@ -84,17 +87,15 @@ public SpineRenderer() {
8487
private AnimationStateListener animationListener = new AnimationStateListener() {
8588
@Override
8689
public void complete(int trackIndex, int loopCount) {
87-
if (currentAnimationType == Tween.REPEAT
88-
&& (currentCount == Tween.INFINITY || currentCount > loopCount)) {
90+
if (currentAnimationType == Tween.REPEAT && (currentCount == Tween.INFINITY || currentCount > loopCount)) {
8991
return;
9092
}
9193

9294
currentSource.animation.setTimeScale(0);
9395
if (animationCb != null || animationCbSer != null) {
9496

9597
if (animationCb == null) {
96-
animationCb = ActionCallbackSerialization
97-
.find(animationCbSer);
98+
animationCb = ActionCallbackSerialization.find(animationCbSer);
9899
animationCbSer = null;
99100
}
100101

@@ -158,8 +159,7 @@ public String getInitAnimation() {
158159
public String[] getInternalAnimations(String source) {
159160
retrieveSource(source);
160161

161-
Array<Animation> animations = sourceCache.get(source).skeleton
162-
.getData().getAnimations();
162+
Array<Animation> animations = sourceCache.get(source).skeleton.getData().getAnimations();
163163
String[] result = new String[animations.size];
164164

165165
for (int i = 0; i < animations.size; i++) {
@@ -188,33 +188,23 @@ public void draw(SpriteBatch batch, float x, float y, float scale) {
188188
currentSource.skeleton.setX(x / scale);
189189
currentSource.skeleton.setY(y / scale);
190190

191-
batch.setTransformMatrix(batch.getTransformMatrix().scale(scale,
192-
scale, 1.0f));
191+
batch.setTransformMatrix(batch.getTransformMatrix().scale(scale, scale, 1.0f));
193192
renderer.draw(batch, currentSource.skeleton);
194-
batch.setTransformMatrix(batch.getTransformMatrix().scale(
195-
1 / scale, 1 / scale, 1.0f));
193+
batch.setTransformMatrix(batch.getTransformMatrix().scale(1 / scale, 1 / scale, 1.0f));
196194
} else {
197195
x = x - getWidth() / 2 * scale;
198-
RectangleRenderer.draw(batch, x, y, getWidth() * scale, getHeight()
199-
* scale, Color.RED);
196+
RectangleRenderer.draw(batch, x, y, getWidth() * scale, getHeight() * scale, Color.RED);
200197
}
201198
}
202199

203200
@Override
204201
public float getWidth() {
205-
if (bounds != null && bounds.getWidth() > 0) {
206-
return bounds.getWidth();
207-
}
208-
209-
return 200;
202+
return width;
210203
}
211204

212205
@Override
213206
public float getHeight() {
214-
if (bounds != null && bounds.getHeight() > 0)
215-
return bounds.getHeight();
216-
217-
return 200;
207+
return height;
218208
}
219209

220210
@Override
@@ -250,18 +240,15 @@ public void stand() {
250240
}
251241

252242
@Override
253-
public void startWalkFA(Vector2 p0, Vector2 pf) {
254-
String currentDirection = AnimationDesc.getFrameDirection(p0.x, p0.y,
255-
pf);
243+
public void walk(Vector2 p0, Vector2 pf) {
244+
String currentDirection = AnimationDesc.getFrameDirection(p0.x, p0.y, pf);
256245
StringBuilder sb = new StringBuilder();
257-
sb.append(AnimationDesc.WALK_ANIM).append('.')
258-
.append(currentDirection);
246+
sb.append(AnimationDesc.WALK_ANIM).append('.').append(currentDirection);
259247
startAnimation(sb.toString(), Tween.FROM_FA, 1, null);
260248
}
261249

262250
@Override
263-
public void startAnimation(String id, int repeatType, int count,
264-
ActionCallback cb) {
251+
public void startAnimation(String id, int repeatType, int count, ActionCallback cb) {
265252
AnimationDesc fa = getAnimation(id);
266253

267254
if (fa == null) {
@@ -270,8 +257,7 @@ public void startAnimation(String id, int repeatType, int count,
270257
return;
271258
}
272259

273-
if (currentAnimation != null
274-
&& currentAnimation.disposeWhenPlayed)
260+
if (currentAnimation != null && currentAnimation.disposeWhenPlayed)
275261
disposeSource(currentAnimation.source);
276262

277263
currentAnimation = fa;
@@ -305,24 +291,67 @@ public void startAnimation(String id, int repeatType, int count,
305291
}
306292

307293
lastAnimationTime = 0f;
308-
setCurrentFA();
294+
setCurrentAnimation();
309295
}
310296

311-
private void setCurrentFA() {
297+
private void setCurrentAnimation() {
312298
try {
313-
// currentSource.skeleton.setToSetupPose();
299+
// TODO Make setup pose parametrizable in the AnimationDesc
300+
currentSource.skeleton.setToSetupPose();
314301
currentSource.skeleton.setFlipX(flipX);
315-
currentSource.animation
316-
.setTimeScale(currentAnimation.duration);
317-
currentSource.animation.setAnimation(0, currentAnimation.id,
318-
currentAnimationType == Tween.REPEAT);
302+
currentSource.animation.setTimeScale(currentAnimation.duration);
303+
currentSource.animation.setAnimation(0, currentAnimation.id, currentAnimationType == Tween.REPEAT);
319304
update(lastAnimationTime);
320-
bounds.update(currentSource.skeleton, true);
305+
computeBounds();
321306
} catch (Exception e) {
322307
EngineLogger.error("SpineRenderer:setCurrentFA " + e.getMessage());
323308
}
324309
}
325310

311+
private void computeBounds() {
312+
bounds.update(currentSource.skeleton, true);
313+
314+
if (bounds.getWidth() > 0 && bounds.getHeight() > 0) {
315+
width = bounds.getWidth();
316+
height = bounds.getHeight();
317+
} else {
318+
currentSource.skeleton.updateWorldTransform();
319+
320+
float minX = Float.MAX_VALUE;
321+
float minY = Float.MAX_VALUE;
322+
float maxX = Float.MIN_VALUE, maxY = Float.MIN_VALUE;
323+
324+
Array<Slot> slots = currentSource.skeleton.getSlots();
325+
326+
for (int i = 0, n = slots.size; i < n; i++) {
327+
Slot slot = slots.get(i);
328+
Attachment attachment = slot.getAttachment();
329+
if (attachment == null)
330+
continue;
331+
332+
if (!(attachment instanceof RegionAttachment))
333+
continue;
334+
335+
((RegionAttachment) attachment).updateWorldVertices(slot, false);
336+
337+
float[] vertices = ((RegionAttachment) attachment).getWorldVertices();
338+
for (int ii = 0, nn = vertices.length; ii < nn; ii += 5) {
339+
minX = Math.min(minX, vertices[ii]);
340+
minY = Math.min(minY, vertices[ii + 1]);
341+
maxX = Math.max(maxX, vertices[ii]);
342+
maxY = Math.max(maxY, vertices[ii + 1]);
343+
}
344+
}
345+
346+
width = (maxX - minX);
347+
height = (maxY - minY);
348+
349+
if(width <= 0 || height <= 0) {
350+
width = height = 200;
351+
}
352+
}
353+
}
354+
326355
private AnimationDesc getAnimation(String id) {
327356
AnimationDesc fa = fanims.get(id);
328357
flipX = false;
@@ -339,12 +368,10 @@ private AnimationDesc getAnimation(String id) {
339368
// search for .left if .frontleft not found and viceversa
340369
StringBuilder sb = new StringBuilder();
341370

342-
if (id.endsWith(AnimationDesc.FRONTLEFT)
343-
|| id.endsWith(AnimationDesc.FRONTRIGHT)) {
371+
if (id.endsWith(AnimationDesc.FRONTLEFT) || id.endsWith(AnimationDesc.FRONTRIGHT)) {
344372
sb.append(id.substring(0, id.lastIndexOf('.') + 1));
345373
sb.append(AnimationDesc.FRONT);
346-
} else if (id.endsWith(AnimationDesc.BACKLEFT)
347-
|| id.endsWith(AnimationDesc.BACKRIGHT)) {
374+
} else if (id.endsWith(AnimationDesc.BACKLEFT) || id.endsWith(AnimationDesc.BACKRIGHT)) {
348375
sb.append(id.substring(0, id.lastIndexOf('.') + 1));
349376
sb.append(AnimationDesc.BACK);
350377
} else if (id.endsWith(AnimationDesc.LEFT)) {
@@ -367,12 +394,13 @@ private AnimationDesc getAnimation(String id) {
367394

368395
if (fa != null) {
369396
flipX = true;
370-
} else if (s.endsWith(AnimationDesc.FRONT)
371-
|| s.endsWith(AnimationDesc.BACK)) { // search for
372-
// only
373-
// right or
374-
// left
375-
// animations
397+
} else if (s.endsWith(AnimationDesc.FRONT) || s.endsWith(AnimationDesc.BACK)) { // search
398+
// for
399+
// only
400+
// right
401+
// or
402+
// left
403+
// animations
376404
if (id.endsWith(AnimationDesc.LEFT)) {
377405
sb.append(id.substring(0, id.lastIndexOf('.') + 1));
378406
sb.append(AnimationDesc.LEFT);
@@ -426,14 +454,11 @@ private void retrieveSource(String source) {
426454
}
427455

428456
if (entry.skeleton == null) {
429-
TextureAtlas atlas = EngineAssetManager.getInstance()
430-
.getTextureAtlas(source);
457+
TextureAtlas atlas = EngineAssetManager.getInstance().getTextureAtlas(source);
431458

432459
SkeletonBinary skel = new SkeletonBinary(atlas);
433460
skel.setScale(EngineAssetManager.getInstance().getScale());
434-
SkeletonData skeletonData = skel
435-
.readSkeletonData(EngineAssetManager.getInstance()
436-
.getSpine(source));
461+
SkeletonData skeletonData = skel.readSkeletonData(EngineAssetManager.getInstance().getSpine(source));
437462

438463
entry.skeleton = new Skeleton(skeletonData);
439464

@@ -489,11 +514,10 @@ public void retrieveAssets() {
489514
}
490515

491516
if (currentAnimation != null) {
492-
SkeletonCacheEntry entry = sourceCache
493-
.get(currentAnimation.source);
517+
SkeletonCacheEntry entry = sourceCache.get(currentAnimation.source);
494518
currentSource = entry;
495519

496-
setCurrentFA();
520+
setCurrentAnimation();
497521

498522
} else if (initAnimation != null) {
499523
startAnimation(initAnimation, Tween.FROM_FA, 1, null);
@@ -522,8 +546,7 @@ public void write(Json json) {
522546
if (currentAnimation != null)
523547
currentAnimationId = currentAnimation.id;
524548

525-
json.writeValue("currentAnimation", currentAnimationId,
526-
currentAnimationId == null ? null : String.class);
549+
json.writeValue("currentAnimation", currentAnimationId, currentAnimationId == null ? null : String.class);
527550

528551
json.writeValue("initAnimation", initAnimation);
529552

@@ -532,9 +555,8 @@ public void write(Json json) {
532555
if (animationCbSer != null)
533556
json.writeValue("cb", animationCbSer);
534557
else
535-
json.writeValue("cb",
536-
ActionCallbackSerialization.find(animationCb),
537-
animationCb == null ? null : String.class);
558+
json.writeValue("cb", ActionCallbackSerialization.find(animationCb), animationCb == null ? null
559+
: String.class);
538560

539561
json.writeValue("currentCount", currentCount);
540562
json.writeValue("currentAnimationType", currentAnimationType);
@@ -545,25 +567,19 @@ public void write(Json json) {
545567
@Override
546568
public void read(Json json, JsonValue jsonData) {
547569

548-
fanims = json.readValue("fanims", HashMap.class, AnimationDesc.class,
549-
jsonData);
570+
fanims = json.readValue("fanims", HashMap.class, AnimationDesc.class, jsonData);
550571

551-
String currentAnimationId = json.readValue(
552-
"currentAnimation", String.class, jsonData);
572+
String currentAnimationId = json.readValue("currentAnimation", String.class, jsonData);
553573

554574
if (currentAnimationId != null)
555-
currentAnimation = (AtlasAnimationDesc) fanims
556-
.get(currentAnimationId);
575+
currentAnimation = (AtlasAnimationDesc) fanims.get(currentAnimationId);
557576

558-
initAnimation = json.readValue("initAnimation", String.class,
559-
jsonData);
577+
initAnimation = json.readValue("initAnimation", String.class, jsonData);
560578

561579
flipX = json.readValue("flipX", Boolean.class, jsonData);
562580
animationCbSer = json.readValue("cb", String.class, jsonData);
563581
currentCount = json.readValue("currentCount", Integer.class, jsonData);
564-
currentAnimationType = json.readValue("currentAnimationType",
565-
Integer.class, jsonData);
566-
lastAnimationTime = json.readValue("lastAnimationTime", Float.class,
567-
jsonData);
582+
currentAnimationType = json.readValue("currentAnimationType", Integer.class, jsonData);
583+
lastAnimationTime = json.readValue("lastAnimationTime", Float.class, jsonData);
568584
}
569585
}

blade-engine/src/com/bladecoder/engine/model/ActorRenderer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public interface ActorRenderer extends Serializable, AssetConsumer {
3838
public void lookat(float x, float y, Vector2 pf);
3939
public void lookat(String direction);
4040
public void stand();
41-
public void startWalkFA(Vector2 p0, Vector2 pf);
41+
public void walk(Vector2 p0, Vector2 pf);
4242
public void startAnimation(String id, int repeatType,
4343
int count, ActionCallback cb);
4444

blade-engine/src/com/bladecoder/engine/model/AtlasRenderer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ public void stand() {
343343
}
344344

345345
@Override
346-
public void startWalkFA(Vector2 p0, Vector2 pf) {
346+
public void walk(Vector2 p0, Vector2 pf) {
347347
String currentDirection = AnimationDesc.getFrameDirection(p0.x, p0.y, pf);
348348
StringBuilder sb = new StringBuilder();
349349
sb.append(AnimationDesc.WALK_ANIM).append('.').append(currentDirection);

blade-engine/src/com/bladecoder/engine/model/ImageRenderer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ public void stand() {
283283
}
284284

285285
@Override
286-
public void startWalkFA(Vector2 p0, Vector2 pf) {
286+
public void walk(Vector2 p0, Vector2 pf) {
287287
String currentDirection = AnimationDesc.getFrameDirection(p0.x, p0.y, pf);
288288
StringBuilder sb = new StringBuilder();
289289
sb.append(AnimationDesc.WALK_ANIM).append('.').append(currentDirection);

blade-engine/src/com/bladecoder/engine/model/Sprite3DRenderer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ public void stand() {
469469
}
470470

471471
@Override
472-
public void startWalkFA(Vector2 p0, Vector2 pf) {
472+
public void walk(Vector2 p0, Vector2 pf) {
473473
lookat(p0.x, p0.y, pf);
474474
startAnimation(AnimationDesc.WALK_ANIM, Tween.REPEAT, -1, null);
475475
}

blade-engine/src/com/bladecoder/engine/model/SpriteActor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ public void stand() {
239239
}
240240

241241
public void startWalkFA(Vector2 p0, Vector2 pf) {
242-
renderer.startWalkFA(p0, pf);
242+
renderer.walk(p0, pf);
243243
if(bboxFromRenderer)
244244
updateBBox();
245245
}

0 commit comments

Comments
 (0)