patch up things from text changes
[fishladder.git] / vg / vg_console.h
index 94de7d205db4c8d46b65eeac44daddf4de837219..42b94f2066ab3b01e1f5332c0696db0d1b1eaad6 100644 (file)
@@ -1,3 +1,5 @@
+// Copyright (C) 2021 Harry Godden (hgn) - All Rights Reserved
+
 struct vg_console
 {
        struct vg_convar
@@ -20,9 +22,18 @@ struct vg_console
                                int min, max, clamp;
                        } opt_i32;
                };
+
+               int persistent; // Should be stored to cfg/auto.conf
        } 
        *convars;
        
+       struct vg_cmd
+       {
+               int (*function)( int argc, char const *argv[] );
+               const char *name;
+       }
+       *functions;
+       
        char lines[16][512];
        u32 current, len;
        
@@ -37,61 +48,102 @@ struct vg_console
 }
 vg_console = { .scale = 1 };
 
-static int vg_console_enabled(void) { return vg_console.enabled; }
+// Declerations
+// ------------
+
+// Registration
+static void vg_convar_push( struct vg_convar cv );
+static void vg_function_push( struct vg_cmd cmd );
+
+static void vg_console_draw( void );
+void vg_console_println( const char *str );
+static int vg_console_list( int argc, char const *argv[] );
+static void vg_console_init(void);
+static void vg_console_write_persistent(void);
+static void vg_console_free(void);
+static void execute_console_input( const char *cmd );
+
+// Console interface
+// -----------------
+
+static void console_make_selection( int* start, int* end );
+static void console_move_cursor( int* cursor0, int* cursor1, int dir, int snap_together );
+static int console_makeroom( int datastart, int length );
+static int console_delete_char( int direction );
+static void console_to_clipboard(void);
+static void console_clipboard_paste(void);
+static void console_put_char( char c );
+static void console_history_get( char* buf, int entry_num );
+static void console_proc_key( GLFWwindow* ptrW, int key, int scancode, int action, int mods );
+static void console_proc_wchar( GLFWwindow* ptrW, u32 uWchar );
+static int vg_console_enabled(void);
+
+// =========================================================================================================
+// Implementation
+
+static int vg_console_enabled(void) 
+{ 
+       return vg_console.enabled; 
+}
 
 static void vg_convar_push( struct vg_convar cv )
 {
        arrpush( vg_console.convars, cv ); 
 }
 
+static void vg_function_push( struct vg_cmd cmd )
+{
+       arrpush( vg_console.functions, cmd );
+}
+
 static void vg_console_draw( void )
 {
        if( !vg_console.enabled )
                return;
 
-       int ptr = vg_console.current - vg_console.len;
-       if( ptr <= 0 )
-               ptr += vg_list_size( vg_console.lines );
-       ptr --;
+       int ptr = vg_console.current-1;
+
+   int const fh = 14;
        
        ui_global_ctx.cursor[0] = 0;
        ui_global_ctx.cursor[1] = 0;
-       ui_global_ctx.cursor[3] = vg_console.len*8*vg_console.scale;
+       ui_global_ctx.cursor[3] = vg_list_size( vg_console.lines )*fh*vg_console.scale;
        ui_fill_x( &ui_global_ctx );
        
        ui_new_node( &ui_global_ctx );
-       {               
+       {
                ui_fill_rect( &ui_global_ctx, ui_global_ctx.cursor, 0x77333333 );
        
-               ui_global_ctx.cursor[3] = 8*vg_console.scale;
+               ui_global_ctx.cursor[3] = fh*vg_console.scale;
                ui_align_bottom( &ui_global_ctx ); 
                
                for( int i = 0; i < vg_console.len; i ++ )
                {
-                       ui_text( &ui_global_ctx, vg_console.lines[ptr], vg_console.scale, 0 );
-                       ui_global_ctx.cursor[1] -= 8*vg_console.scale;
-               
-                       ptr --;
                        if( ptr < 0 )
                                ptr = vg_list_size( vg_console.lines )-1;
+         
+                       ui_text( &ui_global_ctx, vg_console.lines[ptr], vg_console.scale );
+                       ui_global_ctx.cursor[1] -= fh*vg_console.scale;
+               
+                       ptr --;
                }
        }
        ui_end_down( &ui_global_ctx );
        
        ui_global_ctx.cursor[1] += 2;
-       ui_global_ctx.cursor[3] = 8*vg_console.scale;
+       ui_global_ctx.cursor[3] = fh*vg_console.scale;
        
        ui_new_node( &ui_global_ctx );
        {
                ui_fill_rect( &ui_global_ctx, ui_global_ctx.cursor, 0x77333333 );
                
-               ui_text( &ui_global_ctx, vg_console.input, vg_console.scale, 0 );
+               ui_text( &ui_global_ctx, vg_console.input, vg_console.scale );
                
                int start = VG_MIN( vg_console.cursor_pos, vg_console.cursor_user ),
                         end   = VG_MAX( vg_console.cursor_pos, vg_console.cursor_user );
                
-               ui_global_ctx.cursor[0] = start * 6 * vg_console.scale;
-               ui_global_ctx.cursor[2] = (start == end? 1: (end-start)) * 6 * vg_console.scale;
+               ui_global_ctx.cursor[0] = (start * ui_glyph_spacing_x * vg_console.scale);
+               ui_global_ctx.cursor[2] = (start == end? 0.5f: (float)(end-start)) * (float)ui_glyph_spacing_x * (float)vg_console.scale;
                
                ui_fill_rect( &ui_global_ctx, ui_global_ctx.cursor, 0x66ffffff );
        }
@@ -109,6 +161,33 @@ void vg_console_println( const char *str )
                vg_console.current = 0;
 }
 
+static int vg_console_list( int argc, char const *argv[] )
+{
+       for( int i = 0; i < arrlen( vg_console.functions ); i ++ )
+       {
+               struct vg_cmd *cmd = &vg_console.functions[ i ];
+               vg_info( "* %s\n", cmd->name );
+       }
+       
+       vg_info( "* snowsound\n" );
+       
+       for( int i = 0; i < arrlen( vg_console.convars ); i ++ )
+       {
+               struct vg_convar *cv = &vg_console.convars[ i ];
+               vg_info( "%s\n", cv->name );
+       }
+       
+       return 0;
+}
+
+static int vg_console_chartest( int argc, char const *argv[] )
+{
+   vg_info( "\"THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG\"\n" );
+   vg_info( "'the quick brown fox jumps over the lazy dog'\n" );
+   vg_info( ":;!@#$%^& 0123456789 +-*/=~ ()[]{}<>\n" );
+   return 0;
+}
+
 static void vg_console_init(void)
 {
        vg_log_callback = vg_console_println;
@@ -116,66 +195,131 @@ static void vg_console_init(void)
        vg_convar_push( (struct vg_convar)
        { .name = "console_scale", .data = &vg_console.scale, .data_type = k_convar_dtype_i32, 
                .opt_i32 = { .clamp = 1, .min = 1, .max = 7 } } );
+       
+       vg_function_push( (struct vg_cmd){
+               .name = "list",
+               .function = vg_console_list
+       });
+
+   vg_function_push( (struct vg_cmd){
+      .name = "chartest",
+      .function = vg_console_chartest
+   });
+
+       // Read and exec persistent commands
+       FILE *fp = fopen( "cfg/auto.conf", "r" );
+       if( fp )
+       {
+               char line[256];
+
+               while( fgets( line, sizeof( line ), fp ) )
+               {
+                       line[ strcspn( line, "\r\n#" ) ] = 0x00;
+
+                       if( line[0] != 0x00 )
+                       {
+                               execute_console_input( line );
+                       }
+               }
+
+               fclose( fp );
+       }
+}
+
+static void vg_console_write_persistent(void)
+{
+       FILE *fp = fopen( "cfg/auto.conf", "w" );
+       
+       for( int i = 0; i < arrlen( vg_console.convars ); i ++ )
+       {
+               struct vg_convar *cv = &vg_console.convars[i];
+
+               if( cv->persistent )
+               {
+                       switch( cv->data_type )
+                       {
+                               case k_convar_dtype_i32:
+                                       fprintf( fp, "%s %d\n", cv->name, *(i32 *)(cv->data) );
+                               break;
+                               case k_convar_dtype_u32:
+                                       fprintf( fp, "%s %u\n", cv->name, *(u32 *)(cv->data) );
+                               break;
+                               case k_convar_dtype_f32:
+                                       fprintf( fp, "%s %.5f\n", cv->name, *(float *)(cv->data ) );
+                               break;
+                       }
+               }
+       }
+
+       fclose( fp );
 }
 
 static void vg_console_free(void)
 {
+       vg_console_write_persistent();
+
        arrfree( vg_console.convars );
+       arrfree( vg_console.functions );
 }
 
-// Returns advance amount
-static int console_token_read( char *buf, int buf_len, const char *src )
+static void execute_console_input( const char *cmd )
 {
-       int wait_for_next = 0;
-
-       for( int i = 0; i < buf_len; i ++ )
+       char temp[512];
+       char const *args[9];
+       int arg_count = 0;
+       
+       int in_token = 0;
+       
+       // Split string into tokens
+       for( int i = 0; i < vg_list_size( temp ); i ++ )
        {
-               if( src[i] )
+               if( cmd[i] )
                {
-                       if( isspace( src[i] ) )
+                       if( isspace( cmd[i] ) )
                        {
-                               buf[i] = '\0';
-                               wait_for_next = 1;
+                               temp[i] = '\0';
+                               in_token = 0;
+                               
+                               if( arg_count == vg_list_size( args ) )
+                                       break;
                        }
-                       else 
+                       else
                        {
-                               if( wait_for_next )
-                                       return i;
+                               temp[i] = cmd[i];
                                
-                               buf[i] = src[i];
-                       }       
+                               if( !in_token )
+                               {
+                                       args[ arg_count ++ ] = temp + i;
+                                       in_token = 1;
+                               }
+                       }
                }
                else
                {
-                       buf[i] = '\0';
-                       return i;
+                       temp[i] = '\0';
+                       break;
                }
        }
        
-       return 0;
-}
-
-static void execute_console_input( const char *cmd )
-{
-       char temp[512];
-       int cmd_pos = console_token_read( temp, 512, cmd );
+       if( arg_count == 0 )
+               return;
        
        int data_int;
        
        for( int i = 0; i < arrlen( vg_console.convars ); i ++ )
        {
                struct vg_convar *cv = &vg_console.convars[ i ];
-               if( !strcmp( cv->name, temp ) )
+               if( !strcmp( cv->name, args[0] ) )
                {
-                       // Matched, try get value
-                       if( console_token_read( temp, 512, cmd+cmd_pos ) )
+                       // Cvar Matched, try get value
+                       if( arg_count >= 2 )
                        {
                                switch( cv->data_type )
                                {
                                        case k_convar_dtype_u32:
                                        case k_convar_dtype_i32: 
                                                
-                                               data_int = atoi( temp ); 
+                                               data_int = atoi( args[1] ); 
                                                *((int *)cv->data) = cv->opt_i32.clamp? VG_MIN( VG_MAX(data_int, cv->opt_i32.min), cv->opt_i32.max ): data_int;
                                        
                                        break;
@@ -196,7 +340,19 @@ static void execute_console_input( const char *cmd )
                }
        }
        
-       vg_error( "No command/variable named '%s'\n", temp );
+       // Try commands
+       for( int i = 0; i < arrlen( vg_console.functions ); i ++ )
+       {
+               struct vg_cmd *cmd = &vg_console.functions[ i ];
+               if( !strcmp( cmd->name, args[0] ) )
+               {
+                       // Call function
+                       cmd->function( arg_count-1, args+1 );
+                       return;
+               }
+       }
+       
+       vg_error( "No command/variable named '%s'. Use 'list' to view all\n", args[0] );
 }
 
 // =============================================================================================================================