From e18de6cbe6353bad413f844bc4697f6060996456 Mon Sep 17 00:00:00 2001 From: NathanSweet Date: Sun, 31 Aug 2014 12:19:37 +0200 Subject: [PATCH] Moved Skeleton refernce from Slot to Bone. --- .../com/esotericsoftware/spine/Animation.java | 6 +-- .../src/com/esotericsoftware/spine/Bone.java | 37 +++++++++----- .../com/esotericsoftware/spine/Skeleton.java | 51 +++++++------------ .../src/com/esotericsoftware/spine/Slot.java | 32 +++++------- 4 files changed, 60 insertions(+), 66 deletions(-) diff --git a/spine-libgdx/src/com/esotericsoftware/spine/Animation.java b/spine-libgdx/src/com/esotericsoftware/spine/Animation.java index 8f3d9162b..c0220e201 100644 --- a/spine-libgdx/src/com/esotericsoftware/spine/Animation.java +++ b/spine-libgdx/src/com/esotericsoftware/spine/Animation.java @@ -128,11 +128,11 @@ public class Animation { if (high == 0) return 1; int current = high >>> 1; while (true) { - if (values[(current + 1)] <= target) + if (values[current + 1] <= target) low = current + 1; else high = current; - if (low == high) return (low + 1); + if (low == high) return low + 1; current = (low + high) >>> 1; } } @@ -510,7 +510,7 @@ public class Animation { } else if (lastTime > time) // lastTime = -1; - int frameIndex = time >= frames[frames.length - 1] ? frames.length - 1 : binarySearch(frames, time, 1) - 1; + int frameIndex = time >= frames[frames.length - 1] ? frames.length - 1 : binarySearch(frames, time) - 1; if (frames[frameIndex] <= lastTime) return; String attachmentName = attachmentNames[frameIndex]; diff --git a/spine-libgdx/src/com/esotericsoftware/spine/Bone.java b/spine-libgdx/src/com/esotericsoftware/spine/Bone.java index a57c0b3db..6022a6fc3 100644 --- a/spine-libgdx/src/com/esotericsoftware/spine/Bone.java +++ b/spine-libgdx/src/com/esotericsoftware/spine/Bone.java @@ -38,29 +38,38 @@ import com.badlogic.gdx.math.Vector2; public class Bone { final BoneData data; + final Skeleton skeleton; final Bone parent; float x, y; float rotation, rotationIK; float scaleX, scaleY; - boolean flipX, flipY; float m00, m01, worldX; // a b x float m10, m11, worldY; // c d y float worldRotation; float worldScaleX, worldScaleY; - /** @param parent May be null. */ - public Bone (BoneData data, Bone parent) { - if (data == null) throw new IllegalArgumentException("data cannot be null."); + Bone (BoneData data) { this.data = data; + parent = null; + skeleton = null; + } + + /** @param parent May be null. */ + public Bone (BoneData data, Skeleton skeleton, Bone parent) { + if (data == null) throw new IllegalArgumentException("data cannot be null."); + if (skeleton == null) throw new IllegalArgumentException("skeleton cannot be null."); + this.data = data; + this.skeleton = skeleton; this.parent = parent; setToSetupPose(); } /** Copy constructor. * @param parent May be null. */ - public Bone (Bone bone, Bone parent) { + public Bone (Bone bone, Skeleton skeleton, Bone parent) { if (bone == null) throw new IllegalArgumentException("bone cannot be null."); + this.skeleton = skeleton; this.parent = parent; data = bone.data; x = bone.x; @@ -69,12 +78,11 @@ public class Bone { rotationIK = bone.rotationIK; scaleX = bone.scaleX; scaleY = bone.scaleY; - flipX = bone.flipX; - flipY = bone.flipY; } /** Computes the world SRT using the parent bone and the local SRT. */ public void updateWorldTransform () { + Skeleton skeleton = this.skeleton; Bone parent = this.parent; float x = this.x, y = this.y; if (parent != null) { @@ -89,22 +97,22 @@ public class Bone { } worldRotation = data.inheritRotation ? parent.worldRotation + rotationIK : rotationIK; } else { - worldX = flipX ? -x : x; - worldY = flipY ? -y : y; + worldX = skeleton.flipX ? -x : x; + worldY = skeleton.flipY ? -y : y; worldScaleX = scaleX; worldScaleY = scaleY; worldRotation = rotationIK; } float cos = MathUtils.cosDeg(worldRotation); float sin = MathUtils.sinDeg(worldRotation); - if (flipX) { + if (skeleton.flipX) { m00 = -cos * worldScaleX; m01 = sin * worldScaleY; } else { m00 = cos * worldScaleX; m01 = -sin * worldScaleY; } - if (flipY) { + if (skeleton.flipY) { m10 = -sin * worldScaleX; m11 = -cos * worldScaleY; } else { @@ -127,6 +135,10 @@ public class Bone { return data; } + public Skeleton getSkeleton () { + return skeleton; + } + public Bone getParent () { return parent; } @@ -250,7 +262,8 @@ public class Bone { public Vector2 worldToLocal (Vector2 world) { float x = world.x - worldX, y = world.y - worldY; float m00 = this.m00, m10 = this.m10, m01 = this.m01, m11 = this.m11; - if (flipX != flipY) { + Skeleton skeleton = this.skeleton; + if (skeleton.flipX != skeleton.flipY) { m00 *= -1; m11 *= -1; } diff --git a/spine-libgdx/src/com/esotericsoftware/spine/Skeleton.java b/spine-libgdx/src/com/esotericsoftware/spine/Skeleton.java index 90d424667..427bf7d9d 100644 --- a/spine-libgdx/src/com/esotericsoftware/spine/Skeleton.java +++ b/spine-libgdx/src/com/esotericsoftware/spine/Skeleton.java @@ -41,7 +41,7 @@ public class Skeleton { final Array slots; Array drawOrder; final Array ikConstraints; - private final Array> updateBonesCache = new Array(); + private final Array> bonesCache = new Array(); Skin skin; final Color color; float time; @@ -55,14 +55,14 @@ public class Skeleton { bones = new Array(data.bones.size); for (BoneData boneData : data.bones) { Bone parent = boneData.parent == null ? null : bones.get(data.bones.indexOf(boneData.parent, true)); - bones.add(new Bone(boneData, parent)); + bones.add(new Bone(boneData, this, parent)); } slots = new Array(data.slots.size); drawOrder = new Array(data.slots.size); for (SlotData slotData : data.slots) { Bone bone = bones.get(data.bones.indexOf(slotData.boneData, true)); - Slot slot = new Slot(slotData, this, bone); + Slot slot = new Slot(slotData, bone); slots.add(slot); drawOrder.add(slot); } @@ -84,13 +84,13 @@ public class Skeleton { bones = new Array(skeleton.bones.size); for (Bone bone : skeleton.bones) { Bone parent = bone.parent == null ? null : bones.get(skeleton.bones.indexOf(bone.parent, true)); - bones.add(new Bone(bone, parent)); + bones.add(new Bone(bone, this, parent)); } slots = new Array(skeleton.slots.size); for (Slot slot : skeleton.slots) { Bone bone = bones.get(skeleton.bones.indexOf(slot.bone, true)); - slots.add(new Slot(slot, this, bone)); + slots.add(new Slot(slot, bone)); } drawOrder = new Array(slots.size); @@ -117,18 +117,18 @@ public class Skeleton { /** Caches information about bones and IK constraints. Must be called if bones or IK constraints are added or removed. */ public void updateCache () { - Array> updateBonesCache = this.updateBonesCache; + Array> bonesCache = this.bonesCache; Array ikConstraints = this.ikConstraints; int ikConstraintsCount = ikConstraints.size; int arrayCount = ikConstraintsCount + 1; - updateBonesCache.truncate(arrayCount); - for (int i = 0, n = updateBonesCache.size; i < n; i++) - updateBonesCache.get(i).clear(); - while (updateBonesCache.size < arrayCount) - updateBonesCache.add(new Array()); + bonesCache.truncate(arrayCount); + for (int i = 0, n = bonesCache.size; i < n; i++) + bonesCache.get(i).clear(); + while (bonesCache.size < arrayCount) + bonesCache.add(new Array()); - Array nonIkBones = updateBonesCache.first(); + Array nonIkBones = bonesCache.first(); outer: for (int i = 0, n = bones.size; i < n; i++) { @@ -141,8 +141,8 @@ public class Skeleton { Bone child = ikConstraint.bones.peek(); while (true) { if (current == child) { - updateBonesCache.get(ii).add(bone); - updateBonesCache.get(ii + 1).add(bone); + bonesCache.get(ii).add(bone); + bonesCache.get(ii + 1).add(bone); continue outer; } if (child == parent) break; @@ -162,11 +162,11 @@ public class Skeleton { Bone bone = bones.get(i); bone.rotationIK = bone.rotation; } - Array> updateBonesCache = this.updateBonesCache; + Array> bonesCache = this.bonesCache; Array ikConstraints = this.ikConstraints; - int i = 0, last = updateBonesCache.size - 1; + int i = 0, last = bonesCache.size - 1; while (true) { - Array updateBones = updateBonesCache.get(i); + Array updateBones = bonesCache.get(i); for (int ii = 0, nn = updateBones.size; ii < nn; ii++) updateBones.get(ii).updateWorldTransform(); if (i == last) break; @@ -365,11 +365,7 @@ public class Skeleton { } public void setFlipX (boolean flipX) { - if (this.flipX == flipX) return; this.flipX = flipX; - Array bones = this.bones; - for (int i = 0, n = bones.size; i < n; i++) - bones.get(i).flipX = flipX; } public boolean getFlipY () { @@ -377,21 +373,12 @@ public class Skeleton { } public void setFlipY (boolean flipY) { - if (this.flipY == flipY) return; this.flipY = flipY; - Array bones = this.bones; - for (int i = 0, n = bones.size; i < n; i++) - bones.get(i).flipY = flipY; } public void setFlip (boolean flipX, boolean flipY) { - if (this.flipX == flipX && this.flipY == flipY) return; - Array bones = this.bones; - for (int i = 0, n = bones.size; i < n; i++) { - Bone bone = bones.get(i); - bone.flipX = flipX; - bone.flipY = flipY; - } + this.flipX = flipX; + this.flipY = flipY; } public float getX () { diff --git a/spine-libgdx/src/com/esotericsoftware/spine/Slot.java b/spine-libgdx/src/com/esotericsoftware/spine/Slot.java index e100e221d..9990dbc2e 100644 --- a/spine-libgdx/src/com/esotericsoftware/spine/Slot.java +++ b/spine-libgdx/src/com/esotericsoftware/spine/Slot.java @@ -38,37 +38,31 @@ import com.badlogic.gdx.utils.FloatArray; public class Slot { final SlotData data; final Bone bone; - private final Skeleton skeleton; final Color color; Attachment attachment; private float attachmentTime; private FloatArray attachmentVertices = new FloatArray(); - Slot () { - data = null; + Slot (SlotData data) { + this.data = data; bone = null; - skeleton = null; color = new Color(1, 1, 1, 1); } - public Slot (SlotData data, Skeleton skeleton, Bone bone) { + public Slot (SlotData data, Bone bone) { if (data == null) throw new IllegalArgumentException("data cannot be null."); - if (skeleton == null) throw new IllegalArgumentException("skeleton cannot be null."); if (bone == null) throw new IllegalArgumentException("bone cannot be null."); this.data = data; - this.skeleton = skeleton; this.bone = bone; color = new Color(); setToSetupPose(); } /** Copy constructor. */ - public Slot (Slot slot, Skeleton skeleton, Bone bone) { + public Slot (Slot slot, Bone bone) { if (slot == null) throw new IllegalArgumentException("slot cannot be null."); - if (skeleton == null) throw new IllegalArgumentException("skeleton cannot be null."); if (bone == null) throw new IllegalArgumentException("bone cannot be null."); data = slot.data; - this.skeleton = skeleton; this.bone = bone; color = new Color(slot.color); attachment = slot.attachment; @@ -79,14 +73,14 @@ public class Slot { return data; } - public Skeleton getSkeleton () { - return skeleton; - } - public Bone getBone () { return bone; } + public Skeleton getSkeleton () { + return bone.skeleton; + } + public Color getColor () { return color; } @@ -101,17 +95,17 @@ public class Slot { public void setAttachment (Attachment attachment) { if (this.attachment == attachment) return; this.attachment = attachment; - attachmentTime = skeleton.time; + attachmentTime = bone.skeleton.time; attachmentVertices.clear(); } public void setAttachmentTime (float time) { - attachmentTime = skeleton.time - time; + attachmentTime = bone.skeleton.time - time; } /** Returns the time since the attachment was set. */ public float getAttachmentTime () { - return skeleton.time - attachmentTime; + return bone.skeleton.time - attachmentTime; } public void setAttachmentVertices (FloatArray attachmentVertices) { @@ -124,12 +118,12 @@ public class Slot { void setToSetupPose (int slotIndex) { color.set(data.color); - setAttachment(data.attachmentName == null ? null : skeleton.getAttachment(slotIndex, data.attachmentName)); + setAttachment(data.attachmentName == null ? null : bone.skeleton.getAttachment(slotIndex, data.attachmentName)); attachmentVertices.clear(); } public void setToSetupPose () { - setToSetupPose(skeleton.data.slots.indexOf(data, true)); + setToSetupPose(bone.skeleton.data.slots.indexOf(data, true)); } public String toString () {