CCActionEase.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. /****************************************************************************
  2. Copyright (c) 2008-2009 Jason Booth
  3. Copyright (c) 2010-2012 cocos2d-x.org
  4. Copyright (c) 2013-2016 Chukong Technologies Inc.
  5. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  6. http://www.cocos2d-x.org
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. ****************************************************************************/
  23. #ifndef __ACTION_CCEASE_ACTION_H__
  24. #define __ACTION_CCEASE_ACTION_H__
  25. #include "2d/CCActionInterval.h"
  26. #include "2d/CCTweenFunction.h"
  27. NS_CC_BEGIN
  28. /**
  29. * @addtogroup actions
  30. * @{
  31. */
  32. /**
  33. @class ActionEase
  34. @brief Base class for Easing actions.
  35. @details Ease actions are created from other interval actions.
  36. The ease action will change the timeline of the inner action.
  37. @ingroup Actions
  38. */
  39. class CC_DLL ActionEase : public ActionInterval
  40. {
  41. public:
  42. /**
  43. @brief Get the pointer of the inner action.
  44. @return The pointer of the inner action.
  45. */
  46. virtual ActionInterval* getInnerAction();
  47. //
  48. // Overrides
  49. //
  50. virtual void startWithTarget(Node *target) override;
  51. virtual void stop() override;
  52. virtual void update(float time) override;
  53. CC_CONSTRUCTOR_ACCESS:
  54. ActionEase()
  55. : _inner(nullptr)
  56. {}
  57. virtual ~ActionEase();
  58. /**
  59. @brief Initializes the action.
  60. @return Return true when the initialization success, otherwise return false.
  61. */
  62. bool initWithAction(ActionInterval *action);
  63. protected:
  64. /** The inner action */
  65. ActionInterval *_inner;
  66. private:
  67. CC_DISALLOW_COPY_AND_ASSIGN(ActionEase);
  68. };
  69. /**
  70. @class EaseRateAction
  71. @brief Base class for Easing actions with rate parameters
  72. @details Ease the inner action with specified rate.
  73. @ingroup Actions
  74. */
  75. class CC_DLL EaseRateAction : public ActionEase
  76. {
  77. public:
  78. static EaseRateAction* create(ActionInterval* action, float rate);
  79. /**
  80. @brief Set the rate value for the ease rate action.
  81. @param rate The value will be set.
  82. */
  83. void setRate(float rate) { _rate = rate; }
  84. /**
  85. @brief Get the rate value of the ease rate action.
  86. @return Return the rate value of the ease rate action.
  87. */
  88. float getRate() const { return _rate; }
  89. CC_CONSTRUCTOR_ACCESS:
  90. EaseRateAction() {}
  91. virtual ~EaseRateAction() {}
  92. /**
  93. @brief Initializes the action with the inner action and the rate parameter.
  94. @param pAction The pointer of the inner action.
  95. @param fRate The value of the rate parameter.
  96. @return Return true when the initialization success, otherwise return false.
  97. */
  98. bool initWithAction(ActionInterval *pAction, float fRate);
  99. protected:
  100. float _rate;
  101. private:
  102. CC_DISALLOW_COPY_AND_ASSIGN(EaseRateAction);
  103. };
  104. //
  105. // NOTE: Converting these macros into Templates is desirable, but please see
  106. // issue #16159 [https://github.com/cocos2d/cocos2d-x/pull/16159] for further info
  107. //
  108. #define EASE_TEMPLATE_DECL_CLASS(CLASSNAME) \
  109. class CC_DLL CLASSNAME : public ActionEase \
  110. { \
  111. CC_CONSTRUCTOR_ACCESS: \
  112. virtual ~CLASSNAME() { } \
  113. CLASSNAME() { } \
  114. public: \
  115. static CLASSNAME* create(ActionInterval* action); \
  116. virtual CLASSNAME* clone() const override; \
  117. virtual void update(float time) override; \
  118. virtual ActionEase* reverse() const override; \
  119. private: \
  120. CC_DISALLOW_COPY_AND_ASSIGN(CLASSNAME); \
  121. };
  122. /**
  123. @class EaseExponentialIn
  124. @brief Ease Exponential In action.
  125. @details The timeline of inner action will be changed by:
  126. \f${ 2 }^{ 10*(time-1) }-1*0.001\f$.
  127. @ingroup Actions
  128. */
  129. EASE_TEMPLATE_DECL_CLASS(EaseExponentialIn);
  130. /**
  131. @class EaseExponentialOut
  132. @brief Ease Exponential Out
  133. @details The timeline of inner action will be changed by:
  134. \f$1-{ 2 }^{ -10*(time-1) }\f$.
  135. @ingroup Actions
  136. */
  137. EASE_TEMPLATE_DECL_CLASS(EaseExponentialOut);
  138. /**
  139. @class EaseExponentialInOut
  140. @brief Ease Exponential InOut
  141. @details If time * 2 < 1, the timeline of inner action will be changed by:
  142. \f$0.5*{ 2 }^{ 10*(time-1) }\f$.
  143. else, the timeline of inner action will be changed by:
  144. \f$0.5*(2-{ 2 }^{ -10*(time-1) })\f$.
  145. @ingroup Actions
  146. */
  147. EASE_TEMPLATE_DECL_CLASS(EaseExponentialInOut);
  148. /**
  149. @class EaseSineIn
  150. @brief Ease Sine In
  151. @details The timeline of inner action will be changed by:
  152. \f$1-cos(time*\frac { \pi }{ 2 } )\f$.
  153. @ingroup Actions
  154. */
  155. EASE_TEMPLATE_DECL_CLASS(EaseSineIn);
  156. /**
  157. @class EaseSineOut
  158. @brief Ease Sine Out
  159. @details The timeline of inner action will be changed by:
  160. \f$sin(time*\frac { \pi }{ 2 } )\f$.
  161. @ingroup Actions
  162. */
  163. EASE_TEMPLATE_DECL_CLASS(EaseSineOut);
  164. /**
  165. @class EaseSineInOut
  166. @brief Ease Sine InOut
  167. @details The timeline of inner action will be changed by:
  168. \f$-0.5*(cos(\pi *time)-1)\f$.
  169. @ingroup Actions
  170. */
  171. EASE_TEMPLATE_DECL_CLASS(EaseSineInOut);
  172. /**
  173. @class EaseBounce
  174. @brief EaseBounce abstract class.
  175. @since v0.8.2
  176. @ingroup Actions
  177. */
  178. class CC_DLL EaseBounce : public ActionEase {};
  179. /**
  180. @class EaseBounceIn
  181. @brief EaseBounceIn action.
  182. @warning This action doesn't use a bijective function.
  183. Actions like Sequence might have an unexpected result when used with this action.
  184. @since v0.8.2
  185. @ingroup Actions
  186. */
  187. EASE_TEMPLATE_DECL_CLASS(EaseBounceIn);
  188. /**
  189. @class EaseBounceOut
  190. @brief EaseBounceOut action.
  191. @warning This action doesn't use a bijective function.
  192. Actions like Sequence might have an unexpected result when used with this action.
  193. @since v0.8.2
  194. @ingroup Actions
  195. */
  196. EASE_TEMPLATE_DECL_CLASS(EaseBounceOut);
  197. /**
  198. @class EaseBounceInOut
  199. @brief EaseBounceInOut action.
  200. @warning This action doesn't use a bijective function.
  201. Actions like Sequence might have an unexpected result when used with this action.
  202. @since v0.8.2
  203. @ingroup Actions
  204. */
  205. EASE_TEMPLATE_DECL_CLASS(EaseBounceInOut);
  206. /**
  207. @class EaseBackIn
  208. @brief EaseBackIn action.
  209. @warning This action doesn't use a bijective function.
  210. Actions like Sequence might have an unexpected result when used with this action.
  211. @since v0.8.2
  212. @ingroup Actions
  213. */
  214. EASE_TEMPLATE_DECL_CLASS(EaseBackIn);
  215. /**
  216. @class EaseBackOut
  217. @brief EaseBackOut action.
  218. @warning This action doesn't use a bijective function.
  219. Actions like Sequence might have an unexpected result when used with this action.
  220. @since v0.8.2
  221. @ingroup Actions
  222. */
  223. EASE_TEMPLATE_DECL_CLASS(EaseBackOut);
  224. /**
  225. @class EaseBackInOut
  226. @brief EaseBackInOut action.
  227. @warning This action doesn't use a bijective function.
  228. Actions like Sequence might have an unexpected result when used with this action.
  229. @since v0.8.2
  230. @ingroup Actions
  231. */
  232. EASE_TEMPLATE_DECL_CLASS(EaseBackInOut);
  233. /**
  234. @class EaseQuadraticActionIn
  235. @brief Ease Quadratic In
  236. @ingroup Actions
  237. */
  238. EASE_TEMPLATE_DECL_CLASS(EaseQuadraticActionIn);
  239. /**
  240. @class EaseQuadraticActionOut
  241. @brief Ease Quadratic Out
  242. @ingroup Actions
  243. */
  244. EASE_TEMPLATE_DECL_CLASS(EaseQuadraticActionOut);
  245. /**
  246. @class EaseQuadraticActionInOut
  247. @brief Ease Quadratic InOut
  248. @ingroup Actions
  249. */
  250. EASE_TEMPLATE_DECL_CLASS(EaseQuadraticActionInOut);
  251. /**
  252. @class EaseQuarticActionIn
  253. @brief Ease Quartic In
  254. @ingroup Actions
  255. */
  256. EASE_TEMPLATE_DECL_CLASS(EaseQuarticActionIn);
  257. /**
  258. @class EaseQuarticActionOut
  259. @brief Ease Quartic Out
  260. @ingroup Actions
  261. */
  262. EASE_TEMPLATE_DECL_CLASS(EaseQuarticActionOut);
  263. /**
  264. @class EaseQuarticActionInOut
  265. @brief Ease Quartic InOut
  266. @ingroup Actions
  267. */
  268. EASE_TEMPLATE_DECL_CLASS(EaseQuarticActionInOut);
  269. /**
  270. @class EaseQuinticActionIn
  271. @brief Ease Quintic In
  272. @ingroup Actions
  273. */
  274. EASE_TEMPLATE_DECL_CLASS(EaseQuinticActionIn);
  275. /**
  276. @class EaseQuinticActionOut
  277. @brief Ease Quintic Out
  278. @ingroup Actions
  279. */
  280. EASE_TEMPLATE_DECL_CLASS(EaseQuinticActionOut);
  281. /**
  282. @class EaseQuinticActionInOut
  283. @brief Ease Quintic InOut
  284. @ingroup Actions
  285. */
  286. EASE_TEMPLATE_DECL_CLASS(EaseQuinticActionInOut);
  287. /**
  288. @class EaseCircleActionIn
  289. @brief Ease Circle In
  290. @ingroup Actions
  291. */
  292. EASE_TEMPLATE_DECL_CLASS(EaseCircleActionIn);
  293. /**
  294. @class EaseCircleActionOut
  295. @brief Ease Circle Out
  296. @ingroup Actions
  297. */
  298. EASE_TEMPLATE_DECL_CLASS(EaseCircleActionOut);
  299. /**
  300. @class EaseCircleActionInOut
  301. @brief Ease Circle InOut
  302. @ingroup Actions
  303. */
  304. EASE_TEMPLATE_DECL_CLASS(EaseCircleActionInOut);
  305. /**
  306. @class EaseCubicActionIn
  307. @brief Ease Cubic In
  308. @ingroup Actions
  309. */
  310. EASE_TEMPLATE_DECL_CLASS(EaseCubicActionIn);
  311. /**
  312. @class EaseCubicActionOut
  313. @brief Ease Cubic Out
  314. @ingroup Actions
  315. */
  316. EASE_TEMPLATE_DECL_CLASS(EaseCubicActionOut);
  317. /**
  318. @class EaseCubicActionInOut
  319. @brief Ease Cubic InOut
  320. @ingroup Actions
  321. */
  322. EASE_TEMPLATE_DECL_CLASS(EaseCubicActionInOut);
  323. //
  324. // NOTE: Converting these macros into Templates is desirable, but please see
  325. // issue #16159 [https://github.com/cocos2d/cocos2d-x/pull/16159] for further info
  326. //
  327. #define EASERATE_TEMPLATE_DECL_CLASS(CLASSNAME) \
  328. class CC_DLL CLASSNAME : public EaseRateAction \
  329. { \
  330. CC_CONSTRUCTOR_ACCESS: \
  331. virtual ~CLASSNAME() { } \
  332. CLASSNAME() { } \
  333. public: \
  334. static CLASSNAME* create(ActionInterval* action, float rate); \
  335. virtual CLASSNAME* clone() const override; \
  336. virtual void update(float time) override; \
  337. virtual EaseRateAction* reverse() const override; \
  338. private: \
  339. CC_DISALLOW_COPY_AND_ASSIGN(CLASSNAME); \
  340. };
  341. /**
  342. @class EaseIn
  343. @brief EaseIn action with a rate.
  344. @details The timeline of inner action will be changed by:
  345. \f${ time }^{ rate }\f$.
  346. @ingroup Actions
  347. */
  348. EASERATE_TEMPLATE_DECL_CLASS(EaseIn);
  349. /**
  350. @class EaseOut
  351. @brief EaseOut action with a rate.
  352. @details The timeline of inner action will be changed by:
  353. \f${ time }^ { (1/rate) }\f$.
  354. @ingroup Actions
  355. */
  356. EASERATE_TEMPLATE_DECL_CLASS(EaseOut);
  357. /**
  358. @class EaseInOut
  359. @brief EaseInOut action with a rate
  360. @details If time * 2 < 1, the timeline of inner action will be changed by:
  361. \f$0.5*{ time }^{ rate }\f$.
  362. Else, the timeline of inner action will be changed by:
  363. \f$1.0-0.5*{ 2-time }^{ rate }\f$.
  364. @ingroup Actions
  365. */
  366. EASERATE_TEMPLATE_DECL_CLASS(EaseInOut);
  367. /**
  368. @class EaseElastic
  369. @brief Ease Elastic abstract class
  370. @since v0.8.2
  371. @ingroup Actions
  372. */
  373. class CC_DLL EaseElastic : public ActionEase
  374. {
  375. public:
  376. /**
  377. @brief Get period of the wave in radians. Default value is 0.3.
  378. @return Return the period of the wave in radians.
  379. */
  380. float getPeriod() const { return _period; }
  381. /**
  382. @brief Set period of the wave in radians.
  383. @param fPeriod The value will be set.
  384. */
  385. void setPeriod(float fPeriod) { _period = fPeriod; }
  386. CC_CONSTRUCTOR_ACCESS:
  387. EaseElastic() {}
  388. virtual ~EaseElastic() {}
  389. /**
  390. @brief Initializes the action with the inner action and the period in radians.
  391. @param action The pointer of the inner action.
  392. @param period Period of the wave in radians. Default is 0.3.
  393. @return Return true when the initialization success, otherwise return false.
  394. */
  395. bool initWithAction(ActionInterval *action, float period = 0.3f);
  396. protected:
  397. float _period;
  398. private:
  399. CC_DISALLOW_COPY_AND_ASSIGN(EaseElastic);
  400. };
  401. //
  402. // NOTE: Converting these macros into Templates is desirable, but please see
  403. // issue #16159 [https://github.com/cocos2d/cocos2d-x/pull/16159] for further info
  404. //
  405. #define EASEELASTIC_TEMPLATE_DECL_CLASS(CLASSNAME) \
  406. class CC_DLL CLASSNAME : public EaseElastic \
  407. { \
  408. CC_CONSTRUCTOR_ACCESS: \
  409. virtual ~CLASSNAME() { } \
  410. CLASSNAME() { } \
  411. public: \
  412. static CLASSNAME* create(ActionInterval* action, float rate = 0.3f); \
  413. virtual CLASSNAME* clone() const override; \
  414. virtual void update(float time) override; \
  415. virtual EaseElastic* reverse() const override; \
  416. private: \
  417. CC_DISALLOW_COPY_AND_ASSIGN(CLASSNAME); \
  418. };
  419. /**
  420. @class EaseElasticIn
  421. @brief Ease Elastic In action.
  422. @details If time == 0 or time == 1, the timeline of inner action will not be changed.
  423. Else, the timeline of inner action will be changed by:
  424. \f$-{ 2 }^{ 10*(time-1) }*sin((time-1-\frac { period }{ 4 } )*\pi *2/period)\f$.
  425. @warning This action doesn't use a bijective function.
  426. Actions like Sequence might have an unexpected result when used with this action.
  427. @since v0.8.2
  428. @ingroup Actions
  429. */
  430. EASEELASTIC_TEMPLATE_DECL_CLASS(EaseElasticIn);
  431. /**
  432. @class EaseElasticOut
  433. @brief Ease Elastic Out action.
  434. @details If time == 0 or time == 1, the timeline of inner action will not be changed.
  435. Else, the timeline of inner action will be changed by:
  436. \f${ 2 }^{ -10*time }*sin((time-\frac { period }{ 4 } )*\pi *2/period)+1\f$.
  437. @warning This action doesn't use a bijective function.
  438. Actions like Sequence might have an unexpected result when used with this action.
  439. @since v0.8.2
  440. @ingroup Actions
  441. */
  442. EASEELASTIC_TEMPLATE_DECL_CLASS(EaseElasticOut);
  443. /**
  444. @class EaseElasticInOut
  445. @brief Ease Elastic InOut action.
  446. @warning This action doesn't use a bijective function.
  447. Actions like Sequence might have an unexpected result when used with this action.
  448. @since v0.8.2
  449. @ingroup Actions
  450. */
  451. EASEELASTIC_TEMPLATE_DECL_CLASS(EaseElasticInOut);
  452. /**
  453. @class EaseBezierAction
  454. @brief Ease Bezier
  455. @ingroup Actions
  456. */
  457. class CC_DLL EaseBezierAction : public cocos2d::ActionEase
  458. {
  459. public:
  460. /**
  461. @brief Create the action with the inner action.
  462. @param action The pointer of the inner action.
  463. @return A pointer of EaseBezierAction action. If creation failed, return nil.
  464. */
  465. static EaseBezierAction* create(cocos2d::ActionInterval* action);
  466. virtual void update(float time) override;
  467. virtual EaseBezierAction* clone() const override;
  468. virtual EaseBezierAction* reverse() const override;
  469. /**
  470. @brief Set the bezier parameters.
  471. */
  472. virtual void setBezierParamer( float p0, float p1, float p2, float p3);
  473. CC_CONSTRUCTOR_ACCESS:
  474. EaseBezierAction() {}
  475. virtual ~EaseBezierAction() {}
  476. protected:
  477. float _p0;
  478. float _p1;
  479. float _p2;
  480. float _p3;
  481. private:
  482. CC_DISALLOW_COPY_AND_ASSIGN(EaseBezierAction);
  483. };
  484. // end of actions group
  485. /// @}
  486. NS_CC_END
  487. #endif // __ACTION_CCEASE_ACTION_H__