-
Notifications
You must be signed in to change notification settings - Fork 0
/
extra_draw.v
378 lines (336 loc) · 8 KB
/
extra_draw.v
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
module ui
import gx
import math
import sokol.sgl
const (
empty_text_cfg = gx.TextCfg{}
)
pub fn is_empty_text_cfg(t gx.TextCfg) bool {
return t.str() == ui.empty_text_cfg.str()
}
//----- Generic for performance
// T is Widget with text_cfg field
fn text_size<T>(widget &T, text string) (int, int) {
widget.ui.gg.set_cfg(widget.text_cfg)
return widget.ui.gg.text_size(text)
}
fn text_width<T>(w &T, text string) int {
w.ui.gg.set_cfg(w.text_cfg)
return w.ui.gg.text_width(text)
}
fn text_height<T>(w &T, text string) int {
w.ui.gg.set_cfg(w.text_cfg)
return w.ui.gg.text_height(text)
}
// T is a widget Type with text_cfg field
fn draw_text<T>(w &T, x int, y int, text_ string) {
window := w.ui.window
if w.text_size > 0 {
_, win_height := window.size()
tc := gx.TextCfg{
...w.text_cfg
size: text_size_as_int(w.text_size, win_height)
}
w.ui.gg.draw_text(x, y, text_, tc)
} else {
// println("draw_text: $text_ $w.text_cfg.color")
w.ui.gg.draw_text(x, y, text_, w.text_cfg)
}
}
fn draw_text_with_color<T>(w &T, x int, y int, text_ string, color gx.Color) {
if w.text_size > 0 {
_, win_height := w.ui.window.size()
tc := gx.TextCfg{
...w.text_cfg
size: text_size_as_int(w.text_size, win_height)
color: color
}
w.ui.gg.draw_text(x, y, text_, tc)
} else {
tc := gx.TextCfg{
...w.text_cfg
color: color
}
w.ui.gg.draw_text(x, y, text_, tc)
}
}
//--------- DrawText interface (for Tooltip and Message)
// Rmk: this can be used for Widget having these fields too
interface DrawText {
ui &UI
mut:
text_cfg gx.TextCfg
text_size f64
}
fn init_text_cfg(mut w DrawText) {
if is_empty_text_cfg(w.text_cfg) {
w.text_cfg = w.ui.window.text_cfg
}
if w.text_size > 0 {
_, win_height := w.ui.window.size()
w.text_cfg = gx.TextCfg{
...w.text_cfg
size: text_size_as_int(w.text_size, win_height)
}
}
}
// No text_size to not conflict with
fn get_text_size(w DrawText, text_ string) (int, int) {
w.ui.gg.set_cfg(w.text_cfg)
return w.ui.gg.text_size(text_)
}
fn set_text_cfg_color(mut w DrawText, color gx.Color) {
w.text_cfg = gx.TextCfg{
...w.text_cfg
color: color
}
}
fn set_text_cfg_size(mut w DrawText, size int) {
w.text_cfg = gx.TextCfg{
...w.text_cfg
size: size
}
}
fn set_text_cfg_style(mut w DrawText, bold bool, italic bool, mono bool) {
w.text_cfg = gx.TextCfg{
...w.text_cfg
bold: bold
italic: italic
mono: mono
}
}
fn set_text_cfg_align(mut w DrawText, align gx.HorizontalAlign) {
w.text_cfg = gx.TextCfg{
...w.text_cfg
align: align
}
}
fn set_text_cfg_vertical_align(mut w DrawText, align gx.VerticalAlign) {
w.text_cfg = gx.TextCfg{
...w.text_cfg
vertical_align: align
}
}
// pub fn draw_text_line(w DrawText, x int, y int, text_ string) {
// w.ui.gg.draw_text(x, y, text_, w.text_cfg)
// }
// pub fn draw_text_line_with_color(w DrawText, x int, y int, text_ string, color gx.Color) {
// tc := gx.TextCfg{
// ...w.text_cfg
// color: color
// }
// w.ui.gg.draw_text(x, y, text_, tc)
// }
pub fn draw_text_lines(w DrawText, x int, y int, lines []string) {
mut th := 0
for line in lines {
w.ui.gg.draw_text(x, y + th, line, w.text_cfg)
_, tmp := get_text_size(w, line)
th += tmp
}
}
fn update_text_size(mut w DrawText) {
if w.text_size > 0 {
_, win_height := w.ui.window.size()
w.text_cfg = gx.TextCfg{
...w.text_cfg
size: text_size_as_int(w.text_size, win_height)
}
}
}
//------- Futher functions
// text_size: f64
// 0 (default) => system
// 16 (or 16.0) => fixed font size
// .5 (in ]0,1]) => proprtion of height window
pub fn text_size_as_int(size f64, win_height int) int {
return if size > 0 && size < 1 {
// println("tsai: ${int(size * win_height)} = $size * $win_height")
int(size * win_height)
} else if size == int(size) {
int(size)
} else {
0
}
}
// This a a generic function
fn point_inside<T>(w &T, x f64, y f64) bool {
wx, wy := w.x + w.offset_x, w.y + w.offset_y
return x >= wx && x <= wx + w.width && y >= wy && y <= wy + w.height
}
fn point_inside_adj<T>(w &T, x f64, y f64) bool {
wx, wy := w.x + w.offset_x, w.y + w.offset_y
return x >= wx && x <= wx + w.adj_width && y >= wy && y <= wy + w.adj_height
}
// h, s, l in [0,1]
pub fn hsv_to_rgb(h f64, s f64, v f64) gx.Color {
c := v * s
x := c * (1.0 - math.abs(math.fmod(h * 6.0, 2.0) - 1.0))
m := v - c
mut r, mut g, mut b := 0.0, 0.0, 0.0
h6 := h * 6.0
if h6 < 1.0 {
r, g = c, x
} else if h6 < 2.0 {
r, g = x, c
} else if h6 < 3.0 {
g, b = c, x
} else if h6 < 4.0 {
g, b = x, c
} else if h6 < 5.0 {
r, b = x, c
} else {
r, b = c, x
}
return gx.rgb(byte((r + m) * 255.0), byte((g + m) * 255.0), byte((b + m) * 255.0))
}
// h, s, l in [0,1]
pub fn hsl_to_rgb(h f64, s f64, l f64) gx.Color {
c := (1.0 - math.abs(2.0 * l - 1.0)) * s
x := c * (1.0 - math.abs(math.fmod(h * 6.0, 2.0) - 1.0))
m := l - c / 2.0
mut r, mut g, mut b := 0.0, 0.0, 0.0
h6 := h * 6.0
if h6 < 1.0 {
r, g = c, x
} else if h6 < 2.0 {
r, g = x, c
} else if h6 < 3.0 {
g, b = c, x
} else if h6 < 4.0 {
g, b = x, c
} else if h6 < 5.0 {
r, b = x, c
} else {
r, b = c, x
}
return gx.rgb(byte((r + m) * 255.0), byte((g + m) * 255.0), byte((b + m) * 255.0))
}
pub fn rgb_to_hsv(col gx.Color) (f64, f64, f64) {
r, g, b := f64(col.r) / 255.0, f64(col.g) / 255.0, f64(col.b) / 255.0
v, m := f64_max(f64_max(r, g), b), -f64_max(f64_max(-r, -g), -b)
d := v - m
mut h, mut s := 0.0, 0.0
if v == m {
h = 0
} else if v == r {
if g > b {
h = ((g - b) / d) / 6.0
} else {
h = (6.0 - (g - b) / d) / 6
}
} else if v == g {
h = ((b - r) / d + 2.0) / 6.0
} else if v == b {
h = ((r - g) / d + 4.0) / 6.0
}
// println("h: $h")
if v != 0 {
s = d / v
}
// mirror correction
if h > 1.0 {
h = 2.0 - h
}
return h, s, v
}
pub fn rgb_to_hsl(col gx.Color) (f64, f64, f64) {
r, g, b := f64(col.r) / 255.0, f64(col.g) / 255.0, f64(col.b) / 255.0
v, m := f64_max(f64_max(r, g), b), -f64_max(f64_max(-r, -g), -b)
d := v - m
mut h, mut s := 0.0, 0.0
if v == m {
h = 0
} else if v == r {
if g > b {
h = ((g - b) / d) / 6.0
} else {
h = (6.0 - (g - b) / d) / 6
}
} else if v == g {
h = ((b - r) / d + 2.0) / 6.0
} else if v == b {
h = ((r - g) / d + 4.0) / 6.0
}
l := (v + m) / 2.0
// println("h: $h")
if v != 0 {
s = d / (1.0 - math.abs(2 * l - 1.0))
}
return h, s, l
}
// Texture stuff borrowed from @penguindark to deal with texture in sokol
//
pub fn create_texture(w int, h int, buf &byte) C.sg_image {
mut img_desc := C.sg_image_desc{
width: w
height: h
num_mipmaps: 0
min_filter: .linear
mag_filter: .linear
wrap_u: .clamp_to_edge
wrap_v: .clamp_to_edge
label: &byte(0)
d3d11_texture: 0
}
sz := w * h * 4
img_desc.data.subimage[0][0] = C.sg_range{
ptr: buf
size: usize(sz)
}
sg_img := C.sg_make_image(&img_desc)
return sg_img
}
pub fn destroy_texture(sg_img C.sg_image) {
C.sg_destroy_image(sg_img)
}
// Dynamic texture
pub fn create_dynamic_texture(w int, h int) C.sg_image {
mut img_desc := C.sg_image_desc{
width: w
height: h
num_mipmaps: 0
min_filter: .linear
mag_filter: .linear
usage: .dynamic
wrap_u: .clamp_to_edge
wrap_v: .clamp_to_edge
label: &byte(0)
d3d11_texture: 0
}
sg_img := C.sg_make_image(&img_desc)
return sg_img
}
// Use only if usage: .dynamic is enabled
pub fn update_text_texture(sg_img C.sg_image, w int, h int, buf &byte) {
sz := w * h * 4
mut tmp_sbc := C.sg_image_data{}
tmp_sbc.subimage[0][0] = C.sg_range{
ptr: buf
size: usize(sz)
}
C.sg_update_image(sg_img, &tmp_sbc)
}
pub fn (c &CanvasLayout) draw_texture(simg C.sg_image) {
ctx := c.ui.gg
cx, cy := c.x + c.offset_x, c.y + c.offset_y
u0 := f32(0.0)
v0 := f32(0.0)
u1 := f32(1.0)
v1 := f32(1.0)
x0 := f32(cx * ctx.scale)
y0 := f32(cy * ctx.scale)
x1 := f32((cx + c.width) * ctx.scale)
y1 := f32((cy + c.height) * ctx.scale)
sgl.load_pipeline(ctx.timage_pip)
sgl.enable_texture()
sgl.texture(simg)
sgl.begin_quads()
sgl.c4b(255, 255, 255, 255)
sgl.v2f_t2f(x0, y0, u0, v0)
sgl.v2f_t2f(x1, y0, u1, v0)
sgl.v2f_t2f(x1, y1, u1, v1)
sgl.v2f_t2f(x0, y1, u0, v1)
sgl.end()
sgl.disable_texture()
}