PuzzleSDK
UISliderTest.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 "UISliderTest.h"
26
28using namespace cocos2d::ui;
29
30UISliderTests::UISliderTests()
31{
39}
40
41// UISliderTest
42
44: _displayValueLabel(nullptr)
45{
46
47}
48
50{
51}
52
54{
55 if (UIScene::init())
56 {
57 Size widgetSize = _widget->getContentSize();
58
59 // Add a label in which the slider alert will be displayed
60 _displayValueLabel = TextBMFont::create("Move the slider thumb", "ccb/markerfelt24shadow.fnt");
61 _displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
62 _displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
64
65 // Add the alert
66 Text* alert = Text::create("Slider","fonts/Marker Felt.ttf",30);
67 alert->setColor(Color3B(159, 168, 176));
68 alert->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 1.75f));
69 _uiLayer->addChild(alert);
70
71 // Create the slider
72 Slider* slider = Slider::create();
73 slider->loadBarTexture("cocosui/sliderTrack.png");
74 slider->loadSlidBallTextures("cocosui/sliderThumb.png", "cocosui/sliderThumb.png", "");
75 slider->loadProgressBarTexture("cocosui/sliderProgress.png");
76 slider->setMaxPercent(10000);
77 slider->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f/* + slider->getSize().height * 2.0f*/));
78 slider->addEventListener(CC_CALLBACK_2(UISliderTest::sliderEvent, this));
79 _uiLayer->addChild(slider);
80
81 _slider = slider;
82
83 TTFConfig ttfConfig("fonts/arial.ttf", 15);
84 auto label1 = Label::createWithTTF(ttfConfig, "Print Resources");
85 auto item1 = MenuItemLabel::create(label1, CC_CALLBACK_1(UISliderTest::printWidgetResources, this));
86 item1->setPosition(Vec2(VisibleRect::left().x + 60, VisibleRect::bottom().y + item1->getContentSize().height * 3));
87 auto pMenu1 = Menu::create(item1, nullptr);
88 pMenu1->setPosition(Vec2(0.0f, 0.0f));
89 this->addChild(pMenu1, 10);
90
91 return true;
92 }
93 return false;
94}
95
96void UISliderTest::sliderEvent(Ref *pSender, Slider::EventType type)
97{
98 if (type == Slider::EventType::ON_PERCENTAGE_CHANGED)
99 {
100 Slider* slider = dynamic_cast<Slider*>(pSender);
101 int percent = slider->getPercent();
102 int maxPercent = slider->getMaxPercent();
103 _displayValueLabel->setString(StringUtils::format("Percent %f", 10000.0 * percent / maxPercent));
104 }
105}
106void UISliderTest::printWidgetResources(cocos2d::Ref* /*sender*/)
107{
108 cocos2d::ResourceData textureFile = _slider->getBackFile();
109 CCLOG("textureFile Name : %s, Type: %d", textureFile.file.c_str(), textureFile.type);
110 cocos2d::ResourceData progressBarTextureFile = _slider->getProgressBarFile();
111 CCLOG("progressBarTextureFile Name : %s, Type: %d", progressBarTextureFile.file.c_str(), progressBarTextureFile.type);
112 cocos2d::ResourceData slidBallNormalTextureFile = _slider->getBallNormalFile();
113 CCLOG("slidBallNormalTextureFile Name : %s, Type: %d", slidBallNormalTextureFile.file.c_str(), slidBallNormalTextureFile.type);
114 cocos2d::ResourceData slidBallPressedTextureFile = _slider->getBallPressedFile();
115 CCLOG("slidBallPressedTextureFile Name : %s, Type: %d", slidBallPressedTextureFile.file.c_str(), slidBallPressedTextureFile.type);
116 cocos2d::ResourceData slidBallDisabledTextureFile = _slider->getBallDisabledFile();
117 CCLOG("slidBallDisabledTextureFile Name : %s, Type: %d", slidBallDisabledTextureFile.file.c_str(), slidBallDisabledTextureFile.type);
118}
119
120// UISliderTest_Scale9
121
123: _displayValueLabel(nullptr)
124{
125
126}
127
129{
130}
131
133{
134 if (UIScene::init())
135 {
136 Size widgetSize = _widget->getContentSize();
137
138 // Add a label in which the slider alert will be displayed
139 _displayValueLabel = Text::create("Move the slider thumb","fonts/Marker Felt.ttf",32);
140 _displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
141 _displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
142 _uiLayer->addChild(_displayValueLabel);
143
144 // Add the alert
145 Text *alert = Text::create("Slider scale9 render","fonts/Marker Felt.ttf",30);
146 alert->setColor(Color3B(159, 168, 176));
147 alert->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 1.75f));
148 _uiLayer->addChild(alert);
149
150 // Create the slider
151 Slider* slider = Slider::create();
152 slider->loadBarTexture("cocosui/sliderTrack2.png");
153 slider->loadSlidBallTextures("cocosui/sliderThumb.png", "cocosui/sliderThumb.png", "");
154 slider->loadProgressBarTexture("cocosui/slider_bar_active_9patch.png");
155 slider->setScale9Enabled(true);
156 slider->setCapInsets(Rect(0.0f, 0.0f, 0.0f, 0.0f));
157 slider->setContentSize(Size(250.0f, 19.0f));
158 slider->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f/* + slider->getSize().height * 3.0f*/));
159 slider->addEventListener(CC_CALLBACK_2(UISliderTest_Scale9::sliderEvent, this));
160 _uiLayer->addChild(slider);
161
162
163 return true;
164 }
165 return false;
166}
167
168void UISliderTest_Scale9::sliderEvent(Ref *pSender, Slider::EventType type)
169{
170 if (type == Slider::EventType::ON_PERCENTAGE_CHANGED)
171 {
172 Slider* slider = dynamic_cast<Slider*>(pSender);
173 int percent = slider->getPercent();
174 _displayValueLabel->setString(StringUtils::format("Percent %d", percent));
175 }
176}
177
178// UISliderTest_Scale9_State_Change
179
181 : _displayValueLabel(nullptr)
182{
183
184}
185
187{
188}
189
191{
192 if (UIScene::init())
193 {
194 Size widgetSize = _widget->getContentSize();
195
196 // Add a label in which the slider alert will be displayed
197 _displayValueLabel = Text::create("Click the slider thumb", "fonts/Marker Felt.ttf", 32);
198 _displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
199 _displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
200 _uiLayer->addChild(_displayValueLabel);
201
202 // Add the alert
203 Text *alert = Text::create("Slider scale9 render", "fonts/Marker Felt.ttf", 30);
204 alert->setColor(Color3B(159, 168, 176));
205 alert->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 1.75f));
206 _uiLayer->addChild(alert);
207
208 // Create the slider
209 Slider* slider = Slider::create();
210 slider->loadBarTexture("cocosui/sliderballnormal.png");
211 slider->loadSlidBallTextures("cocosui/sliderThumb.png", "cocosui/sliderThumb.png", "");
212 slider->loadProgressBarTexture("cocosui/slider_bar_active_9patch.png");
213 slider->ignoreContentAdaptWithSize(false);
214 slider->setScale9Enabled(true);
215 slider->setCapInsets(Rect(0.0f, 0.0f, 0.0f, 0.0f));
216 slider->setContentSize(Size(200.0f, 60.0f));
217 slider->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f/* + slider->getSize().height * 3.0f*/));
218 slider->addTouchEventListener([=](Ref* /*sender*/, Widget::TouchEventType type){
219 if (type == Widget::TouchEventType::ENDED) {
220 if (slider->isScale9Enabled())
221 {
222 slider->setScale9Enabled(false);
223 }
224 else
225 slider->setScale9Enabled(true);
226 }
227 });
228 _uiLayer->addChild(slider);
229
230
231 return true;
232 }
233 return false;
234}
235
236void UISliderTest_Scale9_State_Change::sliderEvent(Ref *pSender, Slider::EventType type)
237{
238 if (type == Slider::EventType::ON_PERCENTAGE_CHANGED)
239 {
240 Slider* slider = dynamic_cast<Slider*>(pSender);
241 int percent = slider->getPercent();
242 _displayValueLabel->setString(StringUtils::format("Percent %d", percent));
243 }
244}
245
246// UISliderNormalDefaultTest
247
249: _displayValueLabel(nullptr)
250{
251
252}
253
255{
256}
257
259{
260 if (UIScene::init())
261 {
262 Size widgetSize = _widget->getContentSize();
263
264 // Add a label in which the slider alert will be displayed
265 _displayValueLabel = Text::create("","Arial",32);
266 _displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
267 _displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f + 100));
268 _uiLayer->addChild(_displayValueLabel);
269
270 // Add the alert
271 Text* alert = Text::create("when pressed, the slider ball should scale","fonts/Marker Felt.ttf",20);
272 alert->setColor(Color3B(159, 168, 176));
273 alert->setPosition(Vec2(widgetSize.width / 2.0f,
274 widgetSize.height / 2.0f - alert->getContentSize().height * 3.75f));
275 _uiLayer->addChild(alert);
276
277 // Create the slider
278 Slider* slider = Slider::create();
279 slider->loadBarTexture("cocosui/sliderTrack.png");
280 slider->loadSlidBallTextures("cocosui/sliderThumb.png");
281 slider->setPosition(Vec2(widgetSize.width / 2.0f,
282 widgetSize.height / 2.0f + 50));
283 _uiLayer->addChild(slider);
284
285 Slider* sliderScale9 = (Slider*)slider->clone();
286 sliderScale9->setScale9Enabled(true);
287 sliderScale9->setCapInsets(Rect(0.0f, 0.0f, 0.0f, 0.0f));
288 sliderScale9->setZoomScale(1.0);
289 sliderScale9->setContentSize(Size(250.0f, 19.0f));
290 sliderScale9->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - 20));
291 _uiLayer->addChild(sliderScale9);
292
293
294 return true;
295 }
296 return false;
297}
298
299// UISliderDisabledDefaultTest
300
302: _displayValueLabel(nullptr)
303{
304
305}
306
308{
309}
310
312{
313 if (UIScene::init())
314 {
315 Size widgetSize = _widget->getContentSize();
316
317 // Add a label in which the slider alert will be displayed
318 _displayValueLabel = Text::create("","Arial",32);
319 _displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
320 _displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f + 100));
321 _uiLayer->addChild(_displayValueLabel);
322
323 // Add the alert
324 Text* alert = Text::create("slider ball should be gray.","fonts/Marker Felt.ttf",20);
325 alert->setColor(Color3B(159, 168, 176));
326 alert->setPosition(Vec2(widgetSize.width / 2.0f,
327 widgetSize.height / 2.0f - alert->getContentSize().height * 3.75f));
328 _uiLayer->addChild(alert);
329
330 // Create the slider
331 Slider* slider = Slider::create();
332 slider->loadBarTexture("cocosui/slidbar.png");
333 slider->loadSlidBallTextureNormal("cocosui/sliderballnormal.png");
334 slider->setEnabled(false);
335 slider->setBright(false);
336 slider->setPosition(Vec2(widgetSize.width / 2.0f,
337 widgetSize.height / 2.0f + 50));
338 _uiLayer->addChild(slider);
339
340 Slider* sliderScale9 = (Slider*)slider->clone();
341 sliderScale9->setScale9Enabled(true);
342 sliderScale9->setEnabled(false);
343 sliderScale9->setBright(false);
344 sliderScale9->setCapInsets(Rect(0.0f, 0.0f, 0.0f, 0.0f));
345 sliderScale9->setContentSize(Size(250.0f, 10.0f));
346 sliderScale9->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - 20));
347 _uiLayer->addChild(sliderScale9);
348
349
350 return true;
351 }
352 return false;
353}
354
355
356
357// UISliderNewEventCallbackTest
358
360: _displayValueLabel(nullptr)
361{
362
363}
364
366{
367}
368
370{
371 if (UIScene::init())
372 {
373 Size widgetSize = _widget->getContentSize();
374
375 // Add a label in which the slider alert will be displayed
376 _displayValueLabel = Text::create("","Arial",32);
377 _displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
378 _displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f + 100));
379 _uiLayer->addChild(_displayValueLabel);
380
381 // Add the alert
382 Text* alert = Text::create("See console output for Slider Down and Up event.", "fonts/Marker Felt.ttf", 20);
383 alert->setColor(Color3B(159, 168, 176));
384 alert->setPosition(Vec2(widgetSize.width / 2.0f,
385 widgetSize.height / 2.0f - alert->getContentSize().height * 3.75f));
386 _uiLayer->addChild(alert);
387
388 // Create the slider
389 Slider* slider = Slider::create();
390 slider->loadBarTexture("cocosui/sliderTrack.png");
391 slider->loadSlidBallTextures("cocosui/sliderThumb.png", "cocosui/sliderThumb.png", "");
392 slider->loadProgressBarTexture("cocosui/sliderProgress.png");
393 slider->setMaxPercent(1000);
394 slider->setPosition(Vec2(widgetSize.width / 2.0f,
395 widgetSize.height / 2.0f + 50));
396 slider->addEventListener([=](Ref* widget,Slider::EventType type)
397 {
398 Slider* slider = (Slider*)widget;
399 CC_UNUSED_PARAM(slider);
400 if(type == Slider::EventType::ON_SLIDEBALL_DOWN)
401 {
402 CCLOG("slider button pressed!");
403 }
404 else if(type == Slider::EventType::ON_PERCENTAGE_CHANGED)
405 {
406 CCLOG("slider is moving! percent = %f", 100.0f * slider->getPercent() / slider->getMaxPercent() );
407 }
408 else if(type == Slider::EventType::ON_SLIDEBALL_UP)
409 {
410 CCLOG("slider button is released.");
411 }
412 });
413 _uiLayer->addChild(slider);
414
415
416 return true;
417 }
418 return false;
419}
420
421
422// UISliderIssue12249Test
423
425: _displayValueLabel(nullptr)
426{
427
428}
429
431{
432}
433
435{
436 if (UIScene::init())
437 {
438 Size widgetSize = _widget->getContentSize();
439
440 // Add a label in which the slider alert will be displayed
441 _displayValueLabel = TextBMFont::create("Move the slider thumb", "ccb/markerfelt24shadow.fnt");
442 _displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
443 _displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
444 _uiLayer->addChild(_displayValueLabel);
445
446 // Create the slider
447 Slider* slider = Slider::create();
448 slider->setScale9Enabled(true);
449 slider->loadBarTexture("cocosui/sliderTrack.png");
450 slider->loadSlidBallTextures("cocosui/sliderThumb.png", "cocosui/sliderThumb.png", "");
451 slider->loadProgressBarTexture("cocosui/sliderProgress.png");
452 slider->setContentSize(Size(300, slider->getContentSize().height * 1.5));
453 slider->setMaxPercent(10000);
454 slider->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f/* + slider->getSize().height * 2.0f*/));
455 slider->addEventListener(CC_CALLBACK_2(UISliderIssue12249Test::sliderEvent, this));
456 _uiLayer->addChild(slider);
457
458
459 return true;
460 }
461 return false;
462}
463
464void UISliderIssue12249Test::sliderEvent(Ref *pSender, Slider::EventType type)
465{
466 if (type == Slider::EventType::ON_PERCENTAGE_CHANGED)
467 {
468 Slider* slider = dynamic_cast<Slider*>(pSender);
469 int percent = slider->getPercent();
470 int maxPercent = slider->getMaxPercent();
471 _displayValueLabel->setString(StringUtils::format("Percent %f", 10000.0 * percent / maxPercent));
472 }
473}
#define ADD_TEST_CASE(__className__)
Definition: BaseTest.h:211
USING_NS_CC
cocos2d::Layer * _uiLayer
Definition: UIScene.h:44
virtual bool init() override
Definition: UIScene.cpp:46
cocos2d::ui::Layout * _widget
Definition: UIScene.h:45
cocos2d::ui::Text * _displayValueLabel
Definition: UISliderTest.h:101
virtual bool init() override
virtual bool init() override
cocos2d::ui::TextBMFont * _displayValueLabel
Definition: UISliderTest.h:128
void sliderEvent(cocos2d::Ref *sender, cocos2d::ui::Slider::EventType type)
cocos2d::ui::Text * _displayValueLabel
Definition: UISliderTest.h:114
virtual bool init() override
cocos2d::ui::Text * _displayValueLabel
Definition: UISliderTest.h:88
virtual bool init() override
virtual bool init() override
cocos2d::ui::Text * _displayValueLabel
Definition: UISliderTest.h:74
void sliderEvent(cocos2d::Ref *sender, cocos2d::ui::Slider::EventType type)
void sliderEvent(cocos2d::Ref *sender, cocos2d::ui::Slider::EventType type)
cocos2d::ui::Text * _displayValueLabel
Definition: UISliderTest.h:60
virtual bool init() override
void sliderEvent(cocos2d::Ref *sender, cocos2d::ui::Slider::EventType type)
void printWidgetResources(cocos2d::Ref *sender)
cocos2d::ui::TextBMFont * _displayValueLabel
Definition: UISliderTest.h:45
virtual bool init() override
cocos2d::ui::Slider * _slider
Definition: UISliderTest.h:46
static cocos2d::Vec2 bottom()
Definition: VisibleRect.cpp:63
static cocos2d::Vec2 left()
Definition: VisibleRect.cpp:45