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