[Scummvm-cvs-logs] SF.net SVN: scummvm:[53897] scummvm/trunk/engines/sword25/input

fingolfin at users.sourceforge.net fingolfin at users.sourceforge.net
Thu Oct 28 02:24:53 CEST 2010


Revision: 53897
          http://scummvm.svn.sourceforge.net/scummvm/?rev=53897&view=rev
Author:   fingolfin
Date:     2010-10-28 00:24:53 +0000 (Thu, 28 Oct 2010)

Log Message:
-----------
SWORD25: Simplify InputEngine::(un)persist

Modified Paths:
--------------
    scummvm/trunk/engines/sword25/input/inputengine.cpp
    scummvm/trunk/engines/sword25/input/inputengine.h
    scummvm/trunk/engines/sword25/input/inputengine_script.cpp

Modified: scummvm/trunk/engines/sword25/input/inputengine.cpp
===================================================================
--- scummvm/trunk/engines/sword25/input/inputengine.cpp	2010-10-27 23:11:15 UTC (rev 53896)
+++ scummvm/trunk/engines/sword25/input/inputengine.cpp	2010-10-28 00:24:53 UTC (rev 53897)
@@ -39,7 +39,6 @@
 #include "common/system.h"
 #include "common/util.h"
 #include "sword25/kernel/kernel.h"
-#include "sword25/kernel/callbackregistry.h"
 #include "sword25/kernel/inputpersistenceblock.h"
 #include "sword25/kernel/outputpersistenceblock.h"
 #include "sword25/input/inputengine.h"
@@ -221,136 +220,63 @@
 	g_system->warpMouse(_mouseX, _mouseY);
 }
 
-bool InputEngine::registerCharacterCallback(CharacterCallback callback) {
-	if (Common::find(_characterCallbacks.begin(), _characterCallbacks.end(), callback) == _characterCallbacks.end()) {
-		_characterCallbacks.push_back(callback);
-		return true;
-	} else {
-		BS_LOG_WARNINGLN("Tried to register an CharacterCallback that was already registered.");
-		return false;
-	}
+void InputEngine::setCharacterCallback(CharacterCallback callback) {
+	_characterCallback = callback;
 }
 
-bool InputEngine::unregisterCharacterCallback(CharacterCallback callback) {
-	Common::List<CharacterCallback>::iterator callbackIter = Common::find(_characterCallbacks.begin(),
-	        _characterCallbacks.end(), callback);
-	if (callbackIter != _characterCallbacks.end()) {
-		_characterCallbacks.erase(callbackIter);
-		return true;
-	} else {
-		BS_LOG_WARNINGLN("Tried to unregister an CharacterCallback that was not previously registered.");
-		return false;
-	}
+void InputEngine::setCommandCallback(CommandCallback callback) {
+	_commandCallback = callback;
 }
 
-bool InputEngine::registerCommandCallback(CommandCallback callback) {
-	if (Common::find(_commandCallbacks.begin(), _commandCallbacks.end(), callback) == _commandCallbacks.end()) {
-		_commandCallbacks.push_back(callback);
-		return true;
-	} else {
-		BS_LOG_WARNINGLN("Tried to register an CommandCallback that was already registered.");
-		return false;
-	}
-}
-
-bool InputEngine::unregisterCommandCallback(CommandCallback callback) {
-	Common::List<CommandCallback>::iterator callbackIter =
-	    Common::find(_commandCallbacks.begin(), _commandCallbacks.end(), callback);
-	if (callbackIter != _commandCallbacks.end()) {
-		_commandCallbacks.erase(callbackIter);
-		return true;
-	} else {
-		BS_LOG_WARNINGLN("Tried to unregister an CommandCallback that was not previously registered.");
-		return false;
-	}
-}
-
 void InputEngine::reportCharacter(byte character) {
-	Common::List<CharacterCallback>::const_iterator callbackIter = _characterCallbacks.begin();
-	while (callbackIter != _characterCallbacks.end()) {
-		// Iterator vor dem Aufruf erhöhen und im Folgendem auf einer Kopie arbeiten.
-		// Dieses Vorgehen ist notwendig da der Iterator möglicherweise von der Callbackfunktion durch das Deregistrieren des Callbacks
-		// invalidiert wird.
-		Common::List<CharacterCallback>::const_iterator curCallbackIter = callbackIter;
-		++callbackIter;
-
-		(*curCallbackIter)(character);
-	}
+	if (_characterCallback)
+		(*_characterCallback)(character);
 }
 
 void InputEngine::reportCommand(KEY_COMMANDS command) {
-	Common::List<CommandCallback>::const_iterator callbackIter = _commandCallbacks.begin();
-	while (callbackIter != _commandCallbacks.end()) {
-		// Iterator vor dem Aufruf erhöhen und im Folgendem auf einer Kopie arbeiten.
-		// Dieses Vorgehen ist notwendig da der Iterator möglicherweise von der Callbackfunktion durch das Deregistrieren des Callbacks
-		// invalidiert wird.
-		Common::List<CommandCallback>::const_iterator curCallbackIter = callbackIter;
-		++callbackIter;
-
-		(*curCallbackIter)(command);
-	}
+	if (_commandCallback)
+		(*_commandCallback)(command);
 }
 
 bool InputEngine::persist(OutputPersistenceBlock &writer) {
-	// Anzahl an Command-Callbacks persistieren.
-	writer.write(_commandCallbacks.size());
+	// Write out the number of command callbacks and their names.
+	// Note: We do this only for compatibility with older engines resp.
+	// the original engine.
+	writer.write((uint)1);
+	writer.write(Common::String("LuaCommandCB"));
 
-	// Alle Command-Callbacks einzeln persistieren.
-	{
-		Common::List<CommandCallback>::const_iterator It = _commandCallbacks.begin();
-		while (It != _commandCallbacks.end()) {
-			writer.write(CallbackRegistry::instance().resolveCallbackPointer(*It));
-			++It;
-		}
-	}
+	// Write out the number of command callbacks and their names.
+	// Note: We do this only for compatibility with older engines resp.
+	// the original engine.
+	writer.write((uint)1);
+	writer.write(Common::String("LuaCharacterCB"));
 
-	// Anzahl an Character-Callbacks persistieren.
-	writer.write(_characterCallbacks.size());
-
-	// Alle Character-Callbacks einzeln persistieren.
-	{
-		Common::List<CharacterCallback>::const_iterator It = _characterCallbacks.begin();
-		while (It != _characterCallbacks.end()) {
-			writer.write(CallbackRegistry::instance().resolveCallbackPointer(*It));
-			++It;
-		}
-	}
-
 	return true;
 }
 
 bool InputEngine::unpersist(InputPersistenceBlock &reader) {
-	// Command-Callbackliste leeren.
-	_commandCallbacks.clear();
+	Common::String callbackFunctionName;
 
-	// Anzahl an Command-Callbacks lesen.
+	// Read number of command callbacks and their names.
+	// Note: We do this only for compatibility with older engines resp.
+	// the original engine.
 	uint commandCallbackCount;
 	reader.read(commandCallbackCount);
+	assert(commandCallbackCount == 1);
 
-	// Alle Command-Callbacks wieder herstellen.
-	for (uint i = 0; i < commandCallbackCount; ++i) {
-		Common::String callbackFunctionName;
-		reader.read(callbackFunctionName);
+	reader.read(callbackFunctionName);
+	assert(callbackFunctionName == "LuaCommandCB");
 
-		_commandCallbacks.push_back(reinterpret_cast<CommandCallback>(
-		                                 CallbackRegistry::instance().resolveCallbackFunction(callbackFunctionName)));
-	}
-
-	// Character-Callbackliste leeren.
-	_characterCallbacks.clear();
-
-	// Anzahl an Character-Callbacks lesen.
+	// Read number of character callbacks and their names.
+	// Note: We do this only for compatibility with older engines resp.
+	// the original engine.
 	uint characterCallbackCount;
 	reader.read(characterCallbackCount);
+	assert(characterCallbackCount == 1);
 
-	// Alle Character-Callbacks wieder herstellen.
-	for (uint i = 0; i < characterCallbackCount; ++i) {
-		Common::String callbackFunctionName;
-		reader.read(callbackFunctionName);
+	reader.read(callbackFunctionName);
+	assert(callbackFunctionName == "LuaCharacterCB");
 
-		_characterCallbacks.push_back(reinterpret_cast<CharacterCallback>(CallbackRegistry::instance().resolveCallbackFunction(callbackFunctionName)));
-	}
-
 	return reader.isGood();
 }
 

Modified: scummvm/trunk/engines/sword25/input/inputengine.h
===================================================================
--- scummvm/trunk/engines/sword25/input/inputengine.h	2010-10-27 23:11:15 UTC (rev 53896)
+++ scummvm/trunk/engines/sword25/input/inputengine.h	2010-10-28 00:24:53 UTC (rev 53897)
@@ -50,7 +50,6 @@
 #include "sword25/kernel/common.h"
 #include "sword25/kernel/service.h"
 #include "sword25/kernel/persistable.h"
-#include "sword25/kernel/callbackregistry.h"
 
 namespace Sword25 {
 
@@ -258,47 +257,33 @@
 	 */
 	bool wasKeyDown(uint keyCode);
 
-	typedef CallbackPtr CharacterCallback;
+	typedef void (*CharacterCallback)(int command);
 
 	/**
 	 * Registers a callback function for keyboard input.
 	 *
-	 * The callbacks that are registered with this function will be called whenever an
+	 * The callback that is registered with this function will be called whenever an
 	 * input key is pressed. A letter entry is different from the query using the
-	 * methods IsKeyDown () and WasKeyDown () in the sense that are treated instead
+	 * methods isKeyDown() and wasKeyDown() in the sense that are treated instead
 	 * of actual scan-coded letters. These were taken into account, among other things:
 	 * the keyboard layout, the condition the Shift and Caps Lock keys and the repetition
 	 * of longer holding the key.
 	 * The input of strings by the user through use of callbacks should be implemented.
-	 * @return              Returns true if the function was registered, otherwise false.
 	*/
-	bool registerCharacterCallback(CallbackPtr callback);
+	void setCharacterCallback(CharacterCallback callback);
 
-	/**
-	 * De-registeres a previously registered callback function.
-	 * @return              Returns true if the function could be de-registered, otherwise false.
-	 */
-	bool unregisterCharacterCallback(CallbackPtr callback);
+	typedef void (*CommandCallback)(int command);
 
-	typedef CallbackPtr CommandCallback;
-
 	/**
 	 * Registers a callback function for the input of commands that can have influence on the string input
 	 *
-	 * The callbacks that are registered with this function will be called whenever the input service
+	 * The callback that is registered with this function will be called whenever the input service
 	 * has a key that affects the character string input. This could be the following keys:
 	 * Enter, End, Left, Right, ...
 	 * The input of strings by the user through the use of callbacks should be implemented.
-	 * @return              Returns true if the function was registered, otherwise false.
 	 */
-	bool registerCommandCallback(CallbackPtr callback);
+	void setCommandCallback(CommandCallback callback);
 
-	/**
-	 * Un-register a callback function for the input of commands that can have an influence on the string input.
-	 * @return              Returns true if the function could be de-registered, otherwise false.
-	 */
-	bool unregisterCommandCallback(CommandCallback callback);
-
 	void reportCharacter(byte character);
 	void reportCommand(KEY_COMMANDS command);
 
@@ -328,8 +313,8 @@
 	uint _lastLeftClickTime;
 	int _lastLeftClickMouseX;
 	int _lastLeftClickMouseY;
-	Common::List<CommandCallback> _commandCallbacks;
-	Common::List<CharacterCallback> _characterCallbacks;
+	CommandCallback _commandCallback;
+	CharacterCallback _characterCallback;
 };
 
 } // End of namespace Sword25

Modified: scummvm/trunk/engines/sword25/input/inputengine_script.cpp
===================================================================
--- scummvm/trunk/engines/sword25/input/inputengine_script.cpp	2010-10-27 23:11:15 UTC (rev 53896)
+++ scummvm/trunk/engines/sword25/input/inputengine_script.cpp	2010-10-28 00:24:53 UTC (rev 53897)
@@ -36,7 +36,6 @@
 #include "common/str.h"
 #include "sword25/kernel/common.h"
 #include "sword25/kernel/kernel.h"
-#include "sword25/kernel/callbackregistry.h"
 #include "sword25/script/script.h"
 #include "sword25/script/luabindhelper.h"
 #include "sword25/script/luacallback.h"
@@ -103,16 +102,6 @@
 static int update(lua_State *L) {
 	InputEngine *pIE = getIE();
 
-	// Beim ersten Aufruf der Update()-Methode werden die beiden Callbacks am Input-Objekt registriert.
-	// Dieses kann nicht in _RegisterScriptBindings() passieren, da diese Funktion vom Konstruktor der abstrakten Basisklasse aufgerufen wird und die
-	// Register...()-Methoden abstrakt sind, im Konstruktor der Basisklasse also nicht aufgerufen werden k\xF6nnen.
-	static bool firstCall = true;
-	if (firstCall) {
-		firstCall = false;
-		pIE->registerCharacterCallback(theCharacterCallback);
-		pIE->registerCommandCallback(theCommandCallback);
-	}
-
 	pIE->update();
 	return 0;
 }
@@ -291,8 +280,8 @@
 	assert(commandCallbackPtr == 0);
 	commandCallbackPtr = new CommandCallbackClass(L);
 
-	CallbackRegistry::instance().registerCallbackFunction("LuaCommandCB", theCommandCallback);
-	CallbackRegistry::instance().registerCallbackFunction("LuaCharacterCB", theCharacterCallback);
+	setCharacterCallback(theCharacterCallback);
+	setCommandCallback(theCommandCallback);
 
 	return true;
 }


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