From 8fc8c4847df493de85736447c1c8d9528a9b1299 Mon Sep 17 00:00:00 2001 From: Max Horn Date: Sat, 17 Mar 2007 00:07:34 +0000 Subject: Implemented a simple EventManager class svn-id: r26154 --- backends/events/default/default-events.cpp | 87 ++++++++++++++++++++++++++++++ backends/events/default/default-events.h | 61 +++++++++++++++++++++ backends/module.mk | 1 + common/events.h | 84 +++++++++++++++++++++++++++++ common/system.cpp | 19 +++++++ common/system.h | 23 +++++--- 6 files changed, 269 insertions(+), 6 deletions(-) create mode 100644 backends/events/default/default-events.cpp create mode 100644 backends/events/default/default-events.h create mode 100644 common/events.h diff --git a/backends/events/default/default-events.cpp b/backends/events/default/default-events.cpp new file mode 100644 index 0000000000..9c92eef1d2 --- /dev/null +++ b/backends/events/default/default-events.cpp @@ -0,0 +1,87 @@ +/* ScummVM - Scumm Interpreter + * Copyright (C) 2002-2007 The ScummVM project + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * $URL$ + * $Id$ + * + */ + +#if !defined(DISABLE_DEFAULT_EVENTMANAGER) + +#include "common/stdafx.h" +#include "common/system.h" +#include "backends/events/default/default-events.h" + +DefaultEventManager::DefaultEventManager(OSystem *boss) : + _boss(boss), + _buttonState(0), + _modifierState(0), + _shouldQuit(false) { + + assert(_boss); +} + +DefaultEventManager::~DefaultEventManager() { + +} + +bool DefaultEventManager::pollEvent(OSystem::Event &event) { + bool result; + + result = _boss->pollEvent(event); + + if (result) { + switch (event.type) { + case OSystem::EVENT_KEYDOWN: + case OSystem::EVENT_KEYUP: + _modifierState = event.kbd.flags; + break; + case OSystem::EVENT_MOUSEMOVE: + _mousePos = event.mouse; + break; + + case OSystem::EVENT_LBUTTONDOWN: + _mousePos = event.mouse; + _buttonState |= LBUTTON; + break; + case OSystem::EVENT_LBUTTONUP: + _mousePos = event.mouse; + _buttonState &= ~LBUTTON; + break; + + case OSystem::EVENT_RBUTTONDOWN: + _mousePos = event.mouse; + _buttonState |= RBUTTON; + break; + case OSystem::EVENT_RBUTTONUP: + _mousePos = event.mouse; + _buttonState &= ~RBUTTON; + break; + + case OSystem::EVENT_QUIT: + _shouldQuit = true; + break; + + default: + break; + } + } + + return result; +} + +#endif // !defined(DISABLE_DEFAULT_EVENTMANAGER) diff --git a/backends/events/default/default-events.h b/backends/events/default/default-events.h new file mode 100644 index 0000000000..3291257f01 --- /dev/null +++ b/backends/events/default/default-events.h @@ -0,0 +1,61 @@ +/* ScummVM - Scumm Interpreter + * Copyright (C) 2002-2007 The ScummVM project + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * $URL$ + * $Id$ + * + */ + +#if !defined(BACKEND_SAVES_DEFAULT_H) && !defined(DISABLE_DEFAULT_EVENTMANAGER) +#define BACKEND_SAVES_DEFAULT_H + +#include "common/stdafx.h" +#include "common/events.h" + +/* +At some point we will remove pollEvent from OSystem and change +DefaultEventManager to use a "boss" derived from this class: +class EventProvider { +public + virtual bool pollEvent(Common::Event &event) = 0; +}; + +Backends which wish to use the DefaultEventManager then simply can +use a subclass of EventProvider. +*/ + +class DefaultEventManager : public Common::EventManager { + OSystem *_boss; + + Common::Point _mousePos; + int _buttonState; + int _modifierState; + bool _shouldQuit; + +public: + DefaultEventManager(OSystem *boss); + ~DefaultEventManager(); + + virtual bool pollEvent(OSystem::Event &event); + + virtual Common::Point getMousePos() const { return _mousePos; } + virtual int getButtonState() const { return _buttonState; } + virtual int getModifierState() const { return _modifierState; } + virtual int shouldQuit() const { return _shouldQuit; } +}; + +#endif diff --git a/backends/module.mk b/backends/module.mk index 0f0dda454d..1803a6b945 100644 --- a/backends/module.mk +++ b/backends/module.mk @@ -1,6 +1,7 @@ MODULE := backends MODULE_OBJS := \ + events/default/default-events.o \ fs/posix/posix-fs.o \ fs/morphos/abox-fs.o \ fs/windows/windows-fs.o \ diff --git a/common/events.h b/common/events.h new file mode 100644 index 0000000000..443903896e --- /dev/null +++ b/common/events.h @@ -0,0 +1,84 @@ +/* ScummVM - Scumm Interpreter + * Copyright (C) 2002-2007 The ScummVM project + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * $URL$ + * $Id$ + * + */ + +#ifndef COMMON_EVENTS_H +#define COMMON_EVENTS_H + +#include "common/rect.h" +#include "common/system.h" + +namespace Common { + +/** + * The EventManager provides user input events to the client code. + * In addition, it keeps track of the state of various input devices, + * like keys, mouse position and buttons. + */ +class EventManager { +public: + EventManager() {} + virtual ~EventManager() {} + + enum { + LBUTTON = 1 << 0, + RBUTTON = 1 << 1 + }; + + /** + * Get the next event in the event queue. + * @param event point to an Event struct, which will be filled with the event data. + * @return true if an event was retrieved. + */ + virtual bool pollEvent(OSystem::Event &event) = 0; + + + /** Return the current key state */ + virtual Common::Point getMousePos() const = 0; + + /** + * Return a bitmask with the button states: + * - bit 0: left button up=1, down=0 + * - bit 1: right button up=1, down=0 + */ + virtual int getButtonState() const = 0; + + /** Get a bitmask with the current modifier state */ + virtual int getModifierState() const = 0; + + /** + * Should the application terminate? Set to true if we + * received an EVENT_QUIT. + */ + virtual int shouldQuit() const = 0; + + // Optional: check whether a given key is currently pressed ???? + //virtual bool isKeyPressed(int keycode) = 0; + + // TODO: Keyboard repeat support? + + // TODO: Consider removing OSystem::getScreenChangeID and + // replacing it by a generic getScreenChangeID method here +}; + +} + +#endif diff --git a/common/system.cpp b/common/system.cpp index 7231338a85..17ecb3f124 100644 --- a/common/system.cpp +++ b/common/system.cpp @@ -24,6 +24,7 @@ #include "common/stdafx.h" #include "backends/intern.h" +#include "backends/events/default/default-events.h" #include "gui/message.h" @@ -36,6 +37,12 @@ OSystem *g_system = 0; +OSystem::OSystem() { +} + +OSystem::~OSystem() { +} + bool OSystem::setGraphicsMode(const char *name) { if (!name) return false; @@ -80,3 +87,15 @@ void OSystem::stopCD() { void OSystem::updateCD() { } + +static Common::EventManager *s_eventManager = 0; + +Common::EventManager *OSystem::getEventManager() { + // FIXME/TODO: Eventually this method should be turned into an abstract one, + // to force backends to implement this conciously (even if they + // end up returning the default event manager anyway). + if (!s_eventManager) + s_eventManager = new DefaultEventManager(this); + return s_eventManager; +} + diff --git a/common/system.h b/common/system.h index bd0caf4575..a8aa1198bd 100644 --- a/common/system.h +++ b/common/system.h @@ -1,6 +1,6 @@ /* ScummVM - Scumm Interpreter * Copyright (C) 2001 Ludvig Strigeus - * Copyright (C) 2001-2006 The ScummVM project + * Copyright (C) 2001-2007 The ScummVM project * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -37,6 +37,7 @@ namespace Graphics { } namespace Common { + class EventManager; class SaveFileManager; class TimerManager; } @@ -58,8 +59,8 @@ private: OSystem& operator= (const OSystem&); protected: - OSystem() { } - virtual ~OSystem() { } + OSystem(); + virtual ~OSystem(); public: @@ -68,7 +69,7 @@ public: * config data (including command line params etc.) are fully loaded. * * @note Subclasses should always invoke the implementation of their - * parent class. They should so so near the end of their own + * parent class. They should do so near the end of their own * implementation. */ virtual void initBackend() { } @@ -679,6 +680,9 @@ public: /** @name Events and Time */ //@{ +//protected: + friend class Common::EventManager; + /** * The types of events backends may generate. * @see Event @@ -793,6 +797,7 @@ public: */ virtual bool pollEvent(Event &event) = 0; +public: /** Get the number of milliseconds since the program was started. */ virtual uint32 getMillis() = 0; @@ -800,11 +805,17 @@ public: virtual void delayMillis(uint msecs) = 0; /** - * Returh the timer manager. For more information, refer to the - * TimerManager documentation. + * Return the timer manager singleton. For more information, refer + * to the TimerManager documentation. */ virtual Common::TimerManager *getTimerManager() = 0; + /** + * Return the event manager singleton. For more information, refer + * to the EventManager documentation. + */ + virtual Common::EventManager *getEventManager(); + //@} -- cgit v1.2.3