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