axmol/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIRadioButtonTest/UIRadioButtonTest.cpp

355 lines
14 KiB
C++
Raw Normal View History

/****************************************************************************
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
2021-12-28 16:06:23 +08:00
https://axis-project.github.io/
2021-12-28 16:06:23 +08:00
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
2021-12-28 16:06:23 +08:00
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
2021-12-28 16:06:23 +08:00
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "UIRadioButtonTest.h"
USING_NS_AX;
using namespace axis::ui;
UIRadioButtonTests::UIRadioButtonTests()
{
ADD_TEST_CASE(UIRadioButtonTest);
ADD_TEST_CASE(UIRadioButtonTwoGroupsTest);
ADD_TEST_CASE(UIRadioButtonTabTest);
}
// UIRadioButtonTest
2021-12-28 16:06:23 +08:00
UIRadioButtonTest::UIRadioButtonTest() : _radioButtonGroup(nullptr), _allowNoSelectionText(nullptr) {}
UIRadioButtonTest::~UIRadioButtonTest() {}
static const float BUTTON_WIDTH = 30;
2021-12-28 16:06:23 +08:00
static float startPosX = 0;
bool UIRadioButtonTest::init()
{
if (UIScene::init())
{
Size widgetSize = _widget->getContentSize();
2021-12-28 16:06:23 +08:00
// Create a radio button group
_radioButtonGroup = RadioButtonGroup::create();
_uiLayer->addChild(_radioButtonGroup);
2021-12-28 16:06:23 +08:00
// Create the radio buttons
static const int NUMBER_OF_BUTTONS = 5;
2021-12-28 16:06:23 +08:00
startPosX = widgetSize.width / 2.0f - ((NUMBER_OF_BUTTONS - 1) / 2.0f) * BUTTON_WIDTH;
for (int i = 0; i < NUMBER_OF_BUTTONS; ++i)
{
2021-12-28 16:06:23 +08:00
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);
}
2021-12-28 16:06:23 +08:00
// 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));
2022-07-15 19:17:01 +08:00
addButton->addClickEventListener(AX_CALLBACK_1(UIRadioButtonTest::addRadioButton, this));
addButton->setScale(0.7f);
_uiLayer->addChild(addButton);
2021-12-28 16:06:23 +08:00
// 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));
2022-07-15 19:17:01 +08:00
deleteButton->addClickEventListener(AX_CALLBACK_1(UIRadioButtonTest::deleteRadioButton, this));
deleteButton->setScale(0.7f);
_uiLayer->addChild(deleteButton);
2021-12-28 16:06:23 +08:00
// 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());
2021-12-28 16:06:23 +08:00
_allowNoSelectionText->setString(_radioButtonGroup->isAllowedNoSelection() ? "No selection is allowed."
: "No selection is disallowed.");
});
allowNoSelectionToggle->setScale(0.7f);
_uiLayer->addChild(allowNoSelectionToggle);
2021-12-28 16:06:23 +08:00
// 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;
}
void UIRadioButtonTest::addRadioButton(Ref* sender)
{
Vec2 pos;
2021-12-28 16:06:23 +08:00
if (_radioButtonGroup->getNumberOfRadioButtons() > 0)
{
2021-12-28 16:06:23 +08:00
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;
}
2021-12-28 16:06:23 +08:00
RadioButton* radioButton = RadioButton::create("cocosui/radio_button_off.png", "cocosui/radio_button_on.png");
2015-08-19 16:38:50 +08:00
pos.x += BUTTON_WIDTH;
radioButton->setPosition(pos);
_radioButtonGroup->addRadioButton(radioButton);
_uiLayer->addChild(radioButton);
}
void UIRadioButtonTest::deleteRadioButton(Ref* sender)
{
2021-12-28 16:06:23 +08:00
if (_radioButtonGroup->getNumberOfRadioButtons() > 0)
{
2021-12-28 16:06:23 +08:00
RadioButton* radioButton =
_radioButtonGroup->getRadioButtonByIndex((int)_radioButtonGroup->getNumberOfRadioButtons() - 1);
_radioButtonGroup->removeRadioButton(radioButton);
_uiLayer->removeChild(radioButton, true);
}
}
// UIRadioButtonTwoGroupsTest
UIRadioButtonTwoGroupsTest::UIRadioButtonTwoGroupsTest()
2021-12-28 16:06:23 +08:00
: _groupEventLabel(nullptr), _buttonEventLabel(nullptr), _logConsole(nullptr), _numberOfLogLines(0)
{
_radioButtonGroups[0] = nullptr;
_radioButtonGroups[1] = nullptr;
}
2021-12-28 16:06:23 +08:00
UIRadioButtonTwoGroupsTest::~UIRadioButtonTwoGroupsTest() {}
bool UIRadioButtonTwoGroupsTest::init()
{
if (UIScene::init())
{
2016-09-12 09:45:34 +08:00
Size widgetSize = _widget->getContentSize();
2021-12-28 16:06:23 +08:00
// 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.0f));
_groupEventLabel->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f + 70));
_uiLayer->addChild(_groupEventLabel);
2021-12-28 16:06:23 +08:00
_buttonEventLabel = Text::create("RadioButton : No Event", "fonts/Marker Felt.ttf", 25);
_buttonEventLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
_buttonEventLabel->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f + 35));
_uiLayer->addChild(_buttonEventLabel);
2021-12-28 16:06:23 +08:00
_logConsole = Text::create("", "fonts/Marker Felt.ttf", 10);
_logConsole->setAnchorPoint(Vec2(0.0f, 1.0f));
_logConsole->setPosition(Vec2(widgetSize.width / 2.0f + 110, widgetSize.height / 2.0f + 55));
_uiLayer->addChild(_logConsole);
2021-12-28 16:06:23 +08:00
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;
2021-12-28 16:06:23 +08:00
for (int type = 0; type < 2; ++type)
{
// Create a radio button group
const char *normalImage, *selectedImage;
2021-12-28 16:06:23 +08:00
float posYAdjust = 0;
_radioButtonGroups[type] = RadioButtonGroup::create();
2021-12-28 16:06:23 +08:00
if (type == 0)
{
2021-12-28 16:06:23 +08:00
_radioButtonGroups[type]->addEventListener(
2022-07-15 19:17:01 +08:00
AX_CALLBACK_3(UIRadioButtonTwoGroupsTest::onChangedRadioButtonGroup1, this));
2021-12-28 16:06:23 +08:00
normalImage = "cocosui/radio_button_off.png";
selectedImage = "cocosui/radio_button_on.png";
2021-12-28 16:06:23 +08:00
posYAdjust = 35;
}
else
{
2021-12-28 16:06:23 +08:00
_radioButtonGroups[type]->addEventListener(
2022-07-15 19:17:01 +08:00
AX_CALLBACK_3(UIRadioButtonTwoGroupsTest::onChangedRadioButtonGroup2, this));
2021-12-28 16:06:23 +08:00
normalImage = "cocosui/UIEditorTest/2.1/Button/button_common_box03_003 copy 221.png";
selectedImage = "cocosui/UIEditorTest/2.1/Button/button_common_box03_001.png";
2021-12-28 16:06:23 +08:00
posYAdjust = -15;
}
_uiLayer->addChild(_radioButtonGroups[type]);
2021-12-28 16:06:23 +08:00
// Set allowing no selections
_radioButtonGroups[type]->setAllowedNoSelection(type == 0);
// Create the radio buttons
2021-12-28 16:06:23 +08:00
for (int i = 0; i < NUMBER_OF_BUTTONS; ++i)
{
RadioButton* radioButton = nullptr;
2021-12-28 16:06:23 +08:00
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));
2021-12-28 16:06:23 +08:00
radioButton->addEventListener(
2022-07-15 19:17:01 +08:00
AX_CALLBACK_2(UIRadioButtonTwoGroupsTest::onChangedRadioButtonSelect, this));
radioButton->setTag(i);
_uiLayer->addChild(radioButton);
_radioButtonGroups[type]->addRadioButton(radioButton);
}
}
2021-12-28 16:06:23 +08:00
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));
2022-07-15 19:17:01 +08:00
clearButton->addClickEventListener(AX_CALLBACK_1(UIRadioButtonTwoGroupsTest::clearRadioButtonGroup, this));
clearButton->setScale(0.8f);
_uiLayer->addChild(clearButton);
2021-12-28 16:06:23 +08:00
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;
}
return false;
}
2021-12-28 16:06:23 +08:00
void UIRadioButtonTwoGroupsTest::onChangedRadioButtonGroup1(RadioButton* radioButton,
int index,
axis::ui::RadioButtonGroup::EventType type)
{
2022-07-16 10:43:05 +08:00
AXASSERT(index == _radioButtonGroups[0]->getSelectedButtonIndex(), "The two indexes must match!");
2015-07-15 12:04:48 +08:00
auto text = StringUtils::format("RadioButtonGroup1 : %d", index);
_groupEventLabel->setString(text);
addLog(text);
}
2021-12-28 16:06:23 +08:00
void UIRadioButtonTwoGroupsTest::onChangedRadioButtonGroup2(RadioButton* radioButton,
int index,
axis::ui::RadioButtonGroup::EventType type)
{
2022-07-16 10:43:05 +08:00
AXASSERT(index == _radioButtonGroups[1]->getSelectedButtonIndex(), "The two indexes must match!");
2015-07-15 12:04:48 +08:00
auto text = StringUtils::format("RadioButtonGroup2 : %d", index);
_groupEventLabel->setString(text);
addLog(text);
}
void UIRadioButtonTwoGroupsTest::onChangedRadioButtonSelect(RadioButton* radioButton, RadioButton::EventType type)
{
2021-12-28 16:06:23 +08:00
if (radioButton == nullptr)
{
return;
}
2015-07-15 12:04:48 +08:00
auto text = StringUtils::format("RadioButton %d : ", radioButton->getTag());
switch (type)
{
2021-12-28 16:06:23 +08:00
case RadioButton::EventType::SELECTED:
{
text.append("Selected");
break;
}
case RadioButton::EventType::UNSELECTED:
{
text.append("Unselected");
break;
}
default:
break;
}
2015-07-15 12:04:48 +08:00
_buttonEventLabel->setString(text);
addLog(text);
}
void UIRadioButtonTwoGroupsTest::clearRadioButtonGroup(Ref* sender)
{
2021-12-28 16:06:23 +08:00
for (int i = 0; i < 2; ++i)
{
_radioButtonGroups[i]->setSelectedButton(nullptr);
}
}
2021-12-28 15:58:56 +08:00
void UIRadioButtonTwoGroupsTest::addLog(std::string_view log)
{
2021-12-28 17:20:17 +08:00
std::string existingLog{_logConsole->getString()};
2021-12-28 16:06:23 +08:00
if (!existingLog.empty())
{
existingLog = existingLog + "\n";
}
2021-12-28 17:20:17 +08:00
existingLog += log;
++_numberOfLogLines;
2021-12-28 16:06:23 +08:00
if (_numberOfLogLines > 10)
{
2021-12-28 16:06:23 +08:00
size_t pos = existingLog.find('\n') + 1;
std::string newLog = existingLog.substr(pos);
2021-12-28 16:06:23 +08:00
existingLog = newLog;
--_numberOfLogLines;
}
2021-12-28 16:06:23 +08:00
_logConsole->setString(existingLog);
}
// UIRadioButtonTabTest
bool UIRadioButtonTabTest::init()
{
if (UIScene::init())
{
2016-09-12 09:45:34 +08:00
Size widgetSize = _widget->getContentSize();
2021-12-28 16:06:23 +08:00
static const float BUTTON_SCALE = 0.5f;
2021-12-28 16:06:23 +08:00
const float buttonWidth = 350 * BUTTON_SCALE / Director::getInstance()->getContentScaleFactor();
// Background for buttons
LayerColor* colorLayer = LayerColor::create(Color4B::WHITE);
colorLayer->setIgnoreAnchorPointForPosition(false);
colorLayer->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
colorLayer->setContentSize(Size(buttonWidth * 3, 170 / Director::getInstance()->getContentScaleFactor()));
colorLayer->setPosition(widgetSize / 2.0f);
_uiLayer->addChild(colorLayer);
2021-12-28 16:06:23 +08:00
// Create a radio button group
RadioButtonGroup* radioButtonGroup = RadioButtonGroup::create();
_uiLayer->addChild(radioButtonGroup);
2021-12-28 16:06:23 +08:00
// Create the radio buttons
static const int NUMBER_OF_BUTTONS = 3;
2021-12-28 16:06:23 +08:00
float startPosX = widgetSize.width / 2.0f - ((NUMBER_OF_BUTTONS - 1) / 2.0f) * buttonWidth;
for (int i = 0; i < NUMBER_OF_BUTTONS; ++i)
{
2021-12-28 16:06:23 +08:00
auto filePathNormal = StringUtils::format("cocosui/btn_exercise%02d_n.png", i + 1);
auto filePathSelected = StringUtils::format("cocosui/btn_exercise%02d_p.png", i + 1);
2015-07-15 12:04:48 +08:00
RadioButton* radioButton = RadioButton::create(filePathNormal, filePathSelected);
2021-12-28 16:06:23 +08:00
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;
}