This repository has been archived by the owner on Jul 30, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
events.go
387 lines (296 loc) · 8.91 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
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
// Copyright (c) 2018, Randall C. O'Reilly. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package oswin
import (
"image"
// "fmt"
"time"
)
/*
much of this is copied directly from https://github.com/skelterjohn/go.wde
Copyright 2012 the go.wde authors
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.
*/
// EventType determines which type of GUI event is being sent -- need this for indexing into
// different event signalers based on event type, and sending event type in signals
type EventType int64
const (
// mouse events
MouseMovedEventType EventType = iota
MouseDownEventType
MouseUpEventType
MouseDraggedEventType
// gesture events
MagnifyEventType
RotateEventType
ScrollEventType
// key events
KeyDownEventType
KeyUpEventType
KeyTypedEventType
// window events -- todo: need iconfiy, etc events
MouseEnteredEventType
MouseExitedEventType
ResizeEventType
CloseEventType
// number of event types
EventTypeN
)
//go:generate stringer -type=EventType
// buttons that can be pressed
type MouseButton int
const (
LeftButton MouseButton = 1 << iota
MiddleButton
RightButton
WheelUpButton
WheelDownButton
WheelLeftButton // only supported by xgb/win backends atm
WheelRightButton // only supported by xgb/win backends atm
)
// interface for GUI events
type Event interface {
// get the type of event associated with given event
EventType() EventType
// does the event have window position where it takes place?
EventHasPos() bool
// position where event took place -- needed for sending events to the right place
EventPos() image.Point
// does the event operate only on focus item (e.g., keyboard events)
EventOnFocus() bool
// time at which the event was generated
EventTime() time.Time
// has this event already been processed?
IsProcessed() bool
// mark as having been processed
SetProcessed()
}
// base type for events -- records time and whether event has been processed by a receiver of the event -- in which case it is skipped
type EventBase struct {
Processed bool
Time time.Time
}
func (ev EventBase) EventTime() time.Time {
return ev.Time
}
func (ev EventBase) IsProcessed() bool {
return ev.Processed
}
func (ev *EventBase) SetProcessed() {
ev.Processed = true
}
////////////////////////////////////////////////////////////////////////////////////////
// Mouse Events
// MouseEvent is used for data common to all mouse events, and should not appear as an event received by the caller program.
type MouseEvent struct {
EventBase
Where image.Point
}
func (ev MouseEvent) EventHasPos() bool {
return true
}
func (ev MouseEvent) EventPos() image.Point {
return ev.Where
}
func (ev MouseEvent) EventOnFocus() bool {
return false
}
////////////////////////////////////////////
// MouseMovedEvent is for when the mouse moves within the window.
type MouseMovedEvent struct {
MouseEvent
From image.Point
}
func (ev MouseMovedEvent) EventType() EventType {
return MouseMovedEventType
}
// check for interface implementation
var _ Event = &MouseMovedEvent{}
////////////////////////////////////////////
// MouseButtonEvent is used for data common to all mouse button events, and should not appear as an event received by the caller program.
type MouseButtonEvent struct {
MouseEvent
Which MouseButton
}
// MouseDownEvent is for when the mouse is clicked within the window.
type MouseDownEvent MouseButtonEvent
func (ev MouseDownEvent) EventType() EventType {
return MouseDownEventType
}
// check for interface implementation
var _ Event = &MouseDownEvent{}
// MouseUpEvent is for when the mouse is unclicked within the window.
type MouseUpEvent MouseButtonEvent
func (ev MouseUpEvent) EventType() EventType {
return MouseUpEventType
}
// check for interface implementation
var _ Event = &MouseUpEvent{}
////////////////////////////////////////////
// MouseDraggedEvent is for when the mouse is moved while a button is pressed.
type MouseDraggedEvent struct {
MouseMovedEvent
Which MouseButton
}
func (ev MouseDraggedEvent) EventType() EventType {
return MouseDraggedEventType
}
// check for interface implementation
var _ Event = &MouseDraggedEvent{}
////////////////////////////////////////////////////////////////////////////////////////
// Gesture Events
// GestureEvent is used to represents common elements of all gesture-based events
type GestureEvent struct {
MouseEvent
}
////////////////////////////////////////////
// MagnifyEvent is used to represent a magnification gesture.
type MagnifyEvent struct {
GestureEvent
Magnification float64 // the multiplicative scale factor
}
func (ev MagnifyEvent) EventType() EventType {
return MagnifyEventType
}
// check for interface implementation
var _ Event = &MagnifyEvent{}
////////////////////////////////////////////
// RotateEvent is used to represent a rotation gesture.
type RotateEvent struct {
GestureEvent
Rotation float64 // measured in degrees; positive == clockwise
}
func (ev RotateEvent) EventType() EventType {
return RotateEventType
}
// check for interface implementation
var _ Event = &RotateEvent{}
////////////////////////////////////////////
// Scroll Event is used to represent a scrolling gesture.
type ScrollEvent struct {
GestureEvent
Delta image.Point
}
func (ev ScrollEvent) EventType() EventType {
return ScrollEventType
}
// check for interface implementation
var _ Event = &ScrollEvent{}
////////////////////////////////////////////////////////////////////////////////////////
// Key Events
// KeyEvent is used for data common to all key events, and should not appear as an event received by the caller program.
type KeyEvent struct {
EventBase
Key string
}
func (ev KeyEvent) EventHasPos() bool {
return false
}
func (ev KeyEvent) EventPos() image.Point {
return image.ZP
}
func (ev KeyEvent) EventOnFocus() bool {
return true
}
////////////////////////////////////////////
// KeyDownEvent is for when a key is pressed.
type KeyDownEvent struct {
KeyEvent
}
func (ev KeyDownEvent) EventType() EventType {
return KeyDownEventType
}
// check for interface implementation
var _ Event = &KeyDownEvent{}
// KeyUpEvent is for when a key is unpressed.
type KeyUpEvent struct {
KeyEvent
}
func (ev KeyUpEvent) EventType() EventType {
return KeyUpEventType
}
// check for interface implementation
var _ Event = &KeyUpEvent{}
// KeyTypedEvent is for when a key is typed.
type KeyTypedEvent struct {
KeyEvent
// The glyph is the string corresponding to what the user wants to have typed in
// whatever data entry is active.
Glyph string
// The "+" joined set of keys (not glyphs) participating in the chord completed
// by this key event. The keys will be in a consistent order, no matter what
// order they are pressed in.
Chord string
}
func (ev KeyTypedEvent) EventType() EventType {
return KeyTypedEventType
}
// check for interface implementation
var _ Event = &KeyTypedEvent{}
////////////////////////////////////////////////////////////////////////////////////////
// Window Events
////////////////////////////////////////////
// MouseEnteredEvent is for when the mouse enters a window, or a widget (computed by window)
type MouseEnteredEvent struct {
MouseMovedEvent
}
func (ev MouseEnteredEvent) EventType() EventType {
return MouseEnteredEventType
}
// check for interface implementation
var _ Event = &MouseEnteredEvent{}
// MouseExitedEvent is for when the mouse exits a window, or a widget (computed by window)a
type MouseExitedEvent struct {
MouseMovedEvent
}
func (ev MouseExitedEvent) EventType() EventType {
return MouseExitedEventType
}
// check for interface implementation
var _ Event = &MouseExitedEvent{}
// ResizeEvent is for when the window changes size.
type ResizeEvent struct {
EventBase
Width, Height int
}
func (ev ResizeEvent) EventType() EventType {
return ResizeEventType
}
func (ev ResizeEvent) EventHasPos() bool {
return false
}
func (ev ResizeEvent) EventPos() image.Point {
return image.ZP
}
func (ev ResizeEvent) EventOnFocus() bool {
return false
}
// check for interface implementation
var _ Event = &ResizeEvent{}
// CloseEvent is for when the window is closed.
type CloseEvent struct {
EventBase
}
func (ev CloseEvent) EventType() EventType {
return CloseEventType
}
func (ev CloseEvent) EventHasPos() bool {
return false
}
func (ev CloseEvent) EventPos() image.Point {
return image.ZP
}
func (ev CloseEvent) EventOnFocus() bool {
return false
}
// check for interface implementation
var _ Event = &CloseEvent{}