chenxin
2020-10-27 38136a34de9aa36bf15ec7471abd56e2cba6c26f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
using System;
using System.Collections.Generic;
 
namespace KTGMGemClient
{
    /// <summary>
    /// 事件中心基类
    /// </summary>
    public class EventBase
    {
        //携带不同参数个数的委托(系统自带的Action 只有四个参数)
        public delegate void CallBack();
        public delegate void CallBack<T>(T arg);
        public delegate void CallBack<T, X>(T arg1, X arg2);
        public delegate void CallBack<T, X, Y>(T arg1, X arg2, Y arg3);
        public delegate void CallBack<T, X, Y, Z>(T arg1, X arg2, Y arg3, Z arg4);
        public delegate void CallBack<T, X, Y, Z, W>(T arg1, X arg2, Y arg3, Z arg4, W arg5);
        //存放各种事件的字典
        protected Dictionary<int, Delegate> m_EventTable = new Dictionary<int, Delegate>();
 
 
        #region 私有方法
        private void OnListenerAdding(int eventType, Delegate callback)
        {
            if (!m_EventTable.ContainsKey(eventType))
            {
                m_EventTable.Add(eventType, null);
            }
            Delegate del = m_EventTable[eventType];
            //判断该事件码对应的事件类型(参数)是否一样
            if (del != null && del.GetType() != callback.GetType())
            {
                throw new Exception(string.Format("尝试添加事件失败"));
            }
        }
 
        private void OnListenerRemoving(int eventType, Delegate callback)
        {
            if (m_EventTable.ContainsKey(eventType))
            {
                Delegate del = m_EventTable[eventType];
                if (del == null)
                {
                    throw new Exception(string.Format("移除失败,对应事件为空"));
                }
                else if (del.GetType() != callback.GetType())
                {
                    throw new Exception(string.Format("移除失败,对应事件不同"));
                }
            }
            else
            {
                throw new Exception(string.Format("移除失败,事件码为空"));
            }
        }
 
        private void OnListenerRemoved(int eventType)
        {
            if (m_EventTable[eventType] == null)
            {
                m_EventTable.Remove(eventType);
            }
        }
 
        #endregion
 
        //下面方法自行扩展
        #region 没有参数,只传回调
        //添加事件
        public void Add(int eventType, CallBack callback)
        {
            OnListenerAdding(eventType, callback);
            m_EventTable[eventType] = (CallBack)m_EventTable[eventType] + callback;
        }
 
        //移除事件
        public void Remove(int eventType, CallBack callback)
        {
            OnListenerRemoving(eventType, callback);
            m_EventTable[eventType] = (CallBack)m_EventTable[eventType] - callback;
            OnListenerRemoved(eventType);
        }
 
        //事件的广播
        public void BroadCast(int eventType)
        {
            Delegate del;
            if (m_EventTable.TryGetValue(eventType, out del))
            {
                CallBack callback = del as CallBack;
                if (callback != null)
                {
                    callback();
                }
                else
                {
                    throw new Exception(string.Format("广播事件错误,对应事件为空"));
                }
            }
 
        }
        #endregion
 
        #region 一个参数
 
        //添加事件
        public void Add<T>(int eventType, CallBack<T> callback)
        {
            OnListenerAdding(eventType, callback);
            m_EventTable[eventType] = (CallBack<T>)m_EventTable[eventType] + callback;
        }
 
        //移除事件
        public void Remove<T>(int eventType, CallBack<T> callback)
        {
            OnListenerRemoving(eventType, callback);
            m_EventTable[eventType] = (CallBack<T>)m_EventTable[eventType] - callback;
            OnListenerRemoved(eventType);
 
        }
 
        //事件的广播
        public void BroadCast<T>(int eventType, T arg)
        {
            Delegate del;
            if (m_EventTable.TryGetValue(eventType, out del))
            {
                CallBack<T> callback = del as CallBack<T>;
                if (callback != null)
                {
                    callback(arg);
                }
                else
                {
                    throw new Exception(string.Format("广播事件错误,对应事件为空"));
                }
            }
 
        }
        #endregion
 
        #region 两个参数
        //添加事件
        public void Add<T, X>(int eventType, CallBack<T, X> callback)
        {
            OnListenerAdding(eventType, callback);
            m_EventTable[eventType] = (CallBack<T, X>)m_EventTable[eventType] + callback;
        }
 
        //移除事件
        public void Remove<T, X>(int eventType, CallBack<T, X> callback)
        {
            OnListenerRemoving(eventType, callback);
            m_EventTable[eventType] = (CallBack<T, X>)m_EventTable[eventType] - callback;
            OnListenerRemoved(eventType);
 
        }
 
        //事件的广播
        public void BroadCast<T, X>(int eventType, T arg1, X arg2)
        {
            Delegate del;
            if (m_EventTable.TryGetValue(eventType, out del))
            {
                CallBack<T, X> callback = del as CallBack<T, X>;
                if (callback != null)
                {
                    callback(arg1, arg2);
                }
                else
                {
                    throw new Exception(string.Format("广播事件错误,对应事件为空"));
                }
            }
 
        }
        #endregion
 
        #region 三个参数
        //添加事件
        public void Add<T, X, Y>(int eventType, CallBack<T, X, Y> callback)
        {
            OnListenerAdding(eventType, callback);
            m_EventTable[eventType] = (CallBack<T, X, Y>)m_EventTable[eventType] + callback;
        }
 
        //移除事件
        public void Remove<T, X, Y>(int eventType, CallBack<T, X, Y> callback)
        {
            OnListenerRemoving(eventType, callback);
            m_EventTable[eventType] = (CallBack<T, X, Y>)m_EventTable[eventType] - callback;
            OnListenerRemoved(eventType);
 
        }
 
        //事件的广播
        public void BroadCast<T, X, Y>(int eventType, T arg1, X arg2, Y arg3)
        {
            Delegate del;
            if (m_EventTable.TryGetValue(eventType, out del))
            {
                CallBack<T, X, Y> callback = del as CallBack<T, X, Y>;
                if (callback != null)
                {
                    callback(arg1, arg2, arg3);
                }
                else
                {
                    throw new Exception(string.Format("广播事件错误,对应事件为空"));
                }
            }
 
        }
 
        #endregion
 
        #region 四个参数
        //添加事件
        public void Add<T, X, Y, Z>(int eventType, CallBack<T, X, Y, Z> callback)
        {
            OnListenerAdding(eventType, callback);
            m_EventTable[eventType] = (CallBack<T, X, Y, Z>)m_EventTable[eventType] + callback;
        }
 
        //移除事件
        public void Remove<T, X, Y, Z>(int eventType, CallBack<T, X, Y, Z> callback)
        {
            OnListenerRemoving(eventType, callback);
            m_EventTable[eventType] = (CallBack<T, X, Y, Z>)m_EventTable[eventType] - callback;
            OnListenerRemoved(eventType);
 
        }
 
        //事件的广播
        public void BroadCast<T, X, Y, Z>(int eventType, T arg1, X arg2, Y arg3, Z arg4)
        {
            Delegate del;
            if (m_EventTable.TryGetValue(eventType, out del))
            {
                CallBack<T, X, Y, Z> callback = del as CallBack<T, X, Y, Z>;
                if (callback != null)
                {
                    callback(arg1, arg2, arg3, arg4);
                }
                else
                {
                    throw new Exception(string.Format("广播事件错误,对应事件为空"));
                }
            }
 
        }
 
        #endregion
 
 
        #region 五个参数
        //添加事件
        public void Add<T, X, Y, Z, W>(int eventType, CallBack<T, X, Y, Z, W> callback)
        {
            OnListenerAdding(eventType, callback);
            m_EventTable[eventType] = (CallBack<T, X, Y, Z, W>)m_EventTable[eventType] + callback;
        }
 
        //移除事件
        public void Remove<T, X, Y, Z, W>(int eventType, CallBack<T, X, Y, Z, W> callback)
        {
            OnListenerRemoving(eventType, callback);
            m_EventTable[eventType] = (CallBack<T, X, Y, Z, W>)m_EventTable[eventType] - callback;
            OnListenerRemoved(eventType);
 
        }
 
        //事件的广播
        public void BroadCast<T, X, Y, Z, W>(int eventType, T arg1, X arg2, Y arg3, Z arg4, W arg5)
        {
            Delegate del;
            if (m_EventTable.TryGetValue(eventType, out del))
            {
                CallBack<T, X, Y, Z, W> callback = del as CallBack<T, X, Y, Z, W>;
                if (callback != null)
                {
                    callback(arg1, arg2, arg3, arg4, arg5);
                }
                else
                {
                    throw new Exception(string.Format("广播事件错误,对应事件为空"));
                }
            }
 
        }
 
        #endregion
 
    }
}