3756897230c80d909f5293a4c788958503fb4ac4
30 int cursor_user
, cursor_pos
, string_length
;
32 char history
[32][512];
33 int history_last
, history_pos
, history_count
;
38 vg_console
= { .scale
= 1 };
40 static int vg_console_enabled(void) { return vg_console
.enabled
; }
42 static void vg_convar_push( struct vg_convar cv
)
44 arrpush( vg_console
.convars
, cv
);
47 static void vg_console_draw( void )
49 if( !vg_console
.enabled
)
52 int ptr
= vg_console
.current
- vg_console
.len
;
54 ptr
+= vg_list_size( vg_console
.lines
);
57 ui_global_ctx
.cursor
[0] = 0;
58 ui_global_ctx
.cursor
[1] = 0;
59 ui_global_ctx
.cursor
[3] = vg_console
.len
*8*vg_console
.scale
;
60 ui_fill_x( &ui_global_ctx
);
62 ui_new_node( &ui_global_ctx
);
64 ui_fill_rect( &ui_global_ctx
, ui_global_ctx
.cursor
, 0x77333333 );
66 ui_global_ctx
.cursor
[3] = 8*vg_console
.scale
;
67 ui_align_bottom( &ui_global_ctx
);
69 for( int i
= 0; i
< vg_console
.len
; i
++ )
71 ui_text( &ui_global_ctx
, vg_console
.lines
[ptr
], vg_console
.scale
, 0 );
72 ui_global_ctx
.cursor
[1] -= 8*vg_console
.scale
;
76 ptr
= vg_list_size( vg_console
.lines
)-1;
79 ui_end_down( &ui_global_ctx
);
81 ui_global_ctx
.cursor
[1] += 2;
82 ui_global_ctx
.cursor
[3] = 8*vg_console
.scale
;
84 ui_new_node( &ui_global_ctx
);
86 ui_fill_rect( &ui_global_ctx
, ui_global_ctx
.cursor
, 0x77333333 );
88 ui_text( &ui_global_ctx
, vg_console
.input
, vg_console
.scale
, 0 );
90 int start
= VG_MIN( vg_console
.cursor_pos
, vg_console
.cursor_user
),
91 end
= VG_MAX( vg_console
.cursor_pos
, vg_console
.cursor_user
);
93 ui_global_ctx
.cursor
[0] = start
* 6 * vg_console
.scale
;
94 ui_global_ctx
.cursor
[2] = (start
== end
? 1: (end
-start
)) * 6 * vg_console
.scale
;
96 ui_fill_rect( &ui_global_ctx
, ui_global_ctx
.cursor
, 0x66ffffff );
98 ui_end_down( &ui_global_ctx
);
101 void vg_console_println( const char *str
)
103 if( vg_console
.len
< vg_list_size( vg_console
.lines
) )
106 strcpy( vg_console
.lines
[ vg_console
.current
++ ], str
);
108 if( vg_console
.current
>= vg_list_size( vg_console
.lines
) )
109 vg_console
.current
= 0;
112 static void vg_console_init(void)
114 vg_log_callback
= vg_console_println
;
116 vg_convar_push( (struct vg_convar
)
117 { .name
= "console_scale", .data
= &vg_console
.scale
, .data_type
= k_convar_dtype_i32
,
118 .opt_i32
= { .clamp
= 1, .min
= 1, .max
= 7 } } );
121 static void vg_console_free(void)
123 arrfree( vg_console
.convars
);
126 // Returns advance amount
127 static int console_token_read( char *buf
, int buf_len
, const char *src
)
129 int wait_for_next
= 0;
131 for( int i
= 0; i
< buf_len
; i
++ )
135 if( isspace( src
[i
] ) )
158 static void execute_console_input( const char *cmd
)
161 int cmd_pos
= console_token_read( temp
, 512, cmd
);
165 for( int i
= 0; i
< arrlen( vg_console
.convars
); i
++ )
167 struct vg_convar
*cv
= &vg_console
.convars
[ i
];
168 if( !strcmp( cv
->name
, temp
) )
170 // Matched, try get value
171 if( console_token_read( temp
, 512, cmd
+cmd_pos
) )
173 switch( cv
->data_type
)
175 case k_convar_dtype_u32
:
176 case k_convar_dtype_i32
:
178 data_int
= atoi( temp
);
179 *((int *)cv
->data
) = cv
->opt_i32
.clamp
? VG_MIN( VG_MAX(data_int
, cv
->opt_i32
.min
), cv
->opt_i32
.max
): data_int
;
182 case k_convar_dtype_f32
: *((float *)cv
->data
) = atof( temp
); break;
187 switch( cv
->data_type
)
189 case k_convar_dtype_i32
: vg_info( "= %d\n", *((int *)cv
->data
) ); break;
190 case k_convar_dtype_u32
: vg_info( "= %u\n", *((u32
*)cv
->data
) ); break;
191 case k_convar_dtype_f32
: vg_info( "= %.4f\n", *((float *)cv
->data
) ); break;
199 vg_error( "No command/variable named '%s'\n", temp
);
202 // =============================================================================================================================
205 static void console_make_selection( int* start
, int* end
)
207 *start
= VG_MIN( vg_console
.cursor_pos
, vg_console
.cursor_user
);
208 *end
= VG_MAX( vg_console
.cursor_pos
, vg_console
.cursor_user
);
211 static void console_move_cursor( int* cursor0
, int* cursor1
, int dir
, int snap_together
)
213 *cursor0
= VG_MAX( 0, vg_console
.cursor_user
+ dir
);
214 *cursor0
= VG_MIN( VG_MIN( vg_list_size( vg_console
.input
), strlen( vg_console
.input
)), *cursor0
);
219 static int console_makeroom( int datastart
, int length
)
221 int move_to
= VG_MIN( datastart
+length
, vg_list_size( vg_console
.input
) );
222 int move_amount
= strlen( vg_console
.input
)-datastart
;
223 int move_end
= VG_MIN( move_to
+move_amount
, vg_list_size( vg_console
.input
) );
224 move_amount
= move_end
-move_to
;
227 memmove( &vg_console
.input
[ move_to
], &vg_console
.input
[ datastart
], move_end
-move_to
);
229 vg_console
.input
[ move_end
] = '\0';
231 return VG_MIN( length
, vg_list_size( vg_console
.input
)-datastart
);
234 static int console_delete_char( int direction
)
237 console_make_selection( &start
, &end
);
239 // There is no selection
242 if( direction
== 1 ) end
= VG_MIN( end
+1, strlen( vg_console
.input
) );
243 else if( direction
== -1 ) start
= VG_MAX( start
-1, 0 );
246 // Still no selction, no need to do anything
250 // Copy the end->terminator to start
251 int remaining_length
= strlen( vg_console
.input
)+1-end
;
252 memmove( &vg_console
.input
[ start
], &vg_console
.input
[ end
], remaining_length
);
256 static void console_to_clipboard(void)
259 console_make_selection( &start
, &end
);
264 memcpy( buffer
, &vg_console
.input
[ start
], end
-start
);
265 buffer
[ end
-start
] = 0x00;
266 glfwSetClipboardString( NULL
, buffer
);
270 static void console_clipboard_paste(void)
272 int datastart
= console_delete_char(0);
273 const char* clipboard
= glfwGetClipboardString(NULL
);
274 int length
= strlen(clipboard
);
276 int cpylength
= console_makeroom(datastart
, length
);
278 memcpy( vg_console
.input
+ datastart
, clipboard
, cpylength
);
280 console_move_cursor( &vg_console
.cursor_user
, &vg_console
.cursor_pos
, cpylength
, 1 );
283 static void console_put_char( char c
)
285 if( !vg_console
.enabled
)
288 vg_console
.cursor_user
= console_delete_char(0);
290 if( console_makeroom( vg_console
.cursor_user
, 1 ) )
291 vg_console
.input
[ vg_console
.cursor_user
] = c
;
293 console_move_cursor( &vg_console
.cursor_user
, &vg_console
.cursor_pos
, 1, 1 );
296 static void console_add_to_history( const char* str
)
298 if( strcmp( str
, vg_console
.history
[ vg_console
.history_last
]) )
300 vg_console
.history_last
= ( vg_console
.history_last
+ 1) % vg_list_size(vg_console
.history
);
301 vg_console
.history_count
= VG_MIN( vg_list_size( vg_console
.history
), vg_console
.history_count
+ 1 );
302 strcpy( vg_console
.history
[ vg_console
.history_last
], str
);
306 static void console_history_get( char* buf
, int entry_num
)
308 if( !vg_console
.history_count
)
311 int pick
= (vg_console
.history_last
- VG_MIN( entry_num
, vg_console
.history_count
-1 )) % vg_list_size( vg_console
.history
);
312 strcpy( buf
, vg_console
.history
[ pick
] );
315 static void console_proc_key( GLFWwindow
* ptrW
, int key
, int scancode
, int action
, int mods
)
319 int cursor_diff
= vg_console
.cursor_pos
- vg_console
.cursor_user
? 0: 1;
321 if( key
== GLFW_KEY_GRAVE_ACCENT
)
323 vg_console
.enabled
= !vg_console
.enabled
;
327 if( !vg_console
.enabled
)
330 if( key
== GLFW_KEY_LEFT
)
332 if( mods
& GLFW_MOD_SHIFT
) // Receed secondary cursor
334 console_move_cursor( &vg_console
.cursor_user
, NULL
, -1, 0 );
336 else // Match and receed both cursors
338 console_move_cursor( &vg_console
.cursor_user
, &vg_console
.cursor_pos
, -cursor_diff
, 1 );
341 else if( key
== GLFW_KEY_RIGHT
) // Advance secondary cursor
343 if( mods
& GLFW_MOD_SHIFT
)
345 console_move_cursor( &vg_console
.cursor_user
, NULL
, 1, 0 );
347 else // Match and advance both cursors
349 console_move_cursor( &vg_console
.cursor_user
, &vg_console
.cursor_pos
, +cursor_diff
, 1 );
352 else if( key
== GLFW_KEY_DOWN
)
354 if( mods
& GLFW_MOD_SHIFT
){}
357 vg_console
.history_pos
= VG_MAX( 0, vg_console
.history_pos
-1 );
358 console_history_get( vg_console
.input
, vg_console
.history_pos
);
359 console_move_cursor( &vg_console
.cursor_user
, &vg_console
.cursor_pos
, vg_list_size( vg_console
.input
), 1 );
362 else if( key
== GLFW_KEY_UP
)
364 if( mods
& GLFW_MOD_SHIFT
){}
367 vg_console
.history_pos
= VG_MAX
372 vg_console
.history_pos
+1,
375 vg_list_size( vg_console
.history
),
376 vg_console
.history_count
- 1
381 console_history_get( vg_console
.input
, vg_console
.history_pos
);
382 console_move_cursor( &vg_console
.cursor_user
, &vg_console
.cursor_pos
, vg_list_size( vg_console
.input
), 1);
385 else if( key
== GLFW_KEY_BACKSPACE
) // Lookback delete
387 vg_console
.cursor_user
= console_delete_char( -1 );
388 vg_console
.cursor_pos
= vg_console
.cursor_user
;
390 else if( key
== GLFW_KEY_DELETE
) // Lookforward delete
392 vg_console
.cursor_user
= console_delete_char( 1 );
393 vg_console
.cursor_pos
= vg_console
.cursor_user
;
395 else if( key
== GLFW_KEY_HOME
) // Home key
397 if( mods
& GLFW_MOD_SHIFT
)
398 console_move_cursor( &vg_console
.cursor_user
, NULL
, -10000, 0 );
400 console_move_cursor( &vg_console
.cursor_user
, &vg_console
.cursor_pos
, -10000, 1 );
402 else if( key
== GLFW_KEY_END
) // End key
404 if( mods
& GLFW_MOD_SHIFT
)
405 console_move_cursor( &vg_console
.cursor_user
, NULL
, 10000, 0 );
407 console_move_cursor( &vg_console
.cursor_user
, &vg_console
.cursor_pos
, vg_list_size( vg_console
.input
), 1 );
409 else if( key
== GLFW_KEY_A
)
411 if( mods
& GLFW_MOD_CONTROL
) // Select all
413 console_move_cursor( &vg_console
.cursor_user
, NULL
, 10000, 0);
414 console_move_cursor( &vg_console
.cursor_pos
, NULL
, -10000, 0);
417 else if( key
== GLFW_KEY_C
) // Copy
419 if( mods
& GLFW_MOD_CONTROL
)
421 console_to_clipboard();
424 else if( key
== GLFW_KEY_X
) // Cut
426 if( mods
& GLFW_MOD_CONTROL
)
428 console_to_clipboard();
429 vg_console
.cursor_user
= console_delete_char(0);
430 vg_console
.cursor_pos
= vg_console
.cursor_user
;
433 else if( key
== GLFW_KEY_V
) // Paste
435 if( mods
& GLFW_MOD_CONTROL
)
437 console_clipboard_paste();
440 else if( key
== GLFW_KEY_ENTER
)
442 if( !strlen( vg_console
.input
) )
445 vg_info( "%s\n", vg_console
.input
);
447 console_add_to_history( vg_console
.input
);
448 vg_console
.history_pos
= -1;
449 execute_console_input( vg_console
.input
);
450 console_move_cursor( &vg_console
.cursor_user
, &vg_console
.cursor_pos
, -10000, 1 );
451 vg_console
.input
[0] = '\0';
456 // Handle an OS based input of UTF32 character from the keyboard or such
457 static void console_proc_wchar( GLFWwindow
* ptrW
, u32 uWchar
)
459 //LOG_INFO("Recieved wchar: %u\n", uWchar);
460 if( uWchar
<= 0x7F && (char)uWchar
!= 0x60)
462 console_put_char((char)uWchar
);