/
ui.go
380 lines (329 loc) · 8.09 KB
/
ui.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
package main
import (
"log"
"github.com/krig/Go-SDL2/sdl"
"github.com/krig/Go-SDL2/ttf"
)
type Visual interface {
Draw(rend* sdl.Renderer)
Destroy()
GetPos() sdl.Rect
SetPos(pos sdl.Rect)
}
type Layout interface {
UpdateLayout(space sdl.Rect)
}
type Clickable interface {
OnClick(handler func())
}
type MouseLover interface {
GetPos() sdl.Rect
OnMouseButtonEvent(event *sdl.MouseButtonEvent) bool
OnMouseMotionEvent(event *sdl.MouseMotionEvent) bool
}
type Widget struct {
Pos sdl.Rect
}
type Pane struct {
Widget
elements []Visual
layouts []Layout
}
type Button struct {
Widget
// State: 0 = off, 1 = hover, 2 = on, 3 = on+hover
State int
texture *sdl.Texture
clickhandler func()
}
type Label struct {
Widget
Text string
Font *ttf.Font
Color sdl.Color
texture *sdl.Texture
texwidth int32
texheight int32
}
type HorizontalLayout struct {
Widget
elements_left []Visual
element_center Visual
elements_right []Visual
HSpacing int32
}
type MenuEntry struct {
Text string
label *Label
}
type PopupMenu struct {
Widget
entries []MenuEntry
hover int
Visible bool
Spacing, Inset int32
clickhandler func(entry *MenuEntry)
}
func (widget *Widget) GetPos() sdl.Rect {
return widget.Pos
}
func (widget *Widget) SetPos(pos sdl.Rect) {
widget.Pos = pos
}
func (pane *Pane) Draw(rend *sdl.Renderer) {
for _, w := range pane.elements {
w.Draw(rend)
}
}
func (pane *Pane) AddVisual(visual Visual) {
pane.elements = append(pane.elements, visual)
}
func (pane *Pane) AddLayout(layout Layout) {
pane.layouts = append(pane.layouts, layout)
}
func (pane *Pane) UpdateLayout(space sdl.Rect) {
pane.Pos = space
for _, l := range pane.layouts {
l.UpdateLayout(space)
}
}
func (pane *Pane) Destroy() {
for _, e := range pane.elements {
e.Destroy()
}
}
func (h *HorizontalLayout) Init(space sdl.Rect) {
h.Pos = space
h.HSpacing = 2
}
func (h *HorizontalLayout) UpdateLayout(space sdl.Rect) {
x := space.X + h.HSpacing
h.Pos = sdl.Rect{space.X, space.Y, space.W, h.Pos.H}
for _, w := range h.elements_left {
pos := w.GetPos()
w.SetPos(sdl.Rect{x, space.Y, pos.W, pos.H})
x += pos.W + h.HSpacing
}
rx := space.X + space.W - h.HSpacing
for i := len(h.elements_right) - 1; i >= 0; i-- {
w := h.elements_right[i]
pos := w.GetPos()
w.SetPos(sdl.Rect{rx - pos.W, space.Y, pos.W, pos.H})
rx -= (pos.W + h.HSpacing)
}
if h.element_center != nil {
h.element_center.SetPos(sdl.Rect{x, space.Y, rx - x, h.Pos.H})
}
}
func (h *HorizontalLayout) AddLeft(widget Visual) {
h.elements_left = append(h.elements_left, widget)
}
func (h *HorizontalLayout) AddRight(widget Visual) {
h.elements_right = append(h.elements_right, widget)
}
func (h *HorizontalLayout) SetCenter(widget Visual) {
h.element_center = widget
}
func (h *HorizontalLayout) Destroy() {
for _, w := range h.elements_left {
w.Destroy()
}
if h.element_center != nil {
h.element_center.Destroy()
}
for _, w := range h.elements_right {
w.Destroy()
}
}
func (button *Button) Init(rend *sdl.Renderer, space sdl.Rect, texture *sdl.Texture) {
button.Pos = space
button.texture = texture
w, h := texture.GetSize()
button.Pos.W = int32(w) / 3
button.Pos.H = int32(h)
button.State = 0
}
func (label *Label) Init(rend *sdl.Renderer, space sdl.Rect, text string, font *ttf.Font, color sdl.Color) {
label.Pos = space
label.Text = text
label.Font = font
label.texture = nil
label.Color = color
label.Update(rend)
}
func (button *Button) Destroy() {
if button.texture != nil {
button.texture.Destroy()
button.texture = nil
}
}
func (button *Button) OnClick(handler func()) {
button.clickhandler = handler
}
func (button *Button) OnMouseMotionEvent(event *sdl.MouseMotionEvent) bool {
if button.Pos.Contains(event.X, event.Y) {
button.State |= 1
} else {
button.State &= ^1
}
return true
}
func (button *Button) OnMouseButtonEvent(event *sdl.MouseButtonEvent) bool {
if event.Button == sdl.BUTTON_LEFT {
contains := button.Pos.Contains(event.X, event.Y)
pressed := event.State == sdl.PRESSED
was_pressed := (button.State & 2) != 0
if pressed && contains {
button.State |= 2
} else if was_pressed && !pressed && contains {
button.State &= ^2
if button.clickhandler != nil {
button.clickhandler()
}
} else {
button.State &= ^2
}
}
return true
}
func (label *Label) Destroy() {
if label.texture != nil {
label.texture.Destroy()
label.texture = nil
}
}
func (label *Label) Update(rend *sdl.Renderer) {
textw, texth, err := label.Font.SizeText(label.Text)
if err != nil {
log.Fatal(err)
}
txt_surface := label.Font.RenderText_Blended(label.Text, label.Color)
if label.texture != nil {
label.texture.Destroy()
}
label.texture = rend.CreateTextureFromSurface(txt_surface)
txt_surface.Free()
label.texwidth = int32(textw)
label.texheight = int32(texth)
}
func (button *Button) Draw(rend *sdl.Renderer) {
w := button.Pos.W
state := button.State
if state > 2 {
state = 2
}
rend.Copy(button.texture, &sdl.Rect{int32(state) * w, 0, button.Pos.W, button.Pos.H}, &button.Pos)
}
func (label *Label) Draw(rend *sdl.Renderer) {
pos := sdl.Rect{label.Pos.X + (label.Pos.W - label.texwidth) / 2, label.Pos.Y + (label.Pos.H - label.texheight)/2, label.texwidth, label.texheight}
rend.Copy(label.texture, nil, &pos)
}
// Init a popup menu
func (menu *PopupMenu) Init(rend *sdl.Renderer, space sdl.Rect, entries []string, font *ttf.Font) {
menu.Pos = space
menu.Inset = 4
menu.Spacing = 16
entry_pos := sdl.Rect{menu.Pos.X + menu.Inset,
menu.Pos.Y + menu.Inset,
menu.Pos.W - menu.Inset*2,
menu.Spacing}
max_w := int32(0)
for i, e := range entries {
menu.entries = append(menu.entries, MenuEntry{})
menu.entries[i].Init(rend, entry_pos, e, font, hexcolor(0xeeeeec))
if menu.entries[i].label.texwidth > max_w {
max_w = menu.entries[i].label.texwidth
}
entry_pos.Y += menu.Spacing
}
menu.Pos.W = max_w + menu.Inset*2
menu.Pos.H = int32(len(entries))*menu.Spacing + menu.Inset*2
if menu.Pos.W < 100 {
menu.Pos.W = 100
}
if menu.Pos.H < menu.Spacing*2 {
menu.Pos.H = menu.Spacing*2
}
for _, e := range menu.entries {
e.label.Pos.W = menu.Pos.W - menu.Inset*2
e.label.Pos.H = menu.Spacing
}
}
func (menu *PopupMenu) OnClick(handler func(entry *MenuEntry)) {
menu.clickhandler = handler
}
// Draw a popup menu
func (menu *PopupMenu) Draw(rend *sdl.Renderer) {
if !menu.Visible {
return
}
rend.SetDrawColor(hexcolor(0x303030))
rend.FillRect(&menu.Pos)
rend.SetDrawColor(hexcolor(0x363636))
rend.DrawRect(&menu.Pos)
if menu.hover != -1 && menu.hover < len(menu.entries) {
rend.SetDrawColor(hexcolor(0x406f40))
rend.FillRect(&menu.entries[menu.hover].label.Pos)
}
for _, e := range menu.entries {
e.Draw(rend)
}
}
func (menu *PopupMenu) UpdateLayout(space sdl.Rect) {
// TODO
}
func (menu *PopupMenu) Show(x, y int32) {
menu.Visible = true
menu.Pos.X = x
menu.Pos.Y = y
new_x := menu.Pos.X + menu.Inset
new_y := menu.Pos.Y + menu.Inset
for _, e := range menu.entries {
e.label.Pos.X = new_x
e.label.Pos.Y = new_y
new_y += menu.Spacing
}
}
func (menu *PopupMenu) Hide() {
menu.Visible = false
menu.hover = -1
}
func (entry *MenuEntry) Init(rend *sdl.Renderer, space sdl.Rect, text string, font *ttf.Font, color sdl.Color) {
entry.Text = text
entry.label = &Label{}
entry.label.Init(rend, space, text, font, color)
}
func (entry *MenuEntry) Draw(rend *sdl.Renderer) {
entry.label.Draw(rend)
}
func (menu *PopupMenu) OnMouseMotionEvent(event *sdl.MouseMotionEvent) bool {
menu.hover = -1
if menu.Visible {
for i, e := range menu.entries {
if e.label.Pos.Contains(event.X, event.Y) {
//log.Println(e.Text, e.label.Pos, "contains", event.X, event.Y)
menu.hover = i
}
}
} else {
}
return true
}
func (menu *PopupMenu) OnMouseButtonEvent(event *sdl.MouseButtonEvent) bool {
if menu.Visible {
if event.Button == sdl.BUTTON_LEFT {
contains := menu.Pos.Contains(event.X, event.Y)
pressed := event.State == sdl.PRESSED
was_pressed := menu.hover != -1
if was_pressed && !pressed && contains {
if menu.clickhandler != nil {
menu.clickhandler(&menu.entries[menu.hover])
}
}
if was_pressed && !pressed {
menu.Hide()
}
}
}
return true
}