Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / ui / tileSetEditorTerrain / TileSetEditorTerrainPanel.cs
@小李xl 小李xl on 5 Jan 2024 3 KB 继续开发TileSet地形编辑器
  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, bool> _topGrid1;
  18. private UiGrid<RightCell, bool> _topGrid2;
  19. private UiGrid<RightCell, bool> _topGrid3;
  20. private UiGrid<BottomCell, Rect2I> _bottomGrid;
  21. public override void OnCreateUi()
  22. {
  23. EditorPanel = (TileSetEditorPanel)ParentUi;
  24. //改变纹理事件
  25. AddEventListener(EventEnum.OnSetTileTexture, OnSetTileTexture);
  26. //背景颜色改变
  27. AddEventListener(EventEnum.OnSetTileSetBgColor, OnChangeTileSetBgColor);
  28. _bottomGrid = CreateUiGrid<BottomCell, Rect2I, MaskCell>(S_BottomCell);
  29. _bottomGrid.SetCellOffset(Vector2I.Zero);
  30.  
  31. _topGrid1 = InitTopGrid(S_TerrainRoot.L_TerrainTexture1.Instance);
  32. _topGrid2 = InitTopGrid(S_TerrainRoot.L_TerrainTexture2.Instance);
  33. _topGrid3 = InitTopGrid(S_TerrainRoot.L_TerrainTexture3.Instance);
  34.  
  35. OnSetTileTexture(EditorPanel.Texture);
  36. OnChangeTileSetBgColor(EditorPanel.BgColor);
  37. }
  38.  
  39. public override void OnDestroyUi()
  40. {
  41. }
  42.  
  43. public override void Process(float delta)
  44. {
  45. S_MaskBrush.Instance.Visible = !IsDraggingCell;
  46. }
  47.  
  48. private UiGrid<RightCell, bool> InitTopGrid(Control texture)
  49. {
  50. var cellRoot = S_TopBg.L_TerrainRoot.L_CellRoot;
  51. var sRightCell = cellRoot.L_RightCell;
  52. var terrainSize = texture.Size.AsVector2I();
  53. terrainSize = terrainSize / GameConfig.TileCellSize;
  54. sRightCell.Instance.Position = texture.Position;
  55. var grid = CreateUiGrid<RightCell, bool, TerrainCell>(sRightCell, cellRoot.Instance);
  56. grid.SetCellOffset(Vector2I.Zero);
  57. grid.SetColumns(terrainSize.X);
  58. for (var y = 0; y < terrainSize.Y; y++)
  59. {
  60. for (var x = 0; x < terrainSize.X; x++)
  61. {
  62. grid.Add(false);
  63. }
  64. }
  65. return grid;
  66. }
  67.  
  68. //改变TileSet纹理
  69. private void OnSetTileTexture(object arg)
  70. {
  71. S_BottomBg.Instance.OnChangeTileSetTexture();
  72.  
  73. _bottomGrid.RemoveAll();
  74. var cellHorizontal = EditorPanel.CellHorizontal;
  75. if (cellHorizontal <= 0)
  76. {
  77. return;
  78. }
  79. var cellVertical = EditorPanel.CellVertical;
  80. _bottomGrid.SetColumns(cellHorizontal);
  81. for (var y = 0; y < cellVertical; y++)
  82. {
  83. for (var x = 0; x < cellHorizontal; x++)
  84. {
  85. _bottomGrid.Add(new Rect2I(x * GameConfig.TileCellSize, y * GameConfig.TileCellSize, GameConfig.TileCellSize, GameConfig.TileCellSize));
  86. }
  87. }
  88. }
  89. //更改背景颜色
  90. private void OnChangeTileSetBgColor(object obj)
  91. {
  92. S_BottomBg.Instance.Color = EditorPanel.BgColor;
  93. S_TopBg.Instance.Color = EditorPanel.BgColor;
  94. }
  95. }