Minor clean up.

This commit is contained in:
NathanSweet 2019-11-29 14:41:19 +01:00
parent f2a8c9a3c1
commit ab7bd0f75f

View File

@ -70,8 +70,10 @@ namespace Spine {
foreach (ConstraintData data in skin.constraints) foreach (ConstraintData data in skin.constraints)
if (!constraints.Contains(data)) constraints.Add(data); if (!constraints.Contains(data)) constraints.Add(data);
foreach (SkinEntry entry in skin.attachments.Values) for (int i = 0, n = skin.attachments.Count; i < n; i++) {
SetAttachment(entry.SlotIndex, entry.Name, entry.Attachment); SkinEntry entry = skin.attachments[i];
SetAttachment(entry.slotIndex, entry.name, entry.attachment);
}
} }
///<summary>Adds all attachments from the specified skin to this skin. Attachments are deep copied.</summary> ///<summary>Adds all attachments from the specified skin to this skin. Attachments are deep copied.</summary>
@ -82,36 +84,37 @@ namespace Spine {
foreach (ConstraintData data in skin.constraints) foreach (ConstraintData data in skin.constraints)
if (!constraints.Contains(data)) constraints.Add(data); if (!constraints.Contains(data)) constraints.Add(data);
foreach (SkinEntry entry in skin.attachments.Values) { for (int i = 0, n = skin.attachments.Count; i < n; i++) {
if (entry.Attachment is MeshAttachment) SkinEntry entry = skin.attachments[i];
SetAttachment(entry.SlotIndex, entry.Name, if (entry.attachment is MeshAttachment) {
entry.Attachment != null ? ((MeshAttachment)entry.Attachment).NewLinkedMesh() : null); SetAttachment(entry.slotIndex, entry.name,
else entry.attachment != null ? ((MeshAttachment)entry.attachment).NewLinkedMesh() : null);
SetAttachment(entry.SlotIndex, entry.Name, entry.Attachment != null ? entry.Attachment.Copy() : null); } else
SetAttachment(entry.slotIndex, entry.name, entry.attachment != null ? entry.attachment.Copy() : null);
} }
} }
/// <summary>Returns the attachment for the specified slot index and name, or null.</summary> /// <summary>Returns the attachment for the specified slot index and name, or null.</summary>
/// <returns>May be null.</returns> /// <returns>May be null.</returns>
public Attachment GetAttachment (int slotIndex, string name) { public Attachment GetAttachment (int slotIndex, string name) {
var lookup = new SkinKey(slotIndex, name);
SkinEntry entry; SkinEntry entry;
bool containsKey = attachments.TryGetValue(lookup, out entry); bool containsKey = attachments.TryGetValue(new SkinKey(slotIndex, name), out entry);
return containsKey ? entry.Attachment : null; return containsKey ? entry.attachment : null;
} }
/// <summary> Removes the attachment in the skin for the specified slot index and name, if any.</summary> /// <summary> Removes the attachment in the skin for the specified slot index and name, if any.</summary>
public void RemoveAttachment (int slotIndex, string name) { public void RemoveAttachment (int slotIndex, string name) {
if (slotIndex < 0) throw new ArgumentOutOfRangeException("slotIndex", "slotIndex must be >= 0"); if (slotIndex < 0) throw new ArgumentOutOfRangeException("slotIndex", "slotIndex must be >= 0");
var lookup = new SkinKey(slotIndex, name); attachments.Remove(new SkinKey(slotIndex, name));
attachments.Remove(lookup);
} }
/// <summary>Returns all attachments in this skin for the specified slot index.</summary> /// <summary>Returns all attachments in this skin for the specified slot index.</summary>
/// <param name="slotIndex">The target slotIndex. To find the slot index, use <see cref="Spine.Skeleton.FindSlotIndex"/> or <see cref="Spine.SkeletonData.FindSlotIndex"/> /// <param name="slotIndex">The target slotIndex. To find the slot index, use <see cref="Spine.Skeleton.FindSlotIndex"/> or <see cref="Spine.SkeletonData.FindSlotIndex"/>
public void GetAttachments (int slotIndex, List<SkinEntry> attachments) { public void GetAttachments (int slotIndex, List<SkinEntry> attachments) {
foreach (SkinEntry entry in this.attachments.Values) for (int i = 0, n = this.attachments.Count; i < n; i++) {
if (entry.SlotIndex == slotIndex) attachments.Add(entry); SkinEntry entry = this.attachments[i];
if (entry.slotIndex == slotIndex) attachments.Add(entry);
}
} }
///<summary>Clears all attachments, bones, and constraints.</summary> ///<summary>Clears all attachments, bones, and constraints.</summary>
@ -127,11 +130,12 @@ 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 (SkinEntry entry in oldSkin.attachments.Values) { for (int i = 0, n = oldSkin.attachments.Count; i < n; i++) {
int slotIndex = entry.SlotIndex; SkinEntry entry = oldSkin.attachments[i];
int slotIndex = entry.slotIndex;
Slot slot = skeleton.slots.Items[slotIndex]; Slot slot = skeleton.slots.Items[slotIndex];
if (slot.Attachment == entry.Attachment) { if (slot.Attachment == entry.attachment) {
Attachment attachment = GetAttachment(slotIndex, entry.Name); Attachment attachment = GetAttachment(slotIndex, entry.name);
if (attachment != null) slot.Attachment = attachment; if (attachment != null) slot.Attachment = attachment;
} }
} }
@ -139,9 +143,9 @@ namespace Spine {
/// <summary>Stores an entry in the skin consisting of the slot index, name, and attachment.</summary> /// <summary>Stores an entry in the skin consisting of the slot index, name, and attachment.</summary>
public struct SkinEntry { public struct SkinEntry {
private readonly int slotIndex; internal readonly int slotIndex;
private readonly string name; internal readonly string name;
private readonly Attachment attachment; internal readonly Attachment attachment;
public SkinEntry (int slotIndex, string name, Attachment attachment) { public SkinEntry (int slotIndex, string name, Attachment attachment) {
this.slotIndex = slotIndex; this.slotIndex = slotIndex;
@ -169,44 +173,25 @@ namespace Spine {
} }
} }
/// Note: not present in libgdx reference implementation.
/// SkinKey contains slot index and name only, as a once-added SkinEntry cannot have its Attachment changed
/// (SkinEntry is a value type as opposed to the java reference implementation) without removing and
/// re-adding it to a dictionary, leading to problems in order and unnecessary operations.
private struct SkinKey { private struct SkinKey {
private readonly int slotIndex; internal readonly int slotIndex;
private readonly string name; internal readonly string name;
public SkinKey (int slotIndex, string name) { public SkinKey (int slotIndex, string name) {
this.slotIndex = slotIndex; this.slotIndex = slotIndex;
this.name = name; this.name = name;
} }
public int SlotIndex {
get {
return slotIndex;
}
}
/// <summary>The name the attachment is associated with, equivalent to the skin placeholder name in the Spine editor.</summary>
public String Name {
get {
return name;
}
}
} }
class SkinKeyComparer : IEqualityComparer<SkinKey> { class SkinKeyComparer : IEqualityComparer<SkinKey> {
internal static readonly SkinKeyComparer Instance = new SkinKeyComparer(); internal static readonly SkinKeyComparer Instance = new SkinKeyComparer();
bool IEqualityComparer<SkinKey>.Equals (SkinKey e1, SkinKey e2) { bool IEqualityComparer<SkinKey>.Equals (SkinKey e1, SkinKey e2) {
if (e1.SlotIndex != e2.SlotIndex) return false; return e1.slotIndex == e2.slotIndex && string.Equals(e1.name, e2.name, StringComparison.Ordinal);
if (!string.Equals(e1.Name, e2.Name, StringComparison.Ordinal)) return false;
return true;
} }
int IEqualityComparer<SkinKey>.GetHashCode (SkinKey e) { int IEqualityComparer<SkinKey>.GetHashCode (SkinKey e) {
return e.Name.GetHashCode() + e.SlotIndex * 37; return e.name.GetHashCode() + e.slotIndex * 37;
} }
} }
} }