diff --git a/DungeonShooting_Godot/src/framework/common/DragBinder.cs b/DungeonShooting_Godot/src/framework/common/DragBinder.cs deleted file mode 100644 index 62d5897..0000000 --- a/DungeonShooting_Godot/src/framework/common/DragBinder.cs +++ /dev/null @@ -1,26 +0,0 @@ - -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/NodeExtend.cs b/DungeonShooting_Godot/src/framework/common/NodeExtend.cs index 2164423..b46b4b8 100644 --- a/DungeonShooting_Godot/src/framework/common/NodeExtend.cs +++ b/DungeonShooting_Godot/src/framework/common/NodeExtend.cs @@ -208,11 +208,11 @@ /// /// 给Ui节点添加拖拽事件 /// - /// 需要绑定拖拽的节点对象 + /// 需要绑定事件的节点对象 /// 拖拽回调函数 - public static DragBinder AddDragEventListener(this Control control, Action callback) + public static UiEventBinder AddDragListener(this Control control, Action callback) { - return AddDragEventListener(control, DragButtonEnum.Left, callback); + return AddDragListener(control, DragButtonEnum.Left, callback); } /// @@ -221,7 +221,7 @@ /// 需要绑定拖拽的节点对象 /// 可触发拖拽的按钮 /// 拖拽回调函数 - public static DragBinder AddDragEventListener(this Control control, DragButtonEnum triggerButton, Action callback) + public static UiEventBinder AddDragListener(this Control control, DragButtonEnum triggerButton, Action callback) { var dragFlag = false; Control.GuiInputEventHandler handler = (ev) => @@ -258,7 +258,7 @@ } }; control.GuiInput += handler; - return new DragBinder(control, handler); + return new UiEventBinder(control, handler); } private static bool CheckDragButton(MouseButton button, DragButtonEnum triggerButton) @@ -280,4 +280,31 @@ return (buttonEnum & triggerButton) != 0; } + + /// + /// 给Ui节点添加鼠标滚轮事件 + /// + /// 需要绑定事件的节点对象 + /// 滚轮回调, 参数 -1 表示滚轮向下滚动, 1 表示滚轮向上滚动 + public static UiEventBinder AddMouseWheelListener(this Control control, Action callback) + { + Control.GuiInputEventHandler handler = (ev) => + { + if (ev is InputEventMouseButton mouseButton) + { + if (mouseButton.ButtonIndex == MouseButton.WheelDown) + { + control.AcceptEvent(); + callback(-1); + } + else if (mouseButton.ButtonIndex == MouseButton.WheelUp) + { + control.AcceptEvent(); + callback(1); + } + } + }; + control.GuiInput += handler; + return new UiEventBinder(control, handler); + } } \ No newline at end of file diff --git a/DungeonShooting_Godot/src/framework/common/UiEventBinder.cs b/DungeonShooting_Godot/src/framework/common/UiEventBinder.cs new file mode 100644 index 0000000..37ff894 --- /dev/null +++ b/DungeonShooting_Godot/src/framework/common/UiEventBinder.cs @@ -0,0 +1,26 @@ + +using Godot; + +/// +/// Ui事件绑定数据对象 +/// +public class UiEventBinder +{ + + private Control Control; + private Control.GuiInputEventHandler Callback; + + public UiEventBinder(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/Utils.cs b/DungeonShooting_Godot/src/framework/common/Utils.cs index 8f35758..1cc292d 100644 --- a/DungeonShooting_Godot/src/framework/common/Utils.cs +++ b/DungeonShooting_Godot/src/framework/common/Utils.cs @@ -409,4 +409,41 @@ ); } + /// + /// 根据鼠标位置执行单步放大逻辑 + /// + public static bool DoMagnifyByMousePosition(Control control, float maxXScale) + { + var offset = control.GetLocalMousePosition(); + var prevScale = control.Scale; + var newScale = prevScale * 1.1f; + if (newScale.X <= maxXScale) + { + control.Scale = newScale; + var position = control.Position - offset * 0.1f * prevScale; + control.Position = position; + return true; + } + + return false; + } + + /// + /// 根据鼠标位置执行单步缩小逻辑 + /// + public static bool DoShrinkByMousePosition(Control control, float minXScale) + { + var offset = control.GetLocalMousePosition(); + var prevScale = control.Scale; + var newScale = prevScale / 1.1f; + if (newScale.X >= minXScale) + { + control.Scale = newScale; + var position = control.Position + offset * 0.1f * newScale; + control.Position = position; + 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 78699fc..48e3f66 100644 --- a/DungeonShooting_Godot/src/game/ui/editorImportCombination/EditorImportCombinationPanel.cs +++ b/DungeonShooting_Godot/src/game/ui/editorImportCombination/EditorImportCombinationPanel.cs @@ -6,7 +6,7 @@ { public override void OnShowUi() { - S_PreviewBg.Instance.AddDragEventListener(DragButtonEnum.Left | DragButtonEnum.Right | DragButtonEnum.Middle, OnDragPreview); + S_PreviewBg.Instance.AddDragListener(DragButtonEnum.Left | DragButtonEnum.Right | DragButtonEnum.Middle, OnDragPreview); } private void OnDragPreview(DragState state, Vector2 delta) diff --git a/DungeonShooting_Godot/src/game/ui/roomMap/RoomMapPanel.cs b/DungeonShooting_Godot/src/game/ui/roomMap/RoomMapPanel.cs index da0f8e8..3839c26 100644 --- a/DungeonShooting_Godot/src/game/ui/roomMap/RoomMapPanel.cs +++ b/DungeonShooting_Godot/src/game/ui/roomMap/RoomMapPanel.cs @@ -19,7 +19,7 @@ //是否放大地图 private bool _isMagnifyMap = false; - private DragBinder _dragBinder; + private UiEventBinder _dragBinder; //放大地图后拖拽的偏移 private Vector2 _mapOffset; //放大地图后鼠标悬停的房间 @@ -187,7 +187,7 @@ S_MapBar.Instance.Visible = false; _mapOffset = Vector2.Zero; - _dragBinder = S_DrawContainer.Instance.AddDragEventListener((state, delta) => + _dragBinder = S_DrawContainer.Instance.AddDragListener((state, delta) => { if (state == DragState.DragMove) { diff --git a/DungeonShooting_Godot/src/game/ui/tileSetEditorCombination/GridBg.cs b/DungeonShooting_Godot/src/game/ui/tileSetEditorCombination/GridBg.cs index e3ad7b5..0cfa685 100644 --- a/DungeonShooting_Godot/src/game/ui/tileSetEditorCombination/GridBg.cs +++ b/DungeonShooting_Godot/src/game/ui/tileSetEditorCombination/GridBg.cs @@ -11,7 +11,7 @@ public virtual void SetUiNode(IUiNode uiNode) { UiNode = (T)uiNode; - this.AddDragEventListener(DragButtonEnum.Middle, OnDrag); + this.AddDragListener(DragButtonEnum.Middle, OnDrag); Resized += RefreshGridTrans; } @@ -49,29 +49,17 @@ //缩小 private void Shrink() { - var offset = ContainerRoot.GetLocalMousePosition(); - var prevScale = ContainerRoot.Scale; - var newScale = prevScale / 1.1f; - if (newScale.LengthSquared() >= 0.5f) + if (Utils.DoShrinkByMousePosition(ContainerRoot, 0.2f)) { - ContainerRoot.Scale = newScale; - var position = ContainerRoot.Position + offset * 0.1f * newScale; - ContainerRoot.Position = position; - SetGridTransform(position, newScale.X); + SetGridTransform(ContainerRoot.Position, ContainerRoot.Scale.X); } } //放大 private void Magnify() { - var offset = ContainerRoot.GetLocalMousePosition(); - var prevScale = ContainerRoot.Scale; - var newScale = prevScale * 1.1f; - if (newScale.LengthSquared() <= 2000) + if (Utils.DoMagnifyByMousePosition(ContainerRoot, 20)) { - ContainerRoot.Scale = newScale; - var position = ContainerRoot.Position - offset * 0.1f * prevScale; - ContainerRoot.Position = position; - SetGridTransform(position, newScale.X); + SetGridTransform(ContainerRoot.Position, ContainerRoot.Scale.X); } } diff --git a/DungeonShooting_Godot/src/game/ui/tileSetEditorImport/TileSetEditorImportPanel.cs b/DungeonShooting_Godot/src/game/ui/tileSetEditorImport/TileSetEditorImportPanel.cs index c99cb3d..35652b8 100644 --- a/DungeonShooting_Godot/src/game/ui/tileSetEditorImport/TileSetEditorImportPanel.cs +++ b/DungeonShooting_Godot/src/game/ui/tileSetEditorImport/TileSetEditorImportPanel.cs @@ -15,7 +15,6 @@ _tileSetEditor = (TileSetEditor.TileSetEditorPanel)ParentUi; _tileSetEditor.SetBgColor(S_ImportPreviewBg.Instance.Color); S_ImportPreview.Instance.Texture = _tileSetEditor.Texture; - S_ImportPreviewBg.Instance.AddDragEventListener(DragButtonEnum.Left | DragButtonEnum.Middle | DragButtonEnum.Right, OnDragCallback); GetTree().Root.FilesDropped += OnFilesDropped; S_ImportButton.Instance.Pressed += OnImportButtonClick; @@ -30,7 +29,10 @@ AddEventListener(EventEnum.OnSetTileTexture, OnSetTileTexture); //监听TileSet背景颜色改变 AddEventListener(EventEnum.OnSetTileSetBgColor, OnSetTileSetBgColor); - + //监听拖拽 + S_ImportPreviewBg.Instance.AddDragListener(DragButtonEnum.Left | DragButtonEnum.Middle | DragButtonEnum.Right, OnDragCallback); + //监听鼠标滚轮 + S_ImportPreviewBg.Instance.AddMouseWheelListener(OnMouseCallback); } public override void OnDestroyUi() @@ -64,33 +66,6 @@ } } - 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) - { - //缩小 - 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) - { - //放大 - var scale = textureRect.Scale; - scale = new Vector2(Mathf.Min(20f, scale.X * 1.1f), Mathf.Min(20f, scale.Y * 1.1f)); - textureRect.Scale = scale; - } - } - } - } - //点击导入按钮 private void OnImportButtonClick() { @@ -144,6 +119,30 @@ } } + //鼠标滚轮 + private void OnMouseCallback(int v) + { + if (!_tileSetEditor.InitTexture) + { + return; + } + var textureRect = S_Control.L_ImportPreview.Instance; + if (v < 0) + { + //缩小 + 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 + { + //放大 + var scale = textureRect.Scale; + scale = new Vector2(Mathf.Min(20f, scale.X * 1.1f), Mathf.Min(20f, scale.Y * 1.1f)); + textureRect.Scale = scale; + } + } + //拖拽文件进入区域 private void OnFilesDropped(string[] files) {