dir API change, v4_fill, ui API change
authorhgn <hgodden00@gmail.com>
Thu, 20 Mar 2025 03:15:09 +0000 (03:15 +0000)
committerhgn <hgodden00@gmail.com>
Thu, 20 Mar 2025 03:15:09 +0000 (03:15 +0000)
vg_image.h
vg_io.c
vg_io.h
vg_m.h
vg_ui/filebrowser.c
vg_ui/filebrowser.h
vg_ui/imgui.c
vg_ui/imgui.h

index 192909eb917b032de4c9b45a4882fc5a7cc6c2bc..880df61e732c09846db4f0d863bff7ace2a5c732 100644 (file)
@@ -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 da4dd39f584737f0e68b902fba7b4ec46e217d77..206b815e56b9902a049569afae3093089a2aea03 100644 (file)
--- 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 8f81365c781c7fac66cd3c1298e0f98ac08b4892..3c73de5c31ea6ce1d990c2e137002950f036ed3d 100644 (file)
--- 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 5c1cb2dfe21f926a943a9194056cc048c3855b59..0966b4ea892c29aa445bd8a6fa270beb4b50d783 100644 (file)
--- 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 )
index 399a5229b5a966951534055c7012c194c3a0298b..94cc9e7febb5b334be4c6a8fe79af62ece1647a3 100644 (file)
@@ -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;
             }
index ef552d5f7d0a5e46ca3d041dd108d63ab3ca97bb..9b0ec2cade6e14f15c91e678ac875ebc4cc8b4f6 100644 (file)
@@ -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 );
index bddbbcde6a7a39f765b45d1a6a92753bccdd84fb..cd758f607d2443557d124d630a8eb18d2fb8caa3 100644 (file)
@@ -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;
 }
index 1f405539769ff2b1c62eb093be30180e4ab9b87e..b64fd3d1c07826decdd347cd3a4748032cd02d86 100644 (file)
@@ -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 );