/
Gamepad.js
767 lines (661 loc) · 21.3 KB
/
Gamepad.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
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2020 Photon Storm Ltd.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var Axis = require('./Axis');
var Button = require('./Button');
var Class = require('../../utils/Class');
var EventEmitter = require('eventemitter3');
var Vector2 = require('../../math/Vector2');
/**
* @classdesc
* A single Gamepad.
*
* These are created, updated and managed by the Gamepad Plugin.
*
* @class Gamepad
* @extends Phaser.Events.EventEmitter
* @memberof Phaser.Input.Gamepad
* @constructor
* @since 3.0.0
*
* @param {Phaser.Input.Gamepad.GamepadPlugin} manager - A reference to the Gamepad Plugin.
* @param {Phaser.Types.Input.Gamepad.Pad} pad - The Gamepad object, as extracted from GamepadEvent.
*/
var Gamepad = new Class({
Extends: EventEmitter,
initialize:
function Gamepad (manager, pad)
{
EventEmitter.call(this);
/**
* A reference to the Gamepad Plugin.
*
* @name Phaser.Input.Gamepad.Gamepad#manager
* @type {Phaser.Input.Gamepad.GamepadPlugin}
* @since 3.0.0
*/
this.manager = manager;
/**
* A reference to the native Gamepad object that is connected to the browser.
*
* @name Phaser.Input.Gamepad.Gamepad#pad
* @type {any}
* @since 3.10.0
*/
this.pad = pad;
/**
* A string containing some information about the controller.
*
* This is not strictly specified, but in Firefox it will contain three pieces of information
* separated by dashes (-): two 4-digit hexadecimal strings containing the USB vendor and
* product id of the controller, and the name of the controller as provided by the driver.
* In Chrome it will contain the name of the controller as provided by the driver,
* followed by vendor and product 4-digit hexadecimal strings.
*
* @name Phaser.Input.Gamepad.Gamepad#id
* @type {string}
* @since 3.0.0
*/
this.id = pad.id;
/**
* An integer that is unique for each Gamepad currently connected to the system.
* This can be used to distinguish multiple controllers.
* Note that disconnecting a device and then connecting a new device may reuse the previous index.
*
* @name Phaser.Input.Gamepad.Gamepad#index
* @type {number}
* @since 3.0.0
*/
this.index = pad.index;
var buttons = [];
for (var i = 0; i < pad.buttons.length; i++)
{
buttons.push(new Button(this, i));
}
/**
* An array of Gamepad Button objects, corresponding to the different buttons available on the Gamepad.
*
* @name Phaser.Input.Gamepad.Gamepad#buttons
* @type {Phaser.Input.Gamepad.Button[]}
* @since 3.0.0
*/
this.buttons = buttons;
var axes = [];
for (i = 0; i < pad.axes.length; i++)
{
axes.push(new Axis(this, i));
}
/**
* An array of Gamepad Axis objects, corresponding to the different axes available on the Gamepad, if any.
*
* @name Phaser.Input.Gamepad.Gamepad#axes
* @type {Phaser.Input.Gamepad.Axis[]}
* @since 3.0.0
*/
this.axes = axes;
/**
* The Gamepad's Haptic Actuator (Vibration / Rumble support).
* This is highly experimental and only set if both present on the device,
* and exposed by both the hardware and browser.
*
* @name Phaser.Input.Gamepad.Gamepad#vibration
* @type {GamepadHapticActuator}
* @since 3.10.0
*/
this.vibration = pad.vibrationActuator;
// https://w3c.github.io/gamepad/#remapping
var _noButton = { value: 0, pressed: false };
/**
* A reference to the Left Button in the Left Cluster.
*
* @name Phaser.Input.Gamepad.Gamepad#_LCLeft
* @type {Phaser.Input.Gamepad.Button}
* @private
* @since 3.10.0
*/
this._LCLeft = (buttons[14]) ? buttons[14] : _noButton;
/**
* A reference to the Right Button in the Left Cluster.
*
* @name Phaser.Input.Gamepad.Gamepad#_LCRight
* @type {Phaser.Input.Gamepad.Button}
* @private
* @since 3.10.0
*/
this._LCRight = (buttons[15]) ? buttons[15] : _noButton;
/**
* A reference to the Top Button in the Left Cluster.
*
* @name Phaser.Input.Gamepad.Gamepad#_LCTop
* @type {Phaser.Input.Gamepad.Button}
* @private
* @since 3.10.0
*/
this._LCTop = (buttons[12]) ? buttons[12] : _noButton;
/**
* A reference to the Bottom Button in the Left Cluster.
*
* @name Phaser.Input.Gamepad.Gamepad#_LCBottom
* @type {Phaser.Input.Gamepad.Button}
* @private
* @since 3.10.0
*/
this._LCBottom = (buttons[13]) ? buttons[13] : _noButton;
/**
* A reference to the Left Button in the Right Cluster.
*
* @name Phaser.Input.Gamepad.Gamepad#_RCLeft
* @type {Phaser.Input.Gamepad.Button}
* @private
* @since 3.10.0
*/
this._RCLeft = (buttons[2]) ? buttons[2] : _noButton;
/**
* A reference to the Right Button in the Right Cluster.
*
* @name Phaser.Input.Gamepad.Gamepad#_RCRight
* @type {Phaser.Input.Gamepad.Button}
* @private
* @since 3.10.0
*/
this._RCRight = (buttons[1]) ? buttons[1] : _noButton;
/**
* A reference to the Top Button in the Right Cluster.
*
* @name Phaser.Input.Gamepad.Gamepad#_RCTop
* @type {Phaser.Input.Gamepad.Button}
* @private
* @since 3.10.0
*/
this._RCTop = (buttons[3]) ? buttons[3] : _noButton;
/**
* A reference to the Bottom Button in the Right Cluster.
*
* @name Phaser.Input.Gamepad.Gamepad#_RCBottom
* @type {Phaser.Input.Gamepad.Button}
* @private
* @since 3.10.0
*/
this._RCBottom = (buttons[0]) ? buttons[0] : _noButton;
/**
* A reference to the Top Left Front Button (L1 Shoulder Button)
*
* @name Phaser.Input.Gamepad.Gamepad#_FBLeftTop
* @type {Phaser.Input.Gamepad.Button}
* @private
* @since 3.10.0
*/
this._FBLeftTop = (buttons[4]) ? buttons[4] : _noButton;
/**
* A reference to the Bottom Left Front Button (L2 Shoulder Button)
*
* @name Phaser.Input.Gamepad.Gamepad#_FBLeftBottom
* @type {Phaser.Input.Gamepad.Button}
* @private
* @since 3.10.0
*/
this._FBLeftBottom = (buttons[6]) ? buttons[6] : _noButton;
/**
* A reference to the Top Right Front Button (R1 Shoulder Button)
*
* @name Phaser.Input.Gamepad.Gamepad#_FBRightTop
* @type {Phaser.Input.Gamepad.Button}
* @private
* @since 3.10.0
*/
this._FBRightTop = (buttons[5]) ? buttons[5] : _noButton;
/**
* A reference to the Bottom Right Front Button (R2 Shoulder Button)
*
* @name Phaser.Input.Gamepad.Gamepad#_FBRightBottom
* @type {Phaser.Input.Gamepad.Button}
* @private
* @since 3.10.0
*/
this._FBRightBottom = (buttons[7]) ? buttons[7] : _noButton;
var _noAxis = { value: 0 };
/**
* A reference to the Horizontal Axis for the Left Stick.
*
* @name Phaser.Input.Gamepad.Gamepad#_HAxisLeft
* @type {Phaser.Input.Gamepad.Button}
* @private
* @since 3.10.0
*/
this._HAxisLeft = (axes[0]) ? axes[0] : _noAxis;
/**
* A reference to the Vertical Axis for the Left Stick.
*
* @name Phaser.Input.Gamepad.Gamepad#_VAxisLeft
* @type {Phaser.Input.Gamepad.Button}
* @private
* @since 3.10.0
*/
this._VAxisLeft = (axes[1]) ? axes[1] : _noAxis;
/**
* A reference to the Horizontal Axis for the Right Stick.
*
* @name Phaser.Input.Gamepad.Gamepad#_HAxisRight
* @type {Phaser.Input.Gamepad.Button}
* @private
* @since 3.10.0
*/
this._HAxisRight = (axes[2]) ? axes[2] : _noAxis;
/**
* A reference to the Vertical Axis for the Right Stick.
*
* @name Phaser.Input.Gamepad.Gamepad#_VAxisRight
* @type {Phaser.Input.Gamepad.Button}
* @private
* @since 3.10.0
*/
this._VAxisRight = (axes[3]) ? axes[3] : _noAxis;
/**
* A Vector2 containing the most recent values from the Gamepad's left axis stick.
* This is updated automatically as part of the Gamepad.update cycle.
* The H Axis is mapped to the `Vector2.x` property, and the V Axis to the `Vector2.y` property.
* The values are based on the Axis thresholds.
* If the Gamepad does not have a left axis stick, the values will always be zero.
*
* @name Phaser.Input.Gamepad.Gamepad#leftStick
* @type {Phaser.Math.Vector2}
* @since 3.10.0
*/
this.leftStick = new Vector2();
/**
* A Vector2 containing the most recent values from the Gamepad's right axis stick.
* This is updated automatically as part of the Gamepad.update cycle.
* The H Axis is mapped to the `Vector2.x` property, and the V Axis to the `Vector2.y` property.
* The values are based on the Axis thresholds.
* If the Gamepad does not have a right axis stick, the values will always be zero.
*
* @name Phaser.Input.Gamepad.Gamepad#rightStick
* @type {Phaser.Math.Vector2}
* @since 3.10.0
*/
this.rightStick = new Vector2();
/**
* When was this Gamepad created? Used to avoid duplicate event spamming in the update loop.
*
* @name Phaser.Input.Gamepad.Gamepad#_created
* @type {number}
* @private
* @since 3.50.0
*/
this._created = performance.now();
},
/**
* Gets the total number of axis this Gamepad claims to support.
*
* @method Phaser.Input.Gamepad.Gamepad#getAxisTotal
* @since 3.10.0
*
* @return {number} The total number of axes this Gamepad claims to support.
*/
getAxisTotal: function ()
{
return this.axes.length;
},
/**
* Gets the value of an axis based on the given index.
* The index must be valid within the range of axes supported by this Gamepad.
* The return value will be a float between 0 and 1.
*
* @method Phaser.Input.Gamepad.Gamepad#getAxisValue
* @since 3.10.0
*
* @param {number} index - The index of the axes to get the value for.
*
* @return {number} The value of the axis, between 0 and 1.
*/
getAxisValue: function (index)
{
return this.axes[index].getValue();
},
/**
* Sets the threshold value of all axis on this Gamepad.
* The value is a float between 0 and 1 and is the amount below which the axis is considered as not having been moved.
*
* @method Phaser.Input.Gamepad.Gamepad#setAxisThreshold
* @since 3.10.0
*
* @param {number} value - A value between 0 and 1.
*/
setAxisThreshold: function (value)
{
for (var i = 0; i < this.axes.length; i++)
{
this.axes[i].threshold = value;
}
},
/**
* Gets the total number of buttons this Gamepad claims to have.
*
* @method Phaser.Input.Gamepad.Gamepad#getButtonTotal
* @since 3.10.0
*
* @return {number} The total number of buttons this Gamepad claims to have.
*/
getButtonTotal: function ()
{
return this.buttons.length;
},
/**
* Gets the value of a button based on the given index.
* The index must be valid within the range of buttons supported by this Gamepad.
*
* The return value will be either 0 or 1 for an analogue button, or a float between 0 and 1
* for a pressure-sensitive digital button, such as the shoulder buttons on a Dual Shock.
*
* @method Phaser.Input.Gamepad.Gamepad#getButtonValue
* @since 3.10.0
*
* @param {number} index - The index of the button to get the value for.
*
* @return {number} The value of the button, between 0 and 1.
*/
getButtonValue: function (index)
{
return this.buttons[index].value;
},
/**
* Returns if the button is pressed down or not.
* The index must be valid within the range of buttons supported by this Gamepad.
*
* @method Phaser.Input.Gamepad.Gamepad#isButtonDown
* @since 3.10.0
*
* @param {number} index - The index of the button to get the value for.
*
* @return {boolean} `true` if the button is considered as being pressed down, otherwise `false`.
*/
isButtonDown: function (index)
{
return this.buttons[index].pressed;
},
/**
* Internal update handler for this Gamepad.
* Called automatically by the Gamepad Manager as part of its update.
*
* @method Phaser.Input.Gamepad.Gamepad#update
* @private
* @since 3.0.0
*/
update: function (pad)
{
if (pad.timestamp < this._created)
{
return;
}
var i;
// Sync the button values
var localButtons = this.buttons;
var gamepadButtons = pad.buttons;
var len = localButtons.length;
for (i = 0; i < len; i++)
{
localButtons[i].update(gamepadButtons[i].value);
}
// Sync the axis values
var localAxes = this.axes;
var gamepadAxes = pad.axes;
len = localAxes.length;
for (i = 0; i < len; i++)
{
localAxes[i].update(gamepadAxes[i]);
}
if (len >= 2)
{
this.leftStick.set(localAxes[0].getValue(), localAxes[1].getValue());
if (len >= 4)
{
this.rightStick.set(localAxes[2].getValue(), localAxes[3].getValue());
}
}
},
/**
* Destroys this Gamepad instance, its buttons and axes, and releases external references it holds.
*
* @method Phaser.Input.Gamepad.Gamepad#destroy
* @since 3.10.0
*/
destroy: function ()
{
this.removeAllListeners();
this.manager = null;
this.pad = null;
var i;
for (i = 0; i < this.buttons.length; i++)
{
this.buttons[i].destroy();
}
for (i = 0; i < this.axes.length; i++)
{
this.axes[i].destroy();
}
this.buttons = [];
this.axes = [];
},
/**
* Is this Gamepad currently connected or not?
*
* @name Phaser.Input.Gamepad.Gamepad#connected
* @type {boolean}
* @default true
* @since 3.0.0
*/
connected: {
get: function ()
{
return this.pad.connected;
}
},
/**
* A timestamp containing the most recent time this Gamepad was updated.
*
* @name Phaser.Input.Gamepad.Gamepad#timestamp
* @type {number}
* @since 3.0.0
*/
timestamp: {
get: function ()
{
return this.pad.timestamp;
}
},
/**
* Is the Gamepad's Left button being pressed?
* If the Gamepad doesn't have this button it will always return false.
* This is the d-pad left button under standard Gamepad mapping.
*
* @name Phaser.Input.Gamepad.Gamepad#left
* @type {boolean}
* @since 3.10.0
*/
left: {
get: function ()
{
return this._LCLeft.pressed;
}
},
/**
* Is the Gamepad's Right button being pressed?
* If the Gamepad doesn't have this button it will always return false.
* This is the d-pad right button under standard Gamepad mapping.
*
* @name Phaser.Input.Gamepad.Gamepad#right
* @type {boolean}
* @since 3.10.0
*/
right: {
get: function ()
{
return this._LCRight.pressed;
}
},
/**
* Is the Gamepad's Up button being pressed?
* If the Gamepad doesn't have this button it will always return false.
* This is the d-pad up button under standard Gamepad mapping.
*
* @name Phaser.Input.Gamepad.Gamepad#up
* @type {boolean}
* @since 3.10.0
*/
up: {
get: function ()
{
return this._LCTop.pressed;
}
},
/**
* Is the Gamepad's Down button being pressed?
* If the Gamepad doesn't have this button it will always return false.
* This is the d-pad down button under standard Gamepad mapping.
*
* @name Phaser.Input.Gamepad.Gamepad#down
* @type {boolean}
* @since 3.10.0
*/
down: {
get: function ()
{
return this._LCBottom.pressed;
}
},
/**
* Is the Gamepad's bottom button in the right button cluster being pressed?
* If the Gamepad doesn't have this button it will always return false.
* On a Dual Shock controller it's the X button.
* On an XBox controller it's the A button.
*
* @name Phaser.Input.Gamepad.Gamepad#A
* @type {boolean}
* @since 3.10.0
*/
A: {
get: function ()
{
return this._RCBottom.pressed;
}
},
/**
* Is the Gamepad's top button in the right button cluster being pressed?
* If the Gamepad doesn't have this button it will always return false.
* On a Dual Shock controller it's the Triangle button.
* On an XBox controller it's the Y button.
*
* @name Phaser.Input.Gamepad.Gamepad#Y
* @type {boolean}
* @since 3.10.0
*/
Y: {
get: function ()
{
return this._RCTop.pressed;
}
},
/**
* Is the Gamepad's left button in the right button cluster being pressed?
* If the Gamepad doesn't have this button it will always return false.
* On a Dual Shock controller it's the Square button.
* On an XBox controller it's the X button.
*
* @name Phaser.Input.Gamepad.Gamepad#X
* @type {boolean}
* @since 3.10.0
*/
X: {
get: function ()
{
return this._RCLeft.pressed;
}
},
/**
* Is the Gamepad's right button in the right button cluster being pressed?
* If the Gamepad doesn't have this button it will always return false.
* On a Dual Shock controller it's the Circle button.
* On an XBox controller it's the B button.
*
* @name Phaser.Input.Gamepad.Gamepad#B
* @type {boolean}
* @since 3.10.0
*/
B: {
get: function ()
{
return this._RCRight.pressed;
}
},
/**
* Returns the value of the Gamepad's top left shoulder button.
* If the Gamepad doesn't have this button it will always return zero.
* The value is a float between 0 and 1, corresponding to how depressed the button is.
* On a Dual Shock controller it's the L1 button.
* On an XBox controller it's the LB button.
*
* @name Phaser.Input.Gamepad.Gamepad#L1
* @type {number}
* @since 3.10.0
*/
L1: {
get: function ()
{
return this._FBLeftTop.value;
}
},
/**
* Returns the value of the Gamepad's bottom left shoulder button.
* If the Gamepad doesn't have this button it will always return zero.
* The value is a float between 0 and 1, corresponding to how depressed the button is.
* On a Dual Shock controller it's the L2 button.
* On an XBox controller it's the LT button.
*
* @name Phaser.Input.Gamepad.Gamepad#L2
* @type {number}
* @since 3.10.0
*/
L2: {
get: function ()
{
return this._FBLeftBottom.value;
}
},
/**
* Returns the value of the Gamepad's top right shoulder button.
* If the Gamepad doesn't have this button it will always return zero.
* The value is a float between 0 and 1, corresponding to how depressed the button is.
* On a Dual Shock controller it's the R1 button.
* On an XBox controller it's the RB button.
*
* @name Phaser.Input.Gamepad.Gamepad#R1
* @type {number}
* @since 3.10.0
*/
R1: {
get: function ()
{
return this._FBRightTop.value;
}
},
/**
* Returns the value of the Gamepad's bottom right shoulder button.
* If the Gamepad doesn't have this button it will always return zero.
* The value is a float between 0 and 1, corresponding to how depressed the button is.
* On a Dual Shock controller it's the R2 button.
* On an XBox controller it's the RT button.
*
* @name Phaser.Input.Gamepad.Gamepad#R2
* @type {number}
* @since 3.10.0
*/
R2: {
get: function ()
{
return this._FBRightBottom.value;
}
}
});
module.exports = Gamepad;