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