level selector
[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 = aColour * vec4( 1.0, 1.0, 1.0, glyph.r * 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 u8 uv[2]; //16 2
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
116 // User input
117 ui_px mouse[2];
118 int click_state; // 0: released, 1: on down, 2: pressed, 3: on release
119
120 ui_colourset *colours_main;
121 ui_colourset *colours_current;
122 };
123
124 // Shortnames
125 #define gui_draw(...) ui_draw( &ui_global_ctx, __VA_ARGS__)
126 #define gui_current(...) ui_current( &ui_global_ctx, __VA_ARGS__)
127 #define gui_new_node() ui_new_node( &ui_global_ctx )
128 #define gui_hasmouse(...) ui_hasmouse( &ui_global_ctx, __VA_ARGS__)
129 #define gui_end() ui_end( &ui_global_ctx )
130 #define gui_end_down() ui_end_down( &ui_global_ctx )
131 #define gui_end_right() ui_end_right( &ui_global_ctx )
132 #define gui_fill_y() ui_fill_y( &ui_global_ctx)
133 #define gui_fill_x() ui_fill_x( &ui_global_ctx)
134 #define gui_align_bottom() ui_align_bottom( &ui_global_ctx )
135 #define gui_align_right() ui_align_right( &ui_global_ctx )
136 #define gui_align_top() ui_align_top( &ui_global_ctx )
137 #define gui_align_left() ui_align_left( &ui_global_ctx )
138 #define gui_clamp_rect(...) ui_clamp_rect( &ui_global_ctx, __VA_ARGS__)
139 #define gui_group_id(...) ui_group_id( &ui_global_ctx, __VA_ARGS__)
140 #define gui_capture_mouse(...) ui_capture_mouse( &ui_global_ctx, __VA_ARGS__)
141 #define gui_set_clip(...) ui_set_clip( &ui_global_ctx, __VA_ARGS__)
142 #define gui_release_clip() ui_release_clip( &ui_global_ctx )
143 #define gui_fill_rect_uv(...) ui_fill_rect_uv( &ui_global_ctx, __VA_ARGS__)
144 #define gui_fill_rect(...) ui_fill_rect( &ui_global_ctx, __VA_ARGS__)
145 #define gui_text(...) ui_text( &ui_global_ctx, __VA_ARGS__)
146 #define gui_begin(...) ui_begin( &ui_global_ctx, __VA_ARGS__)
147 #define gui_resolve(...) ui_resolve( &ui_global_ctx, __VA_ARGS__)
148 #define gui_set_mouse(...) ui_set_mouse( &ui_global_ctx, __VA_ARGS__)
149 #define gui_button(...) ui_button( &ui_global_ctx, __VA_ARGS__)
150 #define gui_window(...) ui_window( &ui_global_ctx, __VA_ARGS__)
151 #define gui_want_mouse() ui_want_mouse( &ui_global_ctx )
152
153 #define gui_scrollbar(...) ui_scrollbar( &ui_global_ctx, __VA_ARGS__)
154 #define gui_override_colours(...) ui_override_colours( &ui_global_ctx, __VA_ARGS__)
155 #define gui_reset_colours(...) ui_reset_colours( &ui_global_ctx )
156
157 // Globals
158 // ===========================================================================================================
159
160 // Opengl
161 int ui_glyph_override = 0;
162 ui_px ui_glyph_spacing_x = 6;
163 GLuint ui_glyph_texture = 0;
164 GLuint ui_vao;
165 GLuint ui_vbo;
166 GLuint ui_ebo;
167
168 #define UI_BUFFER_SIZE 30000
169 #define UI_INDEX_SIZE 20000
170
171 ui_colourset ui_default_colours = {
172 .main = 0xff00ff00,
173 .hover = 0xffff00ff,
174 .active = 0xffff0000
175 };
176 ui_ctx ui_global_ctx = {
177 .padding = 8,
178 .colours_current = &ui_default_colours,
179 .colours_main = &ui_default_colours
180 };
181
182
183 // Initialization
184 // ===========================================================================================================
185
186 static void ui_override_font( GLuint new_tex, ui_px space_x )
187 {
188 if( ui_glyph_texture )
189 glDeleteTextures( 1, &ui_glyph_texture );
190
191 ui_glyph_texture = new_tex;
192 ui_glyph_override = 1;
193 ui_glyph_spacing_x = space_x;
194 }
195
196 static void ui_default_init(void)
197 {
198 // Load default font
199 if( !ui_glyph_override )
200 {
201 u32 compressed[] = {
202 #include "fonts/weiholmir.h"
203 };
204
205 u32 pixels = 0, total = 72*72, data = 0;
206 u8 *image = malloc( total );
207
208 while( pixels < total )
209 {
210 for( int b = 31; b >= 0; b-- )
211 {
212 image[ pixels ++ ] = (compressed[data] & (0x1 << b))? 0xff: 0x00;
213
214 if( pixels >= total )
215 {
216 total = 0;
217 break;
218 }
219 }
220 data++;
221 }
222
223 glGenTextures( 1, &ui_glyph_texture );
224 glBindTexture( GL_TEXTURE_2D, ui_glyph_texture );
225
226 glTexImage2D( GL_TEXTURE_2D, 0, GL_R8, 72, 72, 0, GL_RED, GL_UNSIGNED_BYTE, image );
227
228 vg_tex2d_clamp();
229 vg_tex2d_nearest();
230
231 free( image );
232 }
233
234 // Setup OpenGL memory
235 {
236 SHADER_INIT( shader_ui );
237
238 // Generate the buffer we are gonna be drawing to
239 glGenVertexArrays(1, &ui_vao);
240 glGenBuffers( 1, &ui_vbo );
241 glGenBuffers( 1, &ui_ebo );
242 glBindVertexArray( ui_vao );
243
244 glBindBuffer( GL_ARRAY_BUFFER, ui_vbo );
245
246 glBufferData( GL_ARRAY_BUFFER, UI_BUFFER_SIZE * sizeof( struct ui_vert ), NULL, GL_DYNAMIC_DRAW );
247 glBindVertexArray( ui_vao );
248
249 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, ui_ebo );
250 glBufferData( GL_ELEMENT_ARRAY_BUFFER, UI_INDEX_SIZE * sizeof( u16 ), NULL, GL_DYNAMIC_DRAW );
251
252 u32 const stride = sizeof( struct ui_vert );
253
254 // XY
255 glVertexAttribPointer( 0, 2, GL_SHORT, GL_FALSE, stride, (void *)offsetof( struct ui_vert, co ) );
256 glEnableVertexAttribArray( 0 );
257
258 // UV
259 glVertexAttribPointer( 1, 2, GL_UNSIGNED_BYTE, GL_FALSE, stride, (void *)offsetof( struct ui_vert, uv ) );
260 glEnableVertexAttribArray( 1 );
261
262 // COLOUR
263 glVertexAttribPointer( 2, 4, GL_UNSIGNED_BYTE, GL_TRUE, stride, (void *)offsetof( struct ui_vert, colour ) );
264 glEnableVertexAttribArray( 2 );
265
266 // CLIPPING
267 glVertexAttribPointer( 3, 4, GL_SHORT, GL_FALSE, stride, (void *)offsetof( struct ui_vert, clip ) );
268 glEnableVertexAttribArray( 3 );
269 }
270
271 // Initialize default context
272 {
273 ui_global_ctx.verts = (struct ui_vert *)malloc( UI_BUFFER_SIZE * sizeof(struct ui_vert) );
274 ui_global_ctx.indices = (u16*)malloc( UI_INDEX_SIZE * sizeof(u16) );
275 }
276 }
277
278 static void ui_default_free(void)
279 {
280 if( !ui_glyph_override )
281 glDeleteTextures( 1, &ui_glyph_texture );
282
283 glDeleteVertexArrays( 1, &ui_vao );
284 glDeleteBuffers( 1, &ui_vbo );
285 glDeleteBuffers( 1, &ui_ebo );
286
287 free( ui_global_ctx.verts );
288 free( ui_global_ctx.indices );
289 }
290
291 static void ui_draw( ui_ctx *ctx, m3x3f view_override )
292 {
293 glBindVertexArray( ui_vao );
294
295 glBindBuffer( GL_ARRAY_BUFFER, ui_vbo );
296 glBufferSubData( GL_ARRAY_BUFFER, 0, ctx->num_verts * sizeof( struct ui_vert ), ctx->verts );
297
298 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, ui_ebo );
299 glBufferSubData( GL_ELEMENT_ARRAY_BUFFER, 0, ctx->num_indices * sizeof( u16 ), ctx->indices );
300
301 glEnable(GL_BLEND);
302 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
303 glBlendEquation(GL_FUNC_ADD);
304
305 SHADER_USE( shader_ui );
306
307 m3x3f view = M3X3_IDENTITY;
308
309 if( !view_override )
310 {
311 view_override = view;
312
313 m3x3_translate( view, (v3f){ -1.0f, 1.0f, 0.0f } );
314 m3x3_scale( view, (v3f){ 1.0f/((float)vg_window_x*0.5f), -1.0f/((float)vg_window_y*0.5f), 1.0f } );
315 }
316
317 glUniformMatrix3fv( SHADER_UNIFORM( shader_ui, "uPv" ), 1, GL_FALSE, (float *)view_override );
318
319 glActiveTexture( GL_TEXTURE0 );
320 glBindTexture( GL_TEXTURE_2D, ui_glyph_texture );
321 glUniform1i( SHADER_UNIFORM( shader_ui, "uTexGlyphs" ), 0 );
322
323 glDrawElements( GL_TRIANGLES, ctx->num_indices, GL_UNSIGNED_SHORT, (void*)(0) );
324
325 //vg_info( "Verts: %u, Indices: %u\n", ctx->num_verts, ctx->num_indices );
326
327 glDisable(GL_BLEND);
328 }
329
330 // Rect controls
331 // ===========================================================================================================
332
333 static void ui_rect_copy( ui_rect src, ui_rect dst )
334 {
335 dst[0] = src[0];
336 dst[1] = src[1];
337 dst[2] = src[2];
338 dst[3] = src[3];
339 }
340
341 static void ui_rect_pad( ui_rect rect, ui_px pad )
342 {
343 rect[0] += pad;
344 rect[1] += pad;
345 rect[2] -= pad*2;
346 rect[3] -= pad*2;
347 }
348
349 static void ui_vis_rect( ui_rect rect, u32 colour )
350 {
351 #ifdef UI_DEBUG
352 v2f p0;
353 v2f p1;
354
355 p0[0] = rect[0];
356 p0[1] = rect[1];
357 p1[0] = rect[0]+rect[2];
358 p1[1] = rect[1]+rect[3];
359
360 vg_line( p0, (v2f){p1[0],p0[1]}, colour );
361 vg_line( (v2f){p1[0],p0[1]}, p1, colour );
362 vg_line( p1, (v2f){p0[0],p1[1]}, colour );
363 vg_line( (v2f){p0[0],p1[1]}, p0, colour );
364 #endif
365 }
366
367 // Stack control
368 // ===========================================================================================================
369
370 static struct ui_qnode *ui_current( ui_ctx *ctx )
371 {
372 return &ctx->stack[ ctx->stack_count-1 ];
373 }
374
375 static void ui_new_node( ui_ctx *ctx )
376 {
377 if( ctx->stack_count == vg_list_size( ctx->stack ) )
378 vg_exiterr( "[UI] Stack overflow while creating box!" );
379
380 struct ui_qnode *parent = &ctx->stack[ ctx->stack_count-1 ];
381 struct ui_qnode *node = &ctx->stack[ ctx->stack_count++ ];
382 ui_rect_copy( ctx->cursor, node->rect );
383
384 if( parent->mouse_over )
385 {
386 if( ctx->mouse[0] >= node->rect[0] && ctx->mouse[0] <= node->rect[0]+node->rect[2] &&
387 ctx->mouse[1] >= node->rect[1] && ctx->mouse[1] <= node->rect[1]+node->rect[3] )
388 node->mouse_over = 1;
389 else
390 node->mouse_over = 0;
391 }
392 else
393 {
394 node->mouse_over = 0;
395 }
396 }
397
398 static int ui_hasmouse( ui_ctx *ctx )
399 {
400 struct ui_qnode *node = ui_current( ctx );
401 return (node->mouse_over && (node->capture_id == ctx->capture_mouse_id));
402 }
403
404 static void ui_end( ui_ctx *ctx )
405 {
406 struct ui_qnode *node = &ctx->stack[ --ctx->stack_count ];
407
408 ui_rect_copy( node->rect, ctx->cursor );
409 ui_vis_rect( ctx->cursor,
410 (node->mouse_over && (node->capture_id == ctx->capture_mouse_id))? 0xffff0000: 0xff0000ff );
411 }
412
413 static void ui_end_down( ui_ctx *ctx )
414 {
415 ui_px height = ui_current( ctx )->rect[3];
416 ui_end( ctx );
417 ctx->cursor[1] += height;
418 }
419
420 static void ui_end_right( ui_ctx *ctx )
421 {
422 ui_px width = ui_current( ctx )->rect[2];
423 ui_end( ctx );
424 ctx->cursor[0] += width;
425 }
426
427 static void ui_fill_y( ui_ctx *ctx )
428 {
429 struct ui_qnode *node = ui_current( ctx );
430 ctx->cursor[3] = node->rect[3] - (ctx->cursor[1]-node->rect[1]);
431 }
432
433 static void ui_fill_x( ui_ctx *ctx )
434 {
435 struct ui_qnode *node = ui_current( ctx );
436 ctx->cursor[2] = node->rect[2] - (ctx->cursor[0]-node->rect[0]);
437 }
438
439 // Alignment: | [] | -> | []|
440 static void ui_align_bottom( ui_ctx *ctx )
441 {
442 struct ui_qnode *node = ui_current( ctx );
443 ctx->cursor[1] = node->rect[1] + node->rect[3] - ctx->cursor[3];
444 }
445
446 static void ui_align_right( ui_ctx *ctx )
447 {
448 struct ui_qnode *node = ui_current( ctx );
449 ctx->cursor[0] = node->rect[0] + node->rect[2] - ctx->cursor[2];
450 }
451
452 static void ui_align_top( ui_ctx *ctx )
453 {
454 ctx->cursor[1] = ui_current( ctx )->rect[1];
455 }
456
457 static void ui_align_left( ui_ctx *ctx )
458 {
459 ctx->cursor[0] = ui_current( ctx )->rect[0];
460 }
461
462 static void ui_clamp_rect( ui_rect parent, ui_rect dest )
463 {
464 dest[0] = vg_min( parent[0] + parent[2] - dest[2], dest[0] );
465 dest[1] = vg_min( parent[1] + parent[3] - dest[3], dest[1] );
466 dest[0] = vg_max( parent[0], dest[0] );
467 dest[1] = vg_max( parent[1], dest[1] );
468 }
469
470 static u32 ui_group_id( ui_ctx *ctx, u32 lesser_unique )
471 {
472 return ctx->id_base | lesser_unique;
473 }
474
475 static void ui_capture_mouse( ui_ctx *ctx, u32 id )
476 {
477 u32 group_uid = ui_group_id(ctx,id);
478
479 struct ui_qnode *node = &ctx->stack[ ctx->stack_count-1 ];
480 node->capture_id = group_uid;
481
482 if( !ctx->capture_lock && node->mouse_over )
483 {
484 ctx->capture_mouse_id = group_uid;
485 }
486 }
487
488 static int ui_want_mouse( ui_ctx *ctx )
489 {
490 return ctx->capture_mouse_id == 0? 0: 1;
491 }
492
493 static void ui_set_clip( ui_ctx *ctx, ui_rect clip )
494 {
495 ctx->clipping[0] = clip[0];
496 ctx->clipping[1] = clip[1];
497 ctx->clipping[2] = clip[0] + clip[2];
498 ctx->clipping[3] = clip[1] + clip[3];
499 }
500
501 static void ui_release_clip( ui_ctx *ctx )
502 {
503 ctx->clipping[0] = -32000;
504 ctx->clipping[1] = -32000;
505 ctx->clipping[2] = 32000;
506 ctx->clipping[3] = 32000;
507 }
508
509 // Drawing
510 // ===========================================================================================================
511
512 static struct ui_vert *ui_fill_rect_uv( ui_ctx *ctx, ui_rect rect, u32 colour, ui_px uv[4] )
513 {
514 struct ui_vert *vertices = &ctx->verts[ ctx->num_verts ];
515 vertices[0].co[0] = rect[0];
516 vertices[0].co[1] = rect[1];
517 vertices[0].uv[0] = uv[0];
518 vertices[0].uv[1] = uv[1];
519 vertices[0].colour = colour;
520 vertices[1].co[0] = rect[0]+rect[2];
521 vertices[1].co[1] = rect[1];
522 vertices[1].uv[0] = uv[2];
523 vertices[1].uv[1] = uv[1];
524 vertices[1].colour = colour;
525 vertices[2].co[0] = rect[0]+rect[2];
526 vertices[2].co[1] = rect[1]+rect[3];
527 vertices[2].uv[0] = uv[2];
528 vertices[2].uv[1] = uv[3];
529 vertices[2].colour = colour;
530 vertices[3].co[0] = rect[0];
531 vertices[3].co[1] = rect[1]+rect[3];
532 vertices[3].uv[0] = uv[0];
533 vertices[3].uv[1] = uv[3];
534 vertices[3].colour = colour;
535 u16 ind_start = ctx->num_verts;
536 u16 *indices = &ctx->indices[ ctx->num_indices ];
537
538 ui_rect_copy( ctx->clipping, vertices[0].clip );
539 ui_rect_copy( ctx->clipping, vertices[1].clip );
540 ui_rect_copy( ctx->clipping, vertices[2].clip );
541 ui_rect_copy( ctx->clipping, vertices[3].clip );
542
543 indices[0] = ind_start+0;
544 indices[1] = ind_start+2;
545 indices[2] = ind_start+1;
546
547 indices[3] = ind_start+0;
548 indices[4] = ind_start+3;
549 indices[5] = ind_start+2;
550
551 ctx->num_indices += 6;
552 ctx->num_verts += 4;
553
554 return vertices;
555 }
556
557 static struct ui_vert *ui_fill_rect( ui_ctx *ctx, ui_rect rect, u32 colour )
558 {
559 return ui_fill_rect_uv( ctx, rect, colour, (ui_px[4]){ 4,124,4,124 } );
560 }
561
562 static void ui_text( ui_ctx *ctx, const char *str, ui_px scale, int alignment )
563 {
564 ui_rect text_cursor;
565
566 text_cursor[0] = ctx->cursor[0];
567 text_cursor[1] = ctx->cursor[1];
568 text_cursor[2] = scale*8;
569 text_cursor[3] = scale*8;
570
571 u32 current_colour = ctx->override_colour;
572
573 const char *_c = str;
574 char c;
575 while( (c = *(_c ++)) )
576 {
577 if( c == '\n' )
578 {
579 text_cursor[1] += 10*scale;
580 text_cursor[0] = ctx->cursor[0];
581 continue;
582 }
583 else if( c >= 33 && c <= 126 )
584 {
585 u8 glyph_base[2];
586 u8 glyph_index = c - 32;
587 glyph_base[0] = glyph_index&0xf;
588 glyph_base[1] = (glyph_index-glyph_base[0])>>4;
589
590 glyph_base[0] *= 8;
591 glyph_base[1] *= 8;
592
593 ui_fill_rect_uv( ctx, text_cursor, current_colour,
594 (ui_px[4]){
595 glyph_base[0],
596 128-glyph_base[1],
597 glyph_base[0]+8,
598 128-(glyph_base[1]+8)
599 });
600 }
601 else if( c == '\x1B' )
602 {
603 _c ++;
604 u16 colour_id = 0;
605 for( int i = 0; i < 3; i ++ )
606 {
607 if( _c[i] )
608 {
609 if( _c[i] == 'm' )
610 {
611 _c = _c + i + 1;
612
613 switch( colour_id )
614 {
615 case '0': current_colour = 0xffffffff; break;
616 case '3'|'1'<<8: current_colour = 0xff201fee; break;
617 case '3'|'2'<<8: current_colour = 0xff37e420; break;
618 case '3'|'3'<<8: current_colour = 0xff0ed8e2; break;
619 case '3'|'4'<<8: current_colour = 0xfff15010; break;
620 case '3'|'5'<<8: current_colour = 0xffee20ee; break;
621 case '3'|'6'<<8: current_colour = 0xffeeee20; break;
622 case '3'|'7'<<8: current_colour = 0xffffffff; break;
623 }
624
625 break;
626 }
627
628 colour_id |= _c[i] << (i*8);
629 }
630 else
631 {
632 _c = _c +i;
633 break;
634 }
635 }
636 continue;
637 }
638
639 text_cursor[0] += (ui_glyph_spacing_x*scale)/2;
640 }
641 }
642
643 // API control
644 // ====================================================================
645
646 static void ui_begin( ui_ctx *ctx, ui_px res_x, ui_px res_y )
647 {
648 ctx->cursor[0] = 0;
649 ctx->cursor[1] = 0;
650 ctx->cursor[2] = res_x;
651 ctx->cursor[3] = res_y;
652
653 ui_rect_copy( ctx->cursor, ctx->stack[0].rect );
654 ctx->stack[0].mouse_over = 1;
655
656 ctx->stack_count = 1;
657
658 ctx->num_verts = 0;
659 ctx->num_indices = 0;
660
661 ui_release_clip( ctx );
662
663 if( ctx->click_state == 0 )
664 ctx->capture_mouse_id = 0;
665 }
666
667 static void ui_resolve( ui_ctx *ctx )
668 {
669 if( ctx->stack_count-1 )
670 vg_exiterr( "[UI] Mismatched node create/drestroy!" );
671
672 if( ctx->click_state == 3 || ctx->click_state == 0 )
673 {
674 ctx->capture_lock = 0;
675 }
676 }
677
678 // User Input piping
679 // ====================================================================
680
681 static void ui_set_mouse( ui_ctx *ctx, int x, int y, int click_state )
682 {
683 ctx->mouse[0] = x;
684 ctx->mouse[1] = y;
685
686 ctx->click_state = click_state;
687 }
688
689 // High level controls
690 // ====================================================================
691
692 struct ui_window
693 {
694 const char *title;
695 ui_rect transform;
696
697 int drag;
698 ui_px drag_offset[2];
699 };
700
701 enum button_state
702 {
703 k_button_released = 0,
704 k_button_start_click,
705 k_button_click,
706 k_button_hold
707 };
708
709 static int ui_button( ui_ctx *ctx, u32 id )
710 {
711 ui_new_node( ctx );
712 {
713 ui_capture_mouse( ctx, id );
714
715 if( ui_hasmouse(ctx) )
716 {
717 ui_fill_rect( ctx, ctx->cursor, ctx->colours_current->hover );
718
719 if( ctx->click_state == 1 )
720 {
721 ctx->capture_lock = 1;
722 return k_button_start_click;
723 }
724 else if( ctx->capture_lock && ctx->click_state == 3 )
725 return k_button_click;
726 else if( ctx->capture_lock && ctx->click_state == 2 )
727 return k_button_hold;
728 }
729 else
730 ui_fill_rect( ctx, ctx->cursor, ctx->colours_current->main );
731 }
732
733 return k_button_released;
734 }
735
736 static int ui_window( ui_ctx *ctx, struct ui_window *window, u32 control_group )
737 {
738 ctx->id_base = control_group << 16;
739
740 if( window->drag )
741 {
742 window->transform[0] = ctx->mouse[0]+window->drag_offset[0];
743 window->transform[1] = ctx->mouse[1]+window->drag_offset[1];
744
745 ui_clamp_rect( ctx->stack[0].rect, window->transform );
746
747 if( ctx->click_state == 0 || ctx->click_state == 3 )
748 {
749 window->drag = 0;
750 }
751 }
752
753 ui_rect_copy( window->transform, ctx->cursor );
754
755 ui_new_node( ctx );
756 {
757 ui_capture_mouse( ctx, __COUNTER__ );
758
759 // Drag bar
760 ctx->cursor[3] = 25;
761 ui_new_node( ctx );
762 {
763 ui_capture_mouse( ctx, __COUNTER__ );
764
765 struct ui_vert *drag_bar = ui_fill_rect( ctx, ctx->cursor, 0xff555555 );
766
767 // title..
768 ctx->cursor[0] += 2;
769 ctx->cursor[1] += 2;
770 ui_text( ctx, window->title, 2, 0 );
771
772 // Close button
773 ctx->cursor[3] = 25;
774 ctx->cursor[2] = 25;
775 ui_align_right( ctx );
776 ui_align_top( ctx );
777 ui_rect_pad( ctx->cursor, 4 );
778
779 if( ui_button( ctx, __COUNTER__ ) )
780 {
781 vg_info( "Click clacked\n" );
782 }
783 ctx->cursor[0] += 2;
784 ui_text( ctx, "x", 2, 0 );
785 ui_end( ctx );
786
787 if( ui_hasmouse( ctx ) )
788 {
789 drag_bar[0].colour = 0xff777777;
790 drag_bar[1].colour = 0xff777777;
791 drag_bar[2].colour = 0xff777777;
792 drag_bar[3].colour = 0xff777777;
793
794 // start drag
795 if( ctx->click_state == 1 )
796 {
797 window->drag = 1;
798 window->drag_offset[0] = window->transform[0]-ctx->mouse[0];
799 window->drag_offset[1] = window->transform[1]-ctx->mouse[1];
800 }
801 }
802 }
803 ui_end_down( ctx );
804 }
805
806 return 1;
807 }
808
809 struct ui_scrollbar
810 {
811 int drag;
812 ui_px drag_offset;
813
814 ui_px py;
815 ui_px bar_height;
816 ui_px view_height;
817 };
818
819 static void ui_scrollbar( ui_ctx *ctx, struct ui_scrollbar *scrollbar, u32 id )
820 {
821 scrollbar->view_height = ctx->cursor[3];
822
823 if( scrollbar->drag )
824 {
825 scrollbar->py = ctx->mouse[1]+scrollbar->drag_offset;
826 scrollbar->py = VG_MAX( scrollbar->py, 0 );
827 scrollbar->py = VG_MIN( scrollbar->py, ctx->cursor[3] - scrollbar->bar_height );
828
829 if( ctx->click_state == 0 || ctx->click_state == 3 )
830 scrollbar->drag = 0;
831 }
832
833 ui_new_node( ctx );
834 {
835 ui_fill_rect( ctx, ctx->cursor, ctx->colours_current->background );
836 ui_capture_mouse( ctx, id );
837
838 ctx->cursor[1] += scrollbar->py;
839 ctx->cursor[3] = scrollbar->bar_height;
840
841 ui_new_node( ctx );
842 {
843 ui_capture_mouse( ctx, __COUNTER__ );
844 struct ui_vert *drag_bar = ui_fill_rect( ctx, ctx->cursor, ctx->colours_current->bar );
845
846 if( ui_hasmouse( ctx ) || scrollbar->drag )
847 {
848 drag_bar[0].colour = ctx->colours_current->bar_hover;
849 drag_bar[1].colour = ctx->colours_current->bar_hover;
850 drag_bar[2].colour = ctx->colours_current->bar_hover;
851 drag_bar[3].colour = ctx->colours_current->bar_hover;
852
853 // start drag
854 if( ctx->click_state == 1 )
855 {
856 scrollbar->drag = 1;
857 scrollbar->drag_offset = scrollbar->py - ctx->mouse[1];
858 }
859 }
860 }
861 ui_end_down( ctx );
862 }
863 ui_end( ctx );
864 }
865
866 static ui_px ui_calculate_content_scroll( struct ui_scrollbar *scrollbar, ui_px content )
867 {
868 float overlap = vg_maxf( 0.0f, (float)(content - scrollbar->view_height) );
869
870 float range = scrollbar->view_height - scrollbar->bar_height;
871 return ((float)scrollbar->py / range) * overlap;
872 }
873
874 static void ui_override_colours( ui_ctx *ctx, ui_colourset *set )
875 {
876 ctx->colours_current = set;
877 }
878
879 static void ui_reset_colours( ui_ctx *ctx )
880 {
881 ctx->colours_current = ctx->colours_main;
882 ctx->override_colour = 0xffffffff;
883 }