-
Notifications
You must be signed in to change notification settings - Fork 136
/
gamepads_js.go
108 lines (101 loc) · 3.99 KB
/
gamepads_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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
//+build js
package engo
import "errors"
// Gampad is a configuration of a joystick that is able to be mapped to the
// SDL_GameControllerDB.
// For more info See https://www.glfw.org/docs/3.3/input_guide.html#gamepad_mapping
type Gamepad struct {
A, B, X, Y GamepadButton
Back, Start, Guide GamepadButton
DpadUp, DpadRight, DpadDown, DpadLeft GamepadButton
LeftBumper, RightBumper GamepadButton
LeftThumb, RightThumb GamepadButton
LeftX, LeftY AxisGamepad
RightX, RightY AxisGamepad
LeftTrigger, RightTrigger AxisGamepad
id string
connected bool
}
var usedGpds []string
func (gm *GamepadManager) registerGamepadImpl(name string) error {
gpds := window.Get("navigator").Call("getGamepads")
found := false
gm.mutex.Lock()
defer gm.mutex.Unlock()
for i := 0; i < gpds.Length(); i++ {
if gpds.Index(i).IsNull() {
continue
}
if gpds.Index(i).Get("mapping").String() != "standard" {
continue
}
gpid := gpds.Index(i).Get("id").String()
gm.gamepads[name] = &Gamepad{
id: gpid,
connected: true,
}
found = true
}
if !found {
warning("Unable to locate any usable gamepads.")
gm.gamepads[name] = &Gamepad{}
return errors.New("unable to locate any usable gamepads \ngamepad will be added when a new one is plugged in")
}
return nil
}
func (gm *GamepadManager) updateImpl() {
if window.IsUndefined() || window.Get("navigator").IsUndefined() {
return // node for testing
}
gpds := window.Get("navigator").Call("getGamepads")
gm.mutex.Lock()
defer gm.mutex.Unlock()
for name, gamepad := range gm.gamepads {
if !gamepad.connected {
warning("Gamepad " + name + " was not available for update!")
continue
}
for i := 0; i < gpds.Length(); i++ {
if gpds.Index(i).IsNull() {
continue
}
gpid := gpds.Index(i).Get("id").String()
if gpid == gamepad.id {
if gpds.Index(i).Get("connected").Bool() {
gamepad.A.set(gpds.Index(i).Get("buttons").Index(0).Get("pressed").Bool())
gamepad.B.set(gpds.Index(i).Get("buttons").Index(1).Get("pressed").Bool())
gamepad.X.set(gpds.Index(i).Get("buttons").Index(2).Get("pressed").Bool())
gamepad.Y.set(gpds.Index(i).Get("buttons").Index(3).Get("pressed").Bool())
gamepad.LeftBumper.set(gpds.Index(i).Get("buttons").Index(4).Get("pressed").Bool())
gamepad.RightBumper.set(gpds.Index(i).Get("buttons").Index(5).Get("pressed").Bool())
if gpds.Index(i).Get("buttons").Index(6).Get("pressed").Bool() {
gamepad.LeftTrigger.set(1.0)
} else {
gamepad.LeftTrigger.set(0.0)
}
if gpds.Index(i).Get("buttons").Index(7).Get("pressed").Bool() {
gamepad.RightTrigger.set(1.0)
} else {
gamepad.RightTrigger.set(0.0)
}
gamepad.Back.set(gpds.Index(i).Get("buttons").Index(8).Get("pressed").Bool())
gamepad.Start.set(gpds.Index(i).Get("buttons").Index(9).Get("pressed").Bool())
gamepad.LeftThumb.set(gpds.Index(i).Get("buttons").Index(10).Get("pressed").Bool())
gamepad.RightThumb.set(gpds.Index(i).Get("buttons").Index(11).Get("pressed").Bool())
gamepad.DpadUp.set(gpds.Index(i).Get("buttons").Index(12).Get("pressed").Bool())
gamepad.DpadDown.set(gpds.Index(i).Get("buttons").Index(13).Get("pressed").Bool())
gamepad.DpadLeft.set(gpds.Index(i).Get("buttons").Index(14).Get("pressed").Bool())
gamepad.DpadRight.set(gpds.Index(i).Get("buttons").Index(15).Get("pressed").Bool())
gamepad.Guide.set(gpds.Index(i).Get("buttons").Index(16).Get("pressed").Bool())
gamepad.LeftX.set(float32(gpds.Index(i).Get("axes").Index(0).Float()))
gamepad.LeftY.set(float32(gpds.Index(i).Get("axes").Index(1).Float()))
gamepad.RightX.set(float32(gpds.Index(i).Get("axes").Index(2).Float()))
gamepad.RightY.set(float32(gpds.Index(i).Get("axes").Index(3).Float()))
} else {
gamepad.connected = false
warning("Gamepad " + name + " was not available to update!")
}
}
}
}
}