SWORD25: Simplify InputEngine::(un)persist

svn-id: r53897
This commit is contained in:
Max Horn 2010-10-28 00:24:53 +00:00
parent dac9493069
commit 7e1d78cab1
3 changed files with 42 additions and 142 deletions

View File

@ -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,135 +220,62 @@ void InputEngine::setMouseY(int posY) {
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;
}
}
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::setCommandCallback(CommandCallback callback) {
_commandCallback = callback;
}
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;
}
}
// 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;
}
}
// 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"));
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);
_characterCallbacks.push_back(reinterpret_cast<CharacterCallback>(CallbackRegistry::instance().resolveCallbackFunction(callbackFunctionName)));
}
reader.read(callbackFunctionName);
assert(callbackFunctionName == "LuaCharacterCB");
return reader.isGood();
}

View File

@ -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,46 +257,32 @@ public:
*/
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 CallbackPtr CommandCallback;
typedef void (*CommandCallback)(int command);
/**
* 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);
/**
* 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 setCommandCallback(CommandCallback callback);
void reportCharacter(byte character);
void reportCommand(KEY_COMMANDS command);
@ -328,8 +313,8 @@ private:
uint _lastLeftClickTime;
int _lastLeftClickMouseX;
int _lastLeftClickMouseY;
Common::List<CommandCallback> _commandCallbacks;
Common::List<CharacterCallback> _characterCallbacks;
CommandCallback _commandCallback;
CharacterCallback _characterCallback;
};
} // End of namespace Sword25

View File

@ -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 init(lua_State *L) {
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önnen.
static bool firstCall = true;
if (firstCall) {
firstCall = false;
pIE->registerCharacterCallback(theCharacterCallback);
pIE->registerCommandCallback(theCommandCallback);
}
pIE->update();
return 0;
}
@ -291,8 +280,8 @@ bool InputEngine::registerScriptBindings() {
assert(commandCallbackPtr == 0);
commandCallbackPtr = new CommandCallbackClass(L);
CallbackRegistry::instance().registerCallbackFunction("LuaCommandCB", theCommandCallback);
CallbackRegistry::instance().registerCallbackFunction("LuaCharacterCB", theCharacterCallback);
setCharacterCallback(theCharacterCallback);
setCommandCallback(theCommandCallback);
return true;
}