CCActionEase.cpp 9.8 KB

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