/
Input.c
1048 lines (885 loc) · 35 KB
/
Input.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
#include "Input.h"
#include "Event.h"
#include "Funcs.h"
#include "Options.h"
#include "Utils.h"
#include "Logger.h"
#include "Platform.h"
#include "Chat.h"
#include "Utils.h"
#include "Server.h"
#include "HeldBlockRenderer.h"
#include "Game.h"
#include "Platform.h"
#include "ExtMath.h"
#include "Camera.h"
#include "Inventory.h"
#include "World.h"
#include "Event.h"
#include "Window.h"
#include "Entity.h"
#include "Chat.h"
#include "Funcs.h"
#include "Screens.h"
#include "Block.h"
#include "Menus.h"
#include "Gui.h"
#include "Protocol.h"
#include "AxisLinesRenderer.h"
static cc_bool input_buttonsDown[3];
static int input_pickingId = -1;
static const short normDists[10] = { 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096 };
static const short classicDists[4] = { 8, 32, 128, 512 };
static TimeMS input_lastClick;
static float input_fovIndex = -1.0f;
#ifdef CC_BUILD_WEB
static cc_bool suppressEscape;
#endif
enum MouseButton_ { MOUSE_LEFT, MOUSE_RIGHT, MOUSE_MIDDLE };
/*########################################################################################################################*
*------------------------------------------------------Touch support------------------------------------------------------*
*#########################################################################################################################*/
#ifdef CC_BUILD_TOUCH
static struct TouchPointer {
long id;
cc_uint8 type;
int begX, begY;
} touches[INPUT_MAX_POINTERS];
int Pointers_Count;
cc_bool Input_Placing;
/* Touch fingers are initially are all type, meaning they could */
/* trigger menu clicks, camera movement, or place/delete blocks */
/* But for example, after clicking on a menu button, you wouldn't */
/* want moving that finger anymore to move the camera */
#define TOUCH_TYPE_GUI 1
#define TOUCH_TYPE_CAMERA 2
#define TOUCH_TYPE_BLOCKS 4
#define TOUCH_TYPE_ALL (TOUCH_TYPE_GUI | TOUCH_TYPE_CAMERA | TOUCH_TYPE_BLOCKS)
static cc_bool AnyBlockTouches(void) {
int i;
for (i = 0; i < Pointers_Count; i++) {
if (touches[i].type & TOUCH_TYPE_BLOCKS) return true;
}
return false;
}
void Input_AddTouch(long id, int x, int y) {
int i = Pointers_Count;
touches[i].id = id;
touches[i].type = TOUCH_TYPE_ALL;
touches[i].begX = x;
touches[i].begY = y;
Pointers_Count++;
Pointer_SetPosition(i, x, y);
Pointer_SetPressed(i, true);
}
static cc_bool MovedFromBeg(int i, int x, int y) {
return Math_AbsI(x - touches[i].begX) > Display_ScaleX(5) ||
Math_AbsI(y - touches[i].begY) > Display_ScaleY(5);
}
void Input_UpdateTouch(long id, int x, int y) {
int i;
for (i = 0; i < Pointers_Count; i++) {
if (touches[i].id != id) continue;
if (Input_RawMode && (touches[i].type & TOUCH_TYPE_CAMERA)) {
/* If the pointer hasn't been locked to gui or block yet, moving a bit */
/* should cause the pointer to get locked to camera movement. */
if (touches[i].type == TOUCH_TYPE_ALL && MovedFromBeg(i, x, y)) {
/* Allow a little bit of leeway because though, because devices */
/* might still report a few pixels of movement depending on how */
/* user is holding the finger down on the toiuch surface */
touches[i].type = TOUCH_TYPE_CAMERA;
}
Event_RaiseMove(&PointerEvents.RawMoved, i, x - Pointers[i].x, y - Pointers[i].y);
}
Pointer_SetPosition(i, x, y);
return;
}
}
void Input_RemoveTouch(long id, int x, int y) {
int i;
for (i = 0; i < Pointers_Count; i++) {
if (touches[i].id != id) continue;
Pointer_SetPosition(i, x, y);
Pointer_SetPressed(i, false);
/* found the touch, remove it */
for (; i < Pointers_Count - 1; i++) {
touches[i] = touches[i + 1];
Pointers[i] = Pointers[i + 1];
}
Pointers_Count--;
return;
}
}
#endif
/*########################################################################################################################*
*-----------------------------------------------------------Key-----------------------------------------------------------*
*#########################################################################################################################*/
cc_bool Input_Pressed[INPUT_COUNT];
#define Key_Function_Names \
"F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10",\
"F11", "F12", "F13", "F14", "F15", "F16", "F17", "F18", "F19", "F20",\
"F21", "F22", "F23", "F24", "F25", "F26", "F27", "F28", "F29", "F30",\
"F31", "F32", "F33", "F34", "F35"
#define Key_Ascii_Names \
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J",\
"K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",\
"U", "V", "W", "X", "Y", "Z"
const char* const Input_Names[INPUT_COUNT] = {
"None",
Key_Function_Names,
"ShiftLeft", "ShiftRight", "ControlLeft", "ControlRight",
"AltLeft", "AltRight", "WinLeft", "WinRight",
"Up", "Down", "Left", "Right",
"Number0", "Number1", "Number2", "Number3", "Number4",
"Number5", "Number6", "Number7", "Number8", "Number9",
"Insert", "Delete", "Home", "End", "PageUp", "PageDown",
"Menu",
Key_Ascii_Names,
"Enter", "Escape", "Space", "BackSpace", "Tab", "CapsLock",
"ScrollLock", "PrintScreen", "Pause", "NumLock",
"Keypad0", "Keypad1", "Keypad2", "Keypad3", "Keypad4",
"Keypad5", "Keypad6", "Keypad7", "Keypad8", "Keypad9",
"KeypadDivide", "KeypadMultiply", "KeypadSubtract",
"KeypadAdd", "KeypadDecimal", "KeypadEnter",
"Tilde", "Minus", "Plus", "BracketLeft", "BracketRight", "Slash",
"Semicolon", "Quote", "Comma", "Period", "BackSlash",
"XButton1", "XButton2", "LeftMouse", "RightMouse", "MiddleMouse"
};
/* TODO: Should this only be shown in GUI? not saved to disc? */
/*const char* Input_Names[INPUT_COUNT] = {
"NONE",
"LSHIFT", "RSHIFT", "LCONTROL", "RCONTROL",
"LMENU", "RMENU", "LWIN", "RWIN", "MENU",
Key_Function_Names,
"UP", "DOWN", "LEFT", "RIGHT",
"RETURN", "ESCAPE", "SPACE", "TAB", "BACK", "INSERT",
"DELETE", "PRIOR", "DOWN", "HOME", "END", "CAPITAL",
"SCROLL", "PRINT", "PAUSE", "NUMLOCK",
"NUMPAD0", "NUMPAD1", "NUMPAD2", "NUMPAD3", "NUMPAD4",
"NUMPAD5", "NUMPAD6", "NUMPAD7", "NUMPAD8", "NUMPAD9",
"DIVIDE", "MULTIPLY", "SUBTRACT",
"ADD", "DECIMAL", "NUMPADENTER",
Key_Ascii_Names,
"0", "1", "2", "3", "4",
"5", "6", "7", "8", "9",
"GRAVE", "MINUS", "PLUS", "LBRACKET", "RBRACKET",
"SEMICOLON", "APOSTROPHE", "COMMA", "PERIOD", "SLASH", "BACKSLASH",
"XBUTTON1", "XBUTTON2", "MMOUSE"
};*/
void Input_SetPressed(int key, cc_bool pressed) {
cc_bool wasPressed = Input_Pressed[key];
Input_Pressed[key] = pressed;
if (pressed) {
Event_RaiseInput(&InputEvents.Down, key, wasPressed);
} else if (wasPressed) {
Event_RaiseInt(&InputEvents.Up, key);
}
/* don't allow multiple left mouse down events */
if (key != KEY_LMOUSE || pressed == wasPressed) return;
Pointer_SetPressed(0, pressed);
}
void Input_Clear(void) {
int i;
for (i = 0; i < INPUT_COUNT; i++) {
if (Input_Pressed[i]) Input_SetPressed(i, false);
}
}
/*########################################################################################################################*
*----------------------------------------------------------Mouse----------------------------------------------------------*
*#########################################################################################################################*/
int Mouse_X, Mouse_Y;
struct Pointer Pointers[INPUT_MAX_POINTERS];
cc_bool Input_RawMode, Input_TouchMode;
void Pointer_SetPressed(int idx, cc_bool pressed) {
#ifdef CC_BUILD_TOUCH
if (Input_TouchMode && !(touches[idx].type & TOUCH_TYPE_GUI)) return;
#endif
if (pressed) {
Event_RaiseInt(&PointerEvents.Down, idx);
} else {
Event_RaiseInt(&PointerEvents.Up, idx);
}
}
void Mouse_ScrollWheel(float delta) {
Event_RaiseFloat(&InputEvents.Wheel, delta);
}
void Pointer_SetPosition(int idx, int x, int y) {
int deltaX = x - Mouse_X, deltaY = y - Mouse_Y;
Mouse_X = x; Mouse_Y = y;
if (x == Pointers[idx].x && y == Pointers[idx].y) return;
/* TODO: reset to -1, -1 when pointer is removed */
Pointers[idx].x = x; Pointers[idx].y = y;
#ifdef CC_BUILD_TOUCH
if (Input_TouchMode && !(touches[idx].type & TOUCH_TYPE_GUI)) return;
#endif
Event_RaiseMove(&PointerEvents.Moved, idx, deltaX, deltaY);
}
/*########################################################################################################################*
*---------------------------------------------------------Keybinds--------------------------------------------------------*
*#########################################################################################################################*/
cc_uint8 KeyBinds[KEYBIND_COUNT];
const cc_uint8 KeyBind_Defaults[KEYBIND_COUNT] = {
'W', 'S', 'A', 'D',
KEY_SPACE, 'R', KEY_ENTER, 'T',
'B', 'F', KEY_ENTER, KEY_TAB,
KEY_LSHIFT, 'X', 'Z', 'Q', 'E',
KEY_LALT, KEY_F3, KEY_F12, KEY_F11,
KEY_F5, KEY_F1, KEY_F7, 'C',
KEY_LCTRL, KEY_LMOUSE, KEY_MMOUSE, KEY_RMOUSE,
KEY_F6, KEY_LALT, KEY_F8,
'G', KEY_F10, 0
};
static const char* const keybindNames[KEYBIND_COUNT] = {
"Forward", "Back", "Left", "Right",
"Jump", "Respawn", "SetSpawn", "Chat", "Inventory",
"ToggleFog", "SendChat", "PlayerList",
"Speed", "NoClip", "Fly", "FlyUp", "FlyDown",
"ExtInput", "HideFPS", "Screenshot", "Fullscreen",
"ThirdPerson", "HideGUI", "AxisLines", "ZoomScrolling",
"HalfSpeed", "DeleteBlock", "PickBlock", "PlaceBlock",
"AutoRotate", "HotbarSwitching", "SmoothCamera",
"DropBlock", "IDOverlay", "BreakableLiquids"
};
cc_bool KeyBind_IsPressed(KeyBind binding) { return Input_Pressed[KeyBinds[binding]]; }
static void KeyBind_Load(void) {
String name; char nameBuffer[STRING_SIZE + 1];
int mapping;
int i;
String_InitArray_NT(name, nameBuffer);
for (i = 0; i < KEYBIND_COUNT; i++) {
name.length = 0;
String_Format1(&name, "key-%c", keybindNames[i]);
name.buffer[name.length] = '\0';
mapping = Options_GetEnum(name.buffer, KeyBind_Defaults[i], Input_Names, INPUT_COUNT);
if (mapping != KEY_ESCAPE) KeyBinds[i] = mapping;
}
}
static void KeyBind_Save(void) {
String name; char nameBuffer[STRING_SIZE];
String value;
int i;
String_InitArray(name, nameBuffer);
for (i = 0; i < KEYBIND_COUNT; i++) {
name.length = 0;
String_Format1(&name, "key-%c", keybindNames[i]);
value = String_FromReadonly(Input_Names[KeyBinds[i]]);
Options_SetString(&name, &value);
}
}
void KeyBind_Set(KeyBind binding, int key) {
KeyBinds[binding] = key;
KeyBind_Save();
}
/* Initialises and loads key bindings from options */
static void KeyBind_Init(void) {
int i;
for (i = 0; i < KEYBIND_COUNT; i++) {
KeyBinds[i] = KeyBind_Defaults[i];
}
KeyBind_Load();
}
/*########################################################################################################################*
*---------------------------------------------------------Hotkeys---------------------------------------------------------*
*#########################################################################################################################*/
const cc_uint8 Hotkeys_LWJGL[256] = {
0, KEY_ESCAPE, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', KEY_MINUS, KEY_EQUALS, KEY_BACKSPACE, KEY_TAB,
'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', KEY_LBRACKET, KEY_RBRACKET, KEY_ENTER, KEY_LCTRL, 'A', 'S',
'D', 'F', 'G', 'H', 'J', 'K', 'L', KEY_SEMICOLON, KEY_QUOTE, KEY_TILDE, KEY_LSHIFT, KEY_BACKSLASH, 'Z', 'X', 'C', 'V',
'B', 'N', 'M', KEY_COMMA, KEY_PERIOD, KEY_SLASH, KEY_RSHIFT, 0, KEY_LALT, KEY_SPACE, KEY_CAPSLOCK, KEY_F1, KEY_F2, KEY_F3, KEY_F4, KEY_F5,
KEY_F6, KEY_F7, KEY_F8, KEY_F9, KEY_F10, KEY_NUMLOCK, KEY_SCROLLLOCK, KEY_KP7, KEY_KP8, KEY_KP9, KEY_KP_MINUS, KEY_KP4, KEY_KP5, KEY_KP6, KEY_KP_PLUS, KEY_KP1,
KEY_KP2, KEY_KP3, KEY_KP0, KEY_KP_DECIMAL, 0, 0, 0, KEY_F11, KEY_F12, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, KEY_F13, KEY_F14, KEY_F15, KEY_F16, KEY_F17, KEY_F18, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, KEY_KP_PLUS, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, KEY_KP_ENTER, KEY_RCTRL, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, KEY_KP_DIVIDE, 0, 0, KEY_RALT, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, KEY_PAUSE, 0, KEY_HOME, KEY_UP, KEY_PAGEUP, 0, KEY_LEFT, 0, KEY_RIGHT, 0, KEY_END,
KEY_DOWN, KEY_PAGEDOWN, KEY_INSERT, KEY_DELETE, 0, 0, 0, 0, 0, 0, 0, KEY_LWIN, KEY_RWIN, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
struct HotkeyData HotkeysList[HOTKEYS_MAX_COUNT];
StringsBuffer HotkeysText;
static void Hotkeys_QuickSort(int left, int right) {
struct HotkeyData* keys = HotkeysList; struct HotkeyData key;
while (left < right) {
int i = left, j = right;
cc_uint8 pivot = keys[(i + j) >> 1].Flags;
/* partition the list */
while (i <= j) {
while (pivot < keys[i].Flags) i++;
while (pivot > keys[j].Flags) j--;
QuickSort_Swap_Maybe();
}
/* recurse into the smaller subset */
QuickSort_Recurse(Hotkeys_QuickSort)
}
}
static void Hotkeys_AddNewHotkey(int trigger, cc_uint8 modifiers, const String* text, cc_bool more) {
struct HotkeyData hKey;
hKey.Trigger = trigger;
hKey.Flags = modifiers;
hKey.TextIndex = HotkeysText.count;
hKey.StaysOpen = more;
if (HotkeysText.count == HOTKEYS_MAX_COUNT) {
Chat_AddRaw("&cCannot define more than 256 hotkeys");
return;
}
HotkeysList[HotkeysText.count] = hKey;
StringsBuffer_Add(&HotkeysText, text);
/* sort so that hotkeys with largest modifiers are first */
Hotkeys_QuickSort(0, HotkeysText.count - 1);
}
static void Hotkeys_RemoveText(int index) {
struct HotkeyData* hKey = HotkeysList;
int i;
for (i = 0; i < HotkeysText.count; i++, hKey++) {
if (hKey->TextIndex >= index) hKey->TextIndex--;
}
StringsBuffer_Remove(&HotkeysText, index);
}
void Hotkeys_Add(int trigger, cc_uint8 modifiers, const String* text, cc_bool more) {
struct HotkeyData* hk = HotkeysList;
int i;
for (i = 0; i < HotkeysText.count; i++, hk++) {
if (hk->Trigger != trigger || hk->Flags != modifiers) continue;
Hotkeys_RemoveText(hk->TextIndex);
hk->StaysOpen = more;
hk->TextIndex = HotkeysText.count;
StringsBuffer_Add(&HotkeysText, text);
return;
}
Hotkeys_AddNewHotkey(trigger, modifiers, text, more);
}
cc_bool Hotkeys_Remove(int trigger, cc_uint8 modifiers) {
struct HotkeyData* hk = HotkeysList;
int i, j;
for (i = 0; i < HotkeysText.count; i++, hk++) {
if (hk->Trigger != trigger || hk->Flags != modifiers) continue;
Hotkeys_RemoveText(hk->TextIndex);
for (j = i; j < HotkeysText.count; j++) {
HotkeysList[j] = HotkeysList[j + 1];
}
return true;
}
return false;
}
int Hotkeys_FindPartial(int key) {
struct HotkeyData hk;
int i, modifiers = 0;
if (Key_IsControlPressed()) modifiers |= HOTKEY_MOD_CTRL;
if (Key_IsShiftPressed()) modifiers |= HOTKEY_MOD_SHIFT;
if (Key_IsAltPressed()) modifiers |= HOTKEY_MOD_ALT;
for (i = 0; i < HotkeysText.count; i++) {
hk = HotkeysList[i];
/* e.g. if holding Ctrl and Shift, a hotkey with only Ctrl modifiers matches */
if ((hk.Flags & modifiers) == hk.Flags && hk.Trigger == key) return i;
}
return -1;
}
/* Initialises and loads hotkeys from options. */
static void Hotkeys_Init(void) {
static const String prefix = String_FromConst("hotkey-");
String strKey, strMods, strMore, strText;
String entry, key, value;
int i;
int trigger;
cc_uint8 modifiers;
cc_bool more;
for (i = 0; i < Options.entries.count; i++) {
entry = StringsBuffer_UNSAFE_Get(&Options.entries, i);
String_UNSAFE_Separate(&entry, Options.separator, &key, &value);
if (!String_CaselessStarts(&key, &prefix)) continue;
/* Format is: key&modifiers = more-input&text */
key.length -= prefix.length; key.buffer += prefix.length;
if (!String_UNSAFE_Separate(&key, '&', &strKey, &strMods)) continue;
if (!String_UNSAFE_Separate(&value, '&', &strMore, &strText)) continue;
trigger = Utils_ParseEnum(&strKey, KEY_NONE, Input_Names, INPUT_COUNT);
if (trigger == KEY_NONE) continue;
if (!Convert_ParseUInt8(&strMods, &modifiers)) continue;
if (!Convert_ParseBool(&strMore, &more)) continue;
Hotkeys_Add(trigger, modifiers, &strText, more);
}
}
void Hotkeys_UserRemovedHotkey(int trigger, cc_uint8 modifiers) {
String key; char keyBuffer[STRING_SIZE];
String_InitArray(key, keyBuffer);
String_Format2(&key, "hotkey-%c&%b", Input_Names[trigger], &modifiers);
Options_SetString(&key, NULL);
}
void Hotkeys_UserAddedHotkey(int trigger, cc_uint8 modifiers, cc_bool moreInput, const String* text) {
String key; char keyBuffer[STRING_SIZE];
String value; char valueBuffer[STRING_SIZE * 2];
String_InitArray(key, keyBuffer);
String_InitArray(value, valueBuffer);
String_Format2(&key, "hotkey-%c&%b", Input_Names[trigger], &modifiers);
String_Format2(&value, "%t&%s", &moreInput, text);
Options_SetString(&key, &value);
}
/*########################################################################################################################*
*-----------------------------------------------------Mouse helpers-------------------------------------------------------*
*#########################################################################################################################*/
static void MouseStateUpdate(int button, cc_bool pressed) {
struct Entity* p;
/* defer getting the targeted entity, as it's a costly operation */
if (input_pickingId == -1) {
p = &LocalPlayer_Instance.Base;
input_pickingId = Entities_GetClosest(p);
}
input_buttonsDown[button] = pressed;
CPE_SendPlayerClick(button, pressed, (EntityID)input_pickingId, &Game_SelectedPos);
}
static void MouseStateChanged(int button, cc_bool pressed) {
if (!Server.SupportsPlayerClick) return;
if (pressed) {
/* Can send multiple Pressed events */
MouseStateUpdate(button, true);
} else {
if (!input_buttonsDown[button]) return;
MouseStateUpdate(button, false);
}
}
static void MouseStatePress(int button) {
input_lastClick = DateTime_CurrentUTC_MS();
input_pickingId = -1;
MouseStateChanged(button, true);
}
static void MouseStateRelease(int button) {
input_pickingId = -1;
MouseStateChanged(button, false);
}
void InputHandler_OnScreensChanged(void) {
input_lastClick = DateTime_CurrentUTC_MS();
input_pickingId = -1;
MouseStateChanged(MOUSE_LEFT, false);
MouseStateChanged(MOUSE_RIGHT, false);
MouseStateChanged(MOUSE_MIDDLE, false);
}
static cc_bool TouchesSolid(BlockID b) { return Blocks.Collide[b] == COLLIDE_SOLID; }
static cc_bool PushbackPlace(struct AABB* blockBB) {
struct Entity* p = &LocalPlayer_Instance.Base;
struct HacksComp* hacks = &LocalPlayer_Instance.Hacks;
Face closestFace;
cc_bool insideMap;
Vec3 pos = p->Position;
struct AABB playerBB;
struct LocationUpdate update;
/* Offset position by the closest face */
closestFace = Game_SelectedPos.Closest;
if (closestFace == FACE_XMAX) {
pos.X = blockBB->Max.X + 0.5f;
} else if (closestFace == FACE_ZMAX) {
pos.Z = blockBB->Max.Z + 0.5f;
} else if (closestFace == FACE_XMIN) {
pos.X = blockBB->Min.X - 0.5f;
} else if (closestFace == FACE_ZMIN) {
pos.Z = blockBB->Min.Z - 0.5f;
} else if (closestFace == FACE_YMAX) {
pos.Y = blockBB->Min.Y + 1 + ENTITY_ADJUSTMENT;
} else if (closestFace == FACE_YMIN) {
pos.Y = blockBB->Min.Y - p->Size.Y - ENTITY_ADJUSTMENT;
}
/* Exclude exact map boundaries, otherwise player can get stuck outside map */
/* Being vertically above the map is acceptable though */
insideMap =
pos.X > 0.0f && pos.Y >= 0.0f && pos.Z > 0.0f &&
pos.X < World.Width && pos.Z < World.Length;
if (!insideMap) return false;
AABB_Make(&playerBB, &pos, &p->Size);
if (!hacks->Noclip && Entity_TouchesAny(&playerBB, TouchesSolid)) {
/* Don't put player inside another block */
return false;
}
LocationUpdate_MakePos(&update, pos, false);
p->VTABLE->SetLocation(p, &update, false);
return true;
}
static cc_bool IntersectsOthers(Vec3 pos, BlockID block) {
struct AABB blockBB, entityBB;
struct Entity* entity;
int id;
Vec3_Add(&blockBB.Min, &pos, &Blocks.MinBB[block]);
Vec3_Add(&blockBB.Max, &pos, &Blocks.MaxBB[block]);
for (id = 0; id < ENTITIES_SELF_ID; id++) {
entity = Entities.List[id];
if (!entity) continue;
Entity_GetBounds(entity, &entityBB);
entityBB.Min.Y += 1.0f / 32.0f; /* when player is exactly standing on top of ground */
if (AABB_Intersects(&entityBB, &blockBB)) return true;
}
return false;
}
static cc_bool CheckIsFree(BlockID block) {
struct Entity* p = &LocalPlayer_Instance.Base;
struct HacksComp* hacks = &LocalPlayer_Instance.Hacks;
Vec3 pos, nextPos;
struct AABB blockBB, playerBB;
struct LocationUpdate update;
/* Non solid blocks (e.g. water/flowers) can always be placed on players */
if (Blocks.Collide[block] != COLLIDE_SOLID) return true;
IVec3_ToVec3(&pos, &Game_SelectedPos.TranslatedPos);
if (IntersectsOthers(pos, block)) return false;
nextPos = LocalPlayer_Instance.Interp.Next.Pos;
Vec3_Add(&blockBB.Min, &pos, &Blocks.MinBB[block]);
Vec3_Add(&blockBB.Max, &pos, &Blocks.MaxBB[block]);
/* NOTE: Need to also test against next position here, otherwise player can */
/* fall through the block at feet as collision is performed against nextPos */
Entity_GetBounds(p, &playerBB);
playerBB.Min.Y = min(nextPos.Y, playerBB.Min.Y);
if (hacks->Noclip || !AABB_Intersects(&playerBB, &blockBB)) return true;
if (hacks->CanPushbackBlocks && hacks->PushbackPlacing && hacks->Enabled) {
return PushbackPlace(&blockBB);
}
playerBB.Min.Y += 0.25f + ENTITY_ADJUSTMENT;
if (AABB_Intersects(&playerBB, &blockBB)) return false;
/* Push player upwards when they are jumping and trying to place a block underneath them */
nextPos.Y = pos.Y + Blocks.MaxBB[block].Y + ENTITY_ADJUSTMENT;
LocationUpdate_MakePos(&update, nextPos, false);
p->VTABLE->SetLocation(p, &update, false);
return true;
}
static void InputHandler_DeleteBlock(void) {
IVec3 pos;
BlockID old;
/* always play delete animations, even if we aren't deleting a block */
HeldBlockRenderer_ClickAnim(true);
pos = Game_SelectedPos.BlockPos;
if (!Game_SelectedPos.Valid || !World_Contains(pos.X, pos.Y, pos.Z)) return;
old = World_GetBlock(pos.X, pos.Y, pos.Z);
if (Blocks.Draw[old] == DRAW_GAS || !Blocks.CanDelete[old]) return;
Game_ChangeBlock(pos.X, pos.Y, pos.Z, BLOCK_AIR);
Event_RaiseBlock(&UserEvents.BlockChanged, pos, old, BLOCK_AIR);
}
static void InputHandler_PlaceBlock(void) {
IVec3 pos;
BlockID old, block;
pos = Game_SelectedPos.TranslatedPos;
if (!Game_SelectedPos.Valid || !World_Contains(pos.X, pos.Y, pos.Z)) return;
old = World_GetBlock(pos.X, pos.Y, pos.Z);
block = Inventory_SelectedBlock;
if (AutoRotate_Enabled) block = AutoRotate_RotateBlock(block);
if (Game_CanPick(old) || !Blocks.CanPlace[block]) return;
/* air-ish blocks can only replace over other air-ish blocks */
if (Blocks.Draw[block] == DRAW_GAS && Blocks.Draw[old] != DRAW_GAS) return;
if (!CheckIsFree(block)) return;
Game_ChangeBlock(pos.X, pos.Y, pos.Z, block);
Event_RaiseBlock(&UserEvents.BlockChanged, pos, old, block);
}
static void InputHandler_PickBlock(void) {
IVec3 pos;
BlockID cur;
pos = Game_SelectedPos.BlockPos;
if (!World_Contains(pos.X, pos.Y, pos.Z)) return;
cur = World_GetBlock(pos.X, pos.Y, pos.Z);
if (Blocks.Draw[cur] == DRAW_GAS) return;
if (!(Blocks.CanPlace[cur] || Blocks.CanDelete[cur])) return;
Inventory_PickBlock(cur);
}
void InputHandler_PickBlocks(void) {
cc_bool left, middle, right;
TimeMS now = DateTime_CurrentUTC_MS();
int delta = (int)(now - input_lastClick);
if (delta < 250) return; /* 4 times per second */
input_lastClick = now;
if (Gui_GetInputGrab()) return;
left = KeyBind_IsPressed(KEYBIND_DELETE_BLOCK);
middle = KeyBind_IsPressed(KEYBIND_PICK_BLOCK);
right = KeyBind_IsPressed(KEYBIND_PLACE_BLOCK);
#ifdef CC_BUILD_TOUCH
if (Input_TouchMode) {
left = !Input_Placing && AnyBlockTouches();
right = Input_Placing && AnyBlockTouches();
middle = false;
}
#endif
if (Server.SupportsPlayerClick) {
input_pickingId = -1;
MouseStateChanged(MOUSE_LEFT, left);
MouseStateChanged(MOUSE_RIGHT, right);
MouseStateChanged(MOUSE_MIDDLE, middle);
}
if (left) {
InputHandler_DeleteBlock();
} else if (right) {
InputHandler_PlaceBlock();
} else if (middle) {
InputHandler_PickBlock();
}
}
/*########################################################################################################################*
*------------------------------------------------------Key helpers--------------------------------------------------------*
*#########################################################################################################################*/
static cc_bool InputHandler_IsShutdown(int key) {
if (key == KEY_F4 && Key_IsAltPressed()) return true;
/* On OSX, Cmd+Q should also terminate the process */
#ifdef CC_BUILD_OSX
return key == 'Q' && Key_IsWinPressed();
#else
return false;
#endif
}
static void InputHandler_Toggle(int key, cc_bool* target, const char* enableMsg, const char* disableMsg) {
*target = !(*target);
if (*target) {
Chat_Add2("%c. &ePress &a%c &eto disable.", enableMsg, Input_Names[key]);
} else {
Chat_Add2("%c. &ePress &a%c &eto re-enable.", disableMsg, Input_Names[key]);
}
}
static void InputHandler_CycleDistanceForwards(const short* viewDists, int count) {
int i, dist;
for (i = 0; i < count; i++) {
dist = viewDists[i];
if (dist > Game_UserViewDistance) {
Game_UserSetViewDistance(dist); return;
}
}
Game_UserSetViewDistance(viewDists[0]);
}
static void InputHandler_CycleDistanceBackwards(const short* viewDists, int count) {
int i, dist;
for (i = count - 1; i >= 0; i--) {
dist = viewDists[i];
if (dist < Game_UserViewDistance) {
Game_UserSetViewDistance(dist); return;
}
}
Game_UserSetViewDistance(viewDists[count - 1]);
}
cc_bool InputHandler_SetFOV(int fov) {
struct HacksComp* h = &LocalPlayer_Instance.Hacks;
if (!h->Enabled || !h->CanUseThirdPersonCamera) return false;
Game_ZoomFov = fov;
Game_SetFov(fov);
return true;
}
static cc_bool InputHandler_DoFovZoom(float deltaPrecise) {
struct HacksComp* h;
if (!KeyBind_IsPressed(KEYBIND_ZOOM_SCROLL)) return false;
h = &LocalPlayer_Instance.Hacks;
if (!h->Enabled || !h->CanUseThirdPersonCamera) return false;
if (input_fovIndex == -1.0f) input_fovIndex = (float)Game_ZoomFov;
input_fovIndex -= deltaPrecise * 5.0f;
Math_Clamp(input_fovIndex, 1.0f, Game_DefaultFov);
return InputHandler_SetFOV((int)input_fovIndex);
}
static void InputHandler_CheckZoomFov(void* obj) {
struct HacksComp* h = &LocalPlayer_Instance.Hacks;
if (!h->Enabled || !h->CanUseThirdPersonCamera) Game_SetFov(Game_DefaultFov);
}
static cc_bool HandleBlockKey(int key) {
if (Gui_GetInputGrab()) return false;
if (key == KeyBinds[KEYBIND_DELETE_BLOCK]) {
MouseStatePress(MOUSE_LEFT);
InputHandler_DeleteBlock();
} else if (key == KeyBinds[KEYBIND_PLACE_BLOCK]) {
MouseStatePress(MOUSE_RIGHT);
InputHandler_PlaceBlock();
} else if (key == KeyBinds[KEYBIND_PICK_BLOCK]) {
MouseStatePress(MOUSE_MIDDLE);
InputHandler_PickBlock();
} else {
return false;
}
return true;
}
static cc_bool HandleNonClassicKey(int key) {
if (key == KeyBinds[KEYBIND_HIDE_GUI]) {
Game_HideGui = !Game_HideGui;
} else if (key == KeyBinds[KEYBIND_SMOOTH_CAMERA]) {
InputHandler_Toggle(key, &Camera.Smooth,
" &eSmooth camera is &aenabled",
" &eSmooth camera is &cdisabled");
} else if (key == KeyBinds[KEYBIND_AXIS_LINES]) {
InputHandler_Toggle(key, &AxisLinesRenderer_Enabled,
" &eAxis lines (&4X&e, &2Y&e, &1Z&e) now show",
" &eAxis lines no longer show");
} else if (key == KeyBinds[KEYBIND_AUTOROTATE]) {
InputHandler_Toggle(key, &AutoRotate_Enabled,
" &eAuto rotate is &aenabled",
" &eAuto rotate is &cdisabled");
} else if (key == KeyBinds[KEYBIND_THIRD_PERSON]) {
Camera_CycleActive();
} else if (key == KeyBinds[KEYBIND_DROP_BLOCK]) {
if (Inventory_CheckChangeSelected() && Inventory_SelectedBlock != BLOCK_AIR) {
/* Don't assign SelectedIndex directly, because we don't want held block
switching positions if they already have air in their inventory hotbar. */
Inventory_Set(Inventory.SelectedIndex, BLOCK_AIR);
Event_RaiseVoid(&UserEvents.HeldBlockChanged);
}
} else if (key == KeyBinds[KEYBIND_IDOVERLAY]) {
TexIdsOverlay_Show();
} else if (key == KeyBinds[KEYBIND_BREAK_LIQUIDS]) {
InputHandler_Toggle(key, &Game_BreakableLiquids,
" &eBreakable liquids is &aenabled",
" &eBreakable liquids is &cdisabled");
} else {
return false;
}
return true;
}
static cc_bool HandleCoreKey(int key) {
cc_result res;
if (key == KeyBinds[KEYBIND_HIDE_FPS]) {
Gui_ShowFPS = !Gui_ShowFPS;
} else if (key == KeyBinds[KEYBIND_FULLSCREEN]) {
int state = Window_GetWindowState();
if (state == WINDOW_STATE_FULLSCREEN) {
res = Window_ExitFullscreen();
if (res) Logger_Warn(res, "leaving fullscreen");
} else {
res = Window_EnterFullscreen();
if (res) Logger_Warn(res, "going fullscreen");
}
} else if (key == KeyBinds[KEYBIND_FOG]) {
const short* viewDists = Gui_ClassicMenu ? classicDists : normDists;
int count = Gui_ClassicMenu ? Array_Elems(classicDists) : Array_Elems(normDists);
if (Key_IsShiftPressed()) {
InputHandler_CycleDistanceBackwards(viewDists, count);
} else {
InputHandler_CycleDistanceForwards(viewDists, count);
}
} else if (key == KEY_F5 && Game_ClassicMode) {
int weather = Env.Weather == WEATHER_SUNNY ? WEATHER_RAINY : WEATHER_SUNNY;
Env_SetWeather(weather);
} else {
if (Game_ClassicMode) return false;
return HandleNonClassicKey(key);
}
return true;
}
static void HandleHotkeyDown(int key) {
struct HotkeyData* hkey;
String text;
int i = Hotkeys_FindPartial(key);
if (i == -1) return;
hkey = &HotkeysList[i];
text = StringsBuffer_UNSAFE_Get(&HotkeysText, hkey->TextIndex);
if (!hkey->StaysOpen) {
Chat_Send(&text, false);
} else if (!Gui_GetInputGrab()) {
ChatScreen_OpenInput(&text);
}
}
/*########################################################################################################################*
*-----------------------------------------------------Base handlers-------------------------------------------------------*
*#########################################################################################################################*/
static void HandleMouseWheel(void* obj, float delta) {
struct Screen* s;
int i;
struct Widget* widget;
cc_bool hotbar;
for (i = 0; i < Gui_ScreensCount; i++) {
s = Gui_Screens[i];
if (s->VTABLE->HandlesMouseScroll(s, delta)) return;
}
hotbar = Key_IsAltPressed() || Key_IsControlPressed() || Key_IsShiftPressed();
if (!hotbar && Camera.Active->Zoom(delta)) return;
if (InputHandler_DoFovZoom(delta) || !Inventory.CanChangeSelected) return;
widget = ChatScreen_GetHotbar();
Elem_HandlesMouseScroll(widget, delta);
}
static void HandlePointerMove(void* obj, int idx, int xDelta, int yDelta) {
struct Screen* s;
int i, x = Pointers[idx].x, y = Pointers[idx].y;
for (i = 0; i < Gui_ScreensCount; i++) {
s = Gui_Screens[i];
if (s->VTABLE->HandlesPointerMove(s, 1 << idx, x, y)) return;
}
}
static void HandlePointerDown(void* obj, int idx) {
struct Screen* s;
int i, x = Pointers[idx].x, y = Pointers[idx].y;
for (i = 0; i < Gui_ScreensCount; i++) {
s = Gui_Screens[i];
#ifdef CC_BUILD_TOUCH
if (s->VTABLE->HandlesPointerDown(s, 1 << idx, x, y)) {
touches[idx].type = TOUCH_TYPE_GUI; return;
}
#else
if (s->VTABLE->HandlesPointerDown(s, 1 << idx, x, y)) return;
#endif
}
}
static void HandlePointerUp(void* obj, int idx) {
struct Screen* s;
int i, x = Pointers[idx].x, y = Pointers[idx].y;
for (i = 0; i < Gui_ScreensCount; i++) {
s = Gui_Screens[i];
if (s->VTABLE->HandlesPointerUp(s, 1 << idx, x, y)) return;
}
}
static void HandleInputDown(void* obj, int key, cc_bool was) {
struct Screen* s;
int i;
#ifndef CC_BUILD_WEB
if (key == KEY_ESCAPE && (s = Gui_GetClosable())) {
/* Don't want holding down escape to go in and out of pause menu */
if (!was) Gui_Remove(s);
return;
}
#endif
if (InputHandler_IsShutdown(key)) {
/* TODO: Do we need a separate exit function in Game class? */
Window_Close(); return;
} else if (key == KeyBinds[KEYBIND_SCREENSHOT] && !was) {
Game_ScreenshotRequested = true; return;
}
for (i = 0; i < Gui_ScreensCount; i++) {
s = Gui_Screens[i];
if (s->VTABLE->HandlesKeyDown(s, key)) return;
}
if ((key == KEY_ESCAPE || key == KEY_PAUSE) && !Gui_GetInputGrab()) {
#ifdef CC_BUILD_WEB
/* Can't do this in KeyUp, because pressing escape without having */
/* explicitly disabled mouse lock means a KeyUp event isn't sent. */
/* But switching to pause screen disables mouse lock, causing a KeyUp */
/* event to be sent, triggering the active->closable case which immediately */
/* closes the pause screen. Hence why the next KeyUp must be supressed. */
suppressEscape = true;
#endif
PauseScreen_Show(); return;
}