BindableObjectHandler.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. using System;
  2. using System.Collections.Generic;
  3. namespace IFramework
  4. {
  5. /// <summary>
  6. /// 绑定器
  7. /// </summary>
  8. public class BindableObjectHandler : Unit
  9. {
  10. struct BindEntity
  11. {
  12. private string _propertyName;
  13. private BindableObject _bind;
  14. private Type _type;
  15. private Action<string, object> _listenner { get { return _handler._callmap[_type][_propertyName]; } }
  16. private BindableObjectHandler _handler;
  17. public Type type { get { return _type; } }
  18. public string propertyName { get { return _propertyName; } }
  19. public BindableObject bindable { get { return _bind; } }
  20. public Action<string, object> setValueAction;
  21. public BindableObject.BindOperation operation;
  22. public BindEntity(BindableObject bind, string propertyName, BindableObjectHandler handler, Type type)
  23. {
  24. this._propertyName = propertyName;
  25. this._bind = bind;
  26. this._handler = handler;
  27. this._type = type;
  28. setValueAction = null;
  29. operation = BindableObject.BindOperation.Both;
  30. }
  31. public void Bind()
  32. {
  33. if (_bind.bindOperation == BindableObject.BindOperation.Both && operation == BindableObject.BindOperation.Both)
  34. bindable.Subscribe(propertyName, _listenner);
  35. }
  36. public void UnBind(bool unbind=true)
  37. {
  38. //此处更改
  39. if (_bind.bindOperation == BindableObject.BindOperation.Both && operation == BindableObject.BindOperation.Both)
  40. bindable.UnSubscribe(propertyName, _listenner);
  41. if (unbind)
  42. _handler._callmap[_type][_propertyName] -= setValueAction;
  43. }
  44. /// <summary>
  45. /// 去除监听
  46. /// </summary>
  47. public void RemoveListener()
  48. {
  49. _handler._callmap[_type][_propertyName] -= setValueAction;
  50. }
  51. }
  52. internal static BindableObjectHandler handler;
  53. private List<BindEntity> _entitys = new List<BindEntity>();
  54. private TypeNameMap _valuemap = new TypeNameMap();
  55. private Dictionary<Type, Dictionary<string, Action<string, object>>> _callmap = new Dictionary<Type, Dictionary<string, Action<string, object>>>();
  56. /// <summary>
  57. /// 绑定
  58. /// </summary>
  59. /// <typeparam name="T"></typeparam>
  60. /// <param name="setter"></param>
  61. /// <param name="getter"></param>
  62. /// <param name="operation"></param>
  63. /// <returns></returns>
  64. public BindableObjectHandler BindProperty<T>(Action<T> setter, Func<T> getter,BindableObject.BindOperation operation= BindableObject.BindOperation.Both)
  65. {
  66. Type type = typeof(T);
  67. if (!_callmap.ContainsKey(type))
  68. _callmap.Add(type, new Dictionary<string, Action<string, object>>());
  69. handler = this;
  70. T tvalue = getter.Invoke();
  71. handler = null;
  72. var lastEntity = _entitys[_entitys.Count - 1];
  73. string propertyName = lastEntity.propertyName;
  74. lastEntity.operation = operation;
  75. lastEntity.setValueAction = (name, obj) => {
  76. T t;
  77. if (obj == null)
  78. {
  79. t = default(T);
  80. }
  81. else
  82. {
  83. t = (T)obj;
  84. }
  85. _valuemap.Set<T>(name, t);
  86. if (setter!=null)
  87. {
  88. setter(t);
  89. }
  90. };
  91. _entitys[_entitys.Count - 1] = lastEntity;
  92. T value = _valuemap.Get<T>(propertyName);
  93. if (value == null)
  94. {
  95. if (!EqualityComparer<T>.Default.Equals(tvalue, default(T)))
  96. {
  97. _entitys[_entitys.Count - 1].setValueAction.Invoke(propertyName, value);
  98. }
  99. }
  100. else
  101. {
  102. if (!EqualityComparer<T>.Default.Equals(tvalue, value))
  103. {
  104. _entitys[_entitys.Count - 1].setValueAction.Invoke(propertyName, value);
  105. }
  106. }
  107. //此处更改
  108. for (int i = 0; i < _entitys.Count; i++)
  109. {
  110. if (_entitys[i].type == type && _entitys[i].propertyName == propertyName)
  111. {
  112. _entitys[i].UnBind(false);
  113. }
  114. }
  115. _callmap[type][propertyName] += _entitys[_entitys.Count - 1].setValueAction;
  116. for (int i = 0; i < _entitys.Count; i++)
  117. {
  118. if (_entitys[i].type == type && _entitys[i].propertyName == propertyName)
  119. {
  120. _entitys[i].Bind();
  121. }
  122. }
  123. return this;
  124. }
  125. internal BindableObjectHandler Subscribe(BindableObject _object, Type propertyType, string propertyName)
  126. {
  127. if (!_callmap[propertyType].ContainsKey(propertyName))
  128. _callmap[propertyType].Add(propertyName, null);
  129. var listenner = _callmap[propertyType];
  130. var bindTarget = new BindEntity(_object, propertyName, this, propertyType);
  131. _entitys.Add(bindTarget);
  132. return this;
  133. }
  134. /// <summary>
  135. /// 获取数值
  136. /// </summary>
  137. /// <typeparam name="T"></typeparam>
  138. /// <param name="name"></param>
  139. /// <returns></returns>
  140. public T GetValue<T>(string name)
  141. {
  142. return _valuemap.Get<T>(name);
  143. }
  144. /// <summary>
  145. /// 获取数值
  146. /// </summary>
  147. /// <param name="type"></param>
  148. /// <param name="name"></param>
  149. /// <returns></returns>
  150. public object GetValue(Type type,string name)
  151. {
  152. return _valuemap.Get(type,name);
  153. }
  154. /// <summary>
  155. /// 发布变化
  156. /// </summary>
  157. /// <param name="type"></param>
  158. /// <param name="value"></param>
  159. /// <param name="propertyName"></param>
  160. /// <returns></returns>
  161. public BindableObjectHandler PublishProperty(Type type,object value, string propertyName)
  162. {
  163. _valuemap.Set(type, propertyName, value);
  164. if (!_callmap.ContainsKey(type))
  165. _callmap.Add(type, new Dictionary<string, Action<string, object>>());
  166. if (!_callmap[type].ContainsKey(propertyName))
  167. _callmap[type].Add(propertyName, null);
  168. if (_callmap[type][propertyName] != null)
  169. {
  170. _callmap[type][propertyName].Invoke(propertyName, value);
  171. }
  172. return this;
  173. }
  174. /// <summary>
  175. /// 发布变化
  176. /// </summary>
  177. /// <typeparam name="T"></typeparam>
  178. /// <param name="value"></param>
  179. /// <param name="propertyName"></param>
  180. /// <returns></returns>
  181. public BindableObjectHandler PublishProperty<T>(T value, string propertyName)
  182. {
  183. Type type = typeof(T);
  184. return PublishProperty(type, value, propertyName);
  185. //_valuemap.Set(type, propertyName, value);
  186. //if (!_callmap.ContainsKey(type))
  187. // _callmap.Add(type, new Dictionary<string, Action<string, object>>());
  188. //if (!_callmap[type].ContainsKey(propertyName))
  189. // _callmap[type].Add(propertyName,new Action<string, object>((str,obj)=> { }));
  190. //if (_callmap[type][propertyName]!=null)
  191. //{
  192. // _callmap[type][propertyName].Invoke(propertyName, value);
  193. //}
  194. //return this;
  195. }
  196. /// <summary>
  197. /// 解绑全部
  198. /// </summary>
  199. public void UnBind()
  200. {
  201. //此处更改
  202. _entitys.ForEach((entity) =>
  203. {
  204. entity.UnBind(false);
  205. });
  206. _entitys.ForEach((entity) =>
  207. {
  208. entity.RemoveListener();
  209. });
  210. _entitys.Clear();
  211. }
  212. /// <summary>
  213. /// 按照对象解绑
  214. /// </summary>
  215. /// <param name="_object"></param>
  216. public void UnBind(BindableObject _object)
  217. {
  218. //此处更改
  219. _entitys.ForEach((entity) =>
  220. {
  221. entity.UnBind(false);
  222. });
  223. var result = _entitys.RemoveAll((entity) =>
  224. {
  225. if (entity.bindable != _object)
  226. {
  227. return false;
  228. }
  229. entity.RemoveListener();
  230. return true;
  231. });
  232. _entitys.ForEach((entity) =>
  233. {
  234. entity.Bind();
  235. });
  236. }
  237. /// <summary>
  238. /// 按照名字解绑
  239. /// </summary>
  240. /// <param name="propertyName"></param>
  241. public void UnBind(string propertyName)
  242. {
  243. //此处更改
  244. _entitys.ForEach((entity) =>
  245. {
  246. entity.UnBind(false);
  247. });
  248. var result = _entitys.RemoveAll((entity) =>
  249. {
  250. if (entity.propertyName != propertyName)
  251. {
  252. return false;
  253. }
  254. entity.RemoveListener();
  255. return true;
  256. });
  257. _entitys.ForEach((entity) =>
  258. {
  259. entity.Bind();
  260. });
  261. }
  262. /// <summary>
  263. /// 解绑
  264. /// </summary>
  265. /// <param name="_object"></param>
  266. /// <param name="propertyName"></param>
  267. public void UnBind(BindableObject _object, string propertyName)
  268. {
  269. //此处更改
  270. _entitys.ForEach((entity) =>
  271. {
  272. entity.UnBind(false);
  273. });
  274. var result = _entitys.RemoveAll((entity) =>
  275. {
  276. if (entity.bindable != _object || entity.propertyName != propertyName) {
  277. return false;
  278. }
  279. entity.RemoveListener();
  280. return true;
  281. });
  282. _entitys.ForEach((entity) =>
  283. {
  284. entity.Bind();
  285. });
  286. }
  287. /// <summary>
  288. /// 解绑
  289. /// </summary>
  290. /// <param name="_object"></param>
  291. /// <param name="type"></param>
  292. /// <param name="propertyName"></param>
  293. public void UnBind(BindableObject _object, Type type, string propertyName)
  294. {
  295. //此处更改
  296. _entitys.ForEach((entity) =>
  297. {
  298. entity.UnBind(false);
  299. });
  300. var result = _entitys.RemoveAll((entity) =>
  301. {
  302. if (entity.bindable != _object || entity.type != type || entity.propertyName != propertyName) {
  303. return false;
  304. }
  305. entity.RemoveListener();
  306. return true;
  307. });
  308. _entitys.ForEach((entity) =>
  309. {
  310. entity.Bind();
  311. });
  312. }
  313. /// <summary>
  314. /// 释放
  315. /// </summary>
  316. protected override void OnDispose()
  317. {
  318. UnBind();
  319. _callmap.Clear();
  320. _valuemap.Clear();
  321. }
  322. }
  323. }