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