-/*
- * Console Interface
- */
-VG_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 );
-}
-
-VG_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)-1, strlen( vg_console.input )),
- *cursor0 );
-
- if( snap_together )
- *cursor1 = *cursor0;
-}
-
-VG_STATIC int console_makeroom( int datastart, int length )
-{
- int move_to = VG_MIN( datastart+length, vg_list_size( vg_console.input )-1 );
- int move_amount = strlen( vg_console.input )-datastart;
- int move_end =
- VG_MIN( move_to+move_amount, vg_list_size( vg_console.input )-1 );
- move_amount = move_end-move_to;
-
- if( move_amount )
- memmove( &vg_console.input[ move_to ],
- &vg_console.input[ datastart ],
- move_end-move_to );
-
- vg_console.input[ move_end ] = '\0';
-
- return VG_MIN( length, vg_list_size( vg_console.input )-datastart-1 );
-}
-
-VG_STATIC int console_delete_char( int direction )
-{
- int start, end;
- console_make_selection( &start, &end );
-
- /* 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 */
- if( !(end-start) )
- return 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 );
- return start;
-}
-
-VG_STATIC void console_to_clipboard(void)
-{
- int start, end;
- console_make_selection( &start, &end );
- char buffer[512];
-
- if( end-start ){
- memcpy( buffer, &vg_console.input[ start ], end-start );
- buffer[ end-start ] = 0x00;
- SDL_SetClipboardText( buffer );
- }
-}
-
-VG_STATIC void console_clipboard_paste(void)
-{
- if( !SDL_HasClipboardText() )
- return;
-
- char *text = SDL_GetClipboardText();
-
- if( !text )
- return;
-
- int datastart = console_delete_char( 0 );
- int length = strlen( text );
- int cpylength = console_makeroom( datastart, length );
-
- memcpy( vg_console.input + datastart, text, cpylength);
- console_move_cursor( &vg_console.cursor_user,
- &vg_console.cursor_pos, cpylength, 1 );
- SDL_free( text );
-
- console_update_suggestions();
-}
-
-VG_STATIC void console_put_char( char c )
-{
- if( !vg_console.enabled )
- return;
-
- vg_console.cursor_user = console_delete_char(0);
-
- if( console_makeroom( vg_console.cursor_user, 1 ) )
- vg_console.input[ vg_console.cursor_user ] = c;
-
- console_move_cursor( &vg_console.cursor_user, &vg_console.cursor_pos, 1, 1 );
-}
-
-VG_STATIC void console_history_get( char* buf, int entry_num )
-{
- if( !vg_console.history_count )
- return;
-
- 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 ] );
-}
-
-/* Receed secondary cursor */
-VG_STATIC void _console_left_select(void)
-{
- console_move_cursor( &vg_console.cursor_user, NULL, -1, 0 );
-}
-
-/* Match and receed both cursors */
-VG_STATIC void _console_left(void)
-{
- int cursor_diff = vg_console.cursor_pos - vg_console.cursor_user? 0: 1;
-
- console_move_cursor( &vg_console.cursor_user,
- &vg_console.cursor_pos, -cursor_diff, 1 );
-}
-
-VG_STATIC void _console_right_select(void)
-{
- console_move_cursor( &vg_console.cursor_user, NULL, 1, 0 );
-}
-
-VG_STATIC void _console_right(void)
-{
- int cursor_diff = vg_console.cursor_pos - vg_console.cursor_user? 0: 1;
-
- console_move_cursor( &vg_console.cursor_user,
- &vg_console.cursor_pos, +cursor_diff, 1 );
-}
-
-VG_STATIC void _console_down(void)
-{
- 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, 1 );
-}
-
-VG_STATIC void _console_up(void)
-{
- vg_console.history_pos = VG_MAX
- (
- 0,
- VG_MIN
- (
- vg_console.history_pos+1,
- VG_MIN
- (
- vg_list_size( vg_console.history ),
- vg_console.history_count - 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, 1);
-}
-
-VG_STATIC void _console_backspace(void)
-{
- vg_console.cursor_user = console_delete_char( -1 );
- vg_console.cursor_pos = vg_console.cursor_user;
-
- console_update_suggestions();
-}
-
-VG_STATIC void _console_delete(void)
-{
- vg_console.cursor_user = console_delete_char( 1 );
- vg_console.cursor_pos = vg_console.cursor_user;
-
- console_update_suggestions();
-}
-
-VG_STATIC void _console_home_select(void)
-{
- console_move_cursor( &vg_console.cursor_user, NULL, -10000, 0 );
-}
-
-VG_STATIC void _console_home(void)
-{
- console_move_cursor( &vg_console.cursor_user,
- &vg_console.cursor_pos, -10000, 1 );
-}
-
-VG_STATIC void _console_end_select(void)
-{
- console_move_cursor( &vg_console.cursor_user, NULL, 10000, 0 );
-}
-
-VG_STATIC void _console_end(void)
-{
- console_move_cursor( &vg_console.cursor_user,
- &vg_console.cursor_pos,
- vg_list_size(vg_console.input)-1, 1 );
-}
-
-VG_STATIC void _console_select_all(void)
-{
- console_move_cursor( &vg_console.cursor_user, NULL, 10000, 0);
- console_move_cursor( &vg_console.cursor_pos, NULL, -10000, 0);
-}
-
-VG_STATIC void _console_cut(void)
-{
- console_to_clipboard();
- vg_console.cursor_user = console_delete_char(0);
- vg_console.cursor_pos = vg_console.cursor_user;
-}
-
-VG_STATIC void _console_enter(void)
-{
- if( !strlen( vg_console.input ) )
- return;
-
- vg_info( "%s\n", vg_console.input );
-
- 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_pos = -1;
- vg_execute_console_input( vg_console.input );
- console_move_cursor( &vg_console.cursor_user,
- &vg_console.cursor_pos, -10000, 1 );
- vg_console.input[0] = '\0';
-
- console_update_suggestions();
-}
-