diff options
author | Stephen Kennedy | 2008-07-19 00:57:37 +0000 |
---|---|---|
committer | Stephen Kennedy | 2008-07-19 00:57:37 +0000 |
commit | 19345b5a624570dd673bdcaf86188e93110f0e5c (patch) | |
tree | ce53d7170fd99a6abd68ef7754a89ca97d03f860 /backends/common | |
parent | f272c81e86611f849dd294c14a28a0043848bd70 (diff) | |
download | scummvm-rg350-19345b5a624570dd673bdcaf86188e93110f0e5c.tar.gz scummvm-rg350-19345b5a624570dd673bdcaf86188e93110f0e5c.tar.bz2 scummvm-rg350-19345b5a624570dd673bdcaf86188e93110f0e5c.zip |
Keymap class implemented. KeymapManager and Keymapper classes started
svn-id: r33107
Diffstat (limited to 'backends/common')
-rw-r--r-- | backends/common/keymap-manager.cpp | 29 | ||||
-rw-r--r-- | backends/common/keymap-manager.h | 33 | ||||
-rw-r--r-- | backends/common/keymap.cpp | 54 | ||||
-rw-r--r-- | backends/common/keymap.h | 177 | ||||
-rw-r--r-- | backends/common/keymapper.h | 23 |
5 files changed, 316 insertions, 0 deletions
diff --git a/backends/common/keymap-manager.cpp b/backends/common/keymap-manager.cpp new file mode 100644 index 0000000000..26c58be517 --- /dev/null +++ b/backends/common/keymap-manager.cpp @@ -0,0 +1,29 @@ +#include "backends/common/keymap-manager.h" + +namespace Common { + +void KeymapManager::registerGlobalKeymap(const String& name, const Keymap& map) { + if (name.size() == 0) { + warning("Name must be specified when registering global keymap"); + return; + } + insertEntry(name, "", map); +} + +void KeymapManager::registerKeymap(const String& name, const String& domain, const Keymap& map) { + if (name.size() == 0 || domain.size() == 0) { + warning("Name and domain must be specified when registering keymap"); + return; + } + insertEntry(name, domain, map); +} + +void KeymapManager::insertEntry(const String& name, const String& domain, const Keymap& map) { + Entry *ent = new Entry; + ent->_name = name; + ent->_domain = domain; + ent->_keymap = new Keymap(map); + _keymaps.push_back(ent); +} + +} // end of namespace Common
\ No newline at end of file diff --git a/backends/common/keymap-manager.h b/backends/common/keymap-manager.h new file mode 100644 index 0000000000..8b945c5c7d --- /dev/null +++ b/backends/common/keymap-manager.h @@ -0,0 +1,33 @@ +#ifndef COMMON_KEYMAP_MANAGER +#define COMMON_KEYMAP_MANAGER + +#include "backends/common/keymap.h" + +namespace Common { + +class KeymapManager { +public: + + KeymapManager(); + + void registerGlobalKeymap(const String& name, const Keymap& map); + + void registerKeymap(const String& name, const String& domain, const Keymap& map); + +private: + + struct Entry { + String _name; + String _domain; + Keymap *_keymap; + }; + + void insertEntry(const String& name, const String& domain, const Keymap& map); + + List<Entry*> _keymaps; + +}; + +} // end of namespace Common + +#endif
\ No newline at end of file diff --git a/backends/common/keymap.cpp b/backends/common/keymap.cpp new file mode 100644 index 0000000000..7e6297917b --- /dev/null +++ b/backends/common/keymap.cpp @@ -0,0 +1,54 @@ +#include "backends/common/keymap.h" + +namespace Common { + +Keymap::Keymap(const Keymap& km) : _actions(km._actions), _keymap() { + for (uint i = 0; i < _actions.size(); i++) { + if (_actions[i]._hwKey) { + _keymap[_actions[i]._hwKey->key] = &_actions[i]; + } + } +} + +void Keymap::addAction(const UserAction& action) { + _actions.push_back(action); + _actions[_actions.size()-1]._hwKey = 0; +} + +void Keymap::mapKeyToAction(UserAction *action, HardwareKey *key) { + for (uint i = 0; i < _actions.size(); i++) { + if (&_actions[i] == action) { + internalMapKey(action, key); + return; + } + } + error("UserAction not contained in KeyMap\n"); +} + +void Keymap::mapKeyToAction(uint idx, HardwareKey *key) { + if (idx >= _actions.size()) + error("Key map index out of bounds!\n"); + internalMapKey(&_actions[idx], key); +} + +void Keymap::internalMapKey(UserAction *action, HardwareKey *hwKey) { + HashMap<KeyState, UserAction*>::iterator it; + it = _keymap.find(hwKey->key); + // if key is already mapped to an action then un-map it + if (it != _keymap.end()) + it->_value->_hwKey = 0; + + action->_hwKey = hwKey; + _keymap[hwKey->key] = action; +} + +UserAction *Keymap::getMappedAction(KeyState ks) { + HashMap<KeyState, UserAction*>::iterator it; + it = _keymap.find(ks); + if (it == _keymap.end()) + return 0; + else + return it->_value; +} + +} // end of namespace Common
\ No newline at end of file diff --git a/backends/common/keymap.h b/backends/common/keymap.h new file mode 100644 index 0000000000..75350a992b --- /dev/null +++ b/backends/common/keymap.h @@ -0,0 +1,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
\ No newline at end of file diff --git a/backends/common/keymapper.h b/backends/common/keymapper.h new file mode 100644 index 0000000000..fd1e46968e --- /dev/null +++ b/backends/common/keymapper.h @@ -0,0 +1,23 @@ +#ifndef COMMON_KEYMAPPER +#define COMMON_KEYMAPPER + +#include "backends/common/keymap-manager.h" + +namespace Common { + +class Keymapper { +public: + + Keymapper(); + + + +private: + + KeymapManager _manager; + + List<HardwareKey> _hardwareKeys; + +}; + +} // end of namespace Common
\ No newline at end of file |