Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / ui / tileSetEditorTerrain / TileSetEditorTerrainPanel.cs
@小李xl 小李xl on 4 Jan 2024 2 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<LeftCell, Rect2I> _leftGrid;
  18. private UiGrid<RightCell, bool> _rightGrid;
  19. public override void OnCreateUi()
  20. {
  21. EditorPanel = (TileSetEditorPanel)ParentUi;
  22. //改变纹理事件
  23. AddEventListener(EventEnum.OnSetTileTexture, OnSetTileTexture);
  24. //背景颜色改变
  25. AddEventListener(EventEnum.OnSetTileSetBgColor, OnChangeTileSetBgColor);
  26. _leftGrid = CreateUiGrid<LeftCell, Rect2I, MaskCell>(S_LeftCell);
  27. _leftGrid.SetCellOffset(Vector2I.Zero);
  28.  
  29. var sRightCell = S_RightCell;
  30. var terrainSize = S_TerrainRoot.Instance.Size.AsVector2I();
  31. terrainSize = terrainSize / GameConfig.TileCellSize;
  32. Debug.Log("terrainSize: " + terrainSize);
  33. sRightCell.Instance.Position = Vector2.Zero;
  34. _rightGrid = CreateUiGrid<RightCell, bool, TerrainCell>(sRightCell);
  35. _rightGrid.SetCellOffset(Vector2I.Zero);
  36. _rightGrid.SetColumns(terrainSize.X);
  37. for (var y = 0; y < terrainSize.Y; y++)
  38. {
  39. for (var x = 0; x < terrainSize.X; x++)
  40. {
  41. _rightGrid.Add(false);
  42. }
  43. }
  44.  
  45. OnSetTileTexture(EditorPanel.Texture);
  46. OnChangeTileSetBgColor(EditorPanel.BgColor);
  47. }
  48.  
  49. public override void OnDestroyUi()
  50. {
  51. }
  52.  
  53. public override void Process(float delta)
  54. {
  55. S_MaskBrush.Instance.Visible = !IsDraggingCell;
  56. }
  57.  
  58. //改变TileSet纹理
  59. private void OnSetTileTexture(object arg)
  60. {
  61. S_LeftBg.Instance.OnChangeTileSetTexture();
  62.  
  63. _leftGrid.RemoveAll();
  64. var cellHorizontal = EditorPanel.CellHorizontal;
  65. if (cellHorizontal <= 0)
  66. {
  67. return;
  68. }
  69. var cellVertical = EditorPanel.CellVertical;
  70. _leftGrid.SetColumns(cellHorizontal);
  71. for (var y = 0; y < cellVertical; y++)
  72. {
  73. for (var x = 0; x < cellHorizontal; x++)
  74. {
  75. _leftGrid.Add(new Rect2I(x * GameConfig.TileCellSize, y * GameConfig.TileCellSize, GameConfig.TileCellSize, GameConfig.TileCellSize));
  76. }
  77. }
  78. }
  79. //更改背景颜色
  80. private void OnChangeTileSetBgColor(object obj)
  81. {
  82. S_LeftBg.Instance.Color = EditorPanel.BgColor;
  83. S_LeftBottomBg.Instance.Color = EditorPanel.BgColor;
  84. }
  85. }