#pragma once #include #include #include #include #include namespace devilution { /** The Keymapper maps keys to actions. */ class Keymapper final { public: using ActionIndex = std::size_t; /** * Action represents an action that can be triggered using a keyboard * shortcut. */ class Action final { public: Action(const std::string &name, int defaultKey, std::function action) : name(name) , defaultKey(defaultKey) , action(action) , enable([] { return true; }) { } Action(const std::string &name, int defaultKey, std::function action, std::function enable) : name(name) , defaultKey(defaultKey) , action(action) , enable(enable) { } void operator()() const { action(); } private: std::string name; int defaultKey; std::function action; std::function enable; int key {}; friend class Keymapper; }; Keymapper(); ActionIndex AddAction(const Action &action); void KeyPressed(int key) const; std::string KeyNameForAction(ActionIndex actionIndex) const; void Save() const; void Load(); private: int GetActionKey(const Action &action); std::vector actions; std::unordered_map> keyIDToAction; std::unordered_map keyIDToKeyName; std::unordered_map keyNameToKeyID; }; } // namespace devilution