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