CCActionInstant.h 13 KB

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