示例#1
0
bool EventManager::Trigger(const Event *event) const
{
	// TODO: validate type

	// trigger events for wildcard listeners first
	EventListenerMap::const_iterator wildcardItor = m_registry.find(EVENT_TYPE_WILDCARD);
	if (wildcardItor != m_registry.end())
	{
		const EventListenerTable &table = wildcardItor->second;
		for (EventListenerTable::const_iterator i = table.begin(); i != table.end(); ++i)
		{
			EventListener *listener = *i;
			listener->Handle(event);
		}
	}

	// find the listener list for the event type
	EventListenerMap::const_iterator itor = m_registry.find(event->GetTypeOf());
	if (itor == m_registry.end())
		return false;

	bool result = false;

	// trigger the event in each listener
	const EventListenerTable &table = itor->second;
	for (EventListenerTable::const_iterator i = table.begin(); i != table.end(); ++i)
	{
		EventListener *listener = *i;
		if (listener->Handle(event))
		{
			// only return true if a listener signals they handled the event
			result = true;
		}
	}

	return result;
}
示例#2
0
bool EventManager::ProcessQueue()
{
	EventListenerMap::const_iterator wildcardItor = m_registry.find(EVENT_TYPE_WILDCARD);

	// swap active queues and empty the new queue
	uint queueToProcess = m_activeQueue;
	m_activeQueue = (m_activeQueue + 1) % NUM_EVENT_QUEUES;
	m_queues[m_activeQueue].clear();

	// process the queue!
	EventQueue &queue = m_queues[queueToProcess];
	while (queue.size() > 0)
	{
		// pop the next event off the queue
		const Event *event = queue.front();
		queue.pop_front();

		EVENT_TYPE type = event->GetTypeOf();

		// process wildcard listeners first (if any)
		if (wildcardItor != m_registry.end())
		{
			const EventListenerTable &table = wildcardItor->second;
			for (EventListenerTable::const_iterator i = table.begin(); i != table.end(); ++i)
			{
				EventListener *listener = *i;
				listener->Handle(event);
			}
		}

		EventListenerMap::const_iterator listenerItor = m_registry.find(type);
		if (listenerItor != m_registry.end())
		{
			const EventListenerTable &table = listenerItor->second;
			for (EventListenerTable::const_iterator i = table.begin(); i != table.end(); ++i)
			{
				EventListener *listener = *i;
				if (listener->Handle(event))
					break;                      // don't let other listeners handle the event if this one signals it handled it
			}
		}

		SAFE_DELETE(event);
	}

	// if there are any events left in the queue, push them onto the active queue
	if (queue.size() != 0)
	{
		while (queue.size() > 0)
		{
			// to preserve sequencing, go bottom-up on the remainder of the queue that was processed
			// inserting them at the head of the active queue
			const Event *event = queue.back();
			queue.pop_back();
			m_queues[m_activeQueue].push_front(event);
		}

		return false;
	}
	else
		return true;
}