diff options
author | Jared Adams <jaxad0127@gmail.com> | 2010-08-12 21:47:59 -0600 |
---|---|---|
committer | Jared Adams <jaxad0127@gmail.com> | 2010-08-12 22:01:57 -0600 |
commit | eb2c0cae1cb7a02b72c15de0f4e8f4a68a9fa53a (patch) | |
tree | 8b1fab9acbd0eb568d7f7e0b31f8fd4ac02b18a4 /src/event.h | |
parent | 2293b1c5ef0bb7378140bf73f1fef03a4504bdd2 (diff) | |
download | mana-eb2c0cae1cb7a02b72c15de0f4e8f4a68a9fa53a.tar.gz mana-eb2c0cae1cb7a02b72c15de0f4e8f4a68a9fa53a.tar.bz2 mana-eb2c0cae1cb7a02b72c15de0f4e8f4a68a9fa53a.tar.xz mana-eb2c0cae1cb7a02b72c15de0f4e8f4a68a9fa53a.zip |
Simplify working with the event system
EventManager has been merged into Event, with some new convinience methods
added.
Reviewed-by: Chuck Miller
Diffstat (limited to 'src/event.h')
-rw-r--r-- | src/event.h | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/src/event.h b/src/event.h index b27b9c33..68560dae 100644 --- a/src/event.h +++ b/src/event.h @@ -23,6 +23,7 @@ #include <string> #include <map> +#include <set> namespace Mana { @@ -34,9 +35,19 @@ enum BadEvent { KEY_ALREADY_EXISTS }; +class Listener; + +typedef std::set<Listener *> ListenerSet; +typedef std::map<std::string, ListenerSet > ListenMap; + class VariableData; typedef std::map<std::string, VariableData *> VariableMap; +#define SERVER_NOTICE(message) { \ +Mana::Event event("ServerNotice"); \ +event.setString("message", message); \ +event.trigger("Notices", event); } + class Event { public: @@ -144,7 +155,48 @@ public: */ bool hasBool(const std::string &key) const; + /** + * Sends this event to all classes listening to the given channel. + */ + inline void trigger(const std::string &channel) const + { trigger(channel, *this); } + + /** + * Sends the given event to all classes listening to the given channel. + */ + static void trigger(const std::string &channel, const Event &event); + + /** + * Sends an empty event with the given name to all classes listening to the + * given channel. + */ + static inline void trigger(const std::string& channel, + const std::string& name) + { trigger(channel, Mana::Event(name)); } + +protected: + friend class Listener; + + /** + * Binds the given listener to the given channel. The listener will receive + * all events triggered on the channel. + */ + static void bind(Listener *listener, const std::string &channel); + + /** + * Unbinds the given listener from the given channel. The listener will no + * longer receive any events from the channel. + */ + static void unbind(Listener *listener, const std::string &channel); + + /** + * Unbinds the given listener from all channels. + */ + static void remove(Listener *listener); + private: + static ListenMap mBindings; + std::string mEventName; VariableMap mData; |