CCActionInstant.cpp 12 KB

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