Skin.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /******************************************************************************
  2. * Spine Runtimes License Agreement
  3. * Last updated January 1, 2020. Replaces all prior versions.
  4. *
  5. * Copyright (c) 2013-2020, Esoteric Software LLC
  6. *
  7. * Integration of the Spine Runtimes into software or otherwise creating
  8. * derivative works of the Spine Runtimes is permitted under the terms and
  9. * conditions of Section 2 of the Spine Editor License Agreement:
  10. * http://esotericsoftware.com/spine-editor-license
  11. *
  12. * Otherwise, it is permitted to integrate the Spine Runtimes into software
  13. * or otherwise create derivative works of the Spine Runtimes (collectively,
  14. * "Products"), provided that each user of the Products must obtain their own
  15. * Spine Editor license and redistribution of the Products in any form must
  16. * include this license and copyright notice.
  17. *
  18. * THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY
  19. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES,
  24. * BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  27. * THE SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *****************************************************************************/
  29. using System;
  30. using System.Collections;
  31. using System.Collections.Generic;
  32. using Spine.Collections;
  33. namespace Spine {
  34. /// <summary>Stores attachments by slot index and attachment name.
  35. /// <para>See SkeletonData <see cref="Spine.SkeletonData.DefaultSkin"/>, Skeleton <see cref="Spine.Skeleton.Skin"/>, and
  36. /// <a href="http://esotericsoftware.com/spine-runtime-skins">Runtime skins</a> in the Spine Runtimes Guide.</para>
  37. /// </summary>
  38. public class Skin {
  39. internal string name;
  40. private OrderedDictionary<SkinEntry, Attachment> attachments = new OrderedDictionary<SkinEntry, Attachment>(SkinEntryComparer.Instance);
  41. internal readonly ExposedList<BoneData> bones = new ExposedList<BoneData>();
  42. internal readonly ExposedList<ConstraintData> constraints = new ExposedList<ConstraintData>();
  43. public string Name { get { return name; } }
  44. public OrderedDictionary<SkinEntry, Attachment> Attachments { get { return attachments; } }
  45. public ExposedList<BoneData> Bones { get { return bones; } }
  46. public ExposedList<ConstraintData> Constraints { get { return constraints; } }
  47. public Skin (string name) {
  48. if (name == null) throw new ArgumentNullException("name", "name cannot be null.");
  49. this.name = name;
  50. }
  51. /// <summary>Adds an attachment to the skin for the specified slot index and name.
  52. /// If the name already exists for the slot, the previous value is replaced.</summary>
  53. public void SetAttachment (int slotIndex, string name, Attachment attachment) {
  54. if (attachment == null) throw new ArgumentNullException("attachment", "attachment cannot be null.");
  55. if (slotIndex < 0) throw new ArgumentNullException("slotIndex", "slotIndex must be >= 0.");
  56. attachments[new SkinEntry(slotIndex, name, attachment)] = attachment;
  57. }
  58. ///<summary>Adds all attachments, bones, and constraints from the specified skin to this skin.</summary>
  59. public void AddSkin (Skin skin) {
  60. foreach (BoneData data in skin.bones)
  61. if (!bones.Contains(data)) bones.Add(data);
  62. foreach (ConstraintData data in skin.constraints)
  63. if (!constraints.Contains(data)) constraints.Add(data);
  64. foreach (SkinEntry entry in skin.attachments.Keys)
  65. SetAttachment(entry.SlotIndex, entry.Name, entry.Attachment);
  66. }
  67. ///<summary>Adds all attachments from the specified skin to this skin. Attachments are deep copied.</summary>
  68. public void CopySkin (Skin skin) {
  69. foreach (BoneData data in skin.bones)
  70. if (!bones.Contains(data)) bones.Add(data);
  71. foreach (ConstraintData data in skin.constraints)
  72. if (!constraints.Contains(data)) constraints.Add(data);
  73. foreach (SkinEntry entry in skin.attachments.Keys) {
  74. if (entry.Attachment is MeshAttachment)
  75. SetAttachment(entry.SlotIndex, entry.Name,
  76. entry.Attachment != null ? ((MeshAttachment)entry.Attachment).NewLinkedMesh() : null);
  77. else
  78. SetAttachment(entry.SlotIndex, entry.Name, entry.Attachment != null ? entry.Attachment.Copy() : null);
  79. }
  80. }
  81. /// <summary>Returns the attachment for the specified slot index and name, or null.</summary>
  82. /// <returns>May be null.</returns>
  83. public Attachment GetAttachment (int slotIndex, string name) {
  84. var lookup = new SkinEntry(slotIndex, name, null);
  85. Attachment attachment = null;
  86. bool containsKey = attachments.TryGetValue(lookup, out attachment);
  87. return containsKey ? attachment : null;
  88. }
  89. /// <summary> Removes the attachment in the skin for the specified slot index and name, if any.</summary>
  90. public void RemoveAttachment (int slotIndex, string name) {
  91. if (slotIndex < 0) throw new ArgumentOutOfRangeException("slotIndex", "slotIndex must be >= 0");
  92. var lookup = new SkinEntry(slotIndex, name, null);
  93. attachments.Remove(lookup);
  94. }
  95. ///<summary>Returns all attachments contained in this skin.</summary>
  96. public ICollection<SkinEntry> GetAttachments () {
  97. return this.attachments.Keys;
  98. }
  99. /// <summary>Returns all attachments in this skin for the specified slot index.</summary>
  100. /// <param name="slotIndex">The target slotIndex. To find the slot index, use <see cref="Spine.Skeleton.FindSlotIndex"/> or <see cref="Spine.SkeletonData.FindSlotIndex"/>
  101. public void GetAttachments (int slotIndex, List<SkinEntry> attachments) {
  102. foreach (SkinEntry entry in this.attachments.Keys)
  103. if (entry.SlotIndex == slotIndex) attachments.Add(entry);
  104. }
  105. ///<summary>Clears all attachments, bones, and constraints.</summary>
  106. public void Clear () {
  107. attachments.Clear();
  108. bones.Clear();
  109. constraints.Clear();
  110. }
  111. override public string ToString () {
  112. return name;
  113. }
  114. /// <summary>Attach all attachments from this skin if the corresponding attachment from the old skin is currently attached.</summary>
  115. internal void AttachAll (Skeleton skeleton, Skin oldSkin) {
  116. foreach (SkinEntry entry in oldSkin.attachments.Keys) {
  117. int slotIndex = entry.SlotIndex;
  118. Slot slot = skeleton.slots.Items[slotIndex];
  119. if (slot.Attachment == entry.Attachment) {
  120. Attachment attachment = GetAttachment(slotIndex, entry.Name);
  121. if (attachment != null) slot.Attachment = attachment;
  122. }
  123. }
  124. }
  125. /// <summary>Stores an entry in the skin consisting of the slot index, name, and attachment.</summary>
  126. public struct SkinEntry {
  127. private readonly int slotIndex;
  128. private readonly string name;
  129. private readonly Attachment attachment;
  130. internal readonly int hashCode;
  131. public SkinEntry (int slotIndex, string name, Attachment attachment) {
  132. this.slotIndex = slotIndex;
  133. this.name = name;
  134. this.attachment = attachment;
  135. this.hashCode = this.name.GetHashCode() + this.slotIndex * 37;
  136. }
  137. public int SlotIndex {
  138. get {
  139. return slotIndex;
  140. }
  141. }
  142. /// <summary>The name the attachment is associated with, equivalent to the skin placeholder name in the Spine editor.</summary>
  143. public String Name {
  144. get {
  145. return name;
  146. }
  147. }
  148. public Attachment Attachment {
  149. get {
  150. return attachment;
  151. }
  152. }
  153. }
  154. // Avoids boxing in the dictionary and is necessary to omit entry.attachment in the comparison.
  155. class SkinEntryComparer : IEqualityComparer<SkinEntry> {
  156. internal static readonly SkinEntryComparer Instance = new SkinEntryComparer();
  157. bool IEqualityComparer<SkinEntry>.Equals (SkinEntry e1, SkinEntry e2) {
  158. if (e1.SlotIndex != e2.SlotIndex) return false;
  159. if (!string.Equals(e1.Name, e2.Name, StringComparison.Ordinal)) return false;
  160. return true;
  161. }
  162. int IEqualityComparer<SkinEntry>.GetHashCode (SkinEntry e) {
  163. return e.Name.GetHashCode() + e.SlotIndex * 37;
  164. }
  165. }
  166. }
  167. }