forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKey.js
More file actions
364 lines (286 loc) · 10.2 KB
/
Key.js
File metadata and controls
364 lines (286 loc) · 10.2 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
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2016 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
/**
* If you need more fine-grained control over the handling of specific keys you can create and use Phaser.Key objects.
*
* @class Phaser.Key
* @constructor
* @param {Phaser.Game} game - Current game instance.
* @param {integer} keycode - The key code this Key is responsible for. See {@link Phaser.KeyCode}.
*/
Phaser.Key = function (game, keycode) {
/**
* @property {Phaser.Game} game - A reference to the currently running game.
*/
this.game = game;
/**
* The enabled state of the key - see `enabled`.
* @property {boolean} _enabled
* @private
*/
this._enabled = true;
/**
* @property {object} event - Stores the most recent DOM event.
* @readonly
*/
this.event = null;
/**
* @property {boolean} isDown - The "down" state of the key. This will remain `true` for as long as the keyboard thinks this key is held down.
* @default
*/
this.isDown = false;
/**
* @property {boolean} isUp - The "up" state of the key. This will remain `true` for as long as the keyboard thinks this key is up.
* @default
*/
this.isUp = true;
/**
* @property {boolean} altKey - The down state of the ALT key, if pressed at the same time as this key.
* @default
*/
this.altKey = false;
/**
* @property {boolean} ctrlKey - The down state of the CTRL key, if pressed at the same time as this key.
* @default
*/
this.ctrlKey = false;
/**
* @property {boolean} shiftKey - The down state of the SHIFT key, if pressed at the same time as this key.
* @default
*/
this.shiftKey = false;
/**
* @property {number} timeDown - The timestamp when the key was last pressed down. This is based on Game.time.now.
*/
this.timeDown = 0;
/**
* If the key is down this value holds the duration of that key press and is constantly updated.
* If the key is up it holds the duration of the previous down session.
* @property {number} duration - The number of milliseconds this key has been held down for.
* @default
*/
this.duration = 0;
/**
* @property {number} timeUp - The timestamp when the key was last released. This is based on Game.time.now.
* @default
*/
this.timeUp = -2500;
/**
* @property {number} repeats - If a key is held down this holds down the number of times the key has 'repeated'.
* @default
*/
this.repeats = 0;
/**
* @property {number} keyCode - The keycode of this key.
*/
this.keyCode = keycode;
/**
* @property {Phaser.Signal} onDown - This Signal is dispatched every time this Key is pressed down. It is only dispatched once (until the key is released again).
*/
this.onDown = new Phaser.Signal();
/**
* @property {function} onHoldCallback - A callback that is called while this Key is held down. Warning: Depending on refresh rate that could be 60+ times per second.
*/
this.onHoldCallback = null;
/**
* @property {object} onHoldContext - The context under which the onHoldCallback will be called.
*/
this.onHoldContext = null;
/**
* @property {Phaser.Signal} onUp - This Signal is dispatched every time this Key is released. It is only dispatched once (until the key is pressed and released again).
*/
this.onUp = new Phaser.Signal();
/**
* @property {boolean} _justDown - True if the key has just been pressed (NOTE: requires to be reset, see justDown getter)
* @private
*/
this._justDown = false;
/**
* @property {boolean} _justUp - True if the key has just been pressed (NOTE: requires to be reset, see justDown getter)
* @private
*/
this._justUp = false;
};
Phaser.Key.prototype = {
/**
* Called automatically by Phaser.Keyboard.
*
* @method Phaser.Key#update
* @protected
*/
update: function () {
if (!this._enabled) { return; }
if (this.isDown)
{
this.duration = this.game.time.time - this.timeDown;
this.repeats++;
if (this.onHoldCallback)
{
this.onHoldCallback.call(this.onHoldContext, this);
}
}
},
/**
* Called automatically by Phaser.Keyboard.
*
* @method Phaser.Key#processKeyDown
* @param {KeyboardEvent} event - The DOM event that triggered this.
* @protected
*/
processKeyDown: function (event) {
if (!this._enabled) { return; }
this.event = event;
// exit if this key down is from auto-repeat
if (this.isDown)
{
return;
}
this.altKey = event.altKey;
this.ctrlKey = event.ctrlKey;
this.shiftKey = event.shiftKey;
this.isDown = true;
this.isUp = false;
this.timeDown = this.game.time.time;
this.duration = 0;
this.repeats = 0;
// _justDown will remain true until it is read via the justDown Getter
// this enables the game to poll for past presses, or reset it at the start of a new game state
this._justDown = true;
this.onDown.dispatch(this);
},
/**
* Called automatically by Phaser.Keyboard.
*
* @method Phaser.Key#processKeyUp
* @param {KeyboardEvent} event - The DOM event that triggered this.
* @protected
*/
processKeyUp: function (event) {
if (!this._enabled) { return; }
this.event = event;
if (this.isUp)
{
return;
}
this.isDown = false;
this.isUp = true;
this.timeUp = this.game.time.time;
this.duration = this.game.time.time - this.timeDown;
// _justUp will remain true until it is read via the justUp Getter
// this enables the game to poll for past presses, or reset it at the start of a new game state
this._justUp = true;
this.onUp.dispatch(this);
},
/**
* Resets the state of this Key.
*
* This sets isDown to false, isUp to true, resets the time to be the current time, and _enables_ the key.
* In addition, if it is a "hard reset", it clears clears any callbacks associated with the onDown and onUp events and removes the onHoldCallback.
*
* @method Phaser.Key#reset
* @param {boolean} [hard=true] - A soft reset won't reset any events or callbacks; a hard reset will.
*/
reset: function (hard) {
if (hard === undefined) { hard = true; }
this.isDown = false;
this.isUp = true;
this.timeUp = this.game.time.time;
this.duration = 0;
this._enabled = true; // .enabled causes reset(false)
this._justDown = false;
this._justUp = false;
if (hard)
{
this.onDown.removeAll();
this.onUp.removeAll();
this.onHoldCallback = null;
this.onHoldContext = null;
}
},
/**
* Returns `true` if the Key was pressed down within the `duration` value given, or `false` if it either isn't down,
* or was pressed down longer ago than then given duration.
*
* @method Phaser.Key#downDuration
* @param {number} [duration=50] - The duration within which the key is considered as being just pressed. Given in ms.
* @return {boolean} True if the key was pressed down within the given duration.
*/
downDuration: function (duration) {
if (duration === undefined) { duration = 50; }
return (this.isDown && this.duration < duration);
},
/**
* Returns `true` if the Key was pressed down within the `duration` value given, or `false` if it either isn't down,
* or was pressed down longer ago than then given duration.
*
* @method Phaser.Key#upDuration
* @param {number} [duration=50] - The duration within which the key is considered as being just released. Given in ms.
* @return {boolean} True if the key was released within the given duration.
*/
upDuration: function (duration) {
if (duration === undefined) { duration = 50; }
return (!this.isDown && ((this.game.time.time - this.timeUp) < duration));
}
};
/**
* The justDown value allows you to test if this Key has just been pressed down or not.
* When you check this value it will return `true` if the Key is down, otherwise `false`.
* You can only call justDown once per key press. It will only return `true` once, until the Key is released and pressed down again.
* This allows you to use it in situations where you want to check if this key is down without using a Signal, such as in a core game loop.
*
* @property {boolean} justDown
* @memberof Phaser.Key
* @default false
*/
Object.defineProperty(Phaser.Key.prototype, "justDown", {
get: function () {
var current = this._justDown;
this._justDown = false;
return current;
}
});
/**
* The justUp value allows you to test if this Key has just been released or not.
* When you check this value it will return `true` if the Key is up, otherwise `false`.
* You can only call justUp once per key release. It will only return `true` once, until the Key is pressed down and released again.
* This allows you to use it in situations where you want to check if this key is up without using a Signal, such as in a core game loop.
*
* @property {boolean} justUp
* @memberof Phaser.Key
* @default false
*/
Object.defineProperty(Phaser.Key.prototype, "justUp", {
get: function () {
var current = this._justUp;
this._justUp = false;
return current;
}
});
/**
* An enabled key processes its update and dispatches events.
* A key can be disabled momentarily at runtime instead of deleting it.
*
* @property {boolean} enabled
* @memberof Phaser.Key
* @default true
*/
Object.defineProperty(Phaser.Key.prototype, "enabled", {
get: function () {
return this._enabled;
},
set: function (value) {
value = !!value;
if (value !== this._enabled)
{
if (!value)
{
this.reset(false);
}
this._enabled = value;
}
}
});
Phaser.Key.prototype.constructor = Phaser.Key;