KeymapManager - implemented loading/saving of keymaps

- Refactoring of code to map a key to a UserAction - now we call a method on UserAction to do it (and it then tells the Keymap class)
- General cleanup of code

svn-id: r33262
This commit is contained in:
Stephen Kennedy 2008-07-24 10:00:56 +00:00
parent 2f064da102
commit dfcdbb0d33
13 changed files with 171 additions and 82 deletions

View file

@ -1,12 +1,14 @@
#include "backends/common/keymap.h"
#include "backends/common/hardware-key.h"
namespace Common {
Keymap::Keymap(const Keymap& km) : _actions(km._actions), _keymap() {
init();
for (uint i = 0; i < _actions.size(); i++) {
if (_actions[i].hwKey) {
_keymap[_actions[i].hwKey->key] = &_actions[i];
const HardwareKey *hwKey = _actions[i].getMappedKey();
if (hwKey) {
_keymap[hwKey->key] = &_actions[i];
}
}
}
@ -15,41 +17,30 @@ void Keymap::init() {
_actions.reserve(20);
}
void Keymap::addAction(const UserAction& action) {
void Keymap::addAction(UserAction& action) {
if (findUserAction(action.id))
error("UserAction with id %d already in KeyMap!\n", action.id);
error("UserAction with id %d already in KeyMap!", action.id);
action.setParent(this);
_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(int32 id, HardwareKey *key) {
UserAction *act = findUserAction(id);
if (act)
internalMapKey(act, key);
}
void Keymap::internalMapKey(UserAction *action, HardwareKey *hwKey) {
void Keymap::registerMapping(UserAction *action, const 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;
it->_value->mapKey(0);
action->hwKey = hwKey;
_keymap[hwKey->key] = action;
}
const UserAction *Keymap::getUserAction(int32 id) const {
void Keymap::unregisterMapping(UserAction *action) {
const HardwareKey *hwKey = action->getMappedKey();
if (hwKey)
_keymap[hwKey->key] = 0;
}
UserAction *Keymap::getUserAction(int32 id) {
return findUserAction(id);
}