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

kenny-d at users.sourceforge.net kenny-d at users.sourceforge.net
Wed Jul 9 13:30:49 CEST 2008


Revision: 32975
          http://scummvm.svn.sourceforge.net/scummvm/?rev=32975&view=rev
Author:   kenny-d
Date:     2008-07-09 04:30:49 -0700 (Wed, 09 Jul 2008)

Log Message:
-----------
- keyboard fully tested with multiple modes - key press events successfully passed back to engine, mode switching also works
- added support to re-parse keyboard pack if overlay size changes (to find more suitable layouts)

Modified Paths:
--------------
    scummvm/branches/gsoc2008-vkeybd/backends/common/virtual-keyboard-parser.cpp
    scummvm/branches/gsoc2008-vkeybd/backends/common/virtual-keyboard-parser.h
    scummvm/branches/gsoc2008-vkeybd/backends/common/virtual-keyboard.cpp
    scummvm/branches/gsoc2008-vkeybd/backends/common/virtual-keyboard.h
    scummvm/branches/gsoc2008-vkeybd/common/image-map.cpp
    scummvm/branches/gsoc2008-vkeybd/common/image-map.h

Modified: scummvm/branches/gsoc2008-vkeybd/backends/common/virtual-keyboard-parser.cpp
===================================================================
--- scummvm/branches/gsoc2008-vkeybd/backends/common/virtual-keyboard-parser.cpp	2008-07-09 10:52:46 UTC (rev 32974)
+++ scummvm/branches/gsoc2008-vkeybd/backends/common/virtual-keyboard-parser.cpp	2008-07-09 11:30:49 UTC (rev 32975)
@@ -45,6 +45,16 @@
 	_closedCallbacks["mode"]     = &VirtualKeyboardParser::parserCallback_ModeClosed;
 }
 
+void VirtualKeyboardParser::cleanup() {
+	_mode = 0;
+	_kbdParsed = false;
+	_initialModeName.clear();
+	if (_parseMode == kParseFull) {
+		// reset keyboard to remove existing config
+		_keyboard->reset();
+	}
+}
+
 bool VirtualKeyboardParser::keyCallback(Common::String keyName) {
 	if (!_callbacks.contains(_activeKey.top()->name))
 		return parserError("%s is not a valid key name.", keyName.c_str());
@@ -70,6 +80,10 @@
 	if (_kbdParsed)
 		return parserError("Only a single keyboard element is allowed");
 
+	// if not full parse then we're done
+	if (_parseMode == kParseCheckResolutions)
+		return true;
+
 	if (!kbdNode->values.contains("initial_mode"))
 		return parserError("Keyboard element must contain initial_mode attribute");
 
@@ -118,47 +132,67 @@
 
 	Common::String name = modeNode->values["name"];
 
-	if (_keyboard->_modes.contains(name))
-		return parserError("Mode '%s' has already been defined", name.c_str());
+	if (_parseMode == kParseFull) {
+		// if full parse then add new mode to keyboard
 
-	// create new mode
-	VirtualKeyboard::Mode mode;
-	mode.name = name;
-	_keyboard->_modes[name] = mode;
-	_mode = &(_keyboard->_modes[name]);
-	
-	// if this is the keyboard's initial mode
-	// then set it to be the current mode
-	if (name == _initialModeName)
-		_keyboard->_initialMode = _mode;
+		if (_keyboard->_modes.contains(name))
+			return parserError("Mode '%s' has already been defined", name.c_str());
 
+		VirtualKeyboard::Mode mode;
+		mode.name = name;
+		_keyboard->_modes[name] = mode;
+		_mode = &(_keyboard->_modes[name]);
+
+		// if this is the keyboard's initial mode
+		// then set it to be the current mode
+		if (name == _initialModeName)
+			_keyboard->_initialMode = _mode;
+	} else
+		_mode = &(_keyboard->_modes[name]);
+
 	Common::String resolutions = modeNode->values["resolutions"];
-	Common::StringTokenizer tok(resolutions, " ,");
+	Common::StringTokenizer tok (resolutions, " ,");
 
-	uint16 scrX = g_system->getOverlayWidth(), scrY = g_system->getOverlayHeight();
+	uint16 scrW = _keyboard->_screenWidth, scrH = _keyboard->_screenHeight;
 	uint32 diff = 0xFFFFFFFF;
-
+	Common::String newResolution;
 	for (Common::String res = tok.nextToken(); res.size() > 0; res = tok.nextToken()) {
-		int resX, resY;
-		if (sscanf(res.c_str(), "%dx%d", &resX, &resY) != 2) {
-			parserError("Invalid resolution specification");
+		// TODO: improve the resolution selection
+		int resW, resH;
+		if (sscanf(res.c_str(), "%dx%d", &resW, &resH) != 2) {
+			return parserError("Invalid resolution specification");
 		} else {
-			if (resX == scrX && resY == scrY) {
-				_mode->resolution = res;
+			if (resW == scrW && resH == scrH) {
+				newResolution = res;
 				break;
 			} else {
-				uint16 newDiff = ABS(scrX - resX) + ABS(scrY - resY);
+				uint16 newDiff = ABS(scrW - resW) + ABS(scrH - resH);
 				if (newDiff < diff) {
 					diff = newDiff;
-					_mode->resolution = res;
+					newResolution = res;
 				}
 			}
 		}
 	}
 
-	if (_mode->resolution.empty())
+	if (newResolution.empty())
 		return parserError("No acceptable resolution was found");
 
+	if (_parseMode == kParseCheckResolutions) {
+		if (_mode->resolution == newResolution) {
+			modeNode->ignore = true;
+			return true;
+		} else {
+			// remove data relating to old resolution
+			ImageMan.unregisterSurface(_mode->bitmapName);
+			_mode->bitmapName.clear();
+			_mode->image = 0;
+			_mode->imageMap.removeAllAreas();
+		}
+	}
+
+	_mode->resolution = newResolution;
+	
 	return true;
 }
 
@@ -181,6 +215,10 @@
 
 	assert(_mode);
 
+	// if just checking resolutions we're done
+	if (_parseMode == kParseCheckResolutions)
+		return true;
+
 	Common::String name = evtNode->values["name"];
 	if (_mode->events.contains(name))
 		return parserError("Event '%s' has already been defined", name.c_str());

Modified: scummvm/branches/gsoc2008-vkeybd/backends/common/virtual-keyboard-parser.h
===================================================================
--- scummvm/branches/gsoc2008-vkeybd/backends/common/virtual-keyboard-parser.h	2008-07-09 10:52:46 UTC (rev 32974)
+++ scummvm/branches/gsoc2008-vkeybd/backends/common/virtual-keyboard-parser.h	2008-07-09 11:30:49 UTC (rev 32975)
@@ -31,30 +31,35 @@
 
 namespace Common {
 
+enum ParseMode {
+	kParseFull,				// when loading keyboard pack for first time
+	kParseCheckResolutions  // when re-parsing following a change in screen size
+};
+
 class VirtualKeyboardParser : public Common::XMLParser {
 
 	typedef bool (VirtualKeyboardParser::*ParserCallback)();
 
 public:
 
+
 	VirtualKeyboardParser(VirtualKeyboard *kbd);
+	void setParseMode(ParseMode m) {
+		_parseMode = m;
+	}
 
 protected:
 	VirtualKeyboard *_keyboard;
 
 	/** internal state variables of parser */
+	ParseMode _parseMode;
 	VirtualKeyboard::Mode *_mode; // pointer to mode currently being parsed
-	bool _modeParsed;
 	Common::String _initialModeName;  // name of initial keyboard mode
 	bool _kbdParsed;
 
 	bool keyCallback(Common::String keyName);
 	bool closedKeyCallback(Common::String keyName);
-	void cleanup() {
-		_mode = 0;
-		_kbdParsed = _modeParsed = false;
-		_initialModeName.clear();
-	}
+	void cleanup();
 
 	bool parserCallback_Keyboard();
 	bool parserCallback_Mode();

Modified: scummvm/branches/gsoc2008-vkeybd/backends/common/virtual-keyboard.cpp
===================================================================
--- scummvm/branches/gsoc2008-vkeybd/backends/common/virtual-keyboard.cpp	2008-07-09 10:52:46 UTC (rev 32974)
+++ scummvm/branches/gsoc2008-vkeybd/backends/common/virtual-keyboard.cpp	2008-07-09 11:30:49 UTC (rev 32975)
@@ -65,8 +65,6 @@
 }
 
 bool VirtualKeyboard::loadKeyboardPack(Common::String packName) {
-	// reset to default settings
-	reset();
 
 	if (ConfMan.hasKey("extrapath"))
 		Common::File::addDefaultDirectoryRecursive(ConfMan.get("extrapath"));
@@ -108,6 +106,7 @@
 		return false;
 	}
 
+	_parser->setParseMode(kParseFull);
 	_loaded = _parser->parse();
 	if (_loaded)
 		printf("Keyboard pack '%s' loaded successfully!\n", packName.c_str());
@@ -148,11 +147,11 @@
 	_kbdBound.moveTo(posX, posY);
 }
 
-void VirtualKeyboard::checkResolution()
+bool VirtualKeyboard::checkModeResolutions()
 {
-	// check if there is a more suitable resolution
-	// in the keyboard pack than the current one
-	// based on the _screenWidth & _screenHeight
+	_parser->setParseMode(kParseCheckResolutions);
+	_loaded = _parser->parse();
+	return _loaded;
 }
 	
 void VirtualKeyboard::move(int16 x, int16 y) {
@@ -215,9 +214,14 @@
 }
 
 void VirtualKeyboard::show() {
+	if (!_loaded) {
+		warning("Keyboard not loaded therefore can't be shown");
+		return;
+	}
 	if (_screenWidth != _system->getOverlayWidth() || _screenHeight != _system->getOverlayHeight()) {
 		_screenWidth = _system->getOverlayWidth();
 		_screenHeight = _system->getOverlayHeight();
+		if (!checkModeResolutions()) return;
 	}
 	switchMode(_initialMode);
 	_displaying = true;
@@ -272,7 +276,8 @@
 			case Common::EVENT_SCREEN_CHANGED:
 				_screenWidth = _system->getOverlayWidth();
 				_screenHeight = _system->getOverlayHeight();
-				checkResolution();
+				if (!checkModeResolutions())
+					_displaying = false;
 				break;
 			case Common::EVENT_QUIT:
 				_system->quit();
@@ -280,6 +285,8 @@
 			default:
 				break;
 			}
+			// TODO - remove this line ?
+			if (!_displaying) break;
 		}
 	}
 	// clear keyboard from overlay

Modified: scummvm/branches/gsoc2008-vkeybd/backends/common/virtual-keyboard.h
===================================================================
--- scummvm/branches/gsoc2008-vkeybd/backends/common/virtual-keyboard.h	2008-07-09 10:52:46 UTC (rev 32974)
+++ scummvm/branches/gsoc2008-vkeybd/backends/common/virtual-keyboard.h	2008-07-09 11:30:49 UTC (rev 32975)
@@ -114,7 +114,7 @@
 
 	// TODO : sort order of all this stuff
 	void reset();
-	void checkResolution();
+	bool checkModeResolutions();
 	void setDefaultPosition();
 	void move(int16 x, int16 y);
 	void switchMode(Mode *newMode);

Modified: scummvm/branches/gsoc2008-vkeybd/common/image-map.cpp
===================================================================
--- scummvm/branches/gsoc2008-vkeybd/common/image-map.cpp	2008-07-09 10:52:46 UTC (rev 32974)
+++ scummvm/branches/gsoc2008-vkeybd/common/image-map.cpp	2008-07-09 11:30:49 UTC (rev 32975)
@@ -28,10 +28,7 @@
 namespace Common {
 
 ImageMap::~ImageMap() {
-	HashMap<String, Shape*>::iterator it;
-	for (it = _areas.begin(); it != _areas.end(); it++) {
-		delete it->_value;
-	}
+	removeAllAreas();
 }
 
 Rect *ImageMap::createRectArea(const String& id) {
@@ -54,22 +51,21 @@
 	return p;
 }
 
-/*
-void ImageMap::addMapArea(Shape *shape, const String& target) {
-	if (_areas.contains(target)) {
-		warning("Image map already contains an area with target of '%s'");
+void ImageMap::removeArea(const String& id) {
+	if (!_areas.contains(id))
 		return;
-	}
-	_areas[target] = shape;
+	delete _areas[id];
+	_areas.erase(id);
 }
-void ImageMap::addRectMapArea(const Rect& rect, const String& target) {
-	areas.push_back(MapArea(rect, target));
-}
 
-void ImageMap::addPolygonMapArea(const Polygon& poly, const String& target) {
-	areas.push_back(MapArea(poly, target));
+void ImageMap::removeAllAreas() {
+	HashMap<String, Shape*>::iterator it;
+	for (it = _areas.begin(); it != _areas.end(); it++) {
+		delete it->_value;
+	}
+	_areas.clear();
 }
-*/
+
 String ImageMap::findMapArea(int16 x, int16 y) {
 	HashMap<String, Shape*>::iterator it;
 	for (it = _areas.begin(); it != _areas.end(); it++) {

Modified: scummvm/branches/gsoc2008-vkeybd/common/image-map.h
===================================================================
--- scummvm/branches/gsoc2008-vkeybd/common/image-map.h	2008-07-09 10:52:46 UTC (rev 32974)
+++ scummvm/branches/gsoc2008-vkeybd/common/image-map.h	2008-07-09 11:30:49 UTC (rev 32975)
@@ -33,32 +33,6 @@
 
 namespace Common {
 
-class MapArea {
-public:
-	MapArea() : _shape(0), _target() {}
-	MapArea(const Rect& r, const String& t) : _target(t) {
-		_shape = new Rect(r);
-	}
-	MapArea(const Polygon& p, const String& t) : _target(t) {
-		_shape = new Polygon(p);
-	}
-	virtual ~MapArea() {
-		delete _shape;
-	}
-
-	virtual bool contains(int x, int y) {
-		return _shape->contains(x, y);
-	}
-
-	String getTarget() { return _target; }
-
-protected:
-	/* shape defining the MapArea's boundary */
-	Shape *_shape;
-	/* string describing the target of MapArea */
-	String _target;
-};
-
 class ImageMap {
 
 public:
@@ -67,6 +41,8 @@
 	
 	Rect *createRectArea(const String& id);
 	Polygon *createPolygonArea(const String& id);
+	void removeArea(const String& id);
+	void removeAllAreas();
 
 	//void addMapArea(Shape *shape, const String& target);
 	/*void addRectMapArea(const Rect& rect, const String& target);


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