03647392a27ebe8cfce24e7f82b891197720066f
[fishladder.git] / vg / vg_ui.h
1 // Copyright (C) 2021 Harry Godden (hgn) - All Rights Reserved
2
3 SHADER_DEFINE( shader_ui,
4
5 // VERTEX
6 "layout (location=0) in vec2 a_co;" // i16, i16, .. ?
7 "layout (location=1) in vec2 a_uv;" // i8, i8
8 "layout (location=2) in vec4 a_colour;" // u32
9 "layout (location=3) in vec4 a_clip;" // i16, i16, i16, i16
10 "uniform mat3 uPv;"
11 ""
12 "out vec2 aTexCoords;"
13 "out vec4 aColour;"
14 "out vec2 aWsp;"
15 "out vec4 aClip;"
16 ""
17 "void main()"
18 "{"
19 "gl_Position = vec4( uPv * vec3( a_co, 1.0 ), 1.0 );"
20 "aTexCoords = a_uv * 0.0078125;"
21 "aColour = a_colour;"
22
23 "aWsp = a_co;"
24 "aClip = a_clip;"
25 "}",
26
27 // FRAGMENT
28 "uniform sampler2D uTexGlyphs;"
29 "out vec4 FragColor;"
30 ""
31 "in vec2 aTexCoords;"
32 "in vec4 aColour;"
33 ""
34 "in vec2 aWsp;"
35 "in vec4 aClip;"
36 ""
37 "void main()"
38 "{"
39 "float clip_blend = step( aWsp.x, aClip.z ) * step( aWsp.y, aClip.w ) * step( aClip.x, aWsp.x ) * step( aClip.y, aWsp.y );"
40
41 "vec4 glyph = texture( uTexGlyphs, aTexCoords );"
42 "FragColor = aColour * vec4( 1.0, 1.0, 1.0, glyph.r * clip_blend );"
43 "}"
44 ,
45 UNIFORMS({ "uPv", "uTexGlyphs" })
46 )
47
48 #define UI_AUTO_FILL 0
49 //#define UI_DEBUG
50
51 // Types
52 // ===========================================================================================================
53
54 typedef i16 ui_px;
55 typedef u32 ui_colour;
56 typedef ui_px ui_rect[4];
57 typedef struct ui_ctx ui_ctx;
58 typedef struct ui_colourset ui_colourset;
59
60 struct ui_colourset
61 {
62 union
63 {
64 struct
65 {
66 ui_colour main;
67 ui_colour hover;
68 ui_colour active;
69 };
70 struct
71 {
72 ui_colour background;
73 ui_colour bar;
74 ui_colour bar_hover;
75 };
76 };
77 };
78
79 struct ui_ctx
80 {
81 ui_px padding;
82
83 struct ui_qnode
84 {
85 ui_rect rect;
86 ui_colour colour;
87 int mouse_over;
88 int capture_id;
89 }
90 stack[ 32 ];
91
92 #pragma pack(push,1)
93 struct ui_vert
94 {
95 ui_px co[2]; //32 4
96 u8 uv[2]; //16 2
97 u32 colour; //32 4
98 ui_rect clip; //64 8
99 }
100 *verts;
101 #pragma pack(pop)
102
103 u32 override_colour;
104
105 u32 num_verts;
106 u16 *indices;
107 u32 num_indices;
108
109 ui_rect clipping;
110 ui_rect cursor;
111 u32 stack_count;
112 u32 capture_mouse_id;
113 int capture_lock;
114 u32 id_base;
115 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
125 // Shortnames
126 #define gui_draw(...) ui_draw( &ui_global_ctx, __VA_ARGS__)
127 #define gui_current(...) ui_current( &ui_global_ctx, __VA_ARGS__)
128 #define gui_new_node() ui_new_node( &ui_global_ctx )
129 #define gui_hasmouse(...) ui_hasmouse( &ui_global_ctx, __VA_ARGS__)
130 #define gui_end() ui_end( &ui_global_ctx )
131 #define gui_end_down() ui_end_down( &ui_global_ctx )
132 #define gui_end_right() ui_end_right( &ui_global_ctx )
133 #define gui_fill_y() ui_fill_y( &ui_global_ctx)
134 #define gui_fill_x() ui_fill_x( &ui_global_ctx)
135 #define gui_align_bottom() ui_align_bottom( &ui_global_ctx )
136 #define gui_align_right() ui_align_right( &ui_global_ctx )
137 #define gui_align_top() ui_align_top( &ui_global_ctx )
138 #define gui_align_left() ui_align_left( &ui_global_ctx )
139 #define gui_clamp_rect(...) ui_clamp_rect( &ui_global_ctx, __VA_ARGS__)
140 #define gui_group_id(...) ui_group_id( &ui_global_ctx, __VA_ARGS__)
141 #define gui_capture_mouse(...) ui_capture_mouse( &ui_global_ctx, __VA_ARGS__)
142 #define gui_set_clip(...) ui_set_clip( &ui_global_ctx, __VA_ARGS__)
143 #define gui_release_clip() ui_release_clip( &ui_global_ctx )
144 #define gui_fill_rect_uv(...) ui_fill_rect_uv( &ui_global_ctx, __VA_ARGS__)
145 #define gui_fill_rect(...) ui_fill_rect( &ui_global_ctx, __VA_ARGS__)
146 #define gui_text(...) ui_text( &ui_global_ctx, __VA_ARGS__)
147 #define gui_begin(...) ui_begin( &ui_global_ctx, __VA_ARGS__)
148 #define gui_resolve(...) ui_resolve( &ui_global_ctx, __VA_ARGS__)
149 #define gui_set_mouse(...) ui_set_mouse( &ui_global_ctx, __VA_ARGS__)
150 #define gui_button(...) ui_button( &ui_global_ctx, __VA_ARGS__)
151 #define gui_window(...) ui_window( &ui_global_ctx, __VA_ARGS__)
152 #define gui_want_mouse() ui_want_mouse( &ui_global_ctx )
153
154 #define gui_scrollbar(...) ui_scrollbar( &ui_global_ctx, __VA_ARGS__)
155 #define gui_override_colours(...) ui_override_colours( &ui_global_ctx, __VA_ARGS__)
156 #define gui_reset_colours(...) ui_reset_colours( &ui_global_ctx )
157
158 // Globals
159 // ===========================================================================================================
160
161 // Opengl
162 int ui_glyph_override = 0;
163 ui_px ui_glyph_spacing_x = 6;
164 GLuint ui_glyph_texture = 0;
165 GLuint ui_vao;
166 GLuint ui_vbo;
167 GLuint ui_ebo;
168
169 #define UI_BUFFER_SIZE 30000
170 #define UI_INDEX_SIZE 20000
171
172 ui_colourset ui_default_colours = {
173 .main = 0xff00ff00,
174 .hover = 0xffff00ff,
175 .active = 0xffff0000
176 };
177 ui_ctx ui_global_ctx = {
178 .padding = 8,
179 .colours_current = &ui_default_colours,
180 .colours_main = &ui_default_colours
181 };
182
183
184 // Initialization
185 // ===========================================================================================================
186
187 static void ui_override_font( GLuint new_tex, ui_px space_x )
188 {
189 if( ui_glyph_texture )
190 glDeleteTextures( 1, &ui_glyph_texture );
191
192 ui_glyph_texture = new_tex;
193 ui_glyph_override = 1;
194 ui_glyph_spacing_x = space_x;
195 }
196
197 static void ui_default_init(void)
198 {
199 // Load default font
200 if( !ui_glyph_override )
201 {
202 u32 compressed[] = {
203 #include "fonts/weiholmir.h"
204 };
205
206 u32 pixels = 0, total = 72*72, data = 0;
207 u8 *image = malloc( total );
208
209 while( pixels < total )
210 {
211 for( int b = 31; b >= 0; b-- )
212 {
213 image[ pixels ++ ] = (compressed[data] & (0x1 << b))? 0xff: 0x00;
214
215 if( pixels >= total )
216 {
217 total = 0;
218 break;
219 }
220 }
221 data++;
222 }
223
224 glGenTextures( 1, &ui_glyph_texture );
225 glBindTexture( GL_TEXTURE_2D, ui_glyph_texture );
226
227 glTexImage2D( GL_TEXTURE_2D, 0, GL_R8, 72, 72, 0, GL_RED, GL_UNSIGNED_BYTE, image );
228
229 vg_tex2d_clamp();
230 vg_tex2d_nearest();
231
232 free( image );
233 }
234
235 // Setup OpenGL memory
236 {
237 SHADER_INIT( shader_ui );
238
239 // Generate the buffer we are gonna be drawing to
240 glGenVertexArrays(1, &ui_vao);
241 glGenBuffers( 1, &ui_vbo );
242 glGenBuffers( 1, &ui_ebo );
243 glBindVertexArray( ui_vao );
244
245 glBindBuffer( GL_ARRAY_BUFFER, ui_vbo );
246
247 glBufferData( GL_ARRAY_BUFFER, UI_BUFFER_SIZE * sizeof( struct ui_vert ), NULL, GL_DYNAMIC_DRAW );
248 glBindVertexArray( ui_vao );
249
250 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, ui_ebo );
251 glBufferData( GL_ELEMENT_ARRAY_BUFFER, UI_INDEX_SIZE * sizeof( u16 ), NULL, GL_DYNAMIC_DRAW );
252
253 u32 const stride = sizeof( struct ui_vert );
254
255 // XY
256 glVertexAttribPointer( 0, 2, GL_SHORT, GL_FALSE, stride, (void *)offsetof( struct ui_vert, co ) );
257 glEnableVertexAttribArray( 0 );
258
259 // UV
260 glVertexAttribPointer( 1, 2, GL_UNSIGNED_BYTE, GL_FALSE, stride, (void *)offsetof( struct ui_vert, uv ) );
261 glEnableVertexAttribArray( 1 );
262
263 // COLOUR
264 glVertexAttribPointer( 2, 4, GL_UNSIGNED_BYTE, GL_TRUE, stride, (void *)offsetof( struct ui_vert, colour ) );
265 glEnableVertexAttribArray( 2 );
266
267 // CLIPPING
268 glVertexAttribPointer( 3, 4, GL_SHORT, GL_FALSE, stride, (void *)offsetof( struct ui_vert, clip ) );
269 glEnableVertexAttribArray( 3 );
270 }
271
272 // Initialize default context
273 {
274 ui_global_ctx.verts = (struct ui_vert *)malloc( UI_BUFFER_SIZE * sizeof(struct ui_vert) );
275 ui_global_ctx.indices = (u16*)malloc( UI_INDEX_SIZE * sizeof(u16) );
276 }
277 }
278
279 static void ui_default_free(void)
280 {
281 if( !ui_glyph_override )
282 glDeleteTextures( 1, &ui_glyph_texture );
283
284 glDeleteVertexArrays( 1, &ui_vao );
285 glDeleteBuffers( 1, &ui_vbo );
286 glDeleteBuffers( 1, &ui_ebo );
287
288 free( ui_global_ctx.verts );
289 free( ui_global_ctx.indices );
290 }
291
292 static void ui_draw( ui_ctx *ctx, m3x3f view_override )
293 {
294 glBindVertexArray( ui_vao );
295
296 glBindBuffer( GL_ARRAY_BUFFER, ui_vbo );
297 glBufferSubData( GL_ARRAY_BUFFER, 0, ctx->num_verts * sizeof( struct ui_vert ), ctx->verts );
298
299 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, ui_ebo );
300 glBufferSubData( GL_ELEMENT_ARRAY_BUFFER, 0, ctx->num_indices * sizeof( u16 ), ctx->indices );
301
302 glEnable(GL_BLEND);
303 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
304 glBlendEquation(GL_FUNC_ADD);
305
306 SHADER_USE( shader_ui );
307
308 m3x3f view = M3X3_IDENTITY;
309
310 if( !view_override )
311 {
312 view_override = view;
313
314 m3x3_translate( view, (v3f){ -1.0f, 1.0f, 0.0f } );
315 m3x3_scale( view, (v3f){ 1.0f/((float)vg_window_x*0.5f), -1.0f/((float)vg_window_y*0.5f), 1.0f } );
316 }
317
318 glUniformMatrix3fv( SHADER_UNIFORM( shader_ui, "uPv" ), 1, GL_FALSE, (float *)view_override );
319
320 glActiveTexture( GL_TEXTURE0 );
321 glBindTexture( GL_TEXTURE_2D, ui_glyph_texture );
322 glUniform1i( SHADER_UNIFORM( shader_ui, "uTexGlyphs" ), 0 );
323
324 glDrawElements( GL_TRIANGLES, ctx->num_indices, GL_UNSIGNED_SHORT, (void*)(0) );
325
326 //vg_info( "Verts: %u, Indices: %u\n", ctx->num_verts, ctx->num_indices );
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 };
578
579 static void ui_text( ui_ctx *ctx, const char *str, ui_px scale, enum text_alignment alignment )
580 {
581 ui_rect text_cursor;
582
583 text_cursor[0] = ctx->cursor[0];
584 text_cursor[1] = ctx->cursor[1];
585 text_cursor[2] = (scale*8)/2;
586 text_cursor[3] = (scale*8)/2;
587
588 u32 current_colour = ctx->override_colour;
589
590 const char *_c = str;
591 char c;
592 while( (c = *(_c ++)) )
593 {
594 if( c == '\n' )
595 {
596 text_cursor[1] += (7*scale)/2;
597 text_cursor[0] = ctx->cursor[0];
598 continue;
599 }
600 else if( c >= 33 && c <= 126 )
601 {
602 u8 glyph_base[2];
603 u8 glyph_index = c - 32;
604 glyph_base[0] = (glyph_index&0xf);
605 glyph_base[1] = ctx->glyph_base + ((glyph_index-glyph_base[0])>>4);
606
607 glyph_base[0] *= 8;
608 glyph_base[1] *= 8;
609
610 ui_fill_rect_uv( ctx, text_cursor, current_colour,
611 (ui_px[4]){
612 glyph_base[0],
613 128-glyph_base[1],
614 glyph_base[0]+8,
615 128-(glyph_base[1]+8)
616 });
617 }
618 else if( c == '\x1B' )
619 {
620 _c ++;
621 u16 colour_id = 0;
622 for( int i = 0; i < 3; i ++ )
623 {
624 if( _c[i] )
625 {
626 if( _c[i] == 'm' )
627 {
628 _c = _c + i + 1;
629
630 switch( colour_id )
631 {
632 case '0': current_colour = 0xffffffff; break;
633 case '3'|'1'<<8: current_colour = 0xff201fee; break;
634 case '3'|'2'<<8: current_colour = 0xff37e420; break;
635 case '3'|'3'<<8: current_colour = 0xff0ed8e2; break;
636 case '3'|'4'<<8: current_colour = 0xfff15010; break;
637 case '3'|'5'<<8: current_colour = 0xffee20ee; break;
638 case '3'|'6'<<8: current_colour = 0xffeeee20; break;
639 case '3'|'7'<<8: current_colour = 0xffffffff; break;
640 }
641
642 break;
643 }
644
645 colour_id |= _c[i] << (i*8);
646 }
647 else
648 {
649 _c = _c +i;
650 break;
651 }
652 }
653 continue;
654 }
655
656 text_cursor[0] += (ui_glyph_spacing_x*scale)/4;
657 }
658 }
659
660 // API control
661 // ====================================================================
662
663 static void ui_begin( ui_ctx *ctx, ui_px res_x, ui_px res_y )
664 {
665 ctx->cursor[0] = 0;
666 ctx->cursor[1] = 0;
667 ctx->cursor[2] = res_x;
668 ctx->cursor[3] = res_y;
669
670 ui_rect_copy( ctx->cursor, ctx->stack[0].rect );
671 ctx->stack[0].mouse_over = 1;
672
673 ctx->stack_count = 1;
674
675 ctx->num_verts = 0;
676 ctx->num_indices = 0;
677
678 ui_release_clip( ctx );
679
680 if( ctx->click_state == 0 )
681 ctx->capture_mouse_id = 0;
682 }
683
684 static void ui_resolve( ui_ctx *ctx )
685 {
686 if( ctx->stack_count-1 )
687 vg_exiterr( "[UI] Mismatched node create/drestroy!" );
688
689 if( ctx->click_state == 3 || ctx->click_state == 0 )
690 {
691 ctx->capture_lock = 0;
692 }
693 }
694
695 // User Input piping
696 // ====================================================================
697
698 static void ui_set_mouse( ui_ctx *ctx, int x, int y, int click_state )
699 {
700 ctx->mouse[0] = x;
701 ctx->mouse[1] = y;
702
703 ctx->click_state = click_state;
704 }
705
706 // High level controls
707 // ====================================================================
708
709 struct ui_window
710 {
711 const char *title;
712 ui_rect transform;
713
714 int drag;
715 ui_px drag_offset[2];
716 };
717
718 enum button_state
719 {
720 k_button_released = 0,
721 k_button_start_click,
722 k_button_click,
723 k_button_hold
724 };
725
726 static int ui_button( ui_ctx *ctx, u32 id )
727 {
728 ui_new_node( ctx );
729 {
730 ui_capture_mouse( ctx, id );
731
732 if( ui_hasmouse(ctx) )
733 {
734 ui_fill_rect( ctx, ctx->cursor, ctx->colours_current->hover );
735
736 if( ctx->click_state == 1 )
737 {
738 ctx->capture_lock = 1;
739 return k_button_start_click;
740 }
741 else if( ctx->capture_lock && ctx->click_state == 3 )
742 return k_button_click;
743 else if( ctx->capture_lock && ctx->click_state == 2 )
744 return k_button_hold;
745 }
746 else
747 ui_fill_rect( ctx, ctx->cursor, ctx->colours_current->main );
748 }
749
750 return k_button_released;
751 }
752
753 static int ui_window( ui_ctx *ctx, struct ui_window *window, u32 control_group )
754 {
755 ctx->id_base = control_group << 16;
756
757 if( window->drag )
758 {
759 window->transform[0] = ctx->mouse[0]+window->drag_offset[0];
760 window->transform[1] = ctx->mouse[1]+window->drag_offset[1];
761
762 ui_clamp_rect( ctx->stack[0].rect, window->transform );
763
764 if( ctx->click_state == 0 || ctx->click_state == 3 )
765 {
766 window->drag = 0;
767 }
768 }
769
770 ui_rect_copy( window->transform, ctx->cursor );
771
772 ui_new_node( ctx );
773 {
774 ui_capture_mouse( ctx, __COUNTER__ );
775
776 // Drag bar
777 ctx->cursor[3] = 25;
778 ui_new_node( ctx );
779 {
780 ui_capture_mouse( ctx, __COUNTER__ );
781
782 struct ui_vert *drag_bar = ui_fill_rect( ctx, ctx->cursor, 0xff555555 );
783
784 // title..
785 ctx->cursor[0] += 2;
786 ctx->cursor[1] += 2;
787 ui_text( ctx, window->title, 2, 0 );
788
789 // Close button
790 ctx->cursor[3] = 25;
791 ctx->cursor[2] = 25;
792 ui_align_right( ctx );
793 ui_align_top( ctx );
794 ui_rect_pad( ctx->cursor, 4 );
795
796 if( ui_button( ctx, __COUNTER__ ) )
797 {
798 vg_info( "Click clacked\n" );
799 }
800 ctx->cursor[0] += 2;
801 ui_text( ctx, "x", 2, 0 );
802 ui_end( ctx );
803
804 if( ui_hasmouse( ctx ) )
805 {
806 drag_bar[0].colour = 0xff777777;
807 drag_bar[1].colour = 0xff777777;
808 drag_bar[2].colour = 0xff777777;
809 drag_bar[3].colour = 0xff777777;
810
811 // start drag
812 if( ctx->click_state == 1 )
813 {
814 window->drag = 1;
815 window->drag_offset[0] = window->transform[0]-ctx->mouse[0];
816 window->drag_offset[1] = window->transform[1]-ctx->mouse[1];
817 }
818 }
819 }
820 ui_end_down( ctx );
821 }
822
823 return 1;
824 }
825
826 struct ui_scrollbar
827 {
828 int drag;
829 ui_px drag_offset;
830
831 ui_px py;
832 ui_px bar_height;
833 ui_px view_height;
834 };
835
836 static void ui_scrollbar( ui_ctx *ctx, struct ui_scrollbar *scrollbar, u32 id )
837 {
838 scrollbar->view_height = ctx->cursor[3];
839
840 if( scrollbar->drag )
841 {
842 scrollbar->py = ctx->mouse[1]+scrollbar->drag_offset;
843 scrollbar->py = VG_MAX( scrollbar->py, 0 );
844 scrollbar->py = VG_MIN( scrollbar->py, ctx->cursor[3] - scrollbar->bar_height );
845
846 if( ctx->click_state == 0 || ctx->click_state == 3 )
847 scrollbar->drag = 0;
848 }
849
850 ui_new_node( ctx );
851 {
852 ui_fill_rect( ctx, ctx->cursor, ctx->colours_current->background );
853 ui_capture_mouse( ctx, id );
854
855 ctx->cursor[1] += scrollbar->py;
856 ctx->cursor[3] = scrollbar->bar_height;
857
858 ui_new_node( ctx );
859 {
860 ui_capture_mouse( ctx, __COUNTER__ );
861 struct ui_vert *drag_bar = ui_fill_rect( ctx, ctx->cursor, ctx->colours_current->bar );
862
863 if( ui_hasmouse( ctx ) || scrollbar->drag )
864 {
865 drag_bar[0].colour = ctx->colours_current->bar_hover;
866 drag_bar[1].colour = ctx->colours_current->bar_hover;
867 drag_bar[2].colour = ctx->colours_current->bar_hover;
868 drag_bar[3].colour = ctx->colours_current->bar_hover;
869
870 // start drag
871 if( ctx->click_state == 1 )
872 {
873 scrollbar->drag = 1;
874 scrollbar->drag_offset = scrollbar->py - ctx->mouse[1];
875 }
876 }
877 }
878 ui_end_down( ctx );
879 }
880 ui_end( ctx );
881 }
882
883 static ui_px ui_calculate_content_scroll( struct ui_scrollbar *scrollbar, ui_px content )
884 {
885 float overlap = vg_maxf( 0.0f, (float)(content - scrollbar->view_height) );
886
887 float range = scrollbar->view_height - scrollbar->bar_height;
888 return ((float)scrollbar->py / range) * overlap;
889 }
890
891 static void ui_override_colours( ui_ctx *ctx, ui_colourset *set )
892 {
893 ctx->colours_current = set;
894 }
895
896 static void ui_reset_colours( ui_ctx *ctx )
897 {
898 ctx->colours_current = ctx->colours_main;
899 ctx->override_colour = 0xffffffff;
900 }