• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
养眼大魔王
博客园    首页    新随笔    联系   管理    订阅  订阅

cocos代码研究(16)Widget子类RadioButton学习笔记

理论基础

RadioButton是一种特定类型的两状态按钮,它与复选框相似。它可以 和RadioButtonGroup一起使用,形成一个"组"。继承自AbstractCheckButton,AbstractCheckButton继承自Widget。

代码部分

void addEventListener (const ccRadioButtonCallback &callback)
注册一个回调函数, 它将在单选按钮被选中或者取消的时候被调用.

bool isSelected () const
查询是否选中复选框。

void setSelected (bool selected)
修改复选框的状态 设置True会使复选框变为“选中状态”,否则传False会使复选框变为“未选中状态”

void loadTextures (const std::string &background, //背景普通状态图片的名字。
const std::string &backgroundSelected, //背景选择状态图片的名字。
const std::string &cross, //勾选选中状态图片的名字。
const std::string &backgroundDisabled, //背景禁用状态图片名字。
const std::string &frontCrossDisabled, //勾选禁用状态图片名字。
TextureResType texType=TextureResType::LOCAL) //
加载所有纹理并初始化一个复选框。

void loadTextureBackGround (const std::string &backGround, TextureResType type=TextureResType::LOCAL)
加载背景普通状态纹理

void loadTextureBackGroundSelected (const std::string &backGroundSelected, TextureResType texType=TextureResType::LOCAL)
加载背景选中状态纹理的图片

void loadTextureFrontCross (const std::string &crossTextureName, TextureResType texType=TextureResType::LOCAL)
加载勾选选中状态的纹理图片

void loadTextureBackGroundDisabled (const std::string &backGroundDisabled, TextureResType texType=TextureResType::LOCAL)
加载背景禁用状态纹理的图片

void loadTextureFrontCrossDisabled (const std::string &frontCrossDisabled, TextureResType texType=TextureResType::LOCAL)
加载勾选禁用状态的纹理图片

virtual Size getVirtualRendererSize () const override
获取虚拟渲染节点的尺寸

virtual Node * getVirtualRenderer () override
控件的内部渲染节点。 例如,一个按钮的虚拟渲染节点是它的纹理节点。

void setZoomScale (float scale)
当用户按下复选框时,按钮将会缩放到一个比例 最后复选框的缩放值等于(复选框原尺寸+ _zoomScale)

float getZoomScale () const
返回一个缩放比例

Sprite * getRendererBackground () const
返回背景普通状态的渲染节点

Sprite * getRendererBackgroundSelected () const
返回背景选择状态的渲染节点

Sprite * getRendererFrontCross () const
返回前景普通状态的渲染节点

Sprite * getRendererBackgroundDisabled () const
返回背景禁用状态的渲染节点

Sprite * getRendererFrontCrossDisabled () const
返回前景禁用状态的渲染节点

实例:

        Size widgetSize = _widget->getContentSize();
        
        // Create a radio button group
        _radioButtonGroup = RadioButtonGroup::create();
        _uiLayer->addChild(_radioButtonGroup);
        
        // Create the radio buttons
        static const int NUMBER_OF_BUTTONS = 5;
        startPosX = widgetSize.width / 2.0f - ((NUMBER_OF_BUTTONS - 1) / 2.0f) * BUTTON_WIDTH;
        for(int i = 0; i < NUMBER_OF_BUTTONS; ++i)
        {
            RadioButton* radioButton = RadioButton::create("cocosui/radio_button_off.png", "cocosui/radio_button_on.png");
            float posX = startPosX + BUTTON_WIDTH * i;
            radioButton->setPosition(Vec2(posX, widgetSize.height / 2.0f + 10));
            radioButton->setScale(1.2f);
            _radioButtonGroup->addRadioButton(radioButton);
            _uiLayer->addChild(radioButton);
        }

 

posted @ 2015-10-08 16:25  养眼大魔王  阅读(1727)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3