-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxPad.js
111 lines (98 loc) · 3.11 KB
/
xPad.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
class XPad {
/** @type {XPad} */
constructor(buttonsConfig, joysticksConfig) {
this.emitters = {}
this.buttons = {}
this.buttonsConfig = buttonsConfig
this.joysticks = {left: {x: 0, y: 0}, right: {x: 0, y: 0}}
this.joysticksConfig = joysticksConfig
this.updateInterval = 10
this.on("update", () => {
this.buttonsConfig.forEach((name, index) => {
const value = XPad.getButtonValue(this.gamepad.buttons[index])
if(this.buttons[name] !== value){
this.buttons[name] = value
this.emit("buttonUpdate", name, value, index)
if(value === 1) this.emit("buttonPressed", name, index)
else if(value === 0) this.emit("buttonReleased", name, index)
}
})
for(const side in this.joysticksConfig){
for(const orientation in this.joysticksConfig[side]){
const index = this.joysticksConfig[side][orientation]
const value = this.gamepad.axes[index]
if(this.joysticks[side][orientation] !== value){
this.joysticks[side][orientation] = value
this.emit("joysticksUpdate", this.joysticks, index)
this.emit(
side + "joystickUpdate",
this.joysticks[side],
index
)
}
}
}
})
window.addEventListener("gamepadconnected", (event) => {
if(event.gamepad.id === "xinput") {
this.buttonsConfig.forEach((name, i) => {
this.buttons[name] = XPad.getButtonValue(event.gamepad.buttons[i])
})
for(const side in this.joysticksConfig){
for(const orientation in this.joysticksConfig[side]) {
const index = this.joysticksConfig[side][orientation]
this.joysticks[side][orientation] = event.gamepad.axes[index]
}
}
this.update = setInterval(() => this.emit("update"), this.updateInterval)
this.gamepad = event.gamepad
this.emit("connected")
}
});
window.addEventListener("gamepaddisconnected", (event) => {
if(event.gamepad === this.gamepad) {
clearInterval(this.update)
this.gamepad = null
this.emit("disconnected")
}
});
}
get connected() {
return !!this.gamepad
}
on(eventName, callback){
if(this.emitters.hasOwnProperty(eventName))
this.emitters[eventName].push(callback)
else this.emitters[eventName] = [callback]
return this
}
once(eventName, callback){
callback.onlyOneTime = true
this.on(eventName, callback)
return this
}
emit(eventName, ...args){
if(this.emitters.hasOwnProperty(eventName))
this.emitters[eventName] = this.emitters[eventName]
.filter(callback => {
callback(...args)
return !callback.onlyOneTime
})
return this
}
static getButtonValue(b) {
return (typeof b == 'number') ? b : b.value
}
}
// Xbox Gamepad
/** @type {XPad} */
const xPad = new XPad([
"A","B","X","Y",
"LB","RB","LT","RT",
"BACK","START",
"L-STICK","R-STICK",
"UP","DOWN","LEFT","RIGHT"
],{
left: {x: 0, y: 1},
right: {x: 2, y: 3}
})