SkeletonData.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. namespace Spine {
  31. /// <summary>Stores the setup pose and all of the stateless data for a skeleton.</summary>
  32. public class SkeletonData {
  33. internal string name;
  34. internal ExposedList<BoneData> bones = new ExposedList<BoneData>(); // Ordered parents first
  35. internal ExposedList<SlotData> slots = new ExposedList<SlotData>(); // Setup pose draw order.
  36. internal ExposedList<Skin> skins = new ExposedList<Skin>();
  37. internal Skin defaultSkin;
  38. internal ExposedList<EventData> events = new ExposedList<EventData>();
  39. internal ExposedList<Animation> animations = new ExposedList<Animation>();
  40. internal ExposedList<IkConstraintData> ikConstraints = new ExposedList<IkConstraintData>();
  41. internal ExposedList<TransformConstraintData> transformConstraints = new ExposedList<TransformConstraintData>();
  42. internal ExposedList<PathConstraintData> pathConstraints = new ExposedList<PathConstraintData>();
  43. internal float x , y, width, height;
  44. internal string version, hash;
  45. // Nonessential.
  46. internal float fps;
  47. internal string imagesPath, audioPath;
  48. public string Name { get { return name; } set { name = value; } }
  49. /// <summary>The skeleton's bones, sorted parent first. The root bone is always the first bone.</summary>
  50. public ExposedList<BoneData> Bones { get { return bones; } }
  51. public ExposedList<SlotData> Slots { get { return slots; } }
  52. /// <summary>All skins, including the default skin.</summary>
  53. public ExposedList<Skin> Skins { get { return skins; } set { skins = value; } }
  54. /// <summary>
  55. /// The skeleton's default skin.
  56. /// By default this skin contains all attachments that were not in a skin in Spine.
  57. /// </summary>
  58. /// <return>May be null.</return>
  59. public Skin DefaultSkin { get { return defaultSkin; } set { defaultSkin = value; } }
  60. public ExposedList<EventData> Events { get { return events; } set { events = value; } }
  61. public ExposedList<Animation> Animations { get { return animations; } set { animations = value; } }
  62. public ExposedList<IkConstraintData> IkConstraints { get { return ikConstraints; } set { ikConstraints = value; } }
  63. public ExposedList<TransformConstraintData> TransformConstraints { get { return transformConstraints; } set { transformConstraints = value; } }
  64. public ExposedList<PathConstraintData> PathConstraints { get { return pathConstraints; } set { pathConstraints = value; } }
  65. public float X { get { return x; } set { x = value; } }
  66. public float Y { get { return y; } set { y = value; } }
  67. public float Width { get { return width; } set { width = value; } }
  68. public float Height { get { return height; } set { height = value; } }
  69. /// <summary>The Spine version used to export this data, or null.</summary>
  70. public string Version { get { return version; } set { version = value; } }
  71. public string Hash { get { return hash; } set { hash = value; } }
  72. /// <summary>The path to the images directory as defined in Spine. Available only when nonessential data was exported. May be null</summary>
  73. public string ImagesPath { get { return imagesPath; } set { imagesPath = value; } }
  74. /// <summary>The path to the audio directory defined in Spine. Available only when nonessential data was exported. May be null.</summary>
  75. public string AudioPath { get { return audioPath; } set { audioPath = value; } }
  76. /// <summary>
  77. /// The dopesheet FPS in Spine. Available only when nonessential data was exported.</summary>
  78. public float Fps { get { return fps; } set { fps = value; } }
  79. // --- Bones.
  80. /// <summary>
  81. /// Finds a bone by comparing each bone's name.
  82. /// It is more efficient to cache the results of this method than to call it multiple times.</summary>
  83. /// <returns>May be null.</returns>
  84. public BoneData FindBone (string boneName) {
  85. if (boneName == null) throw new ArgumentNullException("boneName", "boneName cannot be null.");
  86. var bones = this.bones;
  87. var bonesItems = bones.Items;
  88. for (int i = 0, n = bones.Count; i < n; i++) {
  89. BoneData bone = bonesItems[i];
  90. if (bone.name == boneName) return bone;
  91. }
  92. return null;
  93. }
  94. /// <returns>-1 if the bone was not found.</returns>
  95. public int FindBoneIndex (string boneName) {
  96. if (boneName == null) throw new ArgumentNullException("boneName", "boneName cannot be null.");
  97. var bones = this.bones;
  98. var bonesItems = bones.Items;
  99. for (int i = 0, n = bones.Count; i < n; i++)
  100. if (bonesItems[i].name == boneName) return i;
  101. return -1;
  102. }
  103. // --- Slots.
  104. /// <returns>May be null.</returns>
  105. public SlotData FindSlot (string slotName) {
  106. if (slotName == null) throw new ArgumentNullException("slotName", "slotName cannot be null.");
  107. ExposedList<SlotData> slots = this.slots;
  108. for (int i = 0, n = slots.Count; i < n; i++) {
  109. SlotData slot = slots.Items[i];
  110. if (slot.name == slotName) return slot;
  111. }
  112. return null;
  113. }
  114. /// <returns>-1 if the slot was not found.</returns>
  115. public int FindSlotIndex (string slotName) {
  116. if (slotName == null) throw new ArgumentNullException("slotName", "slotName cannot be null.");
  117. ExposedList<SlotData> slots = this.slots;
  118. for (int i = 0, n = slots.Count; i < n; i++)
  119. if (slots.Items[i].name == slotName) return i;
  120. return -1;
  121. }
  122. // --- Skins.
  123. /// <returns>May be null.</returns>
  124. public Skin FindSkin (string skinName) {
  125. if (skinName == null) throw new ArgumentNullException("skinName", "skinName cannot be null.");
  126. foreach (Skin skin in skins)
  127. if (skin.name == skinName) return skin;
  128. return null;
  129. }
  130. // --- Events.
  131. /// <returns>May be null.</returns>
  132. public EventData FindEvent (string eventDataName) {
  133. if (eventDataName == null) throw new ArgumentNullException("eventDataName", "eventDataName cannot be null.");
  134. foreach (EventData eventData in events)
  135. if (eventData.name == eventDataName) return eventData;
  136. return null;
  137. }
  138. // --- Animations.
  139. /// <returns>May be null.</returns>
  140. public Animation FindAnimation (string animationName) {
  141. if (animationName == null) throw new ArgumentNullException("animationName", "animationName cannot be null.");
  142. ExposedList<Animation> animations = this.animations;
  143. for (int i = 0, n = animations.Count; i < n; i++) {
  144. Animation animation = animations.Items[i];
  145. if (animation.name == animationName) return animation;
  146. }
  147. return null;
  148. }
  149. // --- IK constraints.
  150. /// <returns>May be null.</returns>
  151. public IkConstraintData FindIkConstraint (string constraintName) {
  152. if (constraintName == null) throw new ArgumentNullException("constraintName", "constraintName cannot be null.");
  153. ExposedList<IkConstraintData> ikConstraints = this.ikConstraints;
  154. for (int i = 0, n = ikConstraints.Count; i < n; i++) {
  155. IkConstraintData ikConstraint = ikConstraints.Items[i];
  156. if (ikConstraint.name == constraintName) return ikConstraint;
  157. }
  158. return null;
  159. }
  160. // --- Transform constraints.
  161. /// <returns>May be null.</returns>
  162. public TransformConstraintData FindTransformConstraint (string constraintName) {
  163. if (constraintName == null) throw new ArgumentNullException("constraintName", "constraintName cannot be null.");
  164. ExposedList<TransformConstraintData> transformConstraints = this.transformConstraints;
  165. for (int i = 0, n = transformConstraints.Count; i < n; i++) {
  166. TransformConstraintData transformConstraint = transformConstraints.Items[i];
  167. if (transformConstraint.name == constraintName) return transformConstraint;
  168. }
  169. return null;
  170. }
  171. // --- Path constraints.
  172. /// <returns>May be null.</returns>
  173. public PathConstraintData FindPathConstraint (string constraintName) {
  174. if (constraintName == null) throw new ArgumentNullException("constraintName", "constraintName cannot be null.");
  175. ExposedList<PathConstraintData> pathConstraints = this.pathConstraints;
  176. for (int i = 0, n = pathConstraints.Count; i < n; i++) {
  177. PathConstraintData constraint = pathConstraints.Items[i];
  178. if (constraint.name.Equals(constraintName)) return constraint;
  179. }
  180. return null;
  181. }
  182. /// <returns>-1 if the path constraint was not found.</returns>
  183. public int FindPathConstraintIndex (string pathConstraintName) {
  184. if (pathConstraintName == null) throw new ArgumentNullException("pathConstraintName", "pathConstraintName cannot be null.");
  185. ExposedList<PathConstraintData> pathConstraints = this.pathConstraints;
  186. for (int i = 0, n = pathConstraints.Count; i < n; i++)
  187. if (pathConstraints.Items[i].name.Equals(pathConstraintName)) return i;
  188. return -1;
  189. }
  190. // ---
  191. override public string ToString () {
  192. return name ?? base.ToString();
  193. }
  194. }
  195. }