Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / ui / mapEditorTools / DoorDragButton.cs
@小李xl 小李xl on 19 Aug 2023 3 KB 创建房间标记, 开发中
  1. using System;
  2. using Godot;
  3. using UI.MapEditor;
  4.  
  5. namespace UI.MapEditorTools;
  6.  
  7. public partial class DoorDragButton : TextureButton
  8. {
  9. private static Vector2 _stepValue = new Vector2(GameConfig.TileCellSize, GameConfig.TileCellSize);
  10.  
  11. /// <summary>
  12. /// 拖拽当前物体的回调函数, 第一个参数为拖拽状态, 第二个参数为相对于初始点的拖拽偏移坐标
  13. /// </summary>
  14. public event Action<DragState, Vector2> DragEvent;
  15.  
  16. private DoorDragArea _parent;
  17. private bool _down;
  18. private Vector2 _startPos;
  19. private Vector2 _prevPos;
  20. private MapEditorToolsPanel _mapEditorToolsPanel;
  21. public override void _Ready()
  22. {
  23. _parent = GetParent<DoorDragArea>();
  24. ButtonDown += OnButtonDown;
  25. ButtonUp += OnButtonUp;
  26. }
  27.  
  28. public void SetMapEditorToolsPanel(MapEditorToolsPanel panel)
  29. {
  30. _mapEditorToolsPanel = panel;
  31. }
  32. public override void _Process(double delta)
  33. {
  34. if (_down)
  35. {
  36. if (DragEvent != null)
  37. {
  38. var value = (GetGlobalMousePosition() - _startPos) / _parent.Scale / _mapEditorToolsPanel.S_ToolRoot.Instance.Scale;
  39. var offset = Utils.Adsorption(value, _stepValue);
  40. //处理朝向问题
  41. if (_parent.Direction == DoorDirection.E)
  42. {
  43. offset = new Vector2(offset.Y, offset.X);
  44. }
  45. else if (_parent.Direction == DoorDirection.S)
  46. {
  47. offset = new Vector2(-offset.X, offset.Y);
  48. }
  49. else if (_parent.Direction == DoorDirection.W)
  50. {
  51. offset = new Vector2(offset.Y, -offset.X);
  52. }
  53. if (offset != _prevPos)
  54. {
  55. _prevPos = offset;
  56. DragEvent(DragState.DragMove, offset);
  57. }
  58. }
  59. }
  60. }
  61.  
  62. private void OnButtonDown()
  63. {
  64. if (_mapEditorToolsPanel.EditorMap.Instance.MouseType != EditorTileMap.MouseButtonType.Edit)
  65. {
  66. return;
  67. }
  68. if (_down)
  69. {
  70. return;
  71. }
  72. _down = true;
  73. Modulate = new Color(0.7f, 0.7f, 0.7f, 1);
  74. _startPos = GetGlobalMousePosition();
  75. _prevPos = new Vector2(-99999, -99999);
  76. if (DragEvent != null)
  77. {
  78. DragEvent(DragState.DragStart, Vector2.Zero);
  79. }
  80. }
  81.  
  82. private void OnButtonUp()
  83. {
  84. if (_mapEditorToolsPanel.EditorMap.Instance.MouseType != EditorTileMap.MouseButtonType.Edit)
  85. {
  86. return;
  87. }
  88. if (!_down)
  89. {
  90. return;
  91. }
  92. _down = false;
  93. Modulate = new Color(1, 1, 1, 1);
  94. if (DragEvent != null)
  95. {
  96. var value = (GetGlobalMousePosition() - _startPos) / _parent.Scale / _mapEditorToolsPanel.S_ToolRoot.Instance.Scale;
  97. var offset = Utils.Adsorption(value, _stepValue);
  98. _prevPos = offset;
  99. DragEvent(DragState.DragEnd, offset);
  100. }
  101. }
  102. }