/
app.go
86 lines (73 loc) · 1.63 KB
/
app.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
package gui
import (
"fmt"
"github.com/veandco/go-sdl2/sdl"
)
type App struct {
Title string
ScreenWidth int32
ScreenHeight int32
Containers []*Container
Running bool
MouseX int32
MouseY int32
BackgroundColor sdl.Color
}
func NewApp(title string, w int32, h int32) (*App, error) {
newApp := App{
Title: title,
ScreenWidth: w,
ScreenHeight: h,
Containers: nil,
Running: true,
MouseX: 0,
MouseY: 0,
BackgroundColor: COLOR_BLACK,
}
err := Init(&newApp)
if err != nil {
return nil, err
}
return &newApp, nil
}
func (a *App) SetTitle(title string) {
W.SetTitle(title)
}
func (a *App) SetBackgroundColor(c sdl.Color) {
a.BackgroundColor = c
}
func (a *App) AddContainer(c *Container) {
a.Containers = append(a.Containers, c)
// redraw containers
RedrawAllContainers(a.Containers)
}
func (a *App) RunLoop() {
for a.Running {
// Events
for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
switch event.(type) {
case *sdl.QuitEvent:
fmt.Println("Exiting inet...")
a.Running = false
break
case *sdl.MouseMotionEvent:
e := event.(*sdl.MouseMotionEvent)
// Update Mouse Position
UpdateMousePos(e.X, e.Y)
// Update Loop
UpdateHoverStateForAllContainers(a.Containers)
case *sdl.MouseButtonEvent:
e := event.(*sdl.MouseButtonEvent)
MouseButtonEvent(a.Containers, e)
case *sdl.KeyboardEvent:
e := event.(*sdl.KeyboardEvent)
KeyboardEvent(a.Containers, e.Type, e.Keysym)
}
}
}
W.Destroy()
sdl.Quit()
}
func (s *App) Close() {
s.Running = false
}