mirror of
https://github.com/EsotericSoftware/spine-runtimes.git
synced 2026-02-13 18:48:44 +08:00
removed string comparisons and reduced noticeable method call overhead in Skin
This commit is contained in:
parent
08695c07a3
commit
0df69e04fd
@ -35,8 +35,8 @@ namespace Spine {
|
||||
/// <summary>Stores attachments by slot index and attachment name.</summary>
|
||||
public class Skin {
|
||||
internal String name;
|
||||
private Dictionary<KeyValuePair<int, String>, Attachment> attachments =
|
||||
new Dictionary<KeyValuePair<int, String>, Attachment>(AttachmentComparer.Instance);
|
||||
private Dictionary<AttachmentKeyTuple, Attachment> attachments =
|
||||
new Dictionary<AttachmentKeyTuple, Attachment>(AttachmentKeyTupleComparer.Instance);
|
||||
|
||||
public String Name { get { return name; } }
|
||||
|
||||
@ -47,26 +47,26 @@ namespace Spine {
|
||||
|
||||
public void AddAttachment (int slotIndex, String name, Attachment attachment) {
|
||||
if (attachment == null) throw new ArgumentNullException("attachment cannot be null.");
|
||||
attachments[new KeyValuePair<int, String>(slotIndex, name)] = attachment;
|
||||
attachments[new AttachmentKeyTuple(slotIndex, name)] = attachment;
|
||||
}
|
||||
|
||||
/// <returns>May be null.</returns>
|
||||
public Attachment GetAttachment (int slotIndex, String name) {
|
||||
Attachment attachment;
|
||||
attachments.TryGetValue(new KeyValuePair<int, String>(slotIndex, name), out attachment);
|
||||
attachments.TryGetValue(new AttachmentKeyTuple(slotIndex, name), out attachment);
|
||||
return attachment;
|
||||
}
|
||||
|
||||
public void FindNamesForSlot (int slotIndex, List<String> names) {
|
||||
if (names == null) throw new ArgumentNullException("names cannot be null.");
|
||||
foreach (KeyValuePair<int, String> key in attachments.Keys)
|
||||
if (key.Key == slotIndex) names.Add(key.Value);
|
||||
foreach (AttachmentKeyTuple key in attachments.Keys)
|
||||
if (key.SlotIndex == slotIndex) names.Add(key.Name);
|
||||
}
|
||||
|
||||
public void FindAttachmentsForSlot (int slotIndex, List<Attachment> attachments) {
|
||||
if (attachments == null) throw new ArgumentNullException("attachments cannot be null.");
|
||||
foreach (KeyValuePair<KeyValuePair<int, String>, Attachment> entry in this.attachments)
|
||||
if (entry.Key.Key == slotIndex) attachments.Add(entry.Value);
|
||||
foreach (KeyValuePair<AttachmentKeyTuple, Attachment> entry in this.attachments)
|
||||
if (entry.Key.SlotIndex == slotIndex) attachments.Add(entry.Value);
|
||||
}
|
||||
|
||||
override public String ToString () {
|
||||
@ -75,27 +75,39 @@ namespace Spine {
|
||||
|
||||
/// <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) {
|
||||
foreach (KeyValuePair<KeyValuePair<int, String>, Attachment> entry in oldSkin.attachments) {
|
||||
int slotIndex = entry.Key.Key;
|
||||
foreach (KeyValuePair<AttachmentKeyTuple, Attachment> entry in oldSkin.attachments) {
|
||||
int slotIndex = entry.Key.SlotIndex;
|
||||
Slot slot = skeleton.slots.Items[slotIndex];
|
||||
if (slot.attachment == entry.Value) {
|
||||
Attachment attachment = GetAttachment(slotIndex, entry.Key.Value);
|
||||
Attachment attachment = GetAttachment(slotIndex, entry.Key.Name);
|
||||
if (attachment != null) slot.Attachment = attachment;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Avoids boxing in the dictionary.
|
||||
private class AttachmentComparer : IEqualityComparer<KeyValuePair<int, String>> {
|
||||
internal static readonly AttachmentComparer Instance = new AttachmentComparer();
|
||||
private class AttachmentKeyTupleComparer : IEqualityComparer<AttachmentKeyTuple> {
|
||||
internal static readonly AttachmentKeyTupleComparer Instance = new AttachmentKeyTupleComparer();
|
||||
|
||||
bool IEqualityComparer<KeyValuePair<int, string>>.Equals (KeyValuePair<int, string> o1, KeyValuePair<int, string> o2) {
|
||||
return o1.Key == o2.Key && o1.Value == o2.Value;
|
||||
bool IEqualityComparer<AttachmentKeyTuple>.Equals (AttachmentKeyTuple o1, AttachmentKeyTuple o2) {
|
||||
return o1.SlotIndex == o2.SlotIndex && o1.NameHashCode == o2.NameHashCode;
|
||||
}
|
||||
|
||||
int IEqualityComparer<KeyValuePair<int, string>>.GetHashCode (KeyValuePair<int, string> o) {
|
||||
return o.Key;
|
||||
int IEqualityComparer<AttachmentKeyTuple>.GetHashCode (AttachmentKeyTuple o) {
|
||||
return o.SlotIndex;
|
||||
}
|
||||
}
|
||||
|
||||
private class AttachmentKeyTuple {
|
||||
public readonly int SlotIndex;
|
||||
public readonly string Name;
|
||||
public readonly int NameHashCode;
|
||||
|
||||
public AttachmentKeyTuple(int slotIndex, string name) {
|
||||
SlotIndex = slotIndex;
|
||||
Name = name;
|
||||
NameHashCode = Name.GetHashCode();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user