Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / ui / tileSetEditorCombination / right / TileSelected.cs
@小李xl 小李xl on 11 Mar 2024 2 KB 制作图鉴
  1. using Godot;
  2.  
  3. namespace UI.TileSetEditorCombination;
  4.  
  5. public partial class TileSelected : VBoxContainer, IUiNodeScript
  6. {
  7. /// <summary>
  8. /// 存放所有组合数据
  9. /// </summary>
  10. public UiGrid<TileSetEditorCombination.CellButton, ImportCombinationData> Grid { get; private set; }
  11. private TileSetEditorCombination.RightBg _rightBg;
  12. public void SetUiNode(IUiNode uiNode)
  13. {
  14. _rightBg = (TileSetEditorCombination.RightBg)uiNode;
  15.  
  16. Grid = new UiGrid<TileSetEditorCombination.CellButton, ImportCombinationData>(_rightBg.L_ScrollContainer.L_CellButton, typeof(TileCell));
  17. Grid.SetCellOffset(new Vector2I(5, 5));
  18. Grid.SetAutoColumns(true);
  19. Grid.SetHorizontalExpand(true);
  20. _rightBg.UiPanel.AddEventListener(EventEnum.OnImportCombination, OnImportCombination);
  21. _rightBg.UiPanel.AddEventListener(EventEnum.OnRemoveCombination, OnRemoveCombination);
  22. _rightBg.UiPanel.AddEventListener(EventEnum.OnUpdateCombination, OnUpdateCombination);
  23. }
  24. /// <summary>
  25. /// 导入组合图块
  26. /// </summary>
  27. private void OnImportCombination(object obj)
  28. {
  29. if (obj is ImportCombinationData data)
  30. {
  31. _rightBg.UiPanel.EditorPanel.TileSetSourceInfo.Combination.Add(data.CombinationInfo);
  32. Grid.Add(data);
  33. EventManager.EmitEvent(EventEnum.OnTileSetDirty);
  34. }
  35. }
  36. /// <summary>
  37. /// 移除组合图块
  38. /// </summary>
  39. private void OnRemoveCombination(object obj)
  40. {
  41. if (obj is ImportCombinationData data)
  42. {
  43. var uiCell = Grid.Find(c => c.Data.CombinationInfo.Id == data.CombinationInfo.Id);
  44. _rightBg.UiPanel.EditorPanel.TileSetSourceInfo.Combination.Remove(data.CombinationInfo);
  45. if (uiCell != null)
  46. {
  47. Grid.RemoveByIndex(uiCell.Index);
  48. }
  49. EventManager.EmitEvent(EventEnum.OnTileSetDirty);
  50. }
  51. }
  52.  
  53. /// <summary>
  54. /// 修改组合图块
  55. /// </summary>
  56. private void OnUpdateCombination(object obj)
  57. {
  58. if (obj is ImportCombinationData data)
  59. {
  60. var uiCell = Grid.Find(c => c.Data.CombinationInfo.Id == data.CombinationInfo.Id);
  61. if (uiCell != null)
  62. {
  63. uiCell.UpdateData(data);
  64. }
  65. EventManager.EmitEvent(EventEnum.OnTileSetDirty);
  66. }
  67. }
  68.  
  69. public void OnDestroy()
  70. {
  71. Grid.Destroy();
  72. }
  73.  
  74. /// <summary>
  75. /// 改变TileSet纹理
  76. /// </summary>
  77. public void OnChangeTileSetTexture()
  78. {
  79. //_grid.RemoveAll();
  80. //刷新预览图
  81. Grid.ForEach(cell =>
  82. {
  83. cell.Data.UpdatePreviewTexture(_rightBg.UiPanel.EditorPanel.TextureImage);
  84. });
  85. }
  86. }