Files
OpenRCT2-Web/src/openrct2-ui/input/InputManager.cpp
T

588 lines
18 KiB
C++
Raw Normal View History

2020-12-13 03:40:10 +00:00
/*****************************************************************************
2024-12-31 20:30:04 +01:00
* Copyright (c) 2014-2025 OpenRCT2 developers
2020-12-13 03:40:10 +00:00
*
* For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
*
* OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/
#include "InputManager.h"
2021-01-16 17:39:34 +00:00
#include "ShortcutIds.h"
2020-12-14 23:12:19 +00:00
#include <SDL.h>
#include <SDL_gamecontroller.h>
#include <cmath>
2020-12-14 23:12:19 +00:00
#include <openrct2-ui/UiContext.h>
#include <openrct2-ui/input/MouseInput.h>
2020-12-13 03:40:10 +00:00
#include <openrct2-ui/input/ShortcutManager.h>
2020-12-14 23:12:19 +00:00
#include <openrct2-ui/interface/InGameConsole.h>
#include <openrct2-ui/interface/Window.h>
#include <openrct2-ui/windows/Windows.h>
2020-12-14 23:12:19 +00:00
#include <openrct2/Input.h>
#include <openrct2/OpenRCT2.h>
#include <openrct2/config/Config.h>
#include <openrct2/interface/Chat.h>
#include <openrct2/interface/Viewport.h>
2020-12-14 23:12:19 +00:00
#include <openrct2/interface/Window.h>
#include <openrct2/paint/VirtualFloor.h>
2021-01-16 19:07:07 +00:00
#include <openrct2/ui/UiContext.h>
#include <openrct2/ui/WindowManager.h>
2020-12-13 03:40:10 +00:00
using namespace OpenRCT2::Ui;
InputManager::InputManager()
{
_modifierKeyState = EnumValue(ModifierKey::none);
}
2025-09-30 22:35:02 +02:00
void InputManager::queueInputEvent(const SDL_Event& e)
2020-12-15 23:14:40 +00:00
{
switch (e.type)
{
case SDL_CONTROLLERAXISMOTION:
{
// Process only the stick axes for scrolling (ignore triggers)
if (e.caxis.axis == SDL_CONTROLLER_AXIS_LEFTX || e.caxis.axis == SDL_CONTROLLER_AXIS_LEFTY
|| e.caxis.axis == SDL_CONTROLLER_AXIS_RIGHTX || e.caxis.axis == SDL_CONTROLLER_AXIS_RIGHTY)
{
InputEvent ie;
2025-09-30 22:27:30 +02:00
ie.deviceKind = InputDeviceKind::joyAxis;
ie.modifiers = SDL_GetModState();
ie.button = e.caxis.axis;
ie.state = InputEventState::down;
ie.axisValue = e.caxis.value;
2025-09-30 22:35:02 +02:00
queueInputEvent(std::move(ie));
}
break;
}
2020-12-15 23:14:40 +00:00
case SDL_JOYHATMOTION:
{
if (e.jhat.value != SDL_HAT_CENTERED)
{
InputEvent ie;
2025-09-30 22:27:30 +02:00
ie.deviceKind = InputDeviceKind::joyHat;
ie.modifiers = SDL_GetModState();
ie.button = e.jhat.value;
ie.state = InputEventState::down;
ie.axisValue = 0;
2025-09-30 22:35:02 +02:00
queueInputEvent(std::move(ie));
2020-12-15 23:14:40 +00:00
}
break;
}
case SDL_CONTROLLERBUTTONDOWN:
2020-12-15 23:14:40 +00:00
case SDL_JOYBUTTONDOWN:
{
InputEvent ie;
2025-09-30 22:27:30 +02:00
ie.deviceKind = InputDeviceKind::joyButton;
ie.modifiers = SDL_GetModState();
ie.button = e.cbutton.button;
ie.state = InputEventState::down;
ie.axisValue = 0;
2025-09-30 22:35:02 +02:00
queueInputEvent(std::move(ie));
2020-12-15 23:14:40 +00:00
break;
}
case SDL_CONTROLLERBUTTONUP:
2020-12-15 23:14:40 +00:00
case SDL_JOYBUTTONUP:
{
InputEvent ie;
2025-09-30 22:27:30 +02:00
ie.deviceKind = InputDeviceKind::joyButton;
ie.modifiers = SDL_GetModState();
ie.button = e.cbutton.button;
ie.state = InputEventState::release;
ie.axisValue = 0;
2025-09-30 22:35:02 +02:00
queueInputEvent(std::move(ie));
2020-12-15 23:14:40 +00:00
break;
}
case SDL_CONTROLLERDEVICEADDED:
case SDL_CONTROLLERDEVICEREMOVED:
case SDL_JOYDEVICEADDED:
case SDL_JOYDEVICEREMOVED:
{
// Force joystick refresh on next check
_lastJoystickCheck = 0;
break;
}
2020-12-15 23:14:40 +00:00
}
}
2025-09-30 22:35:02 +02:00
void InputManager::queueInputEvent(InputEvent&& e)
2020-12-13 03:40:10 +00:00
{
_events.push(e);
}
2025-09-30 22:35:02 +02:00
void InputManager::checkJoysticks()
2020-12-15 23:14:40 +00:00
{
2025-01-19 16:59:45 +01:00
constexpr uint32_t kCheckInternalMs = 5000;
2020-12-15 23:14:40 +00:00
auto tick = SDL_GetTicks();
2025-01-19 16:59:45 +01:00
if (tick > _lastJoystickCheck + kCheckInternalMs)
2020-12-15 23:14:40 +00:00
{
_lastJoystickCheck = tick;
_gameControllers.clear();
2020-12-15 23:14:40 +00:00
auto numJoysticks = SDL_NumJoysticks();
for (auto i = 0; i < numJoysticks; i++)
{
if (SDL_IsGameController(i))
2020-12-15 23:14:40 +00:00
{
auto gameController = SDL_GameControllerOpen(i);
if (gameController != nullptr)
{
_gameControllers.push_back(gameController);
}
2020-12-15 23:14:40 +00:00
}
}
}
}
void InputManager::processAnalogueInput()
{
_analogueScroll.x = 0;
_analogueScroll.y = 0;
const int32_t deadzone = Config::Get().general.gamepadDeadzone;
const float sensitivity = Config::Get().general.gamepadSensitivity;
for (auto* gameController : _gameControllers)
{
if (gameController != nullptr)
{
int32_t stickX = SDL_GameControllerGetAxis(gameController, SDL_CONTROLLER_AXIS_LEFTX);
int32_t stickY = SDL_GameControllerGetAxis(gameController, SDL_CONTROLLER_AXIS_LEFTY);
// Calculate the magnitude of the stick input vector
float magnitude = std::sqrt(static_cast<float>(stickX * stickX + stickY * stickY));
if (magnitude > deadzone)
{
// Apply deadzone to the magnitude, creating a more linear response
float adjustedMagnitude = (magnitude - deadzone) / (32767.0f - deadzone);
adjustedMagnitude = std::min(adjustedMagnitude, 1.0f);
float rawX = (stickX / 32767.0f) * adjustedMagnitude;
float rawY = (stickY / 32767.0f) * adjustedMagnitude;
// Use a quadratic curve for better fine control at low sensitivities
float sensitivityCurve = sensitivity * sensitivity;
float moveX = rawX * sensitivityCurve * 8.0f; // Reasonable base scale
float moveY = rawY * sensitivityCurve * 8.0f;
// Accumulate the movement with fractional precision
_analogueScrollAccumX += moveX;
_analogueScrollAccumY += moveY;
// Extract integer movement for this frame
float intPartX, intPartY;
float fracX = std::modf(_analogueScrollAccumX, &intPartX);
float fracY = std::modf(_analogueScrollAccumY, &intPartY);
int pixelsX = static_cast<int>(intPartX);
int pixelsY = static_cast<int>(intPartY);
_analogueScrollAccumX = fracX;
_analogueScrollAccumY = fracY;
_analogueScroll.x += pixelsX;
_analogueScroll.y += pixelsY;
}
}
}
}
void InputManager::updateAnalogueScroll()
{
_viewScroll.x += _analogueScroll.x;
_viewScroll.y += _analogueScroll.y;
}
2025-09-30 22:35:02 +02:00
void InputManager::process()
2020-12-14 23:12:19 +00:00
{
2025-09-30 22:35:02 +02:00
checkJoysticks();
processAnalogueInput();
2025-09-30 22:35:02 +02:00
handleModifiers();
processEvents();
processHoldEvents();
handleViewScrolling();
2020-12-14 23:12:19 +00:00
}
2025-09-30 22:35:02 +02:00
void InputManager::handleViewScrolling()
2020-12-14 23:12:19 +00:00
{
2025-03-05 21:45:04 +01:00
if (gLegacyScene == LegacyScene::titleSequence)
2020-12-14 23:12:19 +00:00
return;
auto& console = GetInGameConsole();
if (console.IsOpen())
return;
auto mainWindow = WindowGetMain();
// Handle gamepad analogue scrolling with cursor-based viewport targeting
if (_analogueScroll.x != 0 || _analogueScroll.y != 0)
2020-12-15 19:55:49 +00:00
{
// Get cursor position to find target viewport
const CursorState* cursorState = ContextGetCursorState();
Viewport* targetViewport = ViewportFindFromPoint(cursorState->position);
WindowBase* targetWindow = nullptr;
if (targetViewport != nullptr)
{
// Find the window that owns this viewport
auto* windowMgr = GetWindowManager();
targetWindow = windowMgr->GetOwner(targetViewport);
}
// Fallback to main window if no viewport found under cursor
if (targetWindow == nullptr)
{
targetWindow = mainWindow;
}
if (targetWindow != nullptr)
{
// Only unfollow sprites for the main window or viewport windows
// Don't unfollow for ride windows that might be following vehicles
if (targetWindow == mainWindow || targetWindow->classification == WindowClass::viewport)
{
WindowUnfollowSprite(*targetWindow);
}
InputScrollViewportSmooth(_analogueScroll, targetWindow);
}
}
// Handle keyboard shortcut scrolling with edge-based scrolling (but ignore gamepad input)
ScreenCoordsXY keyboardScroll = { _viewScroll.x - _analogueScroll.x, _viewScroll.y - _analogueScroll.y };
if (keyboardScroll.x != 0 || keyboardScroll.y != 0)
{
if (mainWindow != nullptr)
{
WindowUnfollowSprite(*mainWindow);
}
InputScrollViewport(keyboardScroll);
2020-12-15 19:55:49 +00:00
}
2020-12-14 23:12:19 +00:00
2020-12-15 19:55:49 +00:00
// Mouse edge scrolling
2025-10-07 20:14:37 +02:00
if (Config::Get().general.edgeScrolling)
2020-12-15 19:55:49 +00:00
{
if (InputGetState() != InputState::Normal)
2020-12-15 19:55:49 +00:00
return;
2020-12-14 23:12:19 +00:00
2025-09-30 22:35:02 +02:00
if (isModifierKeyPressed(ModifierKey::shift) || isModifierKeyPressed(ModifierKey::ctrl))
2020-12-15 19:55:49 +00:00
return;
GameHandleEdgeScroll();
}
2020-12-14 23:12:19 +00:00
}
2025-09-30 22:35:02 +02:00
void InputManager::handleModifiers()
2020-12-14 23:12:19 +00:00
{
_modifierKeyState = EnumValue(ModifierKey::none);
2020-12-14 23:12:19 +00:00
auto modifiers = SDL_GetModState();
if (modifiers & KMOD_SHIFT)
{
_modifierKeyState |= EnumValue(ModifierKey::shift);
2020-12-14 23:12:19 +00:00
}
if (modifiers & KMOD_CTRL)
{
_modifierKeyState |= EnumValue(ModifierKey::ctrl);
2020-12-14 23:12:19 +00:00
}
if (modifiers & KMOD_ALT)
{
_modifierKeyState |= EnumValue(ModifierKey::alt);
2020-12-14 23:12:19 +00:00
}
#ifdef __MACOSX__
if (modifiers & KMOD_GUI)
{
_modifierKeyState |= EnumValue(ModifierKey::cmd);
2020-12-14 23:12:19 +00:00
}
#endif
2025-10-07 20:14:37 +02:00
if (Config::Get().general.virtualFloorStyle != VirtualFloorStyles::Off)
2020-12-14 23:12:19 +00:00
{
2025-09-30 22:35:02 +02:00
if (isModifierKeyPressed(ModifierKey::ctrl) || isModifierKeyPressed(ModifierKey::shift))
VirtualFloorEnable();
2020-12-14 23:12:19 +00:00
else
VirtualFloorDisable();
2020-12-14 23:12:19 +00:00
}
}
2025-09-30 22:35:02 +02:00
bool InputManager::isModifierKeyPressed(ModifierKey modifier) const
{
return _modifierKeyState & EnumValue(modifier);
}
2025-09-30 22:35:02 +02:00
void InputManager::processEvents()
2020-12-13 03:40:10 +00:00
{
while (!_events.empty())
{
const auto& e = _events.front();
2025-09-30 22:35:02 +02:00
process(e);
2020-12-13 03:40:10 +00:00
_events.pop();
}
}
2025-09-30 22:35:02 +02:00
void InputManager::process(const InputEvent& e)
2020-12-13 03:40:10 +00:00
{
auto& shortcutManager = GetShortcutManager();
2025-09-30 22:27:30 +02:00
if (e.deviceKind == InputDeviceKind::keyboard)
2020-12-14 23:12:19 +00:00
{
2020-12-15 19:55:49 +00:00
auto& console = GetInGameConsole();
if (console.IsOpen())
2020-12-14 23:12:19 +00:00
{
2025-09-30 23:05:23 +02:00
if (!shortcutManager.processEventForSpecificShortcut(e, ShortcutId::kDebugToggleConsole))
2020-12-14 23:12:19 +00:00
{
2025-09-30 22:35:02 +02:00
processInGameConsole(e);
2020-12-15 19:55:49 +00:00
}
return;
}
2021-09-15 22:22:15 +02:00
if (gChatOpen)
2020-12-15 19:55:49 +00:00
{
2025-09-30 22:35:02 +02:00
processChat(e);
2020-12-15 19:55:49 +00:00
return;
}
2021-09-15 22:22:15 +02:00
2025-09-30 22:27:30 +02:00
if (e.deviceKind == InputDeviceKind::keyboard)
2020-12-15 19:55:49 +00:00
{
auto* windowMgr = GetWindowManager();
// TODO: replace with event
auto w = windowMgr->FindByClass(WindowClass::textinput);
2021-09-15 22:22:15 +02:00
if (w != nullptr)
2020-12-15 19:55:49 +00:00
{
2025-09-30 22:27:30 +02:00
if (e.state == InputEventState::release)
2020-12-14 23:12:19 +00:00
{
2025-09-30 22:27:30 +02:00
OpenRCT2::Ui::Windows::WindowTextInputKey(w, e.button);
2020-12-14 23:12:19 +00:00
}
2021-09-15 22:22:15 +02:00
return;
}
// TODO: replace with event
w = windowMgr->FindByClass(WindowClass::loadsaveOverwritePrompt);
if (w != nullptr)
{
2025-09-30 22:27:30 +02:00
if (e.state == InputEventState::release)
{
2025-09-30 22:27:30 +02:00
OpenRCT2::Ui::Windows::WindowLoadSaveOverwritePromptInputKey(w, e.button);
}
return;
}
// TODO: replace with event
w = windowMgr->FindByClass(WindowClass::loadsave);
if (w != nullptr)
{
2025-09-30 22:27:30 +02:00
if (e.state == InputEventState::release)
{
2025-09-30 22:27:30 +02:00
OpenRCT2::Ui::Windows::WindowLoadSaveInputKey(w, e.button);
}
return;
}
if (OpenRCT2::Ui::Windows::IsUsingWidgetTextBox())
2021-09-15 22:22:15 +02:00
{
return;
2020-12-14 23:12:19 +00:00
}
}
}
2025-09-30 23:05:23 +02:00
shortcutManager.processEvent(e);
2020-12-14 23:12:19 +00:00
}
2025-09-30 22:35:02 +02:00
void InputManager::processInGameConsole(const InputEvent& e)
2020-12-14 23:12:19 +00:00
{
2025-09-30 22:27:30 +02:00
if (e.deviceKind == InputDeviceKind::keyboard && e.state == InputEventState::release)
2020-12-14 23:12:19 +00:00
{
auto input = ConsoleInput::None;
2025-09-30 22:27:30 +02:00
switch (e.button)
2020-12-14 23:12:19 +00:00
{
case SDLK_ESCAPE:
input = ConsoleInput::LineClear;
break;
case SDLK_RETURN:
case SDLK_KP_ENTER:
input = ConsoleInput::LineExecute;
break;
case SDLK_UP:
input = ConsoleInput::HistoryPrevious;
break;
case SDLK_DOWN:
input = ConsoleInput::HistoryNext;
break;
case SDLK_PAGEUP:
input = ConsoleInput::ScrollPrevious;
break;
case SDLK_PAGEDOWN:
input = ConsoleInput::ScrollNext;
break;
}
if (input != ConsoleInput::None)
{
auto& console = GetInGameConsole();
console.Input(input);
}
}
}
2025-09-30 22:35:02 +02:00
void InputManager::processChat(const InputEvent& e)
2020-12-14 23:12:19 +00:00
{
2025-09-30 22:27:30 +02:00
if (e.deviceKind == InputDeviceKind::keyboard && e.state == InputEventState::down)
2020-12-14 23:12:19 +00:00
{
auto input = ChatInput::None;
2025-09-30 22:27:30 +02:00
switch (e.button)
2020-12-14 23:12:19 +00:00
{
case SDLK_ESCAPE:
input = ChatInput::Close;
break;
case SDLK_RETURN:
case SDLK_KP_ENTER:
input = ChatInput::Send;
break;
}
if (input != ChatInput::None)
{
2023-01-16 21:14:50 +01:00
ChatInput(input);
2020-12-14 23:12:19 +00:00
}
}
2020-12-13 03:40:10 +00:00
}
2025-09-30 22:35:02 +02:00
void InputManager::processHoldEvents()
2021-01-16 19:07:07 +00:00
{
// Get mouse state
_mouseState = SDL_GetMouseState(nullptr, nullptr);
// Get keyboard state
int numkeys;
auto keys = SDL_GetKeyboardState(&numkeys);
_keyboardState.resize(numkeys);
std::memcpy(_keyboardState.data(), keys, numkeys);
// Check view scroll shortcuts
_viewScroll.x = 0;
_viewScroll.y = 0;
2021-01-17 16:11:52 +00:00
2025-09-30 22:35:02 +02:00
if (!hasTextInputFocus())
2021-01-17 16:11:52 +00:00
{
auto& shortcutManager = GetShortcutManager();
2025-09-30 23:05:23 +02:00
if (!shortcutManager.isPendingShortcutChange())
{
2025-09-30 22:35:02 +02:00
processViewScrollEvent(ShortcutId::kViewScrollUp, { 0, -1 });
processViewScrollEvent(ShortcutId::kViewScrollDown, { 0, 1 });
processViewScrollEvent(ShortcutId::kViewScrollLeft, { -1, 0 });
processViewScrollEvent(ShortcutId::kViewScrollRight, { 1, 0 });
}
updateAnalogueScroll();
2021-01-17 16:11:52 +00:00
}
2021-01-16 19:07:07 +00:00
}
2025-09-30 22:35:02 +02:00
void InputManager::processViewScrollEvent(std::string_view shortcutId, const ScreenCoordsXY& delta)
2021-01-16 19:07:07 +00:00
{
auto& shortcutManager = GetShortcutManager();
2025-09-30 23:05:23 +02:00
auto shortcut = shortcutManager.getShortcut(shortcutId);
2025-09-30 22:35:02 +02:00
if (shortcut != nullptr && getState(*shortcut))
2021-01-16 19:07:07 +00:00
{
_viewScroll.x += delta.x;
_viewScroll.y += delta.y;
}
}
2025-09-30 22:35:02 +02:00
bool InputManager::getState(const RegisteredShortcut& shortcut) const
2021-01-16 19:07:07 +00:00
{
2025-09-30 22:57:43 +02:00
for (const auto& i : shortcut.current)
2021-01-16 19:07:07 +00:00
{
2025-09-30 22:35:02 +02:00
if (getState(i))
2021-01-16 19:07:07 +00:00
{
return true;
}
}
return false;
}
2025-09-30 22:35:02 +02:00
bool InputManager::getState(const ShortcutInput& shortcut) const
2021-01-16 19:07:07 +00:00
{
constexpr uint32_t kUsefulModifiers = KMOD_SHIFT | KMOD_CTRL | KMOD_ALT | KMOD_GUI;
auto modifiers = SDL_GetModState() & kUsefulModifiers;
2025-09-30 22:44:47 +02:00
if ((shortcut.modifiers & kUsefulModifiers) == modifiers)
2021-01-16 19:07:07 +00:00
{
2025-09-30 22:44:47 +02:00
switch (shortcut.kind)
2021-01-16 19:07:07 +00:00
{
2025-09-30 22:15:15 +02:00
case InputDeviceKind::mouse:
2021-01-16 19:07:07 +00:00
{
2025-09-30 22:44:47 +02:00
if (_mouseState & (1 << shortcut.button))
2021-01-16 19:07:07 +00:00
{
return true;
}
break;
}
2025-09-30 22:15:15 +02:00
case InputDeviceKind::keyboard:
2021-01-16 19:07:07 +00:00
{
2025-09-30 22:44:47 +02:00
auto scanCode = static_cast<size_t>(SDL_GetScancodeFromKey(shortcut.button));
if (scanCode < _keyboardState.size() && _keyboardState[scanCode])
2021-01-16 19:07:07 +00:00
{
return true;
}
break;
}
2025-09-30 22:15:15 +02:00
case InputDeviceKind::joyButton:
2021-01-16 19:07:07 +00:00
{
for (auto* gameController : _gameControllers)
2021-01-16 19:07:07 +00:00
{
// Get the underlying joystick to maintain compatibility with raw button numbers
auto* joystick = SDL_GameControllerGetJoystick(gameController);
2025-09-30 22:44:47 +02:00
if (joystick && SDL_JoystickGetButton(joystick, shortcut.button))
2021-01-16 19:07:07 +00:00
{
return true;
}
}
break;
}
2025-09-30 22:15:15 +02:00
case InputDeviceKind::joyHat:
2021-01-16 19:07:07 +00:00
{
for (auto* gameController : _gameControllers)
2021-01-16 19:07:07 +00:00
{
// Get the underlying joystick to maintain compatibility with hat functionality
auto* joystick = SDL_GameControllerGetJoystick(gameController);
if (joystick)
2021-01-16 19:07:07 +00:00
{
auto numHats = SDL_JoystickNumHats(joystick);
for (int i = 0; i < numHats; i++)
2021-01-16 19:07:07 +00:00
{
auto hat = SDL_JoystickGetHat(joystick, i);
2025-09-30 22:44:47 +02:00
if (hat & shortcut.button)
{
return true;
}
2021-01-16 19:07:07 +00:00
}
}
}
break;
}
2025-09-30 22:15:15 +02:00
case InputDeviceKind::joyAxis:
{
// analogue axes don't have a simple "pressed" state like buttons
// Return false for shortcuts on analogue axes as they're handled differently
return false;
}
2021-01-16 19:07:07 +00:00
}
}
return false;
}
2025-09-30 22:35:02 +02:00
bool InputManager::hasTextInputFocus() const
{
if (OpenRCT2::Ui::Windows::IsUsingWidgetTextBox() || gChatOpen)
return true;
auto* windowMgr = GetWindowManager();
auto w = windowMgr->FindByClass(WindowClass::textinput);
if (w != nullptr)
return true;
auto& console = GetInGameConsole();
if (console.IsOpen())
return true;
return false;
}