diff options
| -rw-r--r-- | src/player-input.cpp | 40 | ||||
| -rw-r--r-- | src/player-input.hpp | 14 |
2 files changed, 44 insertions, 10 deletions
diff --git a/src/player-input.cpp b/src/player-input.cpp index 8392619..4b2b6be 100644 --- a/src/player-input.cpp +++ b/src/player-input.cpp @@ -20,6 +20,30 @@ TypeSafeInt_B(InputWindowFocused); DynArray<PkeInputEvent> UnhandledPkeInputEvents{0, nullptr}; +bool PkeInput_Query(const PkeInputEvent &mask) { + auto count = UnhandledPkeInputEvents.Count(); + for (long i = 0; i < count; ++i) { + const auto &ev = UnhandledPkeInputEvents[i]; + // TODO this could probably be reduced to a single & op + // - if we exclude x, y, button, scanCode, and mods + // there are less than 64 options total + if (!(mask.type == ev.type)) continue; + if (!(mask.tickCount == ev.tickCount)) continue; + if (!(mask.cursorEntered == ev.cursorEntered)) continue; + if (!(mask.keyboardKeyAction == ev.keyboardKeyAction)) continue; + if (!(mask.mouseButtonAction == ev.mouseButtonAction)) continue; + if (!(mask.windowFocused == ev.windowFocused)) continue; + if (!(mask.x == ev.x)) continue; + if (!(mask.y == ev.y)) continue; + if (!(mask.button == ev.button)) continue; + if (!(mask.scanCode == ev.scanCode)) continue; + if (!(mask.mods == ev.mods)) continue; + UnhandledPkeInputEvents.Remove(i); + return true; + } + return false; +} + void PkeInput_Tick(double delta) { // If we haven't handled an event within two seconds, we probably don't need it static GameTimeDuration eventExpiration(std::chrono::seconds(2)); @@ -27,13 +51,15 @@ void PkeInput_Tick(double delta) { int64_t unhandledCount = UnhandledPkeInputEvents.Count(); auto expiredTime = pkeSettings.steadyClock.now() - eventExpiration; for (int64_t i = 0; i < unhandledCount; ++i) { - if (UnhandledPkeInputEvents[i].timePoint < expiredTime) { + auto &ev = UnhandledPkeInputEvents[i]; + ev.tickCount += 1; + if (ev.timePoint < expiredTime) { removeCount += 1; - continue; } - break; } - UnhandledPkeInputEvents.Remove(0, removeCount); + if (removeCount > 0) { + UnhandledPkeInputEvents.Remove(0, removeCount); + } } void CursorEnterCallback(GLFWwindow *window, int entered) { @@ -49,6 +75,7 @@ void CursorEnterCallback(GLFWwindow *window, int entered) { ev.button = 0; ev.scanCode = 0; ev.mods = 0; + ev.tickCount = 0; if (prevCursorEnterCallback) { prevCursorEnterCallback(window, entered); } @@ -67,6 +94,7 @@ void CursorPosCallback(GLFWwindow *window, double xPos, double yPos) { ev.button = 0; ev.scanCode = 0; ev.mods = 0; + ev.tickCount = 0; if (prevCursorPosCallback) { prevCursorPosCallback(window, xPos, yPos); } @@ -85,6 +113,7 @@ void KeyCallback(GLFWwindow *window, int key, int scancode, int action, int mods ev.button = key; ev.scanCode = scancode; ev.mods = mods; + ev.tickCount = 0; if (prevKeyCallback) { prevKeyCallback(window, key, scancode, action, mods); } @@ -103,6 +132,7 @@ void MouseButtonCallback(GLFWwindow *window, int button, int action, int mods) { ev.button = button; ev.scanCode = 0; ev.mods = mods; + ev.tickCount = 0; if (prevMouseButtonCallback) { prevMouseButtonCallback(window, button, action, mods); } @@ -121,6 +151,7 @@ void ScrollCallback(GLFWwindow *window, double xOffset, double yOffset) { ev.button = 0; ev.scanCode = 0; ev.mods = 0; + ev.tickCount = 0; if (prevScrollCallback) { prevScrollCallback(window, xOffset, yOffset); } @@ -139,6 +170,7 @@ void WindowFocusCallback(GLFWwindow *window, int focused) { ev.button = 0; ev.scanCode = 0; ev.mods = 0; + ev.tickCount = 0; if (prevWindowFocusCallback) { prevWindowFocusCallback(window, focused); } diff --git a/src/player-input.hpp b/src/player-input.hpp index baa4327..a5e64be 100644 --- a/src/player-input.hpp +++ b/src/player-input.hpp @@ -5,12 +5,12 @@ #include "game-type-defs.hpp" #include "macros.hpp" -TypeSafeInt_H(InputEventHash, int16_t, 255); -TypeSafeInt_H(InputEventType, int16_t, 255); -TypeSafeInt_H(InputCursorEntered, int16_t, 255); -TypeSafeInt_H(InputKeyboardKeyAction, int16_t, 255); -TypeSafeInt_H(InputMouseButtonAction, int16_t, 255); -TypeSafeInt_H(InputWindowFocused, int16_t, 255); +TypeSafeInt_H(InputEventHash, int16_t, 0x0FFF); +TypeSafeInt_H(InputEventType, int16_t, 0x0FFF); +TypeSafeInt_H(InputCursorEntered, int16_t, 0x0FFF); +TypeSafeInt_H(InputKeyboardKeyAction, int16_t, 0x0FFF); +TypeSafeInt_H(InputMouseButtonAction, int16_t, 0x0FFF); +TypeSafeInt_H(InputWindowFocused, int16_t, 0x0FFF); const InputEventType PKE_INPUT_EVENT_TYPE_CURSOR_ENTER = InputEventType{0}; const InputEventType PKE_INPUT_EVENT_TYPE_CURSOR_POS = InputEventType{1}; @@ -48,9 +48,11 @@ struct PkeInputEvent { int32_t button; int32_t scanCode; int32_t mods; + uint64_t tickCount; }; void PkeInput_Tick(double delta); +bool PkeInput_Query(const PkeInputEvent &mask); void PkeInput_Init(); void PkeInput_Teardown(); |
