CCActionInstant.h 13 KB

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