SkeletonData.cs 9.1 KB

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