From 6bcb4d70e4b732189d899875acec4d2437a2287d Mon Sep 17 00:00:00 2001 From: staphen Date: Fri, 14 Apr 2023 22:46:58 -0400 Subject: [PATCH] Use padmapper bindings on spell icons --- Source/options.cpp | 28 +++++++++++++++++++++++++--- Source/options.h | 5 ++++- Source/panels/spell_list.cpp | 18 +++++++++++------- 3 files changed, 40 insertions(+), 11 deletions(-) diff --git a/Source/options.cpp b/Source/options.cpp index b65acbe71..b8a0a5dc4 100644 --- a/Source/options.cpp +++ b/Source/options.cpp @@ -1634,22 +1634,44 @@ void PadmapperOptions::Action::UpdateValueDescription() const boundInputDescriptionType = GamepadType; if (boundInput.button == ControllerButton_NONE) { boundInputDescription = ""; + boundInputShortDescription = ""; return; } string_view buttonName = ToString(boundInput.button); if (boundInput.modifier == ControllerButton_NONE) { boundInputDescription = std::string(buttonName); + boundInputShortDescription = std::string(Shorten(buttonName)); return; } string_view modifierName = ToString(boundInput.modifier); boundInputDescription = StrCat(modifierName, "+", buttonName); + boundInputShortDescription = StrCat(Shorten(modifierName), "+", Shorten(buttonName)); +} + +string_view PadmapperOptions::Action::Shorten(string_view buttonName) const +{ + size_t index = 0; + size_t chars = 0; + while (index < buttonName.size()) { + if (!IsTrailUtf8CodeUnit(buttonName[index])) + chars++; + if (chars == 3) + break; + index++; + } + return string_view(buttonName.data(), index); } string_view PadmapperOptions::Action::GetValueDescription() const +{ + return GetValueDescription(false); +} + +string_view PadmapperOptions::Action::GetValueDescription(bool useShortName) const { if (GamepadType != boundInputDescriptionType) UpdateValueDescription(); - return boundInputDescription; + return useShortName ? boundInputShortDescription : boundInputDescription; } bool PadmapperOptions::Action::SetValue(ControllerButtonCombo value) @@ -1738,11 +1760,11 @@ string_view PadmapperOptions::ActionNameTriggeredByButtonEvent(ControllerButtonE return releaseAction->key; } -string_view PadmapperOptions::InputNameForAction(string_view actionName) const +string_view PadmapperOptions::InputNameForAction(string_view actionName, bool useShortName) const { for (const Action &action : actions) { if (action.key == actionName && action.boundInput.button != ControllerButton_NONE) { - return action.GetValueDescription(); + return action.GetValueDescription(useShortName); } } return ""; diff --git a/Source/options.h b/Source/options.h index 9dcb854c7..55abc1c9f 100644 --- a/Source/options.h +++ b/Source/options.h @@ -729,6 +729,7 @@ struct PadmapperOptions : OptionCategoryBase { void SaveToIni(string_view category) const override; [[nodiscard]] string_view GetValueDescription() const override; + [[nodiscard]] string_view GetValueDescription(bool useShortName) const; bool SetValue(ControllerButtonCombo value); @@ -740,11 +741,13 @@ struct PadmapperOptions : OptionCategoryBase { ControllerButtonCombo boundInput {}; mutable GamepadLayout boundInputDescriptionType = GamepadLayout::Generic; mutable std::string boundInputDescription; + mutable std::string boundInputShortDescription; unsigned dynamicIndex; std::string dynamicKey; mutable std::string dynamicName; void UpdateValueDescription() const; + string_view Shorten(string_view buttonName) const; friend struct PadmapperOptions; }; @@ -764,7 +767,7 @@ struct PadmapperOptions : OptionCategoryBase { void ReleaseAllActiveButtons(); bool IsActive(string_view actionName) const; string_view ActionNameTriggeredByButtonEvent(ControllerButtonEvent ctrlEvent) const; - string_view InputNameForAction(string_view actionName) const; + string_view InputNameForAction(string_view actionName, bool useShortName = false) const; ControllerButtonCombo ButtonComboForAction(string_view actionName) const; private: diff --git a/Source/panels/spell_list.cpp b/Source/panels/spell_list.cpp index d9b0f5d51..2c4a06a59 100644 --- a/Source/panels/spell_list.cpp +++ b/Source/panels/spell_list.cpp @@ -3,6 +3,7 @@ #include #include "control.h" +#include "controls/plrctrls.h" #include "engine.h" #include "engine/backbuffer_state.hpp" #include "engine/palette.h" @@ -61,13 +62,15 @@ bool GetSpellListSelection(SpellID &pSpell, SpellType &pSplType) return false; } -std::optional GetHotkeyName(SpellID spellId, SpellType spellType) +std::optional GetHotkeyName(SpellID spellId, SpellType spellType, bool useShortName = false) { Player &myPlayer = *MyPlayer; for (size_t t = 0; t < NumHotkeys; t++) { if (myPlayer._pSplHotKey[t] != spellId || myPlayer._pSplTHotKey[t] != spellType) continue; auto quickSpellActionKey = StrCat("QuickSpell", t + 1); + if (ControlMode == ControlTypes::Gamepad) + return sgOptions.Padmapper.InputNameForAction(quickSpellActionKey, useShortName); return sgOptions.Keymapper.KeyNameForAction(quickSpellActionKey); } return {}; @@ -101,7 +104,7 @@ void DrawSpell(const Surface &out) const Point position = GetMainPanel().position + Displacement { 565, 119 }; DrawLargeSpellIcon(out, position, spl); - std::optional hotkeyName = GetHotkeyName(spl, myPlayer._pRSplType); + std::optional hotkeyName = GetHotkeyName(spl, myPlayer._pRSplType, true); if (hotkeyName) PrintSBookHotkey(out, position, *hotkeyName); } @@ -129,10 +132,10 @@ void DrawSpellList(const Surface &out) SetSpellTrans(transType); DrawLargeSpellIcon(out, spellListItem.location, spellId); - std::optional hotkeyName = GetHotkeyName(spellId, spellListItem.type); + std::optional shortHotkeyName = GetHotkeyName(spellId, spellListItem.type, true); - if (hotkeyName) - PrintSBookHotkey(out, spellListItem.location, *hotkeyName); + if (shortHotkeyName) + PrintSBookHotkey(out, spellListItem.location, *shortHotkeyName); if (!spellListItem.isSelected) continue; @@ -183,8 +186,9 @@ void DrawSpellList(const Surface &out) case SpellType::Invalid: break; } - if (hotkeyName) { - AddPanelString(fmt::format(fmt::runtime(_("Spell Hotkey {:s}")), *hotkeyName)); + std::optional fullHotkeyName = GetHotkeyName(spellId, spellListItem.type); + if (fullHotkeyName) { + AddPanelString(fmt::format(fmt::runtime(_("Spell Hotkey {:s}")), *fullHotkeyName)); } } }