Ejemplo n.º 1
0
        public bool AbortEvent <EventType>(bool allOfType = false) where EventType : Cv_Event
        {
            Cv_Debug.Assert((m_iActiveQueue >= 0 && m_iActiveQueue < NUM_QUEUES), "EventManager must have an active event queue.");
            Cv_EventType eType = Cv_Event.GetType <EventType>();

            var success = false;

            Cv_Debug.Log("Events", "Attempting to abort event type " + typeof(EventType).Name);

            var queue = m_EventQueues[m_iActiveQueue];

            if (allOfType)
            {
                if (queue.Remove(queue.First(e => e.Type == eType)))
                {
                    success = true;
                }
            }
            else
            {
                while (queue.Remove(queue.First(e => e.Type == eType)))
                {
                    success = true;
                }
            }

            if (success)
            {
                Cv_Debug.Log("Events", "Successfully aborted event type " + typeof(EventType).Name);
            }

            return(success);
        }
Ejemplo n.º 2
0
        public bool QueueEvent(Cv_Event newEvent, bool threadSafe = false)
        {
            if (!threadSafe)
            {
                Cv_Debug.Assert((m_iActiveQueue >= 0 && m_iActiveQueue < NUM_QUEUES), "EventManager must have an active event queue.");

                if (newEvent == null)
                {
                    Cv_Debug.Error("Invalid event in QueueEvent.");
                    return(false);
                }

                if (newEvent.WriteToLog)
                {
                    Cv_Debug.Log("Events", "Attempting to queue event " + newEvent.VGetName() + " for entity " + newEvent.EntityID);
                }

                lock (m_EventListeners)
                {
                    if (m_EventListeners.ContainsKey(newEvent.Type))
                    {
                        m_EventQueues[m_iActiveQueue].AddLast(newEvent);
                        if (newEvent.WriteToLog)
                        {
                            Cv_Debug.Log("Events", "Successfully queued event " + newEvent.VGetName());
                        }
                        return(true);
                    }
                }

                lock (m_ScriptEventListeners)
                {
                    if (m_ScriptEventListeners.ContainsKey(newEvent.Type))
                    {
                        m_EventQueues[m_iActiveQueue].AddLast(newEvent);
                        if (newEvent.WriteToLog)
                        {
                            Cv_Debug.Log("Events", "Successfully queued event " + newEvent.VGetName());
                        }
                        return(true);
                    }
                }

                Cv_Debug.Log("Events", "Skipping event " + newEvent.VGetName() + " since there are no listeners for it.");
                return(false);
            }
            else
            {
                m_RealTimeEventQueue.Enqueue(newEvent);
                return(true);
            }
        }
Ejemplo n.º 3
0
        public Cv_EventListenerHandle AddListener(string eventName, string onEvent, Cv_Entity entity)
        {
            Cv_Debug.Log("Events", "Attempting to add listener for event type " + eventName);

            Cv_EventType eType = Cv_Event.GetType(eventName);

            lock (m_ScriptEventListeners)
            {
                if (!m_ScriptEventListeners.ContainsKey(eType))
                {
                    m_ScriptEventListeners[eType] = new List <Cv_ScriptListener>();
                }

                var listeners = m_ScriptEventListeners[eType];

                foreach (var l in listeners)
                {
                    if (l.Delegate == onEvent && l.Entity == entity)
                    {
                        Cv_Debug.Warning("Attempting to double register a listener.");

                        return(Cv_EventListenerHandle.NullHandle);
                    }
                }

                listeners.Add(new Cv_ScriptListener(entity, onEvent));
            }

            Cv_Debug.Log("Events", "Successfully added listener for event type " + eventName);

            var handle = new Cv_EventListenerHandle(m_iEventHandleNum);

            m_iEventHandleNum++;

            handle.EventName        = eventName;
            handle.EventType        = eType;
            handle.ScriptDelegate   = onEvent;
            handle.IsScriptListener = true;
            handle.Entity           = entity;
            handle.Manager          = this;

            return(handle);
        }
Ejemplo n.º 4
0
        public Cv_EventListenerHandle AddListener <EventType>(NewEventDelegate callback) where EventType : Cv_Event
        {
            Cv_Debug.Log("Events", "Attempting to add listener for event type " + typeof(EventType).Name);

            Cv_EventType eType = Cv_Event.GetType <EventType>();

            lock (m_EventListeners)
            {
                if (!m_EventListeners.ContainsKey(eType))
                {
                    m_EventListeners[eType] = new List <NewEventDelegate>();
                }

                var listeners = m_EventListeners[eType];

                foreach (var l in listeners)
                {
                    if (l == callback)
                    {
                        Cv_Debug.Warning("Attempting to double register a listener.");
                        return(Cv_EventListenerHandle.NullHandle);
                    }
                }

                listeners.Add(callback);
            }

            Cv_Debug.Log("Events", "Successfully added listener for event type " + typeof(EventType).Name);

            var handle = new Cv_EventListenerHandle(m_iEventHandleNum);

            m_iEventHandleNum++;

            handle.EventType = eType;
            handle.EventName = typeof(EventType).Name;
            handle.Delegate  = callback;
            handle.Manager   = this;
            return(handle);
        }
Ejemplo n.º 5
0
        private bool RemoveListener(string eventName, string onEvent, Cv_Entity entity)
        {
            Cv_Debug.Log("Events", "Attempting to remove listener from event type " + eventName);
            var success = false;

            var eType = Cv_Event.GetType(eventName);

            lock (m_ScriptEventListeners)
            {
                if (m_ScriptEventListeners.ContainsKey(eType))
                {
                    var listeners = m_ScriptEventListeners[eType];

                    success = listeners.RemoveAll(l => l.Delegate == onEvent && l.Entity == entity) > 0;
                }
            }

            if (success)
            {
                Cv_Debug.Log("Events", "Successfully removed listener from event type " + eType);
            }

            return(success);
        }
Ejemplo n.º 6
0
        public bool RemoveListener <EventType>(NewEventDelegate listener) where EventType : Cv_Event
        {
            Cv_Debug.Log("Events", "Attempting to remove listener from event type " + typeof(EventType).Name);
            var success = false;

            Cv_EventType eType = Cv_Event.GetType <EventType>();

            lock (m_EventListeners)
            {
                if (m_EventListeners.ContainsKey(eType))
                {
                    var listeners = m_EventListeners[eType];

                    success = listeners.Remove(listener);
                }
            }

            if (success)
            {
                Cv_Debug.Log("Events", "Successfully removed listener from event type " + typeof(EventType).Name);
            }

            return(success);
        }
Ejemplo n.º 7
0
        public bool TriggerEvent(Cv_Event newEvent)
        {
            if (newEvent.WriteToLog)
            {
                Cv_Debug.Log("Events", "Attempting to trigger event " + newEvent.VGetName() + " for entity " + newEvent.EntityID);
            }

            var processed = false;

            List <NewEventDelegate> listenersCopy = null;

            lock (m_EventListeners)
            {
                List <NewEventDelegate> listeners;
                if (m_EventListeners.TryGetValue(newEvent.Type, out listeners))
                {
                    listenersCopy = new List <NewEventDelegate>(listeners);
                }
            }

            if (listenersCopy != null)
            {
                foreach (var l in listenersCopy)
                {
                    if (newEvent.WriteToLog)
                    {
                        Cv_Debug.Log("Events", "Sending event " + newEvent.VGetName() + " to listener.");
                    }

                    l(newEvent);
                    processed = true;
                }
            }

            List <Cv_ScriptListener> scriptListenersCopy = null;

            lock (m_ScriptEventListeners)
            {
                List <Cv_ScriptListener> scriptListeners;
                if (m_ScriptEventListeners.TryGetValue(newEvent.Type, out scriptListeners))
                {
                    scriptListenersCopy = new List <Cv_ScriptListener>(scriptListeners);
                }
            }

            if (scriptListenersCopy != null)
            {
                foreach (var l in scriptListenersCopy)
                {
                    if (newEvent.WriteToLog)
                    {
                        Cv_Debug.Log("Events", "Sending event " + newEvent.VGetName() + " to listener.");
                    }

                    Cv_ScriptManager.Instance.VExecuteString("Cv_EventManager", l.Delegate, false, newEvent, l.Entity);
                    processed = true;
                }
            }

            return(processed);
        }