aboutsummaryrefslogtreecommitdiff
path: root/backends/common/keymap.h
blob: 5197e86b489a2bd4c5d945059f273d71652bd154 (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
#ifndef COMMON_KEYMAP
#define COMMON_KEYMAP

#include "backends/common/hardware-key.h"
#include "backends/common/user-action.h"
#include "common/array.h"
#include "common/keyboard.h"
#include "common/func.h"
#include "common/hashmap.h"

namespace Common {

/**
 * 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() { init(); }
	Keymap(const Keymap& km);
private:
	void init();

public:
	/**
	 * 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 of the given id
	 * @param id id of the UserAction to map to
	 * @param key pointer to HardwareKey to map
	 */
	void mapKeyToAction(int32 id, HardwareKey *key);

	/**
	 * Retrieves the UserAction with the given id
	 * @param id id of UserAction to retrieve
	 * @return Pointer to the UserAction or 0 if not found
	 */
	const UserAction *getUserAction(int32 id) const;

	/**
	 * Get a read-only array of all the UserActions contained in this Keymap
	 */
	const Array<UserAction>& getUserActions() const { 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(const KeyState& ks) const;

private:
	
	UserAction *findUserAction(int32 id);
	const UserAction *findUserAction(int32 id) const;

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

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

};


} // end of namespace Common

#endif