diff --git a/UnleashedRecomp/hid/driver/sdl_hid.cpp b/UnleashedRecomp/hid/driver/sdl_hid.cpp index 5e21ada..1cc989d 100644 --- a/UnleashedRecomp/hid/driver/sdl_hid.cpp +++ b/UnleashedRecomp/hid/driver/sdl_hid.cpp @@ -68,7 +68,7 @@ public: bool CanPoll() { - return controller && (GameWindow::s_isFocused || Config::AllowBackgroundInput); + return controller; } void PollAxis() diff --git a/UnleashedRecomp/hid/hid.cpp b/UnleashedRecomp/hid/hid.cpp index 14ac803..8db2ebe 100644 --- a/UnleashedRecomp/hid/hid.cpp +++ b/UnleashedRecomp/hid/hid.cpp @@ -1,4 +1,6 @@ #include "hid.h" +#include +#include hid::EInputDevice hid::g_inputDevice; hid::EInputDevice hid::g_inputDeviceController; @@ -11,6 +13,11 @@ void hid::SetProhibitedButtons(uint16_t wButtons) hid::g_prohibitedButtons = wButtons; } +bool hid::IsInputAllowed() +{ + return GameWindow::s_isFocused || Config::AllowBackgroundInput; +} + bool hid::IsInputDeviceController() { return hid::g_inputDevice != hid::EInputDevice::Keyboard && diff --git a/UnleashedRecomp/hid/hid.h b/UnleashedRecomp/hid/hid.h index 922c21c..da8d973 100644 --- a/UnleashedRecomp/hid/hid.h +++ b/UnleashedRecomp/hid/hid.h @@ -41,6 +41,7 @@ namespace hid uint32_t SetState(uint32_t dwUserIndex, XAMINPUT_VIBRATION* pVibration); uint32_t GetCapabilities(uint32_t dwUserIndex, XAMINPUT_CAPABILITIES* pCaps); + bool IsInputAllowed(); bool IsInputDeviceController(); std::string GetInputDeviceName(); } diff --git a/UnleashedRecomp/kernel/xam.cpp b/UnleashedRecomp/kernel/xam.cpp index 80a0a5a..d69b7df 100644 --- a/UnleashedRecomp/kernel/xam.cpp +++ b/UnleashedRecomp/kernel/xam.cpp @@ -405,7 +405,8 @@ uint32_t XamInputGetState(uint32_t userIndex, uint32_t flags, XAMINPUT_STATE* st { memset(state, 0, sizeof(*state)); - uint32_t result = hid::GetState(userIndex, state); + if (hid::IsInputAllowed()) + hid::GetState(userIndex, state); if (GameWindow::s_isFocused) { diff --git a/UnleashedRecomp/patches/input_patches.cpp b/UnleashedRecomp/patches/input_patches.cpp index 0477b3c..fa2efa2 100644 --- a/UnleashedRecomp/patches/input_patches.cpp +++ b/UnleashedRecomp/patches/input_patches.cpp @@ -123,6 +123,9 @@ public: void OnSDLEvent(SDL_Event* event) override { + if (!hid::IsInputAllowed()) + return; + switch (event->type) { #ifdef UI_KBM_SUPPORT diff --git a/UnleashedRecomp/ui/installer_wizard.cpp b/UnleashedRecomp/ui/installer_wizard.cpp index 347e832..f59ff2f 100644 --- a/UnleashedRecomp/ui/installer_wizard.cpp +++ b/UnleashedRecomp/ui/installer_wizard.cpp @@ -159,100 +159,103 @@ class SDLEventListenerForInstaller : public SDLEventListener public: void OnSDLEvent(SDL_Event *event) override { + if (!InstallerWizard::s_isVisible || !g_currentMessagePrompt.empty() || g_currentPickerVisible || !hid::IsInputAllowed()) + return; + constexpr float AxisValueRange = 32767.0f; constexpr float AxisTapRange = 0.5f; - if (!InstallerWizard::s_isVisible || !g_currentMessagePrompt.empty() || g_currentPickerVisible) - { - return; - } - int newCursorIndex = -1; ImVec2 tapDirection = {}; + switch (event->type) { - case SDL_KEYDOWN: - switch (event->key.keysym.scancode) + case SDL_KEYDOWN: { - case SDL_SCANCODE_LEFT: - case SDL_SCANCODE_RIGHT: - tapDirection.x = (event->key.keysym.scancode == SDL_SCANCODE_RIGHT) ? 1.0f : -1.0f; - break; - case SDL_SCANCODE_UP: - case SDL_SCANCODE_DOWN: - tapDirection.y = (event->key.keysym.scancode == SDL_SCANCODE_DOWN) ? 1.0f : -1.0f; - break; - case SDL_SCANCODE_RETURN: - case SDL_SCANCODE_KP_ENTER: - g_currentCursorAccepted = (g_currentCursorIndex >= 0); - break; - } - - break; - case SDL_CONTROLLERBUTTONDOWN: - switch (event->cbutton.button) - { - case SDL_CONTROLLER_BUTTON_DPAD_LEFT: - tapDirection = { -1.0f, 0.0f }; - break; - case SDL_CONTROLLER_BUTTON_DPAD_RIGHT: - tapDirection = { 1.0f, 0.0f }; - break; - case SDL_CONTROLLER_BUTTON_DPAD_UP: - tapDirection = { 0.0f, -1.0f }; - break; - case SDL_CONTROLLER_BUTTON_DPAD_DOWN: - tapDirection = { 0.0f, 1.0f }; - break; - case SDL_CONTROLLER_BUTTON_A: - g_currentCursorAccepted = (g_currentCursorIndex >= 0); - break; - } - - break; - case SDL_CONTROLLERAXISMOTION: - { - if (event->caxis.axis < 2) - { - float newAxisValue = event->caxis.value / AxisValueRange; - bool sameDirection = (newAxisValue * g_joypadAxis[event->caxis.axis]) > 0.0f; - bool wasInRange = abs(g_joypadAxis[event->caxis.axis]) > AxisTapRange; - bool isInRange = abs(newAxisValue) > AxisTapRange; - if (sameDirection && !wasInRange && isInRange) + switch (event->key.keysym.scancode) { - tapDirection[event->caxis.axis] = newAxisValue; + case SDL_SCANCODE_LEFT: + case SDL_SCANCODE_RIGHT: + tapDirection.x = (event->key.keysym.scancode == SDL_SCANCODE_RIGHT) ? 1.0f : -1.0f; + break; + case SDL_SCANCODE_UP: + case SDL_SCANCODE_DOWN: + tapDirection.y = (event->key.keysym.scancode == SDL_SCANCODE_DOWN) ? 1.0f : -1.0f; + break; + case SDL_SCANCODE_RETURN: + case SDL_SCANCODE_KP_ENTER: + g_currentCursorAccepted = (g_currentCursorIndex >= 0); + break; } - g_joypadAxis[event->caxis.axis] = newAxisValue; + break; } - break; - } - case SDL_MOUSEBUTTONDOWN: - case SDL_MOUSEMOTION: - { - for (size_t i = 0; i < g_currentCursorRects.size(); i++) + case SDL_CONTROLLERBUTTONDOWN: { - auto ¤tRect = g_currentCursorRects[i]; - if (ImGui::IsMouseHoveringRect(currentRect.first, currentRect.second, false)) + switch (event->cbutton.button) { - newCursorIndex = int(i); + case SDL_CONTROLLER_BUTTON_DPAD_LEFT: + tapDirection = { -1.0f, 0.0f }; + break; + case SDL_CONTROLLER_BUTTON_DPAD_RIGHT: + tapDirection = { 1.0f, 0.0f }; + break; + case SDL_CONTROLLER_BUTTON_DPAD_UP: + tapDirection = { 0.0f, -1.0f }; + break; + case SDL_CONTROLLER_BUTTON_DPAD_DOWN: + tapDirection = { 0.0f, 1.0f }; + break; + case SDL_CONTROLLER_BUTTON_A: + g_currentCursorAccepted = (g_currentCursorIndex >= 0); + break; + } - if (event->type == SDL_MOUSEBUTTONDOWN && event->button.button == SDL_BUTTON_LEFT) + break; + } + + case SDL_CONTROLLERAXISMOTION: + { + if (event->caxis.axis < 2) + { + float newAxisValue = event->caxis.value / AxisValueRange; + bool sameDirection = (newAxisValue * g_joypadAxis[event->caxis.axis]) > 0.0f; + bool wasInRange = abs(g_joypadAxis[event->caxis.axis]) > AxisTapRange; + bool isInRange = abs(newAxisValue) > AxisTapRange; + if (sameDirection && !wasInRange && isInRange) { - g_currentCursorAccepted = true; + tapDirection[event->caxis.axis] = newAxisValue; } - break; + g_joypadAxis[event->caxis.axis] = newAxisValue; } + + break; } - if (newCursorIndex < 0) + case SDL_MOUSEBUTTONDOWN: + case SDL_MOUSEMOTION: { - g_currentCursorIndex = -1; - } + for (size_t i = 0; i < g_currentCursorRects.size(); i++) + { + auto ¤tRect = g_currentCursorRects[i]; - break; - } + if (ImGui::IsMouseHoveringRect(currentRect.first, currentRect.second, false)) + { + newCursorIndex = int(i); + + if (event->type == SDL_MOUSEBUTTONDOWN && event->button.button == SDL_BUTTON_LEFT) + g_currentCursorAccepted = true; + + break; + } + } + + if (newCursorIndex < 0) + g_currentCursorIndex = -1; + + break; + } } if (tapDirection.x != 0.0f || tapDirection.y != 0.0f) @@ -300,9 +303,7 @@ public: if (newCursorIndex >= 0) { if (g_currentCursorIndex != newCursorIndex) - { Game_PlaySound("sys_worldmap_cursor"); - } g_currentCursorIndex = newCursorIndex; } diff --git a/UnleashedRecomp/ui/message_window.cpp b/UnleashedRecomp/ui/message_window.cpp index 8968de7..c7e454e 100644 --- a/UnleashedRecomp/ui/message_window.cpp +++ b/UnleashedRecomp/ui/message_window.cpp @@ -53,7 +53,7 @@ class SDLEventListenerForMessageWindow : public SDLEventListener public: void OnSDLEvent(SDL_Event* event) override { - if (App::s_isInit || !MessageWindow::s_isVisible) + if (App::s_isInit || !MessageWindow::s_isVisible || !hid::IsInputAllowed()) return; constexpr float axisValueRange = 32767.0f;