-
Notifications
You must be signed in to change notification settings - Fork 0
/
console.c
1176 lines (978 loc) · 49.4 KB
/
console.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// TODO(jerry): Handle scrollback or don't!
// The ring buffer implementation does not print whatever is at the head
// as it is being used for writing purposes. So an extra byte is added.
#define CONSOLE_SCROLLBACK_BUFFER_SIZE (16384 + 1)
#define CONSOLE_INPUT_LINE_BUFFER_SIZE (512)
#define CONSOLE_DEFAULT_FONT_PATH "resources/console/LiberationMono-Bold.ttf"
#define CONSOLE_CURSOR_BLINK_TIMER_MAX (0.75)
#define CONSOLE_SCREEN_PORTION (0.65)
#define CONSOLE_INPUT_HISTORY_MAX_ENTRIES (8)
#define CONSOLE_DROPSHADOW_X_OFFSET (2.3)
#define CONSOLE_DROPSHADOW_Y_OFFSET (1)
#define CONSOLE_DEFAULT_FONT_SIZE (18)
#define CONSOLE_MAXIMUM_ALLOWED_AUTO_COMPLETION_MATCHES (8)
struct console_color {
float r;
float g;
float b;
float a;
};
struct console_color console_color(float r, float g, float b, float a) {
return (struct console_color) {
.r = r,
.g = g,
.b = b,
.a = a
};
}
struct console_color console_color_from_encoded(uint32_t rgba) {
struct console_color result;
decode_rgba_from_uint32(rgba, (float*)&result);
return result;
}
uint8_t _console_nibble_from_hexadecimal(char hexadecimal_character) {
// Normalize to uppercase if it's a letter.
if (hexadecimal_character >= 'a' && hexadecimal_character <= 'z') {
hexadecimal_character -= 32;
}
switch (hexadecimal_character) {
case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': {
return hexadecimal_character - '0';
} break;
case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': {
return (hexadecimal_character - 'A') + 10;
} break;
}
return 0;
}
struct console_color console_color_from_hexadecimal_string(char* hexadecimal_string, size_t hexadecimal_string_length) {
uint32_t encoded_rgba = 0;
if (hexadecimal_string_length != 6 && hexadecimal_string_length != 8) {
return console_color_from_encoded(0);
}
if (hexadecimal_string_length >= 6) {
uint8_t bytes[4] = {
_console_nibble_from_hexadecimal(hexadecimal_string[0]) << 4 | _console_nibble_from_hexadecimal(hexadecimal_string[1]),
_console_nibble_from_hexadecimal(hexadecimal_string[2]) << 4 | _console_nibble_from_hexadecimal(hexadecimal_string[3]),
_console_nibble_from_hexadecimal(hexadecimal_string[4]) << 4 | _console_nibble_from_hexadecimal(hexadecimal_string[5]),
0xFF,
};
if (hexadecimal_string_length == 8) {
bytes[3] = _console_nibble_from_hexadecimal(hexadecimal_string[6]) << 4 | _console_nibble_from_hexadecimal(hexadecimal_string[7]);
}
encoded_rgba = bytes[0] << 24 | bytes[1] << 16 | bytes[2] << 8 | bytes[3];
} else {
return console_color_from_encoded(0);
}
return console_color_from_encoded(encoded_rgba);
}
enum console_animation_state {
CONSOLE_ANIMATION_STATE_DEFAULT,
CONSOLE_ANIMATION_STATE_OPENING,
CONSOLE_ANIMATION_STATE_CLOSING,
};
struct console_theme {
struct console_color text_color;
struct console_color text_shadow_color;
struct console_color background_color;
struct console_color input_line_color;
struct console_color completion_suggestion_color;
struct console_color currently_selection_completion_suggestion_color;
struct console_color cursor_color;
};
// How I sorely wish these can be default initialized...
// This is the one time I want a constructor.
static struct console_theme default_console_theme = {};
struct console {
struct console_theme theme;
struct allocator* allocator;
graphics_context_font_handle font;
char scrollback_buffer[CONSOLE_SCROLLBACK_BUFFER_SIZE];
size_t scrollback_buffer_read_cursor;
size_t scrollback_buffer_write_cursor;
// since this should be monospaced this is completely fine to use
struct graphics_context_text_extents character_metrics;
float scroll_y;
float width;
float height;
uint32_t lines_per_page;
uint32_t input_line_count;
uint32_t input_line_write_cursor_location;
char input_line[CONSOLE_INPUT_LINE_BUFFER_SIZE];
int8_t selected_history_buffer_index; // read index
int8_t history_buffer_index;
uint8_t history_buffer_count;
struct {
size_t count;
char buffer[CONSOLE_INPUT_LINE_BUFFER_SIZE];
} input_history_buffers[CONSOLE_INPUT_HISTORY_MAX_ENTRIES];
bool shown;
float slide_timer;
float cursor_blink_timer;
bool cursor_show;
uint8_t animation_state;
bool taking_command_suggestions;
uint8_t command_completion_best_match_selected_index;
uint8_t command_completion_best_matches_count;
// The string storage comes from the commands themselves. So this is safe.
char* command_completion_best_matches[CONSOLE_MAXIMUM_ALLOWED_AUTO_COMPLETION_MATCHES];
};
struct console_system {
uint64_t variable_count;
struct {
struct console_system_variable* head;
struct console_system_variable* tail;
struct console_system_variable null;
} variable_list;
uint64_t command_count;
struct {
struct console_system_command* head;
struct console_system_command* tail;
struct console_system_command null;
} command_list;
};
static struct console_system _global_console_system = {};
static struct console _global_console = {};
void console_clear(void) {
memset(_global_console.scrollback_buffer, 0, CONSOLE_SCROLLBACK_BUFFER_SIZE);
}
static inline void _console_update_resolution(struct graphics_context_api* graphics) {
graphics->set_virtual_resolution(0, 0);
struct graphics_context_virtual_dimensions virtual_dimensions = graphics->virtual_dimensions();
_global_console.width = virtual_dimensions.width;
_global_console.height = virtual_dimensions.height * CONSOLE_SCREEN_PORTION;
_global_console.lines_per_page = _global_console.height / (_global_console.character_metrics.height);
}
#include "engine_default_console_commands.c"
// this file was generated with xxd. Don't touch it.
#include "baked_data/liberation_mono_font_data.c"
void console_initialize(struct graphics_context_api* graphics) {
// hardcoded path.
// please make sure this is a monospaced font.
_global_console.font = graphics_context_load_font_from_buffer_and_keyed_as(&application_graphics_context, resources_console_LiberationMono_Bold_ttf, CONSOLE_DEFAULT_FONT_SIZE, "resources/console/LiberationMono-Bold.ttf");
_global_console.character_metrics = graphics->measure_text(_global_console.font, "#", 1.0);
_global_console.allocator = &_global_temporary_allocator.interface;
_global_console_system.variable_list.head = &_global_console_system.variable_list.null;
_global_console_system.variable_list.tail = &_global_console_system.variable_list.null;
_global_console_system.command_list.head = &_global_console_system.command_list.null;
_global_console_system.command_list.tail = &_global_console_system.command_list.null;
_register_default_commands();
// default theme
{
default_console_theme = (struct console_theme) {
.text_color = console_color(1.0, 1.0, 1.0, 1.0), // white
.text_shadow_color = console_color(0.0, 0.0, 0.0, 0.8), // transparent black
.background_color = console_color_from_encoded(0x008C77EC), // aqua green
.input_line_color = console_color_from_encoded(0x005A44FC), // darker aqua green
.completion_suggestion_color = console_color_from_encoded(0x003822FF), // even darker aqua green
.currently_selection_completion_suggestion_color = console_color_from_encoded(0xEA3C53FF), // reddish
.cursor_color = console_color(0.0, 1.0, 0.0, 0.8), // green
};
_global_console.theme = default_console_theme;
}
_console_update_resolution(graphics);
/* console_clear(); */
}
void console_printf(char* text, ...) {
static char _temporary_format_buffer[2048] = {};
memset(_temporary_format_buffer, 0, 2048);
size_t written;
{
va_list variadic_arguments;
va_start(variadic_arguments, text);
written = vsnprintf(_temporary_format_buffer, 2048, text, variadic_arguments);
va_end(variadic_arguments);
}
{
va_list variadic_arguments;
va_start(variadic_arguments, text);
vfprintf(stderr, text, variadic_arguments);
va_end(variadic_arguments);
}
// ring buffer writing
for (size_t character_index = 0; character_index < written; ++character_index) {
_global_console.scrollback_buffer[_global_console.scrollback_buffer_write_cursor++] = _temporary_format_buffer[character_index];
// Ring buffer wrap around writing.
if (_global_console.scrollback_buffer_write_cursor >= CONSOLE_SCROLLBACK_BUFFER_SIZE) {
_global_console.scrollback_buffer_write_cursor = 0;
_global_console.scrollback_buffer_read_cursor += 1;
}
// We have overstepped capacity so we will bump the read cursor forward which will "discard" the last
// character. (This is basically just a queue.)
if (_global_console.scrollback_buffer_write_cursor == _global_console.scrollback_buffer_read_cursor) {
_global_console.scrollback_buffer_read_cursor += 1;
}
if (_global_console.scrollback_buffer_read_cursor >= CONSOLE_SCROLLBACK_BUFFER_SIZE) {
_global_console.scrollback_buffer_read_cursor = 0;
}
}
}
// This does not need to change for the ring buffer implementation.
// This will always be fine.
static inline uint32_t _console_count_lines(void) {
uint32_t counted_lines = 0;
float glyph_width = _global_console.character_metrics.width;
float console_width = _global_console.width;
char* scrollback_buffer = _global_console.scrollback_buffer;
float x_cursor = 0;
for (size_t character_index = 0; character_index < CONSOLE_SCROLLBACK_BUFFER_SIZE; ++character_index) {
bool should_line_break = false;
if (scrollback_buffer[character_index] > 0) {
x_cursor += glyph_width;
if (x_cursor >= console_width) {
should_line_break = true;
}
if (scrollback_buffer[character_index] == '\n' || scrollback_buffer[character_index] == '\r') {
should_line_break = true;
}
if (should_line_break) {
x_cursor = 0;
counted_lines++;
}
}
}
return counted_lines;
}
void console_scroll_by(float amount) {
float glyph_height = _global_console.character_metrics.height;
uint32_t line_count = _console_count_lines();
uint32_t lines_per_page = _global_console.lines_per_page;
float present_pages = (float)line_count / lines_per_page;
float scroll_displacement = (_global_console.scroll_y + amount);
float scrolled_pages_height = scroll_displacement + lines_per_page * glyph_height;
_global_console.scroll_y += amount;
}
//please inline
static void _console_display_codepoint(struct graphics_context_api* graphics, uint32_t codepoint, float* x_cursor, float* y_cursor, float glyph_width, float glyph_height, float console_width, float slide_offset_y) {
bool should_line_break = false;
if (codepoint != ' ' && is_whitespace_character(codepoint)) {
switch (codepoint) {
case '\r':
case '\n': {
should_line_break = true;
} break;
}
} else if (codepoint > 0) {
graphics->draw_codepoint(_global_console.font, codepoint, *x_cursor+CONSOLE_DROPSHADOW_X_OFFSET, *y_cursor+glyph_height+slide_offset_y+CONSOLE_DROPSHADOW_Y_OFFSET, 1.0, 0.0, 0.0, 0.0, 0.8);
graphics->draw_codepoint(_global_console.font, codepoint, *x_cursor, *y_cursor+glyph_height+slide_offset_y, 1.0, 1.0, 1.0, 1.0, 1.0);
*x_cursor += (glyph_width);
}
{
float next_glyph_start = *x_cursor + glyph_width;
if ((next_glyph_start) >= console_width) {
should_line_break = true;
}
}
if (should_line_break) {
*x_cursor = 0;
*y_cursor += (glyph_height);
}
}
void console_display(struct graphics_context_api* graphics) {
_console_update_resolution(graphics);
// To avoid the complexities of supporting virtual resolution,
// the console will be drawn in real screen resolutions always.
// cool sliding visual effects I guess
float slide_offset_y = 0;
{
if (_global_console.animation_state == CONSOLE_ANIMATION_STATE_OPENING) {
slide_offset_y = linear_interpolate_float(_global_console.height, 0, _global_console.slide_timer);
} else if (_global_console.animation_state == CONSOLE_ANIMATION_STATE_CLOSING) {
slide_offset_y = linear_interpolate_float(0, -_global_console.height, _global_console.slide_timer);
}
}
// clamping the console to console height.
float glyph_height = _global_console.character_metrics.height;
float glyph_width = _global_console.character_metrics.width;
uint32_t line_count = _console_count_lines();
// Reserve 1 line for the input line.
uint32_t lines_per_page = _global_console.lines_per_page-1;
// clamp the console scroller.
float max_scroll_y;
{
{
if (line_count < lines_per_page) {
line_count = lines_per_page;
}
max_scroll_y = (line_count-lines_per_page) * glyph_height;
}
if (_global_console.scroll_y < 0) {
_global_console.scroll_y = 0;
} else if (_global_console.scroll_y + lines_per_page * glyph_height > line_count * glyph_height) {
_global_console.scroll_y = max_scroll_y;
}
}
float console_width = _global_console.width;
float console_height = _global_console.height;
float input_line_y = _global_console.height - glyph_height * 0.7;
graphics->begin_drawing((struct camera){.scale_x = 1, .scale_y = 1});
{
struct console_color color = _global_console.theme.background_color;
graphics->draw_untextured_quad(0, slide_offset_y, console_width, console_height, color.r, color.g, color.b, color.a, null_shader);
}
{
struct console_color color = _global_console.theme.input_line_color;
graphics->draw_untextured_quad(0, input_line_y + slide_offset_y, _global_console.width, glyph_height*1.1, color.r, color.g, color.b, color.a, null_shader);
}
graphics->end_drawing();
graphics->begin_drawing((struct camera){.scale_x = 1, .scale_y = 1});
{
float x_cursor = 0;
float y_cursor = -(max_scroll_y);
graphics->set_scissor_region(0, 0, console_width, input_line_y);
// ring buffer code makes this a little nastier to do.
if (_global_console.scrollback_buffer_write_cursor > _global_console.scrollback_buffer_read_cursor) {
for (decode_utf8_iterator iterator = decode_utf8_from(_global_console.scrollback_buffer, CONSOLE_SCROLLBACK_BUFFER_SIZE);
decode_utf8_iterator_valid(&iterator);
decode_utf8_iterator_advance(&iterator)) {
_console_display_codepoint(graphics, iterator.codepoint, &x_cursor, &y_cursor, glyph_width, glyph_height, console_width, slide_offset_y);
}
} else if (_global_console.scrollback_buffer_write_cursor < _global_console.scrollback_buffer_read_cursor) {
// nasty wrap around handling...
// first half (tail -> end)
for (decode_utf8_iterator iterator = decode_utf8_from(_global_console.scrollback_buffer + _global_console.scrollback_buffer_read_cursor, CONSOLE_SCROLLBACK_BUFFER_SIZE - _global_console.scrollback_buffer_read_cursor);
decode_utf8_iterator_valid(&iterator);
decode_utf8_iterator_advance(&iterator)) {
_console_display_codepoint(graphics, iterator.codepoint, &x_cursor, &y_cursor, glyph_width, glyph_height, console_width, slide_offset_y);
}
// second half (begin -> tail)
for (decode_utf8_iterator iterator = decode_utf8_from(_global_console.scrollback_buffer, _global_console.scrollback_buffer_write_cursor);
decode_utf8_iterator_valid(&iterator);
decode_utf8_iterator_advance(&iterator)) {
_console_display_codepoint(graphics, iterator.codepoint, &x_cursor, &y_cursor, glyph_width, glyph_height, console_width, slide_offset_y);
}
}
}
graphics->end_drawing();
graphics->begin_drawing((struct camera){.scale_x = 1, .scale_y = 1});
{
{
struct console_color text_color = _global_console.theme.text_color;
struct console_color shadow_color = _global_console.theme.text_shadow_color;
graphics->draw_text(_global_console.font, _global_console.input_line, CONSOLE_DROPSHADOW_X_OFFSET, input_line_y + slide_offset_y + CONSOLE_DROPSHADOW_Y_OFFSET, 1.0, shadow_color.r, shadow_color.g, shadow_color.b, shadow_color.a);
graphics->draw_text(_global_console.font, _global_console.input_line, 0, input_line_y + slide_offset_y, 1.0, text_color.r, text_color.g, text_color.b, text_color.a);
}
if (_global_console.cursor_show) {
struct console_color cursor_color = _global_console.theme.cursor_color;
graphics->draw_untextured_quad(_global_console.input_line_write_cursor_location * glyph_width, input_line_y + slide_offset_y, glyph_width, glyph_height, cursor_color.r, cursor_color.g, cursor_color.b, cursor_color.a, null_shader);
}
}
graphics->end_drawing();
// NOTE(jerry): Because of the way my graphics_context works... This has to be done in two passes :(
if (_global_console.command_completion_best_matches_count > 0) {
{
{
float autocompletion_y_cursor = input_line_y + glyph_height*1.1;
graphics->begin_drawing((struct camera){.scale_x = 1, .scale_y = 1});
struct console_color selected_color = _global_console.theme.currently_selection_completion_suggestion_color;
struct console_color color = _global_console.theme.completion_suggestion_color;
for (size_t command_completion_suggestion_index = 0; command_completion_suggestion_index < _global_console.command_completion_best_matches_count; ++command_completion_suggestion_index) {
if (command_completion_suggestion_index == _global_console.command_completion_best_match_selected_index) {
graphics->draw_untextured_quad(0, autocompletion_y_cursor + slide_offset_y, _global_console.width, glyph_height*1.1, selected_color.r, selected_color.g, selected_color.b, selected_color.a, null_shader);
} else {
graphics->draw_untextured_quad(0, autocompletion_y_cursor + slide_offset_y, _global_console.width, glyph_height*1.1, color.r, color.g, color.b, color.a, null_shader);
}
autocompletion_y_cursor += glyph_height * 1.1;
}
graphics->end_drawing();
}
{
float autocompletion_y_cursor = input_line_y + glyph_height*1.1;
struct console_color color = _global_console.theme.text_color;
graphics->begin_drawing((struct camera){.scale_x = 1, .scale_y = 1});
for (size_t command_completion_suggestion_index = 0; command_completion_suggestion_index < _global_console.command_completion_best_matches_count; ++command_completion_suggestion_index) {
char* current_suggestion = _global_console.command_completion_best_matches[command_completion_suggestion_index];
graphics->draw_text(_global_console.font, current_suggestion, 0, autocompletion_y_cursor + slide_offset_y, 1.0, color.r, color.g, color.b, color.a);
autocompletion_y_cursor += glyph_height * 1.1;
}
graphics->end_drawing();
}
}
}
}
void console_system_execute(char* line, size_t line_size);
void console_submit(void) {
if (_global_console.input_line_count > 0) {
console_system_execute(_global_console.input_line, _global_console.input_line_count);
memcpy(_global_console.input_history_buffers[_global_console.history_buffer_index].buffer, _global_console.input_line, CONSOLE_INPUT_LINE_BUFFER_SIZE);
_global_console.input_history_buffers[_global_console.history_buffer_index++].count = _global_console.input_line_count;
_global_console.history_buffer_index &= (CONSOLE_INPUT_HISTORY_MAX_ENTRIES-1);
if (_global_console.history_buffer_count < CONSOLE_INPUT_HISTORY_MAX_ENTRIES) {
_global_console.history_buffer_count++;
}
_global_console.input_line_count = 0;
memset(_global_console.input_line, 0, CONSOLE_INPUT_LINE_BUFFER_SIZE);
}
}
void console_send_character(char character) {
if (!console_active()) {
return;
}
// reject characters we use for our own purposes.
if (character == '`' || character == '~' || character == '\t') {
return;
}
if (_global_console.input_line_count >= CONSOLE_INPUT_LINE_BUFFER_SIZE || character == '\r' || character == '\n') {
return;
}
// This looks a little nasty, but I'm not using a gap-buffer or anything
// this is just a normal "string" operation, so it's technically slow, but practically fast!
if (character == '\b') {
if (_global_console.input_line_count > 0) {
for (size_t cursor_location = _global_console.input_line_write_cursor_location-1; cursor_location < _global_console.input_line_count; ++cursor_location) {
_global_console.input_line[cursor_location] = _global_console.input_line[cursor_location+1];
}
_global_console.input_line[--_global_console.input_line_count] = 0;
--_global_console.input_line_write_cursor_location;
} else if (_global_console.input_line_count == 0) {
_global_console.input_line[0] = 0;
}
} else if (is_human_readable_ascii_character(character)) {
for (size_t cursor_location = _global_console.input_line_count; cursor_location > _global_console.input_line_write_cursor_location; --cursor_location) {
_global_console.input_line[cursor_location] = _global_console.input_line[cursor_location-1];
}
_global_console.input_line_count++;
_global_console.input_line[_global_console.input_line_write_cursor_location++] = character;
}
_global_console.command_completion_best_matches_count = 0;
_global_console.taking_command_suggestions = false;
}
bool console_active(void) {
return _global_console.shown;
}
void console_move_forward_character(void) {
if (_global_console.input_line_write_cursor_location+1 <= _global_console.input_line_count) {
_global_console.input_line_write_cursor_location++;
}
}
void console_move_backward_character(void) {
if (_global_console.input_line_write_cursor_location > 0) {
_global_console.input_line_write_cursor_location--;
}
}
void console_begin_command_completion(void) {
if (!_global_console.taking_command_suggestions) {
_global_console.command_completion_best_matches_count = 0;
_global_console.command_completion_best_match_selected_index = 0;
_global_console.taking_command_suggestions = true;
void _console_determine_best_completion_match_for_partial_command_string(char* command_string);
_console_determine_best_completion_match_for_partial_command_string(_global_console.input_line);
}
}
void console_input_command_suggestion(size_t command_suggestion_index) {
if (command_suggestion_index < _global_console.command_completion_best_matches_count) {
char* suggestion_string = _global_console.command_completion_best_matches[command_suggestion_index];
size_t length_of_suggestion_string = cstring_length(suggestion_string);
memset(_global_console.input_line, 0, _global_console.input_line_count);
_global_console.input_line_count = length_of_suggestion_string;
_global_console.input_line_write_cursor_location = _global_console.input_line_count;
strncpy(_global_console.input_line, suggestion_string, length_of_suggestion_string);
}
}
void console_stop_command_completion(void) {
if (_global_console.taking_command_suggestions) {
_global_console.command_completion_best_matches_count = 0;
_global_console.taking_command_suggestions = false;
}
}
void console_previous_command_completion_suggestion(void) {
if (_global_console.taking_command_suggestions) {
if (_global_console.command_completion_best_match_selected_index > 0) {
_global_console.command_completion_best_match_selected_index -= 1;
} else {
_global_console.command_completion_best_match_selected_index = _global_console.command_completion_best_matches_count-1;
}
}
}
void console_next_command_completion_suggestion(void) {
if (_global_console.taking_command_suggestions) {
_global_console.command_completion_best_match_selected_index += 1;
if (_global_console.command_completion_best_match_selected_index >= _global_console.command_completion_best_matches_count) {
_global_console.command_completion_best_match_selected_index = 0;
}
}
}
void console_move_forward_word(void) {
// First finish the current word, by running into whitespace.
while (_global_console.input_line_write_cursor_location < _global_console.input_line_count && !is_whitespace_character(_global_console.input_line[_global_console.input_line_write_cursor_location])) {
console_move_forward_character();
}
// Then find the next word by looking for the first next non-whitespace character.
while (_global_console.input_line_write_cursor_location < _global_console.input_line_count && is_whitespace_character(_global_console.input_line[_global_console.input_line_write_cursor_location])) {
console_move_forward_character();
}
}
void console_move_backward_word(void) {
// same as the above. Just in reverse.
while (_global_console.input_line_write_cursor_location > 0 && !is_whitespace_character(_global_console.input_line[_global_console.input_line_write_cursor_location])) {
console_move_backward_character();
}
while (_global_console.input_line_write_cursor_location > 0 && is_whitespace_character(_global_console.input_line[_global_console.input_line_write_cursor_location])) {
console_move_backward_character();
}
}
void console_kill_line_at_current_position(void) {
if (_global_console.input_line_write_cursor_location < _global_console.input_line_count) {
memset(_global_console.input_line + _global_console.input_line_write_cursor_location, 0, _global_console.input_line_count - _global_console.input_line_write_cursor_location);
_global_console.input_line_count = strlen(_global_console.input_line);
}
}
void console_previous_history_item(void) {
if (_global_console.selected_history_buffer_index < 0) {
_global_console.selected_history_buffer_index = _global_console.history_buffer_count - 1;
if (_global_console.selected_history_buffer_index < 0) {
return;
}
}
memcpy(_global_console.input_line, _global_console.input_history_buffers[_global_console.selected_history_buffer_index].buffer, CONSOLE_INPUT_LINE_BUFFER_SIZE);
_global_console.input_line_write_cursor_location = 0;
_global_console.input_line_count = _global_console.input_history_buffers[_global_console.selected_history_buffer_index--].count;
}
void console_next_history_item() {
if (_global_console.selected_history_buffer_index >= _global_console.history_buffer_count) {
_global_console.selected_history_buffer_index = 0;
}
memcpy(_global_console.input_line, _global_console.input_history_buffers[_global_console.selected_history_buffer_index].buffer, CONSOLE_INPUT_LINE_BUFFER_SIZE);
_global_console.input_line_write_cursor_location = 0;
_global_console.input_line_count = _global_console.input_history_buffers[_global_console.selected_history_buffer_index++].count;
}
void console_send_key_event(bool key_down_event, uint32_t keycode, bool alt_state, bool ctrl_state, bool shift_state) {
if (!console_active()) {
return;
}
if (key_down_event) {
// emacs / readline bindings
if (ctrl_state) {
console_stop_command_completion();
switch (keycode) {
case INPUT_KEY_A: {
_global_console.input_line_write_cursor_location = 0;
} break;
case INPUT_KEY_E: {
_global_console.input_line_write_cursor_location = _global_console.input_line_count;
} break;
case INPUT_KEY_F: {
console_move_forward_character();
} break;
case INPUT_KEY_B: {
console_move_backward_character();
} break;
case INPUT_KEY_K: {
console_kill_line_at_current_position();
} break;
}
} else if (alt_state) {
console_stop_command_completion();
switch (keycode) {
case INPUT_KEY_F: {
console_move_forward_word();
} break;
case INPUT_KEY_B: {
console_move_backward_word();
} break;
}
} else {
switch (keycode) {
case INPUT_KEY_RIGHT: {
console_move_forward_character();
console_stop_command_completion();
} break;
case INPUT_KEY_LEFT: {
console_move_backward_character();
console_stop_command_completion();
} break;
// go through the history buffers...
// This will "forget" the currently typed line, so be ware.
case INPUT_KEY_UP: {
console_previous_history_item();
console_stop_command_completion();
} break;
case INPUT_KEY_DOWN: {
console_next_history_item();
console_stop_command_completion();
} break;
case INPUT_KEY_ESCAPE: {
console_stop_command_completion();
} break;
case INPUT_KEY_RETURN: {
if (_global_console.taking_command_suggestions && _global_console.command_completion_best_matches_count > 0) {
console_input_command_suggestion(_global_console.command_completion_best_match_selected_index);
} else {
console_submit();
}
console_stop_command_completion();
} break;
case INPUT_KEY_TAB: {
if (_global_console.taking_command_suggestions) {
if (shift_state) {
console_previous_command_completion_suggestion();
} else {
console_next_command_completion_suggestion();
}
} else {
if (_global_console.input_line_count > 0) {
console_begin_command_completion();
} else {
console_stop_command_completion();
}
}
} break;
}
}
}
}
static void console_frame(struct graphics_context_api* graphics, struct input_api* input, float delta_time) {
if (input->key_pressed(INPUT_KEY_BACKQUOTE)) {
// only allow actions when the animation is finished.
// Or we are not animating.
if (_global_console.slide_timer == 0.0 || _global_console.animation_state == CONSOLE_ANIMATION_STATE_DEFAULT) {
if (_global_console.shown == false) {
_global_console.shown = true;
_global_console.animation_state = CONSOLE_ANIMATION_STATE_OPENING;
} else if (_global_console.shown == true) {
_global_console.shown = false;
_global_console.animation_state = CONSOLE_ANIMATION_STATE_CLOSING;
}
_global_console.slide_timer = 0;
}
}
_global_console.cursor_blink_timer += delta_time;
_global_console.slide_timer += delta_time * 2.15;
if (_global_console.input_line_write_cursor_location > _global_console.input_line_count) {
_global_console.input_line_write_cursor_location = _global_console.input_line_count;
}
if (_global_console.cursor_blink_timer >= CONSOLE_CURSOR_BLINK_TIMER_MAX) {
_global_console.cursor_blink_timer = 0;
_global_console.cursor_show ^= true;
}
if (_global_console.slide_timer >= 1.0) {
_global_console.slide_timer = 1.0;
_global_console.animation_state = CONSOLE_ANIMATION_STATE_DEFAULT;
}
// When we are no longer animating,
// and we are not trying to show ourselves, we don't do anything else.
if (!_global_console.shown && _global_console.animation_state == CONSOLE_ANIMATION_STATE_DEFAULT) {
return;
}
console_display(graphics);
#if 0
// not doing scrollback yet.
{
const float CONSOLE_SCROLL_SPEED = 300;
if (input->key_down(INPUT_KEY_UP)) {
console_scroll_by(-CONSOLE_SCROLL_SPEED * last_delta_time);
} else if (input->key_down(INPUT_KEY_DOWN)) {
console_scroll_by(CONSOLE_SCROLL_SPEED * last_delta_time);
}
}
#endif
}
static char* console_system_variable_type_as_string(uint8_t type) {
switch (type) {
case CONSOLE_VARIABLE_TYPE_NUMBER: return "number";
case CONSOLE_VARIABLE_TYPE_STRING: return "string";
case CONSOLE_VARIABLE_TYPE_BOOLEAN: return "boolean";
}
return "unknown";
}
static char* console_system_number_type_as_string(uint8_t type) {
switch (type) {
case CONSOLE_VARIABLE_TYPE_NUMBER_REAL: return "real";
case CONSOLE_VARIABLE_TYPE_NUMBER_INTEGER: return "integer";
}
return "unknown";
}
console_system_variable_iterator console_system_begin_iterating_variables(void) {
return (console_system_variable_iterator) {
.current = _global_console_system.variable_list.head
};
}
bool console_system_variable_iterator_finished(console_system_variable_iterator* iterator) {
if (iterator->current == &_global_console_system.variable_list.null) {
return true;
}
return false;
}
struct console_system_variable* console_system_variable_iterator_advance(console_system_variable_iterator* iterator) {
struct console_system_variable* current = iterator->current;
iterator->current = current->next;
return current;
}
// duped.
console_system_command_iterator console_system_begin_iterating_commands(void) {
return (console_system_command_iterator) {
.current = _global_console_system.command_list.head
};
}
bool console_system_command_iterator_finished(console_system_command_iterator* iterator) {
if (iterator->current == &_global_console_system.command_list.null) {
return true;
}
return false;
}
struct console_system_command* console_system_command_iterator_advance(console_system_command_iterator* iterator) {
struct console_system_command* current = iterator->current;
iterator->current = current->next;
return current;
}
size_t console_system_command_count(void) {
size_t length = 0;
for (console_system_command_iterator iterator = console_system_begin_iterating_commands();
!console_system_command_iterator_finished(&iterator);
console_system_command_iterator_advance(&iterator), length++) {
}
return length;
}
size_t console_system_variable_count(void) {
size_t length = 0;
for (console_system_variable_iterator iterator = console_system_begin_iterating_variables();
!console_system_variable_iterator_finished(&iterator);
console_system_variable_iterator_advance(&iterator), length++) {
}
return length;
}
void console_system_register_command(struct console_system_command* command) {
struct console_system_command* sentinel = &_global_console_system.command_list.null;
_global_console_system.command_count++;
if (_global_console_system.command_list.head == sentinel) {
_global_console_system.command_list.head = command;
} else {
struct console_system_command* old_tail = _global_console_system.command_list.tail;
old_tail->next = command;
}
_global_console_system.command_list.tail = command;
command->next = sentinel;
}
void console_system_register_variable(struct console_system_variable* variable) {
struct console_system_variable* sentinel = &_global_console_system.variable_list.null;
_global_console_system.variable_count++;
if (_global_console_system.variable_list.head == sentinel) {
_global_console_system.variable_list.head = variable;
} else {
struct console_system_variable* old_tail = _global_console_system.variable_list.tail;
old_tail->next = variable;
}
_global_console_system.variable_list.tail = variable;
variable->next = sentinel;
}
struct console_system_variable* console_system_find_variable(char* name) {
for (console_system_variable_iterator iterator = console_system_begin_iterating_variables();
!console_system_variable_iterator_finished(&iterator);
console_system_variable_iterator_advance(&iterator)) {
struct console_system_variable* current = iterator.current;
if (strcmp(current->name, name) == 0) {
return current;
}
}
return &_global_console_system.variable_list.null;
}
// NOTE(jerry): this isn't exposed because you shouldn't have to read functions...
// TODO(jerry): differs from find_variable which uses a C string. This is because we use the tokenizer which
// provides a length encoded string! I need to make up my mind on whether to use C strings or go length encoded!
struct console_system_command* console_system_find_command(char* name, size_t name_length) {
for (console_system_command_iterator iterator = console_system_begin_iterating_commands();
!console_system_command_iterator_finished(&iterator);
console_system_command_iterator_advance(&iterator)) {
struct console_system_command* current = iterator.current;
size_t length_of_name = strlen(current->name);
if (name_length == length_of_name) {
if (strncmp(current->name, name, name_length) == 0) {
return current;
}
}
}
return &_global_console_system.command_list.null;
}
static void _console_system_call_procedure_and_do_typechecking(struct console_system_command* command, uint8_t parameter_count, struct console_system_variant* parameters) {
bool allow_procedure_call = true;
if (allow_procedure_call) {
command->procedure(parameter_count, parameters);
}
}
// Some kind of arbitrary string scoring system.
// This has no normalized or determined range.
// However it does differentiate correctly between the matchness of a string.
// NOTE(jerry): Keep fine tuning this.
int32_t _console_determine_string_match_score_of(char* master_string, char* to_match_string) {
int32_t string_match_score = 0;
size_t master_string_length = cstring_length(master_string);
size_t to_match_string_length = cstring_length(to_match_string);
if (to_match_string_length == master_string_length) {
string_match_score += 25;
} else {
string_match_score -= 25;
}
size_t shorter_string_length = to_match_string_length;
if (master_string_length < to_match_string_length) {
shorter_string_length = master_string_length;
}
for (size_t first_character_index = 0; first_character_index < shorter_string_length; ++first_character_index) {
char character_to_match = master_string[first_character_index];
if (to_match_string[first_character_index] == character_to_match) {
string_match_score += 42;
} else {
string_match_score -= 35;
// NOTE(jerry):
// this isn't perfectly accurate.
// As this will not trigger on the correct instance of the letter.
// but it'll probably look good enough.
for (size_t second_character_index = 0; second_character_index < to_match_string_length; ++second_character_index) {
if (to_match_string[second_character_index] == character_to_match) {
string_match_score += 32;
} else {
string_match_score -= 1;
}
}
}
}
return string_match_score;
}
// Call this when pressing tab for tab completion.