You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
69 lines
1.5 KiB
69 lines
1.5 KiB
#pragma once |
|
|
|
#include <cstddef> |
|
#include <string> |
|
#include <functional> |
|
#include <vector> |
|
#include <unordered_map> |
|
|
|
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<void()> action) |
|
: name(name) |
|
, defaultKey(defaultKey) |
|
, action(action) |
|
, enable([] { return true; }) |
|
{ |
|
} |
|
Action(const std::string &name, int defaultKey, std::function<void()> action, std::function<bool()> enable) |
|
: name(name) |
|
, defaultKey(defaultKey) |
|
, action(action) |
|
, enable(enable) |
|
{ |
|
} |
|
|
|
void operator()() const |
|
{ |
|
action(); |
|
} |
|
|
|
private: |
|
std::string name; |
|
int defaultKey; |
|
std::function<void()> action; |
|
std::function<bool()> 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<Action> actions; |
|
std::unordered_map<int, std::reference_wrapper<Action>> keyIDToAction; |
|
std::unordered_map<int, std::string> keyIDToKeyName; |
|
std::unordered_map<std::string, int> keyNameToKeyID; |
|
}; |
|
|
|
} // namespace devilution
|
|
|