1
0

CCProtectedNode.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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/CCProtectedNode.h"
  25. #include "base/CCDirector.h"
  26. #include "2d/CCScene.h"
  27. NS_CC_BEGIN
  28. ProtectedNode::ProtectedNode() : _reorderProtectedChildDirty(false)
  29. {
  30. }
  31. ProtectedNode::~ProtectedNode()
  32. {
  33. CCLOGINFO( "deallocing ProtectedNode: %p - tag: %i", this, _tag );
  34. removeAllProtectedChildren();
  35. }
  36. ProtectedNode * ProtectedNode::create(void)
  37. {
  38. ProtectedNode * ret = new (std::nothrow) ProtectedNode();
  39. if (ret && ret->init())
  40. {
  41. ret->autorelease();
  42. }
  43. else
  44. {
  45. CC_SAFE_DELETE(ret);
  46. }
  47. return ret;
  48. }
  49. void ProtectedNode::cleanup()
  50. {
  51. #if CC_ENABLE_SCRIPT_BINDING
  52. if (_scriptType == kScriptTypeJavascript)
  53. {
  54. if (ScriptEngineManager::sendNodeEventToJSExtended(this, kNodeOnCleanup))
  55. return;
  56. }
  57. #endif // #if CC_ENABLE_SCRIPT_BINDING
  58. Node::cleanup();
  59. // timers
  60. for( const auto &child: _protectedChildren)
  61. child->cleanup();
  62. }
  63. void ProtectedNode::addProtectedChild(cocos2d::Node *child)
  64. {
  65. addProtectedChild(child, child->getLocalZOrder(), child->getTag());
  66. }
  67. void ProtectedNode::addProtectedChild(cocos2d::Node *child, int localZOrder)
  68. {
  69. addProtectedChild(child, localZOrder, child->getTag());
  70. }
  71. /* "add" logic MUST only be on this method
  72. * If a class want's to extend the 'addChild' behavior it only needs
  73. * to override this method
  74. */
  75. void ProtectedNode::addProtectedChild(Node *child, int zOrder, int tag)
  76. {
  77. CCASSERT( child != nullptr, "Argument must be non-nil");
  78. CCASSERT( child->getParent() == nullptr, "child already added. It can't be added again");
  79. if (_protectedChildren.empty())
  80. {
  81. _protectedChildren.reserve(4);
  82. }
  83. this->insertProtectedChild(child, zOrder);
  84. child->setTag(tag);
  85. child->setGlobalZOrder(_globalZOrder);
  86. child->setParent(this);
  87. child->updateOrderOfArrival();
  88. if( _running )
  89. {
  90. child->onEnter();
  91. // prevent onEnterTransitionDidFinish to be called twice when a node is added in onEnter
  92. if (_isTransitionFinished) {
  93. child->onEnterTransitionDidFinish();
  94. }
  95. }
  96. if (_cascadeColorEnabled)
  97. {
  98. updateCascadeColor();
  99. }
  100. if (_cascadeOpacityEnabled)
  101. {
  102. updateCascadeOpacity();
  103. }
  104. }
  105. Node* ProtectedNode::getProtectedChildByTag(int tag)
  106. {
  107. CCASSERT( tag != Node::INVALID_TAG, "Invalid tag");
  108. for (auto& child : _protectedChildren)
  109. {
  110. if(child && child->getTag() == tag)
  111. return child;
  112. }
  113. return nullptr;
  114. }
  115. /* "remove" logic MUST only be on this method
  116. * If a class want's to extend the 'removeChild' behavior it only needs
  117. * to override this method
  118. */
  119. void ProtectedNode::removeProtectedChild(cocos2d::Node *child, bool cleanup)
  120. {
  121. // explicit nil handling
  122. if (_protectedChildren.empty())
  123. {
  124. return;
  125. }
  126. ssize_t index = _protectedChildren.getIndex(child);
  127. if( index != CC_INVALID_INDEX )
  128. {
  129. // IMPORTANT:
  130. // -1st do onExit
  131. // -2nd cleanup
  132. if (_running)
  133. {
  134. child->onExitTransitionDidStart();
  135. child->onExit();
  136. }
  137. // If you don't do cleanup, the child's actions will not get removed and the
  138. // its scheduledSelectors_ dict will not get released!
  139. if (cleanup)
  140. {
  141. child->cleanup();
  142. }
  143. // set parent nil at the end
  144. child->setParent(nullptr);
  145. #if CC_ENABLE_GC_FOR_NATIVE_OBJECTS
  146. auto sEngine = ScriptEngineManager::getInstance()->getScriptEngine();
  147. if (sEngine)
  148. {
  149. sEngine->releaseScriptObject(this, child);
  150. }
  151. #endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS
  152. _protectedChildren.erase(index);
  153. }
  154. }
  155. void ProtectedNode::removeAllProtectedChildren()
  156. {
  157. removeAllProtectedChildrenWithCleanup(true);
  158. }
  159. void ProtectedNode::removeAllProtectedChildrenWithCleanup(bool cleanup)
  160. {
  161. // not using detachChild improves speed here
  162. for (auto& child : _protectedChildren)
  163. {
  164. // IMPORTANT:
  165. // -1st do onExit
  166. // -2nd cleanup
  167. if(_running)
  168. {
  169. child->onExitTransitionDidStart();
  170. child->onExit();
  171. }
  172. if (cleanup)
  173. {
  174. child->cleanup();
  175. }
  176. #if CC_ENABLE_GC_FOR_NATIVE_OBJECTS
  177. auto sEngine = ScriptEngineManager::getInstance()->getScriptEngine();
  178. if (sEngine)
  179. {
  180. sEngine->releaseScriptObject(this, child);
  181. }
  182. #endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS
  183. // set parent nil at the end
  184. child->setParent(nullptr);
  185. }
  186. _protectedChildren.clear();
  187. }
  188. void ProtectedNode::removeProtectedChildByTag(int tag, bool cleanup)
  189. {
  190. CCASSERT( tag != Node::INVALID_TAG, "Invalid tag");
  191. Node *child = this->getProtectedChildByTag(tag);
  192. if (child == nullptr)
  193. {
  194. CCLOG("cocos2d: removeChildByTag(tag = %d): child not found!", tag);
  195. }
  196. else
  197. {
  198. this->removeProtectedChild(child, cleanup);
  199. }
  200. }
  201. // helper used by reorderChild & add
  202. void ProtectedNode::insertProtectedChild(cocos2d::Node *child, int z)
  203. {
  204. #if CC_ENABLE_GC_FOR_NATIVE_OBJECTS
  205. auto sEngine = ScriptEngineManager::getInstance()->getScriptEngine();
  206. if (sEngine)
  207. {
  208. sEngine->retainScriptObject(this, child);
  209. }
  210. #endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS
  211. _reorderProtectedChildDirty = true;
  212. _protectedChildren.pushBack(child);
  213. child->setLocalZOrder(z);
  214. }
  215. void ProtectedNode::sortAllProtectedChildren()
  216. {
  217. if( _reorderProtectedChildDirty ) {
  218. sortNodes(_protectedChildren);
  219. _reorderProtectedChildDirty = false;
  220. }
  221. }
  222. void ProtectedNode::reorderProtectedChild(cocos2d::Node *child, int localZOrder)
  223. {
  224. CCASSERT( child != nullptr, "Child must be non-nil");
  225. _reorderProtectedChildDirty = true;
  226. child->updateOrderOfArrival();
  227. child->setLocalZOrder(localZOrder);
  228. }
  229. void ProtectedNode::visit(Renderer* renderer, const Mat4 &parentTransform, uint32_t parentFlags)
  230. {
  231. // quick return if not visible. children won't be drawn.
  232. if (!_visible)
  233. {
  234. return;
  235. }
  236. uint32_t flags = processParentFlags(parentTransform, parentFlags);
  237. // IMPORTANT:
  238. // To ease the migration to v3.0, we still support the Mat4 stack,
  239. // but it is deprecated and your code should not rely on it
  240. Director* director = Director::getInstance();
  241. CCASSERT(nullptr != director, "Director is null when setting matrix stack");
  242. director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
  243. director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW, _modelViewTransform);
  244. int i = 0; // used by _children
  245. int j = 0; // used by _protectedChildren
  246. sortAllChildren();
  247. sortAllProtectedChildren();
  248. //
  249. // draw children and protectedChildren zOrder < 0
  250. //
  251. for(auto size = _children.size(); i < size; ++i)
  252. {
  253. auto node = _children.at(i);
  254. if ( node && node->getLocalZOrder() < 0 )
  255. node->visit(renderer, _modelViewTransform, flags);
  256. else
  257. break;
  258. }
  259. for(auto size = _protectedChildren.size(); j < size; ++j)
  260. {
  261. auto node = _protectedChildren.at(j);
  262. if ( node && node->getLocalZOrder() < 0 )
  263. node->visit(renderer, _modelViewTransform, flags);
  264. else
  265. break;
  266. }
  267. //
  268. // draw self
  269. //
  270. if (isVisitableByVisitingCamera())
  271. this->draw(renderer, _modelViewTransform, flags);
  272. //
  273. // draw children and protectedChildren zOrder >= 0
  274. //
  275. for(auto it=_protectedChildren.cbegin()+j, itCend = _protectedChildren.cend(); it != itCend; ++it)
  276. (*it)->visit(renderer, _modelViewTransform, flags);
  277. for(auto it=_children.cbegin()+i, itCend = _children.cend(); it != itCend; ++it)
  278. (*it)->visit(renderer, _modelViewTransform, flags);
  279. // FIX ME: Why need to set _orderOfArrival to 0??
  280. // Please refer to https://github.com/cocos2d/cocos2d-x/pull/6920
  281. // setOrderOfArrival(0);
  282. director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
  283. }
  284. void ProtectedNode::onEnter()
  285. {
  286. #if CC_ENABLE_SCRIPT_BINDING
  287. if (_scriptType == kScriptTypeJavascript)
  288. {
  289. if (ScriptEngineManager::sendNodeEventToJSExtended(this, kNodeOnEnter))
  290. return;
  291. }
  292. #endif
  293. Node::onEnter();
  294. for( const auto &child: _protectedChildren)
  295. child->onEnter();
  296. }
  297. void ProtectedNode::onEnterTransitionDidFinish()
  298. {
  299. #if CC_ENABLE_SCRIPT_BINDING
  300. if (_scriptType == kScriptTypeJavascript)
  301. {
  302. if (ScriptEngineManager::sendNodeEventToJSExtended(this, kNodeOnEnterTransitionDidFinish))
  303. return;
  304. }
  305. #endif
  306. Node::onEnterTransitionDidFinish();
  307. for( const auto &child: _protectedChildren)
  308. child->onEnterTransitionDidFinish();
  309. }
  310. void ProtectedNode::onExitTransitionDidStart()
  311. {
  312. #if CC_ENABLE_SCRIPT_BINDING
  313. if (_scriptType == kScriptTypeJavascript)
  314. {
  315. if (ScriptEngineManager::sendNodeEventToJSExtended(this, kNodeOnExitTransitionDidStart))
  316. return;
  317. }
  318. #endif
  319. Node::onExitTransitionDidStart();
  320. for( const auto &child: _protectedChildren)
  321. child->onExitTransitionDidStart();
  322. }
  323. void ProtectedNode::onExit()
  324. {
  325. #if CC_ENABLE_SCRIPT_BINDING
  326. if (_scriptType == kScriptTypeJavascript)
  327. {
  328. if (ScriptEngineManager::sendNodeEventToJSExtended(this, kNodeOnExit))
  329. return;
  330. }
  331. #endif
  332. Node::onExit();
  333. for( const auto &child: _protectedChildren)
  334. child->onExit();
  335. }
  336. void ProtectedNode::updateDisplayedOpacity(GLubyte parentOpacity)
  337. {
  338. _displayedOpacity = _realOpacity * parentOpacity/255.0;
  339. updateColor();
  340. if (_cascadeOpacityEnabled)
  341. {
  342. for(auto child : _children){
  343. child->updateDisplayedOpacity(_displayedOpacity);
  344. }
  345. }
  346. for(auto child : _protectedChildren){
  347. child->updateDisplayedOpacity(_displayedOpacity);
  348. }
  349. }
  350. void ProtectedNode::updateDisplayedColor(const Color3B& parentColor)
  351. {
  352. _displayedColor.r = _realColor.r * parentColor.r/255.0;
  353. _displayedColor.g = _realColor.g * parentColor.g/255.0;
  354. _displayedColor.b = _realColor.b * parentColor.b/255.0;
  355. updateColor();
  356. if (_cascadeColorEnabled)
  357. {
  358. for(const auto &child : _children){
  359. child->updateDisplayedColor(_displayedColor);
  360. }
  361. }
  362. for(const auto &child : _protectedChildren){
  363. child->updateDisplayedColor(_displayedColor);
  364. }
  365. }
  366. void ProtectedNode::disableCascadeColor()
  367. {
  368. for(auto child : _children){
  369. child->updateDisplayedColor(Color3B::WHITE);
  370. }
  371. for(auto child : _protectedChildren){
  372. child->updateDisplayedColor(Color3B::WHITE);
  373. }
  374. }
  375. void ProtectedNode::disableCascadeOpacity()
  376. {
  377. _displayedOpacity = _realOpacity;
  378. for(auto child : _children){
  379. child->updateDisplayedOpacity(255);
  380. }
  381. for(auto child : _protectedChildren){
  382. child->updateDisplayedOpacity(255);
  383. }
  384. }
  385. void ProtectedNode::setCameraMask(unsigned short mask, bool applyChildren)
  386. {
  387. Node::setCameraMask(mask, applyChildren);
  388. if (applyChildren)
  389. {
  390. for (auto& iter: _protectedChildren)
  391. {
  392. iter->setCameraMask(mask);
  393. }
  394. }
  395. }
  396. void ProtectedNode::setGlobalZOrder(float globalZOrder)
  397. {
  398. Node::setGlobalZOrder(globalZOrder);
  399. for (auto &child : _protectedChildren)
  400. child->setGlobalZOrder(globalZOrder);
  401. }
  402. NS_CC_END