From e14a783c385cac12d6d873e7a987d76c5f105d2d Mon Sep 17 00:00:00 2001 From: NathanSweet Date: Fri, 28 Oct 2016 23:11:30 +0200 Subject: [PATCH] Removed findXxxIndex. These are just noise now that bones and slots have an index field. --- .../com/esotericsoftware/spine/Skeleton.java | 48 ++++++------------- .../spine/SkeletonBinary.java | 2 +- .../esotericsoftware/spine/SkeletonData.java | 27 ----------- .../esotericsoftware/spine/SkeletonJson.java | 44 ++++++++--------- 4 files changed, 37 insertions(+), 84 deletions(-) diff --git a/spine-libgdx/spine-libgdx/src/com/esotericsoftware/spine/Skeleton.java b/spine-libgdx/spine-libgdx/src/com/esotericsoftware/spine/Skeleton.java index 5de30788a..28a95f129 100644 --- a/spine-libgdx/spine-libgdx/src/com/esotericsoftware/spine/Skeleton.java +++ b/spine-libgdx/spine-libgdx/src/com/esotericsoftware/spine/Skeleton.java @@ -388,15 +388,6 @@ public class Skeleton { return null; } - /** @return -1 if the bone was not found. */ - public int findBoneIndex (String boneName) { - if (boneName == null) throw new IllegalArgumentException("boneName cannot be null."); - Array bones = this.bones; - for (int i = 0, n = bones.size; i < n; i++) - if (bones.get(i).data.name.equals(boneName)) return i; - return -1; - } - public Array getSlots () { return slots; } @@ -412,15 +403,6 @@ public class Skeleton { return null; } - /** @return -1 if the bone was not found. */ - public int findSlotIndex (String slotName) { - if (slotName == null) throw new IllegalArgumentException("slotName cannot be null."); - Array slots = this.slots; - for (int i = 0, n = slots.size; i < n; i++) - if (slots.get(i).data.name.equals(slotName)) return i; - return -1; - } - /** Returns the slots in the order they will be drawn. The returned array may be modified to change the draw order. */ public Array getDrawOrder () { return drawOrder; @@ -470,7 +452,9 @@ public class Skeleton { /** @return May be null. */ public Attachment getAttachment (String slotName, String attachmentName) { - return getAttachment(data.findSlotIndex(slotName), attachmentName); + SlotData slot = data.findSlot(slotName); + if (slot == null) throw new IllegalArgumentException("Slot not found: " + slotName); + return getAttachment(slot.getIndex(), attachmentName); } /** @return May be null. */ @@ -484,24 +468,20 @@ public class Skeleton { return null; } - /** @param attachmentName May be null. */ + /** Sets an attachment by finding the slot with {@link #findSlot(String)}, finding the attachment with + * {@link #getAttachment(int, String)}, then sets the slot's {@link Slot#attachment}. + * @param attachmentName May be null to clear the slot. */ public void setAttachment (String slotName, String attachmentName) { if (slotName == null) throw new IllegalArgumentException("slotName cannot be null."); - Array slots = this.slots; - for (int i = 0, n = slots.size; i < n; i++) { - Slot slot = slots.get(i); - if (slot.data.name.equals(slotName)) { - Attachment attachment = null; - if (attachmentName != null) { - attachment = getAttachment(i, attachmentName); - if (attachment == null) - throw new IllegalArgumentException("Attachment not found: " + attachmentName + ", for slot: " + slotName); - } - slot.setAttachment(attachment); - return; - } + Slot slot = findSlot(slotName); + if (slot == null) throw new IllegalArgumentException("Slot not found: " + slotName); + Attachment attachment = null; + if (attachmentName != null) { + attachment = getAttachment(slot.data.index, attachmentName); + if (attachment == null) + throw new IllegalArgumentException("Attachment not found: " + attachmentName + ", for slot: " + slotName); } - throw new IllegalArgumentException("Slot not found: " + slotName); + slot.setAttachment(attachment); } public Array getIkConstraints () { diff --git a/spine-libgdx/spine-libgdx/src/com/esotericsoftware/spine/SkeletonBinary.java b/spine-libgdx/spine-libgdx/src/com/esotericsoftware/spine/SkeletonBinary.java index 97f3ea1e8..5dbccaa17 100644 --- a/spine-libgdx/spine-libgdx/src/com/esotericsoftware/spine/SkeletonBinary.java +++ b/spine-libgdx/spine-libgdx/src/com/esotericsoftware/spine/SkeletonBinary.java @@ -617,7 +617,7 @@ public class SkeletonBinary { // Path constraint timelines. for (int i = 0, n = input.readInt(true); i < n; i++) { int index = input.readInt(true); - PathConstraintData data = skeletonData.getPathConstraints().get(index); + PathConstraintData data = skeletonData.pathConstraints.get(index); for (int ii = 0, nn = input.readInt(true); ii < nn; ii++) { int timelineType = input.readByte(); int frameCount = input.readInt(true); diff --git a/spine-libgdx/spine-libgdx/src/com/esotericsoftware/spine/SkeletonData.java b/spine-libgdx/spine-libgdx/src/com/esotericsoftware/spine/SkeletonData.java index e1eef741c..31b7a61c2 100644 --- a/spine-libgdx/spine-libgdx/src/com/esotericsoftware/spine/SkeletonData.java +++ b/spine-libgdx/spine-libgdx/src/com/esotericsoftware/spine/SkeletonData.java @@ -67,15 +67,6 @@ public class SkeletonData { return null; } - /** @return -1 if the bone was not found. */ - public int findBoneIndex (String boneName) { - if (boneName == null) throw new IllegalArgumentException("boneName cannot be null."); - Array bones = this.bones; - for (int i = 0, n = bones.size; i < n; i++) - if (bones.get(i).name.equals(boneName)) return i; - return -1; - } - // --- Slots. public Array getSlots () { @@ -93,15 +84,6 @@ public class SkeletonData { return null; } - /** @return -1 if the slot was not found. */ - public int findSlotIndex (String slotName) { - if (slotName == null) throw new IllegalArgumentException("slotName cannot be null."); - Array slots = this.slots; - for (int i = 0, n = slots.size; i < n; i++) - if (slots.get(i).name.equals(slotName)) return i; - return -1; - } - // --- Skins. /** @return May be null. */ @@ -209,15 +191,6 @@ public class SkeletonData { return null; } - /** @return -1 if the path constraint was not found. */ - public int findPathConstraintIndex (String pathConstraintName) { - if (pathConstraintName == null) throw new IllegalArgumentException("pathConstraintName cannot be null."); - Array pathConstraints = this.pathConstraints; - for (int i = 0, n = pathConstraints.size; i < n; i++) - if (pathConstraints.get(i).name.equals(pathConstraintName)) return i; - return -1; - } - // --- /** @return May be null. */ diff --git a/spine-libgdx/spine-libgdx/src/com/esotericsoftware/spine/SkeletonJson.java b/spine-libgdx/spine-libgdx/src/com/esotericsoftware/spine/SkeletonJson.java index 62a5b07f9..045e3d80f 100644 --- a/spine-libgdx/spine-libgdx/src/com/esotericsoftware/spine/SkeletonJson.java +++ b/spine-libgdx/spine-libgdx/src/com/esotericsoftware/spine/SkeletonJson.java @@ -241,12 +241,12 @@ public class SkeletonJson { for (JsonValue skinMap = root.getChild("skins"); skinMap != null; skinMap = skinMap.next) { Skin skin = new Skin(skinMap.name); for (JsonValue slotEntry = skinMap.child; slotEntry != null; slotEntry = slotEntry.next) { - int slotIndex = skeletonData.findSlotIndex(slotEntry.name); - if (slotIndex == -1) throw new SerializationException("Slot not found: " + slotEntry.name); + SlotData slot = skeletonData.findSlot(slotEntry.name); + if (slot == null) throw new SerializationException("Slot not found: " + slotEntry.name); for (JsonValue entry = slotEntry.child; entry != null; entry = entry.next) { try { - Attachment attachment = readAttachment(entry, skin, slotIndex, entry.name); - if (attachment != null) skin.addAttachment(slotIndex, entry.name, attachment); + Attachment attachment = readAttachment(entry, skin, slot.index, entry.name); + if (attachment != null) skin.addAttachment(slot.index, entry.name, attachment); } catch (Exception ex) { throw new SerializationException("Error reading attachment: " + entry.name + ", skin: " + skin, ex); } @@ -417,13 +417,13 @@ public class SkeletonJson { // Slot timelines. for (JsonValue slotMap = map.getChild("slots"); slotMap != null; slotMap = slotMap.next) { - int slotIndex = skeletonData.findSlotIndex(slotMap.name); - if (slotIndex == -1) throw new SerializationException("Slot not found: " + slotMap.name); + SlotData slot = skeletonData.findSlot(slotMap.name); + if (slot == null) throw new SerializationException("Slot not found: " + slotMap.name); for (JsonValue timelineMap = slotMap.child; timelineMap != null; timelineMap = timelineMap.next) { String timelineName = timelineMap.name; if (timelineName.equals("color")) { ColorTimeline timeline = new ColorTimeline(timelineMap.size); - timeline.slotIndex = slotIndex; + timeline.slotIndex = slot.index; int frameIndex = 0; for (JsonValue valueMap = timelineMap.child; valueMap != null; valueMap = valueMap.next) { @@ -437,7 +437,7 @@ public class SkeletonJson { } else if (timelineName.equals("attachment")) { AttachmentTimeline timeline = new AttachmentTimeline(timelineMap.size); - timeline.slotIndex = slotIndex; + timeline.slotIndex = slot.index; int frameIndex = 0; for (JsonValue valueMap = timelineMap.child; valueMap != null; valueMap = valueMap.next) @@ -451,13 +451,13 @@ public class SkeletonJson { // Bone timelines. for (JsonValue boneMap = map.getChild("bones"); boneMap != null; boneMap = boneMap.next) { - int boneIndex = skeletonData.findBoneIndex(boneMap.name); - if (boneIndex == -1) throw new SerializationException("Bone not found: " + boneMap.name); + BoneData bone = skeletonData.findBone(boneMap.name); + if (bone == null) throw new SerializationException("Bone not found: " + boneMap.name); for (JsonValue timelineMap = boneMap.child; timelineMap != null; timelineMap = timelineMap.next) { String timelineName = timelineMap.name; if (timelineName.equals("rotate")) { RotateTimeline timeline = new RotateTimeline(timelineMap.size); - timeline.boneIndex = boneIndex; + timeline.boneIndex = bone.index; int frameIndex = 0; for (JsonValue valueMap = timelineMap.child; valueMap != null; valueMap = valueMap.next) { @@ -479,7 +479,7 @@ public class SkeletonJson { timeline = new TranslateTimeline(timelineMap.size); timelineScale = scale; } - timeline.boneIndex = boneIndex; + timeline.boneIndex = bone.index; int frameIndex = 0; for (JsonValue valueMap = timelineMap.child; valueMap != null; valueMap = valueMap.next) { @@ -531,9 +531,9 @@ public class SkeletonJson { // Path constraint timelines. for (JsonValue constraintMap = map.getChild("paths"); constraintMap != null; constraintMap = constraintMap.next) { - int index = skeletonData.findPathConstraintIndex(constraintMap.name); - if (index == -1) throw new SerializationException("Path constraint not found: " + constraintMap.name); - PathConstraintData data = skeletonData.getPathConstraints().get(index); + PathConstraintData data = skeletonData.findPathConstraint(constraintMap.name); + if (data == null) throw new SerializationException("Path constraint not found: " + constraintMap.name); + int index = skeletonData.pathConstraints.indexOf(data, true); for (JsonValue timelineMap = constraintMap.child; timelineMap != null; timelineMap = timelineMap.next) { String timelineName = timelineMap.name; if (timelineName.equals("position") || timelineName.equals("spacing")) { @@ -578,17 +578,17 @@ public class SkeletonJson { Skin skin = skeletonData.findSkin(deformMap.name); if (skin == null) throw new SerializationException("Skin not found: " + deformMap.name); for (JsonValue slotMap = deformMap.child; slotMap != null; slotMap = slotMap.next) { - int slotIndex = skeletonData.findSlotIndex(slotMap.name); - if (slotIndex == -1) throw new SerializationException("Slot not found: " + slotMap.name); + SlotData slot = skeletonData.findSlot(slotMap.name); + if (slot == null) throw new SerializationException("Slot not found: " + slotMap.name); for (JsonValue timelineMap = slotMap.child; timelineMap != null; timelineMap = timelineMap.next) { - VertexAttachment attachment = (VertexAttachment)skin.getAttachment(slotIndex, timelineMap.name); + VertexAttachment attachment = (VertexAttachment)skin.getAttachment(slot.index, timelineMap.name); if (attachment == null) throw new SerializationException("Deform attachment not found: " + timelineMap.name); boolean weighted = attachment.getBones() != null; float[] vertices = attachment.getVertices(); int deformLength = weighted ? vertices.length / 3 * 2 : vertices.length; DeformTimeline timeline = new DeformTimeline(timelineMap.size); - timeline.slotIndex = slotIndex; + timeline.slotIndex = slot.index; timeline.attachment = attachment; int frameIndex = 0; @@ -638,10 +638,10 @@ public class SkeletonJson { int[] unchanged = new int[slotCount - offsets.size]; int originalIndex = 0, unchangedIndex = 0; for (JsonValue offsetMap = offsets.child; offsetMap != null; offsetMap = offsetMap.next) { - int slotIndex = skeletonData.findSlotIndex(offsetMap.getString("slot")); - if (slotIndex == -1) throw new SerializationException("Slot not found: " + offsetMap.getString("slot")); + SlotData slot = skeletonData.findSlot(offsetMap.getString("slot")); + if (slot == null) throw new SerializationException("Slot not found: " + offsetMap.getString("slot")); // Collect unchanged items. - while (originalIndex != slotIndex) + while (originalIndex != slot.index) unchanged[unchangedIndex++] = originalIndex++; // Set changed items. drawOrder[originalIndex + offsetMap.getInt("offset")] = originalIndex++;