Merge pull request #3774 from nutty898/optimize_layout

[develop] [GUI] Optimize layout
This commit is contained in:
James Chen 2013-09-23 06:38:21 -07:00
commit 6ded79111b
20 changed files with 395 additions and 594 deletions

View File

@ -1 +1 @@
2abd573d3cb41c16925cbdf14bd55be5f97d88a4
a87ed79f8ef68972cf8791008b9d79f2a5ed84f1

View File

@ -63,7 +63,6 @@ CocoStudio/Components/CCInputDelegate.cpp \
CocoStudio/GUI/BaseClasses/UIRootWidget.cpp \
CocoStudio/GUI/BaseClasses/UIWidget.cpp \
CocoStudio/GUI/Layouts/Layout.cpp \
CocoStudio/GUI/Layouts/LayoutExecutant.cpp \
CocoStudio/GUI/Layouts/LayoutParameter.cpp \
CocoStudio/GUI/Layouts/UILayoutDefine.cpp \
CocoStudio/GUI/System/CocosGUI.cpp \

View File

@ -24,6 +24,7 @@
#include "Layout.h"
#include "../System/UILayer.h"
#include "../System/UIHelper.h"
#include "../../../GUI/CCControlExtension/CCScale9Sprite.h"
NS_CC_EXT_BEGIN
@ -32,7 +33,6 @@ NS_CC_EXT_BEGIN
Layout::Layout():
_clippingEnabled(false),
_layoutExecutant(NULL),
_backGroundScale9Enabled(false),
_backGroundImage(NULL),
_backGroundImageFileName(""),
@ -46,14 +46,14 @@ _gStartColor(Color3B::WHITE),
_gEndColor(Color3B::WHITE),
_alongVector(Point(0.0f, -1.0f)),
_cOpacity(255),
_backGroundImageTextureSize(Size::ZERO)
_backGroundImageTextureSize(Size::ZERO),
_layoutType(LAYOUT_ABSOLUTE)
{
_widgetType = WidgetTypeContainer;
}
Layout::~Layout()
{
CC_SAFE_RELEASE_NULL(_layoutExecutant);
}
Layout* Layout::create()
@ -90,22 +90,6 @@ bool Layout::init()
return true;
}
void Layout::setLayoutExecutant(LayoutExecutant *exe)
{
if (_layoutExecutant)
{
CC_SAFE_RELEASE_NULL(_layoutExecutant);
}
_layoutExecutant = exe;
_layoutExecutant->setLayout(this);
CC_SAFE_RETAIN(_layoutExecutant);
}
LayoutExecutant* Layout::getLayoutExecutant() const
{
return _layoutExecutant;
}
void Layout::initRenderer()
{
_renderer = RectClippingNode::create();
@ -136,10 +120,7 @@ void Layout::setClippingEnabled(bool able)
void Layout::onSizeChanged()
{
DYNAMIC_CAST_CLIPPINGLAYER->setClippingSize(_size);
if (_layoutExecutant)
{
_layoutExecutant->doLayout();
}
doLayout();
if (_backGroundImage)
{
_backGroundImage->setPosition(Point(_size.width/2.0f, _size.height/2.0f));
@ -425,6 +406,283 @@ const Size& Layout::getContentSize() const
return _renderer->getContentSize();
}
void Layout::setLayoutType(LayoutType type)
{
_layoutType = type;
}
LayoutType Layout::getLayoutType() const
{
return _layoutType;
}
void Layout::doLayout()
{
switch (_layoutType)
{
case LAYOUT_ABSOLUTE:
break;
case LAYOUT_LINEAR_VERTICAL:
{
ccArray* layoutChildrenArray = getChildren()->data;
int length = layoutChildrenArray->num;
Size layoutSize = getSize();
float topBoundary = layoutSize.height;
for (int i=0; i<length; ++i)
{
UIWidget* child = dynamic_cast<UIWidget*>(layoutChildrenArray->arr[i]);
LinearLayoutParameter* layoutParameter = dynamic_cast<LinearLayoutParameter*>(child->getLayoutParameter());
if (layoutParameter)
{
WidgetType childType = child->getWidgetType();
UILinearGravity childGravity = layoutParameter->getGravity();
Point ap = child->getAnchorPoint();
Size cs = child->getSize();
float finalPosX = childType == WidgetTypeWidget ? ap.x * cs.width : 0.0f;
float finalPosY = childType == WidgetTypeWidget ? topBoundary - ((1.0f-ap.y) * cs.height) : topBoundary - cs.height;
switch (childGravity)
{
case LINEAR_GRAVITY_NONE:
case LINEAR_GRAVITY_LEFT:
break;
case LINEAR_GRAVITY_RIGHT:
finalPosX = childType == WidgetTypeWidget ? layoutSize.width - ((1.0f - ap.x) * cs.width) : layoutSize.width - cs.width;
break;
case LINEAR_GRAVITY_CENTER_HORIZONTAL:
finalPosX = childType == WidgetTypeWidget ? layoutSize.width / 2.0f - cs.width * (0.5f-ap.x) : (layoutSize.width - cs.width) * 0.5f;
break;
default:
break;
}
UIMargin mg = layoutParameter->getMargin();
finalPosX += mg.left;
finalPosY -= mg.top;
child->setPosition(Point(finalPosX, finalPosY));
topBoundary = child->getBottomInParent() - mg.bottom;
}
}
break;
}
case LAYOUT_LINEAR_HORIZONTAL:
{
ccArray* layoutChildrenArray = getChildren()->data;
int length = layoutChildrenArray->num;
Size layoutSize = getSize();
float leftBoundary = 0.0f;
for (int i=0; i<length; ++i)
{
UIWidget* child = dynamic_cast<UIWidget*>(layoutChildrenArray->arr[i]);
LinearLayoutParameter* layoutParameter = dynamic_cast<LinearLayoutParameter*>(child->getLayoutParameter());
if (layoutParameter)
{
WidgetType childType = child->getWidgetType();
UILinearGravity childGravity = layoutParameter->getGravity();
Point ap = child->getAnchorPoint();
Size cs = child->getSize();
float finalPosX = childType == WidgetTypeWidget ? leftBoundary + (ap.x * cs.width) : leftBoundary;
float finalPosY = childType == WidgetTypeWidget ? layoutSize.height - (1.0f - ap.y) * cs.height : layoutSize.height - cs.height;
switch (childGravity)
{
case LINEAR_GRAVITY_NONE:
case LINEAR_GRAVITY_TOP:
break;
case LINEAR_GRAVITY_BOTTOM:
finalPosY = childType == WidgetTypeWidget ? ap.y * cs.height : 0.0f;
break;
case LINEAR_GRAVITY_CENTER_VERTICAL:
finalPosY = childType == WidgetTypeWidget ? layoutSize.height/2.0f - cs.height * (0.5f - ap.y) : (layoutSize.height - cs.height) * 0.5f;
break;
default:
break;
}
UIMargin mg = layoutParameter->getMargin();
finalPosX += mg.left;
finalPosY -= mg.top;
child->setPosition(Point(finalPosX, finalPosY));
leftBoundary = child->getRightInParent() + mg.right;
}
}
break;
}
case LAYOUT_RELATIVE:
{
ccArray* layoutChildrenArray = getChildren()->data;
int length = layoutChildrenArray->num;
Size layoutSize = getSize();
for (int i=0; i<length; i++)
{
UIWidget* child = dynamic_cast<UIWidget*>(layoutChildrenArray->arr[i]);
WidgetType childType = child->getWidgetType();
Point ap = child->getAnchorPoint();
Size cs = child->getSize();
RelativeLayoutParameter* layoutParameter = dynamic_cast<RelativeLayoutParameter*>(child->getLayoutParameter());
if (layoutParameter)
{
float finalPosX = childType == WidgetTypeWidget ? ap.x * cs.width : 0.0f;
float finalPosY = childType == WidgetTypeWidget ? layoutSize.height - ((1.0f - ap.y) * cs.height) : layoutSize.height - cs.height;
UIRelativeAlign align = layoutParameter->getAlign();
const char* relativeName = layoutParameter->getRelativeToWidgetName();
UIWidget* relativeWidget = NULL;
if (relativeName && strcmp(relativeName, ""))
{
relativeWidget = CCUIHELPER->seekWidgetByRelativeName(this, relativeName);
}
switch (align)
{
case RELATIVE_ALIGN_NONE:
break;
case RELATIVE_ALIGN_PARENT_LEFT:
break;
case RELATIVE_ALIGN_PARENT_TOP:
break;
case RELATIVE_ALIGN_PARENT_RIGHT:
finalPosX = childType == WidgetTypeWidget ? layoutSize.width - ((1.0f - ap.x) * cs.width) : layoutSize.width - cs.width;
break;
case RELATIVE_ALIGN_PARENT_BOTTOM:
finalPosY = childType == WidgetTypeWidget ? ap.y * cs.height : 0.0f;
break;
case RELATIVE_CENTER_IN_PARENT:
finalPosX = childType == WidgetTypeWidget ? layoutSize.width * 0.5f - cs.width * (0.5f - ap.x) : (layoutSize.width - cs.width) * 0.5f;
finalPosY = childType == WidgetTypeWidget ? layoutSize.height * 0.5f - cs.height * (0.5f - ap.y) : (layoutSize.height - cs.height) * 0.5f;
break;
case RELATIVE_CENTER_HORIZONTAL:
finalPosX = childType == WidgetTypeWidget ? layoutSize.width * 0.5f - cs.width * (0.5f - ap.x) : (layoutSize.width - cs.width) * 0.5f;
break;
case RELATIVE_CENTER_VERTICAL:
finalPosY = childType == WidgetTypeWidget ? layoutSize.height * 0.5f - cs.height * (0.5f - ap.y) : (layoutSize.height - cs.height) * 0.5f;
break;
case RELATIVE_LOCATION_LEFT_OF_TOPALIGN:
if (relativeWidget)
{
float locationTop = relativeWidget->getTopInParent();
float locationRight = relativeWidget->getLeftInParent();
finalPosY = childType == WidgetTypeWidget ? locationTop - ap.y * cs.height : locationTop - cs.height;
finalPosX = childType == WidgetTypeWidget ? locationRight - (1.0f - ap.x) * cs.width : locationRight - cs.width;
}
break;
case RELATIVE_LOCATION_LEFT_OF_CENTER:
break;
case RELATIVE_LOCATION_LEFT_OF_BOTTOMALIGN:
if (relativeWidget)
{
float locationRight = relativeWidget->getLeftInParent();
float locationBottom = relativeWidget->getBottomInParent();
finalPosY = childType == WidgetTypeWidget ? locationBottom + ap.y * cs.height : locationBottom;
finalPosX = childType == WidgetTypeWidget ? locationRight - (1.0f - ap.x) * cs.width : locationRight - cs.width;
}
break;
case RELATIVE_LOCATION_RIGHT_OF_TOPALIGN:
if (relativeWidget)
{
float locationTop = relativeWidget->getTopInParent();
float locationLeft = relativeWidget->getRightInParent();
finalPosY = childType == WidgetTypeWidget ? locationTop - ap.y * cs.height : locationTop - cs.height;
finalPosX = childType == WidgetTypeWidget ? locationLeft + ap.x * cs.width : locationLeft;
}
break;
case RELATIVE_LOCATION_RIGHT_OF_CENTER:
break;
case RELATIVE_LOCATION_RIGHT_OF_BOTTOMALIGN:
if (relativeWidget)
{
float locationLeft = relativeWidget->getRightInParent();
float locationBottom = relativeWidget->getBottomInParent();
finalPosY = childType == WidgetTypeWidget ? locationBottom + ap.y * cs.height : locationBottom;
finalPosX = childType == WidgetTypeWidget ? locationLeft + ap.x * cs.width : locationLeft;
}
break;
case RELATIVE_LOCATION_ABOVE_LEFTALIGN:
if (relativeWidget)
{
float locationBottom = relativeWidget->getTopInParent();
float locationLeft = relativeWidget->getLeftInParent();
finalPosY = childType == WidgetTypeWidget ? locationBottom + ap.y * cs.height : locationBottom;
finalPosX = childType == WidgetTypeWidget ? locationLeft + ap.x * cs.width : locationLeft;
}
break;
case RELATIVE_LOCATION_ABOVE_CENTER:
break;
case RELATIVE_LOCATION_ABOVE_RIGHTALIGN:
if (relativeWidget)
{
float locationBottom = relativeWidget->getTopInParent();
float locationRight = relativeWidget->getRightInParent();
finalPosY = childType == WidgetTypeWidget ? locationBottom + ap.y * cs.height : locationBottom;
finalPosX = childType == WidgetTypeWidget ? locationRight - (1.0f - ap.x) * cs.width : locationRight - cs.width;
}
break;
case RELATIVE_LOCATION_BELOW_LEFTALIGN:
if (relativeWidget)
{
float locationTop = relativeWidget->getBottomInParent();
float locationLeft = relativeWidget->getLeftInParent();
finalPosY = childType == WidgetTypeWidget ? locationTop - (1.0f - ap.y) * cs.height : locationTop - cs.height;
finalPosX = childType == WidgetTypeWidget ? locationLeft + ap.x * cs.width : locationLeft;
}
break;
case RELATIVE_LOCATION_BELOW_CENTER:
break;
case RELATIVE_LOCATION_BELOW_RIGHTALIGN:
if (relativeWidget)
{
float locationTop = relativeWidget->getBottomInParent();
float locationRight = relativeWidget->getRightInParent();
finalPosY = childType == WidgetTypeWidget ? locationTop - (1.0f - ap.y) * cs.height : locationTop - cs.height;
finalPosX = childType == WidgetTypeWidget ? locationRight - (1.0f - ap.x) * cs.width : locationRight - cs.width;
}
break;
default:
break;
}
UIMargin relativeWidgetMargin;
UIMargin mg;
if (relativeWidget)
{
relativeWidgetMargin = relativeWidget->getLayoutParameter()->getMargin();
mg = child->getLayoutParameter()->getMargin();
}
//handle margin
switch (align)
{
case RELATIVE_LOCATION_ABOVE_LEFTALIGN:
case RELATIVE_LOCATION_ABOVE_RIGHTALIGN:
case RELATIVE_LOCATION_ABOVE_CENTER:
finalPosY += relativeWidgetMargin.top;
finalPosY += mg.bottom;
break;
case RELATIVE_LOCATION_BELOW_LEFTALIGN:
case RELATIVE_LOCATION_BELOW_RIGHTALIGN:
case RELATIVE_LOCATION_BELOW_CENTER:
finalPosY -= relativeWidgetMargin.bottom;
finalPosY -= mg.top;
break;
case RELATIVE_LOCATION_LEFT_OF_TOPALIGN:
case RELATIVE_LOCATION_LEFT_OF_BOTTOMALIGN:
case RELATIVE_LOCATION_LEFT_OF_CENTER:
finalPosX -= relativeWidgetMargin.left;
finalPosX -= mg.right;
break;
case RELATIVE_LOCATION_RIGHT_OF_TOPALIGN:
case RELATIVE_LOCATION_RIGHT_OF_BOTTOMALIGN:
case RELATIVE_LOCATION_RIGHT_OF_CENTER:
finalPosX += relativeWidgetMargin.right;
finalPosX += mg.left;
break;
default:
break;
}
child->setPosition(Point(finalPosX, finalPosY));
}
}
break;
}
default:
break;
}
}
const char* Layout::getDescription() const
{
return "Layout";

View File

@ -26,7 +26,6 @@
#define __LAYOUT_H__
#include "../BaseClasses/UIWidget.h"
#include "LayoutExecutant.h"
NS_CC_EXT_BEGIN
@ -37,6 +36,14 @@ typedef enum
LAYOUT_COLOR_GRADIENT
}LayoutBackGroundColorType;
typedef enum
{
LAYOUT_ABSOLUTE,
LAYOUT_LINEAR_VERTICAL,
LAYOUT_LINEAR_HORIZONTAL,
LAYOUT_RELATIVE
}LayoutType;
class Layout : public UIWidget
{
public:
@ -55,24 +62,6 @@ public:
*/
static Layout* create();
/**
* Sets a LayoutExecutant for doing layout.
*
* @see LayoutExecutant
*
* @param LayoutExecutant pointer.
*/
virtual void setLayoutExecutant(LayoutExecutant* exe);
/**
* Gets the LayoutExecutant of Layout
*
* @see LayoutExecutant
*
* @return LayoutExecutant pointer.
*/
virtual LayoutExecutant* getLayoutExecutant() const;
//override "hitTest" method of widget.
virtual bool hitTest(const Point &pt);
@ -178,6 +167,26 @@ public:
* Content size is widget's texture size.
*/
virtual const Size& getContentSize() const;
/**
* Sets LayoutType.
*
* @see LayoutType
*
* @param LayoutType
*/
virtual void setLayoutType(LayoutType type);
/**
* Gets LayoutType.
*
* @see LayoutType
*
* @return LayoutType
*/
virtual LayoutType getLayoutType() const;
virtual void doLayout();
/**
* Returns the "class name" of widget.
@ -197,7 +206,6 @@ protected:
void addBackGroundImage();
protected:
bool _clippingEnabled;
LayoutExecutant* _layoutExecutant;
//background
bool _backGroundScale9Enabled;
@ -214,6 +222,7 @@ protected:
Point _alongVector;
int _cOpacity;
Size _backGroundImageTextureSize;
LayoutType _layoutType;
};
class RectClippingNode : public ClippingNode

View File

@ -1,342 +0,0 @@
/****************************************************************************
Copyright (c) 2013 cocos2d-x.org
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
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "LayoutExecutant.h"
#include "Layout.h"
#include "../System/UIHelper.h"
NS_CC_EXT_BEGIN
void LayoutExecutant::setLayout(Layout *layout)
{
_layout = layout;
}
Layout* LayoutExecutant::getLayout() const
{
return _layout;
}
LinearVerticalLayoutExecutant* LinearVerticalLayoutExecutant::create()
{
LinearVerticalLayoutExecutant* executant = new LinearVerticalLayoutExecutant();
if (executant)
{
executant->autorelease();
return executant;
}
CC_SAFE_DELETE(executant);
return NULL;
}
LinearHorizontalLayoutExecutant* LinearHorizontalLayoutExecutant::create()
{
LinearHorizontalLayoutExecutant* executant = new LinearHorizontalLayoutExecutant();
if (executant)
{
executant->autorelease();
return executant;
}
CC_SAFE_DELETE(executant);
return NULL;
}
RelativeLayoutExecutant* RelativeLayoutExecutant::create()
{
RelativeLayoutExecutant* executant = new RelativeLayoutExecutant();
if (executant)
{
executant->autorelease();
return executant;
}
CC_SAFE_DELETE(executant);
return NULL;
}
void LinearVerticalLayoutExecutant::doLayout()
{
if (_layout)
{
ccArray* layoutChildrenArray = _layout->getChildren()->data;
int length = layoutChildrenArray->num;
Size layoutSize = _layout->getSize();
float topBoundary = layoutSize.height;
for (int i=0; i<length; ++i)
{
UIWidget* child = dynamic_cast<UIWidget*>(layoutChildrenArray->arr[i]);
LinearLayoutParameter* layoutParameter = dynamic_cast<LinearLayoutParameter*>(child->getLayoutParameter());
if (layoutParameter)
{
WidgetType childType = child->getWidgetType();
UILinearGravity childGravity = layoutParameter->getGravity();
Point ap = child->getAnchorPoint();
Size cs = child->getSize();
float finalPosX = childType == WidgetTypeWidget ? ap.x * cs.width : 0.0f;
float finalPosY = childType == WidgetTypeWidget ? topBoundary - ((1.0f-ap.y) * cs.height) : topBoundary - cs.height;
switch (childGravity)
{
case LINEAR_GRAVITY_NONE:
case LINEAR_GRAVITY_LEFT:
break;
case LINEAR_GRAVITY_RIGHT:
finalPosX = childType == WidgetTypeWidget ? layoutSize.width - ((1.0f - ap.x) * cs.width) : layoutSize.width - cs.width;
break;
case LINEAR_GRAVITY_CENTER_HORIZONTAL:
finalPosX = childType == WidgetTypeWidget ? layoutSize.width / 2.0f - cs.width * (0.5f-ap.x) : (layoutSize.width - cs.width) * 0.5f;
break;
default:
break;
}
UIMargin mg = layoutParameter->getMargin();
finalPosX += mg.left;
finalPosY -= mg.top;
child->setPosition(Point(finalPosX, finalPosY));
topBoundary = child->getBottomInParent() - mg.bottom;
}
}
}
}
void LinearHorizontalLayoutExecutant::doLayout()
{
if (_layout)
{
ccArray* layoutChildrenArray = _layout->getChildren()->data;
int length = layoutChildrenArray->num;
Size layoutSize = _layout->getSize();
float leftBoundary = 0.0f;
for (int i=0; i<length; ++i)
{
UIWidget* child = dynamic_cast<UIWidget*>(layoutChildrenArray->arr[i]);
LinearLayoutParameter* layoutParameter = dynamic_cast<LinearLayoutParameter*>(child->getLayoutParameter());
if (layoutParameter)
{
WidgetType childType = child->getWidgetType();
UILinearGravity childGravity = layoutParameter->getGravity();
Point ap = child->getAnchorPoint();
Size cs = child->getSize();
float finalPosX = childType == WidgetTypeWidget ? leftBoundary + (ap.x * cs.width) : leftBoundary;
float finalPosY = childType == WidgetTypeWidget ? layoutSize.height - (1.0f - ap.y) * cs.height : layoutSize.height - cs.height;
switch (childGravity)
{
case LINEAR_GRAVITY_NONE:
case LINEAR_GRAVITY_TOP:
break;
case LINEAR_GRAVITY_BOTTOM:
finalPosY = childType == WidgetTypeWidget ? ap.y * cs.height : 0.0f;
break;
case LINEAR_GRAVITY_CENTER_VERTICAL:
finalPosY = childType == WidgetTypeWidget ? layoutSize.height/2.0f - cs.height * (0.5f - ap.y) : (layoutSize.height - cs.height) * 0.5f;
break;
default:
break;
}
UIMargin mg = layoutParameter->getMargin();
finalPosX += mg.left;
finalPosY -= mg.top;
child->setPosition(Point(finalPosX, finalPosY));
leftBoundary = child->getRightInParent() + mg.right;
}
}
}
}
void RelativeLayoutExecutant::doLayout()
{
if (_layout)
{
ccArray* layoutChildrenArray = _layout->getChildren()->data;
int length = layoutChildrenArray->num;
Size layoutSize = _layout->getSize();
for (int i=0; i<length; i++)
{
UIWidget* child = dynamic_cast<UIWidget*>(layoutChildrenArray->arr[i]);
WidgetType childType = child->getWidgetType();
Point ap = child->getAnchorPoint();
Size cs = child->getSize();
RelativeLayoutParameter* layoutParameter = dynamic_cast<RelativeLayoutParameter*>(child->getLayoutParameter());
if (layoutParameter)
{
float finalPosX = childType == WidgetTypeWidget ? ap.x * cs.width : 0.0f;
float finalPosY = childType == WidgetTypeWidget ? layoutSize.height - ((1.0f - ap.y) * cs.height) : layoutSize.height - cs.height;
UIRelativeAlign align = layoutParameter->getAlign();
const char* relativeName = layoutParameter->getRelativeToWidgetName();
UIWidget* relativeWidget = NULL;
if (relativeName && strcmp(relativeName, ""))
{
relativeWidget = CCUIHELPER->seekWidgetByRelativeName(_layout, relativeName);
}
switch (align)
{
case RELATIVE_ALIGN_NONE:
break;
case RELATIVE_ALIGN_PARENT_LEFT:
break;
case RELATIVE_ALIGN_PARENT_TOP:
break;
case RELATIVE_ALIGN_PARENT_RIGHT:
finalPosX = childType == WidgetTypeWidget ? layoutSize.width - ((1.0f - ap.x) * cs.width) : layoutSize.width - cs.width;
break;
case RELATIVE_ALIGN_PARENT_BOTTOM:
finalPosY = childType == WidgetTypeWidget ? ap.y * cs.height : 0.0f;
break;
case RELATIVE_CENTER_IN_PARENT:
finalPosX = childType == WidgetTypeWidget ? layoutSize.width * 0.5f - cs.width * (0.5f - ap.x) : (layoutSize.width - cs.width) * 0.5f;
finalPosY = childType == WidgetTypeWidget ? layoutSize.height * 0.5f - cs.height * (0.5f - ap.y) : (layoutSize.height - cs.height) * 0.5f;
break;
case RELATIVE_CENTER_HORIZONTAL:
finalPosX = childType == WidgetTypeWidget ? layoutSize.width * 0.5f - cs.width * (0.5f - ap.x) : (layoutSize.width - cs.width) * 0.5f;
break;
case RELATIVE_CENTER_VERTICAL:
finalPosY = childType == WidgetTypeWidget ? layoutSize.height * 0.5f - cs.height * (0.5f - ap.y) : (layoutSize.height - cs.height) * 0.5f;
break;
case RELATIVE_LOCATION_LEFT_OF_TOPALIGN:
if (relativeWidget)
{
float locationTop = relativeWidget->getTopInParent();
float locationRight = relativeWidget->getLeftInParent();
finalPosY = childType == WidgetTypeWidget ? locationTop - ap.y * cs.height : locationTop - cs.height;
finalPosX = childType == WidgetTypeWidget ? locationRight - (1.0f - ap.x) * cs.width : locationRight - cs.width;
}
break;
case RELATIVE_LOCATION_LEFT_OF_CENTER:
break;
case RELATIVE_LOCATION_LEFT_OF_BOTTOMALIGN:
if (relativeWidget)
{
float locationRight = relativeWidget->getLeftInParent();
float locationBottom = relativeWidget->getBottomInParent();
finalPosY = childType == WidgetTypeWidget ? locationBottom + ap.y * cs.height : locationBottom;
finalPosX = childType == WidgetTypeWidget ? locationRight - (1.0f - ap.x) * cs.width : locationRight - cs.width;
}
break;
case RELATIVE_LOCATION_RIGHT_OF_TOPALIGN:
if (relativeWidget)
{
float locationTop = relativeWidget->getTopInParent();
float locationLeft = relativeWidget->getRightInParent();
finalPosY = childType == WidgetTypeWidget ? locationTop - ap.y * cs.height : locationTop - cs.height;
finalPosX = childType == WidgetTypeWidget ? locationLeft + ap.x * cs.width : locationLeft;
}
break;
case RELATIVE_LOCATION_RIGHT_OF_CENTER:
break;
case RELATIVE_LOCATION_RIGHT_OF_BOTTOMALIGN:
if (relativeWidget)
{
float locationLeft = relativeWidget->getRightInParent();
float locationBottom = relativeWidget->getBottomInParent();
finalPosY = childType == WidgetTypeWidget ? locationBottom + ap.y * cs.height : locationBottom;
finalPosX = childType == WidgetTypeWidget ? locationLeft + ap.x * cs.width : locationLeft;
}
break;
case RELATIVE_LOCATION_ABOVE_LEFTALIGN:
if (relativeWidget)
{
float locationBottom = relativeWidget->getTopInParent();
float locationLeft = relativeWidget->getLeftInParent();
finalPosY = childType == WidgetTypeWidget ? locationBottom + ap.y * cs.height : locationBottom;
finalPosX = childType == WidgetTypeWidget ? locationLeft + ap.x * cs.width : locationLeft;
}
break;
case RELATIVE_LOCATION_ABOVE_CENTER:
break;
case RELATIVE_LOCATION_ABOVE_RIGHTALIGN:
if (relativeWidget)
{
float locationBottom = relativeWidget->getTopInParent();
float locationRight = relativeWidget->getRightInParent();
finalPosY = childType == WidgetTypeWidget ? locationBottom + ap.y * cs.height : locationBottom;
finalPosX = childType == WidgetTypeWidget ? locationRight - (1.0f - ap.x) * cs.width : locationRight - cs.width;
}
break;
case RELATIVE_LOCATION_BELOW_LEFTALIGN:
if (relativeWidget)
{
float locationTop = relativeWidget->getBottomInParent();
float locationLeft = relativeWidget->getLeftInParent();
finalPosY = childType == WidgetTypeWidget ? locationTop - (1.0f - ap.y) * cs.height : locationTop - cs.height;
finalPosX = childType == WidgetTypeWidget ? locationLeft + ap.x * cs.width : locationLeft;
}
break;
case RELATIVE_LOCATION_BELOW_CENTER:
break;
case RELATIVE_LOCATION_BELOW_RIGHTALIGN:
if (relativeWidget)
{
float locationTop = relativeWidget->getBottomInParent();
float locationRight = relativeWidget->getRightInParent();
finalPosY = childType == WidgetTypeWidget ? locationTop - (1.0f - ap.y) * cs.height : locationTop - cs.height;
finalPosX = childType == WidgetTypeWidget ? locationRight - (1.0f - ap.x) * cs.width : locationRight - cs.width;
}
break;
default:
break;
}
UIMargin relativeWidgetMargin;
UIMargin mg;
if (relativeWidget)
{
relativeWidgetMargin = relativeWidget->getLayoutParameter()->getMargin();
mg = child->getLayoutParameter()->getMargin();
}
//handle margin
switch (align)
{
case RELATIVE_LOCATION_ABOVE_LEFTALIGN:
case RELATIVE_LOCATION_ABOVE_RIGHTALIGN:
case RELATIVE_LOCATION_ABOVE_CENTER:
finalPosY += relativeWidgetMargin.top;
finalPosY += mg.bottom;
break;
case RELATIVE_LOCATION_BELOW_LEFTALIGN:
case RELATIVE_LOCATION_BELOW_RIGHTALIGN:
case RELATIVE_LOCATION_BELOW_CENTER:
finalPosY -= relativeWidgetMargin.bottom;
finalPosY -= mg.top;
break;
case RELATIVE_LOCATION_LEFT_OF_TOPALIGN:
case RELATIVE_LOCATION_LEFT_OF_BOTTOMALIGN:
case RELATIVE_LOCATION_LEFT_OF_CENTER:
finalPosX -= relativeWidgetMargin.left;
finalPosX -= mg.right;
break;
case RELATIVE_LOCATION_RIGHT_OF_TOPALIGN:
case RELATIVE_LOCATION_RIGHT_OF_BOTTOMALIGN:
case RELATIVE_LOCATION_RIGHT_OF_CENTER:
finalPosX += relativeWidgetMargin.right;
finalPosX += mg.left;
break;
default:
break;
}
child->setPosition(Point(finalPosX, finalPosY));
}
}
}
}
NS_CC_EXT_END

View File

@ -1,158 +0,0 @@
/****************************************************************************
Copyright (c) 2013 cocos2d-x.org
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
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#ifndef __LAYOUTEXECUTANT_H__
#define __LAYOUTEXECUTANT_H__
#include "LayoutParameter.h"
NS_CC_EXT_BEGIN
typedef enum
{
LAYOUT_DEFAULT,
LAYOUT_LINEAR_VERTICAL,
LAYOUT_LINEAR_HORIZONTAL,
LAYOUT_RELATIVE
}LayoutType;
class Layout;
class LayoutExecutant : public Object
{
public:
/**
* Default constructor
*/
LayoutExecutant() : _layout(NULL){_layoutType = LAYOUT_DEFAULT;};
/**
* Default destructor
*/
virtual ~LayoutExecutant(){_layout = NULL;};
/**
* To do layout. Need to be overrided.
*/
virtual void doLayout()=0;
/**
* Gets LayoutType.
*
* @see LayoutType
*
* @return LayoutType
*/
LayoutType getLayoutType(){return _layoutType;};
/**
* Binding a Layout to LayoutExecutant.
*
* @param Layout
*/
void setLayout(Layout* layout);
/**
* Gets the Layout of LayoutExecutant.
*
* @return Layout
*/
Layout* getLayout() const;
protected:
LayoutType _layoutType;
Layout* _layout;
};
class LinearVerticalLayoutExecutant : public LayoutExecutant
{
public:
/**
* Default constructor
*/
LinearVerticalLayoutExecutant(){_layoutType = LAYOUT_LINEAR_VERTICAL;};
/**
* Default destructor
*/
virtual ~LinearVerticalLayoutExecutant(){};
/**
* Allocates and initializes.
* @return A initialized LayoutExecutant which is marked as "autorelease".
*/
static LinearVerticalLayoutExecutant* create();
//To do layout.
virtual void doLayout();
};
class LinearHorizontalLayoutExecutant : public LayoutExecutant
{
public:
/**
* Default constructor
*/
LinearHorizontalLayoutExecutant(){_layoutType = LAYOUT_LINEAR_HORIZONTAL;};
/**
* Default destructor
*/
virtual ~LinearHorizontalLayoutExecutant(){};
/**
* Allocates and initializes.
* @return A initialized LayoutExecutant which is marked as "autorelease".
*/
static LinearHorizontalLayoutExecutant* create();
//To do layout.
virtual void doLayout();
};
class RelativeLayoutExecutant : public LayoutExecutant
{
public:
/**
* Default constructor
*/
RelativeLayoutExecutant(){_layoutType = LAYOUT_RELATIVE;};
/**
* Default destructor
*/
virtual ~RelativeLayoutExecutant(){};
/**
* Allocates and initializes.
* @return A initialized LayoutExecutant which is marked as "autorelease".
*/
static RelativeLayoutExecutant* create();
//To do layout.
virtual void doLayout();
};
NS_CC_EXT_END
#endif /* defined(__LayoutExecutant__) */

View File

@ -51,11 +51,7 @@ void LayoutParameter::setMargin(const UIMargin &margin)
Layout* containerParent = dynamic_cast<Layout*>(parent);
if (containerParent)
{
LayoutExecutant* exe = containerParent->getLayoutExecutant();
if (exe)
{
exe->doLayout();
}
containerParent->doLayout();
}
}
}
@ -95,10 +91,9 @@ void LinearLayoutParameter::setGravity(UILinearGravity gravity)
Layout* containerParent = dynamic_cast<Layout*>(parent);
if (containerParent)
{
LayoutExecutant* exe = containerParent->getLayoutExecutant();
if (exe && (exe->getLayoutType() == LAYOUT_LINEAR_HORIZONTAL || exe->getLayoutType() == LAYOUT_LINEAR_VERTICAL))
if ((containerParent->getLayoutType() == LAYOUT_LINEAR_HORIZONTAL || containerParent->getLayoutType() == LAYOUT_LINEAR_VERTICAL))
{
exe->doLayout();
containerParent->doLayout();
}
}
}
@ -134,10 +129,9 @@ void RelativeLayoutParameter::setAlign(UIRelativeAlign align)
Layout* containerParent = dynamic_cast<Layout*>(parent);
if (containerParent)
{
LayoutExecutant* exe = containerParent->getLayoutExecutant();
if (exe && (exe->getLayoutType() == LAYOUT_RELATIVE))
if ((containerParent->getLayoutType() == LAYOUT_RELATIVE))
{
exe->doLayout();
containerParent->doLayout();
}
}
}
@ -161,10 +155,9 @@ void RelativeLayoutParameter::setRelativeToWidgetName(const char *name)
Layout* containerParent = dynamic_cast<Layout*>(parent);
if (containerParent)
{
LayoutExecutant* exe = containerParent->getLayoutExecutant();
if (exe && (exe->getLayoutType() == LAYOUT_RELATIVE))
if ((containerParent->getLayoutType() == LAYOUT_RELATIVE))
{
exe->doLayout();
containerParent->doLayout();
}
}
}
@ -188,10 +181,9 @@ void RelativeLayoutParameter::setRelativeName(const char* name)
Layout* containerParent = dynamic_cast<Layout*>(parent);
if (containerParent)
{
LayoutExecutant* exe = containerParent->getLayoutExecutant();
if (exe && (exe->getLayoutType() == LAYOUT_RELATIVE))
if ((containerParent->getLayoutType() == LAYOUT_RELATIVE))
{
exe->doLayout();
containerParent->doLayout();
}
}
}

View File

@ -45,7 +45,6 @@
#include "UIHelper.h"
#include "../../Reader/CCSGUIReader.h"
#include "UILayer.h"
#include "../Layouts/LayoutExecutant.h"
NS_CC_EXT_BEGIN
const char* CocosGUIVersion();

View File

@ -1244,6 +1244,21 @@ Layout* UIDragPanel::getInnerContainer()
return _innerContainer;
}
void UIDragPanel::setLayoutType(LayoutType type)
{
_innerContainer->setLayoutType(type);
}
LayoutType UIDragPanel::getLayoutType() const
{
return _innerContainer->getLayoutType();
}
void UIDragPanel::doLayout()
{
_innerContainer->doLayout();
}
const char* UIDragPanel::getDescription() const
{
return "DragPanel";

View File

@ -191,6 +191,26 @@ public:
*/
Layout* getInnerContainer();
/**
* Sets LayoutType.
*
* @see LayoutType
*
* @param LayoutType
*/
virtual void setLayoutType(LayoutType type);
/**
* Gets LayoutType.
*
* @see LayoutType
*
* @return LayoutType
*/
virtual LayoutType getLayoutType() const;
virtual void doLayout();
/**
* Returns the "class name" of widget.
*/

View File

@ -139,6 +139,8 @@ public:
/**/
virtual void update(float dt);
virtual void doLayout(){};
/**
* Returns the "class name" of widget.
*/

View File

@ -138,6 +138,8 @@ public:
//override "update" method of widget.
virtual void update(float dt);
virtual void doLayout(){};
/**
* Returns the "class name" of widget.

View File

@ -664,14 +664,19 @@ Layout* UIScrollView::getInnerContainer()
return _innerContainer;
}
void UIScrollView::setLayoutExecutant(LayoutExecutant *exe)
void UIScrollView::setLayoutType(LayoutType type)
{
_innerContainer->setLayoutExecutant(exe);
_innerContainer->setLayoutType(type);
}
LayoutExecutant* UIScrollView::getLayoutExecutant() const
LayoutType UIScrollView::getLayoutType() const
{
return _innerContainer->getLayoutExecutant();
return _innerContainer->getLayoutType();
}
void UIScrollView::doLayout()
{
_innerContainer->doLayout();
}
const char* UIScrollView::getDescription() const

View File

@ -135,13 +135,7 @@ public:
* Add call back function called scrollview event triggered
*/
void addEventListener(Object* target, SEL_ScrollViewEvent selector);
//override "setLayoutExecutant" method of widget.
virtual void setLayoutExecutant(LayoutExecutant* exe);
//override "getLayoutExecutant" method of widget.
virtual LayoutExecutant* getLayoutExecutant() const;
//override "addChild" method of widget.
virtual bool addChild(UIWidget* widget);
@ -171,6 +165,26 @@ public:
virtual void update(float dt);
/**
* Sets LayoutType.
*
* @see LayoutType
*
* @param LayoutType
*/
virtual void setLayoutType(LayoutType type);
/**
* Gets LayoutType.
*
* @see LayoutType
*
* @return LayoutType
*/
virtual LayoutType getLayoutType() const;
virtual void doLayout();
/**
* Returns the "class name" of widget.
*/

View File

@ -86,7 +86,6 @@ SOURCES = ../CCBReader/CCBFileLoader.cpp \
../CocoStudio/GUI/BaseClasses/UIRootWidget.cpp \
../CocoStudio/GUI/BaseClasses/UIWidget.cpp \
../CocoStudio/GUI/Layouts/Layout.cpp \
../CocoStudio/GUI/Layouts/LayoutExecutant.cpp \
../CocoStudio/GUI/Layouts/LayoutParameter.cpp \
../CocoStudio/GUI/Layouts/UILayoutDefine.cpp \
../CocoStudio/GUI/System/CocosGUI.cpp \

View File

@ -84,7 +84,6 @@ SOURCES = ../CCBReader/CCBFileLoader.cpp \
../CocoStudio/GUI/BaseClasses/UIRootWidget.cpp \
../CocoStudio/GUI/BaseClasses/UIWidget.cpp \
../CocoStudio/GUI/Layouts/Layout.cpp \
../CocoStudio/GUI/Layouts/LayoutExecutant.cpp \
../CocoStudio/GUI/Layouts/LayoutParameter.cpp \
../CocoStudio/GUI/Layouts/UILayoutDefine.cpp \
../CocoStudio/GUI/System/CocosGUI.cpp \

View File

@ -71,7 +71,6 @@ EXTENSIONS_SOURCES = ../CCBReader/CCBFileLoader.cpp \
../CocoStudio/GUI/BaseClasses/UIRootWidget.cpp \
../CocoStudio/GUI/BaseClasses/UIWidget.cpp \
../CocoStudio/GUI/Layouts/Layout.cpp \
../CocoStudio/GUI/Layouts/LayoutExecutant.cpp \
../CocoStudio/GUI/Layouts/LayoutParameter.cpp \
../CocoStudio/GUI/Layouts/UILayoutDefine.cpp \
../CocoStudio/GUI/System/CocosGUI.cpp \

View File

@ -149,7 +149,6 @@
<ClCompile Include="..\CocoStudio\GUI\BaseClasses\UIRootWidget.cpp" />
<ClCompile Include="..\CocoStudio\GUI\BaseClasses\UIWidget.cpp" />
<ClCompile Include="..\CocoStudio\GUI\Layouts\Layout.cpp" />
<ClCompile Include="..\CocoStudio\GUI\Layouts\LayoutExecutant.cpp" />
<ClCompile Include="..\CocoStudio\GUI\Layouts\LayoutParameter.cpp" />
<ClCompile Include="..\CocoStudio\GUI\Layouts\UILayoutDefine.cpp" />
<ClCompile Include="..\CocoStudio\GUI\System\CocosGUI.cpp" />
@ -284,7 +283,6 @@
<ClInclude Include="..\CocoStudio\GUI\BaseClasses\UIRootWidget.h" />
<ClInclude Include="..\CocoStudio\GUI\BaseClasses\UIWidget.h" />
<ClInclude Include="..\CocoStudio\GUI\Layouts\Layout.h" />
<ClInclude Include="..\CocoStudio\GUI\Layouts\LayoutExecutant.h" />
<ClInclude Include="..\CocoStudio\GUI\Layouts\LayoutParameter.h" />
<ClInclude Include="..\CocoStudio\GUI\Layouts\UILayoutDefine.h" />
<ClInclude Include="..\CocoStudio\GUI\System\CocosGUI.h" />

View File

@ -436,9 +436,6 @@
<ClCompile Include="..\CocoStudio\GUI\Layouts\Layout.cpp">
<Filter>CocoStudio\GUI\Layouts</Filter>
</ClCompile>
<ClCompile Include="..\CocoStudio\GUI\Layouts\LayoutExecutant.cpp">
<Filter>CocoStudio\GUI\Layouts</Filter>
</ClCompile>
<ClCompile Include="..\CocoStudio\GUI\Layouts\LayoutParameter.cpp">
<Filter>CocoStudio\GUI\Layouts</Filter>
</ClCompile>
@ -950,9 +947,6 @@
<ClInclude Include="..\CocoStudio\GUI\Layouts\Layout.h">
<Filter>CocoStudio\GUI\Layouts</Filter>
</ClInclude>
<ClInclude Include="..\CocoStudio\GUI\Layouts\LayoutExecutant.h">
<Filter>CocoStudio\GUI\Layouts</Filter>
</ClInclude>
<ClInclude Include="..\CocoStudio\GUI\Layouts\LayoutParameter.h">
<Filter>CocoStudio\GUI\Layouts</Filter>
</ClInclude>

View File

@ -362,6 +362,7 @@ bool UIPanelTest_Layout_Linear_Vertical::init()
// Create the layout
Layout* layout = Layout::create();
layout->setLayoutType(LAYOUT_LINEAR_VERTICAL);
layout->setSize(Size(280, 150));
Size backgroundSize = background->getSize();
layout->setPosition(Point((widgetSize.width - backgroundSize.width) / 2 +
@ -407,9 +408,7 @@ bool UIPanelTest_Layout_Linear_Vertical::init()
lp3->setMargin(UIMargin(0, 10, 0, 10));
LinearVerticalLayoutExecutant* exe = LinearVerticalLayoutExecutant::create();
layout->setLayoutExecutant(exe);
exe->doLayout();
layout->doLayout();
return true;
}
@ -446,6 +445,7 @@ bool UIPanelTest_Layout_Linear_Horizontal::init()
// Create the layout
Layout* layout = Layout::create();
layout->setLayoutType(LAYOUT_LINEAR_HORIZONTAL);
layout->setClippingEnabled(true);
layout->setSize(Size(280, 150));
Size backgroundSize = background->getSize();
@ -491,9 +491,7 @@ bool UIPanelTest_Layout_Linear_Horizontal::init()
lp3->setMargin(UIMargin(0, 10, 0, 10));
LinearHorizontalLayoutExecutant* exe = LinearHorizontalLayoutExecutant::create();
layout->setLayoutExecutant(exe);
exe->doLayout();
layout->doLayout();
return true;
}
@ -530,6 +528,7 @@ bool UIPanelTest_Layout_Relative::init()
// Create the layout
Layout* layout = Layout::create();
layout->setLayoutType(LAYOUT_RELATIVE);
layout->setSize(Size(280, 150));
layout->setBackGroundColorType(LAYOUT_COLOR_SOLID);
layout->setBackGroundColor(Color3B::GREEN);
@ -574,9 +573,7 @@ bool UIPanelTest_Layout_Relative::init()
rp3->setAlign(RELATIVE_ALIGN_PARENT_RIGHT);
RelativeLayoutExecutant* exe = RelativeLayoutExecutant::create();
layout->setLayoutExecutant(exe);
exe->doLayout();
layout->doLayout();
return true;
}