1
0

CCNode.cpp 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212
  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-2017 Chukong Technologies Inc.
  7. http://www.cocos2d-x.org
  8. Permission is hereby granted, free of charge, to any person obtaining a copy
  9. of this software and associated documentation files (the "Software"), to deal
  10. in the Software without restriction, including without limitation the rights
  11. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. copies of the Software, and to permit persons to whom the Software is
  13. furnished to do so, subject to the following conditions:
  14. The above copyright notice and this permission notice shall be included in
  15. all copies or substantial portions of the Software.
  16. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. THE SOFTWARE.
  23. ****************************************************************************/
  24. #include "2d/CCNode.h"
  25. #include <algorithm>
  26. #include <string>
  27. #include <regex>
  28. #include "base/CCDirector.h"
  29. #include "base/CCScheduler.h"
  30. #include "base/CCEventDispatcher.h"
  31. #include "base/ccUTF8.h"
  32. #include "2d/CCCamera.h"
  33. #include "2d/CCActionManager.h"
  34. #include "2d/CCScene.h"
  35. #include "2d/CCComponent.h"
  36. #include "renderer/CCGLProgram.h"
  37. #include "renderer/CCGLProgramState.h"
  38. #include "renderer/CCMaterial.h"
  39. #include "math/TransformUtils.h"
  40. #if CC_NODE_RENDER_SUBPIXEL
  41. #define RENDER_IN_SUBPIXEL
  42. #else
  43. #define RENDER_IN_SUBPIXEL(__ARGS__) (ceil(__ARGS__))
  44. #endif
  45. NS_CC_BEGIN
  46. // FIXME:: Yes, nodes might have a sort problem once every 30 days if the game runs at 60 FPS and each frame sprites are reordered.
  47. unsigned int Node::s_globalOrderOfArrival = 0;
  48. int Node::__attachedNodeCount = 0;
  49. // MARK: Constructor, Destructor, Init
  50. Node::Node()
  51. : _rotationX(0.0f)
  52. , _rotationY(0.0f)
  53. , _rotationZ_X(0.0f)
  54. , _rotationZ_Y(0.0f)
  55. , _scaleX(1.0f)
  56. , _scaleY(1.0f)
  57. , _scaleZ(1.0f)
  58. , _positionZ(0.0f)
  59. , _usingNormalizedPosition(false)
  60. , _normalizedPositionDirty(false)
  61. , _skewX(0.0f)
  62. , _skewY(0.0f)
  63. , _contentSize(Size::ZERO)
  64. , _contentSizeDirty(true)
  65. , _transformDirty(true)
  66. , _inverseDirty(true)
  67. , _additionalTransform(nullptr)
  68. , _additionalTransformDirty(false)
  69. , _transformUpdated(true)
  70. // children (lazy allocs)
  71. // lazy alloc
  72. , _localZOrderAndArrival(0)
  73. , _localZOrder(0)
  74. , _globalZOrder(0)
  75. , _parent(nullptr)
  76. // "whole screen" objects. like Scenes and Layers, should set _ignoreAnchorPointForPosition to true
  77. , _tag(Node::INVALID_TAG)
  78. , _name("")
  79. , _hashOfName(0)
  80. // userData is always inited as nil
  81. , _userData(nullptr)
  82. , _userObject(nullptr)
  83. , _glProgramState(nullptr)
  84. , _running(false)
  85. , _visible(true)
  86. , _ignoreAnchorPointForPosition(false)
  87. , _reorderChildDirty(false)
  88. , _isTransitionFinished(false)
  89. #if CC_ENABLE_SCRIPT_BINDING
  90. , _updateScriptHandler(0)
  91. #endif
  92. , _componentContainer(nullptr)
  93. , _displayedOpacity(255)
  94. , _realOpacity(255)
  95. , _displayedColor(Color3B::WHITE)
  96. , _realColor(Color3B::WHITE)
  97. , _cascadeColorEnabled(false)
  98. , _cascadeOpacityEnabled(false)
  99. , _cameraMask(1)
  100. #if CC_USE_PHYSICS
  101. , _physicsBody(nullptr)
  102. #endif
  103. {
  104. // set default scheduler and actionManager
  105. _director = Director::getInstance();
  106. _actionManager = _director->getActionManager();
  107. _actionManager->retain();
  108. _scheduler = _director->getScheduler();
  109. _scheduler->retain();
  110. _eventDispatcher = _director->getEventDispatcher();
  111. _eventDispatcher->retain();
  112. #if CC_ENABLE_SCRIPT_BINDING
  113. ScriptEngineProtocol* engine = ScriptEngineManager::getInstance()->getScriptEngine();
  114. _scriptType = engine != nullptr ? engine->getScriptType() : kScriptTypeNone;
  115. #endif
  116. _transform = _inverse = Mat4::IDENTITY;
  117. }
  118. Node * Node::create()
  119. {
  120. Node * ret = new (std::nothrow) Node();
  121. if (ret && ret->init())
  122. {
  123. ret->autorelease();
  124. }
  125. else
  126. {
  127. CC_SAFE_DELETE(ret);
  128. }
  129. return ret;
  130. }
  131. Node::~Node()
  132. {
  133. CCLOGINFO( "deallocing Node: %p - tag: %i", this, _tag );
  134. #if CC_ENABLE_SCRIPT_BINDING
  135. if (_updateScriptHandler)
  136. {
  137. ScriptEngineManager::getInstance()->getScriptEngine()->removeScriptHandler(_updateScriptHandler);
  138. }
  139. #endif
  140. // User object has to be released before others, since userObject may have a weak reference of this node
  141. // It may invoke `node->stopAllAction();` while `_actionManager` is null if the next line is after `CC_SAFE_RELEASE_NULL(_actionManager)`.
  142. CC_SAFE_RELEASE_NULL(_userObject);
  143. // attributes
  144. CC_SAFE_RELEASE_NULL(_glProgramState);
  145. for (auto& child : _children)
  146. {
  147. child->_parent = nullptr;
  148. }
  149. removeAllComponents();
  150. CC_SAFE_DELETE(_componentContainer);
  151. stopAllActions();
  152. unscheduleAllCallbacks();
  153. CC_SAFE_RELEASE_NULL(_actionManager);
  154. CC_SAFE_RELEASE_NULL(_scheduler);
  155. _eventDispatcher->removeEventListenersForTarget(this);
  156. #if CC_NODE_DEBUG_VERIFY_EVENT_LISTENERS && COCOS2D_DEBUG > 0
  157. _eventDispatcher->debugCheckNodeHasNoEventListenersOnDestruction(this);
  158. #endif
  159. CCASSERT(!_running, "Node still marked as running on node destruction! Was base class onExit() called in derived class onExit() implementations?");
  160. CC_SAFE_RELEASE(_eventDispatcher);
  161. delete[] _additionalTransform;
  162. }
  163. bool Node::init()
  164. {
  165. return true;
  166. }
  167. void Node::cleanup()
  168. {
  169. #if CC_ENABLE_SCRIPT_BINDING
  170. if (_scriptType == kScriptTypeJavascript)
  171. {
  172. if (ScriptEngineManager::sendNodeEventToJS(this, kNodeOnCleanup))
  173. return;
  174. }
  175. else if (_scriptType == kScriptTypeLua)
  176. {
  177. ScriptEngineManager::sendNodeEventToLua(this, kNodeOnCleanup);
  178. }
  179. #endif // #if CC_ENABLE_SCRIPT_BINDING
  180. // actions
  181. this->stopAllActions();
  182. // timers
  183. this->unscheduleAllCallbacks();
  184. for( const auto &child: _children)
  185. child->cleanup();
  186. }
  187. std::string Node::getDescription() const
  188. {
  189. return StringUtils::format("<Node | Tag = %d", _tag);
  190. }
  191. // MARK: getters / setters
  192. float Node::getSkewX() const
  193. {
  194. return _skewX;
  195. }
  196. void Node::setSkewX(float skewX)
  197. {
  198. if (_skewX == skewX)
  199. return;
  200. _skewX = skewX;
  201. _transformUpdated = _transformDirty = _inverseDirty = true;
  202. }
  203. float Node::getSkewY() const
  204. {
  205. return _skewY;
  206. }
  207. void Node::setSkewY(float skewY)
  208. {
  209. if (_skewY == skewY)
  210. return;
  211. _skewY = skewY;
  212. _transformUpdated = _transformDirty = _inverseDirty = true;
  213. }
  214. void Node::setLocalZOrder(int z)
  215. {
  216. if (getLocalZOrder() == z)
  217. return;
  218. _setLocalZOrder(z);
  219. if (_parent)
  220. {
  221. _parent->reorderChild(this, z);
  222. }
  223. _eventDispatcher->setDirtyForNode(this);
  224. }
  225. /// zOrder setter : private method
  226. /// used internally to alter the zOrder variable. DON'T call this method manually
  227. void Node::_setLocalZOrder(int z)
  228. {
  229. _localZOrderAndArrival = (static_cast<std::int64_t>(z) << 32) | (_localZOrderAndArrival & 0xffffffff);
  230. _localZOrder = z;
  231. }
  232. void Node::updateOrderOfArrival()
  233. {
  234. _localZOrderAndArrival = (_localZOrderAndArrival & 0xffffffff00000000) | (++s_globalOrderOfArrival);
  235. }
  236. void Node::setGlobalZOrder(float globalZOrder)
  237. {
  238. if (_globalZOrder != globalZOrder)
  239. {
  240. _globalZOrder = globalZOrder;
  241. _eventDispatcher->setDirtyForNode(this);
  242. }
  243. }
  244. /// rotation getter
  245. float Node::getRotation() const
  246. {
  247. CCASSERT(_rotationZ_X == _rotationZ_Y, "CCNode#rotation. RotationX != RotationY. Don't know which one to return");
  248. return _rotationZ_X;
  249. }
  250. /// rotation setter
  251. void Node::setRotation(float rotation)
  252. {
  253. if (_rotationZ_X == rotation)
  254. return;
  255. _rotationZ_X = _rotationZ_Y = rotation;
  256. _transformUpdated = _transformDirty = _inverseDirty = true;
  257. updateRotationQuat();
  258. }
  259. float Node::getRotationSkewX() const
  260. {
  261. return _rotationZ_X;
  262. }
  263. void Node::setRotation3D(const Vec3& rotation)
  264. {
  265. if (_rotationX == rotation.x &&
  266. _rotationY == rotation.y &&
  267. _rotationZ_X == rotation.z)
  268. return;
  269. _transformUpdated = _transformDirty = _inverseDirty = true;
  270. _rotationX = rotation.x;
  271. _rotationY = rotation.y;
  272. // rotation Z is decomposed in 2 to simulate Skew for Flash animations
  273. _rotationZ_Y = _rotationZ_X = rotation.z;
  274. updateRotationQuat();
  275. }
  276. Vec3 Node::getRotation3D() const
  277. {
  278. // rotation Z is decomposed in 2 to simulate Skew for Flash animations
  279. CCASSERT(_rotationZ_X == _rotationZ_Y, "_rotationZ_X != _rotationZ_Y");
  280. return Vec3(_rotationX,_rotationY,_rotationZ_X);
  281. }
  282. void Node::updateRotationQuat()
  283. {
  284. // convert Euler angle to quaternion
  285. // when _rotationZ_X == _rotationZ_Y, _rotationQuat = RotationZ_X * RotationY * RotationX
  286. // when _rotationZ_X != _rotationZ_Y, _rotationQuat = RotationY * RotationX
  287. float halfRadx = CC_DEGREES_TO_RADIANS(_rotationX / 2.f), halfRady = CC_DEGREES_TO_RADIANS(_rotationY / 2.f), halfRadz = _rotationZ_X == _rotationZ_Y ? -CC_DEGREES_TO_RADIANS(_rotationZ_X / 2.f) : 0;
  288. float coshalfRadx = cosf(halfRadx), sinhalfRadx = sinf(halfRadx), coshalfRady = cosf(halfRady), sinhalfRady = sinf(halfRady), coshalfRadz = cosf(halfRadz), sinhalfRadz = sinf(halfRadz);
  289. _rotationQuat.x = sinhalfRadx * coshalfRady * coshalfRadz - coshalfRadx * sinhalfRady * sinhalfRadz;
  290. _rotationQuat.y = coshalfRadx * sinhalfRady * coshalfRadz + sinhalfRadx * coshalfRady * sinhalfRadz;
  291. _rotationQuat.z = coshalfRadx * coshalfRady * sinhalfRadz - sinhalfRadx * sinhalfRady * coshalfRadz;
  292. _rotationQuat.w = coshalfRadx * coshalfRady * coshalfRadz + sinhalfRadx * sinhalfRady * sinhalfRadz;
  293. }
  294. void Node::updateRotation3D()
  295. {
  296. //convert quaternion to Euler angle
  297. float x = _rotationQuat.x, y = _rotationQuat.y, z = _rotationQuat.z, w = _rotationQuat.w;
  298. _rotationX = atan2f(2.f * (w * x + y * z), 1.f - 2.f * (x * x + y * y));
  299. float sy = 2.f * (w * y - z * x);
  300. sy = clampf(sy, -1, 1);
  301. _rotationY = asinf(sy);
  302. _rotationZ_X = atan2f(2.f * (w * z + x * y), 1.f - 2.f * (y * y + z * z));
  303. _rotationX = CC_RADIANS_TO_DEGREES(_rotationX);
  304. _rotationY = CC_RADIANS_TO_DEGREES(_rotationY);
  305. _rotationZ_X = _rotationZ_Y = -CC_RADIANS_TO_DEGREES(_rotationZ_X);
  306. }
  307. void Node::setRotationQuat(const Quaternion& quat)
  308. {
  309. _rotationQuat = quat;
  310. updateRotation3D();
  311. _transformUpdated = _transformDirty = _inverseDirty = true;
  312. }
  313. Quaternion Node::getRotationQuat() const
  314. {
  315. return _rotationQuat;
  316. }
  317. void Node::setRotationSkewX(float rotationX)
  318. {
  319. if (_rotationZ_X == rotationX)
  320. return;
  321. _rotationZ_X = rotationX;
  322. _transformUpdated = _transformDirty = _inverseDirty = true;
  323. updateRotationQuat();
  324. }
  325. float Node::getRotationSkewY() const
  326. {
  327. return _rotationZ_Y;
  328. }
  329. void Node::setRotationSkewY(float rotationY)
  330. {
  331. if (_rotationZ_Y == rotationY)
  332. return;
  333. _rotationZ_Y = rotationY;
  334. _transformUpdated = _transformDirty = _inverseDirty = true;
  335. updateRotationQuat();
  336. }
  337. /// scale getter
  338. float Node::getScale(void) const
  339. {
  340. CCASSERT( _scaleX == _scaleY, "CCNode#scale. ScaleX != ScaleY. Don't know which one to return");
  341. return _scaleX;
  342. }
  343. /// scale setter
  344. void Node::setScale(float scale)
  345. {
  346. if (_scaleX == scale && _scaleY == scale && _scaleZ == scale)
  347. return;
  348. _scaleX = _scaleY = _scaleZ = scale;
  349. _transformUpdated = _transformDirty = _inverseDirty = true;
  350. }
  351. /// scaleX getter
  352. float Node::getScaleX() const
  353. {
  354. return _scaleX;
  355. }
  356. /// scale setter
  357. void Node::setScale(float scaleX,float scaleY)
  358. {
  359. if (_scaleX == scaleX && _scaleY == scaleY)
  360. return;
  361. _scaleX = scaleX;
  362. _scaleY = scaleY;
  363. _transformUpdated = _transformDirty = _inverseDirty = true;
  364. }
  365. /// scaleX setter
  366. void Node::setScaleX(float scaleX)
  367. {
  368. if (_scaleX == scaleX)
  369. return;
  370. _scaleX = scaleX;
  371. _transformUpdated = _transformDirty = _inverseDirty = true;
  372. }
  373. /// scaleY getter
  374. float Node::getScaleY() const
  375. {
  376. return _scaleY;
  377. }
  378. /// scaleY setter
  379. void Node::setScaleZ(float scaleZ)
  380. {
  381. if (_scaleZ == scaleZ)
  382. return;
  383. _scaleZ = scaleZ;
  384. _transformUpdated = _transformDirty = _inverseDirty = true;
  385. }
  386. /// scaleY getter
  387. float Node::getScaleZ() const
  388. {
  389. return _scaleZ;
  390. }
  391. /// scaleY setter
  392. void Node::setScaleY(float scaleY)
  393. {
  394. if (_scaleY == scaleY)
  395. return;
  396. _scaleY = scaleY;
  397. _transformUpdated = _transformDirty = _inverseDirty = true;
  398. }
  399. /// position getter
  400. const Vec2& Node::getPosition() const
  401. {
  402. return _position;
  403. }
  404. /// position setter
  405. void Node::setPosition(const Vec2& position)
  406. {
  407. setPosition(position.x, position.y);
  408. }
  409. void Node::getPosition(float* x, float* y) const
  410. {
  411. *x = _position.x;
  412. *y = _position.y;
  413. }
  414. void Node::setPosition(float x, float y)
  415. {
  416. if (_position.x == x && _position.y == y)
  417. return;
  418. _position.x = x;
  419. _position.y = y;
  420. _transformUpdated = _transformDirty = _inverseDirty = true;
  421. _usingNormalizedPosition = false;
  422. }
  423. void Node::setPosition3D(const Vec3& position)
  424. {
  425. setPositionZ(position.z);
  426. setPosition(position.x, position.y);
  427. }
  428. Vec3 Node::getPosition3D() const
  429. {
  430. return Vec3(_position.x, _position.y, _positionZ);
  431. }
  432. float Node::getPositionX() const
  433. {
  434. return _position.x;
  435. }
  436. void Node::setPositionX(float x)
  437. {
  438. setPosition(x, _position.y);
  439. }
  440. float Node::getPositionY() const
  441. {
  442. return _position.y;
  443. }
  444. void Node::setPositionY(float y)
  445. {
  446. setPosition(_position.x, y);
  447. }
  448. float Node::getPositionZ() const
  449. {
  450. return _positionZ;
  451. }
  452. void Node::setPositionZ(float positionZ)
  453. {
  454. if (_positionZ == positionZ)
  455. return;
  456. _transformUpdated = _transformDirty = _inverseDirty = true;
  457. _positionZ = positionZ;
  458. }
  459. /// position getter
  460. const Vec2& Node::getPositionNormalized() const
  461. {
  462. return _normalizedPosition;
  463. }
  464. /// position setter
  465. void Node::setPositionNormalized(const Vec2& position)
  466. {
  467. if (_normalizedPosition.equals(position))
  468. return;
  469. _normalizedPosition = position;
  470. _usingNormalizedPosition = true;
  471. _normalizedPositionDirty = true;
  472. _transformUpdated = _transformDirty = _inverseDirty = true;
  473. }
  474. ssize_t Node::getChildrenCount() const
  475. {
  476. return _children.size();
  477. }
  478. /// isVisible getter
  479. bool Node::isVisible() const
  480. {
  481. return _visible;
  482. }
  483. /// isVisible setter
  484. void Node::setVisible(bool visible)
  485. {
  486. if(visible != _visible)
  487. {
  488. _visible = visible;
  489. if(_visible)
  490. _transformUpdated = _transformDirty = _inverseDirty = true;
  491. }
  492. }
  493. const Vec2& Node::getAnchorPointInPoints() const
  494. {
  495. return _anchorPointInPoints;
  496. }
  497. /// anchorPoint getter
  498. const Vec2& Node::getAnchorPoint() const
  499. {
  500. return _anchorPoint;
  501. }
  502. void Node::setAnchorPoint(const Vec2& point)
  503. {
  504. if (! point.equals(_anchorPoint))
  505. {
  506. _anchorPoint = point;
  507. _anchorPointInPoints.set(_contentSize.width * _anchorPoint.x, _contentSize.height * _anchorPoint.y);
  508. _transformUpdated = _transformDirty = _inverseDirty = true;
  509. }
  510. }
  511. /// contentSize getter
  512. const Size& Node::getContentSize() const
  513. {
  514. return _contentSize;
  515. }
  516. void Node::setContentSize(const Size & size)
  517. {
  518. if (! size.equals(_contentSize))
  519. {
  520. _contentSize = size;
  521. _anchorPointInPoints.set(_contentSize.width * _anchorPoint.x, _contentSize.height * _anchorPoint.y);
  522. _transformUpdated = _transformDirty = _inverseDirty = _contentSizeDirty = true;
  523. }
  524. }
  525. // isRunning getter
  526. bool Node::isRunning() const
  527. {
  528. return _running;
  529. }
  530. /// parent setter
  531. void Node::setParent(Node * parent)
  532. {
  533. _parent = parent;
  534. _transformUpdated = _transformDirty = _inverseDirty = true;
  535. }
  536. /// isRelativeAnchorPoint getter
  537. bool Node::isIgnoreAnchorPointForPosition() const
  538. {
  539. return _ignoreAnchorPointForPosition;
  540. }
  541. /// isRelativeAnchorPoint setter
  542. void Node::setIgnoreAnchorPointForPosition(bool newValue)
  543. {
  544. if (newValue != _ignoreAnchorPointForPosition)
  545. {
  546. _ignoreAnchorPointForPosition = newValue;
  547. _transformUpdated = _transformDirty = _inverseDirty = true;
  548. }
  549. }
  550. /// tag getter
  551. int Node::getTag() const
  552. {
  553. return _tag;
  554. }
  555. /// tag setter
  556. void Node::setTag(int tag)
  557. {
  558. _tag = tag ;
  559. }
  560. const std::string& Node::getName() const
  561. {
  562. return _name;
  563. }
  564. void Node::setName(const std::string& name)
  565. {
  566. _name = name;
  567. std::hash<std::string> h;
  568. _hashOfName = h(name);
  569. }
  570. /// userData setter
  571. void Node::setUserData(void *userData)
  572. {
  573. _userData = userData;
  574. }
  575. void Node::setUserObject(Ref* userObject)
  576. {
  577. #if CC_ENABLE_GC_FOR_NATIVE_OBJECTS
  578. auto sEngine = ScriptEngineManager::getInstance()->getScriptEngine();
  579. if (sEngine)
  580. {
  581. if (userObject)
  582. sEngine->retainScriptObject(this, userObject);
  583. if (_userObject)
  584. sEngine->releaseScriptObject(this, _userObject);
  585. }
  586. #endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS
  587. CC_SAFE_RETAIN(userObject);
  588. CC_SAFE_RELEASE(_userObject);
  589. _userObject = userObject;
  590. }
  591. GLProgramState* Node::getGLProgramState() const
  592. {
  593. return _glProgramState;
  594. }
  595. void Node::setGLProgramState(cocos2d::GLProgramState* glProgramState)
  596. {
  597. if (glProgramState != _glProgramState)
  598. {
  599. CC_SAFE_RELEASE(_glProgramState);
  600. _glProgramState = glProgramState;
  601. CC_SAFE_RETAIN(_glProgramState);
  602. if (_glProgramState)
  603. _glProgramState->setNodeBinding(this);
  604. }
  605. }
  606. void Node::setGLProgram(GLProgram* glProgram)
  607. {
  608. if (_glProgramState == nullptr || (_glProgramState && _glProgramState->getGLProgram() != glProgram))
  609. {
  610. CC_SAFE_RELEASE(_glProgramState);
  611. _glProgramState = GLProgramState::getOrCreateWithGLProgram(glProgram);
  612. _glProgramState->retain();
  613. _glProgramState->setNodeBinding(this);
  614. }
  615. }
  616. GLProgram * Node::getGLProgram() const
  617. {
  618. return _glProgramState ? _glProgramState->getGLProgram() : nullptr;
  619. }
  620. Scene* Node::getScene() const
  621. {
  622. if (!_parent)
  623. return nullptr;
  624. auto sceneNode = _parent;
  625. while (sceneNode->_parent)
  626. {
  627. sceneNode = sceneNode->_parent;
  628. }
  629. return dynamic_cast<Scene*>(sceneNode);
  630. }
  631. Rect Node::getBoundingBox() const
  632. {
  633. Rect rect(0, 0, _contentSize.width, _contentSize.height);
  634. return RectApplyAffineTransform(rect, getNodeToParentAffineTransform());
  635. }
  636. // MARK: Children logic
  637. // lazy allocs
  638. void Node::childrenAlloc()
  639. {
  640. _children.reserve(4);
  641. }
  642. Node* Node::getChildByTag(int tag) const
  643. {
  644. CCASSERT(tag != Node::INVALID_TAG, "Invalid tag");
  645. for (const auto child : _children)
  646. {
  647. if(child && child->_tag == tag)
  648. return child;
  649. }
  650. return nullptr;
  651. }
  652. Node* Node::getChildByName(const std::string& name) const
  653. {
  654. CCASSERT(!name.empty(), "Invalid name");
  655. std::hash<std::string> h;
  656. size_t hash = h(name);
  657. for (const auto& child : _children)
  658. {
  659. // Different strings may have the same hash code, but can use it to compare first for speed
  660. if(child->_hashOfName == hash && child->_name.compare(name) == 0)
  661. return child;
  662. }
  663. return nullptr;
  664. }
  665. void Node::enumerateChildren(const std::string &name, std::function<bool (Node *)> callback) const
  666. {
  667. CCASSERT(!name.empty(), "Invalid name");
  668. CCASSERT(callback != nullptr, "Invalid callback function");
  669. size_t length = name.length();
  670. size_t subStrStartPos = 0; // sub string start index
  671. size_t subStrlength = length; // sub string length
  672. // Starts with '//'?
  673. bool searchRecursively = false;
  674. if (length > 2 && name[0] == '/' && name[1] == '/')
  675. {
  676. searchRecursively = true;
  677. subStrStartPos = 2;
  678. subStrlength -= 2;
  679. }
  680. // End with '/..'?
  681. bool searchFromParent = false;
  682. if (length > 3 &&
  683. name[length-3] == '/' &&
  684. name[length-2] == '.' &&
  685. name[length-1] == '.')
  686. {
  687. searchFromParent = true;
  688. subStrlength -= 3;
  689. }
  690. // Remove '//', '/..' if exist
  691. std::string newName = name.substr(subStrStartPos, subStrlength);
  692. if (searchFromParent)
  693. {
  694. newName.insert(0, "[[:alnum:]]+/");
  695. }
  696. if (searchRecursively)
  697. {
  698. // name is '//xxx'
  699. doEnumerateRecursive(this, newName, callback);
  700. }
  701. else
  702. {
  703. // name is xxx
  704. doEnumerate(newName, callback);
  705. }
  706. }
  707. bool Node::doEnumerateRecursive(const Node* node, const std::string &name, std::function<bool (Node *)> callback) const
  708. {
  709. bool ret =false;
  710. if (node->doEnumerate(name, callback))
  711. {
  712. // search itself
  713. ret = true;
  714. }
  715. else
  716. {
  717. // search its children
  718. for (const auto& child : node->getChildren())
  719. {
  720. if (doEnumerateRecursive(child, name, callback))
  721. {
  722. ret = true;
  723. break;
  724. }
  725. }
  726. }
  727. return ret;
  728. }
  729. bool Node::doEnumerate(std::string name, std::function<bool (Node *)> callback) const
  730. {
  731. // name may be xxx/yyy, should find its parent
  732. size_t pos = name.find('/');
  733. std::string searchName = name;
  734. bool needRecursive = false;
  735. if (pos != name.npos)
  736. {
  737. searchName = name.substr(0, pos);
  738. name.erase(0, pos+1);
  739. needRecursive = true;
  740. }
  741. bool ret = false;
  742. for (const auto& child : getChildren())
  743. {
  744. if (std::regex_match(child->_name, std::regex(searchName)))
  745. {
  746. if (!needRecursive)
  747. {
  748. // terminate enumeration if callback return true
  749. if (callback(child))
  750. {
  751. ret = true;
  752. break;
  753. }
  754. }
  755. else
  756. {
  757. ret = child->doEnumerate(name, callback);
  758. if (ret)
  759. break;
  760. }
  761. }
  762. }
  763. return ret;
  764. }
  765. /* "add" logic MUST only be on this method
  766. * If a class want's to extend the 'addChild' behavior it only needs
  767. * to override this method
  768. */
  769. void Node::addChild(Node *child, int localZOrder, int tag)
  770. {
  771. CCASSERT( child != nullptr, "Argument must be non-nil");
  772. CCASSERT( child->_parent == nullptr, "child already added. It can't be added again");
  773. addChildHelper(child, localZOrder, tag, "", true);
  774. }
  775. void Node::addChild(Node* child, int localZOrder, const std::string &name)
  776. {
  777. CCASSERT(child != nullptr, "Argument must be non-nil");
  778. CCASSERT(child->_parent == nullptr, "child already added. It can't be added again");
  779. addChildHelper(child, localZOrder, INVALID_TAG, name, false);
  780. }
  781. void Node::addChildHelper(Node* child, int localZOrder, int tag, const std::string &name, bool setTag)
  782. {
  783. auto assertNotSelfChild
  784. ( [ this, child ]() -> bool
  785. {
  786. for ( Node* parent( getParent() ); parent != nullptr;
  787. parent = parent->getParent() )
  788. if ( parent == child )
  789. return false;
  790. return true;
  791. } );
  792. (void)assertNotSelfChild;
  793. CCASSERT( assertNotSelfChild(),
  794. "A node cannot be the child of his own children" );
  795. if (_children.empty())
  796. {
  797. this->childrenAlloc();
  798. }
  799. this->insertChild(child, localZOrder);
  800. if (setTag)
  801. child->setTag(tag);
  802. else
  803. child->setName(name);
  804. child->setParent(this);
  805. child->updateOrderOfArrival();
  806. if( _running )
  807. {
  808. child->onEnter();
  809. // prevent onEnterTransitionDidFinish to be called twice when a node is added in onEnter
  810. if (_isTransitionFinished)
  811. {
  812. child->onEnterTransitionDidFinish();
  813. }
  814. }
  815. if (_cascadeColorEnabled)
  816. {
  817. updateCascadeColor();
  818. }
  819. if (_cascadeOpacityEnabled)
  820. {
  821. updateCascadeOpacity();
  822. }
  823. }
  824. void Node::addChild(Node *child, int zOrder)
  825. {
  826. CCASSERT( child != nullptr, "Argument must be non-nil");
  827. this->addChild(child, zOrder, child->_name);
  828. }
  829. void Node::addChild(Node *child)
  830. {
  831. CCASSERT( child != nullptr, "Argument must be non-nil");
  832. this->addChild(child, child->getLocalZOrder(), child->_name);
  833. }
  834. void Node::removeFromParent()
  835. {
  836. this->removeFromParentAndCleanup(true);
  837. }
  838. void Node::removeFromParentAndCleanup(bool cleanup)
  839. {
  840. if (_parent != nullptr)
  841. {
  842. _parent->removeChild(this,cleanup);
  843. }
  844. }
  845. /* "remove" logic MUST only be on this method
  846. * If a class want's to extend the 'removeChild' behavior it only needs
  847. * to override this method
  848. */
  849. void Node::removeChild(Node* child, bool cleanup /* = true */)
  850. {
  851. // explicit nil handling
  852. if (_children.empty())
  853. {
  854. return;
  855. }
  856. ssize_t index = _children.getIndex(child);
  857. if( index != CC_INVALID_INDEX )
  858. this->detachChild( child, index, cleanup );
  859. }
  860. void Node::removeChildByTag(int tag, bool cleanup/* = true */)
  861. {
  862. CCASSERT( tag != Node::INVALID_TAG, "Invalid tag");
  863. Node *child = this->getChildByTag(tag);
  864. if (child == nullptr)
  865. {
  866. CCLOG("cocos2d: removeChildByTag(tag = %d): child not found!", tag);
  867. }
  868. else
  869. {
  870. this->removeChild(child, cleanup);
  871. }
  872. }
  873. void Node::removeChildByName(const std::string &name, bool cleanup)
  874. {
  875. CCASSERT(!name.empty(), "Invalid name");
  876. Node *child = this->getChildByName(name);
  877. if (child == nullptr)
  878. {
  879. CCLOG("cocos2d: removeChildByName(name = %s): child not found!", name.c_str());
  880. }
  881. else
  882. {
  883. this->removeChild(child, cleanup);
  884. }
  885. }
  886. void Node::removeAllChildren()
  887. {
  888. this->removeAllChildrenWithCleanup(true);
  889. }
  890. void Node::removeAllChildrenWithCleanup(bool cleanup)
  891. {
  892. // not using detachChild improves speed here
  893. for (const auto& child : _children)
  894. {
  895. // IMPORTANT:
  896. // -1st do onExit
  897. // -2nd cleanup
  898. if(_running)
  899. {
  900. child->onExitTransitionDidStart();
  901. child->onExit();
  902. }
  903. if (cleanup)
  904. {
  905. child->cleanup();
  906. }
  907. #if CC_ENABLE_GC_FOR_NATIVE_OBJECTS
  908. auto sEngine = ScriptEngineManager::getInstance()->getScriptEngine();
  909. if (sEngine)
  910. {
  911. sEngine->releaseScriptObject(this, child);
  912. }
  913. #endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS
  914. // set parent nil at the end
  915. child->setParent(nullptr);
  916. }
  917. _children.clear();
  918. }
  919. void Node::detachChild(Node *child, ssize_t childIndex, bool doCleanup)
  920. {
  921. // IMPORTANT:
  922. // -1st do onExit
  923. // -2nd cleanup
  924. if (_running)
  925. {
  926. child->onExitTransitionDidStart();
  927. child->onExit();
  928. }
  929. // If you don't do cleanup, the child's actions will not get removed and the
  930. // its scheduledSelectors_ dict will not get released!
  931. if (doCleanup)
  932. {
  933. child->cleanup();
  934. }
  935. #if CC_ENABLE_GC_FOR_NATIVE_OBJECTS
  936. auto sEngine = ScriptEngineManager::getInstance()->getScriptEngine();
  937. if (sEngine)
  938. {
  939. sEngine->releaseScriptObject(this, child);
  940. }
  941. #endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS
  942. // set parent nil at the end
  943. child->setParent(nullptr);
  944. _children.erase(childIndex);
  945. }
  946. // helper used by reorderChild & add
  947. void Node::insertChild(Node* child, int z)
  948. {
  949. #if CC_ENABLE_GC_FOR_NATIVE_OBJECTS
  950. auto sEngine = ScriptEngineManager::getInstance()->getScriptEngine();
  951. if (sEngine)
  952. {
  953. sEngine->retainScriptObject(this, child);
  954. }
  955. #endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS
  956. _transformUpdated = true;
  957. _reorderChildDirty = true;
  958. _children.pushBack(child);
  959. child->_setLocalZOrder(z);
  960. }
  961. void Node::reorderChild(Node *child, int zOrder)
  962. {
  963. CCASSERT( child != nullptr, "Child must be non-nil");
  964. _reorderChildDirty = true;
  965. child->updateOrderOfArrival();
  966. child->_setLocalZOrder(zOrder);
  967. }
  968. void Node::sortAllChildren()
  969. {
  970. if (_reorderChildDirty)
  971. {
  972. sortNodes(_children);
  973. _reorderChildDirty = false;
  974. _eventDispatcher->setDirtyForNode(this);
  975. }
  976. }
  977. // MARK: draw / visit
  978. void Node::draw()
  979. {
  980. auto renderer = _director->getRenderer();
  981. draw(renderer, _modelViewTransform, true);
  982. }
  983. void Node::draw(Renderer* /*renderer*/, const Mat4 & /*transform*/, uint32_t /*flags*/)
  984. {
  985. }
  986. void Node::visit()
  987. {
  988. auto renderer = _director->getRenderer();
  989. auto& parentTransform = _director->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
  990. visit(renderer, parentTransform, true);
  991. }
  992. uint32_t Node::processParentFlags(const Mat4& parentTransform, uint32_t parentFlags)
  993. {
  994. if(_usingNormalizedPosition)
  995. {
  996. CCASSERT(_parent, "setPositionNormalized() doesn't work with orphan nodes");
  997. if ((parentFlags & FLAGS_CONTENT_SIZE_DIRTY) || _normalizedPositionDirty)
  998. {
  999. auto& s = _parent->getContentSize();
  1000. _position.x = _normalizedPosition.x * s.width;
  1001. _position.y = _normalizedPosition.y * s.height;
  1002. _transformUpdated = _transformDirty = _inverseDirty = true;
  1003. _normalizedPositionDirty = false;
  1004. }
  1005. }
  1006. // Fixes Github issue #16100. Basically when having two cameras, one camera might set as dirty the
  1007. // node that is not visited by it, and might affect certain calculations. Besides, it is faster to do this.
  1008. if (!isVisitableByVisitingCamera())
  1009. return parentFlags;
  1010. uint32_t flags = parentFlags;
  1011. flags |= (_transformUpdated ? FLAGS_TRANSFORM_DIRTY : 0);
  1012. flags |= (_contentSizeDirty ? FLAGS_CONTENT_SIZE_DIRTY : 0);
  1013. if(flags & FLAGS_DIRTY_MASK)
  1014. _modelViewTransform = this->transform(parentTransform);
  1015. _transformUpdated = false;
  1016. _contentSizeDirty = false;
  1017. return flags;
  1018. }
  1019. bool Node::isVisitableByVisitingCamera() const
  1020. {
  1021. auto camera = Camera::getVisitingCamera();
  1022. bool visibleByCamera = camera ? ((unsigned short)camera->getCameraFlag() & _cameraMask) != 0 : true;
  1023. return visibleByCamera;
  1024. }
  1025. void Node::visit(Renderer* renderer, const Mat4 &parentTransform, uint32_t parentFlags)
  1026. {
  1027. // quick return if not visible. children won't be drawn.
  1028. if (!_visible)
  1029. {
  1030. return;
  1031. }
  1032. uint32_t flags = processParentFlags(parentTransform, parentFlags);
  1033. // IMPORTANT:
  1034. // To ease the migration to v3.0, we still support the Mat4 stack,
  1035. // but it is deprecated and your code should not rely on it
  1036. _director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
  1037. _director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW, _modelViewTransform);
  1038. bool visibleByCamera = isVisitableByVisitingCamera();
  1039. int i = 0;
  1040. if(!_children.empty())
  1041. {
  1042. sortAllChildren();
  1043. // draw children zOrder < 0
  1044. for(auto size = _children.size(); i < size; ++i)
  1045. {
  1046. auto node = _children.at(i);
  1047. if (node && node->_localZOrder < 0)
  1048. node->visit(renderer, _modelViewTransform, flags);
  1049. else
  1050. break;
  1051. }
  1052. // self draw
  1053. if (visibleByCamera)
  1054. this->draw(renderer, _modelViewTransform, flags);
  1055. for(auto it=_children.cbegin()+i, itCend = _children.cend(); it != itCend; ++it)
  1056. (*it)->visit(renderer, _modelViewTransform, flags);
  1057. }
  1058. else if (visibleByCamera)
  1059. {
  1060. this->draw(renderer, _modelViewTransform, flags);
  1061. }
  1062. _director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
  1063. // FIX ME: Why need to set _orderOfArrival to 0??
  1064. // Please refer to https://github.com/cocos2d/cocos2d-x/pull/6920
  1065. // reset for next frame
  1066. // _orderOfArrival = 0;
  1067. }
  1068. Mat4 Node::transform(const Mat4& parentTransform)
  1069. {
  1070. return parentTransform * this->getNodeToParentTransform();
  1071. }
  1072. // MARK: events
  1073. void Node::onEnter()
  1074. {
  1075. if (!_running)
  1076. {
  1077. ++__attachedNodeCount;
  1078. }
  1079. #if CC_ENABLE_SCRIPT_BINDING
  1080. if (_scriptType == kScriptTypeJavascript)
  1081. {
  1082. if (ScriptEngineManager::sendNodeEventToJS(this, kNodeOnEnter))
  1083. return;
  1084. }
  1085. #endif
  1086. if (_onEnterCallback)
  1087. _onEnterCallback();
  1088. if (_componentContainer && !_componentContainer->isEmpty())
  1089. {
  1090. _componentContainer->onEnter();
  1091. }
  1092. _isTransitionFinished = false;
  1093. for( const auto &child: _children)
  1094. child->onEnter();
  1095. this->resume();
  1096. _running = true;
  1097. #if CC_ENABLE_SCRIPT_BINDING
  1098. if (_scriptType == kScriptTypeLua)
  1099. {
  1100. ScriptEngineManager::sendNodeEventToLua(this, kNodeOnEnter);
  1101. }
  1102. #endif
  1103. }
  1104. void Node::onEnterTransitionDidFinish()
  1105. {
  1106. #if CC_ENABLE_SCRIPT_BINDING
  1107. if (_scriptType == kScriptTypeJavascript)
  1108. {
  1109. if (ScriptEngineManager::sendNodeEventToJS(this, kNodeOnEnterTransitionDidFinish))
  1110. return;
  1111. }
  1112. #endif
  1113. if (_onEnterTransitionDidFinishCallback)
  1114. _onEnterTransitionDidFinishCallback();
  1115. _isTransitionFinished = true;
  1116. for( const auto &child: _children)
  1117. child->onEnterTransitionDidFinish();
  1118. #if CC_ENABLE_SCRIPT_BINDING
  1119. if (_scriptType == kScriptTypeLua)
  1120. {
  1121. ScriptEngineManager::sendNodeEventToLua(this, kNodeOnEnterTransitionDidFinish);
  1122. }
  1123. #endif
  1124. }
  1125. void Node::onExitTransitionDidStart()
  1126. {
  1127. #if CC_ENABLE_SCRIPT_BINDING
  1128. if (_scriptType == kScriptTypeJavascript)
  1129. {
  1130. if (ScriptEngineManager::sendNodeEventToJS(this, kNodeOnExitTransitionDidStart))
  1131. return;
  1132. }
  1133. #endif
  1134. if (_onExitTransitionDidStartCallback)
  1135. _onExitTransitionDidStartCallback();
  1136. for( const auto &child: _children)
  1137. child->onExitTransitionDidStart();
  1138. #if CC_ENABLE_SCRIPT_BINDING
  1139. if (_scriptType == kScriptTypeLua)
  1140. {
  1141. ScriptEngineManager::sendNodeEventToLua(this, kNodeOnExitTransitionDidStart);
  1142. }
  1143. #endif
  1144. }
  1145. void Node::onExit()
  1146. {
  1147. if (_running)
  1148. {
  1149. --__attachedNodeCount;
  1150. }
  1151. #if CC_ENABLE_SCRIPT_BINDING
  1152. if (_scriptType == kScriptTypeJavascript)
  1153. {
  1154. if (ScriptEngineManager::sendNodeEventToJS(this, kNodeOnExit))
  1155. return;
  1156. }
  1157. #endif
  1158. if (_onExitCallback)
  1159. _onExitCallback();
  1160. if (_componentContainer && !_componentContainer->isEmpty())
  1161. {
  1162. _componentContainer->onExit();
  1163. }
  1164. this->pause();
  1165. _running = false;
  1166. for( const auto &child: _children)
  1167. child->onExit();
  1168. #if CC_ENABLE_SCRIPT_BINDING
  1169. if (_scriptType == kScriptTypeLua)
  1170. {
  1171. ScriptEngineManager::sendNodeEventToLua(this, kNodeOnExit);
  1172. }
  1173. #endif
  1174. }
  1175. void Node::setEventDispatcher(EventDispatcher* dispatcher)
  1176. {
  1177. if (dispatcher != _eventDispatcher)
  1178. {
  1179. _eventDispatcher->removeEventListenersForTarget(this);
  1180. CC_SAFE_RETAIN(dispatcher);
  1181. CC_SAFE_RELEASE(_eventDispatcher);
  1182. _eventDispatcher = dispatcher;
  1183. }
  1184. }
  1185. void Node::setActionManager(ActionManager* actionManager)
  1186. {
  1187. if( actionManager != _actionManager )
  1188. {
  1189. this->stopAllActions();
  1190. CC_SAFE_RETAIN(actionManager);
  1191. CC_SAFE_RELEASE(_actionManager);
  1192. _actionManager = actionManager;
  1193. }
  1194. }
  1195. // MARK: actions
  1196. Action * Node::runAction(Action* action)
  1197. {
  1198. CCASSERT( action != nullptr, "Argument must be non-nil");
  1199. _actionManager->addAction(action, this, !_running);
  1200. return action;
  1201. }
  1202. void Node::stopAllActions()
  1203. {
  1204. _actionManager->removeAllActionsFromTarget(this);
  1205. }
  1206. void Node::stopAction(Action* action)
  1207. {
  1208. _actionManager->removeAction(action);
  1209. }
  1210. void Node::stopActionByTag(int tag)
  1211. {
  1212. CCASSERT( tag != Action::INVALID_TAG, "Invalid tag");
  1213. _actionManager->removeActionByTag(tag, this);
  1214. }
  1215. void Node::stopAllActionsByTag(int tag)
  1216. {
  1217. CCASSERT( tag != Action::INVALID_TAG, "Invalid tag");
  1218. _actionManager->removeAllActionsByTag(tag, this);
  1219. }
  1220. void Node::stopActionsByFlags(unsigned int flags)
  1221. {
  1222. if (flags > 0)
  1223. {
  1224. _actionManager->removeActionsByFlags(flags, this);
  1225. }
  1226. }
  1227. Action * Node::getActionByTag(int tag)
  1228. {
  1229. CCASSERT( tag != Action::INVALID_TAG, "Invalid tag");
  1230. return _actionManager->getActionByTag(tag, this);
  1231. }
  1232. ssize_t Node::getNumberOfRunningActions() const
  1233. {
  1234. return _actionManager->getNumberOfRunningActionsInTarget(this);
  1235. }
  1236. ssize_t Node::getNumberOfRunningActionsByTag(int tag) const
  1237. {
  1238. return _actionManager->getNumberOfRunningActionsInTargetByTag(this, tag);
  1239. }
  1240. // MARK: Callbacks
  1241. void Node::setScheduler(Scheduler* scheduler)
  1242. {
  1243. if( scheduler != _scheduler )
  1244. {
  1245. this->unscheduleAllCallbacks();
  1246. CC_SAFE_RETAIN(scheduler);
  1247. CC_SAFE_RELEASE(_scheduler);
  1248. _scheduler = scheduler;
  1249. }
  1250. }
  1251. bool Node::isScheduled(SEL_SCHEDULE selector)
  1252. {
  1253. return _scheduler->isScheduled(selector, this);
  1254. }
  1255. bool Node::isScheduled(const std::string &key)
  1256. {
  1257. return _scheduler->isScheduled(key, this);
  1258. }
  1259. void Node::scheduleUpdate()
  1260. {
  1261. scheduleUpdateWithPriority(0);
  1262. }
  1263. void Node::scheduleUpdateWithPriority(int priority)
  1264. {
  1265. _scheduler->scheduleUpdate(this, priority, !_running);
  1266. }
  1267. void Node::scheduleUpdateWithPriorityLua(int nHandler, int priority)
  1268. {
  1269. unscheduleUpdate();
  1270. #if CC_ENABLE_SCRIPT_BINDING
  1271. _updateScriptHandler = nHandler;
  1272. #endif
  1273. _scheduler->scheduleUpdate(this, priority, !_running);
  1274. }
  1275. void Node::unscheduleUpdate()
  1276. {
  1277. _scheduler->unscheduleUpdate(this);
  1278. #if CC_ENABLE_SCRIPT_BINDING
  1279. if (_updateScriptHandler)
  1280. {
  1281. ScriptEngineManager::getInstance()->getScriptEngine()->removeScriptHandler(_updateScriptHandler);
  1282. _updateScriptHandler = 0;
  1283. }
  1284. #endif
  1285. }
  1286. void Node::schedule(SEL_SCHEDULE selector)
  1287. {
  1288. this->schedule(selector, 0.0f, CC_REPEAT_FOREVER, 0.0f);
  1289. }
  1290. void Node::schedule(SEL_SCHEDULE selector, float interval)
  1291. {
  1292. this->schedule(selector, interval, CC_REPEAT_FOREVER, 0.0f);
  1293. }
  1294. void Node::schedule(SEL_SCHEDULE selector, float interval, unsigned int repeat, float delay)
  1295. {
  1296. CCASSERT( selector, "Argument must be non-nil");
  1297. CCASSERT( interval >=0, "Argument must be positive");
  1298. _scheduler->schedule(selector, this, interval , repeat, delay, !_running);
  1299. }
  1300. void Node::schedule(const std::function<void(float)> &callback, const std::string &key)
  1301. {
  1302. _scheduler->schedule(callback, this, 0, !_running, key);
  1303. }
  1304. void Node::schedule(const std::function<void(float)> &callback, float interval, const std::string &key)
  1305. {
  1306. _scheduler->schedule(callback, this, interval, !_running, key);
  1307. }
  1308. void Node::schedule(const std::function<void(float)>& callback, float interval, unsigned int repeat, float delay, const std::string &key)
  1309. {
  1310. _scheduler->schedule(callback, this, interval, repeat, delay, !_running, key);
  1311. }
  1312. void Node::scheduleOnce(SEL_SCHEDULE selector, float delay)
  1313. {
  1314. this->schedule(selector, 0.0f, 0, delay);
  1315. }
  1316. void Node::scheduleOnce(const std::function<void(float)> &callback, float delay, const std::string &key)
  1317. {
  1318. _scheduler->schedule(callback, this, 0, 0, delay, !_running, key);
  1319. }
  1320. void Node::unschedule(SEL_SCHEDULE selector)
  1321. {
  1322. // explicit null handling
  1323. if (selector == nullptr)
  1324. return;
  1325. _scheduler->unschedule(selector, this);
  1326. }
  1327. void Node::unschedule(const std::string &key)
  1328. {
  1329. _scheduler->unschedule(key, this);
  1330. }
  1331. void Node::unscheduleAllCallbacks()
  1332. {
  1333. _scheduler->unscheduleAllForTarget(this);
  1334. }
  1335. void Node::resume()
  1336. {
  1337. _scheduler->resumeTarget(this);
  1338. _actionManager->resumeTarget(this);
  1339. _eventDispatcher->resumeEventListenersForTarget(this);
  1340. }
  1341. void Node::pause()
  1342. {
  1343. _scheduler->pauseTarget(this);
  1344. _actionManager->pauseTarget(this);
  1345. _eventDispatcher->pauseEventListenersForTarget(this);
  1346. }
  1347. void Node::resumeSchedulerAndActions()
  1348. {
  1349. resume();
  1350. }
  1351. void Node::pauseSchedulerAndActions()
  1352. {
  1353. pause();
  1354. }
  1355. // override me
  1356. void Node::update(float fDelta)
  1357. {
  1358. #if CC_ENABLE_SCRIPT_BINDING
  1359. if (0 != _updateScriptHandler)
  1360. {
  1361. //only lua use
  1362. SchedulerScriptData data(_updateScriptHandler,fDelta);
  1363. ScriptEvent event(kScheduleEvent,&data);
  1364. ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&event);
  1365. }
  1366. #endif
  1367. if (_componentContainer && !_componentContainer->isEmpty())
  1368. {
  1369. _componentContainer->visit(fDelta);
  1370. }
  1371. }
  1372. // MARK: coordinates
  1373. AffineTransform Node::getNodeToParentAffineTransform() const
  1374. {
  1375. AffineTransform ret;
  1376. GLToCGAffine(getNodeToParentTransform().m, &ret);
  1377. return ret;
  1378. }
  1379. Mat4 Node::getNodeToParentTransform(Node* ancestor) const
  1380. {
  1381. Mat4 t(this->getNodeToParentTransform());
  1382. for (Node *p = _parent; p != nullptr && p != ancestor ; p = p->getParent())
  1383. {
  1384. t = p->getNodeToParentTransform() * t;
  1385. }
  1386. return t;
  1387. }
  1388. AffineTransform Node::getNodeToParentAffineTransform(Node* ancestor) const
  1389. {
  1390. AffineTransform t(this->getNodeToParentAffineTransform());
  1391. for (Node *p = _parent; p != nullptr && p != ancestor; p = p->getParent())
  1392. t = AffineTransformConcat(t, p->getNodeToParentAffineTransform());
  1393. return t;
  1394. }
  1395. const Mat4& Node::getNodeToParentTransform() const
  1396. {
  1397. if (_transformDirty)
  1398. {
  1399. // Translate values
  1400. float x = _position.x;
  1401. float y = _position.y;
  1402. float z = _positionZ;
  1403. if (_ignoreAnchorPointForPosition)
  1404. {
  1405. x += _anchorPointInPoints.x;
  1406. y += _anchorPointInPoints.y;
  1407. }
  1408. bool needsSkewMatrix = ( _skewX || _skewY );
  1409. // Build Transform Matrix = translation * rotation * scale
  1410. Mat4 translation;
  1411. //move to anchor point first, then rotate
  1412. Mat4::createTranslation(x, y, z, &translation);
  1413. Mat4::createRotation(_rotationQuat, &_transform);
  1414. if (_rotationZ_X != _rotationZ_Y)
  1415. {
  1416. // Rotation values
  1417. // Change rotation code to handle X and Y
  1418. // If we skew with the exact same value for both x and y then we're simply just rotating
  1419. float radiansX = -CC_DEGREES_TO_RADIANS(_rotationZ_X);
  1420. float radiansY = -CC_DEGREES_TO_RADIANS(_rotationZ_Y);
  1421. float cx = cosf(radiansX);
  1422. float sx = sinf(radiansX);
  1423. float cy = cosf(radiansY);
  1424. float sy = sinf(radiansY);
  1425. float m0 = _transform.m[0], m1 = _transform.m[1], m4 = _transform.m[4], m5 = _transform.m[5], m8 = _transform.m[8], m9 = _transform.m[9];
  1426. _transform.m[0] = cy * m0 - sx * m1, _transform.m[4] = cy * m4 - sx * m5, _transform.m[8] = cy * m8 - sx * m9;
  1427. _transform.m[1] = sy * m0 + cx * m1, _transform.m[5] = sy * m4 + cx * m5, _transform.m[9] = sy * m8 + cx * m9;
  1428. }
  1429. _transform = translation * _transform;
  1430. if (_scaleX != 1.f)
  1431. {
  1432. _transform.m[0] *= _scaleX, _transform.m[1] *= _scaleX, _transform.m[2] *= _scaleX;
  1433. }
  1434. if (_scaleY != 1.f)
  1435. {
  1436. _transform.m[4] *= _scaleY, _transform.m[5] *= _scaleY, _transform.m[6] *= _scaleY;
  1437. }
  1438. if (_scaleZ != 1.f)
  1439. {
  1440. _transform.m[8] *= _scaleZ, _transform.m[9] *= _scaleZ, _transform.m[10] *= _scaleZ;
  1441. }
  1442. // FIXME:: Try to inline skew
  1443. // If skew is needed, apply skew and then anchor point
  1444. if (needsSkewMatrix)
  1445. {
  1446. float skewMatArray[16] =
  1447. {
  1448. 1, (float)tanf(CC_DEGREES_TO_RADIANS(_skewY)), 0, 0,
  1449. (float)tanf(CC_DEGREES_TO_RADIANS(_skewX)), 1, 0, 0,
  1450. 0, 0, 1, 0,
  1451. 0, 0, 0, 1
  1452. };
  1453. Mat4 skewMatrix(skewMatArray);
  1454. _transform = _transform * skewMatrix;
  1455. }
  1456. // adjust anchor point
  1457. if (!_anchorPointInPoints.isZero())
  1458. {
  1459. // FIXME:: Argh, Mat4 needs a "translate" method.
  1460. // FIXME:: Although this is faster than multiplying a vec4 * mat4
  1461. _transform.m[12] += _transform.m[0] * -_anchorPointInPoints.x + _transform.m[4] * -_anchorPointInPoints.y;
  1462. _transform.m[13] += _transform.m[1] * -_anchorPointInPoints.x + _transform.m[5] * -_anchorPointInPoints.y;
  1463. _transform.m[14] += _transform.m[2] * -_anchorPointInPoints.x + _transform.m[6] * -_anchorPointInPoints.y;
  1464. }
  1465. }
  1466. if (_additionalTransform)
  1467. {
  1468. // This is needed to support both Node::setNodeToParentTransform() and Node::setAdditionalTransform()
  1469. // at the same time. The scenario is this:
  1470. // at some point setNodeToParentTransform() is called.
  1471. // and later setAdditionalTransform() is called every time. And since _transform
  1472. // is being overwritten everyframe, _additionalTransform[1] is used to have a copy
  1473. // of the last "_transform without _additionalTransform"
  1474. if (_transformDirty)
  1475. _additionalTransform[1] = _transform;
  1476. if (_transformUpdated)
  1477. _transform = _additionalTransform[1] * _additionalTransform[0];
  1478. }
  1479. _transformDirty = _additionalTransformDirty = false;
  1480. return _transform;
  1481. }
  1482. void Node::setNodeToParentTransform(const Mat4& transform)
  1483. {
  1484. _transform = transform;
  1485. _transformDirty = false;
  1486. _transformUpdated = true;
  1487. if (_additionalTransform)
  1488. // _additionalTransform[1] has a copy of lastest transform
  1489. _additionalTransform[1] = transform;
  1490. }
  1491. void Node::setAdditionalTransform(const AffineTransform& additionalTransform)
  1492. {
  1493. Mat4 tmp;
  1494. CGAffineToGL(additionalTransform, tmp.m);
  1495. setAdditionalTransform(&tmp);
  1496. }
  1497. void Node::setAdditionalTransform(const Mat4* additionalTransform)
  1498. {
  1499. if (additionalTransform == nullptr)
  1500. {
  1501. delete[] _additionalTransform;
  1502. _additionalTransform = nullptr;
  1503. }
  1504. else
  1505. {
  1506. if (!_additionalTransform) {
  1507. _additionalTransform = new Mat4[2];
  1508. // _additionalTransform[1] is used as a backup for _transform
  1509. _additionalTransform[1] = _transform;
  1510. }
  1511. _additionalTransform[0] = *additionalTransform;
  1512. }
  1513. _transformUpdated = _additionalTransformDirty = _inverseDirty = true;
  1514. }
  1515. void Node::setAdditionalTransform(const Mat4& additionalTransform)
  1516. {
  1517. setAdditionalTransform(&additionalTransform);
  1518. }
  1519. AffineTransform Node::getParentToNodeAffineTransform() const
  1520. {
  1521. AffineTransform ret;
  1522. GLToCGAffine(getParentToNodeTransform().m,&ret);
  1523. return ret;
  1524. }
  1525. const Mat4& Node::getParentToNodeTransform() const
  1526. {
  1527. if ( _inverseDirty )
  1528. {
  1529. _inverse = getNodeToParentTransform().getInversed();
  1530. _inverseDirty = false;
  1531. }
  1532. return _inverse;
  1533. }
  1534. AffineTransform Node::getNodeToWorldAffineTransform() const
  1535. {
  1536. return this->getNodeToParentAffineTransform(nullptr);
  1537. }
  1538. Mat4 Node::getNodeToWorldTransform() const
  1539. {
  1540. return this->getNodeToParentTransform(nullptr);
  1541. }
  1542. AffineTransform Node::getWorldToNodeAffineTransform() const
  1543. {
  1544. return AffineTransformInvert(this->getNodeToWorldAffineTransform());
  1545. }
  1546. Mat4 Node::getWorldToNodeTransform() const
  1547. {
  1548. return getNodeToWorldTransform().getInversed();
  1549. }
  1550. Vec2 Node::convertToNodeSpace(const Vec2& worldPoint) const
  1551. {
  1552. Mat4 tmp = getWorldToNodeTransform();
  1553. Vec3 vec3(worldPoint.x, worldPoint.y, 0);
  1554. Vec3 ret;
  1555. tmp.transformPoint(vec3,&ret);
  1556. return Vec2(ret.x, ret.y);
  1557. }
  1558. Vec2 Node::convertToWorldSpace(const Vec2& nodePoint) const
  1559. {
  1560. Mat4 tmp = getNodeToWorldTransform();
  1561. Vec3 vec3(nodePoint.x, nodePoint.y, 0);
  1562. Vec3 ret;
  1563. tmp.transformPoint(vec3,&ret);
  1564. return Vec2(ret.x, ret.y);
  1565. }
  1566. Vec2 Node::convertToNodeSpaceAR(const Vec2& worldPoint) const
  1567. {
  1568. Vec2 nodePoint(convertToNodeSpace(worldPoint));
  1569. return nodePoint - _anchorPointInPoints;
  1570. }
  1571. Vec2 Node::convertToWorldSpaceAR(const Vec2& nodePoint) const
  1572. {
  1573. return convertToWorldSpace(nodePoint + _anchorPointInPoints);
  1574. }
  1575. Vec2 Node::convertToWindowSpace(const Vec2& nodePoint) const
  1576. {
  1577. Vec2 worldPoint(this->convertToWorldSpace(nodePoint));
  1578. return _director->convertToUI(worldPoint);
  1579. }
  1580. // convenience methods which take a Touch instead of Vec2
  1581. Vec2 Node::convertTouchToNodeSpace(Touch *touch) const
  1582. {
  1583. return this->convertToNodeSpace(touch->getLocation());
  1584. }
  1585. Vec2 Node::convertTouchToNodeSpaceAR(Touch *touch) const
  1586. {
  1587. Vec2 point = touch->getLocation();
  1588. return this->convertToNodeSpaceAR(point);
  1589. }
  1590. void Node::updateTransform()
  1591. {
  1592. // Recursively iterate over children
  1593. for( const auto &child: _children)
  1594. child->updateTransform();
  1595. }
  1596. // MARK: components
  1597. Component* Node::getComponent(const std::string& name)
  1598. {
  1599. if (_componentContainer)
  1600. return _componentContainer->get(name);
  1601. return nullptr;
  1602. }
  1603. bool Node::addComponent(Component *component)
  1604. {
  1605. // lazy alloc
  1606. if (!_componentContainer)
  1607. _componentContainer = new (std::nothrow) ComponentContainer(this);
  1608. // should enable schedule update, then all components can receive this call back
  1609. scheduleUpdate();
  1610. return _componentContainer->add(component);
  1611. }
  1612. bool Node::removeComponent(const std::string& name)
  1613. {
  1614. if (_componentContainer)
  1615. return _componentContainer->remove(name);
  1616. return false;
  1617. }
  1618. bool Node::removeComponent(Component *component)
  1619. {
  1620. if (_componentContainer)
  1621. {
  1622. return _componentContainer->remove(component);
  1623. }
  1624. return false;
  1625. }
  1626. void Node::removeAllComponents()
  1627. {
  1628. if (_componentContainer)
  1629. _componentContainer->removeAll();
  1630. }
  1631. // MARK: Opacity and Color
  1632. GLubyte Node::getOpacity(void) const
  1633. {
  1634. return _realOpacity;
  1635. }
  1636. GLubyte Node::getDisplayedOpacity() const
  1637. {
  1638. return _displayedOpacity;
  1639. }
  1640. void Node::setOpacity(GLubyte opacity)
  1641. {
  1642. _displayedOpacity = _realOpacity = opacity;
  1643. updateCascadeOpacity();
  1644. }
  1645. void Node::updateDisplayedOpacity(GLubyte parentOpacity)
  1646. {
  1647. _displayedOpacity = _realOpacity * parentOpacity/255.0;
  1648. updateColor();
  1649. if (_cascadeOpacityEnabled)
  1650. {
  1651. for(const auto& child : _children)
  1652. {
  1653. child->updateDisplayedOpacity(_displayedOpacity);
  1654. }
  1655. }
  1656. }
  1657. bool Node::isCascadeOpacityEnabled(void) const
  1658. {
  1659. return _cascadeOpacityEnabled;
  1660. }
  1661. void Node::setCascadeOpacityEnabled(bool cascadeOpacityEnabled)
  1662. {
  1663. if (_cascadeOpacityEnabled == cascadeOpacityEnabled)
  1664. {
  1665. return;
  1666. }
  1667. _cascadeOpacityEnabled = cascadeOpacityEnabled;
  1668. if (cascadeOpacityEnabled)
  1669. {
  1670. updateCascadeOpacity();
  1671. }
  1672. else
  1673. {
  1674. disableCascadeOpacity();
  1675. }
  1676. }
  1677. void Node::updateCascadeOpacity()
  1678. {
  1679. GLubyte parentOpacity = 255;
  1680. if (_parent != nullptr && _parent->isCascadeOpacityEnabled())
  1681. {
  1682. parentOpacity = _parent->getDisplayedOpacity();
  1683. }
  1684. updateDisplayedOpacity(parentOpacity);
  1685. }
  1686. void Node::disableCascadeOpacity()
  1687. {
  1688. _displayedOpacity = _realOpacity;
  1689. for(const auto& child : _children)
  1690. {
  1691. child->updateDisplayedOpacity(255);
  1692. }
  1693. }
  1694. void Node::setOpacityModifyRGB(bool /*value*/)
  1695. {}
  1696. bool Node::isOpacityModifyRGB() const
  1697. {
  1698. return false;
  1699. }
  1700. const Color3B& Node::getColor(void) const
  1701. {
  1702. return _realColor;
  1703. }
  1704. const Color3B& Node::getDisplayedColor() const
  1705. {
  1706. return _displayedColor;
  1707. }
  1708. void Node::setColor(const Color3B& color)
  1709. {
  1710. _displayedColor = _realColor = color;
  1711. updateCascadeColor();
  1712. }
  1713. void Node::updateDisplayedColor(const Color3B& parentColor)
  1714. {
  1715. _displayedColor.r = _realColor.r * parentColor.r/255.0;
  1716. _displayedColor.g = _realColor.g * parentColor.g/255.0;
  1717. _displayedColor.b = _realColor.b * parentColor.b/255.0;
  1718. updateColor();
  1719. if (_cascadeColorEnabled)
  1720. {
  1721. for(const auto &child : _children)
  1722. {
  1723. child->updateDisplayedColor(_displayedColor);
  1724. }
  1725. }
  1726. }
  1727. bool Node::isCascadeColorEnabled(void) const
  1728. {
  1729. return _cascadeColorEnabled;
  1730. }
  1731. void Node::setCascadeColorEnabled(bool cascadeColorEnabled)
  1732. {
  1733. if (_cascadeColorEnabled == cascadeColorEnabled)
  1734. {
  1735. return;
  1736. }
  1737. _cascadeColorEnabled = cascadeColorEnabled;
  1738. if (_cascadeColorEnabled)
  1739. {
  1740. updateCascadeColor();
  1741. }
  1742. else
  1743. {
  1744. disableCascadeColor();
  1745. }
  1746. }
  1747. void Node::updateCascadeColor()
  1748. {
  1749. Color3B parentColor = Color3B::WHITE;
  1750. if (_parent && _parent->isCascadeColorEnabled())
  1751. {
  1752. parentColor = _parent->getDisplayedColor();
  1753. }
  1754. updateDisplayedColor(parentColor);
  1755. }
  1756. void Node::disableCascadeColor()
  1757. {
  1758. for(const auto& child : _children)
  1759. {
  1760. child->updateDisplayedColor(Color3B::WHITE);
  1761. }
  1762. }
  1763. bool isScreenPointInRect(const Vec2 &pt, const Camera* camera, const Mat4& w2l, const Rect& rect, Vec3 *p)
  1764. {
  1765. if (nullptr == camera || rect.size.width <= 0 || rect.size.height <= 0)
  1766. {
  1767. return false;
  1768. }
  1769. // first, convert pt to near/far plane, get Pn and Pf
  1770. Vec3 Pn(pt.x, pt.y, -1), Pf(pt.x, pt.y, 1);
  1771. Pn = camera->unprojectGL(Pn);
  1772. Pf = camera->unprojectGL(Pf);
  1773. // then convert Pn and Pf to node space
  1774. w2l.transformPoint(&Pn);
  1775. w2l.transformPoint(&Pf);
  1776. // Pn and Pf define a line Q(t) = D + t * E which D = Pn
  1777. auto E = Pf - Pn;
  1778. // second, get three points which define content plane
  1779. // these points define a plane P(u, w) = A + uB + wC
  1780. Vec3 A = Vec3(rect.origin.x, rect.origin.y, 0);
  1781. Vec3 B(rect.origin.x + rect.size.width, rect.origin.y, 0);
  1782. Vec3 C(rect.origin.x, rect.origin.y + rect.size.height, 0);
  1783. B = B - A;
  1784. C = C - A;
  1785. // the line Q(t) intercept with plane P(u, w)
  1786. // calculate the intercept point P = Q(t)
  1787. // (BxC).A - (BxC).D
  1788. // t = -----------------
  1789. // (BxC).E
  1790. Vec3 BxC;
  1791. Vec3::cross(B, C, &BxC);
  1792. auto BxCdotE = BxC.dot(E);
  1793. if (BxCdotE == 0) {
  1794. return false;
  1795. }
  1796. auto t = (BxC.dot(A) - BxC.dot(Pn)) / BxCdotE;
  1797. Vec3 P = Pn + t * E;
  1798. if (p) {
  1799. *p = P;
  1800. }
  1801. return rect.containsPoint(Vec2(P.x, P.y));
  1802. }
  1803. // MARK: Camera
  1804. void Node::setCameraMask(unsigned short mask, bool applyChildren)
  1805. {
  1806. _cameraMask = mask;
  1807. if (applyChildren)
  1808. {
  1809. for (const auto& child : _children)
  1810. {
  1811. child->setCameraMask(mask, applyChildren);
  1812. }
  1813. }
  1814. }
  1815. int Node::getAttachedNodeCount()
  1816. {
  1817. return __attachedNodeCount;
  1818. }
  1819. // MARK: Deprecated
  1820. __NodeRGBA::__NodeRGBA()
  1821. {
  1822. CCLOG("NodeRGBA deprecated.");
  1823. }
  1824. NS_CC_END