CCActionInstant.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  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. #ifndef __CCINSTANT_ACTION_H__
  25. #define __CCINSTANT_ACTION_H__
  26. #include <functional>
  27. #include "2d/CCAction.h"
  28. NS_CC_BEGIN
  29. /**
  30. * @addtogroup actions
  31. * @{
  32. */
  33. /** @class ActionInstant
  34. * @brief Instant actions are immediate actions. They don't have a duration like the IntervalAction actions.
  35. **/
  36. class CC_DLL ActionInstant : public FiniteTimeAction
  37. {
  38. public:
  39. //
  40. // Overrides
  41. //
  42. virtual ActionInstant* clone() const override
  43. {
  44. CC_ASSERT(0);
  45. return nullptr;
  46. }
  47. virtual ActionInstant * reverse() const override
  48. {
  49. CC_ASSERT(0);
  50. return nullptr;
  51. }
  52. virtual void startWithTarget(Node *target) override;
  53. virtual bool isDone() const override;
  54. /**
  55. * @param dt In seconds.
  56. */
  57. virtual void step(float dt) override;
  58. /**
  59. * @param time In seconds.
  60. */
  61. virtual void update(float time) override;
  62. private:
  63. bool _done;
  64. };
  65. /** @class Show
  66. * @brief Show the node.
  67. **/
  68. class CC_DLL Show : public ActionInstant
  69. {
  70. public:
  71. /** Allocates and initializes the action.
  72. *
  73. * @return An autoreleased Show object.
  74. */
  75. static Show * create();
  76. //
  77. // Overrides
  78. //
  79. /**
  80. * @param time In seconds.
  81. */
  82. virtual void update(float time) override;
  83. virtual ActionInstant* reverse() const override;
  84. virtual Show* clone() const override;
  85. CC_CONSTRUCTOR_ACCESS:
  86. Show(){}
  87. virtual ~Show(){}
  88. private:
  89. CC_DISALLOW_COPY_AND_ASSIGN(Show);
  90. };
  91. /** @class Hide
  92. * @brief Hide the node.
  93. */
  94. class CC_DLL Hide : public ActionInstant
  95. {
  96. public:
  97. /** Allocates and initializes the action.
  98. *
  99. * @return An autoreleased Hide object.
  100. */
  101. static Hide * create();
  102. //
  103. // Overrides
  104. //
  105. /**
  106. * @param time In seconds.
  107. */
  108. virtual void update(float time) override;
  109. virtual ActionInstant* reverse() const override;
  110. virtual Hide* clone() const override;
  111. CC_CONSTRUCTOR_ACCESS:
  112. Hide(){}
  113. virtual ~Hide(){}
  114. private:
  115. CC_DISALLOW_COPY_AND_ASSIGN(Hide);
  116. };
  117. /** @class ToggleVisibility
  118. * @brief Toggles the visibility of a node.
  119. */
  120. class CC_DLL ToggleVisibility : public ActionInstant
  121. {
  122. public:
  123. /** Allocates and initializes the action.
  124. *
  125. * @return An autoreleased ToggleVisibility object.
  126. */
  127. static ToggleVisibility * create();
  128. //
  129. // Overrides
  130. //
  131. /**
  132. * @param time In seconds.
  133. */
  134. virtual void update(float time) override;
  135. virtual ToggleVisibility* reverse() const override;
  136. virtual ToggleVisibility* clone() const override;
  137. CC_CONSTRUCTOR_ACCESS:
  138. ToggleVisibility(){}
  139. virtual ~ToggleVisibility(){}
  140. private:
  141. CC_DISALLOW_COPY_AND_ASSIGN(ToggleVisibility);
  142. };
  143. /** @class RemoveSelf
  144. * @brief Remove the node.
  145. */
  146. class CC_DLL RemoveSelf : public ActionInstant
  147. {
  148. public:
  149. /** Create the action.
  150. *
  151. * @param isNeedCleanUp Is need to clean up, the default value is true.
  152. * @return An autoreleased RemoveSelf object.
  153. */
  154. static RemoveSelf * create(bool isNeedCleanUp = true);
  155. //
  156. // Override
  157. //
  158. /**
  159. * @param time In seconds.
  160. */
  161. virtual void update(float time) override;
  162. virtual RemoveSelf* clone() const override;
  163. virtual RemoveSelf* reverse() const override;
  164. CC_CONSTRUCTOR_ACCESS:
  165. RemoveSelf() : _isNeedCleanUp(true){}
  166. virtual ~RemoveSelf(){}
  167. /** init the action */
  168. bool init(bool isNeedCleanUp);
  169. protected:
  170. bool _isNeedCleanUp;
  171. private:
  172. CC_DISALLOW_COPY_AND_ASSIGN(RemoveSelf);
  173. };
  174. /** @class FlipX
  175. * @brief Flips the sprite horizontally.
  176. * @since v0.99.0
  177. */
  178. class CC_DLL FlipX : public ActionInstant
  179. {
  180. public:
  181. /** Create the action.
  182. *
  183. * @param x Flips the sprite horizontally if true.
  184. * @return An autoreleased FlipX object.
  185. */
  186. static FlipX * create(bool x);
  187. //
  188. // Overrides
  189. //
  190. /**
  191. * @param time In seconds.
  192. */
  193. virtual void update(float time) override;
  194. virtual FlipX* reverse() const override;
  195. virtual FlipX* clone() const override;
  196. CC_CONSTRUCTOR_ACCESS:
  197. FlipX() :_flipX(false) {}
  198. virtual ~FlipX() {}
  199. /** init the action */
  200. bool initWithFlipX(bool x);
  201. protected:
  202. bool _flipX;
  203. private:
  204. CC_DISALLOW_COPY_AND_ASSIGN(FlipX);
  205. };
  206. /** @class FlipY
  207. * @brief Flips the sprite vertically.
  208. * @since v0.99.0
  209. */
  210. class CC_DLL FlipY : public ActionInstant
  211. {
  212. public:
  213. /** Create the action.
  214. *
  215. * @param y Flips the sprite vertically if true.
  216. * @return An autoreleased FlipY object.
  217. */
  218. static FlipY * create(bool y);
  219. //
  220. // Overrides
  221. //
  222. /**
  223. * @param time In seconds.
  224. */
  225. virtual void update(float time) override;
  226. virtual FlipY* reverse() const override;
  227. virtual FlipY* clone() const override;
  228. CC_CONSTRUCTOR_ACCESS:
  229. FlipY() :_flipY(false) {}
  230. virtual ~FlipY() {}
  231. /** init the action */
  232. bool initWithFlipY(bool y);
  233. protected:
  234. bool _flipY;
  235. private:
  236. CC_DISALLOW_COPY_AND_ASSIGN(FlipY);
  237. };
  238. /** @class Place
  239. * @brief Places the node in a certain position.
  240. */
  241. class CC_DLL Place : public ActionInstant
  242. {
  243. public:
  244. /** Creates a Place action with a position.
  245. *
  246. * @param pos A certain position.
  247. * @return An autoreleased Place object.
  248. */
  249. static Place * create(const Vec2& pos);
  250. //
  251. // Overrides
  252. //
  253. /**
  254. * @param time In seconds.
  255. */
  256. virtual void update(float time) override;
  257. virtual Place* reverse() const override;
  258. virtual Place* clone() const override;
  259. CC_CONSTRUCTOR_ACCESS:
  260. Place(){}
  261. virtual ~Place(){}
  262. /** Initializes a Place action with a position */
  263. bool initWithPosition(const Vec2& pos);
  264. protected:
  265. Vec2 _position;
  266. private:
  267. CC_DISALLOW_COPY_AND_ASSIGN(Place);
  268. };
  269. /** @class CallFunc
  270. * @brief Calls a 'callback'.
  271. */
  272. class CC_DLL CallFunc : public ActionInstant
  273. {
  274. public:
  275. /** Creates the action with the callback of type std::function<void()>.
  276. This is the preferred way to create the callback.
  277. * When this function bound in js or lua ,the input param will be changed.
  278. * In js: var create(var func, var this, var [data]) or var create(var func).
  279. * In lua:local create(local funcID).
  280. *
  281. * @param func A callback function need to be executed.
  282. * @return An autoreleased CallFunc object.
  283. */
  284. static CallFunc * create(const std::function<void()>& func);
  285. /** Creates the action with the callback
  286. typedef void (Ref::*SEL_CallFunc)();
  287. @deprecated Use the std::function API instead.
  288. * @js NA
  289. * @lua NA
  290. */
  291. CC_DEPRECATED_ATTRIBUTE static CallFunc * create(Ref* target, SEL_CallFunc selector);
  292. public:
  293. /** Executes the callback.
  294. */
  295. virtual void execute();
  296. /** Get the selector target.
  297. *
  298. * @return The selector target.
  299. */
  300. Ref* getTargetCallback()
  301. {
  302. return _selectorTarget;
  303. }
  304. /** Set the selector target.
  305. *
  306. * @param sel The selector target.
  307. */
  308. void setTargetCallback(Ref* sel)
  309. {
  310. if (sel != _selectorTarget)
  311. {
  312. CC_SAFE_RETAIN(sel);
  313. CC_SAFE_RELEASE(_selectorTarget);
  314. _selectorTarget = sel;
  315. }
  316. }
  317. //
  318. // Overrides
  319. //
  320. /**
  321. * @param time In seconds.
  322. */
  323. virtual void update(float time) override;
  324. virtual CallFunc* reverse() const override;
  325. virtual CallFunc* clone() const override;
  326. CC_CONSTRUCTOR_ACCESS:
  327. CallFunc()
  328. : _selectorTarget(nullptr)
  329. , _callFunc(nullptr)
  330. , _function(nullptr)
  331. {
  332. }
  333. virtual ~CallFunc();
  334. /** initializes the action with the callback
  335. typedef void (Ref::*SEL_CallFunc)();
  336. @deprecated Use the std::function API instead.
  337. */
  338. CC_DEPRECATED_ATTRIBUTE bool initWithTarget(Ref* target);
  339. /** initializes the action with the std::function<void()>
  340. * @lua NA
  341. */
  342. bool initWithFunction(const std::function<void()>& func);
  343. protected:
  344. /** Target that will be called */
  345. Ref* _selectorTarget;
  346. union
  347. {
  348. SEL_CallFunc _callFunc;
  349. SEL_CallFuncN _callFuncN;
  350. };
  351. /** function that will be called */
  352. std::function<void()> _function;
  353. private:
  354. CC_DISALLOW_COPY_AND_ASSIGN(CallFunc);
  355. };
  356. /** @class CallFuncN
  357. * @brief Calls a 'callback' with the node as the first argument. N means Node.
  358. * @js NA
  359. */
  360. class CC_DLL CallFuncN : public CallFunc
  361. {
  362. public:
  363. /** Creates the action with the callback of type std::function<void()>.
  364. This is the preferred way to create the callback.
  365. *
  366. * @param func A callback function need to be executed.
  367. * @return An autoreleased CallFuncN object.
  368. */
  369. static CallFuncN * create(const std::function<void(Node*)>& func);
  370. /** Creates the action with the callback.
  371. typedef void (Ref::*SEL_CallFuncN)(Node*);
  372. @deprecated Use the std::function API instead.
  373. */
  374. CC_DEPRECATED_ATTRIBUTE static CallFuncN * create(Ref* target, SEL_CallFuncN selector);
  375. //
  376. // Overrides
  377. //
  378. virtual CallFuncN* clone() const override;
  379. virtual void execute() override;
  380. CC_CONSTRUCTOR_ACCESS:
  381. CallFuncN():_functionN(nullptr){}
  382. virtual ~CallFuncN(){}
  383. /** initializes the action with the std::function<void(Node*)> */
  384. bool initWithFunction(const std::function<void(Node*)>& func);
  385. /** initializes the action with the callback
  386. typedef void (Ref::*SEL_CallFuncN)(Node*);
  387. @deprecated Use the std::function API instead.
  388. */
  389. CC_DEPRECATED_ATTRIBUTE bool initWithTarget(Ref* target, SEL_CallFuncN selector);
  390. protected:
  391. /** function that will be called with the "sender" as the 1st argument */
  392. std::function<void(Node*)> _functionN;
  393. private:
  394. CC_DISALLOW_COPY_AND_ASSIGN(CallFuncN);
  395. };
  396. /** @class __CCCallFuncND
  397. * @deprecated Please use CallFuncN instead.
  398. * @brief Calls a 'callback' with the node as the first argument and the 2nd argument is data.
  399. * ND means: Node and Data. Data is void *, so it could be anything.
  400. * @js NA
  401. */
  402. class CC_DLL __CCCallFuncND : public CallFunc
  403. {
  404. public:
  405. /** Creates the action with the callback and the data to pass as an argument.
  406. *
  407. * @param target A certain target.
  408. * @param selector The callback need to be executed.
  409. * @param d Data, is void* type.
  410. * @return An autoreleased __CCCallFuncND object.
  411. */
  412. CC_DEPRECATED_ATTRIBUTE static __CCCallFuncND * create(Ref* target, SEL_CallFuncND selector, void* d);
  413. //
  414. // Overrides
  415. //
  416. virtual __CCCallFuncND* clone() const override;
  417. virtual void execute() override;
  418. CC_CONSTRUCTOR_ACCESS:
  419. __CCCallFuncND() {}
  420. virtual ~__CCCallFuncND() {}
  421. /** initializes the action with the callback and the data to pass as an argument */
  422. bool initWithTarget(Ref* target, SEL_CallFuncND selector, void* d);
  423. protected:
  424. SEL_CallFuncND _callFuncND;
  425. void* _data;
  426. private:
  427. CC_DISALLOW_COPY_AND_ASSIGN(__CCCallFuncND);
  428. };
  429. /** @class __CCCallFuncO
  430. @deprecated Please use CallFuncN instead.
  431. @brief Calls a 'callback' with an object as the first argument. O means Object.
  432. @since v0.99.5
  433. @js NA
  434. */
  435. class CC_DLL __CCCallFuncO : public CallFunc
  436. {
  437. public:
  438. /** Creates the action with the callback.
  439. typedef void (Ref::*SEL_CallFuncO)(Ref*);
  440. *
  441. * @param target A certain target.
  442. * @param selector The callback need to be executed.
  443. * @param object An object as the callback's first argument.
  444. * @return An autoreleased __CCCallFuncO object.
  445. */
  446. CC_DEPRECATED_ATTRIBUTE static __CCCallFuncO * create(Ref* target, SEL_CallFuncO selector, Ref* object);
  447. //
  448. // Overrides
  449. //
  450. virtual __CCCallFuncO* clone() const override;
  451. virtual void execute() override;
  452. Ref* getObject() const;
  453. void setObject(Ref* obj);
  454. CC_CONSTRUCTOR_ACCESS:
  455. __CCCallFuncO();
  456. virtual ~__CCCallFuncO();
  457. /** initializes the action with the callback
  458. typedef void (Ref::*SEL_CallFuncO)(Ref*);
  459. */
  460. bool initWithTarget(Ref* target, SEL_CallFuncO selector, Ref* object);
  461. protected:
  462. /** object to be passed as argument */
  463. Ref* _object;
  464. SEL_CallFuncO _callFuncO;
  465. private:
  466. CC_DISALLOW_COPY_AND_ASSIGN(__CCCallFuncO);
  467. };
  468. // end of actions group
  469. /// @}
  470. NS_CC_END
  471. #endif //__CCINSTANT_ACTION_H__