-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathgamepad_js.go
93 lines (80 loc) · 1.82 KB
/
gamepad_js.go
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
package gamepad
import (
"gioui.org/app"
"gioui.org/io/event"
"gioui.org/io/system"
"syscall/js"
)
// mappingButton corresponds to https://w3c.github.io/gamepad/#dom-gamepad-mapping:
var mappingButton = [...]int{
buttonA,
buttonB,
buttonX,
buttonY,
buttonLB,
buttonRB,
buttonLT,
buttonRT,
buttonBack,
buttonStart,
buttonLeftThumb,
buttonRightThumb,
buttonUp,
buttonDown,
buttonLeft,
buttonRight,
}
type gamepad struct{}
func newGamepad(_ *app.Window) *gamepad {
return &gamepad{}
}
func (g *Gamepad) listenEvents(evt event.Event) {
switch evt.(type) {
case system.FrameEvent:
g.getState()
}
}
var (
_Navigator = js.Global().Get("navigator")
)
func (g *Gamepad) getState() {
gamepads := _Navigator.Get("getGamepads")
if !gamepads.Truthy() {
return
}
gamepads = _Navigator.Call("getGamepads")
for player, controller := range g.Controllers {
controller.updateState(gamepads.Index(player))
}
}
func (controller *Controller) updateState(state js.Value) {
if !state.Truthy() {
controller.Connected = false
controller.Changed = false
return
}
packet := state.Get("timestamp").Float()
if packet == controller.packet {
controller.Changed = false
return
}
controller.packet = packet
controller.Connected = true
controller.Changed = true
// Buttons
buttons := state.Get("buttons")
for index, button := range mappingButton {
btn := buttons.Index(index)
force := 0.0
if btn.Truthy() {
force = btn.Get("value").Float()
}
controller.Buttons.setButtonForce(button, float32(force))
}
// Joysticks
axes := state.Get("axes")
controller.Joysticks.LeftThumb.X = float32(axes.Index(0).Float())
controller.Joysticks.LeftThumb.Y = float32(axes.Index(1).Float())
controller.Joysticks.RightThumb.X = float32(axes.Index(2).Float())
controller.Joysticks.RightThumb.Y = float32(axes.Index(3).Float())
}