PuzzleSDK
UICheckBoxTest.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 "UICheckBoxTest.h"
26
28using namespace cocos2d::ui;
29
30UICheckBoxTests::UICheckBoxTests()
31{
35}
36
37// UICheckBoxTest
39: _displayValueLabel(nullptr)
40{
41
42}
43
45{
46}
47
49{
50 if (UIScene::init())
51 {
52 Size widgetSize = _widget->getContentSize();
53
54 // Add a label in which the checkbox events will be displayed
55 _displayValueLabel = Text::create("No Event", "fonts/Marker Felt.ttf", 32);
56 _displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
57 _displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
59
60 // Add the alert
61 Text* alert = Text::create("CheckBox","fonts/Marker Felt.ttf",30 );
62 alert->setColor(Color3B(159, 168, 176));
63 alert->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 1.75f));
64 _uiLayer->addChild(alert);
65
66 // Create the checkbox
67 _checkBox = CheckBox::create("cocosui/check_box_normal.png",
68 "cocosui/check_box_normal_press.png",
69 "cocosui/check_box_active.png",
70 "cocosui/check_box_normal_disable.png",
71 "cocosui/check_box_active_disable.png");
72 _checkBox->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
73
74 _checkBox->addEventListener(CC_CALLBACK_2(UICheckBoxTest::selectedEvent, this));
75 _uiLayer->addChild(_checkBox);
76
77 TTFConfig ttfConfig("fonts/arial.ttf", 15);
78 auto label1 = Label::createWithTTF(ttfConfig, "Print Resources");
79 auto item1 = MenuItemLabel::create(label1, CC_CALLBACK_1(UICheckBoxTest::printWidgetResources, this));
80 item1->setPosition(Vec2(VisibleRect::left().x + 60, VisibleRect::bottom().y + item1->getContentSize().height * 3));
81 auto pMenu1 = Menu::create(item1, nullptr);
82 pMenu1->setPosition(Vec2(0.0f, 0.0f));
83 this->addChild(pMenu1, 10);
84
85 return true;
86 }
87 return false;
88}
89
90void UICheckBoxTest::selectedEvent(Ref* pSender,CheckBox::EventType type)
91{
92 switch (type)
93 {
94 case CheckBox::EventType::SELECTED:
95 _displayValueLabel->setString(StringUtils::format("Selected"));
96 break;
97
98 case CheckBox::EventType::UNSELECTED:
99 _displayValueLabel->setString(StringUtils::format("Unselected"));
100 break;
101
102 default:
103 break;
104 }
105
106}
107
108void UICheckBoxTest::printWidgetResources(cocos2d::Ref* sender)
109{
110 cocos2d::ResourceData backGroundFileName = _checkBox->getBackNormalFile();
111 CCLOG("backGroundFile Name : %s, Type: %d", backGroundFileName.file.c_str(),backGroundFileName.type);
112 cocos2d::ResourceData backGroundSelectedFileName = _checkBox->getBackPressedFile();
113 CCLOG("backGroundSelectedFile Name : %s, Type: %d", backGroundSelectedFileName.file.c_str(), backGroundSelectedFileName.type);
114 cocos2d::ResourceData backGroundDisabledFileName = _checkBox->getBackDisabledFile();
115 CCLOG("backGroundDisabledFile Name : %s, Type: %d", backGroundDisabledFileName.file.c_str(), backGroundDisabledFileName.type);
116 cocos2d::ResourceData frontCrossFileName = _checkBox->getCrossNormalFile();
117 CCLOG("frontCrossFile Name : %s, Type: %d", frontCrossFileName.file.c_str(), frontCrossFileName.type);
118 cocos2d::ResourceData frontCrossDisabledFileName = _checkBox->getCrossDisabledFile();
119 CCLOG("frontCrossDisabledFile Name : %s, Type: %d", frontCrossDisabledFileName.file.c_str(), frontCrossDisabledFileName.type);
120}
121
122
123// UICheckBoxDefaultBehaviorTest
125: _displayValueLabel(nullptr)
126{
127
128}
129
131{
132}
133
135{
136 if (UIScene::init())
137 {
138 Size widgetSize = _widget->getContentSize();
139
140 // Add a label in which the checkbox events will be displayed
141 _displayValueLabel = Text::create("No Event", "fonts/Marker Felt.ttf", 32);
142 _displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
143 _displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
144 _uiLayer->addChild(_displayValueLabel);
145
146 // Add the alert
147 Text* alert = Text::create("Only left two and the last checkbox can be clicked!","fonts/Marker Felt.ttf",20 );
148 alert->setColor(Color3B(159, 168, 176));
149 alert->setPosition(Vec2(widgetSize.width / 2.0f,
150 widgetSize.height / 2.0f - alert->getContentSize().height * 1.75f));
151 _uiLayer->addChild(alert);
152
153 // Create the checkbox
154 CheckBox* checkBox = CheckBox::create("cocosui/check_box_normal.png",
155 "cocosui/check_box_active.png");
156 checkBox->setPosition(Vec2(widgetSize.width / 2.0f - 50, widgetSize.height / 2.0f));
157
158 _uiLayer->addChild(checkBox);
159
160
161 // Create the checkbox
162 CheckBox* checkBox2 = CheckBox::create("cocosui/check_box_normal.png",
163 "cocosui/check_box_active.png");
164 checkBox2->setPosition(Vec2(widgetSize.width / 2.0f - 150, widgetSize.height / 2.0f));
165 checkBox2->ignoreContentAdaptWithSize(false);
166 checkBox2->setZoomScale(0.5f);
167 checkBox2->setContentSize(Size(80.0f,80.0f));
168 checkBox2->setName("bigCheckBox");
169 _uiLayer->addChild(checkBox2);
170
171
172 // Create the checkbox
173 CheckBox* checkBoxDisabled = CheckBox::create("cocosui/check_box_normal.png",
174 "cocosui/check_box_active.png");
175 checkBoxDisabled->setPosition(Vec2(widgetSize.width / 2.0f + 20, widgetSize.height / 2.0f));
176 checkBoxDisabled->setEnabled(false);
177 checkBoxDisabled->setBright(false);
178 _uiLayer->addChild(checkBoxDisabled);
179
180 CheckBox* checkBoxDisabled2 = CheckBox::create("cocosui/check_box_normal.png",
181 "cocosui/check_box_active.png");
182 checkBoxDisabled2->setPosition(Vec2(widgetSize.width / 2.0f + 70, widgetSize.height / 2.0f));
183 checkBoxDisabled2->setBright(false);
184 checkBoxDisabled2->setSelected(true);
185 _uiLayer->addChild(checkBoxDisabled2);
186 return true;
187 }
188 return false;
189}
190
191
192// UICheckBoxCopyTest
194: _displayValueLabel(nullptr)
195{
196
197}
198
200{
201}
202
204{
205 if (UIScene::init())
206 {
207 Size widgetSize = _widget->getContentSize();
208
209 // Add a label in which the checkbox events will be displayed
210 _displayValueLabel = Text::create("No Event", "fonts/Marker Felt.ttf", 32);
211 _displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
212 _displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
213 _uiLayer->addChild(_displayValueLabel);
214
215 // Add the alert
216 Text* alert = Text::create("Two checkbox are identical.","fonts/Marker Felt.ttf",20 );
217 alert->setColor(Color3B(159, 168, 176));
218 alert->setPosition(Vec2(widgetSize.width / 2.0f,
219 widgetSize.height / 2.0f - alert->getContentSize().height * 1.75f));
220 _uiLayer->addChild(alert);
221
222 // Create the checkbox
223 CheckBox* checkBox = CheckBox::create("cocosui/check_box_normal.png",
224 "cocosui/check_box_active.png");
225 checkBox->setPosition(Vec2(widgetSize.width / 2.0f - 50, widgetSize.height / 2.0f));
226
227 _uiLayer->addChild(checkBox);
228
229 auto checkboxCopy = checkBox->clone();
230 checkboxCopy->setPosition(checkBox->getPosition() + Vec2(50.0f,0.0f));
231 _uiLayer->addChild(checkboxCopy);
232
233
234 return true;
235 }
236 return false;
237}
#define ADD_TEST_CASE(__className__)
Definition: BaseTest.h:211
USING_NS_CC
cocos2d::ui::Text * _displayValueLabel
virtual bool init() override
virtual bool init() override
cocos2d::ui::Text * _displayValueLabel
void selectedEvent(cocos2d::Ref *sender, cocos2d::ui::CheckBox::EventType type)
virtual bool init() override
cocos2d::ui::CheckBox * _checkBox
cocos2d::ui::Text * _displayValueLabel
void printWidgetResources(cocos2d::Ref *sender)
cocos2d::Layer * _uiLayer
Definition: UIScene.h:44
virtual bool init() override
Definition: UIScene.cpp:46
cocos2d::ui::Layout * _widget
Definition: UIScene.h:45
static cocos2d::Vec2 bottom()
Definition: VisibleRect.cpp:63
static cocos2d::Vec2 left()
Definition: VisibleRect.cpp:45