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