CCActionInstant.cpp 11 KB

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