axmol/cocos/2d/CCNodeGrid.cpp

162 lines
3.3 KiB
C++
Raw Normal View History

2013-12-10 10:50:31 +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
NodeGrid* NodeGrid::create()
2013-12-10 10:50:31 +08:00
{
NodeGrid * pRet = new NodeGrid();
2013-12-10 10:50:31 +08:00
if (pRet && pRet->init())
{
pRet->autorelease();
}
else
{
CC_SAFE_DELETE(pRet);
}
return pRet;
}
NodeGrid::NodeGrid()
2013-12-10 10:50:31 +08:00
:Node()
,_nodeGrid(nullptr)
,_gridTarget(nullptr)
2013-12-10 10:50:31 +08:00
{
}
void NodeGrid::setTarget(Node* target)
{
CC_SAFE_RELEASE(_gridTarget);
CC_SAFE_RETAIN(target);
_gridTarget = target;
}
NodeGrid::~NodeGrid()
2013-12-10 10:50:31 +08:00
{
CC_SAFE_RELEASE(_nodeGrid);
CC_SAFE_RELEASE(_gridTarget);
2013-12-10 10:50:31 +08:00
}
bool NodeGrid::init()
2013-12-10 10:50:31 +08:00
{
return Node::init();
}
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);
}
}
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-18 17:19:50 +08:00
kmGLGetMatrix(KM_GL_MODELVIEW, &_cachedMVmat);
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();
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();
// Uses std::for_each to improve performance.
std::for_each(_children.cbegin()+i, _children.cend(), [](Node* node){
node->visit();
});
}
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();
}
void NodeGrid::setGrid(GridBase *grid)
{
CC_SAFE_RELEASE(_nodeGrid);
CC_SAFE_RETAIN(grid);
_nodeGrid = grid;
}
2013-12-10 10:50:31 +08:00
NS_CC_END