axmol/cocos/gui/layouts/Layout.cpp

785 lines
28 KiB
C++
Raw Normal View History

2013-09-13 22:20:20 +08:00
/****************************************************************************
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 "layout.h"
#include "system/UILayer.h"
#include "system/UIHelper.h"
#include "GUI/CCControlExtension/CCScale9Sprite.h"
2013-09-13 22:20:20 +08:00
using namespace cocos2d;
using namespace cocos2d::extension;
namespace gui {
2013-09-13 22:20:20 +08:00
2013-09-16 15:32:52 +08:00
#define DYNAMIC_CAST_CLIPPINGLAYER dynamic_cast<RectClippingNode*>(_renderer)
2013-09-13 22:20:20 +08:00
Layout::Layout():
2013-09-16 20:54:13 +08:00
_clippingEnabled(false),
_backGroundScale9Enabled(false),
_backGroundImage(NULL),
_backGroundImageFileName(""),
_backGroundImageCapInsets(Rect::ZERO),
_colorType(LAYOUT_COLOR_NONE),
_bgImageTexType(UI_TEX_TYPE_LOCAL),
_colorRender(NULL),
_gradientRender(NULL),
_cColor(Color3B::WHITE),
_gStartColor(Color3B::WHITE),
_gEndColor(Color3B::WHITE),
_alongVector(Point(0.0f, -1.0f)),
_cOpacity(255),
2013-09-23 19:16:20 +08:00
_backGroundImageTextureSize(Size::ZERO),
2013-09-23 21:26:44 +08:00
_layoutType(LAYOUT_ABSOLUTE)
2013-09-13 22:20:20 +08:00
{
2013-09-16 15:32:52 +08:00
_widgetType = WidgetTypeContainer;
2013-09-13 22:20:20 +08:00
}
Layout::~Layout()
{
}
Layout* Layout::create()
{
Layout* layout = new Layout();
if (layout && layout->init())
{
layout->autorelease();
return layout;
}
CC_SAFE_DELETE(layout);
return NULL;
}
bool Layout::init()
{
2013-09-16 15:32:52 +08:00
_children = CCArray::create();
_children->retain();
2013-09-13 22:20:20 +08:00
initRenderer();
2013-09-16 15:32:52 +08:00
_renderer->retain();
_renderer->setZOrder(_widgetZOrder);
RGBAProtocol* renderRGBA = dynamic_cast<RGBAProtocol*>(_renderer);
2013-09-13 22:20:20 +08:00
if (renderRGBA)
{
renderRGBA->setCascadeColorEnabled(false);
renderRGBA->setCascadeOpacityEnabled(false);
}
ignoreContentAdaptWithSize(false);
setSize(Size::ZERO);
setBright(true);
setAnchorPoint(Point(0, 0));
2013-09-16 15:32:52 +08:00
_scheduler = Director::getInstance()->getScheduler();
CC_SAFE_RETAIN(_scheduler);
2013-09-13 22:20:20 +08:00
return true;
}
void Layout::initRenderer()
{
2013-09-16 15:32:52 +08:00
_renderer = RectClippingNode::create();
2013-09-13 22:20:20 +08:00
}
bool Layout::isClippingEnabled()
{
2013-09-16 20:54:13 +08:00
return _clippingEnabled;
2013-09-13 22:20:20 +08:00
}
bool Layout::hitTest(const Point &pt)
{
2013-09-16 15:32:52 +08:00
Point nsp = _renderer->convertToNodeSpace(pt);
Rect bb = Rect(0.0f, 0.0f, _size.width, _size.height);
2013-09-13 22:20:20 +08:00
if (nsp.x >= bb.origin.x && nsp.x <= bb.origin.x + bb.size.width && nsp.y >= bb.origin.y && nsp.y <= bb.origin.y + bb.size.height)
{
return true;
}
return false;
}
void Layout::setClippingEnabled(bool able)
{
2013-09-16 20:54:13 +08:00
_clippingEnabled = able;
2013-09-13 22:20:20 +08:00
DYNAMIC_CAST_CLIPPINGLAYER->setClippingEnabled(able);
}
void Layout::onSizeChanged()
{
2013-09-16 15:32:52 +08:00
DYNAMIC_CAST_CLIPPINGLAYER->setClippingSize(_size);
2013-09-23 19:16:20 +08:00
doLayout();
2013-09-16 20:54:13 +08:00
if (_backGroundImage)
2013-09-13 22:20:20 +08:00
{
2013-09-16 20:54:13 +08:00
_backGroundImage->setPosition(Point(_size.width/2.0f, _size.height/2.0f));
if (_backGroundScale9Enabled)
2013-09-13 22:20:20 +08:00
{
2013-09-16 20:54:13 +08:00
dynamic_cast<Scale9Sprite*>(_backGroundImage)->setPreferredSize(_size);
2013-09-13 22:20:20 +08:00
}
}
2013-09-16 20:54:13 +08:00
if (_colorRender)
2013-09-13 22:20:20 +08:00
{
2013-09-16 20:54:13 +08:00
_colorRender->setContentSize(_size);
2013-09-13 22:20:20 +08:00
}
2013-09-16 20:54:13 +08:00
if (_gradientRender)
2013-09-13 22:20:20 +08:00
{
2013-09-16 20:54:13 +08:00
_gradientRender->setContentSize(_size);
2013-09-13 22:20:20 +08:00
}
}
void Layout::setBackGroundImageScale9Enabled(bool able)
{
2013-09-16 20:54:13 +08:00
if (_backGroundScale9Enabled == able)
2013-09-13 22:20:20 +08:00
{
return;
}
2013-09-16 20:54:13 +08:00
_renderer->removeChild(_backGroundImage, true);
_backGroundImage = NULL;
_backGroundScale9Enabled = able;
if (_backGroundScale9Enabled)
2013-09-13 22:20:20 +08:00
{
2013-09-16 20:54:13 +08:00
_backGroundImage = Scale9Sprite::create();
_renderer->addChild(_backGroundImage);
2013-09-13 22:20:20 +08:00
}
else
{
2013-09-16 20:54:13 +08:00
_backGroundImage = Sprite::create();
_renderer->addChild(_backGroundImage);
2013-09-13 22:20:20 +08:00
}
2013-09-16 20:54:13 +08:00
_backGroundImage->setZOrder(-1);
setBackGroundImage(_backGroundImageFileName.c_str(),_bgImageTexType);
setBackGroundImageCapInsets(_backGroundImageCapInsets);
2013-09-13 22:20:20 +08:00
}
void Layout::setBackGroundImage(const char* fileName,TextureResType texType)
{
if (!fileName || strcmp(fileName, "") == 0)
{
return;
}
2013-09-16 20:54:13 +08:00
if (_backGroundImage == NULL)
2013-09-13 22:20:20 +08:00
{
addBackGroundImage();
}
2013-09-16 20:54:13 +08:00
_backGroundImageFileName = fileName;
_bgImageTexType = texType;
if (_backGroundScale9Enabled)
2013-09-13 22:20:20 +08:00
{
2013-09-16 20:54:13 +08:00
switch (_bgImageTexType)
2013-09-13 22:20:20 +08:00
{
case UI_TEX_TYPE_LOCAL:
2013-09-16 20:54:13 +08:00
dynamic_cast<Scale9Sprite*>(_backGroundImage)->initWithFile(fileName);
2013-09-13 22:20:20 +08:00
break;
case UI_TEX_TYPE_PLIST:
2013-09-16 20:54:13 +08:00
dynamic_cast<Scale9Sprite*>(_backGroundImage)->initWithSpriteFrameName(fileName);
2013-09-13 22:20:20 +08:00
break;
default:
break;
}
2013-09-16 20:54:13 +08:00
dynamic_cast<Scale9Sprite*>(_backGroundImage)->setPreferredSize(_size);
2013-09-13 22:20:20 +08:00
}
else
{
2013-09-16 20:54:13 +08:00
switch (_bgImageTexType)
2013-09-13 22:20:20 +08:00
{
case UI_TEX_TYPE_LOCAL:
2013-09-16 20:54:13 +08:00
dynamic_cast<Sprite*>(_backGroundImage)->initWithFile(fileName);
2013-09-13 22:20:20 +08:00
break;
case UI_TEX_TYPE_PLIST:
2013-09-16 20:54:13 +08:00
dynamic_cast<Sprite*>(_backGroundImage)->initWithSpriteFrameName(fileName);
2013-09-13 22:20:20 +08:00
break;
default:
break;
}
}
2013-09-16 20:54:13 +08:00
if (_backGroundScale9Enabled)
2013-09-13 22:20:20 +08:00
{
2013-09-16 20:54:13 +08:00
dynamic_cast<Scale9Sprite*>(_backGroundImage)->setColor(getColor());
dynamic_cast<Scale9Sprite*>(_backGroundImage)->setOpacity(getOpacity());
2013-09-13 22:20:20 +08:00
}
else
{
2013-09-16 20:54:13 +08:00
dynamic_cast<Sprite*>(_backGroundImage)->setColor(getColor());
dynamic_cast<Sprite*>(_backGroundImage)->setOpacity(getOpacity());
2013-09-13 22:20:20 +08:00
}
2013-09-16 20:54:13 +08:00
_backGroundImageTextureSize = _backGroundImage->getContentSize();
_backGroundImage->setPosition(Point(_size.width/2.0f, _size.height/2.0f));
2013-09-13 22:20:20 +08:00
}
void Layout::setBackGroundImageCapInsets(const Rect &capInsets)
{
2013-09-16 20:54:13 +08:00
_backGroundImageCapInsets = capInsets;
if (_backGroundScale9Enabled)
2013-09-13 22:20:20 +08:00
{
2013-09-16 20:54:13 +08:00
dynamic_cast<Scale9Sprite*>(_backGroundImage)->setCapInsets(capInsets);
2013-09-13 22:20:20 +08:00
}
}
void Layout::addBackGroundImage()
{
2013-09-16 20:54:13 +08:00
if (_backGroundScale9Enabled)
2013-09-13 22:20:20 +08:00
{
2013-09-16 20:54:13 +08:00
_backGroundImage = Scale9Sprite::create();
_backGroundImage->setZOrder(-1);
_renderer->addChild(_backGroundImage);
dynamic_cast<Scale9Sprite*>(_backGroundImage)->setPreferredSize(_size);
2013-09-13 22:20:20 +08:00
}
else
{
2013-09-16 20:54:13 +08:00
_backGroundImage = CCSprite::create();
_backGroundImage->setZOrder(-1);
_renderer->addChild(_backGroundImage);
2013-09-13 22:20:20 +08:00
}
2013-09-16 20:54:13 +08:00
_backGroundImage->setPosition(Point(_size.width/2.0f, _size.height/2.0f));
2013-09-13 22:20:20 +08:00
}
void Layout::removeBackGroundImage()
{
2013-09-16 20:54:13 +08:00
if (!_backGroundImage)
2013-09-13 22:20:20 +08:00
{
return;
}
2013-09-16 20:54:13 +08:00
_renderer->removeChild(_backGroundImage, true);
_backGroundImage = NULL;
_backGroundImageFileName = "";
_backGroundImageTextureSize = Size::ZERO;
2013-09-13 22:20:20 +08:00
}
void Layout::setBackGroundColorType(LayoutBackGroundColorType type)
{
2013-09-16 20:54:13 +08:00
if (_colorType == type)
2013-09-13 22:20:20 +08:00
{
return;
}
2013-09-16 20:54:13 +08:00
switch (_colorType)
2013-09-13 22:20:20 +08:00
{
case LAYOUT_COLOR_NONE:
2013-09-16 20:54:13 +08:00
if (_colorRender)
2013-09-13 22:20:20 +08:00
{
2013-09-16 20:54:13 +08:00
_renderer->removeChild(_colorRender, true);
_colorRender = NULL;
2013-09-13 22:20:20 +08:00
}
2013-09-16 20:54:13 +08:00
if (_gradientRender)
2013-09-13 22:20:20 +08:00
{
2013-09-16 20:54:13 +08:00
_renderer->removeChild(_gradientRender, true);
_gradientRender = NULL;
2013-09-13 22:20:20 +08:00
}
break;
case LAYOUT_COLOR_SOLID:
2013-09-16 20:54:13 +08:00
if (_colorRender)
2013-09-13 22:20:20 +08:00
{
2013-09-16 20:54:13 +08:00
_renderer->removeChild(_colorRender, true);
_colorRender = NULL;
2013-09-13 22:20:20 +08:00
}
break;
case LAYOUT_COLOR_GRADIENT:
2013-09-16 20:54:13 +08:00
if (_gradientRender)
2013-09-13 22:20:20 +08:00
{
2013-09-16 20:54:13 +08:00
_renderer->removeChild(_gradientRender, true);
_gradientRender = NULL;
2013-09-13 22:20:20 +08:00
}
break;
default:
break;
}
2013-09-16 20:54:13 +08:00
_colorType = type;
switch (_colorType)
2013-09-13 22:20:20 +08:00
{
case LAYOUT_COLOR_NONE:
break;
case LAYOUT_COLOR_SOLID:
2013-09-16 20:54:13 +08:00
_colorRender = CCLayerColor::create();
_colorRender->setContentSize(_size);
_colorRender->setOpacity(_cOpacity);
_colorRender->setColor(_cColor);
_renderer->addChild(_colorRender,-2);
2013-09-13 22:20:20 +08:00
break;
case LAYOUT_COLOR_GRADIENT:
2013-09-16 20:54:13 +08:00
_gradientRender = CCLayerGradient::create();
_gradientRender->setContentSize(_size);
_gradientRender->setOpacity(_cOpacity);
_gradientRender->setStartColor(_gStartColor);
_gradientRender->setEndColor(_gEndColor);
_gradientRender->setVector(_alongVector);
_renderer->addChild(_gradientRender,-2);
2013-09-13 22:20:20 +08:00
break;
default:
break;
}
}
void Layout::setBackGroundColor(const Color3B &color)
{
2013-09-16 20:54:13 +08:00
_cColor = color;
if (_colorRender)
2013-09-13 22:20:20 +08:00
{
2013-09-16 20:54:13 +08:00
_colorRender->setColor(color);
2013-09-13 22:20:20 +08:00
}
}
void Layout::setBackGroundColor(const Color3B &startColor, const Color3B &endColor)
{
2013-09-16 20:54:13 +08:00
_gStartColor = startColor;
if (_gradientRender)
2013-09-13 22:20:20 +08:00
{
2013-09-16 20:54:13 +08:00
_gradientRender->setStartColor(startColor);
2013-09-13 22:20:20 +08:00
}
2013-09-16 20:54:13 +08:00
_gEndColor = endColor;
if (_gradientRender)
2013-09-13 22:20:20 +08:00
{
2013-09-16 20:54:13 +08:00
_gradientRender->setEndColor(endColor);
2013-09-13 22:20:20 +08:00
}
}
void Layout::setBackGroundColorOpacity(int opacity)
{
2013-09-16 20:54:13 +08:00
_cOpacity = opacity;
switch (_colorType)
2013-09-13 22:20:20 +08:00
{
case LAYOUT_COLOR_NONE:
break;
case LAYOUT_COLOR_SOLID:
2013-09-16 20:54:13 +08:00
_colorRender->setOpacity(opacity);
2013-09-13 22:20:20 +08:00
break;
case LAYOUT_COLOR_GRADIENT:
2013-09-16 20:54:13 +08:00
_gradientRender->setOpacity(opacity);
2013-09-13 22:20:20 +08:00
break;
default:
break;
}
}
void Layout::setBackGroundColorVector(const Point &vector)
{
2013-09-16 20:54:13 +08:00
_alongVector = vector;
if (_gradientRender)
2013-09-13 22:20:20 +08:00
{
2013-09-16 20:54:13 +08:00
_gradientRender->setVector(vector);
2013-09-13 22:20:20 +08:00
}
}
void Layout::setColor(const Color3B &color)
{
UIWidget::setColor(color);
2013-09-16 20:54:13 +08:00
if (_backGroundImage)
2013-09-13 22:20:20 +08:00
{
2013-09-16 20:54:13 +08:00
RGBAProtocol* rgbap = dynamic_cast<RGBAProtocol*>(_backGroundImage);
2013-09-13 22:20:20 +08:00
if (rgbap)
{
rgbap->setColor(color);
}
}
}
void Layout::setOpacity(int opacity)
{
UIWidget::setOpacity(opacity);
2013-09-16 20:54:13 +08:00
if (_backGroundImage)
2013-09-13 22:20:20 +08:00
{
2013-09-16 20:54:13 +08:00
RGBAProtocol* rgbap = dynamic_cast<RGBAProtocol*>(_backGroundImage);
2013-09-13 22:20:20 +08:00
if (rgbap)
{
rgbap->setOpacity(opacity);
}
}
}
const Size& Layout::getBackGroundImageTextureSize() const
{
2013-09-16 20:54:13 +08:00
return _backGroundImageTextureSize;
2013-09-13 22:20:20 +08:00
}
const Size& Layout::getContentSize() const
{
2013-09-16 15:32:52 +08:00
return _renderer->getContentSize();
2013-09-13 22:20:20 +08:00
}
2013-09-23 19:16:20 +08:00
void Layout::setLayoutType(LayoutType type)
{
_layoutType = type;
}
LayoutType Layout::getLayoutType() const
{
return _layoutType;
}
void Layout::doLayout()
{
switch (_layoutType)
{
2013-09-23 21:26:44 +08:00
case LAYOUT_ABSOLUTE:
2013-09-23 19:16:20 +08:00
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;
}
}
2013-09-17 17:59:20 +08:00
const char* Layout::getDescription() const
{
return "Layout";
}
2013-09-13 22:20:20 +08:00
RectClippingNode::RectClippingNode():
m_pInnerStencil(NULL),
2013-09-16 20:54:13 +08:00
_enabled(true),
_clippingSize(Size(50.0f, 50.0f)),
_clippingEnabled(false)
2013-09-13 22:20:20 +08:00
{
}
RectClippingNode::~RectClippingNode()
{
}
RectClippingNode* RectClippingNode::create()
{
RectClippingNode *pRet = new RectClippingNode();
if (pRet && pRet->init())
{
pRet->autorelease();
}
else
{
CC_SAFE_DELETE(pRet);
}
return pRet;
}
bool RectClippingNode::init()
{
m_pInnerStencil = CCDrawNode::create();
rect[0] = Point(0, 0);
2013-09-16 20:54:13 +08:00
rect[1] = Point(_clippingSize.width, 0);
rect[2] = Point(_clippingSize.width, _clippingSize.height);
rect[3] = Point(0, _clippingSize.height);
2013-09-13 22:20:20 +08:00
Color4F green(0, 1, 0, 1);
2013-09-13 22:20:20 +08:00
m_pInnerStencil->drawPolygon(rect, 4, green, 0, green);
if (CCClippingNode::init(m_pInnerStencil))
{
return true;
}
return false;
}
void RectClippingNode::setClippingSize(const Size &size)
{
setContentSize(size);
2013-09-16 20:54:13 +08:00
_clippingSize = size;
2013-09-13 22:20:20 +08:00
rect[0] = Point(0, 0);
2013-09-16 20:54:13 +08:00
rect[1] = Point(_clippingSize.width, 0);
rect[2] = Point(_clippingSize.width, _clippingSize.height);
rect[3] = Point(0, _clippingSize.height);
Color4F green(0, 1, 0, 1);
2013-09-13 22:20:20 +08:00
m_pInnerStencil->clear();
m_pInnerStencil->drawPolygon(rect, 4, green, 0, green);
}
void RectClippingNode::setClippingEnabled(bool enabled)
{
2013-09-16 20:54:13 +08:00
_clippingEnabled = enabled;
2013-09-13 22:20:20 +08:00
}
void RectClippingNode::visit()
{
2013-09-16 20:54:13 +08:00
if (!_enabled)
2013-09-13 22:20:20 +08:00
{
return;
}
2013-09-16 20:54:13 +08:00
if (_clippingEnabled)
2013-09-13 22:20:20 +08:00
{
CCClippingNode::visit();
}
else
{
CCNode::visit();
}
}
void RectClippingNode::setEnabled(bool enabled)
{
2013-09-16 20:54:13 +08:00
_enabled = enabled;
2013-09-13 22:20:20 +08:00
}
bool RectClippingNode::isEnabled() const
{
2013-09-16 20:54:13 +08:00
return _enabled;
2013-09-13 22:20:20 +08:00
}
}