aboutsummaryrefslogtreecommitdiff
path: root/backends/common/keymap.h
blob: 75350a992bef0ddeb540f8d5069846a420c53d00 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#ifndef COMMON_KEYMAP
#define COMMON_KEYMAP

#include "common/array.h"
#include "common/events.h"
#include "common/func.h"
#include "common/hashmap.h"
#include "common/list.h"

namespace Common {


enum UserActionType {
	kGenericUserActionType,

	// common actions
	kDirectionUpUserAction,
	kDirectionDownUserAction,
	kDirectionLeftUserAction,
	kDirectionRightUserAction,
	kLeftClickUserAction,
	kRightClickUserAction,
	kSaveUserAction,
	kMenuUserAction,

	kUserActionTypeMax
};

enum UserActionCategory {
	kGenericUserActionCategory,
	// classes of action - probably need to be slightly more specific than this
	kInGameUserAction,   // effects the actual gameplay
	kSystemUserAction,   //show a menu / change volume / etc

	kUserActionCategoryMax
};

/**
* Describes an available hardware key 
*/
struct HardwareKey {
	/** 
	* The KeyState that is generated by the back-end 
	* when this hardware key is pressed.
	*/
	KeyState key;

	/** Human readable description */
	String description; 

	UserActionCategory preferredCategory;
	UserActionType preferredType;
	int group;

	HardwareKey(KeyState ks = KeyState(), String des = "",
			UserActionCategory cat = kGenericUserActionCategory,
			UserActionType ty = kGenericUserActionType,	int gr = 0) {
		key = ks;
		description = des;
		preferredCategory = cat;
		preferredType = ty;
		group = gr;
	}
};

struct UserAction {
	/** Events to be sent when mapped key is pressed */
	List<Event> events;
	/** Human readable description */
	String description;
	UserActionCategory category;
	UserActionType type;
	int priority;
	int group;
	int flags;

	UserAction(	String des = "", 
				UserActionCategory cat = kGenericUserActionCategory,
				UserActionType ty = kGenericUserActionType,
				int pr = 0, int gr = 0, int fl = 0 ) {
		description = des;
		category = cat;
		type = ty;
		priority = pr;
		group = gr;
		flags = fl;
		_hwKey = 0;
	}

	friend class Keymap;

	HardwareKey *mappedKey() { return _hwKey; }
private:
	/** 
	* Key that is mapped to this UserAction, only KeyMap can set this
	*/
	HardwareKey *_hwKey;
};

/**
 * EqualTo function for KeyState
 */
template<> struct EqualTo<KeyState>
	: public BinaryFunction<KeyState, KeyState, bool> {

	bool operator()(const KeyState &x, const KeyState &y) const { 
		return (x.keycode == y.keycode)
			&& (x.ascii == y.ascii)
			&& (x.flags == y.flags); 
	}
};

/**
 * Hash function for KeyState
 */
template<> struct Hash<KeyState>
	: public UnaryFunction<KeyState, uint> {

	uint operator()(const KeyState &val) const { 
		return (uint)(val.keycode * (val.flags << 1));
	}
};

class Keymap {
public:

	Keymap() {}
	Keymap(const Keymap& km);

	/**
	 * Adds a new UserAction to this Map, 
	 * adding it at the back of the internal array
	 * @param action the UserAction to add
	 */
	void addAction(const UserAction& action);

	/**
	* Maps a HardwareKey to the given UserAction
	* @param action must point to a UserAction in this Keymap
	* @param key pointer to HardwareKey to map
	* @note if action does not point to a UserAction in this Keymap a
	*       fatal error will occur
	*/
	void mapKeyToAction(UserAction *action, HardwareKey *key);

	/**
	 * Maps a HardwareKey to the UserAction at the given index
	 * @param index Index of UserAction in the internal array
	 * @param key pointer to HardwareKey to map
	 */
	void mapKeyToAction(uint index, HardwareKey *key);

	/**
	 * Get a read-only array of all the UserActions contained in this Keymap
	 */
	const Array<UserAction>& getUserActions() { return _actions; }

	/**
	 * Find the UserAction that a key is mapped to
	 * @param key the key that is mapped to the required UserAction
	 * @return a pointer to the UserAction or 0 if no
	 */
	UserAction *getMappedAction(KeyState key);

private:

	void internalMapKey(UserAction *action, HardwareKey *hwKey);

	Array<UserAction> _actions;
	HashMap<KeyState, UserAction*> _keymap; 

};


} // end of namespace Common

#endif