Browse Source

KeyMapper: Use forward_list instead of vector

Similar to how we do this for Padmapper.
pull/5464/head
Gleb Mazovetskiy 3 years ago
parent
commit
e7c0d2943f
  1. 1
      Source/diablo.cpp
  2. 23
      Source/options.cpp
  3. 6
      Source/options.h

1
Source/diablo.cpp

@ -1761,6 +1761,7 @@ void InitKeymapActions()
DebugToggle = !DebugToggle;
});
#endif
sgOptions.Keymapper.CommitActions();
}
void InitPadmapActions()

23
Source/options.cpp

@ -1249,8 +1249,8 @@ KeymapperOptions::KeymapperOptions()
std::vector<OptionEntryBase *> KeymapperOptions::GetEntries()
{
std::vector<OptionEntryBase *> entries;
for (auto &action : actions) {
entries.push_back(action.get());
for (Action &action : actions) {
entries.push_back(&action);
}
return entries;
}
@ -1359,7 +1359,12 @@ bool KeymapperOptions::Action::SetValue(int value)
void KeymapperOptions::AddAction(string_view key, const char *name, const char *description, uint32_t defaultKey, std::function<void()> actionPressed, std::function<void()> actionReleased, std::function<bool()> enable, unsigned index)
{
actions.push_back(std::unique_ptr<Action>(new Action(key, name, description, defaultKey, std::move(actionPressed), std::move(actionReleased), std::move(enable), index)));
actions.emplace_front(key, name, description, defaultKey, std::move(actionPressed), std::move(actionReleased), std::move(enable), index);
}
void KeymapperOptions::CommitActions()
{
actions.reverse();
}
void KeymapperOptions::KeyPressed(uint32_t key) const
@ -1400,9 +1405,9 @@ void KeymapperOptions::KeyReleased(uint32_t key) const
string_view KeymapperOptions::KeyNameForAction(string_view actionName) const
{
for (const auto &action : actions) {
if (action->key == actionName && action->boundKey != SDLK_UNKNOWN) {
return action->GetValueDescription();
for (const Action &action : actions) {
if (action.key == actionName && action.boundKey != SDLK_UNKNOWN) {
return action.GetValueDescription();
}
}
return "";
@ -1410,9 +1415,9 @@ string_view KeymapperOptions::KeyNameForAction(string_view actionName) const
uint32_t KeymapperOptions::KeyForAction(string_view actionName) const
{
for (const auto &action : actions) {
if (action->key == actionName && action->boundKey != SDLK_UNKNOWN) {
return action->boundKey;
for (const Action &action : actions) {
if (action.key == actionName && action.boundKey != SDLK_UNKNOWN) {
return action.boundKey;
}
}
return SDLK_UNKNOWN;

6
Source/options.h

@ -638,6 +638,8 @@ struct KeymapperOptions : OptionCategoryBase {
// The implicit copy constructor would copy that reference instead of referencing the copy.
Action(const Action &) = delete;
Action(string_view key, const char *name, const char *description, uint32_t defaultKey, std::function<void()> actionPressed, std::function<void()> actionReleased, std::function<bool()> enable, unsigned index);
[[nodiscard]] string_view GetName() const override;
[[nodiscard]] OptionEntryType GetType() const override
{
@ -652,7 +654,6 @@ struct KeymapperOptions : OptionCategoryBase {
bool SetValue(int value);
private:
Action(string_view key, const char *name, const char *description, uint32_t defaultKey, std::function<void()> actionPressed, std::function<void()> actionReleased, std::function<bool()> enable, unsigned index);
uint32_t defaultKey;
std::function<void()> actionPressed;
std::function<void()> actionReleased;
@ -674,13 +675,14 @@ struct KeymapperOptions : OptionCategoryBase {
std::function<void()> actionReleased = nullptr,
std::function<bool()> enable = nullptr,
unsigned index = 0);
void CommitActions();
void KeyPressed(uint32_t key) const;
void KeyReleased(uint32_t key) const;
string_view KeyNameForAction(string_view actionName) const;
uint32_t KeyForAction(string_view actionName) const;
private:
std::vector<std::unique_ptr<Action>> actions;
std::forward_list<Action> actions;
std::unordered_map<uint32_t, std::reference_wrapper<Action>> keyIDToAction;
std::unordered_map<uint32_t, std::string> keyIDToKeyName;
std::unordered_map<std::string, uint32_t> keyNameToKeyID;

Loading…
Cancel
Save