Newer
Older
"github.com/faiface/mainthread"
"github.com/go-gl/glfw/v3.2/glfw"
)
// Pressed returns whether button is currently pressed down.
func (w *Window) Pressed(button Button) bool {
return w.currInp.buttons[button]
}
// JustPressed returns whether button has just been pressed down.
func (w *Window) JustPressed(button Button) bool {
return w.currInp.buttons[button] && !w.prevInp.buttons[button]
}
// JustReleased returns whether button has just been released up.
func (w *Window) JustReleased(button Button) bool {
return !w.currInp.buttons[button] && w.prevInp.buttons[button]
}
// MousePosition returns the current mouse position relative to the window.
func (w *Window) MousePosition() Vec {
var x, y, width, height float64
mainthread.Call(func() {
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
x, y = w.window.GetCursorPos()
wi, hi := w.window.GetSize()
width, height = float64(wi), float64(hi)
})
// transform to OpenGL coordinates
x = (x - width/2) / (width / 2)
y = (height/2 - y) / (height / 2)
return V(x, y)
}
// MouseScroll returns the scroll amount (in both axis) since the last call to Window.Update.
func (w *Window) MouseScroll() Vec {
return w.currInp.scroll
}
// Button is a keyboard or mouse button. Why distinguish?
type Button int
// List of all mouse buttons.
const (
MouseButton1 = Button(glfw.MouseButton1)
MouseButton2 = Button(glfw.MouseButton2)
MouseButton3 = Button(glfw.MouseButton3)
MouseButton4 = Button(glfw.MouseButton4)
MouseButton5 = Button(glfw.MouseButton5)
MouseButton6 = Button(glfw.MouseButton6)
MouseButton7 = Button(glfw.MouseButton7)
MouseButton8 = Button(glfw.MouseButton8)
MouseButtonLast = Button(glfw.MouseButtonLast)
MouseButtonLeft = Button(glfw.MouseButtonLeft)
MouseButtonRight = Button(glfw.MouseButtonRight)
MouseButtonMiddle = Button(glfw.MouseButtonMiddle)
)
// List of all keyboard buttons.
const (
KeyUnknown = Button(glfw.KeyUnknown)
KeySpace = Button(glfw.KeySpace)
KeyApostrophe = Button(glfw.KeyApostrophe)
KeyComma = Button(glfw.KeyComma)
KeyMinus = Button(glfw.KeyMinus)
KeyPeriod = Button(glfw.KeyPeriod)
KeySlash = Button(glfw.KeySlash)
Key0 = Button(glfw.Key0)
Key1 = Button(glfw.Key1)
Key2 = Button(glfw.Key2)
Key3 = Button(glfw.Key3)
Key4 = Button(glfw.Key4)
Key5 = Button(glfw.Key5)
Key6 = Button(glfw.Key6)
Key7 = Button(glfw.Key7)
Key8 = Button(glfw.Key8)
Key9 = Button(glfw.Key9)
KeySemicolon = Button(glfw.KeySemicolon)
KeyEqual = Button(glfw.KeyEqual)
KeyA = Button(glfw.KeyA)
KeyB = Button(glfw.KeyB)
KeyC = Button(glfw.KeyC)
KeyD = Button(glfw.KeyD)
KeyE = Button(glfw.KeyE)
KeyF = Button(glfw.KeyF)
KeyG = Button(glfw.KeyG)
KeyH = Button(glfw.KeyH)
KeyI = Button(glfw.KeyI)
KeyJ = Button(glfw.KeyJ)
KeyK = Button(glfw.KeyK)
KeyL = Button(glfw.KeyL)
KeyM = Button(glfw.KeyM)
KeyN = Button(glfw.KeyN)
KeyO = Button(glfw.KeyO)
KeyP = Button(glfw.KeyP)
KeyQ = Button(glfw.KeyQ)
KeyR = Button(glfw.KeyR)
KeyS = Button(glfw.KeyS)
KeyT = Button(glfw.KeyT)
KeyU = Button(glfw.KeyU)
KeyV = Button(glfw.KeyV)
KeyW = Button(glfw.KeyW)
KeyX = Button(glfw.KeyX)
KeyY = Button(glfw.KeyY)
KeyZ = Button(glfw.KeyZ)
KeyLeftBracket = Button(glfw.KeyLeftBracket)
KeyBackslash = Button(glfw.KeyBackslash)
KeyRightBracket = Button(glfw.KeyRightBracket)
KeyGraveAccent = Button(glfw.KeyGraveAccent)
KeyWorld1 = Button(glfw.KeyWorld1)
KeyWorld2 = Button(glfw.KeyWorld2)
KeyEscape = Button(glfw.KeyEscape)
KeyEnter = Button(glfw.KeyEnter)
KeyTab = Button(glfw.KeyTab)
KeyBackspace = Button(glfw.KeyBackspace)
KeyInsert = Button(glfw.KeyInsert)
KeyDelete = Button(glfw.KeyDelete)
KeyRight = Button(glfw.KeyRight)
KeyLeft = Button(glfw.KeyLeft)
KeyDown = Button(glfw.KeyDown)
KeyUp = Button(glfw.KeyUp)
KeyPageUp = Button(glfw.KeyPageUp)
KeyPageDown = Button(glfw.KeyPageDown)
KeyHome = Button(glfw.KeyHome)
KeyEnd = Button(glfw.KeyEnd)
KeyCapsLock = Button(glfw.KeyCapsLock)
KeyScrollLock = Button(glfw.KeyScrollLock)
KeyNumLock = Button(glfw.KeyNumLock)
KeyPrintScreen = Button(glfw.KeyPrintScreen)
KeyPause = Button(glfw.KeyPause)
KeyF1 = Button(glfw.KeyF1)
KeyF2 = Button(glfw.KeyF2)
KeyF3 = Button(glfw.KeyF3)
KeyF4 = Button(glfw.KeyF4)
KeyF5 = Button(glfw.KeyF5)
KeyF6 = Button(glfw.KeyF6)
KeyF7 = Button(glfw.KeyF7)
KeyF8 = Button(glfw.KeyF8)
KeyF9 = Button(glfw.KeyF9)
KeyF10 = Button(glfw.KeyF10)
KeyF11 = Button(glfw.KeyF11)
KeyF12 = Button(glfw.KeyF12)
KeyF13 = Button(glfw.KeyF13)
KeyF14 = Button(glfw.KeyF14)
KeyF15 = Button(glfw.KeyF15)
KeyF16 = Button(glfw.KeyF16)
KeyF17 = Button(glfw.KeyF17)
KeyF18 = Button(glfw.KeyF18)
KeyF19 = Button(glfw.KeyF19)
KeyF20 = Button(glfw.KeyF20)
KeyF21 = Button(glfw.KeyF21)
KeyF22 = Button(glfw.KeyF22)
KeyF23 = Button(glfw.KeyF23)
KeyF24 = Button(glfw.KeyF24)
KeyF25 = Button(glfw.KeyF25)
KeyKP0 = Button(glfw.KeyKP0)
KeyKP1 = Button(glfw.KeyKP1)
KeyKP2 = Button(glfw.KeyKP2)
KeyKP3 = Button(glfw.KeyKP3)
KeyKP4 = Button(glfw.KeyKP4)
KeyKP5 = Button(glfw.KeyKP5)
KeyKP6 = Button(glfw.KeyKP6)
KeyKP7 = Button(glfw.KeyKP7)
KeyKP8 = Button(glfw.KeyKP8)
KeyKP9 = Button(glfw.KeyKP9)
KeyKPDecimal = Button(glfw.KeyKPDecimal)
KeyKPDivide = Button(glfw.KeyKPDivide)
KeyKPMultiply = Button(glfw.KeyKPMultiply)
KeyKPSubtract = Button(glfw.KeyKPSubtract)
KeyKPAdd = Button(glfw.KeyKPAdd)
KeyKPEnter = Button(glfw.KeyKPEnter)
KeyKPEqual = Button(glfw.KeyKPEqual)
KeyLeftShift = Button(glfw.KeyLeftShift)
KeyLeftControl = Button(glfw.KeyLeftControl)
KeyLeftAlt = Button(glfw.KeyLeftAlt)
KeyLeftSuper = Button(glfw.KeyLeftSuper)
KeyRightShift = Button(glfw.KeyRightShift)
KeyRightControl = Button(glfw.KeyRightControl)
KeyRightAlt = Button(glfw.KeyRightAlt)
KeyRightSuper = Button(glfw.KeyRightSuper)
KeyMenu = Button(glfw.KeyMenu)
KeyLast = Button(glfw.KeyLast)
)
func (w *Window) initInput() {
mainthread.Call(func() {
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
w.window.SetMouseButtonCallback(func(_ *glfw.Window, button glfw.MouseButton, action glfw.Action, mod glfw.ModifierKey) {
switch action {
case glfw.Press:
w.currInp.buttons[Button(button)] = true
case glfw.Release:
w.currInp.buttons[Button(button)] = false
}
})
w.window.SetKeyCallback(func(_ *glfw.Window, key glfw.Key, scancode int, action glfw.Action, mods glfw.ModifierKey) {
switch action {
case glfw.Press:
w.currInp.buttons[Button(key)] = true
case glfw.Release:
w.currInp.buttons[Button(key)] = false
}
})
w.window.SetScrollCallback(func(_ *glfw.Window, xoff, yoff float64) {
w.currInp.scroll += V(xoff, yoff)
})
})
}
func (w *Window) updateInput() {
// copy temp to prev
w.prevInp = w.tempInp
// zero current scroll (but keep what was added in callbacks outside of this function)
w.currInp.scroll -= w.tempInp.scroll
// get events (usually calls callbacks, but callbacks can be called outside too)
mainthread.Call(func() {
// cache current state to temp (so that if there are callbacks outside this function,
// everything works)