axmol/extensions/CocoStudio/GUI/Layouts/Layout.cpp

524 lines
13 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 "../../../GUI/CCControlExtension/CCScale9Sprite.h"
NS_CC_EXT_BEGIN
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),
_layoutExecutant(NULL),
_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),
_backGroundImageTextureSize(Size::ZERO)
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()
{
2013-09-16 20:54:13 +08:00
CC_SAFE_RELEASE_NULL(_layoutExecutant);
2013-09-13 22:20:20 +08:00
}
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::setLayoutExecutant(LayoutExecutant *exe)
{
2013-09-16 20:54:13 +08:00
if (_layoutExecutant)
2013-09-13 22:20:20 +08:00
{
2013-09-16 20:54:13 +08:00
CC_SAFE_RELEASE_NULL(_layoutExecutant);
2013-09-13 22:20:20 +08:00
}
2013-09-16 20:54:13 +08:00
_layoutExecutant = exe;
_layoutExecutant->setLayout(this);
CC_SAFE_RETAIN(_layoutExecutant);
2013-09-13 22:20:20 +08:00
}
LayoutExecutant* Layout::getLayoutExecutant() const
{
2013-09-16 20:54:13 +08:00
return _layoutExecutant;
2013-09-13 22:20:20 +08:00
}
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-16 20:54:13 +08:00
if (_layoutExecutant)
2013-09-13 22:20:20 +08:00
{
2013-09-16 20:54:13 +08:00
_layoutExecutant->doLayout();
2013-09-13 22:20:20 +08:00
}
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-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
}
NS_CC_EXT_END