1
0
mirror of https://github.com/OpenRCT2/OpenRCT2 synced 2026-01-20 05:23:04 +01:00

Start work on new shortcut engine

This commit is contained in:
Ted John
2020-07-13 23:12:49 +01:00
parent 345c436f19
commit 28aead5cb5
4 changed files with 172 additions and 0 deletions

View File

@@ -8,6 +8,7 @@
*****************************************************************************/
#include "KeyboardShortcuts.h"
#include "ShortcutManager.h"
#include <iterator>
#include <openrct2-ui/interface/Viewport.h>
@@ -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
}

View File

@@ -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 <SDL.h>
#include <openrct2/localisation/Language.h>
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<SDL_Scancode>(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);
}

View File

@@ -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 <cstdint>
#include <functional>
#include <openrct2/localisation/StringIds.h>
#include <string>
#include <string_view>
#include <vector>
class RegisteredShortcut
{
public:
std::string Id;
rct_string_id LocalisedName = STR_NONE;
uint32_t Modifiers{};
uint32_t Key{};
std::function<void()> Action;
RegisteredShortcut() = default;
RegisteredShortcut(
const std::string_view& id, rct_string_id localisedName, const std::string_view& shortcut,
const std::function<void()>& 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<RegisteredShortcut> Shortcuts;
void RegisterShortcut(RegisteredShortcut&& shortcut);
void RegisterDefaultShortcuts();
};

View File

@@ -42,6 +42,7 @@
<ClInclude Include="drawing\engines\opengl\TransparencyDepth.h" />
<ClInclude Include="input\Input.h" />
<ClInclude Include="input\KeyboardShortcuts.h" />
<ClInclude Include="input\ShortcutManager.h" />
<ClInclude Include="interface\Dropdown.h" />
<ClInclude Include="interface\Graph.h" />
<ClInclude Include="interface\InGameConsole.h" />
@@ -94,6 +95,7 @@
<ClCompile Include="input\KeyboardShortcut.cpp" />
<ClCompile Include="input\KeyboardShortcuts.cpp" />
<ClCompile Include="input\MouseInput.cpp" />
<ClCompile Include="input\ShortcutManager.cpp" />
<ClCompile Include="interface\Graph.cpp" />
<ClCompile Include="interface\InGameConsole.cpp" />
<ClCompile Include="interface\LandTool.cpp" />