2014-10-09 18:28:09 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2013-2014 Chukong Technologies Inc.
|
2014-12-22 11:20:03 +08:00
|
|
|
|
2014-10-09 18:28:09 +08:00
|
|
|
http://www.cocos2d-x.org
|
2014-12-22 11:20:03 +08:00
|
|
|
|
2014-10-09 18:28:09 +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:
|
2014-12-22 11:20:03 +08:00
|
|
|
|
2014-10-09 18:28:09 +08:00
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
2014-12-22 11:20:03 +08:00
|
|
|
|
2014-10-09 18:28:09 +08:00
|
|
|
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.
|
|
|
|
****************************************************************************/
|
2016-03-20 21:53:44 +08:00
|
|
|
#include "ui/UIPageView.h"
|
|
|
|
#include "ui/UILayoutComponent.h"
|
2014-10-09 18:28:09 +08:00
|
|
|
#include "2d/CCNode.h"
|
2016-03-20 21:53:44 +08:00
|
|
|
#include "ui/GUIDefine.h"
|
|
|
|
#include "ui/UIHelper.h"
|
2014-10-09 18:28:09 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
namespace ui {
|
|
|
|
LayoutComponent::LayoutComponent()
|
2014-12-24 10:56:01 +08:00
|
|
|
:_horizontalEdge(HorizontalEdge::None)
|
|
|
|
, _verticalEdge(VerticalEdge::None)
|
2014-12-22 11:20:03 +08:00
|
|
|
, _leftMargin(0)
|
|
|
|
, _rightMargin(0)
|
2014-12-24 10:56:01 +08:00
|
|
|
, _bottomMargin(0)
|
2014-12-22 11:20:03 +08:00
|
|
|
, _topMargin(0)
|
2014-12-23 20:29:53 +08:00
|
|
|
, _usingPositionPercentX(false)
|
|
|
|
, _positionPercentX(0)
|
2014-12-22 11:20:03 +08:00
|
|
|
, _usingPositionPercentY(false)
|
|
|
|
, _positionPercentY(0)
|
2014-12-23 20:29:53 +08:00
|
|
|
, _usingStretchWidth(false)
|
|
|
|
, _usingStretchHeight(false)
|
2014-12-22 11:20:03 +08:00
|
|
|
, _percentWidth(0)
|
|
|
|
, _usingPercentWidth(false)
|
|
|
|
, _percentHeight(0)
|
|
|
|
, _usingPercentHeight(false)
|
|
|
|
, _actived(true)
|
2015-01-19 17:11:04 +08:00
|
|
|
, _isPercentOnly(false)
|
2014-10-09 18:28:09 +08:00
|
|
|
{
|
|
|
|
_name = __LAYOUT_COMPONENT_NAME;
|
|
|
|
}
|
2014-12-22 11:20:03 +08:00
|
|
|
|
2014-10-09 18:28:09 +08:00
|
|
|
LayoutComponent::~LayoutComponent()
|
|
|
|
{
|
2014-12-22 11:20:03 +08:00
|
|
|
|
2014-10-09 18:28:09 +08:00
|
|
|
}
|
2014-12-22 11:20:03 +08:00
|
|
|
|
2014-12-25 15:02:22 +08:00
|
|
|
LayoutComponent* LayoutComponent::bindLayoutComponent(Node* node)
|
2014-12-22 14:51:50 +08:00
|
|
|
{
|
|
|
|
LayoutComponent * layout = (LayoutComponent*)node->getComponent(__LAYOUT_COMPONENT_NAME);
|
|
|
|
if (layout != nullptr)
|
|
|
|
return layout;
|
|
|
|
|
|
|
|
layout = new (std::nothrow) LayoutComponent();
|
|
|
|
if (layout && layout->init())
|
|
|
|
{
|
|
|
|
layout->autorelease();
|
|
|
|
node->addComponent(layout);
|
|
|
|
return layout;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(layout);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-10-09 18:28:09 +08:00
|
|
|
bool LayoutComponent::init()
|
|
|
|
{
|
|
|
|
bool ret = true;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if (!Component::init())
|
|
|
|
{
|
|
|
|
ret = false;
|
|
|
|
break;
|
|
|
|
}
|
2014-12-22 11:20:03 +08:00
|
|
|
|
2015-10-12 12:54:06 +08:00
|
|
|
//put layout component initialized code here
|
2014-12-22 11:20:03 +08:00
|
|
|
|
2014-10-09 18:28:09 +08:00
|
|
|
} while (0);
|
|
|
|
return ret;
|
|
|
|
}
|
2014-12-22 11:20:03 +08:00
|
|
|
|
|
|
|
Node* LayoutComponent::getOwnerParent()
|
2014-10-09 18:28:09 +08:00
|
|
|
{
|
2014-12-22 11:20:03 +08:00
|
|
|
Node* parent = _owner->getParent();
|
|
|
|
return parent;
|
2014-10-09 18:28:09 +08:00
|
|
|
}
|
2014-12-22 11:20:03 +08:00
|
|
|
void LayoutComponent::refreshHorizontalMargin()
|
2014-10-09 18:28:09 +08:00
|
|
|
{
|
2014-12-22 11:20:03 +08:00
|
|
|
Node* parent = this->getOwnerParent();
|
|
|
|
if (parent == nullptr)
|
|
|
|
return;
|
|
|
|
|
2014-12-22 19:05:31 +08:00
|
|
|
const Point& ownerPoint = _owner->getPosition();
|
|
|
|
const Point& ownerAnchor = _owner->getAnchorPoint();
|
|
|
|
const Size& ownerSize = _owner->getContentSize();
|
|
|
|
const Size& parentSize = parent->getContentSize();
|
2014-10-17 19:13:37 +08:00
|
|
|
|
2014-12-22 11:20:03 +08:00
|
|
|
_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;
|
|
|
|
|
2014-12-22 19:05:31 +08:00
|
|
|
const Point& ownerPoint = _owner->getPosition();
|
|
|
|
const Point& ownerAnchor = _owner->getAnchorPoint();
|
|
|
|
const Size& ownerSize = _owner->getContentSize();
|
|
|
|
const Size& parentSize = parent->getContentSize();
|
2014-12-22 11:20:03 +08:00
|
|
|
|
2014-12-24 10:56:01 +08:00
|
|
|
_bottomMargin = ownerPoint.y - ownerAnchor.y * ownerSize.height;
|
2014-12-22 11:20:03 +08:00
|
|
|
_topMargin = parentSize.height - (ownerPoint.y + (1 - ownerAnchor.y) * ownerSize.height);
|
|
|
|
}
|
|
|
|
|
2014-12-22 18:39:44 +08:00
|
|
|
//OldVersion
|
2014-12-22 11:20:03 +08:00
|
|
|
void LayoutComponent::setUsingPercentContentSize(bool isUsed)
|
|
|
|
{
|
|
|
|
_usingPercentWidth = _usingPercentHeight = isUsed;
|
|
|
|
}
|
2014-12-22 19:05:31 +08:00
|
|
|
bool LayoutComponent::getUsingPercentContentSize()const
|
2014-12-22 11:20:03 +08:00
|
|
|
{
|
|
|
|
return _usingPercentWidth && _usingPercentHeight;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LayoutComponent::setPercentContentSize(const Vec2 &percent)
|
|
|
|
{
|
|
|
|
this->setPercentWidth(percent.x);
|
|
|
|
this->setPercentHeight(percent.y);
|
|
|
|
}
|
2014-12-22 19:05:31 +08:00
|
|
|
Vec2 LayoutComponent::getPercentContentSize()const
|
2014-12-22 11:20:03 +08:00
|
|
|
{
|
|
|
|
Vec2 vec2=Vec2(_percentWidth,_percentHeight);
|
|
|
|
return vec2;
|
|
|
|
}
|
|
|
|
|
2014-12-22 18:39:44 +08:00
|
|
|
//Position & Margin
|
2014-12-22 19:05:31 +08:00
|
|
|
const Point& LayoutComponent::getAnchorPosition()const
|
2014-12-22 11:20:03 +08:00
|
|
|
{
|
|
|
|
return _owner->getAnchorPoint();
|
|
|
|
}
|
2014-12-22 19:05:31 +08:00
|
|
|
void LayoutComponent::setAnchorPosition(const Point& point)
|
2014-12-22 11:20:03 +08:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2014-12-22 19:05:31 +08:00
|
|
|
const Point& LayoutComponent::getPosition()const
|
2014-12-22 11:20:03 +08:00
|
|
|
{
|
|
|
|
return _owner->getPosition();
|
|
|
|
}
|
2014-12-22 19:05:31 +08:00
|
|
|
void LayoutComponent::setPosition(const Point& position)
|
2014-12-22 11:20:03 +08:00
|
|
|
{
|
|
|
|
Node* parent = this->getOwnerParent();
|
|
|
|
if (parent != nullptr)
|
2014-10-17 19:13:37 +08:00
|
|
|
{
|
2014-12-24 10:56:01 +08:00
|
|
|
Point ownerPoint = position;
|
2014-12-22 19:05:31 +08:00
|
|
|
const Size& parentSize = parent->getContentSize();
|
2014-10-17 19:13:37 +08:00
|
|
|
|
2014-12-22 11:20:03 +08:00
|
|
|
if (parentSize.width != 0)
|
|
|
|
_positionPercentX = ownerPoint.x / parentSize.width;
|
2014-10-17 19:13:37 +08:00
|
|
|
else
|
2014-12-24 10:56:01 +08:00
|
|
|
{
|
2014-12-22 11:20:03 +08:00
|
|
|
_positionPercentX = 0;
|
2015-11-18 13:38:29 +08:00
|
|
|
if (_usingPositionPercentX || _horizontalEdge == HorizontalEdge::Center)
|
2014-12-24 10:56:01 +08:00
|
|
|
ownerPoint.x = 0;
|
|
|
|
}
|
2014-12-22 11:20:03 +08:00
|
|
|
|
|
|
|
if (parentSize.height != 0)
|
|
|
|
_positionPercentY = ownerPoint.y / parentSize.height;
|
|
|
|
else
|
2014-12-24 10:56:01 +08:00
|
|
|
{
|
2014-12-22 11:20:03 +08:00
|
|
|
_positionPercentY = 0;
|
2015-11-18 13:38:29 +08:00
|
|
|
if (_usingPositionPercentY || _verticalEdge == VerticalEdge::Center)
|
2014-12-24 10:56:01 +08:00
|
|
|
ownerPoint.y = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
_owner->setPosition(ownerPoint);
|
2014-12-22 11:20:03 +08:00
|
|
|
|
|
|
|
this->refreshHorizontalMargin();
|
|
|
|
this->refreshVerticalMargin();
|
2014-10-17 19:13:37 +08:00
|
|
|
}
|
2014-12-24 10:56:01 +08:00
|
|
|
else
|
|
|
|
_owner->setPosition(position);
|
2014-10-09 18:28:09 +08:00
|
|
|
}
|
|
|
|
|
2014-12-23 20:29:53 +08:00
|
|
|
bool LayoutComponent::isPositionPercentXEnabled()const
|
2014-10-09 18:28:09 +08:00
|
|
|
{
|
2014-12-22 11:20:03 +08:00
|
|
|
return _usingPositionPercentX;
|
2014-10-09 18:28:09 +08:00
|
|
|
}
|
2014-12-22 11:20:03 +08:00
|
|
|
void LayoutComponent::setPositionPercentXEnabled(bool isUsed)
|
2014-10-09 18:28:09 +08:00
|
|
|
{
|
2014-12-22 11:20:03 +08:00
|
|
|
_usingPositionPercentX = isUsed;
|
|
|
|
if (_usingPositionPercentX)
|
|
|
|
{
|
2014-12-24 10:56:01 +08:00
|
|
|
_horizontalEdge = HorizontalEdge::None;
|
2014-12-22 11:20:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-22 19:05:31 +08:00
|
|
|
float LayoutComponent::getPositionPercentX()const
|
2014-12-22 11:20:03 +08:00
|
|
|
{
|
|
|
|
return _positionPercentX;
|
|
|
|
}
|
|
|
|
void LayoutComponent::setPositionPercentX(float percentMargin)
|
|
|
|
{
|
|
|
|
_positionPercentX = percentMargin;
|
2014-10-17 19:13:37 +08:00
|
|
|
|
2015-11-18 13:38:29 +08:00
|
|
|
if (_usingPositionPercentX || _horizontalEdge == HorizontalEdge::Center)
|
2014-10-17 19:13:37 +08:00
|
|
|
{
|
2015-11-18 13:38:29 +08:00
|
|
|
Node* parent = this->getOwnerParent();
|
|
|
|
if (parent != nullptr)
|
|
|
|
{
|
|
|
|
_owner->setPositionX(parent->getContentSize().width * _positionPercentX);
|
|
|
|
this->refreshHorizontalMargin();
|
|
|
|
}
|
2014-10-17 19:13:37 +08:00
|
|
|
}
|
2014-10-09 18:28:09 +08:00
|
|
|
}
|
2014-12-22 11:20:03 +08:00
|
|
|
|
2014-12-23 20:29:53 +08:00
|
|
|
bool LayoutComponent::isPositionPercentYEnabled()const
|
2014-12-22 11:20:03 +08:00
|
|
|
{
|
|
|
|
return _usingPositionPercentY;
|
|
|
|
}
|
|
|
|
void LayoutComponent::setPositionPercentYEnabled(bool isUsed)
|
2014-10-09 18:28:09 +08:00
|
|
|
{
|
2014-12-22 11:20:03 +08:00
|
|
|
_usingPositionPercentY = isUsed;
|
|
|
|
if (_usingPositionPercentY)
|
|
|
|
{
|
2014-12-24 10:56:01 +08:00
|
|
|
_verticalEdge = VerticalEdge::None;
|
2014-12-22 11:20:03 +08:00
|
|
|
}
|
2014-10-09 18:28:09 +08:00
|
|
|
}
|
|
|
|
|
2014-12-22 19:05:31 +08:00
|
|
|
float LayoutComponent::getPositionPercentY()const
|
2014-10-09 18:28:09 +08:00
|
|
|
{
|
2014-12-22 11:20:03 +08:00
|
|
|
return _positionPercentY;
|
|
|
|
}
|
|
|
|
void LayoutComponent::setPositionPercentY(float percentMargin)
|
|
|
|
{
|
|
|
|
_positionPercentY = percentMargin;
|
2014-10-09 18:28:09 +08:00
|
|
|
|
2015-11-18 13:38:29 +08:00
|
|
|
if (_usingPositionPercentY || _verticalEdge == VerticalEdge::Center)
|
2014-10-09 18:28:09 +08:00
|
|
|
{
|
2015-11-18 13:38:29 +08:00
|
|
|
Node* parent = this->getOwnerParent();
|
|
|
|
if (parent != nullptr)
|
|
|
|
{
|
|
|
|
_owner->setPositionY(parent->getContentSize().height * _positionPercentY);
|
|
|
|
this->refreshVerticalMargin();
|
|
|
|
}
|
2014-10-09 18:28:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-24 10:56:01 +08:00
|
|
|
LayoutComponent::HorizontalEdge LayoutComponent::getHorizontalEdge()const
|
2014-10-09 18:28:09 +08:00
|
|
|
{
|
2014-12-24 10:56:01 +08:00
|
|
|
return _horizontalEdge;
|
2014-10-09 18:28:09 +08:00
|
|
|
}
|
2014-12-24 10:56:01 +08:00
|
|
|
void LayoutComponent::setHorizontalEdge(HorizontalEdge hEage)
|
2014-10-09 18:28:09 +08:00
|
|
|
{
|
2014-12-24 10:56:01 +08:00
|
|
|
_horizontalEdge = hEage;
|
|
|
|
if (_horizontalEdge != HorizontalEdge::None)
|
2014-10-17 19:13:37 +08:00
|
|
|
{
|
2014-12-22 11:20:03 +08:00
|
|
|
_usingPositionPercentX = false;
|
|
|
|
}
|
2014-10-09 18:28:09 +08:00
|
|
|
}
|
|
|
|
|
2014-12-24 10:56:01 +08:00
|
|
|
LayoutComponent::VerticalEdge LayoutComponent::getVerticalEdge()const
|
2014-10-09 18:28:09 +08:00
|
|
|
{
|
2014-12-24 10:56:01 +08:00
|
|
|
return _verticalEdge;
|
2014-10-09 18:28:09 +08:00
|
|
|
}
|
2014-12-24 10:56:01 +08:00
|
|
|
void LayoutComponent::setVerticalEdge(VerticalEdge vEage)
|
2014-10-09 18:28:09 +08:00
|
|
|
{
|
2014-12-24 10:56:01 +08:00
|
|
|
_verticalEdge = vEage;
|
|
|
|
if (_verticalEdge != VerticalEdge::None)
|
2014-12-22 11:20:03 +08:00
|
|
|
{
|
|
|
|
_usingPositionPercentY = false;
|
|
|
|
}
|
2014-10-09 18:28:09 +08:00
|
|
|
}
|
|
|
|
|
2014-12-22 19:05:31 +08:00
|
|
|
float LayoutComponent::getLeftMargin()const
|
2014-12-22 11:20:03 +08:00
|
|
|
{
|
|
|
|
return _leftMargin;
|
|
|
|
}
|
|
|
|
void LayoutComponent::setLeftMargin(float margin)
|
|
|
|
{
|
|
|
|
_leftMargin = margin;
|
|
|
|
}
|
|
|
|
|
2014-12-22 19:05:31 +08:00
|
|
|
float LayoutComponent::getRightMargin()const
|
2014-12-22 11:20:03 +08:00
|
|
|
{
|
|
|
|
return _rightMargin;
|
|
|
|
}
|
|
|
|
void LayoutComponent::setRightMargin(float margin)
|
|
|
|
{
|
|
|
|
_rightMargin = margin;
|
|
|
|
}
|
|
|
|
|
2014-12-22 19:05:31 +08:00
|
|
|
float LayoutComponent::getTopMargin()const
|
2014-10-09 18:28:09 +08:00
|
|
|
{
|
2014-12-22 11:20:03 +08:00
|
|
|
return _topMargin;
|
|
|
|
}
|
|
|
|
void LayoutComponent::setTopMargin(float margin)
|
|
|
|
{
|
|
|
|
_topMargin = margin;
|
|
|
|
}
|
|
|
|
|
2014-12-24 10:56:01 +08:00
|
|
|
float LayoutComponent::getBottomMargin()const
|
2014-12-22 11:20:03 +08:00
|
|
|
{
|
2014-12-24 10:56:01 +08:00
|
|
|
return _bottomMargin;
|
2014-12-22 11:20:03 +08:00
|
|
|
}
|
2014-12-24 10:56:01 +08:00
|
|
|
void LayoutComponent::setBottomMargin(float margin)
|
2014-12-22 11:20:03 +08:00
|
|
|
{
|
2014-12-24 10:56:01 +08:00
|
|
|
_bottomMargin = margin;
|
2014-12-22 11:20:03 +08:00
|
|
|
}
|
|
|
|
|
2014-12-22 18:39:44 +08:00
|
|
|
//Size & Percent
|
2014-12-22 19:05:31 +08:00
|
|
|
const Size& LayoutComponent::getSize()const
|
2014-12-22 11:20:03 +08:00
|
|
|
{
|
|
|
|
return this->getOwner()->getContentSize();
|
2014-10-09 18:28:09 +08:00
|
|
|
}
|
2014-12-23 20:29:53 +08:00
|
|
|
void LayoutComponent::setSize(const Size& size)
|
2014-10-09 18:28:09 +08:00
|
|
|
{
|
2014-12-22 11:20:03 +08:00
|
|
|
Node* parent = this->getOwnerParent();
|
|
|
|
if (parent != nullptr)
|
2014-10-17 19:13:37 +08:00
|
|
|
{
|
2014-12-24 10:56:01 +08:00
|
|
|
Size ownerSize = size;
|
2014-12-22 19:05:31 +08:00
|
|
|
const Size& parentSize = parent->getContentSize();
|
2014-10-17 19:13:37 +08:00
|
|
|
|
2014-12-22 11:20:03 +08:00
|
|
|
if (parentSize.width != 0)
|
|
|
|
_percentWidth = ownerSize.width / parentSize.width;
|
2014-10-17 19:13:37 +08:00
|
|
|
else
|
2014-12-24 10:56:01 +08:00
|
|
|
{
|
2014-12-22 11:20:03 +08:00
|
|
|
_percentWidth = 0;
|
2015-11-18 13:38:29 +08:00
|
|
|
if (_usingPercentWidth || (this->_horizontalEdge != HorizontalEdge::Center && this->_usingStretchWidth))
|
2014-12-24 10:56:01 +08:00
|
|
|
ownerSize.width = 0;
|
|
|
|
}
|
2014-12-22 11:20:03 +08:00
|
|
|
|
|
|
|
if (parentSize.height != 0)
|
|
|
|
_percentHeight = ownerSize.height / parentSize.height;
|
|
|
|
else
|
2014-12-24 10:56:01 +08:00
|
|
|
{
|
2014-12-22 11:20:03 +08:00
|
|
|
_percentHeight = 0;
|
2015-11-18 13:38:29 +08:00
|
|
|
if (_usingPercentHeight || (this->_verticalEdge != VerticalEdge::Center && this->_usingStretchHeight))
|
2014-12-24 10:56:01 +08:00
|
|
|
ownerSize.height = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
_owner->setContentSize(ownerSize);
|
2014-12-22 11:20:03 +08:00
|
|
|
|
|
|
|
this->refreshHorizontalMargin();
|
|
|
|
this->refreshVerticalMargin();
|
2014-10-17 19:13:37 +08:00
|
|
|
}
|
2014-12-24 10:56:01 +08:00
|
|
|
else
|
|
|
|
_owner->setContentSize(size);
|
2014-12-22 11:20:03 +08:00
|
|
|
}
|
|
|
|
|
2014-12-23 20:29:53 +08:00
|
|
|
bool LayoutComponent::isPercentWidthEnabled()const
|
2014-12-22 11:20:03 +08:00
|
|
|
{
|
|
|
|
return _usingPercentWidth;
|
|
|
|
}
|
|
|
|
void LayoutComponent::setPercentWidthEnabled(bool isUsed)
|
|
|
|
{
|
|
|
|
_usingPercentWidth = isUsed;
|
|
|
|
if (_usingPercentWidth)
|
2014-10-17 19:13:37 +08:00
|
|
|
{
|
2014-12-22 11:20:03 +08:00
|
|
|
_usingStretchWidth = false;
|
2014-10-17 19:13:37 +08:00
|
|
|
}
|
2014-12-22 11:20:03 +08:00
|
|
|
}
|
|
|
|
|
2014-12-22 19:05:31 +08:00
|
|
|
float LayoutComponent::getSizeWidth()const
|
2014-12-22 11:20:03 +08:00
|
|
|
{
|
|
|
|
return _owner->getContentSize().width;
|
|
|
|
}
|
|
|
|
void LayoutComponent::setSizeWidth(float width)
|
|
|
|
{
|
|
|
|
Size ownerSize = _owner->getContentSize();
|
|
|
|
ownerSize.width = width;
|
|
|
|
|
|
|
|
Node* parent = this->getOwnerParent();
|
|
|
|
if (parent != nullptr)
|
|
|
|
{
|
2014-12-22 19:05:31 +08:00
|
|
|
const Size& parentSize = parent->getContentSize();
|
2014-12-22 11:20:03 +08:00
|
|
|
if (parentSize.width != 0)
|
|
|
|
_percentWidth = ownerSize.width / parentSize.width;
|
|
|
|
else
|
2014-12-24 10:56:01 +08:00
|
|
|
{
|
2014-12-22 11:20:03 +08:00
|
|
|
_percentWidth = 0;
|
2014-12-24 10:56:01 +08:00
|
|
|
if (_usingPercentWidth)
|
|
|
|
ownerSize.width = 0;
|
|
|
|
}
|
|
|
|
_owner->setContentSize(ownerSize);
|
2014-12-22 11:20:03 +08:00
|
|
|
this->refreshHorizontalMargin();
|
|
|
|
}
|
2014-12-24 10:56:01 +08:00
|
|
|
else
|
|
|
|
_owner->setContentSize(ownerSize);
|
2014-10-09 18:28:09 +08:00
|
|
|
}
|
|
|
|
|
2014-12-22 19:05:31 +08:00
|
|
|
float LayoutComponent::getPercentWidth()const
|
2014-10-09 18:28:09 +08:00
|
|
|
{
|
2014-12-22 11:20:03 +08:00
|
|
|
return _percentWidth;
|
2014-10-09 18:28:09 +08:00
|
|
|
}
|
2014-12-22 11:20:03 +08:00
|
|
|
void LayoutComponent::setPercentWidth(float percentWidth)
|
2014-10-09 18:28:09 +08:00
|
|
|
{
|
2014-12-22 11:20:03 +08:00
|
|
|
_percentWidth = percentWidth;
|
2014-10-09 18:28:09 +08:00
|
|
|
|
2015-11-18 13:38:29 +08:00
|
|
|
if (_usingPercentWidth)
|
2014-10-09 18:28:09 +08:00
|
|
|
{
|
2015-11-18 13:38:29 +08:00
|
|
|
Node* parent = this->getOwnerParent();
|
|
|
|
if (parent != nullptr)
|
|
|
|
{
|
|
|
|
Size ownerSize = _owner->getContentSize();
|
|
|
|
ownerSize.width = parent->getContentSize().width * _percentWidth;
|
|
|
|
_owner->setContentSize(ownerSize);
|
2014-10-09 18:28:09 +08:00
|
|
|
|
2015-11-18 13:38:29 +08:00
|
|
|
this->refreshHorizontalMargin();
|
|
|
|
}
|
2014-12-22 11:20:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-23 20:29:53 +08:00
|
|
|
bool LayoutComponent::isPercentHeightEnabled()const
|
2014-12-22 11:20:03 +08:00
|
|
|
{
|
|
|
|
return _usingPercentHeight;
|
|
|
|
}
|
|
|
|
void LayoutComponent::setPercentHeightEnabled(bool isUsed)
|
|
|
|
{
|
|
|
|
_usingPercentHeight = isUsed;
|
|
|
|
if (_usingPercentHeight)
|
|
|
|
{
|
|
|
|
_usingStretchHeight = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-22 19:05:31 +08:00
|
|
|
float LayoutComponent::getSizeHeight()const
|
2014-12-22 11:20:03 +08:00
|
|
|
{
|
|
|
|
return _owner->getContentSize().height;
|
|
|
|
}
|
|
|
|
void LayoutComponent::setSizeHeight(float height)
|
|
|
|
{
|
|
|
|
Size ownerSize = _owner->getContentSize();
|
|
|
|
ownerSize.height = height;
|
|
|
|
|
|
|
|
Node* parent = this->getOwnerParent();
|
|
|
|
if (parent != nullptr)
|
|
|
|
{
|
2014-12-22 19:05:31 +08:00
|
|
|
const Size& parentSize = parent->getContentSize();
|
2014-12-22 11:20:03 +08:00
|
|
|
if (parentSize.height != 0)
|
|
|
|
_percentHeight = ownerSize.height / parentSize.height;
|
2014-10-17 19:13:37 +08:00
|
|
|
else
|
2014-12-24 10:56:01 +08:00
|
|
|
{
|
2014-12-22 11:20:03 +08:00
|
|
|
_percentHeight = 0;
|
2014-12-24 10:56:01 +08:00
|
|
|
if (_usingPercentHeight)
|
|
|
|
ownerSize.height = 0;
|
|
|
|
}
|
|
|
|
_owner->setContentSize(ownerSize);
|
2014-12-22 11:20:03 +08:00
|
|
|
this->refreshVerticalMargin();
|
2014-10-09 18:28:09 +08:00
|
|
|
}
|
2014-12-24 10:56:01 +08:00
|
|
|
else
|
|
|
|
_owner->setContentSize(ownerSize);
|
2014-10-09 18:28:09 +08:00
|
|
|
}
|
|
|
|
|
2014-12-22 19:05:31 +08:00
|
|
|
float LayoutComponent::getPercentHeight()const
|
2014-10-17 19:13:37 +08:00
|
|
|
{
|
2014-12-22 11:20:03 +08:00
|
|
|
return _percentHeight;
|
2014-10-17 19:13:37 +08:00
|
|
|
}
|
2014-12-22 11:20:03 +08:00
|
|
|
void LayoutComponent::setPercentHeight(float percentHeight)
|
2014-10-17 19:13:37 +08:00
|
|
|
{
|
2014-12-22 11:20:03 +08:00
|
|
|
_percentHeight = percentHeight;
|
|
|
|
|
2015-11-18 13:38:29 +08:00
|
|
|
if (_usingPercentHeight)
|
2014-12-22 11:20:03 +08:00
|
|
|
{
|
2015-11-18 13:38:29 +08:00
|
|
|
Node* parent = this->getOwnerParent();
|
|
|
|
if (parent != nullptr)
|
|
|
|
{
|
|
|
|
Size ownerSize = _owner->getContentSize();
|
|
|
|
ownerSize.height = parent->getContentSize().height * _percentHeight;
|
|
|
|
_owner->setContentSize(ownerSize);
|
2014-12-22 11:20:03 +08:00
|
|
|
|
2015-11-18 13:38:29 +08:00
|
|
|
this->refreshVerticalMargin();
|
|
|
|
}
|
2014-12-22 11:20:03 +08:00
|
|
|
}
|
2014-10-17 19:13:37 +08:00
|
|
|
}
|
|
|
|
|
2014-12-23 20:29:53 +08:00
|
|
|
bool LayoutComponent::isStretchWidthEnabled()const
|
2014-10-09 18:28:09 +08:00
|
|
|
{
|
2014-12-22 11:20:03 +08:00
|
|
|
return _usingStretchWidth;
|
|
|
|
}
|
|
|
|
void LayoutComponent::setStretchWidthEnabled(bool isUsed)
|
|
|
|
{
|
|
|
|
_usingStretchWidth = isUsed;
|
|
|
|
if (_usingStretchWidth)
|
|
|
|
{
|
|
|
|
_usingPercentWidth = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-23 20:29:53 +08:00
|
|
|
bool LayoutComponent::isStretchHeightEnabled()const
|
2014-12-22 11:20:03 +08:00
|
|
|
{
|
|
|
|
return _usingStretchHeight;
|
|
|
|
}
|
|
|
|
void LayoutComponent::setStretchHeightEnabled(bool isUsed)
|
|
|
|
{
|
|
|
|
_usingStretchHeight = isUsed;
|
|
|
|
if (_usingStretchHeight)
|
|
|
|
{
|
|
|
|
_usingPercentHeight = false;
|
|
|
|
}
|
2014-10-09 18:28:09 +08:00
|
|
|
}
|
2014-10-17 19:13:37 +08:00
|
|
|
|
2014-12-22 11:20:03 +08:00
|
|
|
void LayoutComponent::refreshLayout()
|
2014-10-17 19:13:37 +08:00
|
|
|
{
|
2015-01-19 17:11:04 +08:00
|
|
|
if (!_actived)
|
|
|
|
return;
|
|
|
|
|
2014-12-22 11:20:03 +08:00
|
|
|
Node* parent = this->getOwnerParent();
|
|
|
|
if (parent == nullptr)
|
|
|
|
return;
|
|
|
|
|
2014-12-22 19:05:31 +08:00
|
|
|
const Size& parentSize = parent->getContentSize();
|
|
|
|
const Point& ownerAnchor = _owner->getAnchorPoint();
|
2014-12-22 11:20:03 +08:00
|
|
|
Size ownerSize = _owner->getContentSize();
|
|
|
|
Point ownerPosition = _owner->getPosition();
|
|
|
|
|
2014-12-24 10:56:01 +08:00
|
|
|
switch (this->_horizontalEdge)
|
2014-12-22 11:20:03 +08:00
|
|
|
{
|
2014-12-24 10:56:01 +08:00
|
|
|
case HorizontalEdge::None:
|
2015-01-19 17:11:04 +08:00
|
|
|
if (_usingStretchWidth && !_isPercentOnly)
|
2014-12-22 11:20:03 +08:00
|
|
|
{
|
|
|
|
ownerSize.width = parentSize.width * _percentWidth;
|
|
|
|
ownerPosition.x = _leftMargin + ownerAnchor.x * ownerSize.width;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (_usingPositionPercentX)
|
|
|
|
ownerPosition.x = parentSize.width * _positionPercentX;
|
|
|
|
if (_usingPercentWidth)
|
|
|
|
ownerSize.width = parentSize.width * _percentWidth;
|
|
|
|
}
|
|
|
|
break;
|
2014-12-24 10:56:01 +08:00
|
|
|
case HorizontalEdge::Left:
|
2015-01-19 17:11:04 +08:00
|
|
|
if (_isPercentOnly)
|
|
|
|
break;
|
2014-12-22 11:20:03 +08:00
|
|
|
if (_usingPercentWidth || _usingStretchWidth)
|
|
|
|
ownerSize.width = parentSize.width * _percentWidth;
|
|
|
|
ownerPosition.x = _leftMargin + ownerAnchor.x * ownerSize.width;
|
|
|
|
break;
|
2014-12-24 10:56:01 +08:00
|
|
|
case HorizontalEdge::Right:
|
2015-01-19 17:11:04 +08:00
|
|
|
if (_isPercentOnly)
|
|
|
|
break;
|
2014-12-22 11:20:03 +08:00
|
|
|
if (_usingPercentWidth || _usingStretchWidth)
|
|
|
|
ownerSize.width = parentSize.width * _percentWidth;
|
|
|
|
ownerPosition.x = parentSize.width - (_rightMargin + (1 - ownerAnchor.x) * ownerSize.width);
|
|
|
|
break;
|
2014-12-24 10:56:01 +08:00
|
|
|
case HorizontalEdge::Center:
|
2015-01-19 17:11:04 +08:00
|
|
|
if (_isPercentOnly)
|
|
|
|
break;
|
|
|
|
if (_usingStretchWidth)
|
2014-12-22 11:20:03 +08:00
|
|
|
{
|
|
|
|
ownerSize.width = parentSize.width - _leftMargin - _rightMargin;
|
2014-12-24 10:56:01 +08:00
|
|
|
if (ownerSize.width < 0)
|
|
|
|
ownerSize.width = 0;
|
2014-12-22 11:20:03 +08:00
|
|
|
ownerPosition.x = _leftMargin + ownerAnchor.x * ownerSize.width;
|
|
|
|
}
|
|
|
|
else
|
2015-01-19 17:11:04 +08:00
|
|
|
{
|
|
|
|
if (_usingPercentWidth)
|
|
|
|
ownerSize.width = parentSize.width * _percentWidth;
|
2014-12-22 11:20:03 +08:00
|
|
|
ownerPosition.x = parentSize.width * _positionPercentX;
|
2015-01-19 17:11:04 +08:00
|
|
|
}
|
2014-12-22 11:20:03 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-12-24 10:56:01 +08:00
|
|
|
switch (this->_verticalEdge)
|
2014-10-17 19:13:37 +08:00
|
|
|
{
|
2014-12-24 10:56:01 +08:00
|
|
|
case VerticalEdge::None:
|
2015-01-19 17:11:04 +08:00
|
|
|
if (_usingStretchHeight && !_isPercentOnly)
|
2014-12-22 11:20:03 +08:00
|
|
|
{
|
|
|
|
ownerSize.height = parentSize.height * _percentHeight;
|
2014-12-24 10:56:01 +08:00
|
|
|
ownerPosition.y = _bottomMargin + ownerAnchor.y * ownerSize.height;
|
2014-12-22 11:20:03 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (_usingPositionPercentY)
|
|
|
|
ownerPosition.y = parentSize.height * _positionPercentY;
|
|
|
|
if (_usingPercentHeight)
|
|
|
|
ownerSize.height = parentSize.height * _percentHeight;
|
|
|
|
}
|
|
|
|
break;
|
2014-12-24 10:56:01 +08:00
|
|
|
case VerticalEdge::Bottom:
|
2015-01-19 17:11:04 +08:00
|
|
|
if (_isPercentOnly)
|
|
|
|
break;
|
2014-12-22 11:20:03 +08:00
|
|
|
if (_usingPercentHeight || _usingStretchHeight)
|
|
|
|
ownerSize.height = parentSize.height * _percentHeight;
|
2014-12-24 10:56:01 +08:00
|
|
|
ownerPosition.y = _bottomMargin + ownerAnchor.y * ownerSize.height;
|
2014-10-17 19:13:37 +08:00
|
|
|
break;
|
2014-12-24 10:56:01 +08:00
|
|
|
case VerticalEdge::Top:
|
2015-01-19 17:11:04 +08:00
|
|
|
if (_isPercentOnly)
|
|
|
|
break;
|
2014-12-22 11:20:03 +08:00
|
|
|
if (_usingPercentHeight || _usingStretchHeight)
|
|
|
|
ownerSize.height = parentSize.height * _percentHeight;
|
|
|
|
ownerPosition.y = parentSize.height - (_topMargin + (1 - ownerAnchor.y) * ownerSize.height);
|
2014-10-17 19:13:37 +08:00
|
|
|
break;
|
2014-12-24 10:56:01 +08:00
|
|
|
case VerticalEdge::Center:
|
2015-01-19 17:11:04 +08:00
|
|
|
if (_isPercentOnly)
|
|
|
|
break;
|
|
|
|
if (_usingStretchHeight)
|
2014-12-22 11:20:03 +08:00
|
|
|
{
|
2014-12-24 10:56:01 +08:00
|
|
|
ownerSize.height = parentSize.height - _topMargin - _bottomMargin;
|
|
|
|
if (ownerSize.height < 0)
|
|
|
|
ownerSize.height = 0;
|
|
|
|
ownerPosition.y = _bottomMargin + ownerAnchor.y * ownerSize.height;
|
2014-12-22 11:20:03 +08:00
|
|
|
}
|
|
|
|
else
|
2015-01-19 17:11:04 +08:00
|
|
|
{
|
|
|
|
if (_usingPercentHeight)
|
|
|
|
ownerSize.height = parentSize.height * _percentHeight;
|
2014-12-22 11:20:03 +08:00
|
|
|
ownerPosition.y = parentSize.height* _positionPercentY;
|
2015-01-19 17:11:04 +08:00
|
|
|
}
|
2014-10-17 19:13:37 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2014-12-22 11:20:03 +08:00
|
|
|
|
|
|
|
_owner->setPosition(ownerPosition);
|
|
|
|
_owner->setContentSize(ownerSize);
|
|
|
|
|
2015-07-06 16:02:19 +08:00
|
|
|
if (typeid(*_owner) == typeid(PageView))
|
|
|
|
{
|
|
|
|
PageView* page = static_cast<PageView*>(_owner);
|
|
|
|
page->forceDoLayout();
|
|
|
|
|
2015-11-20 14:53:36 +08:00
|
|
|
Vector<Widget*> _widgetVector = page->getItems();
|
|
|
|
for(auto& item : _widgetVector)
|
2015-07-06 16:02:19 +08:00
|
|
|
{
|
2015-07-06 16:39:00 +08:00
|
|
|
ui::Helper::doLayout(item);
|
2015-07-06 16:02:19 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ui::Helper::doLayout(_owner);
|
|
|
|
}
|
2014-12-22 11:20:03 +08:00
|
|
|
}
|
|
|
|
|
2014-12-23 20:29:53 +08:00
|
|
|
void LayoutComponent::setActiveEnabled(bool enable)
|
2014-12-22 11:20:03 +08:00
|
|
|
{
|
|
|
|
_actived = enable;
|
2014-10-17 19:13:37 +08:00
|
|
|
}
|
2015-01-19 17:11:04 +08:00
|
|
|
|
|
|
|
void LayoutComponent::setPercentOnlyEnabled(bool enable)
|
|
|
|
{
|
|
|
|
_isPercentOnly = enable;
|
|
|
|
}
|
2014-10-09 18:28:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|