-
Notifications
You must be signed in to change notification settings - Fork 0
/
events.go
94 lines (80 loc) · 2.3 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
package sge
import (
"github.com/klkblake/Go-SDL/sdl"
)
type KeyPresser interface {
KeyPress(keysym *sdl.Keysym)
}
type KeyReleaser interface {
KeyRelease(keysym *sdl.Keysym)
}
type MousePresser interface {
MousePress(x, y uint16, button uint8)
}
type MouseReleaser interface {
MouseRelease(x, y uint16, button uint8)
}
type MouseMover interface {
MouseMove(x, y uint16, xrel, yrel int16)
}
type Events struct {
KeyPressers []KeyPresser
KeyReleasers []KeyReleaser
MousePressers []MousePresser
MouseReleasers []MouseReleaser
MouseMovers []MouseMover
}
func NewEvents() *Events {
events := new(Events)
events.KeyPressers = make([]KeyPresser, 0)
events.KeyReleasers = make([]KeyReleaser, 0)
events.MousePressers = make([]MousePresser, 0)
events.MouseReleasers = make([]MouseReleaser, 0)
events.MouseMovers = make([]MouseMover, 0)
return events
}
func (events *Events) AddKeyPresser(keyPresser KeyPresser) {
events.KeyPressers = append(events.KeyPressers, keyPresser)
}
func (events *Events) AddKeyReleaser(keyReleaser KeyReleaser) {
events.KeyReleasers = append(events.KeyReleasers, keyReleaser)
}
func (events *Events) AddMousePresser(mousePresser MousePresser) {
events.MousePressers = append(events.MousePressers, mousePresser)
}
func (events *Events) AddMouseReleaser(mouseReleaser MouseReleaser) {
events.MouseReleasers = append(events.MouseReleasers, mouseReleaser)
}
func (events *Events) AddMouseMover(mouseMover MouseMover) {
events.MouseMovers = append(events.MouseMovers, mouseMover)
}
func (events *Events) Dispatch(event interface{}) {
switch e := event.(type) {
case sdl.KeyboardEvent:
switch e.Type {
case sdl.KEYDOWN:
for _, keyPresser := range events.KeyPressers {
keyPresser.KeyPress(&e.Keysym)
}
case sdl.KEYUP:
for _, keyReleaser := range events.KeyReleasers {
keyReleaser.KeyRelease(&e.Keysym)
}
}
case sdl.MouseButtonEvent:
switch e.Type {
case sdl.MOUSEBUTTONDOWN:
for _, mousePresser := range events.MousePressers {
mousePresser.MousePress(e.X, e.Y, e.Button)
}
case sdl.MOUSEBUTTONUP:
for _, mouseReleaser := range events.MouseReleasers {
mouseReleaser.MouseRelease(e.X, e.Y, e.Button)
}
}
case sdl.MouseMotionEvent:
for _, mouseMover := range events.MouseMovers {
mouseMover.MouseMove(e.X, e.Y, e.Xrel, e.Yrel)
}
}
}