2013-11-06 16:04:06 +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 "gui/UILayout.h"
# include "gui/UIHelper.h"
# include "extensions/GUI/CCControlExtension/CCScale9Sprite.h"
2013-12-23 15:02:52 +08:00
NS_CC_BEGIN
2013-11-06 16:04:06 +08:00
2013-12-23 15:02:52 +08:00
namespace gui {
2013-12-24 20:22:14 +08:00
# define BACKGROUNDIMAGEZ (-1)
# define BCAKGROUNDCOLORRENDERERZ (-2)
2013-11-06 16:04:06 +08:00
2013-12-23 15:02:52 +08:00
static GLint g_sStencilBits = - 1 ;
2013-11-06 16:04:06 +08:00
2013-12-23 15:02:52 +08:00
Layout : : Layout ( ) :
2013-11-06 16:04:06 +08:00
_clippingEnabled ( false ) ,
_backGroundScale9Enabled ( false ) ,
2013-11-14 11:37:46 +08:00
_backGroundImage ( nullptr ) ,
2013-11-06 16:04:06 +08:00
_backGroundImageFileName ( " " ) ,
2013-12-23 15:02:52 +08:00
_backGroundImageCapInsets ( Rect : : ZERO ) ,
2013-11-06 16:04:06 +08:00
_colorType ( LAYOUT_COLOR_NONE ) ,
_bgImageTexType ( UI_TEX_TYPE_LOCAL ) ,
2013-11-14 11:37:46 +08:00
_colorRender ( nullptr ) ,
_gradientRender ( nullptr ) ,
2013-12-23 15:02:52 +08:00
_cColor ( Color3B : : WHITE ) ,
_gStartColor ( Color3B : : WHITE ) ,
_gEndColor ( Color3B : : WHITE ) ,
_alongVector ( Point ( 0.0f , - 1.0f ) ) ,
2013-11-06 16:04:06 +08:00
_cOpacity ( 255 ) ,
2013-12-23 15:02:52 +08:00
_backGroundImageTextureSize ( Size : : ZERO ) ,
_layoutType ( LAYOUT_ABSOLUTE ) ,
_clippingType ( LAYOUT_CLIPPING_STENCIL ) ,
_clippingStencil ( nullptr ) ,
_handleScissor ( false ) ,
_scissorRectDirty ( false ) ,
_clippingRect ( Rect : : ZERO ) ,
_clippingParent ( nullptr ) ,
_doLayoutDirty ( true )
2013-11-06 16:04:06 +08:00
{
_widgetType = WidgetTypeContainer ;
}
2013-12-23 15:02:52 +08:00
Layout : : ~ Layout ( )
2013-11-06 16:04:06 +08:00
{
}
2013-12-23 15:02:52 +08:00
Layout * Layout : : create ( )
2013-11-06 16:04:06 +08:00
{
2013-12-23 15:02:52 +08:00
Layout * layout = new Layout ( ) ;
2013-11-06 16:04:06 +08:00
if ( layout & & layout - > init ( ) )
{
layout - > autorelease ( ) ;
return layout ;
}
CC_SAFE_DELETE ( layout ) ;
2013-11-14 11:37:46 +08:00
return nullptr ;
2013-11-06 16:04:06 +08:00
}
2013-12-23 15:02:52 +08:00
bool Layout : : init ( )
{
if ( Node : : init ( ) )
{
initRenderer ( ) ;
setCascadeColorEnabled ( true ) ;
setCascadeOpacityEnabled ( true ) ;
setBright ( true ) ;
ignoreContentAdaptWithSize ( false ) ;
setSize ( Size : : ZERO ) ;
setAnchorPoint ( Point : : ZERO ) ;
return true ;
}
return false ;
}
void Layout : : addChild ( Node * child )
2013-11-06 16:04:06 +08:00
{
2013-12-23 15:02:52 +08:00
Widget : : addChild ( child ) ;
2013-11-06 16:04:06 +08:00
}
2013-12-23 15:02:52 +08:00
void Layout : : addChild ( Node * child , int zOrder )
2013-11-06 16:04:06 +08:00
{
2013-12-23 15:02:52 +08:00
Widget : : addChild ( child , zOrder ) ;
2013-11-06 16:04:06 +08:00
}
2013-12-23 15:02:52 +08:00
void Layout : : addChild ( Node * child , int zOrder , int tag )
{
supplyTheLayoutParameterLackToChild ( static_cast < Widget * > ( child ) ) ;
Widget : : addChild ( child , zOrder , tag ) ;
_doLayoutDirty = true ;
}
2013-11-06 16:04:06 +08:00
2013-12-23 15:02:52 +08:00
bool Layout : : isClippingEnabled ( )
2013-11-06 16:04:06 +08:00
{
return _clippingEnabled ;
}
2013-12-23 15:02:52 +08:00
void Layout : : visit ( )
2013-11-06 16:04:06 +08:00
{
2013-12-23 15:02:52 +08:00
if ( ! _enabled )
2013-11-06 16:04:06 +08:00
{
2013-12-23 15:02:52 +08:00
return ;
}
if ( _clippingEnabled )
{
switch ( _clippingType )
{
case LAYOUT_CLIPPING_STENCIL :
stencilClippingVisit ( ) ;
break ;
case LAYOUT_CLIPPING_SCISSOR :
scissorClippingVisit ( ) ;
break ;
default :
break ;
}
}
else
{
Node : : visit ( ) ;
}
}
void Layout : : sortAllChildren ( )
{
Widget : : sortAllChildren ( ) ;
doLayout ( ) ;
}
void Layout : : stencilClippingVisit ( )
{
if ( ! _clippingStencil | | ! _clippingStencil - > isVisible ( ) )
{
Node : : visit ( ) ;
return ;
}
if ( g_sStencilBits < 1 )
{
Node : : visit ( ) ;
return ;
}
static GLint layer = - 1 ;
if ( layer + 1 = = g_sStencilBits )
{
static bool once = true ;
if ( once )
{
char warning [ 200 ] = { 0 } ;
snprintf ( warning , sizeof ( warning ) , " Nesting more than %d stencils is not supported. Everything will be drawn without stencil for this node and its childs. " , g_sStencilBits ) ;
CCLOG ( " %s " , warning ) ;
once = false ;
}
Node : : visit ( ) ;
return ;
}
layer + + ;
GLint mask_layer = 0x1 < < layer ;
GLint mask_layer_l = mask_layer - 1 ;
GLint mask_layer_le = mask_layer | mask_layer_l ;
GLboolean currentStencilEnabled = GL_FALSE ;
GLuint currentStencilWriteMask = ~ 0 ;
GLenum currentStencilFunc = GL_ALWAYS ;
GLint currentStencilRef = 0 ;
GLuint currentStencilValueMask = ~ 0 ;
GLenum currentStencilFail = GL_KEEP ;
GLenum currentStencilPassDepthFail = GL_KEEP ;
GLenum currentStencilPassDepthPass = GL_KEEP ;
currentStencilEnabled = glIsEnabled ( GL_STENCIL_TEST ) ;
glGetIntegerv ( GL_STENCIL_WRITEMASK , ( GLint * ) & currentStencilWriteMask ) ;
glGetIntegerv ( GL_STENCIL_FUNC , ( GLint * ) & currentStencilFunc ) ;
glGetIntegerv ( GL_STENCIL_REF , & currentStencilRef ) ;
glGetIntegerv ( GL_STENCIL_VALUE_MASK , ( GLint * ) & currentStencilValueMask ) ;
glGetIntegerv ( GL_STENCIL_FAIL , ( GLint * ) & currentStencilFail ) ;
glGetIntegerv ( GL_STENCIL_PASS_DEPTH_FAIL , ( GLint * ) & currentStencilPassDepthFail ) ;
glGetIntegerv ( GL_STENCIL_PASS_DEPTH_PASS , ( GLint * ) & currentStencilPassDepthPass ) ;
glEnable ( GL_STENCIL_TEST ) ;
CHECK_GL_ERROR_DEBUG ( ) ;
glStencilMask ( mask_layer ) ;
GLboolean currentDepthWriteMask = GL_TRUE ;
glGetBooleanv ( GL_DEPTH_WRITEMASK , & currentDepthWriteMask ) ;
glDepthMask ( GL_FALSE ) ;
glStencilFunc ( GL_NEVER , mask_layer , mask_layer ) ;
glStencilOp ( GL_ZERO , GL_KEEP , GL_KEEP ) ;
kmGLMatrixMode ( KM_GL_MODELVIEW ) ;
kmGLPushMatrix ( ) ;
kmGLLoadIdentity ( ) ;
kmGLMatrixMode ( KM_GL_PROJECTION ) ;
kmGLPushMatrix ( ) ;
kmGLLoadIdentity ( ) ;
DrawPrimitives : : drawSolidRect ( Point ( - 1 , - 1 ) , Point ( 1 , 1 ) , Color4F ( 1 , 1 , 1 , 1 ) ) ;
kmGLMatrixMode ( KM_GL_PROJECTION ) ;
kmGLPopMatrix ( ) ;
kmGLMatrixMode ( KM_GL_MODELVIEW ) ;
kmGLPopMatrix ( ) ;
glStencilFunc ( GL_NEVER , mask_layer , mask_layer ) ;
glStencilOp ( GL_REPLACE , GL_KEEP , GL_KEEP ) ;
# if (CC_TARGET_PLATFORM == CC_PLATFORM_MAC || CC_TARGET_PLATFORM == CC_PLATFORM_WINDOWS || CC_TARGET_PLATFORM == CC_PLATFORM_LINUX)
GLboolean currentAlphaTestEnabled = GL_FALSE ;
GLenum currentAlphaTestFunc = GL_ALWAYS ;
GLclampf currentAlphaTestRef = 1 ;
# endif
kmGLPushMatrix ( ) ;
transform ( ) ;
_clippingStencil - > visit ( ) ;
kmGLPopMatrix ( ) ;
glDepthMask ( currentDepthWriteMask ) ;
glStencilFunc ( GL_EQUAL , mask_layer_le , mask_layer_le ) ;
glStencilOp ( GL_KEEP , GL_KEEP , GL_KEEP ) ;
Node : : visit ( ) ;
glStencilFunc ( currentStencilFunc , currentStencilRef , currentStencilValueMask ) ;
glStencilOp ( currentStencilFail , currentStencilPassDepthFail , currentStencilPassDepthPass ) ;
glStencilMask ( currentStencilWriteMask ) ;
if ( ! currentStencilEnabled )
{
glDisable ( GL_STENCIL_TEST ) ;
}
layer - - ;
}
void Layout : : scissorClippingVisit ( )
{
Rect clippingRect = getClippingRect ( ) ;
if ( _handleScissor )
{
glEnable ( GL_SCISSOR_TEST ) ;
}
EGLView : : getInstance ( ) - > setScissorInPoints ( clippingRect . origin . x , clippingRect . origin . y , clippingRect . size . width , clippingRect . size . height ) ;
Node : : visit ( ) ;
if ( _handleScissor )
{
glDisable ( GL_SCISSOR_TEST ) ;
2013-11-06 16:04:06 +08:00
}
}
2013-12-23 15:02:52 +08:00
void Layout : : setClippingEnabled ( bool able )
2013-11-06 16:04:06 +08:00
{
2013-12-23 15:02:52 +08:00
if ( able = = _clippingEnabled )
{
return ;
}
2013-11-06 16:04:06 +08:00
_clippingEnabled = able ;
2013-12-23 15:02:52 +08:00
switch ( _clippingType )
{
case LAYOUT_CLIPPING_STENCIL :
if ( able )
{
glGetIntegerv ( GL_STENCIL_BITS , & g_sStencilBits ) ;
_clippingStencil = DrawNode : : create ( ) ;
_clippingStencil - > onEnter ( ) ;
_clippingStencil - > retain ( ) ;
setStencilClippingSize ( _size ) ;
}
else
{
_clippingStencil - > onExit ( ) ;
_clippingStencil - > release ( ) ;
_clippingStencil = nullptr ;
}
break ;
default :
break ;
}
2013-11-06 16:04:06 +08:00
}
2013-12-23 15:02:52 +08:00
void Layout : : setClippingType ( LayoutClippingType type )
{
if ( type = = _clippingType )
{
return ;
}
bool clippingEnabled = isClippingEnabled ( ) ;
setClippingEnabled ( false ) ;
_clippingType = type ;
setClippingEnabled ( clippingEnabled ) ;
}
void Layout : : setStencilClippingSize ( const Size & size )
2013-11-06 16:04:06 +08:00
{
2013-12-23 15:02:52 +08:00
if ( _clippingEnabled & & _clippingType = = LAYOUT_CLIPPING_STENCIL )
{
Point rect [ 4 ] ;
rect [ 0 ] = Point : : ZERO ;
rect [ 1 ] = Point ( _size . width , 0 ) ;
rect [ 2 ] = Point ( _size . width , _size . height ) ;
rect [ 3 ] = Point ( 0 , _size . height ) ;
Color4F green ( 0 , 1 , 0 , 1 ) ;
_clippingStencil - > clear ( ) ;
_clippingStencil - > drawPolygon ( rect , 4 , green , 0 , green ) ;
}
}
const Rect & Layout : : getClippingRect ( )
{
_handleScissor = true ;
Point worldPos = convertToWorldSpace ( Point : : ZERO ) ;
AffineTransform t = nodeToWorldTransform ( ) ;
float scissorWidth = _size . width * t . a ;
float scissorHeight = _size . height * t . d ;
Rect parentClippingRect ;
Layout * parent = this ;
bool firstClippingParentFounded = false ;
while ( parent )
{
parent = dynamic_cast < Layout * > ( parent - > getParent ( ) ) ;
if ( parent )
{
if ( parent - > isClippingEnabled ( ) )
{
if ( ! firstClippingParentFounded )
{
_clippingParent = parent ;
firstClippingParentFounded = true ;
}
if ( parent - > _clippingType = = LAYOUT_CLIPPING_SCISSOR )
{
_handleScissor = false ;
break ;
}
}
}
}
if ( _clippingParent )
2013-11-08 23:26:44 +08:00
{
2013-12-23 15:02:52 +08:00
parentClippingRect = _clippingParent - > getClippingRect ( ) ;
float finalX = worldPos . x - ( scissorWidth * _anchorPoint . x ) ;
float finalY = worldPos . y - ( scissorHeight * _anchorPoint . y ) ;
float finalWidth = scissorWidth ;
float finalHeight = scissorHeight ;
float leftOffset = worldPos . x - parentClippingRect . origin . x ;
if ( leftOffset < 0.0f )
{
finalX = parentClippingRect . origin . x ;
finalWidth + = leftOffset ;
}
float rightOffset = ( worldPos . x + scissorWidth ) - ( parentClippingRect . origin . x + parentClippingRect . size . width ) ;
if ( rightOffset > 0.0f )
{
finalWidth - = rightOffset ;
}
float topOffset = ( worldPos . y + scissorHeight ) - ( parentClippingRect . origin . y + parentClippingRect . size . height ) ;
if ( topOffset > 0.0f )
{
finalHeight - = topOffset ;
}
float bottomOffset = worldPos . y - parentClippingRect . origin . y ;
if ( bottomOffset < 0.0f )
2013-11-08 23:26:44 +08:00
{
2013-12-23 15:02:52 +08:00
finalY = parentClippingRect . origin . x ;
finalHeight + = bottomOffset ;
2013-11-08 23:26:44 +08:00
}
2013-12-23 15:02:52 +08:00
if ( finalWidth < 0.0f )
{
finalWidth = 0.0f ;
}
if ( finalHeight < 0.0f )
{
finalHeight = 0.0f ;
}
_clippingRect . origin . x = finalX ;
_clippingRect . origin . y = finalY ;
_clippingRect . size . width = finalWidth ;
_clippingRect . size . height = finalHeight ;
}
else
{
_clippingRect . origin . x = worldPos . x - ( scissorWidth * _anchorPoint . x ) ;
_clippingRect . origin . y = worldPos . y - ( scissorHeight * _anchorPoint . y ) ;
_clippingRect . size . width = scissorWidth ;
_clippingRect . size . height = scissorHeight ;
2013-11-08 23:26:44 +08:00
}
2013-12-23 15:02:52 +08:00
return _clippingRect ;
}
void Layout : : onSizeChanged ( )
{
Widget : : onSizeChanged ( ) ;
setStencilClippingSize ( _size ) ;
_doLayoutDirty = true ;
2013-11-06 16:04:06 +08:00
if ( _backGroundImage )
{
2013-12-23 15:02:52 +08:00
_backGroundImage - > setPosition ( Point ( _size . width / 2.0f , _size . height / 2.0f ) ) ;
2013-11-15 22:25:29 +08:00
if ( _backGroundScale9Enabled & & _backGroundImage )
2013-11-06 16:04:06 +08:00
{
2013-12-23 15:02:52 +08:00
static_cast < extension : : Scale9Sprite * > ( _backGroundImage ) - > setPreferredSize ( _size ) ;
2013-11-06 16:04:06 +08:00
}
}
if ( _colorRender )
{
_colorRender - > setContentSize ( _size ) ;
}
if ( _gradientRender )
{
_gradientRender - > setContentSize ( _size ) ;
}
}
2013-12-23 15:02:52 +08:00
void Layout : : setBackGroundImageScale9Enabled ( bool able )
2013-11-06 16:04:06 +08:00
{
if ( _backGroundScale9Enabled = = able )
{
return ;
}
2013-12-24 20:22:14 +08:00
Node : : removeChild ( _backGroundImage ) ;
2013-11-14 11:37:46 +08:00
_backGroundImage = nullptr ;
2013-11-06 16:04:06 +08:00
_backGroundScale9Enabled = able ;
if ( _backGroundScale9Enabled )
{
2013-12-23 15:02:52 +08:00
_backGroundImage = extension : : Scale9Sprite : : create ( ) ;
2013-12-24 20:22:14 +08:00
Node : : addChild ( _backGroundImage , BACKGROUNDIMAGEZ , - 1 ) ;
2013-11-06 16:04:06 +08:00
}
else
{
2013-12-23 15:02:52 +08:00
_backGroundImage = Sprite : : create ( ) ;
2013-12-24 20:22:14 +08:00
Node : : addChild ( _backGroundImage , BACKGROUNDIMAGEZ , - 1 ) ;
2013-11-06 16:04:06 +08:00
}
2013-12-24 20:22:14 +08:00
setBackGroundImage ( _backGroundImageFileName . c_str ( ) , _bgImageTexType ) ;
2013-11-06 16:04:06 +08:00
setBackGroundImageCapInsets ( _backGroundImageCapInsets ) ;
}
2013-12-23 15:02:52 +08:00
void Layout : : setBackGroundImage ( const char * fileName , TextureResType texType )
2013-11-06 16:04:06 +08:00
{
if ( ! fileName | | strcmp ( fileName , " " ) = = 0 )
{
return ;
}
2013-11-14 11:37:46 +08:00
if ( _backGroundImage = = nullptr )
2013-11-06 16:04:06 +08:00
{
addBackGroundImage ( ) ;
}
_backGroundImageFileName = fileName ;
_bgImageTexType = texType ;
if ( _backGroundScale9Enabled )
{
2013-12-23 15:02:52 +08:00
extension : : Scale9Sprite * bgiScale9 = static_cast < extension : : Scale9Sprite * > ( _backGroundImage ) ;
2013-11-06 16:04:06 +08:00
switch ( _bgImageTexType )
{
case UI_TEX_TYPE_LOCAL :
2013-12-23 15:02:52 +08:00
bgiScale9 - > initWithFile ( fileName ) ;
2013-11-06 16:04:06 +08:00
break ;
case UI_TEX_TYPE_PLIST :
2013-12-23 15:02:52 +08:00
bgiScale9 - > initWithSpriteFrameName ( fileName ) ;
2013-11-06 16:04:06 +08:00
break ;
default :
break ;
}
2013-12-23 15:02:52 +08:00
bgiScale9 - > setPreferredSize ( _size ) ;
2013-11-06 16:04:06 +08:00
}
else
{
switch ( _bgImageTexType )
{
case UI_TEX_TYPE_LOCAL :
2013-12-23 15:02:52 +08:00
static_cast < Sprite * > ( _backGroundImage ) - > setTexture ( fileName ) ;
2013-11-06 16:04:06 +08:00
break ;
case UI_TEX_TYPE_PLIST :
2013-12-23 15:02:52 +08:00
static_cast < Sprite * > ( _backGroundImage ) - > setSpriteFrame ( fileName ) ;
2013-11-06 16:04:06 +08:00
break ;
default :
break ;
}
}
if ( _backGroundScale9Enabled )
{
2013-12-23 15:02:52 +08:00
extension : : Scale9Sprite * bgiScale9 = static_cast < extension : : Scale9Sprite * > ( _backGroundImage ) ;
bgiScale9 - > setColor ( getColor ( ) ) ;
bgiScale9 - > setOpacity ( getOpacity ( ) ) ;
2013-11-06 16:04:06 +08:00
}
else
{
2013-12-23 15:02:52 +08:00
Sprite * bgiScale9 = static_cast < Sprite * > ( _backGroundImage ) ;
bgiScale9 - > setColor ( getColor ( ) ) ;
bgiScale9 - > setOpacity ( getOpacity ( ) ) ;
2013-11-06 16:04:06 +08:00
}
_backGroundImageTextureSize = _backGroundImage - > getContentSize ( ) ;
2013-12-23 15:02:52 +08:00
_backGroundImage - > setPosition ( Point ( _size . width / 2.0f , _size . height / 2.0f ) ) ;
2013-11-06 16:04:06 +08:00
}
2013-12-23 15:02:52 +08:00
void Layout : : setBackGroundImageCapInsets ( const Rect & capInsets )
2013-11-06 16:04:06 +08:00
{
_backGroundImageCapInsets = capInsets ;
2013-11-15 22:25:29 +08:00
if ( _backGroundScale9Enabled & & _backGroundImage )
2013-11-06 16:04:06 +08:00
{
2013-12-23 15:02:52 +08:00
static_cast < extension : : Scale9Sprite * > ( _backGroundImage ) - > setCapInsets ( capInsets ) ;
2013-11-06 16:04:06 +08:00
}
}
2013-12-23 15:02:52 +08:00
void Layout : : supplyTheLayoutParameterLackToChild ( Widget * child )
2013-11-06 16:04:06 +08:00
{
if ( ! child )
{
return ;
}
switch ( _layoutType )
{
case LAYOUT_ABSOLUTE :
break ;
case LAYOUT_LINEAR_HORIZONTAL :
case LAYOUT_LINEAR_VERTICAL :
{
2013-12-23 15:02:52 +08:00
LinearLayoutParameter * layoutParameter = dynamic_cast < LinearLayoutParameter * > ( child - > getLayoutParameter ( LAYOUT_PARAMETER_LINEAR ) ) ;
2013-11-06 16:04:06 +08:00
if ( ! layoutParameter )
{
2013-12-23 15:02:52 +08:00
child - > setLayoutParameter ( LinearLayoutParameter : : create ( ) ) ;
2013-11-06 16:04:06 +08:00
}
break ;
}
case LAYOUT_RELATIVE :
{
2013-12-23 15:02:52 +08:00
RelativeLayoutParameter * layoutParameter = dynamic_cast < RelativeLayoutParameter * > ( child - > getLayoutParameter ( LAYOUT_PARAMETER_RELATIVE ) ) ;
2013-11-06 16:04:06 +08:00
if ( ! layoutParameter )
{
2013-12-23 15:02:52 +08:00
child - > setLayoutParameter ( RelativeLayoutParameter : : create ( ) ) ;
2013-11-06 16:04:06 +08:00
}
break ;
}
default :
break ;
}
}
2013-12-23 15:02:52 +08:00
void Layout : : addBackGroundImage ( )
2013-11-06 16:04:06 +08:00
{
if ( _backGroundScale9Enabled )
{
2013-12-23 15:02:52 +08:00
_backGroundImage = extension : : Scale9Sprite : : create ( ) ;
2013-11-06 16:04:06 +08:00
_backGroundImage - > setZOrder ( - 1 ) ;
2013-12-24 20:22:14 +08:00
Node : : addChild ( _backGroundImage , BACKGROUNDIMAGEZ , - 1 ) ;
2013-12-23 15:02:52 +08:00
static_cast < extension : : Scale9Sprite * > ( _backGroundImage ) - > setPreferredSize ( _size ) ;
2013-11-06 16:04:06 +08:00
}
else
{
2013-12-23 15:02:52 +08:00
_backGroundImage = Sprite : : create ( ) ;
2013-11-06 16:04:06 +08:00
_backGroundImage - > setZOrder ( - 1 ) ;
2013-12-24 20:22:14 +08:00
Node : : addChild ( _backGroundImage , BACKGROUNDIMAGEZ , - 1 ) ;
2013-11-06 16:04:06 +08:00
}
2013-12-23 15:02:52 +08:00
_backGroundImage - > setPosition ( Point ( _size . width / 2.0f , _size . height / 2.0f ) ) ;
2013-11-06 16:04:06 +08:00
}
2013-12-23 15:02:52 +08:00
void Layout : : removeBackGroundImage ( )
2013-11-06 16:04:06 +08:00
{
if ( ! _backGroundImage )
{
return ;
}
2013-12-24 20:22:14 +08:00
Node : : removeChild ( _backGroundImage ) ;
2013-11-14 11:37:46 +08:00
_backGroundImage = nullptr ;
2013-11-06 16:04:06 +08:00
_backGroundImageFileName = " " ;
2013-12-23 15:02:52 +08:00
_backGroundImageTextureSize = Size : : ZERO ;
2013-11-06 16:04:06 +08:00
}
2013-12-23 15:02:52 +08:00
void Layout : : setBackGroundColorType ( LayoutBackGroundColorType type )
2013-11-06 16:04:06 +08:00
{
if ( _colorType = = type )
{
return ;
}
switch ( _colorType )
{
case LAYOUT_COLOR_NONE :
if ( _colorRender )
{
2013-12-24 20:22:14 +08:00
Node : : removeChild ( _colorRender ) ;
2013-11-14 11:37:46 +08:00
_colorRender = nullptr ;
2013-11-06 16:04:06 +08:00
}
if ( _gradientRender )
{
2013-12-24 20:22:14 +08:00
Node : : removeChild ( _gradientRender ) ;
2013-11-14 11:37:46 +08:00
_gradientRender = nullptr ;
2013-11-06 16:04:06 +08:00
}
break ;
case LAYOUT_COLOR_SOLID :
if ( _colorRender )
{
2013-12-24 20:22:14 +08:00
Node : : removeChild ( _colorRender ) ;
2013-11-14 11:37:46 +08:00
_colorRender = nullptr ;
2013-11-06 16:04:06 +08:00
}
break ;
case LAYOUT_COLOR_GRADIENT :
if ( _gradientRender )
{
2013-12-24 20:22:14 +08:00
Node : : removeChild ( _gradientRender ) ;
2013-11-14 11:37:46 +08:00
_gradientRender = nullptr ;
2013-11-06 16:04:06 +08:00
}
break ;
default :
break ;
}
_colorType = type ;
switch ( _colorType )
{
case LAYOUT_COLOR_NONE :
break ;
case LAYOUT_COLOR_SOLID :
2013-12-23 15:02:52 +08:00
_colorRender = LayerColor : : create ( ) ;
2013-11-06 16:04:06 +08:00
_colorRender - > setContentSize ( _size ) ;
_colorRender - > setOpacity ( _cOpacity ) ;
_colorRender - > setColor ( _cColor ) ;
2013-12-24 20:22:14 +08:00
Node : : addChild ( _colorRender , BACKGROUNDIMAGEZ , - 1 ) ;
2013-11-06 16:04:06 +08:00
break ;
case LAYOUT_COLOR_GRADIENT :
2013-12-23 15:02:52 +08:00
_gradientRender = LayerGradient : : create ( ) ;
2013-11-06 16:04:06 +08:00
_gradientRender - > setContentSize ( _size ) ;
_gradientRender - > setOpacity ( _cOpacity ) ;
_gradientRender - > setStartColor ( _gStartColor ) ;
_gradientRender - > setEndColor ( _gEndColor ) ;
_gradientRender - > setVector ( _alongVector ) ;
2013-12-24 20:22:14 +08:00
Node : : addChild ( _gradientRender , BACKGROUNDIMAGEZ , - 1 ) ;
2013-11-06 16:04:06 +08:00
break ;
default :
break ;
}
}
2013-12-23 15:02:52 +08:00
void Layout : : setBackGroundColor ( const Color3B & color )
2013-11-06 16:04:06 +08:00
{
_cColor = color ;
if ( _colorRender )
{
_colorRender - > setColor ( color ) ;
}
}
2013-12-23 15:02:52 +08:00
void Layout : : setBackGroundColor ( const Color3B & startColor , const Color3B & endColor )
2013-11-06 16:04:06 +08:00
{
_gStartColor = startColor ;
if ( _gradientRender )
{
_gradientRender - > setStartColor ( startColor ) ;
}
_gEndColor = endColor ;
if ( _gradientRender )
{
_gradientRender - > setEndColor ( endColor ) ;
}
}
2013-12-23 15:02:52 +08:00
void Layout : : setBackGroundColorOpacity ( int opacity )
2013-11-06 16:04:06 +08:00
{
_cOpacity = opacity ;
switch ( _colorType )
{
case LAYOUT_COLOR_NONE :
break ;
case LAYOUT_COLOR_SOLID :
_colorRender - > setOpacity ( opacity ) ;
break ;
case LAYOUT_COLOR_GRADIENT :
_gradientRender - > setOpacity ( opacity ) ;
break ;
default :
break ;
}
}
2013-12-23 15:02:52 +08:00
void Layout : : setBackGroundColorVector ( const Point & vector )
2013-11-06 16:04:06 +08:00
{
_alongVector = vector ;
if ( _gradientRender )
{
_gradientRender - > setVector ( vector ) ;
}
}
2013-12-23 15:02:52 +08:00
const Size & Layout : : getBackGroundImageTextureSize ( ) const
2013-11-06 16:04:06 +08:00
{
return _backGroundImageTextureSize ;
}
2013-12-23 15:02:52 +08:00
void Layout : : setLayoutType ( LayoutType type )
2013-11-06 16:04:06 +08:00
{
_layoutType = type ;
2013-12-23 15:02:52 +08:00
for ( auto & child : _widgetChildren )
2013-11-06 16:04:06 +08:00
{
2013-12-23 15:02:52 +08:00
if ( child )
{
supplyTheLayoutParameterLackToChild ( static_cast < Widget * > ( child ) ) ;
}
2013-11-06 16:04:06 +08:00
}
2013-12-23 15:02:52 +08:00
_doLayoutDirty = true ;
2013-11-06 16:04:06 +08:00
}
2013-12-23 15:02:52 +08:00
LayoutType Layout : : getLayoutType ( ) const
2013-11-06 16:04:06 +08:00
{
return _layoutType ;
}
2013-12-23 15:02:52 +08:00
void Layout : : doLayout ( )
2013-11-06 16:04:06 +08:00
{
2013-12-23 15:02:52 +08:00
if ( ! _doLayoutDirty )
{
return ;
}
2013-11-06 16:04:06 +08:00
switch ( _layoutType )
{
case LAYOUT_ABSOLUTE :
break ;
case LAYOUT_LINEAR_VERTICAL :
{
2013-12-23 15:02:52 +08:00
int length = _widgetChildren . size ( ) ;
Size layoutSize = getSize ( ) ;
2013-11-06 16:04:06 +08:00
float topBoundary = layoutSize . height ;
for ( int i = 0 ; i < length ; + + i )
{
2013-12-23 15:02:52 +08:00
Widget * child = static_cast < Widget * > ( _widgetChildren . at ( i ) ) ;
LinearLayoutParameter * layoutParameter = dynamic_cast < LinearLayoutParameter * > ( child - > getLayoutParameter ( LAYOUT_PARAMETER_LINEAR ) ) ;
2013-11-06 16:04:06 +08:00
if ( layoutParameter )
{
2013-12-23 15:02:52 +08:00
LinearGravity childGravity = layoutParameter - > getGravity ( ) ;
Point ap = child - > getAnchorPoint ( ) ;
Size cs = child - > getSize ( ) ;
2013-11-06 16:04:06 +08:00
float finalPosX = ap . x * cs . width ;
float finalPosY = topBoundary - ( ( 1.0f - ap . y ) * cs . height ) ;
switch ( childGravity )
{
case LINEAR_GRAVITY_NONE :
case LINEAR_GRAVITY_LEFT :
break ;
case LINEAR_GRAVITY_RIGHT :
finalPosX = layoutSize . width - ( ( 1.0f - ap . x ) * cs . width ) ;
break ;
case LINEAR_GRAVITY_CENTER_HORIZONTAL :
finalPosX = layoutSize . width / 2.0f - cs . width * ( 0.5f - ap . x ) ;
break ;
default :
break ;
}
2013-12-23 15:02:52 +08:00
Margin mg = layoutParameter - > getMargin ( ) ;
2013-11-06 16:04:06 +08:00
finalPosX + = mg . left ;
finalPosY - = mg . top ;
2013-12-23 15:02:52 +08:00
child - > setPosition ( Point ( finalPosX , finalPosY ) ) ;
2013-11-06 16:04:06 +08:00
topBoundary = child - > getBottomInParent ( ) - mg . bottom ;
}
}
break ;
}
case LAYOUT_LINEAR_HORIZONTAL :
{
2013-12-23 15:02:52 +08:00
int length = _widgetChildren . size ( ) ;
Size layoutSize = getSize ( ) ;
2013-11-06 16:04:06 +08:00
float leftBoundary = 0.0f ;
for ( int i = 0 ; i < length ; + + i )
{
2013-12-23 15:02:52 +08:00
Widget * child = static_cast < Widget * > ( _widgetChildren . at ( i ) ) ;
LinearLayoutParameter * layoutParameter = dynamic_cast < LinearLayoutParameter * > ( child - > getLayoutParameter ( LAYOUT_PARAMETER_LINEAR ) ) ;
2013-11-06 16:04:06 +08:00
if ( layoutParameter )
{
2013-12-23 15:02:52 +08:00
LinearGravity childGravity = layoutParameter - > getGravity ( ) ;
Point ap = child - > getAnchorPoint ( ) ;
Size cs = child - > getSize ( ) ;
2013-11-06 16:04:06 +08:00
float finalPosX = leftBoundary + ( ap . x * cs . width ) ;
float finalPosY = layoutSize . height - ( 1.0f - ap . y ) * cs . height ;
switch ( childGravity )
{
case LINEAR_GRAVITY_NONE :
case LINEAR_GRAVITY_TOP :
break ;
case LINEAR_GRAVITY_BOTTOM :
finalPosY = ap . y * cs . height ;
break ;
case LINEAR_GRAVITY_CENTER_VERTICAL :
finalPosY = layoutSize . height / 2.0f - cs . height * ( 0.5f - ap . y ) ;
break ;
default :
break ;
}
2013-12-23 15:02:52 +08:00
Margin mg = layoutParameter - > getMargin ( ) ;
2013-11-06 16:04:06 +08:00
finalPosX + = mg . left ;
finalPosY - = mg . top ;
2013-12-23 15:02:52 +08:00
child - > setPosition ( Point ( finalPosX , finalPosY ) ) ;
2013-11-06 16:04:06 +08:00
leftBoundary = child - > getRightInParent ( ) + mg . right ;
}
}
break ;
}
case LAYOUT_RELATIVE :
{
2013-12-23 15:02:52 +08:00
int length = _widgetChildren . size ( ) ;
int unlayoutChildCount = length ;
Size layoutSize = getSize ( ) ;
2013-11-06 16:04:06 +08:00
for ( int i = 0 ; i < length ; i + + )
{
2013-12-23 15:02:52 +08:00
Widget * child = static_cast < Widget * > ( _widgetChildren . at ( i ) ) ;
RelativeLayoutParameter * layoutParameter = dynamic_cast < RelativeLayoutParameter * > ( child - > getLayoutParameter ( LAYOUT_PARAMETER_RELATIVE ) ) ;
2013-11-06 16:04:06 +08:00
layoutParameter - > _put = false ;
}
while ( unlayoutChildCount > 0 )
{
for ( int i = 0 ; i < length ; i + + )
{
2013-12-23 15:02:52 +08:00
Widget * child = static_cast < Widget * > ( _widgetChildren . at ( i ) ) ;
RelativeLayoutParameter * layoutParameter = dynamic_cast < RelativeLayoutParameter * > ( child - > getLayoutParameter ( LAYOUT_PARAMETER_RELATIVE ) ) ;
2013-11-06 16:04:06 +08:00
if ( layoutParameter )
{
if ( layoutParameter - > _put )
{
continue ;
}
2013-12-23 15:02:52 +08:00
Point ap = child - > getAnchorPoint ( ) ;
Size cs = child - > getSize ( ) ;
RelativeAlign align = layoutParameter - > getAlign ( ) ;
2013-11-06 16:04:06 +08:00
const char * relativeName = layoutParameter - > getRelativeToWidgetName ( ) ;
2013-12-23 15:02:52 +08:00
Widget * relativeWidget = nullptr ;
RelativeLayoutParameter * relativeWidgetLP = nullptr ;
2013-11-06 16:04:06 +08:00
float finalPosX = 0.0f ;
float finalPosY = 0.0f ;
if ( relativeName & & strcmp ( relativeName , " " ) )
{
relativeWidget = UIHelper : : seekWidgetByRelativeName ( this , relativeName ) ;
if ( relativeWidget )
{
2013-12-23 15:02:52 +08:00
relativeWidgetLP = dynamic_cast < RelativeLayoutParameter * > ( relativeWidget - > getLayoutParameter ( LAYOUT_PARAMETER_RELATIVE ) ) ;
2013-11-06 16:04:06 +08:00
}
}
switch ( align )
{
case RELATIVE_ALIGN_NONE :
case RELATIVE_ALIGN_PARENT_TOP_LEFT :
finalPosX = ap . x * cs . width ;
finalPosY = layoutSize . height - ( ( 1.0f - ap . y ) * cs . height ) ;
break ;
case RELATIVE_ALIGN_PARENT_TOP_CENTER_HORIZONTAL :
finalPosX = layoutSize . width * 0.5f - cs . width * ( 0.5f - ap . x ) ;
finalPosY = layoutSize . height - ( ( 1.0f - ap . y ) * cs . height ) ;
break ;
case RELATIVE_ALIGN_PARENT_TOP_RIGHT :
finalPosX = layoutSize . width - ( ( 1.0f - ap . x ) * cs . width ) ;
finalPosY = layoutSize . height - ( ( 1.0f - ap . y ) * cs . height ) ;
break ;
case RELATIVE_ALIGN_PARENT_LEFT_CENTER_VERTICAL :
finalPosX = ap . x * cs . width ;
finalPosY = layoutSize . height * 0.5f - cs . height * ( 0.5f - ap . y ) ;
break ;
case RELATIVE_CENTER_IN_PARENT :
finalPosX = layoutSize . width * 0.5f - cs . width * ( 0.5f - ap . x ) ;
finalPosY = layoutSize . height * 0.5f - cs . height * ( 0.5f - ap . y ) ;
break ;
case RELATIVE_ALIGN_PARENT_RIGHT_CENTER_VERTICAL :
finalPosX = layoutSize . width - ( ( 1.0f - ap . x ) * cs . width ) ;
finalPosY = layoutSize . height * 0.5f - cs . height * ( 0.5f - ap . y ) ;
break ;
case RELATIVE_ALIGN_PARENT_LEFT_BOTTOM :
finalPosX = ap . x * cs . width ;
finalPosY = ap . y * cs . height ;
break ;
case RELATIVE_ALIGN_PARENT_BOTTOM_CENTER_HORIZONTAL :
finalPosX = layoutSize . width * 0.5f - cs . width * ( 0.5f - ap . x ) ;
finalPosY = ap . y * cs . height ;
break ;
case RELATIVE_ALIGN_PARENT_RIGHT_BOTTOM :
finalPosX = layoutSize . width - ( ( 1.0f - ap . x ) * cs . width ) ;
finalPosY = ap . y * cs . height ;
break ;
case RELATIVE_LOCATION_ABOVE_LEFTALIGN :
if ( relativeWidget )
{
if ( relativeWidgetLP & & ! relativeWidgetLP - > _put )
{
continue ;
}
float locationBottom = relativeWidget - > getTopInParent ( ) ;
float locationLeft = relativeWidget - > getLeftInParent ( ) ;
finalPosY = locationBottom + ap . y * cs . height ;
finalPosX = locationLeft + ap . x * cs . width ;
}
break ;
case RELATIVE_LOCATION_ABOVE_CENTER :
if ( relativeWidget )
{
if ( relativeWidgetLP & & ! relativeWidgetLP - > _put )
{
continue ;
}
2013-12-23 15:02:52 +08:00
Size rbs = relativeWidget - > getSize ( ) ;
2013-11-06 16:04:06 +08:00
float locationBottom = relativeWidget - > getTopInParent ( ) ;
finalPosY = locationBottom + ap . y * cs . height ;
finalPosX = relativeWidget - > getLeftInParent ( ) + rbs . width * 0.5f + ap . x * cs . width - cs . width * 0.5f ;
}
break ;
case RELATIVE_LOCATION_ABOVE_RIGHTALIGN :
if ( relativeWidget )
{
if ( relativeWidgetLP & & ! relativeWidgetLP - > _put )
{
continue ;
}
float locationBottom = relativeWidget - > getTopInParent ( ) ;
float locationRight = relativeWidget - > getRightInParent ( ) ;
finalPosY = locationBottom + ap . y * cs . height ;
finalPosX = locationRight - ( 1.0f - ap . x ) * cs . width ;
}
break ;
case RELATIVE_LOCATION_LEFT_OF_TOPALIGN :
if ( relativeWidget )
{
if ( relativeWidgetLP & & ! relativeWidgetLP - > _put )
{
continue ;
}
float locationTop = relativeWidget - > getTopInParent ( ) ;
float locationRight = relativeWidget - > getLeftInParent ( ) ;
finalPosY = locationTop - ( 1.0f - ap . y ) * cs . height ;
finalPosX = locationRight - ( 1.0f - ap . x ) * cs . width ;
}
break ;
case RELATIVE_LOCATION_LEFT_OF_CENTER :
if ( relativeWidget )
{
if ( relativeWidgetLP & & ! relativeWidgetLP - > _put )
{
continue ;
}
2013-12-23 15:02:52 +08:00
Size rbs = relativeWidget - > getSize ( ) ;
2013-11-06 16:04:06 +08:00
float locationRight = relativeWidget - > getLeftInParent ( ) ;
finalPosX = locationRight - ( 1.0f - ap . x ) * cs . width ;
finalPosY = relativeWidget - > getBottomInParent ( ) + rbs . height * 0.5f + ap . y * cs . height - cs . height * 0.5f ;
}
break ;
case RELATIVE_LOCATION_LEFT_OF_BOTTOMALIGN :
if ( relativeWidget )
{
if ( relativeWidgetLP & & ! relativeWidgetLP - > _put )
{
continue ;
}
float locationBottom = relativeWidget - > getBottomInParent ( ) ;
float locationRight = relativeWidget - > getLeftInParent ( ) ;
finalPosY = locationBottom + ap . y * cs . height ;
finalPosX = locationRight - ( 1.0f - ap . x ) * cs . width ;
}
break ;
case RELATIVE_LOCATION_RIGHT_OF_TOPALIGN :
if ( relativeWidget )
{
if ( relativeWidgetLP & & ! relativeWidgetLP - > _put )
{
continue ;
}
float locationTop = relativeWidget - > getTopInParent ( ) ;
float locationLeft = relativeWidget - > getRightInParent ( ) ;
finalPosY = locationTop - ( 1.0f - ap . y ) * cs . height ;
finalPosX = locationLeft + ap . x * cs . width ;
}
break ;
case RELATIVE_LOCATION_RIGHT_OF_CENTER :
if ( relativeWidget )
{
if ( relativeWidgetLP & & ! relativeWidgetLP - > _put )
{
continue ;
}
2013-12-23 15:02:52 +08:00
Size rbs = relativeWidget - > getSize ( ) ;
2013-11-06 16:04:06 +08:00
float locationLeft = relativeWidget - > getRightInParent ( ) ;
finalPosX = locationLeft + ap . x * cs . width ;
finalPosY = relativeWidget - > getBottomInParent ( ) + rbs . height * 0.5f + ap . y * cs . height - cs . height * 0.5f ;
}
break ;
case RELATIVE_LOCATION_RIGHT_OF_BOTTOMALIGN :
if ( relativeWidget )
{
if ( relativeWidgetLP & & ! relativeWidgetLP - > _put )
{
continue ;
}
float locationBottom = relativeWidget - > getBottomInParent ( ) ;
float locationLeft = relativeWidget - > getRightInParent ( ) ;
finalPosY = locationBottom + ap . y * cs . height ;
finalPosX = locationLeft + ap . x * cs . width ;
}
break ;
case RELATIVE_LOCATION_BELOW_LEFTALIGN :
if ( relativeWidget )
{
if ( relativeWidgetLP & & ! relativeWidgetLP - > _put )
{
continue ;
}
float locationTop = relativeWidget - > getBottomInParent ( ) ;
float locationLeft = relativeWidget - > getLeftInParent ( ) ;
finalPosY = locationTop - ( 1.0f - ap . y ) * cs . height ;
finalPosX = locationLeft + ap . x * cs . width ;
}
break ;
case RELATIVE_LOCATION_BELOW_CENTER :
if ( relativeWidget )
{
if ( relativeWidgetLP & & ! relativeWidgetLP - > _put )
{
continue ;
}
2013-12-23 15:02:52 +08:00
Size rbs = relativeWidget - > getSize ( ) ;
2013-11-06 16:04:06 +08:00
float locationTop = relativeWidget - > getBottomInParent ( ) ;
finalPosY = locationTop - ( 1.0f - ap . y ) * cs . height ;
finalPosX = relativeWidget - > getLeftInParent ( ) + rbs . width * 0.5f + ap . x * cs . width - cs . width * 0.5f ;
}
break ;
case RELATIVE_LOCATION_BELOW_RIGHTALIGN :
if ( relativeWidget )
{
if ( relativeWidgetLP & & ! relativeWidgetLP - > _put )
{
continue ;
}
float locationTop = relativeWidget - > getBottomInParent ( ) ;
float locationRight = relativeWidget - > getRightInParent ( ) ;
finalPosY = locationTop - ( 1.0f - ap . y ) * cs . height ;
finalPosX = locationRight - ( 1.0f - ap . x ) * cs . width ;
}
break ;
default :
break ;
}
2013-12-23 15:02:52 +08:00
Margin relativeWidgetMargin ;
Margin mg = layoutParameter - > getMargin ( ) ;
2013-11-15 14:37:34 +08:00
if ( relativeWidgetLP )
2013-11-06 16:04:06 +08:00
{
2013-11-15 14:37:34 +08:00
relativeWidgetMargin = relativeWidgetLP - > getMargin ( ) ;
2013-11-06 16:04:06 +08:00
}
//handle margin
switch ( align )
{
case RELATIVE_ALIGN_NONE :
case RELATIVE_ALIGN_PARENT_TOP_LEFT :
finalPosX + = mg . left ;
finalPosY - = mg . top ;
break ;
case RELATIVE_ALIGN_PARENT_TOP_CENTER_HORIZONTAL :
finalPosY - = mg . top ;
break ;
case RELATIVE_ALIGN_PARENT_TOP_RIGHT :
finalPosX - = mg . right ;
finalPosY - = mg . top ;
break ;
case RELATIVE_ALIGN_PARENT_LEFT_CENTER_VERTICAL :
finalPosX + = mg . left ;
break ;
case RELATIVE_CENTER_IN_PARENT :
break ;
case RELATIVE_ALIGN_PARENT_RIGHT_CENTER_VERTICAL :
finalPosX - = mg . right ;
break ;
case RELATIVE_ALIGN_PARENT_LEFT_BOTTOM :
finalPosX + = mg . left ;
finalPosY + = mg . bottom ;
break ;
case RELATIVE_ALIGN_PARENT_BOTTOM_CENTER_HORIZONTAL :
finalPosY + = mg . bottom ;
break ;
case RELATIVE_ALIGN_PARENT_RIGHT_BOTTOM :
finalPosX - = mg . right ;
finalPosY + = mg . bottom ;
break ;
case RELATIVE_LOCATION_ABOVE_LEFTALIGN :
2013-11-15 13:29:39 +08:00
finalPosY + = mg . bottom ;
2013-11-15 14:37:34 +08:00
if ( relativeWidgetLP - > getAlign ( ) ! = RELATIVE_ALIGN_PARENT_TOP_CENTER_HORIZONTAL
& & relativeWidgetLP - > getAlign ( ) ! = RELATIVE_ALIGN_PARENT_TOP_LEFT
& & relativeWidgetLP - > getAlign ( ) ! = RELATIVE_ALIGN_NONE
& & relativeWidgetLP - > getAlign ( ) ! = RELATIVE_ALIGN_PARENT_TOP_RIGHT )
{
finalPosY + = relativeWidgetMargin . top ;
}
2013-11-15 13:29:39 +08:00
finalPosX + = mg . left ;
break ;
2013-11-06 16:04:06 +08:00
case RELATIVE_LOCATION_ABOVE_RIGHTALIGN :
finalPosY + = mg . bottom ;
2013-11-15 14:37:34 +08:00
if ( relativeWidgetLP - > getAlign ( ) ! = RELATIVE_ALIGN_PARENT_TOP_CENTER_HORIZONTAL
& & relativeWidgetLP - > getAlign ( ) ! = RELATIVE_ALIGN_PARENT_TOP_LEFT
& & relativeWidgetLP - > getAlign ( ) ! = RELATIVE_ALIGN_NONE
& & relativeWidgetLP - > getAlign ( ) ! = RELATIVE_ALIGN_PARENT_TOP_RIGHT )
{
finalPosY + = relativeWidgetMargin . top ;
}
2013-11-15 13:29:39 +08:00
finalPosX - = mg . right ;
2013-11-06 16:04:06 +08:00
break ;
2013-11-15 13:29:39 +08:00
case RELATIVE_LOCATION_ABOVE_CENTER :
finalPosY + = mg . bottom ;
2013-11-15 14:37:34 +08:00
if ( relativeWidgetLP - > getAlign ( ) ! = RELATIVE_ALIGN_PARENT_TOP_CENTER_HORIZONTAL
& & relativeWidgetLP - > getAlign ( ) ! = RELATIVE_ALIGN_PARENT_TOP_LEFT
& & relativeWidgetLP - > getAlign ( ) ! = RELATIVE_ALIGN_NONE
& & relativeWidgetLP - > getAlign ( ) ! = RELATIVE_ALIGN_PARENT_TOP_RIGHT )
{
finalPosY + = relativeWidgetMargin . top ;
}
2013-11-15 13:29:39 +08:00
break ;
2013-11-06 16:04:06 +08:00
case RELATIVE_LOCATION_LEFT_OF_TOPALIGN :
2013-11-15 13:29:39 +08:00
finalPosX - = mg . right ;
2013-11-15 14:37:34 +08:00
if ( relativeWidgetLP - > getAlign ( ) ! = RELATIVE_ALIGN_PARENT_TOP_LEFT
& & relativeWidgetLP - > getAlign ( ) ! = RELATIVE_ALIGN_NONE
& & relativeWidgetLP - > getAlign ( ) ! = RELATIVE_ALIGN_PARENT_LEFT_BOTTOM
& & relativeWidgetLP - > getAlign ( ) ! = RELATIVE_ALIGN_PARENT_LEFT_CENTER_VERTICAL )
{
finalPosX - = relativeWidgetMargin . left ;
}
2013-11-15 13:29:39 +08:00
finalPosY - = mg . top ;
break ;
2013-11-06 16:04:06 +08:00
case RELATIVE_LOCATION_LEFT_OF_BOTTOMALIGN :
finalPosX - = mg . right ;
2013-11-15 14:37:34 +08:00
if ( relativeWidgetLP - > getAlign ( ) ! = RELATIVE_ALIGN_PARENT_TOP_LEFT
& & relativeWidgetLP - > getAlign ( ) ! = RELATIVE_ALIGN_NONE
& & relativeWidgetLP - > getAlign ( ) ! = RELATIVE_ALIGN_PARENT_LEFT_BOTTOM
& & relativeWidgetLP - > getAlign ( ) ! = RELATIVE_ALIGN_PARENT_LEFT_CENTER_VERTICAL )
{
finalPosX - = relativeWidgetMargin . left ;
}
2013-11-15 13:29:39 +08:00
finalPosY + = mg . bottom ;
2013-11-06 16:04:06 +08:00
break ;
2013-11-15 13:29:39 +08:00
case RELATIVE_LOCATION_LEFT_OF_CENTER :
finalPosX - = mg . right ;
2013-11-15 14:37:34 +08:00
if ( relativeWidgetLP - > getAlign ( ) ! = RELATIVE_ALIGN_PARENT_TOP_LEFT
& & relativeWidgetLP - > getAlign ( ) ! = RELATIVE_ALIGN_NONE
& & relativeWidgetLP - > getAlign ( ) ! = RELATIVE_ALIGN_PARENT_LEFT_BOTTOM
& & relativeWidgetLP - > getAlign ( ) ! = RELATIVE_ALIGN_PARENT_LEFT_CENTER_VERTICAL )
{
finalPosX - = relativeWidgetMargin . left ;
}
2013-11-15 13:29:39 +08:00
break ;
2013-11-06 16:04:06 +08:00
case RELATIVE_LOCATION_RIGHT_OF_TOPALIGN :
2013-11-15 13:29:39 +08:00
finalPosX + = mg . left ;
2013-11-15 14:37:34 +08:00
if ( relativeWidgetLP - > getAlign ( ) ! = RELATIVE_ALIGN_PARENT_TOP_RIGHT
& & relativeWidgetLP - > getAlign ( ) ! = RELATIVE_ALIGN_PARENT_RIGHT_BOTTOM
& & relativeWidgetLP - > getAlign ( ) ! = RELATIVE_ALIGN_PARENT_RIGHT_CENTER_VERTICAL )
{
finalPosX + = relativeWidgetMargin . right ;
}
2013-11-15 13:29:39 +08:00
finalPosY - = mg . top ;
break ;
2013-11-06 16:04:06 +08:00
case RELATIVE_LOCATION_RIGHT_OF_BOTTOMALIGN :
finalPosX + = mg . left ;
2013-11-15 14:37:34 +08:00
if ( relativeWidgetLP - > getAlign ( ) ! = RELATIVE_ALIGN_PARENT_TOP_RIGHT
& & relativeWidgetLP - > getAlign ( ) ! = RELATIVE_ALIGN_PARENT_RIGHT_BOTTOM
& & relativeWidgetLP - > getAlign ( ) ! = RELATIVE_ALIGN_PARENT_RIGHT_CENTER_VERTICAL )
{
finalPosX + = relativeWidgetMargin . right ;
}
2013-11-15 13:29:39 +08:00
finalPosY + = mg . bottom ;
2013-11-06 16:04:06 +08:00
break ;
2013-11-15 13:29:39 +08:00
case RELATIVE_LOCATION_RIGHT_OF_CENTER :
finalPosX + = mg . left ;
2013-11-15 14:37:34 +08:00
if ( relativeWidgetLP - > getAlign ( ) ! = RELATIVE_ALIGN_PARENT_TOP_RIGHT
& & relativeWidgetLP - > getAlign ( ) ! = RELATIVE_ALIGN_PARENT_RIGHT_BOTTOM
& & relativeWidgetLP - > getAlign ( ) ! = RELATIVE_ALIGN_PARENT_RIGHT_CENTER_VERTICAL )
{
finalPosX + = relativeWidgetMargin . right ;
}
2013-11-15 13:29:39 +08:00
break ;
2013-11-06 16:04:06 +08:00
case RELATIVE_LOCATION_BELOW_LEFTALIGN :
2013-11-15 13:29:39 +08:00
finalPosY - = mg . top ;
2013-11-15 14:37:34 +08:00
if ( relativeWidgetLP - > getAlign ( ) ! = RELATIVE_ALIGN_PARENT_LEFT_BOTTOM
& & relativeWidgetLP - > getAlign ( ) ! = RELATIVE_ALIGN_PARENT_RIGHT_BOTTOM
& & relativeWidgetLP - > getAlign ( ) ! = RELATIVE_ALIGN_PARENT_BOTTOM_CENTER_HORIZONTAL )
{
finalPosY - = relativeWidgetMargin . bottom ;
}
2013-11-15 13:29:39 +08:00
finalPosX + = mg . left ;
break ;
2013-11-06 16:04:06 +08:00
case RELATIVE_LOCATION_BELOW_RIGHTALIGN :
finalPosY - = mg . top ;
2013-11-15 14:37:34 +08:00
if ( relativeWidgetLP - > getAlign ( ) ! = RELATIVE_ALIGN_PARENT_LEFT_BOTTOM
& & relativeWidgetLP - > getAlign ( ) ! = RELATIVE_ALIGN_PARENT_RIGHT_BOTTOM
& & relativeWidgetLP - > getAlign ( ) ! = RELATIVE_ALIGN_PARENT_BOTTOM_CENTER_HORIZONTAL )
{
finalPosY - = relativeWidgetMargin . bottom ;
}
2013-11-15 13:29:39 +08:00
finalPosX - = mg . right ;
break ;
case RELATIVE_LOCATION_BELOW_CENTER :
2013-11-06 16:04:06 +08:00
finalPosY - = mg . top ;
2013-11-15 14:37:34 +08:00
if ( relativeWidgetLP - > getAlign ( ) ! = RELATIVE_ALIGN_PARENT_LEFT_BOTTOM
& & relativeWidgetLP - > getAlign ( ) ! = RELATIVE_ALIGN_PARENT_RIGHT_BOTTOM
& & relativeWidgetLP - > getAlign ( ) ! = RELATIVE_ALIGN_PARENT_BOTTOM_CENTER_HORIZONTAL )
{
finalPosY - = relativeWidgetMargin . bottom ;
}
2013-11-06 16:04:06 +08:00
break ;
default :
break ;
}
2013-12-23 15:02:52 +08:00
child - > setPosition ( Point ( finalPosX , finalPosY ) ) ;
2013-11-06 16:04:06 +08:00
layoutParameter - > _put = true ;
unlayoutChildCount - - ;
}
}
}
break ;
}
default :
break ;
}
2013-12-23 15:02:52 +08:00
_doLayoutDirty = false ;
2013-11-06 16:04:06 +08:00
}
2013-12-23 15:02:52 +08:00
std : : string Layout : : getDescription ( ) const
2013-11-06 16:04:06 +08:00
{
return " Layout " ;
}
2013-12-23 15:02:52 +08:00
Widget * Layout : : createCloneInstance ( )
2013-11-06 16:04:06 +08:00
{
2013-12-23 15:02:52 +08:00
return Layout : : create ( ) ;
2013-11-06 16:04:06 +08:00
}
2013-12-23 15:02:52 +08:00
void Layout : : copyClonedWidgetChildren ( Widget * model )
2013-11-06 16:04:06 +08:00
{
2013-12-23 15:02:52 +08:00
Widget : : copyClonedWidgetChildren ( model ) ;
2013-11-06 16:04:06 +08:00
}
2013-12-23 15:02:52 +08:00
void Layout : : copySpecialProperties ( Widget * widget )
2013-11-06 16:04:06 +08:00
{
2013-12-23 15:02:52 +08:00
Layout * layout = dynamic_cast < Layout * > ( widget ) ;
2013-11-06 16:04:06 +08:00
if ( layout )
{
setBackGroundImageScale9Enabled ( layout - > _backGroundScale9Enabled ) ;
setBackGroundImage ( layout - > _backGroundImageFileName . c_str ( ) , layout - > _bgImageTexType ) ;
setBackGroundImageCapInsets ( layout - > _backGroundImageCapInsets ) ;
setBackGroundColorType ( layout - > _colorType ) ;
setBackGroundColor ( layout - > _cColor ) ;
setBackGroundColor ( layout - > _gStartColor , layout - > _gEndColor ) ;
setBackGroundColorOpacity ( layout - > _cOpacity ) ;
setBackGroundColorVector ( layout - > _alongVector ) ;
setLayoutType ( layout - > _layoutType ) ;
setClippingEnabled ( layout - > _clippingEnabled ) ;
2013-12-23 15:02:52 +08:00
setClippingType ( layout - > _clippingType ) ;
2013-11-06 16:04:06 +08:00
}
}
}
2013-12-23 15:02:52 +08:00
NS_CC_END