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