/// <summary> /// Add a new event to be processed instantly. /// </summary> /// <param name="inEvent">Event to be processed.</param> /// <returns>true if the event was processed, false otherwise.</returns> public static bool Trigger(IEventData inEvent) { if (!ValidateType(inEvent.GetEventType())) { return(false); } /*if (registry.Count != 0) * { * foreach (IEventListener listener in registry[registry.First().Key]) * listener.HandleEvent(inEvent); * }*/ if (!_registry.ContainsKey(inEvent.GetEventType().ID)) { return(false); } bool processed = false; foreach (IEventListener listener in _registry[inEvent.GetEventType().ID]) { if (listener.HandleEvent(inEvent)) { processed = true; } } return(processed); }
/// <summary> /// Process the next event of the active queue. /// </summary> /// <returns>true if the event was processed, false otherwise.</returns> public static bool Tick() { // Swap active queues, make sure new queue is empty after the swap int queueToProcess = _activeQueue; _activeQueue = (_activeQueue + 1) % 2; _queues[_activeQueue].Clear(); // Now process as many events as we can (possibly time limited)... // always do at least one event, if any are available... while (_queues[queueToProcess].Count > 0) { IEventData sEvent = _queues[queueToProcess][0]; _queues[queueToProcess].Remove(sEvent); // No listeners currently for this event type, skip it HashType type = sEvent.GetEventType(); if (!_registry.ContainsKey(type.ID)) { continue; } if (_registry.ContainsKey(type.ID)) { bool processed = false; //foreach (IEventListener listener in registry[registry.First().Key]) // listener.HandleEvent(sEvent); foreach (IEventListener listener in _registry[type.ID]) { if (listener.HandleEvent(sEvent)) { processed = true; } } } /*foreach (IEventListener listener in registry[type.ID]) * if (listener.HandleEvent(sEvent)) * break;*/ } // If any events left to process, push them onto the active queue bool queueFlushed = (_queues[queueToProcess].Count == 0); if (!queueFlushed) { while (_queues[queueToProcess].Count > 0) { IEventData sEvent = _queues[queueToProcess][_queues[queueToProcess].Count - 1]; _queues[queueToProcess].Remove(sEvent); _queues[_activeQueue].Add(sEvent); } } // All done, this pass return(queueFlushed); }
public static void TriggerEvent(IEventData eventData) { // Get the list of all bound events for this type List <EventListenerDelegate> listenerDelegates = null; if (eventListeners.TryGetValue(eventData.GetEventType(), out listenerDelegates)) { foreach (var item in listenerDelegates) { //Call the delegate, passing in event data for each bound event item(eventData); } } }
/// <summary> /// Add a new event to the queues to be processed when it is its turn in /// the central loop of the event manager. /// </summary> /// <param name="inEvent">Event to be processed.</param> /// <returns>tur if the event was processed, false otherwise.</returns> public static bool QueueEvent(IEventData inEvent) { if (!(_activeQueue >= 0 && _activeQueue <= 1)) { return(false); } if (!ValidateType(inEvent.GetEventType())) { return(false); } if (!_registry.ContainsKey(inEvent.GetEventType().ID)) { if (_registry.Count == 0) { return(false); } } _queues[_activeQueue].Add(inEvent); return(true); }