2013-12-21 03:52:52 +08:00
|
|
|
/****************************************************************************
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2013-2014 Chukong Technologies Inc.
|
2013-12-21 03:52:52 +08:00
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
2013-12-10 10:50:31 +08:00
|
|
|
|
2014-05-01 10:09:13 +08:00
|
|
|
#include "2d/CCNodeGrid.h"
|
|
|
|
#include "2d/CCGrid.h"
|
2014-04-30 08:37:36 +08:00
|
|
|
#include "renderer/CCRenderer.h"
|
2013-12-10 10:50:31 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2013-12-16 14:21:05 +08:00
|
|
|
NodeGrid* NodeGrid::create()
|
2013-12-10 10:50:31 +08:00
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
NodeGrid * ret = new (std::nothrow) NodeGrid();
|
2013-12-19 21:41:07 +08:00
|
|
|
if (ret && ret->init())
|
2013-12-10 10:50:31 +08:00
|
|
|
{
|
2013-12-19 21:41:07 +08:00
|
|
|
ret->autorelease();
|
2013-12-10 10:50:31 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-19 21:41:07 +08:00
|
|
|
CC_SAFE_DELETE(ret);
|
2013-12-10 10:50:31 +08:00
|
|
|
}
|
2013-12-19 21:41:07 +08:00
|
|
|
return ret;
|
2013-12-10 10:50:31 +08:00
|
|
|
}
|
|
|
|
|
2013-12-16 14:21:05 +08:00
|
|
|
NodeGrid::NodeGrid()
|
2013-12-27 15:06:16 +08:00
|
|
|
: _gridTarget(nullptr)
|
|
|
|
, _nodeGrid(nullptr)
|
2013-12-10 10:50:31 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-12-16 14:21:05 +08:00
|
|
|
void NodeGrid::setTarget(Node* target)
|
2013-12-12 13:21:14 +08:00
|
|
|
{
|
|
|
|
CC_SAFE_RELEASE(_gridTarget);
|
|
|
|
CC_SAFE_RETAIN(target);
|
|
|
|
_gridTarget = target;
|
|
|
|
}
|
|
|
|
|
2013-12-16 14:21:05 +08:00
|
|
|
NodeGrid::~NodeGrid()
|
2013-12-10 10:50:31 +08:00
|
|
|
{
|
|
|
|
CC_SAFE_RELEASE(_nodeGrid);
|
2013-12-12 13:21:14 +08:00
|
|
|
CC_SAFE_RELEASE(_gridTarget);
|
2013-12-10 10:50:31 +08:00
|
|
|
}
|
|
|
|
|
2013-12-18 17:19:50 +08:00
|
|
|
void NodeGrid::onGridBeginDraw()
|
|
|
|
{
|
|
|
|
if (_nodeGrid && _nodeGrid->isActive())
|
|
|
|
{
|
|
|
|
_nodeGrid->beforeDraw();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void NodeGrid::onGridEndDraw()
|
|
|
|
{
|
|
|
|
if(_nodeGrid && _nodeGrid->isActive())
|
|
|
|
{
|
|
|
|
_nodeGrid->afterDraw(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-31 07:42:05 +08:00
|
|
|
void NodeGrid::visit(Renderer *renderer, const Mat4 &parentTransform, uint32_t parentFlags)
|
2013-12-10 10:50:31 +08:00
|
|
|
{
|
|
|
|
// quick return if not visible. children won't be drawn.
|
|
|
|
if (!_visible)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-19 03:35:27 +08:00
|
|
|
_groupCommand.init(_globalZOrder);
|
2013-12-26 18:11:23 +08:00
|
|
|
renderer->addCommand(&_groupCommand);
|
|
|
|
renderer->pushGroup(_groupCommand.getRenderQueueID());
|
2013-12-18 17:19:50 +08:00
|
|
|
|
2014-05-31 12:50:39 +08:00
|
|
|
bool dirty = (parentFlags & FLAGS_TRANSFORM_DIRTY) || _transformUpdated;
|
2014-03-01 03:20:53 +08:00
|
|
|
if(dirty)
|
|
|
|
_modelViewTransform = this->transform(parentTransform);
|
2014-03-01 08:10:48 +08:00
|
|
|
_transformUpdated = false;
|
2014-03-01 03:20:53 +08:00
|
|
|
|
|
|
|
// IMPORTANT:
|
2014-05-15 01:07:09 +08:00
|
|
|
// To ease the migration to v3.0, we still support the Mat4 stack,
|
2014-03-01 03:20:53 +08:00
|
|
|
// but it is deprecated and your code should not rely on it
|
2014-04-02 22:50:39 +08:00
|
|
|
Director* director = Director::getInstance();
|
|
|
|
CCASSERT(nullptr != director, "Director is null when seting matrix stack");
|
|
|
|
|
|
|
|
director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
|
|
|
|
director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW, _modelViewTransform);
|
2014-03-01 03:20:53 +08:00
|
|
|
|
2014-04-09 20:41:09 +08:00
|
|
|
Director::Projection beforeProjectionType = Director::Projection::DEFAULT;
|
2013-12-19 16:54:39 +08:00
|
|
|
if(_nodeGrid && _nodeGrid->isActive())
|
|
|
|
{
|
|
|
|
beforeProjectionType = Director::getInstance()->getProjection();
|
|
|
|
_nodeGrid->set2DProjection();
|
|
|
|
}
|
2013-12-21 03:52:52 +08:00
|
|
|
|
2014-01-19 03:35:27 +08:00
|
|
|
_gridBeginCommand.init(_globalZOrder);
|
2013-12-26 21:19:12 +08:00
|
|
|
_gridBeginCommand.func = CC_CALLBACK_0(NodeGrid::onGridBeginDraw, this);
|
|
|
|
renderer->addCommand(&_gridBeginCommand);
|
2013-12-18 17:19:50 +08:00
|
|
|
|
2014-03-01 03:20:53 +08:00
|
|
|
|
2013-12-12 13:21:14 +08:00
|
|
|
if(_gridTarget)
|
|
|
|
{
|
2014-02-28 13:43:54 +08:00
|
|
|
_gridTarget->visit(renderer, _modelViewTransform, dirty);
|
2013-12-12 13:21:14 +08:00
|
|
|
}
|
2013-12-18 17:19:50 +08:00
|
|
|
|
2013-12-10 10:50:31 +08:00
|
|
|
int i = 0;
|
2014-08-14 10:26:37 +08:00
|
|
|
bool visibleByCamera = isVisitableByVisitingCamera();
|
2013-12-10 10:50:31 +08:00
|
|
|
|
|
|
|
if(!_children.empty())
|
|
|
|
{
|
|
|
|
sortAllChildren();
|
|
|
|
// draw children zOrder < 0
|
|
|
|
for( ; i < _children.size(); i++ )
|
|
|
|
{
|
|
|
|
auto node = _children.at(i);
|
|
|
|
|
2014-01-20 15:03:30 +08:00
|
|
|
if ( node && node->getLocalZOrder() < 0 )
|
2014-02-28 13:43:54 +08:00
|
|
|
node->visit(renderer, _modelViewTransform, dirty);
|
2013-12-10 10:50:31 +08:00
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
2013-12-18 17:19:50 +08:00
|
|
|
// self draw,currently we have nothing to draw on NodeGrid, so there is no need to add render command
|
2014-08-13 12:06:51 +08:00
|
|
|
if (visibleByCamera)
|
|
|
|
this->draw(renderer, _modelViewTransform, dirty);
|
2013-12-10 10:50:31 +08:00
|
|
|
|
2013-12-21 03:52:52 +08:00
|
|
|
for(auto it=_children.cbegin()+i; it != _children.cend(); ++it) {
|
2014-02-28 13:43:54 +08:00
|
|
|
(*it)->visit(renderer, _modelViewTransform, dirty);
|
2013-12-21 03:52:52 +08:00
|
|
|
}
|
2013-12-10 10:50:31 +08:00
|
|
|
}
|
2014-08-13 12:06:51 +08:00
|
|
|
else if (visibleByCamera)
|
2013-12-10 10:50:31 +08:00
|
|
|
{
|
2014-02-28 13:43:54 +08:00
|
|
|
this->draw(renderer, _modelViewTransform, dirty);
|
2013-12-10 10:50:31 +08:00
|
|
|
}
|
2013-12-18 17:19:50 +08:00
|
|
|
|
2014-09-11 15:44:31 +08:00
|
|
|
// FIX ME: Why need to set _orderOfArrival to 0??
|
|
|
|
// Please refer to https://github.com/cocos2d/cocos2d-x/pull/6920
|
|
|
|
// setOrderOfArrival(0);
|
2013-12-19 16:54:39 +08:00
|
|
|
|
|
|
|
if(_nodeGrid && _nodeGrid->isActive())
|
|
|
|
{
|
|
|
|
// restore projection
|
|
|
|
director->setProjection(beforeProjectionType);
|
|
|
|
}
|
2013-12-10 10:50:31 +08:00
|
|
|
|
2014-01-19 03:35:27 +08:00
|
|
|
_gridEndCommand.init(_globalZOrder);
|
2013-12-26 21:19:12 +08:00
|
|
|
_gridEndCommand.func = CC_CALLBACK_0(NodeGrid::onGridEndDraw, this);
|
|
|
|
renderer->addCommand(&_gridEndCommand);
|
2013-12-18 17:19:50 +08:00
|
|
|
|
|
|
|
renderer->popGroup();
|
2013-12-10 10:50:31 +08:00
|
|
|
|
2014-04-02 22:50:39 +08:00
|
|
|
director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
|
2013-12-10 10:50:31 +08:00
|
|
|
}
|
|
|
|
|
2013-12-16 14:21:05 +08:00
|
|
|
void NodeGrid::setGrid(GridBase *grid)
|
2013-12-12 16:20:52 +08:00
|
|
|
{
|
|
|
|
CC_SAFE_RELEASE(_nodeGrid);
|
|
|
|
CC_SAFE_RETAIN(grid);
|
|
|
|
_nodeGrid = grid;
|
|
|
|
}
|
2013-12-10 10:50:31 +08:00
|
|
|
|
|
|
|
NS_CC_END
|