CCActionInstant.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. /****************************************************************************
  2. Copyright (c) 2008-2010 Ricardo Quesada
  3. Copyright (c) 2010-2012 cocos2d-x.org
  4. Copyright (c) 2011 Zynga Inc.
  5. Copyright (c) 2013-2016 Chukong Technologies Inc.
  6. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  7. http://www.cocos2d-x.org
  8. Permission is hereby granted, free of charge, to any person obtaining a copy
  9. of this software and associated documentation files (the "Software"), to deal
  10. in the Software without restriction, including without limitation the rights
  11. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. copies of the Software, and to permit persons to whom the Software is
  13. furnished to do so, subject to the following conditions:
  14. The above copyright notice and this permission notice shall be included in
  15. all copies or substantial portions of the Software.
  16. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. THE SOFTWARE.
  23. ****************************************************************************/
  24. #include "2d/CCActionInstant.h"
  25. #include "2d/CCNode.h"
  26. #include "2d/CCSprite.h"
  27. #if defined(__GNUC__) && ((__GNUC__ >= 4) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1)))
  28. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  29. #elif _MSC_VER >= 1400 //vs 2005 or higher
  30. #pragma warning (push)
  31. #pragma warning (disable: 4996)
  32. #endif
  33. NS_CC_BEGIN
  34. //
  35. // InstantAction
  36. //
  37. void ActionInstant::startWithTarget(Node *target)
  38. {
  39. FiniteTimeAction::startWithTarget(target);
  40. _done = false;
  41. }
  42. bool ActionInstant::isDone() const
  43. {
  44. return _done;
  45. }
  46. void ActionInstant::step(float /*dt*/)
  47. {
  48. float updateDt = 1;
  49. #if CC_ENABLE_SCRIPT_BINDING
  50. if (_scriptType == kScriptTypeJavascript)
  51. {
  52. if (ScriptEngineManager::sendActionEventToJS(this, kActionUpdate, (void *)&updateDt))
  53. return;
  54. }
  55. #endif
  56. update(updateDt);
  57. }
  58. void ActionInstant::update(float /*time*/)
  59. {
  60. _done = true;
  61. }
  62. //
  63. // Show
  64. //
  65. Show* Show::create()
  66. {
  67. Show* ret = new (std::nothrow) Show();
  68. if (ret)
  69. {
  70. ret->autorelease();
  71. }
  72. return ret;
  73. }
  74. void Show::update(float time)
  75. {
  76. ActionInstant::update(time);
  77. _target->setVisible(true);
  78. }
  79. ActionInstant* Show::reverse() const
  80. {
  81. return Hide::create();
  82. }
  83. Show* Show::clone() const
  84. {
  85. // no copy constructor
  86. return Show::create();
  87. }
  88. //
  89. // Hide
  90. //
  91. Hide * Hide::create()
  92. {
  93. Hide *ret = new (std::nothrow) Hide();
  94. if (ret)
  95. {
  96. ret->autorelease();
  97. }
  98. return ret;
  99. }
  100. void Hide::update(float time)
  101. {
  102. ActionInstant::update(time);
  103. _target->setVisible(false);
  104. }
  105. ActionInstant *Hide::reverse() const
  106. {
  107. return Show::create();
  108. }
  109. Hide* Hide::clone() const
  110. {
  111. // no copy constructor
  112. return Hide::create();
  113. }
  114. //
  115. // ToggleVisibility
  116. //
  117. ToggleVisibility * ToggleVisibility::create()
  118. {
  119. ToggleVisibility *ret = new (std::nothrow) ToggleVisibility();
  120. if (ret)
  121. {
  122. ret->autorelease();
  123. }
  124. return ret;
  125. }
  126. void ToggleVisibility::update(float time)
  127. {
  128. ActionInstant::update(time);
  129. _target->setVisible(!_target->isVisible());
  130. }
  131. ToggleVisibility * ToggleVisibility::reverse() const
  132. {
  133. return ToggleVisibility::create();
  134. }
  135. ToggleVisibility * ToggleVisibility::clone() const
  136. {
  137. // no copy constructor
  138. return ToggleVisibility::create();
  139. }
  140. //
  141. // Remove Self
  142. //
  143. RemoveSelf * RemoveSelf::create(bool isNeedCleanUp /*= true*/)
  144. {
  145. RemoveSelf *ret = new (std::nothrow) RemoveSelf();
  146. if (ret && ret->init(isNeedCleanUp))
  147. {
  148. ret->autorelease();
  149. }
  150. return ret;
  151. }
  152. bool RemoveSelf::init(bool isNeedCleanUp)
  153. {
  154. _isNeedCleanUp = isNeedCleanUp;
  155. return true;
  156. }
  157. void RemoveSelf::update(float time)
  158. {
  159. ActionInstant::update(time);
  160. _target->removeFromParentAndCleanup(_isNeedCleanUp);
  161. }
  162. RemoveSelf *RemoveSelf::reverse() const
  163. {
  164. return RemoveSelf::create(_isNeedCleanUp);
  165. }
  166. RemoveSelf * RemoveSelf::clone() const
  167. {
  168. // no copy constructor
  169. return RemoveSelf::create(_isNeedCleanUp);
  170. }
  171. //
  172. // FlipX
  173. //
  174. FlipX *FlipX::create(bool x)
  175. {
  176. FlipX *ret = new (std::nothrow) FlipX();
  177. if (ret && ret->initWithFlipX(x))
  178. {
  179. ret->autorelease();
  180. return ret;
  181. }
  182. CC_SAFE_DELETE(ret);
  183. return nullptr;
  184. }
  185. bool FlipX::initWithFlipX(bool x)
  186. {
  187. _flipX = x;
  188. return true;
  189. }
  190. void FlipX::update(float time)
  191. {
  192. ActionInstant::update(time);
  193. static_cast<Sprite*>(_target)->setFlippedX(_flipX);
  194. }
  195. FlipX* FlipX::reverse() const
  196. {
  197. return FlipX::create(!_flipX);
  198. }
  199. FlipX * FlipX::clone() const
  200. {
  201. // no copy constructor
  202. return FlipX::create(_flipX);
  203. }
  204. //
  205. // FlipY
  206. //
  207. FlipY * FlipY::create(bool y)
  208. {
  209. FlipY *ret = new (std::nothrow) FlipY();
  210. if (ret && ret->initWithFlipY(y))
  211. {
  212. ret->autorelease();
  213. return ret;
  214. }
  215. CC_SAFE_DELETE(ret);
  216. return nullptr;
  217. }
  218. bool FlipY::initWithFlipY(bool y)
  219. {
  220. _flipY = y;
  221. return true;
  222. }
  223. void FlipY::update(float time)
  224. {
  225. ActionInstant::update(time);
  226. static_cast<Sprite*>(_target)->setFlippedY(_flipY);
  227. }
  228. FlipY* FlipY::reverse() const
  229. {
  230. return FlipY::create(!_flipY);
  231. }
  232. FlipY * FlipY::clone() const
  233. {
  234. // no copy constructor
  235. return FlipY::create(_flipY);
  236. }
  237. //
  238. // Place
  239. //
  240. Place* Place::create(const Vec2& pos)
  241. {
  242. Place *ret = new (std::nothrow) Place();
  243. if (ret && ret->initWithPosition(pos))
  244. {
  245. ret->autorelease();
  246. return ret;
  247. }
  248. delete ret;
  249. return nullptr;
  250. }
  251. bool Place::initWithPosition(const Vec2& pos)
  252. {
  253. _position = pos;
  254. return true;
  255. }
  256. Place * Place::clone() const
  257. {
  258. // no copy constructor
  259. return Place::create(_position);
  260. }
  261. Place * Place::reverse() const
  262. {
  263. // no reverse, just clone
  264. return this->clone();
  265. }
  266. void Place::update(float time)
  267. {
  268. ActionInstant::update(time);
  269. _target->setPosition(_position);
  270. }
  271. //
  272. // CallFunc
  273. //
  274. CallFunc * CallFunc::create(const std::function<void()> &func)
  275. {
  276. CallFunc *ret = new (std::nothrow) CallFunc();
  277. if (ret && ret->initWithFunction(func) )
  278. {
  279. ret->autorelease();
  280. return ret;
  281. }
  282. CC_SAFE_DELETE(ret);
  283. return nullptr;
  284. }
  285. CallFunc * CallFunc::create(Ref* selectorTarget, SEL_CallFunc selector)
  286. {
  287. CallFunc *ret = new (std::nothrow) CallFunc();
  288. if (ret && ret->initWithTarget(selectorTarget))
  289. {
  290. ret->_callFunc = selector;
  291. ret->autorelease();
  292. return ret;
  293. }
  294. CC_SAFE_DELETE(ret);
  295. return nullptr;
  296. }
  297. bool CallFunc::initWithFunction(const std::function<void()> &func)
  298. {
  299. _function = func;
  300. return true;
  301. }
  302. bool CallFunc::initWithTarget(Ref* target)
  303. {
  304. if (target)
  305. {
  306. target->retain();
  307. }
  308. if (_selectorTarget)
  309. {
  310. _selectorTarget->release();
  311. }
  312. _selectorTarget = target;
  313. return true;
  314. }
  315. CallFunc::~CallFunc()
  316. {
  317. CC_SAFE_RELEASE(_selectorTarget);
  318. }
  319. CallFunc * CallFunc::clone() const
  320. {
  321. // no copy constructor
  322. auto a = new (std::nothrow) CallFunc();
  323. if( _selectorTarget)
  324. {
  325. a->initWithTarget(_selectorTarget);
  326. a->_callFunc = _callFunc;
  327. }
  328. else if( _function )
  329. {
  330. a->initWithFunction(_function);
  331. }
  332. a->autorelease();
  333. return a;
  334. }
  335. CallFunc * CallFunc::reverse() const
  336. {
  337. // no reverse here, just return a clone
  338. return this->clone();
  339. }
  340. void CallFunc::update(float time)
  341. {
  342. ActionInstant::update(time);
  343. this->execute();
  344. }
  345. void CallFunc::execute()
  346. {
  347. if (_callFunc)
  348. {
  349. (_selectorTarget->*_callFunc)();
  350. }
  351. else if( _function )
  352. {
  353. _function();
  354. }
  355. }
  356. //
  357. // CallFuncN
  358. //
  359. CallFuncN * CallFuncN::create(const std::function<void(Node*)> &func)
  360. {
  361. auto ret = new (std::nothrow) CallFuncN();
  362. if (ret && ret->initWithFunction(func) )
  363. {
  364. ret->autorelease();
  365. return ret;
  366. }
  367. CC_SAFE_DELETE(ret);
  368. return nullptr;
  369. }
  370. // FIXME: deprecated
  371. CallFuncN * CallFuncN::create(Ref* selectorTarget, SEL_CallFuncN selector)
  372. {
  373. CallFuncN *ret = new (std::nothrow) CallFuncN();
  374. if (ret && ret->initWithTarget(selectorTarget, selector))
  375. {
  376. ret->autorelease();
  377. return ret;
  378. }
  379. CC_SAFE_DELETE(ret);
  380. return nullptr;
  381. }
  382. void CallFuncN::execute()
  383. {
  384. if (_callFuncN)
  385. {
  386. (_selectorTarget->*_callFuncN)(_target);
  387. }
  388. else if (_functionN)
  389. {
  390. _functionN(_target);
  391. }
  392. }
  393. bool CallFuncN::initWithFunction(const std::function<void (Node *)> &func)
  394. {
  395. _functionN = func;
  396. return true;
  397. }
  398. bool CallFuncN::initWithTarget(Ref* selectorTarget, SEL_CallFuncN selector)
  399. {
  400. if (CallFunc::initWithTarget(selectorTarget))
  401. {
  402. _callFuncN = selector;
  403. return true;
  404. }
  405. return false;
  406. }
  407. CallFuncN * CallFuncN::clone() const
  408. {
  409. // no copy constructor
  410. auto a = new (std::nothrow) CallFuncN();
  411. if( _selectorTarget)
  412. {
  413. a->initWithTarget(_selectorTarget, _callFuncN);
  414. }
  415. else if( _functionN ){
  416. a->initWithFunction(_functionN);
  417. }
  418. a->autorelease();
  419. return a;
  420. }
  421. //
  422. // CallFuncND
  423. //
  424. __CCCallFuncND * __CCCallFuncND::create(Ref* selectorTarget, SEL_CallFuncND selector, void* d)
  425. {
  426. __CCCallFuncND* ret = new (std::nothrow) __CCCallFuncND();
  427. if (ret && ret->initWithTarget(selectorTarget, selector, d))
  428. {
  429. ret->autorelease();
  430. return ret;
  431. }
  432. CC_SAFE_DELETE(ret);
  433. return nullptr;
  434. }
  435. bool __CCCallFuncND::initWithTarget(Ref* selectorTarget, SEL_CallFuncND selector, void* d)
  436. {
  437. if (CallFunc::initWithTarget(selectorTarget))
  438. {
  439. _data = d;
  440. _callFuncND = selector;
  441. return true;
  442. }
  443. return false;
  444. }
  445. void __CCCallFuncND::execute()
  446. {
  447. if (_callFuncND)
  448. {
  449. (_selectorTarget->*_callFuncND)(_target, _data);
  450. }
  451. }
  452. __CCCallFuncND * __CCCallFuncND::clone() const
  453. {
  454. // no copy constructor
  455. auto a = new (std::nothrow) __CCCallFuncND();
  456. if( _selectorTarget)
  457. {
  458. a->initWithTarget(_selectorTarget, _callFuncND, _data);
  459. }
  460. a->autorelease();
  461. return a;
  462. }
  463. //
  464. // CallFuncO
  465. //
  466. __CCCallFuncO::__CCCallFuncO() :
  467. _object(nullptr)
  468. {
  469. }
  470. __CCCallFuncO::~__CCCallFuncO()
  471. {
  472. CC_SAFE_RELEASE(_object);
  473. }
  474. void __CCCallFuncO::execute()
  475. {
  476. if (_callFuncO)
  477. {
  478. (_selectorTarget->*_callFuncO)(_object);
  479. }
  480. }
  481. __CCCallFuncO * __CCCallFuncO::create(Ref* selectorTarget, SEL_CallFuncO selector, Ref* object)
  482. {
  483. __CCCallFuncO *ret = new (std::nothrow) __CCCallFuncO();
  484. if (ret && ret->initWithTarget(selectorTarget, selector, object))
  485. {
  486. ret->autorelease();
  487. return ret;
  488. }
  489. CC_SAFE_DELETE(ret);
  490. return nullptr;
  491. }
  492. bool __CCCallFuncO::initWithTarget(Ref* selectorTarget, SEL_CallFuncO selector, Ref* object)
  493. {
  494. if (CallFunc::initWithTarget(selectorTarget))
  495. {
  496. _object = object;
  497. CC_SAFE_RETAIN(_object);
  498. _callFuncO = selector;
  499. return true;
  500. }
  501. return false;
  502. }
  503. __CCCallFuncO * __CCCallFuncO::clone() const
  504. {
  505. // no copy constructor
  506. auto a = new (std::nothrow) __CCCallFuncO();
  507. if( _selectorTarget)
  508. {
  509. a->initWithTarget(_selectorTarget, _callFuncO, _object);
  510. }
  511. a->autorelease();
  512. return a;
  513. }
  514. Ref* __CCCallFuncO::getObject() const
  515. {
  516. return _object;
  517. }
  518. void __CCCallFuncO::setObject(Ref* obj)
  519. {
  520. if (obj != _object)
  521. {
  522. CC_SAFE_RELEASE(_object);
  523. _object = obj;
  524. CC_SAFE_RETAIN(_object);
  525. }
  526. }
  527. NS_CC_END
  528. #if defined(__GNUC__) && ((__GNUC__ >= 4) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1)))
  529. #pragma GCC diagnostic warning "-Wdeprecated-declarations"
  530. #elif _MSC_VER >= 1400 //vs 2005 or higher
  531. #pragma warning (pop)
  532. #endif