Skip to content

Commit dde0233

Browse files
committed
Added Gamepad Events
1 parent c3ab9dd commit dde0233

10 files changed

Lines changed: 173 additions & 7 deletions

src/input/gamepad/Button.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66

77
var Class = require('../../utils/Class');
8+
var Events = require('./events');
89

910
/**
1011
* @classdesc
@@ -89,10 +90,14 @@ var Button = new Class({
8990
* Called automatically by the Gamepad as part of its update.
9091
*
9192
* @method Phaser.Input.Gamepad.Button#update
93+
* @fires Phaser.Input.Gamepad.Events#BUTTON_DOWN
94+
* @fires Phaser.Input.Gamepad.Events#BUTTON_UP
95+
* @fires Phaser.Input.Gamepad.Events#GAMEPAD_BUTTON_DOWN
96+
* @fires Phaser.Input.Gamepad.Events#GAMEPAD_BUTTON_UP
9297
* @private
9398
* @since 3.0.0
9499
*
95-
* @param {number} value - The GamepadButton value.
100+
* @param {number} value - The value of the button. Between 0 and 1.
96101
*/
97102
update: function (value)
98103
{
@@ -106,15 +111,15 @@ var Button = new Class({
106111
if (!this.pressed)
107112
{
108113
this.pressed = true;
109-
this.events.emit('down', pad, this, value);
110-
this.pad.emit('down', index, value, this);
114+
this.events.emit(Events.BUTTON_DOWN, pad, this, value);
115+
this.pad.emit(Events.GAMEPAD_BUTTON_DOWN, index, value, this);
111116
}
112117
}
113118
else if (this.pressed)
114119
{
115120
this.pressed = false;
116-
this.events.emit('up', pad, this, value);
117-
this.pad.emit('up', index, value, this);
121+
this.events.emit(Events.BUTTON_UP, pad, this, value);
122+
this.pad.emit(Events.GAMEPAD_BUTTON_UP, index, value, this);
118123
}
119124
},
120125

src/input/gamepad/GamepadPlugin.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
var Class = require('../../utils/Class');
88
var EventEmitter = require('eventemitter3');
9+
var Events = require('./events');
910
var Gamepad = require('./Gamepad');
1011
var GetValue = require('../../utils/object/GetValue');
1112
var InputPluginCache = require('../InputPluginCache');
@@ -445,6 +446,8 @@ var GamepadPlugin = new Class({
445446
*
446447
* @method Phaser.Input.Gamepad.GamepadPlugin#update
447448
* @private
449+
* @fires Phaser.Input.Gamepad.Events#CONNECTED
450+
* @fires Phaser.Input.Gamepad.Events#DISCONNECTED
448451
* @since 3.10.0
449452
*/
450453
update: function ()
@@ -473,11 +476,11 @@ var GamepadPlugin = new Class({
473476

474477
if (event.type === 'gamepadconnected')
475478
{
476-
this.emit('connected', pad, event);
479+
this.emit(Events.CONNECTED, pad, event);
477480
}
478481
else if (event.type === 'gamepaddisconnected')
479482
{
480-
this.emit('disconnected', pad, event);
483+
this.emit(Events.DISCONNECTED, pad, event);
481484
}
482485
}
483486
},
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @author Richard Davey <rich@photonstorm.com>
3+
* @copyright 2019 Photon Storm Ltd.
4+
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
5+
*/
6+
7+
/**
8+
* The Gamepad Button Down Event.
9+
*
10+
* This event is dispatched by the Gamepad Plugin when a button has been pressed on any active Gamepad.
11+
*
12+
* Listen to this event from within a Scene using: `this.input.gamepad.on('down', listener)`.
13+
*
14+
* You can also listen for a DOWN event from a Gamepad instance. See [GAMEPAD_BUTTON_DOWN]{Phaser.Input.Gamepad.Events#GAMEPAD_BUTTON_DOWN} for details.
15+
*
16+
* @event Phaser.Input.Gamepad.Events#BUTTON_DOWN
17+
*
18+
* @param {Phaser.Input.Gamepad} pad - A reference to the Gamepad on which the button was pressed.
19+
* @param {Phaser.Input.Gamepad.Button} button - A reference to the Button which was pressed.
20+
* @param {number} value - The value of the button at the time it was pressed. Between 0 and 1. Some Gamepads have pressure-sensitive buttons.
21+
*/
22+
module.exports = 'down';
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @author Richard Davey <rich@photonstorm.com>
3+
* @copyright 2019 Photon Storm Ltd.
4+
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
5+
*/
6+
7+
/**
8+
* The Gamepad Button Up Event.
9+
*
10+
* This event is dispatched by the Gamepad Plugin when a button has been released on any active Gamepad.
11+
*
12+
* Listen to this event from within a Scene using: `this.input.gamepad.on('up', listener)`.
13+
*
14+
* You can also listen for an UP event from a Gamepad instance. See [GAMEPAD_BUTTON_UP]{Phaser.Input.Gamepad.Events#GAMEPAD_BUTTON_UP} for details.
15+
*
16+
* @event Phaser.Input.Gamepad.Events#BUTTON_UP
17+
*
18+
* @param {Phaser.Input.Gamepad} pad - A reference to the Gamepad on which the button was released.
19+
* @param {Phaser.Input.Gamepad.Button} button - A reference to the Button which was released.
20+
* @param {number} value - The value of the button at the time it was released. Between 0 and 1. Some Gamepads have pressure-sensitive buttons.
21+
*/
22+
module.exports = 'up';
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* @author Richard Davey <rich@photonstorm.com>
3+
* @copyright 2019 Photon Storm Ltd.
4+
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
5+
*/
6+
7+
/**
8+
* The Gamepad Connected Event.
9+
*
10+
* This event is dispatched by the Gamepad Plugin when a Gamepad has been connected.
11+
*
12+
* Listen to this event from within a Scene using: `this.input.gamepad.once('connected', listener)`.
13+
*
14+
* Note that the browser may require you to press a button on a gamepad before it will allow you to access it,
15+
* this is for security reasons. However, it may also trust the page already, in which case you won't get the
16+
* 'connected' event and instead should check `GamepadPlugin.total` to see if it thinks there are any gamepads
17+
* already connected.
18+
*
19+
* @event Phaser.Input.Gamepad.Events#CONNECTED
20+
*
21+
* @param {Phaser.Input.Gamepad} pad - A reference to the Gamepad which was connected.
22+
* @param {Event} event - The native DOM Event that triggered the connection.
23+
*/
24+
module.exports = 'connected';
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* @author Richard Davey <rich@photonstorm.com>
3+
* @copyright 2019 Photon Storm Ltd.
4+
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
5+
*/
6+
7+
/**
8+
* The Gamepad Disconnected Event.
9+
*
10+
* This event is dispatched by the Gamepad Plugin when a Gamepad has been disconnected.
11+
*
12+
* Listen to this event from within a Scene using: `this.input.gamepad.once('disconnected', listener)`.
13+
*
14+
* @event Phaser.Input.Gamepad.Events#DISCONNECTED
15+
*
16+
* @param {Phaser.Input.Gamepad} pad - A reference to the Gamepad which was disconnected.
17+
* @param {Event} event - The native DOM Event that triggered the disconnection.
18+
*/
19+
module.exports = 'disconnected';
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @author Richard Davey <rich@photonstorm.com>
3+
* @copyright 2019 Photon Storm Ltd.
4+
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
5+
*/
6+
7+
/**
8+
* The Gamepad Button Down Event.
9+
*
10+
* This event is dispatched by a Gamepad instance when a button has been pressed on it.
11+
*
12+
* Listen to this event from a Gamepad instance. Once way to get this is from the `pad1`, `pad2`, etc properties on the Gamepad Plugin:
13+
* `this.input.gamepad.pad1.on('down', listener)`.
14+
*
15+
* Note that you will not receive any Gamepad button events until the browser considers the Gamepad as being 'connected'.
16+
*
17+
* You can also listen for a DOWN event from the Gamepad Plugin. See [BUTTON_DOWN]{Phaser.Input.Gamepad.Events#BUTTON_DOWN} for details.
18+
*
19+
* @event Phaser.Input.Gamepad.Events#GAMEPAD_BUTTON_DOWN
20+
*
21+
* @param {integer} index - The index of the button that was pressed.
22+
* @param {number} value - The value of the button at the time it was pressed. Between 0 and 1. Some Gamepads have pressure-sensitive buttons.
23+
* @param {Phaser.Input.Gamepad.Button} button - A reference to the Button which was pressed.
24+
*/
25+
module.exports = 'down';
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @author Richard Davey <rich@photonstorm.com>
3+
* @copyright 2019 Photon Storm Ltd.
4+
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
5+
*/
6+
7+
/**
8+
* The Gamepad Button Up Event.
9+
*
10+
* This event is dispatched by a Gamepad instance when a button has been released on it.
11+
*
12+
* Listen to this event from a Gamepad instance. Once way to get this is from the `pad1`, `pad2`, etc properties on the Gamepad Plugin:
13+
* `this.input.gamepad.pad1.on('up', listener)`.
14+
*
15+
* Note that you will not receive any Gamepad button events until the browser considers the Gamepad as being 'connected'.
16+
*
17+
* You can also listen for an UP event from the Gamepad Plugin. See [BUTTON_UP]{Phaser.Input.Gamepad.Events#BUTTON_UP} for details.
18+
*
19+
* @event Phaser.Input.Gamepad.Events#GAMEPAD_BUTTON_UP
20+
*
21+
* @param {integer} index - The index of the button that was released.
22+
* @param {number} value - The value of the button at the time it was released. Between 0 and 1. Some Gamepads have pressure-sensitive buttons.
23+
* @param {Phaser.Input.Gamepad.Button} button - A reference to the Button which was released.
24+
*/
25+
module.exports = 'up';

src/input/gamepad/events/index.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @author Richard Davey <rich@photonstorm.com>
3+
* @copyright 2019 Photon Storm Ltd.
4+
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
5+
*/
6+
7+
/**
8+
* @namespace Phaser.Input.Gamepad.Events
9+
*/
10+
11+
module.exports = {
12+
13+
BUTTON_DOWN: require('./BUTTON_DOWN_EVENT'),
14+
BUTTON_UP: require('./BUTTON_UP_EVENT'),
15+
CONNECTED: require('./CONNECTED_EVENT'),
16+
DISCONNECTED: require('./DISCONNECTED_EVENT'),
17+
GAMEPAD_BUTTON_DOWN: require('./GAMEPAD_BUTTON_DOWN_EVENT'),
18+
GAMEPAD_BUTTON_UP: require('./GAMEPAD_BUTTON_UP_EVENT')
19+
20+
};

src/input/gamepad/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ module.exports = {
1212

1313
Axis: require('./Axis'),
1414
Button: require('./Button'),
15+
Events: require('./events'),
1516
Gamepad: require('./Gamepad'),
1617
GamepadPlugin: require('./GamepadPlugin'),
1718

0 commit comments

Comments
 (0)