diff --git a/cocos/ui/UIRadioButton.cpp b/cocos/ui/UIRadioButton.cpp index 7fe4851baf..a7d0abccde 100644 --- a/cocos/ui/UIRadioButton.cpp +++ b/cocos/ui/UIRadioButton.cpp @@ -149,7 +149,7 @@ void RadioButton::copySpecialProperties(Widget *widget) AbstractCheckButton::copySpecialProperties(widget); _radioButtonEventCallback = radioButton->_radioButtonEventCallback; _ccEventCallback = radioButton->_ccEventCallback; - _group = radioButton->_group; + _group = radioButton->_group; } } @@ -164,7 +164,7 @@ RadioButtonGroup::~RadioButtonGroup() { _radioButtonGroupEventCallback = nullptr; _selectedRadioButton = nullptr; - _radioButtons.clear(); + _radioButtons.clear(); } RadioButtonGroup* RadioButtonGroup::create() @@ -189,7 +189,7 @@ void RadioButtonGroup::addRadioButton(RadioButton* radioButton) if(radioButton != nullptr) { radioButton->_group = this; - _radioButtons.pushBack(radioButton); + _radioButtons.pushBack(radioButton); if(!_allowedNoSelection && _selectedRadioButton == nullptr) { @@ -200,13 +200,13 @@ void RadioButtonGroup::addRadioButton(RadioButton* radioButton) void RadioButtonGroup::removeRadioButton(RadioButton* radioButton) { - ssize_t index = _radioButtons.getIndex(radioButton); - if( index == CC_INVALID_INDEX ) - { - CCLOGERROR("The radio button does not belong to this group!"); - return; - } - + ssize_t index = _radioButtons.getIndex(radioButton); + if( index == CC_INVALID_INDEX ) + { + CCLOGERROR("The radio button does not belong to this group!"); + return; + } + if(radioButton != nullptr) { radioButton->_group = nullptr; @@ -214,28 +214,28 @@ void RadioButtonGroup::removeRadioButton(RadioButton* radioButton) { deselect(); } - _radioButtons.erase(index); - - if(!_allowedNoSelection && _selectedRadioButton == nullptr && !_radioButtons.empty()) - { - setSelectedButton(0); - } + _radioButtons.erase(index); + + if(!_allowedNoSelection && _selectedRadioButton == nullptr && !_radioButtons.empty()) + { + setSelectedButton(0); + } } } ssize_t RadioButtonGroup::getNumberOfRadioButtons() const { - return _radioButtons.size(); + return _radioButtons.size(); } RadioButton* RadioButtonGroup::getRadioButtonByIndex(int index) const { - if(index >= _radioButtons.size()) - { - CCLOGERROR("Out of array index! length=%d, requestedIndex=%d", (int)_radioButtons.size(), index); - return nullptr; - } - return _radioButtons.at(index); + if(index >= _radioButtons.size()) + { + CCLOGERROR("Out of array index! length=%d, requestedIndex=%d", (int)_radioButtons.size(), index); + return nullptr; + } + return _radioButtons.at(index); } void RadioButtonGroup::deselect() @@ -306,14 +306,14 @@ void RadioButtonGroup::copySpecialProperties(Widget *widget) { _radioButtonGroupEventCallback = radioButtonGroup->_radioButtonGroupEventCallback; _ccEventCallback = radioButtonGroup->_ccEventCallback; - _selectedRadioButton = radioButtonGroup->_selectedRadioButton; - _allowedNoSelection = radioButtonGroup->_allowedNoSelection; - - _radioButtons.clear(); - for(const auto& radioButton : radioButtonGroup->_radioButtons) - { - _radioButtons.pushBack(radioButton); - } + _selectedRadioButton = radioButtonGroup->_selectedRadioButton; + _allowedNoSelection = radioButtonGroup->_allowedNoSelection; + + _radioButtons.clear(); + for(const auto& radioButton : radioButtonGroup->_radioButtons) + { + _radioButtons.pushBack(radioButton); + } } } diff --git a/cocos/ui/UIRadioButton.h b/cocos/ui/UIRadioButton.h index 808894232d..069ec91575 100644 --- a/cocos/ui/UIRadioButton.h +++ b/cocos/ui/UIRadioButton.h @@ -182,9 +182,9 @@ public: virtual void addRadioButton(RadioButton* radioButton); virtual void removeRadioButton(RadioButton* radioButton); - - ssize_t getNumberOfRadioButtons() const; - RadioButton* getRadioButtonByIndex(int index) const; + + ssize_t getNumberOfRadioButtons() const; + RadioButton* getRadioButtonByIndex(int index) const; void setAllowedNoSelection(bool allowedNoSelection); bool isAllowedNoSelection() const; @@ -197,8 +197,8 @@ protected: void onChangedRadioButtonSelect(RadioButton* radioButton); void deselect(); - - Vector _radioButtons; + + Vector _radioButtons; ccRadioButtonGroupCallback _radioButtonGroupEventCallback; RadioButton* _selectedRadioButton; bool _allowedNoSelection; diff --git a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIRadioButtonTest/UIRadioButtonTest.cpp b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIRadioButtonTest/UIRadioButtonTest.cpp index aacab007e9..00d46b8ea5 100644 --- a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIRadioButtonTest/UIRadioButtonTest.cpp +++ b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIRadioButtonTest/UIRadioButtonTest.cpp @@ -5,9 +5,9 @@ using namespace cocos2d::ui; UIRadioButtonTests::UIRadioButtonTests() { - ADD_TEST_CASE(UIRadioButtonTest); - ADD_TEST_CASE(UIRadioButtonTwoGroupsTest); - ADD_TEST_CASE(UIRadioButtonTabTest); + ADD_TEST_CASE(UIRadioButtonTest); + ADD_TEST_CASE(UIRadioButtonTwoGroupsTest); + ADD_TEST_CASE(UIRadioButtonTabTest); } @@ -26,93 +26,93 @@ static float startPosX = 0; bool UIRadioButtonTest::init() { - if (UIScene::init()) - { - 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); - } - - // Add button - Button* addButton = Button::create("cocosui/backtotopnormal.png", "cocosui/backtotoppressed.png"); - addButton->setTitleText("Add"); - addButton->setPosition(Vec2(widgetSize.width / 2.0f - 100, widgetSize.height / 2.0f - 65)); - addButton->addClickEventListener(CC_CALLBACK_1(UIRadioButtonTest::addRadioButton, this)); - addButton->setScale(0.7f); - _uiLayer->addChild(addButton); - - // Delete button - Button* deleteButton = Button::create("cocosui/backtotopnormal.png", "cocosui/backtotoppressed.png"); - deleteButton->setTitleText("Delete"); - deleteButton->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - 65)); - deleteButton->addClickEventListener(CC_CALLBACK_1(UIRadioButtonTest::deleteRadioButton, this)); - deleteButton->setScale(0.7f); - _uiLayer->addChild(deleteButton); - - // Toggle button - Button* allowNoSelectionToggle = Button::create("cocosui/backtotopnormal.png", "cocosui/backtotoppressed.png"); - allowNoSelectionToggle->setTitleText("Toggle Allow-No-Selection"); - allowNoSelectionToggle->setPosition(Vec2(widgetSize.width / 2.0f + 100, widgetSize.height / 2.0f - 65)); - allowNoSelectionToggle->addClickEventListener([this](Ref*) { - _radioButtonGroup->setAllowedNoSelection(!_radioButtonGroup->isAllowedNoSelection()); - _allowNoSelectionText->setString(_radioButtonGroup->isAllowedNoSelection() ? "No selection is allowed." : "No selection is disallowed."); - }); - allowNoSelectionToggle->setScale(0.7f); - _uiLayer->addChild(allowNoSelectionToggle); - - // Add a label for toggle - _allowNoSelectionText = Text::create("No selection is disallowed.", "fonts/Marker Felt.ttf", 20); - _allowNoSelectionText->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f + 70)); - _uiLayer->addChild(_allowNoSelectionText); + if (UIScene::init()) + { + 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); + } + + // Add button + Button* addButton = Button::create("cocosui/backtotopnormal.png", "cocosui/backtotoppressed.png"); + addButton->setTitleText("Add"); + addButton->setPosition(Vec2(widgetSize.width / 2.0f - 100, widgetSize.height / 2.0f - 65)); + addButton->addClickEventListener(CC_CALLBACK_1(UIRadioButtonTest::addRadioButton, this)); + addButton->setScale(0.7f); + _uiLayer->addChild(addButton); + + // Delete button + Button* deleteButton = Button::create("cocosui/backtotopnormal.png", "cocosui/backtotoppressed.png"); + deleteButton->setTitleText("Delete"); + deleteButton->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - 65)); + deleteButton->addClickEventListener(CC_CALLBACK_1(UIRadioButtonTest::deleteRadioButton, this)); + deleteButton->setScale(0.7f); + _uiLayer->addChild(deleteButton); + + // Toggle button + Button* allowNoSelectionToggle = Button::create("cocosui/backtotopnormal.png", "cocosui/backtotoppressed.png"); + allowNoSelectionToggle->setTitleText("Toggle Allow-No-Selection"); + allowNoSelectionToggle->setPosition(Vec2(widgetSize.width / 2.0f + 100, widgetSize.height / 2.0f - 65)); + allowNoSelectionToggle->addClickEventListener([this](Ref*) { + _radioButtonGroup->setAllowedNoSelection(!_radioButtonGroup->isAllowedNoSelection()); + _allowNoSelectionText->setString(_radioButtonGroup->isAllowedNoSelection() ? "No selection is allowed." : "No selection is disallowed."); + }); + allowNoSelectionToggle->setScale(0.7f); + _uiLayer->addChild(allowNoSelectionToggle); + + // Add a label for toggle + _allowNoSelectionText = Text::create("No selection is disallowed.", "fonts/Marker Felt.ttf", 20); + _allowNoSelectionText->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f + 70)); + _uiLayer->addChild(_allowNoSelectionText); - return true; - } - return false; + return true; + } + return false; } void UIRadioButtonTest::addRadioButton(Ref* sender) { - Vec2 pos; - if(_radioButtonGroup->getNumberOfRadioButtons() > 0) - { - RadioButton* lastRadioButton = _radioButtonGroup->getRadioButtonByIndex((int)_radioButtonGroup->getNumberOfRadioButtons() - 1); - pos = lastRadioButton->getPosition(); - } - else - { - pos.x = startPosX - BUTTON_WIDTH; - pos.y = _widget->getContentSize().height / 2.0f + 10; - } - - RadioButton* radioButton = RadioButton::create("cocosui/radio_button_off.png", "cocosui/radio_button_on.png"); - pos.x += + BUTTON_WIDTH; - radioButton->setPosition(pos); - _radioButtonGroup->addRadioButton(radioButton); - _uiLayer->addChild(radioButton); + Vec2 pos; + if(_radioButtonGroup->getNumberOfRadioButtons() > 0) + { + RadioButton* lastRadioButton = _radioButtonGroup->getRadioButtonByIndex((int)_radioButtonGroup->getNumberOfRadioButtons() - 1); + pos = lastRadioButton->getPosition(); + } + else + { + pos.x = startPosX - BUTTON_WIDTH; + pos.y = _widget->getContentSize().height / 2.0f + 10; + } + + RadioButton* radioButton = RadioButton::create("cocosui/radio_button_off.png", "cocosui/radio_button_on.png"); + pos.x += + BUTTON_WIDTH; + radioButton->setPosition(pos); + _radioButtonGroup->addRadioButton(radioButton); + _uiLayer->addChild(radioButton); } void UIRadioButtonTest::deleteRadioButton(Ref* sender) { - if(_radioButtonGroup->getNumberOfRadioButtons() > 0) - { - RadioButton* radioButton = _radioButtonGroup->getRadioButtonByIndex((int)_radioButtonGroup->getNumberOfRadioButtons() - 1); - _radioButtonGroup->removeRadioButton(radioButton); - _uiLayer->removeChild(radioButton); - } + if(_radioButtonGroup->getNumberOfRadioButtons() > 0) + { + RadioButton* radioButton = _radioButtonGroup->getRadioButtonByIndex((int)_radioButtonGroup->getNumberOfRadioButtons() - 1); + _radioButtonGroup->removeRadioButton(radioButton); + _uiLayer->removeChild(radioButton); + } } @@ -125,8 +125,8 @@ _buttonEventLabel(nullptr), _logConsole(nullptr), _numberOfLogLines(0) { - _radioButtonGroups[0] = nullptr; - _radioButtonGroups[1] = nullptr; + _radioButtonGroups[0] = nullptr; + _radioButtonGroups[1] = nullptr; } UIRadioButtonTwoGroupsTest::~UIRadioButtonTwoGroupsTest() @@ -140,77 +140,77 @@ bool UIRadioButtonTwoGroupsTest::init() Size widgetSize = _widget->getContentSize();; // Add a label in which the radio button events will be displayed - _groupEventLabel = Text::create("RadioButtonGroup : No Event", "fonts/Marker Felt.ttf", 25); - _groupEventLabel->setAnchorPoint(Vec2(0.5f, -1)); - _groupEventLabel->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f + 70)); - _uiLayer->addChild(_groupEventLabel); - + _groupEventLabel = Text::create("RadioButtonGroup : No Event", "fonts/Marker Felt.ttf", 25); + _groupEventLabel->setAnchorPoint(Vec2(0.5f, -1)); + _groupEventLabel->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f + 70)); + _uiLayer->addChild(_groupEventLabel); + _buttonEventLabel = Text::create("RadioButton : No Event", "fonts/Marker Felt.ttf", 25); _buttonEventLabel->setAnchorPoint(Vec2(0.5f, -1)); _buttonEventLabel->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f + 35)); _uiLayer->addChild(_buttonEventLabel); - - _logConsole = Text::create("", "fonts/Marker Felt.ttf", 10); - _logConsole->setAnchorPoint(Vec2(0, 1)); - _logConsole->setPosition(Vec2(widgetSize.width / 2.0f + 110, widgetSize.height / 2.0f + 55)); - _uiLayer->addChild(_logConsole); + + _logConsole = Text::create("", "fonts/Marker Felt.ttf", 10); + _logConsole->setAnchorPoint(Vec2(0, 1)); + _logConsole->setPosition(Vec2(widgetSize.width / 2.0f + 110, widgetSize.height / 2.0f + 55)); + _uiLayer->addChild(_logConsole); - static const int NUMBER_OF_BUTTONS = 4; - static const float SPACE_BETWEEN_BUTTONS = 50; - float startPosX = widgetSize.width / 2.0f - ((NUMBER_OF_BUTTONS - 1) / 2.0f) * SPACE_BETWEEN_BUTTONS; - - for(int type = 0; type < 2; ++type) - { - // Create a radio button group - const char *normalImage, *selectedImage; - float posYAdjust = 0; - _radioButtonGroups[type] = RadioButtonGroup::create(); - if(type == 0) - { - _radioButtonGroups[type]->addEventListener(CC_CALLBACK_3(UIRadioButtonTwoGroupsTest::onChangedRadioButtonGroup1, this)); - normalImage = "cocosui/radio_button_off.png"; - selectedImage = "cocosui/radio_button_on.png"; - posYAdjust = 35; - } - else - { - _radioButtonGroups[type]->addEventListener(CC_CALLBACK_3(UIRadioButtonTwoGroupsTest::onChangedRadioButtonGroup2, this)); - normalImage = "cocosui/UIEditorTest/2.1/Button/button_common_box03_003 copy 221.png"; - selectedImage = "cocosui/UIEditorTest/2.1/Button/button_common_box03_001.png"; - posYAdjust = -15; - } - _uiLayer->addChild(_radioButtonGroups[type]); - - // Set allowing no selections - _radioButtonGroups[type]->setAllowedNoSelection(type == 0); + static const int NUMBER_OF_BUTTONS = 4; + static const float SPACE_BETWEEN_BUTTONS = 50; + float startPosX = widgetSize.width / 2.0f - ((NUMBER_OF_BUTTONS - 1) / 2.0f) * SPACE_BETWEEN_BUTTONS; + + for(int type = 0; type < 2; ++type) + { + // Create a radio button group + const char *normalImage, *selectedImage; + float posYAdjust = 0; + _radioButtonGroups[type] = RadioButtonGroup::create(); + if(type == 0) + { + _radioButtonGroups[type]->addEventListener(CC_CALLBACK_3(UIRadioButtonTwoGroupsTest::onChangedRadioButtonGroup1, this)); + normalImage = "cocosui/radio_button_off.png"; + selectedImage = "cocosui/radio_button_on.png"; + posYAdjust = 35; + } + else + { + _radioButtonGroups[type]->addEventListener(CC_CALLBACK_3(UIRadioButtonTwoGroupsTest::onChangedRadioButtonGroup2, this)); + normalImage = "cocosui/UIEditorTest/2.1/Button/button_common_box03_003 copy 221.png"; + selectedImage = "cocosui/UIEditorTest/2.1/Button/button_common_box03_001.png"; + posYAdjust = -15; + } + _uiLayer->addChild(_radioButtonGroups[type]); + + // Set allowing no selections + _radioButtonGroups[type]->setAllowedNoSelection(type == 0); - // Create the radio buttons - for(int i = 0; i < NUMBER_OF_BUTTONS; ++i) - { - RadioButton* radioButton = nullptr; - float posY = widgetSize.height / 2.0f + posYAdjust; - radioButton = RadioButton::create(normalImage, selectedImage); - float posX = startPosX + SPACE_BETWEEN_BUTTONS * i; - radioButton->setScale(1.5f); - radioButton->setPosition(Vec2(posX, posY)); - - radioButton->addEventListener(CC_CALLBACK_2(UIRadioButtonTwoGroupsTest::onChangedRadioButtonSelect, this)); - radioButton->setTag(i); - _uiLayer->addChild(radioButton); - _radioButtonGroups[type]->addRadioButton(radioButton); - } - } - - Button* clearButton = Button::create("cocosui/backtotopnormal.png", "cocosui/backtotoppressed.png"); - clearButton->setTitleText("Clear"); - clearButton->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - 65)); - clearButton->addClickEventListener(CC_CALLBACK_1(UIRadioButtonTwoGroupsTest::clearRadioButtonGroup, this)); - clearButton->setScale(0.8f); - _uiLayer->addChild(clearButton); - - Text* text1 = Text::create("Upper radio button group is allowed non-selection, but lower one is not.", "fonts/Marker Felt.ttf", 15); - text1->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - 100)); - _uiLayer->addChild(text1); + // Create the radio buttons + for(int i = 0; i < NUMBER_OF_BUTTONS; ++i) + { + RadioButton* radioButton = nullptr; + float posY = widgetSize.height / 2.0f + posYAdjust; + radioButton = RadioButton::create(normalImage, selectedImage); + float posX = startPosX + SPACE_BETWEEN_BUTTONS * i; + radioButton->setScale(1.5f); + radioButton->setPosition(Vec2(posX, posY)); + + radioButton->addEventListener(CC_CALLBACK_2(UIRadioButtonTwoGroupsTest::onChangedRadioButtonSelect, this)); + radioButton->setTag(i); + _uiLayer->addChild(radioButton); + _radioButtonGroups[type]->addRadioButton(radioButton); + } + } + + Button* clearButton = Button::create("cocosui/backtotopnormal.png", "cocosui/backtotoppressed.png"); + clearButton->setTitleText("Clear"); + clearButton->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - 65)); + clearButton->addClickEventListener(CC_CALLBACK_1(UIRadioButtonTwoGroupsTest::clearRadioButtonGroup, this)); + clearButton->setScale(0.8f); + _uiLayer->addChild(clearButton); + + Text* text1 = Text::create("Upper radio button group is allowed non-selection, but lower one is not.", "fonts/Marker Felt.ttf", 15); + text1->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - 100)); + _uiLayer->addChild(text1); return true; } @@ -219,116 +219,116 @@ bool UIRadioButtonTwoGroupsTest::init() void UIRadioButtonTwoGroupsTest::onChangedRadioButtonGroup1(RadioButton* radioButton, int index, cocos2d::ui::RadioButtonGroup::EventType type) { - CCASSERT(index == _radioButtonGroups[0]->getSelectedButtonIndex(), "The two indexes must match!"); - __String* text = String::createWithFormat("RadioButtonGroup1 : %d", index); - _groupEventLabel->setString(text->getCString()); - addLog(text->getCString()); + CCASSERT(index == _radioButtonGroups[0]->getSelectedButtonIndex(), "The two indexes must match!"); + __String* text = String::createWithFormat("RadioButtonGroup1 : %d", index); + _groupEventLabel->setString(text->getCString()); + addLog(text->getCString()); } void UIRadioButtonTwoGroupsTest::onChangedRadioButtonGroup2(RadioButton* radioButton, int index, cocos2d::ui::RadioButtonGroup::EventType type) { - CCASSERT(index == _radioButtonGroups[1]->getSelectedButtonIndex(), "The two indexes must match!"); - __String* text = String::createWithFormat("RadioButtonGroup2 : %d", index); - _groupEventLabel->setString(text->getCString()); - addLog(text->getCString()); + CCASSERT(index == _radioButtonGroups[1]->getSelectedButtonIndex(), "The two indexes must match!"); + __String* text = String::createWithFormat("RadioButtonGroup2 : %d", index); + _groupEventLabel->setString(text->getCString()); + addLog(text->getCString()); } void UIRadioButtonTwoGroupsTest::onChangedRadioButtonSelect(RadioButton* radioButton, RadioButton::EventType type) { - if(radioButton == nullptr) - { - return; - } - __String* text = String::createWithFormat("RadioButton %d : ", radioButton->getTag()); + if(radioButton == nullptr) + { + return; + } + __String* text = String::createWithFormat("RadioButton %d : ", radioButton->getTag()); switch (type) { case RadioButton::EventType::SELECTED: - { - text->append("Selected"); - break; - } - + { + text->append("Selected"); + break; + } + case RadioButton::EventType::UNSELECTED: - { - text->append("Unselected"); - break; - } + { + text->append("Unselected"); + break; + } default: break; } - _buttonEventLabel->setString(text->getCString()); - addLog(text->getCString()); + _buttonEventLabel->setString(text->getCString()); + addLog(text->getCString()); } void UIRadioButtonTwoGroupsTest::clearRadioButtonGroup(Ref* sender) { - for(int i = 0; i < 2; ++i) - { - _radioButtonGroups[i]->setSelectedButton(nullptr); - } + for(int i = 0; i < 2; ++i) + { + _radioButtonGroups[i]->setSelectedButton(nullptr); + } } void UIRadioButtonTwoGroupsTest::addLog(const std::string& log) { - std::string existingLog = _logConsole->getString(); - if(!existingLog.empty()) - { - existingLog = existingLog + "\n"; - } - existingLog = existingLog + log; - ++_numberOfLogLines; - - if(_numberOfLogLines > 10) - { - size_t pos = existingLog.find("\n") + 1; - std::string newLog = existingLog.substr(pos); - existingLog = newLog; - --_numberOfLogLines; - } - - _logConsole->setString(existingLog); + std::string existingLog = _logConsole->getString(); + if(!existingLog.empty()) + { + existingLog = existingLog + "\n"; + } + existingLog = existingLog + log; + ++_numberOfLogLines; + + if(_numberOfLogLines > 10) + { + size_t pos = existingLog.find("\n") + 1; + std::string newLog = existingLog.substr(pos); + existingLog = newLog; + --_numberOfLogLines; + } + + _logConsole->setString(existingLog); } // UIRadioButtonTabTest bool UIRadioButtonTabTest::init() { - if (UIScene::init()) - { - Size widgetSize = _widget->getContentSize();; - - static const float BUTTON_SCALE = 0.5f; - const float buttonWidth = 350 * BUTTON_SCALE / Director::getInstance()->getContentScaleFactor(); - - // Background for buttons - LayerColor* colorLayer = LayerColor::create(Color4B::WHITE); - colorLayer->ignoreAnchorPointForPosition(false); - colorLayer->setAnchorPoint(Vec2::ANCHOR_MIDDLE); - colorLayer->setContentSize(Size(buttonWidth * 3, 170 / Director::getInstance()->getContentScaleFactor())); - colorLayer->setPosition(widgetSize / 2.0f); - _uiLayer->addChild(colorLayer); - - // Create a radio button group - RadioButtonGroup* radioButtonGroup = RadioButtonGroup::create(); - _uiLayer->addChild(radioButtonGroup); - - // Create the radio buttons - static const int NUMBER_OF_BUTTONS = 3; - float startPosX = widgetSize.width / 2.0f - ((NUMBER_OF_BUTTONS - 1) / 2.0f) * buttonWidth; - for(int i = 0; i < NUMBER_OF_BUTTONS; ++i) - { - __String* filePathNormal = String::createWithFormat("cocosui/btn_exercise%02d_n.png", i + 1); - __String* filePathSelected = String::createWithFormat("cocosui/btn_exercise%02d_p.png", i + 1); - RadioButton* radioButton = RadioButton::create(filePathNormal->getCString(), filePathSelected->getCString()); - float posX = startPosX + buttonWidth * i; - radioButton->setPosition(Vec2(posX, widgetSize.height / 2.0f)); - radioButton->setScale(BUTTON_SCALE); - radioButton->setZoomScale(0.05f); - radioButtonGroup->addRadioButton(radioButton); - _uiLayer->addChild(radioButton); - } - radioButtonGroup->setSelectedButton(0); - return true; - } - return false; + if (UIScene::init()) + { + Size widgetSize = _widget->getContentSize();; + + static const float BUTTON_SCALE = 0.5f; + const float buttonWidth = 350 * BUTTON_SCALE / Director::getInstance()->getContentScaleFactor(); + + // Background for buttons + LayerColor* colorLayer = LayerColor::create(Color4B::WHITE); + colorLayer->ignoreAnchorPointForPosition(false); + colorLayer->setAnchorPoint(Vec2::ANCHOR_MIDDLE); + colorLayer->setContentSize(Size(buttonWidth * 3, 170 / Director::getInstance()->getContentScaleFactor())); + colorLayer->setPosition(widgetSize / 2.0f); + _uiLayer->addChild(colorLayer); + + // Create a radio button group + RadioButtonGroup* radioButtonGroup = RadioButtonGroup::create(); + _uiLayer->addChild(radioButtonGroup); + + // Create the radio buttons + static const int NUMBER_OF_BUTTONS = 3; + float startPosX = widgetSize.width / 2.0f - ((NUMBER_OF_BUTTONS - 1) / 2.0f) * buttonWidth; + for(int i = 0; i < NUMBER_OF_BUTTONS; ++i) + { + __String* filePathNormal = String::createWithFormat("cocosui/btn_exercise%02d_n.png", i + 1); + __String* filePathSelected = String::createWithFormat("cocosui/btn_exercise%02d_p.png", i + 1); + RadioButton* radioButton = RadioButton::create(filePathNormal->getCString(), filePathSelected->getCString()); + float posX = startPosX + buttonWidth * i; + radioButton->setPosition(Vec2(posX, widgetSize.height / 2.0f)); + radioButton->setScale(BUTTON_SCALE); + radioButton->setZoomScale(0.05f); + radioButtonGroup->addRadioButton(radioButton); + _uiLayer->addChild(radioButton); + } + radioButtonGroup->setSelectedButton(0); + return true; + } + return false; } diff --git a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIRadioButtonTest/UIRadioButtonTest.h b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIRadioButtonTest/UIRadioButtonTest.h index b708e2516d..42bf9d6ad2 100644 --- a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIRadioButtonTest/UIRadioButtonTest.h +++ b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIRadioButtonTest/UIRadioButtonTest.h @@ -32,20 +32,20 @@ DEFINE_TEST_SUITE(UIRadioButtonTests); class UIRadioButtonTest : public UIScene { public: - CREATE_FUNC(UIRadioButtonTest); - - UIRadioButtonTest(); - ~UIRadioButtonTest(); - - virtual bool init() override; - - void addRadioButton(Ref* sender); - void deleteRadioButton(Ref* sender); + CREATE_FUNC(UIRadioButtonTest); + + UIRadioButtonTest(); + ~UIRadioButtonTest(); + + virtual bool init() override; + + void addRadioButton(Ref* sender); + void deleteRadioButton(Ref* sender); private: - cocos2d::ui::RadioButtonGroup* _radioButtonGroup; - cocos2d::ui::Text* _allowNoSelectionText; - + cocos2d::ui::RadioButtonGroup* _radioButtonGroup; + cocos2d::ui::Text* _allowNoSelectionText; + }; @@ -58,27 +58,27 @@ public: ~UIRadioButtonTwoGroupsTest(); virtual bool init() override; - void onChangedRadioButtonGroup1(cocos2d::ui::RadioButton* radioButton, int index, cocos2d::ui::RadioButtonGroup::EventType type); - void onChangedRadioButtonGroup2(cocos2d::ui::RadioButton* radioButton, int index, cocos2d::ui::RadioButtonGroup::EventType type); - void onChangedRadioButtonSelect(cocos2d::ui::RadioButton* radioButton,cocos2d::ui::RadioButton::EventType type); - void clearRadioButtonGroup(Ref* sender); + void onChangedRadioButtonGroup1(cocos2d::ui::RadioButton* radioButton, int index, cocos2d::ui::RadioButtonGroup::EventType type); + void onChangedRadioButtonGroup2(cocos2d::ui::RadioButton* radioButton, int index, cocos2d::ui::RadioButtonGroup::EventType type); + void onChangedRadioButtonSelect(cocos2d::ui::RadioButton* radioButton,cocos2d::ui::RadioButton::EventType type); + void clearRadioButtonGroup(Ref* sender); protected: - void addLog(const std::string& log); - - cocos2d::ui::RadioButtonGroup* _radioButtonGroups[2]; - cocos2d::ui::Text* _groupEventLabel; - cocos2d::ui::Text* _buttonEventLabel; - cocos2d::ui::Text* _logConsole; - int _numberOfLogLines; + void addLog(const std::string& log); + + cocos2d::ui::RadioButtonGroup* _radioButtonGroups[2]; + cocos2d::ui::Text* _groupEventLabel; + cocos2d::ui::Text* _buttonEventLabel; + cocos2d::ui::Text* _logConsole; + int _numberOfLogLines; }; class UIRadioButtonTabTest : public UIScene { public: - CREATE_FUNC(UIRadioButtonTabTest); - virtual bool init() override; + CREATE_FUNC(UIRadioButtonTabTest); + virtual bool init() override; }; #endif /* defined(__TestCpp__UIRadioButtonTest__) */