123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354 |
- using System;
- using System.Collections.Generic;
- namespace IFramework
- {
- /// <summary>
- /// 绑定器
- /// </summary>
- public class BindableObjectHandler : Unit
- {
- struct BindEntity
- {
- private string _propertyName;
- private BindableObject _bind;
- private Type _type;
- private Action<string, object> _listenner { get { return _handler._callmap[_type][_propertyName]; } }
- private BindableObjectHandler _handler;
- public Type type { get { return _type; } }
- public string propertyName { get { return _propertyName; } }
- public BindableObject bindable { get { return _bind; } }
- public Action<string, object> setValueAction;
- public BindableObject.BindOperation operation;
- public BindEntity(BindableObject bind, string propertyName, BindableObjectHandler handler, Type type)
- {
- this._propertyName = propertyName;
- this._bind = bind;
- this._handler = handler;
- this._type = type;
- setValueAction = null;
- operation = BindableObject.BindOperation.Both;
- }
- public void Bind()
- {
- if (_bind.bindOperation == BindableObject.BindOperation.Both && operation == BindableObject.BindOperation.Both)
- bindable.Subscribe(propertyName, _listenner);
- }
- public void UnBind(bool unbind=true)
- {
- //此处更改
- if (_bind.bindOperation == BindableObject.BindOperation.Both && operation == BindableObject.BindOperation.Both)
- bindable.UnSubscribe(propertyName, _listenner);
- if (unbind)
- _handler._callmap[_type][_propertyName] -= setValueAction;
- }
- /// <summary>
- /// 去除监听
- /// </summary>
- public void RemoveListener()
- {
- _handler._callmap[_type][_propertyName] -= setValueAction;
- }
- }
- internal static BindableObjectHandler handler;
- private List<BindEntity> _entitys = new List<BindEntity>();
- private TypeNameMap _valuemap = new TypeNameMap();
- private Dictionary<Type, Dictionary<string, Action<string, object>>> _callmap = new Dictionary<Type, Dictionary<string, Action<string, object>>>();
- /// <summary>
- /// 绑定
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="setter"></param>
- /// <param name="getter"></param>
- /// <param name="operation"></param>
- /// <returns></returns>
- public BindableObjectHandler BindProperty<T>(Action<T> setter, Func<T> getter,BindableObject.BindOperation operation= BindableObject.BindOperation.Both)
- {
- Type type = typeof(T);
- if (!_callmap.ContainsKey(type))
- _callmap.Add(type, new Dictionary<string, Action<string, object>>());
- handler = this;
- T tvalue = getter.Invoke();
- handler = null;
- var lastEntity = _entitys[_entitys.Count - 1];
- string propertyName = lastEntity.propertyName;
- lastEntity.operation = operation;
- lastEntity.setValueAction = (name, obj) => {
- T t;
- if (obj == null)
- {
- t = default(T);
- }
- else
- {
- t = (T)obj;
- }
- _valuemap.Set<T>(name, t);
- if (setter!=null)
- {
- setter(t);
- }
- };
- _entitys[_entitys.Count - 1] = lastEntity;
- T value = _valuemap.Get<T>(propertyName);
- if (value == null)
- {
- if (!EqualityComparer<T>.Default.Equals(tvalue, default(T)))
- {
- _entitys[_entitys.Count - 1].setValueAction.Invoke(propertyName, value);
- }
- }
- else
- {
- if (!EqualityComparer<T>.Default.Equals(tvalue, value))
- {
- _entitys[_entitys.Count - 1].setValueAction.Invoke(propertyName, value);
- }
- }
- //此处更改
- for (int i = 0; i < _entitys.Count; i++)
- {
- if (_entitys[i].type == type && _entitys[i].propertyName == propertyName)
- {
- _entitys[i].UnBind(false);
- }
- }
- _callmap[type][propertyName] += _entitys[_entitys.Count - 1].setValueAction;
- for (int i = 0; i < _entitys.Count; i++)
- {
- if (_entitys[i].type == type && _entitys[i].propertyName == propertyName)
- {
- _entitys[i].Bind();
- }
- }
- return this;
- }
- internal BindableObjectHandler Subscribe(BindableObject _object, Type propertyType, string propertyName)
- {
- if (!_callmap[propertyType].ContainsKey(propertyName))
- _callmap[propertyType].Add(propertyName, null);
- var listenner = _callmap[propertyType];
- var bindTarget = new BindEntity(_object, propertyName, this, propertyType);
- _entitys.Add(bindTarget);
- return this;
- }
- /// <summary>
- /// 获取数值
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="name"></param>
- /// <returns></returns>
- public T GetValue<T>(string name)
- {
- return _valuemap.Get<T>(name);
- }
- /// <summary>
- /// 获取数值
- /// </summary>
- /// <param name="type"></param>
- /// <param name="name"></param>
- /// <returns></returns>
- public object GetValue(Type type,string name)
- {
- return _valuemap.Get(type,name);
- }
- /// <summary>
- /// 发布变化
- /// </summary>
- /// <param name="type"></param>
- /// <param name="value"></param>
- /// <param name="propertyName"></param>
- /// <returns></returns>
- public BindableObjectHandler PublishProperty(Type type,object value, string propertyName)
- {
- _valuemap.Set(type, propertyName, value);
- if (!_callmap.ContainsKey(type))
- _callmap.Add(type, new Dictionary<string, Action<string, object>>());
- if (!_callmap[type].ContainsKey(propertyName))
- _callmap[type].Add(propertyName, null);
- if (_callmap[type][propertyName] != null)
- {
- _callmap[type][propertyName].Invoke(propertyName, value);
- }
- return this;
- }
- /// <summary>
- /// 发布变化
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="value"></param>
- /// <param name="propertyName"></param>
- /// <returns></returns>
- public BindableObjectHandler PublishProperty<T>(T value, string propertyName)
- {
- Type type = typeof(T);
- return PublishProperty(type, value, propertyName);
- //_valuemap.Set(type, propertyName, value);
- //if (!_callmap.ContainsKey(type))
- // _callmap.Add(type, new Dictionary<string, Action<string, object>>());
- //if (!_callmap[type].ContainsKey(propertyName))
- // _callmap[type].Add(propertyName,new Action<string, object>((str,obj)=> { }));
- //if (_callmap[type][propertyName]!=null)
- //{
- // _callmap[type][propertyName].Invoke(propertyName, value);
- //}
- //return this;
- }
- /// <summary>
- /// 解绑全部
- /// </summary>
- public void UnBind()
- {
- //此处更改
- _entitys.ForEach((entity) =>
- {
- entity.UnBind(false);
- });
- _entitys.ForEach((entity) =>
- {
- entity.RemoveListener();
- });
- _entitys.Clear();
- }
- /// <summary>
- /// 按照对象解绑
- /// </summary>
- /// <param name="_object"></param>
- public void UnBind(BindableObject _object)
- {
- //此处更改
- _entitys.ForEach((entity) =>
- {
- entity.UnBind(false);
- });
- var result = _entitys.RemoveAll((entity) =>
- {
- if (entity.bindable != _object)
- {
- return false;
- }
- entity.RemoveListener();
- return true;
- });
- _entitys.ForEach((entity) =>
- {
- entity.Bind();
- });
- }
- /// <summary>
- /// 按照名字解绑
- /// </summary>
- /// <param name="propertyName"></param>
- public void UnBind(string propertyName)
- {
- //此处更改
- _entitys.ForEach((entity) =>
- {
- entity.UnBind(false);
- });
- var result = _entitys.RemoveAll((entity) =>
- {
- if (entity.propertyName != propertyName)
- {
- return false;
- }
- entity.RemoveListener();
- return true;
- });
- _entitys.ForEach((entity) =>
- {
- entity.Bind();
- });
- }
- /// <summary>
- /// 解绑
- /// </summary>
- /// <param name="_object"></param>
- /// <param name="propertyName"></param>
- public void UnBind(BindableObject _object, string propertyName)
- {
- //此处更改
- _entitys.ForEach((entity) =>
- {
- entity.UnBind(false);
- });
- var result = _entitys.RemoveAll((entity) =>
- {
- if (entity.bindable != _object || entity.propertyName != propertyName) {
- return false;
- }
- entity.RemoveListener();
- return true;
- });
- _entitys.ForEach((entity) =>
- {
- entity.Bind();
- });
- }
- /// <summary>
- /// 解绑
- /// </summary>
- /// <param name="_object"></param>
- /// <param name="type"></param>
- /// <param name="propertyName"></param>
- public void UnBind(BindableObject _object, Type type, string propertyName)
- {
- //此处更改
- _entitys.ForEach((entity) =>
- {
- entity.UnBind(false);
- });
- var result = _entitys.RemoveAll((entity) =>
- {
- if (entity.bindable != _object || entity.type != type || entity.propertyName != propertyName) {
- return false;
- }
- entity.RemoveListener();
- return true;
- });
- _entitys.ForEach((entity) =>
- {
- entity.Bind();
- });
- }
- /// <summary>
- /// 释放
- /// </summary>
- protected override void OnDispose()
- {
- UnBind();
- _callmap.Clear();
- _valuemap.Clear();
- }
- }
- }
|