Newer
Older
DungeonShooting / DungeonShooting_Godot / src / framework / ui / grid / UiGrid.cs
@lijincheng lijincheng on 27 Sep 2023 13 KB 更新UiGrid
  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.  
  244. /// <summary>
  245. /// 设置当前网格组件中的所有 Cell 数据, 性能较低
  246. /// </summary>
  247. public void SetDataList(TData[] array)
  248. {
  249. //取消选中
  250. SelectIndex = -1;
  251. if (array.Length > _cellList.Count)
  252. {
  253. do
  254. {
  255. var cell = GetCellInstance();
  256. GridContainer.AddChild(cell.CellNode.GetUiInstance());
  257. } while (array.Length > _cellList.Count);
  258. }
  259. else if (array.Length < _cellList.Count)
  260. {
  261. do
  262. {
  263. var cell = _cellList[_cellList.Count - 1];
  264. _cellList.RemoveAt(_cellList.Count - 1);
  265. ReclaimCellInstance(cell);
  266. } while (array.Length < _cellList.Count);
  267. }
  268.  
  269. for (var i = 0; i < _cellList.Count; i++)
  270. {
  271. var data = array[i];
  272. _cellList[i].SetData(data);
  273. }
  274. }
  275.  
  276. /// <summary>
  277. /// 添加单条 Cell 数据
  278. /// </summary>
  279. public void Add(TData data)
  280. {
  281. //取消选中
  282. SelectIndex = -1;
  283. var cell = GetCellInstance();
  284. GridContainer.AddChild(cell.CellNode.GetUiInstance());
  285. cell.SetData(data);
  286. }
  287.  
  288. /// <summary>
  289. /// 修改指定索引的位置的 Cell 数据
  290. /// </summary>
  291. public void UpdateByIndex(int index, TData data)
  292. {
  293. var uiCell = GetCell(index);
  294. if (uiCell != null)
  295. {
  296. uiCell.SetData(data);
  297. }
  298. }
  299.  
  300. /// <summary>
  301. /// 移除指定索引的 Cell
  302. /// </summary>
  303. /// <param name="index"></param>
  304. public void RemoveByIndex(int index)
  305. {
  306. if (index < 0 || index >= _cellList.Count)
  307. {
  308. return;
  309. }
  310.  
  311. if (index >= _selectIndex)
  312. {
  313. //取消选中
  314. SelectIndex = -1;
  315. }
  316.  
  317. var uiCell = _cellList[index];
  318. _cellList.RemoveAt(index);
  319. ReclaimCellInstance(uiCell);
  320. //更新后面的索引
  321. for (var i = index; i < _cellList.Count; i++)
  322. {
  323. var tempCell = _cellList[i];
  324. tempCell.SetIndex(i);
  325. }
  326. }
  327.  
  328. /// <summary>
  329. /// 移除所有 Cell
  330. /// </summary>
  331. public void RemoveAll()
  332. {
  333. //取消选中
  334. SelectIndex = -1;
  335. var uiCells = _cellList.ToArray();
  336. _cellList.Clear();
  337. foreach (var uiCell in uiCells)
  338. {
  339. ReclaimCellInstance(uiCell);
  340. }
  341. }
  342.  
  343. public void Click(int index)
  344. {
  345. if (index < 0 || index >= _cellList.Count)
  346. {
  347. return;
  348. }
  349.  
  350. _cellList[index].Click();
  351. }
  352.  
  353. /// <summary>
  354. /// 对所有已经启用的 Cell 进行排序操作, 排序时会调用 Cell 的 OnSort() 函数用于处理排序逻辑<br/>
  355. /// 注意: 排序会影响 Cell 的 Index
  356. /// </summary>
  357. public void Sort()
  358. {
  359. if (_cellList.Count <= 0)
  360. {
  361. return;
  362. }
  363.  
  364. //这里记录 SelectIndex 是让排序后 SelectIndex 指向的 Cell 不变
  365. var selectIndex = SelectIndex;
  366. var selectCell = GetCell(selectIndex);
  367. //执行排序操作
  368. _cellList.Sort((a, b) => a.OnSort(b));
  369. if (selectIndex >= 0)
  370. {
  371. selectIndex = _cellList.FindIndex(cell => cell == selectCell);
  372. }
  373.  
  374. //先移除所有节点
  375. for (var i = 0; i < _cellList.Count; i++)
  376. {
  377. GridContainer.RemoveChild(_cellList[i].CellNode.GetUiInstance());
  378. }
  379.  
  380. if (selectIndex >= 0)
  381. {
  382. _selectIndex = selectIndex;
  383. }
  384.  
  385. //以新的顺序加入GridContainer
  386. for (var i = 0; i < _cellList.Count; i++)
  387. {
  388. GridContainer.AddChild(_cellList[i].CellNode.GetUiInstance());
  389. }
  390.  
  391. //刷新Index
  392. for (var i = 0; i < _cellList.Count; i++)
  393. {
  394. var cell = _cellList[i];
  395. cell.SetIndex(i);
  396. }
  397. }
  398.  
  399. /// <summary>
  400. /// 销毁当前网格组件
  401. /// </summary>
  402. public void Destroy()
  403. {
  404. if (IsDestroyed)
  405. {
  406. return;
  407. }
  408.  
  409. IsDestroyed = true;
  410.  
  411. for (var i = 0; i < _cellList.Count; i++)
  412. {
  413. _cellList[i].Destroy();
  414. }
  415.  
  416. foreach (var uiCell in _cellPool)
  417. {
  418. uiCell.Destroy();
  419. }
  420.  
  421. _cellList = null;
  422. _cellPool = null;
  423. GridContainer.QueueFree();
  424. }
  425.  
  426. private void OnReady()
  427. {
  428. if (_template.GetUiInstance() is Control control)
  429. {
  430. GridContainer.Position = control.Position;
  431. }
  432. }
  433.  
  434. private void OnProcess(float delta)
  435. {
  436. if (IsDestroyed || !_template.GetUiPanel().IsOpen)
  437. {
  438. return;
  439. }
  440.  
  441. //调用 cell 更新
  442. var uiCells = _cellPool.ToArray();
  443. for (var i = 0; i < uiCells.Length; i++)
  444. {
  445. var item = uiCells[i];
  446. if (item.Enable)
  447. {
  448. item.Process(delta);
  449. }
  450. }
  451. }
  452.  
  453. //获取 cell 实例
  454. private UiCell<TUiCellNode, TData> GetCellInstance()
  455. {
  456. if (_cellPool.Count > 0)
  457. {
  458. var cell = _cellPool.Pop();
  459. cell.SetIndex(_cellList.Count);
  460. cell.SetEnable(true);
  461. _cellList.Add(cell);
  462. return cell;
  463. }
  464.  
  465. var uiCell = Activator.CreateInstance(_cellType) as UiCell<TUiCellNode, TData>;
  466. if (uiCell is null)
  467. {
  468. throw new Exception($"cellType 无法转为'{typeof(UiCell<TUiCellNode, TData>).FullName}'类型!");
  469. }
  470.  
  471. _cellList.Add(uiCell);
  472. uiCell.Init(this, (TUiCellNode)_template.CloneUiCell(), _cellList.Count - 1);
  473. uiCell.SetEnable(true);
  474. return uiCell;
  475. }
  476.  
  477. //回收 cell
  478. private void ReclaimCellInstance(UiCell<TUiCellNode, TData> cell)
  479. {
  480. cell.SetEnable(false);
  481. GridContainer.RemoveChild(cell.CellNode.GetUiInstance());
  482. _cellPool.Push(cell);
  483. }
  484.  
  485. private void OnGridResized()
  486. {
  487. if (_autoColumns && GridContainer != null)
  488. {
  489. var width = GridContainer.Size.X;
  490. if (width <= _size.X + _cellOffset.X)
  491. {
  492. GridContainer.Columns = 1;
  493. }
  494. else
  495. {
  496. GridContainer.Columns = Mathf.FloorToInt(width / (_size.X + _cellOffset.X));
  497. }
  498. }
  499. }
  500.  
  501. /// <summary>
  502. /// 设置Ui布局方式是否横向扩展, 如果为 true, 则 GridContainer 的宽度会撑满父物体
  503. /// </summary>
  504. private static void SetHorizontalExpand(Control control, bool flag)
  505. {
  506. if (flag)
  507. {
  508. control.LayoutMode = 1;
  509. control.AnchorsPreset = (int)Control.LayoutPreset.TopWide;
  510. control.SizeFlagsHorizontal |= Control.SizeFlags.Expand;
  511. }
  512. else if ((control.SizeFlagsHorizontal & Control.SizeFlags.Expand) != 0)
  513. {
  514. control.LayoutMode = 1;
  515. control.AnchorsPreset = (int)Control.LayoutPreset.TopLeft;
  516. control.SizeFlagsHorizontal ^= Control.SizeFlags.Expand;
  517. }
  518. }
  519.  
  520. /// <summary>
  521. /// 获取Ui布局方式是否横向扩展
  522. /// </summary>
  523. private static bool GetHorizontalExpand(Control control)
  524. {
  525. return (control.SizeFlagsHorizontal & Control.SizeFlags.Expand) != 0;
  526. }
  527. }