From e5c0964c932343b6a6141d45dd17ac4dc74f8571 Mon Sep 17 00:00:00 2001
From: GwWuYou <95328647+GeWuYou@users.noreply.github.com>
Date: Fri, 12 Dec 2025 21:23:40 +0800
Subject: [PATCH] =?UTF-8?q?refactor(component):=20=E9=87=8D=E5=91=BD?=
=?UTF-8?q?=E5=90=8D=E6=8B=96=E6=8B=BD=E7=BB=84=E4=BB=B6=E5=B9=B6=E4=BC=98?=
=?UTF-8?q?=E5=8C=96=E8=BE=93=E5=85=A5=E5=A4=84=E7=90=86=E9=80=BB=E8=BE=91?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
- 将 DragDropComponent 类重命名为 AbstractDragDropComponent
- 更新类注释以反映抽象性质
- 重构 _Input 方法中的条件判断为 switch 语句
- 在 OnTargetInputEvent 中移除重复的放置逻辑
- 改进代码注释以更准确描述功能
- 保留原有拖拽核心功能与信号机制
---
...ponent.cs => AbstractDragDropComponent.cs} | 81 ++++++++++---------
1 file changed, 43 insertions(+), 38 deletions(-)
rename GFramework.Core.Godot/component/{DragDropComponent.cs => AbstractDragDropComponent.cs} (71%)
diff --git a/GFramework.Core.Godot/component/DragDropComponent.cs b/GFramework.Core.Godot/component/AbstractDragDropComponent.cs
similarity index 71%
rename from GFramework.Core.Godot/component/DragDropComponent.cs
rename to GFramework.Core.Godot/component/AbstractDragDropComponent.cs
index c637820..b839657 100644
--- a/GFramework.Core.Godot/component/DragDropComponent.cs
+++ b/GFramework.Core.Godot/component/AbstractDragDropComponent.cs
@@ -8,35 +8,35 @@ using Godot;
namespace GFramework.Core.Godot.component;
///
-/// 拖拽组件类,用于处理节点的拖放逻辑。
+/// 抽象拖拽组件类,用于处理节点的拖放逻辑。
/// 实现了 IController 接口以支持架构通信,并通过信号通知拖拽事件的发生。
///
-public abstract partial class DragDropComponent : Node, IController
+public abstract partial class AbstractDragDropComponent : Node, IController
{
- ///
- /// 当拖拽被取消时触发的信号。
- ///
- /// 拖拽起始位置。
- [Signal]
+ ///
+ /// 当拖拽被取消时触发的信号。
+ ///
+ /// 拖拽起始位置。
+ [Signal]
public delegate void DragCanceledEventHandler(Vector2 startingPosition);
- ///
- /// 当拖拽开始时触发的信号。
- ///
- [Signal]
+ ///
+ /// 当拖拽开始时触发的信号。
+ ///
+ [Signal]
public delegate void DragStartedEventHandler();
- ///
- /// 当拖拽结束并放置时触发的信号。
- ///
- /// 拖拽起始位置。
- [Signal]
+ ///
+ /// 当拖拽结束并放置时触发的信号。
+ ///
+ /// 拖拽起始位置。
+ [Signal]
public delegate void DroppedEventHandler(Vector2 startingPosition);
- ///
- /// 取消注册列表,用于管理需要在节点销毁时取消注册的对象
- ///
- private readonly IUnRegisterList _unRegisterList = new UnRegisterList();
+ ///
+ /// 取消注册列表,用于管理需要在节点销毁时取消注册的对象
+ ///
+ private readonly IUnRegisterList _unRegisterList = new UnRegisterList();
private bool _isDragging;
private Vector2 _offset = Vector2.Zero;
@@ -75,25 +75,33 @@ public abstract partial class DragDropComponent : Node, IController
}
///
- /// 处理输入事件的方法
+ /// 处理输入事件的回调方法。
+ /// 根据当前拖拽状态和输入事件类型,执行相应的拖拽操作。
///
/// 输入事件对象
public override void _Input(InputEvent @event)
{
- // 处理取消拖拽操作:当正在拖拽且按下取消拖拽按键时,执行取消拖拽逻辑
- if (!_isDragging || Target.IsInvalidNode() || !@event.IsActionPressed("cancel_drag")) return;
- CancelDragging();
- // 设置输入为处理,防止输入穿透
- this.SetInputAsHandled();
+ switch (_isDragging)
+ {
+ // 处理取消拖拽操作:当正在拖拽且按下取消拖拽按键时,执行取消拖拽逻辑
+ case true when Target.IsValidNode() && @event.IsActionPressed("cancel_drag"):
+ CancelDragging();
+ // 设置输入为处理,防止输入穿透
+ this.SetInputAsHandled();
+ break;
+ case true when @event.IsActionReleased("select"):
+ Drop();
+ break;
+ }
}
///
- /// 处理目标区域接收到的输入事件。
- /// 根据当前是否处于拖拽状态以及用户操作决定执行开始、取消或完成拖拽的动作。
+ /// 目标区域的输入事件处理器。
+ /// 当目标区域接收到输入事件时被调用,用于控制拖拽的开始和结束。
///
- /// 接收输入事件的视图端口节点。
- /// 发生的输入事件。
- /// 事件索引(未使用)。
+ /// 事件发生的视口节点
+ /// 输入事件对象
+ /// 事件触点ID(未使用)
private void OnTargetInputEvent(Node viewport, InputEvent @event, long _)
{
if (!Enable) return;
@@ -102,17 +110,14 @@ public abstract partial class DragDropComponent : Node, IController
var draggingObj = GetTree().GetFirstNodeInGroup(GroupName);
switch (_isDragging)
{
- // 如果当前没有拖拽操作且已有其他对象正在拖拽,则直接返回
- case false when draggingObj is not null:
- return;
// 处理开始拖拽操作:当未在拖拽状态且按下选择按键时,开始拖拽
+ case false when
+ // 如果当前没有拖拽操作且已有其他对象正在拖拽,则直接返回
+ draggingObj is not null:
+ return;
case false when @event.IsActionPressed("select"):
StartDragging();
break;
- // 处理放置操作:当正在拖拽且释放选择按键时,执行放置逻辑
- case true when @event.IsActionReleased("select"):
- Drop();
- break;
}
}