完成参数分组构建功能

This commit is contained in:
zengwenjie
2025-09-19 16:51:51 +08:00
parent a2097d781e
commit e0bb807214
9 changed files with 143 additions and 14 deletions

View File

@@ -10,5 +10,11 @@ namespace Deedy.Activity
public class ParamDefineAttribute : Attribute, IInitialSortable
{
public int InitialSort { get; set; } = 0;
public ParamDefineAttribute() { }
public string Title { get; set; } = "参数名称";
public string Remark { get; set; } = "参数描述";
public string Default { get; set; } = "";
public string DesignValue { get; set; } = "";
public string GroupName { get; set; } = "";
}
}

View File

@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
@@ -8,6 +10,64 @@ namespace Deedy.Activity
{
public class ParamGroupAttribute : Attribute, IInitialSortable
{
protected internal ParamGroupAttribute() { }
public ParamGroupAttribute(string groupName, int initialSort = 0, bool canAddNew = false)
{
if (string.IsNullOrWhiteSpace(groupName)) throw new ArgumentNullException("参数分组名称不可为「null」或「空白字符」");
if (groupName == RuntimeParams || groupName == WorkingParams || groupName == DefaultParams) throw new ArgumentException("不可以用系统保留名称做为自定义分组名称...");
this.GroupName = groupName;
this.InitialSort = initialSort;
this.CanAddNew = canAddNew;
}
public const string RuntimeParams = "系统参数";
public const string WorkingParams = "工作参数";
public const string DefaultParams = "其它参数";
public int InitialSort { get; set; } = 0;
private string _GroupName = "";
public string GroupName
{
get
{
if (string.IsNullOrEmpty(_GroupName)) _GroupName = DefaultParams;
return _GroupName;
}
private set
{
if (value == _GroupName) return;
if (string.IsNullOrEmpty(value) || string.IsNullOrEmpty(value.Trim())) _GroupName = DefaultParams;
else _GroupName = value;
}
}
[AllowNull]
private string _Description;
public string Description
{
get { return string.IsNullOrEmpty(_Description) ? _GroupName : _Description; }
set { _Description = string.IsNullOrEmpty(value) ? _GroupName : value; }
}
public bool CanAddNew { get; protected internal set; }
public ParamDefineCollection Params { get; protected internal set; } = [];
internal static ParamGroupAttribute CreateRuntimeGroup() => new()
{
GroupName = RuntimeParams,
Description = "运行时需要的参数,自定义参数尽量不要放置到此分组中...",
InitialSort = int.MinValue
};
internal static ParamGroupAttribute CreateWorkingParams() => new()
{
GroupName = WorkingParams,
Description = "动作执行时的工作逻辑所需要的参数",
InitialSort = int.MinValue + 1
};
internal static ParamGroupAttribute CreateDefaultGroup() => new()
{
GroupName = DefaultParams,
Description = "所有未分组参数均会被默认放置到此分组中",
InitialSort = int.MaxValue
};
}
}

View File

@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
@@ -38,8 +39,8 @@ namespace Deedy.Activity
public IActionViewer? ActionViewer { get; protected internal set; }
public IElement RootElement => (this.ParentElement == null) ? this : this.ParentElement.RootElement;
public Runtime Runtime => throw new NotImplementedException();
public Runtime? Runtime { get; protected internal set; }
public ParamDefineCollection ParamsMapping { get; set; } = [];
public event PropertyChangedEventHandler? PropertyChanged;

View File

@@ -3,8 +3,10 @@ using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Documents;
namespace Deedy.Activity
{
@@ -12,6 +14,42 @@ namespace Deedy.Activity
[DebuggerDisplay("Count = {Count}")]
public class ParamGroupCollection : InitialSortedObservableCollection<ParamGroupAttribute>
{
internal ParamGroupCollection() { }
private ParamGroupAttribute _DefaultGroup = ParamGroupAttribute.CreateDefaultGroup();
internal ParamGroupCollection()
{
this.Add(ParamGroupAttribute.CreateRuntimeGroup());
this.Add(ParamGroupAttribute.CreateWorkingParams());
this.Add(_DefaultGroup);
}
public override void Add(ParamGroupAttribute item)
{
foreach (var oldItem in this)
{
if (oldItem.GroupName == item.GroupName) return;
}
base.Add(item);
}
public void AddFromType(Type type)
{
var pgas = type.GetCustomAttributes<ParamGroupAttribute>(true);
foreach (var attr in pgas) this.Add(attr);
}
public void AppendParamDefine(ParamDefineAttribute paramDefine)
{
foreach (var item in this)
{
if (item.GroupName == paramDefine.GroupName)
{
item.Params.Add(paramDefine);
return;
}
}
paramDefine.GroupName = ParamGroupAttribute.DefaultParams;
this._DefaultGroup.Params.Add(paramDefine);
}
public void AppendParamRange(IEnumerable<ParamDefineAttribute> paramDefines)
{
foreach (var paramDefine in paramDefines) this.AppendParamDefine(paramDefine);
}
}
}

View File

@@ -24,7 +24,9 @@ namespace Deedy.Activity
public ElementCollection Elements { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
public Runtime Runtime => throw new NotImplementedException();
public Runtime? Runtime => throw new NotImplementedException();
public ParamDefineCollection ParamsMapping { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
public event PropertyChangedEventHandler? PropertyChanged;

View File

@@ -152,15 +152,7 @@ namespace Deedy.Activity
if (oldIndex < 0) @this.Help_Insert(element, (uint)newIndex);
else @this.Elements.Move(oldIndex, newIndex);
}
/// <summary>
/// 帮助一个IElement节点构建参数映射表
/// </summary>
/// <param name="element">需要构建参数映射表的Element节点</param>
/// <param name="output">消息输出器</param>
public static void Help_BuildParamMapping(this IElement element, Output? output = null)
{
//TODO构建元素的参数映射表
}
/// <summary>
/// 将一个元素插入到当前节点前方「前提:当前节点的父级必须是可变容器节点」
/// </summary>

View File

@@ -91,5 +91,32 @@ namespace Deedy.Activity
}
[UnsafeAccessor(UnsafeAccessorKind.Method, Name = "set_ParentElement")]
private static extern void ActionSetter_ParentElement(BasalAction action, IElement? value);
/// <summary>
/// 帮助一个IElement节点构建参数映射表
/// </summary>
/// <param name="element">需要构建参数映射表的Element节点</param>
/// <param name="output">消息输出器</param>
public static void Help_BuildParamMapping(this IElement element, Output? output = null)
{
//TODO构建元素的参数映射表
if (element == null) return;
if (element.ParamsMapping == null) element.ParamsMapping = new();
}
/// <summary>
/// 构建一个元素的参数分组,为设计器准备的辅助方法
/// </summary>
/// <param name="element"></param>
/// <param name="output"></param>
/// <returns></returns>
public static ParamGroupCollection Help_BuildParamGroup(this IElement element, Output? output = null)
{
ParamGroupCollection result = new();
if (element != null)
{
result.AddFromType(element.GetType());
result.AppendParamRange(element.ParamsMapping);
}
return result;
}
}
}

View File

@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
@@ -8,7 +9,7 @@ namespace Deedy.Activity
{
public interface IActivity
{
public Runtime Runtime { get; }
public Runtime? Runtime { get; }
public void ReadyToWorking(IElement? element = null, Output? output = null);
public void ReadyToEditing(Runtime runtime);
public void ReadyToRunning(Runtime runtime);

View File

@@ -19,6 +19,8 @@ namespace Deedy.Activity
public int DepthLevel { get; }
public IElement? ParentElement { get; }
public IElement RootElement { get; }
public ParamDefineCollection ParamsMapping { get; set; }
/// <summary>
/// 将当前节点链接到一个节点上
/// </summary>