CCAnimate3D.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /****************************************************************************
  2. Copyright (c) 2014-2016 Chukong Technologies Inc.
  3. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #ifndef __CCANIMATE3D_H__
  22. #define __CCANIMATE3D_H__
  23. #include <map>
  24. #include <unordered_map>
  25. #include "3d/CCAnimation3D.h"
  26. #include "base/ccMacros.h"
  27. #include "base/CCRef.h"
  28. #include "2d/CCActionInterval.h"
  29. NS_CC_BEGIN
  30. class Bone3D;
  31. class Sprite3D;
  32. class EventCustom;
  33. enum class Animate3DQuality
  34. {
  35. QUALITY_NONE = 0, // it'll be ignore the curve-evaluating(the animation looks like stop), just accumulate transition time.
  36. QUALITY_LOW, // low animation quality, it'll be more efficient.
  37. QUALITY_HIGH, // high animation quality.
  38. };
  39. /**
  40. * @addtogroup _3d
  41. * @{
  42. */
  43. /**
  44. * @brief Animate3D, Animates a Sprite3D given with an Animation3D
  45. */
  46. class CC_DLL Animate3D: public ActionInterval
  47. {
  48. public:
  49. /**create Animate3D using Animation.*/
  50. static Animate3D* create(Animation3D* animation);
  51. /**
  52. * create Animate3D
  53. * @param animation used to generate animate3D
  54. * @param fromTime
  55. * @param duration Time the Animate3D lasts
  56. * @return Animate3D created using animate
  57. */
  58. static Animate3D* create(Animation3D* animation, float fromTime, float duration);
  59. /**
  60. * create Animate3D by frame section, [startFrame, endFrame)
  61. * @param animation used to generate animate3D
  62. * @param startFrame
  63. * @param endFrame
  64. * @param frameRate default is 30 per second
  65. * @return Animate3D created using animate
  66. */
  67. static Animate3D* createWithFrames(Animation3D* animation, int startFrame, int endFrame, float frameRate = 30.f);
  68. //
  69. // Overrides
  70. //
  71. virtual void stop() override;
  72. virtual void step(float dt) override;
  73. virtual void startWithTarget(Node *target) override;
  74. virtual Animate3D* reverse() const override;
  75. virtual Animate3D *clone() const override;
  76. virtual void update(float t) override;
  77. /**get & set speed, negative speed means playing reverse */
  78. float getSpeed() const;
  79. void setSpeed(float speed);
  80. /**get & set blend weight, weight must positive*/
  81. float getWeight() const { return _weight; }
  82. void setWeight(float weight);
  83. /**get & set origin interval*/
  84. void setOriginInterval(float interval);
  85. float getOriginInterval() const {return _originInterval; }
  86. /** get animate transition time between 3d animations */
  87. static float getTransitionTime() { return _transTime; }
  88. /** set animate transition time between 3d animations */
  89. static void setTransitionTime(float transTime) { if (transTime >= 0.f) _transTime = transTime; }
  90. /**get & set play reverse, these are deprecated, use set negative speed instead*/
  91. CC_DEPRECATED_ATTRIBUTE bool getPlayBack() const { return _playReverse; }
  92. CC_DEPRECATED_ATTRIBUTE void setPlayBack(bool reverse) { _playReverse = reverse; }
  93. /**set animate quality*/
  94. void setQuality(Animate3DQuality quality);
  95. /**get animate quality*/
  96. Animate3DQuality getQuality() const;
  97. struct Animate3DDisplayedEventInfo
  98. {
  99. int frame;
  100. Node* target;
  101. const ValueMap* userInfo;
  102. };
  103. void setKeyFrameUserInfo(int keyFrame, const ValueMap &userInfo);
  104. const ValueMap* getKeyFrameUserInfo(int keyFrame) const;
  105. ValueMap* getKeyFrameUserInfo(int keyFrame);
  106. CC_CONSTRUCTOR_ACCESS:
  107. Animate3D();
  108. virtual ~Animate3D();
  109. void removeFromMap();
  110. /** init method */
  111. bool init(Animation3D* animation);
  112. bool init(Animation3D* animation, float fromTime, float duration);
  113. bool initWithFrames(Animation3D* animation, int startFrame, int endFrame, float frameRate);
  114. protected:
  115. enum class Animate3DState
  116. {
  117. FadeIn,
  118. FadeOut,
  119. Running,
  120. };
  121. Animate3DState _state; //animation state
  122. Animation3D* _animation; //animation data
  123. float _absSpeed; //playing speed
  124. float _weight; //blend weight
  125. float _start; //start time 0 - 1, used to generate sub Animate3D
  126. float _last; //last time 0 - 1, used to generate sub Animate3D
  127. bool _playReverse; // is playing reverse
  128. static float _transTime; //transition time from one animate3d to another
  129. float _accTransTime; // accumulate transition time
  130. float _lastTime; // last t (0 - 1)
  131. float _originInterval;// save origin interval time
  132. float _frameRate;
  133. // animation quality
  134. EvaluateType _translateEvaluate;
  135. EvaluateType _roteEvaluate;
  136. EvaluateType _scaleEvaluate;
  137. Animate3DQuality _quality;
  138. std::unordered_map<Bone3D*, Animation3D::Curve*> _boneCurves; //weak ref
  139. std::unordered_map<Node*, Animation3D::Curve*> _nodeCurves;
  140. std::unordered_map<int, ValueMap> _keyFrameUserInfos;
  141. std::unordered_map<int, EventCustom*> _keyFrameEvent;
  142. std::unordered_map<int, Animate3DDisplayedEventInfo> _displayedEventInfo;
  143. //sprite animates
  144. static std::unordered_map<Node*, Animate3D*> s_fadeInAnimates;
  145. static std::unordered_map<Node*, Animate3D*> s_fadeOutAnimates;
  146. static std::unordered_map<Node*, Animate3D*> s_runningAnimates;
  147. };
  148. // end of 3d group
  149. /// @}
  150. NS_CC_END
  151. #endif // __CCANIMATE3D_H__