From ff86ebbfe5875573c823008114387bd2be919898 Mon Sep 17 00:00:00 2001 From: Ted John Date: Mon, 8 Feb 2021 20:24:33 +0000 Subject: [PATCH] Localise shortcut strings --- data/language/en-GB.txt | 21 ++ src/openrct2-ui/input/ShortcutInput.cpp | 221 ++++++++++++---------- src/openrct2-ui/input/ShortcutManager.cpp | 2 +- src/openrct2-ui/input/ShortcutManager.h | 6 +- src/openrct2/localisation/Formatting.cpp | 1 + src/openrct2/localisation/StringIds.h | 25 +++ 6 files changed, 177 insertions(+), 99 deletions(-) diff --git a/data/language/en-GB.txt b/data/language/en-GB.txt index da0857b634..cfb4f194e2 100644 --- a/data/language/en-GB.txt +++ b/data/language/en-GB.txt @@ -3665,6 +3665,27 @@ STR_6408 :Please install “innoextract” to extract GOG Installer, then res STR_6409 :The selected file is not the offline GOG Installer for RollerCoaster Tycoon 2. You may have downloaded the GOG Galaxy downloader stub or selected the wrong file. STR_6410 :Zoom in/out STR_6411 :Show buttons for zooming in and out in the toolbar +STR_6412 :NumPad Return +STR_6413 :SHIFT +STR_6414 :LSHIFT +STR_6415 :RSHIFT +STR_6416 :CTRL +STR_6417 :LCTRL +STR_6418 :RCTRL +STR_6419 :ALT +STR_6420 :LALT +STR_6421 :RALT +STR_6422 :GUI +STR_6423 :LGUI +STR_6424 :RGUI +STR_6425 :JOY LEFT +STR_6426 :JOY RIGHT +STR_6427 :JOY UP +STR_6428 :JOY DOWN +STR_6429 :JOY {INT32} +STR_6430 :LMB +STR_6431 :RMB +STR_6432 :MOUSE {INT32} ############# # Scenarios # diff --git a/src/openrct2-ui/input/ShortcutInput.cpp b/src/openrct2-ui/input/ShortcutInput.cpp index 8b3c01863e..2b57feaf5e 100644 --- a/src/openrct2-ui/input/ShortcutInput.cpp +++ b/src/openrct2-ui/input/ShortcutInput.cpp @@ -12,7 +12,11 @@ #include #include #include +#include +#include +#include +using namespace OpenRCT2; using namespace OpenRCT2::Ui; constexpr uint32_t UsefulModifiers = KMOD_SHIFT | KMOD_CTRL | KMOD_ALT | KMOD_GUI; @@ -187,102 +191,128 @@ ShortcutInput::ShortcutInput(std::string_view value) } } +std::string_view ShortcutInput::GetModifierName(uint32_t key, bool localised) +{ + static std::unordered_map> keys{ + { KMOD_SHIFT, { "SHIFT", STR_SHORTCUT_MOD_SHIFT } }, { KMOD_LSHIFT, { "LSHIFT", STR_SHORTCUT_MOD_LSHIFT } }, + { KMOD_RSHIFT, { "RSHIFT", STR_SHORTCUT_MOD_RSHIFT } }, { KMOD_CTRL, { "CTRL", STR_SHORTCUT_MOD_CTRL } }, + { KMOD_LCTRL, { "LCTRL", STR_SHORTCUT_MOD_LCTRL } }, { KMOD_RCTRL, { "RCTRL", STR_SHORTCUT_MOD_RCTRL } }, + { KMOD_ALT, { "ALT", STR_SHORTCUT_MOD_ALT } }, { KMOD_LALT, { "LALT", STR_SHORTCUT_MOD_LALT } }, + { KMOD_RALT, { "RALT", STR_SHORTCUT_MOD_RALT } }, { KMOD_GUI, { "GUI", STR_SHORTCUT_MOD_GUI } }, + { KMOD_LGUI, { "LGUI", STR_SHORTCUT_MOD_LGUI } }, { KMOD_RGUI, { "RGUI", STR_SHORTCUT_MOD_RGUI } }, + }; + + auto r = keys.find(key); + if (r != keys.end()) + { + if (localised && r->second.second != STR_NONE) + { + return language_get_string(r->second.second); + } + else + { + return r->second.first; + } + } + else + { + return {}; + } +} + +std::string_view ShortcutInput::GetKeyName(uint32_t key, bool localised) +{ + static std::unordered_map> keys{ + { SDLK_LEFT, { "LEFT", STR_SHORTCUT_LEFT } }, + { SDLK_RIGHT, { "RIGHT", STR_SHORTCUT_RIGHT } }, + { SDLK_UP, { "UP", STR_SHORTCUT_UP } }, + { SDLK_DOWN, { "DOWN", STR_SHORTCUT_DOWN } }, + { SDLK_BACKSPACE, { "BACKSPACE", STR_SHORTCUT_BACKSPACE } }, + { SDLK_ESCAPE, { "ESCAPE", STR_SHORTCUT_ESCAPE } }, + { SDLK_SPACE, { "SPACE", STR_SHORTCUT_SPACEBAR } }, + { SDLK_TAB, { "TAB", STR_SHORTCUT_TAB } }, + { SDLK_RETURN, { "RETURN", STR_SHORTCUT_RETURN } }, + { SDLK_PAGEUP, { "PAGE UP", STR_SHORTCUT_PGUP } }, + { SDLK_PAGEDOWN, { "PAGE DOWN", STR_SHORTCUT_PGDN } }, + { SDLK_INSERT, { "INSERT", STR_SHORTCUT_INSERT } }, + { SDLK_DELETE, { "DELETE", STR_SHORTCUT_DELETE } }, + { SDLK_KP_DIVIDE, { "NUMPAD /", STR_SHORTCUT_NUMPAD_DIVIDE } }, + { SDLK_KP_MULTIPLY, { "NUMPAD *", STR_SHORTCUT_NUMPAD_MULTIPLY } }, + { SDLK_KP_MINUS, { "NUMPAD -", STR_SHORTCUT_NUMPAD_MINUS } }, + { SDLK_KP_PLUS, { "NUMPAD +", STR_SHORTCUT_NUMPAD_PLUS } }, + { SDLK_KP_ENTER, { "NUMPAD RETURN", STR_SHORTCUT_NUMPAD_RETURN } }, + { SDLK_KP_1, { "NUMPAD 1", STR_SHORTCUT_NUMPAD_1 } }, + { SDLK_KP_2, { "NUMPAD 2", STR_SHORTCUT_NUMPAD_2 } }, + { SDLK_KP_3, { "NUMPAD 3", STR_SHORTCUT_NUMPAD_3 } }, + { SDLK_KP_4, { "NUMPAD 4", STR_SHORTCUT_NUMPAD_4 } }, + { SDLK_KP_5, { "NUMPAD 5", STR_SHORTCUT_NUMPAD_5 } }, + { SDLK_KP_6, { "NUMPAD 6", STR_SHORTCUT_NUMPAD_6 } }, + { SDLK_KP_7, { "NUMPAD 7", STR_SHORTCUT_NUMPAD_7 } }, + { SDLK_KP_8, { "NUMPAD 8", STR_SHORTCUT_NUMPAD_8 } }, + { SDLK_KP_9, { "NUMPAD 9", STR_SHORTCUT_NUMPAD_9 } }, + { SDLK_KP_0, { "NUMPAD 0", STR_SHORTCUT_NUMPAD_0 } }, + { SDLK_KP_PERIOD, { "NUMPAD .", STR_SHORTCUT_NUMPAD_PERIOD } }, + { SDLK_CAPSLOCK, { "CAPSLOCK", STR_SHORTCUT_NUMPAD_PERIOD } }, + }; + + auto r = keys.find(key); + if (r != keys.end()) + { + if (localised && r->second.second != STR_NONE) + { + return language_get_string(r->second.second); + } + else + { + return r->second.first; + } + } + else + { + return {}; + } +} + std::string ShortcutInput::ToString() const +{ + return ToString(false); +} + +std::string ShortcutInput::ToLocalisedString() const +{ + return ToString(true); +} + +std::string ShortcutInput::ToString(bool localised) const { 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); + AppendModifier(result, KMOD_LSHIFT, KMOD_RSHIFT, localised); + AppendModifier(result, KMOD_LCTRL, KMOD_RCTRL, localised); + AppendModifier(result, KMOD_LALT, KMOD_RALT, localised); + AppendModifier(result, KMOD_LGUI, KMOD_RGUI, localised); if (Kind == InputDeviceKind::Keyboard) { - switch (Button) + if (Button != 0) { - case 0: - break; - case SDLK_BACKSPACE: - result += "BACKSPACE"; - break; - case SDLK_ESCAPE: - result += "ESCAPE"; - break; - case SDLK_SPACE: - result += "SPACE"; - break; - case SDLK_TAB: - result += "TAB"; - break; - case SDLK_RETURN: - result += "RETURN"; - break; - case SDLK_PAGEUP: - result += "PAGE UP"; - break; - case SDLK_PAGEDOWN: - result += "PAGE DOWN"; - break; - - case SDLK_KP_DIVIDE: - result += "NUMPAD /"; - break; - case SDLK_KP_MULTIPLY: - result += "NUMPAD *"; - break; - case SDLK_KP_MINUS: - result += "NUMPAD -"; - break; - case SDLK_KP_PLUS: - result += "NUMPAD +"; - break; - case SDLK_KP_ENTER: - result += "NUMPAD RETURN"; - break; - case SDLK_KP_1: - result += "NUMPAD 1"; - break; - case SDLK_KP_2: - result += "NUMPAD 2"; - break; - case SDLK_KP_3: - result += "NUMPAD 3"; - break; - case SDLK_KP_4: - result += "NUMPAD 4"; - break; - case SDLK_KP_5: - result += "NUMPAD 5"; - break; - case SDLK_KP_6: - result += "NUMPAD 6"; - break; - case SDLK_KP_7: - result += "NUMPAD 7"; - break; - case SDLK_KP_8: - result += "NUMPAD 8"; - break; - case SDLK_KP_9: - result += "NUMPAD 9"; - break; - case SDLK_KP_0: - result += "NUMPAD 0"; - break; - case SDLK_KP_PERIOD: - result += "NUMPAD ."; - break; - - default: + auto text = GetKeyName(Button, localised); + if (text.empty()) + { if (Button & SDLK_SCANCODE_MASK) { - auto name = SDL_GetScancodeName(static_cast(Button & ~SDLK_SCANCODE_MASK)); + auto name = SDL_GetKeyName(Button); + // auto name = SDL_GetScancodeName(static_cast(Button & ~SDLK_SCANCODE_MASK)); result += name; } else { String::AppendCodepoint(result, std::toupper(Button)); } - break; + } + else + { + result += text; + } } } else if (Kind == InputDeviceKind::Mouse) @@ -290,57 +320,54 @@ std::string ShortcutInput::ToString() const switch (Button) { case 0: - result += "LMB"; + result += localised ? FormatStringId(STR_SHORTCUT_MOUSE_LEFT, Button + 1) : "LMB"; break; case 1: - result += "RMB"; + result += localised ? FormatStringId(STR_SHORTCUT_MOUSE_RIGHT, Button + 1) : "RMB"; break; default: - result += "MOUSE "; - result += std::to_string(Button + 1); + result += localised ? FormatStringId(STR_SHORTCUT_MOUSE_NUMBER, Button + 1) + : "MOUSE " + std::to_string(Button + 1); break; } } else if (Kind == InputDeviceKind::JoyButton) { - result += "JOY "; - result += std::to_string(Button + 1); + result += localised ? FormatStringId(STR_SHORTCUT_JOY_NUMBER, Button + 1) : "JOY " + std::to_string(Button + 1); } else if (Kind == InputDeviceKind::JoyHat) { if (Button & SDL_HAT_LEFT) - result += "JOY LEFT"; + result += localised ? language_get_string(STR_SHORTCUT_JOY_LEFT) : "JOY LEFT"; else if (Button & SDL_HAT_RIGHT) - result += "JOY RIGHT"; + result += localised ? language_get_string(STR_SHORTCUT_JOY_RIGHT) : "JOY RIGHT"; else if (Button & SDL_HAT_UP) - result += "JOY UP"; + result += localised ? language_get_string(STR_SHORTCUT_JOY_UP) : "JOY UP"; else if (Button & SDL_HAT_DOWN) - result += "JOY DOWN"; + result += localised ? language_get_string(STR_SHORTCUT_JOY_DOWN) : "JOY DOWN"; else result += "JOY ?"; } return result; } -bool ShortcutInput::AppendModifier(std::string& s, std::string_view text, uint32_t left, uint32_t right) const +bool ShortcutInput::AppendModifier(std::string& s, uint32_t left, uint32_t right, bool localised) const { if ((Modifiers & (left | right)) == (left | right)) { - s += text; + s += GetModifierName(left | right, localised); s += "+"; return true; } else if (Modifiers & left) { - s += "L"; - s += text; + s += GetModifierName(left, localised); s += "+"; return true; } else if (Modifiers & right) { - s += "R"; - s += text; + s += GetModifierName(right, localised); s += "+"; return true; } diff --git a/src/openrct2-ui/input/ShortcutManager.cpp b/src/openrct2-ui/input/ShortcutManager.cpp index af131a7cbd..937dc89885 100644 --- a/src/openrct2-ui/input/ShortcutManager.cpp +++ b/src/openrct2-ui/input/ShortcutManager.cpp @@ -98,7 +98,7 @@ std::string RegisteredShortcut::GetDisplayString() const for (size_t i = 0; i < numChords; i++) { const auto& kc = Current[i]; - result += kc.ToString(); + result += kc.ToLocalisedString(); if (i < numChords - 1) { // TODO localise... diff --git a/src/openrct2-ui/input/ShortcutManager.h b/src/openrct2-ui/input/ShortcutManager.h index d20ec57aea..eca9567ed6 100644 --- a/src/openrct2-ui/input/ShortcutManager.h +++ b/src/openrct2-ui/input/ShortcutManager.h @@ -38,13 +38,17 @@ namespace OpenRCT2::Ui ShortcutInput() = default; ShortcutInput(std::string_view value); std::string ToString() const; + std::string ToLocalisedString() const; bool Matches(const InputEvent& e) const; static std::optional FromInputEvent(const InputEvent& e); private: - bool AppendModifier(std::string& s, std::string_view text, uint32_t left, uint32_t right) const; + bool AppendModifier(std::string& s, uint32_t left, uint32_t right, bool localised) const; + static std::string_view GetModifierName(uint32_t key, bool localised); + static std::string_view GetKeyName(uint32_t key, bool localised); + std::string ToString(bool localised) const; }; class RegisteredShortcut diff --git a/src/openrct2/localisation/Formatting.cpp b/src/openrct2/localisation/Formatting.cpp index 57d2e0073d..2417bb406e 100644 --- a/src/openrct2/localisation/Formatting.cpp +++ b/src/openrct2/localisation/Formatting.cpp @@ -615,6 +615,7 @@ namespace OpenRCT2 template void FormatArgument(FormatBuffer&, FormatToken, int16_t); template void FormatArgument(FormatBuffer&, FormatToken, int32_t); template void FormatArgument(FormatBuffer&, FormatToken, int64_t); + template void FormatArgument(FormatBuffer&, FormatToken, uint32_t); template void FormatArgument(FormatBuffer&, FormatToken, uint64_t); template void FormatArgument(FormatBuffer&, FormatToken, const char*); diff --git a/src/openrct2/localisation/StringIds.h b/src/openrct2/localisation/StringIds.h index 64a1003e09..b8d7875761 100644 --- a/src/openrct2/localisation/StringIds.h +++ b/src/openrct2/localisation/StringIds.h @@ -3921,6 +3921,31 @@ enum STR_ZOOM_BUTTON_ON_TOOLBAR = 6410, STR_ZOOM_BUTTON_ON_TOOLBAR_TIP = 6411, + STR_SHORTCUT_NUMPAD_RETURN = 6412, + + STR_SHORTCUT_MOD_SHIFT = 6413, + STR_SHORTCUT_MOD_LSHIFT = 6414, + STR_SHORTCUT_MOD_RSHIFT = 6415, + STR_SHORTCUT_MOD_CTRL = 6416, + STR_SHORTCUT_MOD_LCTRL = 6417, + STR_SHORTCUT_MOD_RCTRL = 6418, + STR_SHORTCUT_MOD_ALT = 6419, + STR_SHORTCUT_MOD_LALT = 6420, + STR_SHORTCUT_MOD_RALT = 6421, + STR_SHORTCUT_MOD_GUI = 6422, + STR_SHORTCUT_MOD_LGUI = 6423, + STR_SHORTCUT_MOD_RGUI = 6424, + + STR_SHORTCUT_JOY_LEFT = 6425, + STR_SHORTCUT_JOY_RIGHT = 6426, + STR_SHORTCUT_JOY_UP = 6427, + STR_SHORTCUT_JOY_DOWN = 6428, + STR_SHORTCUT_JOY_NUMBER = 6429, + + STR_SHORTCUT_MOUSE_LEFT = 6430, + STR_SHORTCUT_MOUSE_RIGHT = 6431, + STR_SHORTCUT_MOUSE_NUMBER = 6432, + // Have to include resource strings (from scenarios and objects) for the time being now that language is partially working /* MAX_STR_COUNT = 32768 */ // MAX_STR_COUNT - upper limit for number of strings, not the current count strings };