CCActionEase.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. /*
  24. * Elastic, Back and Bounce actions based on code from:
  25. * http://github.com/NikhilK/silverlightfx/
  26. *
  27. * by http://github.com/NikhilK
  28. */
  29. #include "2d/CCActionEase.h"
  30. #include "2d/CCTweenFunction.h"
  31. NS_CC_BEGIN
  32. #ifndef M_PI_X_2
  33. #define M_PI_X_2 (float)M_PI * 2.0f
  34. #endif
  35. //
  36. // EaseAction
  37. //
  38. bool ActionEase::initWithAction(ActionInterval *action)
  39. {
  40. CCASSERT(action != nullptr, "action couldn't be nullptr!");
  41. if (action == nullptr)
  42. {
  43. return false;
  44. }
  45. if (ActionInterval::initWithDuration(action->getDuration()))
  46. {
  47. _inner = action;
  48. action->retain();
  49. return true;
  50. }
  51. return false;
  52. }
  53. ActionEase::~ActionEase(void)
  54. {
  55. CC_SAFE_RELEASE(_inner);
  56. }
  57. void ActionEase::startWithTarget(Node *target)
  58. {
  59. if (target && _inner)
  60. {
  61. ActionInterval::startWithTarget(target);
  62. _inner->startWithTarget(_target);
  63. }
  64. else
  65. {
  66. log("ActionEase::startWithTarget error: target or _inner is nullptr!");
  67. }
  68. }
  69. void ActionEase::stop(void)
  70. {
  71. if (_inner)
  72. _inner->stop();
  73. ActionInterval::stop();
  74. }
  75. void ActionEase::update(float time)
  76. {
  77. _inner->update(time);
  78. }
  79. ActionInterval* ActionEase::getInnerAction()
  80. {
  81. return _inner;
  82. }
  83. //
  84. // EaseRateAction
  85. //
  86. EaseRateAction* EaseRateAction::create(ActionInterval* action, float rate)
  87. {
  88. CCASSERT(action != nullptr, "action cannot be nullptr!");
  89. EaseRateAction *easeRateAction = new (std::nothrow) EaseRateAction();
  90. if (easeRateAction && easeRateAction->initWithAction(action, rate))
  91. {
  92. easeRateAction->autorelease();
  93. return easeRateAction;
  94. }
  95. CC_SAFE_DELETE(easeRateAction);
  96. return nullptr;
  97. }
  98. bool EaseRateAction::initWithAction(ActionInterval *action, float rate)
  99. {
  100. if (ActionEase::initWithAction(action))
  101. {
  102. _rate = rate;
  103. return true;
  104. }
  105. return false;
  106. }
  107. //
  108. // NOTE: Converting these macros into Templates is desirable, but please see
  109. // issue #16159 [https://github.com/cocos2d/cocos2d-x/pull/16159] for further info
  110. //
  111. #define EASE_TEMPLATE_IMPL(CLASSNAME, TWEEN_FUNC, REVERSE_CLASSNAME) \
  112. CLASSNAME* CLASSNAME::create(cocos2d::ActionInterval *action) \
  113. { \
  114. CLASSNAME *ease = new (std::nothrow) CLASSNAME(); \
  115. if (ease) \
  116. { \
  117. if (ease->initWithAction(action)) \
  118. ease->autorelease(); \
  119. else \
  120. CC_SAFE_RELEASE_NULL(ease); \
  121. } \
  122. return ease; \
  123. } \
  124. CLASSNAME* CLASSNAME::clone() const \
  125. { \
  126. if(_inner) return CLASSNAME::create(_inner->clone()); \
  127. return nullptr; \
  128. } \
  129. void CLASSNAME::update(float time) { \
  130. _inner->update(TWEEN_FUNC(time)); \
  131. } \
  132. ActionEase* CLASSNAME::reverse() const { \
  133. return REVERSE_CLASSNAME::create(_inner->reverse()); \
  134. }
  135. EASE_TEMPLATE_IMPL(EaseExponentialIn, tweenfunc::expoEaseIn, EaseExponentialOut);
  136. EASE_TEMPLATE_IMPL(EaseExponentialOut, tweenfunc::expoEaseOut, EaseExponentialIn);
  137. EASE_TEMPLATE_IMPL(EaseExponentialInOut, tweenfunc::expoEaseInOut, EaseExponentialInOut);
  138. EASE_TEMPLATE_IMPL(EaseSineIn, tweenfunc::sineEaseIn, EaseSineOut);
  139. EASE_TEMPLATE_IMPL(EaseSineOut, tweenfunc::sineEaseOut, EaseSineIn);
  140. EASE_TEMPLATE_IMPL(EaseSineInOut, tweenfunc::sineEaseInOut, EaseSineInOut);
  141. EASE_TEMPLATE_IMPL(EaseBounceIn, tweenfunc::bounceEaseIn, EaseBounceOut);
  142. EASE_TEMPLATE_IMPL(EaseBounceOut, tweenfunc::bounceEaseOut, EaseBounceIn);
  143. EASE_TEMPLATE_IMPL(EaseBounceInOut, tweenfunc::bounceEaseInOut, EaseBounceInOut);
  144. EASE_TEMPLATE_IMPL(EaseBackIn, tweenfunc::backEaseIn, EaseBackOut);
  145. EASE_TEMPLATE_IMPL(EaseBackOut, tweenfunc::backEaseOut, EaseBackIn);
  146. EASE_TEMPLATE_IMPL(EaseBackInOut, tweenfunc::backEaseInOut, EaseBackInOut);
  147. EASE_TEMPLATE_IMPL(EaseQuadraticActionIn, tweenfunc::quadraticIn, EaseQuadraticActionIn);
  148. EASE_TEMPLATE_IMPL(EaseQuadraticActionOut, tweenfunc::quadraticOut, EaseQuadraticActionOut);
  149. EASE_TEMPLATE_IMPL(EaseQuadraticActionInOut, tweenfunc::quadraticInOut, EaseQuadraticActionInOut);
  150. EASE_TEMPLATE_IMPL(EaseQuarticActionIn, tweenfunc::quartEaseIn, EaseQuarticActionIn);
  151. EASE_TEMPLATE_IMPL(EaseQuarticActionOut, tweenfunc::quartEaseOut, EaseQuarticActionOut);
  152. EASE_TEMPLATE_IMPL(EaseQuarticActionInOut, tweenfunc::quartEaseInOut, EaseQuarticActionInOut);
  153. EASE_TEMPLATE_IMPL(EaseQuinticActionIn, tweenfunc::quintEaseIn, EaseQuinticActionIn);
  154. EASE_TEMPLATE_IMPL(EaseQuinticActionOut, tweenfunc::quintEaseOut, EaseQuinticActionOut);
  155. EASE_TEMPLATE_IMPL(EaseQuinticActionInOut, tweenfunc::quintEaseInOut, EaseQuinticActionInOut);
  156. EASE_TEMPLATE_IMPL(EaseCircleActionIn, tweenfunc::circEaseIn, EaseCircleActionIn);
  157. EASE_TEMPLATE_IMPL(EaseCircleActionOut, tweenfunc::circEaseOut, EaseCircleActionOut);
  158. EASE_TEMPLATE_IMPL(EaseCircleActionInOut, tweenfunc::circEaseInOut, EaseCircleActionInOut);
  159. EASE_TEMPLATE_IMPL(EaseCubicActionIn, tweenfunc::cubicEaseIn, EaseCubicActionIn);
  160. EASE_TEMPLATE_IMPL(EaseCubicActionOut, tweenfunc::cubicEaseOut, EaseCubicActionOut);
  161. EASE_TEMPLATE_IMPL(EaseCubicActionInOut, tweenfunc::cubicEaseInOut, EaseCubicActionInOut);
  162. //
  163. // NOTE: Converting these macros into Templates is desirable, but please see
  164. // issue #16159 [https://github.com/cocos2d/cocos2d-x/pull/16159] for further info
  165. //
  166. #define EASERATE_TEMPLATE_IMPL(CLASSNAME, TWEEN_FUNC) \
  167. CLASSNAME* CLASSNAME::create(cocos2d::ActionInterval *action, float rate) \
  168. { \
  169. CLASSNAME *ease = new (std::nothrow) CLASSNAME(); \
  170. if (ease) \
  171. { \
  172. if (ease->initWithAction(action, rate)) \
  173. ease->autorelease(); \
  174. else \
  175. CC_SAFE_RELEASE_NULL(ease); \
  176. } \
  177. return ease; \
  178. } \
  179. CLASSNAME* CLASSNAME::clone() const \
  180. { \
  181. if(_inner) return CLASSNAME::create(_inner->clone(), _rate); \
  182. return nullptr; \
  183. } \
  184. void CLASSNAME::update(float time) { \
  185. _inner->update(TWEEN_FUNC(time, _rate)); \
  186. } \
  187. EaseRateAction* CLASSNAME::reverse() const { \
  188. return CLASSNAME::create(_inner->reverse(), 1.f / _rate); \
  189. }
  190. // NOTE: the original code used the same class for the `reverse()` method
  191. EASERATE_TEMPLATE_IMPL(EaseIn, tweenfunc::easeIn);
  192. EASERATE_TEMPLATE_IMPL(EaseOut, tweenfunc::easeOut);
  193. EASERATE_TEMPLATE_IMPL(EaseInOut, tweenfunc::easeInOut);
  194. //
  195. // EaseElastic
  196. //
  197. bool EaseElastic::initWithAction(ActionInterval *action, float period /* = 0.3f*/)
  198. {
  199. if (ActionEase::initWithAction(action))
  200. {
  201. _period = period;
  202. return true;
  203. }
  204. return false;
  205. }
  206. //
  207. // NOTE: Converting these macros into Templates is desirable, but please see
  208. // issue #16159 [https://github.com/cocos2d/cocos2d-x/pull/16159] for further info
  209. //
  210. #define EASEELASTIC_TEMPLATE_IMPL(CLASSNAME, TWEEN_FUNC, REVERSE_CLASSNAME) \
  211. CLASSNAME* CLASSNAME::create(cocos2d::ActionInterval *action, float period /* = 0.3f*/) \
  212. { \
  213. CLASSNAME *ease = new (std::nothrow) CLASSNAME(); \
  214. if (ease) \
  215. { \
  216. if (ease->initWithAction(action, period)) \
  217. ease->autorelease(); \
  218. else \
  219. CC_SAFE_RELEASE_NULL(ease); \
  220. } \
  221. return ease; \
  222. } \
  223. CLASSNAME* CLASSNAME::clone() const \
  224. { \
  225. if(_inner) return CLASSNAME::create(_inner->clone(), _period); \
  226. return nullptr; \
  227. } \
  228. void CLASSNAME::update(float time) { \
  229. _inner->update(TWEEN_FUNC(time, _period)); \
  230. } \
  231. EaseElastic* CLASSNAME::reverse() const { \
  232. return REVERSE_CLASSNAME::create(_inner->reverse(), _period); \
  233. }
  234. EASEELASTIC_TEMPLATE_IMPL(EaseElasticIn, tweenfunc::elasticEaseIn, EaseElasticOut);
  235. EASEELASTIC_TEMPLATE_IMPL(EaseElasticOut, tweenfunc::elasticEaseOut, EaseElasticIn);
  236. EASEELASTIC_TEMPLATE_IMPL(EaseElasticInOut, tweenfunc::elasticEaseInOut, EaseElasticInOut);
  237. //
  238. // EaseBezierAction
  239. //
  240. EaseBezierAction* EaseBezierAction::create(cocos2d::ActionInterval* action)
  241. {
  242. EaseBezierAction *ret = new (std::nothrow) EaseBezierAction();
  243. if (ret && ret->initWithAction(action))
  244. {
  245. ret->autorelease();
  246. return ret;
  247. }
  248. delete ret;
  249. return nullptr;
  250. }
  251. void EaseBezierAction::setBezierParamer( float p0, float p1, float p2, float p3)
  252. {
  253. _p0 = p0;
  254. _p1 = p1;
  255. _p2 = p2;
  256. _p3 = p3;
  257. }
  258. EaseBezierAction* EaseBezierAction::clone() const
  259. {
  260. // no copy constructor
  261. if (_inner)
  262. {
  263. auto ret = EaseBezierAction::create(_inner->clone());
  264. if (ret)
  265. {
  266. ret->setBezierParamer(_p0,_p1,_p2,_p3);
  267. }
  268. return ret;
  269. }
  270. return nullptr;
  271. }
  272. void EaseBezierAction::update(float time)
  273. {
  274. _inner->update(tweenfunc::bezieratFunction(_p0,_p1,_p2,_p3,time));
  275. }
  276. EaseBezierAction* EaseBezierAction::reverse() const
  277. {
  278. EaseBezierAction* reverseAction = EaseBezierAction::create(_inner->reverse());
  279. reverseAction->setBezierParamer(_p3,_p2,_p1,_p0);
  280. return reverseAction;
  281. }
  282. NS_CC_END