SkeletonAnimation.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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_SKELETONANIMATION_H_
  30. #define SPINE_SKELETONANIMATION_H_
  31. #include <spine/spine.h>
  32. #include <spine/spine-cocos2dx.h>
  33. #include "cocos2d.h"
  34. namespace spine {
  35. struct SkeletonFrame
  36. {
  37. SkeletonFrame()
  38. {
  39. this->animation = "";
  40. this->progress = 0;
  41. this->loop = true;
  42. }
  43. SkeletonFrame(std::string animation,float progress, bool loop)
  44. {
  45. this->animation = animation;
  46. this->progress = progress;
  47. this->loop = loop;
  48. };
  49. std::string animation;
  50. float progress;
  51. bool loop;
  52. };
  53. class SkeletonAnimation;
  54. typedef std::function<void(spine::TrackEntry* entry)> StartListener;
  55. typedef std::function<void(spine::TrackEntry* entry)> InterruptListener;
  56. typedef std::function<void(spine::TrackEntry* entry)> EndListener;
  57. typedef std::function<void(spine::TrackEntry* entry)> DisposeListener;
  58. typedef std::function<void(spine::TrackEntry* entry)> CompleteListener;
  59. typedef std::function<void(TrackEntry* entry, Event* event)> EventListener;
  60. typedef std::function<void(SkeletonAnimation* node)> UpdateWorldTransformsListener;
  61. /** Draws an animated skeleton, providing an AnimationState for applying one or more animations and queuing animations to be
  62. * played later. */
  63. class SkeletonAnimation: public SkeletonRenderer {
  64. public:
  65. CREATE_FUNC(SkeletonAnimation);
  66. static SkeletonAnimation* createWithData (SkeletonData* skeletonData, bool ownsSkeletonData = false);
  67. static SkeletonAnimation* createWithJsonFile (const std::string& skeletonJsonFile, Atlas* atlas, float scale = 1);
  68. static SkeletonAnimation* createWithJsonFile (const std::string& skeletonJsonFile, const std::string& atlasFile, float scale = 1);
  69. static SkeletonAnimation* createWithBinaryFile (const std::string& skeletonBinaryFile, Atlas* atlas, float scale = 1);
  70. static SkeletonAnimation* createWithBinaryFile (const std::string& skeletonBinaryFile, const std::string& atlasFile, float scale = 1);
  71. // Use createWithJsonFile instead
  72. CC_DEPRECATED_ATTRIBUTE static SkeletonAnimation* createWithFile (const std::string& skeletonJsonFile, Atlas* atlas, float scale = 1)
  73. {
  74. return SkeletonAnimation::createWithJsonFile(skeletonJsonFile, atlas, scale);
  75. }
  76. // Use createWithJsonFile instead
  77. CC_DEPRECATED_ATTRIBUTE static SkeletonAnimation* createWithFile (const std::string& skeletonJsonFile, const std::string& atlasFile, float scale = 1)
  78. {
  79. return SkeletonAnimation::createWithJsonFile(skeletonJsonFile, atlasFile, scale);
  80. }
  81. virtual void update (float deltaTime) override;
  82. virtual void draw (cocos2d::Renderer* renderer, const cocos2d::Mat4& transform, uint32_t transformFlags) override;
  83. void setAnimationStateData (AnimationStateData* stateData);
  84. void setMix (const std::string& fromAnimation, const std::string& toAnimation, float duration);
  85. TrackEntry* setAnimation (int trackIndex, const std::string& name, bool loop);
  86. TrackEntry* addAnimation (int trackIndex, const std::string& name, bool loop, float delay = 0);
  87. TrackEntry* setEmptyAnimation (int trackIndex, float mixDuration);
  88. void setEmptyAnimations (float mixDuration);
  89. TrackEntry* addEmptyAnimation (int trackIndex, float mixDuration, float delay = 0);
  90. Animation* findAnimation(const std::string& name) const;
  91. TrackEntry* getCurrent (int trackIndex = 0);
  92. void clearTracks ();
  93. void clearTrack (int trackIndex = 0);
  94. void setStartListener (const StartListener& listener);
  95. void setInterruptListener (const InterruptListener& listener);
  96. void setEndListener (const EndListener& listener);
  97. void setDisposeListener (const DisposeListener& listener);
  98. void setCompleteListener (const CompleteListener& listener);
  99. void setEventListener (const EventListener& listener);
  100. void setPreUpdateWorldTransformsListener(const UpdateWorldTransformsListener& listener);
  101. void setPostUpdateWorldTransformsListener(const UpdateWorldTransformsListener& listener);
  102. void setTrackStartListener (TrackEntry* entry, const StartListener& listener);
  103. void setTrackInterruptListener (TrackEntry* entry, const InterruptListener& listener);
  104. void setTrackEndListener (TrackEntry* entry, const EndListener& listener);
  105. void setTrackDisposeListener (TrackEntry* entry, const DisposeListener& listener);
  106. void setTrackCompleteListener (TrackEntry* entry, const CompleteListener& listener);
  107. void setTrackEventListener (TrackEntry* entry, const EventListener& listener);
  108. virtual void onAnimationStateEvent (TrackEntry* entry, EventType type, Event* event);
  109. virtual void onTrackEntryEvent (TrackEntry* entry, EventType type, Event* event);
  110. AnimationState* getState() const;
  111. void setUpdateOnlyIfVisible(bool status);
  112. //add by yuntao.
  113. bool _checkOutScreen = false;
  114. cocos2d::Size _checkContentSz;
  115. void setNotCheckDrawAndUpdateIfOutScreen(const cocos2d::Size& contentSz, const cocos2d::Vec2& anchorPoint);
  116. bool beOutScreen();
  117. void changeSkeleton(const std::string& skeletonJsonFile, const std::string& atlasFile, float scale = 1.0f);
  118. void setDataFile(const std::string& dataFile) ;
  119. void setAtlasFile(const std::string& atlasFile) ;
  120. const std::string getDataFile() const{ return _dataFile;}
  121. const std::string getAtlasFile() const{ return _atlasFile;}
  122. CC_CONSTRUCTOR_ACCESS:
  123. SkeletonAnimation ();
  124. virtual ~SkeletonAnimation ();
  125. virtual void initialize () override;
  126. protected:
  127. AnimationState* _state = nullptr;
  128. bool _ownsAnimationStateData;
  129. bool _updateOnlyIfVisible;
  130. bool _firstDraw;
  131. std::string _dataFile;
  132. std::string _atlasFile;
  133. StartListener _startListener;
  134. InterruptListener _interruptListener;
  135. EndListener _endListener;
  136. DisposeListener _disposeListener;
  137. CompleteListener _completeListener;
  138. EventListener _eventListener;
  139. UpdateWorldTransformsListener _preUpdateListener;
  140. UpdateWorldTransformsListener _postUpdateListener;
  141. private:
  142. typedef SkeletonRenderer super;
  143. };
  144. class SpineActionInstant : public cocos2d::ActionInstant
  145. {
  146. public:
  147. /** creates a Place action with a position */
  148. static SpineActionInstant* create(SkeletonFrame startFrame);
  149. /**
  150. * @js NA
  151. * @lua NA
  152. */
  153. ~SpineActionInstant();
  154. bool initWithSkeletonFrame(SkeletonFrame startFrame);
  155. // Overrides
  156. virtual void update(float time) override;
  157. virtual SpineActionInstant* clone() const override;
  158. virtual SpineActionInstant* reverse() const override;
  159. private:
  160. SkeletonFrame _startFrame;
  161. };
  162. }
  163. #endif /* SPINE_SKELETONANIMATION_H_ */