-
Notifications
You must be signed in to change notification settings - Fork 1
/
gamepadmapping.js
230 lines (209 loc) · 7.77 KB
/
gamepadmapping.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
class GamePadMapping {
constructor(gamepad,buttons,axes,period,deadzone) {
this.gamepad = gamepad
this.index = gamepad.index
this.buttons = []
this.axes = []
this.ready = false
this.period = period
this.deadzone = deadzone
let f = () => {console.warn('Unhandled gamepad event!')}
for(let button of buttons) {
this['on'+button.name]=f
this['on'+button.name+'up']=f
this.buttons.push({name:button.name,symbol:button.symbol,description:button.description,array:null,duration:0})
}
for(let axe of axes) {
this['on'+axe.name]=f
this.axes.push({name:axe.name,symbols:axe.symbols,axe:axe.description,arrays:[null,null,null,null]})
}
this.onmapnext=f
this.onwarn=f
this.onready=f
if(typeof InstallTrigger !== 'undefined') this.iid = setInterval(() => {this.interval()}, period)
else {
// Chrome, opera non updating gamepad fix
let gamepad = {id:this.gamepad.id,index:this.gamepad.index,axes:this.gamepad.axes,buttons:[]}
for(let button of this.gamepad.buttons) gamepad.buttons.push({value:button.value})
this.gamepad = gamepad
this.iid = setInterval(() => {
let gamepad = navigator.getGamepads()[this.index]
this.gamepad.axes = gamepad.axes
for(let i = 0;i<gamepad.buttons.length;i++) this.gamepad.buttons[i].value = gamepad.buttons[i].value
this.interval()
}, period)
}
this.wipecache()
}
disconnect() { clearInterval(this.iid) }
on(name,callback) { this['on'+name]=callback }
up(name,callback) { this['on'+name+'up']=callback }
// Trigger button does not change state in very fast interactions wheras axes do.
// For a better performance store which callbacks fired and if timestamps match fire again
interval() {
if(!this.ready) return this.map()
for(let button of this.buttons) {
let value = this.valueof(button.array)
if(value > this.deadzone) {
button.duration++
this['on'+button.name](value,button.duration*this.period)
} else if(button.duration != 0) {
button.duration = 0
this['on'+button.name+'up']()
}
}
for(let axe of this.axes) {
let r = []
for(let i = 0;i<4;i+=2) {
let a = this.valueof(axe.arrays[i])
if(a > this.deadzone) r.push(a)
else {
let b = this.valueof(axe.arrays[i+1])
if(b > this.deadzone) r.push(-b)
else r.push(0)
}
}
if(r[0]!=0||r[1]!=0) this['on'+axe.name](r)
}
}
map() {
for(let button of this.buttons) {
if(button.array == null) {
this.onmapnext(button.symbol)
let matching = this.matching()
if(matching) {
this.wipecache()
if(!this.isalreadymapped(matching)) button.array = matching
else this.onwarn('Dublicate entry')
}
return
}
}
for(let axe of this.axes) {
for(let i = 0;i<4;i++) {
if(axe.arrays[i] == null) {
this.onmapnext(axe.symbols[(i-i%2)/2].symbol+' '+axe.symbols[(i-i%2)/2].directions[i%2])
let matching = this.matching()
if(matching) {
this.wipecache()
if(!this.isalreadymapped(matching)) axe.arrays[i] = matching
else this.onwarn('Dublicate entry')
}
return
}
}
}
this.finished()
}
finished() {
this.ready = true
this.onready()
}
matching() {
for(let i = 0;i<this.gamepad.buttons.length;i++) {
if(!this.cbuttons[i]) this.cbuttons[i] = 0
if(this.gamepad.buttons[i].value<1&&this.cbuttons[i]==0) this.cbuttons[i]=1
if(this.gamepad.buttons[i].value<this.deadzone&&this.cbuttons[i]<2) this.cbuttons[i]=2
if(this.gamepad.buttons[i].value>1-this.deadzone&&this.cbuttons[i]==2) this.cbuttons[i]=3
else if(this.gamepad.buttons[i].value<this.deadzone&&this.cbuttons[i]==3) {
this.cbuttons[i]++
this.matchingb.push(this.gamepad.buttons[i])
}
}
for(let i = 0;i<this.gamepad.axes.length;i++) {
if(!this.caxes[i]&&this.gamepad.axes[i]!=-1) this.caxes[i] = 0
if(this.gamepad.axes[i]>1-this.deadzone&&this.caxes[i]==0) this.caxes[i]++
else if(this.gamepad.axes[i]<this.deadzone&&this.caxes[i]==1) {
this.caxes[i]++
this.matchinga.push(i)
this.matchinga.push(1)
}
else if(this.gamepad.axes[i]<-1+this.deadzone&&this.caxes[i]==0) this.caxes[i]--
else if(this.gamepad.axes[i]>-this.deadzone&&this.caxes[i]==-1) {
this.caxes[i]--
this.matchinga.push(i)
this.matchinga.push(-1)
}
}
if(this.matchingb.length>1||this.matchinga.length>2) this.wipecache()
else if(this.matchingb.length==1&&this.matchinga.length==2) return this.matchinga.concat(this.matchingb) //trigger
else if(this.matchingb.length==1) {
let i = Math.max(this.caxes.indexOf(1),this.caxes.indexOf(-1))
if(i==-1) return this.matchingb
//else wait for next trigger
} // button
else if(this.matchinga.length==2) {
let i = Math.max(this.cbuttons.indexOf(1),this.cbuttons.indexOf(3))
if(i==-1) return this.matchinga
//else wait for button finish
} // axis or dpad
return false
}
wipecache() {
this.cbuttons = []
this.caxes = []
this.matchingb = []
this.matchinga = []
}
isalreadymapped(o) {
for(let button of this.buttons) if(this.comparearray(button.array, o)) return true
for(let axe of this.axes) {
for(let array of axe.arrays) if(this.comparearray(array,o)) return true
}
}
comparearray(a,b) {
if(a==null||b==null) return false
if(a.length>1&&b.length>1) return a[0]==b[0]&&a[1]==b[1]
return a[0] == b[0]
}
valueof(o) {
if(o==null) return null
if(o.length==1) return o[0].value
if(o.length==2) return this.gamepad.axes[o[0]]*o[1]
return (this.gamepad.axes[o[0]]*o[1]+1)/2
}
export() {
let r = {}
r.id = this.gamepad.id
r.buttons = []
for(let button of this.buttons) {
button = button.array
if(button==null) r.buttons.push(null)
else if(button.length==1) r.buttons.push([this.gamepad.buttons.indexOf(button[0])])
else if(button.length==2) r.buttons.push(button)
else r.buttons.push([button[0],button[1],this.gamepad.buttons.indexOf(button[2])])
}
r.axes = []
for(let axe of this.axes) {
let axy = []
for(let i = 0;i<4;i++) {
let array = axe.arrays[i]
if(array==null) axy.push(null)
else if(array.length==1) axy.push([this.gamepad.array.indexOf(array[0])])
else if(array.length==2) axy.push(array)
else axy.push([array[0],array[1],this.gamepad.array.indexOf(array[2])])
}
r.axes.push(axy)
}
return r
}
import(o) {
if(o.id==this.gamepad.id) {
for(let i = 0;i<o.buttons.length;i++) {
if(o.buttons[i]==null) this.buttons[i].array = null
else if(o.buttons[i].length==1) this.buttons[i].array = [this.gamepad.buttons[o.buttons[i]]]
else if(o.buttons[i].length==2) this.buttons[i].array = o.buttons[i]
else this.buttons[i].array = [o.buttons[i][0],o.buttons[i][1],this.gamepad.buttons[o.buttons[i][2]]]
}
for(let i = 0;i<o.axes.length;i++) {
for(let j = 0;j<4;j++) {
if(o.axes[i][j]==null) this.axes[i].arrays[j] = null
else if(o.axes[i][j].length==1) this.axes[i].arrays[j] = [this.gamepad.buttons[o.axes[i][j]]]
else if(o.axes[i][j].length==2) this.axes[i].arrays[j] = o.axes[i][j]
else this.axes[i].arrays[j] = [o.axes[i][j][0],o.axes[i][j][1],this.gamepad.buttons[o.axes[i][j][2]]]
}
}
this.finished()
}
}
}