184 lines
No EOL
4.1 KiB
C++
184 lines
No EOL
4.1 KiB
C++
#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 {
|
|
/** unique id used for saving/loading to config */
|
|
int32 id;
|
|
/** Human readable description */
|
|
String description;
|
|
/**
|
|
* The KeyState that is generated by the back-end
|
|
* when this hardware key is pressed.
|
|
*/
|
|
KeyState key;
|
|
|
|
UserActionCategory preferredCategory;
|
|
UserActionType preferredType;
|
|
int16 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 {
|
|
/** unique id used for saving/loading to config */
|
|
int32 id;
|
|
/** Human readable description */
|
|
String description;
|
|
/** Events to be sent when mapped key is pressed */
|
|
List<Event> events;
|
|
UserActionCategory category;
|
|
UserActionType type;
|
|
int priority;
|
|
int group;
|
|
int flags;
|
|
|
|
HardwareKey *hwKey;
|
|
|
|
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;
|
|
}
|
|
};
|
|
|
|
/**
|
|
* 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() { 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(KeyState key) 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 |