219 lines
6.2 KiB
C#
219 lines
6.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using AibisDream.Utility;
|
|
using UnityEngine;
|
|
|
|
namespace AibisDream.Framework
|
|
{
|
|
public class EnumEventSystem
|
|
{
|
|
public static readonly EnumEventSystem Global = new();
|
|
|
|
private readonly Dictionary<Type, IEasyEvent[]> _eventDict = new(50);
|
|
|
|
protected EnumEventSystem()
|
|
{
|
|
}
|
|
|
|
public IUnRegister Register<T>(T key, Action onEvent) where T : IConvertible
|
|
{
|
|
var kv = key.ToInt32(null);
|
|
|
|
if (_eventDict.TryGetValue(typeof(T), out var events))
|
|
{
|
|
if (events[kv] == null)
|
|
{
|
|
var tempEvent = new EasyEvent();
|
|
events[kv] = tempEvent;
|
|
return tempEvent.Register(onEvent);
|
|
}
|
|
|
|
if (events[kv] is EasyEvent targetEvent)
|
|
{
|
|
return targetEvent.Register(onEvent);
|
|
}
|
|
|
|
Debug.Log($"{key.ToString()} 注册参数错误");
|
|
return null;
|
|
}
|
|
else
|
|
{
|
|
events = new IEasyEvent[Enum.GetValues(typeof(T)).Length];
|
|
_eventDict.Add(typeof(T), events);
|
|
|
|
var tempEvent = new EasyEvent();
|
|
events[kv] = tempEvent;
|
|
|
|
return tempEvent.Register(onEvent);
|
|
}
|
|
}
|
|
|
|
public IUnRegister Register<T, TE>(T key, Action<TE> onEvent) where T : IConvertible where TE : struct
|
|
{
|
|
var kv = key.ToInt32(null);
|
|
|
|
if (_eventDict.TryGetValue(typeof(T), out var events))
|
|
{
|
|
if (events[kv] == null)
|
|
{
|
|
var tempEvent = new EasyEvent<TE>();
|
|
events[kv] = tempEvent;
|
|
return tempEvent.Register(onEvent);
|
|
}
|
|
|
|
if (events[kv] is EasyEvent<TE> targetEvent)
|
|
{
|
|
return targetEvent.Register(onEvent);
|
|
}
|
|
|
|
Debug.Log($"{key.ToString()} 注册参数错误");
|
|
return null;
|
|
}
|
|
else
|
|
{
|
|
events = new IEasyEvent[Enum.GetValues(typeof(T)).Length];
|
|
_eventDict.Add(typeof(T), events);
|
|
|
|
var tempEvent = new EasyEvent<TE>();
|
|
events[kv] = tempEvent;
|
|
|
|
return tempEvent.Register(onEvent);
|
|
}
|
|
}
|
|
|
|
public void UnRegister<T>(T key, Action onEvent) where T : IConvertible
|
|
{
|
|
var kv = key.ToInt32(null);
|
|
|
|
if (!_eventDict.TryGetValue(typeof(T), out var e)) return;
|
|
if (e.Length <= kv || e[kv] == null) return;
|
|
|
|
if (e[kv] is EasyEvent tempEvent)
|
|
{
|
|
tempEvent.UnRegister(onEvent);
|
|
}
|
|
}
|
|
|
|
public void UnRegister<T>(T key) where T : IConvertible
|
|
{
|
|
var kv = key.ToInt32(null);
|
|
|
|
if (!_eventDict.TryGetValue(typeof(T), out var e)) return;
|
|
if (e.Length <= kv || e[kv] == null) return;
|
|
|
|
e[kv].UnRegisterAll();
|
|
}
|
|
|
|
public void UnRegisterAll()
|
|
{
|
|
foreach (var eventItem in _eventDict.Values.SelectMany(eventArr => eventArr))
|
|
{
|
|
eventItem?.UnRegisterAll();
|
|
}
|
|
|
|
_eventDict.Clear();
|
|
}
|
|
|
|
public void Send<T>(T key) where T : IConvertible
|
|
{
|
|
var kv = key.ToInt32(null);
|
|
|
|
if (!_eventDict.TryGetValue(typeof(T), out var e)) return;
|
|
if (e.Length <= kv || e[kv] == null) return;
|
|
|
|
if (e[kv] is EasyEvent tempEvent)
|
|
{
|
|
tempEvent.Trigger();
|
|
}
|
|
}
|
|
|
|
public void Send<T, TE>(T key, TE param) where T : IConvertible
|
|
{
|
|
var kv = key.ToInt32(null);
|
|
|
|
if (!_eventDict.TryGetValue(typeof(T), out var e)) return;
|
|
if (e.Length <= kv || e[kv] == null) return;
|
|
|
|
if (e[kv] is EasyEvent<TE> tempEvent)
|
|
{
|
|
tempEvent.Trigger(param);
|
|
}
|
|
}
|
|
}
|
|
|
|
public class StringEventSystem
|
|
{
|
|
public static readonly StringEventSystem Global = new();
|
|
|
|
private readonly Dictionary<string, IEasyEvent> _events = new();
|
|
|
|
public IUnRegister Register(string key, Action onEvent)
|
|
{
|
|
if (_events.TryGetValue(key, out var e))
|
|
{
|
|
var easyEvent = e.As<EasyEvent>();
|
|
return easyEvent.Register(onEvent);
|
|
}
|
|
else
|
|
{
|
|
var easyEvent = new EasyEvent();
|
|
_events.Add(key, easyEvent);
|
|
return easyEvent.Register(onEvent);
|
|
}
|
|
}
|
|
|
|
public void UnRegister(string key, Action onEvent)
|
|
{
|
|
if (_events.TryGetValue(key, out var e))
|
|
{
|
|
var easyEvent = e.As<EasyEvent>();
|
|
easyEvent?.UnRegister(onEvent);
|
|
}
|
|
}
|
|
|
|
public void Send(string key)
|
|
{
|
|
if (_events.TryGetValue(key, out var e))
|
|
{
|
|
var easyEvent = e.As<EasyEvent>();
|
|
easyEvent?.Trigger();
|
|
}
|
|
}
|
|
|
|
|
|
public IUnRegister Register<T>(string key, Action<T> onEvent)
|
|
{
|
|
if (_events.TryGetValue(key, out var e))
|
|
{
|
|
var easyEvent = e.As<EasyEvent<T>>();
|
|
return easyEvent.Register(onEvent);
|
|
}
|
|
else
|
|
{
|
|
var easyEvent = new EasyEvent<T>();
|
|
_events.Add(key, easyEvent);
|
|
return easyEvent.Register(onEvent);
|
|
}
|
|
}
|
|
|
|
|
|
public void UnRegister<T>(string key, Action<T> onEvent)
|
|
{
|
|
if (_events.TryGetValue(key, out var e))
|
|
{
|
|
var easyEvent = e.As<EasyEvent<T>>();
|
|
easyEvent?.UnRegister(onEvent);
|
|
}
|
|
}
|
|
|
|
public void Send<T>(string key, T data)
|
|
{
|
|
if (_events.TryGetValue(key, out var e))
|
|
{
|
|
var easyEvent = e.As<EasyEvent<T>>();
|
|
easyEvent?.Trigger(data);
|
|
}
|
|
}
|
|
}
|
|
} |