/
GamePadPlus.js
56 lines (55 loc) · 1.94 KB
/
GamePadPlus.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
const Gamepad = require("gamepad");
class GamePadPlus extends require('events') {
constructor(deadZone = 0.2) {
super();
this.deadZone = deadZone;
}
init() {
Gamepad.init();
Gamepad.on("attach", (id, state) => this._onAttach(id, state));
Gamepad.on("remove", (id) => this._onRemove(id));
Gamepad.on("down", (id, buttonId, timestamp) => this._onDown(id, buttonId, timestamp));
Gamepad.on("up", (id, buttonId, timestamp) => this._onUp(id, buttonId, timestamp));
Gamepad.on("move", (id, axis, value, lastValue, timestamp) => this._onMove(id, axis, value, lastValue, timestamp));
}
shutdown() {
Gamepad.shutdown();
}
numDevices() {
return (Gamepad.numDevices());
}
deviceAtIndex(deviceIndex) {
return (Gamepad.deviceAtIndex(deviceIndex));
}
get detectDevices() {
return (Gamepad.detectDevices);
}
get processEvents() {
return (Gamepad.processEvents);
}
_onAttach(id, state) {
this.emit("attach", id, state);
}
_onRemove(id) {
this.emit("remove", id);
}
_onDown(id, buttonId, timestamp) {
this.emit("down", id, buttonId, timestamp);
}
_onUp(id, buttonId, timestamp) {
this.emit("up", id, buttonId, timestamp);
}
_onMove(id, axis, value, lastValue, timestamp) {
this.emit("move", id, axis, value, lastValue, timestamp);
if (value > this.deadZone && lastValue < this.deadZone) {
this.emit("axisDown", id, axis, 1, value, lastValue, timestamp);
} else if (value < -this.deadZone && lastValue > -this.deadZone) {
this.emit("axisDown", id, axis, 0, value, lastValue, timestamp);
} else if ((value > -this.deadZone && value < this.deadZone) && lastValue < -this.deadZone){
this.emit("axisUp", id, axis, 0, value, lastValue, timestamp);
} else if ((value > -this.deadZone && value < this.deadZone) && lastValue > this.deadZone){
this.emit("axisUp", id, axis, 1, value, lastValue, timestamp);
}
}
}
module.exports = new GamePadPlus();