using System; using System.Collections.Generic; namespace Stary.Evo { public interface IEasyEvent { } public class EasyEvent : IEasyEvent { private Action mOnEvent = () => { }; public IUnRegister Register(Action onEvent) { mOnEvent += onEvent; return new CustomUnRegister(() => { UnRegister(onEvent); }); } public void UnRegister(Action onEvent) { mOnEvent -= onEvent; } public void Trigger() { mOnEvent?.Invoke(); } } public class EasyEvent : IEasyEvent { private Action mOnEvent = e => { }; public IUnRegister Register(Action onEvent) { mOnEvent += onEvent; return new CustomUnRegister(() => { UnRegister(onEvent); }); } public void UnRegister(Action onEvent) { mOnEvent -= onEvent; } public void Trigger(T t) { mOnEvent?.Invoke(t); } } public class EasyEvent : IEasyEvent { private Action mOnEvent = (t, k) => { }; public IUnRegister Register(Action onEvent) { mOnEvent += onEvent; return new CustomUnRegister(() => { UnRegister(onEvent); }); } public void UnRegister(Action onEvent) { mOnEvent -= onEvent; } public void Trigger(T t, K k) { mOnEvent?.Invoke(t, k); } } public class EasyEvent : IEasyEvent { private Action mOnEvent = (t, k, s) => { }; public IUnRegister Register(Action onEvent) { mOnEvent += onEvent; return new CustomUnRegister(() => { UnRegister(onEvent); }); } public void UnRegister(Action onEvent) { mOnEvent -= onEvent; } public void Trigger(T t, K k, S s) { mOnEvent?.Invoke(t, k, s); } } public class EasyEventSystems { private static EasyEventSystems _mGlobalEventSystems = new EasyEventSystems(); public static T Get() where T : IEasyEvent { return _mGlobalEventSystems.GetEvent(); } public static void Register() where T : IEasyEvent, new() { _mGlobalEventSystems.AddEvent(); } private Dictionary mTypeEvents = new Dictionary(); public void AddEvent() where T : IEasyEvent, new() { mTypeEvents.Add(typeof(T), new T()); } public T GetEvent() where T : IEasyEvent { IEasyEvent e; if (mTypeEvents.TryGetValue(typeof(T), out e)) { return (T) e; } return default; } public T GetOrAddEvent() where T : IEasyEvent, new() { var eType = typeof(T); if (mTypeEvents.TryGetValue(eType, out var e)) { return (T) e; } var t = new T(); mTypeEvents.Add(eType, t); return t; } } }