From 066ceeaec1bfb61bfea0076c15ca28dd532f0fff Mon Sep 17 00:00:00 2001
From: GwWuYou <95328647+GeWuYou@users.noreply.github.com>
Date: Fri, 12 Dec 2025 21:43:35 +0800
Subject: [PATCH] =?UTF-8?q?feat(component):=20=E6=96=B0=E5=A2=9E2D?=
=?UTF-8?q?=E6=8B=96=E6=8B=BD=E5=9F=BA=E7=A1=80=E7=BB=84=E4=BB=B6=E5=B9=B6?=
=?UTF-8?q?=E9=87=8D=E6=9E=84=E6=8B=96=E6=8B=BD=E5=8C=BA=E5=9F=9F=E7=BB=84?=
=?UTF-8?q?=E4=BB=B6?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
- 添加抽象基类 AbstractDragDrop2DComponentBase 用于实现通用2D拖拽功能
- 重命名 AbstractDragDropComponent 为 AbstractDragDropArea2DComponent
- 将公共拖拽逻辑提取至基类,减少代码重复
- 统一信号定义与属性配置,提升组件一致性
- 优化拖拽状态管理与位置计算逻辑
- 完善节点生命周期中的资源清理机制
---
.../AbstractDragDrop2DComponentBase.cs | 90 +++++++++++++++++++
....cs => AbstractDragDropArea2DComponent.cs} | 81 +++--------------
2 files changed, 101 insertions(+), 70 deletions(-)
create mode 100644 GFramework.Core.Godot/component/AbstractDragDrop2DComponentBase.cs
rename GFramework.Core.Godot/component/{AbstractDragDropComponent.cs => AbstractDragDropArea2DComponent.cs} (62%)
diff --git a/GFramework.Core.Godot/component/AbstractDragDrop2DComponentBase.cs b/GFramework.Core.Godot/component/AbstractDragDrop2DComponentBase.cs
new file mode 100644
index 0000000..1a932d4
--- /dev/null
+++ b/GFramework.Core.Godot/component/AbstractDragDrop2DComponentBase.cs
@@ -0,0 +1,90 @@
+using GFramework.Core.architecture;
+using GFramework.Core.controller;
+using GFramework.Core.events;
+using GFramework.Core.extensions;
+using Godot;
+
+namespace GFramework.Core.Godot.component;
+
+///
+/// 抽象基类,用于实现2D拖拽功能的组件。
+/// 继承自Godot的Node类并实现了IController接口。
+/// 提供了拖拽相关的信号定义以及基础属性配置。
+///
+public abstract partial class AbstractDragDrop2DComponentBase: Node, IController
+{
+ ///
+ /// 取消注册列表,用于管理需要在节点销毁时取消注册的对象
+ ///
+ protected readonly IUnRegisterList UnRegisterList = new UnRegisterList();
+
+ ///
+ /// 当拖拽被取消时触发的信号。
+ ///
+ /// 拖拽起始位置。
+ [Signal]
+ public delegate void DragCanceledEventHandler(Vector2 startingPosition);
+
+ ///
+ /// 当拖拽开始时触发的信号。
+ ///
+ [Signal]
+ public delegate void DragStartedEventHandler();
+
+ ///
+ /// 当拖拽结束并放置时触发的信号。
+ ///
+ /// 拖拽起始位置。
+ [Signal]
+ public delegate void DroppedEventHandler(Vector2 startingPosition);
+
+ ///
+ /// 是否启用拖拽功能。若为 false,则忽略所有输入事件。
+ ///
+ public bool Enable { get; set; }
+
+ ///
+ /// 拖拽组的名称,用于区分不同的拖拽组。
+ ///
+ public string GroupName { get; set; } = "dragging";
+
+ ///
+ /// 拖拽时元素的最大Z轴索引值。
+ ///
+ public int ZIndexMax { get; set; } = 99;
+
+ ///
+ /// 拖拽时元素的最小Z轴索引值。
+ ///
+ public int ZIndexMin { get; set; } = 0;
+
+ ///
+ /// 获取架构实例。
+ ///
+ /// 返回实现IArchitecture接口的架构实例。
+ public abstract IArchitecture GetArchitecture();
+
+ ///
+ /// 表示是否正在拖拽操作的标志位。
+ ///
+ protected bool IsDragging;
+
+ ///
+ /// 表示拖拽操作中的偏移量,用于计算当前位置与起始位置的差值。
+ ///
+ protected Vector2 Offset = Vector2.Zero;
+
+ ///
+ /// 表示拖拽操作的起始位置坐标。
+ ///
+ protected Vector2 StartingPosition;
+
+ ///
+ /// 节点退出场景树时的回调方法。
+ /// 在节点从场景树移除前调用,用于清理资源。
+ ///
+ public override void _ExitTree()
+ {
+ UnRegisterList.UnRegisterAll();
+ }
+}
diff --git a/GFramework.Core.Godot/component/AbstractDragDropComponent.cs b/GFramework.Core.Godot/component/AbstractDragDropArea2DComponent.cs
similarity index 62%
rename from GFramework.Core.Godot/component/AbstractDragDropComponent.cs
rename to GFramework.Core.Godot/component/AbstractDragDropArea2DComponent.cs
index b839657..3e52bbf 100644
--- a/GFramework.Core.Godot/component/AbstractDragDropComponent.cs
+++ b/GFramework.Core.Godot/component/AbstractDragDropArea2DComponent.cs
@@ -1,8 +1,4 @@
-using GFramework.Core.architecture;
-using GFramework.Core.controller;
-using GFramework.Core.events;
-using GFramework.Core.extensions;
-using GFramework.Core.Godot.extensions;
+using GFramework.Core.Godot.extensions;
using Godot;
namespace GFramework.Core.Godot.component;
@@ -11,59 +7,13 @@ namespace GFramework.Core.Godot.component;
/// 抽象拖拽组件类,用于处理节点的拖放逻辑。
/// 实现了 IController 接口以支持架构通信,并通过信号通知拖拽事件的发生。
///
-public abstract partial class AbstractDragDropComponent : Node, IController
+public abstract partial class AbstractDragDropArea2DComponent : AbstractDragDrop2DComponentBase
{
- ///
- /// 当拖拽被取消时触发的信号。
- ///
- /// 拖拽起始位置。
- [Signal]
- public delegate void DragCanceledEventHandler(Vector2 startingPosition);
-
- ///
- /// 当拖拽开始时触发的信号。
- ///
- [Signal]
- public delegate void DragStartedEventHandler();
-
- ///
- /// 当拖拽结束并放置时触发的信号。
- ///
- /// 拖拽起始位置。
- [Signal]
- public delegate void DroppedEventHandler(Vector2 startingPosition);
-
- ///
- /// 取消注册列表,用于管理需要在节点销毁时取消注册的对象
- ///
- private readonly IUnRegisterList _unRegisterList = new UnRegisterList();
-
- private bool _isDragging;
- private Vector2 _offset = Vector2.Zero;
-
- private Vector2 _startingPosition;
-
///
/// 目标区域,通常是可交互的游戏对象(如单位或物品)所在的碰撞区域。
///
public required Area2D Target { get; set; }
- ///
- /// 是否启用拖拽功能。若为 false,则忽略所有输入事件。
- ///
- public bool Enable { get; set; }
-
- public string GroupName { get; set; } = "dragging";
-
- public int ZIndexMax { get; set; } = 99;
- public int ZIndexMin { get; set; } = 0;
-
- ///
- /// 获取游戏架构实例
- ///
- /// 返回游戏架构接口实例
- public abstract IArchitecture GetArchitecture();
-
///
/// 节点准备就绪时的回调方法。
/// 在节点添加到场景树后调用,绑定目标区域的输入事件处理器。
@@ -81,7 +31,7 @@ public abstract partial class AbstractDragDropComponent : Node, IController
/// 输入事件对象
public override void _Input(InputEvent @event)
{
- switch (_isDragging)
+ switch (IsDragging)
{
// 处理取消拖拽操作:当正在拖拽且按下取消拖拽按键时,执行取消拖拽逻辑
case true when Target.IsValidNode() && @event.IsActionPressed("cancel_drag"):
@@ -108,7 +58,7 @@ public abstract partial class AbstractDragDropComponent : Node, IController
// 获取当前正在拖拽的对象
var draggingObj = GetTree().GetFirstNodeInGroup(GroupName);
- switch (_isDragging)
+ switch (IsDragging)
{
// 处理开始拖拽操作:当未在拖拽状态且按下选择按键时,开始拖拽
case false when
@@ -127,7 +77,7 @@ public abstract partial class AbstractDragDropComponent : Node, IController
/// 与上一帧的时间间隔(秒)。
public override void _Process(double delta)
{
- if (_isDragging && Target.IsValidNode()) Target.GlobalPosition = Target.GetGlobalMousePosition() + _offset;
+ if (IsDragging && Target.IsValidNode()) Target.GlobalPosition = Target.GetGlobalMousePosition() + Offset;
}
///
@@ -136,7 +86,7 @@ public abstract partial class AbstractDragDropComponent : Node, IController
///
private void EndDragging()
{
- _isDragging = false;
+ IsDragging = false;
Target.RemoveFromGroup(GroupName);
Target.ZIndex = ZIndexMin;
}
@@ -148,7 +98,7 @@ public abstract partial class AbstractDragDropComponent : Node, IController
private void CancelDragging()
{
EndDragging();
- EmitSignalDragCanceled(_startingPosition);
+ EmitSignalDragCanceled(StartingPosition);
}
///
@@ -157,11 +107,11 @@ public abstract partial class AbstractDragDropComponent : Node, IController
///
private void StartDragging()
{
- _isDragging = true;
- _startingPosition = Target.GlobalPosition;
+ IsDragging = true;
+ StartingPosition = Target.GlobalPosition;
Target.AddToGroup(GroupName);
Target.ZIndex = ZIndexMax;
- _offset = Target.GlobalPosition - Target.GetGlobalMousePosition();
+ Offset = Target.GlobalPosition - Target.GetGlobalMousePosition();
EmitSignalDragStarted();
}
@@ -172,15 +122,6 @@ public abstract partial class AbstractDragDropComponent : Node, IController
private void Drop()
{
EndDragging();
- EmitSignalDropped(_startingPosition);
- }
-
- ///
- /// 节点退出场景树时的回调方法。
- /// 在节点从场景树移除前调用,用于清理资源。
- ///
- public override void _ExitTree()
- {
- _unRegisterList.UnRegisterAll();
+ EmitSignalDropped(StartingPosition);
}
}
\ No newline at end of file