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
|
|
|
|
2013-12-16 14:21:05 +08:00
|
|
|
#include "CCNodeGrid.h"
|
2013-12-10 10:50:31 +08:00
|
|
|
#include "CCGrid.h"
|
|
|
|
|
2014-01-17 13:35:58 +08:00
|
|
|
#include "renderer/CCGroupCommand.h"
|
|
|
|
#include "renderer/CCRenderer.h"
|
|
|
|
#include "renderer/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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-01 08:10:48 +08:00
|
|
|
void NodeGrid::visit(Renderer *renderer, const kmMat4 &parentTransform, bool parentTransformUpdated)
|
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-03-01 08:10:48 +08:00
|
|
|
bool dirty = parentTransformUpdated || _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-04-02 22:50:39 +08:00
|
|
|
// To ease the migration to v3.0, we still support the kmMat4 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
|
|
|
|
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
|
|
|
|
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;
|
|
|
|
|
|
|
|
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-02-28 13:43:54 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
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
|
|
|
|
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
|
|
|
|
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
|