Skeleton.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. #ifndef Spine_Skeleton_h
  30. #define Spine_Skeleton_h
  31. #include <spine/Vector.h>
  32. #include <spine/MathUtil.h>
  33. #include <spine/SpineObject.h>
  34. #include <spine/SpineString.h>
  35. #include <spine/Color.h>
  36. namespace spine {
  37. class SkeletonData;
  38. class Bone;
  39. class Updatable;
  40. class Slot;
  41. class IkConstraint;
  42. class PathConstraint;
  43. class TransformConstraint;
  44. class Skin;
  45. class Attachment;
  46. class SP_API Skeleton : public SpineObject {
  47. friend class AnimationState;
  48. friend class SkeletonBounds;
  49. friend class SkeletonClipping;
  50. friend class AttachmentTimeline;
  51. friend class ColorTimeline;
  52. friend class DeformTimeline;
  53. friend class DrawOrderTimeline;
  54. friend class EventTimeline;
  55. friend class IkConstraintTimeline;
  56. friend class PathConstraintMixTimeline;
  57. friend class PathConstraintPositionTimeline;
  58. friend class PathConstraintSpacingTimeline;
  59. friend class ScaleTimeline;
  60. friend class ShearTimeline;
  61. friend class TransformConstraintTimeline;
  62. friend class TranslateTimeline;
  63. friend class TwoColorTimeline;
  64. public:
  65. explicit Skeleton(SkeletonData *skeletonData);
  66. ~Skeleton();
  67. /// Caches information about bones and constraints. Must be called if bones, constraints or weighted path attachments are added
  68. /// or removed.
  69. void updateCache();
  70. void printUpdateCache();
  71. /// Updates the world transform for each bone and applies constraints.
  72. void updateWorldTransform();
  73. /// Sets the bones, constraints, and slots to their setup pose values.
  74. void setToSetupPose();
  75. /// Sets the bones and constraints to their setup pose values.
  76. void setBonesToSetupPose();
  77. void setSlotsToSetupPose();
  78. /// @return May be NULL.
  79. Bone *findBone(const String &boneName);
  80. /// @return -1 if the bone was not found.
  81. int findBoneIndex(const String &boneName);
  82. /// @return May be NULL.
  83. Slot *findSlot(const String &slotName);
  84. /// @return -1 if the bone was not found.
  85. int findSlotIndex(const String &slotName);
  86. /// Sets a skin by name (see setSkin).
  87. void setSkin(const String &skinName);
  88. /// Attachments from the new skin are attached if the corresponding attachment from the old skin was attached.
  89. /// If there was no old skin, each slot's setup mode attachment is attached from the new skin.
  90. /// After changing the skin, the visible attachments can be reset to those attached in the setup pose by calling
  91. /// See Skeleton::setSlotsToSetupPose()
  92. /// Also, often AnimationState::apply(Skeleton&) is called before the next time the
  93. /// skeleton is rendered to allow any attachment keys in the current animation(s) to hide or show attachments from the new skin.
  94. /// @param newSkin May be NULL.
  95. void setSkin(Skin *newSkin);
  96. /// @return May be NULL.
  97. Attachment *getAttachment(const String &slotName, const String &attachmentName);
  98. /// @return May be NULL.
  99. Attachment *getAttachment(int slotIndex, const String &attachmentName);
  100. /// @param attachmentName May be empty.
  101. void setAttachment(const String &slotName, const String &attachmentName);
  102. /// @return May be NULL.
  103. IkConstraint *findIkConstraint(const String &constraintName);
  104. /// @return May be NULL.
  105. TransformConstraint *findTransformConstraint(const String &constraintName);
  106. /// @return May be NULL.
  107. PathConstraint *findPathConstraint(const String &constraintName);
  108. void update(float delta);
  109. /// Returns the axis aligned bounding box (AABB) of the region and mesh attachments for the current pose.
  110. /// @param outX The horizontal distance between the skeleton origin and the left side of the AABB.
  111. /// @param outY The vertical distance between the skeleton origin and the bottom side of the AABB.
  112. /// @param outWidth The width of the AABB
  113. /// @param outHeight The height of the AABB.
  114. /// @param outVertexBuffer Reference to hold a Vector of floats. This method will assign it with new floats as needed.
  115. void getBounds(float &outX, float &outY, float &outWidth, float &outHeight, Vector<float> &outVertexBuffer);
  116. Bone *getRootBone();
  117. SkeletonData *getData();
  118. Vector<Bone *> &getBones();
  119. Vector<Updatable *> &getUpdateCacheList();
  120. Vector<Slot *> &getSlots();
  121. Vector<Slot *> &getDrawOrder();
  122. Vector<IkConstraint *> &getIkConstraints();
  123. Vector<PathConstraint *> &getPathConstraints();
  124. Vector<TransformConstraint *> &getTransformConstraints();
  125. Skin *getSkin();
  126. Color &getColor();
  127. float getTime();
  128. void setTime(float inValue);
  129. void setPosition(float x, float y);
  130. float getX();
  131. void setX(float inValue);
  132. float getY();
  133. void setY(float inValue);
  134. float getScaleX();
  135. void setScaleX(float inValue);
  136. float getScaleY();
  137. void setScaleY(float inValue);
  138. private:
  139. SkeletonData *_data;
  140. Vector<Bone *> _bones;
  141. Vector<Slot *> _slots;
  142. Vector<Slot *> _drawOrder;
  143. Vector<IkConstraint *> _ikConstraints;
  144. Vector<TransformConstraint *> _transformConstraints;
  145. Vector<PathConstraint *> _pathConstraints;
  146. Vector<Updatable *> _updateCache;
  147. Vector<Bone *> _updateCacheReset;
  148. Skin *_skin;
  149. Color _color;
  150. float _time;
  151. float _scaleX, _scaleY;
  152. float _x, _y;
  153. void sortIkConstraint(IkConstraint *constraint);
  154. void sortPathConstraint(PathConstraint *constraint);
  155. void sortTransformConstraint(TransformConstraint *constraint);
  156. void sortPathConstraintAttachment(Skin *skin, size_t slotIndex, Bone &slotBone);
  157. void sortPathConstraintAttachment(Attachment *attachment, Bone &slotBone);
  158. void sortBone(Bone *bone);
  159. static void sortReset(Vector<Bone *> &bones);
  160. };
  161. }
  162. #endif /* Spine_Skeleton_h */