Refactoring and clean up.

This commit is contained in:
NathanSweet 2014-08-31 15:21:47 +02:00
parent 2cfb6ad980
commit 2d5b2cdb87
9 changed files with 37 additions and 39 deletions

View File

@ -42,9 +42,8 @@ namespace Spine {
} }
/// <param name="worldVertices">Must have at least the same length as this attachment's vertices.</param> /// <param name="worldVertices">Must have at least the same length as this attachment's vertices.</param>
public void ComputeWorldVertices (float x, float y, Bone bone, float[] worldVertices) { public void ComputeWorldVertices (Bone bone, float[] worldVertices) {
x += bone.worldX; float x = bone.skeleton.x + bone.worldX, y = bone.skeleton.y + bone.worldY;
y += bone.worldY;
float m00 = bone.m00; float m00 = bone.m00;
float m01 = bone.m01; float m01 = bone.m01;
float m10 = bone.m10; float m10 = bone.m10;

View File

@ -38,7 +38,7 @@ namespace Spine {
internal List<Slot> slots; internal List<Slot> slots;
internal List<Slot> drawOrder; internal List<Slot> drawOrder;
internal List<IkConstraint> ikConstraints = new List<IkConstraint>(); internal List<IkConstraint> ikConstraints = new List<IkConstraint>();
private List<List<Bone>> bonesCache = new List<List<Bone>>(); private List<List<Bone>> boneCache = new List<List<Bone>>();
internal Skin skin; internal Skin skin;
internal float r = 1, g = 1, b = 1, a = 1; internal float r = 1, g = 1, b = 1, a = 1;
internal float time; internal float time;
@ -96,18 +96,18 @@ namespace Spine {
/// <summary>Caches information about bones and IK constraints. Must be called if bones or IK constraints are added or /// <summary>Caches information about bones and IK constraints. Must be called if bones or IK constraints are added or
/// removed.</summary> /// removed.</summary>
public void UpdateCache () { public void UpdateCache () {
List<List<Bone>> bonesCache = this.bonesCache; List<List<Bone>> boneCache = this.boneCache;
List<IkConstraint> ikConstraints = this.ikConstraints; List<IkConstraint> ikConstraints = this.ikConstraints;
int ikConstraintsCount = ikConstraints.Count; int ikConstraintsCount = ikConstraints.Count;
int arrayCount = ikConstraintsCount + 1; int arrayCount = ikConstraintsCount + 1;
if (bonesCache.Count > arrayCount) bonesCache.RemoveRange(arrayCount, bonesCache.Count - arrayCount); if (boneCache.Count > arrayCount) boneCache.RemoveRange(arrayCount, boneCache.Count - arrayCount);
for (int i = 0, n = bonesCache.Count; i < n; i++) for (int i = 0, n = boneCache.Count; i < n; i++)
bonesCache[i].Clear(); boneCache[i].Clear();
while (bonesCache.Count < arrayCount) while (boneCache.Count < arrayCount)
bonesCache.Add(new List<Bone>()); boneCache.Add(new List<Bone>());
List<Bone> nonIkBones = bonesCache[0]; List<Bone> nonIkBones = boneCache[0];
for (int i = 0, n = bones.Count; i < n; i++) { for (int i = 0, n = bones.Count; i < n; i++) {
Bone bone = bones[i]; Bone bone = bones[i];
@ -119,8 +119,8 @@ namespace Spine {
Bone child = ikConstraint.bones[ikConstraint.bones.Count - 1]; Bone child = ikConstraint.bones[ikConstraint.bones.Count - 1];
while (true) { while (true) {
if (current == child) { if (current == child) {
bonesCache[ii].Add(bone); boneCache[ii].Add(bone);
bonesCache[ii + 1].Add(bone); boneCache[ii + 1].Add(bone);
goto outer; goto outer;
} }
if (child == parent) break; if (child == parent) break;
@ -141,11 +141,11 @@ namespace Spine {
Bone bone = bones[ii]; Bone bone = bones[ii];
bone.rotationIK = bone.rotation; bone.rotationIK = bone.rotation;
} }
List<List<Bone>> bonesCache = this.bonesCache; List<List<Bone>> boneCache = this.boneCache;
List<IkConstraint> ikConstraints = this.ikConstraints; List<IkConstraint> ikConstraints = this.ikConstraints;
int i = 0, last = bonesCache.Count - 1; int i = 0, last = boneCache.Count - 1;
while (true) { while (true) {
List<Bone> updateBones = bonesCache[i]; List<Bone> updateBones = boneCache[i];
for (int ii = 0, nn = updateBones.Count; ii < nn; ii++) for (int ii = 0, nn = updateBones.Count; ii < nn; ii++)
updateBones[ii].UpdateWorldTransform(); updateBones[ii].UpdateWorldTransform();
if (i == last) break; if (i == last) break;

View File

@ -55,7 +55,6 @@ namespace Spine {
List<Polygon> polygons = Polygons; List<Polygon> polygons = Polygons;
List<Slot> slots = skeleton.slots; List<Slot> slots = skeleton.slots;
int slotCount = slots.Count; int slotCount = slots.Count;
float x = skeleton.x, y = skeleton.y;
boundingBoxes.Clear(); boundingBoxes.Clear();
foreach (Polygon polygon in polygons) foreach (Polygon polygon in polygons)
@ -80,7 +79,7 @@ namespace Spine {
int count = boundingBox.Vertices.Length; int count = boundingBox.Vertices.Length;
polygon.Count = count; polygon.Count = count;
if (polygon.Vertices.Length < count) polygon.Vertices = new float[count]; if (polygon.Vertices.Length < count) polygon.Vertices = new float[count];
boundingBox.ComputeWorldVertices(x, y, slot.bone, polygon.Vertices); boundingBox.ComputeWorldVertices(slot.bone, polygon.Vertices);
} }
if (updateAabb) aabbCompute(); if (updateAabb) aabbCompute();

View File

@ -93,8 +93,8 @@ namespace Spine {
// Skeleton. // Skeleton.
if (root.ContainsKey("skeleton")) { if (root.ContainsKey("skeleton")) {
var skeletonMap = (Dictionary<String, Object>)root["skeleton"]; var skeletonMap = (Dictionary<String, Object>)root["skeleton"];
skeletonData.version = (String)skeletonMap["spine"];
skeletonData.hash = (String)skeletonMap["hash"]; skeletonData.hash = (String)skeletonMap["hash"];
skeletonData.version = (String)skeletonMap["spine"];
skeletonData.width = GetFloat(skeletonMap, "width", 0); skeletonData.width = GetFloat(skeletonMap, "width", 0);
skeletonData.height = GetFloat(skeletonMap, "width", 1); skeletonData.height = GetFloat(skeletonMap, "width", 1);
} }

View File

@ -41,7 +41,7 @@ public class Skeleton {
final Array<Slot> slots; final Array<Slot> slots;
Array<Slot> drawOrder; Array<Slot> drawOrder;
final Array<IkConstraint> ikConstraints; final Array<IkConstraint> ikConstraints;
private final Array<Array<Bone>> bonesCache = new Array(); private final Array<Array<Bone>> boneCache = new Array();
Skin skin; Skin skin;
final Color color; final Color color;
float time; float time;
@ -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. */ /** Caches information about bones and IK constraints. Must be called if bones or IK constraints are added or removed. */
public void updateCache () { public void updateCache () {
Array<Array<Bone>> bonesCache = this.bonesCache; Array<Array<Bone>> boneCache = this.boneCache;
Array<IkConstraint> ikConstraints = this.ikConstraints; Array<IkConstraint> ikConstraints = this.ikConstraints;
int ikConstraintsCount = ikConstraints.size; int ikConstraintsCount = ikConstraints.size;
int arrayCount = ikConstraintsCount + 1; int arrayCount = ikConstraintsCount + 1;
bonesCache.truncate(arrayCount); boneCache.truncate(arrayCount);
for (int i = 0, n = bonesCache.size; i < n; i++) for (int i = 0, n = boneCache.size; i < n; i++)
bonesCache.get(i).clear(); boneCache.get(i).clear();
while (bonesCache.size < arrayCount) while (boneCache.size < arrayCount)
bonesCache.add(new Array()); boneCache.add(new Array());
Array<Bone> nonIkBones = bonesCache.first(); Array<Bone> nonIkBones = boneCache.first();
outer: outer:
for (int i = 0, n = bones.size; i < n; i++) { for (int i = 0, n = bones.size; i < n; i++) {
@ -141,8 +141,8 @@ public class Skeleton {
Bone child = ikConstraint.bones.peek(); Bone child = ikConstraint.bones.peek();
while (true) { while (true) {
if (current == child) { if (current == child) {
bonesCache.get(ii).add(bone); boneCache.get(ii).add(bone);
bonesCache.get(ii + 1).add(bone); boneCache.get(ii + 1).add(bone);
continue outer; continue outer;
} }
if (child == parent) break; if (child == parent) break;
@ -162,11 +162,11 @@ public class Skeleton {
Bone bone = bones.get(i); Bone bone = bones.get(i);
bone.rotationIK = bone.rotation; bone.rotationIK = bone.rotation;
} }
Array<Array<Bone>> bonesCache = this.bonesCache; Array<Array<Bone>> boneCache = this.boneCache;
Array<IkConstraint> ikConstraints = this.ikConstraints; Array<IkConstraint> ikConstraints = this.ikConstraints;
int i = 0, last = bonesCache.size - 1; int i = 0, last = boneCache.size - 1;
while (true) { while (true) {
Array<Bone> updateBones = bonesCache.get(i); Array<Bone> updateBones = boneCache.get(i);
for (int ii = 0, nn = updateBones.size; ii < nn; ii++) for (int ii = 0, nn = updateBones.size; ii < nn; ii++)
updateBones.get(ii).updateWorldTransform(); updateBones.get(ii).updateWorldTransform();
if (i == last) break; if (i == last) break;

View File

@ -107,8 +107,8 @@ public class SkeletonBinary {
DataInput input = new DataInput(file.read(512)); DataInput input = new DataInput(file.read(512));
try { try {
skeletonData.version = input.readString();
skeletonData.hash = input.readString(); skeletonData.hash = input.readString();
skeletonData.version = input.readString();
skeletonData.width = input.readFloat(); skeletonData.width = input.readFloat();
skeletonData.height = input.readFloat(); skeletonData.height = input.readFloat();

View File

@ -52,7 +52,6 @@ public class SkeletonBounds {
Array<FloatArray> polygons = this.polygons; Array<FloatArray> polygons = this.polygons;
Array<Slot> slots = skeleton.slots; Array<Slot> slots = skeleton.slots;
int slotCount = slots.size; int slotCount = slots.size;
float x = skeleton.getX(), y = skeleton.getY();
boundingBoxes.clear(); boundingBoxes.clear();
polygonPool.freeAll(polygons); polygonPool.freeAll(polygons);
@ -71,7 +70,7 @@ public class SkeletonBounds {
polygon.ensureCapacity(vertexCount); polygon.ensureCapacity(vertexCount);
polygon.size = vertexCount; polygon.size = vertexCount;
boundingBox.computeWorldVertices(x, y, slot.bone, polygon.items); boundingBox.computeWorldVertices(slot.bone, polygon.items);
} }
} }

View File

@ -93,8 +93,8 @@ public class SkeletonJson {
// Skeleton. // Skeleton.
JsonValue skeletonMap = root.get("skeleton"); JsonValue skeletonMap = root.get("skeleton");
if (skeletonMap != null) { if (skeletonMap != null) {
skeletonData.version = skeletonMap.getString("spine");
skeletonData.hash = skeletonMap.getString("hash"); skeletonData.hash = skeletonMap.getString("hash");
skeletonData.version = skeletonMap.getString("spine");
skeletonData.width = skeletonMap.getFloat("width"); skeletonData.width = skeletonMap.getFloat("width");
skeletonData.height = skeletonMap.getFloat("height"); skeletonData.height = skeletonMap.getFloat("height");
} }

View File

@ -31,6 +31,7 @@
package com.esotericsoftware.spine.attachments; package com.esotericsoftware.spine.attachments;
import com.esotericsoftware.spine.Bone; import com.esotericsoftware.spine.Bone;
import com.esotericsoftware.spine.Skeleton;
public class BoundingBoxAttachment extends Attachment { public class BoundingBoxAttachment extends Attachment {
private float[] vertices; private float[] vertices;
@ -39,9 +40,9 @@ public class BoundingBoxAttachment extends Attachment {
super(name); super(name);
} }
public void computeWorldVertices (float x, float y, Bone bone, float[] worldVertices) { public void computeWorldVertices (Bone bone, float[] worldVertices) {
x += bone.getWorldX(); Skeleton skeleton = bone.getSkeleton();
y += bone.getWorldY(); float x = skeleton.getX() + bone.getWorldX(), y = skeleton.getY() + bone.getWorldY();
float m00 = bone.getM00(); float m00 = bone.getM00();
float m01 = bone.getM01(); float m01 = bone.getM01();
float m10 = bone.getM10(); float m10 = bone.getM10();