/
render.go
234 lines (199 loc) · 5.98 KB
/
render.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
package ge_go_sdl2
import (
"fmt"
"github.com/veandco/go-sdl2/sdl"
"github.com/veandco/go-sdl2/ttf"
)
var (
window *sdl.Window
views map[string]View
)
func registerViews(gui []View) {
views = make(map[string]View)
for _, v := range gui {
views[v.Id] = v
}
}
func renderGUI(wnd View) {
var err error
if wnd.X == 0 && wnd.Y == 0 {
window, err = sdl.CreateWindow(wnd.Id, sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, wnd.W, wnd.H, sdl.WINDOW_SHOWN)
} else {
window, err = sdl.CreateWindow(wnd.Id, wnd.X, wnd.Y, wnd.W, wnd.H, sdl.WINDOW_SHOWN)
}
if err != nil {
panic(err)
}
surface, err := window.GetSurface()
if err != nil {
panic(err)
}
surface.FillRect(&sdl.Rect{X: 0, Y: 0, W: wnd.W, H: wnd.H}, wnd.BgColor)
for _, child := range wnd.Children {
switch t := child.(type) {
case View:
renderView(surface, t)
break
case Container:
renderContainer(surface, t)
break
case TextField:
renderTextField(surface, t)
break
case Button:
renderButton(surface, t)
break
case Text:
renderText(surface, t)
break
}
}
window.UpdateSurface()
}
func destroy() {
window.Destroy()
}
func renderContainer(surface *sdl.Surface, viewContainer Container) {
renderThis := func(container *Container) {
view := views[container.ViewId]
view.X += container.X
view.Y += container.Y
renderView(surface, view)
}
renderThis(&viewContainer)
updateChan := make(chan propertyChange)
updateChannels[viewContainer.Id] = updateChan
go handleElementPropertyChanges(updateChan, &viewContainer, renderThis)
}
func renderView(surface *sdl.Surface, view View) {
rect := sdl.Rect{X: view.X, Y: view.Y, W: view.W, H: view.H}
borderRect := sdl.Rect{X: view.X - 1, Y: view.Y - 1, W: view.W + 2, H: view.H + 2}
surface.FillRect(&borderRect, view.BorderColor)
surface.FillRect(&rect, view.BgColor)
for _, child := range view.Children {
switch t := child.(type) {
case View:
t.X += view.X
t.Y += view.Y
renderView(surface, t)
break
case Container:
t.X += view.X
t.Y += view.Y
renderContainer(surface, t)
break
case TextField:
t.X += view.X
t.Y += view.Y
renderTextField(surface, t)
break
case Button:
t.X += view.X
t.Y += view.Y
renderButton(surface, t)
break
case Text:
t.X += view.X
t.Y += view.Y
renderText(surface, t)
break
}
}
}
func renderButton(surface *sdl.Surface, btn Button) {
renderThis := func(btn *Button) {
borderRect := sdl.Rect{X: btn.X, Y: btn.Y, W: btn.W, H: btn.H}
innerRect := sdl.Rect{X: btn.X + 1, Y: btn.Y + 1, W: btn.W - 2, H: btn.H - 2}
surface.FillRect(&borderRect, btn.BorderColor)
surface.FillRect(&innerRect, btn.BgColor)
btn.ContentLabel.X = btn.X + 2
btn.ContentLabel.Y = btn.Y + 1
btn.ContentLabel.Content = btn.Content
clickListeners[btn.Id] = clickListener{boundary: borderRect, onClickChan: btn.OnClick}
renderText(surface, btn.ContentLabel)
}
renderThis(&btn)
updateChan := make(chan propertyChange)
updateChannels[btn.Id] = updateChan
go handleElementPropertyChanges(updateChan, &btn, renderThis)
}
func renderText(surface *sdl.Surface, txt Text) {
red, green, blue, alpha := surface.At(int(txt.X), int(txt.Y)).RGBA()
bgColor := sdl.MapRGBA(surface.Format, uint8(red), uint8(green), uint8(blue), uint8(alpha))
clearRect := &sdl.Rect{X: txt.X, Y: txt.Y + (txt.H / 2), W: 0, H: 0} // Initialize with width and heigh 0 as to not clear anything first time around..
renderThis := func(text *Text) {
textFont, err := ttf.OpenFont(text.Font, text.Size)
if err != nil {
panic(err.Error())
}
defer textFont.Close()
label, err := textFont.RenderUTF8Blended(text.Content, text.TextColor)
if err != nil {
return
}
defer label.Free()
switch text.Alignment {
case Left:
surface.FillRect(clearRect, bgColor)
clearRect.X = text.X
clearRect.Y = text.Y + (text.H / 2)
clearRect.W = label.ClipRect.W
clearRect.H = label.ClipRect.H
err = label.Blit(nil, surface, &sdl.Rect{X: text.X, Y: text.Y + (text.H / 2), W: 0, H: 0})
case Center:
surface.FillRect(clearRect, bgColor)
clearRect.X = text.X + (text.W / 2) - (label.ClipRect.W / 2)
clearRect.Y = text.Y + (text.H / 2)
clearRect.W = label.ClipRect.W
clearRect.H = label.ClipRect.H
err = label.Blit(nil, surface, &sdl.Rect{X: text.X + (text.W / 2) - (label.ClipRect.W / 2), Y: text.Y + (text.H / 2), W: 0, H: 0})
case Right:
surface.FillRect(clearRect, bgColor)
clearRect.X = text.X + text.W - label.ClipRect.W
clearRect.Y = text.Y + (text.H / 2)
clearRect.W = label.ClipRect.W
clearRect.H = label.ClipRect.H
err = label.Blit(nil, surface, &sdl.Rect{X: text.X + text.W - label.ClipRect.W, Y: text.Y + (text.H / 2), W: 0, H: 0})
}
if err != nil {
fmt.Errorf(err.Error())
}
}
renderThis(&txt)
updateChan := make(chan propertyChange)
updateChannels[txt.Id] = updateChan
go handleElementPropertyChanges(updateChan, &txt, renderThis)
}
func renderTextField(surface *sdl.Surface, txtField TextField) {
renderThis := func() {
borderRect := sdl.Rect{X: txtField.X, Y: txtField.Y, W: txtField.W, H: txtField.H}
innerRect := sdl.Rect{X: txtField.X + 1, Y: txtField.Y + 1, W: txtField.W - 2, H: txtField.H - 2}
surface.FillRect(&borderRect, txtField.BorderColor)
surface.FillRect(&innerRect, txtField.BgColor)
clickListeners[txtField.Id] = clickListener{boundary: borderRect, onClickChan: focusClick}
txtLabel := Text{X: innerRect.X + 2, Y: innerRect.Y + 2, Size: txtField.Size, Font: txtField.Font, Content: txtField.Value, TextColor: txtField.TextColor}
renderText(surface, txtLabel)
}
renderThis()
onInput := make(chan string)
onEdit := make(chan string)
focusListeners[txtField.Id] = focusListener{onInput: onInput, onEdit: onEdit}
go func() {
for {
select {
case v := <-onInput:
txtField.Value += v
case v := <-onEdit:
switch v {
case "-":
if len(txtField.Value) > 0 {
txtField.Value = txtField.Value[0 : len(txtField.Value)-1]
}
}
}
renderThis()
txtField.notifyChange()
window.UpdateSurface()
}
}()
}