-
Notifications
You must be signed in to change notification settings - Fork 0
/
inputmanager.js
376 lines (325 loc) · 10.8 KB
/
inputmanager.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
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
/*
* GDevelop JS Platform
* Copyright 2013-2016 Florian Rival (Florian.Rival@gmail.com). All rights reserved.
* This project is released under the MIT License.
*/
/**
* Store input made on a canvas: mouse position, key pressed
* and touches states.
*
* See **bindStandardEvents** method for connecting the input
* manager to a canvas and **onFrameEnded** for signaling the
* end of a frame (necessary for proper touches events handling).
*
* @constructor
* @memberof gdjs
* @class InputManager
*/
gdjs.InputManager = function() {
this._pressedKeys = new Hashtable();
this._releasedKeys = new Hashtable();
this._lastPressedKey = 0;
this._pressedMouseButtons = new Array(5);
this._releasedMouseButtons = new Array(5);
this._mouseX = 0;
this._mouseY = 0;
this._mouseWheelDelta = 0;
this._touches = new Hashtable();
this._startedTouches = []; //Identifiers of the touches that started during/before the frame.
this._endedTouches = []; //Identifiers of the touches that ended during/before the frame.
this._touchSimulateMouse = true;
};
/** @constant {number} */
gdjs.InputManager.MOUSE_LEFT_BUTTON = 0;
/** @constant {number} */
gdjs.InputManager.MOUSE_RIGHT_BUTTON = 1;
/** @constant {number} */
gdjs.InputManager.MOUSE_MIDDLE_BUTTON = 2;
/**
* Holds the raw keyCodes of the keys which only have left/right
* variants and should default to their left variant values
* if location is not specified.
*
* @constant {Array<number>}
*/
gdjs.InputManager._DEFAULT_LEFT_VARIANT_KEYS = [16, 17, 18, 91];
/**
* Returns the "location-aware" keyCode, given a raw keyCode
* and location. The location corresponds to KeyboardEvent.location,
* which should be 0 for standard keys, 1 for left keys,
* 2 for right keys, and 3 for numpad keys.
*
* @param {number} keyCode The raw key code
* @param {?number} location The location
*/
gdjs.InputManager.prototype._getLocationAwareKeyCode = function(
keyCode,
location
) {
if (location) {
// If it is a numpad number, do not modify it.
if (96 <= keyCode && keyCode <= 105) {
return keyCode;
}
return keyCode + 1000 * location;
}
if (gdjs.InputManager._DEFAULT_LEFT_VARIANT_KEYS.indexOf(keyCode) !== -1) {
return keyCode + 1000;
}
return keyCode;
};
/**
* Should be called whenever a key is pressed. The location corresponds to
* KeyboardEvent.location, which should be 0 for standard keys, 1 for left keys,
* 2 for right keys, and 3 for numpad keys.
* @param {number} keyCode The raw key code associated to the key press.
* @param {number=} location The location of the event.
*/
gdjs.InputManager.prototype.onKeyPressed = function(keyCode, location) {
var locationAwareKeyCode = this._getLocationAwareKeyCode(keyCode, location);
this._pressedKeys.put(locationAwareKeyCode, true);
this._lastPressedKey = locationAwareKeyCode;
};
/**
* Should be called whenever a key is released. The location corresponds to
* KeyboardEvent.location, which should be 0 for standard keys, 1 for left keys,
* 2 for right keys, and 3 for numpad keys.
* @param {number} keyCode The raw key code associated to the key release.
* @param {number=} location The location of the event.
*/
gdjs.InputManager.prototype.onKeyReleased = function(keyCode, location) {
var locationAwareKeyCode = this._getLocationAwareKeyCode(keyCode, location);
this._pressedKeys.put(locationAwareKeyCode, false);
this._releasedKeys.put(locationAwareKeyCode, true);
};
/**
* Return the location-aware code of the last key that was pressed.
* @return {number} The location-aware code of the last key pressed.
*/
gdjs.InputManager.prototype.getLastPressedKey = function() {
return this._lastPressedKey;
};
/**
* Return true if the key corresponding to the location-aware keyCode is pressed.
* @param {number} locationAwareKeyCode The location-aware key code to be tested.
*/
gdjs.InputManager.prototype.isKeyPressed = function(locationAwareKeyCode) {
return (
this._pressedKeys.containsKey(locationAwareKeyCode) &&
this._pressedKeys.get(locationAwareKeyCode)
);
};
/**
* Return true if the key corresponding to the location-aware keyCode was released during the last frame.
* @param {number} locationAwareKeyCode The location-aware key code to be tested.
*/
gdjs.InputManager.prototype.wasKeyReleased = function(locationAwareKeyCode) {
return (
this._releasedKeys.containsKey(locationAwareKeyCode) &&
this._releasedKeys.get(locationAwareKeyCode)
);
};
/**
* Return true if any key is pressed.
*/
gdjs.InputManager.prototype.anyKeyPressed = function() {
for (var keyCode in this._pressedKeys.items) {
if (this._pressedKeys.items.hasOwnProperty(keyCode)) {
if (this._pressedKeys.items[keyCode]) {
return true;
}
}
}
return false;
};
/**
* Should be called when the mouse is moved.
*
* Please note that the coordinates must be expressed relative to the view position.
*
* @param {number} x The mouse new X position
* @param {number} y The mouse new Y position
*/
gdjs.InputManager.prototype.onMouseMove = function(x, y) {
this._mouseX = x;
this._mouseY = y;
};
/**
* Get the mouse X position
*
* @return the mouse X position, relative to the game view.
*/
gdjs.InputManager.prototype.getMouseX = function() {
return this._mouseX;
};
/**
* Get the mouse Y position
*
* @return the mouse Y position, relative to the game view.
*/
gdjs.InputManager.prototype.getMouseY = function() {
return this._mouseY;
};
/**
* Should be called whenever a mouse button is pressed.
* @param {number} buttonCode The mouse button code associated to the event.
* See gdjs.InputManager.MOUSE_LEFT_BUTTON, gdjs.InputManager.MOUSE_RIGHT_BUTTON, gdjs.InputManager.MOUSE_MIDDLE_BUTTON
*/
gdjs.InputManager.prototype.onMouseButtonPressed = function(buttonCode) {
this._pressedMouseButtons[buttonCode] = true;
this._releasedMouseButtons[buttonCode] = false;
};
/**
* Should be called whenever a mouse button is released.
* @param {number} buttonCode The mouse button code associated to the event. (see onMouseButtonPressed)
*/
gdjs.InputManager.prototype.onMouseButtonReleased = function(buttonCode) {
this._pressedMouseButtons[buttonCode] = false;
this._releasedMouseButtons[buttonCode] = true;
};
/**
* Return true if the mouse button corresponding to buttonCode is pressed.
* @param {number} buttonCode The mouse button code (0: Left button, 1: Right button).
*/
gdjs.InputManager.prototype.isMouseButtonPressed = function(buttonCode) {
return (
this._pressedMouseButtons[buttonCode] !== undefined &&
this._pressedMouseButtons[buttonCode]
);
};
/**
* Return true if the mouse button corresponding to buttonCode was just released.
* @param {number} buttonCode The mouse button code (0: Left button, 1: Right button).
*/
gdjs.InputManager.prototype.isMouseButtonReleased = function(buttonCode) {
return (
this._releasedMouseButtons[buttonCode] !== undefined &&
this._releasedMouseButtons[buttonCode]
);
};
/**
* Should be called whenever the mouse wheel is used
* @param {number} wheelDelta The mouse wheel delta
*/
gdjs.InputManager.prototype.onMouseWheel = function(wheelDelta) {
this._mouseWheelDelta = wheelDelta;
};
/**
* Return the mouse wheel delta
*/
gdjs.InputManager.prototype.getMouseWheelDelta = function() {
return this._mouseWheelDelta;
};
/**
* Get a touch X position
*
* @return the touch X position, relative to the game view.
*/
gdjs.InputManager.prototype.getTouchX = function(identifier) {
if (!this._touches.containsKey(identifier)) return 0;
return this._touches.get(identifier).x;
};
/**
* Get a touch Y position
*
* @return the touch Y position, relative to the game view.
*/
gdjs.InputManager.prototype.getTouchY = function(identifier) {
if (!this._touches.containsKey(identifier)) return 0;
return this._touches.get(identifier).y;
};
/**
* Update and return the array containing the identifiers of all touches.
*
*/
gdjs.InputManager.prototype.getAllTouchIdentifiers = function() {
gdjs.InputManager._allTouchIds = gdjs.InputManager._allTouchIds || [];
gdjs.InputManager._allTouchIds.length = 0;
for (var id in this._touches.items) {
if (this._touches.items.hasOwnProperty(id)) {
gdjs.InputManager._allTouchIds.push(parseInt(id, 10));
}
}
return gdjs.InputManager._allTouchIds;
};
gdjs.InputManager.prototype.onTouchStart = function(identifier, x, y) {
this._startedTouches.push(identifier);
this._touches.put(identifier, { x: x, y: y });
if (this._touchSimulateMouse) {
this.onMouseMove(x, y);
this.onMouseButtonPressed(gdjs.InputManager.MOUSE_LEFT_BUTTON);
}
};
gdjs.InputManager.prototype.onTouchMove = function(identifier, x, y) {
var touch = this._touches.get(identifier);
if (!touch) return;
touch.x = x;
touch.y = y;
if (this._touchSimulateMouse) {
this.onMouseMove(x, y);
}
};
gdjs.InputManager.prototype.onTouchEnd = function(identifier) {
this._endedTouches.push(identifier);
if (this._touches.containsKey(identifier)) {
//Postpone deletion at the end of the frame
this._touches.get(identifier).justEnded = true;
}
if (this._touchSimulateMouse) {
this.onMouseButtonReleased(gdjs.InputManager.MOUSE_LEFT_BUTTON);
}
};
gdjs.InputManager.prototype.getStartedTouchIdentifiers = function() {
return this._startedTouches;
};
gdjs.InputManager.prototype.popStartedTouch = function() {
return this._startedTouches.shift();
};
gdjs.InputManager.prototype.popEndedTouch = function() {
return this._endedTouches.shift();
};
/**
* Set if touch events should simulate mouse events.
*
* If true, any touch will move the mouse position and set mouse buttons
* as pressed/released.
* @param enable {Boolean} true to simulate mouse events, false to disable it.
*/
gdjs.InputManager.prototype.touchSimulateMouse = function(enable) {
if (enable === undefined) enable = true;
this._touchSimulateMouse = enable;
};
/**
* Notify the input manager that the frame ended, so anything that last
* only for one frame (started/ended touches) should be reset.
*
* This method should be called in the game loop (see gdjs.RuntimeGame.startGameLoop).
*/
gdjs.InputManager.prototype.onFrameEnded = function() {
//Only clear the ended touches at the end of the frame.
for (var id in this._touches.items) {
if (this._touches.items.hasOwnProperty(id)) {
var touch = this._touches.items[id];
if (touch.justEnded) {
this._touches.remove(id);
}
}
}
this._startedTouches.length = 0;
this._endedTouches.length = 0;
this._releasedKeys.clear();
this._releasedMouseButtons.length = 0;
this._mouseWheelDelta = 0;
};
/**
* Return true if the mouse wheel scroll to up
*/
gdjs.InputManager.prototype.isScrollingUp = function() {
return this.getMouseWheelDelta() > 0;
};
/**
* Return true if the mouse wheel scroll to down
*/
gdjs.InputManager.prototype.isScrollingDown = function() {
return this.getMouseWheelDelta() < 0;
};