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

446 lines
14 KiB
C++
Raw Normal View History

2020-07-13 23:12:49 +01:00
/*****************************************************************************
* Copyright (c) 2014-2024 OpenRCT2 developers
2020-07-13 23:12:49 +01: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 "ShortcutManager.h"
2021-01-16 17:39:34 +00:00
#include "ShortcutIds.h"
2020-07-13 23:12:49 +01:00
#include <SDL.h>
2021-01-12 22:34:30 +00:00
#include <openrct2/Context.h>
#include <openrct2/PlatformEnvironment.h>
2021-02-18 18:05:53 +00:00
#include <openrct2/core/Console.hpp>
2021-01-12 22:34:30 +00:00
#include <openrct2/core/DataSerialiser.h>
2021-01-17 15:21:17 +00:00
#include <openrct2/core/FileStream.h>
2021-01-12 22:34:30 +00:00
#include <openrct2/core/FileSystem.hpp>
2021-01-14 22:58:48 +00:00
#include <openrct2/core/Json.hpp>
2020-07-15 00:40:29 +01:00
#include <openrct2/core/String.hpp>
2020-12-13 03:40:10 +00:00
#include <openrct2/interface/Window.h>
2020-07-13 23:12:49 +01:00
#include <openrct2/localisation/Language.h>
2020-07-15 00:40:29 +01:00
using namespace OpenRCT2::Ui;
2021-01-13 01:57:43 +00:00
std::string_view RegisteredShortcut::GetTopLevelGroup() const
2020-07-15 00:40:29 +01:00
{
auto fullstopIndex = Id.find('.');
if (fullstopIndex != std::string::npos)
{
return std::string_view(Id.c_str(), fullstopIndex);
}
return {};
}
2021-01-13 01:57:43 +00:00
std::string_view RegisteredShortcut::GetGroup() const
{
auto fullstopIndex = Id.find_last_of('.');
if (fullstopIndex != std::string::npos)
{
return std::string_view(Id.c_str(), fullstopIndex);
}
return {};
}
2020-12-13 13:50:41 +00:00
bool RegisteredShortcut::Matches(const InputEvent& e) const
{
if (IsSuitableInputEvent(e))
{
auto result = std::find_if(
Current.begin(), Current.end(), [e](const ShortcutInput& action) { return action.Matches(e); });
return result != Current.end();
}
return false;
}
bool RegisteredShortcut::IsSuitableInputEvent(const InputEvent& e) const
{
2020-12-15 23:14:40 +00:00
// Do not intercept button releases
if (e.State == InputEventState::Release)
{
return false;
}
2020-12-13 13:50:41 +00:00
if (e.DeviceKind == InputDeviceKind::Mouse)
{
// Do not allow LMB or RMB to be shortcut
if (e.Button == 0 || e.Button == 1)
{
return false;
}
}
2020-12-13 22:51:50 +00:00
else if (e.DeviceKind == InputDeviceKind::Keyboard)
{
2020-12-15 23:14:40 +00:00
// Do not allow modifier keys alone
switch (e.Button)
2020-12-13 22:51:50 +00:00
{
2020-12-15 23:14:40 +00:00
case SDLK_LCTRL:
case SDLK_RCTRL:
case SDLK_LSHIFT:
case SDLK_RSHIFT:
case SDLK_LALT:
case SDLK_RALT:
case SDLK_LGUI:
case SDLK_RGUI:
return false;
2020-12-13 22:51:50 +00:00
}
}
2020-12-15 23:14:40 +00:00
2020-12-13 13:50:41 +00:00
return true;
}
2021-01-16 18:04:58 +00:00
std::string RegisteredShortcut::GetDisplayString() const
{
std::string result;
auto numChords = Current.size();
for (size_t i = 0; i < numChords; i++)
{
const auto& kc = Current[i];
2021-02-08 20:24:33 +00:00
result += kc.ToLocalisedString();
2021-01-16 18:04:58 +00:00
if (i < numChords - 1)
{
// TODO localise...
result += " or ";
}
}
return result;
}
2021-01-12 22:34:30 +00:00
ShortcutManager::ShortcutManager(const std::shared_ptr<IPlatformEnvironment>& env)
: _env(env)
2020-07-15 00:40:29 +01:00
{
RegisterDefaultShortcuts();
}
2020-07-13 23:12:49 +01:00
void ShortcutManager::RegisterShortcut(RegisteredShortcut&& shortcut)
{
2020-12-16 00:15:20 +00:00
if (!shortcut.Id.empty() && GetShortcut(shortcut.Id) == nullptr)
{
auto id = std::make_unique<std::string>(shortcut.Id);
auto idView = std::string_view(*id);
_ids.push_back(std::move(id));
2022-05-27 08:18:13 +09:00
shortcut.OrderIndex = Shortcuts.size();
Shortcuts[idView] = shortcut;
2020-12-16 00:15:20 +00:00
}
2020-07-13 23:12:49 +01:00
}
2020-07-15 00:40:29 +01:00
2020-12-12 17:53:02 +00:00
RegisteredShortcut* ShortcutManager::GetShortcut(std::string_view id)
{
auto result = Shortcuts.find(id);
return result == Shortcuts.end() ? nullptr : &result->second;
2020-12-12 17:53:02 +00:00
}
2020-12-16 00:15:20 +00:00
void ShortcutManager::RemoveShortcut(std::string_view id)
{
Shortcuts.erase(id);
_ids.erase(
std::remove_if(_ids.begin(), _ids.end(), [id](const std::unique_ptr<std::string>& x) { return *x == id; }), _ids.end());
2020-12-16 00:15:20 +00:00
}
2021-01-17 16:11:52 +00:00
bool ShortcutManager::IsPendingShortcutChange() const
{
return !_pendingShortcutChange.empty();
}
2020-12-12 17:53:02 +00:00
void ShortcutManager::SetPendingShortcutChange(std::string_view id)
{
2020-12-13 03:40:10 +00:00
_pendingShortcutChange = id;
}
void ShortcutManager::ProcessEvent(const InputEvent& e)
{
2021-01-17 16:11:52 +00:00
if (!IsPendingShortcutChange())
2020-12-13 03:40:10 +00:00
{
for (const auto& shortcut : Shortcuts)
{
if (shortcut.second.Matches(e))
2020-12-13 03:40:10 +00:00
{
shortcut.second.Action();
2020-12-13 03:40:10 +00:00
}
}
}
2020-12-13 13:50:41 +00:00
else
2020-12-13 03:40:10 +00:00
{
auto shortcut = GetShortcut(_pendingShortcutChange);
2020-12-13 13:50:41 +00:00
if (shortcut != nullptr && shortcut->IsSuitableInputEvent(e))
2020-12-13 03:40:10 +00:00
{
auto shortcutInput = ShortcutInput::FromInputEvent(e);
2021-09-13 19:47:13 +03:00
if (shortcutInput.has_value())
2020-12-13 03:40:10 +00:00
{
shortcut->Current.clear();
2021-09-13 19:47:13 +03:00
shortcut->Current.push_back(std::move(shortcutInput.value()));
2020-12-13 03:40:10 +00:00
}
2020-12-13 13:50:41 +00:00
_pendingShortcutChange.clear();
WindowCloseByClass(WindowClass::ChangeKeyboardShortcut);
2021-01-16 18:04:58 +00:00
SaveUserBindings();
2020-12-13 03:40:10 +00:00
}
}
2020-12-12 17:53:02 +00:00
}
2020-12-14 23:12:19 +00:00
bool ShortcutManager::ProcessEventForSpecificShortcut(const InputEvent& e, std::string_view id)
{
auto shortcut = GetShortcut(id);
if (shortcut != nullptr && shortcut->Matches(e))
{
shortcut->Action();
return true;
}
return false;
}
2021-01-12 22:34:30 +00:00
void ShortcutManager::LoadUserBindings()
2020-07-15 00:40:29 +01:00
{
2021-01-12 22:34:30 +00:00
try
{
2022-03-18 23:21:23 +01:00
auto path = fs::u8path(_env->GetFilePath(PATHID::CONFIG_SHORTCUTS));
2021-01-12 22:34:30 +00:00
if (fs::exists(path))
{
LoadUserBindings(path);
}
else
{
try
{
2021-02-18 18:05:53 +00:00
Console::WriteLine("Importing legacy shortcuts...");
2022-03-18 23:21:23 +01:00
auto legacyPath = fs::u8path(_env->GetFilePath(PATHID::CONFIG_SHORTCUTS_LEGACY));
2021-01-12 22:34:30 +00:00
if (fs::exists(legacyPath))
{
LoadLegacyBindings(legacyPath);
SaveUserBindings();
2021-02-18 18:05:53 +00:00
Console::WriteLine("Legacy shortcuts imported");
2021-01-12 22:34:30 +00:00
}
}
catch (const std::exception& e)
{
2021-02-18 18:05:53 +00:00
Console::Error::WriteLine("Unable to import legacy shortcut bindings: %s", e.what());
2021-01-12 22:34:30 +00:00
}
}
}
catch (const std::exception& e)
{
2021-02-18 18:05:53 +00:00
Console::Error::WriteLine("Unable to load shortcut bindings: %s", e.what());
2021-01-12 22:34:30 +00:00
}
}
std::optional<ShortcutInput> ShortcutManager::ConvertLegacyBinding(uint16_t binding)
{
constexpr uint16_t nullBinding = 0xFFFF;
constexpr uint16_t shift = 0x100;
constexpr uint16_t ctrl = 0x200;
constexpr uint16_t alt = 0x400;
constexpr uint16_t cmd = 0x800;
if (binding == nullBinding)
{
2021-09-13 23:24:57 +02:00
return std::nullopt;
2021-01-12 22:34:30 +00:00
}
2021-09-13 23:25:33 +02:00
ShortcutInput result;
result.Kind = InputDeviceKind::Keyboard;
if (binding & shift)
result.Modifiers |= KMOD_SHIFT;
if (binding & ctrl)
result.Modifiers |= KMOD_CTRL;
if (binding & alt)
result.Modifiers |= KMOD_ALT;
if (binding & cmd)
result.Modifiers |= KMOD_GUI;
result.Button = SDL_GetKeyFromScancode(static_cast<SDL_Scancode>(binding & 0xFF));
return result;
2021-01-12 22:34:30 +00:00
}
void ShortcutManager::LoadLegacyBindings(const fs::path& path)
{
constexpr int32_t SUPPORTED_FILE_VERSION = 1;
2021-01-17 15:21:17 +00:00
constexpr int32_t MAX_LEGACY_SHORTCUTS = 85;
2021-01-12 22:34:30 +00:00
2021-01-17 15:21:17 +00:00
auto fs = FileStream(path, FILE_MODE_OPEN);
auto version = fs.ReadValue<uint16_t>();
if (version == SUPPORTED_FILE_VERSION)
2021-01-12 22:34:30 +00:00
{
2021-01-17 15:21:17 +00:00
for (size_t i = 0; i < MAX_LEGACY_SHORTCUTS; i++)
2021-01-12 22:34:30 +00:00
{
2021-01-17 15:21:17 +00:00
auto value = fs.ReadValue<uint16_t>();
auto shortcutId = GetLegacyShortcutId(i);
if (!shortcutId.empty())
2021-01-12 22:34:30 +00:00
{
2021-01-17 15:21:17 +00:00
auto shortcut = GetShortcut(shortcutId);
if (shortcut != nullptr)
2021-01-12 22:34:30 +00:00
{
2021-01-17 15:21:17 +00:00
shortcut->Current.clear();
auto input = ConvertLegacyBinding(value);
2021-09-13 19:47:13 +03:00
if (input.has_value())
2021-01-12 22:34:30 +00:00
{
2021-09-13 19:47:13 +03:00
shortcut->Current.push_back(std::move(input.value()));
2021-01-12 22:34:30 +00:00
}
}
}
}
}
}
void ShortcutManager::LoadUserBindings(const fs::path& path)
{
auto root = Json::ReadFromFile(path.u8string());
2021-01-14 22:58:48 +00:00
if (root.is_object())
{
for (auto it = root.begin(); it != root.end(); ++it)
{
const auto& key = it.key();
const auto& value = it.value();
const auto& shortcut = GetShortcut(key);
2021-01-16 18:04:58 +00:00
if (shortcut != nullptr)
2021-01-14 22:58:48 +00:00
{
2021-01-16 18:04:58 +00:00
shortcut->Current.clear();
if (value.is_string())
2021-01-14 22:58:48 +00:00
{
2021-01-16 18:04:58 +00:00
shortcut->Current.emplace_back(value.get<std::string>());
}
else if (value.is_array())
{
for (auto& subValue : value)
{
shortcut->Current.emplace_back(subValue.get<std::string>());
}
2021-01-14 22:58:48 +00:00
}
}
}
}
2021-01-12 22:34:30 +00:00
}
void ShortcutManager::SaveUserBindings()
{
try
{
2022-03-18 23:21:23 +01:00
auto path = fs::u8path(_env->GetFilePath(PATHID::CONFIG_SHORTCUTS));
2021-01-12 22:34:30 +00:00
SaveUserBindings(path);
}
catch (const std::exception& e)
{
2021-02-18 18:05:53 +00:00
Console::Error::WriteLine("Unable to save shortcut bindings: %s", e.what());
2021-01-12 22:34:30 +00:00
}
}
void ShortcutManager::SaveUserBindings(const fs::path& path)
{
2021-01-14 22:58:48 +00:00
json_t root;
if (fs::exists(path))
{
root = Json::ReadFromFile(path.u8string());
2021-01-14 22:58:48 +00:00
}
for (const auto& shortcut : Shortcuts)
{
auto& jShortcut = root[shortcut.second.Id];
if (shortcut.second.Current.size() == 1)
2021-01-14 22:58:48 +00:00
{
jShortcut = shortcut.second.Current[0].ToString();
2021-01-14 22:58:48 +00:00
}
else
{
jShortcut = nlohmann::json::array();
for (const auto& binding : shortcut.second.Current)
2021-01-14 22:58:48 +00:00
{
jShortcut.push_back(binding.ToString());
}
}
}
Json::WriteToFile(path.u8string(), root);
2021-01-12 22:34:30 +00:00
}
std::string_view ShortcutManager::GetLegacyShortcutId(size_t index)
{
2021-11-24 17:08:41 +00:00
static constexpr std::string_view _legacyMap[] = {
ShortcutId::kInterfaceCloseTop,
ShortcutId::kInterfaceCloseAll,
ShortcutId::kInterfaceCancelConstruction,
ShortcutId::kInterfacePause,
ShortcutId::kViewGeneralZoomOut,
ShortcutId::kViewGeneralZoomIn,
ShortcutId::kViewGeneralRotateClockwise,
ShortcutId::kViewGeneralRotateAnticlockwise,
ShortcutId::kInterfaceRotateConstruction,
ShortcutId::kViewToggleUnderground,
ShortcutId::kViewToggleBaseLand,
ShortcutId::kViewToggleVerticalLand,
ShortcutId::kViewToggleRides,
ShortcutId::kViewToggleScenery,
ShortcutId::kViewToggleSupports,
ShortcutId::kViewToggleGuests,
ShortcutId::kViewToggleLandHeightMarkers,
ShortcutId::kViewToggleTrackHeightMarkers,
ShortcutId::kViewToggleFootpathHeightMarkers,
ShortcutId::kInterfaceOpenLand,
ShortcutId::kInterfaceOpenWater,
ShortcutId::kInterfaceOpenScenery,
ShortcutId::kInterfaceOpenFootpaths,
ShortcutId::kInterfaceOpenNewRide,
ShortcutId::kInterfaceOpenFinances,
ShortcutId::kInterfaceOpenResearch,
ShortcutId::kInterfaceOpenRides,
ShortcutId::kInterfaceOpenPark,
ShortcutId::kInterfaceOpenGuests,
ShortcutId::kInterfaceOpenStaff,
ShortcutId::kInterfaceOpenMessages,
ShortcutId::kInterfaceOpenMap,
ShortcutId::kInterfaceScreenshot,
ShortcutId::kInterfaceDecreaseSpeed,
ShortcutId::kInterfaceIncreaseSpeed,
ShortcutId::kInterfaceOpenCheats,
ShortcutId::kInterfaceToggleToolbars,
ShortcutId::kViewScrollUp,
ShortcutId::kViewScrollLeft,
ShortcutId::kViewScrollDown,
ShortcutId::kViewScrollRight,
ShortcutId::kInterfaceMultiplayerChat,
ShortcutId::kInterfaceSaveGame,
ShortcutId::kInterfaceShowOptions,
ShortcutId::kInterfaceMute,
ShortcutId::kInterfaceScaleToggleWindowMode,
ShortcutId::kInterfaceMultiplayerShow,
2021-01-31 03:12:19 +00:00
std::string_view(),
ShortcutId::kDebugTogglePaintDebugWindow,
ShortcutId::kViewToggleFootpaths,
ShortcutId::kWindowRideConstructionTurnLeft,
ShortcutId::kWindowRideConstructionTurnRight,
ShortcutId::kWindowRideConstructionDefault,
ShortcutId::kWindowRideConstructionSlopeDown,
ShortcutId::kWindowRideConstructionSlopeUp,
ShortcutId::kWindowRideConstructionChainLift,
ShortcutId::kWindowRideConstructionBankLeft,
ShortcutId::kWindowRideConstructionBankRight,
ShortcutId::kWindowRideConstructionPrevious,
ShortcutId::kWindowRideConstructionNext,
ShortcutId::kWindowRideConstructionBuild,
ShortcutId::kWindowRideConstructionDemolish,
ShortcutId::kInterfaceLoadGame,
ShortcutId::kInterfaceClearScenery,
ShortcutId::kViewToggleGridlines,
ShortcutId::kViewToggleCutAway,
ShortcutId::kViewToggleFootpathIssues,
ShortcutId::kInterfaceOpenTileInspector,
ShortcutId::kDebugAdvanceTick,
ShortcutId::kInterfaceSceneryPicker,
ShortcutId::kInterfaceScaleIncrease,
ShortcutId::kInterfaceScaleDecrease,
ShortcutId::kWindowTileInspectorToggleInvisibility,
ShortcutId::kWindowTileInspectorCopy,
ShortcutId::kWindowTileInspectorPaste,
ShortcutId::kWindowTileInspectorRemove,
ShortcutId::kWindowTileInspectorMoveUp,
ShortcutId::kWindowTileInspectorMoveDown,
ShortcutId::kWindowTileInspectorIncreaseX,
ShortcutId::kWindowTileInspectorDecreaseX,
ShortcutId::kWindowTileInspectorIncreaseY,
ShortcutId::kWindowTileInspectorDecreaseY,
ShortcutId::kWindowTileInspectorIncreaseHeight,
ShortcutId::kWindowTileInspectorDecreaseHeight,
ShortcutId::kInterfaceDisableClearance,
2021-01-12 22:34:30 +00:00
};
2021-11-24 17:08:41 +00:00
return index < std::size(_legacyMap) ? _legacyMap[index] : std::string_view();
2020-07-15 00:40:29 +01:00
}