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 = aColour * vec4( 1.0, 1.0, 1.0, glyph.r * 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
;
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 )
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 )
159 // ===========================================================================================================
162 int ui_glyph_override
= 0;
163 ui_px ui_glyph_spacing_x
= 6;
164 GLuint ui_glyph_texture
= 0;
169 #define UI_BUFFER_SIZE 30000
170 #define UI_INDEX_SIZE 20000
172 ui_colourset ui_default_colours
= {
177 ui_ctx ui_global_ctx
= {
179 .colours_current
= &ui_default_colours
,
180 .colours_main
= &ui_default_colours
185 // ===========================================================================================================
187 static void ui_override_font( GLuint new_tex
, ui_px space_x
)
189 if( ui_glyph_texture
)
190 glDeleteTextures( 1, &ui_glyph_texture
);
192 ui_glyph_texture
= new_tex
;
193 ui_glyph_override
= 1;
194 ui_glyph_spacing_x
= space_x
;
197 static void ui_default_init(void)
200 if( !ui_glyph_override
)
203 #include "fonts/weiholmir.h"
206 u32 pixels
= 0, total
= 72*72, data
= 0;
207 u8
*image
= malloc( total
);
209 while( pixels
< total
)
211 for( int b
= 31; b
>= 0; b
-- )
213 image
[ pixels
++ ] = (compressed
[data
] & (0x1 << b
))? 0xff: 0x00;
215 if( pixels
>= total
)
224 glGenTextures( 1, &ui_glyph_texture
);
225 glBindTexture( GL_TEXTURE_2D
, ui_glyph_texture
);
227 glTexImage2D( GL_TEXTURE_2D
, 0, GL_R8
, 72, 72, 0, GL_RED
, GL_UNSIGNED_BYTE
, image
);
235 // Setup OpenGL memory
237 SHADER_INIT( shader_ui
);
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
);
245 glBindBuffer( GL_ARRAY_BUFFER
, ui_vbo
);
247 glBufferData( GL_ARRAY_BUFFER
, UI_BUFFER_SIZE
* sizeof( struct ui_vert
), NULL
, GL_DYNAMIC_DRAW
);
248 glBindVertexArray( ui_vao
);
250 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER
, ui_ebo
);
251 glBufferData( GL_ELEMENT_ARRAY_BUFFER
, UI_INDEX_SIZE
* sizeof( u16
), NULL
, GL_DYNAMIC_DRAW
);
253 u32
const stride
= sizeof( struct ui_vert
);
256 glVertexAttribPointer( 0, 2, GL_SHORT
, GL_FALSE
, stride
, (void *)offsetof( struct ui_vert
, co
) );
257 glEnableVertexAttribArray( 0 );
260 glVertexAttribPointer( 1, 2, GL_UNSIGNED_BYTE
, GL_FALSE
, stride
, (void *)offsetof( struct ui_vert
, uv
) );
261 glEnableVertexAttribArray( 1 );
264 glVertexAttribPointer( 2, 4, GL_UNSIGNED_BYTE
, GL_TRUE
, stride
, (void *)offsetof( struct ui_vert
, colour
) );
265 glEnableVertexAttribArray( 2 );
268 glVertexAttribPointer( 3, 4, GL_SHORT
, GL_FALSE
, stride
, (void *)offsetof( struct ui_vert
, clip
) );
269 glEnableVertexAttribArray( 3 );
272 // Initialize default context
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
) );
279 static void ui_default_free(void)
281 if( !ui_glyph_override
)
282 glDeleteTextures( 1, &ui_glyph_texture
);
284 glDeleteVertexArrays( 1, &ui_vao
);
285 glDeleteBuffers( 1, &ui_vbo
);
286 glDeleteBuffers( 1, &ui_ebo
);
288 free( ui_global_ctx
.verts
);
289 free( ui_global_ctx
.indices
);
292 static void ui_draw( ui_ctx
*ctx
, m3x3f view_override
)
294 glBindVertexArray( ui_vao
);
296 glBindBuffer( GL_ARRAY_BUFFER
, ui_vbo
);
297 glBufferSubData( GL_ARRAY_BUFFER
, 0, ctx
->num_verts
* sizeof( struct ui_vert
), ctx
->verts
);
299 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER
, ui_ebo
);
300 glBufferSubData( GL_ELEMENT_ARRAY_BUFFER
, 0, ctx
->num_indices
* sizeof( u16
), ctx
->indices
);
303 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
304 glBlendEquation(GL_FUNC_ADD
);
306 SHADER_USE( shader_ui
);
308 m3x3f view
= M3X3_IDENTITY
;
312 view_override
= view
;
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
} );
318 glUniformMatrix3fv( SHADER_UNIFORM( shader_ui
, "uPv" ), 1, GL_FALSE
, (float *)view_override
);
320 glActiveTexture( GL_TEXTURE0
);
321 glBindTexture( GL_TEXTURE_2D
, ui_glyph_texture
);
322 glUniform1i( SHADER_UNIFORM( shader_ui
, "uTexGlyphs" ), 0 );
324 glDrawElements( GL_TRIANGLES
, ctx
->num_indices
, GL_UNSIGNED_SHORT
, (void*)(0) );
326 //vg_info( "Verts: %u, Indices: %u\n", ctx->num_verts, ctx->num_indices );
332 // ===========================================================================================================
334 static void ui_rect_copy( ui_rect src
, ui_rect dst
)
342 static void ui_rect_pad( ui_rect rect
, ui_px pad
)
350 static void ui_vis_rect( ui_rect rect
, u32 colour
)
358 p1
[0] = rect
[0]+rect
[2];
359 p1
[1] = rect
[1]+rect
[3];
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
);
369 // ===========================================================================================================
371 static struct ui_qnode
*ui_current( ui_ctx
*ctx
)
373 return &ctx
->stack
[ ctx
->stack_count
-1 ];
376 static void ui_new_node( ui_ctx
*ctx
)
378 if( ctx
->stack_count
== vg_list_size( ctx
->stack
) )
379 vg_exiterr( "[UI] Stack overflow while creating box!" );
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
);
385 if( parent
->mouse_over
)
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;
391 node
->mouse_over
= 0;
395 node
->mouse_over
= 0;
399 static int ui_hasmouse( ui_ctx
*ctx
)
401 struct ui_qnode
*node
= ui_current( ctx
);
402 return (node
->mouse_over
&& (node
->capture_id
== ctx
->capture_mouse_id
));
405 static void ui_end( ui_ctx
*ctx
)
407 struct ui_qnode
*node
= &ctx
->stack
[ --ctx
->stack_count
];
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 );
414 static void ui_end_down( ui_ctx
*ctx
)
416 ui_px height
= ui_current( ctx
)->rect
[3];
418 ctx
->cursor
[1] += height
;
421 static void ui_end_right( ui_ctx
*ctx
)
423 ui_px width
= ui_current( ctx
)->rect
[2];
425 ctx
->cursor
[0] += width
;
428 static void ui_fill_y( ui_ctx
*ctx
)
430 struct ui_qnode
*node
= ui_current( ctx
);
431 ctx
->cursor
[3] = node
->rect
[3] - (ctx
->cursor
[1]-node
->rect
[1]);
434 static void ui_fill_x( ui_ctx
*ctx
)
436 struct ui_qnode
*node
= ui_current( ctx
);
437 ctx
->cursor
[2] = node
->rect
[2] - (ctx
->cursor
[0]-node
->rect
[0]);
440 // Alignment: | [] | -> | []|
441 static void ui_align_bottom( ui_ctx
*ctx
)
443 struct ui_qnode
*node
= ui_current( ctx
);
444 ctx
->cursor
[1] = node
->rect
[1] + node
->rect
[3] - ctx
->cursor
[3];
447 static void ui_align_right( ui_ctx
*ctx
)
449 struct ui_qnode
*node
= ui_current( ctx
);
450 ctx
->cursor
[0] = node
->rect
[0] + node
->rect
[2] - ctx
->cursor
[2];
453 static void ui_align_top( ui_ctx
*ctx
)
455 ctx
->cursor
[1] = ui_current( ctx
)->rect
[1];
458 static void ui_align_left( ui_ctx
*ctx
)
460 ctx
->cursor
[0] = ui_current( ctx
)->rect
[0];
463 static void ui_clamp_rect( ui_rect parent
, ui_rect dest
)
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] );
471 static u32
ui_group_id( ui_ctx
*ctx
, u32 lesser_unique
)
473 return ctx
->id_base
| lesser_unique
;
476 static void ui_capture_mouse( ui_ctx
*ctx
, u32 id
)
478 u32 group_uid
= ui_group_id(ctx
,id
);
480 struct ui_qnode
*node
= &ctx
->stack
[ ctx
->stack_count
-1 ];
481 node
->capture_id
= group_uid
;
483 if( !ctx
->capture_lock
&& node
->mouse_over
)
485 ctx
->capture_mouse_id
= group_uid
;
489 static int ui_want_mouse( ui_ctx
*ctx
)
491 return ctx
->capture_mouse_id
== 0? 0: 1;
494 static void ui_set_clip( ui_ctx
*ctx
, ui_rect clip
)
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];
502 static void ui_release_clip( ui_ctx
*ctx
)
504 ctx
->clipping
[0] = -32000;
505 ctx
->clipping
[1] = -32000;
506 ctx
->clipping
[2] = 32000;
507 ctx
->clipping
[3] = 32000;
511 // ===========================================================================================================
513 static struct ui_vert
*ui_fill_rect_uv( ui_ctx
*ctx
, ui_rect rect
, u32 colour
, ui_px uv
[4] )
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
];
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
);
544 indices
[0] = ind_start
+0;
545 indices
[1] = ind_start
+2;
546 indices
[2] = ind_start
+1;
548 indices
[3] = ind_start
+0;
549 indices
[4] = ind_start
+3;
550 indices
[5] = ind_start
+2;
552 ctx
->num_indices
+= 6;
558 static struct ui_vert
*ui_fill_rect( ui_ctx
*ctx
, ui_rect rect
, u32 colour
)
560 return ui_fill_rect_uv( ctx
, rect
, colour
, (ui_px
[4]){ 4,124,4,124 } );
563 static void ui_text_use_title( ui_ctx
*ctx
)
568 static void ui_text_use_paragraph( ui_ctx
*ctx
)
575 k_text_alignment_left
= 0,
576 k_text_alignment_center
579 static void ui_text( ui_ctx
*ctx
, const char *str
, ui_px scale
, enum text_alignment alignment
)
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;
588 u32 current_colour
= ctx
->override_colour
;
590 const char *_c
= str
;
592 while( (c
= *(_c
++)) )
596 text_cursor
[1] += (7*scale
)/2;
597 text_cursor
[0] = ctx
->cursor
[0];
600 else if( c
>= 33 && c
<= 126 )
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);
610 ui_fill_rect_uv( ctx
, text_cursor
, current_colour
,
615 128-(glyph_base
[1]+8)
618 else if( c
== '\x1B' )
622 for( int i
= 0; i
< 3; i
++ )
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;
645 colour_id
|= _c
[i
] << (i
*8);
656 text_cursor
[0] += (ui_glyph_spacing_x
*scale
)/4;
661 // ====================================================================
663 static void ui_begin( ui_ctx
*ctx
, ui_px res_x
, ui_px res_y
)
667 ctx
->cursor
[2] = res_x
;
668 ctx
->cursor
[3] = res_y
;
670 ui_rect_copy( ctx
->cursor
, ctx
->stack
[0].rect
);
671 ctx
->stack
[0].mouse_over
= 1;
673 ctx
->stack_count
= 1;
676 ctx
->num_indices
= 0;
678 ui_release_clip( ctx
);
680 if( ctx
->click_state
== 0 )
681 ctx
->capture_mouse_id
= 0;
684 static void ui_resolve( ui_ctx
*ctx
)
686 if( ctx
->stack_count
-1 )
687 vg_exiterr( "[UI] Mismatched node create/drestroy!" );
689 if( ctx
->click_state
== 3 || ctx
->click_state
== 0 )
691 ctx
->capture_lock
= 0;
696 // ====================================================================
698 static void ui_set_mouse( ui_ctx
*ctx
, int x
, int y
, int click_state
)
703 ctx
->click_state
= click_state
;
706 // High level controls
707 // ====================================================================
715 ui_px drag_offset
[2];
720 k_button_released
= 0,
721 k_button_start_click
,
726 static int ui_button( ui_ctx
*ctx
, u32 id
)
730 ui_capture_mouse( ctx
, id
);
732 if( ui_hasmouse(ctx
) )
734 ui_fill_rect( ctx
, ctx
->cursor
, ctx
->colours_current
->hover
);
736 if( ctx
->click_state
== 1 )
738 ctx
->capture_lock
= 1;
739 return k_button_start_click
;
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
;
747 ui_fill_rect( ctx
, ctx
->cursor
, ctx
->colours_current
->main
);
750 return k_button_released
;
753 static int ui_window( ui_ctx
*ctx
, struct ui_window
*window
, u32 control_group
)
755 ctx
->id_base
= control_group
<< 16;
759 window
->transform
[0] = ctx
->mouse
[0]+window
->drag_offset
[0];
760 window
->transform
[1] = ctx
->mouse
[1]+window
->drag_offset
[1];
762 ui_clamp_rect( ctx
->stack
[0].rect
, window
->transform
);
764 if( ctx
->click_state
== 0 || ctx
->click_state
== 3 )
770 ui_rect_copy( window
->transform
, ctx
->cursor
);
774 ui_capture_mouse( ctx
, __COUNTER__
);
780 ui_capture_mouse( ctx
, __COUNTER__
);
782 struct ui_vert
*drag_bar
= ui_fill_rect( ctx
, ctx
->cursor
, 0xff555555 );
787 ui_text( ctx
, window
->title
, 2, 0 );
792 ui_align_right( ctx
);
794 ui_rect_pad( ctx
->cursor
, 4 );
796 if( ui_button( ctx
, __COUNTER__
) )
798 vg_info( "Click clacked\n" );
801 ui_text( ctx
, "x", 2, 0 );
804 if( ui_hasmouse( ctx
) )
806 drag_bar
[0].colour
= 0xff777777;
807 drag_bar
[1].colour
= 0xff777777;
808 drag_bar
[2].colour
= 0xff777777;
809 drag_bar
[3].colour
= 0xff777777;
812 if( ctx
->click_state
== 1 )
815 window
->drag_offset
[0] = window
->transform
[0]-ctx
->mouse
[0];
816 window
->drag_offset
[1] = window
->transform
[1]-ctx
->mouse
[1];
836 static void ui_scrollbar( ui_ctx
*ctx
, struct ui_scrollbar
*scrollbar
, u32 id
)
838 scrollbar
->view_height
= ctx
->cursor
[3];
840 if( scrollbar
->drag
)
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
);
846 if( ctx
->click_state
== 0 || ctx
->click_state
== 3 )
852 ui_fill_rect( ctx
, ctx
->cursor
, ctx
->colours_current
->background
);
853 ui_capture_mouse( ctx
, id
);
855 ctx
->cursor
[1] += scrollbar
->py
;
856 ctx
->cursor
[3] = scrollbar
->bar_height
;
860 ui_capture_mouse( ctx
, __COUNTER__
);
861 struct ui_vert
*drag_bar
= ui_fill_rect( ctx
, ctx
->cursor
, ctx
->colours_current
->bar
);
863 if( ui_hasmouse( ctx
) || scrollbar
->drag
)
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
;
871 if( ctx
->click_state
== 1 )
874 scrollbar
->drag_offset
= scrollbar
->py
- ctx
->mouse
[1];
883 static ui_px
ui_calculate_content_scroll( struct ui_scrollbar
*scrollbar
, ui_px content
)
885 float overlap
= vg_maxf( 0.0f
, (float)(content
- scrollbar
->view_height
) );
887 float range
= scrollbar
->view_height
- scrollbar
->bar_height
;
888 return ((float)scrollbar
->py
/ range
) * overlap
;
891 static void ui_override_colours( ui_ctx
*ctx
, ui_colourset
*set
)
893 ctx
->colours_current
= set
;
896 static void ui_reset_colours( ui_ctx
*ctx
)
898 ctx
->colours_current
= ctx
->colours_main
;
899 ctx
->override_colour
= 0xffffffff;