事件管理器

使用

   ListenMgr.GetIns().Register(EventType.OnLevelUp, OnLvUpCB);
   ListenMgr.GetIns().Trigger(EventType.OnLevelUp);

   ListenMgr.GetIns().Register<int>(EventType.OnBloodChange, OnBloodChangeCB);
   ListenMgr.GetIns().Trigger<int>(EventType.OnBloodChange, 100);

回调

        private static void OnLvUpCB()
        {
            Console.WriteLine("CB");
        }
        private static void OnBloodChangeCB(int changeValue)
        {
            Console.WriteLine(changeValue);
        }

事件管理器

    public enum EventType
    {
        OnLevelUp,
        OnBloodChange
    }

    public class ListenMgr
    {
        public delegate void Callback();
        public delegate void Callback<T>(T arg1);
        public delegate void Callback<T, U>(T arg1, U arg2);
        public delegate void Callback<T, U, V>(T arg1, U arg2, V arg3);

        private Dictionary<EventType, Delegate> m_dicEvents = new Dictionary<EventType, Delegate>();

        #region Singleton

        private static ListenMgr listenMgr;
        private ListenMgr() { }
        public static ListenMgr GetIns()
        {
            if (listenMgr == null)
                listenMgr = new ListenMgr();
            return listenMgr;
        }

        #endregion

        #region Register

        public void Register(EventType eventType, Callback handler)
        {
            if (!m_dicEvents.ContainsKey(eventType))
                m_dicEvents[eventType] = null;
            m_dicEvents[eventType] = (Callback)m_dicEvents[eventType] + handler;
            /* 委托源码 *
            this.m_dicEvents[eventType] = (Callback) Delegate.Combine((Callback) this.m_dicEvents[eventType], handler);
            [__DynamicallyInvokable]
            public static Delegate Combine(Delegate a, Delegate b)
            {
                if (a == null)
                {
                    return b;
                }
                return a.CombineImpl(b);
            }
             * */
        }
        public void Register<T>(EventType eventType, Callback<T> handler)
        {
            if (!m_dicEvents.ContainsKey(eventType))
                m_dicEvents[eventType] = null;
            m_dicEvents[eventType] = (Callback<T>)m_dicEvents[eventType] + handler;
        }
        public void Register<T, K>(EventType eventType, Callback<T, K> handler)
        {
            if (!m_dicEvents.ContainsKey(eventType))
                m_dicEvents[eventType] = null;
            m_dicEvents[eventType] = (Callback<T, K>)m_dicEvents[eventType] + handler;
        }
        public void Register<T, K, U>(EventType eventType, Callback<T, K, U> handler)
        {
            if (!m_dicEvents.ContainsKey(eventType))
                m_dicEvents[eventType] = null;
            m_dicEvents[eventType] = (Callback<T, K, U>)m_dicEvents[eventType] + handler;
        }

        #endregion

        #region Unregister

        public void Unregister(EventType eventType, Callback handler)
        {
            if (m_dicEvents.ContainsKey(eventType))
                m_dicEvents[eventType] = (Callback)m_dicEvents[eventType] - handler;
        }
        public void Unregister<T>(EventType eventType, Callback<T> handler)
        {
            if (m_dicEvents.ContainsKey(eventType))
                m_dicEvents[eventType] = (Callback<T>)m_dicEvents[eventType] - handler;
        }
        public void Unregister<T, K>(EventType eventType, Callback<T, K> handler)
        {
            if (m_dicEvents.ContainsKey(eventType))
                m_dicEvents[eventType] = (Callback<T, K>)m_dicEvents[eventType] - handler;
        }
        public void Unregister<T, K, U>(EventType eventType, Callback<T, K, U> handler)
        {
            if (m_dicEvents.ContainsKey(eventType))
                m_dicEvents[eventType] = (Callback<T, K, U>)m_dicEvents[eventType] - handler;
        }

        #endregion

        #region Trigger

        public void Trigger(EventType eventType)
        {
            Delegate d;
            if (m_dicEvents.TryGetValue(eventType, out d))
            {
                Callback callback = d as Callback;
                if (callback != null)
                    callback();
            }
        }
        public void Trigger<T>(EventType eventType, T args)
        {
            Delegate d;
            if (m_dicEvents.TryGetValue(eventType, out d))
            {
                Callback<T> callback = d as Callback<T>;
                if (callback != null)
                    callback(args);
            }
        }
        public void Trigger<T, K>(EventType eventType, T argst, K agrsu)
        {
            Delegate d;
            if (m_dicEvents.TryGetValue(eventType, out d))
            {
                Callback<T, K> callback = d as Callback<T, K>;
                if (callback != null)
                    callback(argst, agrsu);
            }
        }
        public void Trigger<T, K, U>(EventType eventType, T argst, K argsk, U argsu)
        {
            Delegate d;
            if (m_dicEvents.TryGetValue(eventType, out d))
            {
                Callback<T, K, U> callback = d as Callback<T, K, U>;
                if (callback != null)
                    callback(argst, argsk, argsu);
            }
        }

        #endregion
    }

 

posted @ 2016-08-09 18:53  贴心小冰棍  阅读(293)  评论(0编辑  收藏  举报