94de7d205db4c8d46b65eeac44daddf4de837219
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_history_get( char* buf
, int entry_num
)
298 if( !vg_console
.history_count
)
301 int pick
= (vg_console
.history_last
- VG_MIN( entry_num
, vg_console
.history_count
-1 )) % vg_list_size( vg_console
.history
);
302 strcpy( buf
, vg_console
.history
[ pick
] );
305 static void console_proc_key( GLFWwindow
* ptrW
, int key
, int scancode
, int action
, int mods
)
309 int cursor_diff
= vg_console
.cursor_pos
- vg_console
.cursor_user
? 0: 1;
311 if( key
== GLFW_KEY_GRAVE_ACCENT
)
313 vg_console
.enabled
= !vg_console
.enabled
;
317 if( !vg_console
.enabled
)
320 if( key
== GLFW_KEY_LEFT
)
322 if( mods
& GLFW_MOD_SHIFT
) // Receed secondary cursor
324 console_move_cursor( &vg_console
.cursor_user
, NULL
, -1, 0 );
326 else // Match and receed both cursors
328 console_move_cursor( &vg_console
.cursor_user
, &vg_console
.cursor_pos
, -cursor_diff
, 1 );
331 else if( key
== GLFW_KEY_RIGHT
) // Advance secondary cursor
333 if( mods
& GLFW_MOD_SHIFT
)
335 console_move_cursor( &vg_console
.cursor_user
, NULL
, 1, 0 );
337 else // Match and advance both cursors
339 console_move_cursor( &vg_console
.cursor_user
, &vg_console
.cursor_pos
, +cursor_diff
, 1 );
342 else if( key
== GLFW_KEY_DOWN
)
344 if( mods
& GLFW_MOD_SHIFT
){}
347 vg_console
.history_pos
= VG_MAX( 0, vg_console
.history_pos
-1 );
348 console_history_get( vg_console
.input
, vg_console
.history_pos
);
349 console_move_cursor( &vg_console
.cursor_user
, &vg_console
.cursor_pos
, vg_list_size( vg_console
.input
), 1 );
352 else if( key
== GLFW_KEY_UP
)
354 if( mods
& GLFW_MOD_SHIFT
){}
357 vg_console
.history_pos
= VG_MAX
362 vg_console
.history_pos
+1,
365 vg_list_size( vg_console
.history
),
366 vg_console
.history_count
- 1
371 console_history_get( vg_console
.input
, vg_console
.history_pos
);
372 console_move_cursor( &vg_console
.cursor_user
, &vg_console
.cursor_pos
, vg_list_size( vg_console
.input
), 1);
375 else if( key
== GLFW_KEY_BACKSPACE
) // Lookback delete
377 vg_console
.cursor_user
= console_delete_char( -1 );
378 vg_console
.cursor_pos
= vg_console
.cursor_user
;
380 else if( key
== GLFW_KEY_DELETE
) // Lookforward delete
382 vg_console
.cursor_user
= console_delete_char( 1 );
383 vg_console
.cursor_pos
= vg_console
.cursor_user
;
385 else if( key
== GLFW_KEY_HOME
) // Home key
387 if( mods
& GLFW_MOD_SHIFT
)
388 console_move_cursor( &vg_console
.cursor_user
, NULL
, -10000, 0 );
390 console_move_cursor( &vg_console
.cursor_user
, &vg_console
.cursor_pos
, -10000, 1 );
392 else if( key
== GLFW_KEY_END
) // End key
394 if( mods
& GLFW_MOD_SHIFT
)
395 console_move_cursor( &vg_console
.cursor_user
, NULL
, 10000, 0 );
397 console_move_cursor( &vg_console
.cursor_user
, &vg_console
.cursor_pos
, vg_list_size( vg_console
.input
), 1 );
399 else if( key
== GLFW_KEY_A
)
401 if( mods
& GLFW_MOD_CONTROL
) // Select all
403 console_move_cursor( &vg_console
.cursor_user
, NULL
, 10000, 0);
404 console_move_cursor( &vg_console
.cursor_pos
, NULL
, -10000, 0);
407 else if( key
== GLFW_KEY_C
) // Copy
409 if( mods
& GLFW_MOD_CONTROL
)
411 console_to_clipboard();
414 else if( key
== GLFW_KEY_X
) // Cut
416 if( mods
& GLFW_MOD_CONTROL
)
418 console_to_clipboard();
419 vg_console
.cursor_user
= console_delete_char(0);
420 vg_console
.cursor_pos
= vg_console
.cursor_user
;
423 else if( key
== GLFW_KEY_V
) // Paste
425 if( mods
& GLFW_MOD_CONTROL
)
427 console_clipboard_paste();
430 else if( key
== GLFW_KEY_ENTER
)
432 if( !strlen( vg_console
.input
) )
435 vg_info( "%s\n", vg_console
.input
);
437 if( strcmp( vg_console
.input
, vg_console
.history
[ vg_console
.history_last
]) )
439 vg_console
.history_last
= ( vg_console
.history_last
+ 1) % vg_list_size(vg_console
.history
);
440 vg_console
.history_count
= VG_MIN( vg_list_size( vg_console
.history
), vg_console
.history_count
+ 1 );
441 strcpy( vg_console
.history
[ vg_console
.history_last
], vg_console
.input
);
444 vg_console
.history_pos
= -1;
445 execute_console_input( vg_console
.input
);
446 console_move_cursor( &vg_console
.cursor_user
, &vg_console
.cursor_pos
, -10000, 1 );
447 vg_console
.input
[0] = '\0';
452 // Handle an OS based input of UTF32 character from the keyboard or such
453 static void console_proc_wchar( GLFWwindow
* ptrW
, u32 uWchar
)
455 //LOG_INFO("Recieved wchar: %u\n", uWchar);
456 if( uWchar
<= 0x7F && (char)uWchar
!= 0x60)
458 console_put_char((char)uWchar
);