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
|
#include "player-input.hpp"
#include "pk.h"
#include "window.hpp"
#include <GLFW/glfw3.h>
struct CursorEnterEvent {
bool entered;
};
struct CursorPosEvent {
double x;
double y;
};
struct KeyEvent {
int32_t button;
int32_t mods;
int8_t action;
};
struct MouseButtonEvent {
int32_t button;
int32_t mods;
int8_t action;
};
struct ScrollEvent {
double x;
double y;
};
struct WindowFocusEvent {
bool focused;
};
GLFWcursorenterfun prevCursorEnterCallback;
GLFWcursorposfun prevCursorPosCallback;
GLFWkeyfun prevKeyCallback;
GLFWmousebuttonfun prevMouseButtonCallback;
GLFWscrollfun prevScrollCallback;
GLFWwindowfocusfun prevWindowFocusCallback;
TypeSafeInt_B(InputActionSetHandle);
pk_arr_t<CursorPosEvent> unhandledCursorPosEvents{};
pk_arr_t<CursorEnterEvent> unhandledCursorEnterEvents{};
pk_arr_t<KeyEvent> unhandledKeyEvents{};
pk_arr_t<MouseButtonEvent> unhandledMouseButtonEvents{};
pk_arr_t<ScrollEvent> unhandledScrollEvents{};
pk_arr_t<WindowFocusEvent> unhandledWindowFocusEvents{};
pk_arr_t<PkeCursorEnterEvent> registeredCursorEnterEvents{};
bool lastCursorEntered = false;
pk_arr_t<PkeCursorPosEvent> registeredCursorPosEvents{};
struct {
double x = 0;
double y = 0;
} lastMousePos;
pk_arr_t<PkeKeyEvent> registeredKeyEvents{};
pk_arr_t<PkeMouseButtonEvent> registeredMouseButtonEvents{};
pk_arr_t<PkeScrollEvent> registeredScrollEvents{};
pk_arr_t<PkeWindowFocusEvent> registeredWindowFocusEvents{};
bool lastWindowFocus = false;
pk_arr_t<PkeInputSet> registeredInputSets{};
pk_arr_t<InputActionSetHandle> activeInputSetStack{};
PkeInputAction *FindActionByName(const char *actionName) {
uint32_t count = activeInputSetStack.next;
for (uint32_t i = count; i > 0; --i) {
InputActionSetHandle handle = activeInputSetStack[i-1];
InputActionSetHandle_T index = static_cast<InputActionSetHandle_T>(handle);
PkeInputSet &set = registeredInputSets[index];
for (int64_t k = 0; k < set.actionCount; ++k) {
PkeInputAction &inputAction = set.actions[k];
if (strcmp(actionName, inputAction.name) != 0) {
continue;
};
return &inputAction;
}
}
return nullptr;
}
template<InputEventHash T, typename S> InputEventHash FindActivePkeInputAction_ByAction(const PkeInputAction *inputAction, S *&activeEvent, int32_t index) {
activeEvent = nullptr;
if constexpr((T & PKE_INPUT_HASH_EVENT_TYPE_CURSOR_ENTER) != InputEventHash{0}) {
activeEvent = ®isteredCursorEnterEvents[inputAction->event_indices[index]];
return PKE_INPUT_HASH_EVENT_TYPE_CURSOR_ENTER;
}
if constexpr((T & PKE_INPUT_HASH_EVENT_TYPE_CURSOR_POS) != InputEventHash{0}) {
activeEvent = ®isteredCursorPosEvents[inputAction->event_indices[index]];
return PKE_INPUT_HASH_EVENT_TYPE_CURSOR_POS;
}
if constexpr((T & PKE_INPUT_HASH_EVENT_TYPE_KEY) != InputEventHash{0}) {
activeEvent = ®isteredKeyEvents[inputAction->event_indices[index]];
return PKE_INPUT_HASH_EVENT_TYPE_KEY;
}
if constexpr((T & PKE_INPUT_HASH_EVENT_TYPE_MOUSE_BUTTON) != InputEventHash{0}) {
activeEvent = ®isteredMouseButtonEvents[inputAction->event_indices[index]];
return PKE_INPUT_HASH_EVENT_TYPE_MOUSE_BUTTON;
}
if constexpr((T & PKE_INPUT_HASH_EVENT_TYPE_SCROLL) != InputEventHash{0}) {
activeEvent = ®isteredScrollEvents[inputAction->event_indices[index]];
return PKE_INPUT_HASH_EVENT_TYPE_SCROLL;
}
if constexpr((T & PKE_INPUT_HASH_EVENT_TYPE_WINDOW_FOCUS) != InputEventHash{0}) {
activeEvent = ®isteredWindowFocusEvents[inputAction->event_indices[index]];
return PKE_INPUT_HASH_EVENT_TYPE_WINDOW_FOCUS;
}
return InputEventHash{0};
}
/*
template<InputEventHash T, typename S> InputEventHash FindActivePkeInputAction_ByName(const char *actionName, S *&activeEvent) {
activeEvent = nullptr;
bool any = false;
if constexpr((T & PKE_INPUT_HASH_EVENT_TYPE_CURSOR_ENTER) != InputEventHash{0}) {
any = any || bool(registeredCursorEnterEvents.next);
}
if constexpr((T & PKE_INPUT_HASH_EVENT_TYPE_CURSOR_POS) != InputEventHash{0}) {
any = any || bool(registeredCursorPosEvents.next);
}
if constexpr((T & PKE_INPUT_HASH_EVENT_TYPE_KEY) != InputEventHash{0}) {
any = any || bool(registeredKeyEvents.next);
}
if constexpr((T & PKE_INPUT_HASH_EVENT_TYPE_MOUSE_BUTTON) != InputEventHash{0}) {
any = any || bool(registeredMouseButtonEvents.next);
}
if constexpr((T & PKE_INPUT_HASH_EVENT_TYPE_SCROLL) != InputEventHash{0}) {
any = any || bool(registeredScrollEvents.next);
}
if constexpr((T & PKE_INPUT_HASH_EVENT_TYPE_WINDOW_FOCUS) != InputEventHash{0}) {
any = any || bool(registeredWindowFocusEvents.next);
}
if (any == false) return InputEventHash{0};
uint32_t count = activeInputSetStack.next;
for (uint32_t i = count; i > 0; --i) {
InputActionSetHandle handle = activeInputSetStack[i-1];
InputActionSetHandle_T index = static_cast<InputActionSetHandle_T>(handle);
PkeInputSet &set = registeredInputSets[index];
for (int64_t k = 0; k < set.actionCount; ++k) {
PkeInputAction &inputAction = set.actions[k];
if (strcmp(actionName, inputAction.name) != 0) {
continue;
};
if constexpr((T & PKE_INPUT_HASH_EVENT_TYPE_CURSOR_ENTER) != InputEventHash{0}) {
activeEvent = ®isteredCursorEnterEvents[inputAction.eventIndex];
return PKE_INPUT_HASH_EVENT_TYPE_CURSOR_ENTER;
}
if constexpr((T & PKE_INPUT_HASH_EVENT_TYPE_CURSOR_POS) != InputEventHash{0}) {
activeEvent = ®isteredCursorPosEvents[inputAction.eventIndex];
return PKE_INPUT_HASH_EVENT_TYPE_CURSOR_POS;
}
if constexpr((T & PKE_INPUT_HASH_EVENT_TYPE_KEY) != InputEventHash{0}) {
activeEvent = ®isteredKeyEvents[inputAction.eventIndex];
return PKE_INPUT_HASH_EVENT_TYPE_KEY;
}
if constexpr((T & PKE_INPUT_HASH_EVENT_TYPE_MOUSE_BUTTON) != InputEventHash{0}) {
activeEvent = ®isteredMouseButtonEvents[inputAction.eventIndex];
return PKE_INPUT_HASH_EVENT_TYPE_MOUSE_BUTTON;
}
if constexpr((T & PKE_INPUT_HASH_EVENT_TYPE_SCROLL) != InputEventHash{0}) {
activeEvent = ®isteredScrollEvents[inputAction.eventIndex];
return PKE_INPUT_HASH_EVENT_TYPE_SCROLL;
}
if constexpr((T & PKE_INPUT_HASH_EVENT_TYPE_WINDOW_FOCUS) != InputEventHash{0}) {
activeEvent = ®isteredWindowFocusEvents[inputAction.eventIndex];
return PKE_INPUT_HASH_EVENT_TYPE_WINDOW_FOCUS;
}
}
}
return InputEventHash{0};
}
*/
template<InputEventHash T, typename S> InputEventHash FindActivePkeInputAction_ByType(const PkeInputEventMask &mask, S *&activeEvent) {
uint32_t count, i;
activeEvent = nullptr;
bool any = false;
if constexpr((T & PKE_INPUT_HASH_EVENT_TYPE_CURSOR_ENTER) != InputEventHash{0}) {
any = any || bool(registeredCursorEnterEvents.next);
}
if constexpr((T & PKE_INPUT_HASH_EVENT_TYPE_CURSOR_POS) != InputEventHash{0}) {
any = any || bool(registeredCursorPosEvents.next);
}
if constexpr((T & PKE_INPUT_HASH_EVENT_TYPE_KEY) != InputEventHash{0}) {
any = any || bool(registeredKeyEvents.next);
}
if constexpr((T & PKE_INPUT_HASH_EVENT_TYPE_MOUSE_BUTTON) != InputEventHash{0}) {
any = any || bool(registeredMouseButtonEvents.next);
}
if constexpr((T & PKE_INPUT_HASH_EVENT_TYPE_SCROLL) != InputEventHash{0}) {
any = any || bool(registeredScrollEvents.next);
}
if constexpr((T & PKE_INPUT_HASH_EVENT_TYPE_WINDOW_FOCUS) != InputEventHash{0}) {
any = any || bool(registeredWindowFocusEvents.next);
}
if (any == false) return InputEventHash{0};
count = activeInputSetStack.next;
for (i = count; i > 0; --i) {
InputActionSetHandle handle = activeInputSetStack[i-1];
InputActionSetHandle_T index = static_cast<InputActionSetHandle_T>(handle);
PkeInputSet &set = registeredInputSets[index];
for (int64_t k = 0; k < set.actionCount; ++k) {
PkeInputAction &inputAction = set.actions[k];
for (int64_t l = 0; l < PKE_INPUT_ACTION_MASK_INDEX_COUNT; ++l) {
PkeInputEventMask &evMask = inputAction.masks[l];
if ((evMask.computedHash & mask.computedHash) == InputEventHash{0}) {
continue;
}
if constexpr((T & PKE_INPUT_HASH_EVENT_TYPE_KEY) != InputEventHash{0}) {
if (evMask.button != mask.button) {
continue;
}
if (evMask.mods != mask.mods) {
continue;
}
}
if constexpr((T & PKE_INPUT_HASH_EVENT_TYPE_MOUSE_BUTTON) != InputEventHash{0}) {
if (evMask.button != mask.button) {
continue;
}
if (evMask.mods != mask.mods) {
continue;
}
}
if constexpr((T & PKE_INPUT_HASH_EVENT_TYPE_CURSOR_ENTER) != InputEventHash{0}) {
activeEvent = ®isteredCursorEnterEvents[inputAction.event_indices[l]];
return PKE_INPUT_HASH_EVENT_TYPE_CURSOR_ENTER;
}
if constexpr((T & PKE_INPUT_HASH_EVENT_TYPE_CURSOR_POS) != InputEventHash{0}) {
activeEvent = ®isteredCursorPosEvents[inputAction.event_indices[l]];
return PKE_INPUT_HASH_EVENT_TYPE_CURSOR_POS;
}
if constexpr((T & PKE_INPUT_HASH_EVENT_TYPE_KEY) != InputEventHash{0}) {
activeEvent = ®isteredKeyEvents[inputAction.event_indices[l]];
return PKE_INPUT_HASH_EVENT_TYPE_KEY;
}
if constexpr((T & PKE_INPUT_HASH_EVENT_TYPE_MOUSE_BUTTON) != InputEventHash{0}) {
activeEvent = ®isteredMouseButtonEvents[inputAction.event_indices[l]];
return PKE_INPUT_HASH_EVENT_TYPE_MOUSE_BUTTON;
}
if constexpr((T & PKE_INPUT_HASH_EVENT_TYPE_SCROLL) != InputEventHash{0}) {
activeEvent = ®isteredScrollEvents[inputAction.event_indices[l]];
return PKE_INPUT_HASH_EVENT_TYPE_SCROLL;
}
if constexpr((T & PKE_INPUT_HASH_EVENT_TYPE_WINDOW_FOCUS) != InputEventHash{0}) {
activeEvent = ®isteredWindowFocusEvents[inputAction.event_indices[l]];
return PKE_INPUT_HASH_EVENT_TYPE_WINDOW_FOCUS;
}
}
}
}
return InputEventHash{0};
}
constexpr auto FindActivePkeInputAction_CursorEnter_ByType = FindActivePkeInputAction_ByType<PKE_INPUT_HASH_EVENT_TYPE_CURSOR_ENTER, PkeCursorEnterEvent>;
constexpr auto FindActivePkeInputAction_CursorPos_ByType = FindActivePkeInputAction_ByType<PKE_INPUT_HASH_EVENT_TYPE_CURSOR_POS, PkeCursorPosEvent>;
constexpr auto FindActivePkeInputAction_Key_ByType = FindActivePkeInputAction_ByType<PKE_INPUT_HASH_EVENT_TYPE_KEY, PkeKeyEvent>;
constexpr auto FindActivePkeInputAction_MouseButton_ByType = FindActivePkeInputAction_ByType<PKE_INPUT_HASH_EVENT_TYPE_MOUSE_BUTTON, PkeMouseButtonEvent>;
constexpr auto FindActivePkeInputAction_Scroll_ByType = FindActivePkeInputAction_ByType<PKE_INPUT_HASH_EVENT_TYPE_SCROLL, PkeScrollEvent>;
constexpr auto FindActivePkeInputAction_WindowFocus_ByType = FindActivePkeInputAction_ByType<PKE_INPUT_HASH_EVENT_TYPE_WINDOW_FOCUS, PkeWindowFocusEvent>;
// constexpr auto FindActivePkeInputAction_CursorEnter_ByName = FindActivePkeInputAction_ByName<PKE_INPUT_HASH_EVENT_TYPE_CURSOR_ENTER, PkeCursorEnterEvent>;
// constexpr auto FindActivePkeInputAction_CursorPos_ByName = FindActivePkeInputAction_ByName<PKE_INPUT_HASH_EVENT_TYPE_CURSOR_POS, PkeCursorPosEvent>;
// constexpr auto FindActivePkeInputAction_Key_ByName = FindActivePkeInputAction_ByName<PKE_INPUT_HASH_EVENT_TYPE_KEY, PkeKeyEvent>;
// constexpr auto FindActivePkeInputAction_MouseButton_ByName = FindActivePkeInputAction_ByName<PKE_INPUT_HASH_EVENT_TYPE_MOUSE_BUTTON, PkeMouseButtonEvent>;
// constexpr auto FindActivePkeInputAction_Scroll_ByName = FindActivePkeInputAction_ByName<PKE_INPUT_HASH_EVENT_TYPE_SCROLL, PkeScrollEvent>;
// constexpr auto FindActivePkeInputAction_WindowFocus_ByName = FindActivePkeInputAction_ByName<PKE_INPUT_HASH_EVENT_TYPE_WINDOW_FOCUS, PkeWindowFocusEvent>;
void PkeInput_Tick(double delta) {
(void)delta;
uint32_t count, i;
// reset everything
// @performance could happen concurrently
{
count = registeredCursorPosEvents.next;
for (i = 0; i < count; ++i) {
auto &ev = registeredCursorPosEvents[i];
ev.xMotion = 0;
ev.yMotion = 0;
}
count = registeredKeyEvents.next;
for (i = 0; i < count; ++i) {
auto &ev = registeredKeyEvents[i];
// TODO the idea here was right, but wrong place and too wide a swath
// if (i < count - 1) ev.isPressed = false;
ev.thisTick = false;
}
count = registeredMouseButtonEvents.next;
for (i = 0; i < count; ++i) {
auto &ev = registeredMouseButtonEvents[i];
// TODO the idea here was right, but wrong place and too wide a swath
// if (i < count - 1) ev.isPressed = false;
ev.thisTick = false;
}
count = registeredScrollEvents.next;
for (i = 0; i < count; ++i) {
auto &ev = registeredScrollEvents[i];
ev.xMotion = 0;
ev.yMotion = 0;
}
}
// handle unhandled events
// @performance cache action->event results
{
do { // while (0)
PkeCursorEnterEvent *primaryEvent = nullptr;
FindActivePkeInputAction_CursorEnter_ByType(PkeInputEventMask {
.computedHash = PKE_INPUT_HASH_ALL_CURSOR_ENTER_EVENTS
}, primaryEvent);
if (primaryEvent == nullptr) {
break;
}
const auto &ev = unhandledCursorEnterEvents[unhandledCursorEnterEvents.next-1];
primaryEvent->isEntered = ev.entered;
lastCursorEntered = ev.entered;
} while (0);
do { // while (0)
PkeCursorPosEvent *primaryEvent = nullptr;
FindActivePkeInputAction_CursorPos_ByType(PkeInputEventMask {
.computedHash = PKE_INPUT_HASH_ALL_CURSOR_POS_EVENTS
}, primaryEvent);
if (primaryEvent == nullptr) {
break;
}
count = unhandledCursorPosEvents.next;
for (i = 0; i < count; ++i) {
const auto &ev = unhandledCursorPosEvents[i];
primaryEvent->xMotion += ev.x - lastMousePos.x;
primaryEvent->yMotion += ev.y - lastMousePos.y;
lastMousePos.x = ev.x;
lastMousePos.y = ev.y;
}
} while (0);
do { // while (0)
count = unhandledKeyEvents.next;
for (i = 0; i < count; ++i) {
const auto &ev = unhandledKeyEvents[i];
PkeKeyEvent *primaryEvent = nullptr;
FindActivePkeInputAction_Key_ByType(PkeInputEventMask {
.computedHash = PKE_INPUT_HASH_ALL_KEY_EVENTS,
.button = ev.button,
.mods = ev.mods,
}, primaryEvent);
if (primaryEvent == nullptr) {
continue;
}
if (ev.action == GLFW_PRESS) {
primaryEvent->isPressed = true;
primaryEvent->thisTick = true;
} else if (ev.action == GLFW_RELEASE) {
primaryEvent->isPressed = false;
primaryEvent->thisTick = true;
} else {
// repeat
primaryEvent->isPressed = true;
}
}
} while (0);
do { // while (0)
count = unhandledMouseButtonEvents.next;
for (i = 0; i < count; ++i) {
const auto &ev = unhandledMouseButtonEvents[i];
PkeMouseButtonEvent *primaryEvent = nullptr;
FindActivePkeInputAction_MouseButton_ByType(PkeInputEventMask {
.computedHash = PKE_INPUT_HASH_ALL_MOUSE_BUTTON_EVENTS,
.button = ev.button,
.mods = ev.mods,
}, primaryEvent);
if (primaryEvent == nullptr) {
continue;
}
if (ev.action == GLFW_PRESS) {
primaryEvent->isPressed = true;
primaryEvent->thisTick = true;
} else if (ev.action == GLFW_RELEASE) {
primaryEvent->isPressed = false;
primaryEvent->thisTick = true;
}
}
} while (0);
do { // while (0)
PkeScrollEvent *primaryEvent = nullptr;
FindActivePkeInputAction_Scroll_ByType(PkeInputEventMask {
.computedHash = PKE_INPUT_HASH_ALL_SCROLL_EVENTS
}, primaryEvent);
if (primaryEvent == nullptr) {
break;
}
count = unhandledScrollEvents.next;
for (i = 0; i < count; ++i) {
const auto &ev = unhandledScrollEvents[i];
primaryEvent->xMotion += ev.x;
primaryEvent->yMotion += ev.y;
}
} while (0);
do { // while (0)
PkeWindowFocusEvent *primaryEvent = nullptr;
FindActivePkeInputAction_WindowFocus_ByType(PkeInputEventMask {
.computedHash = PKE_INPUT_HASH_ALL_WINDOW_FOCUS_EVENTS
}, primaryEvent);
if (primaryEvent == nullptr) {
break;
}
const auto &ev = unhandledWindowFocusEvents[unhandledWindowFocusEvents.next-1];
primaryEvent->isFocused = ev.focused;
lastWindowFocus = ev.focused;
} while (0);
}
pk_arr_reset(&unhandledCursorEnterEvents);
pk_arr_reset(&unhandledCursorPosEvents);
pk_arr_reset(&unhandledKeyEvents);
pk_arr_reset(&unhandledMouseButtonEvents);
pk_arr_reset(&unhandledScrollEvents);
pk_arr_reset(&unhandledWindowFocusEvents);
// set boolean events with latest data
// - we do this after processing events to make sure that if a set is
// unregistered, the appropriate values are propagated
// - theoretically this scenario could be handled on unregister,
// but I'm not sure which would be more performant
// JCB 2025-05-21
// I'm refactoring this and it seems to skip the last element.
// At a glance, I'm not sure if this is intentional or a bug.
// I'm going to preserve this logic in my refactor.
// Update this comment as needed and make sure to document the purpose.
{
count = registeredCursorEnterEvents.next;
for (i = 0; i < count; ++i) {
registeredCursorEnterEvents[i].isEntered = lastCursorEntered;
if (i == count-2) break;
}
count = registeredWindowFocusEvents.next;
for (i = 0; i < count; ++i) {
registeredWindowFocusEvents[i].isFocused = lastWindowFocus;
if (i == count-2) break;
}
}
}
PkeInputEventHolder PkeInput_Query(const char *actionName) {
PkeInputEventBase *ev = nullptr;
auto *action = FindActionByName(actionName);
InputEventHash type = InputEventHash{0};
for (int32_t i = 0; i < PKE_INPUT_ACTION_MASK_INDEX_COUNT; ++i) {
PkeInputEventMask event_mask = action->masks[i];
if (bool(event_mask.computedHash & PKE_INPUT_HASH_EVENT_TYPE_CURSOR_ENTER)) {
PkeCursorEnterEvent *event;
type = FindActivePkeInputAction_ByAction<PKE_INPUT_HASH_EVENT_TYPE_CURSOR_ENTER, PkeCursorEnterEvent>(action, event, i);
ev = event;
break;
} else if (bool(event_mask.computedHash & PKE_INPUT_HASH_EVENT_TYPE_CURSOR_POS)) {
PkeCursorPosEvent *event;
type = FindActivePkeInputAction_ByAction<PKE_INPUT_HASH_EVENT_TYPE_CURSOR_POS, PkeCursorPosEvent>(action, event, i);
ev = event;
break;
} else if (bool(event_mask.computedHash & PKE_INPUT_HASH_EVENT_TYPE_KEY)) {
PkeKeyEvent *event;
type = FindActivePkeInputAction_ByAction<PKE_INPUT_HASH_EVENT_TYPE_KEY, PkeKeyEvent>(action, event, i);
ev = event;
break;
} else if (bool(event_mask.computedHash & PKE_INPUT_HASH_EVENT_TYPE_MOUSE_BUTTON)) {
PkeMouseButtonEvent *event;
type = FindActivePkeInputAction_ByAction<PKE_INPUT_HASH_EVENT_TYPE_MOUSE_BUTTON, PkeMouseButtonEvent>(action, event, i);
ev = event;
break;
} else if (bool(event_mask.computedHash & PKE_INPUT_HASH_EVENT_TYPE_SCROLL)) {
PkeScrollEvent *event;
type = FindActivePkeInputAction_ByAction<PKE_INPUT_HASH_EVENT_TYPE_SCROLL, PkeScrollEvent>(action, event, i);
ev = event;
break;
} else if (bool(event_mask.computedHash & PKE_INPUT_HASH_EVENT_TYPE_WINDOW_FOCUS)) {
PkeWindowFocusEvent *event;
type = FindActivePkeInputAction_ByAction<PKE_INPUT_HASH_EVENT_TYPE_WINDOW_FOCUS, PkeWindowFocusEvent>(action, event, i);
ev = event;
break;
}
}
return PkeInputEventHolder {
.type = type,
.ptr = ev,
};
}
void CursorEnterCallback(GLFWwindow *window, int entered) {
if (registeredCursorEnterEvents.next) {
pk_arr_append_t(&unhandledCursorEnterEvents, { .entered = bool(entered) });
}
if (prevCursorEnterCallback) {
prevCursorEnterCallback(window, entered);
}
}
void CursorPosCallback(GLFWwindow *window, double xPos, double yPos) {
if (registeredCursorPosEvents.next) {
pk_arr_append_t(&unhandledCursorPosEvents, {
.x = xPos,
.y = yPos,
});
}
if (prevCursorPosCallback) {
prevCursorPosCallback(window, xPos, yPos);
}
}
void KeyCallback(GLFWwindow *window, int key, int scancode, int action, int mods) {
if (registeredKeyEvents.next) {
pk_arr_append_t(&unhandledKeyEvents, {
.button = key,
.mods = mods & 0x0F,
.action = int8_t(action),
});
}
if (prevKeyCallback) {
prevKeyCallback(window, key, scancode, action, mods);
}
}
void MouseButtonCallback(GLFWwindow *window, int button, int action, int mods) {
if (registeredMouseButtonEvents.next) {
pk_arr_append_t(&unhandledMouseButtonEvents, {
.button = button,
.mods = mods,
.action = int8_t(action),
});
}
if (prevMouseButtonCallback) {
prevMouseButtonCallback(window, button, action, mods);
}
}
void ScrollCallback(GLFWwindow *window, double xOffset, double yOffset) {
if (registeredScrollEvents.next) {
pk_arr_append_t(&unhandledScrollEvents, {
.x = xOffset,
.y = yOffset,
});
}
if (prevScrollCallback) {
prevScrollCallback(window, xOffset, yOffset);
}
}
void WindowFocusCallback(GLFWwindow *window, int focused) {
if (registeredWindowFocusEvents.next) {
pk_arr_append_t(&unhandledWindowFocusEvents, {
.focused = bool(focused),
});
}
if (prevWindowFocusCallback) {
prevWindowFocusCallback(window, focused);
}
}
void PkeInput_Init() {
prevCursorEnterCallback = glfwSetCursorEnterCallback(window, CursorEnterCallback);
prevCursorPosCallback = glfwSetCursorPosCallback(window, CursorPosCallback);
prevKeyCallback = glfwSetKeyCallback(window, KeyCallback);
prevMouseButtonCallback = glfwSetMouseButtonCallback(window, MouseButtonCallback);
prevScrollCallback = glfwSetScrollCallback(window, ScrollCallback);
prevWindowFocusCallback = glfwSetWindowFocusCallback(window, WindowFocusCallback);
}
InputActionSetHandle PkeInput_RegisterSet(const PkeInputSet &set) {
InputActionSetHandle returnValue{static_cast<InputActionSetHandle_T>(registeredInputSets.next)};
pk_arr_append_t(®isteredInputSets, set);
return returnValue;
}
void PkeInput_ActivateSet(InputActionSetHandle handle) {
InputActionSetHandle_T index{static_cast<InputActionSetHandle_T>(handle)};
pk_arr_append_t(&activeInputSetStack, handle);
auto &set = registeredInputSets[index];
for (int64_t i = 0; i < set.actionCount; ++i) {
PkeInputAction &action = set.actions[i];
for (int k = 0; k < PKE_INPUT_ACTION_MASK_INDEX_COUNT; ++k) {
PkeInputEventMask event_mask = action.masks[k];
if ((event_mask.computedHash & PKE_INPUT_HASH_EVENT_TYPE_CURSOR_ENTER) != InputEventHash{0}) {
action.event_indices[k] = registeredCursorEnterEvents.next;
PkeCursorEnterEvent ev{};
ev.sourceSet = handle;
ev.isEntered = lastCursorEntered;
pk_arr_append_t(®isteredCursorEnterEvents, ev);
}
if ((event_mask.computedHash & PKE_INPUT_HASH_EVENT_TYPE_CURSOR_POS) != InputEventHash{0}) {
action.event_indices[k] = registeredCursorPosEvents.next;
PkeCursorPosEvent ev {};
ev.sourceSet = handle;
ev.xMotion = 0;
ev.yMotion = 0;
pk_arr_append_t(®isteredCursorPosEvents, ev);
glfwGetCursorPos(window, &lastMousePos.x, &lastMousePos.y);
}
if ((event_mask.computedHash & PKE_INPUT_HASH_EVENT_TYPE_KEY) != InputEventHash{0}) {
action.event_indices[k] = registeredKeyEvents.next;
PkeKeyEvent ev{};
ev.sourceSet = handle;
ev.button = event_mask.button;
ev.mods = event_mask.mods;
pk_arr_append_t(®isteredKeyEvents, ev);
}
if ((event_mask.computedHash & PKE_INPUT_HASH_EVENT_TYPE_MOUSE_BUTTON) != InputEventHash{0}) {
action.event_indices[k] = registeredMouseButtonEvents.next;
PkeMouseButtonEvent ev{};
ev.sourceSet = handle;
ev.button = event_mask.button;
ev.mods = event_mask.mods;
pk_arr_append_t(®isteredMouseButtonEvents, ev);
}
if ((event_mask.computedHash & PKE_INPUT_HASH_EVENT_TYPE_SCROLL) != InputEventHash{0}) {
action.event_indices[k] = registeredScrollEvents.next;
PkeScrollEvent ev{};
ev.sourceSet = handle;
ev.xMotion = 0;
ev.yMotion = 0;
pk_arr_append_t(®isteredScrollEvents, ev);
}
if ((event_mask.computedHash & PKE_INPUT_HASH_EVENT_TYPE_WINDOW_FOCUS) != InputEventHash{0}) {
action.event_indices[k] = registeredWindowFocusEvents.next;
PkeWindowFocusEvent ev{};
ev.sourceSet = handle;
ev.isFocused = lastWindowFocus;
pk_arr_append_t(®isteredWindowFocusEvents, ev);
}
}
}
}
void PkeInput_DeactivateSet(InputActionSetHandle handle) {
int64_t index = -1;
uint32_t count, i;
count = activeInputSetStack.next;
for (i = 0; i < count; ++i) {
if (activeInputSetStack[i] == handle) {
index = i;
break;
}
}
assert(index >= 0 && "PkeInput_DeactivateSet - expected InputActionSet to be active");
assert(index == activeInputSetStack.next - 1 && "PkeInput_UnregisterSet - expected InputActionSet to be the last set active");
InputActionSetHandle_T handleIndex{static_cast<InputActionSetHandle_T>(handle)};
auto &set = registeredInputSets[handleIndex];
for (int64_t i = set.actionCount - 1; i >= 0; --i) {
PkeInputAction &action = set.actions[i];
for (int k = 0; k < PKE_INPUT_ACTION_MASK_INDEX_COUNT; ++k) {
PkeInputEventMask event_mask = action.masks[k];
if ((event_mask.computedHash & PKE_INPUT_HASH_EVENT_TYPE_CURSOR_ENTER) != InputEventHash{0}) {
pk_arr_remove_at(®isteredCursorEnterEvents, action.event_indices[k]);
}
if ((event_mask.computedHash & PKE_INPUT_HASH_EVENT_TYPE_CURSOR_POS) != InputEventHash{0}) {
pk_arr_remove_at(®isteredCursorPosEvents, action.event_indices[k]);
}
if ((event_mask.computedHash & PKE_INPUT_HASH_EVENT_TYPE_KEY) != InputEventHash{0}) {
pk_arr_remove_at(®isteredKeyEvents, action.event_indices[k]);
}
if ((event_mask.computedHash & PKE_INPUT_HASH_EVENT_TYPE_MOUSE_BUTTON) != InputEventHash{0}) {
pk_arr_remove_at(®isteredMouseButtonEvents, action.event_indices[k]);
}
if ((event_mask.computedHash & PKE_INPUT_HASH_EVENT_TYPE_SCROLL) != InputEventHash{0}) {
pk_arr_remove_at(®isteredScrollEvents, action.event_indices[k]);
}
if ((event_mask.computedHash & PKE_INPUT_HASH_EVENT_TYPE_WINDOW_FOCUS) != InputEventHash{0}) {
pk_arr_remove_at(®isteredWindowFocusEvents, action.event_indices[k]);
}
}
}
pk_arr_remove_at(&activeInputSetStack, activeInputSetStack.next-1);
}
bool PkeInput_pke_arr_find_first_handle(void *search_handle, void *list_handle) {
return reinterpret_cast<InputActionSetHandle *>(search_handle) == reinterpret_cast<InputActionSetHandle *>(list_handle);
}
void PkeInput_UnregisterSet(InputActionSetHandle handle) {
InputActionSetHandle_T index{static_cast<InputActionSetHandle_T>(handle)};
assert(index == registeredInputSets.next - 1 && "PkeInput_UnregisterSet - expected InputActionSet to be the last set registered");
const auto &set = registeredInputSets[index];
if (pk_arr_find_first_index(&activeInputSetStack, &handle, PkeInput_pke_arr_find_first_handle) != uint32_t(-1)) {
PkeInput_DeactivateSet(handle);
}
pk_delete_arr<PkeInputAction>(set.actions, set.actionCount);
pk_arr_remove_at(®isteredInputSets, index);
}
pk_arr_t<PkeInputSet> &PkeInput_GetInputSets() {
return registeredInputSets;
}
void PkeInput_Teardown() {
glfwSetWindowFocusCallback(window, prevWindowFocusCallback);
glfwSetScrollCallback(window, prevScrollCallback);
glfwSetMouseButtonCallback(window, prevMouseButtonCallback);
glfwSetKeyCallback(window, prevKeyCallback);
glfwSetCursorPosCallback(window, prevCursorPosCallback);
glfwSetCursorEnterCallback(window, prevCursorEnterCallback);
pk_arr_reset(&unhandledCursorPosEvents);
pk_arr_reset(&unhandledCursorEnterEvents);
pk_arr_reset(&unhandledKeyEvents);
pk_arr_reset(&unhandledMouseButtonEvents);
pk_arr_reset(&unhandledScrollEvents);
pk_arr_reset(&unhandledWindowFocusEvents);
pk_arr_reset(®isteredCursorEnterEvents);
pk_arr_reset(®isteredCursorPosEvents);
pk_arr_reset(®isteredKeyEvents);
pk_arr_reset(®isteredMouseButtonEvents);
pk_arr_reset(®isteredScrollEvents);
pk_arr_reset(®isteredWindowFocusEvents);
pk_arr_reset(&activeInputSetStack);
pk_arr_reset(®isteredInputSets);
}
|