Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / ui / tileSetEditorTerrain / down / TileEditArea.cs
@小李xl 小李xl on 24 Jan 2024 4 KB 代码调整
  1. using Godot;
  2.  
  3. namespace UI.TileSetEditorTerrain;
  4.  
  5. public partial class TileEditArea : EditorGridBg
  6. {
  7. public new TileSetEditorTerrain.BottomBg UiNode => (TileSetEditorTerrain.BottomBg)base.UiNode;
  8. private bool _dragMoveFlag = false;
  9. private MaskCell _hoverCell = null;
  10. public override void SetUiNode(IUiNode uiNode)
  11. {
  12. base.SetUiNode(uiNode);
  13. InitNode(UiNode.L_TileTexture.Instance, UiNode.L_Grid.Instance);
  14. UiNode.L_TileTexture.L_MaskBrush.Instance.Init(UiNode.L_TileTexture.Instance, UiNode.UiPanel.EditorPanel);
  15.  
  16. UiNode.L_TileTexture.Instance.Texture = UiNode.UiPanel.EditorPanel.Texture;
  17. //聚焦按钮点击
  18. UiNode.L_FocusBtn.Instance.Pressed += OnFocusClick;
  19. //拖拽Cell
  20. UiNode.Instance.AddDragListener(OnDrag);
  21. }
  22.  
  23. public override void _Process(double delta)
  24. {
  25. MaskCell cell = null;
  26. var _panel = UiNode.UiPanel;
  27. if (!_panel.MaskGrid.IsDestroyed && _panel.S_BottomBg.Instance.IsMouseInRect() && _panel.S_TileTexture.Instance.IsMouseInRect())
  28. {
  29. var cellPosition = Utils.GetMouseCellPosition(_panel.S_TileTexture.Instance);
  30. var index = cellPosition.X + cellPosition.Y * _panel.MaskGrid.GetColumns();
  31. var tempCell = (MaskCell)_panel.MaskGrid.GetCell(index);
  32. if (tempCell != null && tempCell.ConnectTerrainCell != null)
  33. {
  34. cell = tempCell;
  35. }
  36. }
  37. SetHoverCell(cell);
  38. }
  39.  
  40. /// <summary>
  41. /// 设置鼠标悬停Cell
  42. /// </summary>
  43. public void SetHoverCell(MaskCell cell)
  44. {
  45. if (cell != _hoverCell)
  46. {
  47. if (_hoverCell != null)
  48. {
  49. _hoverCell.Hover = false;
  50. }
  51.  
  52. if (cell != null)
  53. {
  54. cell.Hover = true;
  55. }
  56.  
  57. _hoverCell = cell;
  58. }
  59. }
  60.  
  61. //拖拽操作
  62. private void OnDrag(DragState state, Vector2 delta)
  63. {
  64. var _panel = UiNode.UiPanel;
  65. var sprite = _panel.S_DragSprite.Instance;
  66. if (state == DragState.DragStart) //拖拽开始
  67. {
  68. //这里要判断一下是否在BottomBg和TileTexture区域内
  69. if (_panel.S_BottomBg.Instance.IsMouseInRect() && _panel.S_TileTexture.Instance.IsMouseInRect())
  70. {
  71. var cellPosition = Utils.GetMouseCellPosition(_panel.S_TileTexture.Instance);
  72. var index = cellPosition.X + cellPosition.Y * _panel.MaskGrid.GetColumns();
  73. var cell = (MaskCell)_panel.MaskGrid.GetCell(index);
  74. if (cell != null && !cell.UseFlag) //必须要没有使用的Cell
  75. {
  76. _panel.DraggingCell = cell;
  77. _panel.MaskGrid.SelectIndex = index;
  78. _dragMoveFlag = false;
  79. }
  80. }
  81. }
  82. else if (state == DragState.DragEnd) //拖拽结束
  83. {
  84. if (_panel.DraggingCell != null)
  85. {
  86. if (_panel.S_TopBg.Instance.IsMouseInRect()) //找到放置的Cell
  87. {
  88. _panel.OnDropCell(_panel.DraggingCell);
  89. }
  90. sprite.Visible = false;
  91. _dragMoveFlag = false;
  92. _panel.DraggingCell = null;
  93. }
  94. }
  95. else if (_panel.DraggingCell != null) //拖拽移动
  96. {
  97. if (!_dragMoveFlag)
  98. {
  99. _dragMoveFlag = true;
  100. sprite.Texture = _panel.S_TileTexture.Instance.Texture;
  101. sprite.RegionRect = _panel.DraggingCell.Data;
  102. sprite.Scale = _panel.S_TopBg.L_TerrainRoot.Instance.Scale;
  103. sprite.Visible = true;
  104. sprite.GlobalPosition = sprite.GetGlobalMousePosition();
  105. }
  106. sprite.GlobalPosition = sprite.GetGlobalMousePosition();
  107. }
  108. }
  109.  
  110. /// <summary>
  111. /// 改变TileSet纹理
  112. /// </summary>
  113. public void OnChangeTileSetTexture()
  114. {
  115. UiNode.L_TileTexture.Instance.Size = UiNode.L_TileTexture.Instance.Texture.GetSize();
  116. OnFocusClick();
  117. }
  118. //聚焦按钮点击
  119. private void OnFocusClick()
  120. {
  121. var texture = UiNode.L_TileTexture.Instance.Texture;
  122. Utils.DoFocusNode(ContainerRoot, Size, texture != null ? texture.GetSize() : Vector2.Zero);
  123. RefreshGridTrans();
  124. }
  125. }