From 7ca87a07f90ed9ee8bf1ae4b3d1ac9db30a21ee5 Mon Sep 17 00:00:00 2001 From: hgn Date: Thu, 20 Mar 2025 03:15:09 +0000 Subject: [PATCH] dir API change, v4_fill, ui API change --- vg_image.h | 3 ++- vg_io.c | 10 +++++++- vg_io.h | 3 ++- vg_m.h | 8 +++++++ vg_ui/filebrowser.c | 58 ++++++++++++++++++++++++++++++++++++--------- vg_ui/filebrowser.h | 3 +++ vg_ui/imgui.c | 22 +++++++++-------- vg_ui/imgui.h | 4 +--- 8 files changed, 84 insertions(+), 27 deletions(-) diff --git a/vg_image.h b/vg_image.h index 192909e..880df61 100644 --- a/vg_image.h +++ b/vg_image.h @@ -1,5 +1,6 @@ #pragma once #include "vg/submodules/stb/stb_image_write.h" -#define STBI_ONLY_JPEG + +//#define STBI_ONLY_JPEG #define STBI_NO_THREAD_LOCALS #include "vg/submodules/stb/stb_image.h" diff --git a/vg_io.c b/vg_io.c index da4dd39..206b815 100644 --- a/vg_io.c +++ b/vg_io.c @@ -26,6 +26,9 @@ enum dir_open_result vg_dir_open( vg_dir *dir, const char *name ) if( !vg_strgood(&q) ) return k_dir_open_path_too_long; + if( !(GetFileAttributes( q.buffer ) & FILE_ATTRIBUTE_DIRECTORY) ) + return k_dir_open_is_file; + vg_info( "FindFirstFile( '%s' )\n", q.buffer ); dir->h = FindFirstFile( q.buffer, &dir->data ); if( dir->h == INVALID_HANDLE_VALUE ) @@ -41,7 +44,12 @@ enum dir_open_result vg_dir_open( vg_dir *dir, const char *name ) #else dir->h = opendir( name ); if( !dir->h ) - return k_dir_open_invalid_path; + { + if( errno == ENOTDIR ) + return k_dir_open_is_file; + else + return k_dir_open_invalid_path; + } #endif dir->index = 1; return k_dir_open_ok; diff --git a/vg_io.h b/vg_io.h index 8f81365..3c73de5 100644 --- a/vg_io.h +++ b/vg_io.h @@ -34,7 +34,8 @@ enum dir_open_result k_dir_open_none, k_dir_open_ok, k_dir_open_path_too_long, - k_dir_open_invalid_path + k_dir_open_invalid_path, + k_dir_open_is_file }; extern const char *dir_open_result_str[]; enum dir_open_result vg_dir_open( vg_dir *dir, const char *name ); diff --git a/vg_m.h b/vg_m.h index 5c1cb2d..0966b4e 100644 --- a/vg_m.h +++ b/vg_m.h @@ -374,6 +374,14 @@ static inline void v3_fill( v3f a, f32 v ) a[2] = v; } +static inline void v4_fill( v4f a, f32 v ) +{ + a[0] = v; + a[1] = v; + a[2] = v; + a[3] = v; +} + static inline void v3_divs( v3f a, f32 s, v3f d ) { if( s == 0.0f ) diff --git a/vg_ui/filebrowser.c b/vg_ui/filebrowser.c index 399a522..94cc9e7 100644 --- a/vg_ui/filebrowser.c +++ b/vg_ui/filebrowser.c @@ -14,6 +14,30 @@ void vg_filebrowser_init( struct vg_filebrowser *browser ) browser->filter = 0; } +void vg_filebrowser_set_path_to_home( struct vg_filebrowser *browser ) +{ + // TODO + strcpy( browser->current_path, "/home/harry" ); +} + +static bool vg_filebrowser_up( struct vg_filebrowser *browser ) +{ + vg_str str = + { + .buffer = browser->current_path, + .i = strlen( browser->current_path ), + .len = sizeof(browser->current_path)-1 + }; + + char *sep = vg_strch( &str, '/' ); + if( sep ) + { + sep[0] = '\0'; + return 1; + } + else return 0; +} + void filebrowser_textbox_callback( ui_context *ctx, char *buffer, u32 len, void *userdata ) { struct vg_filebrowser *browser = userdata; @@ -61,17 +85,8 @@ enum filebrowser_action vg_filebrowser_ui( ui_context *ctx, ui_rect root_rect, s if( ui_button_text( ctx, box_up, "\xb8", 1 ) == k_ui_button_click ) { - vg_str str = - { - .buffer = browser->current_path, - .i = strlen( browser->current_path ), - .len = sizeof(browser->current_path)-1 - }; - - char *sep = vg_strch( &str, '/' ); - if( sep ) + if( vg_filebrowser_up( browser ) ) { - sep[0] = '\0'; vg_filebrowser_free_entries( browser ); vg_filebrowser_populate( browser ); } @@ -241,6 +256,7 @@ enum filebrowser_action vg_filebrowser_ui( ui_context *ctx, ui_rect root_rect, s if( ui_button_text( ctx, cancel_box, "Cancel", 1 ) == k_ui_button_click ) { + vg_filebrowser_free_entries( browser ); result_action = k_filebrowser_action_escape; } @@ -248,6 +264,16 @@ enum filebrowser_action vg_filebrowser_ui( ui_context *ctx, ui_rect root_rect, s { if( ui_button_text( ctx, ok_box, "OK", 1 ) == k_ui_button_click ) { + u32 cur_len = strlen( browser->current_path ); + if( (cur_len + strlen( browser->selected_entry->name ) + 2) > sizeof(browser->current_path) ) + vg_fatal_error( "Max path size exceeded." ); + else + { + strcat( browser->current_path, "/" ); + strcat( browser->current_path, browser->selected_entry->name ); + } + + vg_filebrowser_free_entries( browser ); result_action = k_filebrowser_action_accept; } } @@ -300,8 +326,18 @@ void vg_filebrowser_populate( struct vg_filebrowser *browser ) *folder_list_tail = NULL, *file_list_head = NULL; + if( browser->open_result == k_dir_open_is_file ) + { + if( vg_filebrowser_up( browser ) ) + { + browser->open_result = vg_dir_open( &dir, browser->current_path ); + } + } + if( browser->open_result != k_dir_open_ok ) + { return; + } while( vg_dir_next_entry( &dir ) ) { @@ -326,7 +362,7 @@ void vg_filebrowser_populate( struct vg_filebrowser *browser ) if( ext ) { if( !strcmp( ext, "jpg" ) || !strcmp( ext, "jpeg" ) || !strcmp( ext, "png" ) || !strcmp( ext, "tga" ) || - !strcmp( ext, "bmp" ) ) + !strcmp( ext, "bmp" ) || !strcmp( ext, "psd" ) ) { media_type = k_media_type_image; } diff --git a/vg_ui/filebrowser.h b/vg_ui/filebrowser.h index ef552d5..9b0ec2c 100644 --- a/vg_ui/filebrowser.h +++ b/vg_ui/filebrowser.h @@ -29,6 +29,7 @@ enum filebrowser_action struct vg_filebrowser { char current_path[ 4096 ]; + enum dir_open_result open_result; struct vg_filebrowser_entry *whole_list, *view_top_entry, *selected_entry; @@ -49,3 +50,5 @@ void vg_filebrowser_init( struct vg_filebrowser *browser ); enum filebrowser_action vg_filebrowser_ui( ui_context *ctx, ui_rect root_rect, struct vg_filebrowser *browser ); void vg_filebrowser_populate( struct vg_filebrowser *browser ); void vg_filebrowser_free_entries( struct vg_filebrowser *browser ); + +void vg_filebrowser_set_path_to_home( struct vg_filebrowser *browser ); diff --git a/vg_ui/imgui.c b/vg_ui/imgui.c index bddbbcd..cd758f6 100644 --- a/vg_ui/imgui.c +++ b/vg_ui/imgui.c @@ -613,10 +613,11 @@ u32 ui_text( ui_context *ctx, void ui_panel( ui_context *ctx, ui_rect in_rect, ui_rect out_panel ) { - ui_fill( ctx, in_rect, ui_colour(ctx, k_ui_bg+1 ) ); + //ui_fill( ctx, in_rect, ui_colour(ctx, k_ui_bg+1 ) ); + ui_fill( ctx, in_rect, ui_opacity( ui_colour( ctx, k_ui_bg+1 ), 0.7f ) ); ui_outline( ctx, in_rect, 1, ui_colour(ctx, k_ui_bg+7 ), 0 ); rect_copy( in_rect, out_panel ); - ui_rect_pad( out_panel, NULL ); + ui_rect_pad( out_panel, (ui_px[2]){ 8, 8 } ); } void ui_label( ui_context *ctx, @@ -927,9 +928,13 @@ int ui_checkbox( ui_context *ctx, void ui_enum( ui_context *ctx, ui_rect inout_panel, const char *str_label, struct ui_enum_opt *options, u32 len, i32 *value ) { - ui_rect rect, label, box; + ui_rect rect, box; ui_standard_widget( ctx, inout_panel, rect, 1 ); - ui_label( ctx, rect, str_label, ctx->scale, 0, box ); + + if( str_label ) + ui_label( ctx, rect, str_label, ctx->scale, 0, box ); + else + rect_copy( rect, box ); const char *display = "OUT OF RANGE"; int valid = 0; @@ -1022,8 +1027,7 @@ enum ui_button_state ui_slider_base( ui_context *ctx, ui_rect box, enum ui_axis return state; } -void ui_slider_text( ui_context *ctx, - ui_rect box, const char *format, f32 value ) +void ui_slider_text( ui_context *ctx, ui_rect box, const char *format, f32 value ) { /* TODO: replace this one day with our own function */ char buf[32]; @@ -1031,9 +1035,7 @@ void ui_slider_text( ui_context *ctx, ui_text( ctx, box, buf, 1, k_ui_align_middle_center, 0 ); } -bool ui_slider_standard( ui_context *ctx, - ui_rect box, f32 min, f32 max, f32 *value, - const char *format ) +bool ui_slider_standard( ui_context *ctx, ui_rect box, f32 min, f32 max, f32 *value, const char *format ) { f32 t; @@ -1048,7 +1050,7 @@ bool ui_slider_standard( ui_context *ctx, ui_fill( ctx, line, ui_colour(ctx,state&mask_brighter? k_ui_bg+4: k_ui_bg+2) ); ui_fill( ctx, (ui_rect){ box[0]+line[2], box[1], box[2]-line[2], box[3] }, ui_colour(ctx, k_ui_bg ) ); ui_outline( ctx, box, 1, ui_colour(ctx,state? k_ui_fg+3: k_ui_bg+3), 0 ); - ui_slider_text( ctx, box, NULL, *value ); + ui_slider_text( ctx, box, format, *value ); return (state & mask_using) && 1; } diff --git a/vg_ui/imgui.h b/vg_ui/imgui.h index 1f40553..b64fd3d 100644 --- a/vg_ui/imgui.h +++ b/vg_ui/imgui.h @@ -302,9 +302,7 @@ u32 ui_opacity( u32 colour, f32 opacity ); ui_px ui_standard_widget_height( ui_context *ctx, ui_px count ); void ui_standard_widget( ui_context *ctx, ui_rect inout_panel, ui_rect out_rect, ui_px count ); void ui_panel( ui_context *ctx, ui_rect in_rect, ui_rect out_panel ); -void ui_label( ui_context *ctx, - ui_rect rect, const char *text, ui_px size, - ui_px gap, ui_rect r ); +void ui_label( ui_context *ctx, ui_rect rect, const char *text, ui_px size, ui_px gap, ui_rect r ); void ui_info( ui_context *ctx, ui_rect inout_panel, const char *text ); void ui_image( ui_context *ctx, ui_rect rect, void *resource ); -- 2.25.1