From 56e42b0a0a55b744d02c028d6d7f812fbdfddeec Mon Sep 17 00:00:00 2001 From: Liam Date: Mon, 22 Dec 2014 11:20:03 +0800 Subject: [PATCH] update for lay out --- cocos/ui/UIHelper.cpp | 14 +- cocos/ui/UIHelper.h | 6 + cocos/ui/UILayoutComponent.cpp | 736 +++++++++++++++++++++++---------- cocos/ui/UILayoutComponent.h | 190 ++++++--- cocos/ui/UIWidget.cpp | 128 ++---- cocos/ui/UIWidget.h | 2 +- 6 files changed, 693 insertions(+), 383 deletions(-) diff --git a/cocos/ui/UIHelper.cpp b/cocos/ui/UIHelper.cpp index 8335944b55..b523dcb44a 100644 --- a/cocos/ui/UIHelper.cpp +++ b/cocos/ui/UIHelper.cpp @@ -167,19 +167,7 @@ void Helper::doLayout(cocos2d::Node *rootNode) if (nullptr != com && nullptr != parent) { LayoutComponent* layoutComponent = (LayoutComponent*)com; - if (layoutComponent->isUsingPercentPosition()) - { - layoutComponent->setPercentPosition(layoutComponent->getPercentPosition()); - } - else if (layoutComponent->getReferencePoint() != LayoutComponent::ReferencePoint::BOTTOM_LEFT) - { - layoutComponent->setRelativePosition(layoutComponent->getRelativePosition()); - } - - if (layoutComponent->isUsingPercentContentSize()) - { - layoutComponent->setPercentContentSize(layoutComponent->getPercentContentSize()); - } + layoutComponent->refreshLayout(); } } } diff --git a/cocos/ui/UIHelper.h b/cocos/ui/UIHelper.h index 5f86288960..8a48f5126b 100644 --- a/cocos/ui/UIHelper.h +++ b/cocos/ui/UIHelper.h @@ -79,6 +79,12 @@ public: std::string::size_type start, std::string::size_type length); + /** + * Refresh object and it's children lay out state + * + *@param rootNode object which will be changed + * + */ static void doLayout(Node *rootNode); static void changeLayoutSystemActiveState(bool bActive); diff --git a/cocos/ui/UILayoutComponent.cpp b/cocos/ui/UILayoutComponent.cpp index 64f6f39beb..8fda1baed4 100644 --- a/cocos/ui/UILayoutComponent.cpp +++ b/cocos/ui/UILayoutComponent.cpp @@ -1,18 +1,18 @@ /**************************************************************************** Copyright (c) 2013-2014 Chukong Technologies Inc. - + http://www.cocos2d-x.org - + 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 @@ -25,25 +25,38 @@ #include "UILayoutComponent.h" #include "2d/CCNode.h" #include "GUIDefine.h" - +#include "UIHelper.h" NS_CC_BEGIN namespace ui { LayoutComponent::LayoutComponent() - : _usingPercentContentSize(false) - , _referencePoint(ReferencePoint::BOTTOM_LEFT) - , _usingPercentPosition(false) - , _actived(true) + :_horizontalEage(HorizontalEage::None) + , _verticalEage(VerticalEage::None) + , _leftMargin(0) + , _rightMargin(0) + , _usingPositionPercentX(false) + , _positionPercentX(0) + , _buttomMargin(0) + , _topMargin(0) + , _usingPositionPercentY(false) + , _positionPercentY(0) + , _percentWidth(0) + , _usingPercentWidth(false) + , _percentHeight(0) + , _usingPercentHeight(false) + , _actived(true) + ,_usingStretchWidth(false) + , _usingStretchHeight(false) { _name = __LAYOUT_COMPONENT_NAME; } - + LayoutComponent::~LayoutComponent() { - + } - + bool LayoutComponent::init() { bool ret = true; @@ -54,254 +67,531 @@ namespace ui { ret = false; break; } - + //put layout component initalized code here - + } while (0); return ret; } - - //Size - Vec2 LayoutComponent::getOwnerContentSize()const + + Node* LayoutComponent::getOwnerParent() + { + Node* parent = _owner->getParent(); + return parent; + } + void LayoutComponent::refreshHorizontalMargin() + { + Node* parent = this->getOwnerParent(); + if (parent == nullptr) + return; + + Point ownerPoint = _owner->getPosition(); + Point ownerAnchor = _owner->getAnchorPoint(); + Size ownerSize = _owner->getContentSize(); + Size 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; + + Point ownerPoint = _owner->getPosition(); + Point ownerAnchor = _owner->getAnchorPoint(); + Size ownerSize = _owner->getContentSize(); + Size parentSize = parent->getContentSize(); + + _buttomMargin = ownerPoint.y - ownerAnchor.y * ownerSize.height; + _topMargin = parentSize.height - (ownerPoint.y + (1 - ownerAnchor.y) * ownerSize.height); + } + +#pragma region OldVersion + void LayoutComponent::setUsingPercentContentSize(bool isUsed) + { + _usingPercentWidth = _usingPercentHeight = isUsed; + } + bool LayoutComponent::getUsingPercentContentSize() + { + return _usingPercentWidth && _usingPercentHeight; + } + + void LayoutComponent::setPercentContentSize(const Vec2 &percent) + { + this->setPercentWidth(percent.x); + this->setPercentHeight(percent.y); + } + Vec2 LayoutComponent::getPercentContentSize() + { + Vec2 vec2=Vec2(_percentWidth,_percentHeight); + return vec2; + } +#pragma endregion + +#pragma region Position & Margin + Point LayoutComponent::getAnchorPosition() + { + return _owner->getAnchorPoint(); + } + void LayoutComponent::setAnchorPosition(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); + } + + Point LayoutComponent::getPosition() + { + return _owner->getPosition(); + } + void LayoutComponent::setPosition(Point position) + { + _owner->setPosition(position); + + Node* parent = this->getOwnerParent(); + if (parent != nullptr) + { + Point ownerPoint = _owner->getPosition(); + Size parentSize = parent->getContentSize(); + + if (parentSize.width != 0) + _positionPercentX = ownerPoint.x / parentSize.width; + else + _positionPercentX = 0; + + if (parentSize.height != 0) + _positionPercentY = ownerPoint.y / parentSize.height; + else + _positionPercentY = 0; + + this->refreshHorizontalMargin(); + this->refreshVerticalMargin(); + } + } + + bool LayoutComponent::isUsingPositionPercentX() + { + return _usingPositionPercentX; + } + void LayoutComponent::setPositionPercentXEnabled(bool isUsed) + { + _usingPositionPercentX = isUsed; + if (_usingPositionPercentX) + { + _horizontalEage = HorizontalEage::None; + } + } + + float LayoutComponent::getPositionPercentX() + { + return _positionPercentX; + } + void LayoutComponent::setPositionPercentX(float percentMargin) + { + _positionPercentX = percentMargin; + + Node* parent = this->getOwnerParent(); + if (parent != nullptr) + { + _owner->setPositionX(parent->getContentSize().width * _positionPercentX); + this->refreshHorizontalMargin(); + } + } + + bool LayoutComponent::isUsingPositionPercentY() + { + return _usingPositionPercentY; + } + void LayoutComponent::setPositionPercentYEnabled(bool isUsed) + { + _usingPositionPercentY = isUsed; + if (_usingPositionPercentY) + { + _verticalEage = VerticalEage::None; + } + } + + float LayoutComponent::getPositionPercentY() + { + return _positionPercentY; + } + void LayoutComponent::setPositionPercentY(float percentMargin) + { + _positionPercentY = percentMargin; + + Node* parent = this->getOwnerParent(); + if (parent != nullptr) + { + _owner->setPositionY(parent->getContentSize().height * _positionPercentY); + this->refreshVerticalMargin(); + } + } + + LayoutComponent::HorizontalEage LayoutComponent::getHorizontalEage() + { + return _horizontalEage; + } + void LayoutComponent::setHorizontalEage(HorizontalEage hEage) + { + _horizontalEage = hEage; + if (_horizontalEage != HorizontalEage::None) + { + _usingPositionPercentX = false; + } + + Node* parent = this->getOwnerParent(); + if (parent != nullptr) + { + Size parentSize = parent->getContentSize(); + if (parentSize.width != 0) + _positionPercentX = _owner->getPosition().x / parentSize.width; + else + _positionPercentX = 0; + + this->refreshHorizontalMargin(); + } + } + + LayoutComponent::VerticalEage LayoutComponent::getVerticalEage() + { + return _verticalEage; + } + void LayoutComponent::setVerticalEage(VerticalEage vEage) + { + _verticalEage = vEage; + if (_verticalEage != VerticalEage::None) + { + _usingPositionPercentY = false; + } + + Node* parent = this->getOwnerParent(); + if (parent != nullptr) + { + Size parentSize = parent->getContentSize(); + if (parentSize.height != 0) + _positionPercentY = _owner->getPosition().y / parentSize.height; + else + _positionPercentY = 0; + + this->refreshVerticalMargin(); + } + } + + float LayoutComponent::getLeftMargin() + { + return _leftMargin; + } + void LayoutComponent::setLeftMargin(float margin) + { + _leftMargin = margin; + } + + float LayoutComponent::getRightMargin() + { + return _rightMargin; + } + void LayoutComponent::setRightMargin(float margin) + { + _rightMargin = margin; + } + + float LayoutComponent::getTopMargin() + { + return _topMargin; + } + void LayoutComponent::setTopMargin(float margin) + { + _topMargin = margin; + } + + float LayoutComponent::getButtomMargin() + { + return _buttomMargin; + } + void LayoutComponent::setButtomMargin(float margin) + { + _buttomMargin = margin; + } + +#pragma endregion + +#pragma region Size & Percent + Size LayoutComponent::getSize() { return this->getOwner()->getContentSize(); } - void LayoutComponent::setOwnerContentSize(const Vec2& size) + void LayoutComponent::setSize(Size _size) { - this->getOwner()->setContentSize(Size(size.x,size.y)); + _owner->setContentSize(_size); - Node* parentNode = this->getOwner()->getParent(); - if (parentNode != NULL && _actived) + Node* parent = this->getOwnerParent(); + if (parent != nullptr) { - Size parentSize = parentNode->getContentSize(); + Size ownerSize = _owner->getContentSize(); + Size parentSize = parent->getContentSize(); - if (parentSize.width != 0 && parentSize.height != 0) + if (parentSize.width != 0) + _percentWidth = ownerSize.width / parentSize.width; + else + _percentWidth = 0; + + if (parentSize.height != 0) + _percentHeight = ownerSize.height / parentSize.height; + else + _percentHeight = 0; + + this->refreshHorizontalMargin(); + this->refreshVerticalMargin(); + } + } + + bool LayoutComponent::isUsingPercentWidth() + { + return _usingPercentWidth; + } + void LayoutComponent::setPercentWidthEnabled(bool isUsed) + { + _usingPercentWidth = isUsed; + if (_usingPercentWidth) + { + _usingStretchWidth = false; + } + } + + float LayoutComponent::getSizeWidth() + { + return _owner->getContentSize().width; + } + void LayoutComponent::setSizeWidth(float width) + { + Size ownerSize = _owner->getContentSize(); + ownerSize.width = width; + _owner->setContentSize(ownerSize); + + Node* parent = this->getOwnerParent(); + if (parent != nullptr) + { + Size parentSize = parent->getContentSize(); + if (parentSize.width != 0) + _percentWidth = ownerSize.width / parentSize.width; + else + _percentWidth = 0; + + this->refreshHorizontalMargin(); + } + } + + float LayoutComponent::getPercentWidth() + { + return _percentWidth; + } + void LayoutComponent::setPercentWidth(float percentWidth) + { + _percentWidth = percentWidth; + + Node* parent = this->getOwnerParent(); + if (parent != nullptr) + { + Size ownerSize = _owner->getContentSize(); + ownerSize.width = parent->getContentSize().width * _percentWidth; + _owner->setContentSize(ownerSize); + + this->refreshHorizontalMargin(); + } + } + + bool LayoutComponent::isUsingPercentHeight() + { + return _usingPercentHeight; + } + void LayoutComponent::setPercentHeightEnabled(bool isUsed) + { + _usingPercentHeight = isUsed; + if (_usingPercentHeight) + { + _usingStretchHeight = false; + } + } + + float LayoutComponent::getSizeHeight() + { + return _owner->getContentSize().height; + } + void LayoutComponent::setSizeHeight(float height) + { + Size ownerSize = _owner->getContentSize(); + ownerSize.height = height; + _owner->setContentSize(ownerSize); + + Node* parent = this->getOwnerParent(); + if (parent != nullptr) + { + Size parentSize = parent->getContentSize(); + if (parentSize.height != 0) + _percentHeight = ownerSize.height / parentSize.height; + else + _percentHeight = 0; + + this->refreshVerticalMargin(); + } + } + + float LayoutComponent::getPercentHeight() + { + return _percentHeight; + } + void LayoutComponent::setPercentHeight(float percentHeight) + { + _percentHeight = percentHeight; + + Node* parent = this->getOwnerParent(); + if (parent != nullptr) + { + Size ownerSize = _owner->getContentSize(); + ownerSize.height = parent->getContentSize().height * _percentHeight; + _owner->setContentSize(ownerSize); + + this->refreshVerticalMargin(); + } + } + + bool LayoutComponent::isUsingStretchWidth() + { + return _usingStretchWidth; + } + void LayoutComponent::setStretchWidthEnabled(bool isUsed) + { + _usingStretchWidth = isUsed; + if (_usingStretchWidth) + { + _usingPercentWidth = false; + } + } + + bool LayoutComponent::isUsingStretchHeight() + { + return _usingStretchHeight; + } + void LayoutComponent::setStretchHeightEnabled(bool isUsed) + { + _usingStretchHeight = isUsed; + if (_usingStretchHeight) + { + _usingPercentHeight = false; + } + } + +#pragma endregion + + void LayoutComponent::refreshLayout() + { + Node* parent = this->getOwnerParent(); + if (parent == nullptr) + return; + + Size parentSize = parent->getContentSize(); + Size ownerSize = _owner->getContentSize(); + Point ownerAnchor = _owner->getAnchorPoint(); + Point ownerPosition = _owner->getPosition(); + + switch (this->_horizontalEage) + { + case HorizontalEage::None: + if (_usingStretchWidth) { - _percentContentSize = Point(size.x/parentSize.width,size.y/parentSize.height); + ownerSize.width = parentSize.width * _percentWidth; + ownerPosition.x = _leftMargin + ownerAnchor.x * ownerSize.width; } else { - _percentContentSize = Point(0,0); + if (_usingPositionPercentX) + ownerPosition.x = parentSize.width * _positionPercentX; + if (_usingPercentWidth) + ownerSize.width = parentSize.width * _percentWidth; } - } - } - - const Vec2& LayoutComponent::getPercentContentSize()const - { - return _percentContentSize; - } - - void LayoutComponent::setPercentContentSize(const Vec2& percent) - { - _percentContentSize = percent; - - Node* parentNode = this->getOwner()->getParent(); - if (parentNode != NULL && _actived) - { - Size parentSize = parentNode->getContentSize(); - if (_usingPercentContentSize) + break; + case HorizontalEage::Left: + if (_usingPercentWidth || _usingStretchWidth) + ownerSize.width = parentSize.width * _percentWidth; + ownerPosition.x = _leftMargin + ownerAnchor.x * ownerSize.width; + break; + case HorizontalEage::Right: + if (_usingPercentWidth || _usingStretchWidth) + ownerSize.width = parentSize.width * _percentWidth; + ownerPosition.x = parentSize.width - (_rightMargin + (1 - ownerAnchor.x) * ownerSize.width); + break; + case HorizontalEage::Center: + if (_usingPercentWidth || _usingStretchWidth) { - this->getOwner()->setContentSize(Size(percent.x*parentSize.width,percent.y*parentSize.height)); + ownerSize.width = parentSize.width - _leftMargin - _rightMargin; + ownerPosition.x = _leftMargin + ownerAnchor.x * ownerSize.width; } + else + ownerPosition.x = parentSize.width * _positionPercentX; + break; + default: + break; } - } - - bool LayoutComponent::isUsingPercentContentSize() - { - return _usingPercentContentSize; - } - void LayoutComponent::setUsingPercentContentSize(bool flag) - { - _usingPercentContentSize = flag; - - Node* parentNode = this->getOwner()->getParent(); - if (parentNode != NULL && _actived) + switch (this->_verticalEage) { - Size parentSize = parentNode->getContentSize(); - if (_usingPercentContentSize) + case VerticalEage::None: + if (_usingStretchHeight) { - Size baseSize = this->getOwner()->getContentSize(); - if (parentSize.width != 0) - { - _percentContentSize.x = baseSize.width/parentSize.width; - } - else - { - _percentContentSize.x = 0; - baseSize.width = 0; - } - - if (parentSize.height != 0) - { - _percentContentSize.y = baseSize.height/parentSize.height; - } - else - { - _percentContentSize.y = 0; - baseSize.height = 0; - } - - this->getOwner()->setContentSize(baseSize); - } - } - } - - //Position - bool LayoutComponent::isUsingPercentPosition() - { - return _usingPercentPosition; - } - void LayoutComponent::setUsingPercentPosition(bool flag) - { - _usingPercentPosition = flag; - - Node* parentNode = this->getOwner()->getParent(); - if (parentNode != NULL && _actived) - { - Size parentSize = parentNode->getContentSize(); - - if (_usingPercentPosition) - { - if (parentSize.width != 0) - { - _percentPosition.x = _relativePosition.x/parentSize.width; - } - else - { - _percentPosition.x = 0; - _relativePosition.x = 0; - } - - if (parentSize.height != 0) - { - _percentPosition.y = _relativePosition.y/parentSize.height; - } - else - { - _percentPosition.y = 0; - _relativePosition.y = 0; - } - } - - Point inversePoint = this->converPointWithReferencePointAndSize(_relativePosition,parentSize); - this->getOwner()->setPosition(inversePoint); - } - } - - const Vec2& LayoutComponent::getPercentPosition() - { - return _percentPosition; - } - void LayoutComponent::setPercentPosition(const Vec2& percent) - { - _percentPosition = percent; - - Node* parentNode = this->getOwner()->getParent(); - if (parentNode != NULL && _actived) - { - Size parentSize = parentNode->getContentSize(); - _relativePosition = Point(_percentPosition.x*parentSize.width,_percentPosition.y*parentSize.height); - Point inversePoint = this->converPointWithReferencePointAndSize(_relativePosition,parentSize); - this->getOwner()->setPosition(inversePoint); - } - } - - const Vec2& LayoutComponent::getOwnerPosition()const - { - return this->getOwner()->getPosition(); - } - void LayoutComponent::setOwnerPosition(const Vec2& point) - { - Node* parentNode = this->getOwner()->getParent(); - if (parentNode != NULL && _actived) - { - Size parentSize = parentNode->getContentSize(); - - Point inversePoint = this->converPointWithReferencePointAndSize(point,parentSize); - this->getOwner()->setPosition(point); - _relativePosition = inversePoint; - if (parentSize.width != 0 && parentSize.height != 0) - { - _percentPosition = Point(_relativePosition.x/parentSize.width,_relativePosition.y/parentSize.height); + ownerSize.height = parentSize.height * _percentHeight; + ownerPosition.y = _buttomMargin + ownerAnchor.y * ownerSize.height; } else { - _percentPosition = Point(0,0); + if (_usingPositionPercentY) + ownerPosition.y = parentSize.height * _positionPercentY; + if (_usingPercentHeight) + ownerSize.height = parentSize.height * _percentHeight; } - } - else - { - this->getOwner()->setPosition(point); - if (_referencePoint == ReferencePoint::BOTTOM_LEFT) + break; + case VerticalEage::Buttom: + if (_usingPercentHeight || _usingStretchHeight) + ownerSize.height = parentSize.height * _percentHeight; + ownerPosition.y = _buttomMargin + ownerAnchor.y * ownerSize.height; + break; + case VerticalEage::Top: + if (_usingPercentHeight || _usingStretchHeight) + ownerSize.height = parentSize.height * _percentHeight; + ownerPosition.y = parentSize.height - (_topMargin + (1 - ownerAnchor.y) * ownerSize.height); + break; + case VerticalEage::Center: + if (_usingPercentHeight || _usingStretchHeight) { - _relativePosition = point; - } - } - - } - - const Vec2& LayoutComponent::getRelativePosition() - { - return _relativePosition; - } - void LayoutComponent::setRelativePosition(const Vec2& position) - { - _relativePosition = position; - - Node* parentNode = this->getOwner()->getParent(); - if (parentNode != NULL && _actived) - { - Size parentSize = parentNode->getContentSize(); - - Point inversePoint = this->converPointWithReferencePointAndSize(_relativePosition,parentSize); - this->getOwner()->setPosition(inversePoint); - if (parentSize.width != 0 && parentSize.height != 0) - { - _percentPosition = Point(_relativePosition.x/parentSize.width,_relativePosition.y/parentSize.height); + ownerSize.height = parentSize.height - _topMargin - _buttomMargin; + ownerPosition.y = _buttomMargin + ownerAnchor.y * ownerSize.height; } else - { - _percentPosition = Point(0,0); - } + ownerPosition.y = parentSize.height* _positionPercentY; + break; + default: + break; } - } - LayoutComponent::ReferencePoint LayoutComponent::getReferencePoint() - { - return _referencePoint; - } - void LayoutComponent::setReferencePoint(ReferencePoint point) - { - _referencePoint = point; - this->setRelativePosition(_relativePosition); + _owner->setPosition(ownerPosition); + _owner->setContentSize(ownerSize); + + ui::Helper::doLayout(_owner); } void LayoutComponent::setActiveEnable(bool enable) { _actived = enable; } - - Vec2 LayoutComponent::converPointWithReferencePointAndSize(const Vec2& point,const Size& size) - { - Point inversePoint = point; - switch (_referencePoint) - { - case ReferencePoint::TOP_LEFT: - inversePoint.y = size.height - inversePoint.y; - break; - case ReferencePoint::BOTTOM_RIGHT: - inversePoint.x = size.width - inversePoint.x; - break; - case ReferencePoint::TOP_RIGHT: - inversePoint.x = size.width - inversePoint.x; - inversePoint.y = size.height - inversePoint.y; - break; - default: - break; - } - return inversePoint; - } } NS_CC_END \ No newline at end of file diff --git a/cocos/ui/UILayoutComponent.h b/cocos/ui/UILayoutComponent.h index b45b5ad7c0..f7104f09f7 100644 --- a/cocos/ui/UILayoutComponent.h +++ b/cocos/ui/UILayoutComponent.h @@ -29,68 +29,138 @@ THE SOFTWARE. NS_CC_BEGIN - namespace ui { - class CC_GUI_DLL LayoutComponent : public Component +namespace ui { + class CC_GUI_DLL LayoutComponent : public Component + { + public: + LayoutComponent(); + ~LayoutComponent(); + virtual bool init()override; + CREATE_FUNC(LayoutComponent); + + enum class HorizontalEage { - public: - LayoutComponent(); - ~LayoutComponent(); - virtual bool init()override; - CREATE_FUNC(LayoutComponent); - /** - * When a node has a ReferencePositoin with value equals LEFT_BOTTOM, - * it will treat the left bottom corner of its parent as the origin(0,0) when positioning itself - * which is the same as cocos2d-x does. But you can change it by assigning a - * different ReferencePosition. - * For example: If you use ReferencePosition with value equals RIGHT_TOP, - * then it will treat the right top corner of its parent as the origin(0,0) when positioning itself. - */ - enum class ReferencePoint - { - BOTTOM_LEFT, - TOP_LEFT, - BOTTOM_RIGHT, - TOP_RIGHT - }; - - bool isUsingPercentPosition(); - void setUsingPercentPosition(bool flag); - - const Vec2& getPercentPosition(); - void setPercentPosition(const Vec2& percent); - - const Vec2& getRelativePosition(); - void setRelativePosition(const Vec2& position); - - void setReferencePoint(ReferencePoint point); - ReferencePoint getReferencePoint(); - - const Vec2& getOwnerPosition()const; - void setOwnerPosition(const Vec2& point); - - Vec2 getOwnerContentSize()const; - void setOwnerContentSize(const Vec2& size); - - const Vec2& getPercentContentSize()const; - void setPercentContentSize(const Vec2& percent); - - bool isUsingPercentContentSize(); - void setUsingPercentContentSize(bool flag); - - void setActiveEnable(bool enable); - private: - Vec2 converPointWithReferencePointAndSize(const Vec2& point,const Size& size); - private: - - Vec2 _percentContentSize; - bool _usingPercentContentSize; - - ReferencePoint _referencePoint; - Vec2 _relativePosition; - Vec2 _percentPosition; - bool _usingPercentPosition; - bool _actived; + None, + Left, + Right, + Center }; + enum class VerticalEage + { + None, + Buttom, + Top, + Center + }; +#pragma region OldVersion + virtual void setUsingPercentContentSize(bool isUsed); + virtual bool getUsingPercentContentSize(); + + virtual void setPercentContentSize(const Vec2 &percent); + virtual Vec2 getPercentContentSize(); +#pragma endregion + +#pragma region Position & Margin + virtual Point getAnchorPosition(); + virtual void setAnchorPosition(Point point); + + virtual Point getPosition(); + virtual void setPosition(Point position); + + virtual bool isUsingPositionPercentX(); + virtual void setPositionPercentXEnabled(bool isUsed); + + virtual float getPositionPercentX(); + virtual void setPositionPercentX(float percentMargin); + + virtual bool isUsingPositionPercentY(); + virtual void setPositionPercentYEnabled(bool isUsed); + + virtual float getPositionPercentY(); + virtual void setPositionPercentY(float percentMargin); + + virtual HorizontalEage getHorizontalEage(); + virtual void setHorizontalEage(HorizontalEage hEage); + + virtual VerticalEage getVerticalEage(); + virtual void setVerticalEage(VerticalEage vEage); + + virtual float getLeftMargin(); + virtual void setLeftMargin(float margin); + + virtual float getRightMargin(); + virtual void setRightMargin(float margin); + + virtual float getTopMargin(); + virtual void setTopMargin(float margin); + + virtual float getButtomMargin(); + virtual void setButtomMargin(float margin); + +#pragma endregion + +#pragma region Size & Percent + virtual Size getSize(); + virtual void setSize(Size _size); + + virtual bool isUsingPercentWidth(); + virtual void setPercentWidthEnabled(bool isUsed); + + virtual float getSizeWidth(); + virtual void setSizeWidth(float width); + + virtual float getPercentWidth(); + virtual void setPercentWidth(float percentWidth); + + virtual bool isUsingPercentHeight(); + virtual void setPercentHeightEnabled(bool isUsed); + + virtual float getSizeHeight(); + virtual void setSizeHeight(float height); + + virtual float getPercentHeight(); + virtual void setPercentHeight(float percentHeight); + + virtual bool isUsingStretchWidth(); + virtual void setStretchWidthEnabled(bool isUsed); + + virtual bool isUsingStretchHeight(); + virtual void setStretchHeightEnabled(bool isUsed); + +#pragma endregion + + virtual void setActiveEnable(bool enable); + virtual void refreshLayout(); + + protected: + Node* getOwnerParent(); + virtual void refreshHorizontalMargin(); + virtual void refreshVerticalMargin(); + protected: + HorizontalEage _horizontalEage; + VerticalEage _verticalEage; + + float _leftMargin; + float _rightMargin; + float _buttomMargin; + float _topMargin; + + bool _usingPositionPercentX; + float _positionPercentX; + bool _usingPositionPercentY; + float _positionPercentY; + + bool _usingStretchWidth; + bool _usingStretchHeight; + + float _percentWidth; + bool _usingPercentWidth; + + float _percentHeight; + bool _usingPercentHeight; + + bool _actived; + }; } NS_CC_END diff --git a/cocos/ui/UIWidget.cpp b/cocos/ui/UIWidget.cpp index 3f0a02492a..eb4f996e14 100644 --- a/cocos/ui/UIWidget.cpp +++ b/cocos/ui/UIWidget.cpp @@ -222,7 +222,6 @@ bool Widget::init() void Widget::onEnter() { - updateSizeAndPosition(); ProtectedNode::onEnter(); } @@ -281,30 +280,7 @@ void Widget::setContentSize(const cocos2d::Size &contentSize) { _contentSize = getVirtualRendererSize(); } - if (_running) - { - Widget* widgetParent = getWidgetParent(); - Size pSize; - if (widgetParent) - { - pSize = widgetParent->getContentSize(); - } - else - { - pSize = _parent->getContentSize(); - } - float spx = 0.0f; - float spy = 0.0f; - if (pSize.width > 0.0f) - { - spx = _customSize.width / pSize.width; - } - if (pSize.height > 0.0f) - { - spy = _customSize.height / pSize.height; - } - _sizePercent = Vec2(spx, spy); - } + onSizeChanged(); } @@ -315,29 +291,27 @@ void Widget::setSize(const Size &size) void Widget::setSizePercent(const Vec2 &percent) { - _sizePercent = percent; - Size cSize = _customSize; - if (_running) + auto component = this->getOrCreateLayoutComponent(); + component->setUsingPercentContentSize(true); + component->setPercentContentSize(percent); + component->refreshLayout(); +} + + +void Widget::setSizeType(SizeType type) +{ + _sizeType = type; + + auto component = this->getOrCreateLayoutComponent(); + + if (_sizeType == Widget::SizeType::PERCENT) { - Widget* widgetParent = getWidgetParent(); - if (widgetParent) - { - cSize = Size(widgetParent->getContentSize().width * percent.x , widgetParent->getContentSize().height * percent.y); - } - else - { - cSize = Size(_parent->getContentSize().width * percent.x , _parent->getContentSize().height * percent.y); - } - } - if (_ignoreSize) - { - this->setContentSize(getVirtualRendererSize()); + component->setUsingPercentContentSize(true); } else { - this->setContentSize(cSize); + component->setUsingPercentContentSize(false); } - _customSize = cSize; } void Widget::updateSizeAndPosition() @@ -419,11 +393,6 @@ void Widget::updateSizeAndPosition(const cocos2d::Size &parentSize) setPosition(absPos); } -void Widget::setSizeType(SizeType type) -{ - _sizeType = type; -} - Widget::SizeType Widget::getSizeType() const { return _sizeType; @@ -469,7 +438,9 @@ const Size& Widget::getCustomSize() const const Vec2& Widget::getSizePercent() { - return _sizePercent; + auto component = this->getOrCreateLayoutComponent(); + + return component->getPercentContentSize(); } Vec2 Widget::getWorldPosition()const @@ -484,14 +455,6 @@ Node* Widget::getVirtualRenderer() void Widget::onSizeChanged() { - for (auto& child : getChildren()) - { - Widget* widgetChild = dynamic_cast(child); - if (widgetChild) - { - widgetChild->updateSizeAndPosition(); - } - } } Size Widget::getVirtualRendererSize() const @@ -948,47 +911,40 @@ void Widget::interceptTouchEvent(cocos2d::ui::Widget::TouchEventType event, coco void Widget::setPosition(const Vec2 &pos) { - if (_running) - { - Widget* widgetParent = getWidgetParent(); - if (widgetParent) - { - Size pSize = widgetParent->getContentSize(); - if (pSize.width <= 0.0f || pSize.height <= 0.0f) - { - _positionPercent = Vec2::ZERO; - } - else - { - _positionPercent = Vec2(pos.x / pSize.width, pos.y / pSize.height); - } - } - } ProtectedNode::setPosition(pos); } void Widget::setPositionPercent(const Vec2 &percent) { - _positionPercent = percent; - if (_running) - { - Widget* widgetParent = getWidgetParent(); - if (widgetParent) - { - Size parentSize = widgetParent->getContentSize(); - Vec2 absPos = Vec2(parentSize.width * _positionPercent.x, parentSize.height * _positionPercent.y); - setPosition(absPos); - } - } + auto component = this->getOrCreateLayoutComponent(); + component->setPositionPercentX(percent.x); + component->setPositionPercentY(percent.y); + component->refreshLayout(); } -const Vec2& Widget::getPositionPercent()const{ - return _positionPercent; +Vec2 Widget::getPositionPercent(){ + + auto component = this->getOrCreateLayoutComponent(); + float percentX = component->getPositionPercentX(); + float percentY = component->getPositionPercentY(); + + return Vec2(percentX,percentY); } void Widget::setPositionType(PositionType type) { + auto component = this->getOrCreateLayoutComponent(); _positionType = type; + if (type == Widget::PositionType::ABSOLUTE) + { + component->setPositionPercentXEnabled(false); + component->setPositionPercentYEnabled(false); + } + else + { + component->setPositionPercentXEnabled(true); + component->setPositionPercentYEnabled(true); + } } Widget::PositionType Widget::getPositionType() const diff --git a/cocos/ui/UIWidget.h b/cocos/ui/UIWidget.h index 3d4e11e60f..b82a7f6104 100644 --- a/cocos/ui/UIWidget.h +++ b/cocos/ui/UIWidget.h @@ -272,7 +272,7 @@ public: * * @return The percent (x,y) of the widget in OpenGL coordinates */ - const Vec2& getPositionPercent()const; + Vec2 getPositionPercent(); /** * Changes the position type of the widget