using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BaseEvent
{
//委托
public delegate void CallBack(uint id, params object[] objs);
//事件集合
Dictionary<uint, EventHandler> eventDic = new Dictionary<uint, EventHandler>();
public class EventHandler
{
List<CallBack> mCallBacks = new List<CallBack>();//注册事件集合
List<CallBack> disCallBacks = new List<CallBack>();//分发事件时使用
private readonly uint id;
public EventHandler(uint id)
{
this.id = id;
}
/// <summary>
/// 增加事件
/// </summary>
/// <param name="cb"></param>
public void AddCallBack(CallBack cb)
{
if (!mCallBacks.Contains(cb)) mCallBacks.Add(cb);
}
/// <summary>
/// 移除事件
/// </summary>
/// <param name="cb"></param>
public void RemoveCallBack(CallBack cb)
{
mCallBacks.Remove(cb);
}
/// <summary>
/// 处理事件
/// </summary>
/// <param name="objs"></param>
public void Handler(params object[] objs)
{
disCallBacks.AddRange(mCallBacks);
for (int i = 0; i < disCallBacks.Count; i++)
{
disCallBacks[i](id, objs);
}
disCallBacks.Clear();
}
public bool IsContainCallBack(CallBack callBack)
{
return mCallBacks.Contains(callBack);
}
}
/// <summary>
/// 注册
/// </summary>
/// <param name="id"></param>
/// <param name="cb"></param>
public void RegEvent(uint id,CallBack cb)
{
if (!eventDic.ContainsKey(id)) eventDic.Add(id, new EventHandler(id));
eventDic[id].AddCallBack(cb);
}
/// <summary>
/// 移除
/// </summary>
/// <param name="id"></param>
/// <param name="cb"></param>
public void RemoveEvent(uint id,CallBack cb)
{
if (eventDic.ContainsKey(id)) eventDic[id].RemoveCallBack(cb);
}
/// <summary>
/// 移除当前id下所有事件
/// </summary>
/// <param name="id"></param>
public void RemoveAllEventById(uint id)
{
if (eventDic.ContainsKey(id)) eventDic.Remove(id);
}
/// <summary>
/// 处理事件
/// </summary>
/// <param name="id"></param>
/// <param name="objs"></param>
public void HandlerEvent(uint id,params object[] objs)
{
if (eventDic.ContainsKey(id)) eventDic[id].Handler(objs);
}
public bool IsContainIdAndEvent(uint id,CallBack callBack)
{
return eventDic.ContainsKey(id) && eventDic[id].IsContainCallBack(callBack);
}
}
public enum EventType
{
None,
Client,
Socket,
}
/// <summary>
/// 事件管理
/// </summary>
public class EventHandlerManager
{
public BaseEvent Event = null;
public static BaseEvent mClientEvent = new BaseEvent();
public static BaseEvent mSocketEvent = new BaseEvent();
private List<CBPairs> mPairs = new List<CBPairs>();
public struct CBPairs
{
public uint id;
public BaseEvent.CallBack CallBack;
}
public EventHandlerManager(EventType eventType)
{
switch (eventType)
{
case EventType.None:
break;
case EventType.Client:
Event = mClientEvent;
break;
case EventType.Socket:
Event = mSocketEvent;
break;
default:
break;
}
}
public void Reg(uint id, BaseEvent.CallBack callBack)
{
int count = mPairs.Count;
for (int i = 0; i < count; i++)
{
if (mPairs[i].id == id && mPairs[i].CallBack == callBack)
{
if (Event.IsContainIdAndEvent(id, callBack)) return;
}
}
CBPairs cBPairs = new CBPairs
{
id = id,
CallBack = callBack
};
mPairs.Add(cBPairs);
Event.RegEvent(id, callBack);
}
/// <summary>
/// 移除单个事件
/// </summary>
/// <param name="id"></param>
/// <param name="callBack"></param>
public void RemoveEvent(uint id, BaseEvent.CallBack callBack)
{
int count = mPairs.Count;
for (int i = 0; i < count; i++)
{
if (mPairs[i].id == id && mPairs[i].CallBack == callBack)
{
mPairs.RemoveAt(i);
break;
}
}
Event.RemoveEvent(id, callBack);
}
/// <summary>
/// 移除相同id下所有数据
/// </summary>
/// <param name="id"></param>
public void RemoveEventById(uint id)
{
int count = mPairs.Count;
for (int i = 0; i < count; i++)
{
if (mPairs[i].id == id)
{
mPairs.RemoveAt(i);
i--;
break;
}
}
Event.RemoveAllEventById(id);
}
/// <summary>
/// 移除pairs下的id的事件
/// </summary>
/// <param name="id"></param>
public void RemoveEventByIdAndPairs(uint id)
{
int count = mPairs.Count;
for (int i = 0; i < count; i++)
{
if (mPairs[i].id == id)
{
mPairs.RemoveAt(i);
Event.RemoveEvent(id, mPairs[i].CallBack);
i--;
break;
}
}
}
/// <summary>
/// 移除所有
/// </summary>
/// <param name="id"></param>
public void RemoveAll(uint id)
{
int count = mPairs.Count;
for (int i = 0; i < count; i++)
{
Event.RemoveEvent(mPairs[i].id, mPairs[i].CallBack);
}
mPairs.Clear();
}
/// <summary>
/// 分发事件
/// </summary>
/// <param name="id"></param>
/// <param name="objs"></param>
public void HandlerEvent(uint id,params object[] objs)
{
Event.HandlerEvent(id, objs);
}
}
事件管理
最新推荐文章于 2022-08-09 13:56:58 发布