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>();
|
|
public void RemoveAllListener(){
|
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
|
|
}
|
}
|