1
0
mirror of https://github.com/OpenRCT2/OpenRCT2 synced 2026-01-15 11:03:00 +01:00

Rename members of ShortcutInput

This commit is contained in:
Gymnasiast
2025-09-30 22:44:47 +02:00
parent b1a93b264c
commit a288a91331
4 changed files with 100 additions and 100 deletions

View File

@@ -504,13 +504,13 @@ bool InputManager::getState(const ShortcutInput& shortcut) const
{
constexpr uint32_t kUsefulModifiers = KMOD_SHIFT | KMOD_CTRL | KMOD_ALT | KMOD_GUI;
auto modifiers = SDL_GetModState() & kUsefulModifiers;
if ((shortcut.Modifiers & kUsefulModifiers) == modifiers)
if ((shortcut.modifiers & kUsefulModifiers) == modifiers)
{
switch (shortcut.Kind)
switch (shortcut.kind)
{
case InputDeviceKind::mouse:
{
if (_mouseState & (1 << shortcut.Button))
if (_mouseState & (1 << shortcut.button))
{
return true;
}
@@ -518,7 +518,7 @@ bool InputManager::getState(const ShortcutInput& shortcut) const
}
case InputDeviceKind::keyboard:
{
auto scanCode = static_cast<size_t>(SDL_GetScancodeFromKey(shortcut.Button));
auto scanCode = static_cast<size_t>(SDL_GetScancodeFromKey(shortcut.button));
if (scanCode < _keyboardState.size() && _keyboardState[scanCode])
{
return true;
@@ -531,7 +531,7 @@ bool InputManager::getState(const ShortcutInput& shortcut) const
{
// Get the underlying joystick to maintain compatibility with raw button numbers
auto* joystick = SDL_GameControllerGetJoystick(gameController);
if (joystick && SDL_JoystickGetButton(joystick, shortcut.Button))
if (joystick && SDL_JoystickGetButton(joystick, shortcut.button))
{
return true;
}
@@ -550,7 +550,7 @@ bool InputManager::getState(const ShortcutInput& shortcut) const
for (int i = 0; i < numHats; i++)
{
auto hat = SDL_JoystickGetHat(joystick, i);
if (hat & shortcut.Button)
if (hat & shortcut.button)
{
return true;
}

View File

@@ -107,14 +107,14 @@ static size_t FindPlus(std::string_view s, size_t index)
ShortcutInput::ShortcutInput(std::string_view value)
{
uint32_t modifiers = 0;
uint32_t newModifiers = 0;
size_t index = 0;
auto sepIndex = FindPlus(value, index);
while (sepIndex != std::string::npos)
{
auto text = value.substr(index, sepIndex - index);
auto mod = ParseModifier(text);
modifiers |= mod;
newModifiers |= mod;
index = sepIndex + 1;
sepIndex = FindPlus(value, index);
}
@@ -125,36 +125,36 @@ ShortcutInput::ShortcutInput(std::string_view value)
rem = rem.substr(4);
if (String::equals(rem, "LEFT"))
{
Kind = InputDeviceKind::joyHat;
Modifiers = modifiers;
Button = SDL_HAT_LEFT;
kind = InputDeviceKind::joyHat;
modifiers = newModifiers;
button = SDL_HAT_LEFT;
}
else if (String::equals(rem, "RIGHT"))
{
Kind = InputDeviceKind::joyHat;
Modifiers = modifiers;
Button = SDL_HAT_RIGHT;
kind = InputDeviceKind::joyHat;
modifiers = newModifiers;
button = SDL_HAT_RIGHT;
}
else if (String::equals(rem, "UP"))
{
Kind = InputDeviceKind::joyHat;
Modifiers = modifiers;
Button = SDL_HAT_UP;
kind = InputDeviceKind::joyHat;
modifiers = newModifiers;
button = SDL_HAT_UP;
}
else if (String::equals(rem, "DOWN"))
{
Kind = InputDeviceKind::joyHat;
Modifiers = modifiers;
Button = SDL_HAT_DOWN;
kind = InputDeviceKind::joyHat;
modifiers = newModifiers;
button = SDL_HAT_DOWN;
}
else
{
auto number = String::Parse<int32_t>(rem);
if (number.has_value())
{
Kind = InputDeviceKind::joyButton;
Modifiers = modifiers;
Button = number.value() - 1;
kind = InputDeviceKind::joyButton;
modifiers = newModifiers;
button = number.value() - 1;
}
}
}
@@ -164,32 +164,32 @@ ShortcutInput::ShortcutInput(std::string_view value)
auto number = String::Parse<int32_t>(rem);
if (number)
{
Kind = InputDeviceKind::mouse;
Modifiers = modifiers;
Button = *number - 1;
kind = InputDeviceKind::mouse;
modifiers = newModifiers;
button = *number - 1;
}
}
else if (String::iequals(rem, "LMB"))
{
Kind = InputDeviceKind::mouse;
Modifiers = modifiers;
Button = 0;
kind = InputDeviceKind::mouse;
modifiers = newModifiers;
button = 0;
}
else if (String::iequals(rem, "RMB"))
{
Kind = InputDeviceKind::mouse;
Modifiers = modifiers;
Button = 1;
kind = InputDeviceKind::mouse;
modifiers = newModifiers;
button = 1;
}
else
{
Kind = InputDeviceKind::keyboard;
Modifiers = modifiers;
Button = ParseKey(rem);
kind = InputDeviceKind::keyboard;
modifiers = newModifiers;
button = ParseKey(rem);
}
}
std::string_view ShortcutInput::GetModifierName(uint32_t key, bool localised)
std::string_view ShortcutInput::getModifierName(uint32_t key, bool localised)
{
static std::unordered_map<uint32_t, std::pair<const char*, StringId>> _keys{
{ KMOD_SHIFT, { "SHIFT", STR_SHORTCUT_MOD_SHIFT } }, { KMOD_LSHIFT, { "LSHIFT", STR_SHORTCUT_MOD_LSHIFT } },
@@ -214,7 +214,7 @@ std::string_view ShortcutInput::GetModifierName(uint32_t key, bool localised)
return {};
}
std::string_view ShortcutInput::GetLocalisedKeyName(uint32_t key)
std::string_view ShortcutInput::getLocalisedKeyName(uint32_t key)
{
static std::unordered_map<uint32_t, StringId> _keys{
{ SDLK_LEFT, STR_SHORTCUT_LEFT },
@@ -270,75 +270,75 @@ std::string_view ShortcutInput::GetLocalisedKeyName(uint32_t key)
return {};
}
std::string ShortcutInput::ToString() const
std::string ShortcutInput::toString() const
{
return ToString(false);
return toString(false);
}
std::string ShortcutInput::ToLocalisedString() const
std::string ShortcutInput::toLocalisedString() const
{
return ToString(true);
return toString(true);
}
std::string ShortcutInput::ToString(bool localised) const
std::string ShortcutInput::toString(bool localised) const
{
std::string result;
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);
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)
if (kind == InputDeviceKind::keyboard)
{
if (Button != 0)
if (button != 0)
{
if (localised)
{
auto name = GetLocalisedKeyName(Button);
auto name = getLocalisedKeyName(button);
if (!name.empty())
{
result += name;
}
else
{
result += SDL_GetKeyName(Button);
result += SDL_GetKeyName(button);
}
}
else
{
result += SDL_GetKeyName(Button);
result += SDL_GetKeyName(button);
}
}
}
else if (Kind == InputDeviceKind::mouse)
else if (kind == InputDeviceKind::mouse)
{
switch (Button)
switch (button)
{
case 0:
result += localised ? FormatStringID(STR_SHORTCUT_MOUSE_LEFT, Button + 1) : "LMB";
result += localised ? FormatStringID(STR_SHORTCUT_MOUSE_LEFT, button + 1) : "LMB";
break;
case 1:
result += localised ? FormatStringID(STR_SHORTCUT_MOUSE_RIGHT, Button + 1) : "RMB";
result += localised ? FormatStringID(STR_SHORTCUT_MOUSE_RIGHT, button + 1) : "RMB";
break;
default:
result += localised ? FormatStringID(STR_SHORTCUT_MOUSE_NUMBER, Button + 1)
: "MOUSE " + 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)
else if (kind == InputDeviceKind::joyButton)
{
result += localised ? FormatStringID(STR_SHORTCUT_JOY_NUMBER, Button + 1) : "JOY " + 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)
else if (kind == InputDeviceKind::joyHat)
{
if (Button & SDL_HAT_LEFT)
if (button & SDL_HAT_LEFT)
result += localised ? LanguageGetString(STR_SHORTCUT_JOY_LEFT) : "JOY LEFT";
else if (Button & SDL_HAT_RIGHT)
else if (button & SDL_HAT_RIGHT)
result += localised ? LanguageGetString(STR_SHORTCUT_JOY_RIGHT) : "JOY RIGHT";
else if (Button & SDL_HAT_UP)
else if (button & SDL_HAT_UP)
result += localised ? LanguageGetString(STR_SHORTCUT_JOY_UP) : "JOY UP";
else if (Button & SDL_HAT_DOWN)
else if (button & SDL_HAT_DOWN)
result += localised ? LanguageGetString(STR_SHORTCUT_JOY_DOWN) : "JOY DOWN";
else
result += "JOY ?";
@@ -346,23 +346,23 @@ std::string ShortcutInput::ToString(bool localised) const
return result;
}
bool ShortcutInput::AppendModifier(std::string& s, uint32_t left, uint32_t right, bool localised) const
bool ShortcutInput::appendModifier(std::string& s, uint32_t left, uint32_t right, bool localised) const
{
if ((Modifiers & (left | right)) == (left | right))
if ((modifiers & (left | right)) == (left | right))
{
s += GetModifierName(left | right, localised);
s += getModifierName(left | right, localised);
s += "+";
return true;
}
if (Modifiers & left)
if (modifiers & left)
{
s += GetModifierName(left, localised);
s += getModifierName(left, localised);
s += "+";
return true;
}
if (Modifiers & right)
if (modifiers & right)
{
s += GetModifierName(right, localised);
s += getModifierName(right, localised);
s += "+";
return true;
}
@@ -397,11 +397,11 @@ static bool CompareModifiers(uint32_t shortcut, uint32_t actual)
&& HasModifier(shortcut, actual, KMOD_LALT, KMOD_RALT) && HasModifier(shortcut, actual, KMOD_LGUI, KMOD_RGUI);
}
bool ShortcutInput::Matches(const InputEvent& e) const
bool ShortcutInput::matches(const InputEvent& e) const
{
if (CompareModifiers(Modifiers, e.modifiers))
if (CompareModifiers(modifiers, e.modifiers))
{
if (e.deviceKind == Kind && Button == e.button)
if (e.deviceKind == kind && button == e.button)
{
return true;
}
@@ -409,7 +409,7 @@ bool ShortcutInput::Matches(const InputEvent& e) const
return false;
}
std::optional<ShortcutInput> ShortcutInput::FromInputEvent(const InputEvent& e)
std::optional<ShortcutInput> ShortcutInput::fromInputEvent(const InputEvent& e)
{
// Assume any side modifier (more specific configurations can be done by manually editing config file)
auto modifiers = e.modifiers & kUsefulModifiers;
@@ -422,8 +422,8 @@ std::optional<ShortcutInput> ShortcutInput::FromInputEvent(const InputEvent& e)
}
ShortcutInput result;
result.Kind = e.deviceKind;
result.Modifiers = modifiers;
result.Button = e.button;
result.kind = e.deviceKind;
result.modifiers = modifiers;
result.button = e.button;
return result;
}

View File

@@ -50,7 +50,7 @@ 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); });
Current.begin(), Current.end(), [e](const ShortcutInput& action) { return action.matches(e); });
return result != Current.end();
}
return false;
@@ -99,7 +99,7 @@ std::string RegisteredShortcut::GetDisplayString() const
for (size_t i = 0; i < numChords; i++)
{
const auto& kc = Current[i];
result += kc.ToLocalisedString();
result += kc.toLocalisedString();
if (i < numChords - 1)
{
result += " ";
@@ -168,7 +168,7 @@ void ShortcutManager::ProcessEvent(const InputEvent& e)
auto shortcut = GetShortcut(_pendingShortcutChange);
if (shortcut != nullptr && shortcut->IsSuitableInputEvent(e))
{
auto shortcutInput = ShortcutInput::FromInputEvent(e);
auto shortcutInput = ShortcutInput::fromInputEvent(e);
if (shortcutInput.has_value())
{
shortcut->Current.clear();
@@ -242,16 +242,16 @@ std::optional<ShortcutInput> ShortcutManager::ConvertLegacyBinding(uint16_t bind
}
ShortcutInput result;
result.Kind = InputDeviceKind::keyboard;
result.kind = InputDeviceKind::keyboard;
if (binding & kShift)
result.Modifiers |= KMOD_SHIFT;
result.modifiers |= KMOD_SHIFT;
if (binding & kCtrl)
result.Modifiers |= KMOD_CTRL;
result.modifiers |= KMOD_CTRL;
if (binding & kAlt)
result.Modifiers |= KMOD_ALT;
result.modifiers |= KMOD_ALT;
if (binding & kCmd)
result.Modifiers |= KMOD_GUI;
result.Button = SDL_GetKeyFromScancode(static_cast<SDL_Scancode>(binding & 0xFF));
result.modifiers |= KMOD_GUI;
result.button = SDL_GetKeyFromScancode(static_cast<SDL_Scancode>(binding & 0xFF));
return result;
}
@@ -341,14 +341,14 @@ void ShortcutManager::SaveUserBindings(const fs::path& path)
auto& jShortcut = root[shortcut.second.Id];
if (shortcut.second.Current.size() == 1)
{
jShortcut = shortcut.second.Current[0].ToString();
jShortcut = shortcut.second.Current[0].toString();
}
else
{
jShortcut = nlohmann::json::array();
for (const auto& binding : shortcut.second.Current)
{
jShortcut.push_back(binding.ToString());
jShortcut.push_back(binding.toString());
}
}
}

View File

@@ -33,24 +33,24 @@ namespace OpenRCT2::Ui
struct ShortcutInput
{
public:
InputDeviceKind Kind{};
uint32_t Modifiers{};
uint32_t Button{};
InputDeviceKind kind{};
uint32_t modifiers{};
uint32_t button{};
ShortcutInput() = default;
ShortcutInput(std::string_view value);
std::string ToString() const;
std::string ToLocalisedString() const;
std::string toString() const;
std::string toLocalisedString() const;
bool Matches(const InputEvent& e) const;
bool matches(const InputEvent& e) const;
static std::optional<ShortcutInput> FromInputEvent(const InputEvent& e);
static std::optional<ShortcutInput> fromInputEvent(const InputEvent& e);
private:
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 GetLocalisedKeyName(uint32_t key);
std::string ToString(bool localised) 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 getLocalisedKeyName(uint32_t key);
std::string toString(bool localised) const;
};
class RegisteredShortcut