28using namespace cocos2d::ui;
30UIRadioButtonTests::UIRadioButtonTests()
40_radioButtonGroup(nullptr),
41_allowNoSelectionText(nullptr)
55 Size widgetSize =
_widget->getContentSize();
62 static const int NUMBER_OF_BUTTONS = 5;
64 for(
int i = 0; i < NUMBER_OF_BUTTONS; ++i)
66 RadioButton* radioButton = RadioButton::create(
"cocosui/radio_button_off.png",
"cocosui/radio_button_on.png");
68 radioButton->setPosition(Vec2(posX, widgetSize.height / 2.0f + 10));
69 radioButton->setScale(1.2f);
75 Button* addButton = Button::create(
"cocosui/backtotopnormal.png",
"cocosui/backtotoppressed.png");
76 addButton->setTitleText(
"Add");
77 addButton->setPosition(Vec2(widgetSize.width / 2.0f - 100, widgetSize.height / 2.0f - 65));
79 addButton->setScale(0.7f);
83 Button* deleteButton = Button::create(
"cocosui/backtotopnormal.png",
"cocosui/backtotoppressed.png");
84 deleteButton->setTitleText(
"Delete");
85 deleteButton->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - 65));
87 deleteButton->setScale(0.7f);
91 Button* allowNoSelectionToggle = Button::create(
"cocosui/backtotopnormal.png",
"cocosui/backtotoppressed.png");
92 allowNoSelectionToggle->setTitleText(
"Toggle Allow-No-Selection");
93 allowNoSelectionToggle->setPosition(Vec2(widgetSize.width / 2.0f + 100, widgetSize.height / 2.0f - 65));
94 allowNoSelectionToggle->addClickEventListener([
this](Ref*) {
98 allowNoSelectionToggle->setScale(0.7f);
99 _uiLayer->addChild(allowNoSelectionToggle);
117 pos = lastRadioButton->getPosition();
122 pos.y =
_widget->getContentSize().height / 2.0f + 10;
125 RadioButton* radioButton = RadioButton::create(
"cocosui/radio_button_off.png",
"cocosui/radio_button_on.png");
127 radioButton->setPosition(pos);
147: _groupEventLabel(nullptr),
148_buttonEventLabel(nullptr),
164 Size widgetSize =
_widget->getContentSize();
167 _groupEventLabel = Text::create(
"RadioButtonGroup : No Event",
"fonts/Marker Felt.ttf", 25);
169 _groupEventLabel->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f + 70));
172 _buttonEventLabel = Text::create(
"RadioButton : No Event",
"fonts/Marker Felt.ttf", 25);
174 _buttonEventLabel->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f + 35));
177 _logConsole = Text::create(
"",
"fonts/Marker Felt.ttf", 10);
179 _logConsole->setPosition(Vec2(widgetSize.width / 2.0f + 110, widgetSize.height / 2.0f + 55));
182 static const int NUMBER_OF_BUTTONS = 4;
183 static const float SPACE_BETWEEN_BUTTONS = 50;
184 float startPosX = widgetSize.width / 2.0f - ((NUMBER_OF_BUTTONS - 1) / 2.0f) * SPACE_BETWEEN_BUTTONS;
186 for(
int type = 0; type < 2; ++type)
189 const char *normalImage, *selectedImage;
190 float posYAdjust = 0;
195 normalImage =
"cocosui/radio_button_off.png";
196 selectedImage =
"cocosui/radio_button_on.png";
202 normalImage =
"cocosui/UIEditorTest/2.1/Button/button_common_box03_003 copy 221.png";
203 selectedImage =
"cocosui/UIEditorTest/2.1/Button/button_common_box03_001.png";
212 for(
int i = 0; i < NUMBER_OF_BUTTONS; ++i)
214 RadioButton* radioButton =
nullptr;
215 float posY = widgetSize.height / 2.0f + posYAdjust;
216 radioButton = RadioButton::create(normalImage, selectedImage);
217 float posX =
startPosX + SPACE_BETWEEN_BUTTONS * i;
218 radioButton->setScale(1.5f);
219 radioButton->setPosition(Vec2(posX, posY));
222 radioButton->setTag(i);
228 Button* clearButton = Button::create(
"cocosui/backtotopnormal.png",
"cocosui/backtotoppressed.png");
229 clearButton->setTitleText(
"Clear");
230 clearButton->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - 65));
232 clearButton->setScale(0.8f);
235 Text* text1 = Text::create(
"Upper radio button group is allowed non-selection, but lower one is not.",
"fonts/Marker Felt.ttf", 15);
236 text1->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - 100));
246 CCASSERT(index ==
_radioButtonGroups[0]->getSelectedButtonIndex(),
"The two indexes must match!");
247 auto text = StringUtils::format(
"RadioButtonGroup1 : %d", index);
254 CCASSERT(index ==
_radioButtonGroups[1]->getSelectedButtonIndex(),
"The two indexes must match!");
255 auto text = StringUtils::format(
"RadioButtonGroup2 : %d", index);
262 if(radioButton ==
nullptr)
266 auto text = StringUtils::format(
"RadioButton %d : ", radioButton->getTag());
269 case RadioButton::EventType::SELECTED:
271 text.append(
"Selected");
275 case RadioButton::EventType::UNSELECTED:
277 text.append(
"Unselected");
289 for(
int i = 0; i < 2; ++i)
297 std::string existingLog =
_logConsole->getString();
298 if(!existingLog.empty())
300 existingLog = existingLog +
"\n";
302 existingLog = existingLog + log;
307 size_t pos = existingLog.find(
'\n') + 1;
308 std::string newLog = existingLog.substr(pos);
309 existingLog = newLog;
322 Size widgetSize =
_widget->getContentSize();
324 static const float BUTTON_SCALE = 0.5f;
325 const float buttonWidth = 350 * BUTTON_SCALE / Director::getInstance()->getContentScaleFactor();
328 LayerColor* colorLayer = LayerColor::create(Color4B::WHITE);
329 colorLayer->setIgnoreAnchorPointForPosition(
false);
330 colorLayer->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
331 colorLayer->setContentSize(Size(buttonWidth * 3, 170 / Director::getInstance()->getContentScaleFactor()));
332 colorLayer->setPosition(widgetSize / 2.0f);
336 RadioButtonGroup* radioButtonGroup = RadioButtonGroup::create();
337 _uiLayer->addChild(radioButtonGroup);
340 static const int NUMBER_OF_BUTTONS = 3;
341 float startPosX = widgetSize.width / 2.0f - ((NUMBER_OF_BUTTONS - 1) / 2.0f) * buttonWidth;
342 for(
int i = 0; i < NUMBER_OF_BUTTONS; ++i)
344 auto filePathNormal = StringUtils::format(
"cocosui/btn_exercise%02d_n.png", i + 1);
345 auto filePathSelected = StringUtils::format(
"cocosui/btn_exercise%02d_p.png", i + 1);
346 RadioButton* radioButton = RadioButton::create(filePathNormal, filePathSelected);
347 float posX =
startPosX + buttonWidth * i;
348 radioButton->setPosition(Vec2(posX, widgetSize.height / 2.0f));
349 radioButton->setScale(BUTTON_SCALE);
350 radioButton->setZoomScale(0.05f);
351 radioButtonGroup->addRadioButton(radioButton);
354 radioButtonGroup->setSelectedButton(0);
#define ADD_TEST_CASE(__className__)
cocos2d::Layer * _uiLayer
virtual bool init() override
cocos2d::ui::Layout * _widget