imgui: gamepad capture, stopping propagation

This commit is contained in:
Vinicius Rangel 2024-09-02 21:58:58 -03:00
parent 752e4032c6
commit d6c0943720
No known key found for this signature in database
GPG key ID: A5B154D904B761D9
2 changed files with 23 additions and 4 deletions

View file

@ -12,6 +12,10 @@ public:
static void RemoveLayer(Layer* layer);
virtual void Draw() = 0;
virtual bool ShouldGrabGamepad() {
return false;
}
};
} // namespace ImGui

View file

@ -71,10 +71,7 @@ void Shutdown(const vk::Device& device) {
}
bool ProcessEvent(SDL_Event* event) {
const bool used = Sdl::ProcessEvent(event);
if (!used) {
return false;
}
Sdl::ProcessEvent(event);
switch (event->type) {
case SDL_EVENT_MOUSE_MOTION:
case SDL_EVENT_MOUSE_WHEEL:
@ -85,6 +82,15 @@ bool ProcessEvent(SDL_Event* event) {
case SDL_EVENT_KEY_DOWN:
case SDL_EVENT_KEY_UP:
return GetIO().WantCaptureKeyboard;
case SDL_EVENT_GAMEPAD_BUTTON_DOWN:
case SDL_EVENT_GAMEPAD_BUTTON_UP:
case SDL_EVENT_GAMEPAD_AXIS_MOTION:
case SDL_EVENT_GAMEPAD_ADDED:
case SDL_EVENT_GAMEPAD_REMOVED:
case SDL_EVENT_GAMEPAD_TOUCHPAD_DOWN:
case SDL_EVENT_GAMEPAD_TOUCHPAD_UP:
case SDL_EVENT_GAMEPAD_TOUCHPAD_MOTION:
return (GetIO().BackendFlags & ImGuiBackendFlags_HasGamepad) != 0;
default:
return false;
}
@ -109,8 +115,17 @@ void NewFrame() {
Sdl::NewFrame();
ImGui::NewFrame();
bool capture_gamepad = false;
for (auto* layer : layers) {
layer->Draw();
if (layer->ShouldGrabGamepad()) {
capture_gamepad = true;
}
}
if (capture_gamepad) {
GetIO().BackendFlags |= ImGuiBackendFlags_HasGamepad;
} else {
GetIO().BackendFlags &= ~ImGuiBackendFlags_HasGamepad;
}
}