重新设计了创建连线时的逻辑,能够预览连接成功后的外观样式

This commit is contained in:
fengjiayi
2025-01-04 22:20:01 +08:00
parent 665a722f68
commit 702af587f9
20 changed files with 1040 additions and 277 deletions

View File

@@ -1,18 +1,14 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Primitives;
using Avalonia.Input;
using Avalonia.Media;
using Serein.Library;
using Serein.Workbench.Avalonia.Views;
using System.Drawing;
using Serein.Library.Api;
using Serein.Workbench.Avalonia.Api;
using Serein.Workbench.Avalonia.Extension;
using System;
using Color = Avalonia.Media.Color;
using Point = Avalonia.Point;
using System.Diagnostics;
using Avalonia.Threading;
using Serein.Workbench.Avalonia.Api;
using Serein.Workbench.Avalonia.Extension;
namespace Serein.Workbench.Avalonia.Custom.Views;
@@ -21,55 +17,6 @@ namespace Serein.Workbench.Avalonia.Custom.Views;
/// </summary>
public class NodeJunctionView : TemplatedControl
{
private readonly INodeOperationService nodeOperationService;
/// <summary>
/// Render方法中控制自绘内容
/// </summary>
protected readonly StreamGeometry StreamGeometry = new StreamGeometry();
/// <summary>
/// 正在查看
/// </summary>
private bool IsPreviewing;
public NodeJunctionView()
{
nodeOperationService = App.GetService<INodeOperationService>();
this.PointerMoved += NodeJunctionView_PointerMoved;
this.PointerExited += NodeJunctionView_PointerExited;
this.PointerPressed += NodeJunctionView_PointerPressed;
this.PointerReleased += NodeJunctionView_PointerReleased;
}
private void NodeJunctionView_PointerReleased(object? sender, PointerReleasedEventArgs e)
{
nodeOperationService.ConnectingData.IsCreateing = false;
}
private void NodeJunctionView_PointerPressed(object? sender, PointerPressedEventArgs e)
{
nodeOperationService.TryCreateConnectionOnJunction(this); // 尝试开始创建
Dispatcher.UIThread.InvokeAsync(InvalidateVisual, DispatcherPriority.Background);
}
/// <summary>
/// 获取到控件信息
/// </summary>
/// <param name="e"></param>
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
{
base.OnApplyTemplate(e);
//if (e.NameScope.Find("PART_FlipflopMethodInfos") is ListBox p_fm)
//{
// //p_fm.SelectionChanged += ListBox_SelectionChanged;
// //p_fm.PointerExited += ListBox_PointerExited;
//}
}
public static readonly DirectProperty<NodeJunctionView, JunctionType> JunctionTypeProperty =
AvaloniaProperty.RegisterDirect<NodeJunctionView, JunctionType>(nameof(JunctionType), o => o.JunctionType, (o, v) => o.JunctionType = v);
@@ -88,6 +35,160 @@ public class NodeJunctionView : TemplatedControl
get { return myNode; }
set { SetAndRaise(MyNodeProperty, ref myNode, value); }
}
public static readonly DirectProperty<NodeJunctionView, int> ArgIndexProperty =
AvaloniaProperty.RegisterDirect<NodeJunctionView, int>(nameof(ArgIndex), o => o.ArgIndex, (o, v) => o.ArgIndex = v);
private int argIndex;
public int ArgIndex
{
get { return argIndex; }
set { SetAndRaise(ArgIndexProperty, ref argIndex, value); }
}
private readonly INodeOperationService nodeOperationService;
private readonly IFlowEnvironment flowEnvironment;
/// <summary>
/// Render方法中控制自绘内容
/// </summary>
protected readonly StreamGeometry StreamGeometry = new StreamGeometry();
#region
public NodeJunctionView()
{
nodeOperationService = App.GetService<INodeOperationService>();
flowEnvironment = App.GetService<IFlowEnvironment>();
//this.PointerExited += NodeJunctionView_PointerExited;
this.PointerMoved += NodeJunctionView_PointerMoved;
this.PointerPressed += NodeJunctionView_PointerPressed;
this.PointerReleased += NodeJunctionView_PointerReleased;
}
public bool IsPreviewing { get; set; }
private Guid Guid = Guid.NewGuid();
private void NodeJunctionView_PointerMoved(object? sender, PointerEventArgs e)
{
if (!nodeOperationService.ConnectingData.IsCreateing)
return;
if (nodeOperationService.MainCanvas is not InputElement inputElement)
return;
var currentPoint = e.GetPosition(nodeOperationService.MainCanvas);
if (inputElement.InputHitTest(currentPoint) is NodeJunctionView junctionView)
{
RefreshDisplay(junctionView);
}
else
{
var oldNj = nodeOperationService.ConnectingData.CurrentJunction;
if (oldNj is not null)
{
oldNj.IsPreviewing = false;
oldNj.InvalidateVisual();
}
}
}
private void RefreshDisplay(NodeJunctionView junctionView)
{
var oldNj = nodeOperationService.ConnectingData.CurrentJunction;
if (oldNj is not null )
{
if (junctionView.Equals(oldNj))
{
return;
}
oldNj.IsPreviewing = false;
oldNj.InvalidateVisual();
}
nodeOperationService.ConnectingData.CurrentJunction = junctionView;
if (!this.Equals(junctionView))
{
nodeOperationService.ConnectingData.TempLine?.ToEnd(junctionView);
}
junctionView.IsPreviewing = true;
junctionView.InvalidateVisual();
}
/// <summary>
/// 尝试开始创建连接线
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void NodeJunctionView_PointerPressed(object? sender, PointerPressedEventArgs e)
{
nodeOperationService.TryCreateConnectionOnJunction(this); // 尝试开始创建
}
private void NodeJunctionView_PointerReleased(object? sender, PointerReleasedEventArgs e)
{
CheckJunvtion();
nodeOperationService.ConnectingData.Reset();
}
private void CheckJunvtion()
{
var myData = nodeOperationService.ConnectingData;
if(myData.StartJunction is null || myData.CurrentJunction is null)
{
return;
}
if(myData.StartJunction.MyNode is null || myData.CurrentJunction.MyNode is null)
{
return;
}
if (!myData.IsCanConnected())
{
return;
}
var canvas = nodeOperationService.MainCanvas;
#region
if (myData.Type == JunctionOfConnectionType.Invoke)
{
flowEnvironment.ConnectInvokeNodeAsync(myData.StartJunction.MyNode.Guid, myData.CurrentJunction.MyNode.Guid,
myData.StartJunction.JunctionType,
myData.CurrentJunction.JunctionType,
myData.ConnectionInvokeType);
}
#endregion
#region
else if (myData.Type == JunctionOfConnectionType.Arg)
{
var argIndex = 0;
if (myData.StartJunction.JunctionType == JunctionType.ArgData)
{
argIndex = myData.StartJunction.ArgIndex;
}
else if (myData.CurrentJunction.JunctionType == JunctionType.ArgData)
{
argIndex = myData.CurrentJunction.ArgIndex;
}
flowEnvironment.ConnectArgSourceNodeAsync(myData.StartJunction.MyNode.Guid, myData.CurrentJunction.MyNode.Guid,
myData.StartJunction.JunctionType,
myData.CurrentJunction.JunctionType,
myData.ConnectionArgSourceType,
argIndex);
}
#endregion
}
#endregion
#region UI视觉
@@ -101,7 +202,8 @@ public class NodeJunctionView : TemplatedControl
double width = 44;
double height = 26;
var background = GetBackgrounp();
var pen = new Pen(Brushes.Black, 1);
var pen = new Pen(Brushes.Transparent, 1);
//var pen = nodeOperationService.ConnectingData.IsCreateing ? new Pen(background, 1) : new Pen(Brushes.Black, 1);
// 输入连接器的背景
var connectorRect = new Rect(0, 0, width, height);
@@ -132,32 +234,10 @@ public class NodeJunctionView : TemplatedControl
context.LineTo(new Point(triangleCenterX, triangleCenterY + t), true);
context.LineTo(new Point(triangleCenterX, triangleCenterY - t), true);
}
drawingContext.DrawGeometry(background, new Pen(Brushes.Black, 1), pathGeometry);
drawingContext.DrawGeometry(background, pen, pathGeometry);
}
#region
private void NodeJunctionView_PointerExited(object? sender, PointerEventArgs e)
{
if (IsPreviewing)
{
IsPreviewing = false;
Dispatcher.UIThread.InvokeAsync(InvalidateVisual, DispatcherPriority.Background);
}
}
private void NodeJunctionView_PointerMoved(object? sender, PointerEventArgs e)
{
if (!IsPreviewing)
{
IsPreviewing = true;
Dispatcher.UIThread.InvokeAsync(InvalidateVisual, DispatcherPriority.Background);
}
}
#endregion
/// <summary>
@@ -167,38 +247,25 @@ public class NodeJunctionView : TemplatedControl
protected IBrush GetBackgrounp()
{
var myData = nodeOperationService.ConnectingData;
if (!myData.IsCreateing)
if (IsPreviewing == false || !myData.IsCreateing )
{
//Debug.WriteLine($"return color is {Brushes.BurlyWood}");
return new SolidColorBrush(Color.Parse("#76ABEE"));
}
if (myData.IsCanConnected)
if (!myData.IsCanConnected())
{
if (myData.Type == JunctionOfConnectionType.Invoke)
{
return myData.ConnectionInvokeType.ToLineColor();
}
else
{
return myData.ConnectionArgSourceType.ToLineColor();
}
}
else
{
return Brushes.Red;
return new SolidColorBrush(Color.Parse("#FF0000"));
}
if (IsPreviewing)
if (myData.Type == JunctionOfConnectionType.Invoke)
{
//return new SolidColorBrush(Color.Parse("#04FC10"));
return myData.ConnectionInvokeType.ToLineColor(); // 调用
}
else
{
//Debug.WriteLine($"return color is {Brushes.BurlyWood}");
return new SolidColorBrush(Color.Parse("#76ABEE"));
return myData.ConnectionArgSourceType.ToLineColor(); // 参数
}
}
#endregion