Use struct for Skin.cs

https://github.com/EsotericSoftware/spine-runtimes/issues/460
This commit is contained in:
John 2016-03-11 06:59:25 +08:00
parent 2a244bbbe0
commit 4b23116dff

View File

@ -61,13 +61,13 @@ namespace Spine {
public void FindNamesForSlot (int slotIndex, List<String> names) { public void FindNamesForSlot (int slotIndex, List<String> names) {
if (names == null) throw new ArgumentNullException("names cannot be null."); if (names == null) throw new ArgumentNullException("names cannot be null.");
foreach (AttachmentKeyTuple key in attachments.Keys) foreach (AttachmentKeyTuple key in attachments.Keys)
if (key.SlotIndex == slotIndex) names.Add(key.Name); if (key.slotIndex == slotIndex) names.Add(key.name);
} }
public void FindAttachmentsForSlot (int slotIndex, List<Attachment> attachments) { public void FindAttachmentsForSlot (int slotIndex, List<Attachment> attachments) {
if (attachments == null) throw new ArgumentNullException("attachments cannot be null."); if (attachments == null) throw new ArgumentNullException("attachments cannot be null.");
foreach (KeyValuePair<AttachmentKeyTuple, Attachment> entry in this.attachments) foreach (KeyValuePair<AttachmentKeyTuple, Attachment> entry in this.attachments)
if (entry.Key.SlotIndex == slotIndex) attachments.Add(entry.Value); if (entry.Key.slotIndex == slotIndex) attachments.Add(entry.Value);
} }
override public String ToString () { override public String ToString () {
@ -77,37 +77,37 @@ namespace Spine {
/// <summary>Attach all attachments from this skin if the corresponding attachment from the old skin is currently attached.</summary> /// <summary>Attach all attachments from this skin if the corresponding attachment from the old skin is currently attached.</summary>
internal void AttachAll (Skeleton skeleton, Skin oldSkin) { internal void AttachAll (Skeleton skeleton, Skin oldSkin) {
foreach (KeyValuePair<AttachmentKeyTuple, Attachment> entry in oldSkin.attachments) { foreach (KeyValuePair<AttachmentKeyTuple, Attachment> entry in oldSkin.attachments) {
int slotIndex = entry.Key.SlotIndex; int slotIndex = entry.Key.slotIndex;
Slot slot = skeleton.slots.Items[slotIndex]; Slot slot = skeleton.slots.Items[slotIndex];
if (slot.attachment == entry.Value) { if (slot.attachment == entry.Value) {
Attachment attachment = GetAttachment(slotIndex, entry.Key.Name); Attachment attachment = GetAttachment(slotIndex, entry.Key.name);
if (attachment != null) slot.Attachment = attachment; if (attachment != null) slot.Attachment = attachment;
} }
} }
} }
// Avoids boxing in the dictionary. struct AttachmentKeyTuple {
private class AttachmentKeyTupleComparer : IEqualityComparer<AttachmentKeyTuple> { public readonly int slotIndex;
internal static readonly AttachmentKeyTupleComparer Instance = new AttachmentKeyTupleComparer(); public readonly string name;
internal readonly int nameHashCode;
bool IEqualityComparer<AttachmentKeyTuple>.Equals (AttachmentKeyTuple o1, AttachmentKeyTuple o2) { public AttachmentKeyTuple (int slotIndex, string name) {
return o1.SlotIndex == o2.SlotIndex && o1.NameHashCode == o2.NameHashCode && o1.Name == o2.Name; this.slotIndex = slotIndex;
} this.name = name;
nameHashCode = this.name.GetHashCode();
int IEqualityComparer<AttachmentKeyTuple>.GetHashCode (AttachmentKeyTuple o) {
return o.SlotIndex;
} }
} }
private class AttachmentKeyTuple { // Avoids boxing in the dictionary.
public readonly int SlotIndex; class AttachmentKeyTupleComparer : IEqualityComparer<AttachmentKeyTuple> {
public readonly string Name; internal static readonly AttachmentKeyTupleComparer Instance = new AttachmentKeyTupleComparer();
public readonly int NameHashCode;
public AttachmentKeyTuple (int slotIndex, string name) { bool IEqualityComparer<AttachmentKeyTuple>.Equals (AttachmentKeyTuple o1, AttachmentKeyTuple o2) {
SlotIndex = slotIndex; return o1.slotIndex == o2.slotIndex && o1.nameHashCode == o2.nameHashCode && o1.name == o2.name;
Name = name; }
NameHashCode = Name.GetHashCode();
int IEqualityComparer<AttachmentKeyTuple>.GetHashCode (AttachmentKeyTuple o) {
return o.slotIndex;
} }
} }
} }