Mana
Loading...
Searching...
No Matches
event.h
Go to the documentation of this file.
1/*
2 * The Mana Client
3 * Copyright (C) 2010-2012 The Mana Developers
4 *
5 * This file is part of The Mana Client.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#pragma once
22
23#include <any>
24#include <map>
25#include <set>
26#include <string>
27
28class ActorSprite;
29class Item;
30
31// Possible exception that can be thrown
37
38class EventListener;
39class VariableData;
40
41class Event
42{
43public:
59
106
111 : mType(type)
112 {}
113
117 template<typename T>
118 Event(Type type, const T &value)
119 : mType(type)
120 , mValue(value)
121 {}
122
123 ~Event();
124
128 Type getType() const
129 { return mType; }
130
134 template<typename T>
135 void setValue(const T &value)
136 { mValue = value; }
137
142 template<typename T>
143 const T &value() const
144 { return std::any_cast<const T &>(mValue); }
145
149 template<typename T>
150 bool hasValue(const T &value) const
151 { return mValue.type() == typeid(T) && Event::value<T>() == value; }
152
153// Integers
154
158 void setInt(const std::string &key, int value);
159
163 int getInt(const std::string &key) const;
164
169 int getInt(const std::string &key, int defaultValue) const
170 { try { return getInt(key); } catch (BadEvent) { return defaultValue; }}
171
172// Strings
173
177 void setString(const std::string &key, const std::string &value);
178
182 const std::string &getString(const std::string &key) const;
183
188 std::string getString(const std::string &key,
189 const std::string &defaultValue) const
190 { try { return getString(key); } catch (BadEvent) { return defaultValue; }}
191
192// Floats
193
198 void setFloat(const std::string &key, double value);
199
203 double getFloat(const std::string &key) const;
204
209 double getFloat(const std::string &key, float defaultValue) const
210 { try { return getFloat(key); } catch (BadEvent) { return defaultValue; }}
211
212// Booleans
213
217 void setBool(const std::string &key, bool value);
218
222 bool getBool(const std::string &key) const;
223
228 bool getBool(const std::string &key, bool defaultValue) const
229 { try { return getBool(key); } catch (BadEvent) { return defaultValue; }}
230
231// Items
232
236 void setItem(const std::string &key, Item *value);
237
241 Item *getItem(const std::string &key) const;
242
247 Item *getItem(const std::string &key, Item *defaultValue) const
248 { try { return getItem(key); } catch (BadEvent) { return defaultValue; }}
249
250// ActorSprites
251
255 void setActor(const std::string &key, ActorSprite *value);
256
260 ActorSprite *getActor(const std::string &key) const;
261
266 ActorSprite *getActor(const std::string &key,
267 ActorSprite *defaultValue) const
268 { try { return getActor(key); } catch (BadEvent) { return defaultValue; }}
269
270// Triggers
271
275 void trigger(Channel channel) const
276 { trigger(channel, *this); }
277
281 static void trigger(Channel channel, const Event &event);
282
287 static void trigger(Channel channel, Type type)
288 { trigger(channel, Event(type)); }
289
290protected:
291 friend class EventListener;
292
297 static void bind(EventListener *listener, Channel channel);
298
303 static void unbind(EventListener *listener, Channel channel);
304
308 static void remove(EventListener *listener);
309
310private:
311 using ListenMap = std::map<Channel, std::set<EventListener *>>;
313
314 const Type mType;
315 std::map<std::string, VariableData *> mData;
316 std::any mValue;
317};
318
319inline void serverNotice(const std::string &message)
320{
322 event.setString("message", message);
323 event.trigger(Event::NoticesChannel);
324}
virtual void event(Event::Channel channel, const Event &event)=0
Definition event.h:42
ActorSprite * getActor(const std::string &key) const
Returns the given variable if it is set and an actor.
Definition event.cpp:155
Event(Type type)
Makes an event with the given name.
Definition event.h:110
void setValue(const T &value)
Sets the value of the event.
Definition event.h:135
Type
Definition event.h:61
@ Next
Definition event.h:90
@ Destructed
Definition event.h:72
@ DoUse
Definition event.h:79
@ GuiWindowsLoading
Definition event.h:83
@ DoCloseInventory
Definition event.h:74
@ UpdateAttribute
Definition event.h:99
@ PostCount
Definition event.h:94
@ ServerNotice
Definition event.h:95
@ CloseAll
Definition event.h:66
@ CloseDialog
Definition event.h:67
@ ConfigOptionChanged
Definition event.h:68
@ Menu
Definition event.h:88
@ LoadingDatabases
Definition event.h:70
@ Announcement
Definition event.h:62
@ EnginesInitializing
Definition event.h:81
@ GuiWindowsLoaded
Definition event.h:82
@ QuestVarsChanged
Definition event.h:104
@ StateChange
Definition event.h:96
@ StorageCount
Definition event.h:97
@ EnginesInitialized
Definition event.h:80
@ DoDrop
Definition event.h:75
@ Being
Definition event.h:63
@ DoMove
Definition event.h:77
@ Close
Definition event.h:65
@ Whisper
Definition event.h:102
@ Post
Definition event.h:93
@ Player
Definition event.h:92
@ WhisperError
Definition event.h:103
@ UpdateStatusEffect
Definition event.h:101
@ Message
Definition event.h:89
@ MapLoaded
Definition event.h:87
@ ClearDialog
Definition event.h:64
@ GuiWindowsUnloading
Definition event.h:85
@ UpdateStat
Definition event.h:100
@ StringInput
Definition event.h:98
@ GuiWindowsUnloaded
Definition event.h:84
@ DoEquip
Definition event.h:76
@ Destructing
Definition event.h:73
@ NpcCount
Definition event.h:91
@ DoUnequip
Definition event.h:78
@ Constructed
Definition event.h:69
@ IntegerInput
Definition event.h:86
@ Destroyed
Definition event.h:71
void setBool(const std::string &key, bool value)
Sets the given variable to the given boolean, if it isn't already set.
Definition event.cpp:103
static void trigger(Channel channel, Type type)
Sends an empty event with the given name to all classes listening to the given channel.
Definition event.h:287
const T & value() const
Returns the value of the event.
Definition event.h:143
int getInt(const std::string &key, int defaultValue) const
Returns the given variable if it is set and an integer, returning the given default otherwise.
Definition event.h:169
static void unbind(EventListener *listener, Channel channel)
Unbinds the given listener from the given channel.
Definition event.cpp:187
void setString(const std::string &key, const std::string &value)
Sets the given variable to the given string, if it isn't already set.
Definition event.cpp:58
double getFloat(const std::string &key) const
Returns the given variable if it is set and a floating-point.
Definition event.cpp:89
Item * getItem(const std::string &key, Item *defaultValue) const
Returns the given variable if it is set and an Item, returning the given default otherwise.
Definition event.h:247
~Event()
Definition event.cpp:28
void trigger(Channel channel) const
Sends this event to all classes listening to the given channel.
Definition event.h:275
@ QuestsChannel
Definition event.h:57
@ StorageChannel
Definition event.h:56
@ ChatChannel
Definition event.h:49
@ AttributesChannel
Definition event.h:47
@ ActorSpriteChannel
Definition event.h:46
@ ClientChannel
Definition event.h:50
@ ConfigChannel
Definition event.h:51
@ NpcChannel
Definition event.h:55
@ NoticesChannel
Definition event.h:54
@ ItemChannel
Definition event.h:53
@ BuySellChannel
Definition event.h:48
@ GameChannel
Definition event.h:52
bool hasValue(const T &value) const
Returns whether the event has the given the value.
Definition event.h:150
const std::string & getString(const std::string &key) const
Returns the given variable if it is set and a string.
Definition event.cpp:66
Item * getItem(const std::string &key) const
Returns the given variable if it is set and an Item.
Definition event.cpp:133
static ListenMap mBindings
Definition event.h:312
void setFloat(const std::string &key, double value)
Sets the given variable to the given floating-point, if it isn't already set.
Definition event.cpp:81
void setActor(const std::string &key, ActorSprite *value)
Sets the given variable to the given actor, if it isn't already set.
Definition event.cpp:147
std::string getString(const std::string &key, const std::string &defaultValue) const
Returns the given variable if it is set and a string, returning the given default otherwise.
Definition event.h:188
std::map< std::string, VariableData * > mData
Definition event.h:315
std::any mValue
Definition event.h:316
ActorSprite * getActor(const std::string &key, ActorSprite *defaultValue) const
Returns the given variable if it is set and an actor, returning the given default otherwise.
Definition event.h:266
bool getBool(const std::string &key, bool defaultValue) const
Returns the given variable if it is set and a boolean, returning the given default otherwise.
Definition event.h:228
static void remove(EventListener *listener)
Unbinds the given listener from all channels.
Definition event.cpp:192
void setInt(const std::string &key, int value)
Sets the given variable to the given integer, if it isn't already set.
Definition event.cpp:36
double getFloat(const std::string &key, float defaultValue) const
Returns the given variable if it is set and a floating-point, returning the given default otherwise.
Definition event.h:209
static void bind(EventListener *listener, Channel channel)
Binds the given listener to the given channel.
Definition event.cpp:182
const Type mType
Definition event.h:314
Event(Type type, const T &value)
Makes an event with the given name and value.
Definition event.h:118
std::map< Channel, std::set< EventListener * > > ListenMap
Definition event.h:311
Type getType() const
Returns the name of the event.
Definition event.h:128
int getInt(const std::string &key) const
Returns the given variable if it is set and an integer.
Definition event.cpp:44
void setItem(const std::string &key, Item *value)
Sets the given variable to the given Item, if it isn't already set.
Definition event.cpp:125
bool getBool(const std::string &key) const
Returns the given variable if it is set and a boolean.
Definition event.cpp:111
Represents one or more instances of a certain item type.
Definition item.h:35
BadEvent
Definition event.h:32
@ BAD_KEY
Definition event.h:33
@ BAD_VALUE
Definition event.h:34
@ KEY_ALREADY_EXISTS
Definition event.h:35
void serverNotice(const std::string &message)
Definition event.h:319