From fa8d64ec3a78604d98e59daa46bf34004957c1de Mon Sep 17 00:00:00 2001 From: David Chavez Date: Thu, 22 Sep 2022 03:32:27 +0200 Subject: [PATCH] Disable game input when typing into a textfield (#1398) * Disable game input when typing into a textfield * Only block keyboard input when typing in textfield * Address PR comments * Fix spacing * Generalize check on input device Co-authored-by: Garrett Cox --- libultraship/libultraship/ControlDeck.cpp | 25 +++++++++++++++++++---- libultraship/libultraship/ControlDeck.h | 2 +- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/libultraship/libultraship/ControlDeck.cpp b/libultraship/libultraship/ControlDeck.cpp index 499d34d72..a1ef4da7f 100644 --- a/libultraship/libultraship/ControlDeck.cpp +++ b/libultraship/libultraship/ControlDeck.cpp @@ -5,6 +5,7 @@ #include "DummyController.h" #include #include "Cvar.h" +#include "Lib/ImGui/imgui.h" #ifndef __WIIU__ #include "KeyboardController.h" @@ -75,20 +76,26 @@ namespace Ship { void ControlDeck::WriteToPad(OSContPad* pad) const { for (size_t i = 0; i < virtualDevices.size(); i++) { const std::shared_ptr backend = physicalDevices[virtualDevices[i]]; + + // If the controller backend is "Auto" we need to get the real device + // we search for the real device to read input from it if (backend->GetGuid() == "Auto") { for (const auto& device : physicalDevices) { - if(ShouldBlockGameInput() && device->GetGuid() != "Keyboard") { + if(ShouldBlockGameInput(device->GetGuid())) { device->Read(nullptr, i); continue; } + device->Read(&pad[i], i); } continue; } - if(ShouldBlockGameInput() && backend->GetGuid() != "Keyboard") { + + if(ShouldBlockGameInput(backend->GetGuid())) { backend->Read(nullptr, i); continue; } + backend->Read(&pad[i], i); } } @@ -264,7 +271,17 @@ namespace Ship { shouldBlockGameInput = false; } - bool ControlDeck::ShouldBlockGameInput() const { - return shouldBlockGameInput || (CVar_GetS32("gOpenMenuBar", 0) && CVar_GetS32("gControlNav", 0)); + bool ControlDeck::ShouldBlockGameInput(std::string inputDeviceGuid) const { + // We block controller input if F1 menu is open and control navigation is on. + // This is because we don't want controller inputs to affect the game + bool shouldBlockControllerInput = CVar_GetS32("gOpenMenuBar", 0) && CVar_GetS32("gControlNav", 0); + + // We block keyboard input if you're currently typing into a textfield. + // This is because we don't want your keyboard typing to affect the game. + ImGuiIO io = ImGui::GetIO(); + bool shouldBlockKeyboardInput = io.WantCaptureKeyboard; + + bool inputDeviceIsKeyboard = inputDeviceGuid == "Keyboard"; + return shouldBlockGameInput || (inputDeviceIsKeyboard ? shouldBlockKeyboardInput : shouldBlockControllerInput); } } diff --git a/libultraship/libultraship/ControlDeck.h b/libultraship/libultraship/ControlDeck.h index b72f8c3ce..2e330850c 100644 --- a/libultraship/libultraship/ControlDeck.h +++ b/libultraship/libultraship/ControlDeck.h @@ -22,7 +22,7 @@ namespace Ship { uint8_t* GetControllerBits(); void BlockGameInput(); void UnblockGameInput(); - bool ShouldBlockGameInput() const; + bool ShouldBlockGameInput(std::string inputDeviceGuid) const; private: std::vector virtualDevices = {}; std::vector> physicalDevices = {};