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 <garrettjcox@gmail.com>
This commit is contained in:
David Chavez 2022-09-22 03:32:27 +02:00 committed by GitHub
parent b4740d131f
commit fa8d64ec3a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 5 deletions

View File

@ -5,6 +5,7 @@
#include "DummyController.h" #include "DummyController.h"
#include <Utils/StringHelper.h> #include <Utils/StringHelper.h>
#include "Cvar.h" #include "Cvar.h"
#include "Lib/ImGui/imgui.h"
#ifndef __WIIU__ #ifndef __WIIU__
#include "KeyboardController.h" #include "KeyboardController.h"
@ -75,20 +76,26 @@ namespace Ship {
void ControlDeck::WriteToPad(OSContPad* pad) const { void ControlDeck::WriteToPad(OSContPad* pad) const {
for (size_t i = 0; i < virtualDevices.size(); i++) { for (size_t i = 0; i < virtualDevices.size(); i++) {
const std::shared_ptr<Controller> backend = physicalDevices[virtualDevices[i]]; const std::shared_ptr<Controller> 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") { if (backend->GetGuid() == "Auto") {
for (const auto& device : physicalDevices) { for (const auto& device : physicalDevices) {
if(ShouldBlockGameInput() && device->GetGuid() != "Keyboard") { if(ShouldBlockGameInput(device->GetGuid())) {
device->Read(nullptr, i); device->Read(nullptr, i);
continue; continue;
} }
device->Read(&pad[i], i); device->Read(&pad[i], i);
} }
continue; continue;
} }
if(ShouldBlockGameInput() && backend->GetGuid() != "Keyboard") {
if(ShouldBlockGameInput(backend->GetGuid())) {
backend->Read(nullptr, i); backend->Read(nullptr, i);
continue; continue;
} }
backend->Read(&pad[i], i); backend->Read(&pad[i], i);
} }
} }
@ -264,7 +271,17 @@ namespace Ship {
shouldBlockGameInput = false; shouldBlockGameInput = false;
} }
bool ControlDeck::ShouldBlockGameInput() const { bool ControlDeck::ShouldBlockGameInput(std::string inputDeviceGuid) const {
return shouldBlockGameInput || (CVar_GetS32("gOpenMenuBar", 0) && CVar_GetS32("gControlNav", 0)); // 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);
} }
} }

View File

@ -22,7 +22,7 @@ namespace Ship {
uint8_t* GetControllerBits(); uint8_t* GetControllerBits();
void BlockGameInput(); void BlockGameInput();
void UnblockGameInput(); void UnblockGameInput();
bool ShouldBlockGameInput() const; bool ShouldBlockGameInput(std::string inputDeviceGuid) const;
private: private:
std::vector<int32_t> virtualDevices = {}; std::vector<int32_t> virtualDevices = {};
std::vector<std::shared_ptr<Controller>> physicalDevices = {}; std::vector<std::shared_ptr<Controller>> physicalDevices = {};