GameLobby.js
13.1 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
//游戏大厅列表页
var Common = require('Common');
var TVFocus = require('TVFocus');
var FocusInfo = require('FocusInfo');
var TVCanvas = require('TVCanvas');
var ListView = require('ListView');
var GameLobbyCategoryJson = require('GameLobbyCategoryJson');
cc.Class({
extends: TVCanvas,
properties: {
},
onLoad: function () {
this._super();
//-----------上下文及参数相关处理------------
let oSceneParameter = this._cApplication.getTopSceneParameter();
if (oSceneParameter) {
this._strAppId = oSceneParameter.appId;//应用id
}
//从其他界面退回来的
if (this._cApplication.getBackStatus()) {
this._oSceneContext = this._cApplication.popSceneContext();
this._cApplication.setBackStatus(false);
} else {
this._oSceneContext = {};
// this._oSceneContext.iRecordKeyDownOperation = 0; //记录用户上下左右操作记录,目的是为了解决用户回退界面焦点框位置不准确问题
this._oSceneContext.categoryRecordIndexOfFirstCell = 0;
this._oSceneContext.focusPath = "CategoryList/DataContainerMask/DataContainer/StarListCell0";
}
//---------上下文及参数相关处理结束-----------
this._nodeCategoryList = this.node.getChildByName('CategoryList');
let lvCategoryList = this._nodeCategoryList.addComponent(ListView);
lvCategoryList.init(this, 3, 1, 3, 4, 'GameCategoryListCell',
-380, 230,//起始位置
0, 30, 15, 0, //margin
1, 1,
function () {
this.getCategoryList();
}
);
//滚动条位置
let nodeScrollBtn = cc.find("CategoryList/ScrollBarContainer/ScrollBarShadow/ScrollBarBlock", this.node);
var fiScrollBtn = nodeScrollBtn.addComponent(FocusInfo);
this._aFocusTargets[0]['scroll_btn'] = nodeScrollBtn;
fiScrollBtn.init(null, true);
// this.getPageBg(this, "background");
this.addHomeIcon(this);
},
getCategoryList: function () {
let lvCategoryList = this._nodeCategoryList.getComponent(ListView);
let oCategoryRequestParameters = {
"view": "json",
"name": "starIndexnew",
"sortField": "left",
"sortDirection": "desc",
};
let iStart = 0;
// //滚行:上翻-4,下翻+4,
// //滚页:上翻-8,下翻+8
// if (null != this._oSceneContext.categoryRecordIndexOfFirstCell && this._oSceneContext.categoryRecordIndexOfFirstCell != 0) {
// //TODO:回退界面返回原来焦点框位置
// iStart = this._oSceneContext.categoryRecordIndexOfFirstCell + this._oSceneContext.iRecordKeyDownOperation;
// }
if (null != this._oSceneContext.categoryRecordIndexOfFirstCell) {
iStart = this._oSceneContext.categoryRecordIndexOfFirstCell;
}
lvCategoryList.setDataSource(
"GET",
Common.TOPDRAW_API_SERVER + "Promotion/ListItemByName",
oCategoryRequestParameters,
iStart, (lvCategoryList.getShowCellRows() + lvCategoryList.getAlphaCellRows()) * lvCategoryList.getCellCountEachRow(),
"left", "desc"
);
lvCategoryList.loadData(
function (strResponse) {
strResponse = GameLobbyCategoryJson.gameCategory; //暂时采用假数据
// cc.log("网络列表:" + JSON.stringify(strResponse));
lvCategoryList.renderInitData(JSON.stringify(strResponse)); //将假数据转成字符串在给过去
this._bIsCategoryListDataInit = true;
this._oSceneContext.categoryRecordIndexOfFirstCell = lvCategoryList.getRecordIndexOfFirstCellInPage();
this.checkDataReadyAndInitFocus();
},
null,
this
);
var self = this;
lvCategoryList.setDataPositionRender(function (iPosition, iCount) {
//这里是ListView的this 执行时作用域
let nodeDataPosition = self.node.getChildByName('DataPosition');
if (null == iCount) {
iCount = parseInt(nodeDataPosition.getComponent(cc.Label).string.split('/')[1]);
cc.log(iCount);
}
nodeDataPosition.getComponent(cc.Label).string = Math.ceil(Math.min((iPosition + this._iShowCellRows * this._iCellCountEachRow - 1), iCount) / this._iShowCellRows / this._iCellCountEachRow) + " / " + Math.ceil(iCount / this._iShowCellRows / this._iCellCountEachRow);
nodeDataPosition.x = Common.SCREEN_WIDTH / 2 - 100 - nodeDataPosition.width / 2;
// this._compSceneCanvas.checkCountAndDisplayTopBtns(iCount);
});
},
checkDataReadyAndInitFocus: function () {
if (this._bIsCategoryListDataInit && !this._bIsFocusInit) {
this.scheduleOnce(() => { //指定0让回调函数在下一帧立即执行
this.initFocus();
}, 0.1);
this._bIsFocusInit = true;
}
},
initFocus: function () {
var nodeInitFocus = cc.find(this._oSceneContext.focusPath, this.node);
var nodeFocus = new cc.Node('nodeFocus');
this.node.addChild(nodeFocus, 10);
this._cFocus = this.node.getChildByName('nodeFocus').addComponent(TVFocus);
this._cFocus.init('focusContainer', this,
nodeInitFocus.getComponent(FocusInfo),
Common.SCREEN_WIDTH, Common.SCREEN_HEIGHT, 0, 0, 1.0);
// cc.log("nodeInitFocus的name:"+nodeInitFocus.name);
// if (0 == nodeInitFocus.name.indexOf('StarListCell')) {
// cc.loader.loadRes('texture/ui/home_pserson_bg', cc.Texture2D, function (err, texture) {
// nodeInitFocus.getChildByName('Bg').getComponent(cc.Sprite).spriteFrame = new cc.SpriteFrame(texture, cc.rect(0, 214, 214, 214));
// });
// }
},
onKeyDown: function (event) {
this._super(event);
var fiFocusTarget = null;
var fiCurrentFocus = this._fiCurrentFocus;
var oScrollParameter = null;
//TODO:给ListView传值,使其可以让滑块跟随滚动
let lvCategoryList = this.node.getChildByName('CategoryList').getComponent(ListView);
if (lvCategoryList) {
lvCategoryList.setCurrentFocus(this._fiCurrentFocus);
}
if (event.keyCode == cc.macro.KEY.up || event.keyCode == Common.ANDROID_KEY.up) {
if (this._bIsCategoryItemMoving || this._bIsSongListMoving) {
return;
}
if (0 == this._fiCurrentFocus.node.getName().indexOf('ScrollBarBlock')) { //如果滚动条
let lvCategoryList = this.node.getChildByName('CategoryList').getComponent(ListView);
if (lvCategoryList.scrollAPageUp()) {
// this._oSceneContext.iRecordKeyDownOperation = -8;
// this._oSceneContext.categoryRecordIndexOfFirstCell = lvCategoryList.getRecordIndexOfFirstCellInPage();
}
return;
}
if (0 == this._fiCurrentFocus.node.getName().indexOf('StarListCell')) {
var index = parseInt(this._fiCurrentFocus.node.getName().replace('StarListCell', ''));
let lvCategoryList = this.node.getChildByName('CategoryList').getComponent(ListView);
if (lvCategoryList.scrollARowUp(index)) {
// this._oSceneContext.iRecordKeyDownOperation = -4;
// this._oSceneContext.categoryRecordIndexOfFirstCell = lvCategoryList.getRecordIndexOfFirstCellInPage();
return;
}
}
fiFocusTarget = this._cFocus.findTarget(fiCurrentFocus, this._aFocusTargets, 0, Common.MOVE_DIRECTION_UP);
this._cFocus.flyFocus(this._fiCurrentFocus, fiFocusTarget, Common.MOVE_DIRECTION_UP, null, oScrollParameter);
}
if (event.keyCode == cc.macro.KEY.right || event.keyCode == Common.ANDROID_KEY.right) {
fiFocusTarget = this._cFocus.findTarget(this._fiCurrentFocus, this._aFocusTargets, 0, Common.MOVE_DIRECTION_RIGHT);
this._cFocus.flyFocus(this._fiCurrentFocus, fiFocusTarget, Common.MOVE_DIRECTION_RIGHT, null, oScrollParameter);
}
if (event.keyCode == cc.macro.KEY.down || event.keyCode == Common.ANDROID_KEY.down) {
if (this._bIsCategoryItemMoving || this._bIsSongListMoving) {
return;
}
if (!this._iScrollGroupStatus) {
if (0 == this._fiCurrentFocus.node.getName().indexOf('ScrollBarBlock')) { //如果是滚动条
let lvCategoryList = this.node.getChildByName('CategoryList').getComponent(ListView);
if (lvCategoryList.scrollAPageDown()) {
// this._oSceneContext.iRecordKeyDownOperation = 8;
// this._oSceneContext.categoryRecordIndexOfFirstCell = lvCategoryList.getRecordIndexOfFirstCellInPage();
}
return;
}
if (0 == this._fiCurrentFocus.node.getName().indexOf('StarListCell')) {
var index = parseInt(this._fiCurrentFocus.node.getName().replace('StarListCell', ''));
let lvCategoryList = this.node.getChildByName('CategoryList').getComponent(ListView);
if (lvCategoryList.scrollARowDown(null, index)) {
// this._oSceneContext.iRecordKeyDownOperation = 4;
// this._oSceneContext.categoryRecordIndexOfFirstCell = lvCategoryList.getRecordIndexOfFirstCellInPage();
return;
}
}
fiFocusTarget = this._cFocus.findTarget(this._fiCurrentFocus, this._aFocusTargets, 0, Common.MOVE_DIRECTION_DOWN);
this._cFocus.flyFocus(this._fiCurrentFocus, fiFocusTarget, Common.MOVE_DIRECTION_DOWN, null, oScrollParameter);
}
}
if (event.keyCode == cc.macro.KEY.left || event.keyCode == Common.ANDROID_KEY.left) {
fiFocusTarget = this._cFocus.findTarget(this._fiCurrentFocus, this._aFocusTargets, 0, Common.MOVE_DIRECTION_LEFT);
this._cFocus.flyFocus(this._fiCurrentFocus, fiFocusTarget, Common.MOVE_DIRECTION_LEFT, null, oScrollParameter);
}
if (event.keyCode == cc.macro.KEY.enter || event.keyCode == Common.ANDROID_KEY.enter) {
this.doCurrentFocusTVLinkAction(Common.TV_LINK_ACTION_CLICK);
}
if (event.keyCode == cc.macro.KEY.space) {
// this.doCurrentFocusTVLinkAction(Common.TV_LINK_ACTION_CLICK);
}
if (event.keyCode == cc.macro.KEY.backspace || event.keyCode == Common.ANDROID_KEY.back) {
this.backAScene();
}
//记录CategoryList滚动位置
if (lvCategoryList) {
this._oSceneContext.categoryRecordIndexOfFirstCell = lvCategoryList.getRecordIndexOfFirstCellInPage();
}
},
onBeforeFocusChange: function (event) {
this._super(event);
let fiFrom = event.detail.from;
let fiTo = event.detail.to;
// if (0 == fiFrom.node.getName().indexOf('StarListCell')) {
// // cc.loader.loadRes('Texture/common/home_pserson_bg', cc.Texture2D, function (err, texture) {
// // fiFrom.node.getChildByName('Bg').getComponent(cc.Sprite).spriteFrame.setRect(cc.rect(0, 0, 214, 214));
// cc.loader.loadRes('texture/ui/home_pserson_bg', cc.Texture2D, function (err, texture) {
// fiFrom.node.getChildByName('Bg').getComponent(cc.Sprite).spriteFrame = new cc.SpriteFrame(texture, cc.rect(0, 0, 214, 214));
// });
// // });
// }
},
onAfterFocusChange: function (event) {
this._super(event);
let fiFrom = event.detail.from;
let fiTo = event.detail.to;
// if (0 == fiTo.node.getName().indexOf('StarListCell')) {
// cc.loader.loadRes('texture/ui/home_pserson_bg', cc.Texture2D, function (err, texture) {
// fiTo.node.getChildByName('Bg').getComponent(cc.Sprite).spriteFrame = new cc.SpriteFrame(texture, cc.rect(0, 214, 214, 214));
// });
// }
},
doCurrentFocusTVLinkAction: function (strAction) {
let strTVLink = this._fiCurrentFocus.getTVLink();
var joTVLink = null;
try {
joTVLink = JSON.parse(strTVLink);
let jaOperationList = joTVLink.click;
for (let i = 0; i < jaOperationList.length; i++) {
switch (jaOperationList[i].action) {
default:
this.doTVLinkAction(jaOperationList[i]);
break;
}
}
} catch (error) {
cc.log("runTVLinkAction Exception..." + error);
}
},
onListScrollStart: function () {
this._bIsSongListMoving = true;
cc.log("scroll start");
},
onListScrollEnd: function () {
this._bIsSongListMoving = false;
cc.log("scroll completed");
},
onDestroy: function () {
cc.director.emit('stop_render'); //分发事件
},
});