axmol/extensions/fairygui/display/FUIContainer.cpp

527 lines
17 KiB
C++
Raw Normal View History

2020-08-04 12:31:33 +08:00
#include "FUIContainer.h"
#include "base/CCStencilStateManager.h"
#include "utils/ToolSet.h"
#include "GComponent.h"
NS_FGUI_BEGIN
USING_NS_AX;
2020-08-04 12:31:33 +08:00
#if COCOS2D_VERSION < 0x00040000
2022-07-16 10:43:05 +08:00
#if (AX_TARGET_PLATFORM == AX_PLATFORM_MAC || AX_TARGET_PLATFORM == AX_PLATFORM_WIN32 || AX_TARGET_PLATFORM == AX_PLATFORM_LINUX)
#define AX_CLIPPING_NODE_OPENGLES 0
2020-08-04 12:31:33 +08:00
#else
2022-07-16 10:43:05 +08:00
#define AX_CLIPPING_NODE_OPENGLES 1
2020-08-04 12:31:33 +08:00
#endif
2022-07-16 10:43:05 +08:00
#if AX_CLIPPING_NODE_OPENGLES
2020-08-04 12:31:33 +08:00
static void setProgram(Node *n, GLProgram *p)
{
n->setGLProgram(p);
auto& children = n->getChildren();
for (const auto &child : children) {
setProgram(child, p);
}
}
#endif
#endif // COCOS2D_VERSION < 0x00040000
RectClippingSupport::RectClippingSupport() :
_clippingEnabled(false),
_scissorOldState(false),
_clippingRectDirty(true)
{
}
StencilClippingSupport::StencilClippingSupport() :
_stencil(nullptr),
_originStencilProgram(nullptr),
_stencilStateManager(new StencilStateManager())
{
}
FUIContainer::FUIContainer() :
_rectClippingSupport(nullptr),
_stencilClippingSupport(nullptr),
gOwner(nullptr)
{
}
FUIContainer::~FUIContainer()
{
2022-07-16 10:43:05 +08:00
AX_SAFE_DELETE(_rectClippingSupport);
2020-08-04 12:31:33 +08:00
if (_stencilClippingSupport)
{
if (_stencilClippingSupport->_stencil)
{
_stencilClippingSupport->_stencil->stopAllActions();
_stencilClippingSupport->_stencil->release();
}
2022-07-16 10:43:05 +08:00
AX_SAFE_DELETE(_stencilClippingSupport->_stencilStateManager);
2020-08-04 12:31:33 +08:00
delete _stencilClippingSupport;
}
}
bool FUIContainer::isClippingEnabled() const
{
if (_rectClippingSupport != nullptr)
return _rectClippingSupport->_clippingEnabled;
else
return false;
}
void FUIContainer::setClippingEnabled(bool value)
{
if (_rectClippingSupport == nullptr)
{
if (!value)
return;
_rectClippingSupport = new RectClippingSupport();
}
_rectClippingSupport->_clippingEnabled = value;
}
const Rect & FUIContainer::getClippingRegion() const
{
if (_rectClippingSupport != nullptr)
return _rectClippingSupport->_clippingRegion;
else
return Rect::ZERO;
}
void FUIContainer::setClippingRegion(const Rect & clippingRegion)
{
if (_rectClippingSupport == nullptr)
_rectClippingSupport = new RectClippingSupport();
_rectClippingSupport->_clippingRegion = clippingRegion;
}
2022-08-08 18:02:17 +08:00
ax::Node * FUIContainer::getStencil() const
2020-08-04 12:31:33 +08:00
{
if (_stencilClippingSupport != nullptr)
return _stencilClippingSupport->_stencil;
else
return nullptr;
}
2022-08-08 18:02:17 +08:00
void FUIContainer::setStencil(ax::Node * stencil)
2020-08-04 12:31:33 +08:00
{
if (_stencilClippingSupport == nullptr)
{
if (stencil == nullptr)
return;
_stencilClippingSupport = new StencilClippingSupport();
}
//early out if the stencil is already set
if (_stencilClippingSupport->_stencil == stencil)
return;
2022-07-16 10:43:05 +08:00
#if AX_ENABLE_GC_FOR_NATIVE_OBJECTS
2020-08-04 12:31:33 +08:00
auto sEngine = ScriptEngineManager::getInstance()->getScriptEngine();
if (sEngine)
{
if (_stencilClippingSupport->_stencil)
sEngine->releaseScriptObject(this, _stencilClippingSupport->_stencil);
if (stencil)
sEngine->retainScriptObject(this, stencil);
}
2022-07-16 10:43:05 +08:00
#endif // AX_ENABLE_GC_FOR_NATIVE_OBJECTS
2020-08-04 12:31:33 +08:00
//cleanup current stencil
if (_stencilClippingSupport->_stencil != nullptr && _stencilClippingSupport->_stencil->isRunning())
{
_stencilClippingSupport->_stencil->onExitTransitionDidStart();
_stencilClippingSupport->_stencil->onExit();
}
2022-07-16 10:43:05 +08:00
AX_SAFE_RELEASE_NULL(_stencilClippingSupport->_stencil);
2020-08-04 12:31:33 +08:00
//initialise new stencil
_stencilClippingSupport->_stencil = stencil;
2022-07-16 10:43:05 +08:00
AX_SAFE_RETAIN(_stencilClippingSupport->_stencil);
2020-08-04 12:31:33 +08:00
if (_stencilClippingSupport->_stencil != nullptr && this->isRunning())
{
_stencilClippingSupport->_stencil->onEnter();
if (this->_isTransitionFinished)
{
_stencilClippingSupport->_stencil->onEnterTransitionDidFinish();
}
}
if (_stencilClippingSupport->_stencil != nullptr)
#if COCOS2D_VERSION >= 0x00040000
_stencilClippingSupport->_originStencilProgram = _stencilClippingSupport->_stencil->getProgramState();
#else
_stencilClippingSupport->_originStencilProgram = _stencilClippingSupport->_stencil->getGLProgram();
#endif
}
float FUIContainer::getAlphaThreshold() const
{
if (_stencilClippingSupport != nullptr)
return _stencilClippingSupport->_stencilStateManager->getAlphaThreshold();
else
return 1;
}
void FUIContainer::setAlphaThreshold(float alphaThreshold)
{
if (_stencilClippingSupport == nullptr)
_stencilClippingSupport = new StencilClippingSupport();
#if COCOS2D_VERSION >= 0x00040000
if (alphaThreshold == 1 && alphaThreshold != _stencilClippingSupport->_stencilStateManager->getAlphaThreshold()) {
if (_stencilClippingSupport->_stencil) {
restoreAllProgramStates();
}
}
#else
2022-07-16 10:43:05 +08:00
#if AX_CLIPPING_NODE_OPENGLES
2020-08-04 12:31:33 +08:00
if (alphaThreshold == 1 && alphaThreshold != _stencilClippingSupport->_stencilStateManager->getAlphaThreshold())
{
// should reset program used by _stencil
if (_stencilClippingSupport->_stencil)
setProgram(_stencilClippingSupport->_stencil, _stencilClippingSupport->_originStencilProgram);
}
#endif
#endif
_stencilClippingSupport->_stencilStateManager->setAlphaThreshold(alphaThreshold);
}
bool FUIContainer::isInverted() const
{
if (_stencilClippingSupport != nullptr)
return _stencilClippingSupport->_stencilStateManager->isInverted();
else
return false;
}
void FUIContainer::setInverted(bool inverted)
{
if (_stencilClippingSupport == nullptr)
_stencilClippingSupport = new StencilClippingSupport();
_stencilClippingSupport->_stencilStateManager->setInverted(inverted);
}
void FUIContainer::onEnter()
{
2022-07-16 10:43:05 +08:00
#if AX_ENABLE_SCRIPT_BINDING && COCOS2D_VERSION < 0x00040000
2020-08-04 12:31:33 +08:00
if (_scriptType == kScriptTypeJavascript)
{
if (ScriptEngineManager::sendNodeEventToJSExtended(this, kNodeOnEnter))
return;
}
#endif
Node::onEnter();
if (_stencilClippingSupport != nullptr && _stencilClippingSupport->_stencil != nullptr)
_stencilClippingSupport->_stencil->onEnter();
if (_rectClippingSupport != nullptr)
_rectClippingSupport->_clippingRectDirty = true;
}
void FUIContainer::onEnterTransitionDidFinish()
{
2022-07-16 10:43:05 +08:00
#if AX_ENABLE_SCRIPT_BINDING && COCOS2D_VERSION < 0x00040000
2020-08-04 12:31:33 +08:00
if (_scriptType == kScriptTypeJavascript)
{
if (ScriptEngineManager::sendNodeEventToJSExtended(this, kNodeOnEnterTransitionDidFinish))
return;
}
#endif
Node::onEnterTransitionDidFinish();
if (_stencilClippingSupport != nullptr && _stencilClippingSupport->_stencil != nullptr)
{
_stencilClippingSupport->_stencil->onEnterTransitionDidFinish();
}
}
void FUIContainer::onExitTransitionDidStart()
{
2022-07-16 10:43:05 +08:00
#if AX_ENABLE_SCRIPT_BINDING && COCOS2D_VERSION < 0x00040000
2020-08-04 12:31:33 +08:00
if (_scriptType == kScriptTypeJavascript)
{
if (ScriptEngineManager::sendNodeEventToJSExtended(this, kNodeOnExitTransitionDidStart))
return;
}
#endif
if (_stencilClippingSupport != nullptr && _stencilClippingSupport->_stencil != nullptr)
_stencilClippingSupport->_stencil->onExitTransitionDidStart();
Node::onExitTransitionDidStart();
}
void FUIContainer::onExit()
{
2022-07-16 10:43:05 +08:00
#if AX_ENABLE_SCRIPT_BINDING && COCOS2D_VERSION < 0x00040000
2020-08-04 12:31:33 +08:00
if (_scriptType == kScriptTypeJavascript)
{
if (ScriptEngineManager::sendNodeEventToJSExtended(this, kNodeOnExit))
return;
}
#endif
if (_stencilClippingSupport != nullptr && _stencilClippingSupport->_stencil != nullptr)
_stencilClippingSupport->_stencil->onExit();
2020-08-04 12:31:33 +08:00
Node::onExit();
}
void FUIContainer::setCameraMask(unsigned short mask, bool applyChildren)
{
Node::setCameraMask(mask, applyChildren);
if (_stencilClippingSupport != nullptr && _stencilClippingSupport->_stencil != nullptr)
_stencilClippingSupport->_stencil->setCameraMask(mask, applyChildren);
}
#if COCOS2D_VERSION >= 0x00040000
void FUIContainer::setProgramStateRecursively(Node* node, backend::ProgramState* programState)
{
_originalStencilProgramState[node] = node->getProgramState();
node->setProgramState(programState);
auto& children = node->getChildren();
for (const auto &child : children) {
setProgramStateRecursively(child, programState);
}
}
void FUIContainer::restoreAllProgramStates()
{
for (auto item : _originalStencilProgramState)
{
auto node = item.first;
auto programState = item.second;
node->setProgramState(programState);
}
}
#endif
void FUIContainer::onBeforeVisitScissor()
{
auto glView = Director::getInstance()->getOpenGLView();
_rectClippingSupport->_scissorOldState = glView->isScissorEnabled();
2020-08-04 12:31:33 +08:00
Rect clippingRect = getClippingRect();
if (false == _rectClippingSupport->_scissorOldState)
{
#if COCOS2D_VERSION >= 0x00040000
Director::getInstance()->getRenderer()->setScissorTest(true);
#else
glEnable(GL_SCISSOR_TEST);
#endif
}
else
{
_rectClippingSupport->_clippingOldRect = glView->getScissorRect();
2020-08-04 12:31:33 +08:00
clippingRect = ToolSet::intersection(clippingRect, _rectClippingSupport->_clippingOldRect);
}
glView->setScissorInPoints(clippingRect.origin.x,
2020-08-04 12:31:33 +08:00
clippingRect.origin.y,
clippingRect.size.width,
clippingRect.size.height);
}
void FUIContainer::onAfterVisitScissor()
{
if (_rectClippingSupport->_scissorOldState)
{
auto glView = Director::getInstance()->getOpenGLView();
glView->setScissorInPoints(_rectClippingSupport->_clippingOldRect.origin.x,
2020-08-04 12:31:33 +08:00
_rectClippingSupport->_clippingOldRect.origin.y,
_rectClippingSupport->_clippingOldRect.size.width,
_rectClippingSupport->_clippingOldRect.size.height);
}
else
{
// revert scissor test
#if COCOS2D_VERSION >= 0x00040000
Director::getInstance()->getRenderer()->setScissorTest(false);
#else
glDisable(GL_SCISSOR_TEST);
#endif
}
}
const Rect& FUIContainer::getClippingRect()
{
if (_rectClippingSupport->_clippingRectDirty)
{
Vec2 worldPos = convertToWorldSpaceAR(_rectClippingSupport->_clippingRegion.origin);
AffineTransform t = getNodeToWorldAffineTransform();
float scissorWidth = _rectClippingSupport->_clippingRegion.size.width*t.a;
float scissorHeight = _rectClippingSupport->_clippingRegion.size.height*t.d;
_rectClippingSupport->_clippingRect.setRect(worldPos.x - (scissorWidth * _anchorPoint.x), worldPos.y - (scissorHeight * _anchorPoint.y), scissorWidth, scissorHeight);
_rectClippingSupport->_clippingRectDirty = false;
}
return _rectClippingSupport->_clippingRect;
}
2022-08-08 18:02:17 +08:00
void FUIContainer::visit(ax::Renderer * renderer, const ax::Mat4 & parentTransform, uint32_t parentFlags)
2020-08-04 12:31:33 +08:00
{
if (_stencilClippingSupport != nullptr)
{
if (!_visible || _children.empty())
return;
uint32_t flags = processParentFlags(parentTransform, parentFlags);
// 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
Director* director = Director::getInstance();
2022-07-16 10:43:05 +08:00
AXASSERT(nullptr != director, "Director is null when setting matrix stack");
2020-08-04 12:31:33 +08:00
director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW, _modelViewTransform);
//Add group command
#if defined(AX_VERSION)
auto* stencilGroupCommand = renderer->getNextGroupCommand();
stencilGroupCommand->init(_globalZOrder);
renderer->addCommand(stencilGroupCommand);
renderer->pushGroup(stencilGroupCommand->getRenderQueueID());
#else
2020-08-04 12:31:33 +08:00
_stencilClippingSupport->_groupCommand.init(_globalZOrder);
renderer->addCommand(&_stencilClippingSupport->_groupCommand);
renderer->pushGroup(_stencilClippingSupport->_groupCommand.getRenderQueueID());
#endif
2020-08-04 12:31:33 +08:00
#if COCOS2D_VERSION >= 0x00040000
_stencilClippingSupport->_stencilStateManager->onBeforeVisit(_globalZOrder);
#else
_stencilClippingSupport->_beforeVisitCmd.init(_globalZOrder);
2022-07-16 10:43:05 +08:00
_stencilClippingSupport->_beforeVisitCmd.func = AX_CALLBACK_0(StencilStateManager::onBeforeVisit, _stencilClippingSupport->_stencilStateManager);
2020-08-04 12:31:33 +08:00
renderer->addCommand(&_stencilClippingSupport->_beforeVisitCmd);
#endif
auto alphaThreshold = this->getAlphaThreshold();
if (alphaThreshold < 1)
{
#if COCOS2D_VERSION >= 0x00040000
auto* program = backend::Program::getBuiltinProgram(backend::ProgramType::POSITION_TEXTURE_COLOR_ALPHA_TEST);
2021-12-08 00:11:53 +08:00
auto programState = new backend::ProgramState(program);
2020-08-04 12:31:33 +08:00
auto alphaLocation = programState->getUniformLocation("u_alpha_value");
programState->setUniform(alphaLocation, &alphaThreshold, sizeof(alphaThreshold));
setProgramStateRecursively(_stencilClippingSupport->_stencil, programState);
2022-07-16 10:43:05 +08:00
AX_SAFE_RELEASE_NULL(programState);
2020-08-04 12:31:33 +08:00
#else
2022-07-16 10:43:05 +08:00
#if AX_CLIPPING_NODE_OPENGLES
2020-08-04 12:31:33 +08:00
// since glAlphaTest do not exists in OES, use a shader that writes
// pixel only if greater than an alpha threshold
GLProgram *program = GLProgramCache::getInstance()->getGLProgram(GLProgram::SHADER_NAME_POSITION_TEXTURE_ALPHA_TEST_NO_MV);
GLint alphaValueLocation = glGetUniformLocation(program->getProgram(), GLProgram::UNIFORM_NAME_ALPHA_TEST_VALUE);
// set our alphaThreshold
program->use();
program->setUniformLocationWith1f(alphaValueLocation, alphaThreshold);
// we need to recursively apply this shader to all the nodes in the stencil node
// FIXME: we should have a way to apply shader to all nodes without having to do this
setProgram(_stencilClippingSupport->_stencil, program);
#endif
#endif
}
_stencilClippingSupport->_stencil->visit(renderer, _modelViewTransform, flags);
2022-06-24 14:18:48 +08:00
auto afterDrawStencilCmd = renderer->nextCallbackCommand();
afterDrawStencilCmd->init(_globalZOrder);
2022-07-16 10:43:05 +08:00
afterDrawStencilCmd->func = AX_CALLBACK_0(StencilStateManager::onAfterDrawStencil, _stencilClippingSupport->_stencilStateManager);
2022-06-24 14:18:48 +08:00
renderer->addCommand(afterDrawStencilCmd);
2020-08-04 12:31:33 +08:00
int i = 0;
bool visibleByCamera = isVisitableByVisitingCamera();
if (!_children.empty())
{
sortAllChildren();
// draw children zOrder < 0
for (auto size = _children.size(); i < size; ++i)
{
auto node = _children.at(i);
if (node && node->getLocalZOrder() < 0)
node->visit(renderer, _modelViewTransform, flags);
else
break;
}
// self draw
if (visibleByCamera)
this->draw(renderer, _modelViewTransform, flags);
for (auto it = _children.cbegin() + i, itCend = _children.cend(); it != itCend; ++it)
(*it)->visit(renderer, _modelViewTransform, flags);
}
else if (visibleByCamera)
{
this->draw(renderer, _modelViewTransform, flags);
}
2022-06-24 14:18:48 +08:00
auto afterVisitCmd = renderer->nextCallbackCommand();
afterVisitCmd->init(_globalZOrder);
2022-07-16 10:43:05 +08:00
afterVisitCmd->func = AX_CALLBACK_0(StencilStateManager::onAfterVisit, _stencilClippingSupport->_stencilStateManager);
2022-06-24 14:18:48 +08:00
renderer->addCommand(afterVisitCmd);
2020-08-04 12:31:33 +08:00
renderer->popGroup();
director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
}
else if (_rectClippingSupport != nullptr && _rectClippingSupport->_clippingEnabled)
{
if (_transformUpdated || _contentSizeDirty || (parentFlags & FLAGS_DIRTY_MASK))
2020-08-04 12:31:33 +08:00
{
_rectClippingSupport->_clippingRectDirty = true;
}
#if defined(AX_VERSION)
auto* rectClippingGroupCommand = renderer->getNextGroupCommand();
rectClippingGroupCommand->init(_globalZOrder);
renderer->addCommand(rectClippingGroupCommand);
renderer->pushGroup(rectClippingGroupCommand->getRenderQueueID());
#else
# if COCOS2D_VERSION >= 0x00040000
2020-08-04 12:31:33 +08:00
_rectClippingSupport->_groupCommand.init(_globalZOrder);
renderer->addCommand(&_rectClippingSupport->_groupCommand);
renderer->pushGroup(_rectClippingSupport->_groupCommand.getRenderQueueID());
# endif
2020-08-04 12:31:33 +08:00
#endif
2022-06-24 14:18:48 +08:00
auto beforeVisitCmdScissor = renderer->nextCallbackCommand();
beforeVisitCmdScissor->init(_globalZOrder);
2022-07-16 10:43:05 +08:00
beforeVisitCmdScissor->func = AX_CALLBACK_0(FUIContainer::onBeforeVisitScissor, this);
2022-06-24 14:18:48 +08:00
renderer->addCommand(beforeVisitCmdScissor);
2020-08-04 12:31:33 +08:00
Node::visit(renderer, parentTransform, parentFlags);
2022-06-24 14:18:48 +08:00
auto afterVisitCmdScissor = renderer->nextCallbackCommand();
afterVisitCmdScissor->init(_globalZOrder);
2022-07-16 10:43:05 +08:00
afterVisitCmdScissor->func = AX_CALLBACK_0(FUIContainer::onAfterVisitScissor, this);
2022-06-24 14:18:48 +08:00
renderer->addCommand(afterVisitCmdScissor);
2020-08-04 12:31:33 +08:00
#if COCOS2D_VERSION >= 0x00040000
renderer->popGroup();
#endif
}
else
Node::visit(renderer, parentTransform, parentFlags);
}
void FUIContainer::setGlobalZOrder(float globalZOrder)
{
Node::setGlobalZOrder(globalZOrder);
if (_stencilClippingSupport && _stencilClippingSupport->_stencil)
{
_stencilClippingSupport->_stencil->setGlobalZOrder(globalZOrder);
}
}
2020-08-06 19:58:24 +08:00
NS_FGUI_END