Added in Gamepad axis support

Also removed issue stopping analogue buttons working properly. Added in config for DualShock 4 controller.
This commit is contained in:
Richard Davey 2017-09-11 01:28:09 +01:00
parent ee96c4d352
commit 5369ecbffe
10 changed files with 192 additions and 8 deletions

View file

@ -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;

View file

@ -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));
}
}

View file

@ -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);
}
}
});

View file

@ -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;
}
}
});

View file

@ -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
};

View file

@ -2,6 +2,7 @@
module.exports = {
DUALSHOCK_4: require('./Sony_PlayStation_DualShock_4'),
SNES_USB: require('./SNES_USB_Controller')
};

View file

@ -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;

View file

@ -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;

View file

@ -1,6 +1,8 @@
// Phaser.Input.Gamepad.Events
module.exports = {
CONNECTED: require('./GamepadConnectedEvent'),
DISCONNECTED: require('./GamepadDisconnectedEvent'),
DOWN: require('./GamepadDownEvent'),
UP: require('./GamepadUpEvent')
};

View file

@ -2,6 +2,7 @@
module.exports = {
Axis: require('./Axis'),
Button: require('./Button'),
Gamepad: require('./Gamepad'),
GamepadManager: require('./GamepadManager'),