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