From 6d398233d72c2f17c7ac78a7e21e3b241796d2ec Mon Sep 17 00:00:00 2001 From: NathanSweet Date: Fri, 25 Jul 2014 23:04:32 +0200 Subject: [PATCH] spine-starling, use Vector of Dictionarys to avoid allocation to look up skin attachments. #236 --- spine-as3/spine-as3/src/spine/Skin.as | 37 +++++++++++++++++---------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/spine-as3/spine-as3/src/spine/Skin.as b/spine-as3/spine-as3/src/spine/Skin.as index 1516ac7ee..c12e509ef 100644 --- a/spine-as3/spine-as3/src/spine/Skin.as +++ b/spine-as3/spine-as3/src/spine/Skin.as @@ -29,12 +29,14 @@ *****************************************************************************/ package spine { +import flash.utils.Dictionary; + import spine.attachments.Attachment; /** Stores attachments by slot index and attachment name. */ public class Skin { internal var _name:String; - private var _attachments:Object = new Object(); + private var _attachments:Vector. = new Vector.(); public function Skin (name:String) { if (name == null) @@ -45,15 +47,19 @@ public class Skin { public function addAttachment (slotIndex:int, name:String, attachment:Attachment) : void { if (attachment == 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. */ 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. { 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. */ public function attachAll (skeleton:Skeleton, oldSkin:Skin) : void { - for (var key:String in oldSkin._attachments) { - var colon:int = key.indexOf(":"); - var slotIndex:int = parseInt(key.substring(0, colon)); - var name:String = key.substring(colon + 1); - var slot:Slot = skeleton.slots[slotIndex]; - if (slot.attachment && slot.attachment.name == name) { - var attachment:Attachment = getAttachment(slotIndex, name); - if (attachment != null) - slot.attachment = attachment; + var slotIndex:int = 0; + for each (var slot:Slot in skeleton._slots) { + var slotAttachment:Attachment = slot.attachment; + if (slotAttachment && slotIndex < oldSkin.attachments.length) { + var dictionary:Dictionary = oldSkin.attachments[slotIndex]; + for (var name:String in dictionary) { + var skinAttachment:Attachment = dictionary[name]; + if (slotAttachment == skinAttachment) { + var attachment:Attachment = getAttachment(slotIndex, name); + if (attachment != null) slot.attachment = attachment; + break; + } + } } + slotIndex++; } } }