cocos代码研究(22)Widget子类Layout学习笔记

理论基础

一个包含控件的容器。 子节点可以根据布局类型重新排序,它还可以开启剪裁,设置背景图像和颜色。继承自Widget,以及LayoutProtocol。

被 HBox, PageView, RelativeBox, ScrollView , 以及 VBox 继承。

主要有四种类型的布局:

  • 绝对布局:这个默认的布局类型、子元素按照绝对位置排列。
  • 水平布局:子元素水平排列。
  • 垂直布局:子元素垂直排列。
  • 相对布局:子元素相对于一定的规则排列。

代码实践

static Layout * create ()
创建一个空的layout。

//和背景图像,颜色,透明度有关
void setBackGroundImage (const std::string &fileName,    //图像文件路径。
TextureResType texType=TextureResType::LOCAL)
设置layout的背景图像。

const Rect & getBackGroundImageCapInsets () const
查询背景图像的capInsets。

void setBackGroundColorType (BackGroundColorType type)
设置布局的背景颜色类型

BackGroundColorType getBackGroundColorType () const
查询布局的背景颜色类型。

void setBackGroundColor (const Color3B &color)
设置layout的背景颜色 仅当颜色类型为BackGroundColorType::SOLID的时候,才会影响Layout

const Color3B & getBackGroundColor () const
查询布局的背景颜色。

void setBackGroundColor (const Color3B &startColor, const Color3B &endColor)
为布局设置开始和结束的背景颜色。 该设置只当布局的颜色类型是BackGroundColorType::GRADIENT时生效

//与渐变有关
const Color3B & getBackGroundStartColor () const
获取渐变背景颜色开始的值。

const Color3B & getBackGroundEndColor () const
获取渐变背景颜色的结束值。

void setBackGroundColorOpacity (GLubyte opacity)
设置布局的背景颜色不透明度。

GLubyte getBackGroundColorOpacity () const
获取布局的背景颜色不透明度。

void setBackGroundColorVector (const Vec2 &vector)
设置布局的背景颜色矢量。 该设置只当布局的颜色类型是BackGroundColorType::GRADIENT时生效

const Vec2 & getBackGroundColorVector () const
获取layout的背景颜色向量。

void setBackGroundImageColor (const Color3B &color)
设置layout背景图像的颜色。

void setBackGroundImageOpacity (GLubyte opacity)
设置背景图像的不透明度。

const Color3B & getBackGroundImageColor () const
得到layout的背景图像颜色。

GLubyte getBackGroundImageOpacity () const
获取layout的背景图像不透明度。

void removeBackGroundImage ()
删除layout的背景图像。 

const Size & getBackGroundImageTextureSize () const
获取背景图像纹理大小。

virtual void setClippingEnabled (bool enabled)
切换布局剪裁。 如果你需要裁剪,你需要传递true给这个函数。

void setClippingType (ClippingType type)
改变layout的剪裁类型。 在默认情况下,剪切类型是“ClippingType:STENCIL”。

ClippingType getClippingType () const
virtual bool isClippingEnabled () const
获取layout是否开启了剪裁。

virtual std::string getDescription () const override
返回组件的类名 

virtual void setLayoutType (Type type)
改变布局类型。

virtual Type getLayoutType () const
查询layout类型。

virtual void forceDoLayout ()
强制刷新控件的布局 

void requestDoLayout ()
请求刷新控件的布局 

void setLoopFocus (bool loop)
如果一个布局是循环获取焦点,这意味着焦点在layout中移动

//和scale9技术有关
void setBackGroundImageCapInsets (const Rect &capInsets)
设置layout背景的capInsets,它只在开启scale9后影响背景图的渲染。

void setBackGroundImageScale9Enabled (bool enabled)
开启背景图像scale9的渲染模式。

bool isBackGroundImageScale9Enabled () const
查询背景图像scale9启用状态。

实例:

        Layout* root = static_cast<Layout*>(_uiLayer->getChildByTag(81));
        
        Layout* background = static_cast<Layout*>(root->getChildByName("background_Panel"));
        
        // Create the layout with color render
        Layout* layout = Layout::create();
        layout->setBackGroundColorType(Layout::BackGroundColorType::SOLID);
        layout->setBackGroundColor(Color3B(128, 128, 128));
        layout->setContentSize(Size(280, 150));
        Size backgroundSize = background->getContentSize();
        layout->setPosition(Vec2((widgetSize.width - backgroundSize.width) / 2.0f +
                                  (backgroundSize.width - layout->getContentSize().width) / 2.0f,
                                  (widgetSize.height - backgroundSize.height) / 2.0f +
                                  (backgroundSize.height - layout->getContentSize().height) / 2.0f));
        _uiLayer->addChild(layout);

 

转载于:https://www.cnblogs.com/damowang/p/4864415.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值