-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConsole.h
984 lines (817 loc) · 33.7 KB
/
Console.h
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
#ifndef RR_CONSOLE
#define RR_CONSOLE
enum ConsoleSelectTag
{
SelectTag_Selecting = 0x1,
SelectTag_TextField = 0x2,
SelectTag_History = 0x4,
};
static v4 ColorForHistoryEntry(HistoryEntryEnum flag)
{
Tweekable(v4, consoleCommandTextColor);
Tweekable(v4, consoleErrorTextColor);
Tweekable(v4, consoleDefaultTextColor);
switch (flag)
{
case HistoryEntry_Command:
{
return consoleCommandTextColor;
}break;
case HistoryEntry_Error:
{
return consoleErrorTextColor;
}break;
default:
{
return consoleDefaultTextColor;
}break;
}
}
static ConsoleCommand CreateCommand(char* name, void (*interp)(StringArray args), u32 minArgs, u32 maxArgs)
{
ConsoleCommand ret;
ret.name = CreateString(name);
ret.interp = interp;
ret.minArgs = minArgs;
ret.maxArgs = maxArgs;
return ret;
}
static void InitConsole(Arena *constantArena)
{
console.buffer = PushData(constantArena, Char, console.bufferSize);
console.arena = InitArena(console.buffer, console.bufferSize * sizeof(Char));
console.history = PushData(constantArena, HistoryEntry, console.maxHistoryLength);
console.commandHistory = PushData(constantArena, String, console.maxCommandHistoryLength);
console.inputString = PushArray(constantArena, unsigned char, console.maxInputStringLength);
console.inputString.length = 0;
console.commands = PushArray(constantArena, ConsoleCommand, 0);
BuildStaticArray(constantArena, console.commands, CreateCommand("hello", HelloHelper, 0, 0));
BuildStaticArray(constantArena, console.commands, CreateCommand("add", AddHelper, 2, 2));
BuildStaticArray(constantArena, console.commands, CreateCommand("clear", ClearHelper, 0, 0));
BuildStaticArray(constantArena, console.commands, CreateCommand("lorum", LorumHelper, 0, 0));
BuildStaticArray(constantArena, console.commands, CreateCommand("help", HelpHelper, 0, 0));
BuildStaticArray(constantArena, console.commands, CreateCommand("tweek", TweekHelper, 0, 2));
BuildStaticArray(constantArena, console.commands, CreateCommand("save", SaveTweekersHelper, 1, 1));
BuildStaticArray(constantArena, console.commands, CreateCommand("tweekers", TweekersHelper, 0, 0));
BuildStaticArray(constantArena, console.commands, CreateCommand("grisu", GrisuHelper, 1, 1));
BuildStaticArray(constantArena, console.commands, CreateCommand("convert", ConvertHelper, 1, 1));
BuildStaticArray(constantArena, console.commands, CreateCommand("saveLevel", SaveLevelHelper, 1, 1));
BuildStaticArray(constantArena, console.commands, CreateCommand("loadLevel", LoadLevelHelper, 1, 1));
BuildStaticArray(constantArena, console.commands, CreateCommand("newLevel", NewLevelHelper, 0, 0));
BuildStaticArray(constantArena, console.commands, CreateCommand("addMesh", AddMeshHelper, 1, 1));
BuildStaticArray(constantArena, console.commands, CreateCommand("saveCamera", SaveCameraHelper, 0, 0));
BuildStaticArray(constantArena, console.commands, CreateCommand("setLight", SetLightHelper, 0, 0));
BuildStaticArray(constantArena, console.commands, CreateCommand("levels", LevelsHelper, 0, 0));
BuildStaticArray(constantArena, console.commands, CreateCommand("note", NoteHelper, 0, 0));
}
static bool ConsoleActive()
{
return (console.selecting);
}
static bool ConsoleReadyToType()
{
return (console.selecting & SelectTag_TextField);
}
static bool ConsoleOpen()
{
return (console.intendedOpenness > 0.0f);
}
static void AddSingleLineToHistory(String string, HistoryEntryEnum flag)
{
if (console.historyLength + 1 < console.maxHistoryLength)
{
String copy = CopyString(string, console.arena);
console.history[console.historyLength].entry = copy;
console.history[console.historyLength].flag = flag;
console.historyLength++;
}
else
{
Die;
}
if (!console.intendedOpenness) console.intendedOpenness = 4.0f * console.fontSize;
}
static void AddStringToHistory(String string, HistoryEntryEnum flag = HistoryEntry_Default)
{
String toProcess = string;
toProcess.length = string.length;
String gatheredString = string;
gatheredString.length = 0;
f32 currentLength = 0.0f;
while(toProcess.length)
{
String newWord = EatToNextSpaceReturnHead(&toProcess);
if (toProcess.length)
{
newWord.length++; //there was a space
toProcess.length--;
toProcess.data++;
}
f32 newLength = GetActualStringLength(newWord, console.fontSize, globalFont);
if (currentLength + newLength > (1.0f - console.scrollbarWidth) - 2.0f * console.fontSize)
{
AddSingleLineToHistory(gatheredString, flag);
currentLength = newLength;
gatheredString = newWord;
}
else
{
currentLength += newLength;
gatheredString.length += newWord.length;
}
}
AddSingleLineToHistory(gatheredString, flag);
}
static void ConsoleOutput(String toPrint)
{
AddStringToHistory(toPrint, HistoryEntry_Default);
}
static void ConsoleOutputError(String toPrint)
{
AddStringToHistory(toPrint, HistoryEntry_Error);
}
static void ConsoleOutput(char* format, ...)
{
va_list argList;
va_start(argList, format);
String toPrint = StringFormatHelper(frameArena, format, argList);
va_end(argList);
AddStringToHistory(toPrint, HistoryEntry_Default);
}
static void ConsoleOutputError(char *format, ...)
{
String toPrint = BeginConcatenate(frameArena);
va_list argList;
va_start(argList, format);
StringFormatHelper(frameArena, format, argList);
va_end(argList);
EndConcatenate(&toPrint, frameArena);
AddStringToHistory(toPrint, HistoryEntry_Error);
}
static u32 GetBestCursorLocationForString(String string, float xPos, f32 xOffset)
{
Font font = globalFont;
f32 stringSize = xOffset;
float fScale = console.fontSize / (f32)font.charHeight;
f32 bestDistance = Dist(xPos, stringSize);
for (u32 i = 0; i < string.length; i++)
{
Char currentChar = string[i];
if (currentChar < font.charData.amount)
{
CharData charData = font.charData[currentChar];
stringSize += charData.xAdvance * fScale;
f32 newDist = Dist(xPos, stringSize);
if (newDist > bestDistance)
{
return i;
}
bestDistance = newDist;
}
}
return string.length;
}
static u32 GetBestCursorLocationForConsoleHistory(Input input)
{
Font font = globalFont;
f32 lineSize = 1.5f * console.fontSize;
f32 amountOfDisplayedLinesf = console.openness / lineSize;
u32 amountOfDisplayedLines = (u32)amountOfDisplayedLinesf + 1;
i32 topLine = (i32)(console.historyLength - amountOfDisplayedLines - console.historyPos);
f32 yOffset = 0;
if (topLine < 0)
{
topLine = 0;
yOffset = console.openness - console.textInputFieldSize - (f32)(console.historyLength - console.historyPos - topLine) * lineSize;
}
if (yOffset > input.mouseZeroToOne.y) return 0;
i32 bottomLine = console.historyLength - 1 - console.historyPos;
f32 linef = MapRangeToRangeCapped(input.mouseZeroToOne.y, yOffset, console.openness - console.textInputFieldSize, (f32)topLine, (f32)bottomLine);
u32 line = (u32)(linef + 0.5f);
u32 bestInLine = GetBestCursorLocationForString(console.history[line].entry, input.mouseZeroToOne.x, console.historyXOffset);
return bestInLine + (u32)(console.history[line].entry.data - console.history[0].entry.data);
}
static void ConsoleResetSelection()
{
console.firstSelectPos = 0;
console.endSelectPos = 0;
console.selecting &= ~SelectTag_Selecting;
}
static b32 ConsoleHasActiveTextFieldSelection()
{
return (console.firstSelectPos != console.endSelectPos) && (console.selecting & SelectTag_TextField);
}
static b32 ConsoleHasActiveHistorySelection()
{
return (console.firstSelectPos != console.endSelectPos) && (console.selecting & SelectTag_History);
}
static void ConsoleHandleCommand(String inputLine)
{
if (console.commandHistoryLength + 1 < console.maxCommandHistoryLength)
{
console.commandHistory[console.commandHistoryLength++] = inputLine;
}
String remaining = inputLine;
EatSpaces(&remaining);
if (!remaining.length) { return; }
String command = EatToNextSpaceReturnHead(&remaining);
ConsoleCommand *selectedCommand = NULL;
For(console.commands)
{
if (it->name == command)
{
selectedCommand = it;
}
}
if (!selectedCommand)
{
ConsoleOutputError("Could not find command.");
return;
}
EatSpaces(&remaining);
BeginArray(frameArena, String, args);
while (remaining.length)
{
String arg = EatToNextSpaceReturnHead(&remaining);
Assert(arg.length);
*PushStruct(frameArena, String) = arg;
EatSpaces(&remaining);
}
EndArray(frameArena, String, args);
if (!(selectedCommand->minArgs <= args.amount && args.amount <= selectedCommand->maxArgs))
{
ConsoleOutputError("Wrong amount of Arguments, for command %s! Expected between %u32 and %u32 arguments.", selectedCommand->name, selectedCommand->minArgs, selectedCommand->maxArgs);
return;
}
selectedCommand->interp(args);
ConsoleOutput("Executed command %s!", selectedCommand->name);
}
static void RemoveSubstring(String *string, u32 first, u32 last)
{
if (first == last) return;
Assert(first < last);
Assert(last <= string->length);
String rest = *string;
rest.data += last;
rest.length -= last;
String toWrite = *string;
toWrite.data += first;
CopyStringToString(rest, &toWrite);
string->length -= (last - first);
}
static void ShiftSelectionUpdate(u32 intendedPos)
{
if (ConsoleHasActiveTextFieldSelection())
{
console.endSelectPos = intendedPos;
}
else
{
console.selecting = SelectTag_TextField;
console.firstSelectPos = console.cursorPos;
console.endSelectPos = intendedPos;
}
}
static void ConsoleHandleEvent(KeyStateMessage message, Input *input)
{
TimedBlock;
if (message.flag & (KeyState_PressedThisFrame | KeyState_Repeaded))
{
switch (message.key)
{
case Key_F1:
{
f32 newIntededOpenness = (message.flag & KeyState_ShiftDown) ? console.openWide : console.open;
if (console.intendedOpenness == newIntededOpenness)
{
console.intendedOpenness = 0.0f;
console.selecting = 0;
}
else
{
console.intendedOpenness = newIntededOpenness;
console.selecting = SelectTag_TextField;
}
}break;
case Key_leftMouse:
{
if (console.openness - console.textInputFieldSize <= input->mouseZeroToOne.y && input->mouseZeroToOne.y <= console.openness)
{
console.firstSelectPos = GetBestCursorLocationForString(console.inputString, console.typeFieldTextScrollOffset + input->mouseZeroToOne.x, console.typeFieldXOffset);
console.selecting = SelectTag_Selecting | SelectTag_TextField;
}
else if (input->mouseZeroToOne.y <= console.openness - console.textInputFieldSize && input->mouseZeroToOne.x <= 1.0f - console.scrollbarWidth)
{
console.firstSelectPos = GetBestCursorLocationForConsoleHistory(*input);
console.selecting = SelectTag_Selecting | SelectTag_History;
}
else if (input->mouseZeroToOne.y < console.openness - console.textInputFieldSize && 1.0f - console.scrollbarWidth < input->mouseZeroToOne.x)
{
console.scrollingScrollbar = true;
}
else
{
console.selecting = 0;
}
}break;
}
}
if (message.flag & (KeyState_ReleasedThisFrame))
{
switch (message.key)
{
case Key_leftMouse:
{
console.selecting &= ~SelectTag_Selecting;
console.scrollingScrollbar = false;
}break;
case Key_escape:
{
console.intendedOpenness = 0.0f;
}break;
}
}
if (!ConsoleActive())
{
return;
}
if (message.flag & (KeyState_PressedThisFrame | KeyState_Repeaded))
{
switch (message.key)
{
case Key_mouseWheelForward:
{
f32 lineSize = 1.5f * console.fontSize;
f32 amountOfDisplayedLinesf = console.openness / lineSize;
u32 amountOfDisplayedLines = (u32)amountOfDisplayedLinesf;
if (console.historyLength > amountOfDisplayedLines + console.historyPos)
{
console.historyPos++;
}
}break;
case Key_mouseWheelBack:
{
if (console.historyPos)
{
console.historyPos--;
}
}break;
case Key_c:
{
if (message.flag & KeyState_ControlDown)
{
u32 first = Min((u32)console.firstSelectPos, (u32)console.endSelectPos);
u32 end = Max((u32)console.firstSelectPos, (u32)console.endSelectPos);
if (console.selecting & SelectTag_History)
{
String stringToCopy;
stringToCopy.data = console.history[0].entry.data + first;
stringToCopy.length = end - first;
OSSetClipBoard(stringToCopy);
}
else if (console.selecting & SelectTag_TextField)
{
String stringToCopy;
stringToCopy.data = console.inputString.data + first;
stringToCopy.length = end - first;
OSSetClipBoard(stringToCopy);
}
return;
}
}break;
}
}
if (!ConsoleReadyToType())
{
return;
}
if (message.flag & (KeyState_PressedThisFrame | KeyState_Repeaded))
{
switch (message.key)
{
case Key_v:
{
if (message.flag & KeyState_ControlDown)
{
if (console.selecting & SelectTag_History)
{
return;
}
String toAdd = OSGetClipBoard();
if (console.inputString.length + toAdd.length > console.maxInputStringLength)
{
return;
}
u32 first = Min((u32)console.firstSelectPos, (u32)console.endSelectPos);
u32 end = Max((u32)console.firstSelectPos, (u32)console.endSelectPos);
RemoveSubstring(&console.inputString, first, end);
String head = CreateString(console.inputString.data, console.cursorPos);
String tailToCopy = CreateString(console.inputString.data + console.cursorPos, console.inputString.length);
String tail = CreateString(console.inputString.data + console.cursorPos + toAdd.length, 0);
CopyStringToString(tailToCopy, &tail);
CopyStringToString(toAdd, &tailToCopy);
console.inputString.length += toAdd.length;
console.cursorPos += toAdd.length - (end - first);
ConsoleResetSelection();
return;
}
}break;
case Key_backSpace:
{
if (ConsoleHasActiveTextFieldSelection())
{
u32 first = Min((u32)console.firstSelectPos, (u32)console.endSelectPos);
u32 end = Max((u32)console.firstSelectPos, (u32)console.endSelectPos);
RemoveSubstring(&console.inputString, first, end);
console.cursorPos = first;
console.recallHistoryPos = 0;
ConsoleResetSelection();
}
else if (console.cursorPos)
{
RemoveSubstring(&console.inputString, console.cursorPos - 1, console.cursorPos);
console.cursorPos--;
console.recallHistoryPos = 0;
}
}break;
case Key_enter:
{
Char *beginOfCommand = PushData(console.arena, Char, 0); // todo: stupid hack
AddStringToHistory(console.inputString, HistoryEntry_Command);
String command = CreateString(beginOfCommand, console.inputString.length);
ConsoleHandleCommand(command);
console.typeFieldTextScrollOffset = 0;
console.inputString.length = 0;
console.cursorPos = 0;
console.historyPos = 0;
console.recallHistoryPos = 0;
ConsoleResetSelection();
}break;
case Key_up:
{
if (console.recallHistoryPos < console.commandHistoryLength)
{
console.recallHistoryPos++;
String toCopy = console.commandHistory[console.commandHistoryLength - console.recallHistoryPos];
CopyStringToString(toCopy, &console.inputString);
console.cursorPos = console.inputString.length;
ConsoleResetSelection();
}
}break;
case Key_down:
{
if (console.recallHistoryPos > 1)
{
console.recallHistoryPos--;
String toCopy = console.commandHistory[console.commandHistoryLength - console.recallHistoryPos];
CopyStringToString(toCopy, &console.inputString);
console.cursorPos = console.inputString.length;
ConsoleResetSelection();
}
else if (console.recallHistoryPos == 1)
{
console.recallHistoryPos = 0;
console.inputString.length = 0;
console.cursorPos = 0;
}
}break;
case Key_left:
{
if ((message.flag & KeyState_ShiftDown))
{
if (console.cursorPos > 0)
{
ShiftSelectionUpdate(console.cursorPos - 1);
console.cursorPos--;
}
}
else if (ConsoleHasActiveTextFieldSelection())
{
console.cursorPos = Min(console.firstSelectPos, console.endSelectPos);
ConsoleResetSelection();
}
else if (console.cursorPos > 0)
{
console.cursorPos--;
}
}break;
case Key_right:
{
if ((message.flag & KeyState_ShiftDown) )
{
if (console.cursorPos < console.inputString.length)
{
ShiftSelectionUpdate(console.cursorPos + 1);
console.cursorPos++;
}
}
else if (ConsoleHasActiveTextFieldSelection())
{
console.cursorPos = Max(console.firstSelectPos, console.endSelectPos);
ConsoleResetSelection();
}
else if (console.cursorPos < console.inputString.length)
{
console.cursorPos++;
}
}break;
case Key_end:
{
if ((message.flag & KeyState_ShiftDown))
{
ShiftSelectionUpdate(console.inputString.length);
}
else
{
ConsoleResetSelection();
}
console.cursorPos = console.inputString.length;
}break;
case Key_pos1:
{
if ((message.flag & KeyState_ShiftDown))
{
ShiftSelectionUpdate(0);
}
else
{
ConsoleResetSelection();
}
console.cursorPos = 0;
}break;
default:
{
}break;
}
Char charPressed = KeyToChar(message.key, message.flag & KeyState_ShiftDown);
if (charPressed)
{
console.recallHistoryPos = 0;
if (ConsoleHasActiveTextFieldSelection())
{
u32 first = Min((u32)console.firstSelectPos, (u32)console.endSelectPos);
u32 end = Max((u32)console.firstSelectPos, (u32)console.endSelectPos);
if (first < (end - 1))
{
RemoveSubstring(&console.inputString, first, end - 1);
}
console.inputString[first] = charPressed;
console.cursorPos = first + 1;
ConsoleResetSelection();
}
else if (console.inputString.length + 1 < console.maxInputStringLength)
{
String rest = console.inputString;
rest.data += console.cursorPos;
rest.length -= console.cursorPos;
String copy = CopyString(rest);
console.inputString.data[console.cursorPos++] = charPressed;
console.inputString.length++;
for (u32 i = 0; i < copy.length; i++)
{
console.inputString.data[console.cursorPos + i] = copy.data[i];
}
}
}
console.cursorTimer = 0.0f;
}
}
static void UpdateConsole(Input input)
{
TimedBlock;
if (console.intendedOpenness > console.openness)
{
console.openness += console.dt * input.dt;
if(console.openness > console.intendedOpenness)
{
console.openness = console.intendedOpenness;
}
}
else
{
console.openness -= console.dt * input.dt;
if (console.openness < console.intendedOpenness)
{
console.openness = console.intendedOpenness;
}
}
String preCursorString = console.inputString;
preCursorString.length = console.cursorPos;
f32 stringLengthUpToCursor = GetActualStringLength(preCursorString, console.fontSize, globalFont);
f32 rightEdgePos = (1.0f - console.cursorScrollEdge);
if ((stringLengthUpToCursor - console.typeFieldTextScrollOffset) > rightEdgePos)
{
// scroll right when cursor is past rightEdgePos
console.typeFieldTextScrollOffset = stringLengthUpToCursor - rightEdgePos;
}
else if ((stringLengthUpToCursor > console.cursorScrollEdge) && (stringLengthUpToCursor - console.typeFieldTextScrollOffset) < console.cursorScrollEdge)
{
// if we can and should scoll left scroll left
console.typeFieldTextScrollOffset = stringLengthUpToCursor - console.cursorScrollEdge;
}
else if(stringLengthUpToCursor < (1.0f - console.cursorScrollEdge))
{
// if the string is small enough to fit, just reset
console.typeFieldTextScrollOffset = 0.0f;
}
console.cursorTimer += input.dt;
if (console.cursorTimer > 1.0f)
{
console.cursorTimer -= 1.0f;
}
if (console.selecting & SelectTag_Selecting)
{
if (console.selecting & SelectTag_TextField)
{
u32 bestLocation = GetBestCursorLocationForString(console.inputString, console.typeFieldTextScrollOffset + input.mouseZeroToOne.x, console.typeFieldXOffset);
console.endSelectPos = bestLocation;
console.cursorPos = bestLocation;
console.cursorTimer = 0.0f;
}
else if (console.selecting & SelectTag_History)
{
u32 bestLocation = GetBestCursorLocationForConsoleHistory(input);
console.endSelectPos = bestLocation;
console.cursorTimer = 0.0f;
//ConsoleOutputString(CreateString(workingArena, bestLocation));
}
else
{
ConsoleOutput("What are we selecting?");
}
}
else if (console.scrollingScrollbar)
{
f32 lineSize = 1.5f * console.fontSize;
f32 amountOfDisplayedLinesf = console.openness / lineSize;
u32 amountOfDisplayedLines = (u32)amountOfDisplayedLinesf + 1;
i32 topLine = (i32)(console.historyLength - amountOfDisplayedLines - console.historyPos);
topLine = Max(0, topLine);
i32 bottomLine = console.historyLength - 1 - console.historyPos;
f32 scrollbarBottom = console.openness - console.textInputFieldSize;
f32 scrollMiny = (f32)(topLine) / (f32)console.historyLength * scrollbarBottom;
f32 scrollMaxy = (f32)(bottomLine + 1) / (f32)console.historyLength * scrollbarBottom;
f32 scrollbarHalfLength = 0.5f * (scrollMaxy - scrollMiny);
f32 inpMid = input.mouseZeroToOne.y;
f32 bottomMinusHalfLength = scrollbarBottom - scrollbarHalfLength;
f32 intendedScollbarMid = Clamp(inpMid, scrollbarHalfLength, bottomMinusHalfLength);
f32 scrollbarIntendedBot = intendedScollbarMid + scrollbarHalfLength;
f32 intendedBottomLine = scrollbarIntendedBot / scrollbarBottom * (f32)console.historyLength - 1.0f;
u32 intendedHistoryPos = console.historyLength - 1 - (u32)intendedBottomLine;
console.historyPos = intendedHistoryPos;
}
}
static void ConsoleDrawSelection(RenderGroup *rg, String string, u32 first, u32 end, f32 yMin, f32 yMax, f32 xOffset)
{
String prefirst = string;
prefirst.length = first;
f32 stringLengthUpToSelection = GetActualStringLength(prefirst, console.fontSize, globalFont);
String selection = string;
selection.length = (end - first);
f32 selectionLength = GetActualStringLength(selection, console.fontSize, globalFont);
f32 selectionMinX = stringLengthUpToSelection - console.typeFieldTextScrollOffset + xOffset;
f32 selectionMaxX = selectionMinX + selectionLength;
Tweekable(v4, consoleSelectionColor); // V4(1.0f, 0.0f, 0.2f, 0.45f)
PushRectangle(rg, V2(selectionMinX, yMin), V2(selectionMaxX, yMax), console.selectionLayer, consoleSelectionColor);
}
// 0 to 1 screen space
static void DrawConsole(RenderGroup *rg)
{
if (console.openness == 0.0f) return;
v2 p1 = V2(0, 0);
v2 p2 = V2(1, 0);
v2 p3 = V2(0, console.openness);
v2 p4 = V2(1, console.openness);
//mainField
PushRectangle(rg, p1, V2(1 - console.scrollbarWidth, console.openness) - V2(0, console.textInputFieldSize), console.backLayer, V4(1.0f, 0.5f, 0.6f, 0.85f));
//typeField
Tweekable(v4, consoleTextFieldColor); //V4(1.0f, 0.6f, 0.7f, 0.85f)
v2 typeFieldPos = p3 - V2(0, console.textInputFieldSize);
PushRectangle(rg, typeFieldPos, p4, console.middleLayer, consoleTextFieldColor);
//cursor
if (ConsoleActive() && (console.cursorTimer < 0.5f) && !(console.selecting & SelectTag_History))
{
String preCursorString = console.inputString;
preCursorString.length = console.cursorPos;
f32 stringLengthUpToCursor = GetActualStringLength(preCursorString, console.fontSize, globalFont);
v2 cursorPos = V2(console.typeFieldXOffset + stringLengthUpToCursor - console.typeFieldTextScrollOffset, typeFieldPos.y);
PushRectangle(rg, cursorPos, console.cursorWidth, console.fontSize, console.selectionLayer, V4(1.0f, 1.0f, 1.0f, 0.85f));
}
//typefield Selection
if (ConsoleHasActiveTextFieldSelection())
{
u32 first = Min((u32)console.firstSelectPos, (u32)console.endSelectPos);
u32 end = Max((u32)console.firstSelectPos, (u32)console.endSelectPos);
ConsoleDrawSelection(rg, console.inputString, first, end, typeFieldPos.y, p4.y, console.typeFieldXOffset);
}
f32 lineSize = 1.5f * console.fontSize;
f32 amountOfDisplayedLinesf = console.openness / lineSize;
u32 amountOfDisplayedLines = (u32)amountOfDisplayedLinesf + 1;
if (console.historyLength < amountOfDisplayedLines)
{
amountOfDisplayedLines = console.historyLength;
}
v2 consoleFieldFirstRow = V2(console.historyXOffset, typeFieldPos.y);
i32 topLine = (i32)(console.historyLength - amountOfDisplayedLines - console.historyPos);
topLine = Max(0, topLine);
i32 bottomLine = console.historyLength - 1 - console.historyPos;
//History Selection
if (ConsoleHasActiveHistorySelection())
{
u32 first = Min((u32)console.firstSelectPos, (u32)console.endSelectPos);
u32 end = Max((u32)console.firstSelectPos, (u32)console.endSelectPos);
u32 firstLine = 0;
for (i32 i = topLine; i <= bottomLine; i++)
{
u32 linePos = (u32)(console.history[i].entry.data - console.history[0].entry.data);
if (linePos > first)
{
break;
}
firstLine = i;
}
u32 endLine = firstLine;
for (i32 i = firstLine; i <= bottomLine; i++)
{
u32 linePos = (u32)(console.history[i].entry.data - console.history[0].entry.data);
if (linePos > end)
{
break;
}
endLine = i;
}
if (endLine == firstLine)
{
u32 firstLinePosInBuffer = (u32)(console.history[firstLine].entry.data - console.history[0].entry.data);
u32 newFirst = SaveSubstract(first, firstLinePosInBuffer);
first = newFirst;
u32 newEnd = SaveSubstract(end, firstLinePosInBuffer);
end = newEnd;
f32 pos = (f32)(console.historyLength - console.historyPos - endLine);
ConsoleDrawSelection(rg, console.history[firstLine].entry, first, end, consoleFieldFirstRow.y - pos * lineSize, consoleFieldFirstRow.y - (pos - 1) * lineSize, console.historyXOffset);
}
else
{
u32 firstLinePosInBuffer = (u32)(console.history[firstLine].entry.data - console.history[0].entry.data);
u32 firstLineEndInBuffer = (u32)(console.history[firstLine + 1].entry.data - console.history[0].entry.data);
u32 endLinePosInBuffer = (u32)(console.history[endLine].entry.data - console.history[0].entry.data);
//u32 endLineEndInBuffer = (u32)(console.history[endLine + 1].entry.data - console.history[0].entry.data);
//draw head
{
u32 firstHead = SaveSubstract(first, firstLinePosInBuffer);
u32 endHead = firstLineEndInBuffer;
f32 pos = (f32)(console.historyLength - console.historyPos - firstLine);
ConsoleDrawSelection(rg, console.history[firstLine].entry, firstHead, endHead, consoleFieldFirstRow.y - pos * lineSize, consoleFieldFirstRow.y - (pos - 1) * lineSize, console.historyXOffset);
}
//fill inbetween
for (u32 i = firstLine + 1; i < endLine; i++)
{
u32 firstI = 0;
u32 endI = console.history[i].entry.length;
f32 pos = (f32)(console.historyLength - console.historyPos - i);
ConsoleDrawSelection(rg, console.history[i].entry, firstI, endI, consoleFieldFirstRow.y - pos * lineSize, consoleFieldFirstRow.y - (pos - 1) * lineSize, console.historyXOffset);
}
//draw tail
{
u32 firstEnd = 0;
u32 endEnd = SaveSubstract(end, endLinePosInBuffer);
f32 pos = (f32)(console.historyLength - console.historyPos - endLine);
ConsoleDrawSelection(rg, console.history[endLine].entry, firstEnd, endEnd, consoleFieldFirstRow.y - pos * lineSize, consoleFieldFirstRow.y - (pos - 1) * lineSize, console.historyXOffset);
}
}
}
//typefield Text
v2 typeFieldTextPos = p3 + V2(console.typeFieldXOffset - console.typeFieldTextScrollOffset, -console.textInputFieldSize);
PushString(rg, typeFieldTextPos - V2(0.001f, 0.001f), console.textLayer, console.inputString, console.fontSize, V4(1.0f, 0.5f, 0.5f, 0.5f));
PushString(rg, typeFieldTextPos, console.textLayer, console.inputString, console.fontSize, V4(1.0f, 1.0f, 1.0f, 1.0f));
//History Text
for (i32 i = bottomLine; i >= topLine; i--)
{
f32 pos = (f32)(console.historyLength - console.historyPos - i);
v4 color = ColorForHistoryEntry(console.history[i].flag);
PushString(rg, consoleFieldFirstRow - pos * V2(0, lineSize), console.textLayer, console.history[i].entry, console.fontSize, color);
}
//scrollbar
f32 scrollbarBottom = console.openness - console.textInputFieldSize;
v2 scrollbarFieldMin = V2(1.0f - console.scrollbarWidth, 0.0f);
v2 scrollbarFieldMax = V2(1.0f, scrollbarBottom);
Tweekable(v4, consoleScrollbarBackGroundColor);
PushRectangle(rg, scrollbarFieldMin, scrollbarFieldMax, console.middleLayer, consoleScrollbarBackGroundColor);
Tweekable(v4, consoleScrollbarColor); //V4(1.0f, 0.5f, 0.6f, 0.85f)
if(console.historyLength)
{
f32 scrollMiny = (f32)(topLine) / (f32)console.historyLength * scrollbarBottom;
f32 scrollMaxy = (f32)(bottomLine + 1) / (f32)console.historyLength * scrollbarBottom;
v2 scrollbarMin = V2(1.0f - console.scrollbarWidth + 0.003f, scrollMiny);
v2 scrollbarMax = V2(1.0f - 0.003f, scrollMaxy);
PushRectangle(rg, scrollbarMin, scrollbarMax, console.selectionLayer, consoleScrollbarColor);
}
}
//todo:
// control left right
// maybe handle to long strings?
// make all arrays dynamic?
// dont die on to many symbols i.e dynamic arrays or rolling buffer maybe 65536 long for that juicy speed?
// sanitise strings and handle \n, \r
// dont paste if the copy buffer is a file or smth
#endif // !RR_CONSOLE