CCProtectedNode.cpp 13 KB

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