-
Notifications
You must be signed in to change notification settings - Fork 0
/
inputmapper.js
258 lines (244 loc) · 10.7 KB
/
inputmapper.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
"use strict";
/**
* Mapper that automatically maps keyboard / gamepad input to different player numbers.
* This can be used to implement keyboard / gamepad controls for a single player or a local
* multiplayer game that allows players on the keyboard to play against players on gamepads.
* Requires gamepad.js, mousetrap.js and mousetrap-global-bind.js to be included.
* @param {Object} callbackObj Object on which the callback functions will be called.
* @param {number} maxPlayers Maximum number of players. If there are more active controllers
* than this, then two controllers may be mapped to the same player.
*/
var InputMapper = function(callbackObj, maxPlayers) {
this.gamepads = new Gamepad(this);
this.callbackObj = callbackObj;
this.maxPlayers = maxPlayers;
this.resetPlayerMap();
this.keysDown = []; // Keyboard keys that are currently down
this.callbacks = []; // Callback information for mapping callbacks back to buttons
};
// Controller types
InputMapper.GAMEPAD = 0;
InputMapper.KEYBOARD = 1;
/**
* Helper class to store the controller config each player has.
* @param {number} controllerType Controller type: either InputMapper.GAMEPAD or InputMapper.KEYBOARD
* @param {number} controllerIndex Controller index: in case of keyboard, index into the array of keyboard keys given
* to addListener. In case of gamepad, index of the gamepad.
*/
InputMapper.Controller = function(controllerType, controllerIndex) {
this.controllerType = controllerType;
this.controllerIndex = controllerIndex;
this.lastUsed = 0; // A timestamp for when this controller was last used
};
/**
* Reset the map between controllers and player numbers.
* @param {number?} maxPlayers Maximum player count. Default is to keep existing value.
*/
InputMapper.prototype.resetPlayerMap = function(maxPlayers) {
if (maxPlayers !== undefined) {
this.maxPlayers = maxPlayers;
}
this.players = []; // An array of arrays of controllers. Each player can have multiple controllers.
for (var i = 0; i < this.maxPlayers; ++i) {
this.players.push([]);
}
};
/**
* Update the controller state and call listeners based on that.
*/
InputMapper.prototype.update = function() {
this.gamepads.update();
};
/**
* Return a player index for a player using a given controller.
* @param {number} controllerType Controller type: either InputMapper.GAMEPAD or InputMapper.KEYBOARD
* @param {number} controllerIndex Controller index: in case of keyboard, index into the array of keyboard keys given
* to addListener. In case of gamepad, index of the gamepad.
*/
InputMapper.prototype.getPlayerIndex = function(controllerType, controllerIndex) {
for (var i = 0; i < this.players.length; ++i) {
var player = this.players[i];
for (var j = 0; j < player.length; ++j) {
if (player[j].controllerType == controllerType && player[j].controllerIndex == controllerIndex) {
player[j].lastUsed = Date.now();
return i;
}
}
}
var controller = new InputMapper.Controller(controllerType, controllerIndex);
controller.lastUsed = Date.now();
// Map the controller for a player without a controller if there is one
for (var i = 0; i < this.players.length; ++i) {
var player = this.players[i];
if (player.length === 0) {
player.push(controller);
return i;
}
}
// Map the controller for the first player without this type of a controller
for (var i = 0; i < this.players.length; ++i) {
var player = this.players[i];
var hasSameTypeController = false;
for (var j = 0; j < player.length; ++j) {
if (player[j].controllerType == controllerType) {
hasSameTypeController = true;
}
}
if (!hasSameTypeController) {
player.push(controller);
return i;
}
}
// Just map the controller for the first player
this.players[0].push(controller);
return 0;
};
/**
* @param {number} gamepadButton A button from Gamepad.BUTTONS
* @param {Array} keyboardBindings List of bindings for different players, for example ['up', 'w']
* @param {function=} downCallback Callback when the button is pressed down, that takes a player number as a parameter.
* @param {function=} upCallback Callback when the button is released, that takes a player number as a parameter.
*/
InputMapper.prototype.addListener = function(gamepadButton, keyboardButtons, downCallback, upCallback) {
var gamepadDownCallback = function(gamepadNumber) {
var player = this.getPlayerIndex(InputMapper.GAMEPAD, gamepadNumber);
if (downCallback !== undefined) {
downCallback.call(this.callbackObj, player);
}
};
var gamepadUpCallback = function(gamepadNumber) {
var player = this.getPlayerIndex(InputMapper.GAMEPAD, gamepadNumber);
if (upCallback !== undefined) {
upCallback.call(this.callbackObj, player);
}
};
this.gamepads.addButtonChangeListener(gamepadButton, gamepadDownCallback, gamepadUpCallback);
var gamepadInstruction;
if (gamepadButton < 100) {
gamepadInstruction = Gamepad.BUTTON_INSTRUCTION[gamepadButton];
} else {
gamepadInstruction = Gamepad.BUTTON_INSTRUCTION[gamepadButton - 100];
}
if (downCallback !== undefined) {
this.callbacks.push({key: gamepadInstruction, callback: downCallback, controllerType: InputMapper.GAMEPAD});
}
if (upCallback !== undefined) {
this.callbacks.push({key: gamepadInstruction, callback: upCallback, controllerType: InputMapper.GAMEPAD});
}
var that = this;
for (var i = 0; i < keyboardButtons.length; ++i) {
(function(kbIndex) {
that.keysDown[keyboardButtons[kbIndex]] = false;
// TODO: down events get generated multiple times while a key is down. Work around this...
var keyDownCallback = function() {
var player = that.getPlayerIndex(InputMapper.KEYBOARD, kbIndex);
if (!that.keysDown[keyboardButtons[kbIndex]]) {
that.keysDown[keyboardButtons[kbIndex]] = true;
if (downCallback !== undefined) {
downCallback.call(that.callbackObj, player);
}
}
};
var keyUpCallback = function() {
var player = that.getPlayerIndex(InputMapper.KEYBOARD, kbIndex);
that.keysDown[keyboardButtons[kbIndex]] = false;
if (upCallback !== undefined) {
upCallback.call(that.callbackObj, player);
}
};
Mousetrap.bindGlobal(keyboardButtons[kbIndex], keyDownCallback, 'keydown');
Mousetrap.bindGlobal(keyboardButtons[kbIndex], keyUpCallback, 'keyup');
})(i);
if (downCallback !== undefined) {
this.callbacks.push({key: keyboardButtons[i], callback: downCallback, controllerType: InputMapper.KEYBOARD, kbIndex: i});
}
if (upCallback !== undefined) {
this.callbacks.push({key: keyboardButtons[i], callback: upCallback, controllerType: InputMapper.KEYBOARD, kbIndex: i});
}
}
};
/**
* Check if a given callback uses a given type of controller. Doesn't care about gamepad indices.
* @protected
* @param {InputMapper.Controller} controller
* @param {Object} cbInfo Information on the callback, with keys controllerType and kbIndex in case of a keyboard.
* @return {boolean} True if the given callback uses the given type of a controller.
*/
InputMapper._usesController = function(controller, cbInfo) {
if (cbInfo.controllerType === controller.controllerType) {
if (cbInfo.controllerType === InputMapper.KEYBOARD && controller.controllerIndex !== cbInfo.kbIndex) {
// Each keyboard "controller" has different key bindings.
return false;
}
return true;
}
};
/**
* From an array of controllers, determine the one that was most recently used.
* @protected
* @param {Array.<InputMapper.Controller>} player Array of controllers to check.
* @return {InputMapper.Controller} The most recently used controller.
*/
InputMapper.prototype._getLastUsedController = function(player) {
var controller;
var lastUsed = 0;
for (var j = 0; j < player.length; ++j) {
if (player[j].lastUsed > lastUsed) {
controller = player[j];
lastUsed = player[j].lastUsed;
}
}
return controller;
};
/**
* Get instruction for a key. Prioritizes gamepad over keyboard if keyboard hasn't been used.
* @param {function} callback A callback that has been previously attached to a button.
* @param {playerIndex} index of the player to return information for. Set to undefined if the listener doesn't care
* about the player number.
* @return {string} String identifying the button for the player.
*/
InputMapper.prototype.getKeyInstruction = function(callback, playerIndex) {
var controller;
if (playerIndex !== undefined) {
if (this.players[playerIndex].length > 0) {
controller = this._getLastUsedController(this.players[playerIndex]);
} else {
// Gamepad instructions by default
controller = new InputMapper.Controller(InputMapper.GAMEPAD, 0);
}
}
var returnStr = [];
for (var i = 0; i < this.callbacks.length; ++i) {
var cbInfo = this.callbacks[i];
if (cbInfo.callback === callback) {
if (controller === undefined) {
// Listener doesn't care about the player number.
// Determine all keys mapped to that callback from different controllers.
for (var j = 0; j < this.players.length; ++j) {
for (var k = 0; k < this.players[j].length; ++k) {
if (InputMapper._usesController(this.players[j][k], cbInfo)) {
var hasInstruction = false;
var instruction = cbInfo.key.toUpperCase();
for (var l = 0; l < returnStr.length; ++l) {
if (returnStr[l] == instruction) {
hasInstruction = true;
}
}
if (!hasInstruction) {
returnStr.push(instruction);
}
}
}
}
} else {
if (InputMapper._usesController(controller, cbInfo)) {
return cbInfo.key.toUpperCase();
}
}
}
}
if (controller === undefined) {
return returnStr.join('/');
}
return '';
};