Example #1
0
        /// <summary>
        /// Returns true if handler is mapped to any of the specified phases
        /// </summary>
        /// <param name="eventType"></param>
        /// <param name="handler"></param>
        /// <param name="phases"></param>
        /// <returns></returns>
        public virtual bool MappedToAnyPhase(string eventType, EventHandler handler, EventPhase phases)
        {
            var arr = EventPhaseHelper.BreakUpPhases(phases);

            foreach (EventPhase phase in arr)
            {
                EventTypePhase key = new EventTypePhase(eventType, phase);
                //if (_eventHandlerDict.ContainsKey(key) && _eventHandlerDict[key].Contains(handler))
                if (_eventHandlerDict.ContainsKey(key))
                {
                    var exists = _eventHandlerDict[key].Exists(delegate(PriorityGroup group)
                    {
                        return(group.Contains(handler));
                    });

                    //foreach (PriorityGroup group in _eventHandlerDict[key])
                    //{
                    //    //if (_eventHandlerDict[key].Contains(handler))
                    //    //    return true;
                    //}

                    return(exists);
                }
            }

            return(false);
        }
Example #2
0
        /// <summary>
        /// Executes event handlers listening for a particular event type
        /// NOTE: Public by purpose
        /// </summary>
        /// <param name="e">Event to dispatch</param>
        /// <remarks>Conceived by Danko Kozar</remarks>
        public void ExecuteListeners(Event e)
        {
            // return if event canceled
            if (e.Canceled)
            {
                return;
            }

            EventTypePhase key = new EventTypePhase(e.Type, e.Phase);

            // find event handlers subscribed for this event
            if (_eventHandlerDict.ContainsKey(key) && null != _eventHandlerDict[key])
            {
                // execute each handler
                _eventHandlerDict[key].ForEach(
                    delegate(EventHandler handler)
                {
                    if (e.Canceled)     // the event might have been canceled by the previous listener in the collection
                    {
                        return;
                    }

                    handler(e);     // execute the handler with an event as argument
                }
                    );
            }
        }
Example #3
0
        /// <summary>
        /// Removes all listeners for the spacified event type and phases
        /// </summary>
        /// <param name="eventType">Event type</param>
        /// /// <param name="phases"></param>
        public virtual void RemoveAllListeners(string eventType, EventPhase phases)
        {
            var arr = EventPhaseHelper.BreakUpPhases(phases);

            foreach (EventPhase phase in arr)
            {
                EventTypePhase key = new EventTypePhase(eventType, phase);
                if (_eventHandlerDict.ContainsKey(key))
                {
                    _eventHandlerDict[key].Clear();
                    _eventHandlerDict.Remove(key);
                }
            }
        }
Example #4
0
        /// <summary>
        /// Returns true if handler is mapped to any of the specified phases
        /// </summary>
        /// <param name="eventType"></param>
        /// <param name="handler"></param>
        /// <param name="phases"></param>
        /// <returns></returns>
        public virtual bool MappedToAnyPhase(string eventType, EventHandler handler, EventPhase phases)
        {
            var arr = EventPhaseHelper.BreakUpPhases(phases);

            foreach (EventPhase phase in arr)
            {
                EventTypePhase key = new EventTypePhase(eventType, phase);
                if (_eventHandlerDict.ContainsKey(key) && _eventHandlerDict[key].Contains(handler))
                {
                    return(true);
                }
            }

            return(false);
        }
Example #5
0
        /// <summary>
        /// Adds the event listener
        /// </summary>
        /// <param name="eventType">Event type</param>
        /// <param name="handler">Event handler</param>
        /// <param name="phases">Event bubbling phases that we listen to</param>
        public virtual void AddEventListener(string eventType, EventHandler handler, EventPhase phases)
        {
            var arr = EventPhaseHelper.BreakUpPhases(phases);

            foreach (EventPhase phase in arr)
            {
                EventTypePhase key = new EventTypePhase(eventType, phase);

                if (!_eventHandlerDict.ContainsKey(key)) // avoid key duplication
                {
                    _eventHandlerDict.Add(key, new List <EventHandler>());
                }

                if (!_eventHandlerDict[key].Contains(handler)) // avoid key + value duplication
                {
                    _eventHandlerDict[key].Add(handler);
                }
            }
        }
Example #6
0
        /// <summary>
        /// Removes the event listener
        /// </summary>
        /// <param name="eventType">Event type</param>
        /// <param name="handler">Event handler (function)</param>
        /// <param name="phases">Event bubbling phases that we listen to</param>
        public virtual void RemoveEventListener(string eventType, EventHandler handler, EventPhase phases)
        {
            var arr = EventPhaseHelper.BreakUpPhases(phases);

            foreach (EventPhase phase in arr)
            {
                EventTypePhase key = new EventTypePhase(eventType, phase);

                if (_eventHandlerDict.ContainsKey(key))
                {
                    foreach (PriorityGroup group in _eventHandlerDict[key])
                    {
                        if (group.Contains(handler))
                        {
                            group.Remove(handler);
                        }

                        if (group.Count == 0)
                        {
                            if (null == _tempList)
                            {
                                _tempList = new List <PriorityGroup>();
                            }
                            _tempList.Add(group);
                        }
                    }

                    if (null != _tempList)
                    {
                        foreach (PriorityGroup @group in _tempList)
                        {
                            _eventHandlerDict[key].Remove(@group); // cleanup
                        }
                        _tempList.Clear();
                    }

                    if (_eventHandlerDict[key].Count == 0)
                    {
                        _eventHandlerDict.Remove(key); // cleanup
                    }
                }
            }
        }
Example #7
0
        /// <summary>
        /// Adds the event listener
        /// </summary>
        /// <param name="eventType">Event type</param>
        /// <param name="handler">Event handler</param>
        /// <param name="phases">Event bubbling phases that we listen to</param>
        /// <param name="priority">Event priority</param>
        public virtual void AddEventListener(string eventType, EventHandler handler, EventPhase phases, int priority)
        {
            var arr = EventPhaseHelper.BreakUpPhases(phases);

            foreach (EventPhase phase in arr)
            {
                EventTypePhase key = new EventTypePhase(eventType, phase);

                if (!_eventHandlerDict.ContainsKey(key)) // avoid key duplication
                {
                    _eventHandlerDict.Add(key, new List <PriorityGroup>());
                }

                var group = _eventHandlerDict[key].Find(delegate(PriorityGroup g)
                {
                    return(g.Priority == priority);
                });

                if (null == group)
                {
                    //if (0 != priority)
                    //    Debug.Log("Creating new group with priority " + priority);
                    group = new PriorityGroup {
                        Priority = priority
                    };
                    // add and sort
                    _eventHandlerDict[key].Add(group);

                    // if having multiple priorities, sort now
                    if (_eventHandlerDict[key].Count > 0)
                    {
                        _eventHandlerDict[key].Sort(PriorityComparer);
                    }
                }

                if (!group.Contains(handler)) // avoid key + value duplication
                {
                    group.Add(handler);
                }
            }
        }
Example #8
0
        /// <summary>
        /// Removes the event listener
        /// </summary>
        /// <param name="eventType">Event type</param>
        /// <param name="handler">Event handler (function)</param>
        /// <param name="phases">Event bubbling phases that we listen to</param>
        public virtual void RemoveEventListener(string eventType, EventHandler handler, EventPhase phases)
        {
            var arr = EventPhaseHelper.BreakUpPhases(phases);

            foreach (EventPhase phase in arr)
            {
                EventTypePhase key = new EventTypePhase(eventType, phase);

                if (_eventHandlerDict.ContainsKey(key))
                {
                    if (_eventHandlerDict[key].Contains(handler))
                    {
                        _eventHandlerDict[key].Remove(handler);
                    }

                    if (_eventHandlerDict[key].Count == 0) // cleanup
                    {
                        _eventHandlerDict.Remove(key);
                    }
                }
            }
        }
Example #9
0
 /// <summary>
 /// Indicates whether the current object is equal to another object of the same type.
 /// </summary>
 /// <returns>
 /// true if the current object is equal to the <paramref name="other"/> parameter; otherwise, false.
 /// </returns>
 /// <param name="other">An object to compare with this object.
 /// </param>
 public bool Equals(EventTypePhase other)
 {
     return(Equals(other.EventType, EventType) && Equals(other.Phase, Phase));
 }
Example #10
0
 /// <summary>
 /// Indicates whether the current object is equal to another object of the same type.
 /// </summary>
 /// <returns>
 /// true if the current object is equal to the <paramref name="other"/> parameter; otherwise, false.
 /// </returns>
 /// <param name="other">An object to compare with this object.
 /// </param>
 public bool Equals(EventTypePhase other)
 {
     return Equals(other.EventType, EventType) && Equals(other.Phase, Phase);
 }
Example #11
0
        /// <summary>
        /// Adds the event listener
        /// </summary>
        /// <param name="eventType">Event type</param>
        /// <param name="handler">Event handler</param>
        /// <param name="phases">Event bubbling phases that we listen to</param>
        public virtual void AddEventListener(string eventType, EventHandler handler, EventPhase phases)
        {
            var arr = EventPhaseHelper.BreakUpPhases(phases);

            foreach (EventPhase phase in arr)
            {
                EventTypePhase key = new EventTypePhase(eventType, phase);

                if (!_eventHandlerDict.ContainsKey(key)) // avoid key duplication
                    _eventHandlerDict.Add(key, new List<EventHandler>());

                if (!_eventHandlerDict[key].Contains(handler)) // avoid key + value duplication
                    _eventHandlerDict[key].Add(handler);
            }
        }
Example #12
0
        /// <summary>
        /// Removes the event listener
        /// </summary>
        /// <param name="eventType">Event type</param>
        /// <param name="handler">Event handler (function)</param>
        /// <param name="phases">Event bubbling phases that we listen to</param>
        public virtual void RemoveEventListener(string eventType, EventHandler handler, EventPhase phases)
        {
            var arr = EventPhaseHelper.BreakUpPhases(phases);

            foreach (EventPhase phase in arr)
            {
                EventTypePhase key = new EventTypePhase(eventType, phase);

                if (_eventHandlerDict.ContainsKey(key))
                {
                    if (_eventHandlerDict[key].Contains(handler))
                        _eventHandlerDict[key].Remove(handler);

                    if (_eventHandlerDict[key].Count == 0) // cleanup
                        _eventHandlerDict.Remove(key);
                }
            }
        }
Example #13
0
        /// <summary>
        /// Removes all listeners for the spacified event type and phases
        /// </summary>
        /// <param name="eventType">Event type</param>
        /// /// <param name="phases"></param>
        public virtual void RemoveAllListeners(string eventType, EventPhase phases)
        {
            var arr = EventPhaseHelper.BreakUpPhases(phases);

            foreach (EventPhase phase in arr)
            {
                EventTypePhase key = new EventTypePhase(eventType, phase);
                if (_eventHandlerDict.ContainsKey(key))
                {
                    _eventHandlerDict[key].Clear();
                    _eventHandlerDict.Remove(key);
                }
            }
        }
Example #14
0
        /// <summary>
        /// Returns true if handler is mapped to any of the specified phases
        /// </summary>
        /// <param name="eventType"></param>
        /// <param name="handler"></param>
        /// <param name="phases"></param>
        /// <returns></returns>
        public virtual bool MappedToAnyPhase(string eventType, EventHandler handler, EventPhase phases)
        {
            var arr = EventPhaseHelper.BreakUpPhases(phases);

            foreach (EventPhase phase in arr)
            {
                EventTypePhase key = new EventTypePhase(eventType, phase);
                if (_eventHandlerDict.ContainsKey(key) && _eventHandlerDict[key].Contains(handler))
                    return true;
            }

            return false;
        }
Example #15
0
        /// <summary>
        /// Executes event handlers listening for a particular event type
        /// NOTE: Public by purpose
        /// </summary>
        /// <param name="e">Event to dispatch</param>
        /// <remarks>Conceived by Danko Kozar</remarks>
        public void ExecuteListeners(Event e)
        {
            // return if event canceled
            if (e.Canceled)
                return;

            EventTypePhase key = new EventTypePhase(e.Type, e.Phase);

            // find event handlers subscribed for this event
            if (_eventHandlerDict.ContainsKey(key) && null != _eventHandlerDict[key])
            {
                // execute each handler
                _eventHandlerDict[key].ForEach(
                    delegate(EventHandler handler)
                    {
                        if (e.Canceled) // the event might have been canceled by the previous listener in the collection
                            return;

                        handler(e); // execute the handler with an event as argument
                    }
                );
            }
        }