Newer
Older
DungeonShooting / DungeonShooting_Godot / src / framework / ui / grid / UiCell.cs
  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. public virtual void OnInit()
  32. {
  33. }
  34.  
  35. /// <summary>
  36. /// 当前cell被分配值时调用
  37. /// </summary>
  38. public virtual void OnSetData(T data)
  39. {
  40. }
  41.  
  42.  
  43. public virtual void Process(float delta)
  44. {
  45. }
  46. public virtual void OnClick()
  47. {
  48. }
  49.  
  50. public virtual void OnDoubleClick()
  51. {
  52. }
  53. public virtual void OnEnable()
  54. {
  55. }
  56. public virtual void OnDisable()
  57. {
  58. }
  59.  
  60. public virtual bool CanSelect()
  61. {
  62. return true;
  63. }
  64. public virtual void OnSelect()
  65. {
  66. }
  67. public virtual void OnUnSelect()
  68. {
  69. }
  70. public virtual void OnRefreshIndex()
  71. {
  72. }
  73. public virtual void OnDestroy()
  74. {
  75. }
  76.  
  77. /// <summary>
  78. /// 当 Cell 参与排序时调用, 参数 other 为需要对比的另一个 Cell 对象<br/>
  79. /// 函数返回 -1 表示当前 Cell 在 other 之前<br/>
  80. /// 函数返回 0 表示当前 Cell 和 other 位置相同<br/>
  81. /// 函数返回 1 表示当前 Cell 和 other 之后<br/>
  82. /// </summary>
  83. public virtual int OnSort(UiCell<TUiCellNode, T> other)
  84. {
  85. return 0;
  86. }
  87.  
  88. /// <summary>
  89. /// 初始化数据
  90. /// </summary>
  91. public void Init(UiGrid<TUiCellNode, T> grid, TUiCellNode cellNode, int index)
  92. {
  93. if (_init)
  94. {
  95. return;
  96. }
  97.  
  98. _init = true;
  99. Grid = grid;
  100. CellNode = cellNode;
  101. //绑定点击事件
  102. if (cellNode.GetUiInstance() is BaseButton button)
  103. {
  104. button.Pressed += Click;
  105. }
  106. OnInit();
  107. SetIndex(index);
  108. }
  109. /// <summary>
  110. /// 设置当前 Cell 的值, 该函数由 UiGrid 调用
  111. /// </summary>
  112. public void SetData(T data)
  113. {
  114. Data = data;
  115. OnSetData(data);
  116. }
  117.  
  118. /// <summary>
  119. /// 设置当前 Cell 的索引, 该函数由 UiGrid 对象调用
  120. /// </summary>
  121. public void SetIndex(int index)
  122. {
  123. if (Index != index)
  124. {
  125. Index = index;
  126. OnRefreshIndex();
  127. }
  128. }
  129.  
  130. /// <summary>
  131. /// 设置是否启用该 Cell, 该函数由 UiGrid 对象调用
  132. /// </summary>
  133. public void SetEnable(bool value)
  134. {
  135. Enable = value;
  136. if (value)
  137. {
  138. OnEnable();
  139. }
  140. else
  141. {
  142. OnDisable();
  143. }
  144. }
  145.  
  146. /// <summary>
  147. /// 触发点击当前Ui, 如果 Cell 的模板为 BaseButton 类型, 则 UiCell 会自动绑定点击事件
  148. /// </summary>
  149. public void Click()
  150. {
  151. Grid.SelectIndex = Index;
  152. OnClick();
  153.  
  154. //双击判定
  155. if (_prevClickTime >= 0)
  156. {
  157. var now = DateTime.Now.Ticks / 10000;
  158. if (now <= _prevClickTime + 500)
  159. {
  160. OnDoubleClick();
  161. }
  162.  
  163. _prevClickTime = now;
  164. }
  165. else
  166. {
  167. _prevClickTime = DateTime.Now.Ticks / 10000;
  168. }
  169. }
  170. public void Destroy()
  171. {
  172. if (IsDestroyed)
  173. {
  174. return;
  175. }
  176.  
  177. OnDestroy();
  178. IsDestroyed = true;
  179. }
  180. }