2013-12-21 03:52:52 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2013 cocos2d-x.org
|
|
|
|
|
|
|
|
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
|
|
|
|
2013-12-16 14:21:05 +08:00
|
|
|
#include "CCNodeGrid.h"
|
2013-12-10 10:50:31 +08:00
|
|
|
#include "CCGrid.h"
|
|
|
|
|
2013-12-19 17:20:36 +08:00
|
|
|
#include "CCGroupCommand.h"
|
|
|
|
#include "CCRenderer.h"
|
|
|
|
#include "CCCustomCommand.h"
|
2013-12-18 17:19:50 +08:00
|
|
|
|
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
|
|
|
{
|
2013-12-19 21:41:07 +08:00
|
|
|
NodeGrid * ret = new NodeGrid();
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-16 14:21:05 +08:00
|
|
|
void NodeGrid::visit()
|
2013-12-10 10:50:31 +08:00
|
|
|
{
|
|
|
|
// quick return if not visible. children won't be drawn.
|
|
|
|
if (!_visible)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-12-19 17:20:36 +08:00
|
|
|
Renderer* renderer = Director::getInstance()->getRenderer();
|
2013-12-18 17:19:50 +08:00
|
|
|
|
|
|
|
GroupCommand* groupCommand = GroupCommand::getCommandPool().generateCommand();
|
|
|
|
groupCommand->init(0,_vertexZ);
|
|
|
|
renderer->addCommand(groupCommand);
|
|
|
|
renderer->pushGroup(groupCommand->getRenderQueueID());
|
|
|
|
|
2013-12-10 10:50:31 +08:00
|
|
|
kmGLPushMatrix();
|
2013-12-19 16:54:39 +08:00
|
|
|
Director::Projection beforeProjectionType;
|
|
|
|
if(_nodeGrid && _nodeGrid->isActive())
|
|
|
|
{
|
|
|
|
beforeProjectionType = Director::getInstance()->getProjection();
|
|
|
|
_nodeGrid->set2DProjection();
|
|
|
|
}
|
2013-12-21 03:52:52 +08:00
|
|
|
|
2013-12-18 17:19:50 +08:00
|
|
|
CustomCommand* gridBeginCmd = CustomCommand::getCommandPool().generateCommand();
|
|
|
|
gridBeginCmd->init(0,_vertexZ);
|
|
|
|
gridBeginCmd->func = CC_CALLBACK_0(NodeGrid::onGridBeginDraw, this);
|
|
|
|
renderer->addCommand(gridBeginCmd);
|
|
|
|
|
2013-12-19 16:54:39 +08:00
|
|
|
this->transform();
|
|
|
|
|
2013-12-12 13:21:14 +08:00
|
|
|
if(_gridTarget)
|
|
|
|
{
|
|
|
|
_gridTarget->visit();
|
|
|
|
}
|
2013-12-18 17:19:50 +08:00
|
|
|
|
2013-12-10 10:50:31 +08:00
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
if(!_children.empty())
|
|
|
|
{
|
|
|
|
sortAllChildren();
|
|
|
|
// draw children zOrder < 0
|
|
|
|
for( ; i < _children.size(); i++ )
|
|
|
|
{
|
|
|
|
auto node = _children.at(i);
|
|
|
|
|
|
|
|
if ( node && node->getZOrder() < 0 )
|
|
|
|
node->visit();
|
|
|
|
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
|
2013-12-10 10:50:31 +08:00
|
|
|
this->draw();
|
|
|
|
|
2013-12-21 03:52:52 +08:00
|
|
|
for(auto it=_children.cbegin()+i; it != _children.cend(); ++it) {
|
|
|
|
(*it)->visit();
|
|
|
|
}
|
2013-12-10 10:50:31 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this->draw();
|
|
|
|
}
|
2013-12-18 17:19:50 +08:00
|
|
|
|
2013-12-10 10:50:31 +08:00
|
|
|
// reset for next frame
|
|
|
|
_orderOfArrival = 0;
|
2013-12-19 16:54:39 +08:00
|
|
|
|
|
|
|
if(_nodeGrid && _nodeGrid->isActive())
|
|
|
|
{
|
|
|
|
// restore projection
|
|
|
|
Director *director = Director::getInstance();
|
|
|
|
director->setProjection(beforeProjectionType);
|
|
|
|
}
|
2013-12-10 10:50:31 +08:00
|
|
|
|
2013-12-18 17:19:50 +08:00
|
|
|
CustomCommand* gridEndCmd = CustomCommand::getCommandPool().generateCommand();
|
|
|
|
gridEndCmd->init(0,_vertexZ);
|
|
|
|
gridEndCmd->func = CC_CALLBACK_0(NodeGrid::onGridEndDraw, this);
|
|
|
|
renderer->addCommand(gridEndCmd);
|
|
|
|
|
|
|
|
renderer->popGroup();
|
2013-12-10 10:50:31 +08:00
|
|
|
|
|
|
|
kmGLPopMatrix();
|
|
|
|
}
|
|
|
|
|
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
|