diff options
| author | Jonathan Bradley <jcb@pikum.xyz> | 2023-11-08 10:34:08 -0500 |
|---|---|---|
| committer | Jonathan Bradley <jcb@pikum.xyz> | 2023-11-08 11:09:15 -0500 |
| commit | 2e2d668b6a6989a1471cdc7854d387ba774c19d3 (patch) | |
| tree | 624c7b260ff4994396e1d108aee5c063681ab6ad /src/player-input.cpp | |
| parent | 5fa4f2c31d79adb83e438ad802c4bf176f10a535 (diff) | |
input events are queryable
Diffstat (limited to 'src/player-input.cpp')
| -rw-r--r-- | src/player-input.cpp | 40 |
1 files changed, 36 insertions, 4 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); } |
