mc 1.9
[vg.git] / src / vg / vg_console.h
index e8ca2f06f9dbd180865658d969512a6021e2c20b..304de0f3d3a9ebe754ca9d1030cf472e5b22b66b 100644 (file)
@@ -1,10 +1,11 @@
-// Copyright (C) 2021 Harry Godden (hgn) - All Rights Reserved
+/* Copyright (C) 2021-2022 Harry Godden (hgn) - All Rights Reserved */
 
 struct vg_console
 {
        struct vg_convar
        {
                void *data;
+      void (*update)(void);
                const char *name;
                
                enum vg_convar_dtype
@@ -20,10 +21,18 @@ struct vg_console
                        struct
                        {
                                int min, max, clamp;
-                       } opt_i32;
+                       } 
+         opt_i32;
+
+         struct
+         {
+            float min, max;
+            int clamp;
+         }
+         opt_f32;
                };
 
-               int persistent; // Should be stored to cfg/auto.conf
+               int persistent; /* Should this var be stored to cfg/auto.conf? */
        } 
        *convars;
        
@@ -48,10 +57,6 @@ struct vg_console
 }
 vg_console = { .scale = 1 };
 
-// Declerations
-// ------------
-
-// Registration
 static void vg_convar_push( struct vg_convar cv );
 static void vg_function_push( struct vg_cmd cmd );
 
@@ -63,24 +68,26 @@ static void vg_console_write_persistent(void);
 static void vg_console_free(void);
 static void execute_console_input( const char *cmd );
 
-// Console interface
-// -----------------
-
+/*
+ * 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 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_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
-
+/*
+ * Implementation
+ */
 static int vg_console_enabled(void) 
 { 
        return vg_console.enabled; 
@@ -107,7 +114,8 @@ static void vg_console_draw( void )
        
        ui_global_ctx.cursor[0] = 0;
        ui_global_ctx.cursor[1] = 0;
-       ui_global_ctx.cursor[3] = vg_list_size( vg_console.lines )*fh*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 );
@@ -122,7 +130,8 @@ static void vg_console_draw( void )
                        if( ptr < 0 )
                                ptr = vg_list_size( vg_console.lines )-1;
          
-                       ui_text( &ui_global_ctx, ui_global_ctx.cursor, vg_console.lines[ptr], vg_console.scale, 0 );
+                       ui_text( &ui_global_ctx, ui_global_ctx.cursor, 
+               vg_console.lines[ptr], vg_console.scale, 0 );
                        ui_global_ctx.cursor[1] -= fh*vg_console.scale;
                
                        ptr --;
@@ -137,13 +146,15 @@ static void vg_console_draw( void )
        {
                ui_fill_rect( &ui_global_ctx, ui_global_ctx.cursor, 0x77333333 );
                
-               ui_text( &ui_global_ctx, ui_global_ctx.cursor, vg_console.input, vg_console.scale, 0 );
+               ui_text( &ui_global_ctx, ui_global_ctx.cursor, 
+            vg_console.input, vg_console.scale, 0 );
                
                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 * 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_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 );
        }
@@ -193,8 +204,11 @@ static void vg_console_init(void)
        vg_log_callback = vg_console_println;
        
        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 } } );
+       { .name = "console_scale", .data = &vg_console.scale, 
+      .data_type = k_convar_dtype_i32, 
+               .opt_i32 = { .clamp = 1, .min = 1, .max = 7 },
+      .update = NULL
+   });
        
        vg_function_push( (struct vg_cmd){
                .name = "list",
@@ -206,7 +220,7 @@ static void vg_console_init(void)
       .function = vg_console_chartest
    });
 
-       // Read and exec persistent commands
+       /* Read and exec persistent commands */
        FILE *fp = fopen( "cfg/auto.conf", "r" );
        if( fp )
        {
@@ -270,7 +284,7 @@ static void execute_console_input( const char *cmd )
        
        int in_token = 0;
        
-       // Split string into tokens
+       /* Split string into tokens */
        for( int i = 0; i < vg_list_size( temp ); i ++ )
        {
                if( cmd[i] )
@@ -305,13 +319,14 @@ static void execute_console_input( const char *cmd )
                return;
        
        int data_int;
+   float data_float;
        
        for( int i = 0; i < arrlen( vg_console.convars ); i ++ )
        {
                struct vg_convar *cv = &vg_console.convars[ i ];
                if( !strcmp( cv->name, args[0] ) )
                {
-                       // Cvar Matched, try get value
+                       /* Cvar Matched, try get value */
                        if( arg_count >= 2 )
                        {
                                switch( cv->data_type )
@@ -320,19 +335,36 @@ static void execute_console_input( const char *cmd )
                                        case k_convar_dtype_i32: 
                                                
                                                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;
+                  
+                                               *((int *)cv->data) = cv->opt_i32.clamp? 
+                   VG_MIN( VG_MAX(data_int, cv->opt_i32.min), cv->opt_i32.max ): 
+                   data_int;
                                        
                                        break;
-                                       case k_convar_dtype_f32: *((float *)cv->data) = atof( temp ); break;
+                                       case k_convar_dtype_f32: 
+                  data_float = atof( args[1] );
+                  *((float *)cv->data) = cv->opt_f32.clamp?
+                     vg_minf( vg_maxf( data_float, cv->opt_f32.min), cv->opt_f32.max ):
+                     data_float;
+               break;
                                }
+
+            if( cv->update )
+               cv->update();
                        }
                        else
                        {
                                switch( cv->data_type )
                                {
-                                       case k_convar_dtype_i32: vg_info( "= %d\n", *((int *)cv->data) ); break;
-                                       case k_convar_dtype_u32: vg_info( "= %u\n", *((u32 *)cv->data) ); break;
-                                       case k_convar_dtype_f32: vg_info( "= %.4f\n", *((float *)cv->data) ); break;
+                                       case k_convar_dtype_i32: 
+                  vg_info( "= %d\n", *((int *)cv->data) ); 
+               break;
+                                       case k_convar_dtype_u32: 
+                  vg_info( "= %u\n", *((u32 *)cv->data) );
+               break;
+                                       case k_convar_dtype_f32: 
+                  vg_info( "= %.4f\n", *((float *)cv->data) );
+               break;
                                }
                        }
                
@@ -340,34 +372,40 @@ static void execute_console_input( const char *cmd )
                }
        }
        
-       // Try commands
+   /*
+    * Find and excecute command
+    */
        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] );
+       vg_error( "No command/var named '%s'. Use 'list' to view all\n", args[0] );
 }
 
-// =============================================================================================================================
-// Console interface
-
+/*
+ * Console Interface
+ */
 static void console_make_selection( int* start, int* end )
 {
        *start = VG_MIN( vg_console.cursor_pos, vg_console.cursor_user );
        *end   = VG_MAX( vg_console.cursor_pos, vg_console.cursor_user );
 }
 
-static void console_move_cursor( int* cursor0, int* cursor1, int dir, int snap_together )
+static void console_move_cursor( int* cursor0, int* cursor1, 
+      int dir, int snap_together )
 {
        *cursor0 = VG_MAX( 0, vg_console.cursor_user + dir );
-       *cursor0 = VG_MIN( VG_MIN( vg_list_size( vg_console.input ), strlen( vg_console.input )), *cursor0 );
+       *cursor0 = 
+      VG_MIN( 
+         VG_MIN( vg_list_size( vg_console.input ), strlen( vg_console.input )), 
+      *cursor0 );
+
        if( snap_together ) 
                *cursor1 = *cursor0;
 }
@@ -376,11 +414,14 @@ static int console_makeroom( int datastart, int length )
 {
        int move_to = VG_MIN( datastart+length, vg_list_size( vg_console.input ) );
        int move_amount = strlen( vg_console.input )-datastart;
-       int move_end = VG_MIN( move_to+move_amount, vg_list_size( vg_console.input ) );
+       int move_end = 
+      VG_MIN( move_to+move_amount, vg_list_size( vg_console.input ) );
        move_amount = move_end-move_to;
        
        if( move_amount )
-               memmove( &vg_console.input[ move_to ], &vg_console.input[ datastart ], move_end-move_to );
+               memmove( &vg_console.input[ move_to ], 
+               &vg_console.input[ datastart ], 
+               move_end-move_to );
        
        vg_console.input[ move_end ] = '\0';
        
@@ -392,20 +433,22 @@ static int console_delete_char( int direction )
        int start, end;
        console_make_selection( &start, &end );
        
-       // There is no selection
+       /* There is no selection */
        if( !(end-start) )
        {
                if( direction == 1 ) end = VG_MIN( end+1, strlen( vg_console.input ) );
                else if( direction == -1 ) start = VG_MAX( start-1, 0 );
        }
        
-       // Still no selction, no need to do anything
+       /* Still no selction, no need to do anything */
        if( !(end-start) ) 
                return start;
        
-       // Copy the end->terminator to start
+       /* Copy the end->terminator to start */
        int remaining_length = strlen( vg_console.input )+1-end;
-       memmove( &vg_console.input[ start ], &vg_console.input[ end ], remaining_length );
+       memmove( &vg_console.input[ start ], 
+            &vg_console.input[ end ], 
+            remaining_length );
        return start;
 }
 
@@ -432,8 +475,8 @@ static void console_clipboard_paste(void)
        int cpylength = console_makeroom(datastart, length);
 
        memcpy( vg_console.input + datastart, clipboard, cpylength);
-       
-       console_move_cursor( &vg_console.cursor_user, &vg_console.cursor_pos, cpylength, 1 );
+       console_move_cursor( &vg_console.cursor_user, 
+                        &vg_console.cursor_pos, cpylength, 1 );
 }
 
 static void console_put_char( char c )
@@ -454,11 +497,14 @@ static void console_history_get( char* buf, int entry_num )
        if( !vg_console.history_count )
                return;
        
-       int pick = (vg_console.history_last - VG_MIN( entry_num, vg_console.history_count -1 )) % vg_list_size( vg_console.history );
+   int offset = VG_MIN( entry_num, vg_console.history_count -1 ),
+       pick = (vg_console.history_last - offset) % 
+               vg_list_size( vg_console.history );
        strcpy( buf, vg_console.history[ pick ] );
 }
 
-static void console_proc_key( GLFWwindow* ptrW, int key, int scancode, int action, int mods )
+static void console_proc_key( GLFWwindow* ptrW, int key, int scancode, 
+      int action, int mods )
 {
        if( action )
        {
@@ -475,24 +521,26 @@ static void console_proc_key( GLFWwindow* ptrW, int key, int scancode, int actio
                
                if( key == GLFW_KEY_LEFT )
                {
-                       if( mods & GLFW_MOD_SHIFT ) // Receed secondary cursor
+                       if( mods & GLFW_MOD_SHIFT ) /* Receed secondary cursor */
                        { 
                                console_move_cursor( &vg_console.cursor_user, NULL, -1, 0 );
                        } 
-                       else // Match and receed both cursors
+                       else /* Match and receed both cursors */
                        {
-                               console_move_cursor( &vg_console.cursor_user, &vg_console.cursor_pos, -cursor_diff, 1 );
+                               console_move_cursor( &vg_console.cursor_user, 
+                  &vg_console.cursor_pos, -cursor_diff, 1 );
                        }
                }
-               else if( key == GLFW_KEY_RIGHT ) // Advance secondary cursor
+               else if( key == GLFW_KEY_RIGHT ) /* Advance secondary cursor */
                {
                        if( mods & GLFW_MOD_SHIFT )
                        {
                                console_move_cursor( &vg_console.cursor_user, NULL, 1, 0 );
                        } 
-                       else // Match and advance both cursors 
+                       else /* Match and advance both cursors */
                        {
-                               console_move_cursor( &vg_console.cursor_user, &vg_console.cursor_pos, +cursor_diff, 1 );
+                               console_move_cursor( &vg_console.cursor_user, 
+                  &vg_console.cursor_pos, +cursor_diff, 1 );
                        }
                } 
                else if( key == GLFW_KEY_DOWN )
@@ -502,7 +550,8 @@ static void console_proc_key( GLFWwindow* ptrW, int key, int scancode, int actio
                        {
                                vg_console.history_pos = VG_MAX( 0, vg_console.history_pos-1 );
                                console_history_get( vg_console.input, vg_console.history_pos );
-                               console_move_cursor( &vg_console.cursor_user, &vg_console.cursor_pos, vg_list_size( vg_console.input ), 1 );
+                               console_move_cursor( &vg_console.cursor_user, 
+                  &vg_console.cursor_pos, vg_list_size( vg_console.input ), 1 );
                        }
                } 
                else if( key == GLFW_KEY_UP )
@@ -525,49 +574,54 @@ static void console_proc_key( GLFWwindow* ptrW, int key, int scancode, int actio
                                );
                                
                                console_history_get( vg_console.input, vg_console.history_pos );
-                               console_move_cursor( &vg_console.cursor_user, &vg_console.cursor_pos, vg_list_size( vg_console.input ), 1);
+                               console_move_cursor( &vg_console.cursor_user, 
+                                 &vg_console.cursor_pos, 
+                                 vg_list_size( vg_console.input ), 1);
                        }
                } 
-               else if( key == GLFW_KEY_BACKSPACE ) // Lookback delete
+               else if( key == GLFW_KEY_BACKSPACE ) /* Lookback delete */
                {
                        vg_console.cursor_user = console_delete_char( -1 );
                        vg_console.cursor_pos = vg_console.cursor_user;
                } 
-               else if( key == GLFW_KEY_DELETE ) // Lookforward delete
+               else if( key == GLFW_KEY_DELETE ) /* Lookforward delete */
                {
                        vg_console.cursor_user = console_delete_char( 1 );
                        vg_console.cursor_pos = vg_console.cursor_user;
                } 
-               else if( key == GLFW_KEY_HOME ) // Home key
+               else if( key == GLFW_KEY_HOME ) /* Home key */
                {
                        if( mods & GLFW_MOD_SHIFT ) 
                                console_move_cursor( &vg_console.cursor_user, NULL, -10000, 0 );
                        else 
-                               console_move_cursor( &vg_console.cursor_user, &vg_console.cursor_pos, -10000, 1 );
+                               console_move_cursor( &vg_console.cursor_user, 
+                                 &vg_console.cursor_pos, -10000, 1 );
                } 
-               else if( key == GLFW_KEY_END ) // End key
+               else if( key == GLFW_KEY_END ) /* End key */
                {
                        if( mods & GLFW_MOD_SHIFT ) 
                                console_move_cursor( &vg_console.cursor_user, NULL, 10000, 0 );
                        else 
-                               console_move_cursor( &vg_console.cursor_user, &vg_console.cursor_pos, vg_list_size( vg_console.input ), 1 );
+                               console_move_cursor( &vg_console.cursor_user, 
+                                 &vg_console.cursor_pos, 
+                                 vg_list_size( vg_console.input ), 1 );
                }
                else if( key == GLFW_KEY_A )
                {
-                       if( mods & GLFW_MOD_CONTROL ) // Select all
+                       if( mods & GLFW_MOD_CONTROL ) /* Select all */
                        {
                                console_move_cursor( &vg_console.cursor_user, NULL, 10000, 0);
                                console_move_cursor( &vg_console.cursor_pos, NULL, -10000, 0);
                        }
                } 
-               else if( key == GLFW_KEY_C ) // Copy
+               else if( key == GLFW_KEY_C ) /* Copy */
                {
                        if( mods & GLFW_MOD_CONTROL )
                        {
                                console_to_clipboard();
                        }
                } 
-               else if( key == GLFW_KEY_X ) // Cut
+               else if( key == GLFW_KEY_X ) /* Cut */
                {
                        if( mods & GLFW_MOD_CONTROL )
                        {
@@ -576,7 +630,7 @@ static void console_proc_key( GLFWwindow* ptrW, int key, int scancode, int actio
                                vg_console.cursor_pos = vg_console.cursor_user;
                        }
                } 
-               else if( key == GLFW_KEY_V ) // Paste
+               else if( key == GLFW_KEY_V ) /* Paste */
                {
                        if( mods & GLFW_MOD_CONTROL )
                        {
@@ -590,25 +644,30 @@ static void console_proc_key( GLFWwindow* ptrW, int key, int scancode, int actio
                        
                        vg_info( "%s\n", vg_console.input );
                        
-                       if( strcmp( vg_console.input, vg_console.history[ vg_console.history_last ]) )
+                       if( strcmp( vg_console.input, 
+                  vg_console.history[ vg_console.history_last ]) )
                        {
-                               vg_console.history_last = ( vg_console.history_last + 1) % vg_list_size(vg_console.history );
-                               vg_console.history_count = VG_MIN( vg_list_size( vg_console.history ), vg_console.history_count + 1 );
-                               strcpy( vg_console.history[ vg_console.history_last ], vg_console.input );
+                               vg_console.history_last = ( vg_console.history_last + 1) % 
+                                       vg_list_size(vg_console.history );
+                               vg_console.history_count = 
+               VG_MIN( vg_list_size( vg_console.history ), 
+                     vg_console.history_count + 1 );
+                               strcpy( vg_console.history[ vg_console.history_last ], 
+                    vg_console.input );
                        }
                        
                        vg_console.history_pos = -1;
                        execute_console_input( vg_console.input );
-                       console_move_cursor( &vg_console.cursor_user, &vg_console.cursor_pos, -10000, 1 );
+                       console_move_cursor( &vg_console.cursor_user, 
+                              &vg_console.cursor_pos, -10000, 1 );
                        vg_console.input[0] = '\0';
                }
        }
 }
 
-// Handle an OS based input of UTF32 character from the keyboard or such
+/* Handle an OS based input of UTF32 character from the keyboard or such */
 static void console_proc_wchar( GLFWwindow* ptrW, u32 uWchar )
 {
-       //LOG_INFO("Recieved wchar: %u\n", uWchar);
        if( uWchar <= 0x7F && (char)uWchar != 0x60)
        {
                console_put_char((char)uWchar);