CCNodeGrid.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /****************************************************************************
  2. Copyright (c) 2013-2016 Chukong Technologies Inc.
  3. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #include "2d/CCNodeGrid.h"
  22. #include "2d/CCGrid.h"
  23. #include "renderer/CCRenderer.h"
  24. NS_CC_BEGIN
  25. NodeGrid* NodeGrid::create()
  26. {
  27. NodeGrid * ret = new (std::nothrow) NodeGrid();
  28. if (ret && ret->init())
  29. {
  30. ret->autorelease();
  31. }
  32. else
  33. {
  34. CC_SAFE_DELETE(ret);
  35. }
  36. return ret;
  37. }
  38. NodeGrid* NodeGrid::create(const cocos2d::Rect &rect)
  39. {
  40. NodeGrid* ret = NodeGrid::create();
  41. if (ret) {
  42. ret->setGridRect(rect);
  43. }
  44. return ret;
  45. }
  46. NodeGrid::NodeGrid()
  47. : _gridTarget(nullptr)
  48. , _nodeGrid(nullptr)
  49. , _gridRect(Rect::ZERO)
  50. {
  51. }
  52. void NodeGrid::setTarget(Node* target)
  53. {
  54. #if CC_ENABLE_GC_FOR_NATIVE_OBJECTS
  55. auto sEngine = ScriptEngineManager::getInstance()->getScriptEngine();
  56. if (sEngine)
  57. {
  58. if (_gridTarget)
  59. sEngine->releaseScriptObject(this, _gridTarget);
  60. if (target)
  61. sEngine->retainScriptObject(this, target);
  62. }
  63. #endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS
  64. CC_SAFE_RELEASE(_gridTarget);
  65. CC_SAFE_RETAIN(target);
  66. _gridTarget = target;
  67. }
  68. NodeGrid::~NodeGrid()
  69. {
  70. CC_SAFE_RELEASE(_nodeGrid);
  71. CC_SAFE_RELEASE(_gridTarget);
  72. }
  73. void NodeGrid::onGridBeginDraw()
  74. {
  75. if (_nodeGrid && _nodeGrid->isActive())
  76. {
  77. _nodeGrid->beforeDraw();
  78. }
  79. }
  80. void NodeGrid::onGridEndDraw()
  81. {
  82. if(_nodeGrid && _nodeGrid->isActive())
  83. {
  84. _nodeGrid->afterDraw(this);
  85. }
  86. }
  87. void NodeGrid::visit(Renderer *renderer, const Mat4 &parentTransform, uint32_t parentFlags)
  88. {
  89. // quick return if not visible. children won't be drawn.
  90. if (!_visible)
  91. {
  92. return;
  93. }
  94. bool dirty = (parentFlags & FLAGS_TRANSFORM_DIRTY) || _transformUpdated;
  95. if(dirty)
  96. _modelViewTransform = this->transform(parentTransform);
  97. _transformUpdated = false;
  98. _groupCommand.init(_globalZOrder);
  99. renderer->addCommand(&_groupCommand);
  100. renderer->pushGroup(_groupCommand.getRenderQueueID());
  101. // IMPORTANT:
  102. // To ease the migration to v3.0, we still support the Mat4 stack,
  103. // but it is deprecated and your code should not rely on it
  104. Director* director = Director::getInstance();
  105. CCASSERT(nullptr != director, "Director is null when setting matrix stack");
  106. director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
  107. director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW, _modelViewTransform);
  108. Director::Projection beforeProjectionType = Director::Projection::DEFAULT;
  109. if(_nodeGrid && _nodeGrid->isActive())
  110. {
  111. beforeProjectionType = Director::getInstance()->getProjection();
  112. _nodeGrid->set2DProjection();
  113. }
  114. _gridBeginCommand.init(_globalZOrder);
  115. _gridBeginCommand.func = CC_CALLBACK_0(NodeGrid::onGridBeginDraw, this);
  116. renderer->addCommand(&_gridBeginCommand);
  117. if(_gridTarget)
  118. {
  119. _gridTarget->visit(renderer, _modelViewTransform, dirty);
  120. }
  121. int i = 0;
  122. bool visibleByCamera = isVisitableByVisitingCamera();
  123. if(!_children.empty())
  124. {
  125. sortAllChildren();
  126. // draw children zOrder < 0
  127. for(auto size = _children.size(); i < size; ++i)
  128. {
  129. auto node = _children.at(i);
  130. if ( node && node->getLocalZOrder() < 0 )
  131. node->visit(renderer, _modelViewTransform, dirty);
  132. else
  133. break;
  134. }
  135. // self draw,currently we have nothing to draw on NodeGrid, so there is no need to add render command
  136. if (visibleByCamera)
  137. this->draw(renderer, _modelViewTransform, dirty);
  138. for(auto it=_children.cbegin()+i, itCend = _children.cend(); it != itCend; ++it) {
  139. (*it)->visit(renderer, _modelViewTransform, dirty);
  140. }
  141. }
  142. else if (visibleByCamera)
  143. {
  144. this->draw(renderer, _modelViewTransform, dirty);
  145. }
  146. // FIX ME: Why need to set _orderOfArrival to 0??
  147. // Please refer to https://github.com/cocos2d/cocos2d-x/pull/6920
  148. // setOrderOfArrival(0);
  149. if(_nodeGrid && _nodeGrid->isActive())
  150. {
  151. // restore projection
  152. director->setProjection(beforeProjectionType);
  153. }
  154. _gridEndCommand.init(_globalZOrder);
  155. _gridEndCommand.func = CC_CALLBACK_0(NodeGrid::onGridEndDraw, this);
  156. renderer->addCommand(&_gridEndCommand);
  157. renderer->popGroup();
  158. director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
  159. }
  160. void NodeGrid::setGrid(GridBase *grid)
  161. {
  162. CC_SAFE_RELEASE(_nodeGrid);
  163. CC_SAFE_RETAIN(grid);
  164. _nodeGrid = grid;
  165. }
  166. NS_CC_END