142 lines
4.5 KiB
C#
142 lines
4.5 KiB
C#
using System;
|
|
using AlicizaX.Runtime;
|
|
|
|
namespace AlicizaX.Event.Runtime
|
|
{
|
|
/// <summary>
|
|
/// 事件管理器。
|
|
/// </summary>
|
|
[UnityEngine.Scripting.Preserve]
|
|
internal sealed class EventManager : IEventManager
|
|
{
|
|
private readonly EventPool<GameEventArgs> m_EventPool;
|
|
|
|
/// <summary>
|
|
/// 初始化事件管理器的新实例。
|
|
/// </summary>
|
|
public EventManager()
|
|
{
|
|
m_EventPool = new EventPool<GameEventArgs>(EventPoolMode.AllowNoHandler | EventPoolMode.AllowMultiHandler);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取事件处理函数的数量。
|
|
/// </summary>
|
|
public int EventHandlerCount
|
|
{
|
|
get { return m_EventPool.EventHandlerCount; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取事件数量。
|
|
/// </summary>
|
|
public int EventCount
|
|
{
|
|
get { return m_EventPool.EventCount; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取游戏框架模块优先级。
|
|
/// </summary>
|
|
/// <remarks>优先级较高的模块会优先轮询,并且关闭操作会后进行。</remarks>
|
|
public int Priority
|
|
{
|
|
get => 7;
|
|
}
|
|
|
|
void IModuleAwake.Awake()
|
|
{
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// 事件管理器轮询。
|
|
/// </summary>
|
|
/// <param name="elapseSeconds">逻辑流逝时间,以秒为单位。</param>
|
|
/// <param name="realElapseSeconds">真实流逝时间,以秒为单位。</param>
|
|
void IModuleUpdate.Update(float elapseSeconds, float realElapseSeconds)
|
|
{
|
|
m_EventPool.Update(elapseSeconds, realElapseSeconds);
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 关闭并清理事件管理器。
|
|
/// </summary>
|
|
void IModule.Dispose()
|
|
{
|
|
m_EventPool.Shutdown();
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 获取事件处理函数的数量。
|
|
/// </summary>
|
|
/// <param name="id">事件类型编号。</param>
|
|
/// <returns>事件处理函数的数量。</returns>
|
|
public int Count(string id)
|
|
{
|
|
return m_EventPool.Count(id);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 检查是否存在事件处理函数。
|
|
/// </summary>
|
|
/// <param name="id">事件类型编号。</param>
|
|
/// <param name="handler">要检查的事件处理函数。</param>
|
|
/// <returns>是否存在事件处理函数。</returns>
|
|
public bool Check(string id, EventHandler<GameEventArgs> handler)
|
|
{
|
|
return m_EventPool.Check(id, handler);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 订阅事件处理函数。
|
|
/// </summary>
|
|
/// <param name="id">事件类型编号。</param>
|
|
/// <param name="handler">要订阅的事件处理函数。</param>
|
|
public void Subscribe(string id, EventHandler<GameEventArgs> handler)
|
|
{
|
|
m_EventPool.Subscribe(id, handler);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 取消订阅事件处理函数。
|
|
/// </summary>
|
|
/// <param name="id">事件类型编号。</param>
|
|
/// <param name="handler">要取消订阅的事件处理函数。</param>
|
|
public void Unsubscribe(string id, EventHandler<GameEventArgs> handler)
|
|
{
|
|
m_EventPool.Unsubscribe(id, handler);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 设置默认事件处理函数。
|
|
/// </summary>
|
|
/// <param name="handler">要设置的默认事件处理函数。</param>
|
|
public void SetDefaultHandler(EventHandler<GameEventArgs> handler)
|
|
{
|
|
m_EventPool.SetDefaultHandler(handler);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 抛出事件,这个操作是线程安全的,即使不在主线程中抛出,也可保证在主线程中回调事件处理函数,但事件会在抛出后的下一帧分发。
|
|
/// </summary>
|
|
/// <param name="sender">事件源。</param>
|
|
/// <param name="e">事件参数。</param>
|
|
public void Fire(object sender, GameEventArgs e)
|
|
{
|
|
m_EventPool.Fire(sender, e);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 抛出事件立即模式,这个操作不是线程安全的,事件会立刻分发。
|
|
/// </summary>
|
|
/// <param name="sender">事件源。</param>
|
|
/// <param name="e">事件参数。</param>
|
|
public void FireNow(object sender, GameEventArgs e)
|
|
{
|
|
m_EventPool.FireNow(sender, e);
|
|
}
|
|
}
|
|
} |