目標(biāo):鍵盤導(dǎo)航
機(jī)頂盒游戲與手機(jī)運(yùn)行最大的區(qū)別是輸入方式不同,手機(jī)主要是觸摸輸入,機(jī)頂盒是遙控器和游戲手柄輸入(等價(jià)于鍵盤輸入)
游戲鍵盤導(dǎo)航主要處理上辞色、下、左浮定、右相满、回車操作层亿。
tip:模擬器調(diào)試請(qǐng)開啟軟鍵盤,以保證鍵盤事件的正確模擬立美。
"use strict";
define(function(require, exports, module) {
var gameUtils = {};
gameUtils.getByteLen = function(val) {
var len = 0;
for(var i = 0; i < val.length; i++) {
var a = val.charAt(i);
if(a.match(/[^\x00-\xff]/ig) != null) {
len += 2;
} else {
len += 1;
}
}
return len;
}
gameUtils.cutStr = function(str, len) {
var res = "";
var realLength = 0;
for(var i = 0; i < str.length; i++) {
var charCode = str.charCodeAt(i);
if(charCode >= 0 && charCode <= 128) realLength += 1;
else realLength += 2;
if(realLength <= len) {
res += str[i];
} else {
break;
}
}
return res;
}
gameUtils.KeyNavigator = function(dimRow, dimCol) {
if(!dimRow || !dimCol)
return null;
this.subNavs = [];
this.currentSub = null;
this.dimRow = dimRow;
this.dimCol = dimCol;
this.data = new Array();
this.currentRow = -1;
this.currentCol = -1;
for(var i = 0; i < dimRow; i++) {
this.data[i] = new Array();
}
};
function SelButton(btn, onSel, onUnsel) {
this.btn = btn;
this.onSel = onSel;
this.onUnsel = onUnsel;
}
gameUtils.KeyNavigator.prototype = {
addSubNav: function(name, subNav) {
if(subNav)
this.subNavs[name] = subNav;
},
doSubNav: function(name) {
this.hideCurrent();
this.currentSub = this.subNavs[name];
},
endSubNav: function() {
this.showCurrent();
this.currentSub = null;
},
addButton: function(row, col, btn, onSel, onUnsel, beCurrent) {
if(row < this.dimRow && col < this.dimCol) {
this.data[row][col] = new SelButton(btn, onSel, onUnsel);
if(beCurrent) {
this.setCurrent(row, col);
}
return true;
} else {
return false;
}
},
showCurrent: function() {
if(this.data[this.currentRow][this.currentCol]) {
if(this.data[this.currentRow][this.currentCol].onSel)
this.data[this.currentRow][this.currentCol].onSel();
}
},
hideCurrent: function() {
if(this.data[this.currentRow][this.currentCol]) {
if(this.data[this.currentRow][this.currentCol].onUnsel)
this.data[this.currentRow][this.currentCol].onUnsel();
}
},
setCurrent: function(row, col) {
try {
if(this.data[row][col]) {
if(this.currentRow >= 0 && this.currentCol >= 0 && this.data[this.currentRow][this.currentCol]) {
if(this.data[this.currentRow][this.currentCol].onUnsel)
this.data[this.currentRow][this.currentCol].onUnsel();
}
this.currentRow = row;
this.currentCol = col;
if(this.data[this.currentRow][this.currentCol]) {
if(this.data[this.currentRow][this.currentCol].onSel)
this.data[this.currentRow][this.currentCol].onSel();
}
//console.log('current = ' + this.currentRow + " " + this.currentCol)
return true;
} else {
return false;
}
} catch(err) {
console.log('utils.KeyNavigator.setCurrent error!');
throw err;
return false;
}
},
perform: function(cmd) {
switch(cmd) {
case "ENTER":
this.data[this.currentRow][this.currentCol].btn.onInputUp.dispatch();
break;
}
},
navigate: function(dir) {
if(this.currentRow < 0 || this.currentCol < 0) {
return false;
}
//console.log('this.currentRow and Col = ' + this.currentRow + " " + this.currentCol)
switch(dir) {
case "UP":
if(this.currentRow < this.dimRow - 1) {
var col = -1;
for(var i = this.currentCol; i < this.dimCol; i++) {
if(this.data[this.currentRow + 1][i]) {
col = i;
break;
}
}
if(col < 0) {
for(var i = this.currentCol - 1; i >= 0; i--)
if(this.data[this.currentRow + 1][i]) {
col = i;
break;
}
}
if(col >= 0) {
this.setCurrent(this.currentRow + 1, col);
} else {
return false;
}
} else {
return false;
}
break;
case "DOWN":
if(this.currentRow > 0) {
var col = -1;
for(var i = this.currentCol; i < this.dimCol; i++) {
if(this.data[this.currentRow - 1][i]) {
col = i;
break;
}
}
if(col < 0) {
for(var i = this.currentCol - 1; i >= 0; i--)
if(this.data[this.currentRow - 1][i]) {
col = i;
break;
}
}
if(col >= 0) {
this.setCurrent(this.currentRow - 1, col);
} else {
return false;
}
} else {
return false;
}
break;
case "LEFT":
var col = this.currentCol - 1;
while(col >= 0 && !this.data[this.currentRow][col])
col--;
if(col >= 0) {
this.setCurrent(this.currentRow, col);
} else {
return false;
}
break;
case "RIGHT":
var col = this.currentCol + 1;
while(col < this.dimCol && !this.data[this.currentRow][col])
col++;
if(col < this.dimCol) {
this.setCurrent(this.currentRow, col);
} else {
return false;
}
break;
default:
return false;
}
}
}
return gameUtils;
});