-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPageBoard.cpp
More file actions
93 lines (83 loc) · 2.56 KB
/
Copy pathPageBoard.cpp
File metadata and controls
93 lines (83 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include "PageBoard.h"
#include "cocos/ui/CocosGUI.h"
#include "editor-support/cocostudio/CocoStudio.h"
#include "SupportFunctions.h"
using namespace cocostudio;
using namespace cocos2d::ui;
using namespace cocos2d::experimental;
void PageBoard::init()
{
_board = CSLoader::createNode("Csbs/PageBoard.csb");
_pageView = _board->getChildByName<PageView*>("PageView");
_board->setPosition(supportfunctions::middleCenter());
_world->addChild(_board);
hide();
}
void PageBoard::setTitle(const std::string& title)
{
std::stringstream titleSS;
titleSS << title;
_board->getChildByName<TextBMFont*>("Title")->setString(titleSS.str());
}
void PageBoard::resetPage()
{
getPageView()->removeAllPages();
}
void PageBoard::setPage(Layout* page)
{
auto pageView = getPageView();
pageView->setBounceEnabled(true);
pageView->addPage(page);
}
void PageBoard::onClose(const Touchable::Callback& callback)
{
onButtonPress("Close", callback);
}
void PageBoard::setPager()
{
auto pageView = getPageView();
pageView->setCurrentPageIndex(pageView->getItems().size() - 1);
pageView->scrollToItem(_sctollToPage);
renderPageNumber(pageView);
static_cast<ListView*>(pageView)->addEventListener([&](Ref* ref, ScrollView::EventType eventType){
if(eventType == ScrollView::EventType::CONTAINER_MOVED){
renderPageNumber(static_cast<PageView*>(ref));
}
});
auto prev = _board->getChildByName<Button*>("Prev");
onTouch(prev, [&, pageView](Ref* ref){
pageView->scrollToItem(pageView->getCurrentPageIndex() - 1);
});
auto next = _board->getChildByName<Button*>("Next");
onTouch(next, [&, pageView](Ref* ref){
pageView->scrollToItem(pageView->getCurrentPageIndex() + 1);
});
}
void PageBoard::setScrollToPage(const int scrollToPage)
{
_sctollToPage = scrollToPage;
}
void PageBoard::renderPageNumber(PageView* pageView)
{
auto pages = pageView->getItems();
auto currentPage = pageView->getCurrentPageIndex() + 1;
auto maxPage = pages.size();
std::stringstream pageSS;
pageSS << currentPage;
pageSS << "/";
pageSS << maxPage;
auto prev = _board->getChildByName<Button*>("Prev");
auto next = _board->getChildByName<Button*>("Next");
if (currentPage <= 1) {
prev->setVisible(false);
} else {
prev->setVisible(true);
}
if (currentPage >= maxPage) {
next->setVisible(false);
} else {
next->setVisible(true);
}
auto num = _board->getChildByName<TextBMFont*>("PageNum");
num->setString(pageSS.str());
}