diff options
author | Chuck Miller <shadowmil@gmail.com> | 2010-07-12 21:45:50 -0400 |
---|---|---|
committer | Chuck Miller <shadowmil@gmail.com> | 2010-07-13 17:31:00 -0400 |
commit | 7ae52ac5392607f5cb56dffa9ae45ae5a2f14bd1 (patch) | |
tree | d07c53931d0fe987caa19378c9ed14d381749736 | |
parent | c8aa6f18406459426fe0914d23181eb1e61f0137 (diff) | |
download | mana-7ae52ac5392607f5cb56dffa9ae45ae5a2f14bd1.tar.gz mana-7ae52ac5392607f5cb56dffa9ae45ae5a2f14bd1.tar.bz2 mana-7ae52ac5392607f5cb56dffa9ae45ae5a2f14bd1.tar.xz mana-7ae52ac5392607f5cb56dffa9ae45ae5a2f14bd1.zip |
Add the foundation for the event system
-rw-r--r-- | mana.cbp | 7 | ||||
-rw-r--r-- | src/CMakeLists.txt | 7 | ||||
-rw-r--r-- | src/Makefile.am | 7 | ||||
-rw-r--r-- | src/event.cpp | 99 | ||||
-rw-r--r-- | src/event.h | 73 | ||||
-rw-r--r-- | src/eventmanager.cpp | 67 | ||||
-rw-r--r-- | src/eventmanager.h | 56 | ||||
-rw-r--r-- | src/listener.cpp | 43 | ||||
-rw-r--r-- | src/listener.h | 44 | ||||
-rw-r--r-- | src/variabledata.h | 85 |
10 files changed, 488 insertions, 0 deletions
@@ -130,6 +130,10 @@ <Unit filename="src\emoteshortcut.cpp" /> <Unit filename="src\emoteshortcut.h" /> <Unit filename="src\equipment.h" /> + <Unit filename="src\event.cpp" /> + <Unit filename="src\event.h" /> + <Unit filename="src\eventmanager.cpp" /> + <Unit filename="src\eventmanager.h" /> <Unit filename="src\flooritem.cpp" /> <Unit filename="src\flooritem.h" /> <Unit filename="src\game.cpp" /> @@ -358,6 +362,8 @@ <Unit filename="src\joystick.h" /> <Unit filename="src\keyboardconfig.cpp" /> <Unit filename="src\keyboardconfig.h" /> + <Unit filename="src\listener.cpp" /> + <Unit filename="src\listener.h" /> <Unit filename="src\localplayer.cpp" /> <Unit filename="src\localplayer.h" /> <Unit filename="src\log.cpp" /> @@ -596,6 +602,7 @@ <Unit filename="src\utils\stringutils.h" /> <Unit filename="src\utils\xml.cpp" /> <Unit filename="src\utils\xml.h" /> + <Unit filename="src\variabledata.h" /> <Unit filename="src\vector.cpp" /> <Unit filename="src\vector.h" /> <Unit filename="src\winver.h" /> diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index c632de66..d4845085 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -439,6 +439,10 @@ SET(SRCS emoteshortcut.cpp emoteshortcut.h equipment.h + event.cpp + event.h + eventmanager.cpp + eventmanager.h flooritem.cpp flooritem.h game.cpp @@ -462,6 +466,8 @@ SET(SRCS joystick.h keyboardconfig.cpp keyboardconfig.h + listener.cpp + listener.h localplayer.cpp localplayer.h log.cpp @@ -507,6 +513,7 @@ SET(SRCS tileset.h units.cpp units.h + variabledata.h vector.cpp vector.h ) diff --git a/src/Makefile.am b/src/Makefile.am index 25f62de8..8324671b 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -338,6 +338,10 @@ mana_SOURCES = gui/widgets/avatarlistbox.cpp \ emoteshortcut.cpp \ emoteshortcut.h \ equipment.h \ + event.cpp \ + event.h \ + eventmanager.cpp \ + eventmanager.h \ flooritem.cpp \ flooritem.h \ game.cpp \ @@ -361,6 +365,8 @@ mana_SOURCES = gui/widgets/avatarlistbox.cpp \ joystick.h \ keyboardconfig.cpp \ keyboardconfig.h \ + listener.cpp \ + listener.h \ localplayer.cpp \ localplayer.h \ log.cpp \ @@ -406,6 +412,7 @@ mana_SOURCES = gui/widgets/avatarlistbox.cpp \ tileset.h \ units.cpp \ units.h \ + variabledata.h \ vector.cpp \ vector.h \ winver.h diff --git a/src/event.cpp b/src/event.cpp new file mode 100644 index 00000000..87179ae1 --- /dev/null +++ b/src/event.cpp @@ -0,0 +1,99 @@ +/* + * The Mana Client + * Copyright (C) 2010 The Mana Developers + * + * This file is part of The Mana Client. + * + * 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 + * 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, see <http://www.gnu.org/licenses/>. + */ + +#include "event.h" + +#include "variabledata.h" + +namespace Mana +{ + +Event::~Event() +{ + VariableMap::iterator it = mData.begin(); + while (it != mData.end()) + { + delete it->second; + it++; + } +} + +void Event::setInt(const std::string &key, int value) throw (BadEvent) +{ + if (mData.find(key) != mData.end()) + throw KEY_ALREADY_EXISTS; + + mData[key] = new IntData(value); +} + +int Event::getInt(const std::string &key) const throw (BadEvent) +{ + VariableMap::const_iterator it = mData.find(key); + if (it == mData.end()) + throw BAD_KEY; + + if (it->second->getType() != VariableData::DATA_INT) + throw BAD_VALUE; + + return static_cast<IntData *>(it->second)->getData(); +} + +void Event::setString(const std::string &key, const std::string &value) throw (BadEvent) +{ + if (mData.find(key) != mData.end()) + throw KEY_ALREADY_EXISTS; + + mData[key] = new StringData(value); +} + +const std::string &Event::getString(const std::string &key) const throw (BadEvent) +{ + VariableMap::const_iterator it = mData.find(key); + if (it == mData.end()) + throw BAD_KEY; + + if (it->second->getType() != VariableData::DATA_STRING) + throw BAD_VALUE; + + return static_cast<StringData *>(it->second)->getData(); +} + + +void Event::setFloat(const std::string &key, double value) throw (BadEvent) +{ + if (mData.find(key) != mData.end()) + throw KEY_ALREADY_EXISTS; + + mData[key] = new FloatData(value); +} + +double Event::getFloat(const std::string &key) const throw (BadEvent) +{ + VariableMap::const_iterator it = mData.find(key); + if (it == mData.end()) + throw BAD_KEY; + + if (it->second->getType() != VariableData::DATA_FLOAT) + throw BAD_VALUE; + + return static_cast<FloatData *>(it->second)->getData(); +} + +}; // namespace Mana diff --git a/src/event.h b/src/event.h new file mode 100644 index 00000000..d856bd6c --- /dev/null +++ b/src/event.h @@ -0,0 +1,73 @@ +/* + * The Mana Client + * Copyright (C) 2010 The Mana Developers + * + * This file is part of The Mana Client. + * + * 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 + * 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, see <http://www.gnu.org/licenses/>. + */ + +#ifndef EVENT_H +#define EVENT_H + +#include <string> +#include <map> + +namespace Mana +{ + +// Possible exception that can be thrown +enum BadEvent { + BAD_KEY, + BAD_VALUE, + KEY_ALREADY_EXISTS +}; + +class VariableData; +typedef std::map<std::string, VariableData *> VariableMap; + +class Event +{ + public: + // String passed can be retivered with getName() + // and is to used to identify what type of event + // this is. + Event(const std::string &name) + { mEventName = name; } + + ~Event(); + + const std::string &getName() const + { return mEventName; } + + // Sets or gets a interger with a key to identify + void setInt(const std::string &key, int value) throw (BadEvent); + int getInt(const std::string &key) const throw (BadEvent); + + // Sets or gets a string with a key to identify + void setString(const std::string &key, const std::string &value) throw (BadEvent); + const std::string &getString(const std::string &key) const throw (BadEvent); + + // Sets or gets a floating point number with key to identify + void setFloat(const std::string &key, double value) throw (BadEvent); + double getFloat(const std::string &key) const throw (BadEvent); + + private: + std::string mEventName; + + VariableMap mData; +}; + +}; // namespace Mana +#endif diff --git a/src/eventmanager.cpp b/src/eventmanager.cpp new file mode 100644 index 00000000..ec589bbf --- /dev/null +++ b/src/eventmanager.cpp @@ -0,0 +1,67 @@ +/* + * The Mana Client + * Copyright (C) 2010 The Mana Developers + * + * This file is part of The Mana Client. + * + * 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 + * 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, see <http://www.gnu.org/licenses/>. + */ + +#include "eventmanager.h" + +#include "listener.h" + +namespace Mana +{ + +ListenMap EventManager::mBindings; + +void EventManager::trigger(const std::string &channel, const Event &event) +{ + ListenMap::iterator it = mBindings.find(channel); + + // Make sure something is listening + if (it == mBindings.end()) + return; + + // Loop though all listeners + ListenerSet::iterator lit = it->second.begin(); + while (lit != it->second.end()) + { + (*lit)->event(channel, event); + lit++; + } +} + +void EventManager::remove(Listener *listener) +{ + ListenMap::iterator it = mBindings.begin(); + while (it != mBindings.end()) + { + it->second.erase(listener); + it++; + } +} + +void EventManager::bind(Listener *listener, const std::string &channel) +{ + mBindings[channel].insert(listener); +} + +void EventManager::unbind(Listener *listener, const std::string &channel) +{ + mBindings[channel].erase(listener); +} + +}; // namespace Mana diff --git a/src/eventmanager.h b/src/eventmanager.h new file mode 100644 index 00000000..5823ef62 --- /dev/null +++ b/src/eventmanager.h @@ -0,0 +1,56 @@ +/* + * The Mana Client + * Copyright (C) 2010 The Mana Developers + * + * This file is part of The Mana Client. + * + * 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 + * 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, see <http://www.gnu.org/licenses/>. + */ + +#ifndef EVENTMANAGER_H +#define EVENTMANAGER_H + +#include <string> +#include <map> +#include <set> + +namespace Mana +{ + +class Event; +class Listener; + +typedef std::set<Listener *> ListenerSet; +typedef std::map<std::string, ListenerSet > ListenMap; + +class EventManager +{ + public: + // Sends event to all listener on the channel + static void trigger(const std::string &channel, const Event &event); + + // Removes a listener from all channels + static void remove(Listener *listener); + + // Adds or removes a listener to a channel. + static void bind(Listener *listener, const std::string &channel); + static void unbind(Listener *listener, const std::string &channel); + + private: + static ListenMap mBindings; +}; + +}; // namespace Mana + +#endif diff --git a/src/listener.cpp b/src/listener.cpp new file mode 100644 index 00000000..5ba3b16a --- /dev/null +++ b/src/listener.cpp @@ -0,0 +1,43 @@ +/* + * The Mana Client + * Copyright (C) 2010 The Mana Developers + * + * This file is part of The Mana Client. + * + * 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 + * 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, see <http://www.gnu.org/licenses/>. + */ + +#include "listener.h" + +#include "eventmanager.h" + +namespace Mana +{ + +Listener::~Listener() +{ + EventManager::remove(this); +} + +void Listener::listen(const std::string &channel) +{ + EventManager::bind(this, channel); +} + +void Listener::ignore(const std::string &channel) +{ + EventManager::unbind(this, channel); +} + +}; // namespace Mana diff --git a/src/listener.h b/src/listener.h new file mode 100644 index 00000000..edac13e9 --- /dev/null +++ b/src/listener.h @@ -0,0 +1,44 @@ +/* + * The Mana Client + * Copyright (C) 2010 The Mana Developers + * + * This file is part of The Mana Client. + * + * 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 + * 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, see <http://www.gnu.org/licenses/>. + */ + +#ifndef LISTENER_H +#define LISTENER_H + +#include <string> + +namespace Mana +{ + +class Event; + +class Listener +{ + public: + ~Listener(); + + void listen(const std::string &channel); + + void ignore(const std::string &channel); + + virtual void event(const std::string &channel, const Event &event) = 0; +}; + +}; // namespace Mana +#endif diff --git a/src/variabledata.h b/src/variabledata.h new file mode 100644 index 00000000..6ba76318 --- /dev/null +++ b/src/variabledata.h @@ -0,0 +1,85 @@ +/* + * The Mana Client + * Copyright (C) 2010 The Mana Developers + * + * This file is part of The Mana Client. + * + * 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 + * 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, see <http://www.gnu.org/licenses/>. + */ + +#ifndef VARIABLEDATA_H +#define VARIABLEDATA_H + +#include <string> + +namespace Mana +{ + +class VariableData +{ + public: + enum { + DATA_NONE, + DATA_INT, + DATA_STRING, + DATA_FLOAT + }; + + virtual ~VariableData() {}; + + virtual int getType() const = 0; +}; + +class IntData : public VariableData +{ + public: + IntData(int value) { mData = value; } + + int getData() const { return mData; } + + int getType() const { return DATA_INT; } + + private: + int mData; +}; + +class StringData : public VariableData +{ + public: + StringData(const std::string &value) { mData = value; } + + const std::string &getData() const { return mData; } + + int getType() const { return DATA_STRING; } + + private: + std::string mData; +}; + +class FloatData : public VariableData +{ + public: + FloatData(double value) { mData = value; } + + double getData() const { return mData; } + + int getType() const { return DATA_FLOAT; } + + private: + double mData; +}; + +}; // namespace Mana + +#endif |