Newer
Older
DungeonShooting / DungeonShooting_Godot / src / framework / ui / grid / UiGrid.cs
  1.  
  2.  
  3. using System;
  4. using System.Collections.Generic;
  5. using Godot;
  6.  
  7. /// <summary>
  8. /// Ui网格组件
  9. /// </summary>
  10. /// <typeparam name="TUiCellNode">Ui节点类型</typeparam>
  11. /// <typeparam name="TData">传给Cell的数据类型</typeparam>
  12. public class UiGrid<TUiCellNode, TData> : IUiGrid where TUiCellNode : IUiCellNode
  13. {
  14. public bool IsDestroyed { get; private set; }
  15.  
  16. public int SelectIndex
  17. {
  18. get => _selectIndex;
  19. set
  20. {
  21. var newIndex = Mathf.Clamp(value, -1, _cellList.Count - 1);
  22. if (_selectIndex != newIndex)
  23. {
  24. //检测新的 Cell 是否可以被选中
  25. if (newIndex >= 0)
  26. {
  27. var uiCell = _cellList[newIndex];
  28. //不能被选中, 直接跳出
  29. if (!uiCell.CanSelect())
  30. {
  31. return;
  32. }
  33. }
  34.  
  35. var prevIndex = _selectIndex;
  36. _selectIndex = newIndex;
  37.  
  38. //取消选中上一个
  39. if (prevIndex >= 0 && prevIndex < _cellList.Count)
  40. {
  41. var uiCell = _cellList[prevIndex];
  42. uiCell.OnUnSelect();
  43. }
  44.  
  45. //选中新的
  46. if (newIndex >= 0)
  47. {
  48. var uiCell = _cellList[newIndex];
  49. uiCell.OnSelect();
  50. }
  51. }
  52. }
  53. }
  54.  
  55. /// <summary>
  56. /// 选中的 Cell 包含的数据
  57. /// </summary>
  58. public TData SelectData => _selectIndex >= 0 ? _cellList[_selectIndex].Data : default;
  59.  
  60. public bool Visible
  61. {
  62. get => GridContainer.Visible;
  63. set => GridContainer.Visible = value;
  64. }
  65.  
  66. public int Count => _cellList.Count;
  67.  
  68. public GridContainer GridContainer { get; private set; }
  69.  
  70. //模板对象
  71. private TUiCellNode _template;
  72.  
  73. //模板大小
  74. private Vector2 _size = Vector2.Zero;
  75.  
  76. //cell逻辑处理类
  77. private Type _cellType;
  78.  
  79. //当前活动的cell池
  80. private List<UiCell<TUiCellNode, TData>> _cellList = new List<UiCell<TUiCellNode, TData>>();
  81.  
  82. //当前已被回收的cell池
  83. private Stack<UiCell<TUiCellNode, TData>> _cellPool = new Stack<UiCell<TUiCellNode, TData>>();
  84.  
  85. //单个cell偏移
  86. private Vector2I _cellOffset;
  87.  
  88. //列数
  89. private int _columns;
  90.  
  91. //是否自动扩展列数
  92. private bool _autoColumns;
  93.  
  94. //选中的cell索引
  95. private int _selectIndex = -1;
  96.  
  97. public UiGrid(TUiCellNode template, Type cellType)
  98. {
  99. GridContainer = new UiGridContainer(OnReady, OnProcess);
  100. GridContainer.Ready += OnReady;
  101. _template = template;
  102. _cellType = cellType;
  103. var uiInstance = _template.GetUiInstance();
  104. uiInstance.AddSibling(GridContainer);
  105. uiInstance.GetParent().RemoveChild(uiInstance);
  106. if (uiInstance is Control control)
  107. {
  108. _size = control.Size;
  109. if (control.CustomMinimumSize == Vector2.Zero)
  110. {
  111. control.CustomMinimumSize = _size;
  112. }
  113. }
  114. }
  115.  
  116. /// <summary>
  117. /// 设置每个 Cell 之间的偏移量
  118. /// </summary>
  119. public void SetCellOffset(Vector2I offset)
  120. {
  121. _cellOffset = offset;
  122. GridContainer.AddThemeConstantOverride("h_separation", offset.X);
  123. GridContainer.AddThemeConstantOverride("v_separation", offset.Y);
  124. }
  125.  
  126. /// <summary>
  127. /// 获取每个 Cell 之间的偏移量
  128. /// </summary>
  129. public Vector2I GetCellOffset()
  130. {
  131. return _cellOffset;
  132. }
  133.  
  134. /// <summary>
  135. /// 设置列数
  136. /// </summary>
  137. public void SetColumns(int columns)
  138. {
  139. _columns = columns;
  140. GridContainer.Columns = columns;
  141. }
  142.  
  143. /// <summary>
  144. /// 获取列数
  145. /// </summary>
  146. public int GetColumns()
  147. {
  148. return GridContainer.Columns;
  149. }
  150.  
  151. /// <summary>
  152. /// 设置是否开启自动扩展列, 如果开启, 则组件会根据 GridContainer 组件所占用的宽度自动设置列数
  153. /// </summary>
  154. public void SetAutoColumns(bool flag)
  155. {
  156. if (flag != _autoColumns)
  157. {
  158. _autoColumns = flag;
  159. if (_autoColumns)
  160. {
  161. GridContainer.Resized += OnGridResized;
  162. OnGridResized();
  163. }
  164. else
  165. {
  166. GridContainer.Columns = _columns;
  167. GridContainer.Resized -= OnGridResized;
  168. }
  169. }
  170. }
  171.  
  172. /// <summary>
  173. /// 获取是否开启自动扩展列
  174. /// </summary>
  175. public bool GetAutoColumns()
  176. {
  177. return _autoColumns;
  178. }
  179.  
  180. /// <summary>
  181. /// 设置当前组件布局方式是否横向扩展, 如果为 true, 则 GridContainer 的宽度会撑满父物体
  182. /// </summary>
  183. public void SetHorizontalExpand(bool flag)
  184. {
  185. SetHorizontalExpand(GridContainer, flag);
  186. }
  187.  
  188. /// <summary>
  189. /// 获取当前组件布局方式是否横向扩展
  190. /// </summary>
  191. public bool GetHorizontalExpand()
  192. {
  193. return GetHorizontalExpand(GridContainer);
  194. }
  195.  
  196. /// <summary>
  197. /// 获取所有数据
  198. /// </summary>
  199. public TData[] GetAllData()
  200. {
  201. var array = new TData[_cellList.Count];
  202. for (var i = 0; i < _cellList.Count; i++)
  203. {
  204. array[i] = _cellList[i].Data;
  205. }
  206.  
  207. return array;
  208. }
  209.  
  210. /// <summary>
  211. /// 获取所有 Cell 对象
  212. /// </summary>
  213. public UiCell<TUiCellNode, TData>[] GetAllCell()
  214. {
  215. return _cellList.ToArray();
  216. }
  217.  
  218. /// <summary>
  219. /// 根据指定索引获取数据
  220. /// </summary>
  221. public TData GetData(int index)
  222. {
  223. if (index < 0 || index >= _cellList.Count)
  224. {
  225. return default;
  226. }
  227.  
  228. return _cellList[index].Data;
  229. }
  230.  
  231. /// <summary>
  232. /// 根据指定索引获取 Cell 对象
  233. /// </summary>
  234. public UiCell<TUiCellNode, TData> GetCell(int index)
  235. {
  236. if (index < 0 || index >= _cellList.Count)
  237. {
  238. return default;
  239. }
  240.  
  241. return _cellList[index];
  242. }
  243. /// <summary>
  244. /// 根据自定义回调查询数据
  245. /// </summary>
  246. public UiCell<TUiCellNode, TData> Find(Func<UiCell<TUiCellNode, TData>, bool> func)
  247. {
  248. foreach (var uiCell in _cellList)
  249. {
  250. if (func(uiCell))
  251. {
  252. return uiCell;
  253. }
  254. }
  255.  
  256. return null;
  257. }
  258.  
  259. /// <summary>
  260. /// 设置当前网格组件中的所有 Cell 数据, 性能较低
  261. /// </summary>
  262. public void SetDataList(TData[] array)
  263. {
  264. //取消选中
  265. SelectIndex = -1;
  266. if (array.Length > _cellList.Count)
  267. {
  268. do
  269. {
  270. var cell = GetCellInstance();
  271. GridContainer.AddChild(cell.CellNode.GetUiInstance());
  272. } while (array.Length > _cellList.Count);
  273. }
  274. else if (array.Length < _cellList.Count)
  275. {
  276. do
  277. {
  278. var cell = _cellList[_cellList.Count - 1];
  279. _cellList.RemoveAt(_cellList.Count - 1);
  280. ReclaimCellInstance(cell);
  281. } while (array.Length < _cellList.Count);
  282. }
  283.  
  284. for (var i = 0; i < _cellList.Count; i++)
  285. {
  286. var data = array[i];
  287. _cellList[i].SetData(data);
  288. }
  289. }
  290.  
  291. /// <summary>
  292. /// 添加单条 Cell 数据
  293. /// </summary>
  294. public void Add(TData data)
  295. {
  296. //取消选中
  297. SelectIndex = -1;
  298. var cell = GetCellInstance();
  299. GridContainer.AddChild(cell.CellNode.GetUiInstance());
  300. cell.SetData(data);
  301. }
  302.  
  303. /// <summary>
  304. /// 修改指定索引的位置的 Cell 数据
  305. /// </summary>
  306. public void UpdateByIndex(int index, TData data)
  307. {
  308. var uiCell = GetCell(index);
  309. if (uiCell != null)
  310. {
  311. uiCell.SetData(data);
  312. }
  313. }
  314.  
  315. /// <summary>
  316. /// 移除指定索引的 Cell
  317. /// </summary>
  318. /// <param name="index"></param>
  319. public void RemoveByIndex(int index)
  320. {
  321. if (index < 0 || index >= _cellList.Count)
  322. {
  323. return;
  324. }
  325.  
  326. if (index >= _selectIndex)
  327. {
  328. //取消选中
  329. SelectIndex = -1;
  330. }
  331.  
  332. var uiCell = _cellList[index];
  333. _cellList.RemoveAt(index);
  334. ReclaimCellInstance(uiCell);
  335. //更新后面的索引
  336. for (var i = index; i < _cellList.Count; i++)
  337. {
  338. var tempCell = _cellList[i];
  339. tempCell.SetIndex(i);
  340. }
  341. }
  342.  
  343. /// <summary>
  344. /// 移除所有 Cell
  345. /// </summary>
  346. public void RemoveAll()
  347. {
  348. //取消选中
  349. SelectIndex = -1;
  350. var uiCells = _cellList.ToArray();
  351. _cellList.Clear();
  352. foreach (var uiCell in uiCells)
  353. {
  354. ReclaimCellInstance(uiCell);
  355. }
  356. }
  357.  
  358. public void Click(int index)
  359. {
  360. if (index < 0 || index >= _cellList.Count)
  361. {
  362. return;
  363. }
  364.  
  365. _cellList[index].Click();
  366. }
  367.  
  368. /// <summary>
  369. /// 对所有已经启用的 Cell 进行排序操作, 排序时会调用 Cell 的 OnSort() 函数用于处理排序逻辑<br/>
  370. /// 注意: 排序会影响 Cell 的 Index
  371. /// </summary>
  372. public void Sort()
  373. {
  374. if (_cellList.Count <= 0)
  375. {
  376. return;
  377. }
  378.  
  379. //这里记录 SelectIndex 是让排序后 SelectIndex 指向的 Cell 不变
  380. var selectIndex = SelectIndex;
  381. var selectCell = GetCell(selectIndex);
  382. //执行排序操作
  383. _cellList.Sort((a, b) => a.OnSort(b));
  384. if (selectIndex >= 0)
  385. {
  386. selectIndex = _cellList.FindIndex(cell => cell == selectCell);
  387. }
  388.  
  389. //先移除所有节点
  390. for (var i = 0; i < _cellList.Count; i++)
  391. {
  392. GridContainer.RemoveChild(_cellList[i].CellNode.GetUiInstance());
  393. }
  394.  
  395. if (selectIndex >= 0)
  396. {
  397. _selectIndex = selectIndex;
  398. }
  399.  
  400. //以新的顺序加入GridContainer
  401. for (var i = 0; i < _cellList.Count; i++)
  402. {
  403. GridContainer.AddChild(_cellList[i].CellNode.GetUiInstance());
  404. }
  405.  
  406. //刷新Index
  407. for (var i = 0; i < _cellList.Count; i++)
  408. {
  409. var cell = _cellList[i];
  410. cell.SetIndex(i);
  411. }
  412. }
  413.  
  414. /// <summary>
  415. /// 销毁当前网格组件
  416. /// </summary>
  417. public void Destroy()
  418. {
  419. if (IsDestroyed)
  420. {
  421. return;
  422. }
  423.  
  424. IsDestroyed = true;
  425.  
  426. for (var i = 0; i < _cellList.Count; i++)
  427. {
  428. _cellList[i].Destroy();
  429. }
  430.  
  431. foreach (var uiCell in _cellPool)
  432. {
  433. uiCell.Destroy();
  434. }
  435.  
  436. _cellList = null;
  437. _cellPool = null;
  438. GridContainer.QueueFree();
  439. }
  440.  
  441. private void OnReady()
  442. {
  443. if (_template.GetUiInstance() is Control control)
  444. {
  445. GridContainer.Position = control.Position;
  446. }
  447. }
  448.  
  449. private void OnProcess(float delta)
  450. {
  451. if (IsDestroyed || !_template.GetUiPanel().IsOpen)
  452. {
  453. return;
  454. }
  455.  
  456. //调用 cell 更新
  457. var uiCells = _cellPool.ToArray();
  458. for (var i = 0; i < uiCells.Length; i++)
  459. {
  460. var item = uiCells[i];
  461. if (item.Enable)
  462. {
  463. item.Process(delta);
  464. }
  465. }
  466. }
  467.  
  468. //获取 cell 实例
  469. private UiCell<TUiCellNode, TData> GetCellInstance()
  470. {
  471. if (_cellPool.Count > 0)
  472. {
  473. var cell = _cellPool.Pop();
  474. cell.SetIndex(_cellList.Count);
  475. cell.SetEnable(true);
  476. _cellList.Add(cell);
  477. return cell;
  478. }
  479.  
  480. var uiCell = Activator.CreateInstance(_cellType) as UiCell<TUiCellNode, TData>;
  481. if (uiCell is null)
  482. {
  483. throw new Exception($"cellType 无法转为'{typeof(UiCell<TUiCellNode, TData>).FullName}'类型!");
  484. }
  485.  
  486. _cellList.Add(uiCell);
  487. uiCell.Init(this, (TUiCellNode)_template.CloneUiCell(), _cellList.Count - 1);
  488. uiCell.SetEnable(true);
  489. return uiCell;
  490. }
  491.  
  492. //回收 cell
  493. private void ReclaimCellInstance(UiCell<TUiCellNode, TData> cell)
  494. {
  495. cell.SetEnable(false);
  496. GridContainer.RemoveChild(cell.CellNode.GetUiInstance());
  497. _cellPool.Push(cell);
  498. }
  499.  
  500. private void OnGridResized()
  501. {
  502. if (_autoColumns && GridContainer != null)
  503. {
  504. var width = GridContainer.Size.X;
  505. if (width <= _size.X + _cellOffset.X)
  506. {
  507. GridContainer.Columns = 1;
  508. }
  509. else
  510. {
  511. GridContainer.Columns = Mathf.FloorToInt(width / (_size.X + _cellOffset.X));
  512. }
  513. }
  514. }
  515.  
  516. /// <summary>
  517. /// 设置Ui布局方式是否横向扩展, 如果为 true, 则 GridContainer 的宽度会撑满父物体
  518. /// </summary>
  519. private static void SetHorizontalExpand(Control control, bool flag)
  520. {
  521. if (flag)
  522. {
  523. control.LayoutMode = 1;
  524. control.AnchorsPreset = (int)Control.LayoutPreset.TopWide;
  525. control.SizeFlagsHorizontal |= Control.SizeFlags.Expand;
  526. }
  527. else if ((control.SizeFlagsHorizontal & Control.SizeFlags.Expand) != 0)
  528. {
  529. control.LayoutMode = 1;
  530. control.AnchorsPreset = (int)Control.LayoutPreset.TopLeft;
  531. control.SizeFlagsHorizontal ^= Control.SizeFlags.Expand;
  532. }
  533. }
  534.  
  535. /// <summary>
  536. /// 获取Ui布局方式是否横向扩展
  537. /// </summary>
  538. private static bool GetHorizontalExpand(Control control)
  539. {
  540. return (control.SizeFlagsHorizontal & Control.SizeFlags.Expand) != 0;
  541. }
  542. }