CCBAnimationManager.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /****************************************************************************
  2. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  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 __CCB_CCBANIMATION_MANAGER_H__
  21. #define __CCB_CCBANIMATION_MANAGER_H__
  22. #include "base/CCMap.h"
  23. #include "2d/CCActionInterval.h"
  24. #include "2d/CCActionInstant.h"
  25. #include "2d/CCActionEase.h"
  26. #include "extensions/ExtensionMacros.h"
  27. #include "editor-support/cocosbuilder/CCBSequence.h"
  28. #include "editor-support/cocosbuilder/CCBKeyframe.h"
  29. #include "editor-support/cocosbuilder/CCBSequenceProperty.h"
  30. #include "extensions/GUI/CCControlExtension/CCControl.h"
  31. namespace cocosbuilder {
  32. class CC_DLL CCBAnimationManagerDelegate
  33. {
  34. public:
  35. virtual ~CCBAnimationManagerDelegate() {}
  36. virtual void completedAnimationSequenceNamed(const char *name) = 0;
  37. };
  38. class CC_DLL CCBAnimationManager : public cocos2d::Ref
  39. {
  40. public:
  41. bool _jsControlled;
  42. /**
  43. * @js ctor
  44. */
  45. CCBAnimationManager();
  46. /**
  47. * @js NA
  48. * @lua NA
  49. */
  50. ~CCBAnimationManager();
  51. cocos2d::Ref *_owner;
  52. virtual bool init();
  53. cocos2d::Vector<CCBSequence*>& getSequences();
  54. void setSequences(const cocos2d::Vector<CCBSequence*>& seq);
  55. int getAutoPlaySequenceId();
  56. void setAutoPlaySequenceId(int autoPlaySequenceId);
  57. cocos2d::Node* getRootNode();
  58. void setRootNode(cocos2d::Node* pRootNode); // weak reference
  59. void addDocumentCallbackNode(cocos2d::Node *node);
  60. void addDocumentCallbackName(std::string name);
  61. void addDocumentCallbackControlEvents(cocos2d::extension::Control::EventType eventType);
  62. void addDocumentOutletNode(cocos2d::Node *node);
  63. void addDocumentOutletName(std::string name);
  64. void setDocumentControllerName(const std::string &name);
  65. std::string getDocumentControllerName();
  66. cocos2d::ValueVector& getDocumentCallbackNames();
  67. cocos2d::Vector<cocos2d::Node*>& getDocumentCallbackNodes();
  68. cocos2d::ValueVector& getDocumentCallbackControlEvents();
  69. cocos2d::ValueVector& getDocumentOutletNames();
  70. cocos2d::Vector<cocos2d::Node*>& getDocumentOutletNodes();
  71. std::string getLastCompletedSequenceName();
  72. cocos2d::ValueVector& getKeyframeCallbacks();
  73. const cocos2d::Size& getRootContainerSize();
  74. void setRootContainerSize(const cocos2d::Size &rootContainerSize);
  75. CCBAnimationManagerDelegate* getDelegate();
  76. void setDelegate(CCBAnimationManagerDelegate* pDelegate); // retain
  77. const char* getRunningSequenceName();
  78. const cocos2d::Size& getContainerSize(cocos2d::Node* pNode);
  79. void addNode(cocos2d::Node *pNode, const std::unordered_map<int, cocos2d::Map<std::string, CCBSequenceProperty*>>& seq);
  80. void setBaseValue(const cocos2d::Value& value, cocos2d::Node *pNode, const std::string& propName);
  81. void setObject(cocos2d::Ref* obj, cocos2d::Node *pNode, const std::string& propName);
  82. void moveAnimationsFromNode(cocos2d::Node* fromNode, cocos2d::Node* toNode);
  83. /** @deprecated This interface will be deprecated sooner or later.*/
  84. CC_DEPRECATED_ATTRIBUTE void runAnimations(const char *pName, float fTweenDuration);
  85. /** @deprecated This interface will be deprecated sooner or later.*/
  86. CC_DEPRECATED_ATTRIBUTE void runAnimations(const char *pName);
  87. /** @deprecated This interface will be deprecated sooner or later.*/
  88. CC_DEPRECATED_ATTRIBUTE void runAnimations(int nSeqId, float fTweenDuraiton);
  89. void runAnimationsForSequenceNamedTweenDuration(const char *pName, float fTweenDuration);
  90. void runAnimationsForSequenceNamed(const char *pName);
  91. void runAnimationsForSequenceIdTweenDuration(int nSeqId, float fTweenDuraiton);
  92. /**
  93. * when this function bound to js ,the second param are callfunc_selector
  94. * @lua NA
  95. */
  96. void setAnimationCompletedCallback(cocos2d::Ref *target, cocos2d::SEL_CallFunc callbackFunc);
  97. void debug();
  98. /**
  99. * @js setCallFuncForJSCallbackNamed
  100. */
  101. void setCallFunc(cocos2d::CallFunc *callFunc, const std::string &callbackNamed);
  102. cocos2d::Sequence* actionForCallbackChannel(CCBSequenceProperty* channel);
  103. cocos2d::Sequence* actionForSoundChannel(CCBSequenceProperty* channel);
  104. // return -1 if timeline not exist
  105. int getSequenceId(const char* pSequenceName);
  106. // get timeline duration
  107. float getSequenceDuration(const char* pSequenceName);
  108. private:
  109. const cocos2d::Value& getBaseValue(cocos2d::Node *pNode, const std::string& propName);
  110. Ref* getObject(cocos2d::Node *pNode, const std::string& propName);
  111. CCBSequence* getSequence(int nSequenceId);
  112. cocos2d::ActionInterval* getAction(CCBKeyframe *pKeyframe0, CCBKeyframe *pKeyframe1, const std::string& propName, cocos2d::Node *pNode);
  113. void setAnimatedProperty(const std::string& propName, cocos2d::Node *pNode, const cocos2d::Value& value, Ref* obj, float fTweenDuration);
  114. void setFirstFrame(cocos2d::Node *pNode, CCBSequenceProperty *pSeqProp, float fTweenDuration);
  115. cocos2d::ActionInterval* getEaseAction(cocos2d::ActionInterval *pAction, CCBKeyframe::EasingType easingType, float fEasingOpt);
  116. void runAction(cocos2d::Node *pNode, CCBSequenceProperty *pSeqProp, float fTweenDuration);
  117. void sequenceCompleted();
  118. private:
  119. cocos2d::Vector<CCBSequence*> _sequences;
  120. std::unordered_map<cocos2d::Node*, std::unordered_map<int, cocos2d::Map<std::string, CCBSequenceProperty*>>> _nodeSequences;
  121. std::unordered_map<cocos2d::Node*, std::unordered_map<std::string, cocos2d::Value>> _baseValues;
  122. std::unordered_map<cocos2d::Node*, std::unordered_map<std::string, cocos2d::Ref*>> _objects;
  123. int _autoPlaySequenceId;
  124. cocos2d::Node *_rootNode;
  125. cocos2d::Size _rootContainerSize;
  126. CCBAnimationManagerDelegate *_delegate;
  127. CCBSequence *_runningSequence;
  128. cocos2d::ValueVector _documentOutletNames;
  129. cocos2d::Vector<cocos2d::Node*> _documentOutletNodes;
  130. cocos2d::ValueVector _documentCallbackNames;
  131. cocos2d::Vector<cocos2d::Node*> _documentCallbackNodes;
  132. cocos2d::ValueVector _documentCallbackControlEvents;
  133. cocos2d::ValueVector _keyframeCallbacks;
  134. cocos2d::Map<std::string, cocos2d::CallFunc*> _keyframeCallFuncs;
  135. std::string _documentControllerName;
  136. std::string _lastCompletedSequenceName;
  137. cocos2d::SEL_CallFunc _animationCompleteCallbackFunc;
  138. cocos2d::Ref *_target;
  139. };
  140. class CC_DLL CCBSetSpriteFrame : public cocos2d::ActionInstant
  141. {
  142. public:
  143. /** creates a Place action with a position */
  144. static CCBSetSpriteFrame* create(cocos2d::SpriteFrame *pSpriteFrame);
  145. /**
  146. * @js NA
  147. * @lua NA
  148. */
  149. ~CCBSetSpriteFrame();
  150. bool initWithSpriteFrame(cocos2d::SpriteFrame *pSpriteFrame);
  151. // Overrides
  152. virtual void update(float time) override;
  153. virtual CCBSetSpriteFrame* clone() const override;
  154. virtual CCBSetSpriteFrame* reverse() const override;
  155. private:
  156. cocos2d::SpriteFrame *_spriteFrame;
  157. };
  158. class CC_DLL CCBSoundEffect : public cocos2d::ActionInstant
  159. {
  160. public:
  161. static CCBSoundEffect* actionWithSoundFile(const std::string &file, float pitch, float pan, float gain);
  162. /**
  163. * @js NA
  164. * @lua NA
  165. */
  166. ~CCBSoundEffect();
  167. bool initWithSoundFile(const std::string &file, float pitch, float pan, float gain);
  168. // Overrides
  169. virtual void update(float time) override;
  170. virtual CCBSoundEffect* clone() const override;
  171. virtual CCBSoundEffect* reverse() const override;
  172. private:
  173. std::string _soundFile;
  174. float _pitch, _pan, _gain;
  175. };
  176. class CC_DLL CCBRotateTo : public cocos2d::ActionInterval
  177. {
  178. public:
  179. static CCBRotateTo* create(float fDuration, float fAngle);
  180. bool initWithDuration(float fDuration, float fAngle);
  181. // Override
  182. virtual void update(float time) override;
  183. virtual CCBRotateTo* clone() const override;
  184. virtual CCBRotateTo* reverse() const override;
  185. virtual void startWithTarget(cocos2d::Node *pNode) override;
  186. private:
  187. float _startAngle;
  188. float _dstAngle;
  189. float _diffAngle;
  190. };
  191. class CC_DLL CCBRotateXTo: public cocos2d::ActionInterval
  192. {
  193. public:
  194. static CCBRotateXTo* create(float fDuration, float fAngle);
  195. bool initWithDuration(float fDuration, float fAngle);
  196. // Overrides
  197. virtual void startWithTarget(cocos2d::Node *pNode) override;
  198. virtual CCBRotateXTo* clone() const override;
  199. virtual CCBRotateXTo* reverse() const override;
  200. virtual void update(float time) override;
  201. private:
  202. float _startAngle;
  203. float _dstAngle;
  204. float _diffAngle;
  205. };
  206. class CC_DLL CCBRotateYTo: public cocos2d::ActionInterval
  207. {
  208. public:
  209. static CCBRotateYTo* create(float fDuration, float fAngle);
  210. bool initWithDuration(float fDuration, float fAngle);
  211. // Override
  212. virtual void startWithTarget(cocos2d::Node *pNode) override;
  213. virtual CCBRotateYTo* clone() const override;
  214. virtual CCBRotateYTo* reverse() const override;
  215. virtual void update(float time) override;
  216. private:
  217. float _startAngle;
  218. float _dstAngle;
  219. float _diffAngle;
  220. };
  221. class CC_DLL CCBEaseInstant : public cocos2d::ActionEase
  222. {
  223. public:
  224. static CCBEaseInstant* create(cocos2d::ActionInterval *pAction);
  225. virtual CCBEaseInstant* clone() const override;
  226. virtual CCBEaseInstant* reverse() const override;
  227. virtual void update(float dt) override;
  228. };
  229. }
  230. #endif // __CCB_CCBANIMATION_MANAGER_H__