spine-starling, use Vector of Dictionarys to avoid allocation to look up skin attachments.

#236
This commit is contained in:
NathanSweet 2014-07-25 23:04:32 +02:00
parent dee15d16b3
commit 6d398233d7

View File

@ -29,12 +29,14 @@
*****************************************************************************/ *****************************************************************************/
package spine { package spine {
import flash.utils.Dictionary;
import spine.attachments.Attachment; import spine.attachments.Attachment;
/** Stores attachments by slot index and attachment name. */ /** Stores attachments by slot index and attachment name. */
public class Skin { public class Skin {
internal var _name:String; internal var _name:String;
private var _attachments:Object = new Object(); private var _attachments:Vector.<Dictionary> = new Vector.<Dictionary>();
public function Skin (name:String) { public function Skin (name:String) {
if (name == null) if (name == null)
@ -45,15 +47,19 @@ public class Skin {
public function addAttachment (slotIndex:int, name:String, attachment:Attachment) : void { public function addAttachment (slotIndex:int, name:String, attachment:Attachment) : void {
if (attachment == null) if (attachment == null)
throw new ArgumentError("attachment cannot be null."); throw new ArgumentError("attachment cannot be null.");
_attachments[slotIndex + ":" + name] = attachment; if (slotIndex >= attachments.length) attachments.length = slotIndex + 1;
if (!attachments[slotIndex]) attachments[slotIndex] = new Dictionary();
attachments[slotIndex][name] = attachment;
} }
/** @return May be null. */ /** @return May be null. */
public function getAttachment (slotIndex:int, name:String) : Attachment { public function getAttachment (slotIndex:int, name:String) : Attachment {
return _attachments[slotIndex + ":" + name]; if (slotIndex >= attachments.length) return null;
var dictionary:Dictionary = attachments[slotIndex];
return dictionary ? dictionary[name] : null;
} }
public function get attachments () : Object { public function get attachments () : Vector.<Dictionary> {
return _attachments; return _attachments;
} }
@ -67,16 +73,21 @@ public class Skin {
/** Attach each attachment in this skin if the corresponding attachment in the old skin is currently attached. */ /** Attach each attachment in this skin if the corresponding attachment in the old skin is currently attached. */
public function attachAll (skeleton:Skeleton, oldSkin:Skin) : void { public function attachAll (skeleton:Skeleton, oldSkin:Skin) : void {
for (var key:String in oldSkin._attachments) { var slotIndex:int = 0;
var colon:int = key.indexOf(":"); for each (var slot:Slot in skeleton._slots) {
var slotIndex:int = parseInt(key.substring(0, colon)); var slotAttachment:Attachment = slot.attachment;
var name:String = key.substring(colon + 1); if (slotAttachment && slotIndex < oldSkin.attachments.length) {
var slot:Slot = skeleton.slots[slotIndex]; var dictionary:Dictionary = oldSkin.attachments[slotIndex];
if (slot.attachment && slot.attachment.name == name) { for (var name:String in dictionary) {
var attachment:Attachment = getAttachment(slotIndex, name); var skinAttachment:Attachment = dictionary[name];
if (attachment != null) if (slotAttachment == skinAttachment) {
slot.attachment = attachment; var attachment:Attachment = getAttachment(slotIndex, name);
if (attachment != null) slot.attachment = attachment;
break;
}
}
} }
slotIndex++;
} }
} }
} }