axmol/cocos/gui/UILayout.h

306 lines
8.5 KiB
C
Raw Normal View History

2013-09-13 22:20:20 +08:00
/****************************************************************************
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
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.
****************************************************************************/
2013-09-13 22:20:20 +08:00
#ifndef __LAYOUT_H__
#define __LAYOUT_H__
#include "gui/UIWidget.h"
2013-09-13 22:20:20 +08:00
2013-12-23 15:02:52 +08:00
NS_CC_BEGIN
namespace ui {
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-12-23 15:02:52 +08:00
typedef enum {
LAYOUT_CLIPPING_STENCIL,
LAYOUT_CLIPPING_SCISSOR
}LayoutClippingType;
/**
* @js NA
* @lua NA
*/
class Layout : public Widget
2013-09-13 22:20:20 +08:00
{
public:
/**
* Default constructor
*/
2013-12-23 15:02:52 +08:00
Layout();
2013-09-13 22:20:20 +08:00
/**
* Default destructor
*/
2013-12-23 15:02:52 +08:00
virtual ~Layout();
2013-09-13 22:20:20 +08:00
/**
* Allocates and initializes a layout.
*/
2013-12-23 15:02:52 +08:00
static Layout* create();
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.
*
*/
2013-12-23 15:02:52 +08:00
void setBackGroundImageCapInsets(const 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
*/
2013-12-23 15:02:52 +08:00
void setBackGroundColor(const 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
*/
2013-12-23 15:02:52 +08:00
void setBackGroundColor(const Color3B &startColor, const 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
*/
2013-12-23 15:02:52 +08:00
void setBackGroundColorVector(const Point &vector);
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.
*/
2013-12-23 15:02:52 +08:00
const 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
2013-12-23 15:02:52 +08:00
void setClippingType(LayoutClippingType type);
2013-09-13 22:20:20 +08:00
/**
* Gets if layout is clipping enabled.
*
* @return if layout is clipping enabled.
*/
virtual bool isClippingEnabled();
2013-11-06 16:04:06 +08:00
/**
* Returns the "class name" of widget.
*/
2013-12-23 15:02:52 +08:00
virtual std::string 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-12-23 15:02:52 +08:00
virtual void addChild(Node * child) override;
/**
* Adds a child to the container with a z-order
*
* If the child is added to a 'running' node, then 'onEnter' and 'onEnterTransitionDidFinish' will be called immediately.
*
* @param child A child node
* @param zOrder Z order for drawing priority. Please refer to setLocalZOrder(int)
2013-12-23 15:02:52 +08:00
*/
virtual void addChild(Node * child, int zOrder) override;
2013-09-17 17:59:20 +08:00
/**
2013-12-23 15:02:52 +08:00
* Adds a child to the container with z order and tag
*
* If the child is added to a 'running' node, then 'onEnter' and 'onEnterTransitionDidFinish' will be called immediately.
2013-11-06 16:04:06 +08:00
*
2013-12-23 15:02:52 +08:00
* @param child A child node
* @param zOrder Z order for drawing priority. Please refer to setLocalZOrder(int)
2013-12-23 15:02:52 +08:00
* @param tag A interger to identify the node easily. Please refer to setTag(int)
2013-09-17 17:59:20 +08:00
*/
2013-12-23 15:02:52 +08:00
virtual void addChild(Node* child, int zOrder, int tag) override;
2013-11-06 16:04:06 +08:00
virtual void visit(Renderer *renderer, const kmMat4 &parentTransform, bool parentTransformDirty) override;
2013-12-23 15:02:52 +08:00
virtual void sortAllChildren() override;
2013-12-26 14:57:30 +08:00
void requestDoLayout();
2014-01-06 10:56:33 +08:00
virtual void onEnter() override;
virtual void onExit() override;
virtual bool hitTest(const Point &pt);
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-12-24 20:22:14 +08:00
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
2013-12-23 15:02:52 +08:00
void supplyTheLayoutParameterLackToChild(Widget* child);
virtual Widget* createCloneInstance() override;
virtual void copySpecialProperties(Widget* model) override;
virtual void copyClonedWidgetChildren(Widget* model) override;
void stencilClippingVisit(Renderer *renderer, const kmMat4& parentTransform, bool parentTransformDirty);
void scissorClippingVisit(Renderer *renderer, const kmMat4& parentTransform, bool parentTransformDirty);
2013-12-23 15:02:52 +08:00
void setStencilClippingSize(const Size& size);
const Rect& getClippingRect();
virtual void doLayout();
2014-01-06 10:56:33 +08:00
//clipping
void onBeforeVisitStencil();
void onAfterDrawStencil();
void onAfterVisitStencil();
void onBeforeVisitScissor();
void onAfterVisitScissor();
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;
2013-12-23 15:02:52 +08:00
Node* _backGroundImage;
2013-09-16 20:54:13 +08:00
std::string _backGroundImageFileName;
2013-12-23 15:02:52 +08:00
Rect _backGroundImageCapInsets;
2013-09-16 20:54:13 +08:00
LayoutBackGroundColorType _colorType;
TextureResType _bgImageTexType;
2013-12-23 15:02:52 +08:00
LayerColor* _colorRender;
LayerGradient* _gradientRender;
Color3B _cColor;
Color3B _gStartColor;
Color3B _gEndColor;
Point _alongVector;
2013-09-16 20:54:13 +08:00
int _cOpacity;
2013-12-23 15:02:52 +08:00
Size _backGroundImageTextureSize;
2013-09-23 19:16:20 +08:00
LayoutType _layoutType;
2013-12-23 15:02:52 +08:00
LayoutClippingType _clippingType;
DrawNode* _clippingStencil;
bool _scissorRectDirty;
Rect _clippingRect;
Layout* _clippingParent;
bool _doLayoutDirty;
2014-01-06 10:56:33 +08:00
//clipping
GLboolean _currentStencilEnabled;
GLuint _currentStencilWriteMask;
GLenum _currentStencilFunc;
GLint _currentStencilRef;
GLuint _currentStencilValueMask;
GLenum _currentStencilFail;
GLenum _currentStencilPassDepthFail;
GLenum _currentStencilPassDepthPass;
GLboolean _currentDepthWriteMask;
GLboolean _currentAlphaTestEnabled;
GLenum _currentAlphaTestFunc;
GLclampf _currentAlphaTestRef;
GLint _mask_layer_le;
GroupCommand _groupCommand;
CustomCommand _beforeVisitCmdStencil;
CustomCommand _afterDrawStencilCmd;
CustomCommand _afterVisitCmdStencil;
CustomCommand _beforeVisitCmdScissor;
CustomCommand _afterVisitCmdScissor;
2013-09-13 22:20:20 +08:00
};
2013-12-23 15:02:52 +08:00
}
2013-12-23 15:02:52 +08:00
NS_CC_END
2013-09-13 22:20:20 +08:00
#endif /* defined(__Layout__) */