2018-01-29 16:25:32 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2022-07-09 22:23:34 +08:00
|
|
|
https://axis-project.github.io/
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2018-01-29 16:25:32 +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
|
|
|
|
2018-01-29 16:25:32 +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
|
|
|
|
2018-01-29 16:25:32 +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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-06-25 17:48:15 +08:00
|
|
|
#include "UIRadioButtonTest.h"
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
USING_NS_AX;
|
|
|
|
using namespace axis::ui;
|
2015-06-25 17:48:15 +08:00
|
|
|
|
|
|
|
UIRadioButtonTests::UIRadioButtonTests()
|
|
|
|
{
|
2015-07-02 10:30:14 +08:00
|
|
|
ADD_TEST_CASE(UIRadioButtonTest);
|
|
|
|
ADD_TEST_CASE(UIRadioButtonTwoGroupsTest);
|
|
|
|
ADD_TEST_CASE(UIRadioButtonTabTest);
|
2015-06-25 17:48:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// UIRadioButtonTest
|
2021-12-28 16:06:23 +08:00
|
|
|
UIRadioButtonTest::UIRadioButtonTest() : _radioButtonGroup(nullptr), _allowNoSelectionText(nullptr) {}
|
|
|
|
UIRadioButtonTest::~UIRadioButtonTest() {}
|
2015-07-02 10:28:14 +08:00
|
|
|
|
|
|
|
static const float BUTTON_WIDTH = 30;
|
2021-12-28 16:06:23 +08:00
|
|
|
static float startPosX = 0;
|
2015-07-02 10:28:14 +08:00
|
|
|
|
|
|
|
bool UIRadioButtonTest::init()
|
|
|
|
{
|
2015-07-02 10:30:14 +08:00
|
|
|
if (UIScene::init())
|
|
|
|
{
|
|
|
|
Size widgetSize = _widget->getContentSize();
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2015-07-02 10:30:14 +08:00
|
|
|
// Create a radio button group
|
|
|
|
_radioButtonGroup = RadioButtonGroup::create();
|
|
|
|
_uiLayer->addChild(_radioButtonGroup);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2015-07-02 10:30:14 +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)
|
2015-07-02 10:30:14 +08:00
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
RadioButton* radioButton =
|
|
|
|
RadioButton::create("cocosui/radio_button_off.png", "cocosui/radio_button_on.png");
|
2015-07-02 10:30:14 +08:00
|
|
|
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
|
|
|
|
2015-07-02 10:30:14 +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));
|
2015-07-02 10:30:14 +08:00
|
|
|
addButton->setScale(0.7f);
|
|
|
|
_uiLayer->addChild(addButton);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2015-07-02 10:30:14 +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));
|
2015-07-02 10:30:14 +08:00
|
|
|
deleteButton->setScale(0.7f);
|
|
|
|
_uiLayer->addChild(deleteButton);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2015-07-02 10:30:14 +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.");
|
2015-07-02 10:30:14 +08:00
|
|
|
});
|
|
|
|
allowNoSelectionToggle->setScale(0.7f);
|
|
|
|
_uiLayer->addChild(allowNoSelectionToggle);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2015-07-02 10:30:14 +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);
|
2015-07-02 10:28:14 +08:00
|
|
|
|
2015-07-02 10:30:14 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2015-07-02 10:28:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void UIRadioButtonTest::addRadioButton(Ref* sender)
|
|
|
|
{
|
2015-07-02 10:30:14 +08:00
|
|
|
Vec2 pos;
|
2021-12-28 16:06:23 +08:00
|
|
|
if (_radioButtonGroup->getNumberOfRadioButtons() > 0)
|
2015-07-02 10:30:14 +08:00
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
RadioButton* lastRadioButton =
|
|
|
|
_radioButtonGroup->getRadioButtonByIndex((int)_radioButtonGroup->getNumberOfRadioButtons() - 1);
|
2015-07-02 10:30:14 +08:00
|
|
|
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
|
|
|
|
2015-07-02 10:30:14 +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;
|
2015-07-02 10:30:14 +08:00
|
|
|
radioButton->setPosition(pos);
|
|
|
|
_radioButtonGroup->addRadioButton(radioButton);
|
|
|
|
_uiLayer->addChild(radioButton);
|
2015-07-02 10:28:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void UIRadioButtonTest::deleteRadioButton(Ref* sender)
|
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
if (_radioButtonGroup->getNumberOfRadioButtons() > 0)
|
2015-07-02 10:30:14 +08:00
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
RadioButton* radioButton =
|
|
|
|
_radioButtonGroup->getRadioButtonByIndex((int)_radioButtonGroup->getNumberOfRadioButtons() - 1);
|
2015-07-02 10:30:14 +08:00
|
|
|
_radioButtonGroup->removeRadioButton(radioButton);
|
2021-08-30 19:47:53 +08:00
|
|
|
_uiLayer->removeChild(radioButton, true);
|
2015-07-02 10:30:14 +08:00
|
|
|
}
|
2015-07-02 10:28:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// UIRadioButtonTwoGroupsTest
|
|
|
|
UIRadioButtonTwoGroupsTest::UIRadioButtonTwoGroupsTest()
|
2021-12-28 16:06:23 +08:00
|
|
|
: _groupEventLabel(nullptr), _buttonEventLabel(nullptr), _logConsole(nullptr), _numberOfLogLines(0)
|
2015-06-25 17:48:15 +08:00
|
|
|
{
|
2015-07-02 10:30:14 +08:00
|
|
|
_radioButtonGroups[0] = nullptr;
|
|
|
|
_radioButtonGroups[1] = nullptr;
|
2015-06-25 17:48:15 +08:00
|
|
|
}
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
UIRadioButtonTwoGroupsTest::~UIRadioButtonTwoGroupsTest() {}
|
2015-06-25 17:48:15 +08:00
|
|
|
|
2015-07-02 10:28:14 +08:00
|
|
|
bool UIRadioButtonTwoGroupsTest::init()
|
2015-06-25 17:48:15 +08:00
|
|
|
{
|
|
|
|
if (UIScene::init())
|
|
|
|
{
|
2016-09-12 09:45:34 +08:00
|
|
|
Size widgetSize = _widget->getContentSize();
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2015-06-25 17:48:15 +08:00
|
|
|
// Add a label in which the radio button events will be displayed
|
2015-07-02 10:30:14 +08:00
|
|
|
_groupEventLabel = Text::create("RadioButtonGroup : No Event", "fonts/Marker Felt.ttf", 25);
|
2019-09-24 11:31:35 +08:00
|
|
|
_groupEventLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
|
2015-07-02 10:30:14 +08:00
|
|
|
_groupEventLabel->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f + 70));
|
|
|
|
_uiLayer->addChild(_groupEventLabel);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2015-06-25 17:48:15 +08:00
|
|
|
_buttonEventLabel = Text::create("RadioButton : No Event", "fonts/Marker Felt.ttf", 25);
|
2019-09-24 11:31:35 +08:00
|
|
|
_buttonEventLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
|
2015-06-25 17:48:15 +08:00
|
|
|
_buttonEventLabel->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f + 35));
|
|
|
|
_uiLayer->addChild(_buttonEventLabel);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2015-07-02 10:30:14 +08:00
|
|
|
_logConsole = Text::create("", "fonts/Marker Felt.ttf", 10);
|
2019-09-24 11:31:35 +08:00
|
|
|
_logConsole->setAnchorPoint(Vec2(0.0f, 1.0f));
|
2015-07-02 10:30:14 +08:00
|
|
|
_logConsole->setPosition(Vec2(widgetSize.width / 2.0f + 110, widgetSize.height / 2.0f + 55));
|
|
|
|
_uiLayer->addChild(_logConsole);
|
2015-06-25 17:48:15 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
static const int NUMBER_OF_BUTTONS = 4;
|
2015-07-02 10:30:14 +08:00
|
|
|
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)
|
2015-07-02 10:30:14 +08:00
|
|
|
{
|
|
|
|
// Create a radio button group
|
|
|
|
const char *normalImage, *selectedImage;
|
2021-12-28 16:06:23 +08:00
|
|
|
float posYAdjust = 0;
|
2015-07-02 10:30:14 +08:00
|
|
|
_radioButtonGroups[type] = RadioButtonGroup::create();
|
2021-12-28 16:06:23 +08:00
|
|
|
if (type == 0)
|
2015-07-02 10:30:14 +08:00
|
|
|
{
|
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";
|
2015-07-02 10:30:14 +08:00
|
|
|
selectedImage = "cocosui/radio_button_on.png";
|
2021-12-28 16:06:23 +08:00
|
|
|
posYAdjust = 35;
|
2015-07-02 10:30:14 +08:00
|
|
|
}
|
|
|
|
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";
|
2015-07-02 10:30:14 +08:00
|
|
|
selectedImage = "cocosui/UIEditorTest/2.1/Button/button_common_box03_001.png";
|
2021-12-28 16:06:23 +08:00
|
|
|
posYAdjust = -15;
|
2015-07-02 10:30:14 +08:00
|
|
|
}
|
|
|
|
_uiLayer->addChild(_radioButtonGroups[type]);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2015-07-02 10:30:14 +08:00
|
|
|
// Set allowing no selections
|
|
|
|
_radioButtonGroups[type]->setAllowedNoSelection(type == 0);
|
2015-06-25 17:48:15 +08:00
|
|
|
|
2015-07-02 10:30:14 +08:00
|
|
|
// Create the radio buttons
|
2021-12-28 16:06:23 +08:00
|
|
|
for (int i = 0; i < NUMBER_OF_BUTTONS; ++i)
|
2015-07-02 10:30:14 +08:00
|
|
|
{
|
|
|
|
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;
|
2015-07-02 10:30:14 +08:00
|
|
|
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));
|
2015-07-02 10:30:14 +08:00
|
|
|
radioButton->setTag(i);
|
|
|
|
_uiLayer->addChild(radioButton);
|
|
|
|
_radioButtonGroups[type]->addRadioButton(radioButton);
|
|
|
|
}
|
|
|
|
}
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2015-07-02 10:30:14 +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));
|
2015-07-02 10:30:14 +08:00
|
|
|
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);
|
2015-07-02 10:30:14 +08:00
|
|
|
text1->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - 100));
|
|
|
|
_uiLayer->addChild(text1);
|
2015-06-25 17:48:15 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
void UIRadioButtonTwoGroupsTest::onChangedRadioButtonGroup1(RadioButton* radioButton,
|
|
|
|
int index,
|
2022-07-11 17:50:21 +08:00
|
|
|
axis::ui::RadioButtonGroup::EventType type)
|
2015-06-25 17:48:15 +08:00
|
|
|
{
|
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);
|
2015-06-25 17:48:15 +08:00
|
|
|
}
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
void UIRadioButtonTwoGroupsTest::onChangedRadioButtonGroup2(RadioButton* radioButton,
|
|
|
|
int index,
|
2022-07-11 17:50:21 +08:00
|
|
|
axis::ui::RadioButtonGroup::EventType type)
|
2015-06-25 17:48:15 +08:00
|
|
|
{
|
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);
|
2015-06-25 17:48:15 +08:00
|
|
|
}
|
|
|
|
|
2015-07-02 10:28:14 +08:00
|
|
|
void UIRadioButtonTwoGroupsTest::onChangedRadioButtonSelect(RadioButton* radioButton, RadioButton::EventType type)
|
2015-06-25 17:48:15 +08:00
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
if (radioButton == nullptr)
|
2015-07-02 10:30:14 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2015-07-15 12:04:48 +08:00
|
|
|
auto text = StringUtils::format("RadioButton %d : ", radioButton->getTag());
|
2015-06-25 17:48:15 +08:00
|
|
|
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-06-25 17:48:15 +08:00
|
|
|
}
|
2015-07-15 12:04:48 +08:00
|
|
|
_buttonEventLabel->setString(text);
|
|
|
|
addLog(text);
|
2015-06-25 17:48:15 +08:00
|
|
|
}
|
|
|
|
|
2015-07-02 10:28:14 +08:00
|
|
|
void UIRadioButtonTwoGroupsTest::clearRadioButtonGroup(Ref* sender)
|
2015-06-25 17:48:15 +08:00
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
for (int i = 0; i < 2; ++i)
|
2015-07-02 10:30:14 +08:00
|
|
|
{
|
|
|
|
_radioButtonGroups[i]->setSelectedButton(nullptr);
|
|
|
|
}
|
2015-06-25 17:48:15 +08:00
|
|
|
}
|
|
|
|
|
2021-12-28 15:58:56 +08:00
|
|
|
void UIRadioButtonTwoGroupsTest::addLog(std::string_view log)
|
2015-06-25 17:48:15 +08:00
|
|
|
{
|
2021-12-28 17:20:17 +08:00
|
|
|
std::string existingLog{_logConsole->getString()};
|
2021-12-28 16:06:23 +08:00
|
|
|
if (!existingLog.empty())
|
2015-07-02 10:30:14 +08:00
|
|
|
{
|
|
|
|
existingLog = existingLog + "\n";
|
|
|
|
}
|
2021-12-28 17:20:17 +08:00
|
|
|
existingLog += log;
|
2015-07-02 10:30:14 +08:00
|
|
|
++_numberOfLogLines;
|
2021-12-28 16:06:23 +08:00
|
|
|
|
|
|
|
if (_numberOfLogLines > 10)
|
2015-07-02 10:30:14 +08:00
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
size_t pos = existingLog.find('\n') + 1;
|
2015-07-02 10:30:14 +08:00
|
|
|
std::string newLog = existingLog.substr(pos);
|
2021-12-28 16:06:23 +08:00
|
|
|
existingLog = newLog;
|
2015-07-02 10:30:14 +08:00
|
|
|
--_numberOfLogLines;
|
|
|
|
}
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2015-07-02 10:30:14 +08:00
|
|
|
_logConsole->setString(existingLog);
|
2015-06-25 17:48:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// UIRadioButtonTabTest
|
|
|
|
bool UIRadioButtonTabTest::init()
|
|
|
|
{
|
2015-07-02 10:30:14 +08:00
|
|
|
if (UIScene::init())
|
|
|
|
{
|
2016-09-12 09:45:34 +08:00
|
|
|
Size widgetSize = _widget->getContentSize();
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2015-07-02 10:30:14 +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();
|
|
|
|
|
2015-07-02 10:30:14 +08:00
|
|
|
// Background for buttons
|
|
|
|
LayerColor* colorLayer = LayerColor::create(Color4B::WHITE);
|
2016-04-25 19:43:42 +08:00
|
|
|
colorLayer->setIgnoreAnchorPointForPosition(false);
|
2015-07-02 10:30:14 +08:00
|
|
|
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
|
|
|
|
2015-07-02 10:30:14 +08:00
|
|
|
// Create a radio button group
|
|
|
|
RadioButtonGroup* radioButtonGroup = RadioButtonGroup::create();
|
|
|
|
_uiLayer->addChild(radioButtonGroup);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2015-07-02 10:30:14 +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)
|
2015-07-02 10:30:14 +08:00
|
|
|
{
|
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;
|
2015-07-02 10:30:14 +08:00
|
|
|
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;
|
2015-06-25 17:48:15 +08:00
|
|
|
}
|