PuzzleSDK
UIScrollViewTest.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 "UIScrollViewTest.h"
26
28using namespace cocos2d::ui;
29
30UIScrollViewTests::UIScrollViewTests()
31{
43}
44// UIScrollViewTest_Vertical
45
47: _displayValueLabel(nullptr)
48{
49}
50
52{
53}
54
56{
57 if (UIScene::init())
58 {
59 Size widgetSize = _widget->getContentSize();
60
61 // Add a label in which the scrollview alert will be displayed
62 _displayValueLabel = Text::create("Move by vertical direction", "fonts/Marker Felt.ttf", 32);
63 _displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
64 _displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f,
65 widgetSize.height / 2.0f + _displayValueLabel->getContentSize().height * 1.5f));
67
68 // Add the alert
69 Text* alert = Text::create("ScrollView vertical", "fonts/Marker Felt.ttf", 30);
70 alert->setColor(Color3B(159, 168, 176));
71 alert->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 3.075f));
72 _uiLayer->addChild(alert);
73
74 Layout* root = static_cast<Layout*>(_uiLayer->getChildByTag(81));
75
76 Layout* background = dynamic_cast<Layout*>(root->getChildByName("background_Panel"));
77
78 // Create the scrollview by vertical
79 ui::ScrollView* scrollView = ui::ScrollView::create();
80 scrollView->setContentSize(Size(280.0f, 100.0f));
81 Size backgroundSize = background->getContentSize();
82 scrollView->setPosition(Vec2((widgetSize.width - backgroundSize.width) / 2.0f +
83 (backgroundSize.width - scrollView->getContentSize().width) / 2.0f,
84 (widgetSize.height - backgroundSize.height) / 2.0f +
85 (backgroundSize.height - scrollView->getContentSize().height) / 2.0f));
86 scrollView->setScrollBarWidth(4);
87 scrollView->setScrollBarPositionFromCorner(Vec2(2, 2));
88 scrollView->setScrollBarColor(Color3B::WHITE);
89 scrollView->setGlobalZOrder(200);
90 _uiLayer->addChild(scrollView);
91
92 ImageView* imageView = ImageView::create("cocosui/ccicon.png");
93
94 float innerWidth = scrollView->getContentSize().width;
95 float innerHeight = scrollView->getContentSize().height + imageView->getContentSize().height;
96
97 scrollView->setInnerContainerSize(Size(innerWidth, innerHeight));
98
99 Button* button = Button::create("cocosui/animationbuttonnormal.png", "cocosui/animationbuttonpressed.png");
100 button->setPosition(Vec2(innerWidth / 2.0f, scrollView->getInnerContainerSize().height - button->getContentSize().height / 2.0f));
101 scrollView->addChild(button);
102
103 Button* titleButton = Button::create("cocosui/backtotopnormal.png", "cocosui/backtotoppressed.png");
104 titleButton->setTitleText("Title Button");
105 titleButton->setPosition(Vec2(innerWidth / 2.0f, button->getBottomBoundary() - button->getContentSize().height));
106 scrollView->addChild(titleButton);
107
108 Button* button_scale9 = Button::create("cocosui/button.png", "cocosui/buttonHighlighted.png");
109 button_scale9->setScale9Enabled(true);
110 button_scale9->setContentSize(Size(100.0f, button_scale9->getVirtualRendererSize().height));
111 button_scale9->setPosition(Vec2(innerWidth / 2.0f, titleButton->getBottomBoundary() - titleButton->getContentSize().height));
112 scrollView->addChild(button_scale9);
113
114 imageView->setPosition(Vec2(innerWidth / 2.0f, imageView->getContentSize().height / 2.0f));
115 scrollView->addChild(imageView);
116 scrollView->scrollToBottom(2.0, true);
117 return true;
118 }
119
120 return false;
121}
122
123// UIScrollViewTest_Horizontal
124
126: _displayValueLabel(nullptr)
127{
128}
129
131{
132}
133
135{
136 if (UIScene::init())
137 {
138 Size widgetSize = _widget->getContentSize();
139
140 // Add a label in which the scrollview alert will be displayed
141 _displayValueLabel = Text::create("Move by horizontal direction","fonts/Marker Felt.ttf",32);
142 _displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
143 _displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f + _displayValueLabel->getContentSize().height * 1.5f));
144 _uiLayer->addChild(_displayValueLabel);
145
146 Text* alert = Text::create("ScrollView horizontal","fonts/Marker Felt.ttf",30);
147 alert->setColor(Color3B(159, 168, 176));
148 alert->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 3.075f));
149 _uiLayer->addChild(alert);
150
151 Layout* root = static_cast<Layout*>(_uiLayer->getChildByTag(81));
152
153 Layout* background = dynamic_cast<Layout*>(root->getChildByName("background_Panel"));
154
155 // Create the scrollview by horizontal
156 ui::ScrollView* scrollView = ui::ScrollView::create();
157 scrollView->setBounceEnabled(true);
158 scrollView->setDirection(ui::ScrollView::Direction::HORIZONTAL);
159 scrollView->setContentSize(Size(280.0f, 150.0f));
160 scrollView->setInnerContainerSize(scrollView->getContentSize());
161 scrollView->setScrollBarPositionFromCorner(Vec2(4, 4));
162 scrollView->setScrollBarColor(Color3B::YELLOW);
163 Size backgroundSize = background->getContentSize();
164 scrollView->setPosition((widgetSize - scrollView->getContentSize()) / 2.0f);
165 _uiLayer->addChild(scrollView);
166
167 ImageView* imageView = ImageView::create("cocosui/ccicon.png");
168
169 float innerWidth = scrollView->getContentSize().width + imageView->getContentSize().width;
170 float innerHeight = scrollView->getContentSize().height;
171
172 scrollView->setInnerContainerSize(Size(innerWidth, innerHeight));
173
174 Button* button = Button::create("cocosui/animationbuttonnormal.png", "cocosui/animationbuttonpressed.png");
175 button->setPosition(Vec2(button->getContentSize().width / 2.0f,
176 scrollView->getInnerContainerSize().height - button->getContentSize().height / 2.0f));
177 scrollView->addChild(button);
178
179 Button* titleButton = Button::create("cocosui/backtotopnormal.png", "cocosui/backtotoppressed.png");
180 titleButton->setTitleText("Title Button");
181 titleButton->setPosition(Vec2(button->getRightBoundary() + button->getContentSize().width / 2.0f,
182 button->getBottomBoundary() - button->getContentSize().height / 2.0f));
183 scrollView->addChild(titleButton);
184
185 Button* button_scale9 = Button::create("cocosui/button.png", "cocosui/buttonHighlighted.png");
186 button_scale9->setScale9Enabled(true);
187 button_scale9->setContentSize(Size(100.0f, button_scale9->getVirtualRendererSize().height));
188 button_scale9->setPosition(Vec2(titleButton->getRightBoundary() + titleButton->getContentSize().width / 2.0f,
189 titleButton->getBottomBoundary() - titleButton->getContentSize().height / 2.0f));
190 scrollView->addChild(button_scale9);
191
192 imageView->setPosition(Vec2(innerWidth - imageView->getContentSize().width / 2.0f,
193 button_scale9->getBottomBoundary() - button_scale9->getContentSize().height / 2.0f));
194 scrollView->addChild(imageView);
195
196 // Jump to right
197 scrollView->jumpToRight();
198
199 return true;
200 }
201
202 return false;
203}
204
205// UIScrollViewTest_Both
207: _displayValueLabel(nullptr)
208{
209
210}
211
213{
214}
215
217{
218 if (UIScene::init())
219 {
220 Size widgetSize = _widget->getContentSize();
221
222 // Add a label in which the dragpanel events will be displayed
223 _displayValueLabel = Text::create("Move by any direction","fonts/Marker Felt.ttf",32);
224 _displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
225 _displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f + _displayValueLabel->getContentSize().height * 1.5f));
226 _uiLayer->addChild(_displayValueLabel);
227
228 // Add the alert
229 Text* alert = Text::create("ScrollView both","fonts/Marker Felt.ttf",30);
230 alert->setColor(Color3B(159, 168, 176));
231 alert->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 3.075f));
232 _uiLayer->addChild(alert);
233
234 Layout* root = static_cast<Layout*>(_uiLayer->getChildByTag(81));
235
236 Layout* background = static_cast<Layout*>(root->getChildByName("background_Panel"));
237
238 // Create the dragpanel
239 ui::ScrollView* scrollView = ui::ScrollView::create();
240 scrollView->setDirection(ui::ScrollView::Direction::BOTH);
241 scrollView->setTouchEnabled(true);
242 scrollView->setBounceEnabled(true);
243 scrollView->setBackGroundImageScale9Enabled(true);
244 scrollView->setBackGroundImage("cocosui/green_edit.png");
245 scrollView->setContentSize(Size(210, 122.5));
246 scrollView->setScrollBarWidth(4);
247 scrollView->setScrollBarPositionFromCorner(Vec2(6, 6));
248 Size backgroundSize = background->getContentSize();
249 scrollView->setPosition(Vec2((widgetSize.width - backgroundSize.width) / 2.0f +
250 (backgroundSize.width - scrollView->getContentSize().width) / 2.0f,
251 (widgetSize.height - backgroundSize.height) / 2.0f +
252 (backgroundSize.height - scrollView->getContentSize().height) / 2.0f));
253 ImageView* imageView = ImageView::create("Hello.png");
254 scrollView->addChild(imageView);
255
256 scrollView->setInnerContainerSize(imageView->getContentSize());
257 Size innerSize = scrollView->getInnerContainerSize();
258 imageView->setPosition(Vec2(innerSize.width / 2.0f, innerSize.height / 2.0f));
259
260 _uiLayer->addChild(scrollView);
261
262 // Jump to right bottom
263 scrollView->jumpToBottomRight();
264
265 return true;
266 }
267
268 return false;
269}
270
271// UIScrollViewTest_ScrollToPercentBothDirection
273: _displayValueLabel(nullptr)
274{
275
276}
277
279{
280}
281
283{
284 if (UIScene::init())
285 {
286 Size widgetSize = _widget->getContentSize();
287
288 // Add a label in which the dragpanel events will be displayed
289 _displayValueLabel = Text::create("No Event", "fonts/Marker Felt.ttf",30);
290 _displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
291 _displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f + _displayValueLabel->getContentSize().height * 1.5f));
292 _uiLayer->addChild(_displayValueLabel);
293
294 // Add the alert
295 Text* alert = Text::create("ScrollView scroll to percent both direction without scroll bar","fonts/Marker Felt.ttf",20);
296 alert->setColor(Color3B(159, 168, 176));
297 alert->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 4.5));
298 _uiLayer->addChild(alert);
299
300 Layout* root = static_cast<Layout*>(_uiLayer->getChildByTag(81));
301
302 Layout* background = static_cast<Layout*>(root->getChildByName("background_Panel"));
303
304 ui::ScrollView* sc = ui::ScrollView::create();
305 sc->setBackGroundColor(Color3B::GREEN);
306 sc->setBackGroundColorType(Layout::BackGroundColorType::SOLID);
307 sc->setDirection(ui::ScrollView::Direction::BOTH);
308 sc->setInnerContainerSize(Size(480.0f, 320.0f));
309 sc->setContentSize(Size(100.0f,100.0f));
310 sc->setScrollBarEnabled(false);
311 Size backgroundSize = background->getContentSize();
312 sc->setPosition(Vec2((widgetSize.width - backgroundSize.width) / 2.0f +
313 (backgroundSize.width - sc->getContentSize().width) / 2.0f,
314 (widgetSize.height - backgroundSize.height) / 2.0f +
315 (backgroundSize.height - sc->getContentSize().height) / 2.0f));
316 sc->scrollToPercentBothDirection(Vec2(50, 50), 1, true);
317 ImageView* iv = ImageView::create("cocosui/Hello.png");
318 iv->setPosition(Vec2(240.0f, 160.0f));
319 sc->addChild(iv);
320 _uiLayer->addChild(sc);
321
322 return true;
323 }
324 return false;
325}
326
327// UIScrollViewTest_ScrollToPercentBothDirection_Bounce
329: _displayValueLabel(nullptr)
330{
331
332}
333
335{
336}
337
339{
340 if (UIScene::init())
341 {
342 Size widgetSize = _widget->getContentSize();
343
344 // Add a label in which the dragpanel events will be displayed
345 _displayValueLabel = Text::create("No Event","fonts/Marker Felt.ttf",32);
346 _displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
347 _displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f + _displayValueLabel->getContentSize().height * 1.5f));
348 _uiLayer->addChild(_displayValueLabel);
349
350 // Add the alert
351 Text* alert = Text::create("ScrollView scroll to percent both direction bounce","fonts/Marker Felt.ttf",20);
352 alert->setColor(Color3B(159, 168, 176));
353 alert->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 4.5));
354 _uiLayer->addChild(alert);
355
356 Layout* root = static_cast<Layout*>(_uiLayer->getChildByTag(81));
357
358 Layout* background = dynamic_cast<Layout*>(root->getChildByName("background_Panel"));
359
360 ui::ScrollView* sc = ui::ScrollView::create();
361 sc->setBackGroundColor(Color3B::GREEN);
362 sc->setBackGroundColorType(Layout::BackGroundColorType::SOLID);
363 sc->setBounceEnabled(true);
364 sc->setDirection(ui::ScrollView::Direction::BOTH);
365 sc->setInnerContainerSize(Size(480.0f, 320.0f));
366 sc->setContentSize(Size(100.0f,100.0f));
367 sc->setScrollBarPositionFromCornerForHorizontal(Vec2(5.0f, sc->getContentSize().height - 5));
368 sc->setScrollBarPositionFromCornerForVertical(Vec2(sc->getContentSize().width - 5, 5.0f));
369 sc->setScrollBarAutoHideEnabled(false);
370 Size backgroundSize = background->getContentSize();
371 sc->setPosition(Vec2((widgetSize.width - backgroundSize.width) / 2.0f +
372 (backgroundSize.width - sc->getContentSize().width) / 2.0f,
373 (widgetSize.height - backgroundSize.height) / 2.0f +
374 (backgroundSize.height - sc->getContentSize().height) / 2.0f));
375 sc->scrollToPercentBothDirection(Vec2(50.0f, 50.0f), 1, true);
376 ImageView* iv = ImageView::create("cocosui/Hello.png");
377 iv->setPosition(Vec2(240.0f, 160.0f));
378 sc->addChild(iv);
379 _uiLayer->addChild(sc);
380 return true;
381 }
382
383 return false;
384}
385
386// UIScrollViewNestTest
387
389: _displayValueLabel(nullptr)
390{
391}
392
394{
395}
396
398{
399 if (UIScene::init())
400 {
401 Size widgetSize = _widget->getContentSize();
402
403 // Add a label in which the scrollview alert will be displayed
404 _displayValueLabel = Text::create("Move by vertical direction", "fonts/Marker Felt.ttf", 32);
405 _displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
406 _displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f,
407 widgetSize.height / 2.0f + _displayValueLabel->getContentSize().height * 1.5f));
408 _uiLayer->addChild(_displayValueLabel);
409
410 // Add the alert
411 Text* alert = Text::create("ScrollView vertical", "fonts/Marker Felt.ttf", 30);
412 alert->setColor(Color3B(159, 168, 176));
413 alert->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 3.075f));
414 _uiLayer->addChild(alert);
415
416 Layout* root = static_cast<Layout*>(_uiLayer->getChildByTag(81));
417
418 Layout* background = dynamic_cast<Layout*>(root->getChildByName("background_Panel"));
419
420 // Create the scrollview by vertical
421 ui::ScrollView* scrollView = ui::ScrollView::create();
422 scrollView->setContentSize(Size(280.0f, 150.0f));
423 scrollView->setDirection(ui::ScrollView::Direction::VERTICAL);
424 scrollView->setScrollBarPositionFromCorner(Vec2(4, 4));
425 Size backgroundSize = background->getContentSize();
426 scrollView->setPosition(Vec2((widgetSize.width - backgroundSize.width) / 2.0f +
427 (backgroundSize.width - scrollView->getContentSize().width) / 2.0f,
428 (widgetSize.height - backgroundSize.height) / 2.0f +
429 (backgroundSize.height - scrollView->getContentSize().height) / 2.0f));
430 scrollView->setScrollBarPositionFromCornerForVertical(Vec2(scrollView->getContentSize().width - 4, 4.0f));
431 scrollView->setScrollBarColor(Color3B::BLUE);
432 _uiLayer->addChild(scrollView);
433
434 ImageView* imageView = ImageView::create("cocosui/ccicon.png");
435
436 float innerWidth = scrollView->getContentSize().width;
437 float innerHeight = scrollView->getContentSize().height + imageView->getContentSize().height;
438
439 scrollView->setInnerContainerSize(Size(innerWidth, innerHeight));
440
441 Button* button = Button::create("cocosui/animationbuttonnormal.png", "cocosui/animationbuttonpressed.png");
442 button->setPosition(Vec2(innerWidth / 2.0f, scrollView->getInnerContainerSize().height - button->getContentSize().height / 2.0f));
443 scrollView->addChild(button);
444
445 Button* titleButton = Button::create("cocosui/backtotopnormal.png", "cocosui/backtotoppressed.png");
446 titleButton->setTitleText("Title Button");
447 titleButton->setPosition(Vec2(innerWidth / 2.0f, button->getBottomBoundary() - button->getContentSize().height));
448 scrollView->addChild(titleButton);
449
450 Button* button_scale9 = Button::create("cocosui/button.png", "cocosui/buttonHighlighted.png");
451 button_scale9->setScale9Enabled(true);
452 button_scale9->setContentSize(Size(100.0f, button_scale9->getVirtualRendererSize().height));
453 button_scale9->setPosition(Vec2(innerWidth / 2.0f, titleButton->getBottomBoundary() - titleButton->getContentSize().height));
454 scrollView->addChild(button_scale9);
455
456 imageView->setPosition(Vec2(innerWidth / 2.0f, imageView->getContentSize().height / 2.0f));
457 scrollView->addChild(imageView);
458
459
460
461 // Create the scrollview by both
462 ui::ScrollView* sc = ui::ScrollView::create();
463 sc->setBackGroundColor(Color3B::GREEN);
464 sc->setBackGroundColorType(Layout::BackGroundColorType::SOLID);
465 sc->setBounceEnabled(true);
466 sc->setDirection(ui::ScrollView::Direction::BOTH);
467 sc->setInnerContainerSize(Size(480.0f, 320.0f));
468 sc->setContentSize(Size(100.0f,100.0f));
469 sc->setPropagateTouchEvents(false);
470 sc->setPosition(Vec2(180.0f,100.0f));
471 sc->scrollToPercentBothDirection(Vec2(50.0f, 50.0f), 1, true);
472 sc->setScrollBarPositionFromCorner(Vec2(4.0f, 4.0f));
473 ImageView* iv = ImageView::create("cocosui/Hello.png");
474 iv->setPosition(Vec2(240.0f, 160.0f));
475 sc->addChild(iv);
476
477
478 scrollView->addChild(sc);
479
480 return true;
481 }
482
483 return false;
484}
485
486
487
488
489// UIScrollViewRotated
490
492: _displayValueLabel(nullptr)
493{
494}
495
497{
498}
499
501{
502 if (UIScene::init())
503 {
504 Size widgetSize = _widget->getContentSize();
505
506 // Add a label in which the scrollview alert will be displayed
507 _displayValueLabel = Text::create("Move by vertical direction", "fonts/Marker Felt.ttf", 32);
508 _displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
509 _displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f,
510 widgetSize.height / 2.0f + _displayValueLabel->getContentSize().height * 1.5f));
511 _uiLayer->addChild(_displayValueLabel);
512
513 // Add the alert
514 Text* alert = Text::create("ScrollView vertical", "fonts/Marker Felt.ttf", 30);
515 alert->setColor(Color3B(159, 168, 176));
516 alert->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 3.075f));
517 _uiLayer->addChild(alert);
518
519 Layout* root = static_cast<Layout*>(_uiLayer->getChildByTag(81));
520
521 Layout* background = dynamic_cast<Layout*>(root->getChildByName("background_Panel"));
522
523 // Create the scrollview by vertical
524 ui::ScrollView* scrollView = ui::ScrollView::create();
525 scrollView->setContentSize(Size(280.0f, 150.0f));
526 scrollView->setDirection(ui::ScrollView::Direction::VERTICAL);
527 Size backgroundSize = background->getContentSize();
528 scrollView->setPosition(Vec2((widgetSize.width - backgroundSize.width) / 2.0f +
529 (backgroundSize.width - scrollView->getContentSize().width) / 2.0f,
530 (widgetSize.height - backgroundSize.height) / 2.0f +
531 (backgroundSize.height - scrollView->getContentSize().height) / 2.0f + 100) );
532 scrollView->setRotation(45);
533 scrollView->setScrollBarPositionFromCorner(Vec2(4, 4));
534 scrollView->setScrollBarColor(Color3B::WHITE);
535 _uiLayer->addChild(scrollView);
536
537 ImageView* imageView = ImageView::create("cocosui/ccicon.png");
538
539 float innerWidth = scrollView->getContentSize().width;
540 float innerHeight = scrollView->getContentSize().height + imageView->getContentSize().height;
541
542 scrollView->setInnerContainerSize(Size(innerWidth, innerHeight));
543
544 Button* button = Button::create("cocosui/animationbuttonnormal.png", "cocosui/animationbuttonpressed.png");
545 button->setPosition(Vec2(innerWidth / 2.0f, scrollView->getInnerContainerSize().height - button->getContentSize().height / 2.0f));
546 scrollView->addChild(button);
547
548 Button* titleButton = Button::create("cocosui/backtotopnormal.png", "cocosui/backtotoppressed.png");
549 titleButton->setTitleText("Title Button");
550 titleButton->setPosition(Vec2(innerWidth / 2.0f, button->getBottomBoundary() - button->getContentSize().height));
551 scrollView->addChild(titleButton);
552
553 Button* button_scale9 = Button::create("cocosui/button.png", "cocosui/buttonHighlighted.png");
554 button_scale9->setScale9Enabled(true);
555 button_scale9->setContentSize(Size(100.0f, button_scale9->getVirtualRendererSize().height));
556 button_scale9->setPosition(Vec2(innerWidth / 2.0f, titleButton->getBottomBoundary() - titleButton->getContentSize().height));
557 scrollView->addChild(button_scale9);
558
559 imageView->setPosition(Vec2(innerWidth / 2.0f, imageView->getContentSize().height / 2.0f));
560 scrollView->addChild(imageView);
561
562
563
564 return true;
565 }
566
567 return false;
568}
569
570// UIScrollViewDisableTest
571
573: _displayValueLabel(nullptr)
574{
575}
576
578{
579}
580
582{
583 if (UIScene::init())
584 {
585 Size widgetSize = _widget->getContentSize();
586
587 // Add a label in which the scrollview alert will be displayed
588 _displayValueLabel = Text::create("ScrollView Disable Test", "fonts/Marker Felt.ttf", 32);
589 _displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
590 _displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f,
591 widgetSize.height / 2.0f + _displayValueLabel->getContentSize().height * 1.5f));
592 _uiLayer->addChild(_displayValueLabel);
593
594 // Add the alert
595 Text* alert = Text::create("ScrollView vertical", "fonts/Marker Felt.ttf", 30);
596 alert->setColor(Color3B(159, 168, 176));
597 alert->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 3.075f));
598 _uiLayer->addChild(alert);
599
600 Layout* root = static_cast<Layout*>(_uiLayer->getChildByTag(81));
601
602 Layout* background = dynamic_cast<Layout*>(root->getChildByName("background_Panel"));
603
604 // Create the scrollview by vertical
605 ui::ScrollView* scrollView = ui::ScrollView::create();
606 scrollView->setContentSize(Size(280.0f, 100.0f));
607 Size backgroundSize = background->getContentSize();
608 scrollView->setPosition(Vec2((widgetSize.width - backgroundSize.width) / 2.0f +
609 (backgroundSize.width - scrollView->getContentSize().width) / 2.0f,
610 (widgetSize.height - backgroundSize.height) / 2.0f +
611 (backgroundSize.height - scrollView->getContentSize().height) / 2.0f));
612 scrollView->setScrollBarWidth(4);
613 scrollView->setTouchEnabled(false);
614 scrollView->setScrollBarPositionFromCorner(Vec2(2, 2));
615 scrollView->setScrollBarColor(Color3B::WHITE);
616 _uiLayer->addChild(scrollView);
617
618 ImageView* imageView = ImageView::create("cocosui/ccicon.png");
619
620 float innerWidth = scrollView->getContentSize().width;
621 float innerHeight = scrollView->getContentSize().height + imageView->getContentSize().height;
622
623 scrollView->setInnerContainerSize(Size(innerWidth, innerHeight));
624
625 Button* button = Button::create("cocosui/animationbuttonnormal.png", "cocosui/animationbuttonpressed.png");
626 button->setPosition(Vec2(innerWidth / 2.0f, scrollView->getInnerContainerSize().height - button->getContentSize().height / 2.0f));
627 scrollView->addChild(button);
628
629 Button* titleButton = Button::create("cocosui/backtotopnormal.png", "cocosui/backtotoppressed.png");
630 titleButton->setTitleText("Title Button");
631 titleButton->setPosition(Vec2(innerWidth / 2.0f, button->getBottomBoundary() - button->getContentSize().height));
632 scrollView->addChild(titleButton);
633
634 Button* button_scale9 = Button::create("cocosui/button.png", "cocosui/buttonHighlighted.png");
635 button_scale9->setScale9Enabled(true);
636 button_scale9->setContentSize(Size(100.0f, button_scale9->getVirtualRendererSize().height));
637 button_scale9->setPosition(Vec2(innerWidth / 2.0f, titleButton->getBottomBoundary() - titleButton->getContentSize().height));
638 scrollView->addChild(button_scale9);
639
640 imageView->setPosition(Vec2(innerWidth / 2.0f, imageView->getContentSize().height / 2.0f));
641 scrollView->addChild(imageView);
642
643
644 return true;
645 }
646
647 return false;
648}
649
650// UIScrollViewInnerSize
651
653 : _displayValueLabel(nullptr)
654{
655}
656
658{
659}
660
662{
663 if (UIScene::init())
664 {
665 Size widgetSize = _widget->getContentSize();
666
667 // Add a label in which the scrollview alert will be displayed
668 _displayValueLabel = Text::create("ScrollView InnerSize Test", "fonts/Marker Felt.ttf", 32);
669 _displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
670 _displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f,
671 widgetSize.height / 2.0f + _displayValueLabel->getContentSize().height * 1.5f));
672 _uiLayer->addChild(_displayValueLabel);
673
674 // Add the alert
675 Text* alert = Text::create("ScrollView & InnerContainer should be align to the left and top point", "fonts/Marker Felt.ttf", 10);
676 alert->setColor(Color3B(159, 168, 176));
677 alert->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 3.075f));
678 _uiLayer->addChild(alert);
679
680 Layout* root = static_cast<Layout*>(_uiLayer->getChildByTag(81));
681
682 Layout* background = dynamic_cast<Layout*>(root->getChildByName("background_Panel"));
683
684 // Create the scrollview by vertical
685 ui::ScrollView* scrollView = ui::ScrollView::create();
686 scrollView->setContentSize(Size(200.0f, 50.0f));
687 Size backgroundSize = background->getContentSize();
688 scrollView->setPosition(Vec2((widgetSize.width - backgroundSize.width) / 2.0f +
689 (backgroundSize.width - scrollView->getContentSize().width) / 2.0f,
690 (widgetSize.height - backgroundSize.height) / 2.0f +
691 (backgroundSize.height - scrollView->getContentSize().height) / 2.0f));
692 scrollView->setScrollBarWidth(4);
693 scrollView->setTouchEnabled(false);
694 scrollView->setScrollBarPositionFromCorner(Vec2(2, 2));
695 scrollView->setScrollBarColor(Color3B::WHITE);
696 scrollView->setBackGroundColorType(Layout::BackGroundColorType::SOLID);
697 scrollView->setBackGroundColor(Color3B(150, 150, 100));
698 scrollView->getInnerContainer()->setBackGroundColorType(Layout::BackGroundColorType::SOLID);
699 scrollView->getInnerContainer()->setBackGroundColor(Color3B(50, 100, 50));
700 scrollView->getInnerContainer()->setBackGroundColorOpacity(100);
701 scrollView->setClippingEnabled(false);
702 _uiLayer->addChild(scrollView);
703
704 scrollView->setInnerContainerSize(Size(250.f, 100.f));
705
706 return true;
707 }
708
709 return false;
710}
711
712// UIScrollViewTestEvents
713
715 : _displayValueLabel(nullptr)
716{
717
718}
719
721{
722 if (UIScene::init())
723 {
724 Size widgetSize = _widget->getContentSize();
725
726 // Add a label in which the dragpanel events will be displayed
727 _displayValueLabel = Text::create("(no events)","fonts/Marker Felt.ttf",32);
728 _displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
729 _displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f + _displayValueLabel->getContentSize().height * 1.5f));
730 _uiLayer->addChild(_displayValueLabel);
731
732 // Add the alert
733 Text* alert = Text::create("ScrollView events","fonts/Marker Felt.ttf",30);
734 alert->setColor(Color3B(159, 168, 176));
735 alert->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 3.075f));
736 _uiLayer->addChild(alert);
737
738 Layout* root = static_cast<Layout*>(_uiLayer->getChildByTag(81));
739
740 Layout* background = static_cast<Layout*>(root->getChildByName("background_Panel"));
741
742 // Create the dragpanel
743 ui::ScrollView* scrollView = ui::ScrollView::create();
744 scrollView->setDirection(ui::ScrollView::Direction::BOTH);
745 scrollView->setTouchEnabled(true);
746 scrollView->setBounceEnabled(true);
747 scrollView->setBackGroundImageScale9Enabled(true);
748 scrollView->setBackGroundImage("cocosui/green_edit.png");
749 scrollView->setContentSize(Size(210, 122.5));
750 scrollView->setScrollBarWidth(4);
751 scrollView->setScrollBarPositionFromCorner(Vec2(6, 6));
752 Size backgroundSize = background->getContentSize();
753 scrollView->setPosition(Vec2((widgetSize.width - backgroundSize.width) / 2.0f +
754 (backgroundSize.width - scrollView->getContentSize().width) / 2.0f,
755 (widgetSize.height - backgroundSize.height) / 2.0f +
756 (backgroundSize.height - scrollView->getContentSize().height) / 2.0f));
757 ImageView* imageView = ImageView::create("Hello.png");
758 scrollView->addChild(imageView);
759
760 scrollView->setInnerContainerSize(imageView->getContentSize());
761 Size innerSize = scrollView->getInnerContainerSize();
762 imageView->setPosition(Vec2(innerSize.width / 2.0f, innerSize.height / 2.0f));
763
764 _uiLayer->addChild(scrollView);
765
766 // Jump to right bottom
767 scrollView->jumpToBottomRight();
768
769 auto getRandomColor = [] {
770 return Color4B(random(0, 255), random(0, 255), random(0, 255), 255);
771 };
772 scrollView->addEventListener([&](Ref*, ui::ScrollView::EventType e) {
773 switch ( e ) {
774 case ui::ScrollView::EventType::SCROLLING_BEGAN:
775 _displayValueLabel->setString("scrolling began!");
776 _displayValueLabel->setTextColor(getRandomColor());
777 break;
778 case ui::ScrollView::EventType::SCROLLING_ENDED:
779 _displayValueLabel->setString("scrolling ended!");
780 _displayValueLabel->setTextColor(getRandomColor());
781 break;
782 default: break;
783 }
784 });
785
786 return true;
787 }
788
789 return false;
790}
791
792// UIScrollViewStopScrollingTest
793
795 : _displayValueLabel(nullptr)
796 , _scrollView(nullptr)
797 , _remainingTime(0.0f)
798{
799
800}
801
803{
804 if (UIScene::init())
805 {
806 Size widgetSize = _widget->getContentSize();
807
808 // Add a label in which the time remaining till scrolling stop will be displayed.
809 _displayValueLabel = Text::create("Scrolling stop isn't scheduled", "fonts/Marker Felt.ttf", 32);
810 _displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
811 _displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f + _displayValueLabel->getContentSize().height * 1.5f));
812 _uiLayer->addChild(_displayValueLabel);
813
814 // Add the alert
815 Text* alert = Text::create("Click the button and start to scroll", "fonts/Marker Felt.ttf", 30);
816 alert->setColor(Color3B(159, 168, 176));
817 alert->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 3.075f));
818 _uiLayer->addChild(alert);
819
820 Layout* root = static_cast<Layout*>(_uiLayer->getChildByTag(81));
821
822 Layout* background = static_cast<Layout*>(root->getChildByName("background_Panel"));
823
824 // Create the dragpanel
825 _scrollView = ui::ScrollView::create();
826 _scrollView->setDirection(ui::ScrollView::Direction::BOTH);
827 _scrollView->setTouchEnabled(true);
828 _scrollView->setBounceEnabled(true);
829 _scrollView->setBackGroundImageScale9Enabled(true);
830 _scrollView->setBackGroundImage("cocosui/green_edit.png");
831 _scrollView->setContentSize(Size(210, 122.5));
832 _scrollView->setScrollBarWidth(4);
833 _scrollView->setScrollBarPositionFromCorner(Vec2(6, 6));
834 Size backgroundSize = background->getContentSize();
835 _scrollView->setPosition(Vec2((widgetSize.width - backgroundSize.width) / 2.0f +
836 (backgroundSize.width - _scrollView->getContentSize().width) / 2.0f,
837 (widgetSize.height - backgroundSize.height) / 2.0f +
838 (backgroundSize.height - _scrollView->getContentSize().height) / 2.0f));
839 ImageView* imageView = ImageView::create("Hello.png");
840 _scrollView->addChild(imageView);
841 _scrollView->setInnerContainerSize(imageView->getContentSize());
842 Size innerSize = _scrollView->getInnerContainerSize();
843 imageView->setPosition(Vec2(innerSize.width / 2.0f, innerSize.height / 2.0f));
844 _uiLayer->addChild(_scrollView);
845
846 // Log some ScrollView events.
847 _scrollView->addEventListener([&] (Ref*, ui::ScrollView::EventType e)
848 {
849 switch (e)
850 {
851 case ui::ScrollView::EventType::SCROLLING_BEGAN:
852 CCLOG("scrolling began!");
853 break;
854 case ui::ScrollView::EventType::SCROLLING_ENDED:
855 CCLOG("scrolling ended!");
856 break;
857 case ui::ScrollView::EventType::AUTOSCROLL_ENDED:
858 CCLOG("auto-scrolling ended!");
859 break;
860 default: break;
861 }
862 });
863
864 // Jump to right bottom
865 _scrollView->jumpToBottomRight();
866
867 // Add button that will schedule scrolling stop when it is clicked.
868 Button* button_scale9 = Button::create("cocosui/button.png", "cocosui/buttonHighlighted.png");
869 button_scale9->setTitleText("Stop scrolling in 3 sec.");
870 button_scale9->setScale9Enabled(true);
871 button_scale9->setContentSize(Size(120.0f, button_scale9->getVirtualRendererSize().height));
872 button_scale9->setPosition(Vec2(innerSize.width / 2.0f, innerSize.height / 2.0f));
873 button_scale9->addClickEventListener([this] (Ref*) { this->_remainingTime = 3.0f; });
874 _scrollView->addChild(button_scale9);
875
876 // Schedule update for this scene.
877 Director::getInstance()->getScheduler()->scheduleUpdate(this, 0, false);
878
879 return true;
880 }
881
882 return false;
883}
884
886{
887 UIScene::update(dt);
888
889 if (_remainingTime > 0.0f)
890 {
891 _remainingTime -= dt;
892
893 if (_remainingTime > 0.0f)
894 {
895 // Update timer caption.
896 char strRemainingTime[100];
897 sprintf(strRemainingTime, "Stop scrolling in %.1f sec.", _remainingTime);
898 _displayValueLabel->setString(std::string(strRemainingTime));
899 }
900 else
901 {
902 _scrollView->stopOverallScroll();
903
904 // Update timer caption.
905 std::string strRemainingTime = "Scrolling stop isn't scheduled";
906 _displayValueLabel->setString(strRemainingTime);
907
908 // Show hint label.
909 auto hintLabel = Label::createWithTTF("Stopped!", "fonts/Marker Felt.ttf", 32.0f);
910 Size contentSize = _uiLayer->getContentSize();
911 hintLabel->setPosition(Vec2(contentSize.width / 2.0f, contentSize.height / 2.0f));
912 hintLabel->runAction(Spawn::createWithTwoActions(
913 FadeOut::create(0.3f),
914 ScaleTo::create(0.3f, 2.0f)
915 ));
916 _uiLayer->addChild(hintLabel);
917 }
918 }
919}
#define ADD_TEST_CASE(__className__)
Definition: BaseTest.h:211
USING_NS_CC
cocos2d::Layer * _uiLayer
Definition: UIScene.h:44
virtual bool init() override
Definition: UIScene.cpp:46
cocos2d::ui::Layout * _widget
Definition: UIScene.h:45
virtual bool init() override
cocos2d::ui::Text * _displayValueLabel
virtual bool init() override
cocos2d::ui::Text * _displayValueLabel
cocos2d::ui::Text * _displayValueLabel
virtual bool init() override
virtual bool init() override
cocos2d::ui::Text * _displayValueLabel
virtual void update(float dt) override
cocos2d::ui::ScrollView * _scrollView
virtual bool init() override
cocos2d::ui::Text * _displayValueLabel
virtual bool init() override
cocos2d::ui::Text * _displayValueLabel
cocos2d::ui::Text * _displayValueLabel
virtual bool init() override
cocos2d::ui::Text * _displayValueLabel
virtual bool init() override
cocos2d::ui::Text * _displayValueLabel
virtual bool init() override