/
events.go
186 lines (162 loc) · 4.79 KB
/
events.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
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
// Copyright 2014 Arne Roomann-Kurrik
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package twodee
import (
"github.com/go-gl/glfw/v3.1/glfw"
)
type EventHandler struct {
Events chan Event
window *glfw.Window
}
func NewEventHandler(w *glfw.Window) (e *EventHandler) {
e = &EventHandler{
Events: make(chan Event, 100),
window: w,
}
// See http://www.glfw.org/docs/latest/input.html#input_keyboard
w.SetInputMode(glfw.StickyKeysMode, 1)
w.SetCursorPosCallback(e.onMouseMove)
w.SetKeyCallback(e.onKey)
w.SetMouseButtonCallback(e.onMouseButton)
return
}
func (e *EventHandler) GetKey(code KeyCode) Action {
return Action(e.window.GetKey(glfw.Key(code)))
}
func (e *EventHandler) JoystickPresent(joystick Joystick) bool {
return glfw.JoystickPresent(glfw.Joystick(joystick))
}
func (e *EventHandler) JoystickAxes(joystick Joystick) []float32 {
return glfw.GetJoystickAxes(glfw.Joystick(joystick))
}
func (e *EventHandler) JoystickButtons(joystick Joystick) []byte {
return glfw.GetJoystickButtons(glfw.Joystick(joystick))
}
func (e *EventHandler) Poll() {
glfw.PollEvents()
}
func (e *EventHandler) onKey(w *glfw.Window, key glfw.Key, scancode int, action glfw.Action, mods glfw.ModifierKey) {
event := &KeyEvent{
Code: KeyCode(key),
Type: Action(action),
}
e.enqueue(event)
}
func (e *EventHandler) onMouseMove(w *glfw.Window, xoff float64, yoff float64) {
event := &MouseMoveEvent{
X: float32(xoff),
Y: float32(yoff),
}
e.enqueue(event)
}
func (e *EventHandler) onMouseButton(w *glfw.Window, button glfw.MouseButton, action glfw.Action, mod glfw.ModifierKey) {
event := &MouseButtonEvent{
Type: Action(action),
Button: MouseButton(button),
}
e.enqueue(event)
}
func (e *EventHandler) enqueue(event Event) {
select {
case e.Events <- event:
// Added to events
default:
// Events buffer is too full, not being read.
// Drop the event on the floor.
// TODO: Warn?
}
}
type MouseMoveEvent struct {
X float32
Y float32
}
type MouseButtonEvent struct {
Button MouseButton
Type Action
}
type KeyEvent struct {
Code KeyCode
Type Action
}
type KeyCode int
const (
KeyUp = KeyCode(glfw.KeyUp)
KeyDown = KeyCode(glfw.KeyDown)
KeyLeft = KeyCode(glfw.KeyLeft)
KeyRight = KeyCode(glfw.KeyRight)
KeyEnter = KeyCode(glfw.KeyEnter)
KeyEscape = KeyCode(glfw.KeyEscape)
KeySpace = KeyCode(glfw.KeySpace)
KeyA = KeyCode(glfw.KeyA)
KeyB = KeyCode(glfw.KeyB)
KeyC = KeyCode(glfw.KeyC)
KeyD = KeyCode(glfw.KeyD)
KeyE = KeyCode(glfw.KeyE)
KeyF = KeyCode(glfw.KeyF)
KeyG = KeyCode(glfw.KeyG)
KeyH = KeyCode(glfw.KeyH)
KeyI = KeyCode(glfw.KeyI)
KeyJ = KeyCode(glfw.KeyJ)
KeyK = KeyCode(glfw.KeyK)
KeyL = KeyCode(glfw.KeyL)
KeyM = KeyCode(glfw.KeyM)
KeyN = KeyCode(glfw.KeyN)
KeyO = KeyCode(glfw.KeyO)
KeyP = KeyCode(glfw.KeyP)
KeyQ = KeyCode(glfw.KeyQ)
KeyR = KeyCode(glfw.KeyR)
KeyS = KeyCode(glfw.KeyS)
KeyT = KeyCode(glfw.KeyT)
KeyU = KeyCode(glfw.KeyU)
KeyV = KeyCode(glfw.KeyV)
KeyW = KeyCode(glfw.KeyW)
KeyX = KeyCode(glfw.KeyX)
KeyY = KeyCode(glfw.KeyY)
KeyZ = KeyCode(glfw.KeyZ)
Key1 = KeyCode(glfw.Key1)
Key2 = KeyCode(glfw.Key2)
Key3 = KeyCode(glfw.Key3)
Key4 = KeyCode(glfw.Key4)
Key5 = KeyCode(glfw.Key5)
Key6 = KeyCode(glfw.Key6)
Key7 = KeyCode(glfw.Key7)
Key8 = KeyCode(glfw.Key8)
Key9 = KeyCode(glfw.Key9)
Key0 = KeyCode(glfw.Key0)
)
type Joystick int
const (
Joystick1 = Joystick(glfw.Joystick1)
)
type Action int
const (
Release = Action(glfw.Release)
Press = Action(glfw.Press)
Repeat = Action(glfw.Repeat)
)
type MouseButton int
const (
MouseButton1 = MouseButton(glfw.MouseButton1)
MouseButton2 = MouseButton(glfw.MouseButton2)
MouseButton3 = MouseButton(glfw.MouseButton3)
MouseButton4 = MouseButton(glfw.MouseButton4)
MouseButton5 = MouseButton(glfw.MouseButton5)
MouseButton6 = MouseButton(glfw.MouseButton6)
MouseButton7 = MouseButton(glfw.MouseButton7)
MouseButton8 = MouseButton(glfw.MouseButton8)
MouseButtonLast = MouseButton(glfw.MouseButtonLast)
MouseButtonLeft = MouseButton(glfw.MouseButtonLeft)
MouseButtonRight = MouseButton(glfw.MouseButtonRight)
MouseButtonMiddle = MouseButton(glfw.MouseButtonMiddle)
)