Game Development Reference
In-Depth Information
The
RegisterEvent
method first searched the
m_eventMap
variable using
find
to see if an
Event
with
the same
eventID
already exists. If it does not, then a new
Event
object is created and added to the
unordered_map
. The
AttachEvent
and
DetachEvent
methods find the
Event
object for the specified
eventID
and add or detach the supplied
EventHandler
object, using the code shown in Listing 21-11.
Listing 21-11. The
EventManager AttachEvent
and
DetachEvent
Methods
void EventManager::AttachEvent(EventID eventId, EventHandler& eventHandler)
{
EventMapIterator result = m_eventMap.find(eventId);
assert(result != m_eventMap.end());
if (result != m_eventMap.end())
{
assert(result->second);
result->second->AttachListener(eventHandler);
}
}
void EventManager::DetachEvent(EventID eventId, EventHandler& eventHandler)
{
EventMapIterator result = m_eventMap.find(eventId);
assert(result != m_eventMap.end());
if (result != m_eventMap.end())
{
assert(result->second);
result->second->DetachListener(eventHandler);
}
}
These methods are quite simple: They find the
Event
object for the given
eventID
and call the
Event
class's
AttachListener
or
DetachListener
, respectively. The
SendEvent
and
SendEventToHandler
methods are equally simple and shown in Listing 21-12.
Listing 21-12. The
EventManager SendEvent
and
SendEventToHandler
Methods
void EventManager::SendEvent(EventID eventId)
{
EventMapIterator result = m_eventMap.find(eventId);
if (result != m_eventMap.end())
{
assert(result->second);
if (result->second)
{
result->second->Send();
}
}
}