refactor(core): 优化架构实例访问方式并增强拖拽组件功能

- 将MArchitecture属性重构为IArchitecture类型的Instance属性
- 移除冗余的Interface属性,统一通过Instance访问架构实例
- 为AbstractDragDrop2DComponentBase组件添加可配置的输入操作名称
- 新增CancelDragInputActionName和SelectInputActionName属性
- 使用属性名替代硬编码字符串来处理拖拽相关输入事件
- 清理代码格式,移除多余空行和调整代码间距
- 更新XML文档注释以反映最新的代码结构和功能说明
This commit is contained in:
GwWuYou 2025-12-13 20:05:13 +08:00
parent 066ceeaec1
commit 8188ebbe73
3 changed files with 24 additions and 20 deletions

View File

@ -11,13 +11,13 @@ namespace GFramework.Core.Godot.component;
/// 继承自Godot的Node类并实现了IController接口。
/// 提供了拖拽相关的信号定义以及基础属性配置。
/// </summary>
public abstract partial class AbstractDragDrop2DComponentBase: Node, IController
public abstract partial class AbstractDragDrop2DComponentBase : Node, IController
{
/// <summary>
/// 取消注册列表,用于管理需要在节点销毁时取消注册的对象
/// </summary>
protected readonly IUnRegisterList UnRegisterList = new UnRegisterList();
/// <summary>
/// 当拖拽被取消时触发的信号。
/// </summary>
@ -37,7 +37,7 @@ public abstract partial class AbstractDragDrop2DComponentBase: Node, IController
/// <param name="startingPosition">拖拽起始位置。</param>
[Signal]
public delegate void DroppedEventHandler(Vector2 startingPosition);
/// <summary>
/// 是否启用拖拽功能。若为 false则忽略所有输入事件。
/// </summary>
@ -48,6 +48,16 @@ public abstract partial class AbstractDragDrop2DComponentBase: Node, IController
/// </summary>
public string GroupName { get; set; } = "dragging";
/// <summary>
/// 获取或设置取消拖拽输入操作的名称
/// </summary>
public string CancelDragInputActionName { get; set; } = "cancel_drag";
/// <summary>
/// 获取或设置选择输入操作的名称
/// </summary>
public string SelectInputActionName { get; set; } = "select";
/// <summary>
/// 拖拽时元素的最大Z轴索引值。
/// </summary>
@ -63,12 +73,12 @@ public abstract partial class AbstractDragDrop2DComponentBase: Node, IController
/// </summary>
/// <returns>返回实现IArchitecture接口的架构实例。</returns>
public abstract IArchitecture GetArchitecture();
/// <summary>
/// 表示是否正在拖拽操作的标志位。
/// </summary>
protected bool IsDragging;
/// <summary>
/// 表示拖拽操作中的偏移量,用于计算当前位置与起始位置的差值。
/// </summary>
@ -78,7 +88,7 @@ public abstract partial class AbstractDragDrop2DComponentBase: Node, IController
/// 表示拖拽操作的起始位置坐标。
/// </summary>
protected Vector2 StartingPosition;
/// <summary>
/// 节点退出场景树时的回调方法。
/// 在节点从场景树移除前调用,用于清理资源。
@ -87,4 +97,4 @@ public abstract partial class AbstractDragDrop2DComponentBase: Node, IController
{
UnRegisterList.UnRegisterAll();
}
}
}

View File

@ -34,12 +34,12 @@ public abstract partial class AbstractDragDropArea2DComponent : AbstractDragDrop
switch (IsDragging)
{
// 处理取消拖拽操作:当正在拖拽且按下取消拖拽按键时,执行取消拖拽逻辑
case true when Target.IsValidNode() && @event.IsActionPressed("cancel_drag"):
case true when Target.IsValidNode() && @event.IsActionPressed(CancelDragInputActionName):
CancelDragging();
// 设置输入为处理,防止输入穿透
this.SetInputAsHandled();
break;
case true when @event.IsActionReleased("select"):
case true when @event.IsActionReleased(SelectInputActionName):
Drop();
break;
}
@ -65,7 +65,7 @@ public abstract partial class AbstractDragDropArea2DComponent : AbstractDragDrop
// 如果当前没有拖拽操作且已有其他对象正在拖拽,则直接返回
draggingObj is not null:
return;
case false when @event.IsActionPressed("select"):
case false when @event.IsActionPressed(SelectInputActionName):
StartDragging();
break;
}

View File

@ -81,19 +81,13 @@ public abstract class Architecture<T> : IArchitecture where T : Architecture<T>,
/// </summary>
public static Action<T> OnRegisterPatch { get; set; } = _ => { };
/// <summary>
/// 获取架构实例的受保护静态属性
/// 通过Lazy初始化确保只创建一个实例
/// </summary>
/// <returns>T类型的架构实例</returns>
protected static T MArchitecture => MArchitectureLazy.Value;
/// <summary>
/// 获取架构实例的公共静态属性,以接口形式暴露
/// 提供对外访问架构功能的标准接口
/// 获取架构实例的静态属性
/// </summary>
/// <returns>IArchitecture接口类型的架构实例</returns>
public static IArchitecture Interface => MArchitectureLazy.Value;
/// <returns>返回IArchitecture类型的架构实例</returns>
public static IArchitecture Instance => MArchitectureLazy.Value;
/// <summary>
/// 注册一个系统到架构中。