Newer
Older
DungeonShooting / DungeonShooting_Godot / src / framework / ui / grid / UiCell.cs
@小李xl 小李xl on 15 Aug 2023 3 KB 创建房间标记, 开发中
  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. /// 初始化数据
  79. /// </summary>
  80. public void Init(UiGrid<TUiCellNode, T> grid, TUiCellNode cellNode, int index)
  81. {
  82. if (_init)
  83. {
  84. return;
  85. }
  86.  
  87. _init = true;
  88. Grid = grid;
  89. CellNode = cellNode;
  90. //绑定点击事件
  91. if (cellNode.GetUiInstance() is BaseButton button)
  92. {
  93. button.Pressed += Click;
  94. }
  95. OnInit();
  96. SetIndex(index);
  97. }
  98. /// <summary>
  99. /// 设置当前 Cell 的值, 该函数由 UiGrid 调用
  100. /// </summary>
  101. public void SetData(T data)
  102. {
  103. Data = data;
  104. OnSetData(data);
  105. }
  106.  
  107. /// <summary>
  108. /// 设置当前 Cell 的索引, 该函数由 UiGrid 对象调用
  109. /// </summary>
  110. public void SetIndex(int index)
  111. {
  112. if (Index != index)
  113. {
  114. Index = index;
  115. OnRefreshIndex();
  116. }
  117. }
  118.  
  119. /// <summary>
  120. /// 设置是否启用该 Cell, 该函数由 UiGrid 对象调用
  121. /// </summary>
  122. public void SetEnable(bool value)
  123. {
  124. Enable = value;
  125. if (value)
  126. {
  127. OnEnable();
  128. }
  129. else
  130. {
  131. OnDisable();
  132. }
  133. }
  134.  
  135. /// <summary>
  136. /// 触发点击当前Ui, 如果 Cell 的模板为 BaseButton 类型, 则 UiCell 会自动绑定点击事件
  137. /// </summary>
  138. public void Click()
  139. {
  140. Grid.SelectIndex = Index;
  141. OnClick();
  142.  
  143. //双击判定
  144. if (_prevClickTime >= 0)
  145. {
  146. var now = DateTime.Now.Ticks / 10000;
  147. if (now <= _prevClickTime + 500)
  148. {
  149. OnDoubleClick();
  150. }
  151.  
  152. _prevClickTime = now;
  153. }
  154. else
  155. {
  156. _prevClickTime = DateTime.Now.Ticks / 10000;
  157. }
  158. }
  159. public void Destroy()
  160. {
  161. if (IsDestroyed)
  162. {
  163. return;
  164. }
  165.  
  166. OnDestroy();
  167. IsDestroyed = true;
  168. }
  169. }