You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
88 lines
2.8 KiB
C#
88 lines
2.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
|
|
namespace Game
|
|
{
|
|
public delegate void EventHandler<TParam>(TParam eventParam);
|
|
public delegate void EventVoidHandler();
|
|
public class EventManager : ManagerBase<EventManager>
|
|
{
|
|
private Dictionary<EEvent, Delegate> _mEventDict;
|
|
public EventManager() { }
|
|
public override void OnCreate()
|
|
{
|
|
_mEventDict = new Dictionary<EEvent, Delegate>();
|
|
}
|
|
public override void Update()
|
|
{
|
|
}
|
|
public override void OnDestroy()
|
|
{
|
|
}
|
|
public void AddEvent<TParam>(EEvent eventType, EventHandler<TParam> callback)
|
|
{
|
|
if (_mEventDict.TryGetValue(eventType, out var handler))
|
|
{
|
|
_mEventDict[eventType] = (EventHandler<TParam>)handler + callback;
|
|
}
|
|
else
|
|
{
|
|
_mEventDict[eventType] = callback;
|
|
}
|
|
}
|
|
public void RemoveEvent<TParam>(EEvent eventType, EventHandler<TParam> callback)
|
|
{
|
|
if (_mEventDict.TryGetValue(eventType, out var handler))
|
|
{
|
|
_mEventDict[eventType] = (EventHandler<TParam>)handler - callback;
|
|
}
|
|
}
|
|
public void SendEvent<TParam>(EEvent eventType, TParam param)
|
|
{
|
|
if (_mEventDict.TryGetValue(eventType, out var handler))
|
|
{
|
|
if (handler == null)
|
|
{
|
|
return;
|
|
}
|
|
foreach (var callback in handler.GetInvocationList())
|
|
{
|
|
((EventHandler<TParam>)callback)(param);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void AddEvent(EEvent eventType, EventVoidHandler callback)
|
|
{
|
|
if (_mEventDict.TryGetValue(eventType, out var handler))
|
|
{
|
|
_mEventDict[eventType] = (EventVoidHandler)handler + callback;
|
|
}
|
|
else
|
|
{
|
|
_mEventDict[eventType] = callback;
|
|
}
|
|
}
|
|
public void RemoveEvent(EEvent eventType, EventVoidHandler callback)
|
|
{
|
|
if (_mEventDict.TryGetValue(eventType, out var handler))
|
|
{
|
|
_mEventDict[eventType] = (EventVoidHandler)handler - callback;
|
|
}
|
|
}
|
|
public void SendEvent(EEvent eventType)
|
|
{
|
|
if (_mEventDict.TryGetValue(eventType, out var handler))
|
|
{
|
|
if (handler == null)
|
|
{
|
|
return;
|
|
}
|
|
foreach (var callback in handler.GetInvocationList())
|
|
{
|
|
((EventVoidHandler)callback)();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |