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
- vg_console
.len
;
106 ptr
+= vg_list_size( vg_console
.lines
);
109 ui_global_ctx
.cursor
[0] = 0;
110 ui_global_ctx
.cursor
[1] = 0;
111 ui_global_ctx
.cursor
[3] = vg_console
.len
*8*vg_console
.scale
;
112 ui_fill_x( &ui_global_ctx
);
114 ui_new_node( &ui_global_ctx
);
116 ui_fill_rect( &ui_global_ctx
, ui_global_ctx
.cursor
, 0x77333333 );
118 ui_global_ctx
.cursor
[3] = 8*vg_console
.scale
;
119 ui_align_bottom( &ui_global_ctx
);
121 for( int i
= 0; i
< vg_console
.len
; i
++ )
123 ui_text( &ui_global_ctx
, vg_console
.lines
[ptr
], vg_console
.scale
);
124 ui_global_ctx
.cursor
[1] -= 8*vg_console
.scale
;
128 ptr
= vg_list_size( vg_console
.lines
)-1;
131 ui_end_down( &ui_global_ctx
);
133 ui_global_ctx
.cursor
[1] += 2;
134 ui_global_ctx
.cursor
[3] = 8*vg_console
.scale
;
136 ui_new_node( &ui_global_ctx
);
138 ui_fill_rect( &ui_global_ctx
, ui_global_ctx
.cursor
, 0x77333333 );
140 ui_text( &ui_global_ctx
, vg_console
.input
, vg_console
.scale
);
142 int start
= VG_MIN( vg_console
.cursor_pos
, vg_console
.cursor_user
),
143 end
= VG_MAX( vg_console
.cursor_pos
, vg_console
.cursor_user
);
145 ui_global_ctx
.cursor
[0] = (start
* ui_glyph_spacing_x
* vg_console
.scale
) + 2;
146 ui_global_ctx
.cursor
[2] = (start
== end
? 0.2f
: (float)(end
-start
)) * (float)ui_glyph_spacing_x
* (float)vg_console
.scale
* 0.5f
;
148 ui_fill_rect( &ui_global_ctx
, ui_global_ctx
.cursor
, 0x66ffffff );
150 ui_end_down( &ui_global_ctx
);
153 void vg_console_println( const char *str
)
155 if( vg_console
.len
< vg_list_size( vg_console
.lines
) )
158 strcpy( vg_console
.lines
[ vg_console
.current
++ ], str
);
160 if( vg_console
.current
>= vg_list_size( vg_console
.lines
) )
161 vg_console
.current
= 0;
164 static int vg_console_list( int argc
, char const *argv
[] )
166 for( int i
= 0; i
< arrlen( vg_console
.functions
); i
++ )
168 struct vg_cmd
*cmd
= &vg_console
.functions
[ i
];
169 vg_info( "* %s\n", cmd
->name
);
172 vg_info( "* snowsound\n" );
174 for( int i
= 0; i
< arrlen( vg_console
.convars
); i
++ )
176 struct vg_convar
*cv
= &vg_console
.convars
[ i
];
177 vg_info( "%s\n", cv
->name
);
183 static void vg_console_init(void)
185 vg_log_callback
= vg_console_println
;
187 vg_convar_push( (struct vg_convar
)
188 { .name
= "console_scale", .data
= &vg_console
.scale
, .data_type
= k_convar_dtype_i32
,
189 .opt_i32
= { .clamp
= 1, .min
= 1, .max
= 7 } } );
191 vg_function_push( (struct vg_cmd
){
193 .function
= vg_console_list
196 // Read and exec persistent commands
197 FILE *fp
= fopen( "cfg/auto.conf", "r" );
202 while( fgets( line
, sizeof( line
), fp
) )
204 line
[ strcspn( line
, "\r\n#" ) ] = 0x00;
206 if( line
[0] != 0x00 )
208 execute_console_input( line
);
216 static void vg_console_write_persistent(void)
218 FILE *fp
= fopen( "cfg/auto.conf", "w" );
220 for( int i
= 0; i
< arrlen( vg_console
.convars
); i
++ )
222 struct vg_convar
*cv
= &vg_console
.convars
[i
];
226 switch( cv
->data_type
)
228 case k_convar_dtype_i32
:
229 fprintf( fp
, "%s %d\n", cv
->name
, *(i32
*)(cv
->data
) );
231 case k_convar_dtype_u32
:
232 fprintf( fp
, "%s %u\n", cv
->name
, *(u32
*)(cv
->data
) );
234 case k_convar_dtype_f32
:
235 fprintf( fp
, "%s %.5f\n", cv
->name
, *(float *)(cv
->data
) );
244 static void vg_console_free(void)
246 vg_console_write_persistent();
248 arrfree( vg_console
.convars
);
249 arrfree( vg_console
.functions
);
252 static void execute_console_input( const char *cmd
)
260 // Split string into tokens
261 for( int i
= 0; i
< vg_list_size( temp
); i
++ )
265 if( isspace( cmd
[i
] ) )
270 if( arg_count
== vg_list_size( args
) )
279 args
[ arg_count
++ ] = temp
+ i
;
296 for( int i
= 0; i
< arrlen( vg_console
.convars
); i
++ )
298 struct vg_convar
*cv
= &vg_console
.convars
[ i
];
299 if( !strcmp( cv
->name
, args
[0] ) )
301 // Cvar Matched, try get value
304 switch( cv
->data_type
)
306 case k_convar_dtype_u32
:
307 case k_convar_dtype_i32
:
309 data_int
= atoi( args
[1] );
310 *((int *)cv
->data
) = cv
->opt_i32
.clamp
? VG_MIN( VG_MAX(data_int
, cv
->opt_i32
.min
), cv
->opt_i32
.max
): data_int
;
313 case k_convar_dtype_f32
: *((float *)cv
->data
) = atof( temp
); break;
318 switch( cv
->data_type
)
320 case k_convar_dtype_i32
: vg_info( "= %d\n", *((int *)cv
->data
) ); break;
321 case k_convar_dtype_u32
: vg_info( "= %u\n", *((u32
*)cv
->data
) ); break;
322 case k_convar_dtype_f32
: vg_info( "= %.4f\n", *((float *)cv
->data
) ); break;
331 for( int i
= 0; i
< arrlen( vg_console
.functions
); i
++ )
333 struct vg_cmd
*cmd
= &vg_console
.functions
[ i
];
334 if( !strcmp( cmd
->name
, args
[0] ) )
337 cmd
->function( arg_count
-1, args
+1 );
342 vg_error( "No command/variable named '%s'. Use 'list' to view all\n", args
[0] );
345 // =============================================================================================================================
348 static void console_make_selection( int* start
, int* end
)
350 *start
= VG_MIN( vg_console
.cursor_pos
, vg_console
.cursor_user
);
351 *end
= VG_MAX( vg_console
.cursor_pos
, vg_console
.cursor_user
);
354 static void console_move_cursor( int* cursor0
, int* cursor1
, int dir
, int snap_together
)
356 *cursor0
= VG_MAX( 0, vg_console
.cursor_user
+ dir
);
357 *cursor0
= VG_MIN( VG_MIN( vg_list_size( vg_console
.input
), strlen( vg_console
.input
)), *cursor0
);
362 static int console_makeroom( int datastart
, int length
)
364 int move_to
= VG_MIN( datastart
+length
, vg_list_size( vg_console
.input
) );
365 int move_amount
= strlen( vg_console
.input
)-datastart
;
366 int move_end
= VG_MIN( move_to
+move_amount
, vg_list_size( vg_console
.input
) );
367 move_amount
= move_end
-move_to
;
370 memmove( &vg_console
.input
[ move_to
], &vg_console
.input
[ datastart
], move_end
-move_to
);
372 vg_console
.input
[ move_end
] = '\0';
374 return VG_MIN( length
, vg_list_size( vg_console
.input
)-datastart
);
377 static int console_delete_char( int direction
)
380 console_make_selection( &start
, &end
);
382 // There is no selection
385 if( direction
== 1 ) end
= VG_MIN( end
+1, strlen( vg_console
.input
) );
386 else if( direction
== -1 ) start
= VG_MAX( start
-1, 0 );
389 // Still no selction, no need to do anything
393 // Copy the end->terminator to start
394 int remaining_length
= strlen( vg_console
.input
)+1-end
;
395 memmove( &vg_console
.input
[ start
], &vg_console
.input
[ end
], remaining_length
);
399 static void console_to_clipboard(void)
402 console_make_selection( &start
, &end
);
407 memcpy( buffer
, &vg_console
.input
[ start
], end
-start
);
408 buffer
[ end
-start
] = 0x00;
409 glfwSetClipboardString( NULL
, buffer
);
413 static void console_clipboard_paste(void)
415 int datastart
= console_delete_char(0);
416 const char* clipboard
= glfwGetClipboardString(NULL
);
417 int length
= strlen(clipboard
);
419 int cpylength
= console_makeroom(datastart
, length
);
421 memcpy( vg_console
.input
+ datastart
, clipboard
, cpylength
);
423 console_move_cursor( &vg_console
.cursor_user
, &vg_console
.cursor_pos
, cpylength
, 1 );
426 static void console_put_char( char c
)
428 if( !vg_console
.enabled
)
431 vg_console
.cursor_user
= console_delete_char(0);
433 if( console_makeroom( vg_console
.cursor_user
, 1 ) )
434 vg_console
.input
[ vg_console
.cursor_user
] = c
;
436 console_move_cursor( &vg_console
.cursor_user
, &vg_console
.cursor_pos
, 1, 1 );
439 static void console_history_get( char* buf
, int entry_num
)
441 if( !vg_console
.history_count
)
444 int pick
= (vg_console
.history_last
- VG_MIN( entry_num
, vg_console
.history_count
-1 )) % vg_list_size( vg_console
.history
);
445 strcpy( buf
, vg_console
.history
[ pick
] );
448 static void console_proc_key( GLFWwindow
* ptrW
, int key
, int scancode
, int action
, int mods
)
452 int cursor_diff
= vg_console
.cursor_pos
- vg_console
.cursor_user
? 0: 1;
454 if( key
== GLFW_KEY_GRAVE_ACCENT
)
456 vg_console
.enabled
= !vg_console
.enabled
;
460 if( !vg_console
.enabled
)
463 if( key
== GLFW_KEY_LEFT
)
465 if( mods
& GLFW_MOD_SHIFT
) // Receed secondary cursor
467 console_move_cursor( &vg_console
.cursor_user
, NULL
, -1, 0 );
469 else // Match and receed both cursors
471 console_move_cursor( &vg_console
.cursor_user
, &vg_console
.cursor_pos
, -cursor_diff
, 1 );
474 else if( key
== GLFW_KEY_RIGHT
) // Advance secondary cursor
476 if( mods
& GLFW_MOD_SHIFT
)
478 console_move_cursor( &vg_console
.cursor_user
, NULL
, 1, 0 );
480 else // Match and advance both cursors
482 console_move_cursor( &vg_console
.cursor_user
, &vg_console
.cursor_pos
, +cursor_diff
, 1 );
485 else if( key
== GLFW_KEY_DOWN
)
487 if( mods
& GLFW_MOD_SHIFT
){}
490 vg_console
.history_pos
= VG_MAX( 0, vg_console
.history_pos
-1 );
491 console_history_get( vg_console
.input
, vg_console
.history_pos
);
492 console_move_cursor( &vg_console
.cursor_user
, &vg_console
.cursor_pos
, vg_list_size( vg_console
.input
), 1 );
495 else if( key
== GLFW_KEY_UP
)
497 if( mods
& GLFW_MOD_SHIFT
){}
500 vg_console
.history_pos
= VG_MAX
505 vg_console
.history_pos
+1,
508 vg_list_size( vg_console
.history
),
509 vg_console
.history_count
- 1
514 console_history_get( vg_console
.input
, vg_console
.history_pos
);
515 console_move_cursor( &vg_console
.cursor_user
, &vg_console
.cursor_pos
, vg_list_size( vg_console
.input
), 1);
518 else if( key
== GLFW_KEY_BACKSPACE
) // Lookback delete
520 vg_console
.cursor_user
= console_delete_char( -1 );
521 vg_console
.cursor_pos
= vg_console
.cursor_user
;
523 else if( key
== GLFW_KEY_DELETE
) // Lookforward delete
525 vg_console
.cursor_user
= console_delete_char( 1 );
526 vg_console
.cursor_pos
= vg_console
.cursor_user
;
528 else if( key
== GLFW_KEY_HOME
) // Home key
530 if( mods
& GLFW_MOD_SHIFT
)
531 console_move_cursor( &vg_console
.cursor_user
, NULL
, -10000, 0 );
533 console_move_cursor( &vg_console
.cursor_user
, &vg_console
.cursor_pos
, -10000, 1 );
535 else if( key
== GLFW_KEY_END
) // End key
537 if( mods
& GLFW_MOD_SHIFT
)
538 console_move_cursor( &vg_console
.cursor_user
, NULL
, 10000, 0 );
540 console_move_cursor( &vg_console
.cursor_user
, &vg_console
.cursor_pos
, vg_list_size( vg_console
.input
), 1 );
542 else if( key
== GLFW_KEY_A
)
544 if( mods
& GLFW_MOD_CONTROL
) // Select all
546 console_move_cursor( &vg_console
.cursor_user
, NULL
, 10000, 0);
547 console_move_cursor( &vg_console
.cursor_pos
, NULL
, -10000, 0);
550 else if( key
== GLFW_KEY_C
) // Copy
552 if( mods
& GLFW_MOD_CONTROL
)
554 console_to_clipboard();
557 else if( key
== GLFW_KEY_X
) // Cut
559 if( mods
& GLFW_MOD_CONTROL
)
561 console_to_clipboard();
562 vg_console
.cursor_user
= console_delete_char(0);
563 vg_console
.cursor_pos
= vg_console
.cursor_user
;
566 else if( key
== GLFW_KEY_V
) // Paste
568 if( mods
& GLFW_MOD_CONTROL
)
570 console_clipboard_paste();
573 else if( key
== GLFW_KEY_ENTER
)
575 if( !strlen( vg_console
.input
) )
578 vg_info( "%s\n", vg_console
.input
);
580 if( strcmp( vg_console
.input
, vg_console
.history
[ vg_console
.history_last
]) )
582 vg_console
.history_last
= ( vg_console
.history_last
+ 1) % vg_list_size(vg_console
.history
);
583 vg_console
.history_count
= VG_MIN( vg_list_size( vg_console
.history
), vg_console
.history_count
+ 1 );
584 strcpy( vg_console
.history
[ vg_console
.history_last
], vg_console
.input
);
587 vg_console
.history_pos
= -1;
588 execute_console_input( vg_console
.input
);
589 console_move_cursor( &vg_console
.cursor_user
, &vg_console
.cursor_pos
, -10000, 1 );
590 vg_console
.input
[0] = '\0';
595 // Handle an OS based input of UTF32 character from the keyboard or such
596 static void console_proc_wchar( GLFWwindow
* ptrW
, u32 uWchar
)
598 //LOG_INFO("Recieved wchar: %u\n", uWchar);
599 if( uWchar
<= 0x7F && (char)uWchar
!= 0x60)
601 console_put_char((char)uWchar
);