nodePong/client/lib/keyboard/event_handler.js
2016-02-14 04:37:31 +01:00

99 lines
2.6 KiB
JavaScript

/**
* @file event_handling.js
* @autour frtk@tetalab
*/
NPGClient.evtHandler = {
keyState: {},
//
init: function() {
var self = this;
//
self.keyState = {};
for (k in NPGClient.KEYS) {
self.keyState[NPGClient[k]] = false;
}
//
document.addEventListener('keydown',function(e) {
self.keyState[e.keyCode || e.which] = true;
self.onKeyDown(e);
}, true);
//
document.addEventListener('keyup',function(e){
self.keyState[e.keyCode || e.which] = false;
}, true);
},
//
isValidKey: function(k) {
return (k >= 48 && k <= 90);
},
//
onKeyDown: function(evt) {
var self = this;
if (evt.keyCode == NPGClient.KEYS.ESC) {
//self.playerLogout(); // player logout
} else if (evt.keyCode == NPGClient.KEYS.F1) {
//self.sendToMenuPage(); // back to previous page
} else {
switch (NPGClient.PageHandler.currPage) {
case 'login':
self.userLogin(evt);
break;
default:
break;
}
}
},
/*
*
*/
//
userLogin : function (evt) {
//
var self = this;
var oldNameWidth, newNameWidth;
var size = NPGClient.PageHandler.getPageByName('login').ctx.measureText(NPGClient.userName).width;
oldNameWidth = 2.1*size; // corr factor 2.1
switch (evt.keyCode) {
case NPGClient.KEYS.ENTER:
if (NPGClient.SocketIO.isConnected) {
if (NPGClient.userName.length != 0) {
NPGClient.SocketIO.sendMsg('registration', NPGClient.userName);
}
} else {
//self.serverExit();
}
break;
case NPGClient.KEYS.BACKSPACE:
//--- remove a character
evt.preventDefault();
NPGClient.Utils.removeChar();
newNameWidth = NPGClient.PageHandler.getPageByName('login').ctx.measureText(NPGClient.userName).width
// newNameWidth = 2.1*self.ctx.measureText(self.tmpName).width; // corr factor 2.1
// self.cursor.shiftX(newNameWidth - oldNameWidth);
break;
default:
//console.log(evt.keyCode + ' ' + self.isValidKey(evt.keyCode));
//--- add character
if (self.isValidKey(evt.keyCode)) {
//self.cursor.stopBlink;
NPGClient.Utils.addChar(evt.keyCode);
//newNameWidth = 2.1*self.ctx.measureText(self.tmpName).width; // corr factor 2.1
//if (self.validName()) {
// self.cursor.shiftX(newNameWidth - oldNameWidth);
//}
//self.cursor.startBlink;
}
break;
}
},
}