diff --git a/DungeonShooting_Godot/src/framework/common/DragBinder.cs b/DungeonShooting_Godot/src/framework/common/DragBinder.cs new file mode 100644 index 0000000..62d5897 --- /dev/null +++ b/DungeonShooting_Godot/src/framework/common/DragBinder.cs @@ -0,0 +1,26 @@ + +using Godot; + +/// +/// 拖拽绑定数据对象, 通过 NodeExtend.AddDragEventListener 创建 +/// +public class DragBinder +{ + + private Control Control; + private Control.GuiInputEventHandler Callback; + + public DragBinder(Control control, Control.GuiInputEventHandler callback) + { + Control = control; + Callback = callback; + } + + /// + /// 解除绑定事件 + /// + public void UnBind() + { + Control.GuiInput -= Callback; + } +} \ No newline at end of file diff --git a/DungeonShooting_Godot/src/framework/common/DragButtonEnum.cs b/DungeonShooting_Godot/src/framework/common/DragButtonEnum.cs new file mode 100644 index 0000000..329b5e1 --- /dev/null +++ b/DungeonShooting_Godot/src/framework/common/DragButtonEnum.cs @@ -0,0 +1,22 @@ + +using System; + +/// +/// 用于拖拽的鼠标键位 +/// +[Flags] +public enum DragButtonEnum +{ + /// + /// 鼠标左键 + /// + Left = 0b1, + /// + /// 鼠标右键 + /// + Right = 0b10, + /// + /// 鼠标中键 + /// + Middle = 0b100, +} \ No newline at end of file diff --git a/DungeonShooting_Godot/src/framework/common/DragState.cs b/DungeonShooting_Godot/src/framework/common/DragState.cs new file mode 100644 index 0000000..04f4fb0 --- /dev/null +++ b/DungeonShooting_Godot/src/framework/common/DragState.cs @@ -0,0 +1,19 @@ + +/// +/// 拖拽状态 +/// +public enum DragState +{ + /// + /// 拖拽开始 + /// + DragStart, + /// + /// 拖拽中 + /// + DragMove, + /// + /// 拖拽结束 + /// + DragEnd +} \ No newline at end of file diff --git a/DungeonShooting_Godot/src/framework/common/NodeExtend.cs b/DungeonShooting_Godot/src/framework/common/NodeExtend.cs index fcd6c36..2164423 100644 --- a/DungeonShooting_Godot/src/framework/common/NodeExtend.cs +++ b/DungeonShooting_Godot/src/framework/common/NodeExtend.cs @@ -204,4 +204,80 @@ yield return new WaitForSeconds(delayTime); cb(arg1,arg2, arg3); } + + /// + /// 给Ui节点添加拖拽事件 + /// + /// 需要绑定拖拽的节点对象 + /// 拖拽回调函数 + public static DragBinder AddDragEventListener(this Control control, Action callback) + { + return AddDragEventListener(control, DragButtonEnum.Left, callback); + } + + /// + /// 给Ui节点添加拖拽事件 + /// + /// 需要绑定拖拽的节点对象 + /// 可触发拖拽的按钮 + /// 拖拽回调函数 + public static DragBinder AddDragEventListener(this Control control, DragButtonEnum triggerButton, Action callback) + { + var dragFlag = false; + Control.GuiInputEventHandler handler = (ev) => + { + if (!dragFlag) //未开始拖拽 + { + if (ev is InputEventMouseButton mouseButton && mouseButton.Pressed && + CheckDragButton(mouseButton.ButtonIndex, triggerButton)) //按下按钮 + { + control.AcceptEvent(); + dragFlag = true; + callback(DragState.DragStart, Vector2.Zero); + } + } + else //拖拽中 + { + if (ev is InputEventMouseButton mouseButton) + { + if (!mouseButton.Pressed && CheckDragButton(mouseButton.ButtonIndex, triggerButton)) //松开按钮 + { + control.AcceptEvent(); + dragFlag = false; + callback(DragState.DragEnd, Vector2.Zero); + } + } else if (ev is InputEventMouseMotion mouseMotion) //拖拽中 + { + control.AcceptEvent(); + var delta = mouseMotion.Relative; + if (delta != Vector2.Zero) + { + callback(DragState.DragMove, mouseMotion.Relative); + } + } + } + }; + control.GuiInput += handler; + return new DragBinder(control, handler); + } + + private static bool CheckDragButton(MouseButton button, DragButtonEnum triggerButton) + { + DragButtonEnum buttonEnum; + switch (button) + { + case MouseButton.Left: + buttonEnum = DragButtonEnum.Left; + break; + case MouseButton.Right: + buttonEnum = DragButtonEnum.Right; + break; + case MouseButton.Middle: + buttonEnum = DragButtonEnum.Middle; + break; + default: return false; + } + + return (buttonEnum & triggerButton) != 0; + } } \ No newline at end of file diff --git a/DungeonShooting_Godot/src/game/GameApplication.cs b/DungeonShooting_Godot/src/game/GameApplication.cs index a67cfc6..276b27d 100644 --- a/DungeonShooting_Godot/src/game/GameApplication.cs +++ b/DungeonShooting_Godot/src/game/GameApplication.cs @@ -141,7 +141,6 @@ var newDelta = (float)delta; InputManager.Update(newDelta); SoundManager.Update(newDelta); - UiDragManager.Update(newDelta); //协程更新 ProxyCoroutineHandler.ProxyUpdateCoroutine(ref _coroutineList, newDelta); diff --git a/DungeonShooting_Godot/src/game/data/DragBinder.cs b/DungeonShooting_Godot/src/game/data/DragBinder.cs deleted file mode 100644 index effb33c..0000000 --- a/DungeonShooting_Godot/src/game/data/DragBinder.cs +++ /dev/null @@ -1,31 +0,0 @@ - -using System; -using Godot; - -/// -/// 拖拽绑定数据对象, 通过 DragUiManager 创建 -/// -public class DragBinder -{ - public Control Control; - public bool MouseEntered; - public bool Dragging; - public Vector2 PrevPosition; - public Action Callback; - public StringName[] InputAction; - - public void OnMouseEntered() - { - MouseEntered = true; - } - - public void OnMouseExited() - { - MouseEntered = false; - } - - public void UnBind() - { - UiDragManager.UnBindDrag(this); - } -} \ No newline at end of file diff --git a/DungeonShooting_Godot/src/game/data/DragState.cs b/DungeonShooting_Godot/src/game/data/DragState.cs deleted file mode 100644 index 04f4fb0..0000000 --- a/DungeonShooting_Godot/src/game/data/DragState.cs +++ /dev/null @@ -1,19 +0,0 @@ - -/// -/// 拖拽状态 -/// -public enum DragState -{ - /// - /// 拖拽开始 - /// - DragStart, - /// - /// 拖拽中 - /// - DragMove, - /// - /// 拖拽结束 - /// - DragEnd -} \ No newline at end of file diff --git a/DungeonShooting_Godot/src/game/manager/UiDragManager.cs b/DungeonShooting_Godot/src/game/manager/UiDragManager.cs deleted file mode 100644 index 07348d9..0000000 --- a/DungeonShooting_Godot/src/game/manager/UiDragManager.cs +++ /dev/null @@ -1,128 +0,0 @@ -using System; -using System.Collections.Generic; -using Godot; - -public static class UiDragManager -{ - - - private static readonly List _list = new List(); - private static readonly List _removeList = new List(); - private static readonly List _addList = new List(); - - /// - /// 绑定拖拽事件 - /// - public static DragBinder BindDrag(Control control, StringName[] inputAction, Action callback) - { - var binder = new DragBinder(); - binder.Control = control; - control.MouseEntered += binder.OnMouseEntered; - control.MouseExited += binder.OnMouseExited; - binder.Callback = callback; - binder.InputAction = inputAction; - _addList.Add(binder); - return binder; - } - - /// - /// 绑定拖拽事件 - /// - public static DragBinder BindDrag(Control control, Action callback) - { - return BindDrag(control, new [] { InputAction.MouseLeft }, callback); - } - - /// - /// 解绑拖拽事件 - /// - public static void UnBindDrag(DragBinder binder) - { - if (!_removeList.Contains(binder)) - { - _removeList.Add(binder); - } - } - - public static void Update(float delta) - { - //更新拖拽位置 - if (_list.Count > 0) - { - foreach (var dragBinder in _list) - { - if (dragBinder.Dragging && !CheckActionPressed(dragBinder.InputAction)) //松开鼠标, 结束拖拽 - { - dragBinder.Dragging = false; - dragBinder.Callback(DragState.DragEnd, Vector2.Zero); - } - else if (!dragBinder.Dragging) //开始拖拽 - { - if (dragBinder.MouseEntered && ActionJustPressed(dragBinder.InputAction)) - { - dragBinder.Dragging = true; - dragBinder.PrevPosition = dragBinder.Control.GetGlobalMousePosition(); - dragBinder.Callback(DragState.DragStart, Vector2.Zero); - } - } - else //拖拽更新 - { - var mousePos = dragBinder.Control.GetGlobalMousePosition(); - if (mousePos != dragBinder.PrevPosition) - { - var deltaPosition = mousePos - dragBinder.PrevPosition; - dragBinder.PrevPosition = mousePos; - dragBinder.Callback(DragState.DragMove, deltaPosition); - } - } - } - } - - //移除绑定 - if (_removeList.Count > 0) - { - foreach (var binder in _removeList) - { - if (_list.Remove(binder) && GodotObject.IsInstanceValid(binder.Control)) - { - binder.Control.MouseEntered -= binder.OnMouseEntered; - binder.Control.MouseExited -= binder.OnMouseExited; - } - } - _removeList.Clear(); - } - - //添加绑定 - if (_addList.Count > 0) - { - _list.AddRange(_addList); - _addList.Clear(); - } - } - - private static bool CheckActionPressed(StringName[] array) - { - foreach (var stringName in array) - { - if (Input.IsActionPressed(stringName)) - { - return true; - } - } - - return false; - } - - private static bool ActionJustPressed(StringName[] array) - { - foreach (var stringName in array) - { - if (Input.IsActionJustPressed(stringName)) - { - return true; - } - } - - return false; - } -} \ No newline at end of file diff --git a/DungeonShooting_Godot/src/game/ui/editorImportCombination/EditorImportCombinationPanel.cs b/DungeonShooting_Godot/src/game/ui/editorImportCombination/EditorImportCombinationPanel.cs index cc88bed..78699fc 100644 --- a/DungeonShooting_Godot/src/game/ui/editorImportCombination/EditorImportCombinationPanel.cs +++ b/DungeonShooting_Godot/src/game/ui/editorImportCombination/EditorImportCombinationPanel.cs @@ -4,23 +4,19 @@ public partial class EditorImportCombinationPanel : EditorImportCombination { - private DragBinder _dragBinder; public override void OnShowUi() { - _dragBinder = UiDragManager.BindDrag(S_PreviewBg.Instance, (state, delta) => - { - if (state == DragState.DragMove) - { - S_PreviewTexture.Instance.Position += delta; - } - }); + S_PreviewBg.Instance.AddDragEventListener(DragButtonEnum.Left | DragButtonEnum.Right | DragButtonEnum.Middle, OnDragPreview); } - public override void OnDestroyUi() + private void OnDragPreview(DragState state, Vector2 delta) { - _dragBinder.UnBind(); + if (state == DragState.DragMove) + { + S_PreviewTexture.Instance.Position += delta; + } } - + /// /// 初始化页面数据 /// diff --git a/DungeonShooting_Godot/src/game/ui/roomMap/RoomMapPanel.cs b/DungeonShooting_Godot/src/game/ui/roomMap/RoomMapPanel.cs index d8d0e5a..da0f8e8 100644 --- a/DungeonShooting_Godot/src/game/ui/roomMap/RoomMapPanel.cs +++ b/DungeonShooting_Godot/src/game/ui/roomMap/RoomMapPanel.cs @@ -45,11 +45,6 @@ public override void OnDestroyUi() { _factory.RemoveAllEventListener(); - - if (_dragBinder != null) - { - _dragBinder.UnBind(); - } if (_transmissionTween != null) { @@ -191,8 +186,8 @@ S_MagnifyMapBar.Instance.Visible = true; S_MapBar.Instance.Visible = false; _mapOffset = Vector2.Zero; - - _dragBinder = UiDragManager.BindDrag(S_DrawContainer.Instance, (state, delta) => + + _dragBinder = S_DrawContainer.Instance.AddDragEventListener((state, delta) => { if (state == DragState.DragMove) { @@ -215,6 +210,7 @@ if (_dragBinder != null) { _dragBinder.UnBind(); + _dragBinder = null; } } diff --git a/DungeonShooting_Godot/src/game/ui/tileSetEditorCombination/GridBg.cs b/DungeonShooting_Godot/src/game/ui/tileSetEditorCombination/GridBg.cs index 26a4769..e3ad7b5 100644 --- a/DungeonShooting_Godot/src/game/ui/tileSetEditorCombination/GridBg.cs +++ b/DungeonShooting_Godot/src/game/ui/tileSetEditorCombination/GridBg.cs @@ -8,18 +8,16 @@ public Control ContainerRoot { get; protected set; } public T UiNode { get; private set; } - private DragBinder _dragBinder; - public virtual void SetUiNode(IUiNode uiNode) { UiNode = (T)uiNode; - _dragBinder = UiDragManager.BindDrag(this, new[] { InputAction.mouseMiddle }, OnDrag); + this.AddDragEventListener(DragButtonEnum.Middle, OnDrag); Resized += RefreshGridTrans; } public virtual void OnDestroy() { - _dragBinder.UnBind(); + } /// diff --git a/DungeonShooting_Godot/src/game/ui/tileSetEditorImport/TileSetEditorImportPanel.cs b/DungeonShooting_Godot/src/game/ui/tileSetEditorImport/TileSetEditorImportPanel.cs index cba59e7..c99cb3d 100644 --- a/DungeonShooting_Godot/src/game/ui/tileSetEditorImport/TileSetEditorImportPanel.cs +++ b/DungeonShooting_Godot/src/game/ui/tileSetEditorImport/TileSetEditorImportPanel.cs @@ -5,7 +5,6 @@ public partial class TileSetEditorImportPanel : TileSetEditorImport { - private DragBinder _dragBinder; //是否打开了颜色选择器 private bool _isOpenColorPicker; @@ -16,8 +15,7 @@ _tileSetEditor = (TileSetEditor.TileSetEditorPanel)ParentUi; _tileSetEditor.SetBgColor(S_ImportPreviewBg.Instance.Color); S_ImportPreview.Instance.Texture = _tileSetEditor.Texture; - - _dragBinder = UiDragManager.BindDrag(S_ImportPreviewBg.Instance, OnDragCallback); + S_ImportPreviewBg.Instance.AddDragEventListener(DragButtonEnum.Left | DragButtonEnum.Middle | DragButtonEnum.Right, OnDragCallback); GetTree().Root.FilesDropped += OnFilesDropped; S_ImportButton.Instance.Pressed += OnImportButtonClick; @@ -32,12 +30,12 @@ AddEventListener(EventEnum.OnSetTileTexture, OnSetTileTexture); //监听TileSet背景颜色改变 AddEventListener(EventEnum.OnSetTileSetBgColor, OnSetTileSetBgColor); + } public override void OnDestroyUi() { GetTree().Root.FilesDropped -= OnFilesDropped; - _dragBinder.UnBind(); } //TileSet纹理改变 @@ -66,32 +64,28 @@ } } - public override void _Input(InputEvent @event) + private static int index = 0; + public override void _GuiInput(InputEvent @event) { if (@event is InputEventMouseButton mouseButton) { + AcceptEvent(); var textureRect = S_Control.L_ImportPreview.Instance; if (textureRect.Visible) { if (mouseButton.ButtonIndex == MouseButton.WheelDown) { - if (GetGlobalRect().HasPoint(mouseButton.GlobalPosition)) - { - //缩小 - var scale = textureRect.Scale; - scale = new Vector2(Mathf.Max(0.1f, scale.X / 1.1f), Mathf.Max(0.1f, scale.Y / 1.1f)); - textureRect.Scale = scale; - } + //缩小 + var scale = textureRect.Scale; + scale = new Vector2(Mathf.Max(0.1f, scale.X / 1.1f), Mathf.Max(0.1f, scale.Y / 1.1f)); + textureRect.Scale = scale; } else if (mouseButton.ButtonIndex == MouseButton.WheelUp) { - if (GetGlobalRect().HasPoint(mouseButton.GlobalPosition)) - { - //放大 - var scale = textureRect.Scale; - scale = new Vector2(Mathf.Min(20f, scale.X * 1.1f), Mathf.Min(20f, scale.Y * 1.1f)); - textureRect.Scale = scale; - } + //放大 + var scale = textureRect.Scale; + scale = new Vector2(Mathf.Min(20f, scale.X * 1.1f), Mathf.Min(20f, scale.Y * 1.1f)); + textureRect.Scale = scale; } } }