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:
parent
2f064da102
commit
dfcdbb0d33
13 changed files with 171 additions and 82 deletions
|
@ -80,9 +80,9 @@ private:
|
|||
List<HardwareKey*>::iterator it;
|
||||
for (it = _keys.begin(); it != _keys.end(); it++) {
|
||||
if ((*it)->id == key->id)
|
||||
error("HardwareKey with id %d already given!\n", key->id);
|
||||
error("HardwareKey with id %d already given!", key->id);
|
||||
else if ((*it)->key == key->key)
|
||||
error("HardwareKey with same KeyState already given!\n");
|
||||
error("HardwareKey with same KeyState already given!");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -32,6 +32,11 @@ Keymap *KeymapManager::Domain::getKeymap(const String& name) {
|
|||
return 0;
|
||||
}
|
||||
|
||||
void KeymapManager::registerHardwareKeySet(HardwareKeySet *keys) {
|
||||
if (_hardwareKeys)
|
||||
error("Hardware key set already registered!");
|
||||
_hardwareKeys = keys;
|
||||
}
|
||||
|
||||
void KeymapManager::registerDefaultGlobalKeymap(Keymap *map) {
|
||||
ConfigManager::Domain *dom = ConfMan.getDomain(ConfigManager::kApplicationDomain);
|
||||
|
@ -75,13 +80,65 @@ void KeymapManager::initKeymap(ConfigManager::Domain *domain,
|
|||
bool KeymapManager::loadKeymap(ConfigManager::Domain *domain,
|
||||
const String& name,
|
||||
Keymap *map) {
|
||||
ConfigManager::Domain::iterator it;
|
||||
String prefix = "km_" + name + "_";
|
||||
for (it = domain->begin(); it != domain->end(); it++) {
|
||||
const String& key = it->_key;
|
||||
if (!key.hasPrefix(prefix.c_str()))
|
||||
continue;
|
||||
|
||||
// parse UserAction ID
|
||||
const char *actionIdStart = key.c_str() + prefix.size();
|
||||
char *err;
|
||||
int32 actionId = (int32) strtol(actionIdStart, &err, 0);
|
||||
if (err == actionIdStart) {
|
||||
warning("'%s' is not a valid UserAction ID", err);
|
||||
continue;
|
||||
}
|
||||
UserAction *ua = map->getUserAction(actionId);
|
||||
if (!ua) {
|
||||
warning("'%s' keymap does not contain UserAction with ID %d",
|
||||
name.c_str(), (int)actionId);
|
||||
continue;
|
||||
}
|
||||
|
||||
// parse HardwareKey ID
|
||||
int32 hwKeyId = (int32) strtol(it->_value.c_str(), &err, 0);
|
||||
if (err == it->_value.c_str()) {
|
||||
warning("'%s' is not a valid HardwareKey ID", err);
|
||||
continue;
|
||||
}
|
||||
const HardwareKey *hwKey = _hardwareKeys->findHardwareKey(hwKeyId);
|
||||
if (!hwKey) {
|
||||
warning("HardwareKey with ID %d not known", (int)hwKeyId);
|
||||
continue;
|
||||
}
|
||||
|
||||
ua->mapKey(hwKey);
|
||||
}
|
||||
return isMapComplete(map);
|
||||
}
|
||||
|
||||
bool KeymapManager::isMapComplete(const Keymap *map) {
|
||||
return false;
|
||||
}
|
||||
|
||||
void KeymapManager::saveKeymap(ConfigManager::Domain *domain,
|
||||
const String& name,
|
||||
Keymap *map) {
|
||||
|
||||
const Keymap *map) {
|
||||
const Array<UserAction>& actions = map->getUserActions();
|
||||
Array<UserAction>::const_iterator it;
|
||||
char buf[11];
|
||||
for (it = actions.begin(); it != actions.end(); it++) {
|
||||
String key("km_");
|
||||
sprintf(buf, "%d", it->id);
|
||||
key += name + "_" + buf;
|
||||
if (it->getMappedKey())
|
||||
sprintf(buf, "%d", it->getMappedKey()->id);
|
||||
else
|
||||
strcpy(buf, "");
|
||||
domain->setVal(key, buf);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#ifndef COMMON_KEYMAP_MANAGER
|
||||
#define COMMON_KEYMAP_MANAGER
|
||||
|
||||
#include "backends/common/hardware-key.h"
|
||||
#include "backends/common/keymap.h"
|
||||
#include "common/config-manager.h"
|
||||
#include "common/hash-str.h"
|
||||
|
@ -32,6 +33,8 @@ public:
|
|||
KeymapMap _keymaps;
|
||||
};
|
||||
|
||||
void registerHardwareKeySet(HardwareKeySet *keys);
|
||||
|
||||
void registerDefaultGlobalKeymap(Keymap *map);
|
||||
void registerGlobalKeymap(const String& name, Keymap *map);
|
||||
|
||||
|
@ -46,11 +49,14 @@ private:
|
|||
|
||||
void initKeymap(ConfigManager::Domain *domain, const String& name, Keymap *keymap);
|
||||
bool loadKeymap(ConfigManager::Domain *domain, const String& name, Keymap *keymap);
|
||||
void saveKeymap(ConfigManager::Domain *domain, const String& name, Keymap *keymap);
|
||||
void saveKeymap(ConfigManager::Domain *domain, const String& name, const Keymap *keymap);
|
||||
void automaticMap(Keymap *map);
|
||||
bool isMapComplete(const Keymap *map);
|
||||
|
||||
Domain _globalDomain;
|
||||
Domain _gameDomain;
|
||||
|
||||
HardwareKeySet *_hardwareKeys;
|
||||
};
|
||||
|
||||
} // end of namespace Common
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
@ -1,15 +1,16 @@
|
|||
#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"
|
||||
#include "backends/common/user-action.h"
|
||||
|
||||
namespace Common {
|
||||
|
||||
struct HardwareKey;
|
||||
|
||||
/**
|
||||
* Hash function for KeyState
|
||||
*/
|
||||
|
@ -34,30 +35,14 @@ public:
|
|||
* 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);
|
||||
void addAction(UserAction& action);
|
||||
|
||||
/**
|
||||
* 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;
|
||||
UserAction *getUserAction(int32 id);
|
||||
|
||||
/**
|
||||
* Get a read-only array of all the UserActions contained in this Keymap
|
||||
|
@ -72,6 +57,21 @@ public:
|
|||
UserAction *getMappedAction(const KeyState& ks) const;
|
||||
|
||||
private:
|
||||
friend struct UserAction;
|
||||
/**
|
||||
* Registers a HardwareKey to the given UserAction
|
||||
* @param action UserAction in this Keymap
|
||||
* @param key pointer to HardwareKey to map
|
||||
* @see UserAction::mapKey
|
||||
*/
|
||||
void registerMapping(UserAction *action, const HardwareKey *key);
|
||||
|
||||
/**
|
||||
* Unregisters a HardwareKey from the given UserAction (if one is mapped)
|
||||
* @param action UserAction in this Keymap
|
||||
* @see UserAction::mapKey
|
||||
*/
|
||||
void unregisterMapping(UserAction *action);
|
||||
|
||||
UserAction *findUserAction(int32 id);
|
||||
const UserAction *findUserAction(int32 id) const;
|
||||
|
|
|
@ -7,17 +7,10 @@ Keymapper::Keymapper(EventManager *evtMgr) {
|
|||
_eventMan = evtMgr;
|
||||
_keymapMan = new KeymapManager();
|
||||
_currentMap = 0;
|
||||
_hardwareKeys = 0;
|
||||
}
|
||||
|
||||
void Keymapper::registerHardwareKeySet(HardwareKeySet *keys) {
|
||||
if (_hardwareKeys)
|
||||
error("Hardware key set already registered!\n");
|
||||
_hardwareKeys = keys;
|
||||
}
|
||||
|
||||
const HardwareKeySet *Keymapper::getHardwareKeySet() const {
|
||||
return _hardwareKeys;
|
||||
_keymapMan->registerHardwareKeySet(keys);
|
||||
}
|
||||
|
||||
void Keymapper::addGlobalKeyMap(const String& name, Keymap *keymap) {
|
||||
|
@ -35,7 +28,7 @@ void Keymapper::addGameKeyMap(const String& name, Keymap *keymap) {
|
|||
|
||||
void Keymapper::initGame() {
|
||||
if (ConfMan.getActiveDomain() == 0)
|
||||
error("Call to Keymapper::initGame when no game loaded\n");
|
||||
error("Call to Keymapper::initGame when no game loaded");
|
||||
|
||||
if (_gameId.size() > 0)
|
||||
cleanupGame();
|
||||
|
@ -51,7 +44,7 @@ void Keymapper::cleanupGame() {
|
|||
bool Keymapper::switchKeymap(const String& name) {
|
||||
Keymap *new_map = _keymapMan->getKeymap(name);
|
||||
if (!new_map) {
|
||||
warning("Keymap '%s' not registered\n", name.c_str());
|
||||
warning("Keymap '%s' not registered", name.c_str());
|
||||
return false;
|
||||
}
|
||||
_currentMap = new_map;
|
||||
|
|
|
@ -1,12 +1,16 @@
|
|||
#ifndef COMMON_KEYMAPPER
|
||||
#define COMMON_KEYMAPPER
|
||||
|
||||
#include "backends/common/keymap.h"
|
||||
#include "common/events.h"
|
||||
#include "common/list.h"
|
||||
|
||||
|
||||
namespace Common {
|
||||
|
||||
struct HardwareKey;
|
||||
class HardwareKeySet;
|
||||
class KeymapManager;
|
||||
class Keymap;
|
||||
|
||||
class Keymapper {
|
||||
public:
|
||||
|
@ -19,11 +23,6 @@ public:
|
|||
*/
|
||||
void registerHardwareKeySet(HardwareKeySet *keys);
|
||||
|
||||
/**
|
||||
* Get the HardwareKeySet that is registered with the Keymapper
|
||||
*/
|
||||
const HardwareKeySet *getHardwareKeySet() const;
|
||||
|
||||
/**
|
||||
* Add a keymap to the global domain.
|
||||
* If a saved key setup exists for it in the ini file it will be used.
|
||||
|
@ -88,8 +87,6 @@ private:
|
|||
|
||||
Keymap *_currentMap;
|
||||
|
||||
const HardwareKeySet *_hardwareKeys;
|
||||
|
||||
};
|
||||
|
||||
} // end of namespace Common
|
||||
|
|
33
backends/common/user-action.cpp
Normal file
33
backends/common/user-action.cpp
Normal file
|
@ -0,0 +1,33 @@
|
|||
#include "backends/common/user-action.h"
|
||||
#include "backends/common/keymap.h"
|
||||
|
||||
namespace Common {
|
||||
|
||||
UserAction::UserAction(String des, UserActionCategory cat, UserActionType ty,
|
||||
int pr, int gr, int fl) {
|
||||
description = des;
|
||||
category = cat;
|
||||
type = ty;
|
||||
priority = pr;
|
||||
group = gr;
|
||||
flags = fl;
|
||||
_hwKey = 0;
|
||||
_parent = 0;
|
||||
}
|
||||
|
||||
void UserAction::setParent(Keymap *parent) {
|
||||
_parent = parent;
|
||||
}
|
||||
|
||||
void UserAction::mapKey(const HardwareKey *key) {
|
||||
assert(_parent);
|
||||
if (_hwKey) _parent->unregisterMapping(this);
|
||||
_hwKey = key;
|
||||
if (_hwKey) _parent->registerMapping(this, key);
|
||||
}
|
||||
|
||||
const HardwareKey *UserAction::getMappedKey() const {
|
||||
return _hwKey;
|
||||
}
|
||||
|
||||
} // end of namespace Common
|
|
@ -8,6 +8,8 @@
|
|||
namespace Common {
|
||||
|
||||
struct HardwareKey;
|
||||
class Keymap;
|
||||
|
||||
|
||||
enum UserActionType {
|
||||
kGenericUserActionType,
|
||||
|
@ -48,21 +50,22 @@ struct UserAction {
|
|||
int group;
|
||||
int flags;
|
||||
|
||||
private:
|
||||
/** Hardware key that is mapped to this UserAction */
|
||||
HardwareKey *hwKey;
|
||||
const HardwareKey *_hwKey;
|
||||
Keymap *_parent;
|
||||
|
||||
public:
|
||||
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;
|
||||
}
|
||||
int pr = 0, int gr = 0, int fl = 0 );
|
||||
|
||||
void setParent(Keymap *parent);
|
||||
|
||||
void mapKey(const HardwareKey *key);
|
||||
|
||||
const HardwareKey *getMappedKey() const;
|
||||
};
|
||||
|
||||
} // end of namespace Common
|
||||
|
|
|
@ -103,7 +103,7 @@ bool VirtualKeyboard::loadKeyboardPack(Common::String packName) {
|
|||
return false;
|
||||
}
|
||||
} else {
|
||||
warning("Could not find %s.xml file in %s.zip keyboard pack\n", packName.c_str(), packName.c_str());
|
||||
warning("Could not find %s.xml file in %s.zip keyboard pack", packName.c_str(), packName.c_str());
|
||||
unzClose(zipFile);
|
||||
return false;
|
||||
}
|
||||
|
|
|
@ -28,6 +28,8 @@
|
|||
#include "common/system.h"
|
||||
#include "common/config-manager.h"
|
||||
#include "backends/events/default/default-events.h"
|
||||
#include "backends/common/keymapper.h"
|
||||
#include "backends/common/virtual-keyboard.h"
|
||||
|
||||
#include "engines/engine.h"
|
||||
#include "gui/message.h"
|
||||
|
|
|
@ -29,8 +29,11 @@
|
|||
#include "common/events.h"
|
||||
#include "common/queue.h"
|
||||
#include "common/savefile.h"
|
||||
#include "backends/common/keymapper.h"
|
||||
#include "backends/common/virtual-keyboard.h"
|
||||
|
||||
namespace Common {
|
||||
class VirtualKeyboard;
|
||||
class Keymapper;
|
||||
}
|
||||
|
||||
/*
|
||||
At some point we will remove pollEvent from OSystem and change
|
||||
|
|
|
@ -1084,6 +1084,10 @@
|
|||
RelativePath="..\..\backends\common\keymapper.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\..\backends\common\user-action.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\..\backends\common\user-action.h"
|
||||
>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue