PuzzleSDK
UIRadioButtonTest.cpp
浏览该文件的文档.
1/****************************************************************************
2 Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
3
4 http://www.cocos2d-x.org
5
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 THE SOFTWARE.
23 ****************************************************************************/
24
25#include "UIRadioButtonTest.h"
26
28using namespace cocos2d::ui;
29
30UIRadioButtonTests::UIRadioButtonTests()
31{
35}
36
37
38// UIRadioButtonTest
40_radioButtonGroup(nullptr),
41_allowNoSelectionText(nullptr)
42{
43}
45{
46}
47
48static const float BUTTON_WIDTH = 30;
49static float startPosX = 0;
50
52{
53 if (UIScene::init())
54 {
55 Size widgetSize = _widget->getContentSize();
56
57 // Create a radio button group
58 _radioButtonGroup = RadioButtonGroup::create();
59 _uiLayer->addChild(_radioButtonGroup);
60
61 // Create the radio buttons
62 static const int NUMBER_OF_BUTTONS = 5;
63 startPosX = widgetSize.width / 2.0f - ((NUMBER_OF_BUTTONS - 1) / 2.0f) * BUTTON_WIDTH;
64 for(int i = 0; i < NUMBER_OF_BUTTONS; ++i)
65 {
66 RadioButton* radioButton = RadioButton::create("cocosui/radio_button_off.png", "cocosui/radio_button_on.png");
67 float posX = startPosX + BUTTON_WIDTH * i;
68 radioButton->setPosition(Vec2(posX, widgetSize.height / 2.0f + 10));
69 radioButton->setScale(1.2f);
70 _radioButtonGroup->addRadioButton(radioButton);
71 _uiLayer->addChild(radioButton);
72 }
73
74 // Add button
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));
78 addButton->addClickEventListener(CC_CALLBACK_1(UIRadioButtonTest::addRadioButton, this));
79 addButton->setScale(0.7f);
80 _uiLayer->addChild(addButton);
81
82 // Delete button
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));
86 deleteButton->addClickEventListener(CC_CALLBACK_1(UIRadioButtonTest::deleteRadioButton, this));
87 deleteButton->setScale(0.7f);
88 _uiLayer->addChild(deleteButton);
89
90 // Toggle button
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*) {
95 _radioButtonGroup->setAllowedNoSelection(!_radioButtonGroup->isAllowedNoSelection());
96 _allowNoSelectionText->setString(_radioButtonGroup->isAllowedNoSelection() ? "No selection is allowed." : "No selection is disallowed.");
97 });
98 allowNoSelectionToggle->setScale(0.7f);
99 _uiLayer->addChild(allowNoSelectionToggle);
100
101 // Add a label for toggle
102 _allowNoSelectionText = Text::create("No selection is disallowed.", "fonts/Marker Felt.ttf", 20);
103 _allowNoSelectionText->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f + 70));
105
106 return true;
107 }
108 return false;
109}
110
112{
113 Vec2 pos;
114 if(_radioButtonGroup->getNumberOfRadioButtons() > 0)
115 {
116 RadioButton* lastRadioButton = _radioButtonGroup->getRadioButtonByIndex((int)_radioButtonGroup->getNumberOfRadioButtons() - 1);
117 pos = lastRadioButton->getPosition();
118 }
119 else
120 {
121 pos.x = startPosX - BUTTON_WIDTH;
122 pos.y = _widget->getContentSize().height / 2.0f + 10;
123 }
124
125 RadioButton* radioButton = RadioButton::create("cocosui/radio_button_off.png", "cocosui/radio_button_on.png");
126 pos.x += BUTTON_WIDTH;
127 radioButton->setPosition(pos);
128 _radioButtonGroup->addRadioButton(radioButton);
129 _uiLayer->addChild(radioButton);
130}
131
133{
134 if(_radioButtonGroup->getNumberOfRadioButtons() > 0)
135 {
136 RadioButton* radioButton = _radioButtonGroup->getRadioButtonByIndex((int)_radioButtonGroup->getNumberOfRadioButtons() - 1);
137 _radioButtonGroup->removeRadioButton(radioButton);
138 _uiLayer->removeChild(radioButton);
139 }
140}
141
142
143
144
145// UIRadioButtonTwoGroupsTest
147: _groupEventLabel(nullptr),
148_buttonEventLabel(nullptr),
149_logConsole(nullptr),
150_numberOfLogLines(0)
151{
152 _radioButtonGroups[0] = nullptr;
153 _radioButtonGroups[1] = nullptr;
154}
155
157{
158}
159
161{
162 if (UIScene::init())
163 {
164 Size widgetSize = _widget->getContentSize();
165
166 // Add a label in which the radio button events will be displayed
167 _groupEventLabel = Text::create("RadioButtonGroup : No Event", "fonts/Marker Felt.ttf", 25);
168 _groupEventLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
169 _groupEventLabel->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f + 70));
170 _uiLayer->addChild(_groupEventLabel);
171
172 _buttonEventLabel = Text::create("RadioButton : No Event", "fonts/Marker Felt.ttf", 25);
173 _buttonEventLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
174 _buttonEventLabel->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f + 35));
175 _uiLayer->addChild(_buttonEventLabel);
176
177 _logConsole = Text::create("", "fonts/Marker Felt.ttf", 10);
178 _logConsole->setAnchorPoint(Vec2(0.0f, 1.0f));
179 _logConsole->setPosition(Vec2(widgetSize.width / 2.0f + 110, widgetSize.height / 2.0f + 55));
180 _uiLayer->addChild(_logConsole);
181
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;
185
186 for(int type = 0; type < 2; ++type)
187 {
188 // Create a radio button group
189 const char *normalImage, *selectedImage;
190 float posYAdjust = 0;
191 _radioButtonGroups[type] = RadioButtonGroup::create();
192 if(type == 0)
193 {
194 _radioButtonGroups[type]->addEventListener(CC_CALLBACK_3(UIRadioButtonTwoGroupsTest::onChangedRadioButtonGroup1, this));
195 normalImage = "cocosui/radio_button_off.png";
196 selectedImage = "cocosui/radio_button_on.png";
197 posYAdjust = 35;
198 }
199 else
200 {
201 _radioButtonGroups[type]->addEventListener(CC_CALLBACK_3(UIRadioButtonTwoGroupsTest::onChangedRadioButtonGroup2, this));
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";
204 posYAdjust = -15;
205 }
206 _uiLayer->addChild(_radioButtonGroups[type]);
207
208 // Set allowing no selections
209 _radioButtonGroups[type]->setAllowedNoSelection(type == 0);
210
211 // Create the radio buttons
212 for(int i = 0; i < NUMBER_OF_BUTTONS; ++i)
213 {
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));
220
221 radioButton->addEventListener(CC_CALLBACK_2(UIRadioButtonTwoGroupsTest::onChangedRadioButtonSelect, this));
222 radioButton->setTag(i);
223 _uiLayer->addChild(radioButton);
224 _radioButtonGroups[type]->addRadioButton(radioButton);
225 }
226 }
227
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));
231 clearButton->addClickEventListener(CC_CALLBACK_1(UIRadioButtonTwoGroupsTest::clearRadioButtonGroup, this));
232 clearButton->setScale(0.8f);
233 _uiLayer->addChild(clearButton);
234
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));
237 _uiLayer->addChild(text1);
238
239 return true;
240 }
241 return false;
242}
243
244void UIRadioButtonTwoGroupsTest::onChangedRadioButtonGroup1(RadioButton* radioButton, int index, cocos2d::ui::RadioButtonGroup::EventType type)
245{
246 CCASSERT(index == _radioButtonGroups[0]->getSelectedButtonIndex(), "The two indexes must match!");
247 auto text = StringUtils::format("RadioButtonGroup1 : %d", index);
248 _groupEventLabel->setString(text);
249 addLog(text);
250}
251
252void UIRadioButtonTwoGroupsTest::onChangedRadioButtonGroup2(RadioButton* radioButton, int index, cocos2d::ui::RadioButtonGroup::EventType type)
253{
254 CCASSERT(index == _radioButtonGroups[1]->getSelectedButtonIndex(), "The two indexes must match!");
255 auto text = StringUtils::format("RadioButtonGroup2 : %d", index);
256 _groupEventLabel->setString(text);
257 addLog(text);
258}
259
260void UIRadioButtonTwoGroupsTest::onChangedRadioButtonSelect(RadioButton* radioButton, RadioButton::EventType type)
261{
262 if(radioButton == nullptr)
263 {
264 return;
265 }
266 auto text = StringUtils::format("RadioButton %d : ", radioButton->getTag());
267 switch (type)
268 {
269 case RadioButton::EventType::SELECTED:
270 {
271 text.append("Selected");
272 break;
273 }
274
275 case RadioButton::EventType::UNSELECTED:
276 {
277 text.append("Unselected");
278 break;
279 }
280 default:
281 break;
282 }
283 _buttonEventLabel->setString(text);
284 addLog(text);
285}
286
288{
289 for(int i = 0; i < 2; ++i)
290 {
291 _radioButtonGroups[i]->setSelectedButton(nullptr);
292 }
293}
294
295void UIRadioButtonTwoGroupsTest::addLog(const std::string& log)
296{
297 std::string existingLog = _logConsole->getString();
298 if(!existingLog.empty())
299 {
300 existingLog = existingLog + "\n";
301 }
302 existingLog = existingLog + log;
304
305 if(_numberOfLogLines > 10)
306 {
307 size_t pos = existingLog.find('\n') + 1;
308 std::string newLog = existingLog.substr(pos);
309 existingLog = newLog;
311 }
312
313 _logConsole->setString(existingLog);
314}
315
316
317// UIRadioButtonTabTest
319{
320 if (UIScene::init())
321 {
322 Size widgetSize = _widget->getContentSize();
323
324 static const float BUTTON_SCALE = 0.5f;
325 const float buttonWidth = 350 * BUTTON_SCALE / Director::getInstance()->getContentScaleFactor();
326
327 // Background for buttons
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);
333 _uiLayer->addChild(colorLayer);
334
335 // Create a radio button group
336 RadioButtonGroup* radioButtonGroup = RadioButtonGroup::create();
337 _uiLayer->addChild(radioButtonGroup);
338
339 // Create the radio buttons
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)
343 {
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);
352 _uiLayer->addChild(radioButton);
353 }
354 radioButtonGroup->setSelectedButton(0);
355 return true;
356 }
357 return false;
358}
#define ADD_TEST_CASE(__className__)
Definition: BaseTest.h:211
static float startPosX
static const float BUTTON_WIDTH
virtual bool init() override
cocos2d::ui::RadioButtonGroup * _radioButtonGroup
virtual bool init() override
void deleteRadioButton(Ref *sender)
cocos2d::ui::Text * _allowNoSelectionText
void addRadioButton(Ref *sender)
cocos2d::ui::RadioButtonGroup * _radioButtonGroups[2]
void addLog(const std::string &log)
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)
cocos2d::ui::Text * _logConsole
virtual bool init() override
cocos2d::ui::Text * _groupEventLabel
cocos2d::ui::Text * _buttonEventLabel
void onChangedRadioButtonGroup1(cocos2d::ui::RadioButton *radioButton, int index, cocos2d::ui::RadioButtonGroup::EventType type)
cocos2d::Layer * _uiLayer
Definition: UIScene.h:44
virtual bool init() override
Definition: UIScene.cpp:46
cocos2d::ui::Layout * _widget
Definition: UIScene.h:45