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