CCActionEase.h 14 KB

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