Newer
Older
DungeonShooting / DungeonShooting_Godot / src / framework / ui / grid / UiCell.cs
@小李xl 小李xl on 16 Jan 2024 4 KB 修复UiGrid产生的游离节点
  1.  
  2. using System;
  3. using Godot;
  4.  
  5. /// <summary>
  6. /// 网格组件中单个格子的数据处理类
  7. /// </summary>
  8. /// <typeparam name="TUiCellNode">ui节点类型</typeparam>
  9. /// <typeparam name="T">数据类型</typeparam>
  10. public abstract class UiCell<TUiCellNode, T> : IUiCell, IData<T> where TUiCellNode : IUiCellNode
  11. {
  12. public bool IsDestroyed { get; private set; }
  13. public bool Enable { get; private set; }
  14. public int Index { get; private set; } = -1;
  15. /// <summary>
  16. /// 所在的网格对象
  17. /// </summary>
  18. public UiGrid<TUiCellNode, T> Grid { get; private set; }
  19. /// <summary>
  20. /// 当前cell使用的Ui节点对象
  21. /// </summary>
  22. public TUiCellNode CellNode { get; private set; }
  23. /// <summary>
  24. /// 当前cell分配的数据
  25. /// </summary>
  26. public T Data { get; private set; }
  27.  
  28. private bool _init = false;
  29. //上一次点击的时间
  30. private long _prevClickTime = -1;
  31.  
  32. public IUiGrid GetGrid()
  33. {
  34. return Grid;
  35. }
  36.  
  37. public virtual void OnInit()
  38. {
  39. }
  40.  
  41. /// <summary>
  42. /// 当前cell被分配值时调用
  43. /// </summary>
  44. public virtual void OnSetData(T data)
  45. {
  46. }
  47.  
  48.  
  49. public virtual void Process(float delta)
  50. {
  51. }
  52. public virtual void OnClick()
  53. {
  54. }
  55.  
  56. public virtual void OnDoubleClick()
  57. {
  58. }
  59. public virtual void OnEnable()
  60. {
  61. }
  62. public virtual void OnDisable()
  63. {
  64. }
  65.  
  66. public virtual bool CanSelect()
  67. {
  68. return true;
  69. }
  70. public virtual void OnSelect()
  71. {
  72. }
  73. public virtual void OnUnSelect()
  74. {
  75. }
  76. public virtual void OnRefreshIndex()
  77. {
  78. }
  79. public virtual void OnDestroy()
  80. {
  81. }
  82.  
  83. /// <summary>
  84. /// 当 Cell 参与排序时调用, 参数 other 为需要对比的另一个 Cell 对象<br/>
  85. /// 函数返回 -1 表示当前 Cell 在 other 之前<br/>
  86. /// 函数返回 0 表示当前 Cell 和 other 位置相同<br/>
  87. /// 函数返回 1 表示当前 Cell 和 other 之后<br/>
  88. /// </summary>
  89. public virtual int OnSort(UiCell<TUiCellNode, T> other)
  90. {
  91. return 0;
  92. }
  93.  
  94. /// <summary>
  95. /// 初始化数据
  96. /// </summary>
  97. public void Init(UiGrid<TUiCellNode, T> grid, TUiCellNode cellNode, int index)
  98. {
  99. if (_init)
  100. {
  101. return;
  102. }
  103.  
  104. _init = true;
  105. Grid = grid;
  106. CellNode = cellNode;
  107. //绑定点击事件
  108. if (cellNode.GetUiInstance() is BaseButton button)
  109. {
  110. button.Pressed += Click;
  111. }
  112. OnInit();
  113. SetIndex(index);
  114. }
  115. /// <summary>
  116. /// 设置当前 Cell 的值, 该函数由 UiGrid 调用
  117. /// </summary>
  118. public void SetData(T data)
  119. {
  120. Data = data;
  121. OnSetData(data);
  122. }
  123.  
  124. /// <summary>
  125. /// 设置当前 Cell 的索引, 该函数由 UiGrid 对象调用
  126. /// </summary>
  127. public void SetIndex(int index)
  128. {
  129. if (Index != index)
  130. {
  131. Index = index;
  132. OnRefreshIndex();
  133. }
  134. }
  135.  
  136. /// <summary>
  137. /// 设置是否启用该 Cell, 该函数由 UiGrid 对象调用
  138. /// </summary>
  139. public void SetEnable(bool value)
  140. {
  141. Enable = value;
  142. if (value)
  143. {
  144. OnEnable();
  145. }
  146. else
  147. {
  148. OnDisable();
  149. }
  150. }
  151.  
  152. /// <summary>
  153. /// 触发点击当前Ui, 如果 Cell 的模板为 BaseButton 类型, 则 UiCell 会自动绑定点击事件
  154. /// </summary>
  155. public void Click()
  156. {
  157. Grid.SelectIndex = Index;
  158. OnClick();
  159.  
  160. //双击判定
  161. if (_prevClickTime >= 0)
  162. {
  163. var now = DateTime.Now.Ticks / 10000;
  164. if (now <= _prevClickTime + 500)
  165. {
  166. OnDoubleClick();
  167. }
  168.  
  169. _prevClickTime = now;
  170. }
  171. else
  172. {
  173. _prevClickTime = DateTime.Now.Ticks / 10000;
  174. }
  175. }
  176. public void Destroy()
  177. {
  178. if (IsDestroyed)
  179. {
  180. return;
  181. }
  182.  
  183. OnDestroy();
  184. CellNode.GetUiInstance().QueueFree();
  185. IsDestroyed = true;
  186. }
  187. }