From 56e42b0a0a55b744d02c028d6d7f812fbdfddeec Mon Sep 17 00:00:00 2001 From: Liam Date: Mon, 22 Dec 2014 11:20:03 +0800 Subject: [PATCH 01/13] 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 From 7c4d31feabde174be7980b97950b2f18165f987c Mon Sep 17 00:00:00 2001 From: Liam Date: Mon, 22 Dec 2014 14:51:50 +0800 Subject: [PATCH 02/13] update for lay out samples --- cocos/ui/UILayoutComponent.cpp | 19 +- cocos/ui/UILayoutComponent.h | 4 +- .../CocoStudioGUITest/CocosGUIScene.cpp | 2 +- .../UILayoutTest/UILayoutTest.cpp | 538 +++++++++++------- .../UILayoutTest/UILayoutTest.h | 26 + .../CocoStudioGUITest/UISceneManager.cpp | 8 + .../UITest/CocoStudioGUITest/UISceneManager.h | 2 + 7 files changed, 402 insertions(+), 197 deletions(-) diff --git a/cocos/ui/UILayoutComponent.cpp b/cocos/ui/UILayoutComponent.cpp index 8fda1baed4..ddfcb6b56a 100644 --- a/cocos/ui/UILayoutComponent.cpp +++ b/cocos/ui/UILayoutComponent.cpp @@ -57,6 +57,23 @@ namespace ui { } + LayoutComponent* LayoutComponent::boundingLayoutForNode(Node* node) + { + 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; + } + bool LayoutComponent::init() { bool ret = true; @@ -559,7 +576,7 @@ namespace ui { ownerSize.height = parentSize.height * _percentHeight; } break; - case VerticalEage::Buttom: + case VerticalEage::Bottom: if (_usingPercentHeight || _usingStretchHeight) ownerSize.height = parentSize.height * _percentHeight; ownerPosition.y = _buttomMargin + ownerAnchor.y * ownerSize.height; diff --git a/cocos/ui/UILayoutComponent.h b/cocos/ui/UILayoutComponent.h index f7104f09f7..fddc18e36d 100644 --- a/cocos/ui/UILayoutComponent.h +++ b/cocos/ui/UILayoutComponent.h @@ -38,6 +38,8 @@ namespace ui { virtual bool init()override; CREATE_FUNC(LayoutComponent); + static LayoutComponent* boundingLayoutForNode(Node* node); + enum class HorizontalEage { None, @@ -48,7 +50,7 @@ namespace ui { enum class VerticalEage { None, - Buttom, + Bottom, Top, Center }; diff --git a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/CocosGUIScene.cpp b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/CocosGUIScene.cpp index 8afec2b91d..42209c0ebb 100644 --- a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/CocosGUIScene.cpp +++ b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/CocosGUIScene.cpp @@ -199,7 +199,7 @@ g_guisTests[] = UISceneManager* sceneManager = UISceneManager::sharedUISceneManager(); sceneManager->setCurrentUISceneId(kUILayoutTest); sceneManager->setMinUISceneId(kUILayoutTest); - sceneManager->setMaxUISceneId(kUILayoutTest_Layout_Relative_Location); + sceneManager->setMaxUISceneId(kUILayoutComponent_Berth_Stretch_Test); Scene* scene = sceneManager->currentUIScene(); Director::getInstance()->replaceScene(scene); } diff --git a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UILayoutTest/UILayoutTest.cpp b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UILayoutTest/UILayoutTest.cpp index 3868af3b0c..1fe715a664 100644 --- a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UILayoutTest/UILayoutTest.cpp +++ b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UILayoutTest/UILayoutTest.cpp @@ -18,50 +18,50 @@ bool UILayoutTest::init() if (UIScene::init()) { Size widgetSize = _widget->getContentSize(); - + // Add the alert - Text* alert = Text::create("Layout", "fonts/Marker Felt.ttf", 30 ); + Text* alert = Text::create("Layout", "fonts/Marker Felt.ttf", 30); alert->setColor(Color3B(159, 168, 176)); alert->setPosition(Vec2(widgetSize.width / 2.0f, - widgetSize.height / 2.0f - alert->getContentSize().height * 3.075f)); - + widgetSize.height / 2.0f - alert->getContentSize().height * 3.075f)); + _uiLayer->addChild(alert); - - Layout* root = static_cast(_uiLayer->getChildByTag(81)) ; - + + Layout* root = static_cast(_uiLayer->getChildByTag(81)); + Layout* background = static_cast(root->getChildByName("background_Panel")); - + // Create the layout Layout* layout = Layout::create(); layout->setContentSize(Size(280, 150)); Size backgroundSize = background->getContentSize(); layout->setPosition(Vec2((widgetSize.width - backgroundSize.width) / 2.0f + - (backgroundSize.width - layout->getContentSize().width) / 2.0f, - (widgetSize.height - backgroundSize.height) / 2.0f + - (backgroundSize.height - layout->getContentSize().height) / 2.0f)); + (backgroundSize.width - layout->getContentSize().width) / 2.0f, + (widgetSize.height - backgroundSize.height) / 2.0f + + (backgroundSize.height - layout->getContentSize().height) / 2.0f)); _uiLayer->addChild(layout); - + Button* button = Button::create("cocosui/animationbuttonnormal.png", "cocosui/animationbuttonpressed.png"); button->setPosition(Vec2(button->getContentSize().width / 2.0f, - layout->getContentSize().height - button->getContentSize().height / 2.0f)); + layout->getContentSize().height - button->getContentSize().height / 2.0f)); layout->addChild(button); - + Button* titleButton = Button::create("cocosui/backtotopnormal.png", "cocosui/backtotoppressed.png"); titleButton->setTitleText("Title Button"); titleButton->setPosition(Vec2(layout->getContentSize().width / 2.0f, layout->getContentSize().height / 2.0f)); layout->addChild(titleButton); - + Button* button_scale9 = Button::create("cocosui/button.png", "cocosui/buttonHighlighted.png"); button_scale9->setScale9Enabled(true); button_scale9->setContentSize(Size(100.0f, button_scale9->getVirtualRendererSize().height)); button_scale9->setPosition(Vec2(layout->getContentSize().width - button_scale9->getContentSize().width / 2.0f, - button_scale9->getContentSize().height / 2.0f)); - + button_scale9->getContentSize().height / 2.0f)); + layout->addChild(button_scale9); - + return true; } - + return false; } @@ -80,19 +80,19 @@ bool UILayoutTest_Color::init() if (UIScene::init()) { Size widgetSize = _widget->getContentSize(); - + // Add the alert Text* alert = Text::create("Layout color render", "fonts/Marker Felt.ttf", 30); alert->setColor(Color3B(159, 168, 176)); alert->setPosition(Vec2(widgetSize.width / 2.0f, - widgetSize.height / 2.0f - alert->getContentSize().height * 3.075f)); - + widgetSize.height / 2.0f - alert->getContentSize().height * 3.075f)); + _uiLayer->addChild(alert); - + Layout* root = static_cast(_uiLayer->getChildByTag(81)); - + Layout* background = static_cast(root->getChildByName("background_Panel")); - + // Create the layout with color render Layout* layout = Layout::create(); layout->setBackGroundColorType(Layout::BackGroundColorType::SOLID); @@ -100,30 +100,30 @@ bool UILayoutTest_Color::init() layout->setContentSize(Size(280, 150)); Size backgroundSize = background->getContentSize(); layout->setPosition(Vec2((widgetSize.width - backgroundSize.width) / 2.0f + - (backgroundSize.width - layout->getContentSize().width) / 2.0f, - (widgetSize.height - backgroundSize.height) / 2.0f + - (backgroundSize.height - layout->getContentSize().height) / 2.0f)); + (backgroundSize.width - layout->getContentSize().width) / 2.0f, + (widgetSize.height - backgroundSize.height) / 2.0f + + (backgroundSize.height - layout->getContentSize().height) / 2.0f)); _uiLayer->addChild(layout); - + Button* button = Button::create("cocosui/animationbuttonnormal.png", "cocosui/animationbuttonpressed.png"); button->setPosition(Vec2(button->getContentSize().width / 2.0f, - layout->getContentSize().height - button->getContentSize().height / 2.0f)); - + layout->getContentSize().height - button->getContentSize().height / 2.0f)); + layout->addChild(button); - + Button* titleButton = Button::create("cocosui/backtotopnormal.png", "cocosui/backtotoppressed.png"); titleButton->setTitleText("Title Button"); titleButton->setPosition(Vec2(layout->getContentSize().width / 2.0f, layout->getContentSize().height / 2.0f)); layout->addChild(titleButton); - + Button* button_scale9 = Button::create("cocosui/button.png", "cocosui/buttonHighlighted.png"); button_scale9->setScale9Enabled(true); button_scale9->setContentSize(Size(100.0f, button_scale9->getVirtualRendererSize().height)); button_scale9->setPosition(Vec2(layout->getContentSize().width - button_scale9->getContentSize().width / 2.0f, - button_scale9->getContentSize().height / 2.0f)); - - layout->addChild(button_scale9); - + button_scale9->getContentSize().height / 2.0f)); + + layout->addChild(button_scale9); + return true; } return false; @@ -132,7 +132,7 @@ bool UILayoutTest_Color::init() // UILayoutTest_Gradient UILayoutTest_Gradient::UILayoutTest_Gradient() -{ +{ } UILayoutTest_Gradient::~UILayoutTest_Gradient() @@ -144,19 +144,19 @@ bool UILayoutTest_Gradient::init() if (UIScene::init()) { Size widgetSize = _widget->getContentSize(); - + // Add the alert Text* alert = Text::create("Layout gradient render", "fonts/Marker Felt.ttf", 30); alert->setColor(Color3B(159, 168, 176)); alert->setPosition(Vec2(widgetSize.width / 2.0f, - widgetSize.height / 2.0f - alert->getContentSize().height * 3.075f)); - + widgetSize.height / 2.0f - alert->getContentSize().height * 3.075f)); + _uiLayer->addChild(alert); - + Layout* root = static_cast(_uiLayer->getChildByTag(81)); - + Layout* background = static_cast(root->getChildByName("background_Panel")); - + // Create the layout with gradient render Layout* layout = Layout::create(); layout->setBackGroundColorType(Layout::BackGroundColorType::GRADIENT); @@ -164,30 +164,30 @@ bool UILayoutTest_Gradient::init() layout->setContentSize(Size(280, 150)); Size backgroundSize = background->getContentSize(); layout->setPosition(Vec2((widgetSize.width - backgroundSize.width) / 2.0f + - (backgroundSize.width - layout->getContentSize().width) / 2.0f, - (widgetSize.height - backgroundSize.height) / 2.0f + - (backgroundSize.height - layout->getContentSize().height) / 2.0f)); + (backgroundSize.width - layout->getContentSize().width) / 2.0f, + (widgetSize.height - backgroundSize.height) / 2.0f + + (backgroundSize.height - layout->getContentSize().height) / 2.0f)); _uiLayer->addChild(layout); - + Button* button = Button::create("cocosui/animationbuttonnormal.png", "cocosui/animationbuttonpressed.png"); button->setPosition(Vec2(button->getContentSize().width / 2.0f, - layout->getContentSize().height - button->getContentSize().height / 2.0f)); - + layout->getContentSize().height - button->getContentSize().height / 2.0f)); + layout->addChild(button); - + Button* titleButton = Button::create("cocosui/backtotopnormal.png", "cocosui/backtotoppressed.png"); titleButton->setTitleText("Title Button"); titleButton->setPosition(Vec2(layout->getContentSize().width / 2.0f, layout->getContentSize().height / 2.0f)); layout->addChild(titleButton); - + Button* button_scale9 = Button::create("cocosui/button.png", "cocosui/buttonHighlighted.png"); button_scale9->setScale9Enabled(true); button_scale9->setContentSize(Size(100.0f, button_scale9->getVirtualRendererSize().height)); button_scale9->setPosition(Vec2(layout->getContentSize().width - button_scale9->getContentSize().width / 2.0f, - button_scale9->getContentSize().height / 2.0f)); - - layout->addChild(button_scale9); - + button_scale9->getContentSize().height / 2.0f)); + + layout->addChild(button_scale9); + return true; } return false; @@ -208,17 +208,17 @@ bool UILayoutTest_BackGroundImage::init() if (UIScene::init()) { Size widgetSize = _widget->getContentSize(); - + // Add the alert Text* alert = Text::create("Layout background image", "fonts/Marker Felt.ttf", 20); alert->setColor(Color3B(159, 168, 176)); alert->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 4.5f)); _uiLayer->addChild(alert); - + Layout* root = static_cast(_uiLayer->getChildByTag(81)); - + Layout* background = dynamic_cast(root->getChildByName("background_Panel")); - + // Create the layout with background image Layout* layout = Layout::create(); layout->setClippingEnabled(true); @@ -226,29 +226,29 @@ bool UILayoutTest_BackGroundImage::init() layout->setContentSize(Size(280, 150)); Size backgroundSize = background->getContentSize(); layout->setPosition(Vec2((widgetSize.width - backgroundSize.width) / 2.0f + - (backgroundSize.width - layout->getContentSize().width) / 2.0f, - (widgetSize.height - backgroundSize.height) / 2.0f + - (backgroundSize.height - layout->getContentSize().height) / 2.0f)); + (backgroundSize.width - layout->getContentSize().width) / 2.0f, + (widgetSize.height - backgroundSize.height) / 2.0f + + (backgroundSize.height - layout->getContentSize().height) / 2.0f)); _uiLayer->addChild(layout); - + Button* button = Button::create("cocosui/animationbuttonnormal.png", "cocosui/animationbuttonpressed.png"); button->setPosition(Vec2(button->getContentSize().width / 2.0f, - layout->getContentSize().height - button->getContentSize().height / 2.0f)); + layout->getContentSize().height - button->getContentSize().height / 2.0f)); layout->addChild(button); - + Button* titleButton = Button::create("cocosui/backtotopnormal.png", "cocosui/backtotoppressed.png"); titleButton->setTitleText("Title Button"); titleButton->setPosition(Vec2(layout->getContentSize().width / 2.0f, layout->getContentSize().height / 2.0f)); layout->addChild(titleButton); - + Button* button_scale9 = Button::create("cocosui/button.png", "cocosui/buttonHighlighted.png"); button_scale9->setScale9Enabled(true); button_scale9->setContentSize(Size(100.0f, button_scale9->getVirtualRendererSize().height)); button_scale9->setPosition(Vec2(layout->getContentSize().width - button_scale9->getContentSize().width / 2.0f, - button_scale9->getContentSize().height / 2.0f)); - - layout->addChild(button_scale9); - + button_scale9->getContentSize().height / 2.0f)); + + layout->addChild(button_scale9); + return true; } return false; @@ -269,17 +269,17 @@ bool UILayoutTest_BackGroundImage_Scale9::init() if (UIScene::init()) { Size widgetSize = _widget->getContentSize(); - + // Add the alert Text* alert = Text::create("Layout background image scale9", "fonts/Marker Felt.ttf", 20); alert->setColor(Color3B(159, 168, 176)); alert->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 4.5f)); _uiLayer->addChild(alert); - + Layout* root = static_cast(_uiLayer->getChildByTag(81)); - + Layout* background = dynamic_cast(root->getChildByName("background_Panel")); - + // Create the layout with background image Layout* layout = Layout::create(); layout->setBackGroundImageScale9Enabled(true); @@ -287,29 +287,29 @@ bool UILayoutTest_BackGroundImage_Scale9::init() layout->setContentSize(Size(280, 150)); Size backgroundSize = background->getContentSize(); layout->setPosition(Vec2((widgetSize.width - backgroundSize.width) / 2.0f + - (backgroundSize.width - layout->getContentSize().width) / 2.0f, - (widgetSize.height - backgroundSize.height) / 2.0f + - (backgroundSize.height - layout->getContentSize().height) / 2.0f)); + (backgroundSize.width - layout->getContentSize().width) / 2.0f, + (widgetSize.height - backgroundSize.height) / 2.0f + + (backgroundSize.height - layout->getContentSize().height) / 2.0f)); _uiLayer->addChild(layout); - + Button* button = Button::create("cocosui/animationbuttonnormal.png", "cocosui/animationbuttonpressed.png"); button->setPosition(Vec2(button->getContentSize().width / 2.0f, - layout->getContentSize().height - button->getContentSize().height / 2.0f)); - + layout->getContentSize().height - button->getContentSize().height / 2.0f)); + layout->addChild(button); - + Button* titleButton = Button::create("cocosui/backtotopnormal.png", "cocosui/backtotoppressed.png"); titleButton->setTitleText("Title Button"); titleButton->setPosition(Vec2(layout->getContentSize().width / 2.0f, layout->getContentSize().height / 2.0f)); layout->addChild(titleButton); - + Button* button_scale9 = Button::create("cocosui/button.png", "cocosui/buttonHighlighted.png"); button_scale9->setScale9Enabled(true); button_scale9->setContentSize(Size(100.0f, button_scale9->getVirtualRendererSize().height)); button_scale9->setPosition(Vec2(layout->getContentSize().width - button_scale9->getContentSize().width / 2.0f, - button_scale9->getContentSize().height / 2.0f)); + button_scale9->getContentSize().height / 2.0f)); layout->addChild(button_scale9); - + return true; } return false; @@ -330,64 +330,64 @@ bool UILayoutTest_Layout_Linear_Vertical::init() if (UIScene::init()) { Size widgetSize = _widget->getContentSize(); - + // Add the alert Text* alert = Text::create("Layout Linear Vertical", "fonts/Marker Felt.ttf", 20); alert->setColor(Color3B(159, 168, 176)); alert->setPosition(Vec2(widgetSize.width / 2.0f, - widgetSize.height / 2.0f - alert->getContentSize().height * 4.5f)); - + widgetSize.height / 2.0f - alert->getContentSize().height * 4.5f)); + _uiLayer->addChild(alert); - + Layout* root = static_cast(_uiLayer->getChildByTag(81)); - + Layout* background = static_cast(root->getChildByName("background_Panel")); - + // Create the layout Layout* layout = Layout::create(); layout->setLayoutType(LayoutType::VERTICAL); layout->setContentSize(Size(280, 150)); Size backgroundSize = background->getContentSize(); layout->setPosition(Vec2((widgetSize.width - backgroundSize.width) / 2.0f + - (backgroundSize.width - layout->getContentSize().width) / 2.0f, - (widgetSize.height - backgroundSize.height) / 2.0f + - (backgroundSize.height - layout->getContentSize().height) / 2.0f)); + (backgroundSize.width - layout->getContentSize().width) / 2.0f, + (widgetSize.height - backgroundSize.height) / 2.0f + + (backgroundSize.height - layout->getContentSize().height) / 2.0f)); _uiLayer->addChild(layout); - - + + Button* button = Button::create("cocosui/animationbuttonnormal.png", "cocosui/animationbuttonpressed.png"); layout->addChild(button); - + LinearLayoutParameter* lp1 = LinearLayoutParameter::create(); button->setLayoutParameter(lp1); lp1->setGravity(LinearLayoutParameter::LinearGravity::CENTER_HORIZONTAL); lp1->setMargin(Margin(0.0f, 5.0f, 0.0f, 10.0f)); - - + + Button* titleButton = Button::create("cocosui/backtotopnormal.png", "cocosui/backtotoppressed.png"); titleButton->setTitleText("Title Button"); layout->addChild(titleButton); - + LinearLayoutParameter* lp2 = LinearLayoutParameter::create(); titleButton->setLayoutParameter(lp2); lp2->setGravity(LinearLayoutParameter::LinearGravity::CENTER_HORIZONTAL); lp2->setMargin(Margin(0.0f, 10.0f, 0.0f, 10.0f)); - - + + Button* button_scale9 = Button::create("cocosui/button.png", "cocosui/buttonHighlighted.png"); button_scale9->setScale9Enabled(true); button_scale9->setContentSize(Size(100.0f, button_scale9->getVirtualRendererSize().height)); layout->addChild(button_scale9); - + LinearLayoutParameter* lp3 = LinearLayoutParameter::create(); button_scale9->setLayoutParameter(lp3); lp3->setGravity(LinearLayoutParameter::LinearGravity::CENTER_HORIZONTAL); lp3->setMargin(Margin(0.0f, 10.0f, 0.0f, 10.0f)); - - + + return true; } - + return false; } @@ -406,17 +406,17 @@ bool UILayoutTest_Layout_Linear_Horizontal::init() if (UIScene::init()) { Size widgetSize = _widget->getContentSize(); - + // Add the alert Text* alert = Text::create("Layout Linear Horizontal", "fonts/Marker Felt.ttf", 20); alert->setColor(Color3B(159, 168, 176)); alert->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 4.5f)); _uiLayer->addChild(alert); - + Layout* root = static_cast(_uiLayer->getChildByTag(81)); - + Layout* background = dynamic_cast(root->getChildByName("background_Panel")); - + // Create the layout Layout* layout = Layout::create(); layout->setLayoutType(LayoutType::HORIZONTAL); @@ -424,44 +424,44 @@ bool UILayoutTest_Layout_Linear_Horizontal::init() layout->setContentSize(Size(280, 150)); Size backgroundSize = background->getContentSize(); layout->setPosition(Vec2((widgetSize.width - backgroundSize.width) / 2.0f + - (backgroundSize.width - layout->getContentSize().width) / 2.0f, - (widgetSize.height - backgroundSize.height) / 2.0f + - (backgroundSize.height - layout->getContentSize().height) / 2.0f)); + (backgroundSize.width - layout->getContentSize().width) / 2.0f, + (widgetSize.height - backgroundSize.height) / 2.0f + + (backgroundSize.height - layout->getContentSize().height) / 2.0f)); _uiLayer->addChild(layout); - + Button* button = Button::create("cocosui/animationbuttonnormal.png", "cocosui/animationbuttonpressed.png"); layout->addChild(button); - + LinearLayoutParameter* lp1 = LinearLayoutParameter::create(); button->setLayoutParameter(lp1); lp1->setGravity(LinearLayoutParameter::LinearGravity::CENTER_VERTICAL); lp1->setMargin(Margin(0.0f, 10.0f, 0.0f, 10.0f)); - - + + Button* titleButton = Button::create("cocosui/backtotopnormal.png", "cocosui/backtotoppressed.png"); titleButton->setTitleText("Title Button"); layout->addChild(titleButton); - + LinearLayoutParameter* lp2 = LinearLayoutParameter::create(); titleButton->setLayoutParameter(lp2); lp2->setGravity(LinearLayoutParameter::LinearGravity::CENTER_VERTICAL); lp2->setMargin(Margin(0.0f, 10.0f, 0.0f, 10.0f)); - - + + Button* button_scale9 = Button::create("cocosui/button.png", "cocosui/buttonHighlighted.png"); button_scale9->setScale9Enabled(true); button_scale9->setContentSize(Size(100.0f, button_scale9->getVirtualRendererSize().height)); layout->addChild(button_scale9); - + LinearLayoutParameter* lp3 = LinearLayoutParameter::create(); button_scale9->setLayoutParameter(lp3); lp3->setGravity(LinearLayoutParameter::LinearGravity::CENTER_VERTICAL); lp3->setMargin(Margin(0.0f, 10.0f, 0.0f, 10.0f)); - - + + return true; } - + return false; } @@ -480,17 +480,17 @@ bool UILayoutTest_Layout_Relative_Align_Parent::init() if (UIScene::init()) { Size widgetSize = _widget->getContentSize(); - + // Add the alert Text* alert = Text::create("Layout Relative Align Parent", "fonts/Marker Felt.ttf", 20); alert->setColor(Color3B(159, 168, 176)); alert->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 4.5f)); _uiLayer->addChild(alert); - + Layout* root = static_cast(_uiLayer->getChildByTag(81)); - + Layout* background = dynamic_cast(root->getChildByName("background_Panel")); - + // Create the layout Layout* layout = Layout::create(); layout->setLayoutType(LayoutType::RELATIVE); @@ -499,104 +499,104 @@ bool UILayoutTest_Layout_Relative_Align_Parent::init() layout->setBackGroundColor(Color3B::GREEN); Size backgroundSize = background->getContentSize(); layout->setPosition(Vec2((widgetSize.width - backgroundSize.width) / 2.0f + - (backgroundSize.width - layout->getContentSize().width) / 2.0f, - (widgetSize.height - backgroundSize.height) / 2.0f + - (backgroundSize.height - layout->getContentSize().height) / 2.0f)); + (backgroundSize.width - layout->getContentSize().width) / 2.0f, + (widgetSize.height - backgroundSize.height) / 2.0f + + (backgroundSize.height - layout->getContentSize().height) / 2.0f)); _uiLayer->addChild(layout); - + // top left Button* button_TopLeft = Button::create("cocosui/animationbuttonnormal.png", - "cocosui/animationbuttonpressed.png"); + "cocosui/animationbuttonpressed.png"); layout->addChild(button_TopLeft); - + RelativeLayoutParameter* rp_TopLeft = RelativeLayoutParameter::create(); rp_TopLeft->setAlign(RelativeLayoutParameter::RelativeAlign::PARENT_TOP_LEFT); button_TopLeft->setLayoutParameter(rp_TopLeft); - - + + // top center horizontal Button* button_TopCenter = Button::create("cocosui/animationbuttonnormal.png", - "cocosui/animationbuttonpressed.png"); + "cocosui/animationbuttonpressed.png"); layout->addChild(button_TopCenter); - + RelativeLayoutParameter* rp_TopCenter = RelativeLayoutParameter::create(); rp_TopCenter->setAlign(RelativeLayoutParameter::RelativeAlign::PARENT_TOP_CENTER_HORIZONTAL); button_TopCenter->setLayoutParameter(rp_TopCenter); - - + + // top right Button* button_TopRight = Button::create("cocosui/animationbuttonnormal.png", - "cocosui/animationbuttonpressed.png"); + "cocosui/animationbuttonpressed.png"); layout->addChild(button_TopRight); - + RelativeLayoutParameter* rp_TopRight = RelativeLayoutParameter::create(); rp_TopRight->setAlign(RelativeLayoutParameter::RelativeAlign::PARENT_TOP_RIGHT); button_TopRight->setLayoutParameter(rp_TopRight); - - + + // left center Button* button_LeftCenter = Button::create("cocosui/animationbuttonnormal.png", - "cocosui/animationbuttonpressed.png"); + "cocosui/animationbuttonpressed.png"); layout->addChild(button_LeftCenter); - + RelativeLayoutParameter* rp_LeftCenter = RelativeLayoutParameter::create(); rp_LeftCenter->setAlign(RelativeLayoutParameter::RelativeAlign::PARENT_LEFT_CENTER_VERTICAL); button_LeftCenter->setLayoutParameter(rp_LeftCenter); - - + + // center Button* buttonCenter = Button::create("cocosui/animationbuttonnormal.png", - "cocosui/animationbuttonpressed.png"); + "cocosui/animationbuttonpressed.png"); layout->addChild(buttonCenter); - + RelativeLayoutParameter* rpCenter = RelativeLayoutParameter::create(); rpCenter->setAlign(RelativeLayoutParameter::RelativeAlign::CENTER_IN_PARENT); buttonCenter->setLayoutParameter(rpCenter); - - + + // right center Button* button_RightCenter = Button::create("cocosui/animationbuttonnormal.png", - "cocosui/animationbuttonpressed.png"); + "cocosui/animationbuttonpressed.png"); layout->addChild(button_RightCenter); - + RelativeLayoutParameter* rp_RightCenter = RelativeLayoutParameter::create(); rp_RightCenter->setAlign(RelativeLayoutParameter::RelativeAlign::PARENT_RIGHT_CENTER_VERTICAL); button_RightCenter->setLayoutParameter(rp_RightCenter); - - + + // left bottom Button* button_LeftBottom = Button::create("cocosui/animationbuttonnormal.png", - "cocosui/animationbuttonpressed.png"); + "cocosui/animationbuttonpressed.png"); layout->addChild(button_LeftBottom); - + RelativeLayoutParameter* rp_LeftBottom = RelativeLayoutParameter::create(); rp_LeftBottom->setAlign(RelativeLayoutParameter::RelativeAlign::PARENT_LEFT_BOTTOM); button_LeftBottom->setLayoutParameter(rp_LeftBottom); - - + + // bottom center Button* button_BottomCenter = Button::create("cocosui/animationbuttonnormal.png", - "cocosui/animationbuttonpressed.png"); + "cocosui/animationbuttonpressed.png"); layout->addChild(button_BottomCenter); - + RelativeLayoutParameter* rp_BottomCenter = RelativeLayoutParameter::create(); rp_BottomCenter->setAlign(RelativeLayoutParameter::RelativeAlign::PARENT_BOTTOM_CENTER_HORIZONTAL); button_BottomCenter->setLayoutParameter(rp_BottomCenter); - - + + // right bottom Button* button_RightBottom = Button::create("cocosui/animationbuttonnormal.png", - "cocosui/animationbuttonpressed.png"); + "cocosui/animationbuttonpressed.png"); layout->addChild(button_RightBottom); - + RelativeLayoutParameter* rp_RightBottom = RelativeLayoutParameter::create(); rp_RightBottom->setAlign(RelativeLayoutParameter::RelativeAlign::PARENT_RIGHT_BOTTOM); button_RightBottom->setLayoutParameter(rp_RightBottom); - - + + return true; } - + return false; } @@ -615,82 +615,232 @@ bool UILayoutTest_Layout_Relative_Location::init() if (UIScene::init()) { Size widgetSize = _widget->getContentSize(); - + // Add the alert Text* alert = Text::create("Layout Relative Location", "fonts/Marker Felt.ttf", 20); alert->setColor(Color3B(159, 168, 176)); alert->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 4.5f)); _uiLayer->addChild(alert); - + Layout* root = static_cast(_uiLayer->getChildByTag(81)); - + Layout* background = dynamic_cast(root->getChildByName("background_Panel")); - + // Create the layout Layout* layout = Layout::create(); layout->setLayoutType(LayoutType::RELATIVE); layout->setContentSize(Size(280, 150)); Size backgroundSize = background->getContentSize(); layout->setPosition(Vec2((widgetSize.width - backgroundSize.width) / 2.0f + - (backgroundSize.width - layout->getContentSize().width) / 2.0f, - (widgetSize.height - backgroundSize.height) / 2.0f + - (backgroundSize.height - layout->getContentSize().height) / 2.0f)); + (backgroundSize.width - layout->getContentSize().width) / 2.0f, + (widgetSize.height - backgroundSize.height) / 2.0f + + (backgroundSize.height - layout->getContentSize().height) / 2.0f)); _uiLayer->addChild(layout); - + // center ImageView* imageView_Center = ImageView::create("cocosui/scrollviewbg.png"); layout->addChild(imageView_Center); - + RelativeLayoutParameter* rp_Center = RelativeLayoutParameter::create(); rp_Center->setRelativeName("rp_Center"); rp_Center->setAlign(RelativeLayoutParameter::RelativeAlign::CENTER_IN_PARENT); imageView_Center->setLayoutParameter(rp_Center); - - + + // above center ImageView* imageView_AboveCenter = ImageView::create("cocosui/switch-mask.png"); layout->addChild(imageView_AboveCenter); - + RelativeLayoutParameter* rp_AboveCenter = RelativeLayoutParameter::create(); rp_AboveCenter->setRelativeToWidgetName("rp_Center"); rp_AboveCenter->setAlign(RelativeLayoutParameter::RelativeAlign::LOCATION_ABOVE_CENTER); imageView_AboveCenter->setLayoutParameter(rp_AboveCenter); - - + + // below center ImageView* imageView_BelowCenter = ImageView::create("cocosui/switch-mask.png"); layout->addChild(imageView_BelowCenter); - + RelativeLayoutParameter* rp_BelowCenter = RelativeLayoutParameter::create(); rp_BelowCenter->setRelativeToWidgetName("rp_Center"); rp_BelowCenter->setAlign(RelativeLayoutParameter::RelativeAlign::LOCATION_BELOW_CENTER); imageView_BelowCenter->setLayoutParameter(rp_BelowCenter); - - + + // left center ImageView* imageView_LeftCenter = ImageView::create("cocosui/switch-mask.png"); layout->addChild(imageView_LeftCenter); - + RelativeLayoutParameter* rp_LeftCenter = RelativeLayoutParameter::create(); rp_LeftCenter->setRelativeToWidgetName("rp_Center"); rp_LeftCenter->setAlign(RelativeLayoutParameter::RelativeAlign::LOCATION_LEFT_OF_CENTER); imageView_LeftCenter->setLayoutParameter(rp_LeftCenter); - - - + + + // right center ImageView* imageView_RightCenter = ImageView::create("cocosui/switch-mask.png"); layout->addChild(imageView_RightCenter); - + RelativeLayoutParameter* rp_RightCenter = RelativeLayoutParameter::create(); rp_RightCenter->setRelativeToWidgetName("rp_Center"); rp_RightCenter->setAlign(RelativeLayoutParameter::RelativeAlign::LOCATION_RIGHT_OF_CENTER); imageView_RightCenter->setLayoutParameter(rp_RightCenter); - - + + + return true; + } + + return false; +} + +bool UILayoutComponentTest::init() +{ + if (UIScene::init()) + { + Size widgetSize = _widget->getContentSize(); + + _baseLayer = LayerColor::create(); + _baseLayer->setColor(Color3B(50, 100, 0)); + _baseLayer->setOpacity(100); + _baseLayer->setContentSize(Size(200, 200)); + _uiLayer->addChild(_baseLayer); + + Button* button = Button::create("cocosui/animationbuttonnormal.png"); + CCLOG("content size should be greater than 0: width = %f, height = %f", button->getContentSize().width, + button->getContentSize().height); + button->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f)); + button->addTouchEventListener(CC_CALLBACK_2(UILayoutComponentTest::touchEvent, this)); + button->setZoomScale(0.4f); + button->setPressedActionEnabled(true); + _uiLayer->addChild(button); + + return true; + } + return false; +} + +void UILayoutComponentTest::touchEvent(Ref *pSender, Widget::TouchEventType type) +{ + switch (type) + { + case Widget::TouchEventType::BEGAN: + break; + + case Widget::TouchEventType::MOVED: + break; + + case Widget::TouchEventType::ENDED: + { + Size widgetSize = _widget->getContentSize(); + Size layerSize = _baseLayer->getContentSize(); + if (layerSize.width == widgetSize.width && layerSize.height == widgetSize.height) + _baseLayer->setContentSize(Size(200, 200)); + else + _baseLayer->setContentSize(widgetSize); + ui:Helper::doLayout(_baseLayer); + } + break; + + case Widget::TouchEventType::CANCELED: + break; + + default: + break; + } +} + +bool UILayoutComponent_Berth_Test::init() +{ + if (UILayoutComponentTest::init()) + { + Sprite* leftTopSprite = Sprite::create("cocosui/CloseSelected.png"); + LayoutComponent* leftTop = LayoutComponent::boundingLayoutForNode(leftTopSprite); + leftTop->setHorizontalEage(LayoutComponent::HorizontalEage::Left); + leftTop->setVerticalEage(LayoutComponent::VerticalEage::Top); + _baseLayer->addChild(leftTopSprite); + + Sprite* leftBottomSprite = Sprite::create("cocosui/CloseSelected.png"); + LayoutComponent* leftBottom = LayoutComponent::boundingLayoutForNode(leftBottomSprite); + leftBottom->setHorizontalEage(LayoutComponent::HorizontalEage::Left); + leftBottom->setVerticalEage(LayoutComponent::VerticalEage::Bottom); + _baseLayer->addChild(leftBottomSprite); + + Sprite* rightTopSprite = Sprite::create("cocosui/CloseSelected.png"); + LayoutComponent* rightTop = LayoutComponent::boundingLayoutForNode(rightTopSprite); + rightTop->setHorizontalEage(LayoutComponent::HorizontalEage::Right); + rightTop->setVerticalEage(LayoutComponent::VerticalEage::Top); + _baseLayer->addChild(rightTopSprite); + + Sprite* rightBottomSprite = Sprite::create("cocosui/CloseSelected.png"); + LayoutComponent* rightBottom = LayoutComponent::boundingLayoutForNode(rightBottomSprite); + rightBottom->setHorizontalEage(LayoutComponent::HorizontalEage::Right); + rightBottom->setVerticalEage(LayoutComponent::VerticalEage::Bottom); + _baseLayer->addChild(rightBottomSprite); + + ui::Helper::doLayout(_baseLayer); + + return true; + } + return false; +} + +bool UILayoutComponent_Berth_Stretch_Test::init() +{ + if (UILayoutComponentTest::init()) + { + ImageView* leftTopSprite = ImageView::create("cocosui/CloseSelected.png"); + leftTopSprite->ignoreContentAdaptWithSize(false); + LayoutComponent* leftTop = LayoutComponent::boundingLayoutForNode(leftTopSprite); + leftTop->setHorizontalEage(LayoutComponent::HorizontalEage::Left); + leftTop->setVerticalEage(LayoutComponent::VerticalEage::Top); + leftTop->setStretchWidthEnabled(true); + leftTop->setStretchHeightEnabled(true); + _baseLayer->addChild(leftTopSprite); + leftTop->setSize(leftTopSprite->getContentSize()); + leftTop->setLeftMargin(0); + leftTop->setTopMargin(0); + + ImageView* leftBottomSprite = ImageView::create("cocosui/CloseSelected.png"); + leftBottomSprite->ignoreContentAdaptWithSize(false); + LayoutComponent* leftBottom = LayoutComponent::boundingLayoutForNode(leftBottomSprite); + leftBottom->setHorizontalEage(LayoutComponent::HorizontalEage::Left); + leftBottom->setVerticalEage(LayoutComponent::VerticalEage::Bottom); + leftBottom->setStretchWidthEnabled(true); + leftBottom->setStretchHeightEnabled(true); + _baseLayer->addChild(leftBottomSprite); + leftBottom->setSize(leftBottomSprite->getContentSize()); + leftBottom->setLeftMargin(0); + leftBottom->setButtomMargin(0); + + ImageView* rightTopSprite = ImageView::create("cocosui/CloseSelected.png"); + rightTopSprite->ignoreContentAdaptWithSize(false); + LayoutComponent* rightTop = LayoutComponent::boundingLayoutForNode(rightTopSprite); + rightTop->setHorizontalEage(LayoutComponent::HorizontalEage::Right); + rightTop->setVerticalEage(LayoutComponent::VerticalEage::Top); + rightTop->setStretchWidthEnabled(true); + rightTop->setStretchHeightEnabled(true); + _baseLayer->addChild(rightTopSprite); + rightTop->setSize(rightTopSprite->getContentSize()); + rightTop->setTopMargin(0); + rightTop->setRightMargin(0); + + ImageView* rightBottomSprite = ImageView::create("cocosui/CloseSelected.png"); + rightBottomSprite->ignoreContentAdaptWithSize(false); + LayoutComponent* rightBottom = LayoutComponent::boundingLayoutForNode(rightBottomSprite); + rightBottom->setHorizontalEage(LayoutComponent::HorizontalEage::Right); + rightBottom->setVerticalEage(LayoutComponent::VerticalEage::Bottom); + rightBottom->setStretchWidthEnabled(true); + rightBottom->setStretchHeightEnabled(true); + _baseLayer->addChild(rightBottomSprite); + rightBottom->setSize(rightBottomSprite->getContentSize()); + rightBottom->setButtomMargin(0); + rightBottom->setRightMargin(0); + + ui::Helper::doLayout(_baseLayer); + return true; } - return false; } diff --git a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UILayoutTest/UILayoutTest.h b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UILayoutTest/UILayoutTest.h index 811192d55a..c81f02a322 100644 --- a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UILayoutTest/UILayoutTest.h +++ b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UILayoutTest/UILayoutTest.h @@ -126,6 +126,32 @@ protected: UI_SCENE_CREATE_FUNC(UILayoutTest_Layout_Relative_Location) }; +class UILayoutComponentTest : public UIScene +{ +public: + virtual bool init(); + void touchEvent(Ref *pSender, Widget::TouchEventType type); +protected: + LayerColor* _baseLayer; + UI_SCENE_CREATE_FUNC(UILayoutComponentTest) +}; + +class UILayoutComponent_Berth_Test : public UILayoutComponentTest +{ +public: + virtual bool init() override; +protected: + UI_SCENE_CREATE_FUNC(UILayoutComponent_Berth_Test) +}; + +class UILayoutComponent_Berth_Stretch_Test : public UILayoutComponentTest +{ +public: + virtual bool init() override; +protected: + UI_SCENE_CREATE_FUNC(UILayoutComponent_Berth_Stretch_Test) +}; + /* class UILayoutTest_Layout_Grid : public UIScene { diff --git a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UISceneManager.cpp b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UISceneManager.cpp index f17eae8063..871c626e9b 100644 --- a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UISceneManager.cpp +++ b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UISceneManager.cpp @@ -87,6 +87,8 @@ static const char* s_testArray[] = "UILayoutTest_Layout_Linear_Horizontal", "UILayoutTest_Layout_Relative_Align_Parent", "UILayoutTest_Layout_Relative_Location", + "UILayoutComponent_Berth_Test", + "UILayoutComponent_Berth_Stretch_Test", "UIScrollViewTest_Vertical", "UIScrollViewTest_Horizontal", @@ -317,6 +319,12 @@ Scene *UISceneManager::currentUIScene() case kUILayoutTest_Layout_Relative_Location: return UILayoutTest_Layout_Relative_Location::sceneWithTitle(s_testArray[_currentUISceneId]); + case kUILayoutComponent_Berth_Test: + return UILayoutComponent_Berth_Test::sceneWithTitle(s_testArray[_currentUISceneId]); + + case kUILayoutComponent_Berth_Stretch_Test: + return UILayoutComponent_Berth_Stretch_Test::sceneWithTitle(s_testArray[_currentUISceneId]); + case kUIScrollViewTest_Vertical: return UIScrollViewTest_Vertical::sceneWithTitle(s_testArray[_currentUISceneId]); diff --git a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UISceneManager.h b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UISceneManager.h index 2f6ebd1b9d..7b7dd1edd8 100644 --- a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UISceneManager.h +++ b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UISceneManager.h @@ -80,6 +80,8 @@ enum kUILayoutTest_Layout_Linear_Horizontal, kUILayoutTest_Layout_Relative_Align_Parent, kUILayoutTest_Layout_Relative_Location, + kUILayoutComponent_Berth_Test, + kUILayoutComponent_Berth_Stretch_Test, kUIScrollViewTest_Vertical, kUIScrollViewTest_Horizontal, kUIScrollViewTest_Both, From 5a73ea93135adec0d3ae1e0650719c8d9e603c70 Mon Sep 17 00:00:00 2001 From: Liam Date: Mon, 22 Dec 2014 15:04:25 +0800 Subject: [PATCH 03/13] reset widget chaned --- cocos/ui/UIWidget.cpp | 128 ++++++++++++++++++++++++++++-------------- cocos/ui/UIWidget.h | 2 +- 2 files changed, 87 insertions(+), 43 deletions(-) diff --git a/cocos/ui/UIWidget.cpp b/cocos/ui/UIWidget.cpp index eb4f996e14..3f0a02492a 100644 --- a/cocos/ui/UIWidget.cpp +++ b/cocos/ui/UIWidget.cpp @@ -222,6 +222,7 @@ bool Widget::init() void Widget::onEnter() { + updateSizeAndPosition(); ProtectedNode::onEnter(); } @@ -280,7 +281,30 @@ 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(); } @@ -291,27 +315,29 @@ void Widget::setSize(const Size &size) void Widget::setSizePercent(const Vec2 &percent) { - 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) + _sizePercent = percent; + Size cSize = _customSize; + if (_running) { - component->setUsingPercentContentSize(true); + 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()); } else { - component->setUsingPercentContentSize(false); + this->setContentSize(cSize); } + _customSize = cSize; } void Widget::updateSizeAndPosition() @@ -393,6 +419,11 @@ void Widget::updateSizeAndPosition(const cocos2d::Size &parentSize) setPosition(absPos); } +void Widget::setSizeType(SizeType type) +{ + _sizeType = type; +} + Widget::SizeType Widget::getSizeType() const { return _sizeType; @@ -438,9 +469,7 @@ const Size& Widget::getCustomSize() const const Vec2& Widget::getSizePercent() { - auto component = this->getOrCreateLayoutComponent(); - - return component->getPercentContentSize(); + return _sizePercent; } Vec2 Widget::getWorldPosition()const @@ -455,6 +484,14 @@ Node* Widget::getVirtualRenderer() void Widget::onSizeChanged() { + for (auto& child : getChildren()) + { + Widget* widgetChild = dynamic_cast(child); + if (widgetChild) + { + widgetChild->updateSizeAndPosition(); + } + } } Size Widget::getVirtualRendererSize() const @@ -911,40 +948,47 @@ 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) { - auto component = this->getOrCreateLayoutComponent(); - component->setPositionPercentX(percent.x); - component->setPositionPercentY(percent.y); - component->refreshLayout(); + _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); + } + } } -Vec2 Widget::getPositionPercent(){ - - auto component = this->getOrCreateLayoutComponent(); - float percentX = component->getPositionPercentX(); - float percentY = component->getPositionPercentY(); - - return Vec2(percentX,percentY); +const Vec2& Widget::getPositionPercent()const{ + return _positionPercent; } 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 b82a7f6104..3d4e11e60f 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 */ - Vec2 getPositionPercent(); + const Vec2& getPositionPercent()const; /** * Changes the position type of the widget From 8b32193e04626018bb63b430ff5283458bdb51cf Mon Sep 17 00:00:00 2001 From: Liam Date: Mon, 22 Dec 2014 15:15:40 +0800 Subject: [PATCH 04/13] reset unused format changed --- .../UILayoutTest/UILayoutTest.cpp | 388 +++++++++--------- 1 file changed, 194 insertions(+), 194 deletions(-) diff --git a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UILayoutTest/UILayoutTest.cpp b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UILayoutTest/UILayoutTest.cpp index 1fe715a664..5e6913f1c2 100644 --- a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UILayoutTest/UILayoutTest.cpp +++ b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UILayoutTest/UILayoutTest.cpp @@ -18,50 +18,50 @@ bool UILayoutTest::init() if (UIScene::init()) { Size widgetSize = _widget->getContentSize(); - + // Add the alert - Text* alert = Text::create("Layout", "fonts/Marker Felt.ttf", 30); + Text* alert = Text::create("Layout", "fonts/Marker Felt.ttf", 30 ); alert->setColor(Color3B(159, 168, 176)); alert->setPosition(Vec2(widgetSize.width / 2.0f, - widgetSize.height / 2.0f - alert->getContentSize().height * 3.075f)); - + widgetSize.height / 2.0f - alert->getContentSize().height * 3.075f)); + _uiLayer->addChild(alert); - - Layout* root = static_cast(_uiLayer->getChildByTag(81)); - + + Layout* root = static_cast(_uiLayer->getChildByTag(81)) ; + Layout* background = static_cast(root->getChildByName("background_Panel")); - + // Create the layout Layout* layout = Layout::create(); layout->setContentSize(Size(280, 150)); Size backgroundSize = background->getContentSize(); layout->setPosition(Vec2((widgetSize.width - backgroundSize.width) / 2.0f + - (backgroundSize.width - layout->getContentSize().width) / 2.0f, - (widgetSize.height - backgroundSize.height) / 2.0f + - (backgroundSize.height - layout->getContentSize().height) / 2.0f)); + (backgroundSize.width - layout->getContentSize().width) / 2.0f, + (widgetSize.height - backgroundSize.height) / 2.0f + + (backgroundSize.height - layout->getContentSize().height) / 2.0f)); _uiLayer->addChild(layout); - + Button* button = Button::create("cocosui/animationbuttonnormal.png", "cocosui/animationbuttonpressed.png"); button->setPosition(Vec2(button->getContentSize().width / 2.0f, - layout->getContentSize().height - button->getContentSize().height / 2.0f)); + layout->getContentSize().height - button->getContentSize().height / 2.0f)); layout->addChild(button); - + Button* titleButton = Button::create("cocosui/backtotopnormal.png", "cocosui/backtotoppressed.png"); titleButton->setTitleText("Title Button"); titleButton->setPosition(Vec2(layout->getContentSize().width / 2.0f, layout->getContentSize().height / 2.0f)); layout->addChild(titleButton); - + Button* button_scale9 = Button::create("cocosui/button.png", "cocosui/buttonHighlighted.png"); button_scale9->setScale9Enabled(true); button_scale9->setContentSize(Size(100.0f, button_scale9->getVirtualRendererSize().height)); button_scale9->setPosition(Vec2(layout->getContentSize().width - button_scale9->getContentSize().width / 2.0f, - button_scale9->getContentSize().height / 2.0f)); - + button_scale9->getContentSize().height / 2.0f)); + layout->addChild(button_scale9); - + return true; } - + return false; } @@ -80,19 +80,19 @@ bool UILayoutTest_Color::init() if (UIScene::init()) { Size widgetSize = _widget->getContentSize(); - + // Add the alert Text* alert = Text::create("Layout color render", "fonts/Marker Felt.ttf", 30); alert->setColor(Color3B(159, 168, 176)); alert->setPosition(Vec2(widgetSize.width / 2.0f, - widgetSize.height / 2.0f - alert->getContentSize().height * 3.075f)); - + widgetSize.height / 2.0f - alert->getContentSize().height * 3.075f)); + _uiLayer->addChild(alert); - + Layout* root = static_cast(_uiLayer->getChildByTag(81)); - + Layout* background = static_cast(root->getChildByName("background_Panel")); - + // Create the layout with color render Layout* layout = Layout::create(); layout->setBackGroundColorType(Layout::BackGroundColorType::SOLID); @@ -100,30 +100,30 @@ bool UILayoutTest_Color::init() layout->setContentSize(Size(280, 150)); Size backgroundSize = background->getContentSize(); layout->setPosition(Vec2((widgetSize.width - backgroundSize.width) / 2.0f + - (backgroundSize.width - layout->getContentSize().width) / 2.0f, - (widgetSize.height - backgroundSize.height) / 2.0f + - (backgroundSize.height - layout->getContentSize().height) / 2.0f)); + (backgroundSize.width - layout->getContentSize().width) / 2.0f, + (widgetSize.height - backgroundSize.height) / 2.0f + + (backgroundSize.height - layout->getContentSize().height) / 2.0f)); _uiLayer->addChild(layout); - + Button* button = Button::create("cocosui/animationbuttonnormal.png", "cocosui/animationbuttonpressed.png"); button->setPosition(Vec2(button->getContentSize().width / 2.0f, - layout->getContentSize().height - button->getContentSize().height / 2.0f)); - + layout->getContentSize().height - button->getContentSize().height / 2.0f)); + layout->addChild(button); - + Button* titleButton = Button::create("cocosui/backtotopnormal.png", "cocosui/backtotoppressed.png"); titleButton->setTitleText("Title Button"); titleButton->setPosition(Vec2(layout->getContentSize().width / 2.0f, layout->getContentSize().height / 2.0f)); layout->addChild(titleButton); - + Button* button_scale9 = Button::create("cocosui/button.png", "cocosui/buttonHighlighted.png"); button_scale9->setScale9Enabled(true); button_scale9->setContentSize(Size(100.0f, button_scale9->getVirtualRendererSize().height)); button_scale9->setPosition(Vec2(layout->getContentSize().width - button_scale9->getContentSize().width / 2.0f, - button_scale9->getContentSize().height / 2.0f)); - - layout->addChild(button_scale9); - + button_scale9->getContentSize().height / 2.0f)); + + layout->addChild(button_scale9); + return true; } return false; @@ -132,7 +132,7 @@ bool UILayoutTest_Color::init() // UILayoutTest_Gradient UILayoutTest_Gradient::UILayoutTest_Gradient() -{ +{ } UILayoutTest_Gradient::~UILayoutTest_Gradient() @@ -144,19 +144,19 @@ bool UILayoutTest_Gradient::init() if (UIScene::init()) { Size widgetSize = _widget->getContentSize(); - + // Add the alert Text* alert = Text::create("Layout gradient render", "fonts/Marker Felt.ttf", 30); alert->setColor(Color3B(159, 168, 176)); alert->setPosition(Vec2(widgetSize.width / 2.0f, - widgetSize.height / 2.0f - alert->getContentSize().height * 3.075f)); - + widgetSize.height / 2.0f - alert->getContentSize().height * 3.075f)); + _uiLayer->addChild(alert); - + Layout* root = static_cast(_uiLayer->getChildByTag(81)); - + Layout* background = static_cast(root->getChildByName("background_Panel")); - + // Create the layout with gradient render Layout* layout = Layout::create(); layout->setBackGroundColorType(Layout::BackGroundColorType::GRADIENT); @@ -164,30 +164,30 @@ bool UILayoutTest_Gradient::init() layout->setContentSize(Size(280, 150)); Size backgroundSize = background->getContentSize(); layout->setPosition(Vec2((widgetSize.width - backgroundSize.width) / 2.0f + - (backgroundSize.width - layout->getContentSize().width) / 2.0f, - (widgetSize.height - backgroundSize.height) / 2.0f + - (backgroundSize.height - layout->getContentSize().height) / 2.0f)); + (backgroundSize.width - layout->getContentSize().width) / 2.0f, + (widgetSize.height - backgroundSize.height) / 2.0f + + (backgroundSize.height - layout->getContentSize().height) / 2.0f)); _uiLayer->addChild(layout); - + Button* button = Button::create("cocosui/animationbuttonnormal.png", "cocosui/animationbuttonpressed.png"); button->setPosition(Vec2(button->getContentSize().width / 2.0f, - layout->getContentSize().height - button->getContentSize().height / 2.0f)); - + layout->getContentSize().height - button->getContentSize().height / 2.0f)); + layout->addChild(button); - + Button* titleButton = Button::create("cocosui/backtotopnormal.png", "cocosui/backtotoppressed.png"); titleButton->setTitleText("Title Button"); titleButton->setPosition(Vec2(layout->getContentSize().width / 2.0f, layout->getContentSize().height / 2.0f)); layout->addChild(titleButton); - + Button* button_scale9 = Button::create("cocosui/button.png", "cocosui/buttonHighlighted.png"); button_scale9->setScale9Enabled(true); button_scale9->setContentSize(Size(100.0f, button_scale9->getVirtualRendererSize().height)); button_scale9->setPosition(Vec2(layout->getContentSize().width - button_scale9->getContentSize().width / 2.0f, - button_scale9->getContentSize().height / 2.0f)); - - layout->addChild(button_scale9); - + button_scale9->getContentSize().height / 2.0f)); + + layout->addChild(button_scale9); + return true; } return false; @@ -208,17 +208,17 @@ bool UILayoutTest_BackGroundImage::init() if (UIScene::init()) { Size widgetSize = _widget->getContentSize(); - + // Add the alert Text* alert = Text::create("Layout background image", "fonts/Marker Felt.ttf", 20); alert->setColor(Color3B(159, 168, 176)); alert->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 4.5f)); _uiLayer->addChild(alert); - + Layout* root = static_cast(_uiLayer->getChildByTag(81)); - + Layout* background = dynamic_cast(root->getChildByName("background_Panel")); - + // Create the layout with background image Layout* layout = Layout::create(); layout->setClippingEnabled(true); @@ -226,29 +226,29 @@ bool UILayoutTest_BackGroundImage::init() layout->setContentSize(Size(280, 150)); Size backgroundSize = background->getContentSize(); layout->setPosition(Vec2((widgetSize.width - backgroundSize.width) / 2.0f + - (backgroundSize.width - layout->getContentSize().width) / 2.0f, - (widgetSize.height - backgroundSize.height) / 2.0f + - (backgroundSize.height - layout->getContentSize().height) / 2.0f)); + (backgroundSize.width - layout->getContentSize().width) / 2.0f, + (widgetSize.height - backgroundSize.height) / 2.0f + + (backgroundSize.height - layout->getContentSize().height) / 2.0f)); _uiLayer->addChild(layout); - + Button* button = Button::create("cocosui/animationbuttonnormal.png", "cocosui/animationbuttonpressed.png"); button->setPosition(Vec2(button->getContentSize().width / 2.0f, - layout->getContentSize().height - button->getContentSize().height / 2.0f)); + layout->getContentSize().height - button->getContentSize().height / 2.0f)); layout->addChild(button); - + Button* titleButton = Button::create("cocosui/backtotopnormal.png", "cocosui/backtotoppressed.png"); titleButton->setTitleText("Title Button"); titleButton->setPosition(Vec2(layout->getContentSize().width / 2.0f, layout->getContentSize().height / 2.0f)); layout->addChild(titleButton); - + Button* button_scale9 = Button::create("cocosui/button.png", "cocosui/buttonHighlighted.png"); button_scale9->setScale9Enabled(true); button_scale9->setContentSize(Size(100.0f, button_scale9->getVirtualRendererSize().height)); button_scale9->setPosition(Vec2(layout->getContentSize().width - button_scale9->getContentSize().width / 2.0f, - button_scale9->getContentSize().height / 2.0f)); - - layout->addChild(button_scale9); - + button_scale9->getContentSize().height / 2.0f)); + + layout->addChild(button_scale9); + return true; } return false; @@ -269,17 +269,17 @@ bool UILayoutTest_BackGroundImage_Scale9::init() if (UIScene::init()) { Size widgetSize = _widget->getContentSize(); - + // Add the alert Text* alert = Text::create("Layout background image scale9", "fonts/Marker Felt.ttf", 20); alert->setColor(Color3B(159, 168, 176)); alert->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 4.5f)); _uiLayer->addChild(alert); - + Layout* root = static_cast(_uiLayer->getChildByTag(81)); - + Layout* background = dynamic_cast(root->getChildByName("background_Panel")); - + // Create the layout with background image Layout* layout = Layout::create(); layout->setBackGroundImageScale9Enabled(true); @@ -287,29 +287,29 @@ bool UILayoutTest_BackGroundImage_Scale9::init() layout->setContentSize(Size(280, 150)); Size backgroundSize = background->getContentSize(); layout->setPosition(Vec2((widgetSize.width - backgroundSize.width) / 2.0f + - (backgroundSize.width - layout->getContentSize().width) / 2.0f, - (widgetSize.height - backgroundSize.height) / 2.0f + - (backgroundSize.height - layout->getContentSize().height) / 2.0f)); + (backgroundSize.width - layout->getContentSize().width) / 2.0f, + (widgetSize.height - backgroundSize.height) / 2.0f + + (backgroundSize.height - layout->getContentSize().height) / 2.0f)); _uiLayer->addChild(layout); - + Button* button = Button::create("cocosui/animationbuttonnormal.png", "cocosui/animationbuttonpressed.png"); button->setPosition(Vec2(button->getContentSize().width / 2.0f, - layout->getContentSize().height - button->getContentSize().height / 2.0f)); - + layout->getContentSize().height - button->getContentSize().height / 2.0f)); + layout->addChild(button); - + Button* titleButton = Button::create("cocosui/backtotopnormal.png", "cocosui/backtotoppressed.png"); titleButton->setTitleText("Title Button"); titleButton->setPosition(Vec2(layout->getContentSize().width / 2.0f, layout->getContentSize().height / 2.0f)); layout->addChild(titleButton); - + Button* button_scale9 = Button::create("cocosui/button.png", "cocosui/buttonHighlighted.png"); button_scale9->setScale9Enabled(true); button_scale9->setContentSize(Size(100.0f, button_scale9->getVirtualRendererSize().height)); button_scale9->setPosition(Vec2(layout->getContentSize().width - button_scale9->getContentSize().width / 2.0f, - button_scale9->getContentSize().height / 2.0f)); + button_scale9->getContentSize().height / 2.0f)); layout->addChild(button_scale9); - + return true; } return false; @@ -330,64 +330,64 @@ bool UILayoutTest_Layout_Linear_Vertical::init() if (UIScene::init()) { Size widgetSize = _widget->getContentSize(); - + // Add the alert Text* alert = Text::create("Layout Linear Vertical", "fonts/Marker Felt.ttf", 20); alert->setColor(Color3B(159, 168, 176)); alert->setPosition(Vec2(widgetSize.width / 2.0f, - widgetSize.height / 2.0f - alert->getContentSize().height * 4.5f)); - + widgetSize.height / 2.0f - alert->getContentSize().height * 4.5f)); + _uiLayer->addChild(alert); - + Layout* root = static_cast(_uiLayer->getChildByTag(81)); - + Layout* background = static_cast(root->getChildByName("background_Panel")); - + // Create the layout Layout* layout = Layout::create(); layout->setLayoutType(LayoutType::VERTICAL); layout->setContentSize(Size(280, 150)); Size backgroundSize = background->getContentSize(); layout->setPosition(Vec2((widgetSize.width - backgroundSize.width) / 2.0f + - (backgroundSize.width - layout->getContentSize().width) / 2.0f, - (widgetSize.height - backgroundSize.height) / 2.0f + - (backgroundSize.height - layout->getContentSize().height) / 2.0f)); + (backgroundSize.width - layout->getContentSize().width) / 2.0f, + (widgetSize.height - backgroundSize.height) / 2.0f + + (backgroundSize.height - layout->getContentSize().height) / 2.0f)); _uiLayer->addChild(layout); - - + + Button* button = Button::create("cocosui/animationbuttonnormal.png", "cocosui/animationbuttonpressed.png"); layout->addChild(button); - + LinearLayoutParameter* lp1 = LinearLayoutParameter::create(); button->setLayoutParameter(lp1); lp1->setGravity(LinearLayoutParameter::LinearGravity::CENTER_HORIZONTAL); lp1->setMargin(Margin(0.0f, 5.0f, 0.0f, 10.0f)); - - + + Button* titleButton = Button::create("cocosui/backtotopnormal.png", "cocosui/backtotoppressed.png"); titleButton->setTitleText("Title Button"); layout->addChild(titleButton); - + LinearLayoutParameter* lp2 = LinearLayoutParameter::create(); titleButton->setLayoutParameter(lp2); lp2->setGravity(LinearLayoutParameter::LinearGravity::CENTER_HORIZONTAL); lp2->setMargin(Margin(0.0f, 10.0f, 0.0f, 10.0f)); - - + + Button* button_scale9 = Button::create("cocosui/button.png", "cocosui/buttonHighlighted.png"); button_scale9->setScale9Enabled(true); button_scale9->setContentSize(Size(100.0f, button_scale9->getVirtualRendererSize().height)); layout->addChild(button_scale9); - + LinearLayoutParameter* lp3 = LinearLayoutParameter::create(); button_scale9->setLayoutParameter(lp3); lp3->setGravity(LinearLayoutParameter::LinearGravity::CENTER_HORIZONTAL); lp3->setMargin(Margin(0.0f, 10.0f, 0.0f, 10.0f)); - - + + return true; } - + return false; } @@ -406,17 +406,17 @@ bool UILayoutTest_Layout_Linear_Horizontal::init() if (UIScene::init()) { Size widgetSize = _widget->getContentSize(); - + // Add the alert Text* alert = Text::create("Layout Linear Horizontal", "fonts/Marker Felt.ttf", 20); alert->setColor(Color3B(159, 168, 176)); alert->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 4.5f)); _uiLayer->addChild(alert); - + Layout* root = static_cast(_uiLayer->getChildByTag(81)); - + Layout* background = dynamic_cast(root->getChildByName("background_Panel")); - + // Create the layout Layout* layout = Layout::create(); layout->setLayoutType(LayoutType::HORIZONTAL); @@ -424,44 +424,44 @@ bool UILayoutTest_Layout_Linear_Horizontal::init() layout->setContentSize(Size(280, 150)); Size backgroundSize = background->getContentSize(); layout->setPosition(Vec2((widgetSize.width - backgroundSize.width) / 2.0f + - (backgroundSize.width - layout->getContentSize().width) / 2.0f, - (widgetSize.height - backgroundSize.height) / 2.0f + - (backgroundSize.height - layout->getContentSize().height) / 2.0f)); + (backgroundSize.width - layout->getContentSize().width) / 2.0f, + (widgetSize.height - backgroundSize.height) / 2.0f + + (backgroundSize.height - layout->getContentSize().height) / 2.0f)); _uiLayer->addChild(layout); - + Button* button = Button::create("cocosui/animationbuttonnormal.png", "cocosui/animationbuttonpressed.png"); layout->addChild(button); - + LinearLayoutParameter* lp1 = LinearLayoutParameter::create(); button->setLayoutParameter(lp1); lp1->setGravity(LinearLayoutParameter::LinearGravity::CENTER_VERTICAL); lp1->setMargin(Margin(0.0f, 10.0f, 0.0f, 10.0f)); - - + + Button* titleButton = Button::create("cocosui/backtotopnormal.png", "cocosui/backtotoppressed.png"); titleButton->setTitleText("Title Button"); layout->addChild(titleButton); - + LinearLayoutParameter* lp2 = LinearLayoutParameter::create(); titleButton->setLayoutParameter(lp2); lp2->setGravity(LinearLayoutParameter::LinearGravity::CENTER_VERTICAL); lp2->setMargin(Margin(0.0f, 10.0f, 0.0f, 10.0f)); - - + + Button* button_scale9 = Button::create("cocosui/button.png", "cocosui/buttonHighlighted.png"); button_scale9->setScale9Enabled(true); button_scale9->setContentSize(Size(100.0f, button_scale9->getVirtualRendererSize().height)); layout->addChild(button_scale9); - + LinearLayoutParameter* lp3 = LinearLayoutParameter::create(); button_scale9->setLayoutParameter(lp3); lp3->setGravity(LinearLayoutParameter::LinearGravity::CENTER_VERTICAL); lp3->setMargin(Margin(0.0f, 10.0f, 0.0f, 10.0f)); - - + + return true; } - + return false; } @@ -480,17 +480,17 @@ bool UILayoutTest_Layout_Relative_Align_Parent::init() if (UIScene::init()) { Size widgetSize = _widget->getContentSize(); - + // Add the alert Text* alert = Text::create("Layout Relative Align Parent", "fonts/Marker Felt.ttf", 20); alert->setColor(Color3B(159, 168, 176)); alert->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 4.5f)); _uiLayer->addChild(alert); - + Layout* root = static_cast(_uiLayer->getChildByTag(81)); - + Layout* background = dynamic_cast(root->getChildByName("background_Panel")); - + // Create the layout Layout* layout = Layout::create(); layout->setLayoutType(LayoutType::RELATIVE); @@ -499,104 +499,104 @@ bool UILayoutTest_Layout_Relative_Align_Parent::init() layout->setBackGroundColor(Color3B::GREEN); Size backgroundSize = background->getContentSize(); layout->setPosition(Vec2((widgetSize.width - backgroundSize.width) / 2.0f + - (backgroundSize.width - layout->getContentSize().width) / 2.0f, - (widgetSize.height - backgroundSize.height) / 2.0f + - (backgroundSize.height - layout->getContentSize().height) / 2.0f)); + (backgroundSize.width - layout->getContentSize().width) / 2.0f, + (widgetSize.height - backgroundSize.height) / 2.0f + + (backgroundSize.height - layout->getContentSize().height) / 2.0f)); _uiLayer->addChild(layout); - + // top left Button* button_TopLeft = Button::create("cocosui/animationbuttonnormal.png", - "cocosui/animationbuttonpressed.png"); + "cocosui/animationbuttonpressed.png"); layout->addChild(button_TopLeft); - + RelativeLayoutParameter* rp_TopLeft = RelativeLayoutParameter::create(); rp_TopLeft->setAlign(RelativeLayoutParameter::RelativeAlign::PARENT_TOP_LEFT); button_TopLeft->setLayoutParameter(rp_TopLeft); - - + + // top center horizontal Button* button_TopCenter = Button::create("cocosui/animationbuttonnormal.png", - "cocosui/animationbuttonpressed.png"); + "cocosui/animationbuttonpressed.png"); layout->addChild(button_TopCenter); - + RelativeLayoutParameter* rp_TopCenter = RelativeLayoutParameter::create(); rp_TopCenter->setAlign(RelativeLayoutParameter::RelativeAlign::PARENT_TOP_CENTER_HORIZONTAL); button_TopCenter->setLayoutParameter(rp_TopCenter); - - + + // top right Button* button_TopRight = Button::create("cocosui/animationbuttonnormal.png", - "cocosui/animationbuttonpressed.png"); + "cocosui/animationbuttonpressed.png"); layout->addChild(button_TopRight); - + RelativeLayoutParameter* rp_TopRight = RelativeLayoutParameter::create(); rp_TopRight->setAlign(RelativeLayoutParameter::RelativeAlign::PARENT_TOP_RIGHT); button_TopRight->setLayoutParameter(rp_TopRight); - - + + // left center Button* button_LeftCenter = Button::create("cocosui/animationbuttonnormal.png", - "cocosui/animationbuttonpressed.png"); + "cocosui/animationbuttonpressed.png"); layout->addChild(button_LeftCenter); - + RelativeLayoutParameter* rp_LeftCenter = RelativeLayoutParameter::create(); rp_LeftCenter->setAlign(RelativeLayoutParameter::RelativeAlign::PARENT_LEFT_CENTER_VERTICAL); button_LeftCenter->setLayoutParameter(rp_LeftCenter); - - + + // center Button* buttonCenter = Button::create("cocosui/animationbuttonnormal.png", - "cocosui/animationbuttonpressed.png"); + "cocosui/animationbuttonpressed.png"); layout->addChild(buttonCenter); - + RelativeLayoutParameter* rpCenter = RelativeLayoutParameter::create(); rpCenter->setAlign(RelativeLayoutParameter::RelativeAlign::CENTER_IN_PARENT); buttonCenter->setLayoutParameter(rpCenter); - - + + // right center Button* button_RightCenter = Button::create("cocosui/animationbuttonnormal.png", - "cocosui/animationbuttonpressed.png"); + "cocosui/animationbuttonpressed.png"); layout->addChild(button_RightCenter); - + RelativeLayoutParameter* rp_RightCenter = RelativeLayoutParameter::create(); rp_RightCenter->setAlign(RelativeLayoutParameter::RelativeAlign::PARENT_RIGHT_CENTER_VERTICAL); button_RightCenter->setLayoutParameter(rp_RightCenter); - - + + // left bottom Button* button_LeftBottom = Button::create("cocosui/animationbuttonnormal.png", - "cocosui/animationbuttonpressed.png"); + "cocosui/animationbuttonpressed.png"); layout->addChild(button_LeftBottom); - + RelativeLayoutParameter* rp_LeftBottom = RelativeLayoutParameter::create(); rp_LeftBottom->setAlign(RelativeLayoutParameter::RelativeAlign::PARENT_LEFT_BOTTOM); button_LeftBottom->setLayoutParameter(rp_LeftBottom); - - + + // bottom center Button* button_BottomCenter = Button::create("cocosui/animationbuttonnormal.png", - "cocosui/animationbuttonpressed.png"); + "cocosui/animationbuttonpressed.png"); layout->addChild(button_BottomCenter); - + RelativeLayoutParameter* rp_BottomCenter = RelativeLayoutParameter::create(); rp_BottomCenter->setAlign(RelativeLayoutParameter::RelativeAlign::PARENT_BOTTOM_CENTER_HORIZONTAL); button_BottomCenter->setLayoutParameter(rp_BottomCenter); - - + + // right bottom Button* button_RightBottom = Button::create("cocosui/animationbuttonnormal.png", - "cocosui/animationbuttonpressed.png"); + "cocosui/animationbuttonpressed.png"); layout->addChild(button_RightBottom); - + RelativeLayoutParameter* rp_RightBottom = RelativeLayoutParameter::create(); rp_RightBottom->setAlign(RelativeLayoutParameter::RelativeAlign::PARENT_RIGHT_BOTTOM); button_RightBottom->setLayoutParameter(rp_RightBottom); - - + + return true; } - + return false; } @@ -615,82 +615,82 @@ bool UILayoutTest_Layout_Relative_Location::init() if (UIScene::init()) { Size widgetSize = _widget->getContentSize(); - + // Add the alert Text* alert = Text::create("Layout Relative Location", "fonts/Marker Felt.ttf", 20); alert->setColor(Color3B(159, 168, 176)); alert->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 4.5f)); _uiLayer->addChild(alert); - + Layout* root = static_cast(_uiLayer->getChildByTag(81)); - + Layout* background = dynamic_cast(root->getChildByName("background_Panel")); - + // Create the layout Layout* layout = Layout::create(); layout->setLayoutType(LayoutType::RELATIVE); layout->setContentSize(Size(280, 150)); Size backgroundSize = background->getContentSize(); layout->setPosition(Vec2((widgetSize.width - backgroundSize.width) / 2.0f + - (backgroundSize.width - layout->getContentSize().width) / 2.0f, - (widgetSize.height - backgroundSize.height) / 2.0f + - (backgroundSize.height - layout->getContentSize().height) / 2.0f)); + (backgroundSize.width - layout->getContentSize().width) / 2.0f, + (widgetSize.height - backgroundSize.height) / 2.0f + + (backgroundSize.height - layout->getContentSize().height) / 2.0f)); _uiLayer->addChild(layout); - + // center ImageView* imageView_Center = ImageView::create("cocosui/scrollviewbg.png"); layout->addChild(imageView_Center); - + RelativeLayoutParameter* rp_Center = RelativeLayoutParameter::create(); rp_Center->setRelativeName("rp_Center"); rp_Center->setAlign(RelativeLayoutParameter::RelativeAlign::CENTER_IN_PARENT); imageView_Center->setLayoutParameter(rp_Center); - - + + // above center ImageView* imageView_AboveCenter = ImageView::create("cocosui/switch-mask.png"); layout->addChild(imageView_AboveCenter); - + RelativeLayoutParameter* rp_AboveCenter = RelativeLayoutParameter::create(); rp_AboveCenter->setRelativeToWidgetName("rp_Center"); rp_AboveCenter->setAlign(RelativeLayoutParameter::RelativeAlign::LOCATION_ABOVE_CENTER); imageView_AboveCenter->setLayoutParameter(rp_AboveCenter); - - + + // below center ImageView* imageView_BelowCenter = ImageView::create("cocosui/switch-mask.png"); layout->addChild(imageView_BelowCenter); - + RelativeLayoutParameter* rp_BelowCenter = RelativeLayoutParameter::create(); rp_BelowCenter->setRelativeToWidgetName("rp_Center"); rp_BelowCenter->setAlign(RelativeLayoutParameter::RelativeAlign::LOCATION_BELOW_CENTER); imageView_BelowCenter->setLayoutParameter(rp_BelowCenter); - - + + // left center ImageView* imageView_LeftCenter = ImageView::create("cocosui/switch-mask.png"); layout->addChild(imageView_LeftCenter); - + RelativeLayoutParameter* rp_LeftCenter = RelativeLayoutParameter::create(); rp_LeftCenter->setRelativeToWidgetName("rp_Center"); rp_LeftCenter->setAlign(RelativeLayoutParameter::RelativeAlign::LOCATION_LEFT_OF_CENTER); imageView_LeftCenter->setLayoutParameter(rp_LeftCenter); - - - + + + // right center ImageView* imageView_RightCenter = ImageView::create("cocosui/switch-mask.png"); layout->addChild(imageView_RightCenter); - + RelativeLayoutParameter* rp_RightCenter = RelativeLayoutParameter::create(); rp_RightCenter->setRelativeToWidgetName("rp_Center"); rp_RightCenter->setAlign(RelativeLayoutParameter::RelativeAlign::LOCATION_RIGHT_OF_CENTER); imageView_RightCenter->setLayoutParameter(rp_RightCenter); - - + + return true; } - + return false; } From f5a242be971ef1644b9a1fb83364f0fabc485b24 Mon Sep 17 00:00:00 2001 From: Liam Date: Mon, 22 Dec 2014 15:23:26 +0800 Subject: [PATCH 05/13] update for widget percent position and size logic --- cocos/ui/UIWidget.cpp | 128 ++++++++++++++---------------------------- cocos/ui/UIWidget.h | 2 +- 2 files changed, 43 insertions(+), 87 deletions(-) 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 From ce9f269680298d0a9e03164747a7a08e9233c154 Mon Sep 17 00:00:00 2001 From: Liam Date: Mon, 22 Dec 2014 15:38:47 +0800 Subject: [PATCH 06/13] update object reader for new lay out system --- .../cocostudio/ActionTimeline/CSLoader.cpp | 8 + .../cocostudio/ActionTimeline/CSLoader.h | 4 +- .../cocostudio/CSParseBinary_generated.h | 140 ++++++++++- .../cocostudio/FlatBuffersSerialize.cpp | 34 ++- .../cocostudio/FlatBuffersSerialize.h | 3 +- .../ButtonReader/ButtonReader.cpp | 5 + .../ImageViewReader/ImageViewReader.cpp | 6 +- .../WidgetReader/NodeReader/NodeReader.cpp | 208 +++++++++++++++- .../WidgetReader/NodeReader/NodeReader.h | 1 + .../cocostudio/WidgetReader/WidgetReader.cpp | 231 +++++++++++++++++- .../cocostudio/WidgetReader/WidgetReader.h | 1 + 11 files changed, 614 insertions(+), 27 deletions(-) diff --git a/cocos/editor-support/cocostudio/ActionTimeline/CSLoader.cpp b/cocos/editor-support/cocostudio/ActionTimeline/CSLoader.cpp index 99da4ab027..527c14e1ac 100644 --- a/cocos/editor-support/cocostudio/ActionTimeline/CSLoader.cpp +++ b/cocos/editor-support/cocostudio/ActionTimeline/CSLoader.cpp @@ -762,6 +762,9 @@ Node* CSLoader::nodeWithFlatBuffersFile(const std::string &fileName) SpriteFrameCache::getInstance()->addSpriteFramesWithFile(textures->Get(i)->c_str()); } + auto v = csparsebinary->version(); + if (v) _csdVersion = v->c_str(); + Node* node = nodeWithFlatBuffers(csparsebinary->nodeTree()); return node; @@ -1083,6 +1086,10 @@ Node* CSLoader::createNodeWithFlatBuffersForSimulator(const std::string& filenam } auto nodeTree = csparsebinary->nodeTree(); + + auto v = csparsebinary->version(); + if (v) _csdVersion = v->c_str(); + Node* node = nodeWithFlatBuffersForSimulator(nodeTree); _rootNode = nullptr; @@ -1189,6 +1196,7 @@ Node* CSLoader::nodeWithFlatBuffersForSimulator(const flatbuffers::NodeTree *nod node->addChild(child); } } + Helper::doLayout(node); } // _loadingNodeParentHierarchy.pop_back(); diff --git a/cocos/editor-support/cocostudio/ActionTimeline/CSLoader.h b/cocos/editor-support/cocostudio/ActionTimeline/CSLoader.h index d80d792ef5..38cadc9cef 100644 --- a/cocos/editor-support/cocostudio/ActionTimeline/CSLoader.h +++ b/cocos/editor-support/cocostudio/ActionTimeline/CSLoader.h @@ -100,7 +100,8 @@ public: cocos2d::Node* createNodeWithFlatBuffersForSimulator(const std::string& filename); cocos2d::Node* nodeWithFlatBuffersForSimulator(const flatbuffers::NodeTree* nodetree); - + std::string getCsdVersion() { return _csdVersion; } + protected: cocos2d::Node* loadNode(const rapidjson::Value& json); @@ -147,6 +148,7 @@ protected: Node* _rootNode; // std::vector _loadingNodeParentHierarchy; + std::string _csdVersion; }; NS_CC_END diff --git a/cocos/editor-support/cocostudio/CSParseBinary_generated.h b/cocos/editor-support/cocostudio/CSParseBinary_generated.h index d4360f64a0..d4ebff7371 100644 --- a/cocos/editor-support/cocostudio/CSParseBinary_generated.h +++ b/cocos/editor-support/cocostudio/CSParseBinary_generated.h @@ -12,6 +12,7 @@ struct CSParseBinary; struct NodeTree; struct Options; struct WidgetOptions; +struct LayoutComponentTable; struct SingleNodeOptions; struct SpriteOptions; struct ParticleSystemOptions; @@ -176,6 +177,7 @@ struct CSParseBinary : private flatbuffers::Table { const flatbuffers::Vector> *texturePngs() const { return GetPointer> *>(6); } const NodeTree *nodeTree() const { return GetPointer(8); } const NodeAction *action() const { return GetPointer(10); } + const flatbuffers::String *version() const { return GetPointer(12); } bool Verify(flatbuffers::Verifier &verifier) const { return VerifyTableStart(verifier) && VerifyField(verifier, 4 /* textures */) && @@ -188,6 +190,8 @@ struct CSParseBinary : private flatbuffers::Table { verifier.VerifyTable(nodeTree()) && VerifyField(verifier, 10 /* action */) && verifier.VerifyTable(action()) && + VerifyField(verifier, 12 /* version */) && + verifier.Verify(version()) && verifier.EndTable(); } }; @@ -199,10 +203,11 @@ struct CSParseBinaryBuilder { void add_texturePngs(flatbuffers::Offset>> texturePngs) { fbb_.AddOffset(6, texturePngs); } void add_nodeTree(flatbuffers::Offset nodeTree) { fbb_.AddOffset(8, nodeTree); } void add_action(flatbuffers::Offset action) { fbb_.AddOffset(10, action); } + void add_version(flatbuffers::Offset version) { fbb_.AddOffset(12, version); } CSParseBinaryBuilder(flatbuffers::FlatBufferBuilder &_fbb) : fbb_(_fbb) { start_ = fbb_.StartTable(); } CSParseBinaryBuilder &operator=(const CSParseBinaryBuilder &); flatbuffers::Offset Finish() { - auto o = flatbuffers::Offset(fbb_.EndTable(start_, 4)); + auto o = flatbuffers::Offset(fbb_.EndTable(start_, 5)); return o; } }; @@ -211,8 +216,10 @@ inline flatbuffers::Offset CreateCSParseBinary(flatbuffers::FlatB flatbuffers::Offset>> textures = 0, flatbuffers::Offset>> texturePngs = 0, flatbuffers::Offset nodeTree = 0, - flatbuffers::Offset action = 0) { + flatbuffers::Offset action = 0, + flatbuffers::Offset version = 0) { CSParseBinaryBuilder builder_(_fbb); + builder_.add_version(version); builder_.add_action(action); builder_.add_nodeTree(nodeTree); builder_.add_texturePngs(texturePngs); @@ -318,6 +325,7 @@ struct WidgetOptions : private flatbuffers::Table { const flatbuffers::String *customProperty() const { return GetPointer(38); } const flatbuffers::String *callBackType() const { return GetPointer(40); } const flatbuffers::String *callBackName() const { return GetPointer(42); } + const LayoutComponentTable *layoutComponent() const { return GetPointer(44); } bool Verify(flatbuffers::Verifier &verifier) const { return VerifyTableStart(verifier) && VerifyField(verifier, 4 /* name */) && @@ -345,6 +353,8 @@ struct WidgetOptions : private flatbuffers::Table { verifier.Verify(callBackType()) && VerifyField(verifier, 42 /* callBackName */) && verifier.Verify(callBackName()) && + VerifyField(verifier, 44 /* layoutComponent */) && + verifier.VerifyTable(layoutComponent()) && verifier.EndTable(); } }; @@ -372,10 +382,11 @@ struct WidgetOptionsBuilder { void add_customProperty(flatbuffers::Offset customProperty) { fbb_.AddOffset(38, customProperty); } void add_callBackType(flatbuffers::Offset callBackType) { fbb_.AddOffset(40, callBackType); } void add_callBackName(flatbuffers::Offset callBackName) { fbb_.AddOffset(42, callBackName); } + void add_layoutComponent(flatbuffers::Offset layoutComponent) { fbb_.AddOffset(44, layoutComponent); } WidgetOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) : fbb_(_fbb) { start_ = fbb_.StartTable(); } WidgetOptionsBuilder &operator=(const WidgetOptionsBuilder &); flatbuffers::Offset Finish() { - auto o = flatbuffers::Offset(fbb_.EndTable(start_, 20)); + auto o = flatbuffers::Offset(fbb_.EndTable(start_, 21)); return o; } }; @@ -400,8 +411,10 @@ inline flatbuffers::Offset CreateWidgetOptions(flatbuffers::FlatB flatbuffers::Offset frameEvent = 0, flatbuffers::Offset customProperty = 0, flatbuffers::Offset callBackType = 0, - flatbuffers::Offset callBackName = 0) { + flatbuffers::Offset callBackName = 0, + flatbuffers::Offset layoutComponent = 0) { WidgetOptionsBuilder builder_(_fbb); + builder_.add_layoutComponent(layoutComponent); builder_.add_callBackName(callBackName); builder_.add_callBackType(callBackType); builder_.add_customProperty(customProperty); @@ -425,6 +438,111 @@ inline flatbuffers::Offset CreateWidgetOptions(flatbuffers::FlatB return builder_.Finish(); } +struct LayoutComponentTable : private flatbuffers::Table { + uint8_t positionXPercentEnabled() const { return GetField(4, 0); } + uint8_t positionYPercentEnabled() const { return GetField(6, 0); } + float positionXPercent() const { return GetField(8, 0); } + float positionYPercent() const { return GetField(10, 0); } + uint8_t sizeXPercentEnable() const { return GetField(12, 0); } + uint8_t sizeYPercentEnable() const { return GetField(14, 0); } + float sizeXPercent() const { return GetField(16, 0); } + float sizeYPercent() const { return GetField(18, 0); } + uint8_t stretchHorizontalEnabled() const { return GetField(20, 0); } + uint8_t stretchVerticalEnabled() const { return GetField(22, 0); } + const flatbuffers::String *horizontalEdge() const { return GetPointer(24); } + const flatbuffers::String *verticalEdge() const { return GetPointer(26); } + float leftMargin() const { return GetField(28, 0); } + float rightMargin() const { return GetField(30, 0); } + float topMargin() const { return GetField(32, 0); } + float bottomMargin() const { return GetField(34, 0); } + bool Verify(flatbuffers::Verifier &verifier) const { + return VerifyTableStart(verifier) && + VerifyField(verifier, 4 /* positionXPercentEnabled */) && + VerifyField(verifier, 6 /* positionYPercentEnabled */) && + VerifyField(verifier, 8 /* positionXPercent */) && + VerifyField(verifier, 10 /* positionYPercent */) && + VerifyField(verifier, 12 /* sizeXPercentEnable */) && + VerifyField(verifier, 14 /* sizeYPercentEnable */) && + VerifyField(verifier, 16 /* sizeXPercent */) && + VerifyField(verifier, 18 /* sizeYPercent */) && + VerifyField(verifier, 20 /* stretchHorizontalEnabled */) && + VerifyField(verifier, 22 /* stretchVerticalEnabled */) && + VerifyField(verifier, 24 /* horizontalEdge */) && + verifier.Verify(horizontalEdge()) && + VerifyField(verifier, 26 /* verticalEdge */) && + verifier.Verify(verticalEdge()) && + VerifyField(verifier, 28 /* leftMargin */) && + VerifyField(verifier, 30 /* rightMargin */) && + VerifyField(verifier, 32 /* topMargin */) && + VerifyField(verifier, 34 /* bottomMargin */) && + verifier.EndTable(); + } +}; + +struct LayoutComponentTableBuilder { + flatbuffers::FlatBufferBuilder &fbb_; + flatbuffers::uoffset_t start_; + void add_positionXPercentEnabled(uint8_t positionXPercentEnabled) { fbb_.AddElement(4, positionXPercentEnabled, 0); } + void add_positionYPercentEnabled(uint8_t positionYPercentEnabled) { fbb_.AddElement(6, positionYPercentEnabled, 0); } + void add_positionXPercent(float positionXPercent) { fbb_.AddElement(8, positionXPercent, 0); } + void add_positionYPercent(float positionYPercent) { fbb_.AddElement(10, positionYPercent, 0); } + void add_sizeXPercentEnable(uint8_t sizeXPercentEnable) { fbb_.AddElement(12, sizeXPercentEnable, 0); } + void add_sizeYPercentEnable(uint8_t sizeYPercentEnable) { fbb_.AddElement(14, sizeYPercentEnable, 0); } + void add_sizeXPercent(float sizeXPercent) { fbb_.AddElement(16, sizeXPercent, 0); } + void add_sizeYPercent(float sizeYPercent) { fbb_.AddElement(18, sizeYPercent, 0); } + void add_stretchHorizontalEnabled(uint8_t stretchHorizontalEnabled) { fbb_.AddElement(20, stretchHorizontalEnabled, 0); } + void add_stretchVerticalEnabled(uint8_t stretchVerticalEnabled) { fbb_.AddElement(22, stretchVerticalEnabled, 0); } + void add_horizontalEdge(flatbuffers::Offset horizontalEdge) { fbb_.AddOffset(24, horizontalEdge); } + void add_verticalEdge(flatbuffers::Offset verticalEdge) { fbb_.AddOffset(26, verticalEdge); } + void add_leftMargin(float leftMargin) { fbb_.AddElement(28, leftMargin, 0); } + void add_rightMargin(float rightMargin) { fbb_.AddElement(30, rightMargin, 0); } + void add_topMargin(float topMargin) { fbb_.AddElement(32, topMargin, 0); } + void add_bottomMargin(float bottomMargin) { fbb_.AddElement(34, bottomMargin, 0); } + LayoutComponentTableBuilder(flatbuffers::FlatBufferBuilder &_fbb) : fbb_(_fbb) { start_ = fbb_.StartTable(); } + LayoutComponentTableBuilder &operator=(const LayoutComponentTableBuilder &); + flatbuffers::Offset Finish() { + auto o = flatbuffers::Offset(fbb_.EndTable(start_, 16)); + return o; + } +}; + +inline flatbuffers::Offset CreateLayoutComponentTable(flatbuffers::FlatBufferBuilder &_fbb, + uint8_t positionXPercentEnabled = 0, + uint8_t positionYPercentEnabled = 0, + float positionXPercent = 0, + float positionYPercent = 0, + uint8_t sizeXPercentEnable = 0, + uint8_t sizeYPercentEnable = 0, + float sizeXPercent = 0, + float sizeYPercent = 0, + uint8_t stretchHorizontalEnabled = 0, + uint8_t stretchVerticalEnabled = 0, + flatbuffers::Offset horizontalEdge = 0, + flatbuffers::Offset verticalEdge = 0, + float leftMargin = 0, + float rightMargin = 0, + float topMargin = 0, + float bottomMargin = 0) { + LayoutComponentTableBuilder builder_(_fbb); + builder_.add_bottomMargin(bottomMargin); + builder_.add_topMargin(topMargin); + builder_.add_rightMargin(rightMargin); + builder_.add_leftMargin(leftMargin); + builder_.add_verticalEdge(verticalEdge); + builder_.add_horizontalEdge(horizontalEdge); + builder_.add_sizeYPercent(sizeYPercent); + builder_.add_sizeXPercent(sizeXPercent); + builder_.add_positionYPercent(positionYPercent); + builder_.add_positionXPercent(positionXPercent); + builder_.add_stretchVerticalEnabled(stretchVerticalEnabled); + builder_.add_stretchHorizontalEnabled(stretchHorizontalEnabled); + builder_.add_sizeYPercentEnable(sizeYPercentEnable); + builder_.add_sizeXPercentEnable(sizeXPercentEnable); + builder_.add_positionYPercentEnabled(positionYPercentEnabled); + builder_.add_positionXPercentEnabled(positionXPercentEnabled); + return builder_.Finish(); +} + struct SingleNodeOptions : private flatbuffers::Table { const WidgetOptions *nodeOptions() const { return GetPointer(4); } bool Verify(flatbuffers::Verifier &verifier) const { @@ -1557,12 +1675,16 @@ inline flatbuffers::Offset CreateListViewOptions(flatbuffers::F struct ProjectNodeOptions : private flatbuffers::Table { const WidgetOptions *nodeOptions() const { return GetPointer(4); } const flatbuffers::String *fileName() const { return GetPointer(6); } + uint8_t isLoop() const { return GetField(8, 1); } + uint8_t isAutoPlay() const { return GetField(10, 1); } bool Verify(flatbuffers::Verifier &verifier) const { return VerifyTableStart(verifier) && VerifyField(verifier, 4 /* nodeOptions */) && verifier.VerifyTable(nodeOptions()) && VerifyField(verifier, 6 /* fileName */) && verifier.Verify(fileName()) && + VerifyField(verifier, 8 /* isLoop */) && + VerifyField(verifier, 10 /* isAutoPlay */) && verifier.EndTable(); } }; @@ -1572,20 +1694,26 @@ struct ProjectNodeOptionsBuilder { flatbuffers::uoffset_t start_; void add_nodeOptions(flatbuffers::Offset nodeOptions) { fbb_.AddOffset(4, nodeOptions); } void add_fileName(flatbuffers::Offset fileName) { fbb_.AddOffset(6, fileName); } + void add_isLoop(uint8_t isLoop) { fbb_.AddElement(8, isLoop, 1); } + void add_isAutoPlay(uint8_t isAutoPlay) { fbb_.AddElement(10, isAutoPlay, 1); } ProjectNodeOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) : fbb_(_fbb) { start_ = fbb_.StartTable(); } ProjectNodeOptionsBuilder &operator=(const ProjectNodeOptionsBuilder &); flatbuffers::Offset Finish() { - auto o = flatbuffers::Offset(fbb_.EndTable(start_, 2)); + auto o = flatbuffers::Offset(fbb_.EndTable(start_, 4)); return o; } }; inline flatbuffers::Offset CreateProjectNodeOptions(flatbuffers::FlatBufferBuilder &_fbb, flatbuffers::Offset nodeOptions = 0, - flatbuffers::Offset fileName = 0) { + flatbuffers::Offset fileName = 0, + uint8_t isLoop = 1, + uint8_t isAutoPlay = 1) { ProjectNodeOptionsBuilder builder_(_fbb); builder_.add_fileName(fileName); builder_.add_nodeOptions(nodeOptions); + builder_.add_isAutoPlay(isAutoPlay); + builder_.add_isLoop(isLoop); return builder_.Finish(); } diff --git a/cocos/editor-support/cocostudio/FlatBuffersSerialize.cpp b/cocos/editor-support/cocostudio/FlatBuffersSerialize.cpp index d3c135a831..615973aee7 100644 --- a/cocos/editor-support/cocostudio/FlatBuffersSerialize.cpp +++ b/cocos/editor-support/cocostudio/FlatBuffersSerialize.cpp @@ -167,7 +167,15 @@ std::string FlatBuffersSerialize::serializeFlatBuffersWithXMLFile(const std::str while (element) { // CCLOG("entity name = %s", element->Name()); - + if (strcmp("PropertyGroup", element->Name()) == 0) + { + const tinyxml2::XMLAttribute* attribute = element->FirstAttribute(); + while (attribute && strcmp("Version", attribute->Name()) != 0) + attribute = attribute->Next(); + if (attribute) + _csdVersion = attribute->Value(); + } + if (strcmp("Content", element->Name()) == 0) { const tinyxml2::XMLAttribute* attribute = element->FirstAttribute(); @@ -248,10 +256,11 @@ std::string FlatBuffersSerialize::serializeFlatBuffersWithXMLFile(const std::str auto csparsebinary = CreateCSParseBinary(*_builder, _builder->CreateVector(_textures), - _builder->CreateVector(_texturePngs), + _builder->CreateVector(_texturePngs), nodeTree, - aciton); - _builder->Finish(csparsebinary); + aciton, + _builder->CreateString(_csdVersion)); + _builder->Finish(csparsebinary); _textures.clear(); _texturePngs.clear(); @@ -988,7 +997,15 @@ FlatBufferBuilder* FlatBuffersSerialize::createFlatBuffersWithXMLFileForSimulato while (element) { // CCLOG("entity name = %s", element->Name()); - + if (strcmp("PropertyGroup", element->Name()) == 0) + { + const tinyxml2::XMLAttribute* attribute = element->FirstAttribute(); + while (attribute && strcmp("Version", attribute->Name()) != 0) + attribute = attribute->Next(); + if (attribute) + _csdVersion = attribute->Value(); + } + if (strcmp("Content", element->Name()) == 0) { const tinyxml2::XMLAttribute* attribute = element->FirstAttribute(); @@ -1046,10 +1063,11 @@ FlatBufferBuilder* FlatBuffersSerialize::createFlatBuffersWithXMLFileForSimulato } auto csparsebinary = CreateCSParseBinary(*_builder, - _builder->CreateVector(_textures), - _builder->CreateVector(_texturePngs), + _builder->CreateVector(_textures), + _builder->CreateVector(_texturePngs), nodeTree, - aciton); + aciton, + _builder->CreateString(_csdVersion)); _builder->Finish(csparsebinary); _textures.clear(); diff --git a/cocos/editor-support/cocostudio/FlatBuffersSerialize.h b/cocos/editor-support/cocostudio/FlatBuffersSerialize.h index f514a8f38f..bdaea811ea 100644 --- a/cocos/editor-support/cocostudio/FlatBuffersSerialize.h +++ b/cocos/editor-support/cocostudio/FlatBuffersSerialize.h @@ -128,6 +128,7 @@ public: std::string classType); flatbuffers::Offset createProjectNodeOptionsForSimulator(const tinyxml2::XMLElement* objectData); /**/ + std::string getCsdVersion() { return _csdVersion; } public: std::vector> _textures; @@ -137,7 +138,7 @@ public: private: flatbuffers::FlatBufferBuilder* _builder; flatbuffers::Offset* _csparsebinary; - + std::string _csdVersion; }; } diff --git a/cocos/editor-support/cocostudio/WidgetReader/ButtonReader/ButtonReader.cpp b/cocos/editor-support/cocostudio/WidgetReader/ButtonReader/ButtonReader.cpp index e4136795a5..3064733e99 100644 --- a/cocos/editor-support/cocostudio/WidgetReader/ButtonReader/ButtonReader.cpp +++ b/cocos/editor-support/cocostudio/WidgetReader/ButtonReader/ButtonReader.cpp @@ -796,6 +796,11 @@ namespace cocostudio Size scale9Size(options->scale9Size()->width(), options->scale9Size()->height()); button->setContentSize(scale9Size); } + else + { + Size contentSize(options->widgetOptions()->size()->width(), options->widgetOptions()->size()->height()); + button->setContentSize(contentSize); + } } Node* ButtonReader::createNodeWithFlatBuffers(const flatbuffers::Table *buttonOptions) diff --git a/cocos/editor-support/cocostudio/WidgetReader/ImageViewReader/ImageViewReader.cpp b/cocos/editor-support/cocostudio/WidgetReader/ImageViewReader/ImageViewReader.cpp index 1fbd8da0ac..ac5b11a127 100644 --- a/cocos/editor-support/cocostudio/WidgetReader/ImageViewReader/ImageViewReader.cpp +++ b/cocos/editor-support/cocostudio/WidgetReader/ImageViewReader/ImageViewReader.cpp @@ -375,7 +375,11 @@ namespace cocostudio Rect capInsets(f_capInset->x(), f_capInset->y(), f_capInset->width(), f_capInset->height()); imageView->setCapInsets(capInsets); } - + else + { + Size contentSize(options->widgetOptions()->size()->width(), options->widgetOptions()->size()->height()); + imageView->setContentSize(contentSize); + } } Node* ImageViewReader::createNodeWithFlatBuffers(const flatbuffers::Table *imageViewOptions) diff --git a/cocos/editor-support/cocostudio/WidgetReader/NodeReader/NodeReader.cpp b/cocos/editor-support/cocostudio/WidgetReader/NodeReader/NodeReader.cpp index 38d46b4a88..2270cc66bb 100644 --- a/cocos/editor-support/cocostudio/WidgetReader/NodeReader/NodeReader.cpp +++ b/cocos/editor-support/cocostudio/WidgetReader/NodeReader/NodeReader.cpp @@ -29,7 +29,7 @@ #include "tinyxml2.h" #include "flatbuffers/flatbuffers.h" - +#include "ui/UILayoutComponent.h" USING_NS_CC; using namespace flatbuffers; @@ -87,6 +87,23 @@ namespace cocostudio bool touchEnabled = false; std::string frameEvent = ""; std::string customProperty = ""; + + bool positionXPercentEnabled = false; + bool positionYPercentEnabled = false; + float positionXPercent = 0; + float positionYPercent = 0; + bool sizeXPercentEnable = false; + bool sizeYPercentEnable = false; + float sizeXPercent = 0; + float sizeYPercent = 0; + bool stretchHorizontalEnabled = false; + bool stretchVerticalEnabled = false; + std::string horizontalEdge; + std::string verticalEdge; + float leftMargin = 0; + float rightMargin = 0; + float topMargin = 0; + float bottomMargin = 0; // attributes const tinyxml2::XMLAttribute* attribute = objectData->FirstAttribute(); @@ -151,6 +168,54 @@ namespace cocostudio { frameEvent = value; } + else if (attriname == "PositionPrecentXEnabled") + { + positionXPercentEnabled = value == "True"; + } + else if (attriname == "PositionPrecentYEnabled") + { + positionYPercentEnabled = value == "True"; + } + else if (attriname == "PercentWidthEnable") + { + sizeXPercentEnable = value == "True"; + } + else if (attriname == "PercentHeightEnbale") + { + sizeYPercentEnable = value == "True"; + } + else if (attriname == "StretchWidthEnable") + { + stretchHorizontalEnabled = value == "True"; + } + else if (attriname == "StretchHeightEnable") + { + stretchVerticalEnabled = value == "True"; + } + else if (attriname == "HorizontalEage") + { + horizontalEdge = value; + } + else if (attriname == "VerticalEage") + { + verticalEdge = value; + } + else if (attriname == "LeftMargin") + { + leftMargin = atof(value.c_str()); + } + else if (attriname == "RightMargin") + { + rightMargin = atof(value.c_str()); + } + else if (attriname == "TopMargin") + { + topMargin = atof(value.c_str()); + } + else if (attriname == "ButtomMargin") + { + bottomMargin = atof(value.c_str()); + } attribute = attribute->Next(); } @@ -276,7 +341,48 @@ namespace cocostudio attribute = attribute->Next(); } } - + else if (attriname == "PrePosition") + { + attribute = child->FirstAttribute(); + + while (attribute) + { + attriname = attribute->Name(); + std::string value = attribute->Value(); + + if (attriname == "X") + { + positionXPercent = atof(value.c_str()); + } + else if (attriname == "Y") + { + positionYPercent = atof(value.c_str()); + } + + attribute = attribute->Next(); + } + } + else if (attriname == "PreSize") + { + attribute = child->FirstAttribute(); + + while (attribute) + { + attriname = attribute->Name(); + std::string value = attribute->Value(); + + if (attriname == "X") + { + sizeXPercent = atof(value.c_str()); + } + else if (attriname == "Y") + { + sizeYPercent = atof(value.c_str()); + } + + attribute = attribute->Next(); + } + } child = child->NextSiblingElement(); } @@ -286,7 +392,23 @@ namespace cocostudio AnchorPoint f_anchortpoint(anchorPoint.x, anchorPoint.y); Color f_color(color.a, color.r, color.g, color.b); FlatSize f_size(size.x, size.y); - + auto f_layoutComponent = CreateLayoutComponentTable(*builder, + positionXPercentEnabled, + positionYPercentEnabled, + positionXPercent, + positionYPercent, + sizeXPercentEnable, + sizeYPercentEnable, + sizeXPercent, + sizeYPercent, + stretchHorizontalEnabled, + stretchVerticalEnabled, + builder->CreateString(horizontalEdge), + builder->CreateString(verticalEdge), + leftMargin, + rightMargin, + topMargin, + bottomMargin); auto options = CreateWidgetOptions(*builder, builder->CreateString(name), @@ -306,8 +428,10 @@ namespace cocostudio ignoreSize, touchEnabled, builder->CreateString(frameEvent), - builder->CreateString(customProperty) - ); + builder->CreateString(customProperty), + 0, + 0, + f_layoutComponent); return *(Offset*)(&options); } @@ -368,8 +492,80 @@ namespace cocostudio node->setCascadeColorEnabled(true); node->setCascadeOpacityEnabled(true); + + setLayoutComponentPropsWithFlatBuffers(node, nodeOptions); } - + + void NodeReader::setLayoutComponentPropsWithFlatBuffers(cocos2d::Node* node, const flatbuffers::Table* nodeOptions) + { + auto layoutComponentTable = ((WidgetOptions*)nodeOptions)->layoutComponent(); + if (!layoutComponentTable) return; + + bool positionXPercentEnabled = layoutComponentTable->positionXPercentEnabled(); + bool positionYPercentEnabled = layoutComponentTable->positionYPercentEnabled(); + float positionXPercent = layoutComponentTable->positionXPercent(); + float positionYPercent = layoutComponentTable->positionYPercent(); + bool sizeXPercentEnable = layoutComponentTable->sizeXPercentEnable(); + bool sizeYPercentEnable = layoutComponentTable->sizeYPercentEnable(); + float sizeXPercent = layoutComponentTable->sizeXPercent(); + float sizeYPercent = layoutComponentTable->sizeYPercent(); + bool stretchHorizontalEnabled = layoutComponentTable->stretchHorizontalEnabled(); + bool stretchVerticalEnabled = layoutComponentTable->stretchVerticalEnabled(); + std::string horizontalEdge = layoutComponentTable->horizontalEdge()->c_str(); + std::string verticalEdge = layoutComponentTable->verticalEdge()->c_str(); + float leftMargin = layoutComponentTable->leftMargin(); + float rightMargin = layoutComponentTable->rightMargin(); + float topMargin = layoutComponentTable->topMargin(); + float bottomMargin = layoutComponentTable->bottomMargin(); + + auto layoutComponent = ui::LayoutComponent::create(); + node->addComponent(layoutComponent); + + layoutComponent->setPositionPercentXEnabled(positionXPercentEnabled); + layoutComponent->setPositionPercentYEnabled(positionYPercentEnabled); + layoutComponent->setPositionPercentX(positionXPercent); + layoutComponent->setPositionPercentY(positionYPercent); + layoutComponent->setPercentWidthEnabled(sizeXPercentEnable); + layoutComponent->setPercentHeightEnabled(sizeYPercentEnable); + layoutComponent->setPercentWidth(sizeXPercent); + layoutComponent->setPercentHeight(sizeYPercent); + layoutComponent->setStretchWidthEnabled(stretchHorizontalEnabled); + layoutComponent->setStretchHeightEnabled(stretchVerticalEnabled); + ui::LayoutComponent::HorizontalEage horizontalEdgeType = ui::LayoutComponent::HorizontalEage::None; + if (horizontalEdge == "LeftEage") + { + horizontalEdgeType = ui::LayoutComponent::HorizontalEage::Left; + } + else if (horizontalEdge == "RightEage") + { + horizontalEdgeType = ui::LayoutComponent::HorizontalEage::Right; + } + else if (horizontalEdge == "BothEage") + { + horizontalEdgeType = ui::LayoutComponent::HorizontalEage::Center; + } + layoutComponent->setHorizontalEage(horizontalEdgeType); + ui::LayoutComponent::VerticalEage verticalEdgeType = ui::LayoutComponent::VerticalEage::None; + if (verticalEdge == "TopEage") + { + verticalEdgeType = ui::LayoutComponent::VerticalEage::Top; + } + else if (verticalEdge == "ButtomEage") + { + verticalEdgeType = ui::LayoutComponent::VerticalEage::Bottom; + } + else if (verticalEdge == "BothEage") + { + verticalEdgeType = ui::LayoutComponent::VerticalEage::Center; + } + layoutComponent->setVerticalEage(verticalEdgeType); + + layoutComponent->setTopMargin(topMargin); + layoutComponent->setButtomMargin(bottomMargin); + layoutComponent->setLeftMargin(leftMargin); + layoutComponent->setRightMargin(rightMargin); + } + Node* NodeReader::createNodeWithFlatBuffers(const flatbuffers::Table *nodeOptions) { Node* node = Node::create(); diff --git a/cocos/editor-support/cocostudio/WidgetReader/NodeReader/NodeReader.h b/cocos/editor-support/cocostudio/WidgetReader/NodeReader/NodeReader.h index 57fb46f660..84049e6d73 100644 --- a/cocos/editor-support/cocostudio/WidgetReader/NodeReader/NodeReader.h +++ b/cocos/editor-support/cocostudio/WidgetReader/NodeReader/NodeReader.h @@ -46,6 +46,7 @@ namespace cocostudio flatbuffers::Offset createOptionsWithFlatBuffers(const tinyxml2::XMLElement* objectData, flatbuffers::FlatBufferBuilder* builder); void setPropsWithFlatBuffers(cocos2d::Node* node, const flatbuffers::Table* nodeOptions); + void setLayoutComponentPropsWithFlatBuffers(cocos2d::Node* node, const flatbuffers::Table* nodeOptions); cocos2d::Node* createNodeWithFlatBuffers(const flatbuffers::Table* nodeOptions); }; } diff --git a/cocos/editor-support/cocostudio/WidgetReader/WidgetReader.cpp b/cocos/editor-support/cocostudio/WidgetReader/WidgetReader.cpp index 5e382580c3..4e4d8397ae 100644 --- a/cocos/editor-support/cocostudio/WidgetReader/WidgetReader.cpp +++ b/cocos/editor-support/cocostudio/WidgetReader/WidgetReader.cpp @@ -9,6 +9,8 @@ #include "tinyxml2.h" #include "flatbuffers/flatbuffers.h" +#include "ui/UILayoutComponent.h" +#include "../ActionTimeline/CSLoader.h" USING_NS_CC; using namespace ui; @@ -384,6 +386,23 @@ namespace cocostudio std::string customProperty = ""; std::string callbackType = ""; std::string callbackName = ""; + + bool positionXPercentEnabled = false; + bool positionYPercentEnabled = false; + float positionXPercent = 0; + float positionYPercent = 0; + bool sizeXPercentEnable = false; + bool sizeYPercentEnable = false; + float sizeXPercent = 0; + float sizeYPercent = 0; + bool stretchHorizontalEnabled = false; + bool stretchVerticalEnabled = false; + std::string horizontalEdge; + std::string verticalEdge; + float leftMargin = 0; + float rightMargin = 0; + float topMargin = 0; + float bottomMargin = 0; // attributes const tinyxml2::XMLAttribute* attribute = objectData->FirstAttribute(); @@ -456,7 +475,55 @@ namespace cocostudio { callbackName = value; } - + else if (attriname == "PositionPrecentXEnabled") + { + positionXPercentEnabled = value == "True"; + } + else if (attriname == "PositionPrecentYEnabled") + { + positionYPercentEnabled = value == "True"; + } + else if (attriname == "PercentWidthEnable") + { + sizeXPercentEnable = value == "True"; + } + else if (attriname == "PercentHeightEnbale") + { + sizeYPercentEnable = value == "True"; + } + else if (attriname == "StretchWidthEnable") + { + stretchHorizontalEnabled = value == "True"; + } + else if (attriname == "StretchHeightEnable") + { + stretchVerticalEnabled = value == "True"; + } + else if (attriname == "HorizontalEage") + { + horizontalEdge = value; + } + else if (attriname == "VerticalEage") + { + verticalEdge = value; + } + else if (attriname == "LeftMargin") + { + leftMargin = atof(value.c_str()); + } + else if (attriname == "RightMargin") + { + rightMargin = atof(value.c_str()); + } + else if (attriname == "TopMargin") + { + topMargin = atof(value.c_str()); + } + else if (attriname == "ButtomMargin") + { + bottomMargin = atof(value.c_str()); + } + attribute = attribute->Next(); } @@ -581,6 +648,48 @@ namespace cocostudio attribute = attribute->Next(); } } + else if (attriname == "PrePosition") + { + attribute = child->FirstAttribute(); + + while (attribute) + { + attriname = attribute->Name(); + std::string value = attribute->Value(); + + if (attriname == "X") + { + positionXPercent = atof(value.c_str()); + } + else if (attriname == "Y") + { + positionYPercent = atof(value.c_str()); + } + + attribute = attribute->Next(); + } + } + else if (attriname == "PreSize") + { + attribute = child->FirstAttribute(); + + while (attribute) + { + attriname = attribute->Name(); + std::string value = attribute->Value(); + + if (attriname == "X") + { + sizeXPercent = atof(value.c_str()); + } + else if (attriname == "Y") + { + sizeYPercent = atof(value.c_str()); + } + + attribute = attribute->Next(); + } + } child = child->NextSiblingElement(); } @@ -591,6 +700,23 @@ namespace cocostudio AnchorPoint f_anchortpoint(anchorPoint.x, anchorPoint.y); Color f_color(color.a, color.r, color.g, color.b); FlatSize f_size(size.x, size.y); + auto f_layoutComponent = CreateLayoutComponentTable(*builder, + positionXPercentEnabled, + positionYPercentEnabled, + positionXPercent, + positionYPercent, + sizeXPercentEnable, + sizeYPercentEnable, + sizeXPercent, + sizeYPercent, + stretchHorizontalEnabled, + stretchVerticalEnabled, + builder->CreateString(horizontalEdge), + builder->CreateString(verticalEdge), + leftMargin, + rightMargin, + topMargin, + bottomMargin); auto options = CreateWidgetOptions(*builder, builder->CreateString(name), @@ -612,8 +738,8 @@ namespace cocostudio builder->CreateString(frameEvent), builder->CreateString(customProperty), builder->CreateString(callbackType), - builder->CreateString(callbackName) - ); + builder->CreateString(callbackName), + f_layoutComponent); return *(Offset
*)(&options); } @@ -629,7 +755,33 @@ namespace cocostudio widget->setAnchorPoint(Vec2::ZERO); widget->setUnifySizeEnabled(true); - + std::string versionString = CSLoader::getInstance()->getCsdVersion(); + + //assume versionString is like "2.0.6.0" + if (versionString.length() > 0) + { + int p1, p2, p3, v1, v2, v3; + p1 = p2 = p3 = v1 = v2 = v3 = 0; + p1 = versionString.find('.'); + if (p1 > 0) + { + p2 = versionString.find('.', p1 + 1); + v1 = atoi(versionString.substr(0, p1).c_str()); + } + if (p2 > p1) + { + p3 = versionString.find('.', p2 + 1); + v2 = atoi(versionString.substr(p1 + 1, p2 - p1 - 1).c_str()); + } + if (p3 > p2) + { + v3 = atoi(versionString.substr(p2 + 1, p3 - p2 - 1).c_str()); + } + + if (!(v1 <= 2 && v2 == 0 && v3 <= 6)) + widget->setUnifySizeEnabled(false); + } + bool ignoreSize = options->ignoreSize(); widget->ignoreContentAdaptWithSize(ignoreSize); @@ -691,6 +843,77 @@ namespace cocostudio std::string callbackName = options->callBackName()->c_str(); widget->setCallbackName(callbackName); + setLayoutComponentPropsWithFlatBuffers(widget, widgetOptions); + } + + void WidgetReader::setLayoutComponentPropsWithFlatBuffers(cocos2d::Node* node, const flatbuffers::Table* nodeOptions) + { + auto layoutComponentTable = ((WidgetOptions*)nodeOptions)->layoutComponent(); + if (!layoutComponentTable) return; + + bool positionXPercentEnabled = layoutComponentTable->positionXPercentEnabled(); + bool positionYPercentEnabled = layoutComponentTable->positionYPercentEnabled(); + float positionXPercent = layoutComponentTable->positionXPercent(); + float positionYPercent = layoutComponentTable->positionYPercent(); + bool sizeXPercentEnable = layoutComponentTable->sizeXPercentEnable(); + bool sizeYPercentEnable = layoutComponentTable->sizeYPercentEnable(); + float sizeXPercent = layoutComponentTable->sizeXPercent(); + float sizeYPercent = layoutComponentTable->sizeYPercent(); + bool stretchHorizontalEnabled = layoutComponentTable->stretchHorizontalEnabled(); + bool stretchVerticalEnabled = layoutComponentTable->stretchVerticalEnabled(); + std::string horizontalEdge = layoutComponentTable->horizontalEdge()->c_str(); + std::string verticalEdge = layoutComponentTable->verticalEdge()->c_str(); + float leftMargin = layoutComponentTable->leftMargin(); + float rightMargin = layoutComponentTable->rightMargin(); + float topMargin = layoutComponentTable->topMargin(); + float bottomMargin = layoutComponentTable->bottomMargin(); + + auto layoutComponent = ui::LayoutComponent::create(); + node->addComponent(layoutComponent); + + layoutComponent->setPositionPercentXEnabled(positionXPercentEnabled); + layoutComponent->setPositionPercentYEnabled(positionYPercentEnabled); + layoutComponent->setPositionPercentX(positionXPercent); + layoutComponent->setPositionPercentY(positionYPercent); + layoutComponent->setPercentWidthEnabled(sizeXPercentEnable); + layoutComponent->setPercentHeightEnabled(sizeYPercentEnable); + layoutComponent->setPercentWidth(sizeXPercent); + layoutComponent->setPercentHeight(sizeYPercent); + layoutComponent->setStretchWidthEnabled(stretchHorizontalEnabled); + layoutComponent->setStretchHeightEnabled(stretchVerticalEnabled); + ui::LayoutComponent::HorizontalEage horizontalEdgeType = ui::LayoutComponent::HorizontalEage::None; + if (horizontalEdge == "LeftEage") + { + horizontalEdgeType = ui::LayoutComponent::HorizontalEage::Left; + } + else if (horizontalEdge == "RightEage") + { + horizontalEdgeType = ui::LayoutComponent::HorizontalEage::Right; + } + else if (horizontalEdge == "BothEage") + { + horizontalEdgeType = ui::LayoutComponent::HorizontalEage::Center; + } + layoutComponent->setHorizontalEage(horizontalEdgeType); + ui::LayoutComponent::VerticalEage verticalEdgeType = ui::LayoutComponent::VerticalEage::None; + if (verticalEdge == "TopEage") + { + verticalEdgeType = ui::LayoutComponent::VerticalEage::Top; + } + else if (verticalEdge == "ButtomEage") + { + verticalEdgeType = ui::LayoutComponent::VerticalEage::Bottom; + } + else if (verticalEdge == "BothEage") + { + verticalEdgeType = ui::LayoutComponent::VerticalEage::Center; + } + layoutComponent->setVerticalEage(verticalEdgeType); + + layoutComponent->setTopMargin(topMargin); + layoutComponent->setButtomMargin(bottomMargin); + layoutComponent->setLeftMargin(leftMargin); + layoutComponent->setRightMargin(rightMargin); } Node* WidgetReader::createNodeWithFlatBuffers(const flatbuffers::Table *widgetOptions) diff --git a/cocos/editor-support/cocostudio/WidgetReader/WidgetReader.h b/cocos/editor-support/cocostudio/WidgetReader/WidgetReader.h index 19276f3ea2..6e0a456753 100644 --- a/cocos/editor-support/cocostudio/WidgetReader/WidgetReader.h +++ b/cocos/editor-support/cocostudio/WidgetReader/WidgetReader.h @@ -60,6 +60,7 @@ namespace cocostudio flatbuffers::Offset createOptionsWithFlatBuffers(const tinyxml2::XMLElement* objectData, flatbuffers::FlatBufferBuilder* builder); void setPropsWithFlatBuffers(cocos2d::Node* node, const flatbuffers::Table* widgetOptions); + void setLayoutComponentPropsWithFlatBuffers(cocos2d::Node* node, const flatbuffers::Table* widgetOptions); cocos2d::Node* createNodeWithFlatBuffers(const flatbuffers::Table* widgetOptions); /**/ From 2991b57ec65605aafdbab12391800a9c81539a64 Mon Sep 17 00:00:00 2001 From: Liam Date: Mon, 22 Dec 2014 18:24:06 +0800 Subject: [PATCH 07/13] update lay out reader and samples --- tests/cpp-tests/Classes/AppDelegate.cpp | 2 + .../CocoStudioGUITest/GUIEditorTest.cpp | 2 +- .../UILayoutTest/UILayoutTest_Editor.cpp | 68 +++++++++++++++++++ .../UILayoutTest/UILayoutTest_Editor.h | 12 ++++ .../UISceneManager_Editor.cpp | 8 ++- .../CocoStudioGUITest/UISceneManager_Editor.h | 3 + tests/cpp-tests/Resources/ccs-res | 2 +- 7 files changed, 94 insertions(+), 3 deletions(-) diff --git a/tests/cpp-tests/Classes/AppDelegate.cpp b/tests/cpp-tests/Classes/AppDelegate.cpp index 66a9bbbd6e..c6142aede4 100644 --- a/tests/cpp-tests/Classes/AppDelegate.cpp +++ b/tests/cpp-tests/Classes/AppDelegate.cpp @@ -110,6 +110,7 @@ bool AppDelegate::applicationDidFinishLaunching() searchPaths.push_back("ccs-res/hd/cocosui/UIEditorTest/UILayout/Layout"); searchPaths.push_back("ccs-res/hd/cocosui/UIEditorTest/UILayout/Gradient_Color"); searchPaths.push_back("ccs-res/hd/cocosui/UIEditorTest/UILayout/Scale9_BackgroundImage"); + searchPaths.push_back("ccs-res/hd/cocosui/UIEditorTest/UILayout/LayoutComponent"); searchPaths.push_back("ccs-res/hd/cocosui/UIEditorTest/UILoadingBar"); searchPaths.push_back("ccs-res/hd/cocosui/UIEditorTest/UIPageView"); searchPaths.push_back("ccs-res/hd/cocosui/UIEditorTest/UIScrollView/Both"); @@ -149,6 +150,7 @@ bool AppDelegate::applicationDidFinishLaunching() searchPaths.push_back("ccs-res/cocosui/UIEditorTest/UILayout/Layout"); searchPaths.push_back("ccs-res/cocosui/UIEditorTest/UILayout/Gradient_Color"); searchPaths.push_back("ccs-res/cocosui/UIEditorTest/UILayout/Scale9_BackgroundImage"); + searchPaths.push_back("ccs-res/cocosui/UIEditorTest/UILayout/LayoutComponent"); searchPaths.push_back("ccs-res/cocosui/UIEditorTest/UILoadingBar"); searchPaths.push_back("ccs-res/cocosui/UIEditorTest/UIPageView"); searchPaths.push_back("ccs-res/cocosui/UIEditorTest/UIScrollView/Both"); diff --git a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/GUIEditorTest.cpp b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/GUIEditorTest.cpp index f59843dbe5..152bfc4cbf 100644 --- a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/GUIEditorTest.cpp +++ b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/GUIEditorTest.cpp @@ -177,7 +177,7 @@ g_guisTests[] = UISceneManager_Editor* pManager = UISceneManager_Editor::sharedUISceneManager_Editor(); pManager->setCurrentUISceneId(kUILayoutTest_Editor); pManager->setMinUISceneId(kUILayoutTest_Editor); - pManager->setMaxUISceneId(kUILayoutTest_BackGroundImage_Scale9_Editor); + pManager->setMaxUISceneId(kUILayoutComponentTest_Editor); // pManager->setMaxUISceneId(kUILayoutTest_Layout_Relative_Location_Editor); Scene* pScene = pManager->currentUIScene(); Director::getInstance()->replaceScene(pScene); diff --git a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UILayoutTest/UILayoutTest_Editor.cpp b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UILayoutTest/UILayoutTest_Editor.cpp index ac7b794a89..4e12fb3b23 100644 --- a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UILayoutTest/UILayoutTest_Editor.cpp +++ b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UILayoutTest/UILayoutTest_Editor.cpp @@ -1214,3 +1214,71 @@ bool UILayoutTest_Layout_Relative_Location_Editor::init() return false; } + +UILayoutComponentTest_Editor::UILayoutComponentTest_Editor() +{ + +} + +UILayoutComponentTest_Editor::~UILayoutComponentTest_Editor() +{ + +} + +void UILayoutComponentTest_Editor::configureGUIScene() +{ + Size screenSize = CCDirector::getInstance()->getWinSize(); + + _sceneTitle = Text::create("UILayoutComponentTest_Editor", "", 20); + _sceneTitle->setPosition(Vec2(screenSize.width / 2, screenSize.height - _sceneTitle->getContentSize().height / 2)); + _layout->addChild(_sceneTitle); + + Text* back_label = Text::create("Back", "", 20); + back_label->setTouchEnabled(true); + auto labelLayout = LayoutComponent::boundingLayoutForNode(back_label); + labelLayout->setHorizontalEage(LayoutComponent::HorizontalEage::Right); + labelLayout->setVerticalEage(LayoutComponent::VerticalEage::Bottom); + back_label->addTouchEventListener(CC_CALLBACK_2(UIScene_Editor::toGUIEditorTestScene, this)); + _layout->addChild(back_label); + + Button* left_button = Button::create(); + left_button->loadTextures("Images/b1.png", "Images/b2.png", ""); + left_button->setPosition(Vec2(_layout->getContentSize().width / 2 - left_button->getContentSize().width, + left_button->getContentSize().height * 0.625)); + left_button->setTouchEnabled(true); + left_button->addTouchEventListener(CC_CALLBACK_2(UIScene_Editor::previousCallback, this)); + left_button->setLocalZOrder(_layout->getLocalZOrder() + 1); + _layout->addChild(left_button); + + Button* right_button = Button::create(); + right_button->loadTextures("Images/f1.png", "Images/f2.png", ""); + right_button->setPosition(Vec2(_layout->getContentSize().width / 2 + right_button->getContentSize().width, + right_button->getContentSize().height * 0.625)); + right_button->setTouchEnabled(true); + right_button->setLocalZOrder(_layout->getLocalZOrder() + 1); + right_button->addTouchEventListener(CC_CALLBACK_2(UIScene_Editor::nextCallback, this)); + _layout->addChild(right_button); + +} + +bool UILayoutComponentTest_Editor::init() +{ + if (UIScene_Editor::init()) + { + Size screenSize = CCDirector::getInstance()->getWinSize(); + _layout = Layout::create(); + _layout->setContentSize(screenSize); + _touchGroup->addChild(_layout); + + Node* node = CSLoader::createNode("cocosui/UIEditorTest/UILayout/LayoutComponent/Scene.csb"); + node->setContentSize(screenSize); + _layout->addChild(node); + this->configureGUIScene(); + + ui::Helper::doLayout(_layout); + + return true; + } + + return false; +} \ No newline at end of file diff --git a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UILayoutTest/UILayoutTest_Editor.h b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UILayoutTest/UILayoutTest_Editor.h index 5b0fb91c4f..52004b5d9d 100644 --- a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UILayoutTest/UILayoutTest_Editor.h +++ b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UILayoutTest/UILayoutTest_Editor.h @@ -144,4 +144,16 @@ protected: UI_SCENE_EDITOR_CREATE_FUNC(UILayoutTest_Layout_Relative_Location_Editor) }; +class UILayoutComponentTest_Editor : public UIScene_Editor +{ +public: + UILayoutComponentTest_Editor(); + ~UILayoutComponentTest_Editor(); + virtual bool init(); + void configureGUIScene(); +protected: + LayerColor* _baseLayer; + UI_SCENE_EDITOR_CREATE_FUNC(UILayoutComponentTest_Editor) +}; + #endif /* defined(__TestCpp__UILayoutTest_Editor__) */ diff --git a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UISceneManager_Editor.cpp b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UISceneManager_Editor.cpp index 2e9346bfed..b0106a96c3 100644 --- a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UISceneManager_Editor.cpp +++ b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UISceneManager_Editor.cpp @@ -36,12 +36,15 @@ static const char* s_testArray[] = "UILayoutTest_Gradient_Editor", "UILayoutTest_BackGroundImage_Editor", "UILayoutTest_BackGroundImage_Scale9_Editor", + "UILayoutComponentTest_Editor", + /* "UILayoutTest_Layout_Linear_Vertical_Editor", "UILayoutTest_Layout_Linear_Horizontal_Editor", "UILayoutTest_Layout_Relative_Align_Parent_Editor", "UILayoutTest_Layout_Relative_Location_Editor", */ + "UIListViewTest_Vertical_Editor", "UIListViewTest_Horizontal_Editor", "UIPageViewTest_Editor", @@ -148,7 +151,9 @@ Scene* UISceneManager_Editor::currentUIScene() case kUILayoutTest_BackGroundImage_Scale9_Editor: return UILayoutTest_BackGroundImage_Scale9_Editor::sceneWithTitle(s_testArray[_currentUISceneId]); - + + case kUILayoutComponentTest_Editor: + return UILayoutComponentTest_Editor::sceneWithTitle(s_testArray[_currentUISceneId]); /* case kUILayoutTest_Layout_Linear_Vertical_Editor: return UILayoutTest_Layout_Linear_Vertical_Editor::sceneWithTitle(s_testArray[_currentUISceneId]); @@ -162,6 +167,7 @@ Scene* UISceneManager_Editor::currentUIScene() case kUILayoutTest_Layout_Relative_Location_Editor: return UILayoutTest_Layout_Relative_Location_Editor::sceneWithTitle(s_testArray[_currentUISceneId]); */ + case kUIListViewTest_Vertical_Editor: return UIListViewTest_Vertical_Editor::sceneWithTitle(s_testArray[_currentUISceneId]); diff --git a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UISceneManager_Editor.h b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UISceneManager_Editor.h index c932da5dec..a754cc2e2b 100644 --- a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UISceneManager_Editor.h +++ b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UISceneManager_Editor.h @@ -46,12 +46,15 @@ enum kUILayoutTest_Gradient_Editor, kUILayoutTest_BackGroundImage_Editor, kUILayoutTest_BackGroundImage_Scale9_Editor, + kUILayoutComponentTest_Editor, + /* kUILayoutTest_Layout_Linear_Vertical_Editor, kUILayoutTest_Layout_Linear_Horizontal_Editor, kUILayoutTest_Layout_Relative_Align_Parent_Editor, kUILayoutTest_Layout_Relative_Location_Editor, */ + kUIListViewTest_Vertical_Editor, kUIListViewTest_Horizontal_Editor, kUIPageViewTest_Editor, diff --git a/tests/cpp-tests/Resources/ccs-res b/tests/cpp-tests/Resources/ccs-res index 1d8bede8e9..84e5fd6000 160000 --- a/tests/cpp-tests/Resources/ccs-res +++ b/tests/cpp-tests/Resources/ccs-res @@ -1 +1 @@ -Subproject commit 1d8bede8e900625bc47972c4a925763e43f70ba2 +Subproject commit 84e5fd6000d7439082f6a8b507f520a52c50ad4e From 0465ff83256bd2fa8493befe0eeec64ae6511c14 Mon Sep 17 00:00:00 2001 From: Liam Date: Mon, 22 Dec 2014 18:39:44 +0800 Subject: [PATCH 08/13] remote unuseful format --- cocos/ui/UILayoutComponent.cpp | 11 +++-------- cocos/ui/UILayoutComponent.h | 12 ++++-------- 2 files changed, 7 insertions(+), 16 deletions(-) diff --git a/cocos/ui/UILayoutComponent.cpp b/cocos/ui/UILayoutComponent.cpp index ddfcb6b56a..b6c572f6f0 100644 --- a/cocos/ui/UILayoutComponent.cpp +++ b/cocos/ui/UILayoutComponent.cpp @@ -125,7 +125,7 @@ namespace ui { _topMargin = parentSize.height - (ownerPoint.y + (1 - ownerAnchor.y) * ownerSize.height); } -#pragma region OldVersion + //OldVersion void LayoutComponent::setUsingPercentContentSize(bool isUsed) { _usingPercentWidth = _usingPercentHeight = isUsed; @@ -145,9 +145,8 @@ namespace ui { Vec2 vec2=Vec2(_percentWidth,_percentHeight); return vec2; } -#pragma endregion -#pragma region Position & Margin + //Position & Margin Point LayoutComponent::getAnchorPosition() { return _owner->getAnchorPoint(); @@ -340,9 +339,7 @@ namespace ui { _buttomMargin = margin; } -#pragma endregion - -#pragma region Size & Percent + //Size & Percent Size LayoutComponent::getSize() { return this->getOwner()->getContentSize(); @@ -508,8 +505,6 @@ namespace ui { } } -#pragma endregion - void LayoutComponent::refreshLayout() { Node* parent = this->getOwnerParent(); diff --git a/cocos/ui/UILayoutComponent.h b/cocos/ui/UILayoutComponent.h index fddc18e36d..478ba2fd9d 100644 --- a/cocos/ui/UILayoutComponent.h +++ b/cocos/ui/UILayoutComponent.h @@ -54,15 +54,15 @@ namespace ui { Top, Center }; -#pragma region OldVersion + + // OldVersion virtual void setUsingPercentContentSize(bool isUsed); virtual bool getUsingPercentContentSize(); virtual void setPercentContentSize(const Vec2 &percent); virtual Vec2 getPercentContentSize(); -#pragma endregion -#pragma region Position & Margin + // Position & Margin virtual Point getAnchorPosition(); virtual void setAnchorPosition(Point point); @@ -99,9 +99,7 @@ namespace ui { virtual float getButtomMargin(); virtual void setButtomMargin(float margin); -#pragma endregion - -#pragma region Size & Percent + // Size & Percent virtual Size getSize(); virtual void setSize(Size _size); @@ -129,8 +127,6 @@ namespace ui { virtual bool isUsingStretchHeight(); virtual void setStretchHeightEnabled(bool isUsed); -#pragma endregion - virtual void setActiveEnable(bool enable); virtual void refreshLayout(); From 39d0f183d478eab01b2d44d976428eeb4c302c6e Mon Sep 17 00:00:00 2001 From: Liam Date: Mon, 22 Dec 2014 19:05:31 +0800 Subject: [PATCH 09/13] update for code format --- cocos/ui/UILayoutComponent.cpp | 88 +++++++++++++++++----------------- cocos/ui/UILayoutComponent.h | 52 ++++++++++---------- 2 files changed, 70 insertions(+), 70 deletions(-) diff --git a/cocos/ui/UILayoutComponent.cpp b/cocos/ui/UILayoutComponent.cpp index b6c572f6f0..18334a6904 100644 --- a/cocos/ui/UILayoutComponent.cpp +++ b/cocos/ui/UILayoutComponent.cpp @@ -102,10 +102,10 @@ namespace ui { if (parent == nullptr) return; - Point ownerPoint = _owner->getPosition(); - Point ownerAnchor = _owner->getAnchorPoint(); - Size ownerSize = _owner->getContentSize(); - Size parentSize = parent->getContentSize(); + const Point& ownerPoint = _owner->getPosition(); + const Point& ownerAnchor = _owner->getAnchorPoint(); + const Size& ownerSize = _owner->getContentSize(); + const Size& parentSize = parent->getContentSize(); _leftMargin = ownerPoint.x - ownerAnchor.x * ownerSize.width; _rightMargin = parentSize.width - (ownerPoint.x + (1 - ownerAnchor.x) * ownerSize.width); @@ -116,10 +116,10 @@ namespace ui { if (parent == nullptr) return; - Point ownerPoint = _owner->getPosition(); - Point ownerAnchor = _owner->getAnchorPoint(); - Size ownerSize = _owner->getContentSize(); - Size parentSize = parent->getContentSize(); + const Point& ownerPoint = _owner->getPosition(); + const Point& ownerAnchor = _owner->getAnchorPoint(); + const Size& ownerSize = _owner->getContentSize(); + const Size& parentSize = parent->getContentSize(); _buttomMargin = ownerPoint.y - ownerAnchor.y * ownerSize.height; _topMargin = parentSize.height - (ownerPoint.y + (1 - ownerAnchor.y) * ownerSize.height); @@ -130,7 +130,7 @@ namespace ui { { _usingPercentWidth = _usingPercentHeight = isUsed; } - bool LayoutComponent::getUsingPercentContentSize() + bool LayoutComponent::getUsingPercentContentSize()const { return _usingPercentWidth && _usingPercentHeight; } @@ -140,18 +140,18 @@ namespace ui { this->setPercentWidth(percent.x); this->setPercentHeight(percent.y); } - Vec2 LayoutComponent::getPercentContentSize() + Vec2 LayoutComponent::getPercentContentSize()const { Vec2 vec2=Vec2(_percentWidth,_percentHeight); return vec2; } //Position & Margin - Point LayoutComponent::getAnchorPosition() + const Point& LayoutComponent::getAnchorPosition()const { return _owner->getAnchorPoint(); } - void LayoutComponent::setAnchorPosition(Point point) + void LayoutComponent::setAnchorPosition(const Point& point) { Rect oldRect = _owner->getBoundingBox(); _owner->setAnchorPoint(point); @@ -166,19 +166,19 @@ namespace ui { this->setPosition(ownerPosition); } - Point LayoutComponent::getPosition() + const Point& LayoutComponent::getPosition()const { return _owner->getPosition(); } - void LayoutComponent::setPosition(Point position) + void LayoutComponent::setPosition(const Point& position) { _owner->setPosition(position); Node* parent = this->getOwnerParent(); if (parent != nullptr) { - Point ownerPoint = _owner->getPosition(); - Size parentSize = parent->getContentSize(); + const Point& ownerPoint = _owner->getPosition(); + const Size& parentSize = parent->getContentSize(); if (parentSize.width != 0) _positionPercentX = ownerPoint.x / parentSize.width; @@ -195,7 +195,7 @@ namespace ui { } } - bool LayoutComponent::isUsingPositionPercentX() + bool LayoutComponent::isUsingPositionPercentX()const { return _usingPositionPercentX; } @@ -208,7 +208,7 @@ namespace ui { } } - float LayoutComponent::getPositionPercentX() + float LayoutComponent::getPositionPercentX()const { return _positionPercentX; } @@ -224,7 +224,7 @@ namespace ui { } } - bool LayoutComponent::isUsingPositionPercentY() + bool LayoutComponent::isUsingPositionPercentY()const { return _usingPositionPercentY; } @@ -237,7 +237,7 @@ namespace ui { } } - float LayoutComponent::getPositionPercentY() + float LayoutComponent::getPositionPercentY()const { return _positionPercentY; } @@ -253,7 +253,7 @@ namespace ui { } } - LayoutComponent::HorizontalEage LayoutComponent::getHorizontalEage() + LayoutComponent::HorizontalEage LayoutComponent::getHorizontalEage()const { return _horizontalEage; } @@ -268,7 +268,7 @@ namespace ui { Node* parent = this->getOwnerParent(); if (parent != nullptr) { - Size parentSize = parent->getContentSize(); + const Size& parentSize = parent->getContentSize(); if (parentSize.width != 0) _positionPercentX = _owner->getPosition().x / parentSize.width; else @@ -278,7 +278,7 @@ namespace ui { } } - LayoutComponent::VerticalEage LayoutComponent::getVerticalEage() + LayoutComponent::VerticalEage LayoutComponent::getVerticalEage()const { return _verticalEage; } @@ -293,7 +293,7 @@ namespace ui { Node* parent = this->getOwnerParent(); if (parent != nullptr) { - Size parentSize = parent->getContentSize(); + const Size& parentSize = parent->getContentSize(); if (parentSize.height != 0) _positionPercentY = _owner->getPosition().y / parentSize.height; else @@ -303,7 +303,7 @@ namespace ui { } } - float LayoutComponent::getLeftMargin() + float LayoutComponent::getLeftMargin()const { return _leftMargin; } @@ -312,7 +312,7 @@ namespace ui { _leftMargin = margin; } - float LayoutComponent::getRightMargin() + float LayoutComponent::getRightMargin()const { return _rightMargin; } @@ -321,7 +321,7 @@ namespace ui { _rightMargin = margin; } - float LayoutComponent::getTopMargin() + float LayoutComponent::getTopMargin()const { return _topMargin; } @@ -330,7 +330,7 @@ namespace ui { _topMargin = margin; } - float LayoutComponent::getButtomMargin() + float LayoutComponent::getButtomMargin()const { return _buttomMargin; } @@ -340,19 +340,19 @@ namespace ui { } //Size & Percent - Size LayoutComponent::getSize() + const Size& LayoutComponent::getSize()const { return this->getOwner()->getContentSize(); } - void LayoutComponent::setSize(Size _size) + void LayoutComponent::setSize(const Size& _size) { _owner->setContentSize(_size); Node* parent = this->getOwnerParent(); if (parent != nullptr) { - Size ownerSize = _owner->getContentSize(); - Size parentSize = parent->getContentSize(); + const Size& ownerSize = _owner->getContentSize(); + const Size& parentSize = parent->getContentSize(); if (parentSize.width != 0) _percentWidth = ownerSize.width / parentSize.width; @@ -369,7 +369,7 @@ namespace ui { } } - bool LayoutComponent::isUsingPercentWidth() + bool LayoutComponent::isUsingPercentWidth()const { return _usingPercentWidth; } @@ -382,7 +382,7 @@ namespace ui { } } - float LayoutComponent::getSizeWidth() + float LayoutComponent::getSizeWidth()const { return _owner->getContentSize().width; } @@ -395,7 +395,7 @@ namespace ui { Node* parent = this->getOwnerParent(); if (parent != nullptr) { - Size parentSize = parent->getContentSize(); + const Size& parentSize = parent->getContentSize(); if (parentSize.width != 0) _percentWidth = ownerSize.width / parentSize.width; else @@ -405,7 +405,7 @@ namespace ui { } } - float LayoutComponent::getPercentWidth() + float LayoutComponent::getPercentWidth()const { return _percentWidth; } @@ -424,7 +424,7 @@ namespace ui { } } - bool LayoutComponent::isUsingPercentHeight() + bool LayoutComponent::isUsingPercentHeight()const { return _usingPercentHeight; } @@ -437,7 +437,7 @@ namespace ui { } } - float LayoutComponent::getSizeHeight() + float LayoutComponent::getSizeHeight()const { return _owner->getContentSize().height; } @@ -450,7 +450,7 @@ namespace ui { Node* parent = this->getOwnerParent(); if (parent != nullptr) { - Size parentSize = parent->getContentSize(); + const Size& parentSize = parent->getContentSize(); if (parentSize.height != 0) _percentHeight = ownerSize.height / parentSize.height; else @@ -460,7 +460,7 @@ namespace ui { } } - float LayoutComponent::getPercentHeight() + float LayoutComponent::getPercentHeight()const { return _percentHeight; } @@ -479,7 +479,7 @@ namespace ui { } } - bool LayoutComponent::isUsingStretchWidth() + bool LayoutComponent::isUsingStretchWidth()const { return _usingStretchWidth; } @@ -492,7 +492,7 @@ namespace ui { } } - bool LayoutComponent::isUsingStretchHeight() + bool LayoutComponent::isUsingStretchHeight()const { return _usingStretchHeight; } @@ -511,9 +511,9 @@ namespace ui { if (parent == nullptr) return; - Size parentSize = parent->getContentSize(); + const Size& parentSize = parent->getContentSize(); + const Point& ownerAnchor = _owner->getAnchorPoint(); Size ownerSize = _owner->getContentSize(); - Point ownerAnchor = _owner->getAnchorPoint(); Point ownerPosition = _owner->getPosition(); switch (this->_horizontalEage) diff --git a/cocos/ui/UILayoutComponent.h b/cocos/ui/UILayoutComponent.h index 478ba2fd9d..96ba842ed0 100644 --- a/cocos/ui/UILayoutComponent.h +++ b/cocos/ui/UILayoutComponent.h @@ -57,74 +57,74 @@ namespace ui { // OldVersion virtual void setUsingPercentContentSize(bool isUsed); - virtual bool getUsingPercentContentSize(); + virtual bool getUsingPercentContentSize()const; virtual void setPercentContentSize(const Vec2 &percent); - virtual Vec2 getPercentContentSize(); + virtual Vec2 getPercentContentSize()const; // Position & Margin - virtual Point getAnchorPosition(); - virtual void setAnchorPosition(Point point); + virtual const Point& getAnchorPosition()const; + virtual void setAnchorPosition(const Point& point); - virtual Point getPosition(); - virtual void setPosition(Point position); + virtual const Point& getPosition()const; + virtual void setPosition(const Point& position); - virtual bool isUsingPositionPercentX(); + virtual bool isUsingPositionPercentX()const; virtual void setPositionPercentXEnabled(bool isUsed); - virtual float getPositionPercentX(); + virtual float getPositionPercentX()const; virtual void setPositionPercentX(float percentMargin); - virtual bool isUsingPositionPercentY(); + virtual bool isUsingPositionPercentY()const; virtual void setPositionPercentYEnabled(bool isUsed); - virtual float getPositionPercentY(); + virtual float getPositionPercentY()const; virtual void setPositionPercentY(float percentMargin); - virtual HorizontalEage getHorizontalEage(); + virtual HorizontalEage getHorizontalEage()const; virtual void setHorizontalEage(HorizontalEage hEage); - virtual VerticalEage getVerticalEage(); + virtual VerticalEage getVerticalEage()const; virtual void setVerticalEage(VerticalEage vEage); - virtual float getLeftMargin(); + virtual float getLeftMargin()const; virtual void setLeftMargin(float margin); - virtual float getRightMargin(); + virtual float getRightMargin()const; virtual void setRightMargin(float margin); - virtual float getTopMargin(); + virtual float getTopMargin()const; virtual void setTopMargin(float margin); - virtual float getButtomMargin(); + virtual float getButtomMargin()const; virtual void setButtomMargin(float margin); // Size & Percent - virtual Size getSize(); - virtual void setSize(Size _size); + virtual const Size& getSize()const; + virtual void setSize(const Size& _size); - virtual bool isUsingPercentWidth(); + virtual bool isUsingPercentWidth()const; virtual void setPercentWidthEnabled(bool isUsed); - virtual float getSizeWidth(); + virtual float getSizeWidth()const; virtual void setSizeWidth(float width); - virtual float getPercentWidth(); + virtual float getPercentWidth()const; virtual void setPercentWidth(float percentWidth); - virtual bool isUsingPercentHeight(); + virtual bool isUsingPercentHeight()const; virtual void setPercentHeightEnabled(bool isUsed); - virtual float getSizeHeight(); + virtual float getSizeHeight()const; virtual void setSizeHeight(float height); - virtual float getPercentHeight(); + virtual float getPercentHeight()const; virtual void setPercentHeight(float percentHeight); - virtual bool isUsingStretchWidth(); + virtual bool isUsingStretchWidth()const; virtual void setStretchWidthEnabled(bool isUsed); - virtual bool isUsingStretchHeight(); + virtual bool isUsingStretchHeight()const; virtual void setStretchHeightEnabled(bool isUsed); virtual void setActiveEnable(bool enable); From 815ea2a1a5795c208ea0785fa8306513028726bd Mon Sep 17 00:00:00 2001 From: Liam Date: Tue, 23 Dec 2014 20:29:53 +0800 Subject: [PATCH 10/13] update for code format --- .../cocostudio/FlatBuffersSerialize.cpp | 13 ++ .../WidgetReader/NodeReader/NodeReader.cpp | 54 +++-- .../cocostudio/WidgetReader/WidgetReader.cpp | 60 ++++-- cocos/ui/UIHelper.h | 2 +- cocos/ui/UILayoutComponent.cpp | 26 +-- cocos/ui/UILayoutComponent.h | 100 ++++----- cocos/ui/UIWidget.cpp | 197 ++++++++++++++---- cocos/ui/UIWidget.h | 11 + 8 files changed, 325 insertions(+), 138 deletions(-) diff --git a/cocos/editor-support/cocostudio/FlatBuffersSerialize.cpp b/cocos/editor-support/cocostudio/FlatBuffersSerialize.cpp index 615973aee7..c0f486c3d8 100644 --- a/cocos/editor-support/cocostudio/FlatBuffersSerialize.cpp +++ b/cocos/editor-support/cocostudio/FlatBuffersSerialize.cpp @@ -78,6 +78,19 @@ static const char* FrameType_TextureFrame = "TextureFrame"; static const char* FrameType_EventFrame = "EventFrame"; static const char* FrameType_ZOrderFrame = "ZOrderFrame"; +static const char* Layout_PositionPercentXEnabled = "PositionPercentXEnable"; +static const char* Layout_PositionPercentYEnabled = "PositionPercentYEnable"; +static const char* Layout_PercentWidthEnable = "PercentWidthEnable"; +static const char* Layout_PercentHeightEnable = "PercentHeightEnable"; +static const char* Layout_StretchWidthEnable = "StretchWidthEnable"; +static const char* Layout_StretchHeightEnable = "StretchHeightEnable"; +static const char* Layout_HorizontalEdge = "HorizontalEdge"; +static const char* Layout_VerticalEdge = "VerticalEdge"; +static const char* Layout_LeftMargin = "LeftMargin"; +static const char* Layout_RightMargin = "RightMargin"; +static const char* Layout_TopMargin = "TopMargin"; +static const char* Layout_BottomMargin = "BottomMargin"; + static FlatBuffersSerialize* _instanceFlatBuffersSerialize = nullptr; FlatBuffersSerialize::FlatBuffersSerialize() diff --git a/cocos/editor-support/cocostudio/WidgetReader/NodeReader/NodeReader.cpp b/cocos/editor-support/cocostudio/WidgetReader/NodeReader/NodeReader.cpp index 2270cc66bb..31dc514613 100644 --- a/cocos/editor-support/cocostudio/WidgetReader/NodeReader/NodeReader.cpp +++ b/cocos/editor-support/cocostudio/WidgetReader/NodeReader/NodeReader.cpp @@ -36,6 +36,24 @@ using namespace flatbuffers; namespace cocostudio { + const char* Layout_PositionPercentXEnabled = "PositionPercentXEnable"; + const char* Layout_PositionPercentYEnabled = "PositionPercentYEnable"; + const char* Layout_PercentWidthEnable = "PercentWidthEnable"; + const char* Layout_PercentHeightEnable = "PercentHeightEnable"; + const char* Layout_StretchWidthEnable = "StretchWidthEnable"; + const char* Layout_StretchHeightEnable = "StretchHeightEnable"; + const char* Layout_HorizontalEdge = "HorizontalEdge"; + const char* Layout_VerticalEdge = "VerticalEdge"; + const char* Layout_LeftMargin = "LeftMargin"; + const char* Layout_RightMargin = "RightMargin"; + const char* Layout_TopMargin = "TopMargin"; + const char* Layout_BottomMargin = "BottomMargin"; + const char* Layout_BothEdge = "BothEdge"; + const char* Layout_LeftEdge = "LeftEdge"; + const char* Layout_RightEdge = "RightEdge"; + const char* Layout_TopEdge = "TopEdge"; + const char* Layout_BottomEdge = "BottomEdge"; + IMPLEMENT_CLASS_NODE_READER_INFO(NodeReader) NodeReader::NodeReader() @@ -168,51 +186,51 @@ namespace cocostudio { frameEvent = value; } - else if (attriname == "PositionPrecentXEnabled") + else if (attriname == Layout_PositionPercentXEnabled) { positionXPercentEnabled = value == "True"; } - else if (attriname == "PositionPrecentYEnabled") + else if (attriname == Layout_PositionPercentYEnabled) { positionYPercentEnabled = value == "True"; } - else if (attriname == "PercentWidthEnable") + else if (attriname == Layout_PercentWidthEnable) { sizeXPercentEnable = value == "True"; } - else if (attriname == "PercentHeightEnbale") + else if (attriname == Layout_PercentHeightEnable) { sizeYPercentEnable = value == "True"; } - else if (attriname == "StretchWidthEnable") + else if (attriname == Layout_StretchWidthEnable) { stretchHorizontalEnabled = value == "True"; } - else if (attriname == "StretchHeightEnable") + else if (attriname == Layout_StretchHeightEnable) { stretchVerticalEnabled = value == "True"; } - else if (attriname == "HorizontalEage") + else if (attriname == Layout_HorizontalEdge) { horizontalEdge = value; } - else if (attriname == "VerticalEage") + else if (attriname == Layout_VerticalEdge) { verticalEdge = value; } - else if (attriname == "LeftMargin") + else if (attriname == Layout_LeftMargin) { leftMargin = atof(value.c_str()); } - else if (attriname == "RightMargin") + else if (attriname == Layout_RightMargin) { rightMargin = atof(value.c_str()); } - else if (attriname == "TopMargin") + else if (attriname == Layout_TopMargin) { topMargin = atof(value.c_str()); } - else if (attriname == "ButtomMargin") + else if (attriname == Layout_BottomMargin) { bottomMargin = atof(value.c_str()); } @@ -532,29 +550,29 @@ namespace cocostudio layoutComponent->setStretchWidthEnabled(stretchHorizontalEnabled); layoutComponent->setStretchHeightEnabled(stretchVerticalEnabled); ui::LayoutComponent::HorizontalEage horizontalEdgeType = ui::LayoutComponent::HorizontalEage::None; - if (horizontalEdge == "LeftEage") + if (horizontalEdge == Layout_LeftEdge) { horizontalEdgeType = ui::LayoutComponent::HorizontalEage::Left; } - else if (horizontalEdge == "RightEage") + else if (horizontalEdge == Layout_RightEdge) { horizontalEdgeType = ui::LayoutComponent::HorizontalEage::Right; } - else if (horizontalEdge == "BothEage") + else if (horizontalEdge == Layout_BothEdge) { horizontalEdgeType = ui::LayoutComponent::HorizontalEage::Center; } layoutComponent->setHorizontalEage(horizontalEdgeType); ui::LayoutComponent::VerticalEage verticalEdgeType = ui::LayoutComponent::VerticalEage::None; - if (verticalEdge == "TopEage") + if (verticalEdge == Layout_TopEdge) { verticalEdgeType = ui::LayoutComponent::VerticalEage::Top; } - else if (verticalEdge == "ButtomEage") + else if (verticalEdge == Layout_BottomEdge) { verticalEdgeType = ui::LayoutComponent::VerticalEage::Bottom; } - else if (verticalEdge == "BothEage") + else if (verticalEdge == Layout_BothEdge) { verticalEdgeType = ui::LayoutComponent::VerticalEage::Center; } diff --git a/cocos/editor-support/cocostudio/WidgetReader/WidgetReader.cpp b/cocos/editor-support/cocostudio/WidgetReader/WidgetReader.cpp index 4e4d8397ae..55e4ff103b 100644 --- a/cocos/editor-support/cocostudio/WidgetReader/WidgetReader.cpp +++ b/cocos/editor-support/cocostudio/WidgetReader/WidgetReader.cpp @@ -10,7 +10,7 @@ #include "tinyxml2.h" #include "flatbuffers/flatbuffers.h" #include "ui/UILayoutComponent.h" -#include "../ActionTimeline/CSLoader.h" +#include "cocostudio/ActionTimeline/CSLoader.h" USING_NS_CC; using namespace ui; @@ -22,7 +22,6 @@ using namespace flatbuffers; namespace cocostudio { - const char* P_IgnoreSize = "ignoreSize"; const char* P_SizeType = "sizeType"; const char* P_PositionType = "positionType"; @@ -68,6 +67,24 @@ namespace cocostudio const char* P_ResourceType = "resourceType"; const char* P_Path = "path"; + const char* P_Layout_PositionPercentXEnabled = "PositionPercentXEnable"; + const char* P_Layout_PositionPercentYEnabled = "PositionPercentYEnable"; + const char* P_Layout_PercentWidthEnable = "PercentWidthEnable"; + const char* P_Layout_PercentHeightEnable = "PercentHeightEnable"; + const char* P_Layout_StretchWidthEnable = "StretchWidthEnable"; + const char* P_Layout_StretchHeightEnable = "StretchHeightEnable"; + const char* P_Layout_HorizontalEdge = "HorizontalEdge"; + const char* P_Layout_VerticalEdge = "VerticalEdge"; + const char* P_Layout_LeftMargin = "LeftMargin"; + const char* P_Layout_RightMargin = "RightMargin"; + const char* P_Layout_TopMargin = "TopMargin"; + const char* P_Layout_BottomMargin = "BottomMargin"; + const char* P_Layout_BothEdge = "BothEdge"; + const char* P_Layout_LeftEdge = "LeftEdge"; + const char* P_Layout_RightEdge = "RightEdge"; + const char* P_Layout_TopEdge = "TopEdge"; + const char* P_Layout_BottomEdge = "BottomEdge"; + static WidgetReader* instanceWidgetReader = nullptr; @@ -475,51 +492,51 @@ namespace cocostudio { callbackName = value; } - else if (attriname == "PositionPrecentXEnabled") + else if (attriname == P_Layout_PositionPercentXEnabled) { positionXPercentEnabled = value == "True"; } - else if (attriname == "PositionPrecentYEnabled") + else if (attriname == P_Layout_PositionPercentYEnabled) { positionYPercentEnabled = value == "True"; } - else if (attriname == "PercentWidthEnable") + else if (attriname == P_Layout_PercentWidthEnable) { sizeXPercentEnable = value == "True"; } - else if (attriname == "PercentHeightEnbale") + else if (attriname == P_Layout_PercentHeightEnable) { sizeYPercentEnable = value == "True"; } - else if (attriname == "StretchWidthEnable") + else if (attriname == P_Layout_StretchWidthEnable) { stretchHorizontalEnabled = value == "True"; } - else if (attriname == "StretchHeightEnable") + else if (attriname == P_Layout_StretchHeightEnable) { stretchVerticalEnabled = value == "True"; } - else if (attriname == "HorizontalEage") + else if (attriname == P_Layout_HorizontalEdge) { horizontalEdge = value; } - else if (attriname == "VerticalEage") + else if (attriname == P_Layout_VerticalEdge) { verticalEdge = value; } - else if (attriname == "LeftMargin") + else if (attriname == P_Layout_LeftMargin) { leftMargin = atof(value.c_str()); } - else if (attriname == "RightMargin") + else if (attriname == P_Layout_RightMargin) { rightMargin = atof(value.c_str()); } - else if (attriname == "TopMargin") + else if (attriname == P_Layout_TopMargin) { topMargin = atof(value.c_str()); } - else if (attriname == "ButtomMargin") + else if (attriname == P_Layout_BottomMargin) { bottomMargin = atof(value.c_str()); } @@ -779,7 +796,10 @@ namespace cocostudio } if (!(v1 <= 2 && v2 == 0 && v3 <= 6)) + { widget->setUnifySizeEnabled(false); + widget->setLayoutComponentEnabled(true); + } } bool ignoreSize = options->ignoreSize(); @@ -882,29 +902,29 @@ namespace cocostudio layoutComponent->setStretchWidthEnabled(stretchHorizontalEnabled); layoutComponent->setStretchHeightEnabled(stretchVerticalEnabled); ui::LayoutComponent::HorizontalEage horizontalEdgeType = ui::LayoutComponent::HorizontalEage::None; - if (horizontalEdge == "LeftEage") + if (horizontalEdge == P_Layout_LeftEdge) { horizontalEdgeType = ui::LayoutComponent::HorizontalEage::Left; } - else if (horizontalEdge == "RightEage") + else if (horizontalEdge == P_Layout_RightEdge) { horizontalEdgeType = ui::LayoutComponent::HorizontalEage::Right; } - else if (horizontalEdge == "BothEage") + else if (horizontalEdge == P_Layout_BothEdge) { horizontalEdgeType = ui::LayoutComponent::HorizontalEage::Center; } layoutComponent->setHorizontalEage(horizontalEdgeType); ui::LayoutComponent::VerticalEage verticalEdgeType = ui::LayoutComponent::VerticalEage::None; - if (verticalEdge == "TopEage") + if (verticalEdge == P_Layout_TopEdge) { verticalEdgeType = ui::LayoutComponent::VerticalEage::Top; } - else if (verticalEdge == "ButtomEage") + else if (verticalEdge == P_Layout_BottomEdge) { verticalEdgeType = ui::LayoutComponent::VerticalEage::Bottom; } - else if (verticalEdge == "BothEage") + else if (verticalEdge == P_Layout_BothEdge) { verticalEdgeType = ui::LayoutComponent::VerticalEage::Center; } diff --git a/cocos/ui/UIHelper.h b/cocos/ui/UIHelper.h index 8a48f5126b..0d5e287b7f 100644 --- a/cocos/ui/UIHelper.h +++ b/cocos/ui/UIHelper.h @@ -80,7 +80,7 @@ public: std::string::size_type length); /** - * Refresh object and it's children lay out state + * Refresh object and it's children layout state * *@param rootNode object which will be changed * diff --git a/cocos/ui/UILayoutComponent.cpp b/cocos/ui/UILayoutComponent.cpp index 18334a6904..72def52f39 100644 --- a/cocos/ui/UILayoutComponent.cpp +++ b/cocos/ui/UILayoutComponent.cpp @@ -35,19 +35,19 @@ namespace ui { , _verticalEage(VerticalEage::None) , _leftMargin(0) , _rightMargin(0) - , _usingPositionPercentX(false) - , _positionPercentX(0) , _buttomMargin(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) - ,_usingStretchWidth(false) - , _usingStretchHeight(false) { _name = __LAYOUT_COMPONENT_NAME; } @@ -195,7 +195,7 @@ namespace ui { } } - bool LayoutComponent::isUsingPositionPercentX()const + bool LayoutComponent::isPositionPercentXEnabled()const { return _usingPositionPercentX; } @@ -224,7 +224,7 @@ namespace ui { } } - bool LayoutComponent::isUsingPositionPercentY()const + bool LayoutComponent::isPositionPercentYEnabled()const { return _usingPositionPercentY; } @@ -344,9 +344,9 @@ namespace ui { { return this->getOwner()->getContentSize(); } - void LayoutComponent::setSize(const Size& _size) + void LayoutComponent::setSize(const Size& size) { - _owner->setContentSize(_size); + _owner->setContentSize(size); Node* parent = this->getOwnerParent(); if (parent != nullptr) @@ -369,7 +369,7 @@ namespace ui { } } - bool LayoutComponent::isUsingPercentWidth()const + bool LayoutComponent::isPercentWidthEnabled()const { return _usingPercentWidth; } @@ -424,7 +424,7 @@ namespace ui { } } - bool LayoutComponent::isUsingPercentHeight()const + bool LayoutComponent::isPercentHeightEnabled()const { return _usingPercentHeight; } @@ -479,7 +479,7 @@ namespace ui { } } - bool LayoutComponent::isUsingStretchWidth()const + bool LayoutComponent::isStretchWidthEnabled()const { return _usingStretchWidth; } @@ -492,7 +492,7 @@ namespace ui { } } - bool LayoutComponent::isUsingStretchHeight()const + bool LayoutComponent::isStretchHeightEnabled()const { return _usingStretchHeight; } @@ -600,7 +600,7 @@ namespace ui { ui::Helper::doLayout(_owner); } - void LayoutComponent::setActiveEnable(bool enable) + void LayoutComponent::setActiveEnabled(bool enable) { _actived = enable; } diff --git a/cocos/ui/UILayoutComponent.h b/cocos/ui/UILayoutComponent.h index 96ba842ed0..aeab6c1c8b 100644 --- a/cocos/ui/UILayoutComponent.h +++ b/cocos/ui/UILayoutComponent.h @@ -56,84 +56,84 @@ namespace ui { }; // OldVersion - virtual void setUsingPercentContentSize(bool isUsed); - virtual bool getUsingPercentContentSize()const; + void setUsingPercentContentSize(bool isUsed); + bool getUsingPercentContentSize()const; - virtual void setPercentContentSize(const Vec2 &percent); - virtual Vec2 getPercentContentSize()const; + void setPercentContentSize(const Vec2 &percent); + Vec2 getPercentContentSize()const; // Position & Margin - virtual const Point& getAnchorPosition()const; - virtual void setAnchorPosition(const Point& point); + const Point& getAnchorPosition()const; + void setAnchorPosition(const Point& point); - virtual const Point& getPosition()const; - virtual void setPosition(const Point& position); + const Point& getPosition()const; + void setPosition(const Point& position); - virtual bool isUsingPositionPercentX()const; - virtual void setPositionPercentXEnabled(bool isUsed); + bool isPositionPercentXEnabled()const; + void setPositionPercentXEnabled(bool isUsed); - virtual float getPositionPercentX()const; - virtual void setPositionPercentX(float percentMargin); + float getPositionPercentX()const; + void setPositionPercentX(float percentMargin); - virtual bool isUsingPositionPercentY()const; - virtual void setPositionPercentYEnabled(bool isUsed); + bool isPositionPercentYEnabled()const; + void setPositionPercentYEnabled(bool isUsed); - virtual float getPositionPercentY()const; - virtual void setPositionPercentY(float percentMargin); + float getPositionPercentY()const; + void setPositionPercentY(float percentMargin); - virtual HorizontalEage getHorizontalEage()const; - virtual void setHorizontalEage(HorizontalEage hEage); + HorizontalEage getHorizontalEage()const; + void setHorizontalEage(HorizontalEage hEage); - virtual VerticalEage getVerticalEage()const; - virtual void setVerticalEage(VerticalEage vEage); + VerticalEage getVerticalEage()const; + void setVerticalEage(VerticalEage vEage); - virtual float getLeftMargin()const; - virtual void setLeftMargin(float margin); + float getLeftMargin()const; + void setLeftMargin(float margin); - virtual float getRightMargin()const; - virtual void setRightMargin(float margin); + float getRightMargin()const; + void setRightMargin(float margin); - virtual float getTopMargin()const; - virtual void setTopMargin(float margin); + float getTopMargin()const; + void setTopMargin(float margin); - virtual float getButtomMargin()const; - virtual void setButtomMargin(float margin); + float getButtomMargin()const; + void setButtomMargin(float margin); // Size & Percent - virtual const Size& getSize()const; - virtual void setSize(const Size& _size); + const Size& getSize()const; + void setSize(const Size& size); - virtual bool isUsingPercentWidth()const; - virtual void setPercentWidthEnabled(bool isUsed); + bool isPercentWidthEnabled()const; + void setPercentWidthEnabled(bool isUsed); - virtual float getSizeWidth()const; - virtual void setSizeWidth(float width); + float getSizeWidth()const; + void setSizeWidth(float width); - virtual float getPercentWidth()const; - virtual void setPercentWidth(float percentWidth); + float getPercentWidth()const; + void setPercentWidth(float percentWidth); - virtual bool isUsingPercentHeight()const; - virtual void setPercentHeightEnabled(bool isUsed); + bool isPercentHeightEnabled()const; + void setPercentHeightEnabled(bool isUsed); - virtual float getSizeHeight()const; - virtual void setSizeHeight(float height); + float getSizeHeight()const; + void setSizeHeight(float height); - virtual float getPercentHeight()const; - virtual void setPercentHeight(float percentHeight); + float getPercentHeight()const; + void setPercentHeight(float percentHeight); - virtual bool isUsingStretchWidth()const; - virtual void setStretchWidthEnabled(bool isUsed); + bool isStretchWidthEnabled()const; + void setStretchWidthEnabled(bool isUsed); - virtual bool isUsingStretchHeight()const; - virtual void setStretchHeightEnabled(bool isUsed); + bool isStretchHeightEnabled()const; + void setStretchHeightEnabled(bool isUsed); - virtual void setActiveEnable(bool enable); - virtual void refreshLayout(); + void setActiveEnabled(bool enable); + void refreshLayout(); protected: Node* getOwnerParent(); - virtual void refreshHorizontalMargin(); - virtual void refreshVerticalMargin(); + void refreshHorizontalMargin(); + void refreshVerticalMargin(); protected: HorizontalEage _horizontalEage; VerticalEage _verticalEage; diff --git a/cocos/ui/UIWidget.cpp b/cocos/ui/UIWidget.cpp index eb4f996e14..41e58d1713 100644 --- a/cocos/ui/UIWidget.cpp +++ b/cocos/ui/UIWidget.cpp @@ -136,6 +136,7 @@ Widget* Widget::_focusedWidget = nullptr; Widget::FocusNavigationController* Widget::_focusNavigationController = nullptr; Widget::Widget(): +_usingLayoutComponent(false), _unifySize(false), _enabled(true), _bright(true), @@ -222,6 +223,8 @@ bool Widget::init() void Widget::onEnter() { + if (!_usingLayoutComponent) + updateSizeAndPosition(); ProtectedNode::onEnter(); } @@ -280,7 +283,30 @@ void Widget::setContentSize(const cocos2d::Size &contentSize) { _contentSize = getVirtualRendererSize(); } - + if (!_usingLayoutComponent && _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(); } @@ -291,26 +317,38 @@ void Widget::setSize(const Size &size) void Widget::setSizePercent(const Vec2 &percent) { - 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) + if (_usingLayoutComponent) { + auto component = this->getOrCreateLayoutComponent(); component->setUsingPercentContentSize(true); + component->setPercentContentSize(percent); + component->refreshLayout(); } else { - component->setUsingPercentContentSize(false); + _sizePercent = percent; + Size cSize = _customSize; + if (_running) + { + 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()); + } + else + { + this->setContentSize(cSize); + } + _customSize = cSize; } } @@ -393,6 +431,24 @@ void Widget::updateSizeAndPosition(const cocos2d::Size &parentSize) setPosition(absPos); } +void Widget::setSizeType(SizeType type) +{ + _sizeType = type; + + if (_usingLayoutComponent) + { + auto component = this->getOrCreateLayoutComponent(); + + if (_sizeType == Widget::SizeType::PERCENT) + { + component->setUsingPercentContentSize(true); + } + else + { + component->setUsingPercentContentSize(false); + } + } +} Widget::SizeType Widget::getSizeType() const { return _sizeType; @@ -438,9 +494,13 @@ const Size& Widget::getCustomSize() const const Vec2& Widget::getSizePercent() { - auto component = this->getOrCreateLayoutComponent(); - - return component->getPercentContentSize(); + if (_usingLayoutComponent) + { + auto component = this->getOrCreateLayoutComponent(); + return component->getPercentContentSize(); + } + else + return _sizePercent; } Vec2 Widget::getWorldPosition()const @@ -455,6 +515,17 @@ Node* Widget::getVirtualRenderer() void Widget::onSizeChanged() { + if (!_usingLayoutComponent) + { + for (auto& child : getChildren()) + { + Widget* widgetChild = dynamic_cast(child); + if (widgetChild) + { + widgetChild->updateSizeAndPosition(); + } + } + } } Size Widget::getVirtualRendererSize() const @@ -911,39 +982,81 @@ void Widget::interceptTouchEvent(cocos2d::ui::Widget::TouchEventType event, coco void Widget::setPosition(const Vec2 &pos) { + if (!_usingLayoutComponent && _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) { - auto component = this->getOrCreateLayoutComponent(); - component->setPositionPercentX(percent.x); - component->setPositionPercentY(percent.y); - component->refreshLayout(); + if (_usingLayoutComponent) + { + auto component = this->getOrCreateLayoutComponent(); + component->setPositionPercentX(percent.x); + component->setPositionPercentY(percent.y); + component->refreshLayout(); + } + else + { + _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); + } + } + } } Vec2 Widget::getPositionPercent(){ - auto component = this->getOrCreateLayoutComponent(); - float percentX = component->getPositionPercentX(); - float percentY = component->getPositionPercentY(); + if (_usingLayoutComponent) + { + auto component = this->getOrCreateLayoutComponent(); + float percentX = component->getPositionPercentX(); + float percentY = component->getPositionPercentY(); - return Vec2(percentX,percentY); + return Vec2(percentX, percentY); + } + else + return _positionPercent; } void Widget::setPositionType(PositionType type) { - auto component = this->getOrCreateLayoutComponent(); _positionType = type; - if (type == Widget::PositionType::ABSOLUTE) + + if (_usingLayoutComponent) { - component->setPositionPercentXEnabled(false); - component->setPositionPercentYEnabled(false); - } - else - { - component->setPositionPercentXEnabled(true); - component->setPositionPercentYEnabled(true); + auto component = this->getOrCreateLayoutComponent(); + if (type == Widget::PositionType::ABSOLUTE) + { + component->setPositionPercentXEnabled(false); + component->setPositionPercentYEnabled(false); + } + else + { + component->setPositionPercentXEnabled(true); + component->setPositionPercentYEnabled(true); + } } } @@ -1336,5 +1449,17 @@ void Widget::setUnifySizeEnabled(bool enable) } +void Widget::setLayoutComponentEnabled(bool enable) +{ + _usingLayoutComponent = enable; +} + +bool Widget::isLayoutComponentEnabled()const +{ + return _usingLayoutComponent; +} + + + } NS_CC_END diff --git a/cocos/ui/UIWidget.h b/cocos/ui/UIWidget.h index b82a7f6104..eb030d7ad3 100644 --- a/cocos/ui/UIWidget.h +++ b/cocos/ui/UIWidget.h @@ -635,6 +635,16 @@ public: void setCallbackType(const std::string& callbackType) { _callbackType = callbackType; } const std::string& getCallbackType() const{ return _callbackType; } + /** + *@param enable Layout Component of a widget + *@return void + */ + void setLayoutComponentEnabled(bool enable); + /** + *@return true represent the widget use Layout Component, false represent the widget couldn't use Layout Component. + */ + bool isLayoutComponentEnabled()const; + CC_CONSTRUCTOR_ACCESS: //initializes state of widget. @@ -711,6 +721,7 @@ protected: LayoutComponent* getOrCreateLayoutComponent(); protected: + bool _usingLayoutComponent; bool _unifySize; bool _enabled; bool _bright; From 33bd5e3f4cde90059fd3adca7847b9de9cc14afb Mon Sep 17 00:00:00 2001 From: Liam Date: Tue, 23 Dec 2014 20:36:18 +0800 Subject: [PATCH 11/13] remove unused code --- .../cocostudio/FlatBuffersSerialize.cpp | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/cocos/editor-support/cocostudio/FlatBuffersSerialize.cpp b/cocos/editor-support/cocostudio/FlatBuffersSerialize.cpp index c0f486c3d8..615973aee7 100644 --- a/cocos/editor-support/cocostudio/FlatBuffersSerialize.cpp +++ b/cocos/editor-support/cocostudio/FlatBuffersSerialize.cpp @@ -78,19 +78,6 @@ static const char* FrameType_TextureFrame = "TextureFrame"; static const char* FrameType_EventFrame = "EventFrame"; static const char* FrameType_ZOrderFrame = "ZOrderFrame"; -static const char* Layout_PositionPercentXEnabled = "PositionPercentXEnable"; -static const char* Layout_PositionPercentYEnabled = "PositionPercentYEnable"; -static const char* Layout_PercentWidthEnable = "PercentWidthEnable"; -static const char* Layout_PercentHeightEnable = "PercentHeightEnable"; -static const char* Layout_StretchWidthEnable = "StretchWidthEnable"; -static const char* Layout_StretchHeightEnable = "StretchHeightEnable"; -static const char* Layout_HorizontalEdge = "HorizontalEdge"; -static const char* Layout_VerticalEdge = "VerticalEdge"; -static const char* Layout_LeftMargin = "LeftMargin"; -static const char* Layout_RightMargin = "RightMargin"; -static const char* Layout_TopMargin = "TopMargin"; -static const char* Layout_BottomMargin = "BottomMargin"; - static FlatBuffersSerialize* _instanceFlatBuffersSerialize = nullptr; FlatBuffersSerialize::FlatBuffersSerialize() From 83fcf74882ec1b0621f47bd80861025fe7736946 Mon Sep 17 00:00:00 2001 From: Liam Date: Wed, 24 Dec 2014 10:56:01 +0800 Subject: [PATCH 12/13] update for code format --- .../WidgetReader/NodeReader/NodeReader.cpp | 22 +-- .../cocostudio/WidgetReader/WidgetReader.cpp | 22 +-- cocos/ui/UILayoutComponent.cpp | 140 ++++++++++++------ cocos/ui/UILayoutComponent.h | 26 ++-- .../UILayoutTest/UILayoutTest.cpp | 52 +++---- .../UILayoutTest/UILayoutTest_Editor.cpp | 6 +- 6 files changed, 157 insertions(+), 111 deletions(-) diff --git a/cocos/editor-support/cocostudio/WidgetReader/NodeReader/NodeReader.cpp b/cocos/editor-support/cocostudio/WidgetReader/NodeReader/NodeReader.cpp index 31dc514613..9353f4f5fb 100644 --- a/cocos/editor-support/cocostudio/WidgetReader/NodeReader/NodeReader.cpp +++ b/cocos/editor-support/cocostudio/WidgetReader/NodeReader/NodeReader.cpp @@ -549,37 +549,37 @@ namespace cocostudio layoutComponent->setPercentHeight(sizeYPercent); layoutComponent->setStretchWidthEnabled(stretchHorizontalEnabled); layoutComponent->setStretchHeightEnabled(stretchVerticalEnabled); - ui::LayoutComponent::HorizontalEage horizontalEdgeType = ui::LayoutComponent::HorizontalEage::None; + ui::LayoutComponent::HorizontalEdge horizontalEdgeType = ui::LayoutComponent::HorizontalEdge::None; if (horizontalEdge == Layout_LeftEdge) { - horizontalEdgeType = ui::LayoutComponent::HorizontalEage::Left; + horizontalEdgeType = ui::LayoutComponent::HorizontalEdge::Left; } else if (horizontalEdge == Layout_RightEdge) { - horizontalEdgeType = ui::LayoutComponent::HorizontalEage::Right; + horizontalEdgeType = ui::LayoutComponent::HorizontalEdge::Right; } else if (horizontalEdge == Layout_BothEdge) { - horizontalEdgeType = ui::LayoutComponent::HorizontalEage::Center; + horizontalEdgeType = ui::LayoutComponent::HorizontalEdge::Center; } - layoutComponent->setHorizontalEage(horizontalEdgeType); - ui::LayoutComponent::VerticalEage verticalEdgeType = ui::LayoutComponent::VerticalEage::None; + layoutComponent->setHorizontalEdge(horizontalEdgeType); + ui::LayoutComponent::VerticalEdge verticalEdgeType = ui::LayoutComponent::VerticalEdge::None; if (verticalEdge == Layout_TopEdge) { - verticalEdgeType = ui::LayoutComponent::VerticalEage::Top; + verticalEdgeType = ui::LayoutComponent::VerticalEdge::Top; } else if (verticalEdge == Layout_BottomEdge) { - verticalEdgeType = ui::LayoutComponent::VerticalEage::Bottom; + verticalEdgeType = ui::LayoutComponent::VerticalEdge::Bottom; } else if (verticalEdge == Layout_BothEdge) { - verticalEdgeType = ui::LayoutComponent::VerticalEage::Center; + verticalEdgeType = ui::LayoutComponent::VerticalEdge::Center; } - layoutComponent->setVerticalEage(verticalEdgeType); + layoutComponent->setVerticalEdge(verticalEdgeType); layoutComponent->setTopMargin(topMargin); - layoutComponent->setButtomMargin(bottomMargin); + layoutComponent->setBottomMargin(bottomMargin); layoutComponent->setLeftMargin(leftMargin); layoutComponent->setRightMargin(rightMargin); } diff --git a/cocos/editor-support/cocostudio/WidgetReader/WidgetReader.cpp b/cocos/editor-support/cocostudio/WidgetReader/WidgetReader.cpp index 55e4ff103b..7807517b41 100644 --- a/cocos/editor-support/cocostudio/WidgetReader/WidgetReader.cpp +++ b/cocos/editor-support/cocostudio/WidgetReader/WidgetReader.cpp @@ -901,37 +901,37 @@ namespace cocostudio layoutComponent->setPercentHeight(sizeYPercent); layoutComponent->setStretchWidthEnabled(stretchHorizontalEnabled); layoutComponent->setStretchHeightEnabled(stretchVerticalEnabled); - ui::LayoutComponent::HorizontalEage horizontalEdgeType = ui::LayoutComponent::HorizontalEage::None; + ui::LayoutComponent::HorizontalEdge horizontalEdgeType = ui::LayoutComponent::HorizontalEdge::None; if (horizontalEdge == P_Layout_LeftEdge) { - horizontalEdgeType = ui::LayoutComponent::HorizontalEage::Left; + horizontalEdgeType = ui::LayoutComponent::HorizontalEdge::Left; } else if (horizontalEdge == P_Layout_RightEdge) { - horizontalEdgeType = ui::LayoutComponent::HorizontalEage::Right; + horizontalEdgeType = ui::LayoutComponent::HorizontalEdge::Right; } else if (horizontalEdge == P_Layout_BothEdge) { - horizontalEdgeType = ui::LayoutComponent::HorizontalEage::Center; + horizontalEdgeType = ui::LayoutComponent::HorizontalEdge::Center; } - layoutComponent->setHorizontalEage(horizontalEdgeType); - ui::LayoutComponent::VerticalEage verticalEdgeType = ui::LayoutComponent::VerticalEage::None; + layoutComponent->setHorizontalEdge(horizontalEdgeType); + ui::LayoutComponent::VerticalEdge verticalEdgeType = ui::LayoutComponent::VerticalEdge::None; if (verticalEdge == P_Layout_TopEdge) { - verticalEdgeType = ui::LayoutComponent::VerticalEage::Top; + verticalEdgeType = ui::LayoutComponent::VerticalEdge::Top; } else if (verticalEdge == P_Layout_BottomEdge) { - verticalEdgeType = ui::LayoutComponent::VerticalEage::Bottom; + verticalEdgeType = ui::LayoutComponent::VerticalEdge::Bottom; } else if (verticalEdge == P_Layout_BothEdge) { - verticalEdgeType = ui::LayoutComponent::VerticalEage::Center; + verticalEdgeType = ui::LayoutComponent::VerticalEdge::Center; } - layoutComponent->setVerticalEage(verticalEdgeType); + layoutComponent->setVerticalEdge(verticalEdgeType); layoutComponent->setTopMargin(topMargin); - layoutComponent->setButtomMargin(bottomMargin); + layoutComponent->setBottomMargin(bottomMargin); layoutComponent->setLeftMargin(leftMargin); layoutComponent->setRightMargin(rightMargin); } diff --git a/cocos/ui/UILayoutComponent.cpp b/cocos/ui/UILayoutComponent.cpp index 72def52f39..907cafc029 100644 --- a/cocos/ui/UILayoutComponent.cpp +++ b/cocos/ui/UILayoutComponent.cpp @@ -31,11 +31,11 @@ NS_CC_BEGIN namespace ui { LayoutComponent::LayoutComponent() - :_horizontalEage(HorizontalEage::None) - , _verticalEage(VerticalEage::None) + :_horizontalEdge(HorizontalEdge::None) + , _verticalEdge(VerticalEdge::None) , _leftMargin(0) , _rightMargin(0) - , _buttomMargin(0) + , _bottomMargin(0) , _topMargin(0) , _usingPositionPercentX(false) , _positionPercentX(0) @@ -57,7 +57,7 @@ namespace ui { } - LayoutComponent* LayoutComponent::boundingLayoutForNode(Node* node) + LayoutComponent* LayoutComponent::boundingLayoutComponent(Node* node) { LayoutComponent * layout = (LayoutComponent*)node->getComponent(__LAYOUT_COMPONENT_NAME); if (layout != nullptr) @@ -121,7 +121,7 @@ namespace ui { const Size& ownerSize = _owner->getContentSize(); const Size& parentSize = parent->getContentSize(); - _buttomMargin = ownerPoint.y - ownerAnchor.y * ownerSize.height; + _bottomMargin = ownerPoint.y - ownerAnchor.y * ownerSize.height; _topMargin = parentSize.height - (ownerPoint.y + (1 - ownerAnchor.y) * ownerSize.height); } @@ -172,27 +172,37 @@ namespace ui { } void LayoutComponent::setPosition(const Point& position) { - _owner->setPosition(position); - Node* parent = this->getOwnerParent(); if (parent != nullptr) { - const Point& ownerPoint = _owner->getPosition(); + Point ownerPoint = position; const Size& parentSize = parent->getContentSize(); if (parentSize.width != 0) _positionPercentX = ownerPoint.x / parentSize.width; else + { _positionPercentX = 0; + if (_usingPositionPercentX) + ownerPoint.x = 0; + } if (parentSize.height != 0) _positionPercentY = ownerPoint.y / parentSize.height; else + { _positionPercentY = 0; + if (_usingPositionPercentY) + ownerPoint.y = 0; + } + + _owner->setPosition(ownerPoint); this->refreshHorizontalMargin(); this->refreshVerticalMargin(); } + else + _owner->setPosition(position); } bool LayoutComponent::isPositionPercentXEnabled()const @@ -204,7 +214,7 @@ namespace ui { _usingPositionPercentX = isUsed; if (_usingPositionPercentX) { - _horizontalEage = HorizontalEage::None; + _horizontalEdge = HorizontalEdge::None; } } @@ -233,7 +243,7 @@ namespace ui { _usingPositionPercentY = isUsed; if (_usingPositionPercentY) { - _verticalEage = VerticalEage::None; + _verticalEdge = VerticalEdge::None; } } @@ -253,14 +263,14 @@ namespace ui { } } - LayoutComponent::HorizontalEage LayoutComponent::getHorizontalEage()const + LayoutComponent::HorizontalEdge LayoutComponent::getHorizontalEdge()const { - return _horizontalEage; + return _horizontalEdge; } - void LayoutComponent::setHorizontalEage(HorizontalEage hEage) + void LayoutComponent::setHorizontalEdge(HorizontalEdge hEage) { - _horizontalEage = hEage; - if (_horizontalEage != HorizontalEage::None) + _horizontalEdge = hEage; + if (_horizontalEdge != HorizontalEdge::None) { _usingPositionPercentX = false; } @@ -268,24 +278,30 @@ namespace ui { Node* parent = this->getOwnerParent(); if (parent != nullptr) { + Point ownerPoint = _owner->getPosition(); const Size& parentSize = parent->getContentSize(); if (parentSize.width != 0) - _positionPercentX = _owner->getPosition().x / parentSize.width; + _positionPercentX = ownerPoint.x / parentSize.width; else + { _positionPercentX = 0; + ownerPoint.x = 0; + if (_usingPositionPercentX) + _owner->setPosition(ownerPoint); + } this->refreshHorizontalMargin(); } } - LayoutComponent::VerticalEage LayoutComponent::getVerticalEage()const + LayoutComponent::VerticalEdge LayoutComponent::getVerticalEdge()const { - return _verticalEage; + return _verticalEdge; } - void LayoutComponent::setVerticalEage(VerticalEage vEage) + void LayoutComponent::setVerticalEdge(VerticalEdge vEage) { - _verticalEage = vEage; - if (_verticalEage != VerticalEage::None) + _verticalEdge = vEage; + if (_verticalEdge != VerticalEdge::None) { _usingPositionPercentY = false; } @@ -293,11 +309,17 @@ namespace ui { Node* parent = this->getOwnerParent(); if (parent != nullptr) { + Point ownerPoint = _owner->getPosition(); const Size& parentSize = parent->getContentSize(); if (parentSize.height != 0) - _positionPercentY = _owner->getPosition().y / parentSize.height; + _positionPercentY = ownerPoint.y / parentSize.height; else + { _positionPercentY = 0; + ownerPoint.y = 0; + if (_usingPositionPercentY) + _owner->setPosition(ownerPoint); + } this->refreshVerticalMargin(); } @@ -330,13 +352,13 @@ namespace ui { _topMargin = margin; } - float LayoutComponent::getButtomMargin()const + float LayoutComponent::getBottomMargin()const { - return _buttomMargin; + return _bottomMargin; } - void LayoutComponent::setButtomMargin(float margin) + void LayoutComponent::setBottomMargin(float margin) { - _buttomMargin = margin; + _bottomMargin = margin; } //Size & Percent @@ -346,27 +368,37 @@ namespace ui { } void LayoutComponent::setSize(const Size& size) { - _owner->setContentSize(size); - Node* parent = this->getOwnerParent(); if (parent != nullptr) { - const Size& ownerSize = _owner->getContentSize(); + Size ownerSize = size; const Size& parentSize = parent->getContentSize(); if (parentSize.width != 0) _percentWidth = ownerSize.width / parentSize.width; else + { _percentWidth = 0; + if (_usingPercentWidth) + ownerSize.width = 0; + } if (parentSize.height != 0) _percentHeight = ownerSize.height / parentSize.height; else + { _percentHeight = 0; + if (_usingPercentHeight) + ownerSize.height = 0; + } + + _owner->setContentSize(ownerSize); this->refreshHorizontalMargin(); this->refreshVerticalMargin(); } + else + _owner->setContentSize(size); } bool LayoutComponent::isPercentWidthEnabled()const @@ -390,7 +422,6 @@ namespace ui { { Size ownerSize = _owner->getContentSize(); ownerSize.width = width; - _owner->setContentSize(ownerSize); Node* parent = this->getOwnerParent(); if (parent != nullptr) @@ -399,10 +430,16 @@ namespace ui { if (parentSize.width != 0) _percentWidth = ownerSize.width / parentSize.width; else + { _percentWidth = 0; - + if (_usingPercentWidth) + ownerSize.width = 0; + } + _owner->setContentSize(ownerSize); this->refreshHorizontalMargin(); } + else + _owner->setContentSize(ownerSize); } float LayoutComponent::getPercentWidth()const @@ -445,7 +482,6 @@ namespace ui { { Size ownerSize = _owner->getContentSize(); ownerSize.height = height; - _owner->setContentSize(ownerSize); Node* parent = this->getOwnerParent(); if (parent != nullptr) @@ -454,10 +490,16 @@ namespace ui { if (parentSize.height != 0) _percentHeight = ownerSize.height / parentSize.height; else + { _percentHeight = 0; - + if (_usingPercentHeight) + ownerSize.height = 0; + } + _owner->setContentSize(ownerSize); this->refreshVerticalMargin(); } + else + _owner->setContentSize(ownerSize); } float LayoutComponent::getPercentHeight()const @@ -516,9 +558,9 @@ namespace ui { Size ownerSize = _owner->getContentSize(); Point ownerPosition = _owner->getPosition(); - switch (this->_horizontalEage) + switch (this->_horizontalEdge) { - case HorizontalEage::None: + case HorizontalEdge::None: if (_usingStretchWidth) { ownerSize.width = parentSize.width * _percentWidth; @@ -532,20 +574,22 @@ namespace ui { ownerSize.width = parentSize.width * _percentWidth; } break; - case HorizontalEage::Left: + case HorizontalEdge::Left: if (_usingPercentWidth || _usingStretchWidth) ownerSize.width = parentSize.width * _percentWidth; ownerPosition.x = _leftMargin + ownerAnchor.x * ownerSize.width; break; - case HorizontalEage::Right: + case HorizontalEdge::Right: if (_usingPercentWidth || _usingStretchWidth) ownerSize.width = parentSize.width * _percentWidth; ownerPosition.x = parentSize.width - (_rightMargin + (1 - ownerAnchor.x) * ownerSize.width); break; - case HorizontalEage::Center: + case HorizontalEdge::Center: if (_usingPercentWidth || _usingStretchWidth) { ownerSize.width = parentSize.width - _leftMargin - _rightMargin; + if (ownerSize.width < 0) + ownerSize.width = 0; ownerPosition.x = _leftMargin + ownerAnchor.x * ownerSize.width; } else @@ -555,13 +599,13 @@ namespace ui { break; } - switch (this->_verticalEage) + switch (this->_verticalEdge) { - case VerticalEage::None: + case VerticalEdge::None: if (_usingStretchHeight) { ownerSize.height = parentSize.height * _percentHeight; - ownerPosition.y = _buttomMargin + ownerAnchor.y * ownerSize.height; + ownerPosition.y = _bottomMargin + ownerAnchor.y * ownerSize.height; } else { @@ -571,21 +615,23 @@ namespace ui { ownerSize.height = parentSize.height * _percentHeight; } break; - case VerticalEage::Bottom: + case VerticalEdge::Bottom: if (_usingPercentHeight || _usingStretchHeight) ownerSize.height = parentSize.height * _percentHeight; - ownerPosition.y = _buttomMargin + ownerAnchor.y * ownerSize.height; + ownerPosition.y = _bottomMargin + ownerAnchor.y * ownerSize.height; break; - case VerticalEage::Top: + case VerticalEdge::Top: if (_usingPercentHeight || _usingStretchHeight) ownerSize.height = parentSize.height * _percentHeight; ownerPosition.y = parentSize.height - (_topMargin + (1 - ownerAnchor.y) * ownerSize.height); break; - case VerticalEage::Center: + case VerticalEdge::Center: if (_usingPercentHeight || _usingStretchHeight) { - ownerSize.height = parentSize.height - _topMargin - _buttomMargin; - ownerPosition.y = _buttomMargin + ownerAnchor.y * ownerSize.height; + ownerSize.height = parentSize.height - _topMargin - _bottomMargin; + if (ownerSize.height < 0) + ownerSize.height = 0; + ownerPosition.y = _bottomMargin + ownerAnchor.y * ownerSize.height; } else ownerPosition.y = parentSize.height* _positionPercentY; diff --git a/cocos/ui/UILayoutComponent.h b/cocos/ui/UILayoutComponent.h index aeab6c1c8b..a824e7e190 100644 --- a/cocos/ui/UILayoutComponent.h +++ b/cocos/ui/UILayoutComponent.h @@ -38,16 +38,16 @@ namespace ui { virtual bool init()override; CREATE_FUNC(LayoutComponent); - static LayoutComponent* boundingLayoutForNode(Node* node); + static LayoutComponent* boundingLayoutComponent(Node* node); - enum class HorizontalEage + enum class HorizontalEdge { None, Left, Right, Center }; - enum class VerticalEage + enum class VerticalEdge { None, Bottom, @@ -81,11 +81,11 @@ namespace ui { float getPositionPercentY()const; void setPositionPercentY(float percentMargin); - HorizontalEage getHorizontalEage()const; - void setHorizontalEage(HorizontalEage hEage); + HorizontalEdge getHorizontalEdge()const; + void setHorizontalEdge(HorizontalEdge hEage); - VerticalEage getVerticalEage()const; - void setVerticalEage(VerticalEage vEage); + VerticalEdge getVerticalEdge()const; + void setVerticalEdge(VerticalEdge vEage); float getLeftMargin()const; void setLeftMargin(float margin); @@ -96,8 +96,8 @@ namespace ui { float getTopMargin()const; void setTopMargin(float margin); - float getButtomMargin()const; - void setButtomMargin(float margin); + float getBottomMargin()const; + void setBottomMargin(float margin); // Size & Percent const Size& getSize()const; @@ -135,12 +135,12 @@ namespace ui { void refreshHorizontalMargin(); void refreshVerticalMargin(); protected: - HorizontalEage _horizontalEage; - VerticalEage _verticalEage; + HorizontalEdge _horizontalEdge; + VerticalEdge _verticalEdge; float _leftMargin; float _rightMargin; - float _buttomMargin; + float _bottomMargin; float _topMargin; bool _usingPositionPercentX; @@ -157,7 +157,7 @@ namespace ui { float _percentHeight; bool _usingPercentHeight; - bool _actived; + bool _actived; }; } diff --git a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UILayoutTest/UILayoutTest.cpp b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UILayoutTest/UILayoutTest.cpp index 5e6913f1c2..bbb8e31605 100644 --- a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UILayoutTest/UILayoutTest.cpp +++ b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UILayoutTest/UILayoutTest.cpp @@ -755,27 +755,27 @@ bool UILayoutComponent_Berth_Test::init() if (UILayoutComponentTest::init()) { Sprite* leftTopSprite = Sprite::create("cocosui/CloseSelected.png"); - LayoutComponent* leftTop = LayoutComponent::boundingLayoutForNode(leftTopSprite); - leftTop->setHorizontalEage(LayoutComponent::HorizontalEage::Left); - leftTop->setVerticalEage(LayoutComponent::VerticalEage::Top); + LayoutComponent* leftTop = LayoutComponent::boundingLayoutComponent(leftTopSprite); + leftTop->setHorizontalEdge(LayoutComponent::HorizontalEdge::Left); + leftTop->setVerticalEdge(LayoutComponent::VerticalEdge::Top); _baseLayer->addChild(leftTopSprite); Sprite* leftBottomSprite = Sprite::create("cocosui/CloseSelected.png"); - LayoutComponent* leftBottom = LayoutComponent::boundingLayoutForNode(leftBottomSprite); - leftBottom->setHorizontalEage(LayoutComponent::HorizontalEage::Left); - leftBottom->setVerticalEage(LayoutComponent::VerticalEage::Bottom); + LayoutComponent* leftBottom = LayoutComponent::boundingLayoutComponent(leftBottomSprite); + leftBottom->setHorizontalEdge(LayoutComponent::HorizontalEdge::Left); + leftBottom->setVerticalEdge(LayoutComponent::VerticalEdge::Bottom); _baseLayer->addChild(leftBottomSprite); Sprite* rightTopSprite = Sprite::create("cocosui/CloseSelected.png"); - LayoutComponent* rightTop = LayoutComponent::boundingLayoutForNode(rightTopSprite); - rightTop->setHorizontalEage(LayoutComponent::HorizontalEage::Right); - rightTop->setVerticalEage(LayoutComponent::VerticalEage::Top); + LayoutComponent* rightTop = LayoutComponent::boundingLayoutComponent(rightTopSprite); + rightTop->setHorizontalEdge(LayoutComponent::HorizontalEdge::Right); + rightTop->setVerticalEdge(LayoutComponent::VerticalEdge::Top); _baseLayer->addChild(rightTopSprite); Sprite* rightBottomSprite = Sprite::create("cocosui/CloseSelected.png"); - LayoutComponent* rightBottom = LayoutComponent::boundingLayoutForNode(rightBottomSprite); - rightBottom->setHorizontalEage(LayoutComponent::HorizontalEage::Right); - rightBottom->setVerticalEage(LayoutComponent::VerticalEage::Bottom); + LayoutComponent* rightBottom = LayoutComponent::boundingLayoutComponent(rightBottomSprite); + rightBottom->setHorizontalEdge(LayoutComponent::HorizontalEdge::Right); + rightBottom->setVerticalEdge(LayoutComponent::VerticalEdge::Bottom); _baseLayer->addChild(rightBottomSprite); ui::Helper::doLayout(_baseLayer); @@ -791,9 +791,9 @@ bool UILayoutComponent_Berth_Stretch_Test::init() { ImageView* leftTopSprite = ImageView::create("cocosui/CloseSelected.png"); leftTopSprite->ignoreContentAdaptWithSize(false); - LayoutComponent* leftTop = LayoutComponent::boundingLayoutForNode(leftTopSprite); - leftTop->setHorizontalEage(LayoutComponent::HorizontalEage::Left); - leftTop->setVerticalEage(LayoutComponent::VerticalEage::Top); + LayoutComponent* leftTop = LayoutComponent::boundingLayoutComponent(leftTopSprite); + leftTop->setHorizontalEdge(LayoutComponent::HorizontalEdge::Left); + leftTop->setVerticalEdge(LayoutComponent::VerticalEdge::Top); leftTop->setStretchWidthEnabled(true); leftTop->setStretchHeightEnabled(true); _baseLayer->addChild(leftTopSprite); @@ -803,21 +803,21 @@ bool UILayoutComponent_Berth_Stretch_Test::init() ImageView* leftBottomSprite = ImageView::create("cocosui/CloseSelected.png"); leftBottomSprite->ignoreContentAdaptWithSize(false); - LayoutComponent* leftBottom = LayoutComponent::boundingLayoutForNode(leftBottomSprite); - leftBottom->setHorizontalEage(LayoutComponent::HorizontalEage::Left); - leftBottom->setVerticalEage(LayoutComponent::VerticalEage::Bottom); + LayoutComponent* leftBottom = LayoutComponent::boundingLayoutComponent(leftBottomSprite); + leftBottom->setHorizontalEdge(LayoutComponent::HorizontalEdge::Left); + leftBottom->setVerticalEdge(LayoutComponent::VerticalEdge::Bottom); leftBottom->setStretchWidthEnabled(true); leftBottom->setStretchHeightEnabled(true); _baseLayer->addChild(leftBottomSprite); leftBottom->setSize(leftBottomSprite->getContentSize()); leftBottom->setLeftMargin(0); - leftBottom->setButtomMargin(0); + leftBottom->setBottomMargin(0); ImageView* rightTopSprite = ImageView::create("cocosui/CloseSelected.png"); rightTopSprite->ignoreContentAdaptWithSize(false); - LayoutComponent* rightTop = LayoutComponent::boundingLayoutForNode(rightTopSprite); - rightTop->setHorizontalEage(LayoutComponent::HorizontalEage::Right); - rightTop->setVerticalEage(LayoutComponent::VerticalEage::Top); + LayoutComponent* rightTop = LayoutComponent::boundingLayoutComponent(rightTopSprite); + rightTop->setHorizontalEdge(LayoutComponent::HorizontalEdge::Right); + rightTop->setVerticalEdge(LayoutComponent::VerticalEdge::Top); rightTop->setStretchWidthEnabled(true); rightTop->setStretchHeightEnabled(true); _baseLayer->addChild(rightTopSprite); @@ -827,14 +827,14 @@ bool UILayoutComponent_Berth_Stretch_Test::init() ImageView* rightBottomSprite = ImageView::create("cocosui/CloseSelected.png"); rightBottomSprite->ignoreContentAdaptWithSize(false); - LayoutComponent* rightBottom = LayoutComponent::boundingLayoutForNode(rightBottomSprite); - rightBottom->setHorizontalEage(LayoutComponent::HorizontalEage::Right); - rightBottom->setVerticalEage(LayoutComponent::VerticalEage::Bottom); + LayoutComponent* rightBottom = LayoutComponent::boundingLayoutComponent(rightBottomSprite); + rightBottom->setHorizontalEdge(LayoutComponent::HorizontalEdge::Right); + rightBottom->setVerticalEdge(LayoutComponent::VerticalEdge::Bottom); rightBottom->setStretchWidthEnabled(true); rightBottom->setStretchHeightEnabled(true); _baseLayer->addChild(rightBottomSprite); rightBottom->setSize(rightBottomSprite->getContentSize()); - rightBottom->setButtomMargin(0); + rightBottom->setBottomMargin(0); rightBottom->setRightMargin(0); ui::Helper::doLayout(_baseLayer); diff --git a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UILayoutTest/UILayoutTest_Editor.cpp b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UILayoutTest/UILayoutTest_Editor.cpp index 4e12fb3b23..cc2e483b32 100644 --- a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UILayoutTest/UILayoutTest_Editor.cpp +++ b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UILayoutTest/UILayoutTest_Editor.cpp @@ -1235,9 +1235,9 @@ void UILayoutComponentTest_Editor::configureGUIScene() Text* back_label = Text::create("Back", "", 20); back_label->setTouchEnabled(true); - auto labelLayout = LayoutComponent::boundingLayoutForNode(back_label); - labelLayout->setHorizontalEage(LayoutComponent::HorizontalEage::Right); - labelLayout->setVerticalEage(LayoutComponent::VerticalEage::Bottom); + auto labelLayout = LayoutComponent::boundingLayoutComponent(back_label); + labelLayout->setHorizontalEdge(LayoutComponent::HorizontalEdge::Right); + labelLayout->setVerticalEdge(LayoutComponent::VerticalEdge::Bottom); back_label->addTouchEventListener(CC_CALLBACK_2(UIScene_Editor::toGUIEditorTestScene, this)); _layout->addChild(back_label); From dcd544cc6630ce73fe2b472e497a37448793d945 Mon Sep 17 00:00:00 2001 From: Liam Date: Wed, 24 Dec 2014 11:12:14 +0800 Subject: [PATCH 13/13] update ui widget function format --- cocos/ui/UIWidget.cpp | 13 ++++++------- cocos/ui/UIWidget.h | 2 +- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/cocos/ui/UIWidget.cpp b/cocos/ui/UIWidget.cpp index 41e58d1713..af681f1c87 100644 --- a/cocos/ui/UIWidget.cpp +++ b/cocos/ui/UIWidget.cpp @@ -497,10 +497,10 @@ const Vec2& Widget::getSizePercent() if (_usingLayoutComponent) { auto component = this->getOrCreateLayoutComponent(); - return component->getPercentContentSize(); + _sizePercent = component->getPercentContentSize(); } - else - return _sizePercent; + + return _sizePercent; } Vec2 Widget::getWorldPosition()const @@ -1026,7 +1026,7 @@ void Widget::setPositionPercent(const Vec2 &percent) } } -Vec2 Widget::getPositionPercent(){ +const Vec2& Widget::getPositionPercent(){ if (_usingLayoutComponent) { @@ -1034,10 +1034,9 @@ Vec2 Widget::getPositionPercent(){ float percentX = component->getPositionPercentX(); float percentY = component->getPositionPercentY(); - return Vec2(percentX, percentY); + _positionPercent = Vec2(percentX, percentY); } - else - return _positionPercent; + return _positionPercent; } void Widget::setPositionType(PositionType type) diff --git a/cocos/ui/UIWidget.h b/cocos/ui/UIWidget.h index eb030d7ad3..bab5e8c35d 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 */ - Vec2 getPositionPercent(); + const Vec2& getPositionPercent(); /** * Changes the position type of the widget