-
Notifications
You must be signed in to change notification settings - Fork 0
/
event.go
83 lines (74 loc) · 1.75 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
package gamebuilder
import (
"github.com/veandco/go-sdl2/sdl"
"github.com/ymohl-cl/go-ui/objects"
)
// event : catch mouse and keyboard events
func event(e sdl.Event, s Scene) error {
var err error
switch e.(type) {
case *sdl.MouseMotionEvent:
err = mouseMotionEvent(e.(*sdl.MouseMotionEvent), s)
case *sdl.MouseButtonEvent:
err = mouseButtonEvent(e.(*sdl.MouseButtonEvent), s)
case *sdl.KeyboardEvent:
err = keyboardEvent(e.(*sdl.KeyboardEvent), s)
}
if err != nil {
return err
}
return nil
}
// mouseMotionEvent define a new mouse's position
func mouseMotionEvent(mm *sdl.MouseMotionEvent, s Scene) error {
layers, m := s.GetLayers()
m.Lock()
defer m.Unlock()
size := len(layers)
for i := size - 1; i >= 0; i-- {
layer := layers[uint8(i)]
for _, object := range layer {
if object.IsOver(mm.X, mm.Y) {
if object.GetStatus() != objects.SClick {
go object.SetStatus(objects.SOver)
}
} else {
go object.SetStatus(objects.SBasic)
}
}
}
return nil
}
// mouseButtonEvent define a new mouse's action (click)
func mouseButtonEvent(b *sdl.MouseButtonEvent, s Scene) error {
if b.Button != sdl.BUTTON_LEFT {
return nil
}
layers, m := s.GetLayers()
m.Lock()
defer m.Unlock()
size := len(layers)
for i := size - 1; i >= 0; i-- {
layer := layers[uint8(i)]
for _, object := range layer {
if b.State == sdl.PRESSED {
if object.GetStatus() == objects.SOver {
go object.SetStatus(objects.SClick)
break
}
} else if b.State == sdl.RELEASED {
if object.GetStatus() == objects.SClick {
go object.SetStatus(objects.SOver)
go object.Click()
break
}
}
}
}
return nil
}
// keyboardEvent : _
func keyboardEvent(k *sdl.KeyboardEvent, s Scene) error {
go s.KeyboardEvent(k)
return nil
}