-
Notifications
You must be signed in to change notification settings - Fork 1
/
event.go
325 lines (259 loc) · 10.9 KB
/
event.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
187
188
189
190
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
// Copyright (C) 2012-2014 by krepa098. All rights reserved.
// Use of this source code is governed by a zlib-style
// license that can be found in the license.txt file.
package gosfml2
// #include <SFML/Window.h>
// int getEventType(sfEvent* ev) { return ev->type; }
// sfSizeEvent* getSizeEvent(sfEvent* ev) { return &ev->size; }
// sfKeyEvent* getKeyEvent(sfEvent* ev) { return &ev->key; }
// sfTextEvent* getTextEvent(sfEvent* ev) { return &ev->text; }
// sfMouseMoveEvent* getMouseMoveEvent(sfEvent* ev) { return &ev->mouseMove; }
// sfMouseButtonEvent* getMouseButtonEvent(sfEvent* ev) { return &ev->mouseButton; }
// sfMouseWheelEvent* getMouseWheelEvent(sfEvent* ev) { return &ev->mouseWheel; }
// sfJoystickMoveEvent* getJoystickMoveEvent(sfEvent* ev) { return &ev->joystickMove; }
// sfJoystickButtonEvent* getJoystickButtonEvent(sfEvent* ev) { return &ev->joystickButton; }
// sfJoystickConnectEvent* getJoystickConnectEvent(sfEvent* ev) { return &ev->joystickConnect; }
import "C"
import "log"
/////////////////////////////////////
/// CONSTS
/////////////////////////////////////
type EventType int
const (
EventTypeClosed EventType = C.sfEvtClosed
EventTypeResized EventType = C.sfEvtResized
EventTypeLostFocus EventType = C.sfEvtLostFocus
EventTypeGainedFocus EventType = C.sfEvtGainedFocus
EventTypeTextEntered EventType = C.sfEvtTextEntered
EventTypeKeyPressed EventType = C.sfEvtKeyPressed
EventTypeKeyReleased EventType = C.sfEvtKeyReleased
EventTypeMouseWheelMoved EventType = C.sfEvtMouseWheelMoved
EventTypeMouseWheelScrolled EventType = C.sfEvtMouseWheelScrolled
EventTypeMouseButtonPressed EventType = C.sfEvtMouseButtonPressed
EventTypeMouseButtonReleased EventType = C.sfEvtMouseButtonReleased
EventTypeMouseMoved EventType = C.sfEvtMouseMoved
EventTypeMouseEntered EventType = C.sfEvtMouseEntered
EventTypeMouseLeft EventType = C.sfEvtMouseLeft
EventTypeJoystickButtonPressed EventType = C.sfEvtJoystickButtonPressed
EventTypeJoystickButtonReleased EventType = C.sfEvtJoystickButtonReleased
EventTypeJoystickMoved EventType = C.sfEvtJoystickMoved
EventTypeJoystickConnected EventType = C.sfEvtJoystickConnected
EventTypeJoystickDisconnected EventType = C.sfEvtJoystickDisconnected
)
/////////////////////////////////////
/// INTERFACES
/////////////////////////////////////
type Event interface {
Type() EventType
}
///////////////////////////////////////////////////////////////
// EmptyEvents
// The window lost the focus (no data)
type EventLostFocus struct{}
func (EventLostFocus) Type() EventType {
return EventTypeLostFocus
}
// The window gained the focus (no data)
type EventGainedFocus struct{}
func (EventGainedFocus) Type() EventType {
return EventTypeGainedFocus
}
// The mouse cursor entered the area of the window (no data)
type EventMouseEntered struct{}
func (EventMouseEntered) Type() EventType {
return EventTypeMouseEntered
}
//EventMouseWheelScrolled if the mouse wheel got scrolled
type EventMouseWheelScrolled struct{}
func (EventMouseWheelScrolled) Type() EventType {
return EventTypeMouseWheelScrolled
}
// The mouse cursor left the area of the window (no data)
type EventMouseLeft struct{}
func (EventMouseLeft) Type() EventType {
return EventTypeMouseLeft
}
// The window requested to be closed (no data)
type EventClosed struct{}
func (EventClosed) Type() EventType {
return EventTypeClosed
}
///////////////////////////////////////////////////////////////
// KeyEvent
type eventKey struct {
Code KeyCode //< Code of the key that has been pressed
Alt int //< Is the Alt key pressed?
Control int //< Is the Control key pressed?
Shift int //< Is the Shift key pressed?
System int //< Is the System key pressed?
}
type EventKeyPressed eventKey
type EventKeyReleased eventKey
func newKeyEventFromC(ev *C.sfKeyEvent) eventKey {
return eventKey{Code: KeyCode(ev.code), Alt: int(ev.alt), Control: int(ev.control), Shift: int(ev.shift), System: int(ev.system)}
}
func (EventKeyPressed) Type() EventType {
return EventTypeKeyPressed
}
func (EventKeyReleased) Type() EventType {
return EventTypeKeyReleased
}
///////////////////////////////////////////////////////////////
// SizeEvent
type EventResized struct {
Width uint //< New width, in pixels
Height uint //< New height, in pixels
}
func newSizeEventFromC(ev *C.sfSizeEvent) EventResized {
return EventResized{Width: uint(ev.width), Height: uint(ev.height)}
}
func (EventResized) Type() EventType {
return EventTypeResized
}
///////////////////////////////////////////////////////////////
// TextEvent
type EventTextEntered struct {
Char rune //< Value of the rune
}
func newTextEventFromC(ev *C.sfTextEvent) EventTextEntered {
return EventTextEntered{Char: rune(uint32(ev.unicode))}
}
func (EventTextEntered) Type() EventType {
return EventTypeTextEntered
}
///////////////////////////////////////////////////////////////
// MouseMoveEvent
type EventMouseMoved struct {
X int //< X position of the mouse pointer, relative to the left of the owner window
Y int //< Y position of the mouse pointer, relative to the top of the owner window
}
func newMouseMoveEventFromC(ev *C.sfMouseMoveEvent) EventMouseMoved {
return EventMouseMoved{X: int(ev.x), Y: int(ev.y)}
}
func (EventMouseMoved) Type() EventType {
return EventTypeMouseMoved
}
///////////////////////////////////////////////////////////////
// MouseButtonEvent
type eventMouseButton struct {
Button MouseButton //< Code of the button that has been pressed
X int //< X position of the mouse pointer, relative to the left of the owner window
Y int //< Y position of the mouse pointer, relative to the top of the owner window
}
type EventMouseButtonPressed eventMouseButton
type EventMouseButtonReleased eventMouseButton
func newMouseButtonEventFromC(ev *C.sfMouseButtonEvent) eventMouseButton {
return eventMouseButton{Button: MouseButton(ev.button), X: int(ev.x), Y: int(ev.y)}
}
func (EventMouseButtonPressed) Type() EventType {
return EventTypeMouseButtonPressed
}
func (EventMouseButtonReleased) Type() EventType {
return EventTypeMouseButtonReleased
}
///////////////////////////////////////////////////////////////
// MouseWheelEvent
type EventMouseWheelMoved struct {
Delta int //< Number of ticks the wheel has moved (positive is up, negative is down)
X int //< X position of the mouse pointer, relative to the left of the owner window
Y int //< Y position of the mouse pointer, relative to the top of the owner window
}
func newMouseWheelEventFromC(ev *C.sfMouseWheelEvent) EventMouseWheelMoved {
return EventMouseWheelMoved{Delta: int(ev.delta), X: int(ev.x), Y: int(ev.y)}
}
func (EventMouseWheelMoved) Type() EventType {
return EventTypeMouseWheelMoved
}
///////////////////////////////////////////////////////////////
// JoystickMoveEvent
type EventJoystickMoved struct {
JoystickId uint //< Index of the joystick (in range [0 .. JoystickCount - 1])
Axis JoystickAxis //< Axis on which the joystick moved
Position float32 //< New position on the axis (in range [-100 .. 100])
}
func newJoystickMoveEventFromC(ev *C.sfJoystickMoveEvent) EventJoystickMoved {
return EventJoystickMoved{JoystickId: uint(ev.joystickId), Axis: JoystickAxis(ev.axis), Position: float32(ev.position)}
}
func (EventJoystickMoved) Type() EventType {
return EventTypeJoystickMoved
}
///////////////////////////////////////////////////////////////
// JoystickButtonEvent
type eventJoystickButton struct {
JoystickId uint //< Index of the joystick (in range [0 .. JoystickCount - 1])
Button uint //< Index of the button that has been pressed (in range [0 .. JoystickButtonCount - 1])
}
func newJoystickButtonEventFromC(ev *C.sfJoystickButtonEvent) eventJoystickButton {
return eventJoystickButton{JoystickId: uint(ev.joystickId), Button: uint(ev.button)}
}
type EventJoystickButtonPressed eventJoystickButton
type EventJoystickButtonReleased eventJoystickButton
func (EventJoystickButtonPressed) Type() EventType {
return EventTypeJoystickButtonPressed
}
func (EventJoystickButtonReleased) Type() EventType {
return EventTypeJoystickButtonReleased
}
///////////////////////////////////////////////////////////////
// JoystickConnectEvent
type eventJoystickConnection struct {
JoystickId uint //< Index of the joystick (in range [0 .. JoystickCount - 1])
}
type EventJoystickConnected eventJoystickConnection
type EventJoystickDisconnected eventJoystickConnection
func newJoystickConnectEventFromC(ev *C.sfJoystickConnectEvent) eventJoystickConnection {
return eventJoystickConnection{JoystickId: uint(ev.joystickId)}
}
func (EventJoystickConnected) Type() EventType {
return EventTypeJoystickConnected
}
func (EventJoystickDisconnected) Type() EventType {
return EventTypeJoystickDisconnected
}
///////////////////////////////////////////////////////////////
//standard event handling method used by Window & RenderWindow
func handleEvent(cEvent *C.sfEvent) (ev Event) {
switch EventType(C.getEventType(cEvent)) {
case EventTypeResized:
ev = newSizeEventFromC(C.getSizeEvent(cEvent))
case EventTypeClosed:
ev = EventClosed{}
case EventTypeLostFocus:
ev = EventLostFocus{}
case EventTypeGainedFocus:
ev = EventGainedFocus{}
case EventTypeTextEntered:
ev = newTextEventFromC(C.getTextEvent(cEvent))
case EventTypeKeyReleased:
ev = (EventKeyReleased)(newKeyEventFromC(C.getKeyEvent(cEvent)))
case EventTypeKeyPressed:
ev = (EventKeyPressed)(newKeyEventFromC(C.getKeyEvent(cEvent)))
case EventTypeMouseWheelMoved:
ev = newMouseWheelEventFromC(C.getMouseWheelEvent(cEvent))
case EventTypeMouseWheelScrolled:
ev = newMouseWheelEventFromC(C.getMouseWheelEvent(cEvent))
case EventTypeMouseButtonReleased:
ev = (EventMouseButtonReleased)(newMouseButtonEventFromC(C.getMouseButtonEvent(cEvent)))
case EventTypeMouseButtonPressed:
ev = (EventMouseButtonPressed)(newMouseButtonEventFromC(C.getMouseButtonEvent(cEvent)))
case EventTypeMouseMoved:
ev = newMouseMoveEventFromC(C.getMouseMoveEvent(cEvent))
case EventTypeMouseLeft:
ev = EventMouseLeft{}
case EventTypeMouseEntered:
ev = EventMouseEntered{}
case EventTypeJoystickButtonReleased:
ev = (EventJoystickButtonReleased)(newJoystickButtonEventFromC(C.getJoystickButtonEvent(cEvent)))
case EventTypeJoystickButtonPressed:
ev = (EventJoystickButtonPressed)(newJoystickButtonEventFromC(C.getJoystickButtonEvent(cEvent)))
case EventTypeJoystickMoved:
ev = newJoystickMoveEventFromC(C.getJoystickMoveEvent(cEvent))
case EventTypeJoystickDisconnected:
ev = (EventJoystickDisconnected)(newJoystickConnectEventFromC(C.getJoystickConnectEvent(cEvent)))
case EventTypeJoystickConnected:
ev = (EventJoystickConnected)(newJoystickConnectEventFromC(C.getJoystickConnectEvent(cEvent)))
default:
log.Printf("Unknown event %d occured. No handling defined\n", EventType(C.getEventType(cEvent)))
}
return
}