Implemented a simple EventManager class

svn-id: r26154
This commit is contained in:
Max Horn 2007-03-17 00:07:34 +00:00
parent 4951f82c65
commit 8fc8c4847d
6 changed files with 269 additions and 6 deletions

View File

@ -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)

View File

@ -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

View File

@ -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 \

84
common/events.h Normal file
View File

@ -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

View File

@ -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;
}

View File

@ -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();
//@}