[Scummvm-cvs-logs] SF.net SVN: scummvm:[33262] scummvm/branches/gsoc2008-vkeybd

kenny-d at users.sourceforge.net kenny-d at users.sourceforge.net
Thu Jul 24 12:00:57 CEST 2008


Revision: 33262
          http://scummvm.svn.sourceforge.net/scummvm/?rev=33262&view=rev
Author:   kenny-d
Date:     2008-07-24 10:00:56 +0000 (Thu, 24 Jul 2008)

Log Message:
-----------
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

Modified Paths:
--------------
    scummvm/branches/gsoc2008-vkeybd/backends/common/hardware-key.h
    scummvm/branches/gsoc2008-vkeybd/backends/common/keymap-manager.cpp
    scummvm/branches/gsoc2008-vkeybd/backends/common/keymap-manager.h
    scummvm/branches/gsoc2008-vkeybd/backends/common/keymap.cpp
    scummvm/branches/gsoc2008-vkeybd/backends/common/keymap.h
    scummvm/branches/gsoc2008-vkeybd/backends/common/keymapper.cpp
    scummvm/branches/gsoc2008-vkeybd/backends/common/keymapper.h
    scummvm/branches/gsoc2008-vkeybd/backends/common/user-action.h
    scummvm/branches/gsoc2008-vkeybd/backends/common/virtual-keyboard.cpp
    scummvm/branches/gsoc2008-vkeybd/backends/events/default/default-events.cpp
    scummvm/branches/gsoc2008-vkeybd/backends/events/default/default-events.h
    scummvm/branches/gsoc2008-vkeybd/dists/msvc8/scummvm.vcproj

Added Paths:
-----------
    scummvm/branches/gsoc2008-vkeybd/backends/common/user-action.cpp

Modified: scummvm/branches/gsoc2008-vkeybd/backends/common/hardware-key.h
===================================================================
--- scummvm/branches/gsoc2008-vkeybd/backends/common/hardware-key.h	2008-07-24 09:42:44 UTC (rev 33261)
+++ scummvm/branches/gsoc2008-vkeybd/backends/common/hardware-key.h	2008-07-24 10:00:56 UTC (rev 33262)
@@ -80,9 +80,9 @@
 		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!");
 		}
 	}
 

Modified: scummvm/branches/gsoc2008-vkeybd/backends/common/keymap-manager.cpp
===================================================================
--- scummvm/branches/gsoc2008-vkeybd/backends/common/keymap-manager.cpp	2008-07-24 09:42:44 UTC (rev 33261)
+++ scummvm/branches/gsoc2008-vkeybd/backends/common/keymap-manager.cpp	2008-07-24 10:00:56 UTC (rev 33262)
@@ -32,6 +32,11 @@
 		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 @@
 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);
+	}
 }
 
 

Modified: scummvm/branches/gsoc2008-vkeybd/backends/common/keymap-manager.h
===================================================================
--- scummvm/branches/gsoc2008-vkeybd/backends/common/keymap-manager.h	2008-07-24 09:42:44 UTC (rev 33261)
+++ scummvm/branches/gsoc2008-vkeybd/backends/common/keymap-manager.h	2008-07-24 10:00:56 UTC (rev 33262)
@@ -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 @@
 		KeymapMap _keymaps;
 	};
 
+	void registerHardwareKeySet(HardwareKeySet *keys);
+
 	void registerDefaultGlobalKeymap(Keymap *map);
 	void registerGlobalKeymap(const String& name, Keymap *map);
 
@@ -46,11 +49,14 @@
 
 	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

Modified: scummvm/branches/gsoc2008-vkeybd/backends/common/keymap.cpp
===================================================================
--- scummvm/branches/gsoc2008-vkeybd/backends/common/keymap.cpp	2008-07-24 09:42:44 UTC (rev 33261)
+++ scummvm/branches/gsoc2008-vkeybd/backends/common/keymap.cpp	2008-07-24 10:00:56 UTC (rev 33262)
@@ -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 @@
 	_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);
 }
 

Modified: scummvm/branches/gsoc2008-vkeybd/backends/common/keymap.h
===================================================================
--- scummvm/branches/gsoc2008-vkeybd/backends/common/keymap.h	2008-07-24 09:42:44 UTC (rev 33261)
+++ scummvm/branches/gsoc2008-vkeybd/backends/common/keymap.h	2008-07-24 10:00:56 UTC (rev 33262)
@@ -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 @@
 	 * adding it at the back of the internal array
 	 * @param action the UserAction to add
 	 */
-	void addAction(const UserAction& action);
+	void addAction(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;
+	UserAction *getUserAction(int32 id);
 
 	/**
 	 * Get a read-only array of all the UserActions contained in this Keymap
@@ -72,7 +57,22 @@
 	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;
 

Modified: scummvm/branches/gsoc2008-vkeybd/backends/common/keymapper.cpp
===================================================================
--- scummvm/branches/gsoc2008-vkeybd/backends/common/keymapper.cpp	2008-07-24 09:42:44 UTC (rev 33261)
+++ scummvm/branches/gsoc2008-vkeybd/backends/common/keymapper.cpp	2008-07-24 10:00:56 UTC (rev 33262)
@@ -7,19 +7,12 @@
 	_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;
+	_keymapMan->registerHardwareKeySet(keys);
 }
 
-const HardwareKeySet *Keymapper::getHardwareKeySet() const {
-	return _hardwareKeys;
-}
-
 void Keymapper::addGlobalKeyMap(const String& name, Keymap *keymap) {
 	_keymapMan->registerGlobalKeymap(name, keymap);
 }
@@ -35,7 +28,7 @@
 
 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 @@
 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;

Modified: scummvm/branches/gsoc2008-vkeybd/backends/common/keymapper.h
===================================================================
--- scummvm/branches/gsoc2008-vkeybd/backends/common/keymapper.h	2008-07-24 09:42:44 UTC (rev 33261)
+++ scummvm/branches/gsoc2008-vkeybd/backends/common/keymapper.h	2008-07-24 10:00:56 UTC (rev 33262)
@@ -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:
@@ -20,11 +24,6 @@
 	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.
@@ -88,8 +87,6 @@
 
 	Keymap *_currentMap;
 
-	const HardwareKeySet *_hardwareKeys;
-
 };
 
 } // end of namespace Common

Added: scummvm/branches/gsoc2008-vkeybd/backends/common/user-action.cpp
===================================================================
--- scummvm/branches/gsoc2008-vkeybd/backends/common/user-action.cpp	                        (rev 0)
+++ scummvm/branches/gsoc2008-vkeybd/backends/common/user-action.cpp	2008-07-24 10:00:56 UTC (rev 33262)
@@ -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
\ No newline at end of file


Property changes on: scummvm/branches/gsoc2008-vkeybd/backends/common/user-action.cpp
___________________________________________________________________
Added: svn:mime-type
   + text/plain
Added: svn:keywords
   + Date Rev Author URL Id
Added: svn:eol-style
   + native

Modified: scummvm/branches/gsoc2008-vkeybd/backends/common/user-action.h
===================================================================
--- scummvm/branches/gsoc2008-vkeybd/backends/common/user-action.h	2008-07-24 09:42:44 UTC (rev 33261)
+++ scummvm/branches/gsoc2008-vkeybd/backends/common/user-action.h	2008-07-24 10:00:56 UTC (rev 33262)
@@ -8,7 +8,9 @@
 namespace Common {
 
 struct HardwareKey;
+class Keymap;
 
+
 enum UserActionType {
 	kGenericUserActionType,
 
@@ -48,21 +50,22 @@
 	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

Modified: scummvm/branches/gsoc2008-vkeybd/backends/common/virtual-keyboard.cpp
===================================================================
--- scummvm/branches/gsoc2008-vkeybd/backends/common/virtual-keyboard.cpp	2008-07-24 09:42:44 UTC (rev 33261)
+++ scummvm/branches/gsoc2008-vkeybd/backends/common/virtual-keyboard.cpp	2008-07-24 10:00:56 UTC (rev 33262)
@@ -103,7 +103,7 @@
 				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;
 		}

Modified: scummvm/branches/gsoc2008-vkeybd/backends/events/default/default-events.cpp
===================================================================
--- scummvm/branches/gsoc2008-vkeybd/backends/events/default/default-events.cpp	2008-07-24 09:42:44 UTC (rev 33261)
+++ scummvm/branches/gsoc2008-vkeybd/backends/events/default/default-events.cpp	2008-07-24 10:00:56 UTC (rev 33262)
@@ -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"

Modified: scummvm/branches/gsoc2008-vkeybd/backends/events/default/default-events.h
===================================================================
--- scummvm/branches/gsoc2008-vkeybd/backends/events/default/default-events.h	2008-07-24 09:42:44 UTC (rev 33261)
+++ scummvm/branches/gsoc2008-vkeybd/backends/events/default/default-events.h	2008-07-24 10:00:56 UTC (rev 33262)
@@ -29,9 +29,12 @@
 #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
 DefaultEventManager to use a "boss" derived from this class:

Modified: scummvm/branches/gsoc2008-vkeybd/dists/msvc8/scummvm.vcproj
===================================================================
--- scummvm/branches/gsoc2008-vkeybd/dists/msvc8/scummvm.vcproj	2008-07-24 09:42:44 UTC (rev 33261)
+++ scummvm/branches/gsoc2008-vkeybd/dists/msvc8/scummvm.vcproj	2008-07-24 10:00:56 UTC (rev 33262)
@@ -1085,6 +1085,10 @@
 					>
 				</File>
 				<File
+					RelativePath="..\..\backends\common\user-action.cpp"
+					>
+				</File>
+				<File
 					RelativePath="..\..\backends\common\user-action.h"
 					>
 				</File>


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.




More information about the Scummvm-git-logs mailing list