From: hgn Date: Mon, 8 May 2023 04:29:26 +0000 (+0100) Subject: incomplete ui upgrades X-Git-Url: https://skaterift.com/git/?a=commitdiff_plain;h=fb4da0217c89131c17e33d03804ebd46b9232897;p=vg.git incomplete ui upgrades --- diff --git a/vg_imgui.h b/vg_imgui.h index f865105..244a3f7 100644 --- a/vg_imgui.h +++ b/vg_imgui.h @@ -97,15 +97,21 @@ struct{ void *focused_control_id; /* uses the memory location of various locking controls as an id */ char *textbuf; + struct ui_dropdown_value{ + i32 index, value; + } + *ptr_dropdown; }; u32 focused_control_hit; enum ui_control_type{ k_ui_control_none, k_ui_control_textbox, + k_ui_control_dropdown } focused_control_type; + union{ struct ui_textbuf{ int cursor_user, cursor_pos; u32 len; @@ -120,6 +126,18 @@ struct{ callbacks; } textbox; + + struct ui_dropdown{ + struct ui_dropdown_opt{ + const char *alias; + i32 value; + } + *options; + u32 option_count; + ui_rect rect; + } + dropdown; + }; GLuint tex_glyphs, vao, vbo, ebo; @@ -281,16 +299,6 @@ static struct vg_shader _shader_ui_image = "void main()" "{" "vec4 colour = texture( uTexImage, aTexCoords );" - - /* wtf is this?? */ -#if 0 - "float value = dot(vec4(1.0),colour)*0.25;" - - "vec3 col = vec3(pow(cos(value*3.14159265*2.0)*0.5+0.5,0.5))" - "* vec3(step(value,0.5),0.3,step(1.0-value,0.5));" - "FragColor = vec4( col*4.0, 1.0 );" -#endif - "FragColor = colour;" "}" } @@ -442,6 +450,7 @@ VG_STATIC void ui_flush( enum ui_shader shader ) glEnable( GL_BLEND ); glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ); glBlendEquation( GL_FUNC_ADD ); + glDisable( GL_CULL_FACE ); m3x3f view = M3X3_IDENTITY; m3x3_translate( view, (v3f){ -1.0f, 1.0f, 0.0f } ); @@ -568,23 +577,33 @@ static void ui_outline( ui_rect rect, ui_px thickness, u32 colour ) vg_ui.cur_vert += 8; } -static void ui_split_px( ui_rect rect, - enum ui_axis other, ui_px width, ui_px pad, - ui_rect l, ui_rect r ) +static void ui_split( ui_rect rect, + enum ui_axis other, ui_px width, ui_px gap, + ui_rect l, ui_rect r ) { enum ui_axis dir = other ^ 0x1; + if( width < 0 ) width = rect[ 2+dir ] + width; + ui_rect temp; rect_copy( rect, temp ); - l[ dir ] = temp[ dir ] + pad; - r[ dir ] = temp[ dir ] + width + (pad/2); - l[ other ] = temp[ other ] + pad; - r[ other ] = temp[ other ] + pad; - l[ 2+dir ] = width - ((3*pad)/2); - r[ 2+dir ] = temp[ 2+dir ] - width - ((3*pad)/2); - l[ 2+other ] = temp[ 2+other ] - pad*2; - r[ 2+other ] = temp[ 2+other ] - pad*2; + l[ dir ] = temp[ dir ]; + r[ dir ] = temp[ dir ] + width + (gap/2); + l[ other ] = temp[ other ]; + r[ other ] = temp[ other ]; + l[ 2+dir ] = width - (gap/2); + r[ 2+dir ] = temp[ 2+dir ] - width - (gap/2); + l[ 2+other ] = temp[ 2+other ]; + r[ 2+other ] = temp[ 2+other ]; +} + +static ui_px ui_text_line_width( const char *str ); +static void ui_split_label( ui_rect rect, const char *text, ui_px size, + ui_px gap, ui_rect l, ui_rect r ) +{ + ui_px width = ui_text_line_width( text ) * size; + ui_split( rect, k_ui_axis_v, width, gap, l, r ); } static void ui_rect_center( ui_rect parent, ui_rect rect ) @@ -610,18 +629,18 @@ static void ui_fit_item( ui_rect rect, ui_px size[2], ui_rect d ) } static void ui_split_ratio( ui_rect rect, enum ui_axis dir, float ratio, - ui_px pad, ui_rect l, ui_rect r ) + ui_px gap, ui_rect l, ui_rect r ) { ui_px width = (float)rect[ 2+(dir^0x1) ] * ratio; - ui_split_px( rect, dir, width, pad, l, r ); + ui_split( rect, dir, width, gap, l, r ); } -static void ui_rect_pad( ui_rect rect, ui_px pad ) +static void ui_rect_pad( ui_rect rect, ui_px pad[2] ) { - rect[0] += pad; - rect[1] += pad; - rect[2] -= pad*2; - rect[3] -= pad*2; + rect[0] += pad[0]; + rect[1] += pad[1]; + rect[2] -= pad[0]*2; + rect[3] -= pad[1]*2; } static ui_px ui_text_line_width( const char *str ) @@ -914,7 +933,6 @@ static void ui_text( ui_rect rect, const char *str, ui_px scale, static void ui_image( ui_rect rect, GLuint image ) { ui_flush( k_ui_shader_colour ); - glActiveTexture( GL_TEXTURE0 ); glBindTexture( GL_TEXTURE_2D, image ); ui_fill_rect( rect, 0xffffffff, (ui_px[4]){ 0,128, 128, 0 } ); @@ -953,6 +971,13 @@ static int ui_colourbutton( ui_rect rect, enum ui_scheme_colour colour ) col_highlight = vg_ui.scheme[ k_ui_fg ], col_hover = vg_ui.scheme[ colour + k_ui_brighter ]; + if( vg_ui.focused_control_type != k_ui_control_none ){ + clickup = 0; + click = 0; + target = 0; + hover = 0; + } + if( hover ){ vg_ui.cursor = k_ui_cursor_hand; } @@ -1016,10 +1041,10 @@ static int ui_button_text( ui_rect rect, const char *string, ui_px scale ) return ui_colourbutton_text( rect, string, scale, k_ui_bg+4 ); } +static void ui_enum_post(void); static void ui_postrender(void) { if( vg_ui.click_fade_opacity > 0.0f ){ - float scale = vg_ui.click_fade_opacity; scale = vg_maxf( 1.0f/255.0f, scale*scale ); @@ -1041,6 +1066,11 @@ static void ui_postrender(void) ui_fill( rect, colour ); } + + if( vg_ui.focused_control_type == k_ui_control_dropdown ){ + ui_enum_post(); + } + ui_flush( k_ui_shader_colour ); if( !vg_ui.focused_control_hit ){ @@ -1077,12 +1107,12 @@ static void ui_dev_colourview(void) "k_ui_aqua+8","k_ui_blue+8","k_ui_purple+8","k_ui_gray+8" }; ui_rect col[2]; - ui_split_ratio( window, k_ui_axis_v, 0.5f, 0.0f, col[0], col[1] ); + ui_split_ratio( window, k_ui_axis_v, 0.5f, 0, col[0], col[1] ); for( int i=0; iindex ].alias, 1 ) ){ + vg_ui.focused_control_type = k_ui_control_dropdown; + vg_ui.ptr_dropdown = value; + vg_ui.dropdown.option_count = len; + vg_ui.dropdown.options = options; + rect_copy( box, vg_ui.dropdown.rect ); + } +} + +static void ui_enum_post(void) +{ + ui_rect drawer; + rect_copy( vg_ui.dropdown.rect, drawer ); + drawer[3] *= vg_ui.dropdown.option_count; + + int close = 0; + int clickany= ui_click_up(UI_MOUSE_LEFT|UI_MOUSE_RIGHT|UI_MOUSE_MIDDLE), + hover = ui_inside_rect( drawer, vg_ui.mouse ); + + if( clickany && !hover ){ + return; + } + + /* HACK */ + vg_ui.focused_control_type = k_ui_control_none; + struct ui_dropdown_value *value = vg_ui.ptr_dropdown; + + for( u32 i=0; iindex ) colour = k_ui_orange; + + if( ui_colourbutton_text( button, vg_ui.dropdown.options[i].alias, 1, + colour ) ){ + value->index = i; + value->value = vg_ui.dropdown.options[i].value; + close = 1; + } + } + + /* HACK */ + vg_ui.focused_control_type = k_ui_control_dropdown; + + if( !close ) + vg_ui.focused_control_hit = 1; +} + /* * Textbox chaos * ----------------------------------------------------------------------------- @@ -1514,10 +1611,21 @@ static int ui_textbox( ui_rect rect, char *buf, u32 len, u32 flags, struct ui_textbox_callbacks *callbacks ) { int clickup= ui_click_up(UI_MOUSE_LEFT), + clickdown = ui_click_down(UI_MOUSE_LEFT), click = ui_clicking(UI_MOUSE_LEFT) | clickup, target = ui_inside_rect( rect, vg_ui.mouse_click ) && click, hover = ui_inside_rect( rect, vg_ui.mouse ); + /* allow instant transitions from textbox->textbox */ + if( (vg_ui.focused_control_type != k_ui_control_none) && + (vg_ui.focused_control_type != k_ui_control_textbox) ){ + clickup = 0; + clickdown = 0; + click = 0; + target = 0; + hover = 0; + } + u32 col_base = ui_colour( k_ui_bg ), col_highlight = ui_colour( k_ui_fg ), col_cursor = (0x00ffffff & ui_colour(k_ui_fg))|0x7f000000; @@ -1546,8 +1654,7 @@ static int ui_textbox( ui_rect rect, char *buf, u32 len, u32 flags, ui_fill( rect, col_base ); ui_ntext( text_rect, buf, wrap_length, 1, k_ui_align_left, 0 ); - if( !(flags & UI_TEXTBOX_AUTOFOCUS) && - ((clickup||ui_click_down(UI_MOUSE_LEFT)) && !target) ){ + if( !(flags & UI_TEXTBOX_AUTOFOCUS) && ((clickup||clickdown) && !target)){ ui_defocus_all(); } else{ @@ -1605,9 +1712,6 @@ static int ui_textbox( ui_rect rect, char *buf, u32 len, u32 flags, } else{ while( remaining ){ - /* TODO: scan for newlines and stuff - * eol or wrap_length can have line breaks! */ - int run = _ui_textbox_run_remaining( pos, wrap_length ); run = VG_MIN( run, remaining ); diff --git a/vg_log.h b/vg_log.h index cc42761..72dd82c 100644 --- a/vg_log.h +++ b/vg_log.h @@ -21,6 +21,12 @@ #define PRINTF_v3f( V3 ) "%.4f %.4f %.4f\n", V3[0], V3[1], V3[2] #define PRINTF_v4f( V4 ) "%.4f %.4f %.4f %.4f\n", V4[0], V4[1], V4[2], V4[3] +#ifdef _WIN32 + #define PRINTF_U64 "%llu" +#else + #define PRINTF_U64 "%lu" +#endif + #ifdef VG_GAME static SDL_SpinLock log_print_sl; #endif diff --git a/vg_tex.h b/vg_tex.h index 125b43e..14042a5 100644 --- a/vg_tex.h +++ b/vg_tex.h @@ -127,6 +127,10 @@ struct texture_load_info{ VG_STATIC void async_vg_tex2d_upload( void *payload, u32 size ) { + if( vg_thread_purpose() != k_thread_purpose_main ){ + vg_fatal_error( "Catastrophic programming error.\n" ); + } + struct texture_load_info *info = payload; glGenTextures( 1, info->dest );