コード例 #1
0
ファイル: Cv_EventManager.cs プロジェクト: jocamar/Caravel
        public bool RemoveListener(Cv_EventListenerHandle listenerHandle)
        {
            if (listenerHandle.IsScriptListener)
            {
                return(RemoveListener(listenerHandle.EventName, listenerHandle.ScriptDelegate, listenerHandle.Entity));
            }
            else
            {
                Cv_Debug.Log("Events", "Attempting to remove listener from event type " + listenerHandle.EventName);
                var success = false;

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

                        success = listeners.Remove(listenerHandle.Delegate);
                    }
                }

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

                return(success);
            }
        }
コード例 #2
0
ファイル: Cv_EventManager.cs プロジェクト: jocamar/Caravel
        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);
        }
コード例 #3
0
ファイル: Cv_EventManager.cs プロジェクト: jocamar/Caravel
        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);
        }