axmol/cocos/gui/UILayout.h

273 lines
7.2 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.
****************************************************************************/
#ifndef __LAYOUT_H__
#define __LAYOUT_H__
#include "gui/UIWidget.h"
2013-09-13 22:20:20 +08:00
namespace gui {
2013-09-13 22:20:20 +08:00
typedef enum
{
LAYOUT_COLOR_NONE,
LAYOUT_COLOR_SOLID,
LAYOUT_COLOR_GRADIENT
}LayoutBackGroundColorType;
2013-09-23 19:16:20 +08:00
typedef enum
{
2013-09-23 21:26:44 +08:00
LAYOUT_ABSOLUTE,
2013-09-23 19:16:20 +08:00
LAYOUT_LINEAR_VERTICAL,
LAYOUT_LINEAR_HORIZONTAL,
LAYOUT_RELATIVE
}LayoutType;
2013-11-06 16:04:06 +08:00
/**
* @js NA
* @lua NA
*/
class UILayout : public UIWidget
2013-09-13 22:20:20 +08:00
{
public:
/**
* Default constructor
*/
2013-11-06 16:04:06 +08:00
UILayout();
2013-09-13 22:20:20 +08:00
/**
* Default destructor
*/
2013-11-06 16:04:06 +08:00
virtual ~UILayout();
2013-09-13 22:20:20 +08:00
/**
* Allocates and initializes a layout.
*/
2013-11-06 16:04:06 +08:00
static UILayout* create();
2013-09-13 22:20:20 +08:00
//override "hitTest" method of widget.
2013-11-14 13:42:10 +08:00
virtual bool hitTest(const cocos2d::Point &pt) override;
2013-09-13 22:20:20 +08:00
//background
/**
* Sets a background image for layout
*
* @param fileName image file path.
*
* @param texType @see TextureResType. UI_TEX_TYPE_LOCAL means local file, UI_TEX_TYPE_PLIST means sprite frame.
*/
void setBackGroundImage(const char* fileName,TextureResType texType = UI_TEX_TYPE_LOCAL);
/**
* Sets a background image capinsets for layout, if the background image is a scale9 render.
*
* @param capinsets of background image.
*
*/
void setBackGroundImageCapInsets(const cocos2d::Rect& capInsets);
2013-09-13 22:20:20 +08:00
/**
* Sets Color Type for layout.
*
* @param type @see LayoutBackGroundColorType.
*/
void setBackGroundColorType(LayoutBackGroundColorType type);
/**
* Sets background iamge use scale9 renderer.
*
* @param enabled true that use scale9 renderer, false otherwise.
*/
void setBackGroundImageScale9Enabled(bool enabled);
/**
* Sets background color for layout, if color type is LAYOUT_COLOR_SOLID
*
* @param color
*/
void setBackGroundColor(const cocos2d::Color3B &color);
2013-09-13 22:20:20 +08:00
/**
* Sets background color for layout, if color type is LAYOUT_COLOR_GRADIENT
*
* @param start color
*
* @param end color
*/
void setBackGroundColor(const cocos2d::Color3B &startColor, const cocos2d::Color3B &endColor);
2013-09-13 22:20:20 +08:00
/**
* Sets background opacity layout.
*
* @param opacity
*/
void setBackGroundColorOpacity(int opacity);
/**
* Sets background color vector for layout, if color type is LAYOUT_COLOR_GRADIENT
*
* @param vector
*/
void setBackGroundColorVector(const cocos2d::Point &vector);
2013-09-13 22:20:20 +08:00
//override "setColor" method of widget.
2013-11-14 13:42:10 +08:00
virtual void setColor(const cocos2d::Color3B &color) override;
2013-09-13 22:20:20 +08:00
//override "setOpacity" method of widget.
2013-11-14 13:42:10 +08:00
virtual void setOpacity(int opacity) override;
2013-09-13 22:20:20 +08:00
/**
* Remove the background image of layout.
*/
void removeBackGroundImage();
/**
* Gets background image texture size.
*
* @return background image texture size.
*/
const cocos2d::Size& getBackGroundImageTextureSize() const;
2013-09-13 22:20:20 +08:00
/**
* Changes if layout can clip it's content and child.
*
* If you really need this, please enable it. But it would reduce the rendering efficiency.
*
* @param clipping enabled.
*/
2013-11-14 13:42:10 +08:00
virtual void setClippingEnabled(bool enabled);
2013-09-13 22:20:20 +08:00
/**
* Gets if layout is clipping enabled.
*
* @return if layout is clipping enabled.
*/
virtual bool isClippingEnabled();
/**
* Gets the content size of widget.
*
* Content size is widget's texture size.
*/
2013-11-14 13:42:10 +08:00
virtual const cocos2d::Size& getContentSize() const override;
2013-09-23 19:16:20 +08:00
2013-11-06 16:04:06 +08:00
/**
* Returns the "class name" of widget.
*/
2013-11-14 13:42:10 +08:00
virtual const char* getDescription() const override;
2013-11-06 16:04:06 +08:00
2013-09-23 19:16:20 +08:00
/**
* Sets LayoutType.
*
* @see LayoutType
*
* @param LayoutType
*/
2013-09-23 21:26:44 +08:00
virtual void setLayoutType(LayoutType type);
2013-09-23 19:16:20 +08:00
/**
* Gets LayoutType.
*
* @see LayoutType
*
* @return LayoutType
*/
2013-09-23 21:26:44 +08:00
virtual LayoutType getLayoutType() const;
2013-09-23 19:16:20 +08:00
virtual void doLayout();
2013-11-06 16:04:06 +08:00
2013-09-17 17:59:20 +08:00
/**
2013-11-06 16:04:06 +08:00
* Adds a child to the container.
*
* @param child A child widget
2013-09-17 17:59:20 +08:00
*/
2013-11-14 13:42:10 +08:00
virtual bool addChild(UIWidget* child) override;
2013-11-06 16:04:06 +08:00
2013-09-13 22:20:20 +08:00
protected:
//override "init" method of widget.
2013-11-14 13:42:10 +08:00
virtual bool init() override;
2013-09-13 22:20:20 +08:00
//override "initRenderer" method of widget.
2013-11-14 13:42:10 +08:00
virtual void initRenderer() override;
2013-09-13 22:20:20 +08:00
//override "onSizeChanged" method of widget.
2013-11-14 13:42:10 +08:00
virtual void onSizeChanged() override;
2013-09-13 22:20:20 +08:00
//init background image renderer.
void addBackGroundImage();
2013-11-06 16:04:06 +08:00
void supplyTheLayoutParameterLackToChild(UIWidget* child);
2013-11-14 13:42:10 +08:00
virtual UIWidget* createCloneInstance() override;
virtual void copySpecialProperties(UIWidget* model) override;
virtual void copyClonedWidgetChildren(UIWidget* model) override;
2013-09-13 22:20:20 +08:00
protected:
2013-09-16 20:54:13 +08:00
bool _clippingEnabled;
2013-09-13 22:20:20 +08:00
//background
2013-09-16 20:54:13 +08:00
bool _backGroundScale9Enabled;
cocos2d::Node* _backGroundImage;
2013-09-16 20:54:13 +08:00
std::string _backGroundImageFileName;
cocos2d::Rect _backGroundImageCapInsets;
2013-09-16 20:54:13 +08:00
LayoutBackGroundColorType _colorType;
TextureResType _bgImageTexType;
cocos2d::LayerColor* _colorRender;
cocos2d::LayerGradient* _gradientRender;
cocos2d::Color3B _cColor;
cocos2d::Color3B _gStartColor;
cocos2d::Color3B _gEndColor;
cocos2d::Point _alongVector;
2013-09-16 20:54:13 +08:00
int _cOpacity;
cocos2d::Size _backGroundImageTextureSize;
2013-09-23 19:16:20 +08:00
LayoutType _layoutType;
2013-09-13 22:20:20 +08:00
};
2013-11-06 16:04:06 +08:00
/**
* @js NA
* @lua NA
*/
class UIRectClippingNode : public cocos2d::ClippingNode
2013-09-13 22:20:20 +08:00
{
public:
2013-11-06 16:04:06 +08:00
virtual ~UIRectClippingNode();
2013-09-13 22:20:20 +08:00
virtual bool init();
2013-11-06 16:04:06 +08:00
static UIRectClippingNode* create();
void setClippingSize(const cocos2d::Size& size);
2013-09-13 22:20:20 +08:00
void setClippingEnabled(bool enabled);
virtual void visit();
void setEnabled(bool enabled);
bool isEnabled() const;
protected:
2013-11-06 16:04:06 +08:00
cocos2d::DrawNode* _innerStencil;
2013-09-16 20:54:13 +08:00
bool _enabled;
2013-09-13 22:20:20 +08:00
private:
2013-11-06 16:04:06 +08:00
UIRectClippingNode();
cocos2d::Point rect[4];
cocos2d::Size _clippingSize;
2013-09-16 20:54:13 +08:00
bool _clippingEnabled;
2013-09-13 22:20:20 +08:00
};
}
2013-09-13 22:20:20 +08:00
#endif /* defined(__Layout__) */