2024-10-22 00:13:13 +08:00
|
|
|
|
using Serein.Library;
|
|
|
|
|
|
using Serein.Library.Api;
|
2024-10-15 21:56:09 +08:00
|
|
|
|
using Serein.Workbench.Node.ViewModel;
|
2024-10-23 19:22:27 +08:00
|
|
|
|
using System.Windows;
|
2024-08-05 10:11:58 +08:00
|
|
|
|
using System.Windows.Controls;
|
2024-10-23 19:22:27 +08:00
|
|
|
|
using System.Windows.Data;
|
|
|
|
|
|
using System.Windows.Input;
|
2024-08-05 10:11:58 +08:00
|
|
|
|
using System.Windows.Media;
|
|
|
|
|
|
|
2024-10-15 21:56:09 +08:00
|
|
|
|
namespace Serein.Workbench.Node.View
|
2024-08-05 10:11:58 +08:00
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 节点控件基类(控件)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public abstract class NodeControlBase : UserControl, IDynamicFlowNode
|
|
|
|
|
|
{
|
2024-12-26 00:26:50 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 节点所在的画布(以后需要将画布封装出来,实现多画布的功能)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public Canvas NodeCanvas { get; set; }
|
|
|
|
|
|
|
|
|
|
|
|
private INodeContainerControl nodeContainerControl;
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 如果该节点放置在了某个容器节点,就会记录这个容器节点
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private INodeContainerControl NodeContainerControl { get; }
|
|
|
|
|
|
|
2024-10-24 23:32:43 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 记录与该节点控件有关的所有连接
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private readonly List<ConnectionControl> connectionControls = new List<ConnectionControl>();
|
|
|
|
|
|
|
2024-09-12 20:32:54 +08:00
|
|
|
|
public NodeControlViewModelBase ViewModel { get; set; }
|
|
|
|
|
|
|
2024-12-14 23:46:37 +08:00
|
|
|
|
|
2024-08-05 10:11:58 +08:00
|
|
|
|
protected NodeControlBase()
|
|
|
|
|
|
{
|
|
|
|
|
|
this.Background = Brushes.Transparent;
|
|
|
|
|
|
}
|
2024-12-12 20:31:50 +08:00
|
|
|
|
|
2024-09-12 20:32:54 +08:00
|
|
|
|
protected NodeControlBase(NodeControlViewModelBase viewModelBase)
|
2024-08-05 10:11:58 +08:00
|
|
|
|
{
|
2024-09-12 20:32:54 +08:00
|
|
|
|
ViewModel = viewModelBase;
|
2024-08-05 19:43:57 +08:00
|
|
|
|
this.Background = Brushes.Transparent;
|
2024-10-22 00:13:13 +08:00
|
|
|
|
this.DataContext = viewModelBase;
|
2024-10-23 19:22:27 +08:00
|
|
|
|
SetBinding();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-12-26 00:26:50 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 放置在某个节点容器中
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public void PlaceToContainer(INodeContainerControl nodeContainerControl)
|
|
|
|
|
|
{
|
|
|
|
|
|
this.nodeContainerControl = nodeContainerControl;
|
|
|
|
|
|
NodeCanvas.Children.Remove(this); // 从画布上移除
|
|
|
|
|
|
nodeContainerControl.PlaceNode(this);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 从某个节点容器取出
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public void TakeOutContainer()
|
|
|
|
|
|
{
|
|
|
|
|
|
nodeContainerControl.TakeOutNode(this);
|
|
|
|
|
|
NodeCanvas.Children.Add(this); // 重新添加到画布上
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-24 23:32:43 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 添加与该节点有关的连接后,记录下来
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="connection"></param>
|
|
|
|
|
|
public void AddCnnection(ConnectionControl connection)
|
|
|
|
|
|
{
|
|
|
|
|
|
connectionControls.Add(connection);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 删除了连接之后,还需要从节点中的记录移除
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="connection"></param>
|
2024-11-04 23:30:52 +08:00
|
|
|
|
public void RemoveConnection(ConnectionControl connection)
|
2024-10-24 23:32:43 +08:00
|
|
|
|
{
|
|
|
|
|
|
connectionControls.Remove(connection);
|
|
|
|
|
|
connection.Remote();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2024-11-04 23:30:52 +08:00
|
|
|
|
/// 删除所有连接
|
2024-10-24 23:32:43 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
public void RemoveAllConection()
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (var connection in this.connectionControls)
|
|
|
|
|
|
{
|
2024-11-04 23:30:52 +08:00
|
|
|
|
connection.Remote();
|
2024-10-24 23:32:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-11-04 23:30:52 +08:00
|
|
|
|
|
2024-10-24 23:32:43 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 更新与该节点有关的数据
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public void UpdateLocationConnections()
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (var connection in this.connectionControls)
|
|
|
|
|
|
{
|
|
|
|
|
|
connection.RefreshLine(); // 主动更新连线位置
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-23 19:22:27 +08:00
|
|
|
|
|
2024-10-24 23:32:43 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 设置绑定:
|
|
|
|
|
|
/// Canvas.X and Y : 画布位置
|
|
|
|
|
|
/// </summary>
|
2024-10-23 19:22:27 +08:00
|
|
|
|
public void SetBinding()
|
|
|
|
|
|
{
|
|
|
|
|
|
// 绑定 Canvas.Left
|
|
|
|
|
|
Binding leftBinding = new Binding("X")
|
|
|
|
|
|
{
|
|
|
|
|
|
Source = ViewModel.NodeModel.Position, // 如果 X 属性在当前 DataContext 中
|
|
|
|
|
|
Mode = BindingMode.TwoWay
|
|
|
|
|
|
};
|
|
|
|
|
|
BindingOperations.SetBinding(this, Canvas.LeftProperty, leftBinding);
|
|
|
|
|
|
|
|
|
|
|
|
// 绑定 Canvas.Top
|
|
|
|
|
|
Binding topBinding = new Binding("Y")
|
|
|
|
|
|
{
|
|
|
|
|
|
Source = ViewModel.NodeModel.Position, // 如果 Y 属性在当前 DataContext 中
|
|
|
|
|
|
Mode = BindingMode.TwoWay
|
|
|
|
|
|
};
|
|
|
|
|
|
BindingOperations.SetBinding(this, Canvas.TopProperty, topBinding);
|
2024-08-05 10:11:58 +08:00
|
|
|
|
}
|
2024-10-23 19:22:27 +08:00
|
|
|
|
|
2024-10-28 10:25:57 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 穿透视觉树获取指定类型的第一个元素
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
|
/// <param name="parent"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
protected T FindVisualChild<T>(DependencyObject parent) where T : DependencyObject
|
|
|
|
|
|
{
|
|
|
|
|
|
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
var child = VisualTreeHelper.GetChild(parent, i);
|
|
|
|
|
|
if (child is T typedChild)
|
|
|
|
|
|
{
|
|
|
|
|
|
return typedChild;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var childOfChild = FindVisualChild<T>(child);
|
|
|
|
|
|
if (childOfChild != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return childOfChild;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
2024-10-23 19:22:27 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-08-05 10:11:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-10-15 21:56:09 +08:00
|
|
|
|
|
|
|
|
|
|
//public class FLowNodeObObservableCollection<T> : ObservableCollection<T>
|
|
|
|
|
|
//{
|
2024-08-05 10:11:58 +08:00
|
|
|
|
|
2024-10-15 21:56:09 +08:00
|
|
|
|
// public void AddRange(IEnumerable<T> items)
|
|
|
|
|
|
// {
|
|
|
|
|
|
// foreach (var item in items)
|
|
|
|
|
|
// {
|
|
|
|
|
|
// this.Items.Add(item);
|
|
|
|
|
|
// }
|
|
|
|
|
|
// OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add));
|
|
|
|
|
|
// }
|
|
|
|
|
|
//}
|
2024-08-05 10:11:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|