-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgamepad.js
159 lines (152 loc) · 5.58 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
class GamepadHandler {
gamepads;
timeout;
listeners;
constructor() {
this.buttonLabels = {
0: 'BUTTON_1',
1: 'BUTTON_2',
2: 'BUTTON_3',
3: 'BUTTON_4',
4: 'LEFT_TOP_SHOULDER',
5: 'RIGHT_TOP_SHOULDER',
6: 'LEFT_BOTTOM_SHOULDER',
7: 'RIGHT_BOTTOM_SHOULDER',
8: 'SELECT',
9: 'START',
10: 'LEFT_STICK',
11: 'RIGHT_STICK',
12: 'DPAD_UP',
13: 'DPAD_DOWN',
14: 'DPAD_LEFT',
15: 'DPAD_RIGHT',
};
this.gamepads = [];
this.listeners = {};
this.timeout = null;
this.loop();
}
terminate() {
window.clearTimeout(this.timeout);
}
getGamepads() {
return navigator.getGamepads ? navigator.getGamepads() : (navigator.webkitGetGamepads ? navigator.webkitGetGamepads() : []);
}
loop() {
this.updateGamepadState();
this.timeout = setTimeout(this.loop.bind(this), 10);
}
updateGamepadState() {
let gamepads = Array.from(this.getGamepads());
if (!gamepads) return;
if (!Array.isArray(gamepads) && gamepads.length) {
let gp = [];
for (let i=0; i<gamepads.length; i++) {
gp.push(gamepads[i]);
}
gamepads = gp;
} else if (!Array.isArray(gamepads)) return;
gamepads.forEach((gamepad, index) => {
if (!gamepad) return;
let hasGamepad = false;
this.gamepads.forEach((oldGamepad, oldIndex) => {
if (oldGamepad.index !== gamepad.index) return;
const gamepadToSave = {
axes: [],
buttons: {},
index: oldGamepad.index,
id: oldGamepad.id
}
hasGamepad = true;
oldGamepad.axes.forEach((axis, axisIndex) => {
const val = (axis < 0.01 && axis > -0.01) ? 0 : axis;
const newVal = (gamepad.axes[axisIndex] < 0.01 && gamepad.axes[axisIndex] > -0.01) ? 0 : gamepad.axes[axisIndex];
if (newVal !== val) {
const axis = ['LEFT_STICK_X', 'LEFT_STICK_Y', 'RIGHT_STICK_X', 'RIGHT_STICK_Y'][axisIndex];
if (!axis) return;
this.dispatchEvent('axischanged', {
axis: axis,
value: newVal,
index: gamepad.index,
label: this.getAxisLabel(axis, newVal),
gamepadIndex: gamepad.index,
});
}
gamepadToSave.axes[axisIndex] = newVal;
})
gamepad.buttons.forEach((button, buttonIndex) => {
let pressed = oldGamepad.buttons[buttonIndex] === 1.0;
if (typeof oldGamepad.buttons[buttonIndex] === "object") {
pressed = oldGamepad.buttons[buttonIndex].pressed;
}
let pressed2 = button === 1.0;
if (typeof button === "object") {
pressed2 = button.pressed;
}
gamepadToSave.buttons[buttonIndex] = {pressed:pressed2};
if (pressed !== pressed2) {
if (pressed2) {
this.dispatchEvent('buttondown', {index: buttonIndex, label: this.getButtonLabel(buttonIndex), gamepadIndex: gamepad.index});
} else {
this.dispatchEvent('buttonup', {index: buttonIndex, label:this.getButtonLabel(buttonIndex), gamepadIndex: gamepad.index});
}
}
})
this.gamepads[oldIndex] = gamepadToSave;
})
if (!hasGamepad) {
this.gamepads.push(gamepads[index]);
this.dispatchEvent('connected', {gamepadIndex: gamepad.index});
}
});
for (let j=0; j<this.gamepads.length; j++) {
if (!this.gamepads[j]) continue;
let has = false;
for (let i=0; i<gamepads.length; i++) {
if (!gamepads[i]) continue;
if (this.gamepads[j].index === gamepads[i].index) {
has = true;
break;
}
}
if (!has) {
this.dispatchEvent('disconnected', {gamepadIndex: this.gamepads[j].index});
this.gamepads.splice(j, 1);
j--;
}
}
}
dispatchEvent(name, arg) {
if (typeof this.listeners[name] !== 'function') return;
if (!arg) arg={};
arg.type = name;
this.listeners[name](arg);
}
on(name, cb) {
this.listeners[name.toLowerCase()] = cb;
}
getButtonLabel(index) {
if (index === null || index === undefined) {
return null;
}
if (this.buttonLabels[index] === undefined) {
return `GAMEPAD_${index}`;
}
return this.buttonLabels[index];
}
getAxisLabel(axis, value) {
let valueLabel = null;
if (value > 0.5 || value < -0.5) {
if (value > 0) {
valueLabel = '+1';
} else {
valueLabel = '-1';
}
}
if (!axis || !valueLabel) {
return null;
}
return `${axis}:${valueLabel}`;
}
}
window.GamepadHandler = GamepadHandler;