axmol/cocos/2d/CCNodeGrid.cpp

175 lines
4.3 KiB
C++
Raw Normal View History

/****************************************************************************
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
#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 * ret = new NodeGrid();
if (ret && ret->init())
2013-12-10 10:50:31 +08:00
{
ret->autorelease();
2013-12-10 10:50:31 +08:00
}
else
{
CC_SAFE_DELETE(ret);
2013-12-10 10:50:31 +08:00
}
return ret;
2013-12-10 10:50:31 +08:00
}
NodeGrid::NodeGrid()
2013-12-27 15:06:16 +08:00
: _gridTarget(nullptr)
, _nodeGrid(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
}
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
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();
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();
}
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