PuzzleSDK
UILayoutTest.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 "UILayoutTest.h"
26
28using namespace cocos2d::ui;
29
30UILayoutTests::UILayoutTests()
31{
45}
46
47// UILayoutTest
48
50{
51}
52
54{
55}
56
58{
59 if (UIScene::init())
60 {
61 Size widgetSize = _widget->getContentSize();
62
63 // Add the alert
64 Text* alert = Text::create("Layout", "fonts/Marker Felt.ttf", 30 );
65 alert->setColor(Color3B(159, 168, 176));
66 alert->setPosition(Vec2(widgetSize.width / 2.0f,
67 widgetSize.height / 2.0f - alert->getContentSize().height * 3.075f));
68
69 _uiLayer->addChild(alert);
70
71 Layout* root = static_cast<Layout*>(_uiLayer->getChildByTag(81)) ;
72
73 Layout* background = static_cast<Layout*>(root->getChildByName("background_Panel"));
74
75 // Create the layout
76 Layout* layout = Layout::create();
77 layout->setContentSize(Size(280, 150));
78 Size backgroundSize = background->getContentSize();
79 layout->setPosition(Vec2((widgetSize.width - backgroundSize.width) / 2.0f +
80 (backgroundSize.width - layout->getContentSize().width) / 2.0f,
81 (widgetSize.height - backgroundSize.height) / 2.0f +
82 (backgroundSize.height - layout->getContentSize().height) / 2.0f));
83 _uiLayer->addChild(layout);
84
85 Button* button = Button::create("cocosui/animationbuttonnormal.png", "cocosui/animationbuttonpressed.png");
86 button->setPosition(Vec2(button->getContentSize().width / 2.0f,
87 layout->getContentSize().height - button->getContentSize().height / 2.0f));
88 layout->addChild(button);
89
90 Button* titleButton = Button::create("cocosui/backtotopnormal.png", "cocosui/backtotoppressed.png");
91 titleButton->setTitleText("Title Button");
92 titleButton->setPosition(Vec2(layout->getContentSize().width / 2.0f, layout->getContentSize().height / 2.0f));
93 layout->addChild(titleButton);
94
95 Button* button_scale9 = Button::create("cocosui/button.png", "cocosui/buttonHighlighted.png");
96 button_scale9->setScale9Enabled(true);
97 button_scale9->setContentSize(Size(100.0f, button_scale9->getVirtualRendererSize().height));
98 button_scale9->setPosition(Vec2(layout->getContentSize().width - button_scale9->getContentSize().width / 2.0f,
99 button_scale9->getContentSize().height / 2.0f));
100
101 layout->addChild(button_scale9);
102
103 return true;
104 }
105
106 return false;
107}
108
109// UILayoutTest_Color
110
112{
113}
114
116{
117}
118
120{
121 if (UIScene::init())
122 {
123 Size widgetSize = _widget->getContentSize();
124
125 // Add the alert
126 Text* alert = Text::create("Layout color render", "fonts/Marker Felt.ttf", 30);
127 alert->setColor(Color3B(159, 168, 176));
128 alert->setPosition(Vec2(widgetSize.width / 2.0f,
129 widgetSize.height / 2.0f - alert->getContentSize().height * 3.075f));
130
131 _uiLayer->addChild(alert);
132
133 Layout* root = static_cast<Layout*>(_uiLayer->getChildByTag(81));
134
135 Layout* background = static_cast<Layout*>(root->getChildByName("background_Panel"));
136
137 // Create the layout with color render
138 Layout* layout = Layout::create();
139 layout->setBackGroundColorType(Layout::BackGroundColorType::SOLID);
140 layout->setBackGroundColor(Color3B(128, 128, 128));
141 layout->setContentSize(Size(280, 150));
142 Size backgroundSize = background->getContentSize();
143 layout->setPosition(Vec2((widgetSize.width - backgroundSize.width) / 2.0f +
144 (backgroundSize.width - layout->getContentSize().width) / 2.0f,
145 (widgetSize.height - backgroundSize.height) / 2.0f +
146 (backgroundSize.height - layout->getContentSize().height) / 2.0f));
147 _uiLayer->addChild(layout);
148
149 Button* button = Button::create("cocosui/animationbuttonnormal.png", "cocosui/animationbuttonpressed.png");
150 button->setPosition(Vec2(button->getContentSize().width / 2.0f,
151 layout->getContentSize().height - button->getContentSize().height / 2.0f));
152
153 layout->addChild(button);
154
155 Button* titleButton = Button::create("cocosui/backtotopnormal.png", "cocosui/backtotoppressed.png");
156 titleButton->setTitleText("Title Button");
157 titleButton->setPosition(Vec2(layout->getContentSize().width / 2.0f, layout->getContentSize().height / 2.0f));
158 layout->addChild(titleButton);
159
160 Button* button_scale9 = Button::create("cocosui/button.png", "cocosui/buttonHighlighted.png");
161 button_scale9->setScale9Enabled(true);
162 button_scale9->setContentSize(Size(100.0f, button_scale9->getVirtualRendererSize().height));
163 button_scale9->setPosition(Vec2(layout->getContentSize().width - button_scale9->getContentSize().width / 2.0f,
164 button_scale9->getContentSize().height / 2.0f));
165
166 layout->addChild(button_scale9);
167
168 return true;
169 }
170 return false;
171}
172
173// UILayoutTest_Gradient
174
176{
177}
178
180{
181}
182
184{
185 if (UIScene::init())
186 {
187 Size widgetSize = _widget->getContentSize();
188
189 // Add the alert
190 Text* alert = Text::create("Layout gradient render", "fonts/Marker Felt.ttf", 30);
191 alert->setColor(Color3B(159, 168, 176));
192 alert->setPosition(Vec2(widgetSize.width / 2.0f,
193 widgetSize.height / 2.0f - alert->getContentSize().height * 3.075f));
194
195 _uiLayer->addChild(alert);
196
197 Layout* root = static_cast<Layout*>(_uiLayer->getChildByTag(81));
198
199 Layout* background = static_cast<Layout*>(root->getChildByName("background_Panel"));
200
201 // Create the layout with gradient render
202 Layout* layout = Layout::create();
203 layout->setBackGroundColorType(Layout::BackGroundColorType::GRADIENT);
204 layout->setBackGroundColor(Color3B(64, 64, 64), Color3B(192, 192, 192));
205 layout->setContentSize(Size(280, 150));
206 Size backgroundSize = background->getContentSize();
207 layout->setPosition(Vec2((widgetSize.width - backgroundSize.width) / 2.0f +
208 (backgroundSize.width - layout->getContentSize().width) / 2.0f,
209 (widgetSize.height - backgroundSize.height) / 2.0f +
210 (backgroundSize.height - layout->getContentSize().height) / 2.0f));
211 _uiLayer->addChild(layout);
212
213 Button* button = Button::create("cocosui/animationbuttonnormal.png", "cocosui/animationbuttonpressed.png");
214 button->setPosition(Vec2(button->getContentSize().width / 2.0f,
215 layout->getContentSize().height - button->getContentSize().height / 2.0f));
216
217 layout->addChild(button);
218
219 Button* titleButton = Button::create("cocosui/backtotopnormal.png", "cocosui/backtotoppressed.png");
220 titleButton->setTitleText("Title Button");
221 titleButton->setPosition(Vec2(layout->getContentSize().width / 2.0f, layout->getContentSize().height / 2.0f));
222 layout->addChild(titleButton);
223
224 Button* button_scale9 = Button::create("cocosui/button.png", "cocosui/buttonHighlighted.png");
225 button_scale9->setScale9Enabled(true);
226 button_scale9->setContentSize(Size(100.0f, button_scale9->getVirtualRendererSize().height));
227 button_scale9->setPosition(Vec2(layout->getContentSize().width - button_scale9->getContentSize().width / 2.0f,
228 button_scale9->getContentSize().height / 2.0f));
229
230 layout->addChild(button_scale9);
231
232 return true;
233 }
234 return false;
235}
236
237// UILayoutTest_BackGroundImage
238
240{
241}
242
244{
245}
246
248{
249 if (UIScene::init())
250 {
251 Size widgetSize = _widget->getContentSize();
252
253 // Add the alert
254 Text* alert = Text::create("Layout background image", "fonts/Marker Felt.ttf", 20);
255 alert->setColor(Color3B(159, 168, 176));
256 alert->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 4.5f));
257 _uiLayer->addChild(alert);
258
259 Layout* root = static_cast<Layout*>(_uiLayer->getChildByTag(81));
260
261 Layout* background = dynamic_cast<Layout*>(root->getChildByName("background_Panel"));
262
263 // Create the layout with background image
264 Layout* layout = Layout::create();
265 layout->setClippingEnabled(true);
266 layout->setBackGroundImage("cocosui/Hello.png");
267 layout->setContentSize(Size(280, 150));
268 Size backgroundSize = background->getContentSize();
269 layout->setPosition(Vec2((widgetSize.width - backgroundSize.width) / 2.0f +
270 (backgroundSize.width - layout->getContentSize().width) / 2.0f,
271 (widgetSize.height - backgroundSize.height) / 2.0f +
272 (backgroundSize.height - layout->getContentSize().height) / 2.0f));
273 _uiLayer->addChild(layout);
274
275 Button* button = Button::create("cocosui/animationbuttonnormal.png", "cocosui/animationbuttonpressed.png");
276 button->setPosition(Vec2(button->getContentSize().width / 2.0f,
277 layout->getContentSize().height - button->getContentSize().height / 2.0f));
278 layout->addChild(button);
279
280 Button* titleButton = Button::create("cocosui/backtotopnormal.png", "cocosui/backtotoppressed.png");
281 titleButton->setTitleText("Title Button");
282 titleButton->setPosition(Vec2(layout->getContentSize().width / 2.0f, layout->getContentSize().height / 2.0f));
283 layout->addChild(titleButton);
284
285 Button* button_scale9 = Button::create("cocosui/button.png", "cocosui/buttonHighlighted.png");
286 button_scale9->setScale9Enabled(true);
287 button_scale9->setContentSize(Size(100.0f, button_scale9->getVirtualRendererSize().height));
288 button_scale9->setPosition(Vec2(layout->getContentSize().width - button_scale9->getContentSize().width / 2.0f,
289 button_scale9->getContentSize().height / 2.0f));
290
291 layout->addChild(button_scale9);
292
293 _layout = layout;
294
295 TTFConfig ttfConfig("fonts/arial.ttf", 15);
296 auto label1 = Label::createWithTTF(ttfConfig, "Print Resources");
297 auto item1 = MenuItemLabel::create(label1, CC_CALLBACK_1(UILayoutTest_BackGroundImage::printWidgetResources, this));
298 item1->setPosition(Vec2(VisibleRect::left().x + 60, VisibleRect::bottom().y + item1->getContentSize().height * 3));
299 auto pMenu1 = Menu::create(item1, nullptr);
300 pMenu1->setPosition(Vec2(0, 0));
301 this->addChild(pMenu1, 10);
302
303 return true;
304 }
305 return false;
306}
307
309{
310 cocos2d::ResourceData textureFile = _layout->getRenderFile();
311 CCLOG("textureFile Name : %s, Type: %d", textureFile.file.c_str(), textureFile.type);
312}
313
314// UILayoutTest_BackGroundImage_Scale9
315
317{
318}
319
321{
322}
323
325{
326 if (UIScene::init())
327 {
328 Size widgetSize = _widget->getContentSize();
329
330 // Add the alert
331 Text* alert = Text::create("Layout background image scale9", "fonts/Marker Felt.ttf", 20);
332 alert->setColor(Color3B(159, 168, 176));
333 alert->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 4.5f));
334 _uiLayer->addChild(alert);
335
336 Layout* root = static_cast<Layout*>(_uiLayer->getChildByTag(81));
337
338 Layout* background = dynamic_cast<Layout*>(root->getChildByName("background_Panel"));
339
340 // Create the layout with background image
341 Layout* layout = Layout::create();
342 layout->setBackGroundImageScale9Enabled(true);
343 layout->setBackGroundImage("cocosui/green_edit.png");
344 layout->setContentSize(Size(280, 150));
345 Size backgroundSize = background->getContentSize();
346 layout->setPosition(Vec2((widgetSize.width - backgroundSize.width) / 2.0f +
347 (backgroundSize.width - layout->getContentSize().width) / 2.0f,
348 (widgetSize.height - backgroundSize.height) / 2.0f +
349 (backgroundSize.height - layout->getContentSize().height) / 2.0f));
350 _uiLayer->addChild(layout);
351
352 Button* button = Button::create("cocosui/animationbuttonnormal.png", "cocosui/animationbuttonpressed.png");
353 button->setPosition(Vec2(button->getContentSize().width / 2.0f,
354 layout->getContentSize().height - button->getContentSize().height / 2.0f));
355
356 layout->addChild(button);
357
358 Button* titleButton = Button::create("cocosui/backtotopnormal.png", "cocosui/backtotoppressed.png");
359 titleButton->setTitleText("Title Button");
360 titleButton->setPosition(Vec2(layout->getContentSize().width / 2.0f, layout->getContentSize().height / 2.0f));
361 layout->addChild(titleButton);
362
363 Button* button_scale9 = Button::create("cocosui/button.png", "cocosui/buttonHighlighted.png");
364 button_scale9->setScale9Enabled(true);
365 button_scale9->setContentSize(Size(100.0f, button_scale9->getVirtualRendererSize().height));
366 button_scale9->setPosition(Vec2(layout->getContentSize().width - button_scale9->getContentSize().width / 2.0f,
367 button_scale9->getContentSize().height / 2.0f));
368 layout->addChild(button_scale9);
369
370 return true;
371 }
372 return false;
373}
374
375// UILayoutTest_Layout_Linear_Vertical
376
378{
379}
380
382{
383}
384
386{
387 if (UIScene::init())
388 {
389 Size widgetSize = _widget->getContentSize();
390
391 // Add the alert
392 Text* alert = Text::create("Layout Linear Vertical", "fonts/Marker Felt.ttf", 20);
393 alert->setColor(Color3B(159, 168, 176));
394 alert->setPosition(Vec2(widgetSize.width / 2.0f,
395 widgetSize.height / 2.0f - alert->getContentSize().height * 4.5f));
396
397 _uiLayer->addChild(alert);
398
399 Layout* root = static_cast<Layout*>(_uiLayer->getChildByTag(81));
400
401 Layout* background = static_cast<Layout*>(root->getChildByName("background_Panel"));
402
403 // Create the layout
404 Layout* layout = Layout::create();
405 layout->setLayoutType(Layout::Type::VERTICAL);
406 layout->setContentSize(Size(280, 150));
407 Size backgroundSize = background->getContentSize();
408 layout->setPosition(Vec2((widgetSize.width - backgroundSize.width) / 2.0f +
409 (backgroundSize.width - layout->getContentSize().width) / 2.0f,
410 (widgetSize.height - backgroundSize.height) / 2.0f +
411 (backgroundSize.height - layout->getContentSize().height) / 2.0f));
412 _uiLayer->addChild(layout);
413
414
415 Button* button = Button::create("cocosui/animationbuttonnormal.png", "cocosui/animationbuttonpressed.png");
416 layout->addChild(button);
417
418 LinearLayoutParameter* lp1 = LinearLayoutParameter::create();
419 button->setLayoutParameter(lp1);
420 lp1->setGravity(LinearLayoutParameter::LinearGravity::CENTER_HORIZONTAL);
421 lp1->setMargin(Margin(0.0f, 5.0f, 0.0f, 10.0f));
422
423
424 Button* titleButton = Button::create("cocosui/backtotopnormal.png", "cocosui/backtotoppressed.png");
425 titleButton->setTitleText("Title Button");
426 layout->addChild(titleButton);
427
428 LinearLayoutParameter* lp2 = LinearLayoutParameter::create();
429 titleButton->setLayoutParameter(lp2);
430 lp2->setGravity(LinearLayoutParameter::LinearGravity::CENTER_HORIZONTAL);
431 lp2->setMargin(Margin(0.0f, 10.0f, 0.0f, 10.0f));
432
433
434 Button* button_scale9 = Button::create("cocosui/button.png", "cocosui/buttonHighlighted.png");
435 button_scale9->setScale9Enabled(true);
436 button_scale9->setContentSize(Size(100.0f, button_scale9->getVirtualRendererSize().height));
437 layout->addChild(button_scale9);
438
439 LinearLayoutParameter* lp3 = LinearLayoutParameter::create();
440 button_scale9->setLayoutParameter(lp3);
441 lp3->setGravity(LinearLayoutParameter::LinearGravity::CENTER_HORIZONTAL);
442 lp3->setMargin(Margin(0.0f, 10.0f, 0.0f, 10.0f));
443
444
445 return true;
446 }
447
448 return false;
449}
450
451// UILayoutTest_Layout_Linear_Horizontal
452
454{
455}
456
458{
459}
460
462{
463 if (UIScene::init())
464 {
465 Size widgetSize = _widget->getContentSize();
466
467 // Add the alert
468 Text* alert = Text::create("Layout Linear Horizontal", "fonts/Marker Felt.ttf", 20);
469 alert->setColor(Color3B(159, 168, 176));
470 alert->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 4.5f));
471 _uiLayer->addChild(alert);
472
473 Layout* root = static_cast<Layout*>(_uiLayer->getChildByTag(81));
474
475 Layout* background = dynamic_cast<Layout*>(root->getChildByName("background_Panel"));
476
477 // Create the layout
478 Layout* layout = Layout::create();
479 layout->setLayoutType(Layout::Type::HORIZONTAL);
480 layout->setClippingEnabled(true);
481 layout->setContentSize(Size(280, 150));
482 Size backgroundSize = background->getContentSize();
483 layout->setPosition(Vec2((widgetSize.width - backgroundSize.width) / 2.0f +
484 (backgroundSize.width - layout->getContentSize().width) / 2.0f,
485 (widgetSize.height - backgroundSize.height) / 2.0f +
486 (backgroundSize.height - layout->getContentSize().height) / 2.0f));
487 _uiLayer->addChild(layout);
488
489 Button* button = Button::create("cocosui/animationbuttonnormal.png", "cocosui/animationbuttonpressed.png");
490 layout->addChild(button);
491
492 LinearLayoutParameter* lp1 = LinearLayoutParameter::create();
493 button->setLayoutParameter(lp1);
494 lp1->setGravity(LinearLayoutParameter::LinearGravity::CENTER_VERTICAL);
495 lp1->setMargin(Margin(0.0f, 10.0f, 0.0f, 10.0f));
496
497
498 Button* titleButton = Button::create("cocosui/backtotopnormal.png", "cocosui/backtotoppressed.png");
499 titleButton->setTitleText("Title Button");
500 layout->addChild(titleButton);
501
502 LinearLayoutParameter* lp2 = LinearLayoutParameter::create();
503 titleButton->setLayoutParameter(lp2);
504 lp2->setGravity(LinearLayoutParameter::LinearGravity::CENTER_VERTICAL);
505 lp2->setMargin(Margin(0.0f, 10.0f, 0.0f, 10.0f));
506
507
508 Button* button_scale9 = Button::create("cocosui/button.png", "cocosui/buttonHighlighted.png");
509 button_scale9->setScale9Enabled(true);
510 button_scale9->setContentSize(Size(100.0f, button_scale9->getVirtualRendererSize().height));
511 layout->addChild(button_scale9);
512
513 LinearLayoutParameter* lp3 = LinearLayoutParameter::create();
514 button_scale9->setLayoutParameter(lp3);
515 lp3->setGravity(LinearLayoutParameter::LinearGravity::CENTER_VERTICAL);
516 lp3->setMargin(Margin(0.0f, 10.0f, 0.0f, 10.0f));
517
518
519 return true;
520 }
521
522 return false;
523}
524
525// UILayoutTest_Layout_Relative_Align_Parent
526
528{
529}
530
532{
533}
534
536{
537 if (UIScene::init())
538 {
539 Size widgetSize = _widget->getContentSize();
540
541 // Add the alert
542 Text* alert = Text::create("Layout Relative Align Parent", "fonts/Marker Felt.ttf", 20);
543 alert->setColor(Color3B(159, 168, 176));
544 alert->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 4.5f));
545 _uiLayer->addChild(alert);
546
547 Layout* root = static_cast<Layout*>(_uiLayer->getChildByTag(81));
548
549 Layout* background = dynamic_cast<Layout*>(root->getChildByName("background_Panel"));
550
551 // Create the layout
552 Layout* layout = Layout::create();
553 layout->setLayoutType(Layout::Type::RELATIVE);
554 layout->setContentSize(Size(280, 150));
555 layout->setBackGroundColorType(Layout::BackGroundColorType::SOLID);
556 layout->setBackGroundColor(Color3B::GREEN);
557 Size backgroundSize = background->getContentSize();
558 layout->setPosition(Vec2((widgetSize.width - backgroundSize.width) / 2.0f +
559 (backgroundSize.width - layout->getContentSize().width) / 2.0f,
560 (widgetSize.height - backgroundSize.height) / 2.0f +
561 (backgroundSize.height - layout->getContentSize().height) / 2.0f));
562 _uiLayer->addChild(layout);
563
564 // top left
565 Button* button_TopLeft = Button::create("cocosui/animationbuttonnormal.png",
566 "cocosui/animationbuttonpressed.png");
567 layout->addChild(button_TopLeft);
568
569 RelativeLayoutParameter* rp_TopLeft = RelativeLayoutParameter::create();
570 rp_TopLeft->setAlign(RelativeLayoutParameter::RelativeAlign::PARENT_TOP_LEFT);
571 button_TopLeft->setLayoutParameter(rp_TopLeft);
572
573
574 // top center horizontal
575 Button* button_TopCenter = Button::create("cocosui/animationbuttonnormal.png",
576 "cocosui/animationbuttonpressed.png");
577 layout->addChild(button_TopCenter);
578
579 RelativeLayoutParameter* rp_TopCenter = RelativeLayoutParameter::create();
580 rp_TopCenter->setAlign(RelativeLayoutParameter::RelativeAlign::PARENT_TOP_CENTER_HORIZONTAL);
581 button_TopCenter->setLayoutParameter(rp_TopCenter);
582
583
584 // top right
585 Button* button_TopRight = Button::create("cocosui/animationbuttonnormal.png",
586 "cocosui/animationbuttonpressed.png");
587 layout->addChild(button_TopRight);
588
589 RelativeLayoutParameter* rp_TopRight = RelativeLayoutParameter::create();
590 rp_TopRight->setAlign(RelativeLayoutParameter::RelativeAlign::PARENT_TOP_RIGHT);
591 button_TopRight->setLayoutParameter(rp_TopRight);
592
593
594 // left center
595 Button* button_LeftCenter = Button::create("cocosui/animationbuttonnormal.png",
596 "cocosui/animationbuttonpressed.png");
597 layout->addChild(button_LeftCenter);
598
599 RelativeLayoutParameter* rp_LeftCenter = RelativeLayoutParameter::create();
600 rp_LeftCenter->setAlign(RelativeLayoutParameter::RelativeAlign::PARENT_LEFT_CENTER_VERTICAL);
601 button_LeftCenter->setLayoutParameter(rp_LeftCenter);
602
603
604 // center
605 Button* buttonCenter = Button::create("cocosui/animationbuttonnormal.png",
606 "cocosui/animationbuttonpressed.png");
607 layout->addChild(buttonCenter);
608
609 RelativeLayoutParameter* rpCenter = RelativeLayoutParameter::create();
610 rpCenter->setAlign(RelativeLayoutParameter::RelativeAlign::CENTER_IN_PARENT);
611 buttonCenter->setLayoutParameter(rpCenter);
612
613
614 // right center
615 Button* button_RightCenter = Button::create("cocosui/animationbuttonnormal.png",
616 "cocosui/animationbuttonpressed.png");
617 layout->addChild(button_RightCenter);
618
619 RelativeLayoutParameter* rp_RightCenter = RelativeLayoutParameter::create();
620 rp_RightCenter->setAlign(RelativeLayoutParameter::RelativeAlign::PARENT_RIGHT_CENTER_VERTICAL);
621 button_RightCenter->setLayoutParameter(rp_RightCenter);
622
623
624 // left bottom
625 Button* button_LeftBottom = Button::create("cocosui/animationbuttonnormal.png",
626 "cocosui/animationbuttonpressed.png");
627 layout->addChild(button_LeftBottom);
628
629 RelativeLayoutParameter* rp_LeftBottom = RelativeLayoutParameter::create();
630 rp_LeftBottom->setAlign(RelativeLayoutParameter::RelativeAlign::PARENT_LEFT_BOTTOM);
631 button_LeftBottom->setLayoutParameter(rp_LeftBottom);
632
633
634 // bottom center
635 Button* button_BottomCenter = Button::create("cocosui/animationbuttonnormal.png",
636 "cocosui/animationbuttonpressed.png");
637 layout->addChild(button_BottomCenter);
638
639 RelativeLayoutParameter* rp_BottomCenter = RelativeLayoutParameter::create();
640 rp_BottomCenter->setAlign(RelativeLayoutParameter::RelativeAlign::PARENT_BOTTOM_CENTER_HORIZONTAL);
641 button_BottomCenter->setLayoutParameter(rp_BottomCenter);
642
643
644 // right bottom
645 Button* button_RightBottom = Button::create("cocosui/animationbuttonnormal.png",
646 "cocosui/animationbuttonpressed.png");
647 layout->addChild(button_RightBottom);
648
649 RelativeLayoutParameter* rp_RightBottom = RelativeLayoutParameter::create();
650 rp_RightBottom->setAlign(RelativeLayoutParameter::RelativeAlign::PARENT_RIGHT_BOTTOM);
651 button_RightBottom->setLayoutParameter(rp_RightBottom);
652
653
654 return true;
655 }
656
657 return false;
658}
659
660// UILayoutTest_Layout_Relative_Location
661
663{
664}
665
667{
668}
669
671{
672 if (UIScene::init())
673 {
674 Size widgetSize = _widget->getContentSize();
675
676 // Add the alert
677 Text* alert = Text::create("Layout Relative Location", "fonts/Marker Felt.ttf", 20);
678 alert->setColor(Color3B(159, 168, 176));
679 alert->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 4.5f));
680 _uiLayer->addChild(alert);
681
682 Layout* root = static_cast<Layout*>(_uiLayer->getChildByTag(81));
683
684 Layout* background = dynamic_cast<Layout*>(root->getChildByName("background_Panel"));
685
686 // Create the layout
687 Layout* layout = Layout::create();
688 layout->setLayoutType(Layout::Type::RELATIVE);
689 layout->setContentSize(Size(280, 150));
690 Size backgroundSize = background->getContentSize();
691 layout->setPosition(Vec2((widgetSize.width - backgroundSize.width) / 2.0f +
692 (backgroundSize.width - layout->getContentSize().width) / 2.0f,
693 (widgetSize.height - backgroundSize.height) / 2.0f +
694 (backgroundSize.height - layout->getContentSize().height) / 2.0f));
695 _uiLayer->addChild(layout);
696
697 // center
698 ImageView* imageView_Center = ImageView::create("cocosui/scrollviewbg.png");
699 layout->addChild(imageView_Center);
700
701 RelativeLayoutParameter* rp_Center = RelativeLayoutParameter::create();
702 rp_Center->setRelativeName("rp_Center");
703 rp_Center->setAlign(RelativeLayoutParameter::RelativeAlign::CENTER_IN_PARENT);
704 imageView_Center->setLayoutParameter(rp_Center);
705
706
707 // above center
708 ImageView* imageView_AboveCenter = ImageView::create("cocosui/switch-mask.png");
709 layout->addChild(imageView_AboveCenter);
710
711 RelativeLayoutParameter* rp_AboveCenter = RelativeLayoutParameter::create();
712 rp_AboveCenter->setRelativeToWidgetName("rp_Center");
713 rp_AboveCenter->setAlign(RelativeLayoutParameter::RelativeAlign::LOCATION_ABOVE_CENTER);
714 imageView_AboveCenter->setLayoutParameter(rp_AboveCenter);
715
716
717 // below center
718 ImageView* imageView_BelowCenter = ImageView::create("cocosui/switch-mask.png");
719 layout->addChild(imageView_BelowCenter);
720
721 RelativeLayoutParameter* rp_BelowCenter = RelativeLayoutParameter::create();
722 rp_BelowCenter->setRelativeToWidgetName("rp_Center");
723 rp_BelowCenter->setAlign(RelativeLayoutParameter::RelativeAlign::LOCATION_BELOW_CENTER);
724 imageView_BelowCenter->setLayoutParameter(rp_BelowCenter);
725
726
727 // left center
728 ImageView* imageView_LeftCenter = ImageView::create("cocosui/switch-mask.png");
729 layout->addChild(imageView_LeftCenter);
730
731 RelativeLayoutParameter* rp_LeftCenter = RelativeLayoutParameter::create();
732 rp_LeftCenter->setRelativeToWidgetName("rp_Center");
733 rp_LeftCenter->setAlign(RelativeLayoutParameter::RelativeAlign::LOCATION_LEFT_OF_CENTER);
734 imageView_LeftCenter->setLayoutParameter(rp_LeftCenter);
735
736
737
738 // right center
739 ImageView* imageView_RightCenter = ImageView::create("cocosui/switch-mask.png");
740 layout->addChild(imageView_RightCenter);
741
742 RelativeLayoutParameter* rp_RightCenter = RelativeLayoutParameter::create();
743 rp_RightCenter->setRelativeToWidgetName("rp_Center");
744 rp_RightCenter->setAlign(RelativeLayoutParameter::RelativeAlign::LOCATION_RIGHT_OF_CENTER);
745 imageView_RightCenter->setLayoutParameter(rp_RightCenter);
746
747
748 return true;
749 }
750
751 return false;
752}
753
754// UILayoutTest_Layout_Relative_Location
755
757{
758}
759
761{
762}
763
765{
766 if (UIScene::init())
767 {
768 Size widgetSize = _widget->getContentSize();
769
770 // Add the alert
771 Text* alert = Text::create("Layout Scaled Widget", "fonts/Marker Felt.ttf", 20);
772 alert->setColor(Color3B(159, 168, 176));
773 alert->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 4.5f));
774 _uiLayer->addChild(alert);
775
776 Layout* root = static_cast<Layout*>(_uiLayer->getChildByTag(81));
777
778 Layout* background = dynamic_cast<Layout*>(root->getChildByName("background_Panel"));
779
780 // Create the layout
781 Layout* layout = Layout::create();
782 layout->setLayoutType(Layout::Type::HORIZONTAL);
783 layout->setContentSize(Size(280, 150));
784 Size backgroundSize = background->getContentSize();
785 layout->setPosition(Vec2((widgetSize.width - backgroundSize.width) / 2.0f +
786 (backgroundSize.width - layout->getContentSize().width) / 2.0f,
787 (widgetSize.height - backgroundSize.height) / 2.0f +
788 (backgroundSize.height - layout->getContentSize().height) / 2.0f));
789 _uiLayer->addChild(layout);
790
791 ImageView* imageView_Center1 = ImageView::create("cocosui/scrollviewbg.png");
792 imageView_Center1->setScale(0.5);
793 layout->addChild(imageView_Center1);
794
795 ImageView* imageView_Center2 = ImageView::create("cocosui/scrollviewbg.png");
796 imageView_Center2->setScale(1.2);
797 layout->addChild(imageView_Center2);
798
799 ImageView* imageView_Center3 = ImageView::create("cocosui/scrollviewbg.png");
800 imageView_Center3->setScale(0.8);
801 layout->addChild(imageView_Center3);
802
803 return true;
804 }
805
806 return false;
807}
808
809
811{
812 if (UIScene::init())
813 {
814 Size widgetSize = _widget->getContentSize();
815
816 _baseLayer = LayerColor::create();
817 _baseLayer->setColor(Color3B(50, 100, 0));
818 _baseLayer->setOpacity(100);
819 _baseLayer->setContentSize(Size(200, 200));
820 _uiLayer->addChild(_baseLayer);
821
822 Button* button = Button::create("cocosui/animationbuttonnormal.png");
823 CCLOG("content size should be greater than 0: width = %f, height = %f", button->getContentSize().width,
824 button->getContentSize().height);
825 button->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
826 button->addTouchEventListener(CC_CALLBACK_2(UILayoutComponentTest::touchEvent, this));
827 button->setZoomScale(0.4f);
828 button->setPressedActionEnabled(true);
829 _uiLayer->addChild(button);
830
831 return true;
832 }
833 return false;
834}
835
836void UILayoutComponentTest::touchEvent(Ref *pSender, Widget::TouchEventType type)
837{
838 switch (type)
839 {
840 case Widget::TouchEventType::BEGAN:
841 break;
842
843 case Widget::TouchEventType::MOVED:
844 break;
845
846 case Widget::TouchEventType::ENDED:
847 {
848 Size widgetSize = _widget->getContentSize();
849 Size layerSize = _baseLayer->getContentSize();
850 if (layerSize.width == widgetSize.width && layerSize.height == widgetSize.height)
851 _baseLayer->setContentSize(Size(200, 200));
852 else
853 _baseLayer->setContentSize(widgetSize);
854 Helper::doLayout(_baseLayer);
855 }
856 break;
857
858 case Widget::TouchEventType::CANCELED:
859 break;
860
861 default:
862 break;
863 }
864}
865
867{
869 {
870 Sprite* leftTopSprite = Sprite::create("cocosui/CloseSelected.png");
871 LayoutComponent* leftTop = LayoutComponent::bindLayoutComponent(leftTopSprite);
872 leftTop->setHorizontalEdge(LayoutComponent::HorizontalEdge::Left);
873 leftTop->setVerticalEdge(LayoutComponent::VerticalEdge::Top);
874 _baseLayer->addChild(leftTopSprite);
875
876 Sprite* leftBottomSprite = Sprite::create("cocosui/CloseSelected.png");
877 LayoutComponent* leftBottom = LayoutComponent::bindLayoutComponent(leftBottomSprite);
878 leftBottom->setHorizontalEdge(LayoutComponent::HorizontalEdge::Left);
879 leftBottom->setVerticalEdge(LayoutComponent::VerticalEdge::Bottom);
880 _baseLayer->addChild(leftBottomSprite);
881
882 Sprite* rightTopSprite = Sprite::create("cocosui/CloseSelected.png");
883 LayoutComponent* rightTop = LayoutComponent::bindLayoutComponent(rightTopSprite);
884 rightTop->setHorizontalEdge(LayoutComponent::HorizontalEdge::Right);
885 rightTop->setVerticalEdge(LayoutComponent::VerticalEdge::Top);
886 _baseLayer->addChild(rightTopSprite);
887
888 Sprite* rightBottomSprite = Sprite::create("cocosui/CloseSelected.png");
889 LayoutComponent* rightBottom = LayoutComponent::bindLayoutComponent(rightBottomSprite);
890 rightBottom->setHorizontalEdge(LayoutComponent::HorizontalEdge::Right);
891 rightBottom->setVerticalEdge(LayoutComponent::VerticalEdge::Bottom);
892 _baseLayer->addChild(rightBottomSprite);
893
894 ui::Helper::doLayout(_baseLayer);
895
896 return true;
897 }
898 return false;
899}
900
902{
904 {
905 ImageView* leftTopSprite = ImageView::create("cocosui/CloseSelected.png");
906 leftTopSprite->ignoreContentAdaptWithSize(false);
907 LayoutComponent* leftTop = LayoutComponent::bindLayoutComponent(leftTopSprite);
908 leftTop->setHorizontalEdge(LayoutComponent::HorizontalEdge::Left);
909 leftTop->setVerticalEdge(LayoutComponent::VerticalEdge::Top);
910 leftTop->setStretchWidthEnabled(true);
911 leftTop->setStretchHeightEnabled(true);
912 _baseLayer->addChild(leftTopSprite);
913 leftTop->setSize(leftTopSprite->getContentSize());
914 leftTop->setLeftMargin(0);
915 leftTop->setTopMargin(0);
916
917 ImageView* leftBottomSprite = ImageView::create("cocosui/CloseSelected.png");
918 leftBottomSprite->ignoreContentAdaptWithSize(false);
919 LayoutComponent* leftBottom = LayoutComponent::bindLayoutComponent(leftBottomSprite);
920 leftBottom->setHorizontalEdge(LayoutComponent::HorizontalEdge::Left);
921 leftBottom->setVerticalEdge(LayoutComponent::VerticalEdge::Bottom);
922 leftBottom->setStretchWidthEnabled(true);
923 leftBottom->setStretchHeightEnabled(true);
924 _baseLayer->addChild(leftBottomSprite);
925 leftBottom->setSize(leftBottomSprite->getContentSize());
926 leftBottom->setLeftMargin(0);
927 leftBottom->setBottomMargin(0);
928
929 ImageView* rightTopSprite = ImageView::create("cocosui/CloseSelected.png");
930 rightTopSprite->ignoreContentAdaptWithSize(false);
931 LayoutComponent* rightTop = LayoutComponent::bindLayoutComponent(rightTopSprite);
932 rightTop->setHorizontalEdge(LayoutComponent::HorizontalEdge::Right);
933 rightTop->setVerticalEdge(LayoutComponent::VerticalEdge::Top);
934 rightTop->setStretchWidthEnabled(true);
935 rightTop->setStretchHeightEnabled(true);
936 _baseLayer->addChild(rightTopSprite);
937 rightTop->setSize(rightTopSprite->getContentSize());
938 rightTop->setTopMargin(0);
939 rightTop->setRightMargin(0);
940
941 ImageView* rightBottomSprite = ImageView::create("cocosui/CloseSelected.png");
942 rightBottomSprite->ignoreContentAdaptWithSize(false);
943 LayoutComponent* rightBottom = LayoutComponent::bindLayoutComponent(rightBottomSprite);
944 rightBottom->setHorizontalEdge(LayoutComponent::HorizontalEdge::Right);
945 rightBottom->setVerticalEdge(LayoutComponent::VerticalEdge::Bottom);
946 rightBottom->setStretchWidthEnabled(true);
947 rightBottom->setStretchHeightEnabled(true);
948 _baseLayer->addChild(rightBottomSprite);
949 rightBottom->setSize(rightBottomSprite->getContentSize());
950 rightBottom->setBottomMargin(0);
951 rightBottom->setRightMargin(0);
952
953 ui::Helper::doLayout(_baseLayer);
954
955 return true;
956 }
957 return false;
958}
#define ADD_TEST_CASE(__className__)
Definition: BaseTest.h:211
USING_NS_CC
virtual bool init() override
cocos2d::LayerColor * _baseLayer
Definition: UILayoutTest.h:145
virtual bool init() override
void touchEvent(cocos2d::Ref *sender, cocos2d::ui::Widget::TouchEventType type)
cocos2d::ui::Layout * _layout
Definition: UILayoutTest.h:74
virtual bool init() override
void printWidgetResources(cocos2d::Ref *sender)
virtual bool init() override
virtual bool init() override
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