From 7ae52ac5392607f5cb56dffa9ae45ae5a2f14bd1 Mon Sep 17 00:00:00 2001 From: Chuck Miller Date: Mon, 12 Jul 2010 21:45:50 -0400 Subject: Add the foundation for the event system --- src/CMakeLists.txt | 7 ++++ src/Makefile.am | 7 ++++ src/event.cpp | 99 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/event.h | 73 ++++++++++++++++++++++++++++++++++++++ src/eventmanager.cpp | 67 +++++++++++++++++++++++++++++++++++ src/eventmanager.h | 56 +++++++++++++++++++++++++++++ src/listener.cpp | 43 +++++++++++++++++++++++ src/listener.h | 44 +++++++++++++++++++++++ src/variabledata.h | 85 ++++++++++++++++++++++++++++++++++++++++++++ 9 files changed, 481 insertions(+) create mode 100644 src/event.cpp create mode 100644 src/event.h create mode 100644 src/eventmanager.cpp create mode 100644 src/eventmanager.h create mode 100644 src/listener.cpp create mode 100644 src/listener.h create mode 100644 src/variabledata.h (limited to 'src') 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 . + */ + +#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(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(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(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 . + */ + +#ifndef EVENT_H +#define EVENT_H + +#include +#include + +namespace Mana +{ + +// Possible exception that can be thrown +enum BadEvent { + BAD_KEY, + BAD_VALUE, + KEY_ALREADY_EXISTS +}; + +class VariableData; +typedef std::map 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 . + */ + +#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 . + */ + +#ifndef EVENTMANAGER_H +#define EVENTMANAGER_H + +#include +#include +#include + +namespace Mana +{ + +class Event; +class Listener; + +typedef std::set ListenerSet; +typedef std::map 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 . + */ + +#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 . + */ + +#ifndef LISTENER_H +#define LISTENER_H + +#include + +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 . + */ + +#ifndef VARIABLEDATA_H +#define VARIABLEDATA_H + +#include + +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 -- cgit v1.2.3-70-g09d2