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