CCNode.h 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107
  1. /****************************************************************************
  2. Copyright (c) 2008-2010 Ricardo Quesada
  3. Copyright (c) 2009 Valentin Milea
  4. Copyright (c) 2010-2012 cocos2d-x.org
  5. Copyright (c) 2011 Zynga Inc.
  6. Copyright (c) 2013-2016 Chukong Technologies Inc.
  7. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  8. http://www.cocos2d-x.org
  9. Permission is hereby granted, free of charge, to any person obtaining a copy
  10. of this software and associated documentation files (the "Software"), to deal
  11. in the Software without restriction, including without limitation the rights
  12. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. copies of the Software, and to permit persons to whom the Software is
  14. furnished to do so, subject to the following conditions:
  15. The above copyright notice and this permission notice shall be included in
  16. all copies or substantial portions of the Software.
  17. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. THE SOFTWARE.
  24. ****************************************************************************/
  25. #ifndef __CCNODE_H__
  26. #define __CCNODE_H__
  27. #include <cstdint>
  28. #include "base/ccMacros.h"
  29. #include "base/CCVector.h"
  30. #include "base/CCProtocols.h"
  31. #include "base/CCScriptSupport.h"
  32. #include "math/CCAffineTransform.h"
  33. #include "math/CCMath.h"
  34. #include "2d/CCComponentContainer.h"
  35. #include "2d/CCComponent.h"
  36. #if CC_USE_PHYSICS
  37. #include "physics/CCPhysicsBody.h"
  38. #endif
  39. NS_CC_BEGIN
  40. class GridBase;
  41. class Touch;
  42. class Action;
  43. class LabelProtocol;
  44. class Scheduler;
  45. class ActionManager;
  46. class Component;
  47. class ComponentContainer;
  48. class EventDispatcher;
  49. class Scene;
  50. class Renderer;
  51. class Director;
  52. class GLProgram;
  53. class GLProgramState;
  54. class Material;
  55. class Camera;
  56. class PhysicsBody;
  57. /**
  58. * @addtogroup _2d
  59. * @{
  60. */
  61. enum {
  62. kNodeOnEnter,
  63. kNodeOnExit,
  64. kNodeOnEnterTransitionDidFinish,
  65. kNodeOnExitTransitionDidStart,
  66. kNodeOnCleanup
  67. };
  68. class EventListener;
  69. /** @class Node
  70. * @brief Node is the base element of the Scene Graph. Elements of the Scene Graph must be Node objects or subclasses of it.
  71. The most common Node objects are: Scene, Layer, Sprite, Menu, Label.
  72. The main features of a Node are:
  73. - They can contain other Node objects (`addChild`, `getChildByTag`, `removeChild`, etc)
  74. - They can schedule periodic callback (`schedule`, `unschedule`, etc)
  75. - They can execute actions (`runAction`, `stopAction`, etc)
  76. Subclassing a Node usually means (one/all) of:
  77. - overriding init to initialize resources and schedule callbacks
  78. - create callbacks to handle the advancement of time
  79. - overriding `draw` to render the node
  80. Properties of Node:
  81. - position (default: x=0, y=0)
  82. - scale (default: x=1, y=1)
  83. - rotation (in degrees, clockwise) (default: 0)
  84. - anchor point (default: x=0, y=0)
  85. - contentSize (default: width=0, height=0)
  86. - visible (default: true)
  87. Limitations:
  88. - A Node is a "void" object. If you want to draw something on the screen, you should use a Sprite instead. Or subclass Node and override `draw`.
  89. */
  90. class CC_DLL Node : public Ref
  91. {
  92. public:
  93. /** Default tag used for all the nodes */
  94. static const int INVALID_TAG = -1;
  95. enum {
  96. FLAGS_TRANSFORM_DIRTY = (1 << 0),
  97. FLAGS_CONTENT_SIZE_DIRTY = (1 << 1),
  98. FLAGS_RENDER_AS_3D = (1 << 3),
  99. FLAGS_DIRTY_MASK = (FLAGS_TRANSFORM_DIRTY | FLAGS_CONTENT_SIZE_DIRTY),
  100. };
  101. /// @{
  102. /// @name Constructor, Destructor and Initializers
  103. /**
  104. * Allocates and initializes a node.
  105. * @return A initialized node which is marked as "autorelease".
  106. */
  107. static Node * create();
  108. /**
  109. * Gets count of nodes those are attached to scene graph.
  110. */
  111. static int getAttachedNodeCount();
  112. public:
  113. /**
  114. * Gets the description string. It makes debugging easier.
  115. * @return A string
  116. * @js NA
  117. * @lua NA
  118. */
  119. virtual std::string getDescription() const;
  120. /// @} end of initializers
  121. /// @{
  122. /// @name Setters & Getters for Graphic Properties
  123. /**
  124. LocalZOrder is the 'key' used to sort the node relative to its siblings.
  125. The Node's parent will sort all its children based on the LocalZOrder value.
  126. If two nodes have the same LocalZOrder, then the node that was added first to the children's array will be in front of the other node in the array.
  127. Also, the Scene Graph is traversed using the "In-Order" tree traversal algorithm ( http://en.wikipedia.org/wiki/Tree_traversal#In-order )
  128. And Nodes that have LocalZOrder values < 0 are the "left" subtree
  129. While Nodes with LocalZOrder >=0 are the "right" subtree.
  130. @see `setGlobalZOrder`
  131. @see `setVertexZ`
  132. *
  133. * @param localZOrder The local Z order value.
  134. */
  135. virtual void setLocalZOrder(std::int32_t localZOrder);
  136. CC_DEPRECATED_ATTRIBUTE virtual void setZOrder(std::int32_t localZOrder) { setLocalZOrder(localZOrder); }
  137. /*
  138. Helper function used by `setLocalZOrder`. Don't use it unless you know what you are doing.
  139. @js NA
  140. */
  141. virtual void _setLocalZOrder(std::int32_t z);
  142. /** !!! ONLY FOR INTERNAL USE
  143. * Sets the arrival order when this node has a same ZOrder with other children.
  144. *
  145. * A node which called addChild subsequently will take a larger arrival order,
  146. * If two children have the same Z order, the child with larger arrival order will be drawn later.
  147. *
  148. * @warning This method is used internally for localZOrder sorting, don't change this manually
  149. *
  150. * @param orderOfArrival The arrival order.
  151. */
  152. void updateOrderOfArrival();
  153. /**
  154. * Gets the local Z order of this node.
  155. *
  156. * @see `setLocalZOrder(int)`
  157. *
  158. * @return The local (relative to its siblings) Z order.
  159. */
  160. virtual std::int32_t getLocalZOrder() const { return _localZOrder; }
  161. CC_DEPRECATED_ATTRIBUTE virtual std::int32_t getZOrder() const { return getLocalZOrder(); }
  162. /**
  163. Defines the order in which the nodes are renderer.
  164. Nodes that have a Global Z Order lower, are renderer first.
  165. In case two or more nodes have the same Global Z Order, the order is not guaranteed.
  166. The only exception if the Nodes have a Global Z Order == 0. In that case, the Scene Graph order is used.
  167. By default, all nodes have a Global Z Order = 0. That means that by default, the Scene Graph order is used to render the nodes.
  168. Global Z Order is useful when you need to render nodes in an order different than the Scene Graph order.
  169. Limitations: Global Z Order can't be used by Nodes that have SpriteBatchNode as one of their ancestors.
  170. And if ClippingNode is one of the ancestors, then "global Z order" will be relative to the ClippingNode.
  171. @see `setLocalZOrder()`
  172. @see `setVertexZ()`
  173. @since v3.0
  174. *
  175. * @param globalZOrder The global Z order value.
  176. */
  177. virtual void setGlobalZOrder(float globalZOrder);
  178. /**
  179. * Returns the Node's Global Z Order.
  180. *
  181. * @see `setGlobalZOrder(int)`
  182. *
  183. * @return The node's global Z order
  184. */
  185. virtual float getGlobalZOrder() const { return _globalZOrder; }
  186. /**
  187. * Sets the scale (x) of the node.
  188. *
  189. * It is a scaling factor that multiplies the width of the node and its children.
  190. *
  191. * @param scaleX The scale factor on X axis.
  192. *
  193. * @warning The physics body doesn't support this.
  194. */
  195. virtual void setScaleX(float scaleX);
  196. /**
  197. * Returns the scale factor on X axis of this node
  198. *
  199. * @see setScaleX(float)
  200. *
  201. * @return The scale factor on X axis.
  202. */
  203. virtual float getScaleX() const;
  204. /**
  205. * Sets the scale (y) of the node.
  206. *
  207. * It is a scaling factor that multiplies the height of the node and its children.
  208. *
  209. * @param scaleY The scale factor on Y axis.
  210. *
  211. * @warning The physics body doesn't support this.
  212. */
  213. virtual void setScaleY(float scaleY);
  214. /**
  215. * Returns the scale factor on Y axis of this node
  216. *
  217. * @see `setScaleY(float)`
  218. *
  219. * @return The scale factor on Y axis.
  220. */
  221. virtual float getScaleY() const;
  222. /**
  223. * Changes the scale factor on Z axis of this node
  224. *
  225. * The Default value is 1.0 if you haven't changed it before.
  226. *
  227. * @param scaleZ The scale factor on Z axis.
  228. *
  229. * @warning The physics body doesn't support this.
  230. */
  231. virtual void setScaleZ(float scaleZ);
  232. /**
  233. * Returns the scale factor on Z axis of this node
  234. *
  235. * @see `setScaleZ(float)`
  236. *
  237. * @return The scale factor on Z axis.
  238. */
  239. virtual float getScaleZ() const;
  240. /**
  241. * Sets the scale (x,y,z) of the node.
  242. *
  243. * It is a scaling factor that multiplies the width, height and depth of the node and its children.
  244. *
  245. * @param scale The scale factor for both X and Y axis.
  246. *
  247. * @warning The physics body doesn't support this.
  248. */
  249. virtual void setScale(float scale);
  250. /**
  251. * Gets the scale factor of the node, when X and Y have the same scale factor.
  252. *
  253. * @warning Assert when `_scaleX != _scaleY`
  254. * @see setScale(float)
  255. *
  256. * @return The scale factor of the node.
  257. */
  258. virtual float getScale() const;
  259. /**
  260. * Sets the scale (x,y) of the node.
  261. *
  262. * It is a scaling factor that multiplies the width and height of the node and its children.
  263. *
  264. * @param scaleX The scale factor on X axis.
  265. * @param scaleY The scale factor on Y axis.
  266. *
  267. * @warning The physics body doesn't support this.
  268. */
  269. virtual void setScale(float scaleX, float scaleY);
  270. /**
  271. * Sets the position (x,y) of the node in its parent's coordinate system.
  272. *
  273. * Usually we use `Vec2(x,y)` to compose Vec2 object.
  274. * This code snippet sets the node in the center of screen.
  275. @code
  276. Size size = Director::getInstance()->getWinSize();
  277. node->setPosition(size.width/2, size.height/2);
  278. @endcode
  279. *
  280. * @param position The position (x,y) of the node in OpenGL coordinates.
  281. */
  282. virtual void setPosition(const Vec2 &position);
  283. /** Sets the position (x,y) using values between 0 and 1.
  284. The positions in pixels is calculated like the following:
  285. @code
  286. // pseudo code
  287. void setNormalizedPosition(Vec2 pos) {
  288. Size s = getParent()->getContentSize();
  289. _position = pos * s;
  290. }
  291. @endcode
  292. *
  293. * @param position The normalized position (x,y) of the node, using value between 0 and 1.
  294. */
  295. virtual void setPositionNormalized(const Vec2 &position);
  296. // FIXME: should get deprecated in v4.0
  297. virtual void setNormalizedPosition(const Vec2 &position) { setPositionNormalized(position); }
  298. /**
  299. * Gets the position (x,y) of the node in its parent's coordinate system.
  300. *
  301. * @see setPosition(const Vec2&)
  302. *
  303. * @return The position (x,y) of the node in OpenGL coordinates.
  304. * @code
  305. * In js and lua return value is table which contains x,y.
  306. * @endcode
  307. */
  308. virtual const Vec2& getPosition() const;
  309. /** Returns the normalized position.
  310. *
  311. * @return The normalized position.
  312. */
  313. virtual const Vec2& getPositionNormalized() const;
  314. // FIXME: should get deprecated in v4.0
  315. virtual const Vec2& getNormalizedPosition() const { return getPositionNormalized(); }
  316. /**
  317. * Sets the position (x,y) of the node in its parent's coordinate system.
  318. *
  319. * Passing two numbers (x,y) is much efficient than passing Vec2 object.
  320. * This method is bound to Lua and JavaScript.
  321. * Passing a number is 10 times faster than passing a object from Lua to c++.
  322. *
  323. @code
  324. // sample code in Lua
  325. local pos = node::getPosition() -- returns Vec2 object from C++.
  326. node:setPosition(x, y) -- pass x, y coordinate to C++.
  327. @endcode
  328. *
  329. * @param x X coordinate for position.
  330. * @param y Y coordinate for position.
  331. */
  332. virtual void setPosition(float x, float y);
  333. /**
  334. * Gets position in a more efficient way, returns two number instead of a Vec2 object.
  335. *
  336. * @see `setPosition(float, float)`
  337. * In js,out value not return.
  338. *
  339. * @param x To receive x coordinate for position.
  340. * @param y To receive y coordinate for position.
  341. */
  342. virtual void getPosition(float* x, float* y) const;
  343. /**
  344. * Gets/Sets x or y coordinate individually for position.
  345. * These methods are used in Lua and Javascript Bindings
  346. */
  347. /** Sets the x coordinate of the node in its parent's coordinate system.
  348. *
  349. * @param x The x coordinate of the node.
  350. */
  351. virtual void setPositionX(float x);
  352. /** Gets the x coordinate of the node in its parent's coordinate system.
  353. *
  354. * @return The x coordinate of the node.
  355. */
  356. virtual float getPositionX(void) const;
  357. /** Sets the y coordinate of the node in its parent's coordinate system.
  358. *
  359. * @param y The y coordinate of the node.
  360. */
  361. virtual void setPositionY(float y);
  362. /** Gets the y coordinate of the node in its parent's coordinate system.
  363. *
  364. * @return The y coordinate of the node.
  365. */
  366. virtual float getPositionY(void) const;
  367. /**
  368. * Sets the position (X, Y, and Z) in its parent's coordinate system.
  369. *
  370. * @param position The position (X, Y, and Z) in its parent's coordinate system.
  371. * @js NA
  372. */
  373. virtual void setPosition3D(const Vec3& position);
  374. /**
  375. * Returns the position (X,Y,Z) in its parent's coordinate system.
  376. *
  377. * @return The position (X, Y, and Z) in its parent's coordinate system.
  378. * @js NA
  379. */
  380. virtual Vec3 getPosition3D() const;
  381. /**
  382. * Sets the 'z' coordinate in the position. It is the OpenGL Z vertex value.
  383. *
  384. * The OpenGL depth buffer and depth testing are disabled by default. You need to turn them on.
  385. * In order to use this property correctly.
  386. *
  387. * `setPositionZ()` also sets the `setGlobalZValue()` with the positionZ as value.
  388. *
  389. * @see `setGlobalZValue()`
  390. *
  391. * @param positionZ OpenGL Z vertex of this node.
  392. * @js setVertexZ
  393. */
  394. virtual void setPositionZ(float positionZ);
  395. CC_DEPRECATED_ATTRIBUTE virtual void setVertexZ(float vertexZ) { setPositionZ(vertexZ); }
  396. /**
  397. * Gets position Z coordinate of this node.
  398. *
  399. * @see setPositionZ(float)
  400. *
  401. * @return The position Z coordinate of this node.
  402. * @js getVertexZ
  403. */
  404. virtual float getPositionZ() const;
  405. CC_DEPRECATED_ATTRIBUTE virtual float getVertexZ() const { return getPositionZ(); }
  406. /**
  407. * Changes the X skew angle of the node in degrees.
  408. *
  409. * The difference between `setRotationalSkew()` and `setSkew()` is that the first one simulate Flash's skew functionality
  410. * while the second one uses the real skew function.
  411. *
  412. * This angle describes the shear distortion in the X direction.
  413. * Thus, it is the angle between the Y coordinate and the left edge of the shape
  414. * The default skewX angle is 0. Positive values distort the node in a CW direction.
  415. *
  416. * @param skewX The X skew angle of the node in degrees.
  417. *
  418. * @warning The physics body doesn't support this.
  419. */
  420. virtual void setSkewX(float skewX);
  421. /**
  422. * Returns the X skew angle of the node in degrees.
  423. *
  424. * @see `setSkewX(float)`
  425. *
  426. * @return The X skew angle of the node in degrees.
  427. */
  428. virtual float getSkewX() const;
  429. /**
  430. * Changes the Y skew angle of the node in degrees.
  431. *
  432. * The difference between `setRotationalSkew()` and `setSkew()` is that the first one simulate Flash's skew functionality
  433. * while the second one uses the real skew function.
  434. *
  435. * This angle describes the shear distortion in the Y direction.
  436. * Thus, it is the angle between the X coordinate and the bottom edge of the shape.
  437. * The default skewY angle is 0. Positive values distort the node in a CCW direction.
  438. *
  439. * @param skewY The Y skew angle of the node in degrees.
  440. *
  441. * @warning The physics body doesn't support this.
  442. */
  443. virtual void setSkewY(float skewY);
  444. /**
  445. * Returns the Y skew angle of the node in degrees.
  446. *
  447. * @see `setSkewY(float)`
  448. *
  449. * @return The Y skew angle of the node in degrees.
  450. */
  451. virtual float getSkewY() const;
  452. /**
  453. * Sets the anchor point in percent.
  454. *
  455. * anchorPoint is the point around which all transformations and positioning manipulations take place.
  456. * It's like a pin in the node where it is "attached" to its parent.
  457. * The anchorPoint is normalized, like a percentage. (0,0) means the bottom-left corner and (1,1) means the top-right corner.
  458. * But you can use values higher than (1,1) and lower than (0,0) too.
  459. * The default anchorPoint is (0,0), so it starts in the lower left corner of the node.
  460. * @note If node has a physics body, the anchor must be in the middle, you can't change this to other value.
  461. *
  462. * @param anchorPoint The anchor point of node.
  463. */
  464. virtual void setAnchorPoint(const Vec2& anchorPoint);
  465. /**
  466. * Returns the anchor point in percent.
  467. *
  468. * @see `setAnchorPoint(const Vec2&)`
  469. *
  470. * @return The anchor point of node.
  471. */
  472. virtual const Vec2& getAnchorPoint() const;
  473. /**
  474. * Returns the anchorPoint in absolute pixels.
  475. *
  476. * @warning You can only read it. If you wish to modify it, use anchorPoint instead.
  477. * @see `getAnchorPoint()`
  478. *
  479. * @return The anchor point in absolute pixels.
  480. */
  481. virtual const Vec2& getAnchorPointInPoints() const;
  482. /**
  483. * Sets the untransformed size of the node.
  484. *
  485. * The contentSize remains the same no matter the node is scaled or rotated.
  486. * All nodes has a size. Layer and Scene has the same size of the screen.
  487. *
  488. * @param contentSize The untransformed size of the node.
  489. */
  490. virtual void setContentSize(const Size& contentSize);
  491. /**
  492. * Returns the untransformed size of the node.
  493. *
  494. * @see `setContentSize(const Size&)`
  495. *
  496. * @return The untransformed size of the node.
  497. */
  498. virtual const Size& getContentSize() const;
  499. /**
  500. * Sets whether the node is visible.
  501. *
  502. * The default value is true, a node is default to visible.
  503. *
  504. * @param visible true if the node is visible, false if the node is hidden.
  505. */
  506. virtual void setVisible(bool visible);
  507. /**
  508. * Determines if the node is visible.
  509. *
  510. * @see `setVisible(bool)`
  511. *
  512. * @return true if the node is visible, false if the node is hidden.
  513. */
  514. virtual bool isVisible() const;
  515. /**
  516. * Sets the rotation (angle) of the node in degrees.
  517. *
  518. * 0 is the default rotation angle.
  519. * Positive values rotate node clockwise, and negative values for anti-clockwise.
  520. *
  521. * @param rotation The rotation of the node in degrees.
  522. */
  523. virtual void setRotation(float rotation);
  524. /**
  525. * Returns the rotation of the node in degrees.
  526. *
  527. * @see `setRotation(float)`
  528. *
  529. * @return The rotation of the node in degrees.
  530. */
  531. virtual float getRotation() const;
  532. /**
  533. * Sets the rotation (X,Y,Z) in degrees.
  534. * Useful for 3d rotations.
  535. *
  536. * @warning The physics body doesn't support this.
  537. *
  538. * @param rotation The rotation of the node in 3d.
  539. * @js NA
  540. */
  541. virtual void setRotation3D(const Vec3& rotation);
  542. /**
  543. * Returns the rotation (X,Y,Z) in degrees.
  544. *
  545. * @return The rotation of the node in 3d.
  546. * @js NA
  547. */
  548. virtual Vec3 getRotation3D() const;
  549. /**
  550. * Set rotation by quaternion. You should make sure the quaternion is normalized.
  551. *
  552. * @param quat The rotation in quaternion, note that the quat must be normalized.
  553. * @js NA
  554. */
  555. virtual void setRotationQuat(const Quaternion& quat);
  556. /**
  557. * Return the rotation by quaternion, Note that when _rotationZ_X == _rotationZ_Y, the returned quaternion equals to RotationZ_X * RotationY * RotationX,
  558. * it equals to RotationY * RotationX otherwise.
  559. *
  560. * @return The rotation in quaternion.
  561. * @js NA
  562. */
  563. virtual Quaternion getRotationQuat() const;
  564. /**
  565. * Sets the X rotation (angle) of the node in degrees which performs a horizontal rotational skew.
  566. *
  567. * The difference between `setRotationalSkew()` and `setSkew()` is that the first one simulate Flash's skew functionality,
  568. * while the second one uses the real skew function.
  569. *
  570. * 0 is the default rotation angle.
  571. * Positive values rotate node clockwise, and negative values for anti-clockwise.
  572. *
  573. * @param rotationX The X rotation in degrees which performs a horizontal rotational skew.
  574. *
  575. * @warning The physics body doesn't support this.
  576. * @js setRotationX
  577. */
  578. virtual void setRotationSkewX(float rotationX);
  579. CC_DEPRECATED_ATTRIBUTE virtual void setRotationX(float rotationX) { return setRotationSkewX(rotationX); }
  580. /**
  581. * Gets the X rotation (angle) of the node in degrees which performs a horizontal rotation skew.
  582. *
  583. * @see `setRotationSkewX(float)`
  584. *
  585. * @return The X rotation in degrees.
  586. * @js getRotationX
  587. */
  588. virtual float getRotationSkewX() const;
  589. CC_DEPRECATED_ATTRIBUTE virtual float getRotationX() const { return getRotationSkewX(); }
  590. /**
  591. * Sets the Y rotation (angle) of the node in degrees which performs a vertical rotational skew.
  592. *
  593. * The difference between `setRotationalSkew()` and `setSkew()` is that the first one simulate Flash's skew functionality,
  594. * while the second one uses the real skew function.
  595. *
  596. * 0 is the default rotation angle.
  597. * Positive values rotate node clockwise, and negative values for anti-clockwise.
  598. *
  599. * @param rotationY The Y rotation in degrees.
  600. *
  601. * @warning The physics body doesn't support this.
  602. * @js setRotationY
  603. */
  604. virtual void setRotationSkewY(float rotationY);
  605. CC_DEPRECATED_ATTRIBUTE virtual void setRotationY(float rotationY) { return setRotationSkewY(rotationY); }
  606. /**
  607. * Gets the Y rotation (angle) of the node in degrees which performs a vertical rotational skew.
  608. *
  609. * @see `setRotationSkewY(float)`
  610. *
  611. * @return The Y rotation in degrees.
  612. * @js getRotationY
  613. */
  614. virtual float getRotationSkewY() const;
  615. CC_DEPRECATED_ATTRIBUTE virtual float getRotationY() const { return getRotationSkewY(); }
  616. /** @deprecated No longer needed
  617. * @lua NA
  618. */
  619. CC_DEPRECATED_ATTRIBUTE void setGLServerState(int /*serverState*/) {}
  620. /** @deprecated No longer needed
  621. * @lua NA
  622. */
  623. CC_DEPRECATED_ATTRIBUTE int getGLServerState() const { return 0; }
  624. /**
  625. * Sets whether the anchor point will be (0,0) when you position this node.
  626. *
  627. * This is an internal method, only used by Layer and Scene. Don't call it outside framework.
  628. * The default value is false, while in Layer and Scene are true.
  629. *
  630. * @param ignore true if anchor point will be (0,0) when you position this node.
  631. */
  632. virtual void setIgnoreAnchorPointForPosition(bool ignore);
  633. CC_DEPRECATED_ATTRIBUTE virtual void ignoreAnchorPointForPosition(bool ignore) { setIgnoreAnchorPointForPosition(ignore); }
  634. /**
  635. * Gets whether the anchor point will be (0,0) when you position this node.
  636. *
  637. * @see `setIgnoreAnchorPointForPosition(bool)`
  638. *
  639. * @return true if the anchor point will be (0,0) when you position this node.
  640. */
  641. virtual bool isIgnoreAnchorPointForPosition() const;
  642. /// @} end of Setters & Getters for Graphic Properties
  643. /// @{
  644. /// @name Children and Parent
  645. /**
  646. * Adds a child to the container with z-order as 0.
  647. *
  648. * If the child is added to a 'running' node, then 'onEnter' and 'onEnterTransitionDidFinish' will be called immediately.
  649. *
  650. * @param child A child node.
  651. */
  652. virtual void addChild(Node * child);
  653. /**
  654. * Adds a child to the container with a local z-order.
  655. *
  656. * If the child is added to a 'running' node, then 'onEnter' and 'onEnterTransitionDidFinish' will be called immediately.
  657. *
  658. * @param child A child node.
  659. * @param localZOrder Z order for drawing priority. Please refer to `setLocalZOrder(int)`.
  660. */
  661. virtual void addChild(Node * child, int localZOrder);
  662. /**
  663. * Adds a child to the container with z order and tag.
  664. *
  665. * If the child is added to a 'running' node, then 'onEnter' and 'onEnterTransitionDidFinish' will be called immediately.
  666. *
  667. * @param child A child node.
  668. * @param localZOrder Z order for drawing priority. Please refer to `setLocalZOrder(int)`.
  669. * @param tag An integer to identify the node easily. Please refer to `setTag(int)`.
  670. *
  671. * Please use `addChild(Node* child, int localZOrder, const std::string &name)` instead.
  672. */
  673. virtual void addChild(Node* child, int localZOrder, int tag);
  674. /**
  675. * Adds a child to the container with z order and tag
  676. *
  677. * If the child is added to a 'running' node, then 'onEnter' and 'onEnterTransitionDidFinish' will be called immediately.
  678. *
  679. * @param child A child node.
  680. * @param localZOrder Z order for drawing priority. Please refer to `setLocalZOrder(int)`.
  681. * @param name A string to identify the node easily. Please refer to `setName(int)`.
  682. *
  683. */
  684. virtual void addChild(Node* child, int localZOrder, const std::string &name);
  685. /**
  686. * Gets a child from the container with its tag.
  687. *
  688. * @param tag An identifier to find the child node.
  689. *
  690. * @return a Node object whose tag equals to the input parameter.
  691. *
  692. * Please use `getChildByName()` instead.
  693. */
  694. virtual Node * getChildByTag(int tag) const;
  695. /**
  696. * Gets a child from the container with its tag that can be cast to Type T.
  697. *
  698. * @param tag An identifier to find the child node.
  699. *
  700. * @return a Node with the given tag that can be cast to Type T.
  701. */
  702. template <typename T>
  703. T getChildByTag(int tag) const { return static_cast<T>(getChildByTag(tag)); }
  704. /**
  705. * Gets a child from the container with its name.
  706. *
  707. * @param name An identifier to find the child node.
  708. *
  709. * @return a Node object whose name equals to the input parameter.
  710. *
  711. * @since v3.2
  712. */
  713. virtual Node* getChildByName(const std::string& name) const;
  714. /**
  715. * Gets a child from the container with its name that can be cast to Type T.
  716. *
  717. * @param name An identifier to find the child node.
  718. *
  719. * @return a Node with the given name that can be cast to Type T.
  720. */
  721. template <typename T>
  722. T getChildByName(const std::string& name) const { return static_cast<T>(getChildByName(name)); }
  723. /** Search the children of the receiving node to perform processing for nodes which share a name.
  724. *
  725. * @param name The name to search for, supports c++11 regular expression.
  726. * Search syntax options:
  727. * `//`: Can only be placed at the begin of the search string. This indicates that it will search recursively.
  728. * `..`: The search should move up to the node's parent. Can only be placed at the end of string.
  729. * `/` : When placed anywhere but the start of the search string, this indicates that the search should move to the node's children.
  730. *
  731. * @code
  732. * enumerateChildren("//MyName", ...): This searches the children recursively and matches any node with the name `MyName`.
  733. * enumerateChildren("[[:alnum:]]+", ...): This search string matches every node of its children.
  734. * enumerateChildren("A[[:digit:]]", ...): This searches the node's children and returns any child named `A0`, `A1`, ..., `A9`.
  735. * enumerateChildren("Abby/Normal", ...): This searches the node's grandchildren and returns any node whose name is `Normal`
  736. * and whose parent is named `Abby`.
  737. * enumerateChildren("//Abby/Normal", ...): This searches recursively and returns any node whose name is `Normal` and whose
  738. * parent is named `Abby`.
  739. * @endcode
  740. *
  741. * @warning Only support alpha or number for name, and not support unicode.
  742. *
  743. * @param callback A callback function to execute on nodes that match the `name` parameter. The function takes the following arguments:
  744. * `node`
  745. * A node that matches the name
  746. * And returns a boolean result. Your callback can return `true` to terminate the enumeration.
  747. *
  748. * @since v3.2
  749. */
  750. virtual void enumerateChildren(const std::string &name, std::function<bool(Node* node)> callback) const;
  751. /**
  752. * Returns the array of the node's children.
  753. *
  754. * @return the array the node's children.
  755. */
  756. virtual Vector<Node*>& getChildren() { return _children; }
  757. virtual const Vector<Node*>& getChildren() const { return _children; }
  758. /**
  759. * Returns the amount of children.
  760. *
  761. * @return The amount of children.
  762. */
  763. virtual ssize_t getChildrenCount() const;
  764. /**
  765. * Sets the parent node.
  766. *
  767. * @param parent A pointer to the parent node.
  768. */
  769. virtual void setParent(Node* parent);
  770. /**
  771. * Returns a pointer to the parent node.
  772. *
  773. * @see `setParent(Node*)`
  774. *
  775. * @returns A pointer to the parent node.
  776. */
  777. virtual Node* getParent() { return _parent; }
  778. virtual const Node* getParent() const { return _parent; }
  779. ////// REMOVES //////
  780. /**
  781. * Removes this node itself from its parent node with a cleanup.
  782. * If the node orphan, then nothing happens.
  783. * @see `removeFromParentAndCleanup(bool)`
  784. */
  785. virtual void removeFromParent();
  786. /**
  787. * Removes this node itself from its parent node.
  788. * If the node orphan, then nothing happens.
  789. * @param cleanup true if all actions and callbacks on this node should be removed, false otherwise.
  790. * @js removeFromParent
  791. * @lua removeFromParent
  792. */
  793. virtual void removeFromParentAndCleanup(bool cleanup);
  794. /**
  795. * Removes a child from the container. It will also cleanup all running actions depending on the cleanup parameter.
  796. *
  797. * @param child The child node which will be removed.
  798. * @param cleanup True if all running actions and callbacks on the child node will be cleanup, false otherwise.
  799. */
  800. virtual void removeChild(Node* child, bool cleanup = true);
  801. /**
  802. * Removes a child from the container by tag value. It will also cleanup all running actions depending on the cleanup parameter.
  803. *
  804. * @param tag An integer number that identifies a child node.
  805. * @param cleanup True if all running actions and callbacks on the child node will be cleanup, false otherwise.
  806. *
  807. * Please use `removeChildByName` instead.
  808. */
  809. virtual void removeChildByTag(int tag, bool cleanup = true);
  810. /**
  811. * Removes a child from the container by tag value. It will also cleanup all running actions depending on the cleanup parameter.
  812. *
  813. * @param name A string that identifies a child node.
  814. * @param cleanup True if all running actions and callbacks on the child node will be cleanup, false otherwise.
  815. */
  816. virtual void removeChildByName(const std::string &name, bool cleanup = true);
  817. /**
  818. * Removes all children from the container with a cleanup.
  819. *
  820. * @see `removeAllChildrenWithCleanup(bool)`
  821. */
  822. virtual void removeAllChildren();
  823. /**
  824. * Removes all children from the container, and do a cleanup to all running actions depending on the cleanup parameter.
  825. *
  826. * @param cleanup True if all running actions on all children nodes should be cleanup, false otherwise.
  827. * @js removeAllChildren
  828. * @lua removeAllChildren
  829. */
  830. virtual void removeAllChildrenWithCleanup(bool cleanup);
  831. /**
  832. * Reorders a child according to a new z value.
  833. *
  834. * @param child An already added child node. It MUST be already added.
  835. * @param localZOrder Z order for drawing priority. Please refer to setLocalZOrder(int).
  836. */
  837. virtual void reorderChild(Node * child, int localZOrder);
  838. /**
  839. * Sorts the children array once before drawing, instead of every time when a child is added or reordered.
  840. * This approach can improves the performance massively.
  841. * @note Don't call this manually unless a child added needs to be removed in the same frame.
  842. */
  843. virtual void sortAllChildren();
  844. /**
  845. * Sorts helper function
  846. *
  847. */
  848. template<typename _T> inline
  849. static void sortNodes(cocos2d::Vector<_T*>& nodes)
  850. {
  851. static_assert(std::is_base_of<Node, _T>::value, "Node::sortNodes: Only accept derived of Node!");
  852. #if CC_64BITS
  853. std::sort(std::begin(nodes), std::end(nodes), [](_T* n1, _T* n2) {
  854. return (n1->_localZOrder$Arrival < n2->_localZOrder$Arrival);
  855. });
  856. #else
  857. std::sort(std::begin(nodes), std::end(nodes), [](_T* n1, _T* n2) {
  858. return (n1->_localZOrder == n2->_localZOrder && n1->_orderOfArrival < n2->_orderOfArrival) || n1->_localZOrder < n2->_localZOrder;
  859. });
  860. #endif
  861. }
  862. /// @} end of Children and Parent
  863. /// @{
  864. /// @name Tag & User data
  865. /**
  866. * Returns a tag that is used to identify the node easily.
  867. *
  868. * @return An integer that identifies the node.
  869. *
  870. * Please use `getTag()` instead.
  871. */
  872. virtual int getTag() const;
  873. /**
  874. * Changes the tag that is used to identify the node easily.
  875. *
  876. * Please refer to getTag for the sample code.
  877. *
  878. * @param tag A integer that identifies the node.
  879. *
  880. * Please use `setName()` instead.
  881. */
  882. virtual void setTag(int tag);
  883. /** Returns a string that is used to identify the node.
  884. * @return A string that identifies the node.
  885. *
  886. * @since v3.2
  887. */
  888. virtual const std::string& getName() const;
  889. /** Changes the name that is used to identify the node easily.
  890. * @param name A string that identifies the node.
  891. *
  892. * @since v3.2
  893. */
  894. virtual void setName(const std::string& name);
  895. /**
  896. * Returns a custom user data pointer.
  897. *
  898. * You can set everything in UserData pointer, a data block, a structure or an object.
  899. *
  900. * @return A custom user data pointer.
  901. * @lua NA
  902. */
  903. virtual void* getUserData() { return _userData; }
  904. /**
  905. * @lua NA
  906. */
  907. virtual const void* getUserData() const { return _userData; }
  908. /**
  909. * Sets a custom user data pointer.
  910. *
  911. * You can set everything in UserData pointer, a data block, a structure or an object, etc.
  912. * @warning Don't forget to release the memory manually,
  913. * especially before you change this data pointer, and before this node is autoreleased.
  914. *
  915. * @param userData A custom user data pointer.
  916. * @lua NA
  917. */
  918. virtual void setUserData(void *userData);
  919. /**
  920. * Returns a user assigned Object.
  921. *
  922. * Similar to userData, but instead of holding a void* it holds an object.
  923. *
  924. * @return A user assigned Object.
  925. * @lua NA
  926. */
  927. virtual Ref* getUserObject() { return _userObject; }
  928. /**
  929. * @lua NA
  930. */
  931. virtual const Ref* getUserObject() const { return _userObject; }
  932. /**
  933. * Returns a user assigned Object.
  934. *
  935. * Similar to UserData, but instead of holding a void* it holds an object.
  936. * The UserObject will be retained once in this method,
  937. * and the previous UserObject (if existed) will be released.
  938. * The UserObject will be released in Node's destructor.
  939. *
  940. * @param userObject A user assigned Object.
  941. */
  942. virtual void setUserObject(Ref *userObject);
  943. /// @} end of Tag & User Data
  944. /// @{
  945. /// @name GLProgram
  946. /**
  947. * Return the GLProgram (shader) currently used for this node.
  948. *
  949. * @return The GLProgram (shader) currently used for this node.
  950. */
  951. GLProgram* getGLProgram() const;
  952. CC_DEPRECATED_ATTRIBUTE GLProgram* getShaderProgram() const { return getGLProgram(); }
  953. /**
  954. * Sets the shader program for this node
  955. *
  956. * Since v2.0, each rendering node must set its shader program.
  957. * It should be set in initialize phase.
  958. @code
  959. node->setGLProgram(GLProgramCache::getInstance()->getProgram(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR));
  960. @endcode
  961. *
  962. * @param glprogram The shader program.
  963. */
  964. virtual void setGLProgram(GLProgram *glprogram);
  965. CC_DEPRECATED_ATTRIBUTE void setShaderProgram(GLProgram *glprogram) { setGLProgram(glprogram); }
  966. /**
  967. * Return the GLProgramState currently used for this node.
  968. *
  969. * @return The GLProgramState currently used for this node.
  970. */
  971. GLProgramState *getGLProgramState() const;
  972. /**
  973. * Set the GLProgramState for this node.
  974. *
  975. * @param glProgramState The GLProgramState for this node.
  976. */
  977. virtual void setGLProgramState(GLProgramState *glProgramState);
  978. /// @} end of Shader Program
  979. /**
  980. * Returns whether or not the node is "running".
  981. *
  982. * If the node is running it will accept event callbacks like onEnter(), onExit(), update().
  983. *
  984. * @return Whether or not the node is running.
  985. */
  986. virtual bool isRunning() const;
  987. /**
  988. * Schedules for lua script.
  989. * @js NA
  990. *
  991. * @param handler The key to search lua function.
  992. * @param priority A given priority value.
  993. */
  994. void scheduleUpdateWithPriorityLua(int handler, int priority);
  995. /// @} end Script Bindings
  996. /// @{
  997. /// @name Event Callbacks
  998. /**
  999. * Event callback that is invoked every time when Node enters the 'stage'.
  1000. * If the Node enters the 'stage' with a transition, this event is called when the transition starts.
  1001. * During onEnter you can't access a "sister/brother" node.
  1002. * If you override onEnter, you shall call its parent's one, e.g., Node::onEnter().
  1003. * @lua NA
  1004. */
  1005. virtual void onEnter();
  1006. /** Event callback that is invoked when the Node enters in the 'stage'.
  1007. * If the Node enters the 'stage' with a transition, this event is called when the transition finishes.
  1008. * If you override onEnterTransitionDidFinish, you shall call its parent's one, e.g. Node::onEnterTransitionDidFinish()
  1009. * @lua NA
  1010. */
  1011. virtual void onEnterTransitionDidFinish();
  1012. /**
  1013. * Event callback that is invoked every time the Node leaves the 'stage'.
  1014. * If the Node leaves the 'stage' with a transition, this event is called when the transition finishes.
  1015. * During onExit you can't access a sibling node.
  1016. * If you override onExit, you shall call its parent's one, e.g., Node::onExit().
  1017. * @lua NA
  1018. */
  1019. virtual void onExit();
  1020. /**
  1021. * Event callback that is called every time the Node leaves the 'stage'.
  1022. * If the Node leaves the 'stage' with a transition, this callback is called when the transition starts.
  1023. * @lua NA
  1024. */
  1025. virtual void onExitTransitionDidStart();
  1026. /// @} end of event callbacks.
  1027. /**
  1028. * Stops all running actions and schedulers
  1029. */
  1030. virtual void cleanup();
  1031. /**
  1032. * Override this method to draw your own node.
  1033. * The following GL states will be enabled by default:
  1034. * - `glEnableClientState(GL_VERTEX_ARRAY);`
  1035. * - `glEnableClientState(GL_COLOR_ARRAY);`
  1036. * - `glEnableClientState(GL_TEXTURE_COORD_ARRAY);`
  1037. * - `glEnable(GL_TEXTURE_2D);`
  1038. * AND YOU SHOULD NOT DISABLE THEM AFTER DRAWING YOUR NODE
  1039. * But if you enable any other GL state, you should disable it after drawing your node.
  1040. *
  1041. * @param renderer A given renderer.
  1042. * @param transform A transform matrix.
  1043. * @param flags Renderer flag.
  1044. */
  1045. virtual void draw(Renderer *renderer, const Mat4& transform, uint32_t flags);
  1046. virtual void draw() final;
  1047. /**
  1048. * Visits this node's children and draw them recursively.
  1049. *
  1050. * @param renderer A given renderer.
  1051. * @param parentTransform A transform matrix.
  1052. * @param parentFlags Renderer flag.
  1053. */
  1054. virtual void visit(Renderer *renderer, const Mat4& parentTransform, uint32_t parentFlags);
  1055. virtual void visit() final;
  1056. /** Returns the Scene that contains the Node.
  1057. It returns `nullptr` if the node doesn't belong to any Scene.
  1058. This function recursively calls parent->getScene() until parent is a Scene object. The results are not cached. It is that the user caches the results in case this functions is being used inside a loop.
  1059. *
  1060. * @return The Scene that contains the node.
  1061. */
  1062. virtual Scene* getScene() const;
  1063. /**
  1064. * Returns an AABB (axis-aligned bounding-box) in its parent's coordinate system.
  1065. *
  1066. * @return An AABB (axis-aligned bounding-box) in its parent's coordinate system
  1067. */
  1068. virtual Rect getBoundingBox() const;
  1069. /** @deprecated Use getBoundingBox instead */
  1070. CC_DEPRECATED_ATTRIBUTE virtual Rect boundingBox() const { return getBoundingBox(); }
  1071. /** Set event dispatcher for scene.
  1072. *
  1073. * @param dispatcher The event dispatcher of scene.
  1074. */
  1075. virtual void setEventDispatcher(EventDispatcher* dispatcher);
  1076. /** Get the event dispatcher of scene.
  1077. *
  1078. * @return The event dispatcher of scene.
  1079. */
  1080. virtual EventDispatcher* getEventDispatcher() const { return _eventDispatcher; };
  1081. /// @{
  1082. /// @name Actions
  1083. /**
  1084. * Sets the ActionManager object that is used by all actions.
  1085. *
  1086. * @warning If you set a new ActionManager, then previously created actions will be removed.
  1087. *
  1088. * @param actionManager A ActionManager object that is used by all actions.
  1089. */
  1090. virtual void setActionManager(ActionManager* actionManager);
  1091. /**
  1092. * Gets the ActionManager object that is used by all actions.
  1093. * @see setActionManager(ActionManager*)
  1094. * @return A ActionManager object.
  1095. */
  1096. virtual ActionManager* getActionManager() { return _actionManager; }
  1097. virtual const ActionManager* getActionManager() const { return _actionManager; }
  1098. /**
  1099. * Executes an action, and returns the action that is executed.
  1100. *
  1101. * This node becomes the action's target. Refer to Action::getTarget().
  1102. * @warning Actions don't retain their target.
  1103. *
  1104. * @param action An Action pointer.
  1105. */
  1106. virtual Action* runAction(Action* action);
  1107. /**
  1108. * Stops and removes all actions from the running action list .
  1109. */
  1110. void stopAllActions();
  1111. /**
  1112. * Stops and removes an action from the running action list.
  1113. *
  1114. * @param action The action object to be removed.
  1115. */
  1116. void stopAction(Action* action);
  1117. /**
  1118. * Removes an action from the running action list by its tag.
  1119. *
  1120. * @param tag A tag that indicates the action to be removed.
  1121. */
  1122. void stopActionByTag(int tag);
  1123. /**
  1124. * Removes all actions from the running action list by its tag.
  1125. *
  1126. * @param tag A tag that indicates the action to be removed.
  1127. */
  1128. void stopAllActionsByTag(int tag);
  1129. /**
  1130. * Removes all actions from the running action list by its flags.
  1131. *
  1132. * @param flags A flag field that removes actions based on bitwise AND.
  1133. */
  1134. void stopActionsByFlags(unsigned int flags);
  1135. /**
  1136. * Gets an action from the running action list by its tag.
  1137. *
  1138. * @see `setTag(int)`, `getTag()`.
  1139. *
  1140. * @return The action object with the given tag.
  1141. */
  1142. Action* getActionByTag(int tag);
  1143. /**
  1144. * Returns the numbers of actions that are running plus the ones that are schedule to run (actions in actionsToAdd and actions arrays).
  1145. *
  1146. * Composable actions are counted as 1 action. Example:
  1147. * If you are running 1 Sequence of 7 actions, it will return 1.
  1148. * If you are running 7 Sequences of 2 actions, it will return 7.
  1149. *
  1150. * @return The number of actions that are running plus the ones that are schedule to run.
  1151. */
  1152. ssize_t getNumberOfRunningActions() const;
  1153. /**
  1154. * Returns the numbers of actions that are running plus the ones that are
  1155. * schedule to run (actions in actionsToAdd and actions arrays) with a
  1156. * specific tag.
  1157. *
  1158. * Composable actions are counted as 1 action. Example:
  1159. * If you are running 1 Sequence of 7 actions, it will return 1.
  1160. * If you are running 7 Sequences of 2 actions, it will return 7.
  1161. *
  1162. * @param tag The tag that will be searched.
  1163. *
  1164. * @return The number of actions that are running plus the
  1165. * ones that are schedule to run with specific tag.
  1166. */
  1167. ssize_t getNumberOfRunningActionsByTag(int tag) const;
  1168. /** @deprecated Use getNumberOfRunningActions() instead */
  1169. CC_DEPRECATED_ATTRIBUTE ssize_t numberOfRunningActions() const { return getNumberOfRunningActions(); };
  1170. /// @} end of Actions
  1171. /// @{
  1172. /// @name Scheduler and Timer
  1173. /**
  1174. * Sets a Scheduler object that is used to schedule all "updates" and timers.
  1175. *
  1176. * @warning If you set a new Scheduler, then previously created timers/update are going to be removed.
  1177. * @param scheduler A Scheduler object that is used to schedule all "update" and timers.
  1178. */
  1179. virtual void setScheduler(Scheduler* scheduler);
  1180. /**
  1181. * Gets a Scheduler object.
  1182. *
  1183. * @see setScheduler(Scheduler*)
  1184. * @return A Scheduler object.
  1185. */
  1186. virtual Scheduler* getScheduler() { return _scheduler; }
  1187. virtual const Scheduler* getScheduler() const { return _scheduler; }
  1188. /**
  1189. * Checks whether a selector is scheduled.
  1190. *
  1191. * @param selector A function selector
  1192. * @return Whether the function selector is scheduled.
  1193. * @js NA
  1194. * @lua NA
  1195. */
  1196. bool isScheduled(SEL_SCHEDULE selector) const;
  1197. /**
  1198. * Checks whether a lambda function is scheduled.
  1199. *
  1200. * @param key key of the callback
  1201. * @return Whether the lambda function selector is scheduled.
  1202. * @js NA
  1203. * @lua NA
  1204. */
  1205. bool isScheduled(const std::string &key) const;
  1206. /**
  1207. * Schedules the "update" method.
  1208. *
  1209. * It will use the order number 0. This method will be called every frame.
  1210. * Scheduled methods with a lower order value will be called before the ones that have a higher order value.
  1211. * Only one "update" method could be scheduled per node.
  1212. * @lua NA
  1213. */
  1214. void scheduleUpdate(void);
  1215. /**
  1216. * Schedules the "update" method with a custom priority.
  1217. *
  1218. * This selector will be called every frame.
  1219. * Scheduled methods with a lower priority will be called before the ones that have a higher value.
  1220. * Only one "update" selector could be scheduled per node (You can't have 2 'update' selectors).
  1221. * @lua NA
  1222. *
  1223. * @param priority A given priority value.
  1224. */
  1225. void scheduleUpdateWithPriority(int priority);
  1226. /*
  1227. * Unschedules the "update" method.
  1228. * @see scheduleUpdate();
  1229. */
  1230. void unscheduleUpdate(void);
  1231. /**
  1232. * Schedules a custom selector.
  1233. *
  1234. * If the selector is already scheduled, then the interval parameter will be updated without scheduling it again.
  1235. @code
  1236. // firstly, implement a schedule function
  1237. void MyNode::TickMe(float dt);
  1238. // wrap this function into a selector via schedule_selector macro.
  1239. this->schedule(CC_SCHEDULE_SELECTOR(MyNode::TickMe), 0, 0, 0);
  1240. @endcode
  1241. *
  1242. * @param selector The SEL_SCHEDULE selector to be scheduled.
  1243. * @param interval Tick interval in seconds. 0 means tick every frame. If interval = 0, it's recommended to use scheduleUpdate() instead.
  1244. * @param repeat The selector will be executed (repeat + 1) times, you can use CC_REPEAT_FOREVER for tick infinitely.
  1245. * @param delay The amount of time that the first tick will wait before execution.
  1246. * @lua NA
  1247. */
  1248. void schedule(SEL_SCHEDULE selector, float interval, unsigned int repeat, float delay);
  1249. /**
  1250. * Schedules a custom selector with an interval time in seconds.
  1251. * @see `schedule(SEL_SCHEDULE, float, unsigned int, float)`
  1252. *
  1253. * @param selector The SEL_SCHEDULE selector to be scheduled.
  1254. * @param interval Callback interval time in seconds. 0 means tick every frame,
  1255. * @lua NA
  1256. */
  1257. void schedule(SEL_SCHEDULE selector, float interval);
  1258. /**
  1259. * Schedules a selector that runs only once, with a delay of 0 or larger
  1260. * @see `schedule(SEL_SCHEDULE, float, unsigned int, float)`
  1261. *
  1262. * @param selector The SEL_SCHEDULE selector to be scheduled.
  1263. * @param delay The amount of time that the first tick will wait before execution.
  1264. * @lua NA
  1265. */
  1266. void scheduleOnce(SEL_SCHEDULE selector, float delay);
  1267. /**
  1268. * Schedules a lambda function that runs only once, with a delay of 0 or larger
  1269. *
  1270. * @param callback The lambda function to be scheduled.
  1271. * @param delay The amount of time that the first tick will wait before execution.
  1272. * @param key The key of the lambda function. To be used if you want to unschedule it.
  1273. * @lua NA
  1274. */
  1275. void scheduleOnce(const std::function<void(float)>& callback, float delay, const std::string &key);
  1276. /**
  1277. * Schedules a custom selector, the scheduled selector will be ticked every frame.
  1278. * @see schedule(SEL_SCHEDULE, float, unsigned int, float)
  1279. *
  1280. * @param selector A function wrapped as a selector
  1281. * @lua NA
  1282. */
  1283. void schedule(SEL_SCHEDULE selector);
  1284. /**
  1285. * Schedules a lambda function. The scheduled lambda function will be called every frame.
  1286. *
  1287. * @param callback The lambda function to be scheduled.
  1288. * @param key The key of the lambda function. To be used if you want to unschedule it.
  1289. * @lua NA
  1290. */
  1291. void schedule(const std::function<void(float)>& callback, const std::string &key);
  1292. /**
  1293. * Schedules a lambda function. The scheduled lambda function will be called every "interval" seconds
  1294. *
  1295. * @param callback The lambda function to be scheduled
  1296. * @param interval Callback interval time in seconds. 0 means every frame,
  1297. * @param key The key of the lambda function. To be used if you want to unschedule it
  1298. * @lua NA
  1299. */
  1300. void schedule(const std::function<void(float)>& callback, float interval, const std::string &key);
  1301. /**
  1302. * Schedules a lambda function.
  1303. *
  1304. * @param callback The lambda function to be schedule.
  1305. * @param interval Tick interval in seconds. 0 means tick every frame.
  1306. * @param repeat The selector will be executed (repeat + 1) times, you can use CC_REPEAT_FOREVER for tick infinitely.
  1307. * @param delay The amount of time that the first tick will wait before execution.
  1308. * @param key The key of the lambda function. To be used if you want to unschedule it.
  1309. * @lua NA
  1310. */
  1311. void schedule(const std::function<void(float)>& callback, float interval, unsigned int repeat, float delay, const std::string &key);
  1312. /**
  1313. * Unschedules a custom selector.
  1314. * @see `schedule(SEL_SCHEDULE, float, unsigned int, float)`
  1315. *
  1316. * @param selector A function wrapped as a selector.
  1317. * @lua NA
  1318. */
  1319. void unschedule(SEL_SCHEDULE selector);
  1320. /**
  1321. * Unschedules a lambda function.
  1322. *
  1323. * @param key The key of the lambda function to be unscheduled.
  1324. * @lua NA
  1325. */
  1326. void unschedule(const std::string &key);
  1327. /**
  1328. * Unschedule all scheduled selectors and lambda functions: custom selectors, and the 'update' selector and lambda functions.
  1329. * Actions are not affected by this method.
  1330. * @lua NA
  1331. */
  1332. void unscheduleAllCallbacks();
  1333. CC_DEPRECATED_ATTRIBUTE void unscheduleAllSelectors() { unscheduleAllCallbacks(); }
  1334. /**
  1335. * Resumes all scheduled selectors, actions and event listeners.
  1336. * This method is called internally by onEnter.
  1337. */
  1338. virtual void resume(void);
  1339. /**
  1340. * Pauses all scheduled selectors, actions and event listeners.
  1341. * This method is called internally by onExit.
  1342. */
  1343. virtual void pause(void);
  1344. /**
  1345. * Resumes all scheduled selectors, actions and event listeners.
  1346. * This method is called internally by onEnter.
  1347. */
  1348. CC_DEPRECATED_ATTRIBUTE void resumeSchedulerAndActions();
  1349. /**
  1350. * Pauses all scheduled selectors, actions and event listeners.
  1351. * This method is called internally by onExit.
  1352. */
  1353. CC_DEPRECATED_ATTRIBUTE void pauseSchedulerAndActions();
  1354. /**
  1355. * Update method will be called automatically every frame if "scheduleUpdate" is called, and the node is "live".
  1356. * @param delta In seconds.
  1357. */
  1358. virtual void update(float delta);
  1359. /// @} end of Scheduler and Timer
  1360. /// @{
  1361. /// @name Transformations
  1362. /**
  1363. * Calls children's updateTransform() method recursively.
  1364. *
  1365. * This method is moved from Sprite, so it's no longer specific to Sprite.
  1366. * As the result, you apply SpriteBatchNode's optimization on your customed Node.
  1367. * e.g., `batchNode->addChild(myCustomNode)`, while you can only addChild(sprite) before.
  1368. */
  1369. virtual void updateTransform();
  1370. /**
  1371. * Returns the matrix that transform the node's (local) space coordinates into the parent's space coordinates.
  1372. * The matrix is in Pixels.
  1373. *
  1374. * @return The transformation matrix.
  1375. */
  1376. virtual const Mat4& getNodeToParentTransform() const;
  1377. virtual AffineTransform getNodeToParentAffineTransform() const;
  1378. /**
  1379. * Returns the matrix that transform the node's (local) space coordinates into the parent's space coordinates.
  1380. * The matrix is in Pixels.
  1381. * Note: If ancestor is not a valid ancestor of the node, the API would return the same value as @see getNodeToWorldTransform
  1382. *
  1383. * @param ancestor The parent's node pointer.
  1384. * @since v3.7
  1385. * @return The transformation matrix.
  1386. */
  1387. virtual Mat4 getNodeToParentTransform(Node* ancestor) const;
  1388. /**
  1389. * Returns the affine transform matrix that transform the node's (local) space coordinates into the parent's space coordinates.
  1390. * The matrix is in Pixels.
  1391. *
  1392. * Note: If ancestor is not a valid ancestor of the node, the API would return the same value as @see getNodeToWorldAffineTransform
  1393. *
  1394. * @param ancestor The parent's node pointer.
  1395. * @since v3.7
  1396. * @return The affine transformation matrix.
  1397. */
  1398. virtual AffineTransform getNodeToParentAffineTransform(Node* ancestor) const;
  1399. /**
  1400. * Sets the transformation matrix manually.
  1401. *
  1402. * @param transform A given transformation matrix.
  1403. */
  1404. virtual void setNodeToParentTransform(const Mat4& transform);
  1405. /** @deprecated use getNodeToParentTransform() instead */
  1406. CC_DEPRECATED_ATTRIBUTE virtual AffineTransform nodeToParentTransform() const { return getNodeToParentAffineTransform(); }
  1407. /**
  1408. * Returns the matrix that transform parent's space coordinates to the node's (local) space coordinates.
  1409. * The matrix is in Pixels.
  1410. *
  1411. * @return The transformation matrix.
  1412. */
  1413. virtual const Mat4& getParentToNodeTransform() const;
  1414. virtual AffineTransform getParentToNodeAffineTransform() const;
  1415. /** @deprecated Use getParentToNodeTransform() instead */
  1416. CC_DEPRECATED_ATTRIBUTE virtual AffineTransform parentToNodeTransform() const { return getParentToNodeAffineTransform(); }
  1417. /**
  1418. * Returns the world affine transform matrix. The matrix is in Pixels.
  1419. *
  1420. * @return transformation matrix, in pixels.
  1421. */
  1422. virtual Mat4 getNodeToWorldTransform() const;
  1423. virtual AffineTransform getNodeToWorldAffineTransform() const;
  1424. /** @deprecated Use getNodeToWorldTransform() instead */
  1425. CC_DEPRECATED_ATTRIBUTE virtual AffineTransform nodeToWorldTransform() const { return getNodeToWorldAffineTransform(); }
  1426. /**
  1427. * Returns the inverse world affine transform matrix. The matrix is in Pixels.
  1428. *
  1429. * @return The transformation matrix.
  1430. */
  1431. virtual Mat4 getWorldToNodeTransform() const;
  1432. virtual AffineTransform getWorldToNodeAffineTransform() const;
  1433. /** @deprecated Use getWorldToNodeTransform() instead */
  1434. CC_DEPRECATED_ATTRIBUTE virtual AffineTransform worldToNodeTransform() const { return getWorldToNodeAffineTransform(); }
  1435. /// @} end of Transformations
  1436. /// @{
  1437. /// @name Coordinate Converters
  1438. /**
  1439. * Converts a Vec2 to node (local) space coordinates. The result is in Points.
  1440. *
  1441. * @param worldPoint A given coordinate.
  1442. * @return A point in node (local) space coordinates.
  1443. */
  1444. Vec2 convertToNodeSpace(const Vec2& worldPoint) const;
  1445. /**
  1446. * Converts a Vec2 to world space coordinates. The result is in Points.
  1447. *
  1448. * @param nodePoint A given coordinate.
  1449. * @return A point in world space coordinates.
  1450. */
  1451. Vec2 convertToWorldSpace(const Vec2& nodePoint) const;
  1452. /**
  1453. * Converts a Vec2 to node (local) space coordinates. The result is in Points.
  1454. * treating the returned/received node point as anchor relative.
  1455. *
  1456. * @param worldPoint A given coordinate.
  1457. * @return A point in node (local) space coordinates, anchor relative.
  1458. */
  1459. Vec2 convertToNodeSpaceAR(const Vec2& worldPoint) const;
  1460. /**
  1461. * Converts a local Vec2 to world space coordinates.The result is in Points.
  1462. * treating the returned/received node point as anchor relative.
  1463. *
  1464. * @param nodePoint A given coordinate.
  1465. * @return A point in world space coordinates, anchor relative.
  1466. */
  1467. Vec2 convertToWorldSpaceAR(const Vec2& nodePoint) const;
  1468. /**
  1469. * convenience methods which take a Touch instead of Vec2.
  1470. *
  1471. * @param touch A given touch.
  1472. * @return A point in world space coordinates.
  1473. */
  1474. Vec2 convertTouchToNodeSpace(Touch * touch) const;
  1475. /**
  1476. * converts a Touch (world coordinates) into a local coordinate. This method is AR (Anchor Relative).
  1477. *
  1478. * @param touch A given touch.
  1479. * @return A point in world space coordinates, anchor relative.
  1480. */
  1481. Vec2 convertTouchToNodeSpaceAR(Touch * touch) const;
  1482. /**
  1483. * Sets an additional transform matrix to the node.
  1484. *
  1485. * In order to remove it, call it again with the argument `nullptr`.
  1486. *
  1487. * @note The additional transform will be concatenated at the end of getNodeToParentTransform.
  1488. * It could be used to simulate `parent-child` relationship between two nodes (e.g. one is in BatchNode, another isn't).
  1489. *
  1490. * @param additionalTransform An additional transform matrix.
  1491. */
  1492. void setAdditionalTransform(const Mat4* additionalTransform);
  1493. void setAdditionalTransform(const Mat4& additionalTransform);
  1494. void setAdditionalTransform(const AffineTransform& additionalTransform);
  1495. /// @} end of Coordinate Converters
  1496. /// @{
  1497. /// @name component functions
  1498. /**
  1499. * Gets a component by its name.
  1500. *
  1501. * @param name A given name of component.
  1502. * @return The Component by name.
  1503. */
  1504. Component* getComponent(const std::string& name);
  1505. /**
  1506. * Adds a component.
  1507. *
  1508. * @param component A given component.
  1509. * @return True if added success.
  1510. */
  1511. virtual bool addComponent(Component *component);
  1512. /**
  1513. * Removes a component by its name.
  1514. *
  1515. * @param name A given name of component.
  1516. * @return True if removed success.
  1517. */
  1518. virtual bool removeComponent(const std::string& name);
  1519. /**
  1520. * Removes a component by its pointer.
  1521. *
  1522. * @param component A given component.
  1523. * @return True if removed success.
  1524. */
  1525. virtual bool removeComponent(Component *component);
  1526. /**
  1527. * Removes all components
  1528. */
  1529. virtual void removeAllComponents();
  1530. /// @} end of component functions
  1531. // overrides
  1532. /**
  1533. * Return the node's opacity.
  1534. * @return A GLubyte value.
  1535. */
  1536. virtual GLubyte getOpacity() const;
  1537. /**
  1538. * Return the node's display opacity.
  1539. * The difference between opacity and displayedOpacity is:
  1540. * The displayedOpacity is what's the final rendering opacity of node.
  1541. * @return A GLubyte value.
  1542. */
  1543. virtual GLubyte getDisplayedOpacity() const;
  1544. /**
  1545. * Change node opacity.
  1546. * @param opacity A GLubyte opacity value.
  1547. */
  1548. virtual void setOpacity(GLubyte opacity);
  1549. /**
  1550. * Update the displayed opacity of node with it's parent opacity;
  1551. * @param parentOpacity The opacity of parent node.
  1552. */
  1553. virtual void updateDisplayedOpacity(GLubyte parentOpacity);
  1554. /**
  1555. * Whether cascadeOpacity is enabled or not.
  1556. * @return A boolean value.
  1557. */
  1558. virtual bool isCascadeOpacityEnabled() const;
  1559. /**
  1560. * Change node's cascadeOpacity property.
  1561. * @param cascadeOpacityEnabled True to enable cascadeOpacity, false otherwise.
  1562. */
  1563. virtual void setCascadeOpacityEnabled(bool cascadeOpacityEnabled);
  1564. /**
  1565. * Query node's color value.
  1566. * @return A Color3B color value.
  1567. */
  1568. virtual const Color3B& getColor() const;
  1569. /**
  1570. * Query node's displayed color.
  1571. * @return A Color3B color value.
  1572. */
  1573. virtual const Color3B& getDisplayedColor() const;
  1574. /**
  1575. * Change the color of node.
  1576. * @param color A Color3B color value.
  1577. */
  1578. virtual void setColor(const Color3B& color);
  1579. /**
  1580. * Update node's displayed color with its parent color.
  1581. * @param parentColor A Color3B color value.
  1582. */
  1583. virtual void updateDisplayedColor(const Color3B& parentColor);
  1584. /**
  1585. * Query whether cascadeColor is enabled or not.
  1586. * @return Whether cascadeColor is enabled or not.
  1587. */
  1588. virtual bool isCascadeColorEnabled() const;
  1589. /**
  1590. * If you want node's color affect the children node's color, then set it to true.
  1591. * Otherwise, set it to false.
  1592. * @param cascadeColorEnabled A boolean value.
  1593. */
  1594. virtual void setCascadeColorEnabled(bool cascadeColorEnabled);
  1595. /**
  1596. * If you want the opacity affect the color property, then set to true.
  1597. * @param value A boolean value.
  1598. */
  1599. virtual void setOpacityModifyRGB(bool value);
  1600. /**
  1601. * If node opacity will modify the RGB color value, then you should override this method and return true.
  1602. * @return A boolean value, true indicates that opacity will modify color; false otherwise.
  1603. */
  1604. virtual bool isOpacityModifyRGB() const;
  1605. /**
  1606. * Set the callback of event onEnter.
  1607. * @param callback A std::function<void()> callback.
  1608. */
  1609. void setOnEnterCallback(const std::function<void()>& callback) { _onEnterCallback = callback; }
  1610. /**
  1611. * Get the callback of event onEnter.
  1612. * @return A std:function<void()> callback.
  1613. */
  1614. const std::function<void()>& getOnEnterCallback() const { return _onEnterCallback; }
  1615. /**
  1616. * Set the callback of event onExit.
  1617. * @param callback A std::function<void()> callback.
  1618. */
  1619. void setOnExitCallback(const std::function<void()>& callback) { _onExitCallback = callback; }
  1620. /**
  1621. * Get the callback of event onExit.
  1622. * @return A std::function<void()>.
  1623. */
  1624. const std::function<void()>& getOnExitCallback() const { return _onExitCallback; }
  1625. /**
  1626. * Set the callback of event EnterTransitionDidFinish.
  1627. * @param callback A std::function<void()> callback.
  1628. */
  1629. void setonEnterTransitionDidFinishCallback(const std::function<void()>& callback) { _onEnterTransitionDidFinishCallback = callback; }
  1630. /**
  1631. * Get the callback of event EnterTransitionDidFinish.
  1632. * @return std::function<void()>
  1633. */
  1634. const std::function<void()>& getonEnterTransitionDidFinishCallback() const { return _onEnterTransitionDidFinishCallback; }
  1635. /**
  1636. * Set the callback of event ExitTransitionDidStart.
  1637. * @param callback A std::function<void()> callback.
  1638. */
  1639. void setonExitTransitionDidStartCallback(const std::function<void()>& callback) { _onExitTransitionDidStartCallback = callback; }
  1640. /**
  1641. * Get the callback of event ExitTransitionDidStart.
  1642. * @return std::function<void()>
  1643. */
  1644. const std::function<void()>& getonExitTransitionDidStartCallback() const { return _onExitTransitionDidStartCallback; }
  1645. /**
  1646. * get & set camera mask, the node is visible by the camera whose camera flag & node's camera mask is true
  1647. */
  1648. unsigned short getCameraMask() const { return _cameraMask; }
  1649. /**
  1650. * Modify the camera mask for current node.
  1651. * If applyChildren is true, then it will modify the camera mask of its children recursively.
  1652. * @param mask A unsigned short bit for mask.
  1653. * @param applyChildren A boolean value to determine whether the mask bit should apply to its children or not.
  1654. */
  1655. virtual void setCameraMask(unsigned short mask, bool applyChildren = true);
  1656. CC_CONSTRUCTOR_ACCESS:
  1657. // Nodes should be created using create();
  1658. Node();
  1659. virtual ~Node();
  1660. virtual bool init();
  1661. protected:
  1662. /// lazy allocs
  1663. void childrenAlloc(void);
  1664. /// helper that reorder a child
  1665. void insertChild(Node* child, int z);
  1666. /// Removes a child, call child->onExit(), do cleanup, remove it from children array.
  1667. void detachChild(Node *child, ssize_t index, bool doCleanup);
  1668. /// Convert cocos2d coordinates to UI windows coordinate.
  1669. Vec2 convertToWindowSpace(const Vec2& nodePoint) const;
  1670. Mat4 transform(const Mat4 &parentTransform);
  1671. uint32_t processParentFlags(const Mat4& parentTransform, uint32_t parentFlags);
  1672. virtual void updateCascadeOpacity();
  1673. virtual void disableCascadeOpacity();
  1674. virtual void updateCascadeColor();
  1675. virtual void disableCascadeColor();
  1676. virtual void updateColor() {}
  1677. bool doEnumerate(std::string name, std::function<bool (Node *)> callback) const;
  1678. bool doEnumerateRecursive(const Node* node, const std::string &name, std::function<bool (Node *)> callback) const;
  1679. //check whether this camera mask is visible by the current visiting camera
  1680. bool isVisitableByVisitingCamera() const;
  1681. // update quaternion from Rotation3D
  1682. void updateRotationQuat();
  1683. // update Rotation3D from quaternion
  1684. void updateRotation3D();
  1685. private:
  1686. void addChildHelper(Node* child, int localZOrder, int tag, const std::string &name, bool setTag);
  1687. protected:
  1688. float _rotationX; ///< rotation on the X-axis
  1689. float _rotationY; ///< rotation on the Y-axis
  1690. // rotation Z is decomposed in 2 to simulate Skew for Flash animations
  1691. float _rotationZ_X; ///< rotation angle on Z-axis, component X
  1692. float _rotationZ_Y; ///< rotation angle on Z-axis, component Y
  1693. Quaternion _rotationQuat; ///rotation using quaternion, if _rotationZ_X == _rotationZ_Y, _rotationQuat = RotationZ_X * RotationY * RotationX, else _rotationQuat = RotationY * RotationX
  1694. float _scaleX; ///< scaling factor on x-axis
  1695. float _scaleY; ///< scaling factor on y-axis
  1696. float _scaleZ; ///< scaling factor on z-axis
  1697. Vec2 _position; ///< position of the node
  1698. float _positionZ; ///< OpenGL real Z position
  1699. Vec2 _normalizedPosition;
  1700. bool _usingNormalizedPosition;
  1701. bool _normalizedPositionDirty;
  1702. float _skewX; ///< skew angle on x-axis
  1703. float _skewY; ///< skew angle on y-axis
  1704. Vec2 _anchorPointInPoints; ///< anchor point in points
  1705. Vec2 _anchorPoint; ///< anchor point normalized (NOT in points)
  1706. Size _contentSize; ///< untransformed size of the node
  1707. bool _contentSizeDirty; ///< whether or not the contentSize is dirty
  1708. Mat4 _modelViewTransform; ///< ModelView transform of the Node.
  1709. // "cache" variables are allowed to be mutable
  1710. mutable Mat4 _transform; ///< transform
  1711. mutable bool _transformDirty; ///< transform dirty flag
  1712. mutable Mat4 _inverse; ///< inverse transform
  1713. mutable bool _inverseDirty; ///< inverse transform dirty flag
  1714. mutable Mat4* _additionalTransform; ///< two transforms needed by additional transforms
  1715. mutable bool _additionalTransformDirty; ///< transform dirty ?
  1716. bool _transformUpdated; ///< Whether or not the Transform object was updated since the last frame
  1717. #if CC_LITTLE_ENDIAN
  1718. union {
  1719. struct {
  1720. std::uint32_t _orderOfArrival;
  1721. std::int32_t _localZOrder;
  1722. };
  1723. std::int64_t _localZOrder$Arrival;
  1724. };
  1725. #else
  1726. union {
  1727. struct {
  1728. std::int32_t _localZOrder;
  1729. std::uint32_t _orderOfArrival;
  1730. };
  1731. std::int64_t _localZOrder$Arrival;
  1732. };
  1733. #endif
  1734. float _globalZOrder; ///< Global order used to sort the node
  1735. static std::uint32_t s_globalOrderOfArrival;
  1736. Vector<Node*> _children; ///< array of children nodes
  1737. Node *_parent; ///< weak reference to parent node
  1738. Director* _director; //cached director pointer to improve rendering performance
  1739. int _tag; ///< a tag. Can be any number you assigned just to identify this node
  1740. std::string _name; ///<a string label, an user defined string to identify this node
  1741. size_t _hashOfName; ///<hash value of _name, used for speed in getChildByName
  1742. void *_userData; ///< A user assigned void pointer, Can be point to any cpp object
  1743. Ref *_userObject; ///< A user assigned Object
  1744. GLProgramState *_glProgramState; ///< OpenGL Program State
  1745. Scheduler *_scheduler; ///< scheduler used to schedule timers and updates
  1746. ActionManager *_actionManager; ///< a pointer to ActionManager singleton, which is used to handle all the actions
  1747. EventDispatcher* _eventDispatcher; ///< event dispatcher used to dispatch all kinds of events
  1748. bool _running; ///< is running
  1749. bool _visible; ///< is this node visible
  1750. bool _ignoreAnchorPointForPosition; ///< true if the Anchor Vec2 will be (0,0) when you position the Node, false otherwise.
  1751. ///< Used by Layer and Scene.
  1752. bool _reorderChildDirty; ///< children order dirty flag
  1753. bool _isTransitionFinished; ///< flag to indicate whether the transition was finished
  1754. #if CC_ENABLE_SCRIPT_BINDING
  1755. int _scriptHandler; ///< script handler for onEnter() & onExit(), used in Javascript binding and Lua binding.
  1756. int _updateScriptHandler; ///< script handler for update() callback per frame, which is invoked from lua & javascript.
  1757. ccScriptType _scriptType; ///< type of script binding, lua or javascript
  1758. #endif
  1759. ComponentContainer *_componentContainer; ///< Dictionary of components
  1760. // opacity controls
  1761. GLubyte _displayedOpacity;
  1762. GLubyte _realOpacity;
  1763. Color3B _displayedColor;
  1764. Color3B _realColor;
  1765. bool _cascadeColorEnabled;
  1766. bool _cascadeOpacityEnabled;
  1767. // camera mask, it is visible only when _cameraMask & current camera' camera flag is true
  1768. unsigned short _cameraMask;
  1769. std::function<void()> _onEnterCallback;
  1770. std::function<void()> _onExitCallback;
  1771. std::function<void()> _onEnterTransitionDidFinishCallback;
  1772. std::function<void()> _onExitTransitionDidStartCallback;
  1773. //Physics:remaining backwardly compatible
  1774. #if CC_USE_PHYSICS
  1775. PhysicsBody* _physicsBody;
  1776. public:
  1777. void setPhysicsBody(PhysicsBody* physicsBody)
  1778. {
  1779. if (_physicsBody != nullptr)
  1780. {
  1781. removeComponent(_physicsBody);
  1782. }
  1783. addComponent(physicsBody);
  1784. }
  1785. PhysicsBody* getPhysicsBody() const { return _physicsBody; }
  1786. friend class PhysicsBody;
  1787. #endif
  1788. static int __attachedNodeCount;
  1789. private:
  1790. CC_DISALLOW_COPY_AND_ASSIGN(Node);
  1791. };
  1792. /**
  1793. * This is a helper function, checks a GL screen point is in content rectangle space.
  1794. *
  1795. * The content rectangle defined by origin(0,0) and content size.
  1796. * This function convert GL screen point to near and far planes as points Pn and Pf,
  1797. * then calculate the intersect point P which the line PnPf intersect with content rectangle.
  1798. * If P in content rectangle means this node be hit.
  1799. *
  1800. * @param pt The point in GL screen space.
  1801. * @param camera Which camera used to unproject pt to near/far planes.
  1802. * @param w2l World to local transform matrix, used to convert Pn and Pf to rectangle space.
  1803. * @param rect The test rectangle in local space.
  1804. * @parma p Point to a Vec3 for store the intersect point, if don't need them set to nullptr.
  1805. * @return true if the point is in content rectangle, false otherwise.
  1806. */
  1807. bool CC_DLL isScreenPointInRect(const Vec2 &pt, const Camera* camera, const Mat4& w2l, const Rect& rect, Vec3 *p);
  1808. // NodeRGBA
  1809. /** @class __NodeRGBA
  1810. * @brief __NodeRGBA is a subclass of Node that implements the RGBAProtocol protocol.
  1811. All features from Node are valid, plus the following new features:
  1812. - opacity
  1813. - RGB colors
  1814. Opacity/Color propagates into children that conform to the RGBAProtocol if cascadeOpacity/cascadeColor is enabled.
  1815. @since v2.1
  1816. @js NA
  1817. */
  1818. class CC_DLL __NodeRGBA : public Node, public __RGBAProtocol
  1819. {
  1820. public:
  1821. // overrides
  1822. virtual GLubyte getOpacity() const override { return Node::getOpacity(); }
  1823. virtual GLubyte getDisplayedOpacity() const override { return Node::getDisplayedOpacity(); }
  1824. virtual void setOpacity(GLubyte opacity) override { return Node::setOpacity(opacity); }
  1825. virtual void updateDisplayedOpacity(GLubyte parentOpacity) override { return Node::updateDisplayedOpacity(parentOpacity); }
  1826. virtual bool isCascadeOpacityEnabled() const override { return Node::isCascadeOpacityEnabled(); }
  1827. virtual void setCascadeOpacityEnabled(bool cascadeOpacityEnabled) override { return Node::setCascadeOpacityEnabled(cascadeOpacityEnabled); }
  1828. virtual const Color3B& getColor(void) const override { return Node::getColor(); }
  1829. virtual const Color3B& getDisplayedColor() const override { return Node::getDisplayedColor(); }
  1830. virtual void setColor(const Color3B& color) override { return Node::setColor(color); }
  1831. virtual void updateDisplayedColor(const Color3B& parentColor) override { return Node::updateDisplayedColor(parentColor); }
  1832. virtual bool isCascadeColorEnabled() const override { return Node::isCascadeColorEnabled(); }
  1833. virtual void setCascadeColorEnabled(bool cascadeColorEnabled) override { return Node::setCascadeColorEnabled(cascadeColorEnabled); }
  1834. virtual void setOpacityModifyRGB(bool bValue) override { return Node::setOpacityModifyRGB(bValue); }
  1835. virtual bool isOpacityModifyRGB() const override { return Node::isOpacityModifyRGB(); }
  1836. CC_CONSTRUCTOR_ACCESS:
  1837. __NodeRGBA();
  1838. virtual ~__NodeRGBA() {}
  1839. private:
  1840. CC_DISALLOW_COPY_AND_ASSIGN(__NodeRGBA);
  1841. };
  1842. // end of _2d group
  1843. /// @}
  1844. NS_CC_END
  1845. #endif // __CCNODE_H__