Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / ui / tileSetEditorTerrain / TileSetEditorTerrainPanel.cs
@小李xl 小李xl on 9 Jan 2024 4 KB 存储和读取房间Tile数据
  1. using Godot;
  2. using UI.TileSetEditor;
  3.  
  4. namespace UI.TileSetEditorTerrain;
  5.  
  6. public partial class TileSetEditorTerrainPanel : TileSetEditorTerrain
  7. {
  8. /// <summary>
  9. /// 父Ui
  10. /// </summary>
  11. public TileSetEditorPanel EditorPanel;
  12. /// <summary>
  13. /// 是否正在拖拽图块
  14. /// </summary>
  15. public bool IsDraggingCell { get; set; }
  16.  
  17. private UiGrid<RightCell, byte> _topGrid1;
  18. private UiGrid<RightCell, byte> _topGrid2;
  19. private UiGrid<RightCell, byte> _topGrid3;
  20. private UiGrid<BottomCell, Rect2I> _bottomGrid;
  21. public override void OnCreateUi()
  22. {
  23. EditorPanel = (TileSetEditorPanel)ParentUi;
  24. //改变选中的TileSet资源
  25. AddEventListener(EventEnum.OnSelectTileSetSource, OnSelectTileSetSource);
  26. //改变纹理事件
  27. AddEventListener(EventEnum.OnSetTileTexture, OnSetTileTexture);
  28. //背景颜色改变
  29. AddEventListener(EventEnum.OnSetTileSetBgColor, OnChangeTileSetBgColor);
  30. _bottomGrid = CreateUiGrid<BottomCell, Rect2I, MaskCell>(S_BottomCell);
  31. _bottomGrid.SetCellOffset(Vector2I.Zero);
  32.  
  33. _topGrid1 = InitTopGrid(S_TerrainRoot.L_TerrainTexture1.Instance, GameConfig.TerrainBitSize1, 1);
  34. _topGrid2 = InitTopGrid(S_TerrainRoot.L_TerrainTexture2.Instance, GameConfig.TerrainBitSize2, 2);
  35. _topGrid3 = InitTopGrid(S_TerrainRoot.L_TerrainTexture3.Instance, GameConfig.TerrainBitSize3, 3);
  36.  
  37. OnSetTileTexture(EditorPanel.Texture);
  38. OnChangeTileSetBgColor(EditorPanel.BgColor);
  39. }
  40.  
  41. public override void OnDestroyUi()
  42. {
  43. }
  44.  
  45. public override void Process(float delta)
  46. {
  47. S_MaskBrush.Instance.Visible = !IsDraggingCell;
  48. }
  49.  
  50. private UiGrid<RightCell, byte> InitTopGrid(Control texture, Vector2I size, byte type)
  51. {
  52. var cellRoot = S_TopBg.L_TerrainRoot.L_CellRoot;
  53. var sRightCell = cellRoot.L_RightCell;
  54. sRightCell.Instance.Position = texture.Position;
  55. var grid = CreateUiGrid<RightCell, byte, TerrainCell>(sRightCell, cellRoot.Instance);
  56. grid.SetCellOffset(Vector2I.Zero);
  57. grid.SetColumns(size.X);
  58. for (var y = 0; y < size.Y; y++)
  59. {
  60. for (var x = 0; x < size.X; x++)
  61. {
  62. grid.Add(type);
  63. }
  64. }
  65. return grid;
  66. }
  67.  
  68. //改变选中的TileSet资源
  69. private void OnSelectTileSetSource(object obj)
  70. {
  71. //先清除所有绑定的Terrain
  72. _topGrid1.ForEach(cell => cell.CellNode.Instance.ClearCell());
  73. _topGrid2.ForEach(cell => cell.CellNode.Instance.ClearCell());
  74. _topGrid3.ForEach(cell => cell.CellNode.Instance.ClearCell());
  75. //再加载Terrain
  76. if (obj != null)
  77. {
  78. var terrain = ((TileSetSourceInfo)obj).Terrain;
  79. _topGrid1.ForEach(cell =>
  80. {
  81. var ints = terrain.GetTerrainCell( cell.Index, cell.Data);
  82. if (ints != null)
  83. {
  84. cell.CellNode.Instance.SetCell(new Rect2I(ints[0], ints[1], GameConfig.TileCellSize, GameConfig.TileCellSize));
  85. }
  86. });
  87. _topGrid2.ForEach(cell =>
  88. {
  89. var ints = terrain.GetTerrainCell(cell.Index, cell.Data);
  90. if (ints != null)
  91. {
  92. cell.CellNode.Instance.SetCell(new Rect2I(ints[0], ints[1], GameConfig.TileCellSize, GameConfig.TileCellSize));
  93. }
  94. });
  95. _topGrid3.ForEach(cell =>
  96. {
  97. var ints = terrain.GetTerrainCell( cell.Index, cell.Data);
  98. if (ints != null)
  99. {
  100. cell.CellNode.Instance.SetCell(new Rect2I(ints[0], ints[1], GameConfig.TileCellSize, GameConfig.TileCellSize));
  101. }
  102. });
  103. }
  104. }
  105. //改变TileSet纹理
  106. private void OnSetTileTexture(object arg)
  107. {
  108. S_BottomBg.Instance.OnChangeTileSetTexture();
  109.  
  110. _bottomGrid.RemoveAll();
  111. var cellHorizontal = EditorPanel.CellHorizontal;
  112. if (cellHorizontal <= 0)
  113. {
  114. return;
  115. }
  116. var cellVertical = EditorPanel.CellVertical;
  117. _bottomGrid.SetColumns(cellHorizontal);
  118. for (var y = 0; y < cellVertical; y++)
  119. {
  120. for (var x = 0; x < cellHorizontal; x++)
  121. {
  122. _bottomGrid.Add(new Rect2I(x * GameConfig.TileCellSize, y * GameConfig.TileCellSize, GameConfig.TileCellSize, GameConfig.TileCellSize));
  123. }
  124. }
  125. }
  126. //更改背景颜色
  127. private void OnChangeTileSetBgColor(object obj)
  128. {
  129. S_BottomBg.Instance.Color = EditorPanel.BgColor;
  130. S_TopBg.Instance.Color = EditorPanel.BgColor;
  131. }
  132. }