Browse Source

Use padmapper bindings on spell icons

pull/5978/head
staphen 3 years ago committed by Anders Jenbo
parent
commit
6bcb4d70e4
  1. 28
      Source/options.cpp
  2. 5
      Source/options.h
  3. 18
      Source/panels/spell_list.cpp

28
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 "";

5
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:

18
Source/panels/spell_list.cpp

@ -3,6 +3,7 @@
#include <fmt/format.h>
#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<string_view> GetHotkeyName(SpellID spellId, SpellType spellType)
std::optional<string_view> 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<string_view> hotkeyName = GetHotkeyName(spl, myPlayer._pRSplType);
std::optional<string_view> 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<string_view> hotkeyName = GetHotkeyName(spellId, spellListItem.type);
std::optional<string_view> 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<string_view> fullHotkeyName = GetHotkeyName(spellId, spellListItem.type);
if (fullHotkeyName) {
AddPanelString(fmt::format(fmt::runtime(_("Spell Hotkey {:s}")), *fullHotkeyName));
}
}
}

Loading…
Cancel
Save