UIPanelTable.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. namespace QFramework
  6. {
  7. public class UIPanelTable : UIKitTable<IPanel>
  8. {
  9. public UIKitTableIndex<string, IPanel> GameObjectNameIndex =
  10. new UIKitTableIndex<string, IPanel>(panel => panel.Transform.name);
  11. public UIKitTableIndex<Type, IPanel> TypeIndex = new UIKitTableIndex<Type, IPanel>(panel => panel.GetType());
  12. public IEnumerable<IPanel> GetPanelsByPanelSearchKeys(PanelSearchKeys panelSearchKeys)
  13. {
  14. if (panelSearchKeys.PanelType != null && (!string.IsNullOrEmpty(panelSearchKeys.GameObjName) || panelSearchKeys.Panel != null ))
  15. {
  16. return TypeIndex.Get(panelSearchKeys.PanelType)
  17. .Where(p => p.Transform.name == panelSearchKeys.GameObjName || p == panelSearchKeys.Panel);
  18. }
  19. if (panelSearchKeys.PanelType != null)
  20. {
  21. return TypeIndex.Get(panelSearchKeys.PanelType);
  22. }
  23. if (panelSearchKeys.Panel != null)
  24. {
  25. return GameObjectNameIndex.Get(panelSearchKeys.Panel.Transform.gameObject.name).Where(p => p == panelSearchKeys.Panel);
  26. }
  27. if (string.IsNullOrEmpty(panelSearchKeys.GameObjName))
  28. {
  29. return GameObjectNameIndex.Get(panelSearchKeys.GameObjName);
  30. }
  31. return Enumerable.Empty<IPanel>();
  32. }
  33. protected override void OnAdd(IPanel item)
  34. {
  35. GameObjectNameIndex.Add(item);
  36. TypeIndex.Add(item);
  37. }
  38. protected override void OnRemove(IPanel item)
  39. {
  40. GameObjectNameIndex.Remove(item);
  41. TypeIndex.Remove(item);
  42. }
  43. protected override void OnClear()
  44. {
  45. GameObjectNameIndex.Clear();
  46. TypeIndex.Clear();
  47. ;
  48. }
  49. public override IEnumerator<IPanel> GetEnumerator()
  50. {
  51. return GameObjectNameIndex.Dictionary.SelectMany(kv => kv.Value).GetEnumerator();
  52. }
  53. protected override void OnDispose()
  54. {
  55. GameObjectNameIndex.Dispose();
  56. TypeIndex.Dispose();
  57. GameObjectNameIndex = null;
  58. TypeIndex = null;
  59. }
  60. }
  61. public abstract class UIKitTable<TDataItem> : IEnumerable<TDataItem>, IDisposable
  62. {
  63. public void Add(TDataItem item)
  64. {
  65. OnAdd(item);
  66. }
  67. public void Remove(TDataItem item)
  68. {
  69. OnRemove(item);
  70. }
  71. public void Clear()
  72. {
  73. OnClear();
  74. }
  75. // 改,由于 TDataItem 是引用类型,所以直接改值即可。
  76. public void Update()
  77. {
  78. }
  79. protected abstract void OnAdd(TDataItem item);
  80. protected abstract void OnRemove(TDataItem item);
  81. protected abstract void OnClear();
  82. public abstract IEnumerator<TDataItem> GetEnumerator();
  83. IEnumerator IEnumerable.GetEnumerator()
  84. {
  85. return GetEnumerator();
  86. }
  87. public void Dispose()
  88. {
  89. OnDispose();
  90. }
  91. protected abstract void OnDispose();
  92. }
  93. public class UIKitTableIndex<TKeyType, TDataItem> : IDisposable
  94. {
  95. private Dictionary<TKeyType, List<TDataItem>> mIndex = new Dictionary<TKeyType, List<TDataItem>>();
  96. private Func<TDataItem, TKeyType> mGetKeyByDataItem = null;
  97. public UIKitTableIndex(Func<TDataItem, TKeyType> keyGetter)
  98. {
  99. mGetKeyByDataItem = keyGetter;
  100. }
  101. public IDictionary<TKeyType, List<TDataItem>> Dictionary
  102. {
  103. get { return mIndex; }
  104. }
  105. public void Add(TDataItem dataItem)
  106. {
  107. var key = mGetKeyByDataItem(dataItem);
  108. if (mIndex.ContainsKey(key))
  109. {
  110. mIndex[key].Add(dataItem);
  111. }
  112. else
  113. {
  114. var list = ListPool<TDataItem>.Get();
  115. list.Add(dataItem);
  116. mIndex.Add(key, list);
  117. }
  118. }
  119. public void Remove(TDataItem dataItem)
  120. {
  121. var key = mGetKeyByDataItem(dataItem);
  122. mIndex[key].Remove(dataItem);
  123. }
  124. public IEnumerable<TDataItem> Get(TKeyType key)
  125. {
  126. List<TDataItem> retList = null;
  127. if (mIndex.TryGetValue(key, out retList))
  128. {
  129. return retList;
  130. }
  131. // 返回一个空的集合
  132. return Enumerable.Empty<TDataItem>();
  133. }
  134. public void Clear()
  135. {
  136. foreach (var value in mIndex.Values)
  137. {
  138. value.Clear();
  139. }
  140. mIndex.Clear();
  141. }
  142. public void Dispose()
  143. {
  144. foreach (var value in mIndex.Values)
  145. {
  146. value.Release2Pool();
  147. }
  148. mIndex.Release2Pool();
  149. mIndex = null;
  150. }
  151. }
  152. }