48dee1a4a0fbd36d4f625fdab7c0f25dfc9da7f4
28 int (*function
)( int argc
, char const *argv
[] );
37 int cursor_user
, cursor_pos
, string_length
;
39 char history
[32][512];
40 int history_last
, history_pos
, history_count
;
45 vg_console
= { .scale
= 2 };
47 static int vg_console_enabled(void) { return vg_console
.enabled
; }
49 static void vg_convar_push( struct vg_convar cv
)
51 arrpush( vg_console
.convars
, cv
);
54 static void vg_function_push( struct vg_cmd cmd
)
56 arrpush( vg_console
.functions
, cmd
);
59 static void vg_console_draw( void )
61 if( !vg_console
.enabled
)
64 int ptr
= vg_console
.current
- vg_console
.len
;
66 ptr
+= vg_list_size( vg_console
.lines
);
69 ui_global_ctx
.cursor
[0] = 0;
70 ui_global_ctx
.cursor
[1] = 0;
71 ui_global_ctx
.cursor
[3] = vg_console
.len
*8*vg_console
.scale
;
72 ui_fill_x( &ui_global_ctx
);
74 ui_new_node( &ui_global_ctx
);
76 ui_fill_rect( &ui_global_ctx
, ui_global_ctx
.cursor
, 0x77333333 );
78 ui_global_ctx
.cursor
[3] = 8*vg_console
.scale
;
79 ui_align_bottom( &ui_global_ctx
);
81 for( int i
= 0; i
< vg_console
.len
; i
++ )
83 ui_text( &ui_global_ctx
, vg_console
.lines
[ptr
], vg_console
.scale
, 0 );
84 ui_global_ctx
.cursor
[1] -= 8*vg_console
.scale
;
88 ptr
= vg_list_size( vg_console
.lines
)-1;
91 ui_end_down( &ui_global_ctx
);
93 ui_global_ctx
.cursor
[1] += 2;
94 ui_global_ctx
.cursor
[3] = 8*vg_console
.scale
;
96 ui_new_node( &ui_global_ctx
);
98 ui_fill_rect( &ui_global_ctx
, ui_global_ctx
.cursor
, 0x77333333 );
100 ui_text( &ui_global_ctx
, vg_console
.input
, vg_console
.scale
, 0 );
102 int start
= VG_MIN( vg_console
.cursor_pos
, vg_console
.cursor_user
),
103 end
= VG_MAX( vg_console
.cursor_pos
, vg_console
.cursor_user
);
105 ui_global_ctx
.cursor
[0] = start
* ui_glyph_spacing_x
* vg_console
.scale
+ 2;
106 ui_global_ctx
.cursor
[2] = (start
== end
? 0.2f
: (float)(end
-start
)) * (float)ui_glyph_spacing_x
* (float)vg_console
.scale
;
108 ui_fill_rect( &ui_global_ctx
, ui_global_ctx
.cursor
, 0x66ffffff );
110 ui_end_down( &ui_global_ctx
);
113 void vg_console_println( const char *str
)
115 if( vg_console
.len
< vg_list_size( vg_console
.lines
) )
118 strcpy( vg_console
.lines
[ vg_console
.current
++ ], str
);
120 if( vg_console
.current
>= vg_list_size( vg_console
.lines
) )
121 vg_console
.current
= 0;
124 static int vg_console_list( int argc
, char const *argv
[] )
126 for( int i
= 0; i
< arrlen( vg_console
.functions
); i
++ )
128 struct vg_cmd
*cmd
= &vg_console
.functions
[ i
];
129 vg_info( "* %s\n", cmd
->name
);
132 vg_info( "* snowsound\n" );
134 for( int i
= 0; i
< arrlen( vg_console
.convars
); i
++ )
136 struct vg_convar
*cv
= &vg_console
.convars
[ i
];
137 vg_info( "%s\n", cv
->name
);
143 static void vg_console_init(void)
145 vg_log_callback
= vg_console_println
;
147 vg_convar_push( (struct vg_convar
)
148 { .name
= "console_scale", .data
= &vg_console
.scale
, .data_type
= k_convar_dtype_i32
,
149 .opt_i32
= { .clamp
= 1, .min
= 1, .max
= 7 } } );
151 vg_function_push( (struct vg_cmd
){
153 .function
= vg_console_list
157 static void vg_console_free(void)
159 arrfree( vg_console
.convars
);
160 arrfree( vg_console
.functions
);
163 static void execute_console_input( const char *cmd
)
171 // Split string into tokens
172 for( int i
= 0; i
< vg_list_size( temp
); i
++ )
176 if( isspace( cmd
[i
] ) )
187 args
[ arg_count
++ ] = temp
+ i
;
204 for( int i
= 0; i
< arrlen( vg_console
.convars
); i
++ )
206 struct vg_convar
*cv
= &vg_console
.convars
[ i
];
207 if( !strcmp( cv
->name
, args
[0] ) )
209 // Cvar Matched, try get value
212 switch( cv
->data_type
)
214 case k_convar_dtype_u32
:
215 case k_convar_dtype_i32
:
217 data_int
= atoi( args
[1] );
218 *((int *)cv
->data
) = cv
->opt_i32
.clamp
? VG_MIN( VG_MAX(data_int
, cv
->opt_i32
.min
), cv
->opt_i32
.max
): data_int
;
221 case k_convar_dtype_f32
: *((float *)cv
->data
) = atof( temp
); break;
226 switch( cv
->data_type
)
228 case k_convar_dtype_i32
: vg_info( "= %d\n", *((int *)cv
->data
) ); break;
229 case k_convar_dtype_u32
: vg_info( "= %u\n", *((u32
*)cv
->data
) ); break;
230 case k_convar_dtype_f32
: vg_info( "= %.4f\n", *((float *)cv
->data
) ); break;
239 for( int i
= 0; i
< arrlen( vg_console
.functions
); i
++ )
241 struct vg_cmd
*cmd
= &vg_console
.functions
[ i
];
242 if( !strcmp( cmd
->name
, args
[0] ) )
245 cmd
->function( arg_count
-1, args
+1 );
250 vg_error( "No command/variable named '%s'. Use 'list' to view all\n", args
[0] );
253 // =============================================================================================================================
256 static void console_make_selection( int* start
, int* end
)
258 *start
= VG_MIN( vg_console
.cursor_pos
, vg_console
.cursor_user
);
259 *end
= VG_MAX( vg_console
.cursor_pos
, vg_console
.cursor_user
);
262 static void console_move_cursor( int* cursor0
, int* cursor1
, int dir
, int snap_together
)
264 *cursor0
= VG_MAX( 0, vg_console
.cursor_user
+ dir
);
265 *cursor0
= VG_MIN( VG_MIN( vg_list_size( vg_console
.input
), strlen( vg_console
.input
)), *cursor0
);
270 static int console_makeroom( int datastart
, int length
)
272 int move_to
= VG_MIN( datastart
+length
, vg_list_size( vg_console
.input
) );
273 int move_amount
= strlen( vg_console
.input
)-datastart
;
274 int move_end
= VG_MIN( move_to
+move_amount
, vg_list_size( vg_console
.input
) );
275 move_amount
= move_end
-move_to
;
278 memmove( &vg_console
.input
[ move_to
], &vg_console
.input
[ datastart
], move_end
-move_to
);
280 vg_console
.input
[ move_end
] = '\0';
282 return VG_MIN( length
, vg_list_size( vg_console
.input
)-datastart
);
285 static int console_delete_char( int direction
)
288 console_make_selection( &start
, &end
);
290 // There is no selection
293 if( direction
== 1 ) end
= VG_MIN( end
+1, strlen( vg_console
.input
) );
294 else if( direction
== -1 ) start
= VG_MAX( start
-1, 0 );
297 // Still no selction, no need to do anything
301 // Copy the end->terminator to start
302 int remaining_length
= strlen( vg_console
.input
)+1-end
;
303 memmove( &vg_console
.input
[ start
], &vg_console
.input
[ end
], remaining_length
);
307 static void console_to_clipboard(void)
310 console_make_selection( &start
, &end
);
315 memcpy( buffer
, &vg_console
.input
[ start
], end
-start
);
316 buffer
[ end
-start
] = 0x00;
317 glfwSetClipboardString( NULL
, buffer
);
321 static void console_clipboard_paste(void)
323 int datastart
= console_delete_char(0);
324 const char* clipboard
= glfwGetClipboardString(NULL
);
325 int length
= strlen(clipboard
);
327 int cpylength
= console_makeroom(datastart
, length
);
329 memcpy( vg_console
.input
+ datastart
, clipboard
, cpylength
);
331 console_move_cursor( &vg_console
.cursor_user
, &vg_console
.cursor_pos
, cpylength
, 1 );
334 static void console_put_char( char c
)
336 if( !vg_console
.enabled
)
339 vg_console
.cursor_user
= console_delete_char(0);
341 if( console_makeroom( vg_console
.cursor_user
, 1 ) )
342 vg_console
.input
[ vg_console
.cursor_user
] = c
;
344 console_move_cursor( &vg_console
.cursor_user
, &vg_console
.cursor_pos
, 1, 1 );
347 static void console_history_get( char* buf
, int entry_num
)
349 if( !vg_console
.history_count
)
352 int pick
= (vg_console
.history_last
- VG_MIN( entry_num
, vg_console
.history_count
-1 )) % vg_list_size( vg_console
.history
);
353 strcpy( buf
, vg_console
.history
[ pick
] );
356 static void console_proc_key( GLFWwindow
* ptrW
, int key
, int scancode
, int action
, int mods
)
360 int cursor_diff
= vg_console
.cursor_pos
- vg_console
.cursor_user
? 0: 1;
362 if( key
== GLFW_KEY_GRAVE_ACCENT
)
364 vg_console
.enabled
= !vg_console
.enabled
;
368 if( !vg_console
.enabled
)
371 if( key
== GLFW_KEY_LEFT
)
373 if( mods
& GLFW_MOD_SHIFT
) // Receed secondary cursor
375 console_move_cursor( &vg_console
.cursor_user
, NULL
, -1, 0 );
377 else // Match and receed both cursors
379 console_move_cursor( &vg_console
.cursor_user
, &vg_console
.cursor_pos
, -cursor_diff
, 1 );
382 else if( key
== GLFW_KEY_RIGHT
) // Advance secondary cursor
384 if( mods
& GLFW_MOD_SHIFT
)
386 console_move_cursor( &vg_console
.cursor_user
, NULL
, 1, 0 );
388 else // Match and advance both cursors
390 console_move_cursor( &vg_console
.cursor_user
, &vg_console
.cursor_pos
, +cursor_diff
, 1 );
393 else if( key
== GLFW_KEY_DOWN
)
395 if( mods
& GLFW_MOD_SHIFT
){}
398 vg_console
.history_pos
= VG_MAX( 0, vg_console
.history_pos
-1 );
399 console_history_get( vg_console
.input
, vg_console
.history_pos
);
400 console_move_cursor( &vg_console
.cursor_user
, &vg_console
.cursor_pos
, vg_list_size( vg_console
.input
), 1 );
403 else if( key
== GLFW_KEY_UP
)
405 if( mods
& GLFW_MOD_SHIFT
){}
408 vg_console
.history_pos
= VG_MAX
413 vg_console
.history_pos
+1,
416 vg_list_size( vg_console
.history
),
417 vg_console
.history_count
- 1
422 console_history_get( vg_console
.input
, vg_console
.history_pos
);
423 console_move_cursor( &vg_console
.cursor_user
, &vg_console
.cursor_pos
, vg_list_size( vg_console
.input
), 1);
426 else if( key
== GLFW_KEY_BACKSPACE
) // Lookback delete
428 vg_console
.cursor_user
= console_delete_char( -1 );
429 vg_console
.cursor_pos
= vg_console
.cursor_user
;
431 else if( key
== GLFW_KEY_DELETE
) // Lookforward delete
433 vg_console
.cursor_user
= console_delete_char( 1 );
434 vg_console
.cursor_pos
= vg_console
.cursor_user
;
436 else if( key
== GLFW_KEY_HOME
) // Home key
438 if( mods
& GLFW_MOD_SHIFT
)
439 console_move_cursor( &vg_console
.cursor_user
, NULL
, -10000, 0 );
441 console_move_cursor( &vg_console
.cursor_user
, &vg_console
.cursor_pos
, -10000, 1 );
443 else if( key
== GLFW_KEY_END
) // End key
445 if( mods
& GLFW_MOD_SHIFT
)
446 console_move_cursor( &vg_console
.cursor_user
, NULL
, 10000, 0 );
448 console_move_cursor( &vg_console
.cursor_user
, &vg_console
.cursor_pos
, vg_list_size( vg_console
.input
), 1 );
450 else if( key
== GLFW_KEY_A
)
452 if( mods
& GLFW_MOD_CONTROL
) // Select all
454 console_move_cursor( &vg_console
.cursor_user
, NULL
, 10000, 0);
455 console_move_cursor( &vg_console
.cursor_pos
, NULL
, -10000, 0);
458 else if( key
== GLFW_KEY_C
) // Copy
460 if( mods
& GLFW_MOD_CONTROL
)
462 console_to_clipboard();
465 else if( key
== GLFW_KEY_X
) // Cut
467 if( mods
& GLFW_MOD_CONTROL
)
469 console_to_clipboard();
470 vg_console
.cursor_user
= console_delete_char(0);
471 vg_console
.cursor_pos
= vg_console
.cursor_user
;
474 else if( key
== GLFW_KEY_V
) // Paste
476 if( mods
& GLFW_MOD_CONTROL
)
478 console_clipboard_paste();
481 else if( key
== GLFW_KEY_ENTER
)
483 if( !strlen( vg_console
.input
) )
486 vg_info( "%s\n", vg_console
.input
);
488 if( strcmp( vg_console
.input
, vg_console
.history
[ vg_console
.history_last
]) )
490 vg_console
.history_last
= ( vg_console
.history_last
+ 1) % vg_list_size(vg_console
.history
);
491 vg_console
.history_count
= VG_MIN( vg_list_size( vg_console
.history
), vg_console
.history_count
+ 1 );
492 strcpy( vg_console
.history
[ vg_console
.history_last
], vg_console
.input
);
495 vg_console
.history_pos
= -1;
496 execute_console_input( vg_console
.input
);
497 console_move_cursor( &vg_console
.cursor_user
, &vg_console
.cursor_pos
, -10000, 1 );
498 vg_console
.input
[0] = '\0';
503 // Handle an OS based input of UTF32 character from the keyboard or such
504 static void console_proc_wchar( GLFWwindow
* ptrW
, u32 uWchar
)
506 //LOG_INFO("Recieved wchar: %u\n", uWchar);
507 if( uWchar
<= 0x7F && (char)uWchar
!= 0x60)
509 console_put_char((char)uWchar
);