-
Notifications
You must be signed in to change notification settings - Fork 0
/
events.cpp
401 lines (322 loc) · 11.5 KB
/
events.cpp
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
#include "events.h"
// Per-frame mouse deltas:
bool first_time_handling_mouse = true;
i32 mouse_delta_x;
i32 mouse_delta_y;
i32 last_mouse_x;
i32 last_mouse_y;
Wheel_Delta mouse_wheel_delta;
RArr<Event> events_this_frame;
SArr<Key_Current_State> input_button_states(CODE_TOTAL_COUNT);
#include "table.h"
Table<u32, bool> key_down_state_table;
void input_per_frame_event_and_flag_update()
{
// Called once per frame, probably
array_reset(&events_this_frame);
auto mask = ~KSTATE_START;
auto end_mask = ~(KSTATE_END | KSTATE_DOWN | KSTATE_START);
for (i64 i = 0; i < input_button_states.count; ++i)
{
auto it = &input_button_states.data[i];
if (*it & KSTATE_END)
{
*it = (Key_Current_State)(*it & end_mask);
}
else
{
*it = (Key_Current_State)(*it & mask);
}
}
mouse_delta_x = 0;
mouse_delta_y = 0;
mouse_wheel_delta = (Wheel_Delta){0, 0};
}
void update_linux_events()
{
input_per_frame_event_and_flag_update();
}
#define check_kc_and_ret(glfw_key, code_name) \
if (ikey == GLFW_KEY_##glfw_key) {return CODE_##code_name;}
// https://www.glfw.org/docs/3.3/group__keys.html
Key_Code get_key_code(i32 ikey)
{
check_kc_and_ret(BACKSPACE, BACKSPACE);
check_kc_and_ret(TAB, TAB);
check_kc_and_ret(ENTER, ENTER);
check_kc_and_ret(ESCAPE, ESCAPE);
check_kc_and_ret(SPACE, SPACEBAR);
check_kc_and_ret(0, 0);
check_kc_and_ret(1, 1);
check_kc_and_ret(2, 2);
check_kc_and_ret(3, 3);
check_kc_and_ret(4, 4);
check_kc_and_ret(5, 5);
check_kc_and_ret(6, 6);
check_kc_and_ret(7, 7);
check_kc_and_ret(8, 8);
check_kc_and_ret(9, 9);
check_kc_and_ret(A, A);
check_kc_and_ret(B, B);
check_kc_and_ret(C, C);
check_kc_and_ret(D, D);
check_kc_and_ret(E, E);
check_kc_and_ret(F, F);
check_kc_and_ret(G, G);
check_kc_and_ret(H, H);
check_kc_and_ret(I, I);
check_kc_and_ret(J, J);
check_kc_and_ret(K, K);
check_kc_and_ret(L, L);
check_kc_and_ret(M, M);
check_kc_and_ret(N, N);
check_kc_and_ret(O, O);
check_kc_and_ret(P, P);
check_kc_and_ret(Q, Q);
check_kc_and_ret(R, R);
check_kc_and_ret(S, S);
check_kc_and_ret(T, T);
check_kc_and_ret(U, U);
check_kc_and_ret(V, V);
check_kc_and_ret(W, W);
check_kc_and_ret(X, X);
check_kc_and_ret(Y, Y);
check_kc_and_ret(Z, Z);
check_kc_and_ret(DELETE, DELETE);
check_kc_and_ret(UP, ARROW_UP);
check_kc_and_ret(DOWN, ARROW_DOWN);
check_kc_and_ret(LEFT, ARROW_LEFT);
check_kc_and_ret(RIGHT, ARROW_RIGHT);
check_kc_and_ret(PAGE_UP, PAGE_UP);
check_kc_and_ret(PAGE_DOWN, PAGE_DOWN);
check_kc_and_ret(HOME, HOME);
check_kc_and_ret(END, END);
check_kc_and_ret(INSERT, INSERT);
check_kc_and_ret(PAUSE, PAUSE);
check_kc_and_ret(SCROLL_LOCK, SCROLL_LOCK);
check_kc_and_ret(LEFT_ALT, ALT);
check_kc_and_ret(RIGHT_ALT, ALT);
check_kc_and_ret(LEFT_CONTROL, CTRL);
check_kc_and_ret(RIGHT_CONTROL, CTRL);
check_kc_and_ret(LEFT_SHIFT, SHIFT);
check_kc_and_ret(RIGHT_SHIFT, SHIFT);
check_kc_and_ret(F1, F1);
check_kc_and_ret(F2, F2);
check_kc_and_ret(F3, F3);
check_kc_and_ret(F4, F4);
check_kc_and_ret(F5, F5);
check_kc_and_ret(F6, F6);
check_kc_and_ret(F7, F7);
check_kc_and_ret(F8, F8);
check_kc_and_ret(F9, F9);
check_kc_and_ret(F10, F10);
check_kc_and_ret(F11, F11);
check_kc_and_ret(F12, F12);
check_kc_and_ret(LEFT_BRACKET, LEFT_BRACKET);
check_kc_and_ret(RIGHT_BRACKET, RIGHT_BRACKET);
check_kc_and_ret(SEMICOLON, SEMICOLON);
check_kc_and_ret(APOSTROPHE, QUOTE);
// assert(0);
return CODE_UNKNOWN;
}
#undef check_kc_and_ret
void glfw_keyboard_callback(GLFWwindow *window, i32 ikey, i32 scancode, i32 action, i32 mods);
void glfw_mouse_position_callback(GLFWwindow *window, f64 x, f64 y);
void glfw_mouse_wheel_callback(GLFWwindow *window, f64 x_offset, f64 y_offset);
void glfw_mouse_button_callback(GLFWwindow *window, i32 button, i32 action, i32 mods);
void glfw_text_callback(GLFWwindow *window, u32 codepoint, i32 mods);
void hook_our_input_event_system_to_glfw(GLFWwindow *glfw_window)
{
// Set keyboard callback
// return the value of the previous callback function (used to chain callbacks)
glfwSetKeyCallback(glfw_window, glfw_keyboard_callback);
// Set the text input callback
glfwSetCharModsCallback(glfw_window, glfw_text_callback);
// Set mouse position callback
// return the value of the previous callback function (used to chain callbacks)
glfwSetCursorPosCallback(glfw_window, glfw_mouse_position_callback);
// Set mouse wheel callback
// return the value of the previous callback function (used to chain callbacks)
glfwSetScrollCallback(glfw_window, glfw_mouse_wheel_callback);
glfwSetMouseButtonCallback(glfw_window, glfw_mouse_button_callback);
}
void glfw_keyboard_callback(GLFWwindow *window, i32 ikey, i32 scancode, i32 action, i32 mods)
{
// key is the keyboard key that was PRESSED or RELEASED
// scancode is the platform-dependent scan code for the key
// action is one of GLFW_PRESS, GLFW_RELEASE, or GLFW_REPEAT
// mods contains flags describing which modifier keys (such as Shift/Ctrl) were pressed at the same time
u32 ukey = (u32)ikey;
auto is_down = (action == GLFW_PRESS) || (action == GLFW_REPEAT);
auto was_down = table_find_pointer(&key_down_state_table, ukey) != NULL;
auto key_code = get_key_code(ikey);
if (key_code == CODE_UNKNOWN)
{
// printf("Unknown keycode %d, returning...\n", ikey);
return;
}
if (is_down && !was_down)
{
input_button_states[key_code] = (Key_Current_State)(KSTATE_START | input_button_states[key_code]);
input_button_states[key_code] = (Key_Current_State)(KSTATE_DOWN | input_button_states[key_code]);
table_add(&key_down_state_table, ukey, true);
}
else if (was_down && !is_down)
{
input_button_states[key_code] = (Key_Current_State)((KSTATE_END) | input_button_states[key_code]);
table_remove(&key_down_state_table, ukey);
}
auto repeat = action == GLFW_REPEAT;
// printf("isdown(%d)\twasdown(%d)\trepeat(%d)\n", is_down, was_down, repeat);
// When release key, isdown == 0, wasdown == 1, repeat == 0
if (!is_down && !was_down)
{
// Redundant key_up event
return;
}
if (is_down && repeat && !was_down)
{
// @Check: is is relevant?
// key was pressed while we didn't have focus so the first
// event we see is incorrectly labeled as a repeat.
repeat = false;
}
// printf("Setting key event\n");
bool shift_state, ctrl_state, alt_state;
shift_state = ui_button_is_down(CODE_SHIFT);
ctrl_state = ui_button_is_down(CODE_CTRL);
alt_state = ui_button_is_down(CODE_ALT);
Event event;
event.type = EVENT_KEYBOARD;
event.key_code = key_code;
event.key_pressed = (u32)is_down;
event.shift_pressed = shift_state;
event.ctrl_pressed = ctrl_state;
event.alt_pressed = alt_state;
event.repeat = repeat;
array_add(&events_this_frame, event);
// printf("events count %ld\n", events_this_frame.count);
}
void glfw_mouse_position_callback(GLFWwindow *window, f64 x, f64 y)
{
// @Important: the x and y parameters contain the new position of the mouse cursor
// relative to the TOP LEFT of the window
//
// !!! Not the case of OpenGL because the origin there starts at the BOTTOM LEFT corner of the window !!!!
// BE WARY OF THIS WHEN HANDLING THE MOUSE ^
}
void glfw_mouse_wheel_callback(GLFWwindow *window, f64 x_offset, f64 y_offset)
{
// @Note: this can take offset in the x- and y- direction, so 2 dimensional scrolling is supported
mouse_wheel_delta.vertical = y_offset;
mouse_wheel_delta.horizontal = x_offset;
}
// @Cutnpaste from glfw_keyboard_callback @Cleanup
void glfw_mouse_button_callback(GLFWwindow *window, i32 button, i32 action, i32 mods)
{
auto ukey = (u32)button;
auto is_down = (action == GLFW_PRESS) || (action == GLFW_REPEAT);
auto was_down = table_find_pointer(&key_down_state_table, ukey) != NULL;
Key_Code code;
if (button == GLFW_MOUSE_BUTTON_RIGHT)
{
code = CODE_MOUSE_RIGHT;
}
else if (button == GLFW_MOUSE_BUTTON_LEFT)
{
code = CODE_MOUSE_LEFT;
}
else
{
printf("Unknown keycode %d, returning...\n", ukey);
return;
}
if (is_down && !was_down)
{
input_button_states[code] = (Key_Current_State)(KSTATE_START | input_button_states[code]);
input_button_states[code] = (Key_Current_State)(KSTATE_DOWN | input_button_states[code]);
table_add(&key_down_state_table, ukey, true);
}
else if (was_down && !is_down)
{
input_button_states[code] = (Key_Current_State)((KSTATE_END) | input_button_states[code]);
table_remove(&key_down_state_table, ukey);
}
auto repeat = action == GLFW_REPEAT;
if (!is_down && !was_down)
{
// Redundant key_up event
return;
}
if (is_down && repeat && !was_down)
{
// @Check: is is relevant?
// key was pressed while we didn't have focus so the first
// event we see is incorrectly labeled as a repeat.
repeat = false;
}
Event event;
event.type = EVENT_KEYBOARD;
event.key_code = code;
event.key_pressed = (u32)is_down;
event.repeat = repeat;
array_add(&events_this_frame, event);
}
// Although we are passing mods as a parameter, I think we should handle modifier keys
// within the keyboard callback, not here.
void glfw_text_callback(GLFWwindow *window, u32 codepoint, i32 mods)
{
// if (mods & GLFW_MOD_SHIFT) printf("holding shift!\n");
Event event;
event.type = EVENT_TEXT_INPUT;
event.utf32 = codepoint; // tolower(codepoint);
array_add(&events_this_frame, event);
}
void per_frame_update_mouse_position()
{
f64 x_pos, y_pos;
glfwGetCursorPos(glfw_window, &x_pos, &y_pos);
i32 ix = static_cast<i32>(x_pos);
i32 iy = static_cast<i32>(y_pos);
if (first_time_handling_mouse)
{
first_time_handling_mouse = false;
last_mouse_x = ix;
last_mouse_y = iy;
}
mouse_delta_x = ix - last_mouse_x;
mouse_delta_y = last_mouse_y - iy; // Reversed because GLFW mouse starts y from top
last_mouse_x = ix;
last_mouse_y = iy;
}
u64 glfw_cursor_state = GLFW_CURSOR_NORMAL;
inline
void set_glfw_cursor_mode(GLFWwindow *glfw_window, u64 mode)
{
if (mode == glfw_cursor_state) return; // So that we don't flicker.
glfw_cursor_state = mode;
glfwSetInputMode(glfw_window, GLFW_CURSOR, mode);
}
void hide_os_cursor(GLFWwindow *glfw_window)
{
set_glfw_cursor_mode(glfw_window, GLFW_CURSOR_HIDDEN);
}
void show_os_cursor(GLFWwindow *glfw_window)
{
set_glfw_cursor_mode(glfw_window, GLFW_CURSOR_NORMAL);
}
// Used for locking the the mouse cursor for camera controls
void disable_os_cursor(GLFWwindow *glfw_window)
{
set_glfw_cursor_mode(glfw_window, GLFW_CURSOR_DISABLED);
}
bool ui_button_was_pressed(Key_Code key_code)
{
if (key_code == CODE_UNKNOWN) return false;
return (input_button_states[key_code] & KSTATE_START);
}
bool ui_button_is_down(Key_Code key_code)
{
if (key_code == CODE_UNKNOWN) return false;
return (input_button_states[key_code] & KSTATE_DOWN);
}