Skin.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. namespace Spine {
  33. /// <summary>Stores attachments by slot index and attachment name.
  34. /// <para>See SkeletonData <see cref="Spine.SkeletonData.DefaultSkin"/>, Skeleton <see cref="Spine.Skeleton.Skin"/>, and
  35. /// <a href="http://esotericsoftware.com/spine-runtime-skins">Runtime skins</a> in the Spine Runtimes Guide.</para>
  36. /// </summary>
  37. public class Skin {
  38. internal string name;
  39. // Difference to reference implementation: using Dictionary<SkinKey, SkinEntry> instead of HashSet<SkinEntry>.
  40. // Reason is that there is no efficient way to replace or access an already added element, losing any benefits.
  41. private Dictionary<SkinKey, SkinEntry> attachments = new Dictionary<SkinKey, SkinEntry>(SkinKeyComparer.Instance);
  42. internal readonly ExposedList<BoneData> bones = new ExposedList<BoneData>();
  43. internal readonly ExposedList<ConstraintData> constraints = new ExposedList<ConstraintData>();
  44. public string Name { get { return name; } }
  45. ///<summary>Returns all attachments contained in this skin.</summary>
  46. public ICollection<SkinEntry> Attachments { get { return attachments.Values; } }
  47. public ExposedList<BoneData> Bones { get { return bones; } }
  48. public ExposedList<ConstraintData> Constraints { get { return constraints; } }
  49. public Skin (string name) {
  50. if (name == null) throw new ArgumentNullException("name", "name cannot be null.");
  51. this.name = name;
  52. }
  53. /// <summary>Adds an attachment to the skin for the specified slot index and name.
  54. /// If the name already exists for the slot, the previous value is replaced.</summary>
  55. public void SetAttachment (int slotIndex, string name, Attachment attachment) {
  56. if (attachment == null) throw new ArgumentNullException("attachment", "attachment cannot be null.");
  57. attachments[new SkinKey(slotIndex, name)] = new SkinEntry(slotIndex, name, attachment);
  58. }
  59. ///<summary>Adds all attachments, bones, and constraints from the specified skin to this skin.</summary>
  60. public void AddSkin (Skin skin) {
  61. foreach (BoneData data in skin.bones)
  62. if (!bones.Contains(data)) bones.Add(data);
  63. foreach (ConstraintData data in skin.constraints)
  64. if (!constraints.Contains(data)) constraints.Add(data);
  65. foreach (var item in skin.attachments) {
  66. SkinEntry entry = item.Value;
  67. SetAttachment(entry.slotIndex, entry.name, entry.attachment);
  68. }
  69. }
  70. ///<summary>Adds all attachments from the specified skin to this skin. Attachments are deep copied.</summary>
  71. public void CopySkin (Skin skin) {
  72. foreach (BoneData data in skin.bones)
  73. if (!bones.Contains(data)) bones.Add(data);
  74. foreach (ConstraintData data in skin.constraints)
  75. if (!constraints.Contains(data)) constraints.Add(data);
  76. foreach (var item in skin.attachments) {
  77. SkinEntry entry = item.Value;
  78. if (entry.attachment is MeshAttachment) {
  79. SetAttachment(entry.slotIndex, entry.name,
  80. entry.attachment != null ? ((MeshAttachment)entry.attachment).NewLinkedMesh() : null);
  81. } else
  82. SetAttachment(entry.slotIndex, entry.name, entry.attachment != null ? entry.attachment.Copy() : null);
  83. }
  84. }
  85. /// <summary>Returns the attachment for the specified slot index and name, or null.</summary>
  86. /// <returns>May be null.</returns>
  87. public Attachment GetAttachment (int slotIndex, string name) {
  88. SkinEntry entry;
  89. bool containsKey = attachments.TryGetValue(new SkinKey(slotIndex, name), out entry);
  90. return containsKey ? entry.attachment : null;
  91. }
  92. /// <summary> Removes the attachment in the skin for the specified slot index and name, if any.</summary>
  93. public void RemoveAttachment (int slotIndex, string name) {
  94. attachments.Remove(new SkinKey(slotIndex, name));
  95. }
  96. /// <summary>Returns all attachments in this skin for the specified slot index.</summary>
  97. /// <param name="slotIndex">The target slotIndex. To find the slot index, use <see cref="Spine.SkeletonData.FindSlot"/> and <see cref="Spine.SlotData.Index"/>.
  98. public void GetAttachments (int slotIndex, List<SkinEntry> attachments) {
  99. if (slotIndex < 0) throw new ArgumentException("slotIndex must be >= 0.");
  100. if (attachments == null) throw new ArgumentNullException("attachments", "attachments cannot be null.");
  101. foreach (var item in this.attachments) {
  102. SkinEntry entry = item.Value;
  103. if (entry.slotIndex == slotIndex) attachments.Add(entry);
  104. }
  105. }
  106. ///<summary>Clears all attachments, bones, and constraints.</summary>
  107. public void Clear () {
  108. attachments.Clear();
  109. bones.Clear();
  110. constraints.Clear();
  111. }
  112. override public string ToString () {
  113. return name;
  114. }
  115. /// <summary>Attach all attachments from this skin if the corresponding attachment from the old skin is currently attached.</summary>
  116. internal void AttachAll (Skeleton skeleton, Skin oldSkin) {
  117. Slot[] slots = skeleton.slots.Items;
  118. foreach (var item in oldSkin.attachments) {
  119. SkinEntry entry = item.Value;
  120. int slotIndex = entry.slotIndex;
  121. Slot slot = slots[slotIndex];
  122. if (slot.Attachment == entry.attachment) {
  123. Attachment attachment = GetAttachment(slotIndex, entry.name);
  124. if (attachment != null) slot.Attachment = attachment;
  125. }
  126. }
  127. }
  128. /// <summary>Stores an entry in the skin consisting of the slot index, name, and attachment.</summary>
  129. public struct SkinEntry {
  130. internal readonly int slotIndex;
  131. internal readonly string name;
  132. internal readonly Attachment attachment;
  133. public SkinEntry (int slotIndex, string name, Attachment attachment) {
  134. this.slotIndex = slotIndex;
  135. this.name = name;
  136. this.attachment = attachment;
  137. }
  138. public int SlotIndex {
  139. get {
  140. return slotIndex;
  141. }
  142. }
  143. /// <summary>The name the attachment is associated with, equivalent to the skin placeholder name in the Spine editor.</summary>
  144. public String Name {
  145. get {
  146. return name;
  147. }
  148. }
  149. public Attachment Attachment {
  150. get {
  151. return attachment;
  152. }
  153. }
  154. }
  155. private struct SkinKey {
  156. internal readonly int slotIndex;
  157. internal readonly string name;
  158. internal readonly int hashCode;
  159. public SkinKey (int slotIndex, string name) {
  160. if (slotIndex < 0) throw new ArgumentException("slotIndex must be >= 0.");
  161. if (name == null) throw new ArgumentNullException("name", "name cannot be null");
  162. this.slotIndex = slotIndex;
  163. this.name = name;
  164. this.hashCode = name.GetHashCode() + slotIndex * 37;
  165. }
  166. }
  167. class SkinKeyComparer : IEqualityComparer<SkinKey> {
  168. internal static readonly SkinKeyComparer Instance = new SkinKeyComparer();
  169. bool IEqualityComparer<SkinKey>.Equals (SkinKey e1, SkinKey e2) {
  170. return e1.slotIndex == e2.slotIndex && string.Equals(e1.name, e2.name, StringComparison.Ordinal);
  171. }
  172. int IEqualityComparer<SkinKey>.GetHashCode (SkinKey e) {
  173. return e.hashCode;
  174. }
  175. }
  176. }
  177. }