cocos2dx 3.x (单选,多选,复选checkBox按钮的实现) RadioButton

时间:2023-03-08 20:49:50
 //
//  LandLordsMakeNewRoom.hpp
//  MalaGame39
//
//  Created by work on 2016/12/19.
//
//
 
#ifndef LandLordsMakeNewRoom_hpp
#define LandLordsMakeNewRoom_hpp
 
#include <stdio.h>
#include <cocos2d.h>
#include "ui/cocosGUI.h"
#include <cocostudio/CocoStudio.h>
using namespace cocos2d::ui;
 
class LandLordsMakeNewRoom : public cocos2d::Layer
{
private:
    cocos2d::Node* m_rootNode;
    Button* m_return;
//    long long m_ruleNum;
    RadioButton* m_boomButton[];
    RadioButton* m_numberButton[];
    int m_ruleNum[] = {,,,,,};
public:
    bool init();
    void onEnter();
    void onExit();
    void buttonReturnCallback(Ref *senderz, cocos2d::extension::Control::EventType controlEvent);
//    void onChangedRadioButtonOneSelect(RadioButton* radioButton, RadioButton::EventType type);
//    void onChangedRadioButtonTwoSelect(RadioButton* radioButton, RadioButton::EventType type);
//    void onChangedRadioButtonThreeSelect(RadioButton* radioButton, RadioButton::EventType type);
//    void onChangedRadioButtonFourSelect(RadioButton* radioButton, RadioButton::EventType type);
    void buttonMakeNewRoomCallback(Ref *senderz, cocos2d::extension::Control::EventType controlEvent);
    void onChangedRadioButtonBoomBgSelect(RadioButton* radioButton, RadioButton::EventType type);
    void onChangedRadioButtonChooseBgSelect(RadioButton* radioButton, RadioButton::EventType type);
    void setLayerData(GameRuleData& rule);
    void checkBoxCallback(CheckBox* sender,CheckBoxEventType type);
    CREATE_FUNC(LandLordsMakeNewRoom);
};
 
 
#endif /* LandLordsMakeNewRoom_hpp */
 
 
 //
//  LandLordsMakeNewRoom.cpp
//  MalaGame39
//
//  Created by work on 2016/12/19.
//
//
 
#include "LandLordsMakeNewRoom.hpp"
#include "cocos-ext.h"
#include <stdio.h>
USING_NS_CC;
using namespace cocos2d::ui;
using namespace cocos2d::extension;
bool LandLordsMakeNewRoom::init()
{
    if (!Layer::init()) {
        return false;
    }
    
    Sprite* bg = Sprite::create("LandLord/LandLordsmake_room_bg.png");//背景图片
    bg->setPosition(Vec2(,));
    this->addChild(bg);
    
    
    auto Relieve=cocos2d::ui::Scale9Sprite::createWithSpriteFrameName("friend_fight_x.png");
    auto m_buttonRelieve = ControlButton::create(Relieve);
    m_buttonRelieve->setPosition( 991.5,644.8);
    m_buttonRelieve->addTargetWithActionForControlEvents(this, cccontrol_selector(LandLordsMakeNewRoom::buttonReturnCallback), Control::EventType::TOUCH_UP_INSIDE);//按钮点击事件
    this->addChild(m_buttonRelieve);
    
    
    auto share=cocos2d::ui::Scale9Sprite::createWithSpriteFrameName("make_btn.png");
    auto m_buttonShare = ControlButton::create(share);
    m_buttonShare->setPosition( ,);
    m_buttonShare->addTargetWithActionForControlEvents(this, cccontrol_selector(LandLordsMakeNewRoom::buttonMakeNewRoomCallback), Control::EventType::TOUCH_UP_INSIDE);
    this->addChild(m_buttonShare);
 
    
    CheckBox* checkBox[] = {nullptr};
    for (int i=;i<;i++){
        //for循环创建4个复选按钮
        checkBox[i] = CheckBox::create("LandLord/make_room_unselected.png",//未选中时的显示图片
                                       "LandLord/make_room_unselected.png",//点击checkBox后触摸没有松开时的图片
                                       "LandLord/make_room_select.png",//选中时的显示图片
                                       "LandLord/make_room_select.png",//从选中到未选中切换过程中,触摸没有松开时的显示图片
                                       "LandLord/make_room_select.png");
        
         checkBox[i]->::LandLordsMakeNewRoom::setPosition(,-(i*));
         checkBox[i]->setTag(i+);
         checkBox[i]->addEventListenerCheckBox(this ,checkboxselectedeventselector(LandLordsMakeNewRoom::checkBoxCallback));
         this->addChild(checkBox[i]);
        
    }
    
    
 
    for (int i = ; i<; i++) {//单选(按钮)
        m_boomButton[i] = RadioButton::create("make_room_unselected.png", "make_room_select.png", Widget::TextureResType::PLIST);
        m_boomButton[i]->setPosition(Vec2(+(*i), ));
        m_boomButton[i]->setTag(i+);
        m_boomButton[i]->addEventListener(CC_CALLBACK_2(LandLordsMakeNewRoom::onChangedRadioButtonBoomBgSelect,this));
        this->addChild(m_boomButton[i]);
    }
 
    
    
    int number[] = {,,};//局数单选
    for (int i = ; i<; i++) {
        m_numberButton[i] = RadioButton::create("make_room_unselected.png", "make_room_select.png", Widget::TextureResType::PLIST);
        m_numberButton[i]->setPosition(Vec2(+*i+, ));
        m_numberButton[i]->setTag(i+);
        m_numberButton[i]->addEventListener(CC_CALLBACK_2(::LandLordsMakeNewRoom::onChangedRadioButtonChooseBgSelect,this));
        this->addChild(m_numberButton[i]);
        auto label = Label::createWithSystemFont(StringUtils::format("%d局(房卡x%d)",number[i],i+), "", );
        label->setColor(Color3B::YELLOW);
        label->setPosition(Vec2(+*i+, ));
        this->addChild(label);
    }
    
    m_boomButton[]->setSelected(true);//默认第一个单选按钮为选中
    m_numberButton[]->setSelected(true);
    
    PublicMethod::addLayerTouchShield(this);
 
    return true;
}
void LandLordsMakeNewRoom::onEnter()
{
    Layer::onEnter();
}
void LandLordsMakeNewRoom::onExit()
{
    Layer::onExit();
}
 
void LandLordsMakeNewRoom::onChangedRadioButtonBoomBgSelect(RadioButton* radioButton, RadioButton::EventType type)
{
    
    if (radioButton == nullptr) {
        return;
    }
    
    int tag = ((RadioButton*)radioButton)->getTag();//获取按钮当前的tag值
    
    for (int i=; i<; i++) {//for循环实现单选
        m_boomButton[i]->setSelected(false);//先让全部按钮设为处于未选中状态
        if (i+==tag) {//通过tag值进入判断
            m_boomButton[i]->setSelected(true);//让当前按钮为选中
            m_ruleNum[]=tag-;
        }
    }
    
}
void LandLordsMakeNewRoom::onChangedRadioButtonChooseBgSelect(RadioButton* radioButton, RadioButton::EventType type)
{
    if (radioButton == nullptr) {
        return;
    }
    
    int tag = ((RadioButton*)radioButton)->getTag();
    
    for (int i=; i<; i++) {
        m_numberButton[i]->setSelected(false);
        if (i+==tag) {
            m_numberButton[i]->setSelected(true);
            m_ruleNum[]=tag-;
        }
    }
} //复选按钮的实现方法
void LandLordsMakeNewRoom::checkBoxCallback(CheckBox* sender,CheckBoxEventType type)
{
    int tag = sender->getTag();//同样需要拿到当前checkBox的tag值
    if(type==CHECKBOX_STATE_EVENT_SELECTED){//判断check的状态(选中状态)
        CCLOG("sender:%d",tag);
        m_ruleNum[tag-]=;
//在这里可做其他操作
    }
    else if(type==CHECKBOX_STATE_EVENT_UNSELECTED)//为未选中状态
    {
        m_ruleNum[tag-]=;
    }
 
}
void LandLordsMakeNewRoom::buttonReturnCallback(Ref *senderz, Control::EventType controlEvent)
{
        removeFromParent();//移除当前图层
    
}