SkeletonData.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /******************************************************************************
  2. * Spine Runtimes License Agreement
  3. * Last updated July 28, 2023. Replaces all prior versions.
  4. *
  5. * Copyright (c) 2013-2023, 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 or
  13. * 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 THE
  27. * 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 ExposedList<PhysicsConstraintData> physicsConstraints = new ExposedList<PhysicsConstraintData>();
  44. internal float x, y, width, height, referenceScale = 100;
  45. internal string version, hash;
  46. // Nonessential.
  47. internal float fps;
  48. internal string imagesPath, audioPath;
  49. /// <summary>The skeleton's name, which by default is the name of the skeleton data file when possible, or null when a name hasn't been
  50. /// set.</summary>
  51. public string Name { get { return name; } set { name = value; } }
  52. /// <summary>The skeleton's bones, sorted parent first. The root bone is always the first bone.</summary>
  53. public ExposedList<BoneData> Bones { get { return bones; } }
  54. /// <summary>The skeleton's slots in the setup pose draw order.</summary>
  55. public ExposedList<SlotData> Slots { get { return slots; } }
  56. /// <summary>All skins, including the default skin.</summary>
  57. public ExposedList<Skin> Skins { get { return skins; } set { skins = value; } }
  58. /// <summary>
  59. /// The skeleton's default skin.
  60. /// By default this skin contains all attachments that were not in a skin in Spine.
  61. /// </summary>
  62. /// <return>May be null.</return>
  63. public Skin DefaultSkin { get { return defaultSkin; } set { defaultSkin = value; } }
  64. /// <summary>The skeleton's events.</summary>
  65. public ExposedList<EventData> Events { get { return events; } set { events = value; } }
  66. /// <summary>The skeleton's animations.</summary>
  67. public ExposedList<Animation> Animations { get { return animations; } set { animations = value; } }
  68. /// <summary>The skeleton's IK constraints.</summary>
  69. public ExposedList<IkConstraintData> IkConstraints { get { return ikConstraints; } set { ikConstraints = value; } }
  70. /// <summary>The skeleton's transform constraints.</summary>
  71. public ExposedList<TransformConstraintData> TransformConstraints { get { return transformConstraints; } set { transformConstraints = value; } }
  72. /// <summary>The skeleton's path constraints.</summary>
  73. public ExposedList<PathConstraintData> PathConstraints { get { return pathConstraints; } set { pathConstraints = value; } }
  74. /// <summary>The skeleton's physics constraints.</summary>
  75. public ExposedList<PhysicsConstraintData> PhysicsConstraints { get { return physicsConstraints; } set { physicsConstraints = value; } }
  76. public float X { get { return x; } set { x = value; } }
  77. public float Y { get { return y; } set { y = value; } }
  78. public float Width { get { return width; } set { width = value; } }
  79. public float Height { get { return height; } set { height = value; } }
  80. /// <summary> Baseline scale factor for applying distance-dependent effects on non-scalable properties, such as angle or scale. Default
  81. /// is 100.</summary>
  82. public float ReferenceScale { get { return referenceScale; } set { referenceScale = value; } }
  83. /// <summary>The Spine version used to export this data, or null.</summary>
  84. public string Version { get { return version; } set { version = value; } }
  85. /// <summary>The skeleton data hash. This value will change if any of the skeleton data has changed.
  86. /// May be null.</summary>
  87. public string Hash { get { return hash; } set { hash = value; } }
  88. public string ImagesPath { get { return imagesPath; } set { imagesPath = value; } }
  89. /// <summary> The path to the audio directory as defined in Spine. Available only when nonessential data was exported.
  90. /// May be null.</summary>
  91. public string AudioPath { get { return audioPath; } set { audioPath = value; } }
  92. /// <summary>The dopesheet FPS in Spine, or zero if nonessential data was not exported.</summary>
  93. public float Fps { get { return fps; } set { fps = value; } }
  94. // --- Bones
  95. /// <summary>
  96. /// Finds a bone by comparing each bone's name.
  97. /// It is more efficient to cache the results of this method than to call it multiple times.</summary>
  98. /// <returns>May be null.</returns>
  99. public BoneData FindBone (string boneName) {
  100. if (boneName == null) throw new ArgumentNullException("boneName", "boneName cannot be null.");
  101. BoneData[] bones = this.bones.Items;
  102. for (int i = 0, n = this.bones.Count; i < n; i++) {
  103. BoneData bone = bones[i];
  104. if (bone.name == boneName) return bone;
  105. }
  106. return null;
  107. }
  108. // --- Slots
  109. /// <returns>May be null.</returns>
  110. public SlotData FindSlot (string slotName) {
  111. if (slotName == null) throw new ArgumentNullException("slotName", "slotName cannot be null.");
  112. SlotData[] slots = this.slots.Items;
  113. for (int i = 0, n = this.slots.Count; i < n; i++) {
  114. SlotData slot = slots[i];
  115. if (slot.name == slotName) return slot;
  116. }
  117. return null;
  118. }
  119. // --- Skins
  120. /// <returns>May be null.</returns>
  121. public Skin FindSkin (string skinName) {
  122. if (skinName == null) throw new ArgumentNullException("skinName", "skinName cannot be null.");
  123. foreach (Skin skin in skins)
  124. if (skin.name == skinName) return skin;
  125. return null;
  126. }
  127. // --- Events
  128. /// <returns>May be null.</returns>
  129. public EventData FindEvent (string eventDataName) {
  130. if (eventDataName == null) throw new ArgumentNullException("eventDataName", "eventDataName cannot be null.");
  131. foreach (EventData eventData in events)
  132. if (eventData.name == eventDataName) return eventData;
  133. return null;
  134. }
  135. // --- Animations
  136. /// <returns>May be null.</returns>
  137. public Animation FindAnimation (string animationName) {
  138. if (animationName == null) throw new ArgumentNullException("animationName", "animationName cannot be null.");
  139. Animation[] animations = this.animations.Items;
  140. for (int i = 0, n = this.animations.Count; i < n; i++) {
  141. Animation animation = animations[i];
  142. if (animation.name == animationName) return animation;
  143. }
  144. return null;
  145. }
  146. // --- IK constraints
  147. /// <returns>May be null.</returns>
  148. public IkConstraintData FindIkConstraint (string constraintName) {
  149. if (constraintName == null) throw new ArgumentNullException("constraintName", "constraintName cannot be null.");
  150. IkConstraintData[] ikConstraints = this.ikConstraints.Items;
  151. for (int i = 0, n = this.ikConstraints.Count; i < n; i++) {
  152. IkConstraintData ikConstraint = ikConstraints[i];
  153. if (ikConstraint.name == constraintName) return ikConstraint;
  154. }
  155. return null;
  156. }
  157. // --- Transform constraints
  158. /// <returns>May be null.</returns>
  159. public TransformConstraintData FindTransformConstraint (string constraintName) {
  160. if (constraintName == null) throw new ArgumentNullException("constraintName", "constraintName cannot be null.");
  161. TransformConstraintData[] transformConstraints = this.transformConstraints.Items;
  162. for (int i = 0, n = this.transformConstraints.Count; i < n; i++) {
  163. TransformConstraintData transformConstraint = transformConstraints[i];
  164. if (transformConstraint.name == constraintName) return transformConstraint;
  165. }
  166. return null;
  167. }
  168. // --- Path constraints
  169. /// <summary>
  170. /// Finds a path constraint by comparing each path constraint's name. It is more efficient to cache the results of this method
  171. /// than to call it multiple times.
  172. /// </summary>
  173. /// <returns>May be null.</returns>
  174. public PathConstraintData FindPathConstraint (string constraintName) {
  175. if (constraintName == null) throw new ArgumentNullException("constraintName", "constraintName cannot be null.");
  176. PathConstraintData[] pathConstraints = this.pathConstraints.Items;
  177. for (int i = 0, n = this.pathConstraints.Count; i < n; i++) {
  178. PathConstraintData constraint = pathConstraints[i];
  179. if (constraint.name.Equals(constraintName)) return constraint;
  180. }
  181. return null;
  182. }
  183. // --- Physics constraints
  184. /// <summary>
  185. /// Finds a physics constraint by comparing each physics constraint's name. It is more efficient to cache the results of this
  186. /// method than to call it multiple times.
  187. /// </summary>
  188. /// <returns>May be null.</returns>
  189. public PhysicsConstraintData FindPhysicsConstraint (String constraintName) {
  190. if (constraintName == null) throw new ArgumentNullException("constraintName", "constraintName cannot be null.");
  191. PhysicsConstraintData[] physicsConstraints = this.physicsConstraints.Items;
  192. for (int i = 0, n = this.physicsConstraints.Count; i < n; i++) {
  193. PhysicsConstraintData constraint = (PhysicsConstraintData)physicsConstraints[i];
  194. if (constraint.name.Equals(constraintName)) return constraint;
  195. }
  196. return null;
  197. }
  198. // ---
  199. override public string ToString () {
  200. return name ?? base.ToString();
  201. }
  202. }
  203. }