axmol/cocos/ui/UILayout.cpp

1897 lines
50 KiB
C++
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
Copyright (c) 2021 Bytedance Inc.
2019-11-23 20:27:39 +08:00
https://adxe.org
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 "ui/UILayout.h"
#include "ui/UIHelper.h"
#include "ui/UIScale9Sprite.h"
#include "renderer/CCRenderState.h"
#include "base/CCDirector.h"
#include "renderer/CCRenderer.h"
#include "ui/UILayoutManager.h"
#include "2d/CCDrawNode.h"
#include "2d/CCLayer.h"
#include "2d/CCSprite.h"
#include "base/CCEventFocus.h"
#include "base/CCStencilStateManager.h"
#include <algorithm>
2019-11-23 20:27:39 +08:00
NS_CC_BEGIN
2021-12-25 10:04:45 +08:00
namespace ui
{
static const int BACKGROUNDIMAGE_Z = (-1);
2019-11-23 20:27:39 +08:00
static const int BCAKGROUNDCOLORRENDERER_Z = (-2);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
IMPLEMENT_CLASS_GUI_INFO(Layout)
2021-12-25 10:04:45 +08:00
Layout::Layout()
: _backGroundScale9Enabled(false)
, _backGroundImage(nullptr)
, _backGroundImageFileName("")
, _backGroundImageCapInsets(Rect::ZERO)
, _colorType(BackGroundColorType::NONE)
, _bgImageTexType(TextureResType::LOCAL)
, _backGroundImageTextureSize(Vec2::ZERO)
, _backGroundImageColor(Color3B::WHITE)
, _backGroundImageOpacity(255)
, _colorRender(nullptr)
, _gradientRender(nullptr)
, _cColor(Color3B::WHITE)
, _gStartColor(Color3B::WHITE)
, _gEndColor(Color3B::WHITE)
, _alongVector(Vec2(0.0f, -1.0f))
, _cOpacity(255)
, _clippingEnabled(false)
, _layoutType(Type::ABSOLUTE)
, _clippingType(ClippingType::STENCIL)
, _clippingStencil(nullptr)
, _clippingRect(Rect::ZERO)
, _clippingParent(nullptr)
, _clippingRectDirty(true)
, _stencilStateManager(new StencilStateManager())
, _doLayoutDirty(true)
, _isInterceptTouch(false)
, _loopFocus(false)
, _passFocusToChild(true)
, _isFocusPassing(false)
{
// no-op
2019-11-23 20:27:39 +08:00
}
Layout::~Layout()
{
CC_SAFE_RELEASE(_clippingStencil);
CC_SAFE_DELETE(_stencilStateManager);
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
void Layout::onEnter()
{
Widget::onEnter();
if (_clippingStencil)
{
_clippingStencil->onEnter();
}
2021-12-25 10:04:45 +08:00
_doLayoutDirty = true;
2019-11-23 20:27:39 +08:00
_clippingRectDirty = true;
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
void Layout::onExit()
{
Widget::onExit();
if (_clippingStencil)
{
_clippingStencil->onExit();
}
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
void Layout::setGlobalZOrder(float globalZOrder)
{
// _protectedChildren's global z order is set in ProtectedNode::setGlobalZOrder()
Widget::setGlobalZOrder(globalZOrder);
if (_clippingStencil)
_clippingStencil->setGlobalZOrder(globalZOrder);
2021-12-25 10:04:45 +08:00
for (auto& child : _children)
2019-11-23 20:27:39 +08:00
child->setGlobalZOrder(globalZOrder);
}
Layout* Layout::create()
{
2021-12-08 00:11:53 +08:00
Layout* layout = new Layout();
if (layout->init())
2019-11-23 20:27:39 +08:00
{
layout->autorelease();
return layout;
}
CC_SAFE_DELETE(layout);
return nullptr;
}
bool Layout::init()
{
if (Widget::init())
{
ignoreContentAdaptWithSize(false);
2021-10-23 23:27:14 +08:00
setContentSize(Vec2::ZERO);
2019-11-23 20:27:39 +08:00
setAnchorPoint(Vec2::ZERO);
onPassFocusToChild = CC_CALLBACK_2(Layout::findNearestChildWidgetIndex, this);
return true;
}
return false;
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
void Layout::addChild(Node* child)
{
Layout::addChild(child, child->getLocalZOrder(), child->getTag());
}
2021-12-25 10:04:45 +08:00
void Layout::addChild(Node* child, int localZOrder)
2019-11-23 20:27:39 +08:00
{
Layout::addChild(child, localZOrder, child->getTag());
}
2021-12-25 10:04:45 +08:00
void Layout::addChild(Node* child, int zOrder, int tag)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
if (dynamic_cast<Widget*>(child))
{
2019-11-23 20:27:39 +08:00
supplyTheLayoutParameterLackToChild(static_cast<Widget*>(child));
}
child->setGlobalZOrder(_globalZOrder);
Widget::addChild(child, zOrder, tag);
_doLayoutDirty = true;
}
2021-12-25 10:04:45 +08:00
2021-12-26 23:26:34 +08:00
void Layout::addChild(Node* child, int zOrder, std::string_view name)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
if (dynamic_cast<Widget*>(child))
{
2019-11-23 20:27:39 +08:00
supplyTheLayoutParameterLackToChild(static_cast<Widget*>(child));
}
child->setGlobalZOrder(_globalZOrder);
Widget::addChild(child, zOrder, name);
_doLayoutDirty = true;
}
2021-12-25 10:04:45 +08:00
void Layout::removeChild(Node* child, bool cleanup)
2019-11-23 20:27:39 +08:00
{
Widget::removeChild(child, cleanup);
_doLayoutDirty = true;
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
void Layout::removeAllChildren()
{
Widget::removeAllChildren();
_doLayoutDirty = true;
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
void Layout::removeAllChildrenWithCleanup(bool cleanup)
{
Widget::removeAllChildrenWithCleanup(cleanup);
_doLayoutDirty = true;
}
2021-12-25 10:04:45 +08:00
bool Layout::isClippingEnabled() const
2019-11-23 20:27:39 +08:00
{
return _clippingEnabled;
}
2021-12-25 10:04:45 +08:00
void Layout::visit(Renderer* renderer, const Mat4& parentTransform, uint32_t parentFlags)
2019-11-23 20:27:39 +08:00
{
if (!_visible)
{
return;
}
2021-12-25 10:04:45 +08:00
if (FLAGS_TRANSFORM_DIRTY & parentFlags || _transformUpdated || _contentSizeDirty)
_clippingRectDirty = true;
2019-11-23 20:27:39 +08:00
adaptRenderers();
doLayout();
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
if (_clippingEnabled)
{
switch (_clippingType)
{
2021-12-25 10:04:45 +08:00
case ClippingType::STENCIL:
stencilClippingVisit(renderer, parentTransform, parentFlags);
break;
case ClippingType::SCISSOR:
scissorClippingVisit(renderer, parentTransform, parentFlags);
break;
default:
break;
2019-11-23 20:27:39 +08:00
}
}
else
{
2021-12-25 10:04:45 +08:00
// no need to adapt render again
ProtectedNode::visit(renderer, parentTransform, parentFlags);
2019-11-23 20:27:39 +08:00
}
}
2021-12-25 10:04:45 +08:00
void Layout::stencilClippingVisit(Renderer* renderer, const Mat4& parentTransform, uint32_t parentFlags)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
if (!_visible)
2019-11-23 20:27:39 +08:00
return;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
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->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
_director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW, _modelViewTransform);
2021-12-25 10:04:45 +08:00
// Add group command
2019-11-23 20:27:39 +08:00
_groupCommand.init(_globalZOrder);
renderer->addCommand(&_groupCommand);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
renderer->pushGroup(_groupCommand.getRenderQueueID());
2021-12-25 10:04:45 +08:00
// _beforeVisitCmdStencil.init(_globalZOrder);
// _beforeVisitCmdStencil.func = CC_CALLBACK_0(StencilStateManager::onBeforeVisit, _stencilStateManager);
// renderer->addCommand(&_beforeVisitCmdStencil);
2019-11-23 20:27:39 +08:00
_stencilStateManager->onBeforeVisit(_globalZOrder);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
_clippingStencil->visit(renderer, _modelViewTransform, flags);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
_afterDrawStencilCmd.init(_globalZOrder);
_afterDrawStencilCmd.func = CC_CALLBACK_0(StencilStateManager::onAfterDrawStencil, _stencilStateManager);
renderer->addCommand(&_afterDrawStencilCmd);
2021-12-25 10:04:45 +08:00
int i = 0; // used by _children
int j = 0; // used by _protectedChildren
2019-11-23 20:27:39 +08:00
sortAllChildren();
sortAllProtectedChildren();
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
//
// draw children and protectedChildren 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
2019-11-23 20:27:39 +08:00
if (node && node->getLocalZOrder() < 0)
node->visit(renderer, _modelViewTransform, flags);
else
break;
}
2021-12-25 10:04:45 +08:00
for (auto size = _protectedChildren.size(); j < size; j++)
2019-11-23 20:27:39 +08:00
{
auto node = _protectedChildren.at(j);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
if (node && node->getLocalZOrder() < 0)
node->visit(renderer, _modelViewTransform, flags);
else
break;
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
//
// draw self
//
this->draw(renderer, _modelViewTransform, flags);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
//
// draw children and protectedChildren zOrder >= 0
//
2021-12-25 10:04:45 +08:00
for (auto it = _protectedChildren.cbegin() + j, itCend = _protectedChildren.cend(); it != itCend; ++it)
2019-11-23 20:27:39 +08:00
(*it)->visit(renderer, _modelViewTransform, flags);
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, flags);
_afterVisitCmdStencil.init(_globalZOrder);
_afterVisitCmdStencil.func = CC_CALLBACK_0(StencilStateManager::onAfterVisit, _stencilStateManager);
renderer->addCommand(&_afterVisitCmdStencil);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
renderer->popGroup();
2021-12-25 10:04:45 +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
2019-11-23 20:27:39 +08:00
void Layout::onBeforeVisitScissor()
{
auto glview = _director->getOpenGLView();
2019-11-23 20:27:39 +08:00
// apply scissor test
_scissorOldState = glview->isScissorEnabled();
if (false == _scissorOldState)
{
auto renderer = _director->getRenderer();
2019-11-23 20:27:39 +08:00
renderer->setScissorTest(true);
}
// apply scissor box
Rect clippingRect = getClippingRect();
2021-12-25 10:04:45 +08:00
_clippingOldRect = glview->getScissorRect();
2019-11-23 20:27:39 +08:00
if (false == _clippingOldRect.equals(clippingRect))
{
2021-12-25 10:04:45 +08:00
glview->setScissorInPoints(clippingRect.origin.x, clippingRect.origin.y, clippingRect.size.width,
2019-11-23 20:27:39 +08:00
clippingRect.size.height);
}
}
void Layout::onAfterVisitScissor()
{
if (_scissorOldState)
{
// revert scissor box
if (false == _clippingOldRect.equals(_clippingRect))
{
auto glview = _director->getOpenGLView();
2021-12-25 10:04:45 +08:00
glview->setScissorInPoints(_clippingOldRect.origin.x, _clippingOldRect.origin.y,
_clippingOldRect.size.width, _clippingOldRect.size.height);
2019-11-23 20:27:39 +08:00
}
}
else
{
// revert scissor test
auto renderer = _director->getRenderer();
2019-11-23 20:27:39 +08:00
renderer->setScissorTest(false);
}
}
2021-12-25 10:04:45 +08:00
void Layout::scissorClippingVisit(Renderer* renderer, const Mat4& parentTransform, uint32_t parentFlags)
2019-11-23 20:27:39 +08:00
{
if (parentFlags & FLAGS_DIRTY_MASK)
{
_clippingRectDirty = true;
}
2021-12-25 10:04:45 +08:00
_director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
_director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW, _modelViewTransform);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
_groupCommand.init(_globalZOrder);
renderer->addCommand(&_groupCommand);
renderer->pushGroup(_groupCommand.getRenderQueueID());
_beforeVisitCmdScissor.init(_globalZOrder);
_beforeVisitCmdScissor.func = CC_CALLBACK_0(Layout::onBeforeVisitScissor, this);
renderer->addCommand(&_beforeVisitCmdScissor);
ProtectedNode::visit(renderer, parentTransform, parentFlags);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
_afterVisitCmdScissor.init(_globalZOrder);
_afterVisitCmdScissor.func = CC_CALLBACK_0(Layout::onAfterVisitScissor, this);
renderer->addCommand(&_afterVisitCmdScissor);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
renderer->popGroup();
_director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
2019-11-23 20:27:39 +08:00
}
void Layout::setClippingEnabled(bool able)
{
if (able == _clippingEnabled)
{
return;
}
_clippingEnabled = able;
switch (_clippingType)
{
2021-12-25 10:04:45 +08:00
case ClippingType::STENCIL:
if (able)
{
_clippingStencil = DrawNode::create();
_clippingStencil->setGlobalZOrder(_globalZOrder);
if (_running)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
_clippingStencil->onEnter();
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
_clippingStencil->retain();
setStencilClippingSize(_contentSize);
}
else
{
if (_running)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
_clippingStencil->onExit();
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
_clippingStencil->release();
_clippingStencil = nullptr;
}
break;
default:
break;
2019-11-23 20:27:39 +08:00
}
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
void Layout::setClippingType(ClippingType type)
{
if (type == _clippingType)
{
return;
}
bool clippingEnabled = isClippingEnabled();
setClippingEnabled(false);
_clippingType = type;
setClippingEnabled(clippingEnabled);
}
2021-12-25 10:04:45 +08:00
Layout::ClippingType Layout::getClippingType() const
2019-11-23 20:27:39 +08:00
{
return _clippingType;
}
2021-12-25 10:04:45 +08:00
2021-10-23 23:27:14 +08:00
void Layout::setStencilClippingSize(const Vec2& /*size*/)
2019-11-23 20:27:39 +08:00
{
if (_clippingEnabled && _clippingType == ClippingType::STENCIL)
{
Vec2 rect[4];
// rect[0].setZero(); Zero default
rect[1].set(_contentSize.width, 0.0f);
rect[2].set(_contentSize.width, _contentSize.height);
rect[3].set(0.0f, _contentSize.height);
Color4F green(0.0f, 1.0f, 0.0f, 1.0f);
_clippingStencil->clear();
_clippingStencil->drawPolygon(rect, 4, green, 0, green);
}
}
2021-12-25 10:04:45 +08:00
const Rect& Layout::getClippingRect()
2019-11-23 20:27:39 +08:00
{
if (_clippingRectDirty)
{
const auto worldPos1 = convertToWorldSpace(Vec2::ZERO);
const auto worldPos2 = convertToWorldSpace(Vec2(_contentSize.width, _contentSize.height));
2021-12-25 10:04:45 +08:00
// Node can be flipped
const auto worldPos = Vec2(std::min(worldPos1.x, worldPos2.x), std::min(worldPos1.y, worldPos2.y));
const auto scissorWidth = std::fabs(worldPos2.x - worldPos1.x);
const auto scissorHeight = std::fabs(worldPos2.y - worldPos1.y);
2019-11-23 20:27:39 +08:00
Layout* parent = this;
while (parent)
{
parent = dynamic_cast<Layout*>(parent->getParent());
if (parent)
2019-11-23 20:27:39 +08:00
{
if (parent->isClippingEnabled())
{
_clippingParent = parent;
break;
}
}
}
2019-11-23 20:27:39 +08:00
if (_clippingParent)
{
const auto& parentClippingRect = _clippingParent->getClippingRect();
_clippingRect.origin.x = std::max(parentClippingRect.origin.x, worldPos.x);
_clippingRect.origin.y = std::max(parentClippingRect.origin.y, worldPos.y);
2021-12-25 10:04:45 +08:00
const auto right =
std::min(parentClippingRect.origin.x + parentClippingRect.size.width, worldPos.x + scissorWidth);
const auto top =
std::min(parentClippingRect.origin.y + parentClippingRect.size.height, worldPos.y + scissorHeight);
2021-12-25 10:04:45 +08:00
_clippingRect.size.width = std::max(0.0f, right - _clippingRect.origin.x);
_clippingRect.size.height = std::max(0.0f, top - _clippingRect.origin.y);
2019-11-23 20:27:39 +08:00
}
else
{
2021-12-25 10:04:45 +08:00
_clippingRect.origin.x = worldPos.x;
_clippingRect.origin.y = worldPos.y;
_clippingRect.size.width = scissorWidth;
2019-11-23 20:27:39 +08:00
_clippingRect.size.height = scissorHeight;
}
_clippingRectDirty = false;
}
return _clippingRect;
}
void Layout::onSizeChanged()
{
Widget::onSizeChanged();
setStencilClippingSize(_contentSize);
2021-12-25 10:04:45 +08:00
_doLayoutDirty = true;
2019-11-23 20:27:39 +08:00
_clippingRectDirty = true;
if (_backGroundImage)
{
2021-12-25 10:04:45 +08:00
_backGroundImage->setPosition(_contentSize.width / 2.0f, _contentSize.height / 2.0f);
if (_backGroundScale9Enabled)
{
2019-11-23 20:27:39 +08:00
_backGroundImage->setPreferredSize(_contentSize);
}
2021-12-25 10:04:45 +08:00
else
{
2019-11-23 20:27:39 +08:00
_backGroundImage->setPreferredSize(_backGroundImageTextureSize);
}
}
if (_colorRender)
{
_colorRender->setContentSize(_contentSize);
}
if (_gradientRender)
{
_gradientRender->setContentSize(_contentSize);
}
}
void Layout::setBackGroundImageScale9Enabled(bool able)
{
if (_backGroundScale9Enabled == able)
{
return;
}
_backGroundScale9Enabled = able;
if (nullptr == _backGroundImage)
{
addBackGroundImage();
2021-12-25 10:04:45 +08:00
setBackGroundImage(_backGroundImageFileName, _bgImageTexType);
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
if (_backGroundScale9Enabled)
{
2019-11-23 20:27:39 +08:00
_backGroundImage->setRenderingType(Scale9Sprite::RenderingType::SLICE);
_backGroundImage->setPreferredSize(_contentSize);
2021-12-25 10:04:45 +08:00
}
else
{
2019-11-23 20:27:39 +08:00
_backGroundImage->setRenderingType(Scale9Sprite::RenderingType::SIMPLE);
_backGroundImage->setPreferredSize(_backGroundImageTextureSize);
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
setBackGroundImageCapInsets(_backGroundImageCapInsets);
}
2021-12-25 10:04:45 +08:00
bool Layout::isBackGroundImageScale9Enabled() const
2019-11-23 20:27:39 +08:00
{
return _backGroundScale9Enabled;
}
2021-12-26 23:26:34 +08:00
void Layout::setBackGroundImage(std::string_view fileName, TextureResType texType)
2019-11-23 20:27:39 +08:00
{
if (fileName.empty())
{
return;
}
if (_backGroundImage == nullptr)
{
addBackGroundImage();
2021-12-25 10:04:45 +08:00
if (_backGroundScale9Enabled)
{
2019-11-23 20:27:39 +08:00
_backGroundImage->setRenderingType(Scale9Sprite::RenderingType::SLICE);
2021-12-25 10:04:45 +08:00
}
else
{
2019-11-23 20:27:39 +08:00
_backGroundImage->setRenderingType(Scale9Sprite::RenderingType::SIMPLE);
}
}
_backGroundImageFileName = fileName;
2021-12-25 10:04:45 +08:00
_bgImageTexType = texType;
2019-11-23 20:27:39 +08:00
switch (_bgImageTexType)
{
2021-12-25 10:04:45 +08:00
case TextureResType::LOCAL:
_backGroundImage->initWithFile(fileName);
break;
case TextureResType::PLIST:
_backGroundImage->initWithSpriteFrameName(fileName);
break;
default:
break;
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
_backGroundImageTextureSize = _backGroundImage->getContentSize();
2021-12-25 10:04:45 +08:00
_backGroundImage->setPosition(_contentSize.width / 2.0f, _contentSize.height / 2.0f);
if (_backGroundScale9Enabled)
{
2019-11-23 20:27:39 +08:00
_backGroundImage->setPreferredSize(_contentSize);
}
2021-12-25 10:04:45 +08:00
else
{
2019-11-23 20:27:39 +08:00
_backGroundImage->setPreferredSize(_backGroundImageTextureSize);
}
updateBackGroundImageRGBA();
}
2021-12-25 10:04:45 +08:00
void Layout::setBackGroundImageCapInsets(const Rect& capInsets)
2019-11-23 20:27:39 +08:00
{
_backGroundImageCapInsets = capInsets;
if (_backGroundScale9Enabled && _backGroundImage)
{
_backGroundImage->setCapInsets(capInsets);
}
}
2021-12-25 10:04:45 +08:00
const Rect& Layout::getBackGroundImageCapInsets() const
2019-11-23 20:27:39 +08:00
{
return _backGroundImageCapInsets;
}
2021-12-25 10:04:45 +08:00
void Layout::supplyTheLayoutParameterLackToChild(Widget* child)
2019-11-23 20:27:39 +08:00
{
if (!child)
{
return;
}
switch (_layoutType)
{
2021-12-25 10:04:45 +08:00
case Type::ABSOLUTE:
break;
case Type::HORIZONTAL:
case Type::VERTICAL:
case Type::CENTER_VERTICAL:
{
LinearLayoutParameter* layoutParameter = dynamic_cast<LinearLayoutParameter*>(child->getLayoutParameter());
if (!layoutParameter)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
child->setLayoutParameter(LinearLayoutParameter::create());
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
break;
}
case Type::RELATIVE:
{
RelativeLayoutParameter* layoutParameter = dynamic_cast<RelativeLayoutParameter*>(child->getLayoutParameter());
if (!layoutParameter)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
child->setLayoutParameter(RelativeLayoutParameter::create());
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
break;
}
default:
break;
2019-11-23 20:27:39 +08:00
}
}
void Layout::addBackGroundImage()
{
_backGroundImage = Scale9Sprite::create();
_backGroundImage->setRenderingType(Scale9Sprite::RenderingType::SIMPLE);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
addProtectedChild(_backGroundImage, BACKGROUNDIMAGE_Z, -1);
2021-12-25 10:04:45 +08:00
_backGroundImage->setPosition(_contentSize.width / 2.0f, _contentSize.height / 2.0f);
2019-11-23 20:27:39 +08:00
}
void Layout::removeBackGroundImage()
{
if (!_backGroundImage)
{
return;
}
removeProtectedChild(_backGroundImage);
2021-12-25 10:04:45 +08:00
_backGroundImage = nullptr;
_backGroundImageFileName = "";
2021-10-23 23:27:14 +08:00
_backGroundImageTextureSize = Vec2::ZERO;
2019-11-23 20:27:39 +08:00
}
void Layout::setBackGroundColorType(BackGroundColorType type)
{
if (_colorType == type)
{
return;
}
switch (_colorType)
{
2021-12-25 10:04:45 +08:00
case BackGroundColorType::NONE:
if (_colorRender)
{
removeProtectedChild(_colorRender);
_colorRender = nullptr;
}
if (_gradientRender)
{
removeProtectedChild(_gradientRender);
_gradientRender = nullptr;
}
break;
case BackGroundColorType::SOLID:
if (_colorRender)
{
removeProtectedChild(_colorRender);
_colorRender = nullptr;
}
break;
case BackGroundColorType::GRADIENT:
if (_gradientRender)
{
removeProtectedChild(_gradientRender);
_gradientRender = nullptr;
}
break;
default:
break;
2019-11-23 20:27:39 +08:00
}
_colorType = type;
switch (_colorType)
{
2021-12-25 10:04:45 +08:00
case BackGroundColorType::NONE:
break;
case BackGroundColorType::SOLID:
_colorRender = LayerColor::create();
_colorRender->setContentSize(_contentSize);
_colorRender->setOpacity(_cOpacity);
_colorRender->setColor(_cColor);
addProtectedChild(_colorRender, BCAKGROUNDCOLORRENDERER_Z, -1);
break;
case BackGroundColorType::GRADIENT:
_gradientRender = LayerGradient::create();
_gradientRender->setContentSize(_contentSize);
_gradientRender->setOpacity(_cOpacity);
_gradientRender->setStartColor(_gStartColor);
_gradientRender->setEndColor(_gEndColor);
_gradientRender->setVector(_alongVector);
addProtectedChild(_gradientRender, BCAKGROUNDCOLORRENDERER_Z, -1);
break;
default:
break;
2019-11-23 20:27:39 +08:00
}
}
2021-12-25 10:04:45 +08:00
Layout::BackGroundColorType Layout::getBackGroundColorType() const
2019-11-23 20:27:39 +08:00
{
return _colorType;
}
2021-12-25 10:04:45 +08:00
void Layout::setBackGroundColor(const Color3B& color)
2019-11-23 20:27:39 +08:00
{
_cColor = color;
if (_colorRender)
{
_colorRender->setColor(color);
}
}
2021-12-25 10:04:45 +08:00
const Color3B& Layout::getBackGroundColor() const
2019-11-23 20:27:39 +08:00
{
return _cColor;
}
2021-12-25 10:04:45 +08:00
void Layout::setBackGroundColor(const Color3B& startColor, const Color3B& endColor)
2019-11-23 20:27:39 +08:00
{
_gStartColor = startColor;
if (_gradientRender)
{
_gradientRender->setStartColor(startColor);
}
_gEndColor = endColor;
if (_gradientRender)
{
_gradientRender->setEndColor(endColor);
}
}
2021-12-25 10:04:45 +08:00
const Color3B& Layout::getBackGroundStartColor() const
2019-11-23 20:27:39 +08:00
{
return _gStartColor;
}
2021-12-25 10:04:45 +08:00
const Color3B& Layout::getBackGroundEndColor() const
2019-11-23 20:27:39 +08:00
{
return _gEndColor;
}
void Layout::setBackGroundColorOpacity(uint8_t opacity)
{
_cOpacity = opacity;
switch (_colorType)
{
2021-12-25 10:04:45 +08:00
case BackGroundColorType::NONE:
break;
case BackGroundColorType::SOLID:
_colorRender->setOpacity(opacity);
break;
case BackGroundColorType::GRADIENT:
_gradientRender->setOpacity(opacity);
break;
default:
break;
2019-11-23 20:27:39 +08:00
}
}
2021-12-25 10:04:45 +08:00
uint8_t Layout::getBackGroundColorOpacity() const
2019-11-23 20:27:39 +08:00
{
return _cOpacity;
}
2021-12-25 10:04:45 +08:00
void Layout::setBackGroundColorVector(const Vec2& vector)
2019-11-23 20:27:39 +08:00
{
_alongVector = vector;
if (_gradientRender)
{
_gradientRender->setVector(vector);
}
}
2021-12-25 10:04:45 +08:00
const Vec2& Layout::getBackGroundColorVector() const
2019-11-23 20:27:39 +08:00
{
return _alongVector;
}
2021-12-25 10:04:45 +08:00
void Layout::setBackGroundImageColor(const Color3B& color)
2019-11-23 20:27:39 +08:00
{
_backGroundImageColor = color;
updateBackGroundImageColor();
}
void Layout::setBackGroundImageOpacity(uint8_t opacity)
{
_backGroundImageOpacity = opacity;
updateBackGroundImageOpacity();
}
2021-12-25 10:04:45 +08:00
const Color3B& Layout::getBackGroundImageColor() const
2019-11-23 20:27:39 +08:00
{
return _backGroundImageColor;
}
2021-12-25 10:04:45 +08:00
uint8_t Layout::getBackGroundImageOpacity() const
2019-11-23 20:27:39 +08:00
{
return _backGroundImageOpacity;
}
void Layout::updateBackGroundImageColor()
{
if (_backGroundImage)
{
_backGroundImage->setColor(_backGroundImageColor);
}
}
void Layout::updateBackGroundImageOpacity()
{
if (_backGroundImage)
{
_backGroundImage->setOpacity(_backGroundImageOpacity);
}
}
void Layout::updateBackGroundImageRGBA()
{
if (_backGroundImage)
{
_backGroundImage->setColor(_backGroundImageColor);
_backGroundImage->setOpacity(_backGroundImageOpacity);
}
}
2021-10-23 23:27:14 +08:00
const Vec2& Layout::getBackGroundImageTextureSize() const
2019-11-23 20:27:39 +08:00
{
return _backGroundImageTextureSize;
}
void Layout::setLayoutType(Type type)
{
_layoutType = type;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
for (auto& child : _children)
{
Widget* widgetChild = dynamic_cast<Widget*>(child);
if (widgetChild)
{
supplyTheLayoutParameterLackToChild(static_cast<Widget*>(child));
}
}
_doLayoutDirty = true;
}
Layout::Type Layout::getLayoutType() const
{
return _layoutType;
}
void Layout::forceDoLayout()
{
this->requestDoLayout();
this->doLayout();
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
void Layout::requestDoLayout()
{
_doLayoutDirty = true;
}
2021-12-25 10:04:45 +08:00
Vec2 Layout::getLayoutContentSize() const
2019-11-23 20:27:39 +08:00
{
return this->getContentSize();
}
2021-12-25 10:04:45 +08:00
const Vector<Node*>& Layout::getLayoutElements() const
2019-11-23 20:27:39 +08:00
{
return this->getChildren();
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
LayoutManager* Layout::createLayoutManager()
{
LayoutManager* exe = nullptr;
switch (_layoutType)
{
2021-12-25 10:04:45 +08:00
case Type::VERTICAL:
exe = LinearVerticalLayoutManager::create();
break;
case Type::CENTER_VERTICAL:
exe = LinearCenterVerticalLayoutManager::create();
break;
case Type::HORIZONTAL:
exe = LinearHorizontalLayoutManager::create();
break;
case Type::RELATIVE:
exe = RelativeLayoutManager::create();
break;
default:
break;
2019-11-23 20:27:39 +08:00
}
return exe;
}
void Layout::doLayout()
{
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
if (!_doLayoutDirty)
{
return;
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
sortAllChildren();
LayoutManager* executant = this->createLayoutManager();
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
if (executant)
{
executant->doLayout(this);
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
_doLayoutDirty = false;
}
std::string Layout::getDescription() const
{
return "Layout";
}
Widget* Layout::createCloneInstance()
{
return Layout::create();
}
void Layout::copyClonedWidgetChildren(Widget* model)
{
Widget::copyClonedWidgetChildren(model);
}
2021-12-25 10:04:45 +08:00
void Layout::copySpecialProperties(Widget* widget)
2019-11-23 20:27:39 +08:00
{
Layout* layout = dynamic_cast<Layout*>(widget);
if (layout)
{
setBackGroundImageScale9Enabled(layout->_backGroundScale9Enabled);
2021-12-25 10:04:45 +08:00
setBackGroundImage(layout->_backGroundImageFileName, layout->_bgImageTexType);
2019-11-23 20:27:39 +08:00
setBackGroundImageCapInsets(layout->_backGroundImageCapInsets);
setBackGroundColorType(layout->_colorType);
setBackGroundColor(layout->_cColor);
setBackGroundColor(layout->_gStartColor, layout->_gEndColor);
setBackGroundColorOpacity(layout->_cOpacity);
setBackGroundColorVector(layout->_alongVector);
setLayoutType(layout->_layoutType);
setClippingEnabled(layout->_clippingEnabled);
setClippingType(layout->_clippingType);
2021-12-25 10:04:45 +08:00
_loopFocus = layout->_loopFocus;
2019-11-23 20:27:39 +08:00
_passFocusToChild = layout->_passFocusToChild;
_isInterceptTouch = layout->_isInterceptTouch;
}
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
void Layout::setLoopFocus(bool loop)
{
_loopFocus = loop;
}
2021-12-25 10:04:45 +08:00
bool Layout::isLoopFocus() const
2019-11-23 20:27:39 +08:00
{
return _loopFocus;
}
void Layout::setPassFocusToChild(bool pass)
{
_passFocusToChild = pass;
}
2021-12-25 10:04:45 +08:00
bool Layout::isPassFocusToChild() const
2019-11-23 20:27:39 +08:00
{
return _passFocusToChild;
}
2021-12-25 10:04:45 +08:00
Vec2 Layout::getLayoutAccumulatedSize() const
2019-11-23 20:27:39 +08:00
{
const auto& children = this->getChildren();
2021-12-25 10:04:45 +08:00
Vec2 layoutSize = Vec2::ZERO;
int widgetCount = 0;
for (const auto& widget : children)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
Layout* layout = dynamic_cast<Layout*>(widget);
2019-11-23 20:27:39 +08:00
if (nullptr != layout)
{
layoutSize = layoutSize + layout->getLayoutAccumulatedSize();
}
else
{
2021-12-25 10:04:45 +08:00
Widget* w = dynamic_cast<Widget*>(widget);
2019-11-23 20:27:39 +08:00
if (w)
{
widgetCount++;
2021-12-25 10:04:45 +08:00
Margin m = w->getLayoutParameter()->getMargin();
layoutSize = layoutSize + w->getContentSize() + Vec2(m.right + m.left, m.top + m.bottom) * 0.5;
2019-11-23 20:27:39 +08:00
}
}
}
2021-12-25 10:04:45 +08:00
// subtract extra size
2019-11-23 20:27:39 +08:00
Type type = this->getLayoutType();
if (type == Type::HORIZONTAL)
{
2021-12-25 10:04:45 +08:00
layoutSize = layoutSize - Vec2(0, layoutSize.height / widgetCount * (widgetCount - 1));
2019-11-23 20:27:39 +08:00
}
if (type == Type::VERTICAL || type == Type::CENTER_VERTICAL)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
layoutSize = layoutSize - Vec2(layoutSize.width / widgetCount * (widgetCount - 1), 0);
2019-11-23 20:27:39 +08:00
}
return layoutSize;
}
2021-12-25 10:04:45 +08:00
Vec2 Layout::getWorldCenterPoint(Widget* widget) const
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
Layout* layout = dynamic_cast<Layout*>(widget);
// FIXEDME: we don't need to calculate the content size of layout anymore
Vec2 widgetSize = layout ? layout->getLayoutAccumulatedSize() : widget->getContentSize();
// CCLOG("content size : width = %f, height = %f", widgetSize.width, widgetSize.height);
return widget->convertToWorldSpace(Vec2(widgetSize.width / 2, widgetSize.height / 2));
2019-11-23 20:27:39 +08:00
}
float Layout::calculateNearestDistance(Widget* baseWidget)
{
float distance = FLT_MAX;
2021-12-25 10:04:45 +08:00
Vec2 widgetPosition = this->getWorldCenterPoint(baseWidget);
2019-11-23 20:27:39 +08:00
for (Node* node : _children)
{
2021-12-25 10:04:45 +08:00
Layout* layout = dynamic_cast<Layout*>(node);
2019-11-23 20:27:39 +08:00
int length;
if (layout)
{
length = layout->calculateNearestDistance(baseWidget);
}
else
{
Widget* w = dynamic_cast<Widget*>(node);
if (w && w->isFocusEnabled())
{
Vec2 wPosition = this->getWorldCenterPoint(w);
2021-12-25 10:04:45 +08:00
length = (wPosition - widgetPosition).length();
2019-11-23 20:27:39 +08:00
}
else
{
continue;
}
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
if (length < distance)
{
distance = length;
}
}
return distance;
}
2021-12-25 10:04:45 +08:00
float Layout::calculateFarthestDistance(cocos2d::ui::Widget* baseWidget)
2019-11-23 20:27:39 +08:00
{
float distance = -FLT_MAX;
2021-12-25 10:04:45 +08:00
Vec2 widgetPosition = this->getWorldCenterPoint(baseWidget);
2019-11-23 20:27:39 +08:00
for (Node* node : _children)
{
2021-12-25 10:04:45 +08:00
Layout* layout = dynamic_cast<Layout*>(node);
2019-11-23 20:27:39 +08:00
int length;
if (layout)
{
length = layout->calculateFarthestDistance(baseWidget);
}
else
{
Widget* w = dynamic_cast<Widget*>(node);
2021-12-25 10:04:45 +08:00
if (w && w->isFocusEnabled())
{
2019-11-23 20:27:39 +08:00
Vec2 wPosition = this->getWorldCenterPoint(w);
2021-12-25 10:04:45 +08:00
length = (wPosition - widgetPosition).length();
2019-11-23 20:27:39 +08:00
}
else
{
continue;
}
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
if (length > distance)
{
distance = length;
}
}
return distance;
}
int Layout::findFirstFocusEnabledWidgetIndex()
{
ssize_t index = 0;
ssize_t count = this->getChildren().size();
while (index < count)
{
2021-12-25 10:04:45 +08:00
Widget* w = dynamic_cast<Widget*>(_children.at(index));
2019-11-23 20:27:39 +08:00
if (w && w->isFocusEnabled())
{
return (int)index;
}
index++;
}
CCASSERT(0, "invalid operation");
return 0;
}
int Layout::findNearestChildWidgetIndex(FocusDirection direction, Widget* baseWidget)
{
if (baseWidget == nullptr || baseWidget == this)
{
return this->findFirstFocusEnabledWidgetIndex();
}
2021-12-25 10:04:45 +08:00
int index = 0;
2019-11-23 20:27:39 +08:00
ssize_t count = this->getChildren().size();
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
float distance = FLT_MAX;
2021-12-25 10:04:45 +08:00
int found = 0;
if (direction == FocusDirection::LEFT || direction == FocusDirection::RIGHT || direction == FocusDirection::DOWN ||
direction == FocusDirection::UP)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
Vec2 widgetPosition = this->getWorldCenterPoint(baseWidget);
while (index < count)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
Widget* w = dynamic_cast<Widget*>(this->getChildren().at(index));
2019-11-23 20:27:39 +08:00
if (w && w->isFocusEnabled())
{
Vec2 wPosition = this->getWorldCenterPoint(w);
float length;
2021-12-25 10:04:45 +08:00
Layout* layout = dynamic_cast<Layout*>(w);
2019-11-23 20:27:39 +08:00
if (layout)
{
length = layout->calculateNearestDistance(baseWidget);
}
else
{
length = (wPosition - widgetPosition).getLength();
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
if (length < distance)
{
2021-12-25 10:04:45 +08:00
found = index;
distance = length;
2019-11-23 20:27:39 +08:00
}
}
index++;
}
2021-12-25 10:04:45 +08:00
return found;
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
CCASSERT(0, "invalid focus direction!!!");
return 0;
}
2021-12-25 10:04:45 +08:00
int Layout::findFarthestChildWidgetIndex(FocusDirection direction, cocos2d::ui::Widget* baseWidget)
2019-11-23 20:27:39 +08:00
{
if (baseWidget == nullptr || baseWidget == this)
{
return this->findFirstFocusEnabledWidgetIndex();
}
2021-12-25 10:04:45 +08:00
int index = 0;
2019-11-23 20:27:39 +08:00
ssize_t count = this->getChildren().size();
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
float distance = -FLT_MAX;
2021-12-25 10:04:45 +08:00
int found = 0;
if (direction == FocusDirection::LEFT || direction == FocusDirection::RIGHT || direction == FocusDirection::DOWN ||
direction == FocusDirection::UP)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
Vec2 widgetPosition = this->getWorldCenterPoint(baseWidget);
while (index < count)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
Widget* w = dynamic_cast<Widget*>(this->getChildren().at(index));
2019-11-23 20:27:39 +08:00
if (w && w->isFocusEnabled())
{
Vec2 wPosition = this->getWorldCenterPoint(w);
float length;
2021-12-25 10:04:45 +08:00
Layout* layout = dynamic_cast<Layout*>(w);
2019-11-23 20:27:39 +08:00
if (layout)
{
length = layout->calculateFarthestDistance(baseWidget);
}
else
{
length = (wPosition - widgetPosition).getLength();
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
if (length > distance)
{
2021-12-25 10:04:45 +08:00
found = index;
2019-11-23 20:27:39 +08:00
distance = length;
}
}
index++;
}
2021-12-25 10:04:45 +08:00
return found;
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
CCASSERT(0, "invalid focus direction!!!");
return 0;
}
Widget* Layout::findFocusEnabledChildWidgetByIndex(ssize_t index)
{
2021-12-25 10:04:45 +08:00
Widget* widget = this->getChildWidgetByIndex(index);
2019-11-23 20:27:39 +08:00
if (widget)
{
if (widget->isFocusEnabled())
{
return widget;
}
index = index + 1;
return this->findFocusEnabledChildWidgetByIndex(index);
}
return nullptr;
}
2021-12-25 10:04:45 +08:00
Widget* Layout::findFirstNonLayoutWidget()
2019-11-23 20:27:39 +08:00
{
Widget* widget = nullptr;
2021-12-25 10:04:45 +08:00
for (Node* node : _children)
2019-11-23 20:27:39 +08:00
{
Layout* layout = dynamic_cast<Layout*>(node);
if (layout)
{
widget = layout->findFirstNonLayoutWidget();
if (widget != nullptr)
{
return widget;
}
}
else
{
2021-12-25 10:04:45 +08:00
Widget* w = dynamic_cast<Widget*>(node);
2019-11-23 20:27:39 +08:00
if (w)
{
widget = w;
break;
}
}
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
return widget;
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
void Layout::findProperSearchingFunctor(FocusDirection dir, Widget* baseWidget)
{
if (baseWidget == nullptr)
{
return;
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
Vec2 previousWidgetPosition = this->getWorldCenterPoint(baseWidget);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
Vec2 widgetPosition = this->getWorldCenterPoint(this->findFirstNonLayoutWidget());
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
if (dir == FocusDirection::LEFT)
{
if (previousWidgetPosition.x > widgetPosition.x)
{
onPassFocusToChild = CC_CALLBACK_2(Layout::findNearestChildWidgetIndex, this);
}
else
{
onPassFocusToChild = CC_CALLBACK_2(Layout::findFarthestChildWidgetIndex, this);
}
}
2021-12-25 10:04:45 +08:00
else if (dir == FocusDirection::RIGHT)
2019-11-23 20:27:39 +08:00
{
if (previousWidgetPosition.x > widgetPosition.x)
{
onPassFocusToChild = CC_CALLBACK_2(Layout::findFarthestChildWidgetIndex, this);
}
else
{
onPassFocusToChild = CC_CALLBACK_2(Layout::findNearestChildWidgetIndex, this);
}
}
2021-12-25 10:04:45 +08:00
else if (dir == FocusDirection::DOWN)
2019-11-23 20:27:39 +08:00
{
if (previousWidgetPosition.y > widgetPosition.y)
{
onPassFocusToChild = CC_CALLBACK_2(Layout::findNearestChildWidgetIndex, this);
}
else
{
onPassFocusToChild = CC_CALLBACK_2(Layout::findFarthestChildWidgetIndex, this);
}
}
2021-12-25 10:04:45 +08:00
else if (dir == FocusDirection::UP)
2019-11-23 20:27:39 +08:00
{
if (previousWidgetPosition.y < widgetPosition.y)
{
onPassFocusToChild = CC_CALLBACK_2(Layout::findNearestChildWidgetIndex, this);
}
else
{
onPassFocusToChild = CC_CALLBACK_2(Layout::findFarthestChildWidgetIndex, this);
}
}
else
{
CCASSERT(0, "invalid direction!");
}
}
2021-12-25 10:04:45 +08:00
Widget* Layout::passFocusToChild(FocusDirection dir, cocos2d::ui::Widget* current)
2019-11-23 20:27:39 +08:00
{
if (checkFocusEnabledChild())
{
Widget* previousWidget = this->getCurrentFocusedWidget();
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
this->findProperSearchingFunctor(dir, previousWidget);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
int index = onPassFocusToChild(dir, previousWidget);
2021-12-25 10:04:45 +08:00
Widget* widget = this->getChildWidgetByIndex(index);
Layout* layout = dynamic_cast<Layout*>(widget);
2019-11-23 20:27:39 +08:00
if (layout)
{
layout->_isFocusPassing = true;
return layout->findNextFocusedWidget(dir, layout);
}
else
{
this->dispatchFocusEvent(current, widget);
return widget;
}
}
else
{
return this;
}
}
2021-12-25 10:04:45 +08:00
bool Layout::checkFocusEnabledChild() const
2019-11-23 20:27:39 +08:00
{
bool ret = false;
2021-12-25 10:04:45 +08:00
for (Node* node : _children)
2019-11-23 20:27:39 +08:00
{
Widget* widget = dynamic_cast<Widget*>(node);
if (widget && widget->isFocusEnabled())
{
ret = true;
break;
}
}
return ret;
}
2021-12-25 10:04:45 +08:00
Widget* Layout::getChildWidgetByIndex(ssize_t index) const
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
ssize_t size = _children.size();
int count = 0;
2019-11-23 20:27:39 +08:00
ssize_t oldIndex = index;
2021-12-25 10:04:45 +08:00
Widget* widget = nullptr;
2019-11-23 20:27:39 +08:00
while (index < size)
{
Widget* firstChild = dynamic_cast<Widget*>(_children.at(index));
if (firstChild)
{
widget = firstChild;
break;
}
count++;
index++;
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
if (nullptr == widget)
{
int begin = 0;
while (begin < oldIndex)
{
Widget* firstChild = dynamic_cast<Widget*>(_children.at(begin));
if (firstChild)
{
widget = firstChild;
break;
}
count++;
begin++;
}
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
return widget;
}
2021-12-25 10:04:45 +08:00
Widget* Layout::getPreviousFocusedWidget(FocusDirection direction, Widget* current)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
Widget* nextWidget = nullptr;
2019-11-23 20:27:39 +08:00
ssize_t previousWidgetPos = _children.getIndex(current);
2021-12-25 10:04:45 +08:00
previousWidgetPos = previousWidgetPos - 1;
2019-11-23 20:27:39 +08:00
if (previousWidgetPos >= 0)
{
nextWidget = this->getChildWidgetByIndex(previousWidgetPos);
if (nextWidget->isFocusEnabled())
{
Layout* layout = dynamic_cast<Layout*>(nextWidget);
if (layout)
{
layout->_isFocusPassing = true;
return layout->findNextFocusedWidget(direction, layout);
}
this->dispatchFocusEvent(current, nextWidget);
return nextWidget;
}
else
{
2021-12-25 10:04:45 +08:00
// handling the disabled widget, there is no actual focus lose or get, so we don't need any event
2019-11-23 20:27:39 +08:00
return this->getPreviousFocusedWidget(direction, nextWidget);
}
}
else
{
if (_loopFocus)
{
if (checkFocusEnabledChild())
{
2021-12-25 10:04:45 +08:00
previousWidgetPos = _children.size() - 1;
nextWidget = this->getChildWidgetByIndex(previousWidgetPos);
2019-11-23 20:27:39 +08:00
if (nextWidget->isFocusEnabled())
{
Layout* layout = dynamic_cast<Layout*>(nextWidget);
if (layout)
{
layout->_isFocusPassing = true;
return layout->findNextFocusedWidget(direction, layout);
}
else
{
this->dispatchFocusEvent(current, nextWidget);
return nextWidget;
}
}
else
{
return this->getPreviousFocusedWidget(direction, nextWidget);
}
}
else
{
if (dynamic_cast<Layout*>(current))
{
return current;
}
else
{
return _focusedWidget;
}
}
}
else
{
if (isLastWidgetInContainer(current, direction))
{
if (isWidgetAncestorSupportLoopFocus(this, direction))
{
return Widget::findNextFocusedWidget(direction, this);
}
if (dynamic_cast<Layout*>(current))
{
return current;
}
else
{
return _focusedWidget;
}
}
else
{
return Widget::findNextFocusedWidget(direction, this);
}
}
}
}
2021-12-25 10:04:45 +08:00
Widget* Layout::getNextFocusedWidget(FocusDirection direction, Widget* current)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
Widget* nextWidget = nullptr;
2019-11-23 20:27:39 +08:00
ssize_t previousWidgetPos = _children.getIndex(current);
2021-12-25 10:04:45 +08:00
previousWidgetPos = previousWidgetPos + 1;
2019-11-23 20:27:39 +08:00
if (previousWidgetPos < _children.size())
{
nextWidget = this->getChildWidgetByIndex(previousWidgetPos);
2021-12-25 10:04:45 +08:00
// handle widget
2019-11-23 20:27:39 +08:00
if (nextWidget)
{
if (nextWidget->isFocusEnabled())
{
Layout* layout = dynamic_cast<Layout*>(nextWidget);
if (layout)
{
layout->_isFocusPassing = true;
return layout->findNextFocusedWidget(direction, layout);
}
else
{
this->dispatchFocusEvent(current, nextWidget);
return nextWidget;
}
}
else
{
return this->getNextFocusedWidget(direction, nextWidget);
}
}
else
{
return current;
}
}
else
{
if (_loopFocus)
{
if (checkFocusEnabledChild())
{
previousWidgetPos = 0;
2021-12-25 10:04:45 +08:00
nextWidget = this->getChildWidgetByIndex(previousWidgetPos);
2019-11-23 20:27:39 +08:00
if (nextWidget->isFocusEnabled())
{
Layout* layout = dynamic_cast<Layout*>(nextWidget);
if (layout)
{
layout->_isFocusPassing = true;
return layout->findNextFocusedWidget(direction, layout);
}
else
{
this->dispatchFocusEvent(current, nextWidget);
return nextWidget;
}
}
else
{
return this->getNextFocusedWidget(direction, nextWidget);
}
}
else
{
2021-12-25 10:04:45 +08:00
if (dynamic_cast<Layout*>(current))
{
2019-11-23 20:27:39 +08:00
return current;
}
else
{
return _focusedWidget;
}
}
}
else
{
if (isLastWidgetInContainer(current, direction))
{
if (isWidgetAncestorSupportLoopFocus(this, direction))
{
return Widget::findNextFocusedWidget(direction, this);
}
2021-12-25 10:04:45 +08:00
if (dynamic_cast<Layout*>(current))
{
2019-11-23 20:27:39 +08:00
return current;
}
else
{
return _focusedWidget;
}
}
else
{
return Widget::findNextFocusedWidget(direction, this);
}
}
}
}
2021-12-25 10:04:45 +08:00
bool Layout::isLastWidgetInContainer(Widget* widget, FocusDirection direction) const
2019-11-23 20:27:39 +08:00
{
Layout* parent = dynamic_cast<Layout*>(widget->getParent());
if (parent == nullptr)
{
return true;
}
2021-12-25 10:04:45 +08:00
auto& container = parent->getChildren();
ssize_t index = container.getIndex(widget);
const auto parentLayoutType = parent->getLayoutType();
if (parentLayoutType == Type::HORIZONTAL)
2019-11-23 20:27:39 +08:00
{
if (direction == FocusDirection::LEFT)
{
if (index == 0)
{
return isLastWidgetInContainer(parent, direction);
}
else
{
return false;
}
}
if (direction == FocusDirection::RIGHT)
{
2021-12-25 10:04:45 +08:00
if (index == container.size() - 1)
2019-11-23 20:27:39 +08:00
{
return isLastWidgetInContainer(parent, direction);
}
else
{
return false;
}
}
if (direction == FocusDirection::DOWN)
{
return isLastWidgetInContainer(parent, direction);
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
if (direction == FocusDirection::UP)
{
return isLastWidgetInContainer(parent, direction);
}
}
else if (parentLayoutType == Type::VERTICAL || parentLayoutType == Type::CENTER_VERTICAL)
2019-11-23 20:27:39 +08:00
{
if (direction == FocusDirection::UP)
{
if (index == 0)
{
return isLastWidgetInContainer(parent, direction);
}
else
{
return false;
}
}
if (direction == FocusDirection::DOWN)
{
if (index == container.size() - 1)
{
return isLastWidgetInContainer(parent, direction);
}
else
{
return false;
}
}
if (direction == FocusDirection::LEFT)
{
return isLastWidgetInContainer(parent, direction);
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
if (direction == FocusDirection::RIGHT)
{
return isLastWidgetInContainer(parent, direction);
}
}
else
{
CCASSERT(0, "invalid layout Type");
return false;
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
return false;
}
2021-12-25 10:04:45 +08:00
bool Layout::isWidgetAncestorSupportLoopFocus(Widget* widget, FocusDirection direction) const
2019-11-23 20:27:39 +08:00
{
Layout* parent = dynamic_cast<Layout*>(widget->getParent());
if (parent == nullptr)
{
return false;
}
if (parent->isLoopFocus())
{
const auto layoutType = parent->getLayoutType();
2019-11-23 20:27:39 +08:00
if (layoutType == Type::HORIZONTAL)
{
if (direction == FocusDirection::LEFT || direction == FocusDirection::RIGHT)
{
return true;
}
else
{
return isWidgetAncestorSupportLoopFocus(parent, direction);
}
}
if (layoutType == Type::VERTICAL || layoutType == Type::CENTER_VERTICAL)
2019-11-23 20:27:39 +08:00
{
if (direction == FocusDirection::DOWN || direction == FocusDirection::UP)
{
return true;
}
else
{
return isWidgetAncestorSupportLoopFocus(parent, direction);
}
}
else
{
CCASSERT(0, "invalid layout type");
return false;
}
}
else
{
return isWidgetAncestorSupportLoopFocus(parent, direction);
}
}
Widget* Layout::findNextFocusedWidget(FocusDirection direction, Widget* current)
{
if (_isFocusPassing || this->isFocused())
{
2021-12-25 10:04:45 +08:00
Layout* parent = dynamic_cast<Layout*>(this->getParent());
2019-11-23 20:27:39 +08:00
_isFocusPassing = false;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
if (_passFocusToChild)
{
2021-12-25 10:04:45 +08:00
Widget* w = this->passFocusToChild(direction, current);
2019-11-23 20:27:39 +08:00
if (dynamic_cast<Layout*>(w))
{
if (parent)
{
parent->_isFocusPassing = true;
return parent->findNextFocusedWidget(direction, this);
}
}
return w;
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
if (nullptr == parent)
{
return this;
}
parent->_isFocusPassing = true;
return parent->findNextFocusedWidget(direction, this);
}
2021-12-25 10:04:45 +08:00
else if (current->isFocused() || dynamic_cast<Layout*>(current))
2019-11-23 20:27:39 +08:00
{
if (_layoutType == Type::HORIZONTAL)
{
switch (direction)
{
2021-12-25 10:04:45 +08:00
case FocusDirection::LEFT:
{
return this->getPreviousFocusedWidget(direction, current);
}
break;
case FocusDirection::RIGHT:
{
return this->getNextFocusedWidget(direction, current);
}
break;
case FocusDirection::DOWN:
case FocusDirection::UP:
{
if (isLastWidgetInContainer(this, direction))
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
if (isWidgetAncestorSupportLoopFocus(current, direction))
2019-11-23 20:27:39 +08:00
{
return Widget::findNextFocusedWidget(direction, this);
}
return current;
}
2021-12-25 10:04:45 +08:00
else
{
return Widget::findNextFocusedWidget(direction, this);
}
}
break;
default:
{
CCASSERT(0, "Invalid Focus Direction");
return current;
}
break;
2019-11-23 20:27:39 +08:00
}
}
else if (_layoutType == Type::VERTICAL || _layoutType == Type::CENTER_VERTICAL)
2019-11-23 20:27:39 +08:00
{
switch (direction)
{
2021-12-25 10:04:45 +08:00
case FocusDirection::LEFT:
case FocusDirection::RIGHT:
{
if (isLastWidgetInContainer(this, direction))
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
if (isWidgetAncestorSupportLoopFocus(current, direction))
2019-11-23 20:27:39 +08:00
{
return Widget::findNextFocusedWidget(direction, this);
}
2021-12-25 10:04:45 +08:00
return current;
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
else
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
return Widget::findNextFocusedWidget(direction, this);
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
}
break;
case FocusDirection::DOWN:
{
return getNextFocusedWidget(direction, current);
}
break;
case FocusDirection::UP:
{
return getPreviousFocusedWidget(direction, current);
}
break;
default:
{
CCASSERT(0, "Invalid Focus Direction");
return current;
}
break;
2019-11-23 20:27:39 +08:00
}
}
else
{
CCASSERT(0, "Un Supported Layout type, please use VBox and HBox instead!!!");
return current;
}
}
else
{
return current;
}
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
void Layout::setCameraMask(unsigned short mask, bool applyChildren)
{
Widget::setCameraMask(mask, applyChildren);
2021-12-25 10:04:45 +08:00
if (_clippingStencil)
{
2019-11-23 20:27:39 +08:00
_clippingStencil->setCameraMask(mask, applyChildren);
}
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
ResourceData Layout::getRenderFile()
{
ResourceData rData;
rData.type = (int)_bgImageTexType;
rData.file = _backGroundImageFileName;
return rData;
}
2021-12-25 10:04:45 +08:00
} // namespace ui
2019-11-23 20:27:39 +08:00
NS_CC_END