PuzzleSDK
UIPageViewTest.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 "UIPageViewTest.h"
26#include "cocos2d.h"
27
29using namespace cocos2d::ui;
30
31UIPageViewTests::UIPageViewTests()
32{
42}
43
44// UIPageViewTest
46: _displayValueLabel(nullptr)
47{
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 dragpanel events will be displayed
62 _displayValueLabel = Text::create("Move by horizontal 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 +
66 _displayValueLabel->getContentSize().height * 1.5));
68
69 // Add the black background
70 Text* alert = Text::create("PageView", "fonts/Marker Felt.ttf", 30);
71 alert->setColor(Color3B(159, 168, 176));
72 alert->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 3.075f));
73 _uiLayer->addChild(alert);
74
75 Layout* root = static_cast<Layout*>(_uiLayer->getChildByTag(81));
76
77 Layout* background = dynamic_cast<Layout*>(root->getChildByName("background_Panel"));
78
79 // Create the page view
80 Size size(240, 130);
81 PageView* pageView = PageView::create();
82 pageView->setDirection(PageView::Direction::HORIZONTAL);
83 pageView->setContentSize(size);
84 Size backgroundSize = background->getContentSize();
85 pageView->setPosition((widgetSize - pageView->getContentSize()) / 2.0f);
86 pageView->removeAllItems();
87 pageView->setIndicatorEnabled(true);
88 pageView->setGlobalZOrder(200);
89
90 int pageCount = 4;
91 for (int i = 0; i < pageCount; ++i)
92 {
93 Layout* layout = Layout::create();
94 layout->setContentSize(size);
95
96 ImageView* imageView = ImageView::create("cocosui/scrollviewbg.png");
97 imageView->setScale9Enabled(true);
98 imageView->setContentSize(size);
99 imageView->setPosition(Vec2(layout->getContentSize().width / 2.0f, layout->getContentSize().height / 2.0f));
100 layout->addChild(imageView);
101
102 Text* label = Text::create(StringUtils::format("page %d",(i+1)), "fonts/Marker Felt.ttf", 30);
103 label->setColor(Color3B(192, 192, 192));
104 label->setPosition(Vec2(layout->getContentSize().width / 2.0f, layout->getContentSize().height / 2.0f));
105 layout->addChild(label);
106
107 pageView->insertCustomItem(layout, i);
108 }
109
110 pageView->removeItem(0);
111 pageView->scrollToItem(pageCount - 2);
112
113 cocos2d::log("TODO in %s %s %d", __FILE__, __FUNCTION__, __LINE__);
114
115 pageView->setIndicatorIndexNodesOpacity(255);
116
117 _uiLayer->addChild(pageView);
118
119 return true;
120 }
121 return false;
122}
123
124void UIPageViewTest::pageViewEvent(Ref *pSender, PageView::EventType type)
125{
126 switch (type)
127 {
128 case PageView::EventType::TURNING:
129 {
130 PageView* pageView = dynamic_cast<PageView*>(pSender);
131
132 _displayValueLabel->setString(StringUtils::format("page = %ld", static_cast<long>(pageView->getCurrentPageIndex() + 1)));
133 }
134 break;
135
136 default:
137 break;
138 }
139}
140
141
142// UIPageViewButtonTest
144: _displayValueLabel(nullptr)
145{
146
147}
148
150{
151}
152
154{
155 if (UIScene::init())
156 {
157 Size widgetSize = _widget->getContentSize();
158
159 // Add a label in which the drag panel events will be displayed
160 _displayValueLabel = Text::create("Move by horizontal direction", "fonts/Marker Felt.ttf", 32);
161 _displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
162 _displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f,
163 widgetSize.height / 2.0f +
164 _displayValueLabel->getContentSize().height * 1.5));
165 _uiLayer->addChild(_displayValueLabel);
166
167 // Add the black background
168 Text* alert = Text::create("PageView with Buttons", "fonts/Marker Felt.ttf", 30);
169 alert->setColor(Color3B(159, 168, 176));
170 alert->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 3.075f));
171 _uiLayer->addChild(alert);
172
173 Layout* root = static_cast<Layout*>(_uiLayer->getChildByTag(81));
174
175 Layout* background = dynamic_cast<Layout*>(root->getChildByName("background_Panel"));
176
177 // Create the page view
178 PageView* pageView = PageView::create();
179 pageView->setContentSize(Size(240.0f, 130.0f));
180 Size backgroundSize = background->getContentSize();
181 pageView->setPosition(Vec2((widgetSize.width - backgroundSize.width) / 2.0f +
182 (backgroundSize.width - pageView->getContentSize().width) / 2.0f,
183 (widgetSize.height - backgroundSize.height) / 2.0f +
184 (backgroundSize.height - pageView->getContentSize().height) / 2.0f));
185
186 pageView->removeAllItems();
187
188 int pageCount = 4;
189 for (int i = 0; i < pageCount; ++i)
190 {
191 HBox* outerBox = HBox::create();
192 outerBox->setContentSize(Size(240.0f, 130.0f));
193
194 for (int k = 0; k < 2; ++k) {
195 VBox* innerBox = VBox::create();
196
197 for (int j = 0; j < 3; j++) {
198 Button *btn = Button::create("cocosui/animationbuttonnormal.png",
199 "cocosui/animationbuttonpressed.png");
200 btn->setName(StringUtils::format("button %d", j));
201 btn->addTouchEventListener( CC_CALLBACK_2(UIPageViewButtonTest::onButtonClicked, this));
202
203 innerBox->addChild(btn);
204 }
205
206 LinearLayoutParameter *parameter = LinearLayoutParameter::create();
207 parameter->setMargin(Margin(0.0f,0.0f,100.0f,0.0f));
208 innerBox->setLayoutParameter(parameter);
209
210 outerBox->addChild(innerBox);
211
212 }
213
214 pageView->insertCustomItem(outerBox, i);
215 }
216
217 pageView->removeItem(0);
218
219 pageView->addEventListener((PageView::ccPageViewCallback)CC_CALLBACK_2(UIPageViewButtonTest::pageViewEvent, this));
220
221 _uiLayer->addChild(pageView);
222
223 return true;
224 }
225 return false;
226}
227
228void UIPageViewButtonTest::onButtonClicked(Ref* sender, Widget::TouchEventType type)
229{
230 if(type == Widget::TouchEventType::ENDED) {
231 log("button %s clicked", static_cast<Button*>(sender)->getName().c_str());
232 }
233}
234
235
236void UIPageViewButtonTest::pageViewEvent(Ref *pSender, PageView::EventType type)
237{
238 switch (type)
239 {
240 case PageView::EventType::TURNING:
241 {
242 PageView* pageView = dynamic_cast<PageView*>(pSender);
243
244 _displayValueLabel->setString(StringUtils::format("page = %ld", static_cast<long>(pageView->getCurrentPageIndex() + 1)));
245 }
246 break;
247
248 default:
249 break;
250 }
251}
252
253
254// UIPageViewTouchPropagationTest
256: _displayValueLabel(nullptr)
257{
258
259}
260
262{
263}
264
266{
267 if (UIScene::init())
268 {
269 Size widgetSize = _widget->getContentSize();
270
271 // Add a label in which the dragpanel events will be displayed
272 _displayValueLabel = Text::create("Move by horizontal direction", "fonts/Marker Felt.ttf", 32);
273 _displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
274 _displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f,
275 widgetSize.height / 2.0f +
276 _displayValueLabel->getContentSize().height * 1.5));
277 _uiLayer->addChild(_displayValueLabel);
278
279 // Add the black background
280 Text* alert = Text::create("PageView Touch Propagation", "fonts/Marker Felt.ttf", 30);
281 alert->setColor(Color3B(159, 168, 176));
282 alert->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 3.075f));
283 _uiLayer->addChild(alert);
284
285 Layout* root = static_cast<Layout*>(_uiLayer->getChildByTag(81));
286
287 Layout* background = dynamic_cast<Layout*>(root->getChildByName("background_Panel"));
288
289 // Create the page view
290 PageView* pageView = PageView::create();
291 pageView->setContentSize(Size(240.0f, 130.0f));
292 pageView->setAnchorPoint(Vec2(0.5f,0.5f));
293 Size backgroundSize = background->getContentSize();
294 pageView->setPosition(Vec2(widgetSize.width / 2.0f ,widgetSize.height / 2.0f));
295 pageView->setBackGroundColor(Color3B::GREEN);
296 pageView->setBackGroundColorType(Layout::BackGroundColorType::SOLID);
297
298 int pageCount = 4;
299 for (int i = 0; i < pageCount; ++i)
300 {
301 HBox* outerBox = HBox::create();
302 outerBox->setContentSize(Size(240.0f, 130.0f));
303
304 for (int k = 0; k < 2; ++k) {
305 VBox* innerBox = VBox::create();
306
307 for (int j = 0; j < 3; j++) {
308 Button *btn = Button::create("cocosui/animationbuttonnormal.png",
309 "cocosui/animationbuttonpressed.png");
310 btn->setName(StringUtils::format("button %d", j));
311 btn->addTouchEventListener( CC_CALLBACK_2(UIPageViewTouchPropagationTest::onButtonClicked, this));
312
313 innerBox->addChild(btn);
314 }
315
316 LinearLayoutParameter *parameter = LinearLayoutParameter::create();
317 parameter->setMargin(Margin(0.0f,0.0f,100.0f,0.0f));
318 innerBox->setLayoutParameter(parameter);
319
320 outerBox->addChild(innerBox);
321
322 }
323
324 pageView->insertCustomItem(outerBox, i);
325 }
326
327 pageView->addEventListener((PageView::ccPageViewCallback)CC_CALLBACK_2(UIPageViewTouchPropagationTest::pageViewEvent, this));
328 pageView->setName("pageView");
329 pageView->addTouchEventListener([](Ref* sender, Widget::TouchEventType type){
330 if (type == Widget::TouchEventType::BEGAN)
331 {
332 CCLOG("page view touch began");
333 }
334 else if(type == Widget::TouchEventType::MOVED)
335 {
336 CCLOG("page view touch moved");
337 }
338 else if(type == Widget::TouchEventType::ENDED)
339 {
340 CCLOG("page view touch ended");
341 }
342 else
343 {
344 CCLOG("page view touch cancelled");
345 }
346 });
347 _uiLayer->addChild(pageView);
348
349 Text *propagationText = Text::create("Allow Propagation", "Arial", 10);
350 propagationText->setAnchorPoint(Vec2(0.0f,0.5f));
351 propagationText->setTextColor(Color4B::RED);
352 propagationText->setPosition(Vec2(0.0f, pageView->getPosition().y + 50));
353 _uiLayer->addChild(propagationText);
354
355 Text *swallowTouchText = Text::create("Swallow Touches", "Arial", 10);
356 swallowTouchText->setAnchorPoint(Vec2(0.f,0.5f));
357 swallowTouchText->setTextColor(Color4B::RED);
358 swallowTouchText->setPosition(Vec2(0.0f, pageView->getPosition().y));
359 _uiLayer->addChild(swallowTouchText);
360
361 // Create the checkbox
362 CheckBox* checkBox1 = CheckBox::create("cocosui/check_box_normal.png",
363 "cocosui/check_box_normal_press.png",
364 "cocosui/check_box_active.png",
365 "cocosui/check_box_normal_disable.png",
366 "cocosui/check_box_active_disable.png");
367 checkBox1->setPosition(propagationText->getPosition() + Vec2(propagationText->getContentSize().width/2, -20.0f));
368
369 checkBox1->setName("propagation");
370 _uiLayer->addChild(checkBox1);
371
372
373 // Create the checkbox
374 CheckBox* checkBox2 = CheckBox::create("cocosui/check_box_normal.png",
375 "cocosui/check_box_normal_press.png",
376 "cocosui/check_box_active.png",
377 "cocosui/check_box_normal_disable.png",
378 "cocosui/check_box_active_disable.png");
379 checkBox2->setPosition(swallowTouchText->getPosition() + Vec2(swallowTouchText->getContentSize().width/2, -20.0f));
380
381 checkBox2->setName("swallow");
382 _uiLayer->addChild(checkBox2);
383
384
385 auto eventListener = EventListenerTouchOneByOne::create();
386 eventListener->onTouchBegan = [](Touch* touch, Event* event) -> bool{
387 CCLOG("layout receives touches");
388 return true;
389 };
390 _eventDispatcher->addEventListenerWithSceneGraphPriority(eventListener, this);
391
392 return true;
393 }
394 return false;
395}
396
397void UIPageViewTouchPropagationTest::onButtonClicked(Ref* pSender, Widget::TouchEventType type)
398{
399 Button *btn = (Button*)pSender;
400 CheckBox *ck1 = (CheckBox*)_uiLayer->getChildByName("propagation");
401 CheckBox *ck2 = (CheckBox*)_uiLayer->getChildByName("swallow");
402 auto pageView = (PageView*)_uiLayer->getChildByName("pageView");
403
404 if (type == Widget::TouchEventType::BEGAN)
405 {
406 if (ck1->isSelected())
407 {
408 btn->setPropagateTouchEvents(true);
409 pageView->setPropagateTouchEvents(true);
410
411 }else
412 {
413 btn->setPropagateTouchEvents(false);
414 pageView->setPropagateTouchEvents(false);
415 }
416
417 if (ck2->isSelected())
418 {
419 btn->setSwallowTouches(true);
420 pageView->setSwallowTouches(true);
421 }else
422 {
423 btn->setSwallowTouches(false);
424 pageView->setSwallowTouches(false);
425 }
426 }
427 if (type == Widget::TouchEventType::ENDED) {
428 CCLOG("button clicked");
429 }
430}
431
432
433void UIPageViewTouchPropagationTest::pageViewEvent(Ref *pSender, PageView::EventType type)
434{
435 switch (type)
436 {
437 case PageView::EventType::TURNING:
438 {
439 PageView* pageView = dynamic_cast<PageView*>(pSender);
440
441 _displayValueLabel->setString(StringUtils::format("page = %ld", static_cast<long>(pageView->getCurrentPageIndex() + 1)));
442 }
443 break;
444
445 default:
446 break;
447 }
448}
449
450// UIPageViewDynamicAddAndRemoveTest
452: _displayValueLabel(nullptr)
453{
454
455}
456
458{
459}
460
462{
463 if (UIScene::init())
464 {
465 Size widgetSize = _widget->getContentSize();
466
467 // Add a label in which the dragpanel events will be displayed
468 _displayValueLabel = Text::create("Click Buttons on the Left", "fonts/Marker Felt.ttf", 32);
469 _displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
470 _displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f,
471 widgetSize.height / 2.0f +
472 _displayValueLabel->getContentSize().height * 1.5));
473 _uiLayer->addChild(_displayValueLabel);
474
475 // Add the black background
476 Text* alert = Text::create("PageView Dynamic Modification", "fonts/Marker Felt.ttf", 30);
477 alert->setColor(Color3B(159, 168, 176));
478 alert->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 3.075f));
479 _uiLayer->addChild(alert);
480
481 Layout* root = static_cast<Layout*>(_uiLayer->getChildByTag(81));
482
483 Layout* background = dynamic_cast<Layout*>(root->getChildByName("background_Panel"));
484
485 // Create the page view
486 PageView* pageView = PageView::create();
487 pageView->setDirection(ui::PageView::Direction::VERTICAL);
488 pageView->setContentSize(Size(240.0f, 130.0f));
489 pageView->setAnchorPoint(Vec2(0.5f,0.5f));
490 Size backgroundSize = background->getContentSize();
491 pageView->setPosition(Vec2(widgetSize.width / 2.0f ,widgetSize.height / 2.0f));
492 pageView->setBackGroundColor(Color3B::GREEN);
493 pageView->setBackGroundColorType(Layout::BackGroundColorType::SOLID);
494 pageView->setIndicatorEnabled(true);
495 pageView->setIndicatorSpaceBetweenIndexNodes(10);
496
497 int pageCount = 4;
498 for (int i = 0; i < pageCount; ++i)
499 {
500 HBox* outerBox = HBox::create();
501 outerBox->setContentSize(Size(240.0f, 130.0f));
502
503 for (int k = 0; k < 2; ++k)
504 {
505 VBox* innerBox = VBox::create();
506
507 for (int j = 0; j < 3; j++)
508 {
509 Button *btn = Button::create("cocosui/animationbuttonnormal.png",
510 "cocosui/animationbuttonpressed.png");
511 btn->setName(StringUtils::format("button %d", j));
512
513 innerBox->addChild(btn);
514 }
515
516 LinearLayoutParameter *parameter = LinearLayoutParameter::create();
517 parameter->setMargin(Margin(0.0f,0.0f,100.0f,0.0f));
518 innerBox->setLayoutParameter(parameter);
519
520 outerBox->addChild(innerBox);
521
522 }
523
524 pageView->insertCustomItem(outerBox, i);
525 }
526
527 pageView->addEventListener((PageView::ccPageViewCallback)CC_CALLBACK_2(UIPageViewDynamicAddAndRemoveTest::pageViewEvent, this));
528 pageView->setName("pageView");
529 _uiLayer->addChild(pageView);
530
531 //add buttons
532 auto button = Button::create();
533 button->setPositionNormalized(Vec2(0.12f,0.7f));
534 button->setTitleText("Add A Page");
535 button->setZoomScale(0.3f);
536 button->setPressedActionEnabled(true);
537 button->setTitleColor(Color3B::RED);
538 button->addClickEventListener([=](Ref* sender)
539 {
540 HBox* outerBox = HBox::create();
541 outerBox->setContentSize(Size(240.0f, 130.0f));
542
543 for (int k = 0; k < 2; ++k)
544 {
545 VBox* innerBox = VBox::create();
546 for (int j = 0; j < 3; j++)
547 {
548 Button *btn = Button::create("cocosui/animationbuttonnormal.png",
549 "cocosui/animationbuttonpressed.png");
550 btn->setName(StringUtils::format("button %d", j));
551 innerBox->addChild(btn);
552 }
553
554 LinearLayoutParameter *parameter = LinearLayoutParameter::create();
555 parameter->setMargin(Margin(0.0f,0.0f,100.0f,0.0f));
556 innerBox->setLayoutParameter(parameter);
557 outerBox->addChild(innerBox);
558 }
559
560 pageView->pushBackCustomItem(outerBox);
561 _displayValueLabel->setString(StringUtils::format("page count = %ld", static_cast<long>(pageView->getItems().size())));
562 CCLOG("current page index = %zd", pageView->getCurrentPageIndex());
563 });
564 _uiLayer->addChild(button);
565
566 auto button2 = Button::create();
567 button2->setPositionNormalized(Vec2(0.12f,0.5f));
568 button2->setTitleText("Remove A Page");
569 button2->setZoomScale(0.3f);
570 button2->setPressedActionEnabled(true);
571 button2->setTitleColor(Color3B::RED);
572 button2->addClickEventListener([=](Ref* sender)
573 {
574 if (pageView->getItems().size() > 0)
575 {
576 pageView->removeItem(pageView->getItems().size()-1);
577 }
578 else
579 {
580 CCLOG("There is no page to remove!");
581 }
582 _displayValueLabel->setString(StringUtils::format("page count = %ld", static_cast<long>(pageView->getItems().size())));
583 CCLOG("current page index = %zd", pageView->getCurrentPageIndex());
584
585 });
586 _uiLayer->addChild(button2);
587
588 auto button3 = Button::create();
589 button3->setPositionNormalized(Vec2(0.12f,0.3f));
590 button3->setTitleText("Remove All Pages");
591 button3->setZoomScale(0.3f);
592 button3->setPressedActionEnabled(true);
593 button3->setTitleColor(Color3B::RED);
594 button3->addClickEventListener([=](Ref* sender)
595 {
596 pageView->removeAllItems();
597 _displayValueLabel->setString(StringUtils::format("page count = %ld", static_cast<long>(pageView->getItems().size())));
598 CCLOG("current page index = %zd", pageView->getCurrentPageIndex());
599
600 });
601 _uiLayer->addChild(button3);
602
603 auto button4 = (ui::Button*)button3->clone();
604 button4->setTitleText("Scroll to Page4");
605 button4->setPositionNormalized(Vec2(0.85f, 0.5f));
606 button4->addClickEventListener([=](Ref* sender){
607 pageView->scrollToItem(3);
608 CCLOG("current page index = %zd", pageView->getCurrentPageIndex());
609 });
610 _uiLayer->addChild(button4);
611
612 return true;
613 }
614 return false;
615}
616
617
618void UIPageViewDynamicAddAndRemoveTest::pageViewEvent(Ref *pSender, PageView::EventType type)
619{
620 switch (type)
621 {
622 case PageView::EventType::TURNING:
623 {
624 PageView* pageView = dynamic_cast<PageView*>(pSender);
625
626 _displayValueLabel->setString(StringUtils::format("page = %ld", static_cast<long>((pageView->getCurrentPageIndex() + 1))));
627 }
628 break;
629
630 default:
631 break;
632 }
633}
634
635
636// UIPageViewJumpToPageTest
638: _displayValueLabel(nullptr)
639{
640
641}
642
644{
645}
646
648{
649 if (UIScene::init())
650 {
651 Size widgetSize = _widget->getContentSize();
652
653 // Add a label in which the dragpanel events will be displayed
654 _displayValueLabel = Text::create("setCurrentPageIndex API Test", "fonts/Marker Felt.ttf", 32);
655 _displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
656 _displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f,
657 widgetSize.height / 2.0f +
658 _displayValueLabel->getContentSize().height * 1.5));
659 _uiLayer->addChild(_displayValueLabel);
660
661 // Add the black background
662 Text* alert = Text::create("PageView", "fonts/Marker Felt.ttf", 30);
663 alert->setColor(Color3B(159, 168, 176));
664 alert->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 3.075f));
665 _uiLayer->addChild(alert);
666
667 Layout* root = static_cast<Layout*>(_uiLayer->getChildByTag(81));
668
669 Layout* background = dynamic_cast<Layout*>(root->getChildByName("background_Panel"));
670
671 // Create the page view
672 PageView* pageView = PageView::create();
673 pageView->setContentSize(Size(240.0f, 130.0f));
674 Size backgroundSize = background->getContentSize();
675 pageView->setPosition(Vec2((widgetSize.width - backgroundSize.width) / 2.0f +
676 (backgroundSize.width - pageView->getContentSize().width) / 2.0f,
677 (widgetSize.height - backgroundSize.height) / 2.0f +
678 (backgroundSize.height - pageView->getContentSize().height) / 2.0f));
679 pageView->setIndicatorEnabled(true);
680 pageView->removeAllItems();
681
682 int pageCount = 4;
683 for (int i = 0; i < pageCount; ++i)
684 {
685 Layout* layout = Layout::create();
686 layout->setContentSize(Size(240.0f, 130.0f));
687
688 ImageView* imageView = ImageView::create("cocosui/scrollviewbg.png");
689 imageView->setScale9Enabled(true);
690 imageView->setContentSize(Size(240.0f, 130.0f));
691 imageView->setPosition(Vec2(layout->getContentSize().width / 2.0f, layout->getContentSize().height / 2.0f));
692 layout->addChild(imageView);
693
694 Text* label = Text::create(StringUtils::format("page %d",(i+1)), "fonts/Marker Felt.ttf", 30);
695 label->setColor(Color3B(192, 192, 192));
696 label->setPosition(Vec2(layout->getContentSize().width / 2.0f, layout->getContentSize().height / 2.0f));
697 layout->addChild(label);
698
699 pageView->insertCustomItem(layout, i);
700 }
701
702 pageView->setCurrentPageIndex(1);
703
704 //add buttons to jump to specific page
705 auto button1 = ui::Button::create();
706 button1->setPositionNormalized(Vec2(0.1f, 0.75f));
707 button1->setTitleText("Jump to Page1");
708 CCLOG("button1 content Size = %f, %f", button1->getContentSize().width,
709 button1->getContentSize().height);
710 button1->addClickEventListener([=](Ref*){
711 pageView->setCurrentPageIndex(0);
712 });
713 _uiLayer->addChild(button1);
714
715 auto button2 = static_cast<ui::Button*>(button1->clone());
716 button2->setTitleText("Jump to Page2");
717 button2->setPositionNormalized(Vec2(0.1f, 0.65f));
718 CCLOG("button2 content Size = %f, %f", button2->getContentSize().width,
719 button2->getContentSize().height);
720 button2->addClickEventListener([=](Ref*){
721 pageView->setCurrentPageIndex(1);
722 });
723 _uiLayer->addChild(button2);
724
725 auto button3 = static_cast<ui::Button*>(button2->clone());
726 button3->setTitleText("Jump to Page3");
727 button3->setPositionNormalized(Vec2(0.9f, 0.75f));
728 button3->addClickEventListener([=](Ref*){
729 pageView->setCurrentPageIndex(2);
730 });
731 _uiLayer->addChild(button3);
732
733 auto button4 = static_cast<ui::Button*>(button2->clone());
734 button4->setTitleText("Jump to Page4");
735 button4->setPositionNormalized(Vec2(0.9f, 0.65f));
736 button4->addClickEventListener([=](Ref*){
737 pageView->setCurrentPageIndex(3);
738 });
739 _uiLayer->addChild(button4);
740 _uiLayer->addChild(pageView);
741
742 return true;
743 }
744 return false;
745}
746
747// UIPageViewVerticalTest
749: _displayValueLabel(nullptr)
750{
751
752}
753
755{
756}
757
759{
760 if (UIScene::init())
761 {
762 Size widgetSize = _widget->getContentSize();
763
764 // Add a label in which the dragpanel events will be displayed
765 _displayValueLabel = Text::create("Move by Vertical direction", "fonts/Marker Felt.ttf", 32);
766 _displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
767 _displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f,
768 widgetSize.height / 2.0f +
769 _displayValueLabel->getContentSize().height * 1.5));
770 _uiLayer->addChild(_displayValueLabel);
771
772 // Add the black background
773 Text* alert = Text::create("PageView", "fonts/Marker Felt.ttf", 30);
774 alert->setColor(Color3B(159, 168, 176));
775 alert->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 3.075f));
776 _uiLayer->addChild(alert);
777
778 Layout* root = static_cast<Layout*>(_uiLayer->getChildByTag(81));
779
780 Layout* background = dynamic_cast<Layout*>(root->getChildByName("background_Panel"));
781
782 // Create the page view
783 PageView* pageView = PageView::create();
784 pageView->setIndicatorEnabled(true);
785 pageView->setDirection(ui::PageView::Direction::VERTICAL);
786 pageView->setContentSize(Size(240.0f, 130.0f));
787 Size backgroundSize = background->getContentSize();
788 pageView->setPosition((widgetSize - pageView->getContentSize()) / 2.0f);
789 pageView->removeAllItems();
790
791 int pageCount = 4;
792 for (int i = 0; i < pageCount; ++i)
793 {
794 Layout* layout = Layout::create();
795 layout->setContentSize(Size(240.0f, 130.0f));
796
797 ImageView* imageView = ImageView::create("cocosui/scrollviewbg.png");
798 imageView->setScale9Enabled(true);
799 imageView->setContentSize(Size(240.0f, 130.0f));
800 imageView->setPosition(Vec2(layout->getContentSize().width / 2.0f, layout->getContentSize().height / 2.0f));
801 layout->addChild(imageView);
802
803 Text* label = Text::create(StringUtils::format("page %d",(i+1)), "fonts/Marker Felt.ttf", 30);
804 label->setColor(Color3B(192, 192, 192));
805 label->setPosition(Vec2(layout->getContentSize().width / 2.0f, layout->getContentSize().height / 2.0f));
806 layout->addChild(label);
807
808 pageView->insertCustomItem(layout, i);
809 }
810
811 pageView->addEventListener((PageView::ccPageViewCallback)CC_CALLBACK_2(UIPageViewVerticalTest::pageViewEvent, this));
812
813 _uiLayer->addChild(pageView);
814
815 return true;
816 }
817 return false;
818}
819
820void UIPageViewVerticalTest::pageViewEvent(Ref *pSender, PageView::EventType type)
821{
822 switch (type)
823 {
824 case PageView::EventType::TURNING:
825 {
826 PageView* pageView = dynamic_cast<PageView*>(pSender);
827
828 _displayValueLabel->setString(StringUtils::format("page = %ld", static_cast<long>(pageView->getCurrentPageIndex() + 1)));
829 }
830 break;
831
832 default:
833 break;
834 }
835}
836
837// UIPageViewDisableTouchTest
839: _displayValueLabel(nullptr)
840{
841
842}
843
845{
846}
847
849{
850 if (UIScene::init())
851 {
852 Size widgetSize = _widget->getContentSize();
853
854 // Add a label in which the dragpanel events will be displayed
855 _displayValueLabel = Text::create("PageView disable touch", "fonts/Marker Felt.ttf", 32);
856 _displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
857 _displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f,
858 widgetSize.height / 2.0f +
859 _displayValueLabel->getContentSize().height * 1.5));
860 _uiLayer->addChild(_displayValueLabel);
861
862 // Add the black background
863 Text* alert = Text::create("PageView", "fonts/Marker Felt.ttf", 30);
864 alert->setColor(Color3B(159, 168, 176));
865 alert->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 3.075f));
866 _uiLayer->addChild(alert);
867
868 Layout* root = static_cast<Layout*>(_uiLayer->getChildByTag(81));
869
870 Layout* background = dynamic_cast<Layout*>(root->getChildByName("background_Panel"));
871
872 // Create the page view
873 PageView* pageView = PageView::create();
874 pageView->setContentSize(Size(240.0f, 130.0f));
875 Size backgroundSize = background->getContentSize();
876 pageView->setPosition(Vec2((widgetSize.width - backgroundSize.width) / 2.0f +
877 (backgroundSize.width - pageView->getContentSize().width) / 2.0f,
878 (widgetSize.height - backgroundSize.height) / 2.0f +
879 (backgroundSize.height - pageView->getContentSize().height) / 2.0f));
880 pageView->setDirection(ui::PageView::Direction::VERTICAL);
881 pageView->setTouchEnabled(false);
882 pageView->removeAllItems();
883
884 int pageCount = 4;
885 for (int i = 0; i < pageCount; ++i)
886 {
887 Layout* layout = Layout::create();
888 layout->setContentSize(Size(240.0f, 130.0f));
889
890 ImageView* imageView = ImageView::create("cocosui/scrollviewbg.png");
891 imageView->setScale9Enabled(true);
892 imageView->setContentSize(Size(240.0f, 130.0f));
893 imageView->setPosition(Vec2(layout->getContentSize().width / 2.0f, layout->getContentSize().height / 2.0f));
894 layout->addChild(imageView);
895
896 Text* label = Text::create(StringUtils::format("page %d",(i+1)), "fonts/Marker Felt.ttf", 30);
897 label->setColor(Color3B(192, 192, 192));
898 label->setPosition(Vec2(layout->getContentSize().width / 2.0f, layout->getContentSize().height / 2.0f));
899 layout->addChild(label);
900
901 pageView->insertCustomItem(layout, i);
902 }
903
904 _uiLayer->addChild(pageView);
905
906 return true;
907 }
908 return false;
909}
910
911// UIPageViewTest
913 : _displayValueLabel(nullptr)
914{
915
916}
917
919{
920}
921
923{
924 if (UIScene::init())
925 {
926 Size widgetSize = _widget->getContentSize();
927
928 // Add a label in which the dragpanel events will be displayed
929 _displayValueLabel = Text::create("Move by horizontal direction", "fonts/Marker Felt.ttf", 32);
930 _displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
931 _displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f,
932 widgetSize.height / 2.0f +
933 _displayValueLabel->getContentSize().height * 1.5));
934 _uiLayer->addChild(_displayValueLabel);
935
936 // Add the black background
937 Text* alert = Text::create("PageView", "fonts/Marker Felt.ttf", 30);
938 alert->setColor(Color3B(159, 168, 176));
939 alert->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 3.075f));
940 _uiLayer->addChild(alert);
941
942 Layout* root = static_cast<Layout*>(_uiLayer->getChildByTag(81));
943
944 Layout* background = dynamic_cast<Layout*>(root->getChildByName("background_Panel"));
945
946 // Create the page view
947 Size size(240, 130);
948 PageView* pageView = PageView::create();
949 pageView->setDirection(PageView::Direction::HORIZONTAL);
950 pageView->setContentSize(size);
951 Size backgroundSize = background->getContentSize();
952 pageView->setPosition((widgetSize - pageView->getContentSize()) / 2.0f);
953 pageView->removeAllItems();
954 pageView->setIndicatorEnabled(true);
955
956 int pageCount = 4;
957 for (int i = 0; i < pageCount; ++i)
958 {
959 ImageView* imageView = ImageView::create("cocosui/scrollviewbg.png");
960 imageView->setScale9Enabled(true);
961
962 Text* label = Text::create(StringUtils::format("page %d", (i + 1)), "fonts/Marker Felt.ttf", 30);
963 label->setColor(Color3B(192, 192, 192));
964 label->setAnchorPoint(Vec2::ZERO);
965 imageView->addChild(label);
966
967 pageView->insertCustomItem(imageView, i);
968 }
969
970 pageView->addEventListener((PageView::ccPageViewCallback)CC_CALLBACK_2(UIPageViewChildSizeTest::pageViewEvent, this));
971
972 _uiLayer->addChild(pageView);
973
974 return true;
975 }
976 return false;
977}
978
979void UIPageViewChildSizeTest::pageViewEvent(Ref *pSender, PageView::EventType type)
980{
981 switch (type)
982 {
983 case PageView::EventType::TURNING:
984 {
985 PageView* pageView = dynamic_cast<PageView*>(pSender);
986
987 _displayValueLabel->setString(StringUtils::format("page = %ld", static_cast<long>(pageView->getCurrentPageIndex() + 1)));
988 }
989 break;
990
991 default:
992 break;
993 }
994}
995
996// UIPageViewIndicatorTest
998: _displayValueLabel(nullptr)
999{
1000
1001}
1002
1004{
1005}
1006
1008{
1009 if (UIScene::init())
1010 {
1011 Size widgetSize = _widget->getContentSize();
1012
1013 // Add a label in which the dragpanel events will be displayed
1014 _displayValueLabel = Text::create("PageView indicator custom texture\nscale : 0.5, index color: RED", "fonts/Marker Felt.ttf", 16);
1015 _displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
1016 _displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f,
1017 widgetSize.height / 2.0f +
1018 _displayValueLabel->getContentSize().height));
1019 _uiLayer->addChild(_displayValueLabel);
1020
1021 // Add the black background
1022 Text* alert = Text::create("PageView", "fonts/Marker Felt.ttf", 30);
1023 alert->setColor(Color3B(159, 168, 176));
1024 alert->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 3.075f));
1025 _uiLayer->addChild(alert);
1026
1027 Layout* root = static_cast<Layout*>(_uiLayer->getChildByTag(81));
1028
1029 Layout* background = dynamic_cast<Layout*>(root->getChildByName("background_Panel"));
1030
1031 // Create the page view
1032 PageView* pageView = PageView::create();
1033 pageView->setContentSize(Size(240.0f, 130.0f));
1034 Size backgroundSize = background->getContentSize();
1035 pageView->setPosition(Vec2((widgetSize.width - backgroundSize.width) / 2.0f +
1036 (backgroundSize.width - pageView->getContentSize().width) / 2.0f,
1037 (widgetSize.height - backgroundSize.height) / 2.0f +
1038 (backgroundSize.height - pageView->getContentSize().height) / 2.0f));
1039 pageView->setDirection(ui::PageView::Direction::VERTICAL);
1040 pageView->removeAllItems();
1041
1042 pageView->setIndicatorEnabled(true);
1043 pageView->setIndicatorSpaceBetweenIndexNodes(5);
1044 pageView->setIndicatorIndexNodesScale(0.5);
1045 pageView->setIndicatorIndexNodesTexture("cocosui/green_edit.png");
1046 pageView->setIndicatorIndexNodesColor(Color3B::RED);
1047
1048 int pageCount = 4;
1049 for (int i = 0; i < pageCount; ++i)
1050 {
1051 Layout* layout = Layout::create();
1052 layout->setContentSize(Size(240.0f, 130.0f));
1053
1054 ImageView* imageView = ImageView::create("cocosui/scrollviewbg.png");
1055 imageView->setScale9Enabled(true);
1056 imageView->setContentSize(Size(240.0f, 130.0f));
1057 imageView->setPosition(Vec2(layout->getContentSize().width / 2.0f, layout->getContentSize().height / 2.0f));
1058 layout->addChild(imageView);
1059
1060 Text* label = Text::create(StringUtils::format("page %d",(i+1)), "fonts/Marker Felt.ttf", 30);
1061 label->setColor(Color3B(192, 192, 192));
1062 label->setPosition(Vec2(layout->getContentSize().width / 2.0f, layout->getContentSize().height / 2.0f));
1063 layout->addChild(label);
1064
1065 pageView->insertCustomItem(layout, i);
1066 }
1067
1068 _uiLayer->addChild(pageView);
1069
1070
1071 return true;
1072 }
1073 return false;
1074}
1075
1076
#define ADD_TEST_CASE(__className__)
Definition: BaseTest.h:211
USING_NS_CC
virtual bool init() override
cocos2d::ui::Text * _displayValueLabel
void pageViewEvent(cocos2d::Ref *sender, cocos2d::ui::PageView::EventType type)
void onButtonClicked(cocos2d::Ref *sender, cocos2d::ui::Widget::TouchEventType type)
void pageViewEvent(cocos2d::Ref *sender, cocos2d::ui::PageView::EventType type)
virtual bool init() override
cocos2d::ui::Text * _displayValueLabel
cocos2d::ui::Text * _displayValueLabel
virtual bool init() override
void pageViewEvent(cocos2d::Ref *sender, cocos2d::ui::PageView::EventType type)
cocos2d::ui::Text * _displayValueLabel
cocos2d::ui::Text * _displayValueLabel
virtual bool init() override
virtual bool init() override
cocos2d::ui::Text * _displayValueLabel
cocos2d::ui::Text * _displayValueLabel
virtual bool init() override
void pageViewEvent(cocos2d::Ref *sender, cocos2d::ui::PageView::EventType type)
void onButtonClicked(cocos2d::Ref *sender, cocos2d::ui::Widget::TouchEventType type)
virtual bool init() override
void pageViewEvent(cocos2d::Ref *sender, cocos2d::ui::PageView::EventType type)
cocos2d::ui::Text * _displayValueLabel
virtual bool init() override
void pageViewEvent(cocos2d::Ref *sender, cocos2d::ui::PageView::EventType type)
cocos2d::ui::Text * _displayValueLabel
cocos2d::Layer * _uiLayer
Definition: UIScene.h:44
virtual bool init() override
Definition: UIScene.cpp:46
cocos2d::ui::Layout * _widget
Definition: UIScene.h:45