-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathskinmap.go
393 lines (287 loc) · 7.53 KB
/
skinmap.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
381
382
383
384
385
386
387
388
389
390
391
392
393
package glui
import (
"unsafe"
"github.com/go-gl/gl/v4.1-core/gl"
"github.com/veandco/go-sdl2/sdl"
)
type SkinMap struct {
tb *TextureBuilder
skin Skin
data []byte
width int
height int
buttonX int
buttonY int
buttonT int
buttonPressedX int
buttonPressedY int
inputX int
inputY int
inputT int
focusX int
focusY int
focusT int
insetX int
insetY int
cornerX int
cornerY int
barX int
barY int
barT int // not the border thickness of the bar, but the actual intended bar thickness
radioOffX int
radioOffY int
radioOnX int
radioOnY int
radioSize int // size of one side
tickX int
tickY int
tickSize int // internal size of one side
sbTrackX int
sbTrackY int
sbTrackSize int
loc uint32
tid uint32
tunit uint32
}
func newSkinMap(s Skin) *SkinMap {
d := &SkinMap{} // zero construct, because number of fields of SkinMap increases a lot
d.skin = s
d.genData(s)
return d
}
func (sm *SkinMap) genData(s Skin) {
sm.tb = NewTextureBuilder(4, 128, 128)
sm.genButtonData(s, sm.tb)
sm.genInputData(s, sm.tb)
sm.genFocusData(s, sm.tb)
sm.genInsetData(s, sm.tb)
sm.genCornerData(s, sm.tb)
sm.genBarData(s, sm.tb)
sm.genRadioOffData(s, sm.tb)
sm.genRadioOnData(s, sm.tb)
sm.genTickData(s, sm.tb)
sm.genScrollbarTrack(s, sm.tb)
if err := sm.tb.ToImage("skin.png"); err != nil {
panic(err)
}
sm.syncTextureBuilder()
}
func (sm *SkinMap) syncTextureBuilder() {
sm.data = sm.tb.data
sm.width = sm.tb.width
sm.height = sm.tb.height
sm.tb.dirty = false
}
func (sm *SkinMap) genButtonData(s Skin, tb *TextureBuilder) {
sm.buttonX, sm.buttonY, sm.buttonT = sm.genBordered(s.Button(), tb, false)
sm.buttonPressedX, sm.buttonPressedY, _ = sm.genBordered(s.ButtonPressed(), tb, true)
}
func (sm *SkinMap) genInputData(s Skin, tb *TextureBuilder) {
sm.inputX, sm.inputY, sm.inputT = sm.genBordered(s.Input(), tb, false)
}
func (sm *SkinMap) genFocusData(s Skin, tb *TextureBuilder) {
sm.focusX, sm.focusY, sm.focusT = sm.genBordered(s.Focus(), tb, false)
}
func (sm *SkinMap) genInsetData(s Skin, tb *TextureBuilder) {
sm.insetX, sm.insetY, _ = sm.genBordered(s.Inset(), tb, true)
}
func (sm *SkinMap) genCornerData(s Skin, tb *TextureBuilder) {
sm.cornerX, sm.cornerY, _ = sm.genBordered(s.Corner(), tb, true)
}
func (sm *SkinMap) genBarData(s Skin, tb *TextureBuilder) {
bar := s.Bar()
n := calcSquareSkinSize(bar)
sm.barX, sm.barY = tb.Build(bar, n, n)
sm.barT = n
}
func (sm *SkinMap) genRadioOffData(s Skin, tb *TextureBuilder) {
d := s.RadioOff()
n := calcSquareSkinSize(d)
sm.radioOffX, sm.radioOffY = tb.Build(d, n, n)
sm.radioSize = n
}
func (sm *SkinMap) genRadioOnData(s Skin, tb *TextureBuilder) {
d := s.RadioOn()
n := calcSquareSkinSize(d)
sm.radioOnX, sm.radioOnY = tb.Build(d, n, n)
if sm.radioSize != n {
panic("inconsistent radio button size")
}
}
func (sm *SkinMap) genTickData(s Skin, tb *TextureBuilder) {
d := s.Tick()
n := calcSquareSkinSize(d)
sm.tickX, sm.tickY = tb.Build(d, n, n)
sm.tickSize = n
}
func (sm *SkinMap) genScrollbarTrack(s Skin, tb *TextureBuilder) {
d := s.ScrollbarTrack()
n := len(d)/4
sm.sbTrackX, sm.sbTrackY = tb.Build(d, 1, n)
sm.sbTrackSize = n
}
func (sm *SkinMap) genBordered(d []byte, tb *TextureBuilder, checkT bool) (int, int, int) {
var t int
if checkT {
t = calcSkinThicknessCheckRef(d, sm.buttonT)
} else {
t = calcSkinThickness(d)
}
x, y := tb.BuildBordered(d, t)
return x, y, t
}
func (s *SkinMap) initGL(uTexLoc uint32, texID uint32, texUnit uint32) {
s.loc = uTexLoc
s.tid = texID
s.tunit = texUnit
checkGLError()
gl.ActiveTexture(s.tunit)
checkGLError()
gl.BindTexture(gl.TEXTURE_2D, s.tid)
checkGLError()
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST)
checkGLError()
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST)
checkGLError()
// remember: transpose for some reason
gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, int32(s.height), int32(s.width), 0, gl.RGBA,
gl.UNSIGNED_BYTE, unsafe.Pointer(&(s.data[0])))
checkGLError()
gl.BindTexture(gl.TEXTURE_2D, 0)
checkGLError()
}
func (s *SkinMap) bind() {
checkGLError()
gl.ActiveTexture(s.tunit)
gl.BindTexture(gl.TEXTURE_2D, s.tid)
if s.tb.dirty {
s.syncTextureBuilder()
// remember: transpose for some reason
gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, int32(s.height), int32(s.width), 0, gl.RGBA,
gl.UNSIGNED_BYTE, unsafe.Pointer(&(s.data[0])))
}
checkGLError()
}
func (s *SkinMap) ButtonOrigin() (int, int) {
return s.buttonX, s.buttonY
}
func (s *SkinMap) ButtonPressedOrigin() (int, int) {
return s.buttonPressedX, s.buttonPressedY
}
func (s *SkinMap) CornerOrigin() (int, int) {
return s.cornerX, s.cornerY
}
func (s *SkinMap) ButtonBorderThickness() int {
return s.buttonT
}
func (s *SkinMap) BGColor() sdl.Color {
return s.skin.BGColor()
}
func (s *SkinMap) SelColor() sdl.Color {
return s.skin.SelColor()
}
func (s *SkinMap) InputOrigin() (int, int) {
return s.inputX, s.inputY
}
func (s *SkinMap) InputBorderThickness() int {
return s.inputT
}
func (s *SkinMap) InputBGColor() sdl.Color {
i, j := s.InputOrigin()
i += s.inputT
j += s.inputT
k := i*s.height + j
r := s.data[k*4+0]
g := s.data[k*4+1]
b := s.data[k*4+2]
a := s.data[k*4+3]
return sdl.Color{r, g, b, a}
}
func (s *SkinMap) FocusOrigin() (int, int) {
return s.focusX, s.focusY
}
func (s *SkinMap) FocusThickness() int {
return s.focusT
}
func (s *SkinMap) InsetOrigin() (int, int) {
return s.insetX, s.insetY
}
func (s *SkinMap) BarOrigin() (int, int) {
return s.barX, s.barY
}
func (s *SkinMap) BarThickness() int {
return s.barT
}
func (s SkinMap) RadioOffOrigin() (int, int) {
return s.radioOffX, s.radioOffY
}
func (s SkinMap) RadioOnOrigin() (int, int) {
return s.radioOnX, s.radioOnY
}
func (s *SkinMap) RadioSize() int {
return s.radioSize
}
func (s SkinMap) TickOrigin() (int, int) {
return s.tickX, s.tickY
}
func (s *SkinMap) TickSize() int {
return s.tickSize
}
func (s *SkinMap) ScrollbarTrackOrigin() (int, int) {
return s.sbTrackX, s.sbTrackY
}
func (s *SkinMap) ScrollbarTrackSize() int {
return s.sbTrackSize
}
func (s *SkinMap) getButtonCoords() ([4]int, [4]int) {
t := s.ButtonBorderThickness()
x0, y0 := s.ButtonOrigin()
return getBorderedSkinCoords(x0, y0, t)
}
func (s *SkinMap) getCornerCoords() ([4]int, [4]int) {
t := s.ButtonBorderThickness()
x0, y0 := s.CornerOrigin()
return getBorderedSkinCoords(x0, y0, t)
}
func (s *SkinMap) getBarCoords() ([2]int, [4]int) {
t := s.BarThickness()
x0, y0 := s.BarOrigin()
var (
x [2]int
y [4]int
)
x[0] = x0
x[1] = x0 + t
dt := (t - 1)/2
y[0] = y0
y[1] = y0 + dt
y[2] = y0 + dt + 1
y[3] = y0 + t
return x, y
}
func (s *SkinMap) AllocImage(img *ImageData, infos map[*ImageData]ImageInfo) (int, int) {
someFreeableFound := true
for !s.tb.HaveFreeSpace(img.W, img.H) && someFreeableFound {
someFreeableFound = false
keys := make([]*ImageData, 0)
for k, _ := range infos {
keys = append(keys, k)
}
keys = sortImageData(keys)
for _, k := range keys {
info := infos[k]
if !info.Used && info.X >= 0 && info.Y >= 0 {
s.tb.Free(info.X, info.Y, k.W, k.H)
someFreeableFound = true
delete(infos, k)
break
}
}
}
// optionally we could defrag here
x, y := s.tb.Build(img.Pix, img.W, img.H)
return x, y
}
func (s *SkinMap) DeallocImage(x, y, w, h int) {
s.tb.Free(x, y, w, h)
}