2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2021-10-24 14:09:59 +08:00
|
|
|
Copyright (c) 2021 Bytedance Inc.
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-07-09 07:07:01 +08:00
|
|
|
https://adxeproject.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 "ui/UIPageView.h"
|
|
|
|
#include "ui/UILayoutComponent.h"
|
|
|
|
#include "2d/CCNode.h"
|
|
|
|
#include "ui/GUIDefine.h"
|
|
|
|
#include "ui/UIHelper.h"
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
namespace ui
|
|
|
|
{
|
|
|
|
LayoutComponent::LayoutComponent()
|
|
|
|
: _horizontalEdge(HorizontalEdge::None)
|
|
|
|
, _verticalEdge(VerticalEdge::None)
|
|
|
|
, _leftMargin(0)
|
|
|
|
, _rightMargin(0)
|
|
|
|
, _bottomMargin(0)
|
|
|
|
, _topMargin(0)
|
|
|
|
, _usingPositionPercentX(false)
|
|
|
|
, _positionPercentX(0)
|
|
|
|
, _usingPositionPercentY(false)
|
|
|
|
, _positionPercentY(0)
|
|
|
|
, _usingStretchWidth(false)
|
|
|
|
, _usingStretchHeight(false)
|
|
|
|
, _percentWidth(0)
|
|
|
|
, _usingPercentWidth(false)
|
|
|
|
, _percentHeight(0)
|
|
|
|
, _usingPercentHeight(false)
|
|
|
|
, _actived(true)
|
|
|
|
, _isPercentOnly(false)
|
|
|
|
{
|
|
|
|
_name = __LAYOUT_COMPONENT_NAME;
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
LayoutComponent::~LayoutComponent() {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
LayoutComponent* LayoutComponent::bindLayoutComponent(Node* node)
|
|
|
|
{
|
|
|
|
LayoutComponent* layout = (LayoutComponent*)node->getComponent(__LAYOUT_COMPONENT_NAME);
|
|
|
|
if (layout != nullptr)
|
|
|
|
return layout;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
layout = new LayoutComponent();
|
|
|
|
if (layout->init())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
layout->autorelease();
|
|
|
|
node->addComponent(layout);
|
|
|
|
return layout;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
CC_SAFE_DELETE(layout);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool LayoutComponent::init()
|
|
|
|
{
|
|
|
|
bool ret = true;
|
|
|
|
do
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!Component::init())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
ret = false;
|
|
|
|
break;
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
// put layout component initialized code here
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
} while (0);
|
|
|
|
return ret;
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
Node* LayoutComponent::getOwnerParent()
|
|
|
|
{
|
|
|
|
Node* parent = _owner->getParent();
|
|
|
|
return parent;
|
|
|
|
}
|
|
|
|
void LayoutComponent::refreshHorizontalMargin()
|
|
|
|
{
|
|
|
|
Node* parent = this->getOwnerParent();
|
|
|
|
if (parent == nullptr)
|
|
|
|
return;
|
|
|
|
|
|
|
|
const Point& ownerPoint = _owner->getPosition();
|
|
|
|
const Point& ownerAnchor = _owner->getAnchorPoint();
|
|
|
|
const Vec2& ownerSize = _owner->getContentSize();
|
|
|
|
const Vec2& parentSize = parent->getContentSize();
|
|
|
|
|
|
|
|
_leftMargin = ownerPoint.x - ownerAnchor.x * ownerSize.width;
|
|
|
|
_rightMargin = parentSize.width - (ownerPoint.x + (1 - ownerAnchor.x) * ownerSize.width);
|
|
|
|
}
|
|
|
|
void LayoutComponent::refreshVerticalMargin()
|
|
|
|
{
|
|
|
|
Node* parent = this->getOwnerParent();
|
|
|
|
if (parent == nullptr)
|
|
|
|
return;
|
|
|
|
|
|
|
|
const Point& ownerPoint = _owner->getPosition();
|
|
|
|
const Point& ownerAnchor = _owner->getAnchorPoint();
|
|
|
|
const Vec2& ownerSize = _owner->getContentSize();
|
|
|
|
const Vec2& parentSize = parent->getContentSize();
|
|
|
|
|
|
|
|
_bottomMargin = ownerPoint.y - ownerAnchor.y * ownerSize.height;
|
|
|
|
_topMargin = parentSize.height - (ownerPoint.y + (1 - ownerAnchor.y) * ownerSize.height);
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
// OldVersion
|
|
|
|
void LayoutComponent::setUsingPercentContentSize(bool isUsed)
|
|
|
|
{
|
|
|
|
_usingPercentWidth = _usingPercentHeight = isUsed;
|
|
|
|
}
|
|
|
|
bool LayoutComponent::getUsingPercentContentSize() const
|
|
|
|
{
|
|
|
|
return _usingPercentWidth && _usingPercentHeight;
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void LayoutComponent::setPercentContentSize(const Vec2& percent)
|
|
|
|
{
|
|
|
|
this->setPercentWidth(percent.x);
|
|
|
|
this->setPercentHeight(percent.y);
|
|
|
|
}
|
|
|
|
Vec2 LayoutComponent::getPercentContentSize() const
|
|
|
|
{
|
|
|
|
Vec2 vec2 = Vec2(_percentWidth, _percentHeight);
|
|
|
|
return vec2;
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
// Position & Margin
|
|
|
|
const Point& LayoutComponent::getAnchorPosition() const
|
|
|
|
{
|
|
|
|
return _owner->getAnchorPoint();
|
|
|
|
}
|
|
|
|
void LayoutComponent::setAnchorPosition(const Point& point)
|
|
|
|
{
|
|
|
|
Rect oldRect = _owner->getBoundingBox();
|
|
|
|
_owner->setAnchorPoint(point);
|
|
|
|
Rect newRect = _owner->getBoundingBox();
|
|
|
|
float offSetX = oldRect.origin.x - newRect.origin.x;
|
|
|
|
float offSetY = oldRect.origin.y - newRect.origin.y;
|
|
|
|
|
|
|
|
Point ownerPosition = _owner->getPosition();
|
|
|
|
ownerPosition.x += offSetX;
|
|
|
|
ownerPosition.y += offSetY;
|
|
|
|
|
|
|
|
this->setPosition(ownerPosition);
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
const Point& LayoutComponent::getPosition() const
|
|
|
|
{
|
|
|
|
return _owner->getPosition();
|
|
|
|
}
|
|
|
|
void LayoutComponent::setPosition(const Point& position)
|
|
|
|
{
|
|
|
|
Node* parent = this->getOwnerParent();
|
|
|
|
if (parent != nullptr)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
Point ownerPoint = position;
|
|
|
|
const Vec2& parentSize = parent->getContentSize();
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (parentSize.width != 0)
|
|
|
|
_positionPercentX = ownerPoint.x / parentSize.width;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_positionPercentX = 0;
|
|
|
|
if (_usingPositionPercentX || _horizontalEdge == HorizontalEdge::Center)
|
|
|
|
ownerPoint.x = 0;
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (parentSize.height != 0)
|
|
|
|
_positionPercentY = ownerPoint.y / parentSize.height;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_positionPercentY = 0;
|
|
|
|
if (_usingPositionPercentY || _verticalEdge == VerticalEdge::Center)
|
|
|
|
ownerPoint.y = 0;
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
_owner->setPosition(ownerPoint);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
this->refreshHorizontalMargin();
|
|
|
|
this->refreshVerticalMargin();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else
|
|
|
|
_owner->setPosition(position);
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool LayoutComponent::isPositionPercentXEnabled() const
|
|
|
|
{
|
|
|
|
return _usingPositionPercentX;
|
|
|
|
}
|
|
|
|
void LayoutComponent::setPositionPercentXEnabled(bool isUsed)
|
|
|
|
{
|
|
|
|
_usingPositionPercentX = isUsed;
|
|
|
|
if (_usingPositionPercentX)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_horizontalEdge = HorizontalEdge::None;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
float LayoutComponent::getPositionPercentX() const
|
|
|
|
{
|
|
|
|
return _positionPercentX;
|
|
|
|
}
|
|
|
|
void LayoutComponent::setPositionPercentX(float percentMargin)
|
|
|
|
{
|
|
|
|
_positionPercentX = percentMargin;
|
|
|
|
|
|
|
|
if (_usingPositionPercentX || _horizontalEdge == HorizontalEdge::Center)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
Node* parent = this->getOwnerParent();
|
|
|
|
if (parent != nullptr)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_owner->setPositionX(parent->getContentSize().width * _positionPercentX);
|
2019-11-23 20:27:39 +08:00
|
|
|
this->refreshHorizontalMargin();
|
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool LayoutComponent::isPositionPercentYEnabled() const
|
|
|
|
{
|
|
|
|
return _usingPositionPercentY;
|
|
|
|
}
|
|
|
|
void LayoutComponent::setPositionPercentYEnabled(bool isUsed)
|
|
|
|
{
|
|
|
|
_usingPositionPercentY = isUsed;
|
|
|
|
if (_usingPositionPercentY)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_verticalEdge = VerticalEdge::None;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
float LayoutComponent::getPositionPercentY() const
|
|
|
|
{
|
|
|
|
return _positionPercentY;
|
|
|
|
}
|
|
|
|
void LayoutComponent::setPositionPercentY(float percentMargin)
|
|
|
|
{
|
|
|
|
_positionPercentY = percentMargin;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_usingPositionPercentY || _verticalEdge == VerticalEdge::Center)
|
|
|
|
{
|
|
|
|
Node* parent = this->getOwnerParent();
|
|
|
|
if (parent != nullptr)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_owner->setPositionY(parent->getContentSize().height * _positionPercentY);
|
|
|
|
this->refreshVerticalMargin();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
LayoutComponent::HorizontalEdge LayoutComponent::getHorizontalEdge() const
|
|
|
|
{
|
|
|
|
return _horizontalEdge;
|
|
|
|
}
|
|
|
|
void LayoutComponent::setHorizontalEdge(HorizontalEdge hEage)
|
|
|
|
{
|
|
|
|
_horizontalEdge = hEage;
|
|
|
|
if (_horizontalEdge != HorizontalEdge::None)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_usingPositionPercentX = false;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
LayoutComponent::VerticalEdge LayoutComponent::getVerticalEdge() const
|
|
|
|
{
|
|
|
|
return _verticalEdge;
|
|
|
|
}
|
|
|
|
void LayoutComponent::setVerticalEdge(VerticalEdge vEage)
|
|
|
|
{
|
|
|
|
_verticalEdge = vEage;
|
|
|
|
if (_verticalEdge != VerticalEdge::None)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_usingPositionPercentY = false;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
float LayoutComponent::getLeftMargin() const
|
|
|
|
{
|
|
|
|
return _leftMargin;
|
|
|
|
}
|
|
|
|
void LayoutComponent::setLeftMargin(float margin)
|
|
|
|
{
|
|
|
|
_leftMargin = margin;
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
float LayoutComponent::getRightMargin() const
|
|
|
|
{
|
|
|
|
return _rightMargin;
|
|
|
|
}
|
|
|
|
void LayoutComponent::setRightMargin(float margin)
|
|
|
|
{
|
|
|
|
_rightMargin = margin;
|
|
|
|
}
|
|
|
|
|
|
|
|
float LayoutComponent::getTopMargin() const
|
|
|
|
{
|
|
|
|
return _topMargin;
|
|
|
|
}
|
|
|
|
void LayoutComponent::setTopMargin(float margin)
|
|
|
|
{
|
|
|
|
_topMargin = margin;
|
|
|
|
}
|
|
|
|
|
|
|
|
float LayoutComponent::getBottomMargin() const
|
|
|
|
{
|
|
|
|
return _bottomMargin;
|
|
|
|
}
|
|
|
|
void LayoutComponent::setBottomMargin(float margin)
|
|
|
|
{
|
|
|
|
_bottomMargin = margin;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Size & Percent
|
|
|
|
const Vec2& LayoutComponent::getSize() const
|
|
|
|
{
|
|
|
|
return this->getOwner()->getContentSize();
|
|
|
|
}
|
|
|
|
void LayoutComponent::setSize(const Vec2& size)
|
|
|
|
{
|
|
|
|
Node* parent = this->getOwnerParent();
|
|
|
|
if (parent != nullptr)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
Vec2 ownerSize = size;
|
|
|
|
const Vec2& parentSize = parent->getContentSize();
|
|
|
|
|
|
|
|
if (parentSize.width != 0)
|
|
|
|
_percentWidth = ownerSize.width / parentSize.width;
|
|
|
|
else
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_percentWidth = 0;
|
|
|
|
if (_usingPercentWidth || (this->_horizontalEdge != HorizontalEdge::Center && this->_usingStretchWidth))
|
|
|
|
ownerSize.width = 0;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (parentSize.height != 0)
|
|
|
|
_percentHeight = ownerSize.height / parentSize.height;
|
|
|
|
else
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_percentHeight = 0;
|
|
|
|
if (_usingPercentHeight || (this->_verticalEdge != VerticalEdge::Center && this->_usingStretchHeight))
|
|
|
|
ownerSize.height = 0;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
_owner->setContentSize(ownerSize);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
this->refreshHorizontalMargin();
|
|
|
|
this->refreshVerticalMargin();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else
|
|
|
|
_owner->setContentSize(size);
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool LayoutComponent::isPercentWidthEnabled() const
|
|
|
|
{
|
|
|
|
return _usingPercentWidth;
|
|
|
|
}
|
|
|
|
void LayoutComponent::setPercentWidthEnabled(bool isUsed)
|
|
|
|
{
|
|
|
|
_usingPercentWidth = isUsed;
|
|
|
|
if (_usingPercentWidth)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_usingStretchWidth = false;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
float LayoutComponent::getSizeWidth() const
|
|
|
|
{
|
|
|
|
return _owner->getContentSize().width;
|
|
|
|
}
|
|
|
|
void LayoutComponent::setSizeWidth(float width)
|
|
|
|
{
|
|
|
|
Vec2 ownerSize = _owner->getContentSize();
|
|
|
|
ownerSize.width = width;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
Node* parent = this->getOwnerParent();
|
|
|
|
if (parent != nullptr)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
const Vec2& parentSize = parent->getContentSize();
|
|
|
|
if (parentSize.width != 0)
|
|
|
|
_percentWidth = ownerSize.width / parentSize.width;
|
2019-11-23 20:27:39 +08:00
|
|
|
else
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_percentWidth = 0;
|
|
|
|
if (_usingPercentWidth)
|
|
|
|
ownerSize.width = 0;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
_owner->setContentSize(ownerSize);
|
|
|
|
this->refreshHorizontalMargin();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else
|
|
|
|
_owner->setContentSize(ownerSize);
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
float LayoutComponent::getPercentWidth() const
|
|
|
|
{
|
|
|
|
return _percentWidth;
|
|
|
|
}
|
|
|
|
void LayoutComponent::setPercentWidth(float percentWidth)
|
|
|
|
{
|
|
|
|
_percentWidth = percentWidth;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_usingPercentWidth)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
Node* parent = this->getOwnerParent();
|
|
|
|
if (parent != nullptr)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
Vec2 ownerSize = _owner->getContentSize();
|
|
|
|
ownerSize.width = parent->getContentSize().width * _percentWidth;
|
2019-11-23 20:27:39 +08:00
|
|
|
_owner->setContentSize(ownerSize);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
this->refreshHorizontalMargin();
|
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool LayoutComponent::isPercentHeightEnabled() const
|
|
|
|
{
|
|
|
|
return _usingPercentHeight;
|
|
|
|
}
|
|
|
|
void LayoutComponent::setPercentHeightEnabled(bool isUsed)
|
|
|
|
{
|
|
|
|
_usingPercentHeight = isUsed;
|
|
|
|
if (_usingPercentHeight)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_usingStretchHeight = false;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
float LayoutComponent::getSizeHeight() const
|
|
|
|
{
|
|
|
|
return _owner->getContentSize().height;
|
|
|
|
}
|
|
|
|
void LayoutComponent::setSizeHeight(float height)
|
|
|
|
{
|
|
|
|
Vec2 ownerSize = _owner->getContentSize();
|
|
|
|
ownerSize.height = height;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
Node* parent = this->getOwnerParent();
|
|
|
|
if (parent != nullptr)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
const Vec2& parentSize = parent->getContentSize();
|
|
|
|
if (parentSize.height != 0)
|
|
|
|
_percentHeight = ownerSize.height / parentSize.height;
|
|
|
|
else
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_percentHeight = 0;
|
|
|
|
if (_usingPercentHeight)
|
|
|
|
ownerSize.height = 0;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
_owner->setContentSize(ownerSize);
|
|
|
|
this->refreshVerticalMargin();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else
|
|
|
|
_owner->setContentSize(ownerSize);
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
float LayoutComponent::getPercentHeight() const
|
|
|
|
{
|
|
|
|
return _percentHeight;
|
|
|
|
}
|
|
|
|
void LayoutComponent::setPercentHeight(float percentHeight)
|
|
|
|
{
|
|
|
|
_percentHeight = percentHeight;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_usingPercentHeight)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
Node* parent = this->getOwnerParent();
|
|
|
|
if (parent != nullptr)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
Vec2 ownerSize = _owner->getContentSize();
|
|
|
|
ownerSize.height = parent->getContentSize().height * _percentHeight;
|
2019-11-23 20:27:39 +08:00
|
|
|
_owner->setContentSize(ownerSize);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
this->refreshVerticalMargin();
|
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool LayoutComponent::isStretchWidthEnabled() const
|
|
|
|
{
|
|
|
|
return _usingStretchWidth;
|
|
|
|
}
|
|
|
|
void LayoutComponent::setStretchWidthEnabled(bool isUsed)
|
|
|
|
{
|
|
|
|
_usingStretchWidth = isUsed;
|
|
|
|
if (_usingStretchWidth)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_usingPercentWidth = false;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool LayoutComponent::isStretchHeightEnabled() const
|
|
|
|
{
|
|
|
|
return _usingStretchHeight;
|
|
|
|
}
|
|
|
|
void LayoutComponent::setStretchHeightEnabled(bool isUsed)
|
|
|
|
{
|
|
|
|
_usingStretchHeight = isUsed;
|
|
|
|
if (_usingStretchHeight)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_usingPercentHeight = false;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void LayoutComponent::refreshLayout()
|
|
|
|
{
|
|
|
|
if (!_actived)
|
|
|
|
return;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
Node* parent = this->getOwnerParent();
|
|
|
|
if (parent == nullptr)
|
|
|
|
return;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
const Vec2& parentSize = parent->getContentSize();
|
|
|
|
const Point& ownerAnchor = _owner->getAnchorPoint();
|
|
|
|
Vec2 ownerSize = _owner->getContentSize();
|
|
|
|
Point ownerPosition = _owner->getPosition();
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
switch (this->_horizontalEdge)
|
|
|
|
{
|
|
|
|
case HorizontalEdge::None:
|
|
|
|
if (_usingStretchWidth && !_isPercentOnly)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
ownerSize.width = parentSize.width * _percentWidth;
|
2019-11-23 20:27:39 +08:00
|
|
|
ownerPosition.x = _leftMargin + ownerAnchor.x * ownerSize.width;
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (_usingPositionPercentX)
|
|
|
|
ownerPosition.x = parentSize.width * _positionPercentX;
|
|
|
|
if (_usingPercentWidth)
|
2019-11-23 20:27:39 +08:00
|
|
|
ownerSize.width = parentSize.width * _percentWidth;
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case HorizontalEdge::Left:
|
|
|
|
if (_isPercentOnly)
|
2019-11-23 20:27:39 +08:00
|
|
|
break;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_usingPercentWidth || _usingStretchWidth)
|
|
|
|
ownerSize.width = parentSize.width * _percentWidth;
|
|
|
|
ownerPosition.x = _leftMargin + ownerAnchor.x * ownerSize.width;
|
|
|
|
break;
|
|
|
|
case HorizontalEdge::Right:
|
|
|
|
if (_isPercentOnly)
|
2019-11-23 20:27:39 +08:00
|
|
|
break;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_usingPercentWidth || _usingStretchWidth)
|
|
|
|
ownerSize.width = parentSize.width * _percentWidth;
|
|
|
|
ownerPosition.x = parentSize.width - (_rightMargin + (1 - ownerAnchor.x) * ownerSize.width);
|
|
|
|
break;
|
|
|
|
case HorizontalEdge::Center:
|
|
|
|
if (_isPercentOnly)
|
2019-11-23 20:27:39 +08:00
|
|
|
break;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_usingStretchWidth)
|
|
|
|
{
|
|
|
|
ownerSize.width = parentSize.width - _leftMargin - _rightMargin;
|
|
|
|
if (ownerSize.width < 0)
|
|
|
|
ownerSize.width = 0;
|
|
|
|
ownerPosition.x = _leftMargin + ownerAnchor.x * ownerSize.width;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (_usingPercentWidth)
|
|
|
|
ownerSize.width = parentSize.width * _percentWidth;
|
|
|
|
ownerPosition.x = parentSize.width * _positionPercentX;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
switch (this->_verticalEdge)
|
|
|
|
{
|
|
|
|
case VerticalEdge::None:
|
|
|
|
if (_usingStretchHeight && !_isPercentOnly)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
ownerSize.height = parentSize.height * _percentHeight;
|
|
|
|
ownerPosition.y = _bottomMargin + ownerAnchor.y * ownerSize.height;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (_usingPositionPercentY)
|
|
|
|
ownerPosition.y = parentSize.height * _positionPercentY;
|
|
|
|
if (_usingPercentHeight)
|
2019-11-23 20:27:39 +08:00
|
|
|
ownerSize.height = parentSize.height * _percentHeight;
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case VerticalEdge::Bottom:
|
|
|
|
if (_isPercentOnly)
|
2019-11-23 20:27:39 +08:00
|
|
|
break;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_usingPercentHeight || _usingStretchHeight)
|
|
|
|
ownerSize.height = parentSize.height * _percentHeight;
|
|
|
|
ownerPosition.y = _bottomMargin + ownerAnchor.y * ownerSize.height;
|
|
|
|
break;
|
|
|
|
case VerticalEdge::Top:
|
|
|
|
if (_isPercentOnly)
|
2019-11-23 20:27:39 +08:00
|
|
|
break;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_usingPercentHeight || _usingStretchHeight)
|
|
|
|
ownerSize.height = parentSize.height * _percentHeight;
|
|
|
|
ownerPosition.y = parentSize.height - (_topMargin + (1 - ownerAnchor.y) * ownerSize.height);
|
|
|
|
break;
|
|
|
|
case VerticalEdge::Center:
|
|
|
|
if (_isPercentOnly)
|
2019-11-23 20:27:39 +08:00
|
|
|
break;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_usingStretchHeight)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
ownerSize.height = parentSize.height - _topMargin - _bottomMargin;
|
|
|
|
if (ownerSize.height < 0)
|
|
|
|
ownerSize.height = 0;
|
|
|
|
ownerPosition.y = _bottomMargin + ownerAnchor.y * ownerSize.height;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_usingPercentHeight)
|
|
|
|
ownerSize.height = parentSize.height * _percentHeight;
|
|
|
|
ownerPosition.y = parentSize.height * _positionPercentY;
|
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
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
_owner->setPosition(ownerPosition);
|
|
|
|
_owner->setContentSize(ownerSize);
|
|
|
|
|
|
|
|
if (typeid(*_owner) == typeid(PageView))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
PageView* page = static_cast<PageView*>(_owner);
|
|
|
|
page->forceDoLayout();
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
Vector<Widget*> _widgetVector = page->getItems();
|
|
|
|
for (auto& item : _widgetVector)
|
|
|
|
{
|
|
|
|
ui::Helper::doLayout(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
ui::Helper::doLayout(_owner);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void LayoutComponent::setActiveEnabled(bool enable)
|
|
|
|
{
|
|
|
|
_actived = enable;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LayoutComponent::setPercentOnlyEnabled(bool enable)
|
|
|
|
{
|
|
|
|
_isPercentOnly = enable;
|
|
|
|
}
|
|
|
|
} // namespace ui
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
NS_CC_END
|