Browse Source

[libgdx] SkinEntry equals and hashCode do not take the attachment field into account. When setting a new SkinEntry for an existing slot + name combination, the old key with the old attachment stays in the keys table of the OrderedMap. Upon a call to getAttachments() that keys table is returned, which means the old attachment is returned instead of the newly set attachment. This commit fixes this buggy behaviour by storing SkinEntries as values, giving us access to the re-used key on which we can then update the attachment field. I'm sorry. Closes #1485.

badlogic 6 years ago
parent
commit
0ad6eb1b8d

+ 8 - 4
spine-libgdx/spine-libgdx/src/com/esotericsoftware/spine/Skin.java

@@ -31,7 +31,6 @@ package com.esotericsoftware.spine;
 
 import com.badlogic.gdx.utils.Array;
 import com.badlogic.gdx.utils.OrderedMap;
-
 import com.esotericsoftware.spine.attachments.Attachment;
 import com.esotericsoftware.spine.attachments.MeshAttachment;
 
@@ -41,7 +40,7 @@ import com.esotericsoftware.spine.attachments.MeshAttachment;
  * <a href="http://esotericsoftware.com/spine-runtime-skins">Runtime skins</a> in the Spine Runtimes Guide. */
 public class Skin {
 	final String name;
-	final OrderedMap<SkinEntry, Attachment> attachments = new OrderedMap();
+	final OrderedMap<SkinEntry, SkinEntry> attachments = new OrderedMap();
 	final Array<BoneData> bones = new Array();
 	final Array<ConstraintData> constraints = new Array();
 	private final SkinEntry lookup = new SkinEntry();
@@ -56,7 +55,11 @@ public class Skin {
 	public void setAttachment (int slotIndex, String name, Attachment attachment) {
 		if (slotIndex < 0) throw new IllegalArgumentException("slotIndex must be >= 0.");
 		if (attachment == null) throw new IllegalArgumentException("attachment cannot be null.");
-		attachments.put(new SkinEntry(slotIndex, name, attachment), attachment);
+		SkinEntry newEntry = new SkinEntry(slotIndex, name, attachment);
+		SkinEntry oldEntry = attachments.put(newEntry, newEntry);
+		if (oldEntry != null) {
+			oldEntry.attachment = attachment;
+		}
 	}
 
 	/** Adds all attachments, bones, and constraints from the specified skin to this skin. */
@@ -96,7 +99,8 @@ public class Skin {
 	public Attachment getAttachment (int slotIndex, String name) {
 		if (slotIndex < 0) throw new IllegalArgumentException("slotIndex must be >= 0.");
 		lookup.set(slotIndex, name);
-		return attachments.get(lookup);
+		SkinEntry entry = attachments.get(lookup);
+		return entry != null ? entry.attachment : null;
 	}
 
 	/** Removes the attachment in the skin for the specified slot index and name, if any. */