-
Notifications
You must be signed in to change notification settings - Fork 158
Expand file tree
/
Copy pathextra_draw.v
More file actions
402 lines (360 loc) · 8.89 KB
/
extra_draw.v
File metadata and controls
402 lines (360 loc) · 8.89 KB
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
394
395
396
397
398
399
400
401
402
module ui
import gg
import math
import sokol.sgl
import sokol.gfx
// const (
// empty_text_cfg = gg.TextCfg{}
// )
// pub fn is_empty_text_cfg(t gg.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.dd.set_text_cfg(widget.text_cfg)
// return widget.ui.dd.text_size(text)
// }
// fn text_width<T>(w &T, text string) int {
// w.ui.dd.set_text_cfg(w.text_cfg)
// return w.ui.dd.text_width(text)
// }
// fn text_height<T>(w &T, text string) int {
// w.ui.dd.set_text_cfg(w.text_cfg)
// return w.ui.dd.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 := gg.TextCfg{
// ...w.text_cfg
// size: text_size_as_int(w.text_size, win_height)
// }
// w.ui.dd.draw_text(x, y, text_, tc)
// } else {
// // println("draw_text: $text_ $w.text_cfg.color")
// w.ui.dd.draw_text(x, y, text_, w.text_cfg)
// }
// }
// fn draw_text_with_color<T>(w &T, x int, y int, text_ string, color gg.Color) {
// if w.text_size > 0 {
// _, win_height := w.ui.window.size()
// tc := gg.TextCfg{
// ...w.text_cfg
// size: text_size_as_int(w.text_size, win_height)
// color: color
// }
// w.ui.dd.draw_text(x, y, text_, tc)
// } else {
// tc := gg.TextCfg{
// ...w.text_cfg
// color: color
// }
// w.ui.dd.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 gg.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 = gg.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) {
dd := w.ui.dd
dd.set_text_cfg(w.text_cfg)
return dd.text_size(text_)
}
fn set_text_cfg_color(mut w DrawText, color gg.Color) {
w.text_cfg = gg.TextCfg{
...w.text_cfg
color: color
}
}
fn set_text_cfg_size(mut w DrawText, size int) {
w.text_cfg = gg.TextCfg{
...w.text_cfg
size: size
}
}
fn set_text_cfg_style(mut w DrawText, bold bool, italic bool, mono bool) {
w.text_cfg = gg.TextCfg{
...w.text_cfg
bold: bold
italic: italic
mono: mono
}
}
fn set_text_cfg_align(mut w DrawText, align gg.HorizontalAlign) {
w.text_cfg = gg.TextCfg{
...w.text_cfg
align: align
}
}
fn set_text_cfg_vertical_align(mut w DrawText, align gg.VerticalAlign) {
w.text_cfg = gg.TextCfg{
...w.text_cfg
vertical_align: align
}
}
// pub fn draw_text_line(w DrawText, x int, y int, text_ string) {
// w.ui.dd.draw_text(x, y, text_, w.text_cfg)
// }
// pub fn draw_text_line_with_color(w DrawText, x int, y int, text_ string, color gg.Color) {
// tc := gg.TextCfg{
// ...w.text_cfg
// color: color
// }
// w.ui.dd.draw_text(x, y, text_, tc)
// }
// TODO: documentation
pub fn draw_text_lines(w DrawText, x int, y int, lines []string) {
mut th := 0
dd := w.ui.dd
for line in lines {
dd.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 = gg.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
}
// h, s, l in [0,1]
pub fn hsv_to_rgb(h f64, s f64, v f64) gg.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 gg.rgb(u8((r + m) * 255.0), u8((g + m) * 255.0), u8((b + m) * 255.0))
}
// h, s, l in [0,1]
pub fn hsl_to_rgb(h f64, s f64, l f64) gg.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 gg.rgb(u8((r + m) * 255.0), u8((g + m) * 255.0), u8((b + m) * 255.0))
}
// TODO: documentation
pub fn rgb_to_hsv(col gg.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
}
// TODO: documentation
pub fn rgb_to_hsl(col gg.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
//
// TODO: documentation
pub fn create_texture(w int, h int, buf &u8) 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: &u8(unsafe { nil })
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
}
// TODO: documentation
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
pixel_format: .rgba8
num_mipmaps: 1
num_slices: 1
usage: .dynamic
label: c'a dynamic texture'
}
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 &u8) {
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)
}
@[params]
pub struct StreamingImageConfig {
pub:
wrap_u gfx.Wrap = .clamp_to_edge
wrap_v gfx.Wrap = .clamp_to_edge
min_filter gfx.Filter = .linear
mag_filter gfx.Filter = .linear
num_mipmaps int = 1
num_slices int = 1
}
pub fn create_image_sampler(sicfg StreamingImageConfig) gfx.Sampler {
mut smp_desc := gfx.SamplerDesc{
wrap_u: sicfg.wrap_u
wrap_v: sicfg.wrap_v
min_filter: sicfg.min_filter
mag_filter: sicfg.mag_filter
}
return gfx.make_sampler(&smp_desc)
}
// REMOVED: this function uses internals of gg.Context which we probably do not
// want to reproduce in the DrawDevice interface
pub fn (c &CanvasLayout) draw_texture(simg gfx.Image, sampler gfx.Sampler) {
ctx := c.ui.dd
if ctx is DrawDeviceContext {
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.pipeline.alpha)
sgl.enable_texture()
sgl.texture(simg, sampler)
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()
}
}