Skip to content

Commit

Permalink
新增 插件自定义输入节点控件
Browse files Browse the repository at this point in the history
修复 情景Tick方法执行
  • Loading branch information
MakesYT committed May 21, 2024
1 parent 29e16aa commit bba147b
Show file tree
Hide file tree
Showing 17 changed files with 177 additions and 21 deletions.
13 changes: 8 additions & 5 deletions Core/SDKs/CustomScenario/ConnectorItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Messaging;
using Core.SDKs.Services.Config;
using PluginCore;

namespace Core.SDKs.CustomScenario;

public partial class ConnectorItem : ObservableRecipient
public partial class ConnectorItem : ObservableRecipient, IConnectorItem
{
[JsonConverter(typeof(PointJsonConverter))]
#pragma warning disable CS0657 // 不是此声明的有效特性位置
[property: JsonConverter(typeof(PointJsonConverter))]
#pragma warning restore CS0657 // 不是此声明的有效特性位置
[ObservableProperty]
private Point _anchor;

Expand All @@ -37,10 +40,6 @@ public partial class ConnectorItem : ObservableRecipient
[JsonConverter(typeof(TypeJsonConverter))]
public Type Type { get; set; }

/// <summary>
///
/// </summary>
///
[JsonConverter(typeof(TypeJsonConverter))]
public Type RealType
{
Expand Down Expand Up @@ -81,4 +80,8 @@ partial void OnInputObjectChanged(object? value)
{
WeakReferenceMessenger.Default.Send(new CustomScenarioChangeMsg() { PointItem = Source, ConnectorItem = this });
}

//插件自定义输入连接器
public bool isPluginInputConnector { get; set; }
public INodeInputConnector PluginInputConnector { get; set; }
}
10 changes: 2 additions & 8 deletions Core/SDKs/CustomScenario/CustomScenario.cs
Original file line number Diff line number Diff line change
Expand Up @@ -325,14 +325,8 @@ private void TickMethod(object sender, long JumpPeriod, long interval)
return;
}

var connectionItem = connections.FirstOrDefault((e) => e.Source == nodes[1]
.Output[0]);
var firstNodes = connectionItem.Target.Source;
nodes[1].Status = s节点状态.已验证;
_tickTasks.Add(nodes[0], null);
_tickTasks.Add(nodes[1], null);
_tickTasks.Add(firstNodes, null);
ParsePointItem(_tickTasks, firstNodes, false, true, _cancellationTokenSource.Token);
var nowPointItem = nodes[1];
ParsePointItem(_tickTasks, nowPointItem, false, true, _cancellationTokenSource.Token);

while (true)
{
Expand Down
28 changes: 25 additions & 3 deletions Core/SDKs/Services/Plugin/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,6 @@ private object GetPointItemByMethodInfo(MethodInfo methodInfo)
}
}


inpItems.Add(new ConnectorItem()
{
Source = pointItem,
Expand All @@ -262,14 +261,32 @@ private object GetPointItemByMethodInfo(MethodInfo methodInfo)
}
else
{
inpItems.Add(new ConnectorItem()
var connectorItem = new ConnectorItem()
{
Source = pointItem,
Type = parameterInfo.ParameterType,
IsSelf = IsSelf,
Title = customAttribute.GetParameterName(parameterInfo.Name),
TypeName = BaseNodeMethodsGen.GetI18N(parameterInfo.ParameterType.FullName)
});
};
if (parameterInfo.ParameterType.GetCustomAttribute<CustomNodeInputType>() is not null
and var customNodeInputType)
{
connectorItem.isPluginInputConnector = true;
connectorItem.IsSelf = true;
try
{
var service = ServiceProvider.GetService(customNodeInputType.Type);
connectorItem.PluginInputConnector = service as INodeInputConnector;
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}

inpItems.Add(connectorItem);
}

//Log.Debug($"参数{index}:类型为{parameterInfo.ParameterType}");
Expand Down Expand Up @@ -337,6 +354,11 @@ private object GetPointItemByMethodInfo(MethodInfo methodInfo)
return pointItem;
}

private ObservableCollection<ConnectorItem> getConnectorItemsBy()
{
return new ObservableCollection<ConnectorItem>();
}


[MethodImpl(MethodImplOptions.NoInlining)]
public static void UnloadByPluginInfo(string pluginInfoEx, out WeakReference weakReference)
Expand Down
28 changes: 28 additions & 0 deletions Core/SDKs/Services/Plugin/PluginAvaloniaResourceManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Avalonia.Styling;

namespace Core.SDKs.Services.Plugin;

public static class PluginAvaloniaResourceManager
{
private static Dictionary<string, IStyle> _resources = new();

public static IStyle GetStyle(string key)
{
if (_resources.ContainsKey(key))
{
return _resources[key];
}

return null;
}

public static IStyle AddStyle(string key, IStyle style)
{
if (!_resources.ContainsKey(key))
{
_resources.Add(key, style);
}

return style;
}
}
5 changes: 4 additions & 1 deletion Core/SDKs/Tools/ScreenCapture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using Avalonia;
using Avalonia.Media.Imaging;
using Avalonia.Platform;
using Core.SDKs.Services;
using Microsoft.Extensions.DependencyInjection;
using PluginCore;
using ScreenCapture.NET;
using SixLabors.ImageSharp;
Expand Down Expand Up @@ -175,6 +177,7 @@ private static Configuration CreateDefaultInstance()

public ScreenCaptureInfo GetScreenCaptureInfoByUserManual()
{
throw new NotImplementedException();
return ServiceManager.Services.GetService<IScreenCaptureWindow>()!.GetScreenCaptureInfo()
.Result;
}
}
8 changes: 6 additions & 2 deletions Core/ViewModel/TaskEditor/TaskEditorViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,9 @@ private void AddNodes(PointItem pointItem)
AutoUnboxIndex = connectorItem.AutoUnboxIndex,
IsSelf = connectorItem.IsSelf,
SelfInputAble = connectorItem.SelfInputAble,
IsOut = connectorItem.IsOut
IsOut = connectorItem.IsOut,
isPluginInputConnector = connectorItem.isPluginInputConnector,
PluginInputConnector = connectorItem.PluginInputConnector
});
var plugin = PluginManager.EnablePlugin.FirstOrDefault((e) => e.Value._dll == connectorItem.Type.Assembly)
.Value;
Expand Down Expand Up @@ -427,7 +429,9 @@ private void CopyNode(PointItem pointItem)
AutoUnboxIndex = connectorItem.AutoUnboxIndex,
IsSelf = connectorItem.IsSelf,
SelfInputAble = connectorItem.SelfInputAble,
IsOut = connectorItem.IsOut
IsOut = connectorItem.IsOut,
isPluginInputConnector = connectorItem.isPluginInputConnector,
PluginInputConnector = connectorItem.PluginInputConnector
});
}

Expand Down
9 changes: 9 additions & 0 deletions KitopiaAvalonia/Pages/MyDataTemplateSelector.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#region

using System;
using System.Collections.Generic;
using Avalonia.Controls;
using Avalonia.Controls.Templates;
Expand Down Expand Up @@ -27,6 +28,14 @@ public class MyDataTemplateSelector : IDataTemplate
.Build(item);
}

if (pointItem.isPluginInputConnector)
{
var control = pointItem.PluginInputConnector.IDataTemplate.Build(item);
pointItem.PluginInputConnector.Value.Subscribe(x => { pointItem.InputObject = x; });
control!.Styles.Add(pointItem.PluginInputConnector.Style);
return control;
}

return (pointItem.RealType.FullName! switch
{
"System.String" => Templates["StringTemplate"]
Expand Down
1 change: 1 addition & 0 deletions KitopiaEx/KitopiaEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public static IServiceProvider GetServiceProvider()
services.AddSingleton<ClipboardEx>();
services.AddSingleton<ImageTools>();
services.AddSingleton<Test1>();
services.AddSingleton<NodeInputConnector1>();
return services.BuildServiceProvider();
}
}
22 changes: 22 additions & 0 deletions KitopiaEx/NodeInputConnector1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using Avalonia.Controls.Templates;
using Avalonia.Markup.Xaml.Styling;
using PluginCore;

namespace KitopiaEx;

public class NodeInputConnector1 : INodeInputConnector
{
public StyleInclude Style =>
new(new Uri("avares://KitopiaEx"))
{ Source = new Uri("NodeInputConnector1Style.axaml", UriKind.Relative) };

public IDataTemplate IDataTemplate =>
new ResourceInclude(new Uri("avares://KitopiaEx"))
{ Source = new Uri("NodeInputConnector1DataTemple.axaml", UriKind.Relative) }
.TryGetResource("Template", null, out var variant)
? (IDataTemplate)variant
: null;

public ObservableValue Value { get; } = new ObservableValue();
}
10 changes: 10 additions & 0 deletions KitopiaEx/NodeInputConnector1DataTemple.axaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<ResourceDictionary xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:customScenario="clr-namespace:Core.SDKs.CustomScenario;assembly=PluginCore">
<!-- Add Resources Here -->
<DataTemplate x:Key="Template" DataType="customScenario:IConnectorItem">
<Button Height="30" Width="30" Foreground="Black">
<Ellipse Width="30" Height="30" Fill="Red" />
</Button>
</DataTemplate>
</ResourceDictionary>
16 changes: 16 additions & 0 deletions KitopiaEx/NodeInputConnector1Style.axaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Styles xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="using:KitopiaEx">
<Design.PreviewWith>
<controls:NodeInputConnector1Style />
</Design.PreviewWith>

<Style Selector="controls|NodeInputConnector1Style">
<!-- Set Defaults -->
<Setter Property="Template">
<ControlTemplate>
<TextBlock Text="Templated Control" />
</ControlTemplate>
</Setter>
</Style>
</Styles>
7 changes: 7 additions & 0 deletions KitopiaEx/NodeInputConnector1Style.axaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using Avalonia.Controls.Primitives;

namespace KitopiaEx;

public class NodeInputConnector1Style : TemplatedControl
{
}
9 changes: 9 additions & 0 deletions KitopiaEx/NodeInputType1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using PluginCore.Attribute;

namespace KitopiaEx;

[CustomNodeInputType(typeof(NodeInputConnector1))]
public class NodeInputType1
{
public string Name { get; set; }
}
11 changes: 11 additions & 0 deletions KitopiaEx/Styles1.axaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Styles xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Design.PreviewWith>
<Border Padding="20">
<!-- Add Controls for Previewer Here -->
</Border>
</Design.PreviewWith>

<!-- Add Styles Here -->

</Styles>
2 changes: 1 addition & 1 deletion KitopiaEx/Test1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class Test1
{
[PluginMethod("Test", $"{nameof(item)}=本地项目",
"return=返回参数")]
public void OpenSearchViewItem(Test1 item, CancellationToken cancellationToken)
public void OpenSearchViewItem(NodeInputType1 item, CancellationToken cancellationToken)
{
}
}
17 changes: 17 additions & 0 deletions KitopiaTest/UnitTest1.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Management;

namespace KitopiaTest;

public class Tests
Expand All @@ -10,6 +12,21 @@ public void Setup()
[Test]
public void Test1()
{
try
{
ManagementObjectSearcher searcher =
new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_OperatingSystem");

foreach (ManagementObject queryObj in searcher.Get())
{
Console.WriteLine("Caption: {0}", queryObj["Caption"]);
}
}
catch (ManagementException e)
{
Console.WriteLine("An error occurred while querying for WMI data: " + e.Message);
}

Assert.Pass();
}
}
2 changes: 1 addition & 1 deletion PluginCore

0 comments on commit bba147b

Please sign in to comment.