bad char
[vg.git] / vg_ui.h
1 /* Copyright (C) 2021-2022 Harry Godden (hgn) - All Rights Reserved */
2
3 #ifndef VG_UI_H
4 #define VG_UI_H
5
6 #define VG_GAME
7 #include "vg/vg.h"
8 #include "vg/vg_tex.h"
9 #include "vg/vg_shader.h"
10
11 static struct vg_shader _shader_ui =
12 {
13 .name = "[vg] ui",
14 .link = NULL,
15 .vs =
16 {
17 .orig_file = NULL,
18 .static_src =
19 "layout (location=0) in vec2 a_co;"
20 "layout (location=1) in vec2 a_uv;"
21 "layout (location=2) in vec4 a_colour;"
22 "layout (location=3) in vec4 a_clip;"
23 "uniform mat3 uPv;"
24 ""
25 "out vec2 aTexCoords;"
26 "out vec4 aColour;"
27 "out vec2 aWsp;"
28 "out vec4 aClip;"
29 ""
30 "void main()"
31 "{"
32 "gl_Position = vec4( uPv * vec3( a_co, 1.0 ), 1.0 );"
33 "aTexCoords = a_uv * 0.0078125;"
34 "aColour = a_colour;"
35
36 "aWsp = a_co;"
37 "aClip = a_clip;"
38 "}",
39 },
40 .fs =
41 {
42 .orig_file = NULL,
43 .static_src =
44 "uniform sampler2D uTexGlyphs;"
45 "out vec4 FragColor;"
46 ""
47 "in vec2 aTexCoords;"
48 "in vec4 aColour;"
49 ""
50 "in vec2 aWsp;"
51 "in vec4 aClip;"
52 ""
53 "void main()"
54 "{"
55 "float clip_blend = step( aWsp.x, aClip.z ) *"
56 "step( aWsp.y, aClip.w ) *"
57 "step( aClip.x, aWsp.x ) *"
58 "step( aClip.y, aWsp.y ); "
59
60 "vec4 glyph = vec4(1.0,1.0,1.0,1.0);"
61
62 "if( aColour.a == 0.0 )"
63 "{"
64 "glyph = texture( uTexGlyphs, aTexCoords );"
65 "glyph.a = smoothstep( 0.47, 0.53, glyph.r );"
66 "}"
67 "else"
68 "{"
69 "glyph.a = aColour.a;"
70 "}"
71
72 "FragColor = vec4( aColour.rgb, glyph.a*clip_blend );"
73 "}"
74 }
75 };
76
77 static struct vg_shader _shader_ui_image =
78 {
79 .name = "[vg] ui_image",
80 .link = NULL,
81 .vs =
82 {
83 .orig_file = NULL,
84 .static_src =
85 "layout (location=0) in vec2 a_co;"
86 "layout (location=1) in vec2 a_uv;"
87 "layout (location=2) in vec4 a_colour;"
88 "layout (location=3) in vec4 a_clip;"
89 "uniform mat3 uPv;"
90 ""
91 "out vec2 aTexCoords;"
92 "out vec4 aColour;"
93 "out vec2 aWsp;"
94 "out vec4 aClip;"
95 ""
96 "void main()"
97 "{"
98 "gl_Position = vec4( uPv * vec3( a_co, 1.0 ), 1.0 );"
99 "aTexCoords = a_uv * 0.0078125;"
100 "aColour = a_colour;"
101
102 "aWsp = a_co;"
103 "aClip = a_clip;"
104 "}",
105 },
106 .fs =
107 {
108 .orig_file = NULL,
109 .static_src =
110 "uniform sampler2D uTexImage;"
111 "out vec4 FragColor;"
112 ""
113 "in vec2 aTexCoords;"
114 "in vec4 aColour;"
115 ""
116 "in vec2 aWsp;"
117 "in vec4 aClip;"
118
119 "void main()"
120 "{"
121 "float clip_blend = step( aWsp.x, aClip.z ) *"
122 "step( aWsp.y, aClip.w ) *"
123 "step( aClip.x, aWsp.x ) *"
124 "step( aClip.y, aWsp.y ); "
125
126 "vec4 colour = texture( uTexImage, aTexCoords );"
127 "float value = dot(vec4(1.0),colour)*0.25;"
128
129 "vec3 col = vec3(pow(cos(value*3.14159265*2.0)*0.5+0.5,0.5))"
130 "* vec3(step(value,0.5),0.3,step(1.0-value,0.5));"
131
132 "FragColor = vec4( col*4.0, clip_blend );"
133 "}"
134 }
135 };
136
137 typedef i16 ui_px;
138 typedef u32 ui_colour;
139 typedef ui_px ui_rect[4];
140 typedef struct ui_colourset ui_colourset;
141
142 /* Relative to cursor p0 */
143 enum ui_text_align
144 {
145 k_text_align_left = 0,
146 k_text_align_right = 1,
147 k_text_align_center = 2
148 };
149
150 struct ui_colourset
151 {
152 union
153 {
154 struct
155 {
156 ui_colour main;
157 ui_colour hover;
158 ui_colour active;
159 };
160 struct
161 {
162 ui_colour background;
163 ui_colour bar;
164 ui_colour bar_hover;
165 };
166 };
167 };
168
169 #pragma pack(push,1)
170 struct ui_vert
171 {
172 ui_px co[2];
173 u8 uv[2];
174 u32 colour;
175 ui_rect clip;
176 };
177 #pragma pack(pop)
178
179 struct
180 {
181 struct ui_qnode
182 {
183 ui_rect rect;
184 ui_colour colour;
185 int mouse_over;
186 int capture_id;
187 }
188 stack[ 32 ];
189
190 u32 control_id;
191
192 struct ui_vert *vertex_buffer;
193 u16 *indice_buffer;
194 u32 max_verts, max_indices, cur_vert, cur_indice;
195
196 ui_rect clipping;
197 ui_rect cursor;
198 u32 stack_count;
199 u32 capture_mouse_id;
200 int capture_lock;
201
202 /* User input */
203 ui_px mouse[2];
204 int click_state; /* 0: released, 1: on down, 2: pressed, 3: on release */
205
206 ui_colourset *colours;
207
208 GLuint vao;
209 GLuint vbo;
210 GLuint ebo;
211
212 struct ui_image
213 {
214 ui_rect rc;
215 GLuint image;
216 }
217 images[16];
218 int image_count;
219 }
220 static vg_uictx;
221
222 #define UI_GLYPH_SPACING_X 8
223
224 static GLuint ui_glyph_texture = 0;
225 static ui_colourset ui_default_colours = {
226 .main = 0xff807373,
227 .hover = 0xff918484,
228 .active = 0xffad9f9e
229 };
230
231 VG_STATIC void _vg_ui_init(void)
232 {
233 if( !vg_shader_compile( &_shader_ui ) ||
234 !vg_shader_compile( &_shader_ui_image ) )
235 vg_fatal_exit_loop( "Failed to compile ui shader" );
236
237 /*
238 * Vertex buffer
239 * ----------------------------------------
240 */
241
242 vg_uictx.max_indices = 20000;
243 vg_uictx.max_verts = 30000;
244 vg_uictx.colours = &ui_default_colours;
245
246 /* Generate the buffer we are gonna be drawing to */
247 glGenVertexArrays( 1, &vg_uictx.vao );
248 glGenBuffers( 1, &vg_uictx.vbo );
249 glGenBuffers( 1, &vg_uictx.ebo );
250
251 glBindVertexArray( vg_uictx.vao );
252 glBindBuffer( GL_ARRAY_BUFFER, vg_uictx.vbo );
253
254 glBufferData( GL_ARRAY_BUFFER,
255 vg_uictx.max_verts * sizeof( struct ui_vert ),
256 NULL, GL_DYNAMIC_DRAW );
257 glBindVertexArray( vg_uictx.vao );
258
259 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, vg_uictx.ebo );
260 glBufferData( GL_ELEMENT_ARRAY_BUFFER,
261 vg_uictx.max_indices * sizeof( u16 ), NULL, GL_DYNAMIC_DRAW );
262
263 VG_CHECK_GL_ERR();
264
265 /* Set pointers */
266 u32 const stride = sizeof( struct ui_vert );
267
268 /* XY */
269 glVertexAttribPointer( 0, 2, GL_SHORT, GL_FALSE,
270 stride, (void *)offsetof( struct ui_vert, co ) );
271 glEnableVertexAttribArray( 0 );
272
273 /* UV */
274 glVertexAttribPointer( 1, 2, GL_UNSIGNED_BYTE, GL_FALSE,
275 stride, (void *)offsetof( struct ui_vert, uv ) );
276 glEnableVertexAttribArray( 1 );
277
278 /* COLOUR */
279 glVertexAttribPointer( 2, 4, GL_UNSIGNED_BYTE, GL_TRUE, stride,
280 (void *)offsetof( struct ui_vert, colour ) );
281 glEnableVertexAttribArray( 2 );
282
283 /* CLIPPING */
284 glVertexAttribPointer( 3, 4, GL_SHORT, GL_FALSE, stride,
285 (void *)offsetof( struct ui_vert, clip ) );
286 glEnableVertexAttribArray( 3 );
287
288 VG_CHECK_GL_ERR();
289
290 /* Alloc RAM default context */
291 u32 vert_size = vg_uictx.max_verts*sizeof(struct ui_vert),
292 inds_size = vg_align8( vg_uictx.max_indices*sizeof(u16) );
293
294 vg_uictx.vertex_buffer = vg_linear_alloc( vg_mem.rtmemory, vert_size );
295 vg_uictx.indice_buffer = vg_linear_alloc( vg_mem.rtmemory, inds_size );
296
297 /* font
298 * -----------------------------------------------------
299 */
300
301 /* Load default font */
302 u32 compressed[] = {
303 #include "vg/vg_pxfont_thin.h"
304 };
305
306 u32 pixels = 0, total = 256*256, data = 0;
307 u8 image[256*256];
308
309 while( pixels < total )
310 {
311 for( int b = 31; b >= 0; b-- )
312 {
313 image[ pixels ++ ] = (compressed[data] & (0x1 << b))? 0xff: 0x00;
314
315 if( pixels >= total )
316 {
317 total = 0;
318 break;
319 }
320 }
321 data++;
322 }
323
324 glGenTextures( 1, &ui_glyph_texture );
325 glBindTexture( GL_TEXTURE_2D, ui_glyph_texture );
326 glTexImage2D( GL_TEXTURE_2D, 0, GL_R8, 256, 256, 0,
327 GL_RED, GL_UNSIGNED_BYTE, image );
328
329 VG_CHECK_GL_ERR();
330 vg_tex2d_clamp();
331 vg_tex2d_nearest();
332 }
333
334 static struct ui_vert *ui_fill_rect_uv( ui_rect rect, u32 colour, ui_px uv[4] );
335
336 VG_STATIC void ui_draw( m3x3f view_override )
337 {
338 u32 num_indices_normal = vg_uictx.cur_indice;
339
340 /* Append images to back of buffer */
341 for( int i = 0; i < vg_uictx.image_count; i ++ )
342 {
343 ui_fill_rect_uv( vg_uictx.images[i].rc, 0xffffffff,
344 (ui_px[4]){0,0,128,128} );
345 }
346
347 glBindVertexArray( vg_uictx.vao );
348
349 glBindBuffer( GL_ARRAY_BUFFER, vg_uictx.vbo );
350 glBufferSubData( GL_ARRAY_BUFFER, 0,
351 vg_uictx.cur_vert * sizeof( struct ui_vert ),
352 vg_uictx.vertex_buffer );
353
354 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, vg_uictx.ebo );
355 glBufferSubData( GL_ELEMENT_ARRAY_BUFFER, 0,
356 vg_uictx.cur_indice * sizeof( u16 ),
357 vg_uictx.indice_buffer );
358
359 glEnable(GL_BLEND);
360 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
361 glBlendEquation(GL_FUNC_ADD);
362
363 glUseProgram( _shader_ui.id );
364
365 m3x3f view = M3X3_IDENTITY;
366
367 if( !view_override )
368 {
369 view_override = view;
370
371 m3x3_translate( view, (v3f){ -1.0f, 1.0f, 0.0f } );
372 m3x3_scale( view, (v3f){ 1.0f/((float)vg.window_x*0.5f),
373 -1.0f/((float)vg.window_y*0.5f), 1.0f } );
374 }
375
376 /* TODO? */
377 glUniformMatrix3fv( glGetUniformLocation( _shader_ui.id, "uPv" ), 1,
378 GL_FALSE, (float *)view_override );
379
380 glActiveTexture( GL_TEXTURE0 );
381 glBindTexture( GL_TEXTURE_2D, ui_glyph_texture );
382 glUniform1i( glGetUniformLocation( _shader_ui.id, "uTexGlyphs" ), 0 );
383
384 glDrawElements( GL_TRIANGLES, num_indices_normal,
385 GL_UNSIGNED_SHORT, (void*)(0) );
386
387
388 /* images */
389 glUseProgram( _shader_ui_image.id );
390 glUniformMatrix3fv( glGetUniformLocation( _shader_ui_image.id, "uPv" ), 1,
391 GL_FALSE, (float *)view_override );
392
393 glActiveTexture( GL_TEXTURE1 );
394 glUniform1i( glGetUniformLocation( _shader_ui_image.id, "uTexImage" ), 1 );
395
396 /* Draw image elements */
397 for( int i = 0; i < vg_uictx.image_count; i ++ )
398 {
399 struct ui_image *img = &vg_uictx.images[i];
400
401 glBindTexture( GL_TEXTURE_2D, img->image );
402 glDrawElements( GL_TRIANGLES, 6, GL_UNSIGNED_SHORT,
403 (void*)( (num_indices_normal + 6*i)*sizeof(u16) ) );
404 }
405
406 glDisable(GL_BLEND);
407 }
408
409 /*
410 * Rect controls
411 */
412 VG_STATIC void ui_rect_copy( ui_rect src, ui_rect dst )
413 {
414 dst[0] = src[0];
415 dst[1] = src[1];
416 dst[2] = src[2];
417 dst[3] = src[3];
418 }
419
420 VG_STATIC void ui_rect_pad( ui_rect rect, ui_px pad )
421 {
422 rect[0] += pad;
423 rect[1] += pad;
424 rect[2] -= pad*2;
425 rect[3] -= pad*2;
426 }
427
428 /*
429 * Stack control
430 */
431 static struct ui_qnode *ui_current(void)
432 {
433 return &vg_uictx.stack[ vg_uictx.stack_count-1 ];
434 }
435
436 VG_STATIC void ui_new_node(void)
437 {
438 if( vg_uictx.stack_count == vg_list_size( vg_uictx.stack ) )
439 {
440 vg_error( "[UI] Stack overflow while creating box!" );
441 return;
442 }
443
444 struct ui_qnode *parent = &vg_uictx.stack[ vg_uictx.stack_count-1 ];
445 struct ui_qnode *node = &vg_uictx.stack[ vg_uictx.stack_count++ ];
446 ui_rect_copy( vg_uictx.cursor, node->rect );
447
448 if( parent->mouse_over )
449 {
450 if( vg_uictx.mouse[0] >= node->rect[0] &&
451 vg_uictx.mouse[0] < node->rect[0]+node->rect[2] &&
452 vg_uictx.mouse[1] >= node->rect[1] &&
453 vg_uictx.mouse[1] < node->rect[1]+node->rect[3] )
454 node->mouse_over = 1;
455 else
456 node->mouse_over = 0;
457 }
458 else
459 {
460 node->mouse_over = 0;
461 }
462 }
463
464 static int ui_hasmouse(void)
465 {
466 struct ui_qnode *node = ui_current();
467 return (node->mouse_over && (node->capture_id == vg_uictx.capture_mouse_id));
468 }
469
470 VG_STATIC void ui_end(void)
471 {
472 struct ui_qnode *node = &vg_uictx.stack[ --vg_uictx.stack_count ];
473 ui_rect_copy( node->rect, vg_uictx.cursor );
474 }
475
476 VG_STATIC void ui_end_down(void)
477 {
478 ui_px height = ui_current()->rect[3];
479 ui_end();
480 vg_uictx.cursor[1] += height;
481 }
482
483 VG_STATIC void ui_end_right(void)
484 {
485 ui_px width = ui_current()->rect[2];
486 ui_end();
487 vg_uictx.cursor[0] += width;
488 }
489
490 VG_STATIC void ui_fill_y(void)
491 {
492 struct ui_qnode *node = ui_current();
493 vg_uictx.cursor[3] = node->rect[3] - (vg_uictx.cursor[1]-node->rect[1]);
494 }
495
496 VG_STATIC void ui_fill_x(void)
497 {
498 struct ui_qnode *node = ui_current();
499 vg_uictx.cursor[2] = node->rect[2] - (vg_uictx.cursor[0]-node->rect[0]);
500 }
501
502 VG_STATIC void ui_align_bottom(void)
503 {
504 struct ui_qnode *node = ui_current();
505 vg_uictx.cursor[1] = node->rect[1] + node->rect[3] - vg_uictx.cursor[3];
506 }
507
508 VG_STATIC void ui_align_right(void)
509 {
510 struct ui_qnode *node = ui_current();
511 vg_uictx.cursor[0] = node->rect[0] + node->rect[2] - vg_uictx.cursor[2];
512 }
513
514 VG_STATIC void ui_align_top(void)
515 {
516 vg_uictx.cursor[1] = ui_current()->rect[1];
517 }
518
519 VG_STATIC void ui_align_left(void)
520 {
521 vg_uictx.cursor[0] = ui_current()->rect[0];
522 }
523
524 VG_STATIC void ui_clamp_rect( ui_rect parent, ui_rect dest )
525 {
526 dest[0] = vg_min( parent[0] + parent[2] - dest[2], dest[0] );
527 dest[1] = vg_min( parent[1] + parent[3] - dest[3], dest[1] );
528 dest[0] = vg_max( parent[0], dest[0] );
529 dest[1] = vg_max( parent[1], dest[1] );
530 }
531
532 VG_STATIC void ui_capture_mouse( u32 id )
533 {
534 struct ui_qnode *node = &vg_uictx.stack[ vg_uictx.stack_count-1 ];
535 node->capture_id = id;
536
537 if( !vg_uictx.capture_lock && node->mouse_over )
538 {
539 vg_uictx.capture_mouse_id = id;
540 }
541 }
542
543 static int ui_want_mouse(void)
544 {
545 return vg_uictx.capture_mouse_id == 0? 0: 1;
546 }
547
548 VG_STATIC void ui_set_clip( ui_rect clip )
549 {
550 vg_uictx.clipping[0] = clip[0];
551 vg_uictx.clipping[1] = clip[1];
552 vg_uictx.clipping[2] = clip[0] + clip[2];
553 vg_uictx.clipping[3] = clip[1] + clip[3];
554 }
555
556 VG_STATIC void ui_release_clip(void)
557 {
558 vg_uictx.clipping[0] = -32000;
559 vg_uictx.clipping[1] = -32000;
560 vg_uictx.clipping[2] = 32000;
561 vg_uictx.clipping[3] = 32000;
562 }
563
564 static struct ui_vert *ui_fill_rect_uv( ui_rect rect, u32 colour, ui_px uv[4] )
565 {
566 /* this if far from ideal but stops us from crashing */
567 if( (vg_uictx.cur_vert + 6 > vg_uictx.max_verts) ||
568 (vg_uictx.cur_indice + 4 > vg_uictx.max_indices))
569 return vg_uictx.vertex_buffer;
570
571 struct ui_vert *vertices = &vg_uictx.vertex_buffer[ vg_uictx.cur_vert ];
572 u16 *indices = &vg_uictx.indice_buffer[ vg_uictx.cur_indice ];
573
574 vertices[0].co[0] = rect[0];
575 vertices[0].co[1] = rect[1];
576 vertices[0].uv[0] = uv[0];
577 vertices[0].uv[1] = uv[1];
578 vertices[0].colour = colour;
579 vertices[1].co[0] = rect[0]+rect[2];
580 vertices[1].co[1] = rect[1];
581 vertices[1].uv[0] = uv[2];
582 vertices[1].uv[1] = uv[1];
583 vertices[1].colour = colour;
584 vertices[2].co[0] = rect[0]+rect[2];
585 vertices[2].co[1] = rect[1]+rect[3];
586 vertices[2].uv[0] = uv[2];
587 vertices[2].uv[1] = uv[3];
588 vertices[2].colour = colour;
589 vertices[3].co[0] = rect[0];
590 vertices[3].co[1] = rect[1]+rect[3];
591 vertices[3].uv[0] = uv[0];
592 vertices[3].uv[1] = uv[3];
593 vertices[3].colour = colour;
594 u16 ind_start = vg_uictx.cur_vert;
595
596 ui_rect_copy( vg_uictx.clipping, vertices[0].clip );
597 ui_rect_copy( vg_uictx.clipping, vertices[1].clip );
598 ui_rect_copy( vg_uictx.clipping, vertices[2].clip );
599 ui_rect_copy( vg_uictx.clipping, vertices[3].clip );
600
601 indices[0] = ind_start+0;
602 indices[1] = ind_start+2;
603 indices[2] = ind_start+1;
604
605 indices[3] = ind_start+0;
606 indices[4] = ind_start+3;
607 indices[5] = ind_start+2;
608
609 vg_uictx.cur_indice += 6;
610 vg_uictx.cur_vert += 4;
611
612 return vertices;
613 }
614
615 static struct ui_vert *ui_fill_rect( ui_rect rect, u32 colour )
616 {
617 return ui_fill_rect_uv( rect, colour, (ui_px[4]){ 4,4, 4,4 } );
618 }
619
620 static ui_px ui_text_line_offset( const char *str, ui_px scale,
621 enum ui_text_align align )
622 {
623 if( align == k_text_align_left )
624 return 0;
625
626 int length = 0;
627 const char *_c = str;
628 char c;
629
630 while( (c = *(_c ++)) )
631 if( c >= 32 && c <= 126 )
632 length ++;
633 else if( c == '\n' )
634 break;
635
636 if( align == k_text_align_right )
637 return -length * scale*8;
638 else
639 return (-length * scale*8) / 2;
640 }
641
642 VG_STATIC void ui_text( ui_px pos[2],
643 const char *str, ui_px scale, enum ui_text_align align )
644 {
645 ui_rect text_cursor;
646 u32 current_colour = 0x00ffffff;
647
648 const char *_c = str;
649 u8 c;
650
651 text_cursor[0] = pos[0] + ui_text_line_offset( str, scale, align );
652 text_cursor[1] = pos[1];
653 text_cursor[2] = 8*scale;
654 text_cursor[3] = 14*scale;
655
656 while( (c = *(_c ++)) )
657 {
658 if( c == '\n' )
659 {
660 text_cursor[1] += 14*scale;
661 text_cursor[0] = pos[0] + ui_text_line_offset( _c, scale, align );
662 continue;
663 }
664 else if( c >= 33 )
665 {
666 u8 glyph_base[2];
667 u8 glyph_index = c;
668 glyph_base[0] = glyph_index & 0xf;
669 glyph_base[1] = (glyph_index-glyph_base[0])>>4;
670
671 glyph_base[0] *= 8;
672 glyph_base[1] *= 8;
673
674 ui_fill_rect_uv( text_cursor, current_colour, (ui_px[4])
675 {
676 glyph_base[0]+2,
677 glyph_base[1]+1,
678 glyph_base[0]+6,
679 glyph_base[1]+8
680 });
681 }
682 else if( c == '\x1B' )
683 {
684 _c ++;
685 u16 colour_id = 0;
686 for( int i = 0; i < 3; i ++ )
687 {
688 if( _c[i] )
689 {
690 if( _c[i] == 'm' )
691 {
692 _c = _c + i + 1;
693
694 switch( colour_id )
695 {
696 case '0': current_colour = 0x00ffffff; break;
697 case '3'|'1'<<8: current_colour = 0x00201fee; break;
698 case '3'|'2'<<8: current_colour = 0x0037e420; break;
699 case '3'|'3'<<8: current_colour = 0x000ed8e2; break;
700 case '3'|'4'<<8: current_colour = 0x00f15010; break;
701 case '3'|'5'<<8: current_colour = 0x00ee20ee; break;
702 case '3'|'6'<<8: current_colour = 0x00eeee20; break;
703 case '3'|'7'<<8: current_colour = 0x00ffffff; break;
704 }
705
706 break;
707 }
708
709 colour_id |= _c[i] << (i*8);
710 }
711 else
712 {
713 _c = _c +i;
714 break;
715 }
716 }
717
718 continue;
719 }
720 else if( c == '\t' )
721 {
722 text_cursor[0] += UI_GLYPH_SPACING_X*scale*4;
723 continue;
724 }
725
726 text_cursor[0] += UI_GLYPH_SPACING_X*scale;
727 }
728 }
729
730 /*
731 * API Control
732 */
733 VG_STATIC void ui_begin( ui_px res_x, ui_px res_y )
734 {
735 vg_uictx.cursor[0] = 0;
736 vg_uictx.cursor[1] = 0;
737 vg_uictx.cursor[2] = res_x;
738 vg_uictx.cursor[3] = res_y;
739
740 ui_rect_copy( vg_uictx.cursor, vg_uictx.stack[0].rect );
741 vg_uictx.stack[0].mouse_over = 1;
742
743 vg_uictx.stack_count = 1;
744
745 vg_uictx.cur_vert = 0;
746 vg_uictx.cur_indice = 0;
747
748 ui_release_clip();
749
750 if( vg_uictx.click_state == 0 )
751 vg_uictx.capture_mouse_id = 0;
752
753 vg_uictx.image_count = 0;
754 }
755
756 VG_STATIC void ui_resolve(void)
757 {
758 if( vg_uictx.stack_count-1 )
759 {
760 vg_error( "[UI] Mismatched node create/drestroy!" );
761 return;
762 }
763
764 if( vg_uictx.click_state == 3 || vg_uictx.click_state == 0 )
765 {
766 vg_uictx.capture_lock = 0;
767 }
768 }
769
770 VG_STATIC void ui_set_mouse( int x, int y, int click_state )
771 {
772 vg_uictx.mouse[0] = x;
773 vg_uictx.mouse[1] = y;
774
775 vg_uictx.click_state = click_state;
776 }
777
778 /*
779 * High level controls
780 */
781 struct ui_window
782 {
783 const char *title;
784 ui_rect transform;
785
786 int drag;
787 ui_px drag_offset[2];
788 };
789
790 enum button_state
791 {
792 k_button_released = 0,
793 k_button_start_click,
794 k_button_click,
795 k_button_hold
796 };
797
798 static int ui_button(void)
799 {
800 u32 uid = vg_uictx.control_id ++;
801
802 ui_new_node();
803 {
804 ui_capture_mouse( uid );
805
806 if( ui_hasmouse() )
807 {
808 ui_fill_rect( vg_uictx.cursor, vg_uictx.colours->hover );
809
810 if( vg_uictx.click_state == 1 )
811 {
812 vg_uictx.capture_lock = 1;
813 return k_button_start_click;
814 }
815 else if( vg_uictx.capture_lock && vg_uictx.click_state == 3 )
816 return k_button_click;
817 else if( vg_uictx.capture_lock && vg_uictx.click_state == 2 )
818 return k_button_hold;
819
820 return k_button_click;
821 }
822 else
823 ui_fill_rect( vg_uictx.cursor, vg_uictx.colours->main );
824 }
825
826 return k_button_released;
827 }
828
829 static int ui_window( struct ui_window *window, u32 control_group )
830 {
831 if( window->drag )
832 {
833 window->transform[0] = vg_uictx.mouse[0]+window->drag_offset[0];
834 window->transform[1] = vg_uictx.mouse[1]+window->drag_offset[1];
835
836 ui_clamp_rect( vg_uictx.stack[0].rect, window->transform );
837
838 if( vg_uictx.click_state == 0 || vg_uictx.click_state == 3 )
839 {
840 window->drag = 0;
841 }
842 }
843
844 ui_rect_copy( window->transform, vg_uictx.cursor );
845
846 ui_new_node();
847 {
848 ui_capture_mouse( vg_uictx.control_id ++ );
849
850 /* Drag bar */
851 vg_uictx.cursor[3] = 25;
852 ui_new_node();
853 {
854 ui_capture_mouse( vg_uictx.control_id ++ );
855
856 struct ui_vert *drag_bar = ui_fill_rect( vg_uictx.cursor, 0xff555555 );
857
858 /* title.. */
859 vg_uictx.cursor[0] += 2;
860 vg_uictx.cursor[1] += 2;
861 ui_text( vg_uictx.cursor, window->title, 2, 0 );
862
863 /* Close button */
864 vg_uictx.cursor[3] = 25;
865 vg_uictx.cursor[2] = 25;
866 ui_align_right();
867 ui_align_top();
868 ui_rect_pad( vg_uictx.cursor, 4 );
869
870 if( ui_button() )
871 {
872 vg_info( "Click clacked\n" );
873 }
874 vg_uictx.cursor[0] += 2;
875 ui_text( vg_uictx.cursor, "x", 2, 0 );
876 ui_end();
877
878 if( ui_hasmouse() )
879 {
880 drag_bar[0].colour = 0xff777777;
881 drag_bar[1].colour = 0xff777777;
882 drag_bar[2].colour = 0xff777777;
883 drag_bar[3].colour = 0xff777777;
884
885 /* start drag */
886 if( vg_uictx.click_state == 1 )
887 {
888 window->drag = 1;
889 window->drag_offset[0] = window->transform[0]-vg_uictx.mouse[0];
890 window->drag_offset[1] = window->transform[1]-vg_uictx.mouse[1];
891 }
892 }
893 }
894 ui_end_down();
895 }
896
897 return 1;
898 }
899
900 VG_STATIC void ui_push_image( ui_rect rc, GLuint image )
901 {
902 struct ui_image *img = &vg_uictx.images[ vg_uictx.image_count ++ ];
903 ui_rect_copy( rc, img->rc );
904 img->image = image;
905 }
906
907 struct ui_slider
908 {
909 float *data;
910 float min, max;
911 };
912
913 struct ui_slider_vector
914 {
915 float *data, min, max;
916 struct ui_slider sub[4];
917 u32 len;
918 };
919
920 struct ui_checkbox
921 {
922 int *data;
923 };
924
925 VG_STATIC void ui_slider( struct ui_slider *slider )
926 {
927 ui_new_node();
928
929 ui_px slider_start = vg_uictx.cursor[0];
930
931 float const ftotal = vg_uictx.cursor[2],
932 fwidth = ftotal*0.25f,
933 fmove = ftotal - fwidth,
934 fstart = fwidth*0.5f,
935 frange = slider->max-slider->min,
936 fpos = (*slider->data - slider->min) / frange;
937
938 ui_fill_rect( vg_uictx.cursor, 0xff111111 );
939 vg_uictx.cursor[2] = fwidth;
940 vg_uictx.cursor[0] = slider_start + fpos * fmove;
941
942 u32 uid = vg_uictx.control_id ++;
943 int status = ui_button();
944 if( vg_uictx.capture_lock && (vg_uictx.capture_mouse_id == uid))
945 {
946 float ui_new = vg_uictx.mouse[0],
947 local = ui_new - (slider_start + fstart),
948 zo = vg_clampf(local / fmove,0.0f,1.0f);
949
950 *slider->data = vg_lerpf( slider->min, slider->max, zo );
951 }
952
953 vg_uictx.cursor[0] += 4;
954 vg_uictx.cursor[1] += 4;
955
956 char buf[12];
957 snprintf( buf, 12, "%.2f", *slider->data );
958 ui_text( vg_uictx.cursor, buf, 1, 0 );
959 ui_end_down();
960 ui_end_down();
961 }
962
963 VG_STATIC void ui_slider_vector( struct ui_slider_vector *slider )
964 {
965 for( int i=0; i<slider->len; i++ )
966 {
967 slider->sub[i].data = &slider->data[i];
968 slider->sub[i].min = slider->min;
969 slider->sub[i].max = slider->max;
970 ui_slider( &slider->sub[i] );
971 }
972 }
973
974 VG_STATIC void ui_checkbox( struct ui_checkbox *cb )
975 {
976 if( ui_button() == k_button_click )
977 *cb->data ^= 0x1;
978
979 ui_new_node();
980 ui_rect_pad( vg_uictx.cursor, 4 );
981 if( *cb->data )
982 ui_fill_rect( vg_uictx.cursor, 0xff00e052 );
983 else
984 ui_fill_rect( vg_uictx.cursor, 0xff0052e0 );
985
986 ui_end();
987 ui_end_down();
988 }
989
990 #endif /* VG_UI_H */