1 // Copyright (C) 2021 Harry Godden (hgn) - All Rights Reserved
3 SHADER_DEFINE( shader_ui
,
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
12 "out vec2 aTexCoords;"
19 "gl_Position = vec4( uPv * vec3( a_co, 1.0 ), 1.0 );"
20 "aTexCoords = a_uv * 0.0078125;"
28 "uniform sampler2D uTexGlyphs;"
39 "float clip_blend = step( aWsp.x, aClip.z ) * step( aWsp.y, aClip.w ) * step( aClip.x, aWsp.x ) * step( aClip.y, aWsp.y );"
41 "vec4 glyph = texture( uTexGlyphs, aTexCoords );"
42 "FragColor = vec4( aColour.rgb * glyph.rgb, aColour.a*glyph.a*clip_blend );"
45 UNIFORMS({ "uPv", "uTexGlyphs" })
48 #define UI_AUTO_FILL 0
52 // ===========================================================================================================
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
;
112 u32 capture_mouse_id
;
119 int click_state
; // 0: released, 1: on down, 2: pressed, 3: on release
121 ui_colourset
*colours_main
;
122 ui_colourset
*colours_current
;
139 int x
, y
, width
, height
, originX
, originY
, advance
;
145 int size
, width
, height
;
146 struct ui_sdf_char
*characters
;
150 // ===========================================================================================================
153 int ui_glyph_override
= 0;
154 ui_px ui_glyph_spacing_x
= 6;
155 GLuint ui_glyph_texture
= 0;
157 ui_colourset ui_default_colours
= {
162 ui_ctx ui_global_ctx
= {
164 .colours_main
= &ui_default_colours
169 // ===========================================================================================================
171 static void ui_reset_colours( ui_ctx
*ctx
);
172 static void ui_init_context( ui_ctx
*ctx
, int index_buffer_size
)
174 ui_reset_colours( ctx
);
176 u32 vertex_buffer_size
= (index_buffer_size
+(index_buffer_size
/2));
178 // Generate the buffer we are gonna be drawing to
180 glGenVertexArrays(1, &ctx
->vao
);
181 glGenBuffers( 1, &ctx
->vbo
);
182 glGenBuffers( 1, &ctx
->ebo
);
183 glBindVertexArray( ctx
->vao
);
185 glBindBuffer( GL_ARRAY_BUFFER
, ctx
->vbo
);
187 glBufferData( GL_ARRAY_BUFFER
, vertex_buffer_size
* sizeof( struct ui_vert
), NULL
, GL_DYNAMIC_DRAW
);
188 glBindVertexArray( ctx
->vao
);
190 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER
, ctx
->ebo
);
191 glBufferData( GL_ELEMENT_ARRAY_BUFFER
, index_buffer_size
* sizeof( u16
), NULL
, GL_DYNAMIC_DRAW
);
193 u32
const stride
= sizeof( struct ui_vert
);
196 glVertexAttribPointer( 0, 2, GL_SHORT
, GL_FALSE
, stride
, (void *)offsetof( struct ui_vert
, co
) );
197 glEnableVertexAttribArray( 0 );
200 glVertexAttribPointer( 1, 2, GL_SHORT
, GL_FALSE
, stride
, (void *)offsetof( struct ui_vert
, uv
) );
201 glEnableVertexAttribArray( 1 );
204 glVertexAttribPointer( 2, 4, GL_UNSIGNED_BYTE
, GL_TRUE
, stride
, (void *)offsetof( struct ui_vert
, colour
) );
205 glEnableVertexAttribArray( 2 );
208 glVertexAttribPointer( 3, 4, GL_SHORT
, GL_FALSE
, stride
, (void *)offsetof( struct ui_vert
, clip
) );
209 glEnableVertexAttribArray( 3 );
212 // Initialize default context
214 ctx
->verts
= (struct ui_vert
*)malloc( vertex_buffer_size
* sizeof(struct ui_vert
) );
215 ctx
->indices
= (u16
*)malloc( index_buffer_size
* sizeof(u16
) );
219 static void ui_context_free( ui_ctx
*ctx
)
221 glDeleteVertexArrays( 1, &ctx
->vao
);
222 glDeleteBuffers( 1, &ctx
->vbo
);
223 glDeleteBuffers( 1, &ctx
->ebo
);
226 free( ctx
->indices
);
229 static void ui_override_font( GLuint new_tex
, ui_px space_x
)
231 if( ui_glyph_texture
)
232 glDeleteTextures( 1, &ui_glyph_texture
);
234 ui_glyph_texture
= new_tex
;
235 ui_glyph_override
= 1;
236 ui_glyph_spacing_x
= space_x
;
239 static void ui_default_init(void)
242 if( !ui_glyph_override
)
245 #include "fonts/weiholmir.h"
248 u32 pixels
= 0, total
= 72*72, data
= 0;
249 u8
*image
= malloc( total
);
251 while( pixels
< total
)
253 for( int b
= 31; b
>= 0; b
-- )
255 image
[ pixels
++ ] = (compressed
[data
] & (0x1 << b
))? 0xff: 0x00;
257 if( pixels
>= total
)
266 glGenTextures( 1, &ui_glyph_texture
);
267 glBindTexture( GL_TEXTURE_2D
, ui_glyph_texture
);
269 glTexImage2D( GL_TEXTURE_2D
, 0, GL_R8
, 72, 72, 0, GL_RED
, GL_UNSIGNED_BYTE
, image
);
277 // Setup OpenGL memory
278 SHADER_INIT( shader_ui
);
279 ui_init_context( &ui_global_ctx
, 20000 );
282 static void ui_default_free(void)
284 if( !ui_glyph_override
)
285 glDeleteTextures( 1, &ui_glyph_texture
);
287 ui_context_free( &ui_global_ctx
);
290 static struct ui_vert
*ui_fill_rect_uv( ui_ctx
*ctx
, ui_rect rect
, u32 colour
, ui_px uv
[4] );
291 static void ui_draw( ui_ctx
*ctx
, m3x3f view_override
)
293 u32 num_indices_normal
= ctx
->num_indices
;
295 // Append images to back of buffer
296 for( int i
= 0; i
< ctx
->image_count
; i
++ )
297 ui_fill_rect_uv( ctx
, ctx
->images
[i
].rc
, 0xffffffff, (ui_px
[4]){0,0,128,128} );
299 glBindVertexArray( ctx
->vao
);
301 glBindBuffer( GL_ARRAY_BUFFER
, ctx
->vbo
);
302 glBufferSubData( GL_ARRAY_BUFFER
, 0, ctx
->num_verts
* sizeof( struct ui_vert
), ctx
->verts
);
304 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER
, ctx
->ebo
);
305 glBufferSubData( GL_ELEMENT_ARRAY_BUFFER
, 0, ctx
->num_indices
* sizeof( u16
), ctx
->indices
);
308 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
309 glBlendEquation(GL_FUNC_ADD
);
311 SHADER_USE( shader_ui
);
313 m3x3f view
= M3X3_IDENTITY
;
317 view_override
= view
;
319 m3x3_translate( view
, (v3f
){ -1.0f
, 1.0f
, 0.0f
} );
320 m3x3_scale( view
, (v3f
){ 1.0f
/((float)vg_window_x
*0.5f
), -1.0f
/((float)vg_window_y
*0.5f
), 1.0f
} );
323 glUniformMatrix3fv( SHADER_UNIFORM( shader_ui
, "uPv" ), 1, GL_FALSE
, (float *)view_override
);
325 glActiveTexture( GL_TEXTURE0
);
326 glBindTexture( GL_TEXTURE_2D
, ui_glyph_texture
);
327 glUniform1i( SHADER_UNIFORM( shader_ui
, "uTexGlyphs" ), 0 );
329 glDrawElements( GL_TRIANGLES
, num_indices_normal
, GL_UNSIGNED_SHORT
, (void*)(0) );
331 // Draw image elements
332 for( int i
= 0; i
< ctx
->image_count
; i
++ )
334 struct ui_image
*img
= &ctx
->images
[i
];
336 glBindTexture( GL_TEXTURE_2D
, img
->image
);
337 glDrawElements( GL_TRIANGLES
, 6, GL_UNSIGNED_SHORT
, (void*)( (num_indices_normal
+ 6*i
)*sizeof(u16
) ) );
344 // ===========================================================================================================
346 static void ui_rect_copy( ui_rect src
, ui_rect dst
)
354 static void ui_rect_pad( ui_rect rect
, ui_px pad
)
362 static void ui_vis_rect( ui_rect rect
, u32 colour
)
370 p1
[0] = rect
[0]+rect
[2];
371 p1
[1] = rect
[1]+rect
[3];
373 vg_line( p0
, (v2f
){p1
[0],p0
[1]}, colour
);
374 vg_line( (v2f
){p1
[0],p0
[1]}, p1
, colour
);
375 vg_line( p1
, (v2f
){p0
[0],p1
[1]}, colour
);
376 vg_line( (v2f
){p0
[0],p1
[1]}, p0
, colour
);
381 // ===========================================================================================================
383 static struct ui_qnode
*ui_current( ui_ctx
*ctx
)
385 return &ctx
->stack
[ ctx
->stack_count
-1 ];
388 static void ui_new_node( ui_ctx
*ctx
)
390 if( ctx
->stack_count
== vg_list_size( ctx
->stack
) )
391 vg_exiterr( "[UI] Stack overflow while creating box!" );
393 struct ui_qnode
*parent
= &ctx
->stack
[ ctx
->stack_count
-1 ];
394 struct ui_qnode
*node
= &ctx
->stack
[ ctx
->stack_count
++ ];
395 ui_rect_copy( ctx
->cursor
, node
->rect
);
397 if( parent
->mouse_over
)
399 if( ctx
->mouse
[0] >= node
->rect
[0] && ctx
->mouse
[0] < node
->rect
[0]+node
->rect
[2] &&
400 ctx
->mouse
[1] >= node
->rect
[1] && ctx
->mouse
[1] < node
->rect
[1]+node
->rect
[3] )
401 node
->mouse_over
= 1;
403 node
->mouse_over
= 0;
407 node
->mouse_over
= 0;
411 static int ui_hasmouse( ui_ctx
*ctx
)
413 struct ui_qnode
*node
= ui_current( ctx
);
414 return (node
->mouse_over
&& (node
->capture_id
== ctx
->capture_mouse_id
));
417 static void ui_end( ui_ctx
*ctx
)
419 struct ui_qnode
*node
= &ctx
->stack
[ --ctx
->stack_count
];
421 ui_rect_copy( node
->rect
, ctx
->cursor
);
422 ui_vis_rect( ctx
->cursor
,
423 (node
->mouse_over
&& (node
->capture_id
== ctx
->capture_mouse_id
))? 0xffff0000: 0xff0000ff );
426 static void ui_end_down( ui_ctx
*ctx
)
428 ui_px height
= ui_current( ctx
)->rect
[3];
430 ctx
->cursor
[1] += height
;
433 static void ui_end_right( ui_ctx
*ctx
)
435 ui_px width
= ui_current( ctx
)->rect
[2];
437 ctx
->cursor
[0] += width
;
440 static void ui_fill_y( ui_ctx
*ctx
)
442 struct ui_qnode
*node
= ui_current( ctx
);
443 ctx
->cursor
[3] = node
->rect
[3] - (ctx
->cursor
[1]-node
->rect
[1]);
446 static void ui_fill_x( ui_ctx
*ctx
)
448 struct ui_qnode
*node
= ui_current( ctx
);
449 ctx
->cursor
[2] = node
->rect
[2] - (ctx
->cursor
[0]-node
->rect
[0]);
452 // Alignment: | [] | -> | []|
453 static void ui_align_bottom( ui_ctx
*ctx
)
455 struct ui_qnode
*node
= ui_current( ctx
);
456 ctx
->cursor
[1] = node
->rect
[1] + node
->rect
[3] - ctx
->cursor
[3];
459 static void ui_align_right( ui_ctx
*ctx
)
461 struct ui_qnode
*node
= ui_current( ctx
);
462 ctx
->cursor
[0] = node
->rect
[0] + node
->rect
[2] - ctx
->cursor
[2];
465 static void ui_align_top( ui_ctx
*ctx
)
467 ctx
->cursor
[1] = ui_current( ctx
)->rect
[1];
470 static void ui_align_left( ui_ctx
*ctx
)
472 ctx
->cursor
[0] = ui_current( ctx
)->rect
[0];
475 static void ui_clamp_rect( ui_rect parent
, ui_rect dest
)
477 dest
[0] = vg_min( parent
[0] + parent
[2] - dest
[2], dest
[0] );
478 dest
[1] = vg_min( parent
[1] + parent
[3] - dest
[3], dest
[1] );
479 dest
[0] = vg_max( parent
[0], dest
[0] );
480 dest
[1] = vg_max( parent
[1], dest
[1] );
483 static u32
ui_group_id( ui_ctx
*ctx
, u32 lesser_unique
)
485 return ctx
->id_base
| lesser_unique
;
488 static void ui_capture_mouse( ui_ctx
*ctx
, u32 id
)
490 u32 group_uid
= ui_group_id(ctx
,id
);
492 struct ui_qnode
*node
= &ctx
->stack
[ ctx
->stack_count
-1 ];
493 node
->capture_id
= group_uid
;
495 if( !ctx
->capture_lock
&& node
->mouse_over
)
497 ctx
->capture_mouse_id
= group_uid
;
501 static int ui_want_mouse( ui_ctx
*ctx
)
503 return ctx
->capture_mouse_id
== 0? 0: 1;
506 static void ui_set_clip( ui_ctx
*ctx
, ui_rect clip
)
508 ctx
->clipping
[0] = clip
[0];
509 ctx
->clipping
[1] = clip
[1];
510 ctx
->clipping
[2] = clip
[0] + clip
[2];
511 ctx
->clipping
[3] = clip
[1] + clip
[3];
514 static void ui_release_clip( ui_ctx
*ctx
)
516 ctx
->clipping
[0] = -32000;
517 ctx
->clipping
[1] = -32000;
518 ctx
->clipping
[2] = 32000;
519 ctx
->clipping
[3] = 32000;
523 // ===========================================================================================================
525 static struct ui_vert
*ui_fill_rect_uv( ui_ctx
*ctx
, ui_rect rect
, u32 colour
, ui_px uv
[4] )
527 struct ui_vert
*vertices
= &ctx
->verts
[ ctx
->num_verts
];
528 vertices
[0].co
[0] = rect
[0];
529 vertices
[0].co
[1] = rect
[1];
530 vertices
[0].uv
[0] = uv
[0];
531 vertices
[0].uv
[1] = uv
[1];
532 vertices
[0].colour
= colour
;
533 vertices
[1].co
[0] = rect
[0]+rect
[2];
534 vertices
[1].co
[1] = rect
[1];
535 vertices
[1].uv
[0] = uv
[2];
536 vertices
[1].uv
[1] = uv
[1];
537 vertices
[1].colour
= colour
;
538 vertices
[2].co
[0] = rect
[0]+rect
[2];
539 vertices
[2].co
[1] = rect
[1]+rect
[3];
540 vertices
[2].uv
[0] = uv
[2];
541 vertices
[2].uv
[1] = uv
[3];
542 vertices
[2].colour
= colour
;
543 vertices
[3].co
[0] = rect
[0];
544 vertices
[3].co
[1] = rect
[1]+rect
[3];
545 vertices
[3].uv
[0] = uv
[0];
546 vertices
[3].uv
[1] = uv
[3];
547 vertices
[3].colour
= colour
;
548 u16 ind_start
= ctx
->num_verts
;
549 u16
*indices
= &ctx
->indices
[ ctx
->num_indices
];
551 ui_rect_copy( ctx
->clipping
, vertices
[0].clip
);
552 ui_rect_copy( ctx
->clipping
, vertices
[1].clip
);
553 ui_rect_copy( ctx
->clipping
, vertices
[2].clip
);
554 ui_rect_copy( ctx
->clipping
, vertices
[3].clip
);
556 indices
[0] = ind_start
+0;
557 indices
[1] = ind_start
+2;
558 indices
[2] = ind_start
+1;
560 indices
[3] = ind_start
+0;
561 indices
[4] = ind_start
+3;
562 indices
[5] = ind_start
+2;
564 ctx
->num_indices
+= 6;
570 static struct ui_vert
*ui_fill_rect( ui_ctx
*ctx
, ui_rect rect
, u32 colour
)
572 return ui_fill_rect_uv( ctx
, rect
, colour
, (ui_px
[4]){ 4,124,4,124 } );
575 static void ui_text_use_title( ui_ctx
*ctx
)
580 static void ui_text_use_paragraph( ui_ctx
*ctx
)
587 k_text_alignment_left
= 0,
588 k_text_alignment_center
,
589 k_text_alignment_right
592 static ui_px
ui_text( ui_ctx
*ctx
, const char *str
, ui_px scale
, enum text_alignment alignment
)
596 text_cursor
[0] = ctx
->cursor
[0];
597 text_cursor
[1] = ctx
->cursor
[1];
598 text_cursor
[2] = (scale
*8)/2;
599 text_cursor
[3] = (scale
*8)/2;
601 u32 current_colour
= ctx
->override_colour
;
604 if( alignment
!= k_text_alignment_left
)
606 const char *pch
= str
;
609 offset
+= (ui_glyph_spacing_x
*scale
)/4;
610 if( !(*pch
) || *pch
== '\n' )
615 if( alignment
== k_text_alignment_right
)
616 text_cursor
[0] = ctx
->cursor
[0]+ctx
->cursor
[2]-offset
;
618 text_cursor
[0] = (ctx
->cursor
[0]+(ctx
->cursor
[2]/2))-(offset
/2);
621 const char *_c
= str
;
623 while( (c
= *(_c
++)) )
627 text_cursor
[1] += (7*scale
)/2;
628 text_cursor
[0] = ctx
->cursor
[0];
631 else if( c
>= 33 && c
<= 126 )
634 u8 glyph_index
= c
- 32;
635 glyph_base
[0] = (glyph_index
&0xf);
636 glyph_base
[1] = ctx
->glyph_base
+ ((glyph_index
-glyph_base
[0])>>4);
641 ui_fill_rect_uv( ctx
, text_cursor
, current_colour
,
646 128-(glyph_base
[1]+8)
649 else if( c
== '\x1B' )
653 for( int i
= 0; i
< 3; i
++ )
663 case '0': current_colour
= 0xffffffff; break;
664 case '3'|'1'<<8: current_colour
= 0xff201fee; break;
665 case '3'|'2'<<8: current_colour
= 0xff37e420; break;
666 case '3'|'3'<<8: current_colour
= 0xff0ed8e2; break;
667 case '3'|'4'<<8: current_colour
= 0xfff15010; break;
668 case '3'|'5'<<8: current_colour
= 0xffee20ee; break;
669 case '3'|'6'<<8: current_colour
= 0xffeeee20; break;
670 case '3'|'7'<<8: current_colour
= 0xffffffff; break;
676 colour_id
|= _c
[i
] << (i
*8);
687 text_cursor
[0] += (ui_glyph_spacing_x
*scale
)/4;
690 return text_cursor
[0];
694 // ====================================================================
696 static void ui_begin( ui_ctx
*ctx
, ui_px res_x
, ui_px res_y
)
700 ctx
->cursor
[2] = res_x
;
701 ctx
->cursor
[3] = res_y
;
703 ui_rect_copy( ctx
->cursor
, ctx
->stack
[0].rect
);
704 ctx
->stack
[0].mouse_over
= 1;
706 ctx
->stack_count
= 1;
709 ctx
->num_indices
= 0;
711 ui_release_clip( ctx
);
713 if( ctx
->click_state
== 0 )
714 ctx
->capture_mouse_id
= 0;
716 ctx
->image_count
= 0;
719 static void ui_resolve( ui_ctx
*ctx
)
721 if( ctx
->stack_count
-1 )
722 vg_exiterr( "[UI] Mismatched node create/drestroy!" );
724 if( ctx
->click_state
== 3 || ctx
->click_state
== 0 )
726 ctx
->capture_lock
= 0;
731 // ====================================================================
733 static void ui_set_mouse( ui_ctx
*ctx
, int x
, int y
, int click_state
)
738 ctx
->click_state
= click_state
;
741 // High level controls
742 // ====================================================================
750 ui_px drag_offset
[2];
755 k_button_released
= 0,
756 k_button_start_click
,
761 static int ui_button( ui_ctx
*ctx
, u32 id
)
765 ui_capture_mouse( ctx
, id
);
767 if( ui_hasmouse(ctx
) )
769 ui_fill_rect( ctx
, ctx
->cursor
, ctx
->colours_current
->hover
);
771 if( ctx
->click_state
== 1 )
773 ctx
->capture_lock
= 1;
774 return k_button_start_click
;
776 else if( ctx
->capture_lock
&& ctx
->click_state
== 3 )
777 return k_button_click
;
778 else if( ctx
->capture_lock
&& ctx
->click_state
== 2 )
779 return k_button_hold
;
782 ui_fill_rect( ctx
, ctx
->cursor
, ctx
->colours_current
->main
);
785 return k_button_released
;
788 static int ui_window( ui_ctx
*ctx
, struct ui_window
*window
, u32 control_group
)
790 ctx
->id_base
= control_group
<< 16;
794 window
->transform
[0] = ctx
->mouse
[0]+window
->drag_offset
[0];
795 window
->transform
[1] = ctx
->mouse
[1]+window
->drag_offset
[1];
797 ui_clamp_rect( ctx
->stack
[0].rect
, window
->transform
);
799 if( ctx
->click_state
== 0 || ctx
->click_state
== 3 )
805 ui_rect_copy( window
->transform
, ctx
->cursor
);
809 ui_capture_mouse( ctx
, __COUNTER__
);
815 ui_capture_mouse( ctx
, __COUNTER__
);
817 struct ui_vert
*drag_bar
= ui_fill_rect( ctx
, ctx
->cursor
, 0xff555555 );
822 ui_text( ctx
, window
->title
, 2, 0 );
827 ui_align_right( ctx
);
829 ui_rect_pad( ctx
->cursor
, 4 );
831 if( ui_button( ctx
, __COUNTER__
) )
833 vg_info( "Click clacked\n" );
836 ui_text( ctx
, "x", 2, 0 );
839 if( ui_hasmouse( ctx
) )
841 drag_bar
[0].colour
= 0xff777777;
842 drag_bar
[1].colour
= 0xff777777;
843 drag_bar
[2].colour
= 0xff777777;
844 drag_bar
[3].colour
= 0xff777777;
847 if( ctx
->click_state
== 1 )
850 window
->drag_offset
[0] = window
->transform
[0]-ctx
->mouse
[0];
851 window
->drag_offset
[1] = window
->transform
[1]-ctx
->mouse
[1];
871 static void ui_scrollbar( ui_ctx
*ctx
, struct ui_scrollbar
*scrollbar
, u32 id
)
873 scrollbar
->view_height
= ctx
->cursor
[3];
875 if( scrollbar
->drag
)
877 scrollbar
->py
= ctx
->mouse
[1]+scrollbar
->drag_offset
;
878 scrollbar
->py
= VG_MAX( scrollbar
->py
, 0 );
879 scrollbar
->py
= VG_MIN( scrollbar
->py
, ctx
->cursor
[3] - scrollbar
->bar_height
);
881 if( ctx
->click_state
== 0 || ctx
->click_state
== 3 )
887 ui_fill_rect( ctx
, ctx
->cursor
, ctx
->colours_current
->background
);
888 ui_capture_mouse( ctx
, id
);
890 ctx
->cursor
[1] += scrollbar
->py
;
891 ctx
->cursor
[3] = scrollbar
->bar_height
;
895 ui_capture_mouse( ctx
, __COUNTER__
);
896 struct ui_vert
*drag_bar
= ui_fill_rect( ctx
, ctx
->cursor
, ctx
->colours_current
->bar
);
898 if( ui_hasmouse( ctx
) || scrollbar
->drag
)
900 drag_bar
[0].colour
= ctx
->colours_current
->bar_hover
;
901 drag_bar
[1].colour
= ctx
->colours_current
->bar_hover
;
902 drag_bar
[2].colour
= ctx
->colours_current
->bar_hover
;
903 drag_bar
[3].colour
= ctx
->colours_current
->bar_hover
;
906 if( ctx
->click_state
== 1 )
909 scrollbar
->drag_offset
= scrollbar
->py
- ctx
->mouse
[1];
918 static ui_px
ui_calculate_content_scroll( struct ui_scrollbar
*scrollbar
, ui_px content
)
920 float overlap
= vg_maxf( 0.0f
, (float)(content
- scrollbar
->view_height
) );
922 float range
= scrollbar
->view_height
- scrollbar
->bar_height
;
923 return ((float)scrollbar
->py
/ range
) * overlap
;
926 static void ui_override_colours( ui_ctx
*ctx
, ui_colourset
*set
)
928 ctx
->colours_current
= set
;
931 static void ui_reset_colours( ui_ctx
*ctx
)
933 ctx
->colours_current
= ctx
->colours_main
;
934 ctx
->override_colour
= 0xffffffff;
937 static void ui_push_image( ui_ctx
*ctx
, ui_rect rc
, GLuint image
)
939 struct ui_image
*img
= &ctx
->images
[ ctx
->image_count
++ ];
940 ui_rect_copy( rc
, img
->rc
);
945 #define gui_draw(...) ui_draw( &ui_global_ctx, __VA_ARGS__)
946 #define gui_current(...) ui_current( &ui_global_ctx, __VA_ARGS__)
947 #define gui_new_node() ui_new_node( &ui_global_ctx )
948 #define gui_hasmouse(...) ui_hasmouse( &ui_global_ctx, __VA_ARGS__)
949 #define gui_end() ui_end( &ui_global_ctx )
950 #define gui_end_down() ui_end_down( &ui_global_ctx )
951 #define gui_end_right() ui_end_right( &ui_global_ctx )
952 #define gui_fill_y() ui_fill_y( &ui_global_ctx)
953 #define gui_fill_x() ui_fill_x( &ui_global_ctx)
954 #define gui_align_bottom() ui_align_bottom( &ui_global_ctx )
955 #define gui_align_right() ui_align_right( &ui_global_ctx )
956 #define gui_align_top() ui_align_top( &ui_global_ctx )
957 #define gui_align_left() ui_align_left( &ui_global_ctx )
958 #define gui_clamp_rect(...) ui_clamp_rect( &ui_global_ctx, __VA_ARGS__)
959 #define gui_group_id(...) ui_group_id( &ui_global_ctx, __VA_ARGS__)
960 #define gui_capture_mouse(...) ui_capture_mouse( &ui_global_ctx, __VA_ARGS__)
961 #define gui_set_clip(...) ui_set_clip( &ui_global_ctx, __VA_ARGS__)
962 #define gui_release_clip() ui_release_clip( &ui_global_ctx )
963 #define gui_fill_rect_uv(...) ui_fill_rect_uv( &ui_global_ctx, __VA_ARGS__)
964 #define gui_fill_rect(...) ui_fill_rect( &ui_global_ctx, __VA_ARGS__)
965 #define gui_text(...) ui_text( &ui_global_ctx, __VA_ARGS__)
966 #define gui_begin(...) ui_begin( &ui_global_ctx, __VA_ARGS__)
967 #define gui_resolve(...) ui_resolve( &ui_global_ctx, __VA_ARGS__)
968 #define gui_set_mouse(...) ui_set_mouse( &ui_global_ctx, __VA_ARGS__)
969 #define gui_button(...) ui_button( &ui_global_ctx, __VA_ARGS__)
970 #define gui_window(...) ui_window( &ui_global_ctx, __VA_ARGS__)
971 #define gui_want_mouse() ui_want_mouse( &ui_global_ctx )
972 #define gui_push_image(...) ui_push_image( &ui_global_ctx, __VA_ARGS__ )
973 #define gui_scrollbar(...) ui_scrollbar( &ui_global_ctx, __VA_ARGS__)
974 #define gui_override_colours(...) ui_override_colours( &ui_global_ctx, __VA_ARGS__)
975 #define gui_reset_colours(...) ui_reset_colours( &ui_global_ctx )