Mana
Loading...
Searching...
No Matches
event.cpp
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#include "event.h"
22
23#include "eventlistener.h"
24#include "variabledata.h"
25
27
29{
30 for (auto &[_, data] : mData)
31 delete data;
32}
33
34// Integers
35
36void Event::setInt(const std::string &key, int value)
37{
38 if (mData.find(key) != mData.end())
40
41 mData[key] = new IntData(value);
42}
43
44int Event::getInt(const std::string &key) const
45{
46 auto it = mData.find(key);
47 if (it == mData.end())
48 throw BAD_KEY;
49
50 if (it->second->getType() != VariableData::DATA_INT)
51 throw BAD_VALUE;
52
53 return static_cast<IntData *>(it->second)->getData();
54}
55
56// Strings
57
58void Event::setString(const std::string &key, const std::string &value)
59{
60 if (mData.find(key) != mData.end())
62
63 mData[key] = new StringData(value);
64}
65
66const std::string &Event::getString(const std::string &key) const
67{
68 auto it = mData.find(key);
69 if (it == mData.end())
70 throw BAD_KEY;
71
72 if (it->second->getType() != VariableData::DATA_STRING)
73 throw BAD_VALUE;
74
75 return static_cast<StringData *>(it->second)->getData();
76}
77
78
79// Floats
80
81void Event::setFloat(const std::string &key, double value)
82{
83 if (mData.find(key) != mData.end())
85
86 mData[key] = new FloatData(value);
87}
88
89double Event::getFloat(const std::string &key) const
90{
91 auto it = mData.find(key);
92 if (it == mData.end())
93 throw BAD_KEY;
94
95 if (it->second->getType() != VariableData::DATA_FLOAT)
96 throw BAD_VALUE;
97
98 return static_cast<FloatData *>(it->second)->getData();
99}
100
101// Booleans
102
103void Event::setBool(const std::string &key, bool value)
104{
105 if (mData.find(key) != mData.end())
106 throw KEY_ALREADY_EXISTS;
107
108 mData[key] = new BoolData(value);
109}
110
111bool Event::getBool(const std::string &key) const
112{
113 auto it = mData.find(key);
114 if (it == mData.end())
115 throw BAD_KEY;
116
117 if (it->second->getType() != VariableData::DATA_BOOL)
118 throw BAD_VALUE;
119
120 return static_cast<BoolData *>(it->second)->getData();
121}
122
123// Items
124
125void Event::setItem(const std::string &key, Item *value)
126{
127 if (mData.find(key) != mData.end())
128 throw KEY_ALREADY_EXISTS;
129
130 mData[key] = new ItemData(value);
131}
132
133Item *Event::getItem(const std::string &key) const
134{
135 auto it = mData.find(key);
136 if (it == mData.end())
137 throw BAD_KEY;
138
139 if (it->second->getType() != VariableData::DATA_ITEM)
140 throw BAD_VALUE;
141
142 return static_cast<ItemData *>(it->second)->getData();
143}
144
145// Actors
146
147void Event::setActor(const std::string &key, ActorSprite *value)
148{
149 if (mData.find(key) != mData.end())
150 throw KEY_ALREADY_EXISTS;
151
152 mData[key] = new ActorData(value);
153}
154
155ActorSprite *Event::getActor(const std::string &key) const
156{
157 auto it = mData.find(key);
158 if (it == mData.end())
159 throw BAD_KEY;
160
161 if (it->second->getType() != VariableData::DATA_ACTOR)
162 throw BAD_VALUE;
163
164 return static_cast<ActorData *>(it->second)->getData();
165}
166
167// Triggers
168
169void Event::trigger(Channel channel, const Event &event)
170{
171 auto it = mBindings.find(channel);
172
173 // Make sure something is listening
174 if (it == mBindings.end())
175 return;
176
177 // Loop though all listeners
178 for (auto *listener : it->second)
179 listener->event(channel, event);
180}
181
182void Event::bind(EventListener *listener, Channel channel)
183{
184 mBindings[channel].insert(listener);
185}
186
187void Event::unbind(EventListener *listener, Channel channel)
188{
189 mBindings[channel].erase(listener);
190}
191
193{
194 for (auto &[_, listeners] : mBindings)
195 listeners.erase(listener);
196}
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
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
const T & value() const
Returns the value of the event.
Definition event.h:143
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
~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
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::map< std::string, VariableData * > mData
Definition event.h:315
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
static void bind(EventListener *listener, Channel channel)
Binds the given listener to the given channel.
Definition event.cpp:182
std::map< Channel, std::set< EventListener * > > ListenMap
Definition event.h:311
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
@ BAD_KEY
Definition event.h:33
@ BAD_VALUE
Definition event.h:34
@ KEY_ALREADY_EXISTS
Definition event.h:35
#define _(s)
Definition gettext.h:38