2013-12-10 10:50:31 +08:00
|
|
|
|
|
|
|
#include "CCGridNode.h"
|
|
|
|
#include "CCGrid.h"
|
|
|
|
|
|
|
|
#include "kazmath/GL/matrix.h"
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
GridNode* GridNode::create()
|
|
|
|
{
|
|
|
|
GridNode * pRet = new GridNode();
|
|
|
|
if (pRet && pRet->init())
|
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(pRet);
|
|
|
|
}
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
|
|
|
GridNode::GridNode()
|
|
|
|
:Node()
|
|
|
|
,_nodeGrid(nullptr)
|
2013-12-12 13:21:14 +08:00
|
|
|
,_gridTarget(nullptr)
|
2013-12-10 10:50:31 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-12-12 13:21:14 +08:00
|
|
|
void GridNode::setGridTarget(Node* target)
|
|
|
|
{
|
|
|
|
CC_SAFE_RELEASE(_gridTarget);
|
|
|
|
CC_SAFE_RETAIN(target);
|
|
|
|
_gridTarget = target;
|
|
|
|
}
|
|
|
|
|
2013-12-10 10:50:31 +08:00
|
|
|
GridNode::~GridNode()
|
|
|
|
{
|
|
|
|
CC_SAFE_RELEASE(_nodeGrid);
|
2013-12-12 13:21:14 +08:00
|
|
|
CC_SAFE_RELEASE(_gridTarget);
|
2013-12-10 10:50:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool GridNode::init()
|
|
|
|
{
|
|
|
|
return Node::init();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GridNode::visit()
|
|
|
|
{
|
|
|
|
// quick return if not visible. children won't be drawn.
|
|
|
|
if (!_visible)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
kmGLPushMatrix();
|
|
|
|
|
|
|
|
if (_nodeGrid && _nodeGrid->isActive())
|
|
|
|
{
|
|
|
|
_nodeGrid->beforeDraw();
|
|
|
|
}
|
|
|
|
|
|
|
|
this->transform();
|
2013-12-12 13:21:14 +08:00
|
|
|
|
|
|
|
if(_gridTarget)
|
|
|
|
{
|
|
|
|
_gridTarget->visit();
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
// self draw
|
|
|
|
this->draw();
|
|
|
|
|
|
|
|
// Uses std::for_each to improve performance.
|
|
|
|
std::for_each(_children.cbegin()+i, _children.cend(), [](Node* node){
|
|
|
|
node->visit();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this->draw();
|
|
|
|
}
|
|
|
|
|
|
|
|
// reset for next frame
|
|
|
|
_orderOfArrival = 0;
|
|
|
|
|
|
|
|
if (_nodeGrid && _nodeGrid->isActive())
|
|
|
|
{
|
|
|
|
_nodeGrid->afterDraw(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
kmGLPopMatrix();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_CC_END
|