Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / ui / tileSetEditorTerrain / up / TileEditTerrain.cs
@小李xl 小李xl on 24 Jan 2024 4 KB 代码调整
  1. using Godot;
  2.  
  3. namespace UI.TileSetEditorTerrain;
  4.  
  5. public partial class TileEditTerrain : EditorGridBg
  6. {
  7. public new TileSetEditorTerrain.TopBg UiNode => (TileSetEditorTerrain.TopBg)base.UiNode;
  8. private bool _dragMoveFlag = false;
  9. private TerrainCell _hoverCell = null;
  10. public override void SetUiNode(IUiNode uiNode)
  11. {
  12. base.SetUiNode(uiNode);
  13. var tileTexture = UiNode.L_TerrainRoot;
  14. InitNode(tileTexture.Instance, UiNode.L_Grid.Instance);
  15. var terrainBrush = tileTexture.L_Brush.Instance;
  16. terrainBrush.Root = tileTexture.Instance;
  17. terrainBrush.TerrainTextureList.Add(tileTexture.L_TerrainTexture1.Instance);
  18. terrainBrush.TerrainTextureList.Add(tileTexture.L_TerrainTexture2.Instance);
  19. terrainBrush.TerrainTextureList.Add(tileTexture.L_TerrainTexture3.Instance);
  20. terrainBrush.TerrainTextureList.Add(tileTexture.L_TerrainTexture4.Instance);
  21. //聚焦按钮点击
  22. UiNode.L_FocusBtn.Instance.Pressed += OnFocusClick;
  23. }
  24.  
  25. /// <summary>
  26. /// 改变TileSet纹理
  27. /// </summary>
  28. public void OnChangeTileSetTexture()
  29. {
  30. //UiNode.L_TileTexture.Instance.Size = UiNode.L_TileTexture.Instance.Texture.GetSize();
  31. OnFocusClick();
  32. }
  33.  
  34. public override void _Process(double delta)
  35. {
  36. //鼠标悬停的图块
  37. TerrainCell hover = null;
  38. var _panel = UiNode.UiPanel;
  39. var terrain = _panel.CurrTerrain;
  40. if (terrain != null && _panel.S_TopBg.Instance.IsMouseInRect())
  41. {
  42. if (terrain.TerrainType == 0) //选中47个Terrain
  43. {
  44. hover = CalcMouseHoverCell(_panel.S_TerrainTexture1.Instance, _panel.TerrainGrid3x3);
  45. if (_panel.EditorPanel.TileSetSourceIndex == 0 && _panel.CurrTerrainIndex == 0) //选中Main Source
  46. {
  47. if (hover == null)
  48. {
  49. hover = CalcMouseHoverCell(_panel.S_TerrainTexture2.Instance, _panel.TerrainGridMiddle);
  50. }
  51. if (hover == null)
  52. {
  53. hover = CalcMouseHoverCell(_panel.S_TerrainTexture3.Instance, _panel.TerrainGridFloor);
  54. }
  55. }
  56. }
  57. else //选中13格Terrain
  58. {
  59. hover = CalcMouseHoverCell(_panel.S_TerrainTexture4.Instance, _panel.TerrainGrid2x2);
  60. }
  61. }
  62.  
  63. if (hover != null && Input.IsActionJustPressed(InputAction.MouseRight))
  64. {
  65. //右键擦除
  66. hover.EraseCell();
  67. }
  68. else
  69. {
  70. SetHoverCell(hover);
  71. }
  72. }
  73.  
  74. /// <summary>
  75. /// 计算鼠标悬停在的地形单元。
  76. /// </summary>
  77. /// <param name="rectControl">矩形控件。</param>
  78. /// <param name="grid">网格。</param>
  79. /// <returns>鼠标悬停在的地形单元,如果鼠标不在矩形控件内或单元无效则返回null。</returns>
  80. private TerrainCell CalcMouseHoverCell(Control rectControl, UiGrid<TileSetEditorTerrain.RightCell, byte> grid)
  81. {
  82. if (rectControl.IsMouseInRect())
  83. {
  84. var cellPosition = Utils.GetMouseCellPosition(rectControl);
  85. var index = cellPosition.X + cellPosition.Y * grid.GetColumns();
  86. var tempCell = (TerrainCell)grid.GetCell(index);
  87. if (tempCell != null && tempCell.ConnectMaskCell != null)
  88. {
  89. return tempCell;
  90. }
  91. }
  92.  
  93. return null;
  94. }
  95. /// <summary>
  96. /// 设置鼠标悬停Cell
  97. /// </summary>
  98. public void SetHoverCell(TerrainCell cell)
  99. {
  100. if (cell != _hoverCell)
  101. {
  102. if (_hoverCell != null)
  103. {
  104. _hoverCell.Hover = false;
  105. }
  106.  
  107. if (cell != null)
  108. {
  109. cell.Hover = true;
  110. }
  111.  
  112. _hoverCell = cell;
  113. }
  114. }
  115.  
  116. //聚焦按钮点击
  117. private void OnFocusClick()
  118. {
  119. Vector2 rootSize;
  120. var panel = UiNode.UiPanel;
  121. if (panel.EditorPanel.TileSetSourceIndex == 0 && panel.CurrTerrainIndex == 0) //选中 Main Source
  122. {
  123. rootSize = UiNode.L_TerrainRoot.Instance.Size;
  124. }
  125. else if (panel.CurrTerrain != null)
  126. {
  127. if (panel.CurrTerrain.TerrainType == 0) //选中 47 格 Terrain
  128. {
  129. rootSize = UiNode.L_TerrainRoot.L_TerrainTexture1.Instance.Size;
  130. }
  131. else //13 格 Terrain
  132. {
  133. rootSize = UiNode.L_TerrainRoot.L_TerrainTexture4.Instance.Size;
  134. }
  135. }
  136. else
  137. {
  138. rootSize = Vector2.One;
  139. }
  140.  
  141. Utils.DoFocusNode(ContainerRoot, Size, rootSize);
  142. RefreshGridTrans();
  143. }
  144. }