forked from playcanvas/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.js
More file actions
417 lines (376 loc) · 12.9 KB
/
controller.js
File metadata and controls
417 lines (376 loc) · 12.9 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
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
import { type } from '../core/core.js';
import {
ACTION_GAMEPAD, ACTION_KEYBOARD, ACTION_MOUSE,
EVENT_MOUSEMOVE,
PAD_1,
PAD_L_STICK_X, PAD_L_STICK_Y, PAD_R_STICK_X, PAD_R_STICK_Y
} from './constants.js';
import { Keyboard } from './keyboard.js';
import { Mouse } from './mouse.js';
/**
* @class
* @name Controller
* @classdesc A general input handler which handles both mouse and keyboard input assigned to named actions.
* This allows you to define input handlers separately to defining keyboard/mouse configurations.
* @description Create a new instance of a Controller.
* @param {Element} [element] - Element to attach Controller to.
* @param {object} [options] - Optional arguments.
* @param {Keyboard} [options.keyboard] - A Keyboard object to use.
* @param {Mouse} [options.mouse] - A Mouse object to use.
* @param {GamePads} [options.gamepads] - A Gamepads object to use.
* @example
* var c = new pc.Controller(document);
*
* // Register the "fire" action and assign it to both the Enter key and the Spacebar.
* c.registerKeys("fire", [pc.KEY_ENTER, pc.KEY_SPACE]);
*/
class Controller {
constructor(element, options = {}) {
this._keyboard = options.keyboard || null;
this._mouse = options.mouse || null;
this._gamepads = options.gamepads || null;
this._element = null;
this._actions = {};
this._axes = {};
this._axesValues = {};
if (element) {
this.attach(element);
}
}
/**
* @function
* @name Controller#attach
* @description Attach Controller to a Element, this is required before you can monitor for key/mouse inputs.
* @param {Element} element - The element to attach mouse and keyboard event handler too.
*/
attach(element) {
this._element = element;
if (this._keyboard) {
this._keyboard.attach(element);
}
if (this._mouse) {
this._mouse.attach(element);
}
}
/**
* @function
* @name Controller#detach
* @description Detach Controller from an Element, this should be done before the Controller is destroyed.
*/
detach() {
if (this._keyboard) {
this._keyboard.detach();
}
if (this._mouse) {
this._mouse.detach();
}
this._element = null;
}
/**
* @function
* @name Controller#disableContextMenu
* @description Disable the context menu usually activated with the right mouse button.
*/
disableContextMenu() {
if (!this._mouse) {
this._enableMouse();
}
this._mouse.disableContextMenu();
}
/**
* @function
* @name Controller#enableContextMenu
* @description Enable the context menu usually activated with the right mouse button. This is enabled by default.
*/
enableContextMenu() {
if (!this._mouse) {
this._enableMouse();
}
this._mouse.enableContextMenu();
}
/**
* @function
* @name Controller#update
* @description Update the Keyboard and Mouse handlers.
* @param {object} dt - The time since the last frame.
*/
update(dt) {
if (this._keyboard) {
this._keyboard.update(dt);
}
if (this._mouse) {
this._mouse.update(dt);
}
if (this._gamepads) {
this._gamepads.update(dt);
}
// clear axes values
this._axesValues = {};
for (var key in this._axes) {
this._axesValues[key] = [];
}
}
/**
* @function
* @name Controller#registerKeys
* @description Create or update a action which is enabled when the supplied keys are pressed.
* @param {string} action - The name of the action.
* @param {number[]} keys - A list of keycodes.
*/
registerKeys(action, keys) {
if (!this._keyboard) {
this._enableKeyboard();
}
if (this._actions[action]) {
throw new Error("Action: " + action + " already registered");
}
if (keys === undefined) {
throw new Error("Invalid button");
}
// convert to an array
if (!keys.length) {
keys = [keys];
}
if (this._actions[action]) {
this._actions[action].push({
type: ACTION_KEYBOARD,
keys: keys
});
} else {
this._actions[action] = [{
type: ACTION_KEYBOARD,
keys: keys
}];
}
}
/**
* @function
* @name Controller#registerMouse
* @description Create or update an action which is enabled when the supplied mouse button is pressed.
* @param {string} action - The name of the action.
* @param {number} button - The mouse button.
*/
registerMouse(action, button) {
if (!this._mouse) {
this._enableMouse();
}
if (button === undefined) {
throw new Error('Invalid button');
}
// Mouse actions are stored as negative numbers to prevent clashing with keycodes.
if (this._actions[action]) {
this._actions[action].push({
type: ACTION_MOUSE,
button: button
});
} else {
this._actions[action] = [{
type: ACTION_MOUSE,
button: -button
}];
}
}
/**
* @function
* @name Controller#registerPadButton
* @description Create or update an action which is enabled when the gamepad button is pressed.
* @param {string} action - The name of the action.
* @param {number} pad - The index of the pad to register (use {@link PAD_1}, etc).
* @param {number} button - The pad button.
*/
registerPadButton(action, pad, button) {
if (button === undefined) {
throw new Error('Invalid button');
}
// Mouse actions are stored as negative numbers to prevent clashing with keycodes.
if (this._actions[action]) {
this._actions[action].push({
type: ACTION_GAMEPAD,
button: button,
pad: pad
});
} else {
this._actions[action] = [{
type: ACTION_GAMEPAD,
button: button,
pad: pad
}];
}
}
/**
* @function
* @name Controller#registerAxis
* @param {object} [options] - Optional options object.
* @param {object} [options.pad] - The index of the game pad to register for (use {@link PAD_1}, etc).
*/
registerAxis(options) {
var name = options.name;
if (!this._axes[name]) {
this._axes[name] = [];
}
var i = this._axes[name].push(name);
//
options = options || {};
options.pad = options.pad || PAD_1;
var bind = function (controller, source, value, key) {
switch (source) {
case 'mousex':
controller._mouse.on(EVENT_MOUSEMOVE, function (e) {
controller._axesValues[name][i] = e.dx / 10;
});
break;
case 'mousey':
controller._mouse.on(EVENT_MOUSEMOVE, function (e) {
controller._axesValues[name][i] = e.dy / 10;
});
break;
case 'key':
controller._axes[name].push(function () {
return controller._keyboard.isPressed(key) ? value : 0;
});
break;
case 'padrx':
controller._axes[name].push(function () {
return controller._gamepads.getAxis(options.pad, PAD_R_STICK_X);
});
break;
case 'padry':
controller._axes[name].push(function () {
return controller._gamepads.getAxis(options.pad, PAD_R_STICK_Y);
});
break;
case 'padlx':
controller._axes[name].push(function () {
return controller._gamepads.getAxis(options.pad, PAD_L_STICK_X);
});
break;
case 'padly':
controller._axes[name].push(function () {
return controller._gamepads.getAxis(options.pad, PAD_L_STICK_Y);
});
break;
default:
throw new Error('Unknown axis');
}
};
bind(this, options.positive, 1, options.positiveKey);
if (options.negativeKey || options.negative !== options.positive) {
bind(this, options.negative, -1, options.negativeKey);
}
}
/**
* @function
* @name Controller#isPressed
* @description Returns true if the current action is enabled.
* @param {string} actionName - The name of the action.
* @returns {boolean} True if the action is enabled.
*/
isPressed(actionName) {
if (!this._actions[actionName]) {
return false;
}
var action;
var index = 0;
var length = this._actions[actionName].length;
for (index = 0; index < length; ++index) {
action = this._actions[actionName][index];
switch (action.type) {
case ACTION_KEYBOARD:
if (this._keyboard) {
var i, len = action.keys.length;
for (i = 0; i < len; i++) {
if (this._keyboard.isPressed(action.keys[i])) {
return true;
}
}
}
break;
case ACTION_MOUSE:
if (this._mouse && this._mouse.isPressed(action.button)) {
return true;
}
break;
case ACTION_GAMEPAD:
if (this._gamepads && this._gamepads.isPressed(action.pad, action.button)) {
return true;
}
break;
}
}
return false;
}
/**
* @function
* @name Controller#wasPressed
* @description Returns true if the action was enabled this since the last update.
* @param {string} actionName - The name of the action.
* @returns {boolean} True if the action was enabled this since the last update.
*/
wasPressed(actionName) {
if (!this._actions[actionName]) {
return false;
}
var index = 0;
var length = this._actions[actionName].length;
for (index = 0; index < length; ++index) {
var action = this._actions[actionName][index];
switch (action.type) {
case ACTION_KEYBOARD:
if (this._keyboard) {
var i, len = action.keys.length;
for (i = 0; i < len; i++) {
if (this._keyboard.wasPressed(action.keys[i])) {
return true;
}
}
}
break;
case ACTION_MOUSE:
if (this._mouse && this._mouse.wasPressed(action.button)) {
return true;
}
break;
case ACTION_GAMEPAD:
if (this._gamepads && this._gamepads.wasPressed(action.pad, action.button)) {
return true;
}
break;
}
}
return false;
}
getAxis(name) {
var value = 0;
if (this._axes[name]) {
var i, len = this._axes[name].length;
for (i = 0; i < len; i++) {
if (type(this._axes[name][i]) === 'function') {
var v = this._axes[name][i]();
if (Math.abs(v) > Math.abs(value)) {
value = v;
}
} else if (this._axesValues[name]) {
if (Math.abs(this._axesValues[name][i]) > Math.abs(value)) {
value = this._axesValues[name][i];
}
}
}
}
return value;
}
_enableMouse() {
this._mouse = new Mouse();
if (!this._element) {
throw new Error("Controller must be attached to an Element");
}
this._mouse.attach(this._element);
}
_enableKeyboard() {
this._keyboard = new Keyboard();
if (!this._element) {
throw new Error("Controller must be attached to an Element");
}
this._keyboard.attach(this._element);
}
}
export { Controller };