From 5369ecbffe8a297cdf1bfe81985a81a03fd0c024 Mon Sep 17 00:00:00 2001 From: Richard Davey Date: Mon, 11 Sep 2017 01:28:09 +0100 Subject: [PATCH] Added in Gamepad axis support Also removed issue stopping analogue buttons working properly. Added in config for DualShock 4 controller. --- v3/src/input/gamepad/Axis.js | 44 +++++++++++++++++ v3/src/input/gamepad/Button.js | 14 ++++-- v3/src/input/gamepad/Gamepad.js | 15 +++++- v3/src/input/gamepad/GamepadManager.js | 49 ++++++++++++++++++- .../configs/Sony_PlayStation_DualShock_4.js | 32 ++++++++++++ v3/src/input/gamepad/configs/index.js | 1 + .../gamepad/events/GamepadConnectedEvent.js | 21 ++++++++ .../events/GamepadDisconnectedEvent.js | 21 ++++++++ v3/src/input/gamepad/events/index.js | 2 + v3/src/input/gamepad/index.js | 1 + 10 files changed, 192 insertions(+), 8 deletions(-) create mode 100644 v3/src/input/gamepad/Axis.js create mode 100644 v3/src/input/gamepad/configs/Sony_PlayStation_DualShock_4.js create mode 100644 v3/src/input/gamepad/events/GamepadConnectedEvent.js create mode 100644 v3/src/input/gamepad/events/GamepadDisconnectedEvent.js diff --git a/v3/src/input/gamepad/Axis.js b/v3/src/input/gamepad/Axis.js new file mode 100644 index 000000000..9bb0b01cd --- /dev/null +++ b/v3/src/input/gamepad/Axis.js @@ -0,0 +1,44 @@ +// Phaser.Input.Gamepad.Axis + +var Class = require('../../utils/Class'); +var GamepadEvent = require('./events/'); + +var Axis = new Class({ + + initialize: + + function Axis (pad, index) + { + this.pad = pad; + + this.events = pad.events; + + this.index = index; + + // Between -1 and 1 with 0 being dead center + this.value = 0; + + this.threshold = 0.05; + }, + + update: function (value) + { + this.value = value; + }, + + // Applies threshold to the value and returns it + getValue: function () + { + var percentage = (Math.abs(this.value) - this.threshold) / (1 - this.threshold); + + if (percentage < 0) + { + percentage = 0; + } + + return percentage * (this.value > 0 ? 1 : -1); + } + +}); + +module.exports = Axis; diff --git a/v3/src/input/gamepad/Button.js b/v3/src/input/gamepad/Button.js index f2787d77b..1137c99db 100644 --- a/v3/src/input/gamepad/Button.js +++ b/v3/src/input/gamepad/Button.js @@ -15,19 +15,24 @@ var Button = new Class({ this.index = index; - this.pressed = false; - + // Between 0 and 1 this.value = 0; + + // Can be set for Analogue buttons to enable a 'pressure' threshold before considered as 'pressed' + this.threshold = 0; + + this.pressed = false; }, update: function (data) { - if (data.pressed) + this.value = data.value; + + if (this.value >= this.threshold) { if (!this.pressed) { this.pressed = true; - this.value = data.value; this.events.dispatch(new GamepadEvent.DOWN(this.pad, this, this.value, data)); } } @@ -36,7 +41,6 @@ var Button = new Class({ if (this.pressed) { this.pressed = false; - this.value = data.value; this.events.dispatch(new GamepadEvent.UP(this.pad, this, this.value, data)); } } diff --git a/v3/src/input/gamepad/Gamepad.js b/v3/src/input/gamepad/Gamepad.js index 118d59278..1a566a589 100644 --- a/v3/src/input/gamepad/Gamepad.js +++ b/v3/src/input/gamepad/Gamepad.js @@ -1,5 +1,6 @@ // Phaser.Input.Gamepad.Gamepad +var Axis = require('./Axis'); var Button = require('./Button'); var Class = require('../../utils/Class'); var GamepadEvent = require('./events/'); @@ -23,6 +24,7 @@ var Gamepad = new Class({ this.timestamp = 0; this.buttons = []; + this.axes = []; }, update: function (data) @@ -44,7 +46,18 @@ var Gamepad = new Class({ this.buttons[i].update(buttonData); } - // TODO: Axes + // Axes + for (var i = 0; i < data.axes.length; i++) + { + var axisData = data.axes[i]; + + if (this.axes[i] === undefined) + { + this.axes[i] = new Axis(this, i); + } + + this.axes[i].update(axisData); + } } }); diff --git a/v3/src/input/gamepad/GamepadManager.js b/v3/src/input/gamepad/GamepadManager.js index c911241c1..4adc383df 100644 --- a/v3/src/input/gamepad/GamepadManager.js +++ b/v3/src/input/gamepad/GamepadManager.js @@ -7,6 +7,7 @@ var GamepadEvent = require('./events/'); // https://developer.mozilla.org/en-US/docs/Web/API/Gamepad_API // https://developer.mozilla.org/en-US/docs/Web/API/Gamepad_API/Using_the_Gamepad_API // https://www.smashingmagazine.com/2015/11/gamepad-api-in-web-games/ +// http://html5gamepad.com/ var GamepadManager = new Class({ @@ -129,6 +130,32 @@ var GamepadManager = new Class({ } }, + getAll: function () + { + var out = []; + + for (var i = 0; i < this.gamepads.length; i++) + { + if (this.gamepads[i]) + { + out.push(this.gamepads[i]); + } + } + + return out; + }, + + getPad: function (index) + { + for (var i = 0; i < this.gamepads.length; i++) + { + if (this.gamepads[i].index === index) + { + return this.gamepads[i]; + } + } + }, + update: function () { if (!this.enabled) @@ -151,18 +178,36 @@ var GamepadManager = new Class({ for (var i = 0; i < len; i++) { var event = queue[i]; + var pad; switch (event.type) { case 'gamepadconnected': - // Event? + + pad = this.getPad(event.gamepad.index); + + this.events.dispatch(new GamepadEvent.CONNECTED(pad, event)); + break; case 'gamepaddisconnected': - // Event? + + pad = this.getPad(event.gamepad.index); + + this.events.dispatch(new GamepadEvent.DISCONNECTED(pad, event)); + break; } } + }, + + total: { + + get: function () + { + return this.gamepads.length; + } + } }); diff --git a/v3/src/input/gamepad/configs/Sony_PlayStation_DualShock_4.js b/v3/src/input/gamepad/configs/Sony_PlayStation_DualShock_4.js new file mode 100644 index 000000000..58d276fbd --- /dev/null +++ b/v3/src/input/gamepad/configs/Sony_PlayStation_DualShock_4.js @@ -0,0 +1,32 @@ +// Sony PlayStation DualShock 4 (v2) wireless controller + +module.exports = { + + UP: 12, + DOWN: 13, + LEFT: 14, + RIGHT: 15, + + SHARE: 8, + OPTIONS: 9, + PS: 16, + TOUCHBAR: 17, + + X: 0, + CIRCLE: 1, + SQUARE: 2, + TRIANGLE: 3, + + L1: 4, + R1: 5, + L2: 6, + R2: 7, + L3: 10, + R3: 11, + + LEFT_STICK_H: 0, + LEFT_STICK_V: 1, + RIGHT_STICK_H: 2, + RIGHT_STICK_V: 3 + +}; diff --git a/v3/src/input/gamepad/configs/index.js b/v3/src/input/gamepad/configs/index.js index b33af449c..166874b56 100644 --- a/v3/src/input/gamepad/configs/index.js +++ b/v3/src/input/gamepad/configs/index.js @@ -2,6 +2,7 @@ module.exports = { + DUALSHOCK_4: require('./Sony_PlayStation_DualShock_4'), SNES_USB: require('./SNES_USB_Controller') }; diff --git a/v3/src/input/gamepad/events/GamepadConnectedEvent.js b/v3/src/input/gamepad/events/GamepadConnectedEvent.js new file mode 100644 index 000000000..bc8caadf3 --- /dev/null +++ b/v3/src/input/gamepad/events/GamepadConnectedEvent.js @@ -0,0 +1,21 @@ +var Class = require('../../../utils/Class'); +var Event = require('../../../events/Event'); + +var GamepadConnectedEvent = new Class({ + + Extends: Event, + + initialize: + + function GamepadConnectedEvent (gamepad, nativeEvent) + { + Event.call(this, 'GAMEPAD_CONNECTED_EVENT'); + + this.data = nativeEvent; + + this.gamepad = gamepad; + } + +}); + +module.exports = GamepadConnectedEvent; diff --git a/v3/src/input/gamepad/events/GamepadDisconnectedEvent.js b/v3/src/input/gamepad/events/GamepadDisconnectedEvent.js new file mode 100644 index 000000000..476248fa7 --- /dev/null +++ b/v3/src/input/gamepad/events/GamepadDisconnectedEvent.js @@ -0,0 +1,21 @@ +var Class = require('../../../utils/Class'); +var Event = require('../../../events/Event'); + +var GamepadDisconnectedEvent = new Class({ + + Extends: Event, + + initialize: + + function GamepadDisconnectedEvent (gamepad, nativeEvent) + { + Event.call(this, 'GAMEPAD_DISCONNECTED_EVENT'); + + this.data = nativeEvent; + + this.gamepad = gamepad; + } + +}); + +module.exports = GamepadDisconnectedEvent; diff --git a/v3/src/input/gamepad/events/index.js b/v3/src/input/gamepad/events/index.js index 75c3a4ca2..0402d3cf2 100644 --- a/v3/src/input/gamepad/events/index.js +++ b/v3/src/input/gamepad/events/index.js @@ -1,6 +1,8 @@ // Phaser.Input.Gamepad.Events module.exports = { + CONNECTED: require('./GamepadConnectedEvent'), + DISCONNECTED: require('./GamepadDisconnectedEvent'), DOWN: require('./GamepadDownEvent'), UP: require('./GamepadUpEvent') }; diff --git a/v3/src/input/gamepad/index.js b/v3/src/input/gamepad/index.js index b0b5087e1..edfd0e57b 100644 --- a/v3/src/input/gamepad/index.js +++ b/v3/src/input/gamepad/index.js @@ -2,6 +2,7 @@ module.exports = { + Axis: require('./Axis'), Button: require('./Button'), Gamepad: require('./Gamepad'), GamepadManager: require('./GamepadManager'),