From e6b64030247ae247d13b33ea5b0406581eeadbf1 Mon Sep 17 00:00:00 2001 From: Ted John Date: Tue, 15 Dec 2020 19:55:49 +0000 Subject: [PATCH] Refactor and fix shortcut --- src/openrct2-ui/WindowManager.cpp | 3 - src/openrct2-ui/input/Input.cpp | 126 --------------------- src/openrct2-ui/input/Input.h | 12 -- src/openrct2-ui/input/InputManager.cpp | 82 ++++++++------ src/openrct2-ui/input/InputManager.h | 4 +- src/openrct2-ui/input/KeyboardShortcut.cpp | 5 +- src/openrct2-ui/input/ShortcutManager.cpp | 48 ++++++-- src/openrct2-ui/libopenrct2ui.vcxproj | 2 - 8 files changed, 94 insertions(+), 188 deletions(-) delete mode 100644 src/openrct2-ui/input/Input.cpp delete mode 100644 src/openrct2-ui/input/Input.h diff --git a/src/openrct2-ui/WindowManager.cpp b/src/openrct2-ui/WindowManager.cpp index f790258ab4..a393fefc8a 100644 --- a/src/openrct2-ui/WindowManager.cpp +++ b/src/openrct2-ui/WindowManager.cpp @@ -9,7 +9,6 @@ #include "WindowManager.h" -#include "input/Input.h" #include "input/KeyboardShortcuts.h" #include "interface/Theme.h" #include "windows/Window.h" @@ -517,8 +516,6 @@ public: void HandleKeyboard(bool isTitle) override { - InputHandleKeyboard(isTitle); - auto& inputManager = GetInputManager(); inputManager.Process(); } diff --git a/src/openrct2-ui/input/Input.cpp b/src/openrct2-ui/input/Input.cpp deleted file mode 100644 index 7e9a770d7d..0000000000 --- a/src/openrct2-ui/input/Input.cpp +++ /dev/null @@ -1,126 +0,0 @@ -/***************************************************************************** - * Copyright (c) 2014-2020 OpenRCT2 developers - * - * 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 "Input.h" - -#include "../UiContext.h" -#include "../interface/InGameConsole.h" -#include "KeyboardShortcuts.h" - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -using namespace OpenRCT2::Ui; - -static void InputHandleConsole(int32_t key) -{ - ConsoleInput input = ConsoleInput::None; - switch (key) - { - case SDL_SCANCODE_ESCAPE: - input = ConsoleInput::LineClear; - break; - case SDL_SCANCODE_RETURN: - case SDL_SCANCODE_KP_ENTER: - input = ConsoleInput::LineExecute; - break; - case SDL_SCANCODE_UP: - input = ConsoleInput::HistoryPrevious; - break; - case SDL_SCANCODE_DOWN: - input = ConsoleInput::HistoryNext; - break; - case SDL_SCANCODE_PAGEUP: - input = ConsoleInput::ScrollPrevious; - break; - case SDL_SCANCODE_PAGEDOWN: - input = ConsoleInput::ScrollNext; - break; - } - if (input != ConsoleInput::None) - { - auto& console = GetInGameConsole(); - console.Input(input); - } -} - -static void InputHandleChat(int32_t key) -{ - ChatInput input = ChatInput::None; - switch (key) - { - case SDL_SCANCODE_ESCAPE: - input = ChatInput::Close; - break; - case SDL_SCANCODE_RETURN: - case SDL_SCANCODE_KP_ENTER: - input = ChatInput::Send; - break; - } - if (input != ChatInput::None) - { - chat_input(input); - } -} - -static void GameHandleKeyScroll() -{ - rct_window* mainWindow; - - mainWindow = window_get_main(); - if (mainWindow == nullptr) - return; - if ((mainWindow->flags & WF_NO_SCROLLING) || (gScreenFlags & (SCREEN_FLAGS_TRACK_MANAGER | SCREEN_FLAGS_TITLE_DEMO))) - return; - if (mainWindow->viewport == nullptr) - return; - - rct_window* textWindow; - - textWindow = window_find_by_class(WC_TEXTINPUT); - if (textWindow || gUsingWidgetTextBox) - return; - if (gChatOpen) - return; - - const uint8_t* keysState = context_get_keys_state(); - auto scrollCoords = GetKeyboardMapScroll(keysState); - - if (scrollCoords.x != 0 || scrollCoords.y != 0) - { - window_unfollow_sprite(mainWindow); - } - InputScrollViewport(scrollCoords); -} - -static int32_t InputScancodeToRCTKeycode(int32_t sdl_key) -{ - char keycode = static_cast(SDL_GetKeyFromScancode(static_cast(sdl_key))); - - // Until we reshuffle the text files to use the new positions - // this will suffice to move the majority to the correct positions. - // Note any special buttons PgUp PgDwn are mapped wrong. - if (keycode >= 'a' && keycode <= 'z') - keycode = toupper(keycode); - - return keycode; -} - -void InputHandleKeyboard(bool isTitle) -{ -} diff --git a/src/openrct2-ui/input/Input.h b/src/openrct2-ui/input/Input.h deleted file mode 100644 index 1ce065075b..0000000000 --- a/src/openrct2-ui/input/Input.h +++ /dev/null @@ -1,12 +0,0 @@ -/***************************************************************************** - * Copyright (c) 2014-2020 OpenRCT2 developers - * - * 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. - *****************************************************************************/ - -#pragma once - -void InputHandleKeyboard(bool isTitle); diff --git a/src/openrct2-ui/input/InputManager.cpp b/src/openrct2-ui/input/InputManager.cpp index 7ba102866e..1fbf05c834 100644 --- a/src/openrct2-ui/input/InputManager.cpp +++ b/src/openrct2-ui/input/InputManager.cpp @@ -31,15 +31,12 @@ void InputManager::QueueInputEvent(InputEvent&& e) void InputManager::Process() { HandleModifiers(); - HandleMouseEdgeScrolling(); ProcessEvents(); + HandleViewScrolling(); } -void InputManager::HandleMouseEdgeScrolling() +void InputManager::HandleViewScrolling() { - if (!gConfigGeneral.edge_scrolling) - return; - if (gScreenFlags & SCREEN_FLAGS_TITLE_DEMO) return; @@ -47,13 +44,25 @@ void InputManager::HandleMouseEdgeScrolling() if (console.IsOpen()) return; - if (input_get_state() != InputState::Normal) - return; + // Shortcut scrolling + auto mainWindow = window_get_main(); + if (mainWindow != nullptr && _viewScroll.x != 0 || _viewScroll.y != 0) + { + window_unfollow_sprite(mainWindow); + } + InputScrollViewport(_viewScroll); - if (gInputPlaceObjectModifier & (PLACE_OBJECT_MODIFIER_SHIFT_Z | PLACE_OBJECT_MODIFIER_COPY_Z)) - return; + // Mouse edge scrolling + if (gConfigGeneral.edge_scrolling) + { + if (input_get_state() != InputState::Normal) + return; - GameHandleEdgeScroll(); + if (gInputPlaceObjectModifier & (PLACE_OBJECT_MODIFIER_SHIFT_Z | PLACE_OBJECT_MODIFIER_COPY_Z)) + return; + + GameHandleEdgeScroll(); + } } void InputManager::HandleModifiers() @@ -101,38 +110,43 @@ void InputManager::ProcessEvents() void InputManager::Process(const InputEvent& e) { auto& shortcutManager = GetShortcutManager(); - auto& console = GetInGameConsole(); - if (console.IsOpen()) + if (e.DeviceKind == InputDeviceKind::Keyboard) { - if (!shortcutManager.ProcessEventForSpecificShortcut(e, SHORTCUT_ID_DEBUG_CONSOLE)) + auto& console = GetInGameConsole(); + if (console.IsOpen()) { - ProcessInGameConsole(e); - } - } - else if (gChatOpen) - { - ProcessChat(e); - } - else - { - if (e.DeviceKind == InputDeviceKind::Keyboard) - { - auto w = window_find_by_class(WC_TEXTINPUT); - if (w != nullptr) + if (!shortcutManager.ProcessEventForSpecificShortcut(e, SHORTCUT_ID_DEBUG_CONSOLE)) { - if (e.State == InputEventState::Release) + ProcessInGameConsole(e); + } + return; + } + else if (gChatOpen) + { + ProcessChat(e); + return; + } + else + { + if (e.DeviceKind == InputDeviceKind::Keyboard) + { + auto w = window_find_by_class(WC_TEXTINPUT); + if (w != nullptr) { - window_text_input_key(w, e.Button); + if (e.State == InputEventState::Release) + { + window_text_input_key(w, e.Button); + } + return; + } + else if (gUsingWidgetTextBox) + { + return; } - return; - } - else if (!gUsingWidgetTextBox) - { - return; } } - shortcutManager.ProcessEvent(e); } + shortcutManager.ProcessEvent(e); } void InputManager::ProcessInGameConsole(const InputEvent& e) diff --git a/src/openrct2-ui/input/InputManager.h b/src/openrct2-ui/input/InputManager.h index 8700ffb8cd..67d9536d7e 100644 --- a/src/openrct2-ui/input/InputManager.h +++ b/src/openrct2-ui/input/InputManager.h @@ -9,6 +9,7 @@ #pragma once +#include #include namespace OpenRCT2::Ui @@ -38,8 +39,9 @@ namespace OpenRCT2::Ui { private: std::queue _events; + ScreenCoordsXY _viewScroll; - void HandleMouseEdgeScrolling(); + void HandleViewScrolling(); void HandleModifiers(); void ProcessEvents(); void Process(const InputEvent& e); diff --git a/src/openrct2-ui/input/KeyboardShortcut.cpp b/src/openrct2-ui/input/KeyboardShortcut.cpp index e11e71b71d..1b37aab851 100644 --- a/src/openrct2-ui/input/KeyboardShortcut.cpp +++ b/src/openrct2-ui/input/KeyboardShortcut.cpp @@ -707,7 +707,10 @@ void ShortcutManager::RegisterDefaultShortcuts() RegisterShortcut("multiplayer.chat", STR_SEND_MESSAGE, "C", []() { if (!(gScreenFlags & SCREEN_FLAGS_TITLE_DEMO)) { - chat_toggle(); + if (chat_available()) + { + chat_toggle(); + } } }); diff --git a/src/openrct2-ui/input/ShortcutManager.cpp b/src/openrct2-ui/input/ShortcutManager.cpp index 4574d9346f..d7de5d426d 100644 --- a/src/openrct2-ui/input/ShortcutManager.cpp +++ b/src/openrct2-ui/input/ShortcutManager.cpp @@ -111,7 +111,7 @@ ShortcutInput::ShortcutInput(const std::string_view& value) auto sepIndex = FindPlus(value, index); while (sepIndex != std::string::npos) { - auto text = value.substr(index, sepIndex); + auto text = value.substr(index, sepIndex - index); auto mod = ParseModifier(text); modifiers |= mod; index = sepIndex + 1; @@ -283,22 +283,52 @@ bool ShortcutInput::AppendModifier(std::string& s, const std::string_view& text, return false; } -bool ShortcutInput::Matches(const InputEvent& e) const +static bool HasModifier(uint32_t shortcut, uint32_t actual, uint32_t left, uint32_t right) { - auto modifiers = e.Modifiers & UsefulModifiers; - if (e.DeviceKind == InputDeviceKind::Mouse) + if (shortcut & (left | right)) { - if (Kind == ShortcutInputKind::Mouse && Modifiers == modifiers && Key == e.Button) + if ((shortcut & left) && (actual & left)) { return true; } - } - else if (e.DeviceKind == InputDeviceKind::Keyboard) - { - if (Kind == ShortcutInputKind::Keyboard && Modifiers == modifiers && Key == e.Button) + if ((shortcut & right) && (actual & right)) { return true; } + return false; + } + else if (actual & (left | right)) + { + return false; + } + return true; +} + +static bool CompareModifiers(uint32_t shortcut, uint32_t actual) +{ + shortcut &= UsefulModifiers; + return HasModifier(shortcut, actual, KMOD_LCTRL, KMOD_RCTRL) && HasModifier(shortcut, actual, KMOD_LSHIFT, KMOD_RSHIFT) + && HasModifier(shortcut, actual, KMOD_LALT, KMOD_RALT) && HasModifier(shortcut, actual, KMOD_LGUI, KMOD_RGUI); +} + +bool ShortcutInput::Matches(const InputEvent& e) const +{ + if (CompareModifiers(Modifiers, e.Modifiers)) + { + if (e.DeviceKind == InputDeviceKind::Mouse) + { + if (Kind == ShortcutInputKind::Mouse && Key == e.Button) + { + return true; + } + } + else if (e.DeviceKind == InputDeviceKind::Keyboard) + { + if (Kind == ShortcutInputKind::Keyboard && Key == e.Button) + { + return true; + } + } } return false; } diff --git a/src/openrct2-ui/libopenrct2ui.vcxproj b/src/openrct2-ui/libopenrct2ui.vcxproj index b2168611ac..5b8f36c976 100644 --- a/src/openrct2-ui/libopenrct2ui.vcxproj +++ b/src/openrct2-ui/libopenrct2ui.vcxproj @@ -40,7 +40,6 @@ - @@ -92,7 +91,6 @@ -