diff options
author | Paul Gilbert | 2010-08-03 09:47:45 +0000 |
---|---|---|
committer | Eugene Sandulenko | 2010-10-12 22:21:24 +0000 |
commit | 86bef1508c3fb3c21a10e3f35305f6eaf641a33e (patch) | |
tree | c2a47a3cca578ba5b9b6a156ddbc576221f14beb /engines/sword25/input | |
parent | fab86ba22250c282139fb0c94a250499abdc875f (diff) | |
download | scummvm-rg350-86bef1508c3fb3c21a10e3f35305f6eaf641a33e.tar.gz scummvm-rg350-86bef1508c3fb3c21a10e3f35305f6eaf641a33e.tar.bz2 scummvm-rg350-86bef1508c3fb3c21a10e3f35305f6eaf641a33e.zip |
SWORD25: Converted the input engine to use the ScummVM event manager
svn-id: r53203
Diffstat (limited to 'engines/sword25/input')
-rw-r--r-- | engines/sword25/input/scummvminput.cpp (renamed from engines/sword25/input/stdwininput.cpp) | 298 | ||||
-rw-r--r-- | engines/sword25/input/scummvminput.h (renamed from engines/sword25/input/stdwininput.h) | 7 |
2 files changed, 142 insertions, 163 deletions
diff --git a/engines/sword25/input/stdwininput.cpp b/engines/sword25/input/scummvminput.cpp index 3da3a6747c..d0824cd0e4 100644 --- a/engines/sword25/input/stdwininput.cpp +++ b/engines/sword25/input/scummvminput.cpp @@ -32,36 +32,41 @@ * */ +#include "common/algorithm.h" +#include "common/events.h" +#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/stdwininput.h" +#include "sword25/input/scummvminput.h" -#include <algorithm> -using namespace std; +#define BS_LOG_PREFIX "SCUMMVMINPUT" -#define BS_LOG_PREFIX "WININPUT" +namespace Sword25 { + +#define DOUBLE_CLICK_TIME 500 +#define DOUBLE_CLICK_RECT_SIZE 4 // ----------------------------------------------------------------------------- -// Konstruktion / Destruktion +// Constructor / Destructor // ----------------------------------------------------------------------------- -BS_StdWinInput::BS_StdWinInput(BS_Kernel* pKernel) : - m_CurrentState(0), - m_LeftMouseDown(false), - m_RightMouseDown(false), - m_MouseX(0), - m_MouseY(0), - m_LeftDoubleClick(false), - m_DoubleClickTime(GetDoubleClickTime()), - m_DoubleClickRectWidth(GetSystemMetrics(SM_CXDOUBLECLK)), - m_DoubleClickRectHeight(GetSystemMetrics(SM_CYDOUBLECLK)), - m_LastLeftClickTime(0), - m_LastLeftClickMouseX(0), - m_LastLeftClickMouseY(0), - BS_InputEngine(pKernel) -{ +ScummVMInput::ScummVMInput(BS_Kernel *pKernel) : + m_CurrentState(0), + m_LeftMouseDown(false), + m_RightMouseDown(false), + m_MouseX(0), + m_MouseY(0), + m_LeftDoubleClick(false), + m_DoubleClickTime(DOUBLE_CLICK_TIME), + m_DoubleClickRectWidth(DOUBLE_CLICK_RECT_SIZE), + m_DoubleClickRectHeight(DOUBLE_CLICK_RECT_SIZE), + m_LastLeftClickTime(0), + m_LastLeftClickMouseX(0), + m_LastLeftClickMouseY(0), + BS_InputEngine(pKernel) { memset(m_KeyboardState[0], 0, sizeof(m_KeyboardState[0])); memset(m_KeyboardState[1], 0, sizeof(m_KeyboardState[1])); m_LeftMouseState[0] = false; @@ -70,105 +75,101 @@ BS_StdWinInput::BS_StdWinInput(BS_Kernel* pKernel) : m_RightMouseState[1] = false; } -BS_StdWinInput::~BS_StdWinInput() -{ +ScummVMInput::~ScummVMInput() { } // ----------------------------------------------------------------------------- -BS_Service * BS_StdWinInput_CreateObject(BS_Kernel* pKernel) { return new BS_StdWinInput(pKernel); } +BS_Service *ScummVMInput_CreateObject(BS_Kernel *pKernel) { return new ScummVMInput(pKernel); } // ----------------------------------------------------------------------------- -bool BS_StdWinInput::Init() -{ - // Keine Inialisierung notwendig +bool ScummVMInput::Init() { + // No initialisation needed return true; } // ----------------------------------------------------------------------------- -void BS_StdWinInput::Update() -{ - // Der Status wird nur aktualisiert, wenn das Applikationsfenster den Fokus hat, so wird verhindert, dass - // Eingaben verarbeitet werden, die eigentlich für eine andere Applikation gedacht waren. - if (BS_Kernel::GetInstance()->GetWindow()->HasFocus()) - { - m_CurrentState ^= 1; - - // Der Status der Eingabegeräte wird nur einmal pro Frame ausgelesen, damit für - // jeden Frame gleiche Anfragen die gleiche Antwort erhalten. - - POINT MousePos; - if (GetCursorPos(&MousePos)) - { - m_MouseX = MousePos.x - BS_Kernel::GetInstance()->GetWindow()->GetClientX(); - m_MouseY = MousePos.y - BS_Kernel::GetInstance()->GetWindow()->GetClientY(); +void ScummVMInput::Update() { + Common::Event event; + m_CurrentState ^= 1; + + // Loop through processing any pending events + bool handleEvents = true; + while (handleEvents && g_system->getEventManager()->pollEvent(event)) { + switch (event.type) { + case Common::EVENT_LBUTTONDOWN: + case Common::EVENT_LBUTTONUP: + m_LeftMouseDown = event.type == Common::EVENT_LBUTTONDOWN; + m_MouseX = event.mouse.x; m_MouseY = event.mouse.y; + handleEvents = false; + break; + case Common::EVENT_RBUTTONDOWN: + case Common::EVENT_RBUTTONUP: + m_RightMouseDown = event.type == Common::EVENT_RBUTTONDOWN; + m_MouseX = event.mouse.x; m_MouseY = event.mouse.y; + handleEvents = false; + break; + + case Common::EVENT_MOUSEMOVE: + m_MouseX = event.mouse.x; m_MouseY = event.mouse.y; + break; + + case Common::EVENT_KEYDOWN: + case Common::EVENT_KEYUP: + AlterKeyboardState(event.kbd.keycode, (event.type == Common::EVENT_KEYDOWN) ? 0x80 : 0); + break; + + default: + break; } - else - { - BS_LOG_ERRORLN("Call to GetCursorPos() failed."); - m_MouseX = 0; - m_MouseY = 0; - } - - GetKeyboardState(m_KeyboardState[m_CurrentState]); + } - m_LeftMouseDown = (GetAsyncKeyState(VK_LBUTTON) & 0x8000) != 0; - m_RightMouseDown = (GetAsyncKeyState(VK_RBUTTON) & 0x8000) != 0; - m_LeftMouseState[m_CurrentState] = m_LeftMouseDown; - m_RightMouseState[m_CurrentState] = m_RightMouseDown; + m_LeftMouseState[m_CurrentState] = m_LeftMouseDown; + m_RightMouseState[m_CurrentState] = m_RightMouseDown; - TestForLeftDoubleClick(); - } + TestForLeftDoubleClick(); } // ----------------------------------------------------------------------------- -bool BS_StdWinInput::IsLeftMouseDown() -{ +bool ScummVMInput::IsLeftMouseDown() { return m_LeftMouseDown; } // ----------------------------------------------------------------------------- -bool BS_StdWinInput::IsRightMouseDown() -{ +bool ScummVMInput::IsRightMouseDown() { return m_RightMouseDown; } // ----------------------------------------------------------------------------- -void BS_StdWinInput::TestForLeftDoubleClick() -{ - // Das Doppelklick-Flag wird gelöscht, für den Fall, dass im letzten Frame ein Doppelklick ausgetreten ist. +void ScummVMInput::TestForLeftDoubleClick() { m_LeftDoubleClick = false; - // Die linke Maustaste wurde geklickt, also muss getestet werden, ob ein Doppelklick vorliegt. - if (WasLeftMouseDown()) - { - // Die Zeit auslesen. + // Only bother checking for a double click if the left mouse button was clicked + if (WasLeftMouseDown()) { + // Get the time now unsigned int Now = BS_Kernel::GetInstance()->GetMilliTicks(); - // Ein Doppelklick wird erkannt, wenn: - // 1. Die zwei Klicks liegen nah genug zusammen. - // 2. Der Mauscursor wurde zwischen den Klicks nicht zu viel bewegt. + // A double click is signalled if + // 1. The two clicks are close enough together + // 2. The mouse cursor hasn't moved much if (Now - m_LastLeftClickTime <= m_DoubleClickTime && - abs(m_MouseX - m_LastLeftClickMouseX) <= m_DoubleClickRectWidth / 2 && - abs(m_MouseY - m_LastLeftClickMouseY) <= m_DoubleClickRectHeight / 2) - { + ABS(m_MouseX - m_LastLeftClickMouseX) <= m_DoubleClickRectWidth / 2 && + ABS(m_MouseY - m_LastLeftClickMouseY) <= m_DoubleClickRectHeight / 2) { m_LeftDoubleClick = true; - // Die Zeit und Position des letzten Linksklicks zurücksetzen, damit dieser Klick nicht als erster Klick eines weiteren Doppelklicks - // interpretiert wird. + // Reset the time and position of the last click, so that clicking is not + // interpreted as the first click of a further double-click m_LastLeftClickTime = 0; m_LastLeftClickMouseX = 0; m_LastLeftClickMouseY = 0; - } - else - { - // Es liegt kein Doppelklick vor, die Zeit und die Position dieses Klicks merken, für den Fall das dies der erste Klick eines - // zukünftigen Doppelklicks wird. + } else { + // There is no double click. Remember the position and time of the click, + // in case it's the first click of a double-click sequence m_LastLeftClickTime = Now; m_LastLeftClickMouseX = m_MouseX; m_LastLeftClickMouseY = m_MouseY; @@ -178,80 +179,74 @@ void BS_StdWinInput::TestForLeftDoubleClick() // ----------------------------------------------------------------------------- -bool BS_StdWinInput::IsLeftDoubleClick() -{ +void AlterKeyboardState(int keycode, byte newState) { + +} + +// ----------------------------------------------------------------------------- + +bool ScummVMInput::IsLeftDoubleClick() { return m_LeftDoubleClick; } // ----------------------------------------------------------------------------- -bool BS_StdWinInput::WasLeftMouseDown() -{ +bool ScummVMInput::WasLeftMouseDown() { return (m_LeftMouseState[m_CurrentState] == false) && (m_LeftMouseState[m_CurrentState ^ 1] == true); } // ----------------------------------------------------------------------------- -bool BS_StdWinInput::WasRightMouseDown() -{ +bool ScummVMInput::WasRightMouseDown() { return (m_RightMouseState[m_CurrentState] == false) && (m_RightMouseState[m_CurrentState ^ 1] == true); } // ----------------------------------------------------------------------------- -int BS_StdWinInput::GetMouseX() -{ +int ScummVMInput::GetMouseX() { return m_MouseX; } // ----------------------------------------------------------------------------- -int BS_StdWinInput::GetMouseY() -{ +int ScummVMInput::GetMouseY() { return m_MouseY; } // ----------------------------------------------------------------------------- -bool BS_StdWinInput::IsKeyDown(unsigned int KeyCode) -{ +bool ScummVMInput::IsKeyDown(unsigned int KeyCode) { return (m_KeyboardState[m_CurrentState][KeyCode] & 0x80) != 0; } // ----------------------------------------------------------------------------- -bool BS_StdWinInput::WasKeyDown(unsigned int KeyCode) -{ - return ((m_KeyboardState[m_CurrentState][KeyCode] & 0x80) == 0) && ((m_KeyboardState[m_CurrentState ^ 1][KeyCode] & 0x80) != 0); +bool ScummVMInput::WasKeyDown(unsigned int KeyCode) { + return ((m_KeyboardState[m_CurrentState][KeyCode] & 0x80) == 0) && + ((m_KeyboardState[m_CurrentState ^ 1][KeyCode] & 0x80) != 0); } // ----------------------------------------------------------------------------- -void BS_StdWinInput::SetMouseX(int PosX) -{ +void ScummVMInput::SetMouseX(int PosX) { m_MouseX = PosX; - SetCursorPos(m_MouseX + BS_Kernel::GetInstance()->GetWindow()->GetClientX(), m_MouseY + BS_Kernel::GetInstance()->GetWindow()->GetClientY()); + g_system->warpMouse(m_MouseX, m_MouseY); } // ----------------------------------------------------------------------------- -void BS_StdWinInput::SetMouseY(int PosY) -{ +void ScummVMInput::SetMouseY(int PosY) { m_MouseY = PosY; - SetCursorPos(m_MouseX + BS_Kernel::GetInstance()->GetWindow()->GetClientX(), m_MouseY + BS_Kernel::GetInstance()->GetWindow()->GetClientY()); + g_system->warpMouse(m_MouseX, m_MouseY); } // ----------------------------------------------------------------------------- -bool BS_StdWinInput::RegisterCharacterCallback(CharacterCallback Callback) -{ - if (find(m_CharacterCallbacks.begin(), m_CharacterCallbacks.end(), Callback) == m_CharacterCallbacks.end()) - { +bool ScummVMInput::RegisterCharacterCallback(CharacterCallback Callback) { + if (Common::find(m_CharacterCallbacks.begin(), m_CharacterCallbacks.end(), Callback) == m_CharacterCallbacks.end()) { m_CharacterCallbacks.push_back(Callback); return true; - } - else - { + } else { BS_LOG_WARNINGLN("Tried to register an CharacterCallback that was already registered."); return false; } @@ -259,16 +254,13 @@ bool BS_StdWinInput::RegisterCharacterCallback(CharacterCallback Callback) // ----------------------------------------------------------------------------- -bool BS_StdWinInput::UnregisterCharacterCallback(CharacterCallback Callback) -{ - list<CharacterCallback>::iterator CallbackIter = find(m_CharacterCallbacks.begin(), m_CharacterCallbacks.end(), Callback); - if (CallbackIter != m_CharacterCallbacks.end()) - { +bool ScummVMInput::UnregisterCharacterCallback(CharacterCallback Callback) { + Common::List<CharacterCallback>::iterator CallbackIter = Common::find(m_CharacterCallbacks.begin(), + m_CharacterCallbacks.end(), Callback); + if (CallbackIter != m_CharacterCallbacks.end()) { m_CharacterCallbacks.erase(CallbackIter); return true; - } - else - { + } else { BS_LOG_WARNINGLN("Tried to unregister an CharacterCallback that was not previously registered."); return false; } @@ -276,15 +268,11 @@ bool BS_StdWinInput::UnregisterCharacterCallback(CharacterCallback Callback) // ----------------------------------------------------------------------------- -bool BS_StdWinInput::RegisterCommandCallback(CommandCallback Callback) -{ - if (find(m_CommandCallbacks.begin(), m_CommandCallbacks.end(), Callback) == m_CommandCallbacks.end()) - { +bool ScummVMInput::RegisterCommandCallback(CommandCallback Callback) { + if (Common::find(m_CommandCallbacks.begin(), m_CommandCallbacks.end(), Callback) == m_CommandCallbacks.end()) { m_CommandCallbacks.push_back(Callback); return true; - } - else - { + } else { BS_LOG_WARNINGLN("Tried to register an CommandCallback that was already registered."); return false; } @@ -292,16 +280,13 @@ bool BS_StdWinInput::RegisterCommandCallback(CommandCallback Callback) // ----------------------------------------------------------------------------- -bool BS_StdWinInput::UnregisterCommandCallback(CommandCallback Callback) -{ - list<CommandCallback>::iterator CallbackIter = find(m_CommandCallbacks.begin(), m_CommandCallbacks.end(), Callback); - if (CallbackIter != m_CommandCallbacks.end()) - { +bool ScummVMInput::UnregisterCommandCallback(CommandCallback Callback) { + Common::List<CommandCallback>::iterator CallbackIter = + Common::find(m_CommandCallbacks.begin(), m_CommandCallbacks.end(), Callback); + if (CallbackIter != m_CommandCallbacks.end()) { m_CommandCallbacks.erase(CallbackIter); return true; - } - else - { + } else { BS_LOG_WARNINGLN("Tried to unregister an CommandCallback that was not previously registered."); return false; } @@ -309,15 +294,13 @@ bool BS_StdWinInput::UnregisterCommandCallback(CommandCallback Callback) // ----------------------------------------------------------------------------- -void BS_StdWinInput::ReportCharacter(unsigned char Character) -{ - list<CharacterCallback>::const_iterator CallbackIter = m_CharacterCallbacks.begin(); - while (CallbackIter != m_CharacterCallbacks.end()) - { +void ScummVMInput::ReportCharacter(unsigned char Character) { + Common::List<CharacterCallback>::const_iterator CallbackIter = m_CharacterCallbacks.begin(); + while (CallbackIter != m_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. - list<CharacterCallback>::const_iterator CurCallbackIter = CallbackIter; + Common::List<CharacterCallback>::const_iterator CurCallbackIter = CallbackIter; ++CallbackIter; (*CurCallbackIter)(Character); @@ -326,15 +309,13 @@ void BS_StdWinInput::ReportCharacter(unsigned char Character) // ----------------------------------------------------------------------------- -void BS_StdWinInput::ReportCommand(KEY_COMMANDS Command) -{ - list<CommandCallback>::const_iterator CallbackIter = m_CommandCallbacks.begin(); - while (CallbackIter != m_CommandCallbacks.end()) - { +void ScummVMInput::ReportCommand(KEY_COMMANDS Command) { + Common::List<CommandCallback>::const_iterator CallbackIter = m_CommandCallbacks.begin(); + while (CallbackIter != m_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. - list<CommandCallback>::const_iterator CurCallbackIter = CallbackIter; + Common::List<CommandCallback>::const_iterator CurCallbackIter = CallbackIter; ++CallbackIter; (*CurCallbackIter)(Command); @@ -345,16 +326,14 @@ void BS_StdWinInput::ReportCommand(KEY_COMMANDS Command) // Persistenz // ----------------------------------------------------------------------------- -bool BS_StdWinInput::Persist(BS_OutputPersistenceBlock & Writer) -{ +bool ScummVMInput::Persist(BS_OutputPersistenceBlock &Writer) { // Anzahl an Command-Callbacks persistieren. Writer.Write(m_CommandCallbacks.size()); // Alle Command-Callbacks einzeln persistieren. { - list<CommandCallback>::const_iterator It = m_CommandCallbacks.begin(); - while (It != m_CommandCallbacks.end()) - { + Common::List<CommandCallback>::const_iterator It = m_CommandCallbacks.begin(); + while (It != m_CommandCallbacks.end()) { Writer.Write(BS_CallbackRegistry::GetInstance().ResolveCallbackPointer(*It)); ++It; } @@ -365,9 +344,8 @@ bool BS_StdWinInput::Persist(BS_OutputPersistenceBlock & Writer) // Alle Character-Callbacks einzeln persistieren. { - list<CharacterCallback>::const_iterator It = m_CharacterCallbacks.begin(); - while (It != m_CharacterCallbacks.end()) - { + Common::List<CharacterCallback>::const_iterator It = m_CharacterCallbacks.begin(); + while (It != m_CharacterCallbacks.end()) { Writer.Write(BS_CallbackRegistry::GetInstance().ResolveCallbackPointer(*It)); ++It; } @@ -378,8 +356,7 @@ bool BS_StdWinInput::Persist(BS_OutputPersistenceBlock & Writer) // ----------------------------------------------------------------------------- -bool BS_StdWinInput::Unpersist(BS_InputPersistenceBlock & Reader) -{ +bool ScummVMInput::Unpersist(BS_InputPersistenceBlock &Reader) { // Command-Callbackliste leeren. m_CommandCallbacks.clear(); @@ -388,12 +365,12 @@ bool BS_StdWinInput::Unpersist(BS_InputPersistenceBlock & Reader) Reader.Read(CommandCallbackCount); // Alle Command-Callbacks wieder herstellen. - for (unsigned int i = 0; i < CommandCallbackCount; ++i) - { - std::string CallbackFunctionName; + for (unsigned int i = 0; i < CommandCallbackCount; ++i) { + Common::String CallbackFunctionName; Reader.Read(CallbackFunctionName); - m_CommandCallbacks.push_back(reinterpret_cast<CommandCallback>(BS_CallbackRegistry::GetInstance().ResolveCallbackFunction(CallbackFunctionName))); + m_CommandCallbacks.push_back(reinterpret_cast<CommandCallback>( + BS_CallbackRegistry::GetInstance().ResolveCallbackFunction(CallbackFunctionName))); } // Character-Callbackliste leeren. @@ -404,9 +381,8 @@ bool BS_StdWinInput::Unpersist(BS_InputPersistenceBlock & Reader) Reader.Read(CharacterCallbackCount); // Alle Character-Callbacks wieder herstellen. - for (unsigned int i = 0; i < CharacterCallbackCount; ++i) - { - std::string CallbackFunctionName; + for (unsigned int i = 0; i < CharacterCallbackCount; ++i) { + Common::String CallbackFunctionName; Reader.Read(CallbackFunctionName); m_CharacterCallbacks.push_back(reinterpret_cast<CharacterCallback>(BS_CallbackRegistry::GetInstance().ResolveCallbackFunction(CallbackFunctionName))); @@ -414,3 +390,5 @@ bool BS_StdWinInput::Unpersist(BS_InputPersistenceBlock & Reader) return Reader.IsGood(); } + +} // End of namespace Sword25 diff --git a/engines/sword25/input/stdwininput.h b/engines/sword25/input/scummvminput.h index 11ed63be49..a26060f4ab 100644 --- a/engines/sword25/input/stdwininput.h +++ b/engines/sword25/input/scummvminput.h @@ -47,10 +47,10 @@ namespace Sword25 { class BS_Kernel; /// Class definitions -class BS_StdWinInput : public BS_InputEngine { +class ScummVMInput : public BS_InputEngine { public: - BS_StdWinInput(BS_Kernel *pKernel); - virtual ~BS_StdWinInput(); + ScummVMInput(BS_Kernel *pKernel); + virtual ~ScummVMInput(); virtual bool Init(); virtual void Update(); @@ -77,6 +77,7 @@ public: private: void TestForLeftDoubleClick(); + void AlterKeyboardState(int keycode, byte newState); byte m_KeyboardState[2][256]; bool m_LeftMouseState[2]; |