Skip to content

Commit 5369ecb

Browse files
committed
Added in Gamepad axis support
Also removed issue stopping analogue buttons working properly. Added in config for DualShock 4 controller.
1 parent ee96c4d commit 5369ecb

File tree

10 files changed

+192
-8
lines changed

10 files changed

+192
-8
lines changed

v3/src/input/gamepad/Axis.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Phaser.Input.Gamepad.Axis
2+
3+
var Class = require('../../utils/Class');
4+
var GamepadEvent = require('./events/');
5+
6+
var Axis = new Class({
7+
8+
initialize:
9+
10+
function Axis (pad, index)
11+
{
12+
this.pad = pad;
13+
14+
this.events = pad.events;
15+
16+
this.index = index;
17+
18+
// Between -1 and 1 with 0 being dead center
19+
this.value = 0;
20+
21+
this.threshold = 0.05;
22+
},
23+
24+
update: function (value)
25+
{
26+
this.value = value;
27+
},
28+
29+
// Applies threshold to the value and returns it
30+
getValue: function ()
31+
{
32+
var percentage = (Math.abs(this.value) - this.threshold) / (1 - this.threshold);
33+
34+
if (percentage < 0)
35+
{
36+
percentage = 0;
37+
}
38+
39+
return percentage * (this.value > 0 ? 1 : -1);
40+
}
41+
42+
});
43+
44+
module.exports = Axis;

v3/src/input/gamepad/Button.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,24 @@ var Button = new Class({
1515

1616
this.index = index;
1717

18-
this.pressed = false;
19-
18+
// Between 0 and 1
2019
this.value = 0;
20+
21+
// Can be set for Analogue buttons to enable a 'pressure' threshold before considered as 'pressed'
22+
this.threshold = 0;
23+
24+
this.pressed = false;
2125
},
2226

2327
update: function (data)
2428
{
25-
if (data.pressed)
29+
this.value = data.value;
30+
31+
if (this.value >= this.threshold)
2632
{
2733
if (!this.pressed)
2834
{
2935
this.pressed = true;
30-
this.value = data.value;
3136
this.events.dispatch(new GamepadEvent.DOWN(this.pad, this, this.value, data));
3237
}
3338
}
@@ -36,7 +41,6 @@ var Button = new Class({
3641
if (this.pressed)
3742
{
3843
this.pressed = false;
39-
this.value = data.value;
4044
this.events.dispatch(new GamepadEvent.UP(this.pad, this, this.value, data));
4145
}
4246
}

v3/src/input/gamepad/Gamepad.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Phaser.Input.Gamepad.Gamepad
22

3+
var Axis = require('./Axis');
34
var Button = require('./Button');
45
var Class = require('../../utils/Class');
56
var GamepadEvent = require('./events/');
@@ -23,6 +24,7 @@ var Gamepad = new Class({
2324
this.timestamp = 0;
2425

2526
this.buttons = [];
27+
this.axes = [];
2628
},
2729

2830
update: function (data)
@@ -44,7 +46,18 @@ var Gamepad = new Class({
4446
this.buttons[i].update(buttonData);
4547
}
4648

47-
// TODO: Axes
49+
// Axes
50+
for (var i = 0; i < data.axes.length; i++)
51+
{
52+
var axisData = data.axes[i];
53+
54+
if (this.axes[i] === undefined)
55+
{
56+
this.axes[i] = new Axis(this, i);
57+
}
58+
59+
this.axes[i].update(axisData);
60+
}
4861
}
4962

5063
});

v3/src/input/gamepad/GamepadManager.js

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ var GamepadEvent = require('./events/');
77
// https://developer.mozilla.org/en-US/docs/Web/API/Gamepad_API
88
// https://developer.mozilla.org/en-US/docs/Web/API/Gamepad_API/Using_the_Gamepad_API
99
// https://www.smashingmagazine.com/2015/11/gamepad-api-in-web-games/
10+
// http://html5gamepad.com/
1011

1112
var GamepadManager = new Class({
1213

@@ -129,6 +130,32 @@ var GamepadManager = new Class({
129130
}
130131
},
131132

133+
getAll: function ()
134+
{
135+
var out = [];
136+
137+
for (var i = 0; i < this.gamepads.length; i++)
138+
{
139+
if (this.gamepads[i])
140+
{
141+
out.push(this.gamepads[i]);
142+
}
143+
}
144+
145+
return out;
146+
},
147+
148+
getPad: function (index)
149+
{
150+
for (var i = 0; i < this.gamepads.length; i++)
151+
{
152+
if (this.gamepads[i].index === index)
153+
{
154+
return this.gamepads[i];
155+
}
156+
}
157+
},
158+
132159
update: function ()
133160
{
134161
if (!this.enabled)
@@ -151,18 +178,36 @@ var GamepadManager = new Class({
151178
for (var i = 0; i < len; i++)
152179
{
153180
var event = queue[i];
181+
var pad;
154182

155183
switch (event.type)
156184
{
157185
case 'gamepadconnected':
158-
// Event?
186+
187+
pad = this.getPad(event.gamepad.index);
188+
189+
this.events.dispatch(new GamepadEvent.CONNECTED(pad, event));
190+
159191
break;
160192

161193
case 'gamepaddisconnected':
162-
// Event?
194+
195+
pad = this.getPad(event.gamepad.index);
196+
197+
this.events.dispatch(new GamepadEvent.DISCONNECTED(pad, event));
198+
163199
break;
164200
}
165201
}
202+
},
203+
204+
total: {
205+
206+
get: function ()
207+
{
208+
return this.gamepads.length;
209+
}
210+
166211
}
167212

168213
});
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Sony PlayStation DualShock 4 (v2) wireless controller
2+
3+
module.exports = {
4+
5+
UP: 12,
6+
DOWN: 13,
7+
LEFT: 14,
8+
RIGHT: 15,
9+
10+
SHARE: 8,
11+
OPTIONS: 9,
12+
PS: 16,
13+
TOUCHBAR: 17,
14+
15+
X: 0,
16+
CIRCLE: 1,
17+
SQUARE: 2,
18+
TRIANGLE: 3,
19+
20+
L1: 4,
21+
R1: 5,
22+
L2: 6,
23+
R2: 7,
24+
L3: 10,
25+
R3: 11,
26+
27+
LEFT_STICK_H: 0,
28+
LEFT_STICK_V: 1,
29+
RIGHT_STICK_H: 2,
30+
RIGHT_STICK_V: 3
31+
32+
};

v3/src/input/gamepad/configs/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
module.exports = {
44

5+
DUALSHOCK_4: require('./Sony_PlayStation_DualShock_4'),
56
SNES_USB: require('./SNES_USB_Controller')
67

78
};
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
var Class = require('../../../utils/Class');
2+
var Event = require('../../../events/Event');
3+
4+
var GamepadConnectedEvent = new Class({
5+
6+
Extends: Event,
7+
8+
initialize:
9+
10+
function GamepadConnectedEvent (gamepad, nativeEvent)
11+
{
12+
Event.call(this, 'GAMEPAD_CONNECTED_EVENT');
13+
14+
this.data = nativeEvent;
15+
16+
this.gamepad = gamepad;
17+
}
18+
19+
});
20+
21+
module.exports = GamepadConnectedEvent;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
var Class = require('../../../utils/Class');
2+
var Event = require('../../../events/Event');
3+
4+
var GamepadDisconnectedEvent = new Class({
5+
6+
Extends: Event,
7+
8+
initialize:
9+
10+
function GamepadDisconnectedEvent (gamepad, nativeEvent)
11+
{
12+
Event.call(this, 'GAMEPAD_DISCONNECTED_EVENT');
13+
14+
this.data = nativeEvent;
15+
16+
this.gamepad = gamepad;
17+
}
18+
19+
});
20+
21+
module.exports = GamepadDisconnectedEvent;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Phaser.Input.Gamepad.Events
22

33
module.exports = {
4+
CONNECTED: require('./GamepadConnectedEvent'),
5+
DISCONNECTED: require('./GamepadDisconnectedEvent'),
46
DOWN: require('./GamepadDownEvent'),
57
UP: require('./GamepadUpEvent')
68
};

v3/src/input/gamepad/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
module.exports = {
44

5+
Axis: require('./Axis'),
56
Button: require('./Button'),
67
Gamepad: require('./Gamepad'),
78
GamepadManager: require('./GamepadManager'),

0 commit comments

Comments
 (0)