PuzzleSDK
UILoadingBarTest.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 "UILoadingBarTest.h"
26
28using namespace cocos2d::ui;
29
30UILoadingBarTests::UILoadingBarTests()
31{
40}
41
42// UILoadingBarTest_Left
43
45: _count(0)
46{
47
48}
49
51{
52 unscheduleUpdate();
53}
54
56{
57 if (UIScene::init())
58 {
59 scheduleUpdate();
60
61 Size widgetSize = _widget->getContentSize();
62
63 // Add the alert
64 Text* alert = Text::create("Test LoadingBar Change Direction",
65 "fonts/Marker Felt.ttf", 30);
66 alert->setColor(Color3B(159, 168, 176));
67 alert->setPosition(Vec2(widgetSize.width / 2.0f,
68 widgetSize.height / 2.0f - alert->getContentSize().height * 1.75f));
69 _uiLayer->addChild(alert);
70
71 // Create the loading bar
72 LoadingBar* loadingBar = LoadingBar::create("cocosui/sliderProgress.png");
73 loadingBar->setTag(0);
74 loadingBar->setPosition(Vec2(widgetSize.width / 2.0f,
75 widgetSize.height / 2.0f + loadingBar->getContentSize().height / 4.0f));
76
77 auto loadingBarCopy = (LoadingBar*)loadingBar->clone();
78 loadingBarCopy->setTag(1);
79 loadingBarCopy->setPosition(loadingBar->getPosition()
80 + Vec2(0, -40));
81 loadingBarCopy->setDirection(LoadingBar::Direction::RIGHT);
82
83 Button* button = Button::create("cocosui/animationbuttonnormal.png",
84 "cocosui/animationbuttonpressed.png");
85 button->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f + 50));
86 button->setTitleText("Click to change direction!");
87
88 button->addTouchEventListener([=](Ref*, Widget::TouchEventType type)
89 {
90 if (type == Widget::TouchEventType::ENDED)
91 {
92 if (loadingBar->getDirection() == LoadingBar::Direction::LEFT)
93 {
94 loadingBar->setDirection(LoadingBar::Direction::RIGHT);
95 loadingBarCopy->setDirection(LoadingBar::Direction::LEFT);
96 }
97 else
98 {
99 loadingBar->setDirection(LoadingBar::Direction::LEFT);
100 loadingBarCopy->setDirection(LoadingBar::Direction::RIGHT);
101 }
102 }
103 });
104 _uiLayer->addChild(loadingBar,1);
105 _uiLayer->addChild(loadingBarCopy,2);
106 _uiLayer->addChild(button);
107
108 _loadingBar = loadingBar;
109
110 TTFConfig ttfConfig("fonts/arial.ttf", 15);
111 auto label1 = Label::createWithTTF(ttfConfig, "Print Resources");
112 auto item1 = MenuItemLabel::create(label1, CC_CALLBACK_1(UILoadingBarTest_Left::printWidgetResources, this));
113 item1->setPosition(Vec2(VisibleRect::left().x + 60, VisibleRect::bottom().y + item1->getContentSize().height * 3));
114 auto pMenu1 = Menu::create(item1, nullptr);
115 pMenu1->setPosition(Vec2(0, 0));
116 this->addChild(pMenu1, 10);
117
118 return true;
119 }
120 return false;
121}
122
124{
125 _count++;
126 if (_count > 100)
127 {
128 _count = 0;
129 }
130 LoadingBar* loadingBar = static_cast<LoadingBar*>(_uiLayer->getChildByTag(0));
131 LoadingBar* loadingBarCopy = static_cast<LoadingBar*>(_uiLayer->getChildByTag(1));
132 loadingBar->setPercent(_count);
133 loadingBarCopy->setPercent(_count);
134}
135
137{
138 cocos2d::ResourceData textureFile = _loadingBar->getRenderFile();
139 CCLOG("textureFile Name : %s, Type: %d", textureFile.file.c_str(), textureFile.type);
140}
141
142// UILoadingBarTest_Right
143
145: _count(0)
146{
147
148}
149
151{
152 unscheduleUpdate();
153}
154
156{
157 if (UIScene::init())
158 {
159 scheduleUpdate();
160
161 Size widgetSize = _widget->getContentSize();
162
163 // Add the alert
164 Text *alert = Text::create("LoadingBar right", "fonts/Marker Felt.ttf", 30);
165 alert->setColor(Color3B(159, 168, 176));
166 alert->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 1.75f));
167 _uiLayer->addChild(alert);
168
169 // Create the loading bar
170 LoadingBar* loadingBar = LoadingBar::create("cocosui/sliderProgress.png");
171 loadingBar->setTag(0);
172 loadingBar->setDirection(LoadingBar::Direction::RIGHT);
173
174 loadingBar->setPosition(Vec2(widgetSize.width / 2.0f,
175 widgetSize.height / 2.0f + loadingBar->getContentSize().height / 4.0f));
176
177 _uiLayer->addChild(loadingBar);
178
179 return true;
180 }
181 return false;
182}
183
185{
186 _count++;
187 if (_count > 100)
188 {
189 _count = 0;
190 }
191 LoadingBar* loadingBar = static_cast<LoadingBar*>(_uiLayer->getChildByTag(0));
192 loadingBar->setPercent(_count);
193}
194
195// UILoadingBarTest_Left_Scale9
196
198: _count(0)
199{
200
201}
202
204{
205 unscheduleUpdate();
206}
207
209{
210 if (UIScene::init())
211 {
212 scheduleUpdate();
213
214 Size widgetSize = _widget->getContentSize();
215
216 // Add the alert
217 Text* alert = Text::create("LoadingBar left scale9 render", "fonts/Marker Felt.ttf", 20);
218 alert->setColor(Color3B(159, 168, 176));
219 alert->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 2.7f));
220 _uiLayer->addChild(alert);
221
222 // Create the loading bar
223 LoadingBar* loadingBar = LoadingBar::create("cocosui/slider_bar_active_9patch.png");
224 loadingBar->setTag(0);
225 loadingBar->setScale9Enabled(true);
226 loadingBar->setCapInsets(Rect(0, 0, 0, 0));
227 loadingBar->setContentSize(Size(300, 13));
228
229 loadingBar->setPosition(Vec2(widgetSize.width / 2.0f,
230 widgetSize.height / 2.0f + loadingBar->getContentSize().height / 4.0f));
231
232 _uiLayer->addChild(loadingBar);
233
234 return true;
235 }
236 return false;
237}
238
240{
241 _count++;
242 if (_count > 100)
243 {
244 _count = 0;
245 }
246 LoadingBar* loadingBar = static_cast<LoadingBar*>(_uiLayer->getChildByTag(0));
247 loadingBar->setPercent(_count);
248}
249
250// UILoadingBarTest_Right_Scale9
251
253: _count(0)
254{
255
256}
257
259{
260 unscheduleUpdate();
261}
262
264{
265 if (UIScene::init())
266 {
267 scheduleUpdate();
268
269 Size widgetSize = _widget->getContentSize();
270
271 // Add the alert
272 Text *alert = Text::create("LoadingBar right scale9 render", "fonts/Marker Felt.ttf", 20);
273 alert->setColor(Color3B(159, 168, 176));
274 alert->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 2.7f));
275 _uiLayer->addChild(alert);
276
277 // Create the loading bar
278 LoadingBar* loadingBar = LoadingBar::create("cocosui/slider_bar_active_9patch.png");
279 loadingBar->setTag(0);
280 loadingBar->setScale9Enabled(true);
281 loadingBar->setCapInsets(Rect(0, 0, 0, 0));
282 loadingBar->setContentSize(Size(300, 13));
283 loadingBar->setDirection(LoadingBar::Direction::RIGHT);
284
285 loadingBar->setPosition(Vec2(widgetSize.width / 2.0f,
286 widgetSize.height / 2.0f + loadingBar->getContentSize().height / 4.0f));
287
288 _uiLayer->addChild(loadingBar);
289
290 return true;
291 }
292 return false;
293}
294
296{
297 _count++;
298 if (_count > 100)
299 {
300 _count = 0;
301 }
302 LoadingBar* loadingBar = static_cast<LoadingBar*>(_uiLayer->getChildByTag(0));
303 loadingBar->setPercent(_count);
304}
305
306// UILoadingBarTest_Scale9_State_Change
307
309 : _count(0)
310{
311
312}
313
315{
316
317}
318
320{
321 if (UIScene::init())
322 {
323 Size widgetSize = _widget->getContentSize();
324
325 // Add the alert
326 Text *alert = Text::create("LoadingBar right scale9 render", "fonts/Marker Felt.ttf", 20);
327 alert->setColor(Color3B(159, 168, 176));
328 alert->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 2.7f));
329 _uiLayer->addChild(alert);
330
331 // Create the loading bar
332 LoadingBar* loadingBar = LoadingBar::create("cocosui/sliderThumb.png");
333 loadingBar->setTag(0);
334 loadingBar->ignoreContentAdaptWithSize(false);
335 //loadingBar->setScale9Enabled(true);
336 loadingBar->setCapInsets(Rect(0, 0, 0, 0));
337 loadingBar->setContentSize(Size(200, 80));
338 loadingBar->setDirection(LoadingBar::Direction::LEFT);
339 loadingBar->setPercent(100);
340
341 loadingBar->setTouchEnabled(true);
342 loadingBar->addTouchEventListener([=](Ref* sender, Widget::TouchEventType type){
343 if (type == Widget::TouchEventType::ENDED) {
344 if (loadingBar->isScale9Enabled())
345 {
346 loadingBar->setScale9Enabled(false);
347 }
348 else
349 loadingBar->setScale9Enabled(true);
350 }
351 });
352
353 loadingBar->setPosition(Vec2(widgetSize.width / 2.0f,
354 widgetSize.height / 2.0f + loadingBar->getContentSize().height / 4.0f));
355
356 _uiLayer->addChild(loadingBar);
357
358 return true;
359 }
360 return false;
361}
362
363
364// UILoadingBarReloadTexture
365
367: _count(0)
368{
369
370}
371
373{
374
375}
376
378{
379 if (UIScene::init())
380 {
381 Size widgetSize = _widget->getContentSize();
382
383 // Add the alert
384 Text *alert = Text::create("Click button to Toggle Scale9 and switch Texture.", "fonts/Marker Felt.ttf", 20);
385 alert->setColor(Color3B(159, 168, 176));
386 alert->setPosition(Vec2(widgetSize.width / 2.0f,
387 widgetSize.height / 2.0f - alert->getContentSize().height * 2.7f));
388 _uiLayer->addChild(alert);
389
390 LoadingBar* loadingBar = LoadingBar::create("cocosui/slider_bar_active_9patch.png");
391 loadingBar->setTag(0);
392 loadingBar->ignoreContentAdaptWithSize(false);
393// loadingBar->setScale9Enabled(true);
394 loadingBar->setCapInsets(Rect(0, 0, 0, 0));
395 loadingBar->setContentSize(Size(300, 13));
396 loadingBar->setName("texture0");
397 loadingBar->setDirection(LoadingBar::Direction::RIGHT);
398 loadingBar->setPercent(70);
399 loadingBar->setPosition(Vec2(widgetSize.width / 2.0f,
400 widgetSize.height / 2.0f + loadingBar->getContentSize().height / 4.0f));
401
402 _uiLayer->addChild(loadingBar);
403
404 auto buttonScale9 = Button::create("cocosui/animationbuttonnormal.png",
405 "cocosui/animationbuttonpressed.png");
406 buttonScale9->setTitleText("ToggleScale9");
407 buttonScale9->addClickEventListener([=](Ref*){
408 loadingBar->setScale9Enabled(!loadingBar->isScale9Enabled());
409 });
410 buttonScale9->setPosition(loadingBar->getPosition() + Vec2(-50,50));
411 _uiLayer->addChild(buttonScale9);
412
413 auto buttonChangeTexture = Button::create("cocosui/animationbuttonnormal.png",
414 "cocosui/animationbuttonpressed.png");
415 buttonChangeTexture->setTitleText("ChangeTexture");
416 buttonChangeTexture->addClickEventListener([=](Ref*){
417 auto name = loadingBar->getName();
418 if (name == "texture0")
419 {
420 loadingBar->loadTexture("cocosui/slider_bar_active_9patch2.png");
421 loadingBar->setName("texture1");
422 }
423 else
424 {
425 loadingBar->loadTexture("cocosui/slider_bar_active_9patch.png");
426 loadingBar->setName("texture0");
427 }
428 });
429 buttonChangeTexture->setPosition(loadingBar->getPosition() + Vec2(50,50));
430 _uiLayer->addChild(buttonChangeTexture);
431
432 this->scheduleUpdate();
433 return true;
434 }
435 return false;
436}
437
439{
440 _count++;
441 if (_count > 100)
442 {
443 _count = 0;
444 }
445 LoadingBar* loadingBar = static_cast<LoadingBar*>(_uiLayer->getChildByTag(0));
446 loadingBar->setPercent(_count);
447}
448
449
450// UILoadingBarIssue12249
451
453: _count(0)
454{
455
456}
457
459{
460 unscheduleUpdate();
461}
462
464{
465 if (UIScene::init())
466 {
467 scheduleUpdate();
468
469 Size widgetSize = _widget->getContentSize();
470
471 // Add the alert
472 Text* alert = Text::create("Test LoadingBar Change Direction",
473 "fonts/Marker Felt.ttf", 30);
474 alert->setColor(Color3B(159, 168, 176));
475 alert->setPosition(Vec2(widgetSize.width / 2.0f,
476 widgetSize.height / 2.0f - alert->getContentSize().height * 1.75f));
477 _uiLayer->addChild(alert);
478
479 // Create the loading bar
480 LoadingBar* loadingBar = LoadingBar::create("cocosui/sliderProgress.png");
481 loadingBar->setScale9Enabled(true);
482 loadingBar->setContentSize(Size(200, loadingBar->getContentSize().height * 1.5));
483 loadingBar->setTag(0);
484 loadingBar->setPosition(Vec2(widgetSize.width / 2.0f,
485 widgetSize.height / 2.0f + loadingBar->getContentSize().height / 4.0f));
486
487 LoadingBar* loadingBarCopy = LoadingBar::create();
488 loadingBarCopy->setScale9Enabled(true);
489 loadingBarCopy->loadTexture("cocosui/sliderProgress.png");
490 loadingBarCopy->setContentSize(Size(200, loadingBarCopy->getContentSize().height * 1.5));
491 loadingBarCopy->setTag(1);
492 loadingBarCopy->setPosition(loadingBar->getPosition()
493 + Vec2(0, -40));
494 loadingBarCopy->setDirection(LoadingBar::Direction::RIGHT);
495
496 Button* button = Button::create("cocosui/animationbuttonnormal.png",
497 "cocosui/animationbuttonpressed.png");
498 button->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f + 50));
499 button->setTitleText("Click to change direction!");
500
501 button->addTouchEventListener([=](Ref*, Widget::TouchEventType type)
502 {
503 if (type == Widget::TouchEventType::ENDED)
504 {
505 if (loadingBar->getDirection() == LoadingBar::Direction::LEFT)
506 {
507 loadingBar->setDirection(LoadingBar::Direction::RIGHT);
508 loadingBarCopy->setDirection(LoadingBar::Direction::LEFT);
509 }
510 else
511 {
512 loadingBar->setDirection(LoadingBar::Direction::LEFT);
513 loadingBarCopy->setDirection(LoadingBar::Direction::RIGHT);
514 }
515 }
516 });
517 _uiLayer->addChild(loadingBar,1);
518 _uiLayer->addChild(loadingBarCopy,2);
519 _uiLayer->addChild(button);
520
521 return true;
522 }
523 return false;
524}
525
527{
528 _count++;
529 if (_count > 100)
530 {
531 _count = 0;
532 }
533 LoadingBar* loadingBar = static_cast<LoadingBar*>(_uiLayer->getChildByTag(0));
534 LoadingBar* loadingBarCopy = static_cast<LoadingBar*>(_uiLayer->getChildByTag(1));
535 loadingBar->setPercent(_count);
536 loadingBarCopy->setPercent(_count);
537}
538
539// UILoadingBarTest_Direction
540
542 : _count(0)
543{
544
545}
546
548{
549 //unscheduleUpdate();
550}
551
553{
554 if (UIScene::init())
555 {
556 //scheduleUpdate();
557
558 Size widgetSize = _widget->getContentSize();
559
560 // Add the alert
561 Text* alert = Text::create("Test LoadingBar Change Direction",
562 "fonts/Marker Felt.ttf", 30);
563 alert->setColor(Color3B(159, 168, 176));
564 alert->setPosition(Vec2(widgetSize.width / 2.0f,
565 widgetSize.height / 2.0f - alert->getContentSize().height * 1.75f));
566 _uiLayer->addChild(alert);
567
568 // Create the loading bar
569 LoadingBar* loadingBar = LoadingBar::create("cocosui/sliderProgress.png");
570 loadingBar->setTag(0);
571 loadingBar->setPercent(80);
572 loadingBar->setPosition(Vec2(widgetSize.width / 2.0f,
573 widgetSize.height / 2.0f + loadingBar->getContentSize().height / 4.0f));
574
575 auto loadingBarCopy = (LoadingBar*)loadingBar->clone();
576 loadingBarCopy->setTag(1);
577 loadingBarCopy->setPosition(loadingBar->getPosition()
578 + Vec2(0, -40));
579 loadingBarCopy->setDirection(LoadingBar::Direction::RIGHT);
580
581 Button* button = Button::create("cocosui/animationbuttonnormal.png",
582 "cocosui/animationbuttonpressed.png");
583 button->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f + 50));
584 button->setTitleText("Click to change direction!");
585
586 button->addTouchEventListener([=](Ref*, Widget::TouchEventType type)
587 {
588 if (type == Widget::TouchEventType::ENDED)
589 {
590 if (loadingBar->getDirection() == LoadingBar::Direction::LEFT)
591 {
592 loadingBar->setDirection(LoadingBar::Direction::RIGHT);
593 loadingBarCopy->setDirection(LoadingBar::Direction::LEFT);
594 }
595 else
596 {
597 loadingBar->setDirection(LoadingBar::Direction::LEFT);
598 loadingBarCopy->setDirection(LoadingBar::Direction::RIGHT);
599 }
600 }
601 });
602 _uiLayer->addChild(loadingBar, 1);
603 _uiLayer->addChild(loadingBarCopy, 2);
604 _uiLayer->addChild(button);
605
606 _loadingBar = loadingBar;
607 return true;
608 }
609 return false;
610}
#define ADD_TEST_CASE(__className__)
Definition: BaseTest.h:211
USING_NS_CC
virtual bool init() override
void update(float delta) override
virtual bool init() override
void update(float dt) override
cocos2d::ui::LoadingBar * _loadingBar
virtual bool init() override
void update(float delta) override
virtual bool init() override
void printWidgetResources(cocos2d::Ref *sender)
virtual bool init() override
cocos2d::ui::LoadingBar * _loadingBar
void update(float delta) override
virtual bool init() override
void update(float delta) override
virtual bool init() override
void update(float delta) override
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