-
Notifications
You must be signed in to change notification settings - Fork 244
/
joystick.go
145 lines (126 loc) · 4.6 KB
/
joystick.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
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
package pixelgl
import (
"github.com/go-gl/glfw/v3.3/glfw"
)
// Joystick is a joystick or controller.
type Joystick int
// List all of the joysticks.
const (
Joystick1 = Joystick(glfw.Joystick1)
Joystick2 = Joystick(glfw.Joystick2)
Joystick3 = Joystick(glfw.Joystick3)
Joystick4 = Joystick(glfw.Joystick4)
Joystick5 = Joystick(glfw.Joystick5)
Joystick6 = Joystick(glfw.Joystick6)
Joystick7 = Joystick(glfw.Joystick7)
Joystick8 = Joystick(glfw.Joystick8)
Joystick9 = Joystick(glfw.Joystick9)
Joystick10 = Joystick(glfw.Joystick10)
Joystick11 = Joystick(glfw.Joystick11)
Joystick12 = Joystick(glfw.Joystick12)
Joystick13 = Joystick(glfw.Joystick13)
Joystick14 = Joystick(glfw.Joystick14)
Joystick15 = Joystick(glfw.Joystick15)
Joystick16 = Joystick(glfw.Joystick16)
JoystickLast = Joystick(glfw.JoystickLast)
)
// JoystickPresent returns if the joystick is currently connected.
//
// This API is experimental.
func (w *Window) JoystickPresent(js Joystick) bool {
return w.currJoy.connected[js]
}
// JoystickName returns the name of the joystick. A disconnected joystick will return an
// empty string.
//
// This API is experimental.
func (w *Window) JoystickName(js Joystick) string {
return w.currJoy.name[js]
}
// JoystickButtonCount returns the number of buttons a connected joystick has.
//
// This API is experimental.
func (w *Window) JoystickButtonCount(js Joystick) int {
return len(w.currJoy.buttons[js])
}
// JoystickAxisCount returns the number of axes a connected joystick has.
//
// This API is experimental.
func (w *Window) JoystickAxisCount(js Joystick) int {
return len(w.currJoy.axis[js])
}
// JoystickPressed returns whether the joystick Button is currently pressed down.
// If the button index is out of range, this will return false.
//
// This API is experimental.
func (w *Window) JoystickPressed(js Joystick, button int) bool {
return w.currJoy.getButton(js, button)
}
// JoystickJustPressed returns whether the joystick Button has just been pressed down.
// If the button index is out of range, this will return false.
//
// This API is experimental.
func (w *Window) JoystickJustPressed(js Joystick, button int) bool {
return w.currJoy.getButton(js, button) && !w.prevJoy.getButton(js, button)
}
// JoystickJustReleased returns whether the joystick Button has just been released up.
// If the button index is out of range, this will return false.
//
// This API is experimental.
func (w *Window) JoystickJustReleased(js Joystick, button int) bool {
return !w.currJoy.getButton(js, button) && w.prevJoy.getButton(js, button)
}
// JoystickAxis returns the value of a joystick axis at the last call to Window.Update.
// If the axis index is out of range, this will return 0.
//
// This API is experimental.
func (w *Window) JoystickAxis(js Joystick, axis int) float64 {
return w.currJoy.getAxis(js, axis)
}
// Used internally during Window.UpdateInput to update the state of the joysticks.
func (w *Window) updateJoystickInput() {
for js := Joystick1; js <= JoystickLast; js++ {
// Determine and store if the joystick was connected
joystickPresent := glfw.Joystick(js).Present()
w.tempJoy.connected[js] = joystickPresent
if joystickPresent {
w.tempJoy.buttons[js] = glfw.Joystick(js).GetButtons()
w.tempJoy.axis[js] = glfw.Joystick(js).GetAxes()
if !w.currJoy.connected[js] {
// The joystick was recently connected, we get the name
w.tempJoy.name[js] = glfw.Joystick(js).GetName()
} else {
// Use the name from the previous one
w.tempJoy.name[js] = w.currJoy.name[js]
}
} else {
w.tempJoy.buttons[js] = []glfw.Action{}
w.tempJoy.axis[js] = []float32{}
w.tempJoy.name[js] = ""
}
}
w.prevJoy = w.currJoy
w.currJoy = w.tempJoy
}
type joystickState struct {
connected [JoystickLast + 1]bool
name [JoystickLast + 1]string
buttons [JoystickLast + 1][]glfw.Action
axis [JoystickLast + 1][]float32
}
// Returns if a button on a joystick is down, returning false if the button or joystick is invalid.
func (js *joystickState) getButton(joystick Joystick, button int) bool {
// Check that the joystick and button is valid, return false by default
if js.buttons[joystick] == nil || button >= len(js.buttons[joystick]) || button < 0 {
return false
}
return js.buttons[joystick][byte(button)] == 1
}
// Returns the value of a joystick axis, returning 0 if the button or joystick is invalid.
func (js *joystickState) getAxis(joystick Joystick, axis int) float64 {
// Check that the joystick and axis is valid, return 0 by default.
if js.axis[joystick] == nil || axis >= len(js.axis[joystick]) || axis < 0 {
return 0
}
return float64(js.axis[joystick][axis])
}