PuzzleSDK
UIButtonTest.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 "UIButtonTest.h"
26
28using namespace cocos2d::ui;
29
30UIButtonTests::UIButtonTests()
31{
51}
52
53// UIButtonTest
55: _displayValueLabel(nullptr)
56{
57
58}
59
61{
62}
63
65{
66 if (UIScene::init())
67 {
68 Size widgetSize = _widget->getContentSize();
69
70 // Add a label in which the button events will be displayed
71 _displayValueLabel = Text::create("No Event", "fonts/Marker Felt.ttf",32);
72 _displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
73 _displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
75
76 // Add the alert
77 Text* alert = Text::create("Button","fonts/Marker Felt.ttf",30);
78 alert->setColor(Color3B(159, 168, 176));
79
80 alert->setPosition(Vec2(widgetSize.width / 2.0f,
81 widgetSize.height / 2.0f - alert->getContentSize().height * 1.75f));
82
83 _uiLayer->addChild(alert);
84
85 // Create the button
86 Button* button = Button::create("cocosui/animationbuttonnormal.png",
87 "cocosui/animationbuttonpressed.png");
88 CCLOG("content size should be greater than 0: width = %f, height = %f", button->getContentSize().width,
89 button->getContentSize().height);
90 button->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
91 button->addTouchEventListener(CC_CALLBACK_2(UIButtonTest::touchEvent, this));
92 button->setZoomScale(0.4f);
93 button->setPressedActionEnabled(true);
94 _uiLayer->addChild(button);
95 button->setOpacity(100);
96 // Create the imageview
97 ImageView* imageView = ImageView::create();
98
99 imageView->setPosition(Vec2(widgetSize.width / 2.0f + 50+ button->getContentSize().width/2,
100 widgetSize.height / 2.0f));
101 imageView->setTag(12);
102
103 _uiLayer->addChild(imageView);
104
105 _button = button;
106
107 TTFConfig ttfConfig("fonts/arial.ttf", 15);
108 auto label1 = Label::createWithTTF(ttfConfig, "Print Resources");
109 auto item1 = MenuItemLabel::create(label1, CC_CALLBACK_1(UIButtonTest::printWidgetResources, this));
110 item1->setPosition(Vec2(VisibleRect::left().x + 60, VisibleRect::bottom().y + item1->getContentSize().height * 3));
111 auto pMenu1 = Menu::create(item1, nullptr);
112 pMenu1->setPosition(Vec2(0.0f, 0.0f));
113 this->addChild(pMenu1, 10);
114
115 return true;
116 }
117 return false;
118}
119
120void UIButtonTest::touchEvent(Ref *pSender, Widget::TouchEventType type)
121{
122 switch (type)
123 {
124 case Widget::TouchEventType::BEGAN:
125 _displayValueLabel->setString(StringUtils::format("Touch Down"));
126 break;
127
128 case Widget::TouchEventType::MOVED:
129 _displayValueLabel->setString(StringUtils::format("Touch Move"));
130 break;
131
132 case Widget::TouchEventType::ENDED:
133 {
134 _displayValueLabel->setString(StringUtils::format("Touch Up"));
135 ImageView* imageView = (ImageView*)_uiLayer->getChildByTag(12);
136 imageView->setVisible(false);
137 imageView->loadTexture("cocosui/ccicon.png");
138 imageView->setOpacity(0);
139 imageView->setVisible(true);
140 imageView->runAction(Sequence::create(FadeIn::create(0.5),DelayTime::create(1.0),FadeOut::create(0.5), nullptr));
141 Button *btn = (Button*)pSender;
142 btn->loadTextureNormal("cocosui/animationbuttonnormal.png");
143 }
144 break;
145
146 case Widget::TouchEventType::CANCELED:
147 _displayValueLabel->setString(StringUtils::format("Touch Cancelled"));
148 break;
149
150 default:
151 break;
152 }
153}
154
155void UIButtonTest::printWidgetResources(cocos2d::Ref* sender)
156{
157 cocos2d::ResourceData normalFileName = _button->getNormalFile();
158 CCLOG("normalFileName Name : %s, Type: %d", normalFileName.file.c_str(), normalFileName.type);
159 cocos2d::ResourceData clickedFileName = _button->getPressedFile();
160 CCLOG("clickedFileName Name : %s, Type: %d", clickedFileName.file.c_str(), clickedFileName.type);
161 cocos2d::ResourceData disabledFileName = _button->getDisabledFile();
162 CCLOG("disabledFileName Name : %s, Type: %d", disabledFileName.file.c_str(), disabledFileName.type);
163}
164
165
166// UIButtonTest_Scale9
168: _displayValueLabel(nullptr)
169{
170
171}
172
174{
175
176}
177
179{
180 if (UIScene::init())
181 {
182 Size widgetSize = _widget->getContentSize();
183
184 // Add a label in which the button events will be displayed
185 _displayValueLabel = Text::create("No Event", "fonts/Marker Felt.ttf", 32);
186 _displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
187 _displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
188 _uiLayer->addChild(_displayValueLabel);
189
190 // Add the alert
191 Text* alert = Text::create("Button scale9 render", "fonts/Marker Felt.ttf",30);
192 alert->setColor(Color3B(159, 168, 176));
193 alert->setPosition(Vec2(widgetSize.width / 2.0f,
194 widgetSize.height / 2.0f - alert->getContentSize().height * 1.75f));
195 _uiLayer->addChild(alert);
196
197 // Create the button
198 Button* button = Button::create("cocosui/button.png", "cocosui/buttonHighlighted.png");
199 // open scale9 render
200 button->setScale9Enabled(true);
201 button->setContentSize(Size(150.0f, 70.0f));
202 button->setPosition(Vec2(-button->getContentSize().width - 10, widgetSize.height / 2.0f));
203 auto moveBy = MoveBy::create(1.0f, Vec2(widgetSize.width/2, 0.0f));
204 auto moveByReverse = moveBy->reverse()->clone();
205 button->runAction(RepeatForever::create(
206 Sequence::create(moveBy,moveByReverse, NULL)));
207 button->setPressedActionEnabled(true);
208 button->addTouchEventListener(CC_CALLBACK_2(UIButtonTest_Scale9::touchEvent, this));
209 _uiLayer->addChild(button);
210
211 // Create the imageview
212 Button* button2 = Button::create();
213 button2->setPosition(Vec2(widgetSize.width / 2.0f + button->getContentSize().width + 20, widgetSize.height / 2.0f));
214 button2->setName("normal");
215 _uiLayer->addChild(button2);
216
217 Sprite *sprite = Sprite::create("cocosui/animationbuttonnormal.png");
218 button2->addChild(sprite);
219
220 return true;
221 }
222 return false;
223}
224
225void UIButtonTest_Scale9::touchEvent(Ref *pSender, Widget::TouchEventType type)
226{
227 switch (type)
228 {
229 case Widget::TouchEventType::BEGAN:
230 _displayValueLabel->setString(StringUtils::format("Touch Down"));
231 break;
232
233 case Widget::TouchEventType::MOVED:
234 _displayValueLabel->setString(StringUtils::format("Touch Move"));
235 break;
236
237 case Widget::TouchEventType::ENDED:
238 {
239 _displayValueLabel->setString(StringUtils::format("Touch Up"));
240 Button *btn = (Button*)_uiLayer->getChildByName("normal");
241 btn->loadTextureNormal("cocosui/animationbuttonnormal.png");
242 btn->loadTexturePressed("cocosui/animationbuttonpressed.png");
243 btn->runAction(Sequence::create(FadeIn::create(0.5),DelayTime::create(1.0),FadeOut::create(0.5), nullptr));
244 }
245 break;
246
247 case Widget::TouchEventType::CANCELED:
248 _displayValueLabel->setString(StringUtils::format("Touch Cancelled"));
249 break;
250
251 default:
252 break;
253 }
254}
255
256// UIButtonTest_Scale9_State_Change
258 : _displayValueLabel(nullptr)
259{
260
261}
262
264{
265
266}
267
269{
270 if (UIScene::init())
271 {
272 Size widgetSize = _widget->getContentSize();
273
274 // Add a label in which the button events will be displayed
275 _displayValueLabel = Text::create("No Event", "fonts/Marker Felt.ttf", 32);
276 _displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
277 _displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
278 _uiLayer->addChild(_displayValueLabel);
279
280 // Add the alert
281 Text* alert = Text::create("Button scale9 render", "fonts/Marker Felt.ttf", 30);
282 alert->setColor(Color3B(159, 168, 176));
283 alert->setPosition(Vec2(widgetSize.width / 2.0f,
284 widgetSize.height / 2.0f - alert->getContentSize().height * 1.75f));
285 _uiLayer->addChild(alert);
286
287 // Create the button
288 Button* button = Button::create("cocosui/button.png");
289 // open scale9 render
290 button->ignoreContentAdaptWithSize(false);
291 button->setScale9Enabled(true);
292 button->setPosition(Vec2(widgetSize.width / 2.0f - 100, widgetSize.height / 2.0f));
293 button->setContentSize(Size(180.0f, 60.0f));
294 button->setTitleText("Hello Scale9");
295 button->setPressedActionEnabled(false);
296 button->addTouchEventListener(CC_CALLBACK_2(UIButtonTest_Scale9_State_Change::touchEvent, this));
297 _uiLayer->addChild(button);
298
299 Button* button2 = Button::create("cocosui/button.png", "cocosui/buttonHighlighted.png");
300 // open scale9 render
301 button2->ignoreContentAdaptWithSize(false);
302 button2->setScale9Enabled(true);
303 button2->setTitleText("Hello scale9");
304 button2->setPosition(Vec2(widgetSize.width / 2.0f + 100, widgetSize.height / 2.0f));
305 button2->setContentSize(Size(180.0f, 60.0f));
306 button2->setPressedActionEnabled(true);
307 button2->addTouchEventListener(CC_CALLBACK_2(UIButtonTest_Scale9_State_Change::touchEvent, this));
308 _uiLayer->addChild(button2);
309 return true;
310 }
311 return false;
312}
313
314void UIButtonTest_Scale9_State_Change::touchEvent(Ref *pSender, Widget::TouchEventType type)
315{
316 switch (type)
317 {
318 case Widget::TouchEventType::BEGAN:
319 _displayValueLabel->setString(StringUtils::format("Touch Down"));
320 break;
321
322 case Widget::TouchEventType::MOVED:
323 _displayValueLabel->setString(StringUtils::format("Touch Move"));
324 break;
325
326 case Widget::TouchEventType::ENDED:
327 {
328 _displayValueLabel->setString(StringUtils::format("Touch Up"));
329 Button *btn = (Button*)pSender;
330 if (btn->isScale9Enabled())
331 {
332 btn->setScale9Enabled(false);
333 }
334 else
335 btn->setScale9Enabled(true);
336 }
337 break;
338
339 case Widget::TouchEventType::CANCELED:
340 _displayValueLabel->setString(StringUtils::format("Touch Cancelled"));
341 break;
342
343 default:
344 break;
345 }
346}
347
348// UIButtonTest_PressAction
350: _displayValueLabel(nullptr)
351{
352}
353
355{
356}
357
359{
360 if (UIScene::init())
361 {
362 Size widgetSize = _widget->getContentSize();
363
364 // Add a label in which the button events will be displayed
365 _displayValueLabel = Text::create("No Event", "fonts/Marker Felt.ttf",32);
366 _displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
367 _displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
368 _uiLayer->addChild(_displayValueLabel);
369
370 // Add the alert
371 Text* alert = Text::create("Button Pressed Action", "fonts/Marker Felt.ttf", 30);
372 alert->setColor(Color3B(159, 168, 176));
373
374 alert->setPosition(Vec2(widgetSize.width / 2.0f,
375 widgetSize.height / 2.0f - alert->getContentSize().height * 1.75f));
376
377 _uiLayer->addChild(alert);
378
379 // Create the button
380 Button* button = Button::create("cocosui/animationbuttonnormal.png", "cocosui/animationbuttonpressed.png");
381 button->setPressedActionEnabled(true);
382 button->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
383 button->setColor(Color3B::GREEN);
384 button->setOpacity(30);
385 button->addTouchEventListener(CC_CALLBACK_2(UIButtonTest_PressedAction::touchEvent, this));
386 button->setName("button");
387 _uiLayer->addChild(button);
388
389 Button* button2 = Button::create("cocosui/animationbuttonnormal.png", "cocosui/animationbuttonpressed.png");
390 button2->setPosition(button->getPosition() + Vec2(100.0f,0.0f));
391 button2->setName("button2");
392 _uiLayer->addChild(button2);
393
394 return true;
395 }
396 return false;
397}
398
399void UIButtonTest_PressedAction::touchEvent(Ref *pSender, Widget::TouchEventType type)
400{
401 switch (type)
402 {
403 case Widget::TouchEventType::BEGAN:
404 _displayValueLabel->setString(StringUtils::format("Touch Down"));
405 break;
406
407 case Widget::TouchEventType::MOVED:
408 _displayValueLabel->setString(StringUtils::format("Touch Move"));
409 break;
410
411 case Widget::TouchEventType::ENDED:
412 {
413 _displayValueLabel->setString(StringUtils::format("Touch Up"));
414 Button* btn = (Button*)_uiLayer->getChildByName("button");
415 btn->loadTextureNormal("cocosui/animationbuttonnormal.png");
416
417 Button* btn2 = (Button*)_uiLayer->getChildByName("button2");
418 btn2->setAnchorPoint(Vec2(0,0.5));
419 }
420 break;
421
422 case Widget::TouchEventType::CANCELED:
423 _displayValueLabel->setString(StringUtils::format("Touch Cancelled"));
424 break;
425
426 default:
427 break;
428 }
429}
430
431// UIButtonTest_Title
433: _displayValueLabel(nullptr)
434{
435
436}
437
439{
440}
441
443{
444 if (UIScene::init())
445 {
446 Size widgetSize = _widget->getContentSize();
447
448 // Add a label in which the text button events will be displayed
449 _displayValueLabel = Text::create("No Event", "fonts/Marker Felt.ttf", 32);
450 _displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
451 _displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
452 _uiLayer->addChild(_displayValueLabel);
453
454 // Add the alert
455 Text* alert = Text::create("Button with title, title should be flipped!", "fonts/Marker Felt.ttf", 30);
456 alert->setColor(Color3B(159, 168, 176));
457 alert->setPosition(Vec2(widgetSize.width / 2.0f,
458 widgetSize.height / 2.0f - alert->getContentSize().height * 1.75f));
459
460 _uiLayer->addChild(alert);
461
462 // Create the button with title
463 Button* button = Button::create("cocosui/backtotoppressed.png", "cocosui/backtotopnormal.png");
464 button->setTitleText("Title Button!");
465 button->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
466 button->setTitleColor(Color3B::YELLOW);
467 CCASSERT(button->getTitleColor() == Color3B::YELLOW, "Button setTitleColor & getTitleColor not match!");
468 button->addTouchEventListener(CC_CALLBACK_2(UIButtonTest_Title::touchEvent, this));
469 _uiLayer->addChild(button);
470 button->setFlippedX(true);
471 auto label = button->getTitleRenderer();
472 label->setScale(4.0);
473 button->runAction(RepeatForever::create(Sequence::create(ScaleTo::create(1.0f, 1.2f),
474 ScaleTo::create(1.0f, 1.0f),nullptr)));
475
476
477 TextBMFont *text = TextBMFont::create("BMFont", "cocosui/bitmapFontTest2.fnt");
478 text->setPosition(button->getPosition() + Vec2(button->getContentSize().width/2 + 50,0.0f));
479 text->setColor(Color3B::YELLOW);
480 text->setOpacity(50);
481 text->setName("text");
482
483 auto button2 = Button::create("cocosui/backtotopnormal.png", "cocosui/backtotoppressed.png");
484 button2->setTitleFontName("fonts/helvetica-32.fnt");
485 button2->setTitleText("BMFont");
486 //the change of font size will take no effect.
487 button2->setTitleFontSize(30);
488 button2->setPosition(Vec2(button->getPosition() + Vec2(50.0f,-80.0f)));
489 this->addChild(button2);
490
491 _uiLayer->addChild(text);
492
493 return true;
494 }
495 return false;
496}
497
498
499void UIButtonTest_Title::touchEvent(Ref *pSender, Widget::TouchEventType type)
500{
501 switch (type)
502 {
503 case Widget::TouchEventType::BEGAN:
504 _displayValueLabel->setString(StringUtils::format("Touch Down"));
505 break;
506
507 case Widget::TouchEventType::MOVED:
508 _displayValueLabel->setString(StringUtils::format("Touch Move"));
509 break;
510
511 case Widget::TouchEventType::ENDED:
512 {
513 _displayValueLabel->setString(StringUtils::format("Touch Up"));
514 TextBMFont *text = (TextBMFont*)_uiLayer->getChildByName("text");
515 text->setFntFile("cocosui/bitmapFontTest2.fnt");
516 if (text->getString() == "BMFont") {
517 text->setString("Hello");
518 }
519 else{
520 text->setString("BMFont");
521 }
522 }
523 break;
524
525 case Widget::TouchEventType::CANCELED:
526 _displayValueLabel->setString(StringUtils::format("Touch Cancelled"));
527 break;
528
529 default:
530 break;
531 }
532}
533
534
535// UIButtonTest_RemoveSelf
537: _displayValueLabel(nullptr)
538{
539
540}
541
543{
544}
545
547{
548 if (UIScene::init())
549 {
550 Size widgetSize = _widget->getContentSize();
551
552 // Add a label in which the button events will be displayed
553 _displayValueLabel = Text::create("No Event", "fonts/Marker Felt.ttf",32);
554 _displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
555 _displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
556 _uiLayer->addChild(_displayValueLabel);
557
558 // Add the alert
559 Text* alert = Text::create("Remove Self in the Button's Callback shouldn't cause crash!","fonts/Marker Felt.ttf",10);
560 alert->setColor(Color3B(159, 168, 176));
561
562 alert->setPosition(Vec2(widgetSize.width / 2.0f,
563 widgetSize.height / 2.0f - alert->getContentSize().height * 2.75f));
564
565 _uiLayer->addChild(alert);
566
567 Layout *layout = Layout::create();
568 layout->setContentSize(widgetSize * 0.6f);
569 layout->setBackGroundColor(Color3B::GREEN);
570 layout->setBackGroundColorType(Layout::BackGroundColorType::SOLID);
571 layout->setBackGroundColorOpacity(100);
572 layout->setPosition(Size(widgetSize.width/2, widgetSize.height/2));
573 layout->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
574 layout->setTag(12);
575 _uiLayer->addChild(layout);
576
577 // Create the button
578 Button* button = Button::create("cocosui/animationbuttonnormal.png",
579 "cocosui/animationbuttonpressed.png");
580 button->setPosition(Vec2(layout->getContentSize().width / 2.0f, layout->getContentSize().height / 2.0f));
581 // button->addTouchEventListener(this, toucheventselector(UIButtonTest::touchEvent));
582 button->addTouchEventListener(CC_CALLBACK_2(UIButtonTestRemoveSelf::touchEvent, this));
583 layout->addChild(button);
584
585
586
587 return true;
588 }
589 return false;
590}
591
592void UIButtonTestRemoveSelf::touchEvent(Ref *pSender, Widget::TouchEventType type)
593{
594 switch (type)
595 {
596 case Widget::TouchEventType::BEGAN:
597 _displayValueLabel->setString(StringUtils::format("Touch Down"));
598 break;
599
600 case Widget::TouchEventType::MOVED:
601 _displayValueLabel->setString(StringUtils::format("Touch Move"));
602 break;
603
604 case Widget::TouchEventType::ENDED:
605 {
606 _displayValueLabel->setString(StringUtils::format("Touch Up"));
607 auto layout = _uiLayer->getChildByTag(12);
608 layout->removeFromParentAndCleanup(true);
609 }
610 break;
611
612 case Widget::TouchEventType::CANCELED:
613 _displayValueLabel->setString(StringUtils::format("Touch Cancelled"));
614 break;
615
616 default:
617 break;
618 }
619}
620
621// UIButtonTestSwitchScale9
623: _displayValueLabel(nullptr)
624{
625
626}
627
629{
630}
631
633{
634 if (UIScene::init())
635 {
636 Size widgetSize = _widget->getContentSize();
637
638 // Add a label in which the button events will be displayed
639 _displayValueLabel = Text::create("No Event", "fonts/Marker Felt.ttf",32);
640 _displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
641 _displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
642 _uiLayer->addChild(_displayValueLabel);
643
644
645 // Create the button
646 Button* button = Button::create("cocosui/animationbuttonnormal.png",
647 "cocosui/animationbuttonpressed.png");
648 button->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
649 button->addTouchEventListener(CC_CALLBACK_2(UIButtonTestSwitchScale9::touchEvent, this));
650 button->setTitleText("Button Title");
651 button->ignoreContentAdaptWithSize(false);
652
653 _uiLayer->addChild(button);
654
655
656
657 return true;
658 }
659 return false;
660}
661
662void UIButtonTestSwitchScale9::touchEvent(Ref *pSender, Widget::TouchEventType type)
663{
664 switch (type)
665 {
666 case Widget::TouchEventType::BEGAN:
667 _displayValueLabel->setString(StringUtils::format("Touch Down"));
668 break;
669
670 case Widget::TouchEventType::MOVED:
671 _displayValueLabel->setString(StringUtils::format("Touch Move"));
672 break;
673
674 case Widget::TouchEventType::ENDED:
675 {
676 _displayValueLabel->setString(StringUtils::format("Touch Up"));
677 auto btn = ((Button*)pSender);
678 btn->setScale9Enabled(!btn->isScale9Enabled());
679 btn->setContentSize(Size(200.0f,100.0f));
680 }
681 break;
682
683 case Widget::TouchEventType::CANCELED:
684 _displayValueLabel->setString(StringUtils::format("Touch Cancelled"));
685 break;
686
687 default:
688 break;
689 }
690}
691
692
693// UIButtonTestZoomScale
695: _displayValueLabel(nullptr)
696{
697
698}
699
701{
702}
703
705{
706 if (UIScene::init())
707 {
708 Size widgetSize = _widget->getContentSize();
709
710 // Add a label in which the button events will be displayed
711 _displayValueLabel = Text::create("Zoom Scale: -0.5", "fonts/Marker Felt.ttf",32);
712 _displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
713 _displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f + 20));
714 _uiLayer->addChild(_displayValueLabel);
715
716
717 // Create the button
718 Button* button = Button::create("cocosui/animationbuttonnormal.png",
719 "cocosui/animationbuttonpressed.png");
720 button->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f + 20));
721 button->setPressedActionEnabled(true);
722 button->addClickEventListener([=](Ref* sender){
723 CCLOG("Button clicked, position = (%f, %f)", button->getPosition().x,
724 button->getPosition().y);
725
726 });
727 button->setName("button");
728 _uiLayer->addChild(button);
729 button->setZoomScale(-0.5);
730
731 Slider* slider = Slider::create();
732 slider->loadBarTexture("cocosui/sliderTrack.png");
733 slider->loadSlidBallTextures("cocosui/sliderThumb.png", "cocosui/sliderThumb.png", "");
734 slider->loadProgressBarTexture("cocosui/sliderProgress.png");
735 slider->setPosition(Vec2(widgetSize.width / 2.0f , widgetSize.height / 2.0f - 20));
736 slider->addEventListener(CC_CALLBACK_2(UIButtonTestZoomScale::sliderEvent, this));
737 slider->setPercent(button->getZoomScale()*100);
738 _uiLayer->addChild(slider);
739 return true;
740 }
741 return false;
742}
743
744void UIButtonTestZoomScale::sliderEvent(Ref *pSender, Slider::EventType type)
745{
746 if (type == Slider::EventType::ON_PERCENTAGE_CHANGED)
747 {
748 Slider* slider = dynamic_cast<Slider*>(pSender);
749 int percent = slider->getPercent();
750 Button* btn = (Button*)_uiLayer->getChildByName("button");
751 float zoomScale = percent * 0.01;
752 btn->setZoomScale(zoomScale);
753 _displayValueLabel->setString(StringUtils::format("Zoom Scale: %f", zoomScale));
754 }
755}
756
757// UIButtonTextOnly
759: _displayValueLabel(nullptr)
760{
761
762}
763
765{
766}
767
769{
770 if (UIScene::init())
771 {
772 Size widgetSize = _widget->getContentSize();
773
774 // Add a label in which the button events will be displayed
775 _displayValueLabel = Text::create("Text Only Button", "fonts/Marker Felt.ttf",32);
776 _displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
777 _displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f + 20));
778 _uiLayer->addChild(_displayValueLabel);
779
780
781 // Create the button
782 auto button = Button::create();
783 button->setPositionNormalized(Vec2(0.5f, 0.5f));
784
785 button->setTitleText("PLAY GAME");
786 CCLOG("content size should be greater than 0: width = %f, height = %f", button->getContentSize().width,
787 button->getContentSize().height);
788 button->setZoomScale(0.3f);
789 button->setPressedActionEnabled(true);
790 button->addClickEventListener([](Ref* sender) {
791 CCLOG("clicked!");
792 });
793 _uiLayer->addChild(button);
794
795 return true;
796 }
797 return false;
798}
799
800// UIButtonIgnoreContentSizeTest
802: _displayValueLabel(nullptr)
803{
804
805}
806
808{
809}
810
812{
813 if (UIScene::init())
814 {
815 Size widgetSize = _widget->getContentSize();
816
817 // Add a label in which the button events will be displayed
818 _displayValueLabel = Text::create("Button IgnoreContent Size Test", "fonts/Marker Felt.ttf",32);
819 _displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
820 _displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f + 20));
821 _uiLayer->addChild(_displayValueLabel);
822
823
824 // Create the button
825 auto button = Button::create("cocosui/animationbuttonnormal.png",
826 "cocosui/animationbuttonpressed.png");
827 button->ignoreContentAdaptWithSize(false);
828 button->setContentSize(Size(200.0f,100.0f));
829 button->setPositionNormalized(Vec2(0.3f, 0.5f));
830 button->setTitleText("PLAY GAME");
831 button->setZoomScale(0.3f);
832 button->setPressedActionEnabled(true);
833 button->addClickEventListener([=](Ref* sender) {
834 CCLOG("clicked!");
835 button->setScale(1.2f);
836 });
837 _uiLayer->addChild(button);
838
839 // Create the button
840 auto button2 = Button::create("cocosui/animationbuttonnormal.png",
841 "cocosui/animationbuttonpressed.png");
842 button2->ignoreContentAdaptWithSize(false);
843 button2->setContentSize(Size(200.0f,100.0f));
844 button2->setPositionNormalized(Vec2(0.8f, 0.5f));
845 button2->setTitleText("PLAY GAME");
846 button2->setZoomScale(0.3f);
847 button2->setPressedActionEnabled(true);
848 button2->addClickEventListener([=](Ref* sender) {
849 button2->runAction(ScaleTo::create(1.0f, 1.2f));
850 CCLOG("clicked!");
851 });
852 _uiLayer->addChild(button2);
853
854 return true;
855 }
856 return false;
857}
858
859
860// UIButtonTitleEffectTest
862: _displayValueLabel(nullptr)
863{
864
865}
866
868{
869}
870
872{
873 if (UIScene::init())
874 {
875 Size widgetSize = _widget->getContentSize();
876
877 // Add a label in which the button events will be displayed
878 _displayValueLabel = Text::create("Button Title Effect", "fonts/Marker Felt.ttf",32);
879 _displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
880 _displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f + 20));
881 _uiLayer->addChild(_displayValueLabel);
882
883
884 // Create the button
885 auto button = Button::create("cocosui/animationbuttonnormal.png",
886 "cocosui/animationbuttonpressed.png");
887 button->setPositionNormalized(Vec2(0.3f, 0.5f));
888 button->setTitleText("PLAY GAME");
889 button->setTitleFontName("fonts/Marker Felt.ttf");
890 button->setZoomScale(0.3f);
891 button->setScale(2.0f);
892 button->setPressedActionEnabled(true);
893 Label *title = button->getTitleRenderer();
894 button->setTitleColor(Color3B::RED);
895 title->enableShadow(Color4B::BLACK,Size(2,-2));
896
897
898 _uiLayer->addChild(button);
899
900 // Create the button
901 auto button2 = Button::create("cocosui/animationbuttonnormal.png",
902 "cocosui/animationbuttonpressed.png");
903 button2->setPositionNormalized(Vec2(0.8f, 0.5f));
904 button2->setTitleText("PLAY GAME");
905 auto title2 = button2->getTitleRenderer();
906 title2->enableOutline(Color4B::GREEN, 3);
907 _uiLayer->addChild(button2);
908
909 return true;
910 }
911 return false;
912}
913// UIButtonFlipTest
915: _displayValueLabel(nullptr)
916{
917
918}
919
921{
922}
923
925{
926 if (UIScene::init())
927 {
928 Size widgetSize = _widget->getContentSize();
929
930 // Add a label in which the button events will be displayed
931 _displayValueLabel = Text::create("Button X Flipped", "fonts/Marker Felt.ttf",20);
932 _displayValueLabel->setPositionNormalized(Vec2(0.3f, 0.7f));
933 _uiLayer->addChild(_displayValueLabel);
934
935
936 // Create the button
937 auto button = Button::create("cocosui/animationbuttonnormal.png",
938 "cocosui/animationbuttonpressed.png");
939 button->setPositionNormalized(Vec2(0.3f, 0.5f));
940 button->setTitleText("PLAY GAME");
941 button->setTitleFontName("fonts/Marker Felt.ttf");
942 button->setZoomScale(0.3f);
943 button->setScale(2.0f);
944 button->setFlippedX(true);
945 button->setPressedActionEnabled(true);
946
947
948 _uiLayer->addChild(button);
949
950 // Create the button
951 auto button2 = Button::create("cocosui/animationbuttonnormal.png",
952 "cocosui/animationbuttonpressed.png");
953 button2->setPositionNormalized(Vec2(0.8f, 0.5f));
954 button2->setTitleText("PLAY GAME");
955 button2->setFlippedY(true);
956 _uiLayer->addChild(button2);
957
958 auto titleLabel = Text::create("Button Y flipped", "Arial", 20);
959 titleLabel->setPositionNormalized(Vec2(0.8f, 0.7f));
960 this->addChild(titleLabel);
961
962 return true;
963 }
964 return false;
965}
966
967// UIButtonNormalDefaultTest
969: _displayValueLabel(nullptr)
970{
971
972}
973
975{
976}
977
979{
980 if (UIScene::init())
981 {
982 Size widgetSize = _widget->getContentSize();
983
984 // Add a label in which the button events will be displayed
985 _displayValueLabel = Text::create("", "fonts/Marker Felt.ttf",32);
986 _displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
987 _displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
988 _uiLayer->addChild(_displayValueLabel);
989
990 // Add the alert
991 Text* alert = Text::create("Button should scale when clicked","fonts/Marker Felt.ttf",20);
992 alert->setColor(Color3B(159, 168, 176));
993
994 alert->setPosition(Vec2(widgetSize.width / 2.0f,
995 widgetSize.height / 2.0f - alert->getContentSize().height * 1.75f));
996
997 _uiLayer->addChild(alert);
998
999 // Create the button
1000 Button* button = Button::create("cocosui/animationbuttonnormal.png");
1001 button->setPosition(Vec2(widgetSize.width / 2.0f - 80, widgetSize.height / 2.0f + 40));
1002 button->setZoomScale(0.4f);
1003 button->setPressedActionEnabled(true);
1004 _uiLayer->addChild(button);
1005
1006 // Create the button
1007 Button* buttonScale9 = Button::create("cocosui/button.png");
1008 // open scale9 render
1009 buttonScale9->setScale9Enabled(true);
1010 buttonScale9->setPosition(Vec2(widgetSize.width / 2.0f + 50, widgetSize.height / 2.0f + 40));
1011 buttonScale9->setContentSize(Size(150.0f, 70.0f));
1012 buttonScale9->setPressedActionEnabled(true);
1013 _uiLayer->addChild(buttonScale9);
1014
1015
1016
1017
1018 return true;
1019 }
1020 return false;
1021}
1022
1023
1024// UIButtonDisableDefaultTest
1026: _displayValueLabel(nullptr)
1027{
1028
1029}
1030
1032{
1033}
1034
1036{
1037 if (UIScene::init())
1038 {
1039 Size widgetSize = _widget->getContentSize();
1040
1041 // Add a label in which the button events will be displayed
1042 _displayValueLabel = Text::create("", "fonts/Marker Felt.ttf",32);
1043 _displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
1044 _displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
1045 _uiLayer->addChild(_displayValueLabel);
1046
1047 // Add the alert
1048 Text* alert = Text::create("Left button will turn normal when clicked","fonts/Marker Felt.ttf",20);
1049 alert->setColor(Color3B(159, 168, 176));
1050
1051 alert->setPosition(Vec2(widgetSize.width / 2.0f,
1052 widgetSize.height / 2.0f - alert->getContentSize().height * 1.75f));
1053
1054 _uiLayer->addChild(alert);
1055
1056 // Create the button
1057 Button* button = Button::create("cocosui/animationbuttonnormal.png");
1058 button->setPosition(Vec2(widgetSize.width / 2.0f - 80, widgetSize.height / 2.0f + 40));
1059 button->setZoomScale(0.4f);
1060 button->setPressedActionEnabled(true);
1061 button->setBright(false);
1062 button->setScale9Enabled(true);
1063 button->setCapInsets(Rect(3,3,5,5));
1064 button->addClickEventListener([=](Ref*){
1065 button->setBright(true);
1066 });
1067 _uiLayer->addChild(button);
1068
1069 // Create the button
1070 Button* buttonScale9 = Button::create("cocosui/button.png");
1071 // open scale9 render
1072 buttonScale9->setScale9Enabled(true);
1073 buttonScale9->setPosition(Vec2(widgetSize.width / 2.0f + 50, widgetSize.height / 2.0f + 40));
1074 buttonScale9->setContentSize(Size(150.0f, 70.0f));
1075 buttonScale9->setPressedActionEnabled(true);
1076 buttonScale9->setEnabled(false);
1077 buttonScale9->setBright(false);
1078 _uiLayer->addChild(buttonScale9);
1079
1080
1081
1082
1083 return true;
1084 }
1085 return false;
1086}
1087
1088// UIButtonCloneTest
1090: _displayValueLabel(nullptr)
1091{
1092
1093}
1094
1096{
1097}
1098
1100{
1101 if (UIScene::init())
1102 {
1103 Size widgetSize = _widget->getContentSize();
1104
1105 // Add a label in which the button events will be displayed
1106 _displayValueLabel = Text::create("", "fonts/Marker Felt.ttf",32);
1107 _displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
1108 _displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f,
1109 widgetSize.height / 2.0f));
1110 _uiLayer->addChild(_displayValueLabel);
1111
1112 // Add the alert
1113 Text* alert = Text::create("This test case shouldn't trigger the Assertion!",
1114 "fonts/Marker Felt.ttf",20);
1115 alert->setColor(Color3B(159, 168, 176));
1116
1117 alert->setPosition(Vec2(widgetSize.width / 2.0f,
1118 widgetSize.height / 2.0f
1119 - alert->getContentSize().height * 1.75f));
1120
1121 _uiLayer->addChild(alert);
1122
1123 // Create the button
1124 Button* button = Button::create("cocosui/animationbuttonnormal.png");
1125 button->setPosition(Vec2(widgetSize.width / 2.0f - 80,
1126 widgetSize.height / 2.0f + 40));
1127 _uiLayer->addChild(button);
1128
1129
1130 CCASSERT(button->getTitleRenderer() == nullptr,
1131 "Button title render must be nullptr ");
1132
1133 auto buttonCopy = (Button*)button->clone();
1134 buttonCopy->setPosition(Vec2(widgetSize.width / 2.0f + 80,
1135 widgetSize.height / 2.0f + 40));
1136 this->addChild(buttonCopy);
1137
1138
1139
1140 auto buttonScale9Copy = (Button*)button->clone();
1141 buttonScale9Copy->setPosition(button->getPosition() + Vec2(0.0f, -60.0f));
1142 buttonScale9Copy->setScale9Enabled(true);
1143 buttonScale9Copy->setContentSize(button->getContentSize() * 1.5f);
1144 this->addChild(buttonScale9Copy);
1145
1146
1147 auto buttonScale9Copy2 = (Button*)buttonScale9Copy->clone();
1148 buttonScale9Copy2->setPosition(buttonCopy->getPosition() + Vec2(0.0f, -60.0f));
1149 buttonScale9Copy2->setScale9Enabled(true);
1150 buttonScale9Copy2->setContentSize(buttonCopy->getContentSize() * 1.5f);
1151 this->addChild(buttonScale9Copy2);
1152
1153
1154 CCASSERT(button->getTitleRenderer() == nullptr,
1155 "Original Button title render must be nullptr ");
1156
1157 CCASSERT(buttonCopy->getTitleRenderer() == nullptr,
1158 "Copied Button title render must be nullptr ");
1159
1160 return true;
1161 }
1162 return false;
1163}
1164
1165// https://github.com/cocos2d/cocos2d-x/issues/12249
1167: _displayValueLabel(nullptr)
1168{
1169
1170}
1171
1173{
1174 if (UIScene::init())
1175 {
1176 Size widgetSize = _widget->getContentSize();
1177
1178 // Add a label in which the button events will be displayed
1179 _displayValueLabel = Text::create("Test Issue 12249", "fonts/Marker Felt.ttf",32);
1180 _displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
1181 _displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f,
1182 widgetSize.height / 2.0f));
1183 _uiLayer->addChild(_displayValueLabel);
1184
1185 // Add the alert
1186 Text* alert = Text::create("This test case two buttons should seem to be same!",
1187 "fonts/Marker Felt.ttf",20);
1188 alert->setColor(Color3B(159, 168, 176));
1189
1190 alert->setPosition(Vec2(widgetSize.width / 2.0f,
1191 widgetSize.height / 2.0f
1192 - alert->getContentSize().height * 2.0f));
1193
1194 _uiLayer->addChild(alert);
1195
1196 // Create the button 1
1197 Button* button = nullptr, *button2 = nullptr;
1198 float btnWidth = 100;
1199 button = Button::create("cocosui/button.png", "cocosui/buttonHighlighted.png");
1200 button->setScale9Enabled(true);
1201 button->setContentSize(Size(btnWidth, button->getContentSize().height));
1202 button->setTitleText("Scale9 Button 1");
1203 button->setPosition(Vec2(widgetSize.width / 2.0f - btnWidth, widgetSize.height / 2.0f));
1204 _uiLayer->addChild(button);
1205
1206 // create button 2, load texture after button creation
1207 button2 = Button::create();
1208 button2->setScale9Enabled(true);
1209 button2->loadTextures("cocosui/button.png", "cocosui/buttonHighlighted.png", "");
1210 button2->setContentSize(Size(btnWidth, button2->getContentSize().height));
1211 button2->setTitleText("Scale9 Button 2");
1212 button2->setPosition(Vec2(widgetSize.width / 2.0f + btnWidth, widgetSize.height / 2.0f));
1213 _uiLayer->addChild(button2);
1214
1215 return true;
1216 }
1217 return false;
1218}
1219
1220// https://github.com/cocos2d/cocos2d-x/issues/17116
1222{
1223}
1224
1226{
1227 if (UIScene::init())
1228 {
1229 auto visibleSize = Director::getInstance()->getVisibleSize();
1230
1231 SpriteFrameCache::getInstance()->addSpriteFramesWithFile("Images/issue_17116.plist");
1232 auto button = ui::Button::create();
1233 button->loadTextureNormal("buttons/play-big", ui::Widget::TextureResType::PLIST);
1234 button->setPosition(Vec2(visibleSize.width/2, visibleSize.height/2));
1235 button->setOpacity(100);
1236 addChild(button);
1237 return true;
1238 }
1239 return false;
1240}
1241
1243{
1244}
1245
1247{
1248 if (UIScene::init())
1249 {
1250 auto visibleSize = Director::getInstance()->getVisibleSize();
1251
1252 SpriteFrameCache::getInstance()->addSpriteFramesWithFile("Images/poly_test_textures.plist");
1253 auto button = ui::Button::create();
1254 button->loadTextureNormal("poly_test/wheel_disc_back.png", ui::Widget::TextureResType::PLIST);
1255 button->setPosition(Vec2(visibleSize.width/2 - 100, visibleSize.height/2));
1256 button->setScale(0.5);
1257 button->setScale9Enabled(true);
1258 addChild(button);
1259
1260 auto buttonCopy = button->clone();
1261 button->setPosition(Vec2(visibleSize.width/2 + 100, visibleSize.height/2));
1262 addChild(buttonCopy);
1263
1264 return true;
1265 }
1266 return false;
1267}
1268
1270{
1271}
1272
1274{
1275 if (UIScene::init())
1276 {
1277 ImageView* sprite = ImageView::create("Images/blocks.png");
1278 sprite->setScale9Enabled(true);
1279 sprite->setCapInsets(Rect(32, 32, 32, 32));
1280 sprite->setContentSize(Size(96 * 1.5, 96));
1281 sprite->setNormalizedPosition(Vec2(0.2f, 0.5f));
1282 this->addChild(sprite);
1283
1284 auto button = Button::create("cocosui/button.png", "cocosui/buttonHighlighted.png");
1285 button->setScale9Enabled(true);
1286 button->setContentSize(Size(100.0f, 50.0f));
1287 button->setNormalizedPosition(Vec2(0.6f, 0.5f));
1288 button->setTitleText("Press me");
1289 addChild(button);
1290
1291 button->addClickEventListener([sprite](Ref* button){
1292 sprite->loadTexture("Images/blocks9.png");
1293 });
1294
1295
1296 return true;
1297 }
1298 return false;
1299}
1300
#define ADD_TEST_CASE(__className__)
Definition: BaseTest.h:211
USING_NS_CC
virtual bool init() override
cocos2d::ui::Text * _displayValueLabel
Definition: UIButtonTest.h:263
virtual bool init() override
virtual std::string title() const
Definition: BaseTest.h:59
cocos2d::ui::Text * _displayValueLabel
Definition: UIButtonTest.h:250
virtual bool init() override
virtual bool init() override
cocos2d::ui::Text * _displayValueLabel
Definition: UIButtonTest.h:236
virtual bool init() override
cocos2d::ui::Text * _displayValueLabel
Definition: UIButtonTest.h:208
cocos2d::ui::Text * _displayValueLabel
Definition: UIButtonTest.h:180
virtual bool init() override
cocos2d::ui::Text * _displayValueLabel
Definition: UIButtonTest.h:222
virtual bool init() override
virtual bool init() override
virtual bool init() override
void touchEvent(cocos2d::Ref *sender, cocos2d::ui::Widget::TouchEventType type)
cocos2d::ui::Text * _displayValueLabel
Definition: UIButtonTest.h:92
cocos2d::ui::Text * _displayValueLabel
Definition: UIButtonTest.h:77
virtual bool init() override
void touchEvent(cocos2d::Ref *sender, cocos2d::ui::Widget::TouchEventType type)
cocos2d::ui::Text * _displayValueLabel
Definition: UIButtonTest.h:62
virtual bool init() override
void touchEvent(cocos2d::Ref *sender, cocos2d::ui::Widget::TouchEventType type)
void touchEvent(cocos2d::Ref *sender, cocos2d::ui::Widget::TouchEventType type)
cocos2d::ui::Text * _displayValueLabel
Definition: UIButtonTest.h:107
virtual bool init() override
cocos2d::ui::Button * _button
Definition: UIButtonTest.h:47
virtual bool init() override
void printWidgetResources(cocos2d::Ref *sender)
void touchEvent(cocos2d::Ref *sender, cocos2d::ui::Widget::TouchEventType type)
cocos2d::ui::Text * _displayValueLabel
Definition: UIButtonTest.h:46
cocos2d::ui::Text * _displayValueLabel
Definition: UIButtonTest.h:122
void touchEvent(cocos2d::Ref *sender, cocos2d::ui::Widget::TouchEventType type)
virtual bool init() override
virtual bool init() override
void touchEvent(cocos2d::Ref *sender, cocos2d::ui::Widget::TouchEventType type)
cocos2d::ui::Text * _displayValueLabel
Definition: UIButtonTest.h:137
void sliderEvent(cocos2d::Ref *sender, cocos2d::ui::Slider::EventType type)
virtual bool init() override
cocos2d::ui::Text * _displayValueLabel
Definition: UIButtonTest.h:152
virtual bool init() override
cocos2d::ui::Text * _displayValueLabel
Definition: UIButtonTest.h:166
cocos2d::ui::Text * _displayValueLabel
Definition: UIButtonTest.h:194
virtual bool init() override
virtual bool init() 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