/
kejo.js
308 lines (264 loc) 路 10.4 KB
/
kejo.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
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
//kejo.js
export default class KeJo {
constructor({
autoupdategamepad,
map,
}={}) {
//a simple version string
this._version = '0.1.0'
//this contains keys currently pressed as enum, should only be accessed
//through isPressed
this._pressed = {}
//this allows key enum to key name translation
this._KeysString = ['LEFT','UP','RIGHT','DOWN',
'BUTTONA','BUTTONB','BUTTONX','BUTTONY']
//enum containing each key as number
this.key = {
LEFT: 0, UP: 1, RIGHT: 2, DOWN: 3,
BUTTONA: 4, BUTTONB: 5, BUTTONX: 6, BUTTONY: 7
},
//when holding, emit a new key down on every refresh rate milissecond
//if refresh rate is 0, don't emit a new key down
this._KEY_REFRESH_RATE = 0
//
this._keyboardMap = {}
this._gamepadMap = {}
this._previousGamepadKeys = {}
this._lastInputType = ''
this.map = {}
//when KeJo is loaded, if no alternative map is specified, it loads the _defaultMap
this._defaultmap = {
k: {
LEFT: [37, 65], //arrow left, key a
UP: [38, 87], //arrow up, key w
RIGHT: [39, 68], //arrow right, key d
DOWN: [40, 83], //arrow down, key s
BUTTONA: [73, 13, 32], //key i, enter, space
BUTTONB: [74,17], //key j, ctrl
BUTTONX: [79, 220, 191], //key o, key \, key ;
BUTTONY: [75, 16], //key k, shift
},
g: {
LEFT: ['axes', 0, '<'], //left pad, to left
UP: ['axes', 1, '<'], //left pad, to up
RIGHT: ['axes', 0, '>'], //left pad, to right
DOWN: ['axes', 1, '>'], //left pad, to down
BUTTONA: ['buttons', 0], //button 'A'
BUTTONB: ['buttons', 1], //button 'B'
BUTTONX: ['buttons', 2], //button 'X'
BUTTONY: ['buttons', 3], //button 'Y'
}
}
//this provides man readable translation for keyboard keycodes
this._readableKeyCodeMap = {
8: 'backspace', 9: 'tab', 13: 'enter', 16: 'shift', 17: 'ctrl', 18: 'alt',
19: 'pause/break', 20: 'caps lock', 27: 'escape', 32: 'space',
33: 'page up', 34: 'page down', 35: 'end', 36: 'home', 37: 'left arrow',
38: 'up arrow', 39: 'right arrow', 40: 'down arrow', 45: 'insert',
46: 'delete', 48: '0', 49: '1', 50: '2', 51: '3', 52: '4', 53: '5',
54: '6', 55: '7', 56: '8', 57: '9', 65: 'a', 66: 'b', 67: 'c', 68: 'd',
69: 'e', 70: 'f', 71: 'g', 72: 'h', 73: 'i', 74: 'j', 75: 'k', 76: 'l',
77: 'm', 78: 'n', 79: 'o', 80: 'p', 81: 'q', 82: 'r', 83: 's', 84: 't',
85: 'u', 86: 'v', 87: 'w', 88: 'x', 89: 'y', 90: 'z',
91: 'left super key', 92: 'right super key', 93: 'select key',
96: 'numpad 0', 97: 'numpad 1', 98: 'numpad 2', 99: 'numpad 3',
100: 'numpad 4', 101: 'numpad 5', 102: 'numpad 6', 103: 'numpad 7',
104: 'numpad 8', 105: 'numpad 9', 106: 'multiply', 107: 'add',
109: 'subtract', 110: 'decimal point', 111: 'divide', 112: 'f1',
113: 'f2', 114: 'f3', 115: 'f4', 116: 'f5', 117: 'f6', 118: 'f7',
119: 'f8', 120: 'f9', 121: 'f10', 122: 'f11', 123: 'f12', 144: 'num lock',
145: 'scroll lock', 186: 'semi-colon', 187: 'equal sign', 188: 'comma',
189: 'dash', 190: 'period', 191: 'forward slash', 192: 'grave accent',
219: 'open bracket', 220: 'back slash', 221: 'close braket',
222: 'single quote'
}
//this is set to true by default, meaning it will auto update. You can control
//this value during setup.
this._loopUpdateGamepad = true
//the analog stick will be considered activated and interpreted as binary
//true when it's bigger than this value. (smaller if negative)
this._gamepadDeadzone = 0.4
//let's set the initial conditions for every parameter!
if(typeof autoupdategamepad === 'undefined' || autoupdategamepad === null){
this._loopUpdateGamepad = true;
} else {
this._loopUpdateGamepad = autoupdategamepad;
}
if(typeof map === 'undefined' || map === null){
this.map = this._defaultmap;
} else {
this.map = map;
}
//configures keyboard key map to internal keys
for(var ibutton in this.map.k){
var keyCodes = this.map.k[ibutton];
for(var i=0; i<keyCodes.length; i++){
var keyCode = keyCodes[i];
this._keyboardMap[keyCode] = this.key[ibutton];
}
}
//configures gamepad buttons and axes map to internal keys
for(var ibutton in this.map.g){
var gamepadCodes = this.map.g[ibutton];
this._gamepadMap[this.key[ibutton]] = this._genericGamepadCheck(gamepadCodes);
}
//setting inital condition for _previousGamepadKeys
for(var k in this.key){
var kvalue=this.key[k];
this._previousGamepadKeys[kvalue]=false;
}
window.addEventListener('keyup', this.onKeyup.bind(this) ,false);
window.addEventListener('keydown', this.onKeydown.bind(this),false);
if(this._loopUpdateGamepad){
setTimeout(this.updateGamepad.bind(this),1000/60);
}
}
// helper function used to translate the gamepad map.g
_genericGamepadCheck(gamepadCodes){
var axesbuttons = gamepadCodes[0]; //is either 'axes' or 'buttons'
var n = gamepadCodes[1]; // the number of either the axe or the button
var moreless = gamepadCodes[2]; // for axes, is either '>' or '<'
if(axesbuttons=='axes'){
if(moreless == '<'){
return (pad)=>{
if ((typeof pad !== 'undefined') && pad != null) {
return pad.axes[n] < -this._gamepadDeadzone;
}
};
} else {
return (pad)=>{
if ((typeof pad !== 'undefined') && pad != null) {
return pad.axes[n] > this._gamepadDeadzone;
}
};
}
} else {
return function(pad){
if ((typeof pad !== 'undefined') && pad != null) {
return !!pad.buttons[n].pressed;
}
};
}
}
keyCodeToReadable(keyCode) {
if(typeof keyCode === undefined){
return;
}
if( keyCode in this._readableKeyCodeMap){
return this._readableKeyCodeMap[keyCode];
}
return parseInt(keyCode).toString();
}
charToKeyCode(char) {
return char.charCodeAt(0)
}
_keyPressedEvent(keyString,refresh){
if(refresh==true&&(this._KEY_REFRESH_RATE==0 || !this.isPressed(this.key[keyString]) )){
return;
}
var newEvent = new CustomEvent('kejo_KeyPressed', {
'detail': keyString});
window.dispatchEvent(newEvent);
setTimeout(this._keyPressedEvent.bind(this),this._KEY_REFRESH_RATE,keyString,true)
}
_keyReleasedEvent(keyString,refresh){
var newEvent = new CustomEvent('kejo_KeyReleased', {
'detail': keyString});
window.dispatchEvent(newEvent);
}
onKeydown(event) {
if(event.keyCode in this._keyboardMap){
event.preventDefault()
if(this._pressed[this._keyboardMap[event.keyCode]] != true){
this._keyPressedEvent(this._KeysString[this._keyboardMap[event.keyCode]]);
}
this._pressed[this._keyboardMap[event.keyCode]] = true;
this._lastInputType = 'keyboard'
}
}
onKeyup(event) {
if(event.keyCode in this._keyboardMap){
event.preventDefault()
delete this._pressed[this._keyboardMap[event.keyCode]];
this._keyReleasedEvent(this._KeysString[this._keyboardMap[event.keyCode]]);
this._lastInputType = 'keyboard'
}
}
updateGamepad(){
var gamepad = navigator.getGamepads()[0]
if ((typeof gamepad !== 'undefined') && gamepad != null) {
for(var k in this.key){
var kvalue=this.key[k]
var padkeypressed = this._gamepadMap[kvalue](gamepad)
//check if key was just pressed
if(padkeypressed == true && this._previousGamepadKeys[kvalue] == false){
this._pressed[kvalue] = true
this._previousGamepadKeys[kvalue] = true
//throw a kejo event
this._keyPressedEvent(this._KeysString[kvalue])
this._lastInputType = 'gamepad'
//if it was not, check if it was just released
} else if(padkeypressed == false && this._previousGamepadKeys[kvalue] == true){
this._previousGamepadKeys[kvalue] = false;
delete this._pressed[kvalue]
//throw a kejo event
this._keyReleasedEvent(this._KeysString[kvalue])
this._lastInputType = 'gamepad'
}
}
}
if(this._loopUpdateGamepad){
setTimeout(this.updateGamepad.bind(this),1000/60);
}
}
isPressed(key) {
return !!this._pressed[key]
}
//if you need to handle game ui changes depending on last input being from
//keyboard or gamepad (or touch), you can read information using this function
getLastInputType(){
return this._lastInputType;
}
//a printable map of the keyboard, may be ueful to present in a menu.
getPrintableKeyboardMap(){
var text=''
for(var i=0; i<this._KeysString.length; i++){
text=text+this._KeysString[i]+': '
var keyCodes = this.map.k[this._KeysString[i]];
for(var j=0; j<keyCodes.length; j++){
text=text+this.keyCodeToReadable(keyCodes[j]);
if(j<keyCodes.length-1)
text=text+', '
}
if(i<this._KeysString.length-1)
text=text+"\n"
}
return text
}
destroy() {
//remove listeners
window.removeEventListener('keyup', this.onKeyup.bind(this) ,false)
window.removeEventListener('keydown', this.onKeydown.bind(this),false)
}
}
// MIT License
//
// Copyright (c) 2018 脡rico Vieira Porto
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.