76 lines
1.8 KiB
C++
76 lines
1.8 KiB
C++
/**
|
|
* @author: Thomas Brandého
|
|
*/
|
|
|
|
#ifndef KEYBINDINGS_H
|
|
#define KEYBINDINGS_H
|
|
|
|
#include <SFML/Window.hpp>
|
|
|
|
#include <unordered_map>
|
|
|
|
#define NO_KEY -1
|
|
#define NO_ACTION -1
|
|
|
|
namespace input{
|
|
enum Source{NONE=-1,KEYBOARD,MOUSE,CONTROLLER,SOURCE_COUNT};
|
|
}
|
|
|
|
struct Action {
|
|
int action;
|
|
input::Source source;
|
|
int controller_id;
|
|
static Action getNull();
|
|
bool isNull(){
|
|
return (action == NO_ACTION) && (source == input::NONE);
|
|
}
|
|
friend bool operator ==(const Action& action1, const Action& action2){
|
|
return (action1.action == action2.action) && (action1.source == action2.source);
|
|
}
|
|
};
|
|
|
|
struct Binding{
|
|
Action action;
|
|
int key;
|
|
int type;
|
|
};
|
|
|
|
class IKeysMap{
|
|
public:
|
|
enum {PRESSED, RELEASED, HOLD};
|
|
IKeysMap();
|
|
std::vector<Binding> getBindings(Action action) const;
|
|
protected:
|
|
std::vector<Binding> keys;
|
|
};
|
|
|
|
class Context {
|
|
public:
|
|
Context(std::string name, std::vector<Action> actions);
|
|
std::string getName();
|
|
std::vector<Action> getActions() const;
|
|
private:
|
|
std::string name;
|
|
std::vector<Action> actions;
|
|
};
|
|
|
|
class KeyBindings {
|
|
public:
|
|
KeyBindings();
|
|
KeyBindings(const Context &context, const IKeysMap &keysmap);
|
|
Action getPressedAction(input::Source src,int key) const;
|
|
Action getReleasedAction(input::Source src,int key) const;
|
|
Action getHoldAction(input::Source src,int key) const;
|
|
|
|
private:
|
|
std::unordered_map<int,std::unordered_map<int,int>> bindings_pressed;
|
|
std::unordered_map<int,std::unordered_map<int,int>> bindings_released;
|
|
std::unordered_map<int,std::unordered_map<int,int>> bindings_hold;
|
|
void setPressedAction(int key, Action action);
|
|
void setReleasedAction(int key, Action action);
|
|
void setHoldAction(int key, Action action);
|
|
void init_map_bindings();
|
|
};
|
|
|
|
#endif
|