Exemplo n.º 1
0
        /// <summary>
        /// Dispatches the given event of the specified type onto the object.
        /// </summary>
        /// <typeparam name="T">The event class.</typeparam>
        /// <param name="obj">The object to which to connect to.</param>
        /// <param name="eventObj">The name of the event to which to listen for.</param>
        /// <param name="nextFrame">Whether to send this event on the next frame.</param>
        public static void Dispatch(GameObject obj, Event eventObj, System.Type type, bool nextFrame = false)
        {
            var key = type.ToString();

            if (logging.Connect)
            {
                Trace.Script("'" + key + "' to '" + obj.name + "'");
            }

            // If this a delayed dispatch...
            if (nextFrame)
            {
                get.StartCoroutine(DispatchNextFrame(obj, eventObj, type));
            }

            // Check if the object has been registered onto the event system.
            // If not, it will be.
            CheckRegistration(obj);

            // If there is no delegate registered to this object, do nothing.
            if (!HasDelegate(obj, key))
            {
                if (logging.Dispatch)
                {
                    Trace.Script("No delegate registered to " + obj.name + " for " + eventObj.ToString());
                }
                return;
            }

            // If we are watching events of this type
            bool watching = false;

            if (get.eventWatchList.Contains(key))
            {
                watching = true;
            }


            // Invoke the method for every delegate
            var delegateMap = Events.get.dispatchMap[obj][key];
            DelegateTypeList delegatesToRemove = null;

            foreach (var deleg in delegateMap)
            {
                // If we are watching events of this type
                if (watching)
                {
                    Trace.Script("Invoking member function on " + deleg.Target.ToString());
                }

                // Do a lazy delete if it has been nulled out?
                if (IsNull(deleg.Method) || IsNull(deleg.Target))
                {
                    if (delegatesToRemove == null)
                    {
                        delegatesToRemove = new DelegateTypeList();
                    }
                    delegatesToRemove.Add(deleg);
                    continue;
                }

                deleg.DynamicInvoke(eventObj);
            }

            // If any delegates were found to be null, remove them (lazy delete)
            if (delegatesToRemove != null)
            {
                foreach (var deleg in delegatesToRemove)
                {
                    delegateMap.Remove(deleg);
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Dispatches the given event of the specified type onto the object.
        /// </summary>
        /// <typeparam name="T">The event class.</typeparam>
        /// <param name="obj">The object to which to connect to.</param>
        /// <param name="e">The event to which to listen for.</param>
        /// <param name="nextFrame">Whether to send this event on the next frame.</param>
        public static void Dispatch <T>(GameObject obj, T e, bool nextFrame = false) where T : StratusEvent
        {
            string key = typeof(T).ToString();

#if STRATUS_EVENTS_DEBUG
            //  Trace.Script("'" + key + "' to '" + obj.name + "'");
#endif

            // If this a delayed dispatch...
            if (nextFrame)
            {
                instance.StartCoroutine(DispatchNextFrame <T>(obj, e));
            }

            // Check if the object has been registered onto the event system.
            // If not, it will be.
            CheckRegistration(obj);

            // If there is no delegate registered to this object, do nothing.
            if (!HasDelegate(obj, key))
            {
#if STRATUS_EVENTS_DEBUG
                //if (logging.Dispatch)
                //  Trace.Script("No delegate registered to " + obj.name + " for " + eventObj.ToString());
#endif
                return;
            }

            // If we are watching events of this type
            bool watching = false;
            if (instance.eventWatchList.Contains(key))
            {
                watching = true;
            }

            // Invoke the method for every delegate
            DelegateTypeList delegateMap       = StratusEvents.instance.dispatchMap[obj][key];
            DelegateTypeList delegatesToRemove = null;
            foreach (Delegate deleg in delegateMap)
            {
                // If we are watching events of this type
                if (watching)
                {
                    StratusDebug.Log("Invoking member function on " + deleg.Target.ToString());
                }

                // Do a lazy delete if it has been nulled out?
                if (IsNull(deleg.Method) || IsNull(deleg.Target))
                {
                    if (delegatesToRemove == null)
                    {
                        delegatesToRemove = new DelegateTypeList();
                    }

                    delegatesToRemove.Add(deleg);
                    continue;
                }

                deleg.DynamicInvoke(e);
                e.handled = true;
            }

            // If any delegates were found to be null, remove them (lazy delete)
            if (delegatesToRemove != null)
            {
                foreach (Delegate deleg in delegatesToRemove)
                {
                    delegateMap.Remove(deleg);
                }
            }
        }