76d38007241ef256112d1440159b7dcccc38331c
1 // Copyright (C) 2021 Harry Godden (hgn) - All Rights Reserved
26 int persistent
; // Should be stored to cfg/auto.conf
32 int (*function
)( int argc
, char const *argv
[] );
41 int cursor_user
, cursor_pos
, string_length
;
43 char history
[32][512];
44 int history_last
, history_pos
, history_count
;
49 vg_console
= { .scale
= 2 };
55 static void vg_convar_push( struct vg_convar cv
);
56 static void vg_function_push( struct vg_cmd cmd
);
58 static void vg_console_draw( void );
59 void vg_console_println( const char *str
);
60 static int vg_console_list( int argc
, char const *argv
[] );
61 static void vg_console_init(void);
62 static void vg_console_write_persistent(void);
63 static void vg_console_free(void);
64 static void execute_console_input( const char *cmd
);
69 static void console_make_selection( int* start
, int* end
);
70 static void console_move_cursor( int* cursor0
, int* cursor1
, int dir
, int snap_together
);
71 static int console_makeroom( int datastart
, int length
);
72 static int console_delete_char( int direction
);
73 static void console_to_clipboard(void);
74 static void console_clipboard_paste(void);
75 static void console_put_char( char c
);
76 static void console_history_get( char* buf
, int entry_num
);
77 static void console_proc_key( GLFWwindow
* ptrW
, int key
, int scancode
, int action
, int mods
);
78 static void console_proc_wchar( GLFWwindow
* ptrW
, u32 uWchar
);
79 static int vg_console_enabled(void);
81 // =========================================================================================================
84 static int vg_console_enabled(void)
86 return vg_console
.enabled
;
89 static void vg_convar_push( struct vg_convar cv
)
91 arrpush( vg_console
.convars
, cv
);
94 static void vg_function_push( struct vg_cmd cmd
)
96 arrpush( vg_console
.functions
, cmd
);
99 static void vg_console_draw( void )
101 if( !vg_console
.enabled
)
104 int ptr
= vg_console
.current
-1;
106 ui_global_ctx
.cursor
[0] = 0;
107 ui_global_ctx
.cursor
[1] = 0;
108 ui_global_ctx
.cursor
[3] = vg_list_size( vg_console
.lines
)*8*vg_console
.scale
;
109 ui_fill_x( &ui_global_ctx
);
111 ui_new_node( &ui_global_ctx
);
113 ui_fill_rect( &ui_global_ctx
, ui_global_ctx
.cursor
, 0x77333333 );
115 ui_global_ctx
.cursor
[3] = 8*vg_console
.scale
;
116 ui_align_bottom( &ui_global_ctx
);
118 for( int i
= 0; i
< vg_console
.len
; i
++ )
121 ptr
= vg_list_size( vg_console
.lines
)-1;
123 ui_text( &ui_global_ctx
, vg_console
.lines
[ptr
], vg_console
.scale
);
124 ui_global_ctx
.cursor
[1] -= 8*vg_console
.scale
;
129 ui_end_down( &ui_global_ctx
);
131 ui_global_ctx
.cursor
[1] += 2;
132 ui_global_ctx
.cursor
[3] = 8*vg_console
.scale
;
134 ui_new_node( &ui_global_ctx
);
136 ui_fill_rect( &ui_global_ctx
, ui_global_ctx
.cursor
, 0x77333333 );
138 ui_text( &ui_global_ctx
, vg_console
.input
, vg_console
.scale
);
140 int start
= VG_MIN( vg_console
.cursor_pos
, vg_console
.cursor_user
),
141 end
= VG_MAX( vg_console
.cursor_pos
, vg_console
.cursor_user
);
143 ui_global_ctx
.cursor
[0] = (start
* ui_glyph_spacing_x
* vg_console
.scale
) + 2;
144 ui_global_ctx
.cursor
[2] = (start
== end
? 0.2f
: (float)(end
-start
)) * (float)ui_glyph_spacing_x
* (float)vg_console
.scale
* 0.5f
;
146 ui_fill_rect( &ui_global_ctx
, ui_global_ctx
.cursor
, 0x66ffffff );
148 ui_end_down( &ui_global_ctx
);
151 void vg_console_println( const char *str
)
153 if( vg_console
.len
< vg_list_size( vg_console
.lines
) )
156 strcpy( vg_console
.lines
[ vg_console
.current
++ ], str
);
158 if( vg_console
.current
>= vg_list_size( vg_console
.lines
) )
159 vg_console
.current
= 0;
162 static int vg_console_list( int argc
, char const *argv
[] )
164 for( int i
= 0; i
< arrlen( vg_console
.functions
); i
++ )
166 struct vg_cmd
*cmd
= &vg_console
.functions
[ i
];
167 vg_info( "* %s\n", cmd
->name
);
170 vg_info( "* snowsound\n" );
172 for( int i
= 0; i
< arrlen( vg_console
.convars
); i
++ )
174 struct vg_convar
*cv
= &vg_console
.convars
[ i
];
175 vg_info( "%s\n", cv
->name
);
181 static void vg_console_init(void)
183 vg_log_callback
= vg_console_println
;
185 vg_convar_push( (struct vg_convar
)
186 { .name
= "console_scale", .data
= &vg_console
.scale
, .data_type
= k_convar_dtype_i32
,
187 .opt_i32
= { .clamp
= 1, .min
= 1, .max
= 7 } } );
189 vg_function_push( (struct vg_cmd
){
191 .function
= vg_console_list
194 // Read and exec persistent commands
195 FILE *fp
= fopen( "cfg/auto.conf", "r" );
200 while( fgets( line
, sizeof( line
), fp
) )
202 line
[ strcspn( line
, "\r\n#" ) ] = 0x00;
204 if( line
[0] != 0x00 )
206 execute_console_input( line
);
214 static void vg_console_write_persistent(void)
216 FILE *fp
= fopen( "cfg/auto.conf", "w" );
218 for( int i
= 0; i
< arrlen( vg_console
.convars
); i
++ )
220 struct vg_convar
*cv
= &vg_console
.convars
[i
];
224 switch( cv
->data_type
)
226 case k_convar_dtype_i32
:
227 fprintf( fp
, "%s %d\n", cv
->name
, *(i32
*)(cv
->data
) );
229 case k_convar_dtype_u32
:
230 fprintf( fp
, "%s %u\n", cv
->name
, *(u32
*)(cv
->data
) );
232 case k_convar_dtype_f32
:
233 fprintf( fp
, "%s %.5f\n", cv
->name
, *(float *)(cv
->data
) );
242 static void vg_console_free(void)
244 vg_console_write_persistent();
246 arrfree( vg_console
.convars
);
247 arrfree( vg_console
.functions
);
250 static void execute_console_input( const char *cmd
)
258 // Split string into tokens
259 for( int i
= 0; i
< vg_list_size( temp
); i
++ )
263 if( isspace( cmd
[i
] ) )
268 if( arg_count
== vg_list_size( args
) )
277 args
[ arg_count
++ ] = temp
+ i
;
294 for( int i
= 0; i
< arrlen( vg_console
.convars
); i
++ )
296 struct vg_convar
*cv
= &vg_console
.convars
[ i
];
297 if( !strcmp( cv
->name
, args
[0] ) )
299 // Cvar Matched, try get value
302 switch( cv
->data_type
)
304 case k_convar_dtype_u32
:
305 case k_convar_dtype_i32
:
307 data_int
= atoi( args
[1] );
308 *((int *)cv
->data
) = cv
->opt_i32
.clamp
? VG_MIN( VG_MAX(data_int
, cv
->opt_i32
.min
), cv
->opt_i32
.max
): data_int
;
311 case k_convar_dtype_f32
: *((float *)cv
->data
) = atof( temp
); break;
316 switch( cv
->data_type
)
318 case k_convar_dtype_i32
: vg_info( "= %d\n", *((int *)cv
->data
) ); break;
319 case k_convar_dtype_u32
: vg_info( "= %u\n", *((u32
*)cv
->data
) ); break;
320 case k_convar_dtype_f32
: vg_info( "= %.4f\n", *((float *)cv
->data
) ); break;
329 for( int i
= 0; i
< arrlen( vg_console
.functions
); i
++ )
331 struct vg_cmd
*cmd
= &vg_console
.functions
[ i
];
332 if( !strcmp( cmd
->name
, args
[0] ) )
335 cmd
->function( arg_count
-1, args
+1 );
340 vg_error( "No command/variable named '%s'. Use 'list' to view all\n", args
[0] );
343 // =============================================================================================================================
346 static void console_make_selection( int* start
, int* end
)
348 *start
= VG_MIN( vg_console
.cursor_pos
, vg_console
.cursor_user
);
349 *end
= VG_MAX( vg_console
.cursor_pos
, vg_console
.cursor_user
);
352 static void console_move_cursor( int* cursor0
, int* cursor1
, int dir
, int snap_together
)
354 *cursor0
= VG_MAX( 0, vg_console
.cursor_user
+ dir
);
355 *cursor0
= VG_MIN( VG_MIN( vg_list_size( vg_console
.input
), strlen( vg_console
.input
)), *cursor0
);
360 static int console_makeroom( int datastart
, int length
)
362 int move_to
= VG_MIN( datastart
+length
, vg_list_size( vg_console
.input
) );
363 int move_amount
= strlen( vg_console
.input
)-datastart
;
364 int move_end
= VG_MIN( move_to
+move_amount
, vg_list_size( vg_console
.input
) );
365 move_amount
= move_end
-move_to
;
368 memmove( &vg_console
.input
[ move_to
], &vg_console
.input
[ datastart
], move_end
-move_to
);
370 vg_console
.input
[ move_end
] = '\0';
372 return VG_MIN( length
, vg_list_size( vg_console
.input
)-datastart
);
375 static int console_delete_char( int direction
)
378 console_make_selection( &start
, &end
);
380 // There is no selection
383 if( direction
== 1 ) end
= VG_MIN( end
+1, strlen( vg_console
.input
) );
384 else if( direction
== -1 ) start
= VG_MAX( start
-1, 0 );
387 // Still no selction, no need to do anything
391 // Copy the end->terminator to start
392 int remaining_length
= strlen( vg_console
.input
)+1-end
;
393 memmove( &vg_console
.input
[ start
], &vg_console
.input
[ end
], remaining_length
);
397 static void console_to_clipboard(void)
400 console_make_selection( &start
, &end
);
405 memcpy( buffer
, &vg_console
.input
[ start
], end
-start
);
406 buffer
[ end
-start
] = 0x00;
407 glfwSetClipboardString( NULL
, buffer
);
411 static void console_clipboard_paste(void)
413 int datastart
= console_delete_char(0);
414 const char* clipboard
= glfwGetClipboardString(NULL
);
415 int length
= strlen(clipboard
);
417 int cpylength
= console_makeroom(datastart
, length
);
419 memcpy( vg_console
.input
+ datastart
, clipboard
, cpylength
);
421 console_move_cursor( &vg_console
.cursor_user
, &vg_console
.cursor_pos
, cpylength
, 1 );
424 static void console_put_char( char c
)
426 if( !vg_console
.enabled
)
429 vg_console
.cursor_user
= console_delete_char(0);
431 if( console_makeroom( vg_console
.cursor_user
, 1 ) )
432 vg_console
.input
[ vg_console
.cursor_user
] = c
;
434 console_move_cursor( &vg_console
.cursor_user
, &vg_console
.cursor_pos
, 1, 1 );
437 static void console_history_get( char* buf
, int entry_num
)
439 if( !vg_console
.history_count
)
442 int pick
= (vg_console
.history_last
- VG_MIN( entry_num
, vg_console
.history_count
-1 )) % vg_list_size( vg_console
.history
);
443 strcpy( buf
, vg_console
.history
[ pick
] );
446 static void console_proc_key( GLFWwindow
* ptrW
, int key
, int scancode
, int action
, int mods
)
450 int cursor_diff
= vg_console
.cursor_pos
- vg_console
.cursor_user
? 0: 1;
452 if( key
== GLFW_KEY_GRAVE_ACCENT
)
454 vg_console
.enabled
= !vg_console
.enabled
;
458 if( !vg_console
.enabled
)
461 if( key
== GLFW_KEY_LEFT
)
463 if( mods
& GLFW_MOD_SHIFT
) // Receed secondary cursor
465 console_move_cursor( &vg_console
.cursor_user
, NULL
, -1, 0 );
467 else // Match and receed both cursors
469 console_move_cursor( &vg_console
.cursor_user
, &vg_console
.cursor_pos
, -cursor_diff
, 1 );
472 else if( key
== GLFW_KEY_RIGHT
) // Advance secondary cursor
474 if( mods
& GLFW_MOD_SHIFT
)
476 console_move_cursor( &vg_console
.cursor_user
, NULL
, 1, 0 );
478 else // Match and advance both cursors
480 console_move_cursor( &vg_console
.cursor_user
, &vg_console
.cursor_pos
, +cursor_diff
, 1 );
483 else if( key
== GLFW_KEY_DOWN
)
485 if( mods
& GLFW_MOD_SHIFT
){}
488 vg_console
.history_pos
= VG_MAX( 0, vg_console
.history_pos
-1 );
489 console_history_get( vg_console
.input
, vg_console
.history_pos
);
490 console_move_cursor( &vg_console
.cursor_user
, &vg_console
.cursor_pos
, vg_list_size( vg_console
.input
), 1 );
493 else if( key
== GLFW_KEY_UP
)
495 if( mods
& GLFW_MOD_SHIFT
){}
498 vg_console
.history_pos
= VG_MAX
503 vg_console
.history_pos
+1,
506 vg_list_size( vg_console
.history
),
507 vg_console
.history_count
- 1
512 console_history_get( vg_console
.input
, vg_console
.history_pos
);
513 console_move_cursor( &vg_console
.cursor_user
, &vg_console
.cursor_pos
, vg_list_size( vg_console
.input
), 1);
516 else if( key
== GLFW_KEY_BACKSPACE
) // Lookback delete
518 vg_console
.cursor_user
= console_delete_char( -1 );
519 vg_console
.cursor_pos
= vg_console
.cursor_user
;
521 else if( key
== GLFW_KEY_DELETE
) // Lookforward delete
523 vg_console
.cursor_user
= console_delete_char( 1 );
524 vg_console
.cursor_pos
= vg_console
.cursor_user
;
526 else if( key
== GLFW_KEY_HOME
) // Home key
528 if( mods
& GLFW_MOD_SHIFT
)
529 console_move_cursor( &vg_console
.cursor_user
, NULL
, -10000, 0 );
531 console_move_cursor( &vg_console
.cursor_user
, &vg_console
.cursor_pos
, -10000, 1 );
533 else if( key
== GLFW_KEY_END
) // End key
535 if( mods
& GLFW_MOD_SHIFT
)
536 console_move_cursor( &vg_console
.cursor_user
, NULL
, 10000, 0 );
538 console_move_cursor( &vg_console
.cursor_user
, &vg_console
.cursor_pos
, vg_list_size( vg_console
.input
), 1 );
540 else if( key
== GLFW_KEY_A
)
542 if( mods
& GLFW_MOD_CONTROL
) // Select all
544 console_move_cursor( &vg_console
.cursor_user
, NULL
, 10000, 0);
545 console_move_cursor( &vg_console
.cursor_pos
, NULL
, -10000, 0);
548 else if( key
== GLFW_KEY_C
) // Copy
550 if( mods
& GLFW_MOD_CONTROL
)
552 console_to_clipboard();
555 else if( key
== GLFW_KEY_X
) // Cut
557 if( mods
& GLFW_MOD_CONTROL
)
559 console_to_clipboard();
560 vg_console
.cursor_user
= console_delete_char(0);
561 vg_console
.cursor_pos
= vg_console
.cursor_user
;
564 else if( key
== GLFW_KEY_V
) // Paste
566 if( mods
& GLFW_MOD_CONTROL
)
568 console_clipboard_paste();
571 else if( key
== GLFW_KEY_ENTER
)
573 if( !strlen( vg_console
.input
) )
576 vg_info( "%s\n", vg_console
.input
);
578 if( strcmp( vg_console
.input
, vg_console
.history
[ vg_console
.history_last
]) )
580 vg_console
.history_last
= ( vg_console
.history_last
+ 1) % vg_list_size(vg_console
.history
);
581 vg_console
.history_count
= VG_MIN( vg_list_size( vg_console
.history
), vg_console
.history_count
+ 1 );
582 strcpy( vg_console
.history
[ vg_console
.history_last
], vg_console
.input
);
585 vg_console
.history_pos
= -1;
586 execute_console_input( vg_console
.input
);
587 console_move_cursor( &vg_console
.cursor_user
, &vg_console
.cursor_pos
, -10000, 1 );
588 vg_console
.input
[0] = '\0';
593 // Handle an OS based input of UTF32 character from the keyboard or such
594 static void console_proc_wchar( GLFWwindow
* ptrW
, u32 uWchar
)
596 //LOG_INFO("Recieved wchar: %u\n", uWchar);
597 if( uWchar
<= 0x7F && (char)uWchar
!= 0x60)
599 console_put_char((char)uWchar
);