X-Git-Url: https://harrygodden.com/git/?a=blobdiff_plain;f=vg%2Fvg_ui.h;h=fe46da0040a8bcd795f9628134d3519d78807b63;hb=a46972aa1ee718339c2512cae986f2943bed0e04;hp=bf9adc85549d761ecbd833c5df438aa1ad1c3b31;hpb=79844daac2e6c95698639247b2affcf75fabe5db;p=fishladder.git diff --git a/vg/vg_ui.h b/vg/vg_ui.h index bf9adc8..fe46da0 100644 --- a/vg/vg_ui.h +++ b/vg/vg_ui.h @@ -6,16 +6,22 @@ SHADER_DEFINE( shader_ui, "layout (location=0) in vec2 a_co;" // i16, i16, .. ? "layout (location=1) in vec2 a_uv;" // i8, i8 "layout (location=2) in vec4 a_colour;" // u32 + "layout (location=3) in vec4 a_clip;" // i16, i16, i16, i16 "uniform mat3 uPv;" "" "out vec2 aTexCoords;" "out vec4 aColour;" + "out vec2 aWsp;" + "out vec4 aClip;" "" "void main()" "{" "gl_Position = vec4( uPv * vec3( a_co, 1.0 ), 1.0 );" - "aTexCoords = a_uv * 0.01388888888;" + "aTexCoords = a_uv * 0.0078125;" "aColour = a_colour;" + + "aWsp = a_co;" + "aClip = a_clip;" "}", // FRAGMENT @@ -25,10 +31,15 @@ SHADER_DEFINE( shader_ui, "in vec2 aTexCoords;" "in vec4 aColour;" "" + "in vec2 aWsp;" + "in vec4 aClip;" + "" "void main()" "{" + "float clip_blend = step( aWsp.x, aClip.z ) * step( aWsp.y, aClip.w ) * step( aClip.x, aWsp.x ) * step( aClip.y, aWsp.y );" + "vec4 glyph = texture( uTexGlyphs, aTexCoords );" - "FragColor = aColour * vec4( 1.0, 1.0, 1.0, glyph.r );" + "FragColor = aColour * vec4( 1.0, 1.0, 1.0, glyph.r * clip_blend );" "}" , UNIFORMS({ "uPv", "uTexGlyphs" }) @@ -44,6 +55,26 @@ typedef i16 ui_px; typedef u32 ui_colour; typedef ui_px ui_rect[4]; typedef struct ui_ctx ui_ctx; +typedef struct ui_colourset ui_colourset; + +struct ui_colourset +{ + union + { + struct + { + ui_colour main; + ui_colour hover; + ui_colour active; + }; + struct + { + ui_colour background; + ui_colour bar; + ui_colour bar_hover; + }; + }; +}; struct ui_ctx { @@ -61,17 +92,21 @@ struct ui_ctx #pragma pack(push,1) struct ui_vert { - ui_px co[2]; - u8 uv[2]; - u32 colour; + ui_px co[2]; //32 4 + u8 uv[2]; //16 2 + u32 colour; //32 4 + ui_rect clip; //64 8 } *verts; #pragma pack(pop) + u32 override_colour; + u32 num_verts; u16 *indices; u32 num_indices; + ui_rect clipping; ui_rect cursor; u32 stack_count; u32 capture_mouse_id; @@ -81,13 +116,51 @@ struct ui_ctx // User input ui_px mouse[2]; int click_state; // 0: released, 1: on down, 2: pressed, 3: on release + + ui_colourset *colours_main; + ui_colourset *colours_current; }; +// Shortnames +#define gui_draw(...) ui_draw( &ui_global_ctx, __VA_ARGS__) +#define gui_current(...) ui_current( &ui_global_ctx, __VA_ARGS__) +#define gui_new_node() ui_new_node( &ui_global_ctx ) +#define gui_hasmouse(...) ui_hasmouse( &ui_global_ctx, __VA_ARGS__) +#define gui_end() ui_end( &ui_global_ctx ) +#define gui_end_down() ui_end_down( &ui_global_ctx ) +#define gui_end_right() ui_end_right( &ui_global_ctx ) +#define gui_fill_y() ui_fill_y( &ui_global_ctx) +#define gui_fill_x() ui_fill_x( &ui_global_ctx) +#define gui_align_bottom() ui_align_bottom( &ui_global_ctx ) +#define gui_align_right() ui_align_right( &ui_global_ctx ) +#define gui_align_top() ui_align_top( &ui_global_ctx ) +#define gui_align_left() ui_align_left( &ui_global_ctx ) +#define gui_clamp_rect(...) ui_clamp_rect( &ui_global_ctx, __VA_ARGS__) +#define gui_group_id(...) ui_group_id( &ui_global_ctx, __VA_ARGS__) +#define gui_capture_mouse(...) ui_capture_mouse( &ui_global_ctx, __VA_ARGS__) +#define gui_set_clip(...) ui_set_clip( &ui_global_ctx, __VA_ARGS__) +#define gui_release_clip() ui_release_clip( &ui_global_ctx ) +#define gui_fill_rect_uv(...) ui_fill_rect_uv( &ui_global_ctx, __VA_ARGS__) +#define gui_fill_rect(...) ui_fill_rect( &ui_global_ctx, __VA_ARGS__) +#define gui_text(...) ui_text( &ui_global_ctx, __VA_ARGS__) +#define gui_begin(...) ui_begin( &ui_global_ctx, __VA_ARGS__) +#define gui_resolve(...) ui_resolve( &ui_global_ctx, __VA_ARGS__) +#define gui_set_mouse(...) ui_set_mouse( &ui_global_ctx, __VA_ARGS__) +#define gui_button(...) ui_button( &ui_global_ctx, __VA_ARGS__) +#define gui_window(...) ui_window( &ui_global_ctx, __VA_ARGS__) +#define gui_want_mouse() ui_want_mouse( &ui_global_ctx ) + +#define gui_scrollbar(...) ui_scrollbar( &ui_global_ctx, __VA_ARGS__) +#define gui_override_colours(...) ui_override_colours( &ui_global_ctx, __VA_ARGS__) +#define gui_reset_colours(...) ui_reset_colours( &ui_global_ctx ) + // Globals // =========================================================================================================== // Opengl -GLuint ui_glyph_texture; +int ui_glyph_override = 0; +ui_px ui_glyph_spacing_x = 6; +GLuint ui_glyph_texture = 0; GLuint ui_vao; GLuint ui_vbo; GLuint ui_ebo; @@ -95,15 +168,35 @@ GLuint ui_ebo; #define UI_BUFFER_SIZE 30000 #define UI_INDEX_SIZE 20000 -ui_ctx ui_global_ctx = { .padding = 8 }; +ui_colourset ui_default_colours = { + .main = 0xff00ff00, + .hover = 0xffff00ff, + .active = 0xffff0000 +}; +ui_ctx ui_global_ctx = { + .padding = 8, + .colours_current = &ui_default_colours, + .colours_main = &ui_default_colours +}; // Initialization // =========================================================================================================== +static void ui_override_font( GLuint new_tex, ui_px space_x ) +{ + if( ui_glyph_texture ) + glDeleteTextures( 1, &ui_glyph_texture ); + + ui_glyph_texture = new_tex; + ui_glyph_override = 1; + ui_glyph_spacing_x = space_x; +} + static void ui_default_init(void) { - // Load font + // Load default font + if( !ui_glyph_override ) { u32 compressed[] = { #include "fonts/weiholmir.h" @@ -159,7 +252,7 @@ static void ui_default_init(void) u32 const stride = sizeof( struct ui_vert ); // XY - glVertexAttribPointer( 0, 2, GL_UNSIGNED_SHORT, GL_FALSE, stride, (void *)offsetof( struct ui_vert, co ) ); + glVertexAttribPointer( 0, 2, GL_SHORT, GL_FALSE, stride, (void *)offsetof( struct ui_vert, co ) ); glEnableVertexAttribArray( 0 ); // UV @@ -169,6 +262,10 @@ static void ui_default_init(void) // COLOUR glVertexAttribPointer( 2, 4, GL_UNSIGNED_BYTE, GL_TRUE, stride, (void *)offsetof( struct ui_vert, colour ) ); glEnableVertexAttribArray( 2 ); + + // CLIPPING + glVertexAttribPointer( 3, 4, GL_SHORT, GL_FALSE, stride, (void *)offsetof( struct ui_vert, clip ) ); + glEnableVertexAttribArray( 3 ); } // Initialize default context @@ -180,7 +277,8 @@ static void ui_default_init(void) static void ui_default_free(void) { - glDeleteTextures( 1, &ui_glyph_texture ); + if( !ui_glyph_override ) + glDeleteTextures( 1, &ui_glyph_texture ); glDeleteVertexArrays( 1, &ui_vao ); glDeleteBuffers( 1, &ui_vbo ); @@ -190,7 +288,7 @@ static void ui_default_free(void) free( ui_global_ctx.indices ); } -static void ui_draw( ui_ctx *ctx ) +static void ui_draw( ui_ctx *ctx, m3x3f view_override ) { glBindVertexArray( ui_vao ); @@ -207,9 +305,16 @@ static void ui_draw( ui_ctx *ctx ) SHADER_USE( shader_ui ); m3x3f view = M3X3_IDENTITY; - m3x3_translate( view, (v3f){ -1.0f, 1.0f, 0.0f } ); - m3x3_scale( view, (v3f){ 1.0f/((float)vg_window_x*0.5f), -1.0f/((float)vg_window_y*0.5f), 1.0f } ); - glUniformMatrix3fv( SHADER_UNIFORM( shader_ui, "uPv" ), 1, GL_FALSE, (float *)view ); + + if( !view_override ) + { + view_override = view; + + m3x3_translate( view, (v3f){ -1.0f, 1.0f, 0.0f } ); + m3x3_scale( view, (v3f){ 1.0f/((float)vg_window_x*0.5f), -1.0f/((float)vg_window_y*0.5f), 1.0f } ); + } + + glUniformMatrix3fv( SHADER_UNIFORM( shader_ui, "uPv" ), 1, GL_FALSE, (float *)view_override ); glActiveTexture( GL_TEXTURE0 ); glBindTexture( GL_TEXTURE_2D, ui_glyph_texture ); @@ -380,6 +485,27 @@ static void ui_capture_mouse( ui_ctx *ctx, u32 id ) } } +static int ui_want_mouse( ui_ctx *ctx ) +{ + return ctx->capture_mouse_id == 0? 0: 1; +} + +static void ui_set_clip( ui_ctx *ctx, ui_rect clip ) +{ + ctx->clipping[0] = clip[0]; + ctx->clipping[1] = clip[1]; + ctx->clipping[2] = clip[0] + clip[2]; + ctx->clipping[3] = clip[1] + clip[3]; +} + +static void ui_release_clip( ui_ctx *ctx ) +{ + ctx->clipping[0] = -32000; + ctx->clipping[1] = -32000; + ctx->clipping[2] = 32000; + ctx->clipping[3] = 32000; +} + // Drawing // =========================================================================================================== @@ -409,6 +535,11 @@ static struct ui_vert *ui_fill_rect_uv( ui_ctx *ctx, ui_rect rect, u32 colour, u u16 ind_start = ctx->num_verts; u16 *indices = &ctx->indices[ ctx->num_indices ]; + ui_rect_copy( ctx->clipping, vertices[0].clip ); + ui_rect_copy( ctx->clipping, vertices[1].clip ); + ui_rect_copy( ctx->clipping, vertices[2].clip ); + ui_rect_copy( ctx->clipping, vertices[3].clip ); + indices[0] = ind_start+0; indices[1] = ind_start+2; indices[2] = ind_start+1; @@ -425,7 +556,7 @@ static struct ui_vert *ui_fill_rect_uv( ui_ctx *ctx, ui_rect rect, u32 colour, u static struct ui_vert *ui_fill_rect( ui_ctx *ctx, ui_rect rect, u32 colour ) { - return ui_fill_rect_uv( ctx, rect, colour, (ui_px[4]){ 66, 66, 66, 66 } ); + return ui_fill_rect_uv( ctx, rect, colour, (ui_px[4]){ 4,124,4,124 } ); } static void ui_text( ui_ctx *ctx, const char *str, ui_px scale, int alignment ) @@ -434,10 +565,10 @@ static void ui_text( ui_ctx *ctx, const char *str, ui_px scale, int alignment ) text_cursor[0] = ctx->cursor[0]; text_cursor[1] = ctx->cursor[1]; - text_cursor[2] = 7*scale; - text_cursor[3] = 7*scale; + text_cursor[2] = scale*8; + text_cursor[3] = scale*8; - u32 current_colour = 0xffffffff; + u32 current_colour = ctx->override_colour; const char *_c = str; char c; @@ -453,13 +584,19 @@ static void ui_text( ui_ctx *ctx, const char *str, ui_px scale, int alignment ) { u8 glyph_base[2]; u8 glyph_index = c - 32; - glyph_base[0] = glyph_index%10; - glyph_base[1] = (glyph_index-glyph_base[0])/10; + glyph_base[0] = glyph_index&0xf; + glyph_base[1] = (glyph_index-glyph_base[0])>>4; - glyph_base[0] *= 7; - glyph_base[1] *= 7; + glyph_base[0] *= 8; + glyph_base[1] *= 8; - ui_fill_rect_uv( ctx, text_cursor, current_colour, (ui_px[4]){glyph_base[0],glyph_base[1],glyph_base[0]+7,glyph_base[1]+7} ); + ui_fill_rect_uv( ctx, text_cursor, current_colour, + (ui_px[4]){ + glyph_base[0], + 128-glyph_base[1], + glyph_base[0]+8, + 128-(glyph_base[1]+8) + }); } else if( c == '\x1B' ) { @@ -496,9 +633,10 @@ static void ui_text( ui_ctx *ctx, const char *str, ui_px scale, int alignment ) break; } } + continue; } - text_cursor[0] += 6*scale; + text_cursor[0] += (ui_glyph_spacing_x*scale)/2; } } @@ -519,6 +657,11 @@ static void ui_begin( ui_ctx *ctx, ui_px res_x, ui_px res_y ) ctx->num_verts = 0; ctx->num_indices = 0; + + ui_release_clip( ctx ); + + if( ctx->click_state == 0 ) + ctx->capture_mouse_id = 0; } static void ui_resolve( ui_ctx *ctx ) @@ -555,6 +698,14 @@ struct ui_window ui_px drag_offset[2]; }; +enum button_state +{ + k_button_released = 0, + k_button_start_click, + k_button_click, + k_button_hold +}; + static int ui_button( ui_ctx *ctx, u32 id ) { ui_new_node( ctx ); @@ -563,20 +714,23 @@ static int ui_button( ui_ctx *ctx, u32 id ) if( ui_hasmouse(ctx) ) { - ui_fill_rect( ctx, ctx->cursor, 0xffcccccc ); - + ui_fill_rect( ctx, ctx->cursor, ctx->colours_current->hover ); + if( ctx->click_state == 1 ) - ctx->capture_lock = 1; - else if( ctx->capture_lock && ctx->click_state == 3 ) { - return 1; + ctx->capture_lock = 1; + return k_button_start_click; } + else if( ctx->capture_lock && ctx->click_state == 3 ) + return k_button_click; + else if( ctx->capture_lock && ctx->click_state == 2 ) + return k_button_hold; } else - ui_fill_rect( ctx, ctx->cursor, 0xff999999 ); + ui_fill_rect( ctx, ctx->cursor, ctx->colours_current->main ); } - return 0; + return k_button_released; } static int ui_window( ui_ctx *ctx, struct ui_window *window, u32 control_group ) @@ -602,8 +756,6 @@ static int ui_window( ui_ctx *ctx, struct ui_window *window, u32 control_group ) { ui_capture_mouse( ctx, __COUNTER__ ); - //ui_fill_rect( ctx, ctx->cursor, 0xff333333 ); - // Drag bar ctx->cursor[3] = 25; ui_new_node( ctx ); @@ -654,99 +806,78 @@ static int ui_window( ui_ctx *ctx, struct ui_window *window, u32 control_group ) return 1; } -static void ui_test(void) -{ - /* - +------------------------------------------------------+ - | Central Market [x]| - +------+--------------+-+------------------------------+ - | Buy | Balance |#| [filters] [favorites] | - | <>_ | () 2,356 |#|----------------------------+-+ - |------|--------------|#| [] potion of madness 4 |#| - | Sell | \ Main sword |#|----------------------------|#| - | _*^ |--------------|#| [] Balance of time 23 | | - |------| * Side arm |#|----------------------------| | - | 235 |--------------| | [] Strength 5,300 | | - | | () Sheild | |----------------------------| | - | |--------------| | [] Bewilder 2,126 | | - | [ & Spells ] |----------------------------| | - | |--------------| | [] Eternal flames 6 | | - +------+--------------+-+----------------------------+-+ - */ - - ui_begin( &ui_global_ctx, vg_window_x, vg_window_y ); - - // TODO: Find a more elegent form for this - int mouse_state = 0; - if( vg_get_button( "primary" ) ) mouse_state = 2; - if( vg_get_button_down( "primary" ) ) mouse_state = 1; - if( vg_get_button_up( "primary" ) ) mouse_state = 3; - - ui_set_mouse( &ui_global_ctx, vg_mouse[0], vg_mouse[1], mouse_state ); +struct ui_scrollbar +{ + int drag; + ui_px drag_offset; - /* - static struct ui_window window = + ui_px py; + ui_px bar_height; + ui_px view_height; +}; + +static void ui_scrollbar( ui_ctx *ctx, struct ui_scrollbar *scrollbar, u32 id ) +{ + scrollbar->view_height = ctx->cursor[3]; + + if( scrollbar->drag ) { - .transform = { 20, 20, 500, 350 }, - .title = "Central Market" - }; + scrollbar->py = ctx->mouse[1]+scrollbar->drag_offset; + scrollbar->py = VG_MAX( scrollbar->py, 0 ); + scrollbar->py = VG_MIN( scrollbar->py, ctx->cursor[3] - scrollbar->bar_height ); + + if( ctx->click_state == 0 || ctx->click_state == 3 ) + scrollbar->drag = 0; + } - if( ui_window( &ui_global_ctx, &window, __COUNTER__ ) ) + ui_new_node( ctx ); { - // Contents - //ui_text( &ui_global_ctx, "A slice of heaven. O for awesome, this chocka \nfull cuzzie is as rip-off as a cracker.\nMeanwhile, in behind the bicycle shed, Hercules Morse,\nas big as a horse and Mrs Falani were up to no \ngood with a bunch of crook pikelets. Meanwhile, \nat the black singlet woolshed party, not even au,\nsort your drinking out.", 1, 0 ); - ui_global_ctx.cursor[2] = 75; - ui_fill_y( &ui_global_ctx ); + ui_fill_rect( ctx, ctx->cursor, ctx->colours_current->background ); + ui_capture_mouse( ctx, id ); - ui_new_node( &ui_global_ctx ); + ctx->cursor[1] += scrollbar->py; + ctx->cursor[3] = scrollbar->bar_height; + + ui_new_node( ctx ); { - ui_global_ctx.cursor[3] = 75; - - if( ui_button( &ui_global_ctx, __COUNTER__ ) ) - vg_info( "Buy\n" ); + ui_capture_mouse( ctx, __COUNTER__ ); + struct ui_vert *drag_bar = ui_fill_rect( ctx, ctx->cursor, ctx->colours_current->bar ); + + if( ui_hasmouse( ctx ) || scrollbar->drag ) { - ui_rect_pad( ui_global_ctx.cursor, 4 ); - ui_text( &ui_global_ctx, "Buy", 2, 0 ); - } - ui_end_down( &ui_global_ctx ); + drag_bar[0].colour = ctx->colours_current->bar_hover; + drag_bar[1].colour = ctx->colours_current->bar_hover; + drag_bar[2].colour = ctx->colours_current->bar_hover; + drag_bar[3].colour = ctx->colours_current->bar_hover; - if( ui_button( &ui_global_ctx, __COUNTER__ ) ) - vg_info( "Sell\n" ); - { - ui_rect_pad( ui_global_ctx.cursor, 4 ); - ui_text( &ui_global_ctx, "Sell", 2, 0 ); + // start drag + if( ctx->click_state == 1 ) + { + scrollbar->drag = 1; + scrollbar->drag_offset = scrollbar->py - ctx->mouse[1]; + } } - ui_end_down( &ui_global_ctx ); - } - ui_end_right( &ui_global_ctx ); - - ui_global_ctx.cursor[2] = 200; - ui_new_node( &ui_global_ctx ); - { - } - ui_end_right( &ui_global_ctx ); + ui_end_down( ctx ); } - ui_end( &ui_global_ctx ); - */ - - ui_rect rbf; - ui_rect_copy( ui_global_ctx.cursor, rbf ); - - ui_global_ctx.cursor[0] = 6; - ui_global_ctx.cursor[3] = 21; - ui_fill_x( &ui_global_ctx ); - ui_align_bottom( &ui_global_ctx ); - ui_text( &ui_global_ctx, "Marble computer build 2 -- development version -- (C) Harry Godden 2021\nAknowledgements: 'credits' in console (`)", 1, 0 ); - - ui_resolve( &ui_global_ctx ); - - m3x3f view = M3X3_IDENTITY; - m3x3_translate( view, (v3f){ -1.0f, 1.0f, 0.0f } ); - m3x3_scale( view, (v3f){ 1.0f/((float)vg_window_x*0.5f), -1.0f/((float)vg_window_y*0.5f), 1.0f } ); - //vg_lines_drawall( (float*)view ); - - ui_draw( &ui_global_ctx ); + ui_end( ctx ); +} + +static ui_px ui_calculate_content_scroll( struct ui_scrollbar *scrollbar, ui_px content ) +{ + float overlap = vg_maxf( 0.0f, (float)(content - scrollbar->view_height) ); - ui_rect_copy( rbf, ui_global_ctx.cursor ); + float range = scrollbar->view_height - scrollbar->bar_height; + return ((float)scrollbar->py / range) * overlap; +} + +static void ui_override_colours( ui_ctx *ctx, ui_colourset *set ) +{ + ctx->colours_current = set; +} + +static void ui_reset_colours( ui_ctx *ctx ) +{ + ctx->colours_current = ctx->colours_main; + ctx->override_colour = 0xffffffff; }