CCAnimate3D.h 6.3 KB

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