aboutsummaryrefslogtreecommitdiff
path: root/backends/common
diff options
context:
space:
mode:
authorStephen Kennedy2008-07-23 08:45:12 +0000
committerStephen Kennedy2008-07-23 08:45:12 +0000
commit2f064da1021344f28c9106285475c72930638390 (patch)
treed636949ffff9972e30511446d79ff3a8d4369b81 /backends/common
parent0861fa4c00f0ecb82f3607127c8278d55f95376c (diff)
downloadscummvm-rg350-2f064da1021344f28c9106285475c72930638390.tar.gz
scummvm-rg350-2f064da1021344f28c9106285475c72930638390.tar.bz2
scummvm-rg350-2f064da1021344f28c9106285475c72930638390.zip
Most of Keymapper class complete
DefaultEventManager now initialises Keymapper and passes key press events to it svn-id: r33227
Diffstat (limited to 'backends/common')
-rw-r--r--backends/common/keymap-manager.cpp42
-rw-r--r--backends/common/keymap-manager.h7
-rw-r--r--backends/common/keymap.cpp2
-rw-r--r--backends/common/keymap.h2
-rw-r--r--backends/common/keymapper.cpp56
-rw-r--r--backends/common/keymapper.h53
6 files changed, 154 insertions, 8 deletions
diff --git a/backends/common/keymap-manager.cpp b/backends/common/keymap-manager.cpp
index 73ce705414..8ef89add6e 100644
--- a/backends/common/keymap-manager.cpp
+++ b/backends/common/keymap-manager.cpp
@@ -1,7 +1,5 @@
#include "backends/common/keymap-manager.h"
-#define GLOBAL_ID_STR "___GLOBAL"
-
namespace Common {
@@ -36,21 +34,61 @@ Keymap *KeymapManager::Domain::getKeymap(const String& name) {
void KeymapManager::registerDefaultGlobalKeymap(Keymap *map) {
+ ConfigManager::Domain *dom = ConfMan.getDomain(ConfigManager::kApplicationDomain);
+ assert(dom);
+ initKeymap(dom, "default", map);
_globalDomain.addDefaultKeymap(map);
}
void KeymapManager::registerGlobalKeymap(const String& name, Keymap *map) {
+ ConfigManager::Domain *dom = ConfMan.getDomain(ConfigManager::kApplicationDomain);
+ assert(dom);
+
+ initKeymap(dom, name, map);
_globalDomain.addKeymap(name, map);
}
void KeymapManager::registerDefaultGameKeymap(Keymap *map) {
+ ConfigManager::Domain *dom = ConfMan.getActiveDomain();
+ assert(dom);
+
+ initKeymap(dom, "default", map);
_gameDomain.addDefaultKeymap(map);
}
void KeymapManager::registerGameKeymap(const String& name, Keymap *map) {
+ ConfigManager::Domain *dom = ConfMan.getActiveDomain();
+ assert(dom);
+
+ initKeymap(dom, name, map);
_gameDomain.addKeymap(name, map);
}
+void KeymapManager::initKeymap(ConfigManager::Domain *domain,
+ const String& name,
+ Keymap *map) {
+ if (!loadKeymap(domain, name, map))
+ return;
+ automaticMap(map);
+}
+
+bool KeymapManager::loadKeymap(ConfigManager::Domain *domain,
+ const String& name,
+ Keymap *map) {
+ return false;
+}
+
+void KeymapManager::saveKeymap(ConfigManager::Domain *domain,
+ const String& name,
+ Keymap *map) {
+
+}
+
+
+void KeymapManager::automaticMap(Keymap *map) {
+
+}
+
void KeymapManager::unregisterAllGameKeymaps() {
_gameDomain.deleteAllKeyMaps();
}
diff --git a/backends/common/keymap-manager.h b/backends/common/keymap-manager.h
index e5152d2426..c056fbe024 100644
--- a/backends/common/keymap-manager.h
+++ b/backends/common/keymap-manager.h
@@ -2,6 +2,7 @@
#define COMMON_KEYMAP_MANAGER
#include "backends/common/keymap.h"
+#include "common/config-manager.h"
#include "common/hash-str.h"
#include "common/hashmap.h"
@@ -13,6 +14,7 @@ public:
class Domain {
public:
Domain() : _defaultKeymap(0) {}
+ ~Domain() { deleteAllKeyMaps(); }
void addDefaultKeymap(Keymap *map);
void addKeymap(const String& name, Keymap *map);
@@ -42,6 +44,11 @@ public:
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 automaticMap(Keymap *map);
+
Domain _globalDomain;
Domain _gameDomain;
};
diff --git a/backends/common/keymap.cpp b/backends/common/keymap.cpp
index 9fb80bcc58..770e308fbc 100644
--- a/backends/common/keymap.cpp
+++ b/backends/common/keymap.cpp
@@ -71,7 +71,7 @@ const UserAction *Keymap::findUserAction(int32 id) const {
return 0;
}
-UserAction *Keymap::getMappedAction(KeyState ks) const {
+UserAction *Keymap::getMappedAction(const KeyState& ks) const {
HashMap<KeyState, UserAction*>::iterator it;
it = _keymap.find(ks);
if (it == _keymap.end())
diff --git a/backends/common/keymap.h b/backends/common/keymap.h
index a2e204827e..5197e86b48 100644
--- a/backends/common/keymap.h
+++ b/backends/common/keymap.h
@@ -69,7 +69,7 @@ public:
* @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;
+ UserAction *getMappedAction(const KeyState& ks) const;
private:
diff --git a/backends/common/keymapper.cpp b/backends/common/keymapper.cpp
index a70a691907..bb98eff7ef 100644
--- a/backends/common/keymapper.cpp
+++ b/backends/common/keymapper.cpp
@@ -38,11 +38,11 @@ void Keymapper::initGame() {
error("Call to Keymapper::initGame when no game loaded\n");
if (_gameId.size() > 0)
- deInitGame();
+ cleanupGame();
_gameId = ConfMan.getActiveDomainName();
}
-void Keymapper::deInitGame() {
+void Keymapper::cleanupGame() {
_keymapMan->unregisterAllGameKeymaps();
_gameId.clear();
}
@@ -51,11 +51,61 @@ void Keymapper::deInitGame() {
bool Keymapper::switchKeymap(const String& name) {
Keymap *new_map = _keymapMan->getKeymap(name);
if (!new_map) {
- warning("Keymap '%s' could not be found\n", name.c_str());
+ warning("Keymap '%s' not registered\n", name.c_str());
return false;
}
_currentMap = new_map;
return true;
}
+bool Keymapper::mapKeyDown(const KeyState& key) {
+ return mapKey(key, true);
+}
+
+bool Keymapper::mapKeyUp(const KeyState& key) {
+ return mapKey(key, false);
+}
+
+bool Keymapper::mapKey(const KeyState& key, bool isKeyDown) {
+ if (!_currentMap) return false;
+ UserAction *action = _currentMap->getMappedAction(key);
+ if (!action) return false;
+ List<Event>::iterator it;
+ for (it = action->events.begin(); it != action->events.end(); it++) {
+ Event evt = *it;
+ bool pushEvent = true;
+ switch (evt.type) {
+ case EVENT_KEYDOWN:
+ if (!isKeyDown) evt.type = EVENT_KEYUP;
+ break;
+ case EVENT_KEYUP:
+ if (isKeyDown) evt.type = EVENT_KEYDOWN;
+ break;
+ case EVENT_LBUTTONDOWN:
+ if (!isKeyDown) evt.type = EVENT_LBUTTONUP;
+ break;
+ case EVENT_LBUTTONUP:
+ if (isKeyDown) evt.type = EVENT_LBUTTONDOWN;
+ break;
+ case EVENT_RBUTTONDOWN:
+ if (!isKeyDown) evt.type = EVENT_RBUTTONUP;
+ break;
+ case EVENT_RBUTTONUP:
+ if (isKeyDown) evt.type = EVENT_RBUTTONDOWN;
+ break;
+ case EVENT_MBUTTONDOWN:
+ if (!isKeyDown) evt.type = EVENT_MBUTTONUP;
+ break;
+ case EVENT_MBUTTONUP:
+ if (isKeyDown) evt.type = EVENT_MBUTTONDOWN;
+ break;
+ default:
+ // don't deliver other events on key up
+ if (!isKeyDown) pushEvent = false;
+ }
+ if (pushEvent) _eventMan->pushEvent(evt);
+ }
+ return true;
+}
+
} // end of namespace Common
diff --git a/backends/common/keymapper.h b/backends/common/keymapper.h
index d9b1b050fb..3252d336e1 100644
--- a/backends/common/keymapper.h
+++ b/backends/common/keymapper.h
@@ -13,19 +13,70 @@ public:
Keymapper(EventManager *eventMan);
+ /**
+ * Registers a HardwareKeySet with the Keymapper
+ * @note should only be called once (during backend initialisation)
+ */
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.
+ * Else, the key setup will be automatically mapped.
+ */
void addGlobalKeyMap(const String& name, Keymap *keymap);
+ /**
+ * Add a keymap to the game domain.
+ * @see addGlobalKeyMap
+ * @note initGame() should be called before any game keymaps are added.
+ */
void addGameKeyMap(const String& name, Keymap *keymap);
+ /**
+ * Initialise the keymapper for a new game
+ */
void initGame();
- void deInitGame();
+ /**
+ * Cleanup the keymapper after a game has ended
+ */
+ void cleanupGame();
+
+ /**
+ * Switch the active keymap.
+ * @param name name of the new keymap
+ * @return true if successful
+ */
bool switchKeymap(const String& name);
+ /**
+ * @brief Map a key press event.
+ * If the active keymap contains a UserAction mapped to the given key, then
+ * the UserAction's events are pushed into the EventManager's event queue.
+ * @param key key that was pressed
+ * @param isKeyDown true for key down, false for key up
+ * @return true if key was mapped
+ */
+ bool mapKey(const KeyState& key, bool isKeyDown);
+
+ /**
+ * @brief Map a key down event.
+ * @see mapKey
+ */
+ bool mapKeyDown(const KeyState& key);
+
+ /**
+ * @brief Map a key up event.
+ * @see mapKey
+ */
+ bool mapKeyUp(const KeyState& key);
+
private:
typedef List<HardwareKey*>::iterator Iterator;