2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
|
|
|
|
2022-08-08 18:02:17 +08:00
|
|
|
https://axys1.github.io/
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
#include "2d/CCNodeGrid.h"
|
|
|
|
#include "2d/CCGrid.h"
|
|
|
|
#include "renderer/CCRenderer.h"
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BEGIN
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
NodeGrid* NodeGrid::create()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
NodeGrid* ret = new NodeGrid();
|
2021-12-08 00:11:53 +08:00
|
|
|
if (ret->init())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
ret->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_DELETE(ret);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-08-08 18:02:17 +08:00
|
|
|
NodeGrid* NodeGrid::create(const ax::Rect& rect)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
NodeGrid* ret = NodeGrid::create();
|
2021-12-25 10:04:45 +08:00
|
|
|
if (ret)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
ret->setGridRect(rect);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
NodeGrid::NodeGrid() {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
void NodeGrid::setTarget(Node* target)
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
#if AX_ENABLE_GC_FOR_NATIVE_OBJECTS
|
2019-11-23 20:27:39 +08:00
|
|
|
auto sEngine = ScriptEngineManager::getInstance()->getScriptEngine();
|
|
|
|
if (sEngine)
|
|
|
|
{
|
|
|
|
if (_gridTarget)
|
|
|
|
sEngine->releaseScriptObject(this, _gridTarget);
|
|
|
|
if (target)
|
|
|
|
sEngine->retainScriptObject(this, target);
|
|
|
|
}
|
2022-07-16 10:43:05 +08:00
|
|
|
#endif // AX_ENABLE_GC_FOR_NATIVE_OBJECTS
|
|
|
|
AX_SAFE_RELEASE(_gridTarget);
|
|
|
|
AX_SAFE_RETAIN(target);
|
2019-11-23 20:27:39 +08:00
|
|
|
_gridTarget = target;
|
|
|
|
}
|
|
|
|
|
|
|
|
NodeGrid::~NodeGrid()
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_RELEASE(_nodeGrid);
|
|
|
|
AX_SAFE_RELEASE(_gridTarget);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void NodeGrid::onGridBeginDraw()
|
|
|
|
{
|
|
|
|
if (_nodeGrid && _nodeGrid->isActive())
|
|
|
|
{
|
|
|
|
_nodeGrid->beforeDraw();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void NodeGrid::onGridEndDraw()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_nodeGrid && _nodeGrid->isActive())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_nodeGrid->afterDraw(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void NodeGrid::visit(Renderer* renderer, const Mat4& parentTransform, uint32_t parentFlags)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
// quick return if not visible. children won't be drawn.
|
|
|
|
if (!_visible)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool dirty = (parentFlags & FLAGS_TRANSFORM_DIRTY) || _transformUpdated;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (dirty)
|
2019-11-23 20:27:39 +08:00
|
|
|
_modelViewTransform = this->transform(parentTransform);
|
|
|
|
_transformUpdated = false;
|
|
|
|
|
|
|
|
// IMPORTANT:
|
|
|
|
// To ease the migration to v3.0, we still support the Mat4 stack,
|
|
|
|
// but it is deprecated and your code should not rely on it
|
2021-04-22 22:01:47 +08:00
|
|
|
_director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
|
|
|
|
_director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW, _modelViewTransform);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
Director::Projection beforeProjectionType = Director::Projection::DEFAULT;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_nodeGrid && _nodeGrid->isActive())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-04-22 22:01:47 +08:00
|
|
|
beforeProjectionType = _director->getProjection();
|
2019-11-23 20:27:39 +08:00
|
|
|
_nodeGrid->set2DProjection();
|
|
|
|
}
|
|
|
|
|
|
|
|
onGridBeginDraw();
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_gridTarget)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_gridTarget->visit(renderer, _modelViewTransform, dirty);
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
int i = 0;
|
2019-11-23 20:27:39 +08:00
|
|
|
bool visibleByCamera = isVisitableByVisitingCamera();
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!_children.empty())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
sortAllChildren();
|
|
|
|
// draw children zOrder < 0
|
2021-12-25 10:04:45 +08:00
|
|
|
for (auto size = _children.size(); i < size; ++i)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
auto node = _children.at(i);
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (node && node->getLocalZOrder() < 0)
|
2019-11-23 20:27:39 +08:00
|
|
|
node->visit(renderer, _modelViewTransform, dirty);
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// self draw,currently we have nothing to draw on NodeGrid, so there is no need to add render command
|
|
|
|
if (visibleByCamera)
|
|
|
|
this->draw(renderer, _modelViewTransform, dirty);
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
for (auto it = _children.cbegin() + i, itCend = _children.cend(); it != itCend; ++it)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
(*it)->visit(renderer, _modelViewTransform, dirty);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (visibleByCamera)
|
|
|
|
{
|
|
|
|
this->draw(renderer, _modelViewTransform, dirty);
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// FIX ME: Why need to set _orderOfArrival to 0??
|
|
|
|
// Please refer to https://github.com/cocos2d/cocos2d-x/pull/6920
|
|
|
|
// setOrderOfArrival(0);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
if (_nodeGrid && _nodeGrid->isActive())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
// restore projection
|
2021-04-22 22:01:47 +08:00
|
|
|
_director->setProjection(beforeProjectionType);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
onGridEndDraw();
|
|
|
|
|
2021-04-22 22:01:47 +08:00
|
|
|
_director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void NodeGrid::setGrid(GridBase* grid)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_RELEASE(_nodeGrid);
|
|
|
|
AX_SAFE_RETAIN(grid);
|
2019-11-23 20:27:39 +08:00
|
|
|
_nodeGrid = grid;
|
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_END
|