-
Notifications
You must be signed in to change notification settings - Fork 1
/
DPageIndicator.js
93 lines (90 loc) · 3.38 KB
/
DPageIndicator.js
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
/* global MainGame */
var DPageIndicator={
// TODO: set font style here
style: {font:"20px myKaiti", fill:"#ffffff", boundsAlignH: 'center', boundsAlignV: 'middle', shadowBlur: 1, shadowColor: "rgba(0,0,0,0.75)", shadowOffsetX: 2, shadowOffsetY: 2 },
// ****************** Attention ****************** //
// pageCount
// layOut: {margin,textPos}
// *pageChangedCallback: function(pageIndex)
// (* === optional)
//createNew: function(pageCount, layout, pageChangedCallback, priorityID){
createNew: function(margin, textPosition){
var v=MainGame.game.add.group();
// add two buttons as sprites
v.prevPage=MainGame.game.make.button(0,0,"triangleArrowButton",function(){
DPageIndicator.onPrevPage,v},v.prevPage,1,0,2,1);
v.prevPage.x = textPosition.x - margin;
v.prevPage.anchor.setTo(0.5,0.5);
v.prevPage.scale.setTo(-0.6,0.6);
v.prevPage.visible=false;
v.addChild(v.prevPage);
v.nextPage=MainGame.game.make.button(0,0,"triangleArrowButton",function(){
DPageIndicator.onNextPage,v},v.nextPage,1,0,2,1);
v.nextPage.x=textPosition.x + margin;
v.nextPage.anchor.setTo(0.5,0.5);
v.nextPage.scale.setTo(0.6,0.6);
v.nextPage.visible=false;
v.addChild(v.nextPage);
// add & update page text 4/7
v.pageText=MainGame.game.make.text(0,0,"", DPageIndicator.style);
v.addChild(v.pageText);
DPageIndicator._setPageText_(v);
v.pageText.x=textPosition.x, v.pageText.y=textPosition.y;
v.pageText.anchor.setTo(0.5,0.5);
// Class funcs
v.setModel=function(cur,max){DPageIndicator.setModel(v,cur,max)};
// sets the callback: function pageChange(curPage){}
v.setController=function(callback,_priorityID){return DPageIndicator.setController(v,callback,_priorityID)};
// returns the current page index (starting 0)
v.getCurPage=function(){return v.curPage};
// sets the callback func of PageChanged
v.setPageChangedCallback=function(callback){v.pageChangedCallback=callback};
return v;
},
setModel: function(v, curPage, pageCount){
console.assert(curPage>=0 && curPage<pageCount
|| curPage===0 && pageCount===0);
v.curPage=curPage;
v.pageCount=pageCount;
DPageIndicator._setPageText_(v);
DPageIndicator._setButtonVisibility_(v);
},
setController: function(v, callback, _priorityID){
var pageNames=["prevPage","nextPage"];
var pageCb=[DPageIndicator.onPrevPage, DPageIndicator.onNextPage];
for(var i=0;i<pageNames.length;i++){
v[pageNames[i]].inputEnabled=true;
v[pageNames[i]].input.priorityID=(_priorityID?_priorityID:20);
v[pageNames[i]].events.onInputDown.removeAll();
v[pageNames[i]].events.onInputDown.add(pageCb[i],v);
}
v.pageChangedCallback=callback;
},
onPrevPage: function(){
// console.log("DPageIndicator: onPrevPage");
this.curPage--;
DPageIndicator._setPageText_(this);
DPageIndicator._setButtonVisibility_(this);
if(this.pageChangedCallback)
this.pageChangedCallback(this.curPage);
},
onNextPage: function(){
// console.log("DPageIndicator: onNextPage");
this.curPage++;
DPageIndicator._setPageText_(this);
DPageIndicator._setButtonVisibility_(this);
if(this.pageChangedCallback)
this.pageChangedCallback(this.curPage);
},
_setPageText_: function(v){
if(v.curPage===0 && v.pageCount===0){
v.pageText.text="<Empty>";
return;
}
v.pageText.text=""+(v.curPage+1)+" / "+v.pageCount;
},
_setButtonVisibility_: function(v){
v.prevPage.visible=(v.curPage>0);
v.nextPage.visible=(v.curPage<v.pageCount-1);
}
};