1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066 |
- using System.Threading.Tasks;
- using System.Collections.Generic;
- using System;
- using UnityEngine;
- using System.Reflection;
- /*
- 修改第三版
- 将事件从IArchitecture中剥离
- */
- namespace Blue
- {
- #region Architecture
- /// <summary>
- /// 所有功能在 IArchitecture 接口中定义
- /// 1. 注册/获取Service、Model、Utility;
- /// 2. 发送命令、事件、查询
- /// 3. 注册、取消注册事件
- /// </summary>
- public interface IArchitecture
- {
- void SetArchitecture(IArchitecture instance);
- void InitArchitecture();
- void RegisterService<T>(T service) where T : IService;
- void RegisterModel<T>(T Model) where T : IModel;
- void RegisterUtility<T>(T utility) where T : IUtility;
- T GetService<T>() where T : class, IService;
- T GetModel<T>() where T : class, IModel;
- T GetUtility<T>() where T : class, IUtility;
- }
- public abstract class AbstractArchitecture<T> : IArchitecture where T : AbstractArchitecture<T>, new()
- {
- private IOCContainer mContainer = new IOCContainer(); // 将类型、对象存储进IOC容器
- private ICommandHandler commandHandler = new DefaultCommandHandler();
- private IQueryHandler queryHandler = new DefaultQueryHandler();
- private ITypeEventSystem mTypeEventSystem = new TypeEventSystem();
- private HashSet<IService> mService;
- private HashSet<IModel> mModels;
- protected abstract void Init();
- private static IArchitecture mArchitectureInstance;
- void IArchitecture.SetArchitecture(IArchitecture instance)
- {
- mArchitectureInstance = instance;
- CanGetModelExtension.SetArchitecture(mArchitectureInstance);
- CanGetServiceExtension.SetArchitecture(mArchitectureInstance);
- CanGetUtilityExtension.SetArchitecture(mArchitectureInstance);
- CanSendCommandExtension.SetCommandHandler(commandHandler);
- CanSendQueryExtension.SetQueryHandler(queryHandler);
- CanRegisterEventExtension.SetTypeEventSystem(mTypeEventSystem);
- CanSendEventExtension.SetTypeEventSystem(mTypeEventSystem);
- }
- void IArchitecture.InitArchitecture()
- {
- Debug.Log("InitArchitectureInitArchitectureInitArchitecture");
- StartInit();
- FinishInit();
- }
- private void StartInit()
- {
- mService = new HashSet<IService>();
- mModels = new HashSet<IModel>();
- Init();
- foreach (var architectureModel in mModels)
- {
- architectureModel.OnInit();
- }
- foreach (var service in mService)
- {
- service.OnInit();
- }
- }
- private void FinishInit()
- {
- mService.Clear();
- mModels.Clear();
- mService = null;
- mModels = null;
- }
- public void RegisterService<TService>(TService service) where TService : IService
- {
- mContainer.Register<TService>(service);
- mService.Add(service);
- }
- public void RegisterModel<TModel>(TModel model) where TModel : IModel
- {
- mContainer.Register<TModel>(model);
- mModels.Add(model);
- }
- public void RegisterUtility<TUtility>(TUtility utility) where TUtility : IUtility
- {
- mContainer.Register<TUtility>(utility);
- }
- public TService GetService<TService>() where TService : class, IService
- {
- return mContainer.Get<TService>();
- }
- public TModel GetModel<TModel>() where TModel : class, IModel
- {
- return mContainer.Get<TModel>();
- }
- public TUtility GetUtility<TUtility>() where TUtility : class, IUtility
- {
- return mContainer.Get<TUtility>();
- }
- }
- /// <summary>
- /// 启动 Architecture
- /// </summary>
- public class ArchitectureInitiator
- {
- private ArchitectureInitiator() { }
- private static ArchitectureInitiator mInstance;
- private IArchitecture architectureInstance = null; // Architecture 实例
- private Type architectureType = typeof(IArchitecture);
- private bool architectureInited; // Architecture 是否初始化完成
- public static void Initiate()
- {
- Debug.Log("ArchitectureInitiatorArchitectureInitiatorArchitectureInitiatorArchitectureInitiatorArchitectureInitiator");
- mInstance = new ArchitectureInitiator();
- Type[] typeArr = Assembly.GetExecutingAssembly().GetTypes(); // 获取正在执行的程序集类型数组
- for (int i = 0; i < typeArr.Length; i++)
- {
- Type tmpType = typeArr[i];
- if (!tmpType.IsInterface)
- {
- if (mInstance.IsArchitecture(tmpType))
- {
- mInstance.CreateArchitectureInstance(tmpType);
- }
- }
- }
- mInstance.RegisterArchitecture();
- mInstance.InitArchitecture();
- }
- private void RegisterArchitecture()
- {
- if (architectureInstance != null)
- {
- architectureInstance.SetArchitecture(architectureInstance);
- }
- }
- private void InitArchitecture()
- {
- if (architectureInstance != null)
- {
- architectureInstance.InitArchitecture();
- }
- }
- private bool IsArchitecture(Type tmpType)
- {
- if (architectureInited)
- {
- return false;
- }
- if (tmpType.IsAbstract)
- {
- return false;
- }
- return architectureType.IsAssignableFrom(tmpType); // 确定指定类型 c 的实例是否能分配给当前类型的变量
- }
- private void CreateArchitectureInstance(Type archiType)
- {
- if (architectureInstance == null)
- {
- architectureInstance = (IArchitecture)Activator.CreateInstance(archiType);
- architectureInited = true;
- }
- else
- {
- throw new Exception("More than one Architecture sub-class in the application,it should be only one!");
- }
- }
- }
- public interface IOnEvent<T>
- {
- void OnEvent(T e);
- }
- public static class OnGlobalEventExtension
- {
- public static IUnRegister RegisterEvent<T>(this IOnEvent<T> self) where T :struct,IEvent
- {
- return TypeEventSystem.Global.Register<T>(self.OnEvent);
- }
- public static void UnRegisterEvent<T>(this IOnEvent<T> self) where T : struct,IEvent
- {
- TypeEventSystem.Global.UnRegister<T>(self.OnEvent);
- }
- }
- #endregion
- #region Controller
- public interface IController : ICanGetUtility, ICanGetService, ICanGetModel,
- ICanRegisterEvent, ICanSendQuery, ICanSendCommand
- {
- }
- #endregion
- #region Service
- public interface IService : ICanGetService, ICanGetModel, ICanGetUtility,
- ICanRegisterEvent, ICanSendEvent
- {
- void OnInit();
- }
- #endregion
- #region Model
- public interface IModel : ICanGetUtility, ICanSendEvent
- {
- void OnInit();
- }
- #endregion
- #region Utility
- public interface IUtility
- {
- }
- #endregion
- #region Command
- public interface ICommand : ICanGetService, ICanGetModel, ICanGetUtility,
- ICanSendEvent, ICanSendCommand, ICanSendQuery
- {
- void OnExcute();
- }
- public interface ICommand<T> : ICanGetService, ICanGetModel, ICanGetUtility,
- ICanSendEvent, ICanSendCommand, ICanSendQuery
- {
- T OnExcute();
- }
- public interface ICommandHandler
- {
- void ExcuteCommand(ICommand command);
- void ExcuteCommand<T>() where T : ICommand, new();
- T ExcuteCommand<T>(ICommand<T> command);
- }
- public class DefaultCommandHandler : ICommandHandler
- {
- public void ExcuteCommand(ICommand command)
- {
- command.OnExcute();
- }
- public void ExcuteCommand<T>() where T : ICommand, new()
- {
- ExcuteCommand(new T());
- }
- public T ExcuteCommand<T>(ICommand<T> command)
- {
- return command.OnExcute();
- }
- }
- #endregion
- #region Query
- public interface IQuery<T> : ICanGetModel, ICanGetService, ICanSendQuery
- {
- T DoQuery();
- }
- public interface IQueryAsync : ICanGetModel, ICanGetService, ICanSendQuery
- {
- void DoQueryAsync<T>(Action<T> onQueryCompleted);
- }
- public interface IQueryResult<T>
- {
- void OnQuerySucceed(Action<T> onQuerySucceed);
- void OnQueryFailed(Action onQueryFailed);
- }
- public interface IQueryHandler
- {
- T DoQuery<T>(IQuery<T> query);
- K DoQuery<T, K>() where T : IQuery<K>, new();
- IQueryResult<T> DoQueryAsync<T>(IQuery<T> query);
- IQueryResult<K> DoQueryAsync<T, K>() where T : IQuery<K>, new();
- }
- public class DefaultQueryHandler : IQueryHandler
- {
- public T DoQuery<T>(IQuery<T> query)
- {
- return query.DoQuery();
- }
- public K DoQuery<T, K>() where T : IQuery<K>, new()
- {
- return DoQuery(new T());
- }
- public IQueryResult<T> DoQueryAsync<T>(IQuery<T> query)
- {
- DefaultQueryResult<T> queryResult = new DefaultQueryResult<T>();
- var queryTask = Task.Run(() =>
- {
- return query.DoQuery();
- });
- var awaiter = queryTask.GetAwaiter();
- awaiter.OnCompleted(() =>
- {
- if (queryTask.IsFaulted | queryTask.IsCanceled)
- {
- queryResult.TriggerFailed();
- }
- else
- {
- queryResult.TriggerSuccess(awaiter.GetResult());
- }
- });
- return queryResult;
- }
- public IQueryResult<K> DoQueryAsync<T, K>() where T : IQuery<K>, new()
- {
- return DoQueryAsync(new T());
- }
- }
- public class DefaultQueryResult<T> : IQueryResult<T>
- {
- private Action<T> mOnQuerySucceed;
- private Action mOnQueryFailed;
- public void OnQuerySucceed(Action<T> onQuerySucceed)
- {
- mOnQuerySucceed += onQuerySucceed;
- }
- public void OnQueryFailed(Action onQueryFailed)
- {
- mOnQueryFailed += onQueryFailed;
- }
- public void TriggerSuccess(T result)
- {
- mOnQuerySucceed?.Invoke(result);
- }
- public void TriggerFailed()
- {
- mOnQueryFailed?.Invoke();
- }
- }
- #endregion
- #region Rule
- /// <summary>
- /// 能够 GetModel 的接口
- /// </summary>
- public interface ICanGetModel
- {
- }
- /// <summary>
- /// ICanGetModel 的静态扩展 GetModel
- /// </summary>
- public static class CanGetModelExtension
- {
- public static T GetModel<T>(this ICanGetModel self) where T : class, IModel
- {
- return _architecture.GetModel<T>();
- }
- private static IArchitecture _architecture;
- public static void SetArchitecture(IArchitecture architecture)
- {
- _architecture = architecture;
- }
- }
- /// <summary>
- /// 能够 GetService 的接口
- /// </summary>
- public interface ICanGetService
- {
- }
- /// <summary>
- /// ICanGetService 的静态扩展 GetService
- /// </summary>
- public static class CanGetServiceExtension
- {
- public static T GetService<T>(this ICanGetService self) where T : class, IService
- {
- return _architecture.GetService<T>();
- }
- private static IArchitecture _architecture;
- public static void SetArchitecture(IArchitecture architecture)
- {
- _architecture = architecture;
- }
- }
- /// <summary>
- /// 能够 GetUtility 的接口
- /// </summary>
- public interface ICanGetUtility
- {
- }
- /// <summary>
- /// ICanGetUtility 的静态扩展 GetUtility
- /// </summary>
- public static class CanGetUtilityExtension
- {
- public static T GetUtility<T>(this ICanGetUtility self) where T : class, IUtility
- {
- return _architecture.GetUtility<T>();
- }
- private static IArchitecture _architecture;
- public static void SetArchitecture(IArchitecture architecture)
- {
- _architecture = architecture;
- }
- }
- /// <summary>
- /// 能够 RegisterEvent 的接口
- /// </summary>
- public interface ICanRegisterEvent
- {
- }
- /// <summary>
- /// ICanRegisterEvent 的静态扩展 RegisterEvent
- /// </summary>
- public static class CanRegisterEventExtension
- {
- public static IUnRegister RegisterEvent<T>(this ICanRegisterEvent self, Action<T> onEvent) where T : IEvent
- {
- if(typeEventSystem==null) return null;
- return typeEventSystem.Register<T>(onEvent);
- }
- public static void UnRegisterEvent<T>(this ICanRegisterEvent self, Action<T> onEvent) where T : IEvent
- {
- typeEventSystem.UnRegister<T>(onEvent);
- }
- private static ITypeEventSystem typeEventSystem;
- public static void SetTypeEventSystem(ITypeEventSystem mTypeEventSystem)
- {
- typeEventSystem = mTypeEventSystem;
- }
- }
- /// <summary>
- /// 能够 SendCommand 的接口
- /// </summary>
- public interface ICanSendCommand
- {
- }
- /// <summary>
- /// ICanSendCommand 的静态扩展 SendCommand
- /// </summary>
- public static class CanSendCommandExtension
- {
- public static void SendCommand<T>(this ICanSendCommand self) where T : ICommand, new()
- {
- mCommandHandler.ExcuteCommand<T>();
- }
- public static void SendCommand<T>(this ICanSendCommand self, T command) where T : ICommand
- {
- mCommandHandler.ExcuteCommand(command);
- }
- public static T SendCommand<T>(this ICanSendCommand self, ICommand<T> command)
- {
- return mCommandHandler.ExcuteCommand(command);
- }
- private static ICommandHandler mCommandHandler;
- public static void SetCommandHandler(ICommandHandler commandHandler)
- {
- mCommandHandler = commandHandler;
- }
- }
- /// <summary>
- /// 能够 SendEvent 的接口
- /// </summary>
- public interface ICanSendEvent
- {
- }
- /// <summary>
- /// ICanSendEvent 的静态扩展 SendEvent
- /// </summary>
- public static class CanSendEventExtension
- {
- public static void SendEvent<T>(this ICanSendEvent self) where T : IEvent, new()
- {
- typeEventSystem.Send<T>();
- }
- public static void SendEvent<T>(this ICanSendEvent self, T e) where T : IEvent
- {
- typeEventSystem.Send<T>(e);
- }
- private static ITypeEventSystem typeEventSystem;
- public static void SetTypeEventSystem(ITypeEventSystem mTypeEventSystem)
- {
- typeEventSystem = mTypeEventSystem;
- }
- }
- /// <summary>
- /// 能够 SendQuery 的接口
- /// </summary>
- public interface ICanSendQuery
- {
- }
- /// <summary>
- /// ICanSendQuery 的静态扩展 SendQuery
- /// </summary>
- public static class CanSendQueryExtension
- {
- public static T SendQuery<T>(this ICanSendQuery self, IQuery<T> queryInstance)
- {
- return _queryHandler.DoQuery<T>(queryInstance);
- }
- public static K SendQuery<T, K>(this ICanSendQuery self) where T : IQuery<K>, new()
- {
- return _queryHandler.DoQuery<T, K>();
- }
- public static IQueryResult<T> SendQueryAsync<T>(this ICanSendQuery self, IQuery<T> queryInstance)
- {
- return _queryHandler.DoQueryAsync<T>(queryInstance);
- }
- public static IQueryResult<K> SendQueryAsync<T, K>(this ICanSendQuery self) where T : IQuery<K>, new()
- {
- return _queryHandler.DoQueryAsync<T, K>();
- }
- private static IQueryHandler _queryHandler;
- public static void SetQueryHandler(IQueryHandler queryHandler)
- {
- _queryHandler = queryHandler;
- }
- }
- #endregion
- #region TypeEventSystem
- /// <summary>
- /// 取消监听接口
- /// </summary>
- public interface IUnRegister
- {
- void UnRegister();
- }
- /// <summary>
- /// 取消监听列表接口
- /// </summary>
- public interface IUnRegisterList
- {
- List<IUnRegister> UnRegistersList { get; }
- }
- /// <summary>
- /// 取消监听列表接口的静态扩展
- /// 【IUnRegister 添加进 IUnRegisterList】
- /// 【IUnRegisterList全部取消监听】
- /// </summary>
- public static class IUnRegisterListExtension
- {
- public static void AddToUnRegisterList(this IUnRegister self, IUnRegisterList unRegisterList)
- {
- unRegisterList.UnRegistersList.Add(self);
- }
- public static void UnRegisterAll(this IUnRegisterList self)
- {
- foreach (IUnRegister unRegister in self.UnRegistersList)
- {
- unRegister.UnRegister();
- }
- self.UnRegistersList.Clear();
- }
- }
- /// <summary>
- /// 取消监听的类
- /// </summary>
- public struct CustomUnRegister : IUnRegister
- {
- private Action mOnUnregister { get; set; }
- public CustomUnRegister(Action onUnRegsiter)
- {
- mOnUnregister = onUnRegsiter;
- }
- public void UnRegister()
- {
- mOnUnregister?.Invoke();
- mOnUnregister = null;
- }
- }
- /// <summary>
- /// 物体销毁时触发取消监听
- /// </summary>
- public class UnRegisterOnDestroyTrigger : MonoBehaviour
- {
- private readonly HashSet<IUnRegister> mUnRegisters = new HashSet<IUnRegister>();
- public void AddUnRegister(IUnRegister unRegister)
- {
- mUnRegisters.Add(unRegister);
- }
- public void RemoveUnRegister(IUnRegister unRegister)
- {
- mUnRegisters.Remove(unRegister);
- }
- private void OnDestroy()
- {
- foreach (IUnRegister unRegister in mUnRegisters)
- {
- unRegister.UnRegister();
- }
- mUnRegisters.Clear();
- }
- }
- /// <summary>
- /// 取消监听的静态扩展
- /// 【物体销毁时触发取消监听】
- /// 【组件所属物体销毁时触发取消监听】
- /// </summary>
- public static class UnRegisterExtension
- {
- /// <summary>
- /// 物体:物体销毁时触发取消监听
- /// </summary>
- /// <param name="unRegister">取消监听的接口</param>
- /// <param name="component">物体对象</param>
- /// <returns>取消监听的接口</returns>
- public static IUnRegister UnRegisterWhenGameObjectDestroyed(this IUnRegister unRegister, GameObject go)
- {
- UnRegisterOnDestroyTrigger trigger = go.GetComponent<UnRegisterOnDestroyTrigger>();
- if (!trigger)
- {
- trigger = go.AddComponent<UnRegisterOnDestroyTrigger>();
- }
- trigger.AddUnRegister(unRegister);
- return unRegister;
- }
- /// <summary>
- /// 组件:物体销毁时触发取消监听
- /// </summary>
- /// <param name="unRegister">取消监听的接口</param>
- /// <param name="component">组件对象</param>
- /// <returns>取消监听的接口</returns>
- public static IUnRegister UnRegisterWhenGameObjectDestroyed<T>(this IUnRegister unRegister, T component) where T : Component
- {
- return unRegister.UnRegisterWhenGameObjectDestroyed(component.gameObject);
- }
- }
- public interface ITypeEventSystem
- {
- IUnRegister Register<T>(Action<T> onEvent) where T: IEvent;
- void UnRegister<T>(Action<T> onEvent) where T: IEvent;
- void Send<T>() where T: IEvent, new();
- void Send<T>(T e) where T: IEvent;
- }
- public class TypeEventSystem:ITypeEventSystem
- {
- private readonly EasyEvents mEvents = new EasyEvents();
- public static readonly TypeEventSystem Global = new TypeEventSystem();
- public void Send<T>() where T : IEvent,new()
- {
- mEvents.GetEvent<EasyEvent<T>>()?.Trigger(new T());
- }
- public void Send<T>(T e) where T: IEvent
- {
- mEvents.GetEvent<EasyEvent<T>>()?.Trigger(e);
- }
- public IUnRegister Register<T>(Action<T> onEvent)where T: IEvent
- {
- var e = mEvents.GetOrAddEvent<EasyEvent<T>>();
- return e.Register(onEvent);
- }
- public void UnRegister<T>(Action<T> onEvent)where T: IEvent
- {
- var e = mEvents.GetEvent<EasyEvent<T>>();
- if (e != null)
- {
- e.UnRegister(onEvent);
- }
- }
- }
- #endregion
- #region IOC
- /// <summary>
- /// IOC 容器 ,将类型对应的对象存储进字典
- /// 【注册 + 获取】
- /// </summary>
- public class IOCContainer
- {
- private Dictionary<Type, object> mInstances = new Dictionary<Type, object>();
- /// <summary>
- /// IOC 容器注册方法
- /// </summary>
- /// <param name="instance">实例</param>
- /// <typeparam name="T">指定类型</typeparam>
- public void Register<T>(T instance)
- {
- Type key = typeof(T);
- if (mInstances.ContainsKey(key))
- {
- mInstances[key] = instance;
- }
- else
- {
- mInstances.Add(key, instance);
- }
- }
- /// <summary>
- /// IOC 容器获取方法
- /// </summary>
- /// <typeparam name="T">指定类型</typeparam>
- public T Get<T>() where T : class
- {
- Type key = typeof(T);
- if (mInstances.TryGetValue(key, out object retInstance))
- {
- return retInstance as T;
- }
- return null;
- }
- }
- #endregion
- #region BindableProperty
- public interface IReadonlyBindableProperty<T>
- {
- T Value { get; }
- IUnRegister Register(Action<T> onValueChanged);
- void UnRegister(Action<T> onValueChanged);
- IUnRegister RegisterWithInitValue(Action<T> action);
- }
- public interface IBindableProperty<T> : IReadonlyBindableProperty<T>
- {
- new T Value { get; set; }
- void SetValueWithoutEvent(T newValue);
- }
- public class BindableProperty<T> : IBindableProperty<T>
- {
- private Action<T> mOnValueChanged = (v) => { };
- public BindableProperty(T defaultValue = default)
- {
- mValue = defaultValue;
- }
- protected T mValue;
- public T Value
- {
- get => GetValue();
- set
- {
- if (value == null && mValue == null) return;
- if (value != null && value.Equals(mValue)) return;
- SetValue(value);
- mOnValueChanged?.Invoke(value);
- }
- }
- protected virtual void SetValue(T newValue)
- {
- mValue = newValue;
- }
- protected virtual T GetValue()
- {
- return mValue;
- }
- public void SetValueWithoutEvent(T newValue)
- {
- mValue = newValue;
- }
- public IUnRegister Register(Action<T> onValueChanged)
- {
- mOnValueChanged += onValueChanged;
- return new BindablePropertyUnRegister<T>()
- {
- BindableProperty = this,
- OnValueChanged = onValueChanged
- };
- }
- public IUnRegister RegisterWithInitValue(Action<T> onValueChanged)
- {
- onValueChanged(mValue);
- return Register(onValueChanged);
- }
- public static implicit operator T(BindableProperty<T> property)
- {
- return property.Value;
- }
- public override string ToString()
- {
- return Value.ToString();
- }
- public void UnRegister(Action<T> onValueChanged)
- {
- mOnValueChanged -= onValueChanged;
- }
- }
- public class BindablePropertyUnRegister<T> : IUnRegister
- {
- public BindableProperty<T> BindableProperty { get; set; }
- public Action<T> OnValueChanged { get; set; }
- public void UnRegister()
- {
- BindableProperty.UnRegister(OnValueChanged);
- BindableProperty = null;
- OnValueChanged = null;
- }
- }
- #endregion
- #region EasyEvent
- 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<T> : IEasyEvent
- {
- private Action<T> mOnEvent = e => { };
- public IUnRegister Register(Action<T> onEvent)
- {
- mOnEvent += onEvent;
- return new CustomUnRegister(() =>
- {
- UnRegister(onEvent);
- });
- }
- public void UnRegister(Action<T> onEvent)
- {
- mOnEvent -= onEvent;
- }
- public void Trigger(T t)
- {
- mOnEvent?.Invoke(t);
- }
- }
- public class EasyEvent<T, K> : IEasyEvent
- {
- private Action<T, K> mOnEvent = (t, k) => { };
- public IUnRegister Register(Action<T, K> onEvent)
- {
- mOnEvent += onEvent;
- return new CustomUnRegister(() =>
- {
- UnRegister(onEvent);
- });
- }
- public void UnRegister(Action<T, K> onEvent)
- {
- mOnEvent -= onEvent;
- }
- public void Trigger(T t, K k)
- {
- mOnEvent?.Invoke(t, k);
- }
- }
- public class EasyEvent<T, K, S> : IEasyEvent
- {
- private Action<T, K, S> mOnEvent = (t, k, s) => { };
- public IUnRegister Register(Action<T, K, S> onEvent)
- {
- mOnEvent += onEvent;
- return new CustomUnRegister(() =>
- {
- UnRegister(onEvent);
- });
- }
- public void UnRegister(Action<T, K, S> onEvent)
- {
- mOnEvent -= onEvent;
- }
- public void Trigger(T t, K k, S s)
- {
- mOnEvent?.Invoke(t, k, s);
- }
- }
- public class EasyEvents
- {
- private static EasyEvents mGlobalEvents = new EasyEvents();
- public static T Get<T>() where T : IEasyEvent
- {
- return mGlobalEvents.GetEvent<T>();
- }
- public static void Register<T>() where T : IEasyEvent, new()
- {
- mGlobalEvents.AddEvent<T>();
- }
- private Dictionary<Type, IEasyEvent> mTypeEvents = new Dictionary<Type, IEasyEvent>();
- public void AddEvent<T>() where T : IEasyEvent, new()
- {
- mTypeEvents.Add(typeof(T), new T());
- }
- public T GetEvent<T>() where T : IEasyEvent
- {
- if (mTypeEvents.TryGetValue(typeof(T), out IEasyEvent e))
- {
- return (T)e;
- }
- return default;
- }
- public T GetOrAddEvent<T>() 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;
- }
- }
- #endregion
- #region Event
- public interface IEvent
- {
- }
- #endregion
- }
|