From 28aead5cb584cb6fd25729d2b6f3d5f00c443f81 Mon Sep 17 00:00:00 2001 From: Ted John Date: Mon, 13 Jul 2020 23:12:49 +0100 Subject: [PATCH] Start work on new shortcut engine --- src/openrct2-ui/input/KeyboardShortcut.cpp | 55 ++++++++++++++++++ src/openrct2-ui/input/ShortcutManager.cpp | 65 ++++++++++++++++++++++ src/openrct2-ui/input/ShortcutManager.h | 50 +++++++++++++++++ src/openrct2-ui/libopenrct2ui.vcxproj | 2 + 4 files changed, 172 insertions(+) create mode 100644 src/openrct2-ui/input/ShortcutManager.cpp create mode 100644 src/openrct2-ui/input/ShortcutManager.h diff --git a/src/openrct2-ui/input/KeyboardShortcut.cpp b/src/openrct2-ui/input/KeyboardShortcut.cpp index be39752fbb..4ae4a1f8ca 100644 --- a/src/openrct2-ui/input/KeyboardShortcut.cpp +++ b/src/openrct2-ui/input/KeyboardShortcut.cpp @@ -8,6 +8,7 @@ *****************************************************************************/ #include "KeyboardShortcuts.h" +#include "ShortcutManager.h" #include #include @@ -1131,3 +1132,57 @@ namespace } // anonymous namespace #pragma endregion + +void ShortcutManager::RegisterDefaultShortcuts() +{ + // clang-format off + RegisterShortcut(RegisteredShortcut("interface.close_top", STR_SHORTCUT_CLOSE_TOP_MOST_WINDOW, "BACKSPACE", []() { window_close_top(); })); + RegisterShortcut(RegisteredShortcut("interface.close_all", STR_SHORTCUT_CLOSE_ALL_FLOATING_WINDOWS, "SHIFT+BACKSPACE", []() { + if (!(gScreenFlags & SCREEN_FLAGS_SCENARIO_EDITOR)) + window_close_all(); + else if (gS6Info.editor_step == EDITOR_STEP_LANDSCAPE_EDITOR) + window_close_top(); + })); + RegisterShortcut(RegisteredShortcut("interface.cancel_construction", STR_SHORTCUT_CANCEL_CONSTRUCTION_MODE, "ESCAPE", []() { + if (gScreenFlags & SCREEN_FLAGS_TITLE_DEMO) + return; + + rct_window* window = window_find_by_class(WC_ERROR); + if (window != nullptr) + window_close(window); + else if (input_test_flag(INPUT_FLAG_TOOL_ACTIVE)) + tool_cancel(); + })); + RegisterShortcut(RegisteredShortcut("interface.pause", STR_SHORTCUT_PAUSE_GAME, "PAUSE", []() { + if (!(gScreenFlags & (SCREEN_FLAGS_TITLE_DEMO | SCREEN_FLAGS_SCENARIO_EDITOR | SCREEN_FLAGS_TRACK_MANAGER))) + { + rct_window* window = window_find_by_class(WC_TOP_TOOLBAR); + if (window != nullptr) + { + window->Invalidate(); + window_event_mouse_up_call(window, WC_TOP_TOOLBAR__WIDX_PAUSE); + } + } + })); + RegisterShortcut(RegisteredShortcut("interface.zoom_out", STR_SHORTCUT_ZOOM_VIEW_OUT, "PAGEUP", []() { + main_window_zoom(false, false); + })); + RegisterShortcut(RegisteredShortcut("interface.zoom_in", STR_SHORTCUT_ZOOM_VIEW_IN, "PAGEDOWN", []() { + main_window_zoom(true, false); + })); + RegisterShortcut(RegisteredShortcut("interface.rotate_clockwise", STR_SHORTCUT_ROTATE_VIEW_CLOCKWISE, "RETURN", []() { + if (gScreenFlags & SCREEN_FLAGS_TITLE_DEMO) + return; + + rct_window* w = window_get_main(); + window_rotate_camera(w, 1); + })); + RegisterShortcut(RegisteredShortcut("interface.rotate_anticlockwise", STR_SHORTCUT_ROTATE_VIEW_ANTICLOCKWISE, "SHIFT+RETURN", []() { + if (gScreenFlags & SCREEN_FLAGS_TITLE_DEMO) + return; + + rct_window* w = window_get_main(); + window_rotate_camera(w, -1); + })); + // clang-format on +} diff --git a/src/openrct2-ui/input/ShortcutManager.cpp b/src/openrct2-ui/input/ShortcutManager.cpp new file mode 100644 index 0000000000..0433ed6747 --- /dev/null +++ b/src/openrct2-ui/input/ShortcutManager.cpp @@ -0,0 +1,65 @@ +/***************************************************************************** + * 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 "ShortcutManager.h" + +#include +#include + +std::string RegisteredShortcut::GetKeyText() +{ + std::string result; + AppendModifier(result, "SHIFT", KMOD_LSHIFT, KMOD_RSHIFT); + AppendModifier(result, "CTRL", KMOD_LCTRL, KMOD_RCTRL); + AppendModifier(result, "ALT", KMOD_LALT, KMOD_RALT); + AppendModifier(result, "GUI", KMOD_LGUI, KMOD_RGUI); + + if (Key & SDLK_SCANCODE_MASK) + { + auto name = SDL_GetScancodeName(static_cast(Key & ~SDLK_SCANCODE_MASK)); + result += name; + } + else + { + char buffer[8]{}; + utf8_write_codepoint(buffer, Key); + result += buffer; + } + return result; +} + +bool RegisteredShortcut::AppendModifier(std::string& s, const std::string_view& text, uint32_t left, uint32_t right) +{ + if ((Modifiers & (left | right)) == (left | right)) + { + s += text; + s += "+"; + return true; + } + else if (Modifiers & left) + { + s += "L"; + s += text; + s += "+"; + return true; + } + else if (Modifiers & right) + { + s += "R"; + s += text; + s += "+"; + return true; + } + return false; +} + +void ShortcutManager::RegisterShortcut(RegisteredShortcut&& shortcut) +{ + Shortcuts.push_back(shortcut); +} diff --git a/src/openrct2-ui/input/ShortcutManager.h b/src/openrct2-ui/input/ShortcutManager.h new file mode 100644 index 0000000000..525fd84498 --- /dev/null +++ b/src/openrct2-ui/input/ShortcutManager.h @@ -0,0 +1,50 @@ +/***************************************************************************** + * 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 + +#include +#include +#include +#include +#include +#include + +class RegisteredShortcut +{ +public: + std::string Id; + rct_string_id LocalisedName = STR_NONE; + uint32_t Modifiers{}; + uint32_t Key{}; + std::function Action; + + RegisteredShortcut() = default; + RegisteredShortcut( + const std::string_view& id, rct_string_id localisedName, const std::string_view& shortcut, + const std::function& action) + : Id(id) + , LocalisedName(localisedName) + , Action(action) + { + } + + std::string GetKeyText(); + +private: + bool AppendModifier(std::string& s, const std::string_view& text, uint32_t left, uint32_t right); +}; + +class ShortcutManager +{ + std::vector Shortcuts; + + void RegisterShortcut(RegisteredShortcut&& shortcut); + void RegisterDefaultShortcuts(); +}; diff --git a/src/openrct2-ui/libopenrct2ui.vcxproj b/src/openrct2-ui/libopenrct2ui.vcxproj index 54276d1838..4ae8ece945 100644 --- a/src/openrct2-ui/libopenrct2ui.vcxproj +++ b/src/openrct2-ui/libopenrct2ui.vcxproj @@ -42,6 +42,7 @@ + @@ -94,6 +95,7 @@ +