zoukankan      html  css  js  c++  java
  • 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);
  • 相关阅读:
    mysql--数据库的基本操作(二)
    mysql--数据库剧本指令操作
    poj_3461 KMP算法解析
    POJ_3122 经典二分题
    LIS(最长上升子序列)的 DP 与 (贪心+二分) 两种解法
    HDU_1059 多重背包问题
    HDU-1114 完全背包+恰好装满问题
    HDU _2546 01背包问题
    POJ-1611 并查集
    HDU——Monkey and Banana 动态规划
  • 原文地址:https://www.cnblogs.com/damowang/p/4864415.html
Copyright © 2011-2022 走看看