2023-06-20 13:46:26 +08:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
using System.Threading.Tasks;
|
2023-06-27 22:57:08 +08:00
|
|
|
|
using System.Windows;
|
2023-06-20 13:46:26 +08:00
|
|
|
|
|
|
|
|
|
|
namespace AIStudio.Wpf.DiagramDesigner
|
|
|
|
|
|
{
|
|
|
|
|
|
public static class BlockDesignerItemViewModelHelper
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// type=0最近且没有依附;=1
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="blockDesignerItemViewModel"></param>
|
|
|
|
|
|
/// <param name="type"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2023-06-27 22:57:08 +08:00
|
|
|
|
public static Tuple<FullyCreatedConnectorInfo, FullyCreatedConnectorInfo> FindNearPortToAttachTo(this IDiagramViewModel diagramViewModel, BlockDesignerItemTempLink blockDesignerItemTempLink)
|
2023-06-20 13:46:26 +08:00
|
|
|
|
{
|
|
|
|
|
|
if (blockDesignerItemTempLink == null || blockDesignerItemTempLink.Items == null || blockDesignerItemTempLink.Items.Count == 0)
|
|
|
|
|
|
return new Tuple<FullyCreatedConnectorInfo, FullyCreatedConnectorInfo>(null, null);
|
|
|
|
|
|
|
2023-06-27 22:57:08 +08:00
|
|
|
|
List<BlockDesignerItemViewModel> items = diagramViewModel.Items.OfType<BlockDesignerItemViewModel>().Where(p => !blockDesignerItemTempLink.Items.Contains(p)).ToList();
|
|
|
|
|
|
|
|
|
|
|
|
return diagramViewModel.FindNearPortToAttachTo(items, blockDesignerItemTempLink);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static Tuple<FullyCreatedConnectorInfo, FullyCreatedConnectorInfo> FindNearPortToAttachTo(this IDiagramViewModel diagramViewModel, List<BlockDesignerItemViewModel> items, BlockDesignerItemTempLink blockDesignerItemTempLink)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (blockDesignerItemTempLink == null || blockDesignerItemTempLink.Items == null || blockDesignerItemTempLink.Items.Count == 0)
|
|
|
|
|
|
return new Tuple<FullyCreatedConnectorInfo, FullyCreatedConnectorInfo>(null, null);
|
2023-06-20 13:46:26 +08:00
|
|
|
|
|
|
|
|
|
|
FullyCreatedConnectorInfo parent = null;
|
|
|
|
|
|
FullyCreatedConnectorInfo next = null;
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var port in items.SelectMany(n => n.Connectors).OfType<BlockConnectorInfo>())
|
|
|
|
|
|
{
|
|
|
|
|
|
//已经被连接的不允许在顶部吸附了
|
|
|
|
|
|
if ((port.Orientation == ConnectorOrientation.Top || port.Orientation == ConnectorOrientation.Left) && port.DataItem.Prev != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var port2 in blockDesignerItemTempLink.Connectors)
|
|
|
|
|
|
{
|
|
|
|
|
|
//parent
|
|
|
|
|
|
if (parent == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
if ((port.Orientation == ConnectorOrientation.Right && port2.Orientation == ConnectorOrientation.Left)
|
|
|
|
|
|
|| (port.Orientation == ConnectorOrientation.Bottom && port2.Orientation == ConnectorOrientation.Top))
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
if (port.Position.DistanceTo(port2.Position) < diagramViewModel.DiagramOption.SnappingOption.SnappingRadius)
|
|
|
|
|
|
{
|
|
|
|
|
|
port.DataItem.ShowConnectors = true;
|
|
|
|
|
|
if (port2.CanAttachTo(port) == true)
|
|
|
|
|
|
{
|
|
|
|
|
|
port.BeAttachTo = true;
|
|
|
|
|
|
parent = port;
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
port.DisableAttachTo = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//next
|
|
|
|
|
|
if (next == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
if ((port.Orientation == ConnectorOrientation.Left && port2.Orientation == ConnectorOrientation.Right)
|
|
|
|
|
|
|| (port.Orientation == ConnectorOrientation.Top && port2.Orientation == ConnectorOrientation.Bottom))
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
if (port.Position.DistanceTo(port2.Position) < diagramViewModel.DiagramOption.SnappingOption.SnappingRadius)
|
|
|
|
|
|
{
|
|
|
|
|
|
port.DataItem.ShowConnectors = true;
|
|
|
|
|
|
if (port2.CanAttachTo(port) == true)
|
|
|
|
|
|
{
|
|
|
|
|
|
port.BeAttachTo = true;
|
|
|
|
|
|
next = port;
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
port.DisableAttachTo = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return new Tuple<FullyCreatedConnectorInfo, FullyCreatedConnectorInfo>(parent, next);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void ClearNearContain(this IDiagramViewModel diagramViewModel)
|
|
|
|
|
|
{
|
2023-06-27 22:57:08 +08:00
|
|
|
|
diagramViewModel.Items.OfType<BlockDesignerItemViewModel>().ToList().SelectMany(n => n.GetAllContainers()).ToList()
|
2023-06-20 13:46:26 +08:00
|
|
|
|
.ForEach(p => {
|
|
|
|
|
|
p.DisableAttachTo = false;
|
|
|
|
|
|
p.BeAttachTo = false;
|
2023-06-27 22:57:08 +08:00
|
|
|
|
p.Children.SelectMany(n => n.Connectors).Where(q => q.BeAttachTo == true || q.DisableAttachTo == true).ToList().ForEach(q => {
|
|
|
|
|
|
q.DisableAttachTo = false;
|
|
|
|
|
|
q.BeAttachTo = false;
|
|
|
|
|
|
});
|
2023-06-20 13:46:26 +08:00
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static BlockItemsContainerInfo FindNearContainerToAttachTo(this IDiagramViewModel diagramViewModel, BlockDesignerItemTempLink blockDesignerItemTempLink)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (blockDesignerItemTempLink == null || blockDesignerItemTempLink.Items == null || blockDesignerItemTempLink.Items.Count == 0)
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
|
|
List<BlockDesignerItemViewModel> items;
|
|
|
|
|
|
|
|
|
|
|
|
items = diagramViewModel.Items.OfType<BlockDesignerItemViewModel>().Where(p => !blockDesignerItemTempLink.Items.Contains(p)).ToList();
|
|
|
|
|
|
|
2023-06-26 16:45:48 +08:00
|
|
|
|
foreach (var container in items.SelectMany(n => n.Containers))
|
2023-06-20 13:46:26 +08:00
|
|
|
|
{
|
2023-06-26 16:45:48 +08:00
|
|
|
|
if (container.GetBounds().IntersectsWith(blockDesignerItemTempLink.GetBounds())) //如果两个位置相交
|
2023-06-20 13:46:26 +08:00
|
|
|
|
{
|
2023-06-26 16:45:48 +08:00
|
|
|
|
var innerport = container.GetAllContainers(container.Children, false).Where(p => p.GetBounds().IntersectsWith(blockDesignerItemTempLink.GetBounds())).OrderByDescending(p => p.ContainerLevel).FirstOrDefault();
|
2023-06-20 13:46:26 +08:00
|
|
|
|
if (innerport != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
innerport.DataItem.ShowConnectors = true;
|
|
|
|
|
|
if (innerport.CanAttachTo(blockDesignerItemTempLink.Items.FirstOrDefault()) == true)
|
|
|
|
|
|
{
|
2023-06-27 19:58:08 +08:00
|
|
|
|
if (innerport.OnlyOneChild || innerport.Children.Count == 0)
|
2023-06-26 16:45:48 +08:00
|
|
|
|
{
|
|
|
|
|
|
innerport.BeAttachTo = true;
|
|
|
|
|
|
return innerport;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2023-06-27 22:57:08 +08:00
|
|
|
|
diagramViewModel.FindNearPortToAttachTo(innerport.Children.ToList(), blockDesignerItemTempLink);
|
2023-06-26 16:45:48 +08:00
|
|
|
|
return innerport;
|
|
|
|
|
|
}
|
2023-06-20 13:46:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
//else
|
|
|
|
|
|
//{
|
|
|
|
|
|
// innerport.DisableAttachTo = true;
|
|
|
|
|
|
// return null;
|
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2023-06-26 16:45:48 +08:00
|
|
|
|
container.DataItem.ShowConnectors = true;
|
|
|
|
|
|
if (container.CanAttachTo(blockDesignerItemTempLink.Items.FirstOrDefault()) == true)
|
2023-06-20 13:46:26 +08:00
|
|
|
|
{
|
2023-06-27 22:57:08 +08:00
|
|
|
|
if (container.OnlyOneChild || container.Children.Count == 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
container.BeAttachTo = true;
|
|
|
|
|
|
return container;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
diagramViewModel.FindNearPortToAttachTo(container.Children.ToList(), blockDesignerItemTempLink);
|
|
|
|
|
|
return container;
|
|
|
|
|
|
}
|
2023-06-20 13:46:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
//else
|
|
|
|
|
|
//{
|
|
|
|
|
|
// port.DisableAttachTo = true;
|
|
|
|
|
|
// return null;
|
|
|
|
|
|
//}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#region Block拖拽预览-拖拽完成
|
|
|
|
|
|
public static void PreviewNearBlock(this IBlockDiagramViewModel diagramViewModel, List<BlockDesignerItemViewModel> blocks)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (diagramViewModel == null)
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
if (blocks.Any())
|
|
|
|
|
|
{
|
|
|
|
|
|
diagramViewModel.ClearNearPort();
|
|
|
|
|
|
diagramViewModel.ClearNearContain();
|
|
|
|
|
|
var links = BlockDesignerItemTempLink.Build(blocks);
|
|
|
|
|
|
foreach (BlockDesignerItemTempLink item in links)
|
|
|
|
|
|
{
|
|
|
|
|
|
var container = diagramViewModel.FindNearContainerToAttachTo(item);
|
|
|
|
|
|
if (container != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
2023-06-27 22:57:08 +08:00
|
|
|
|
diagramViewModel.FindNearPortToAttachTo(item);
|
2023-06-20 13:46:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void FinishNearBlock(this IBlockDiagramViewModel diagramViewModel, List<BlockDesignerItemViewModel> blocks)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (diagramViewModel == null)
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
if (blocks.Any())
|
|
|
|
|
|
{
|
|
|
|
|
|
diagramViewModel.ClearNearPort();
|
|
|
|
|
|
diagramViewModel.ClearNearContain();
|
|
|
|
|
|
var links = BlockDesignerItemTempLink.Build(blocks);
|
|
|
|
|
|
|
|
|
|
|
|
foreach (BlockDesignerItemTempLink item in links)
|
|
|
|
|
|
{
|
|
|
|
|
|
var container = diagramViewModel.FindNearContainerToAttachTo(item);
|
|
|
|
|
|
if (container != null)
|
|
|
|
|
|
{
|
2023-06-27 22:57:08 +08:00
|
|
|
|
int index = 0;
|
|
|
|
|
|
var child = container.Children.FirstOrDefault(p => p.Connectors.Any(q => q.BeAttachTo == true));
|
|
|
|
|
|
if (child != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
index = container.Children.IndexOf(child);
|
|
|
|
|
|
if (child.RightConnector?.BeAttachTo == true || child.BottomConnector?.BeAttachTo == true)
|
|
|
|
|
|
{
|
|
|
|
|
|
index ++;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
diagramViewModel.InsertChildCommand.Execute(new BlockContainerPara() { Item = container.DataItem, Child = item.Items.FirstOrDefault(), Container = container, Index = index });//待完善
|
|
|
|
|
|
|
2023-06-20 13:46:26 +08:00
|
|
|
|
container.BeAttachTo = false;
|
|
|
|
|
|
container.DisableAttachTo = false;
|
2023-06-27 22:57:08 +08:00
|
|
|
|
container.Children.SelectMany(n => n.Connectors).Where(q => q.BeAttachTo == true || q.DisableAttachTo == true).ToList().ForEach(q => {
|
|
|
|
|
|
q.DisableAttachTo = false;
|
|
|
|
|
|
q.BeAttachTo = false;
|
|
|
|
|
|
});
|
2023-06-20 13:46:26 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-06-27 22:57:08 +08:00
|
|
|
|
var portTuple = diagramViewModel.FindNearPortToAttachTo(item);
|
2023-06-20 13:46:26 +08:00
|
|
|
|
var portParent = portTuple.Item1;
|
|
|
|
|
|
var portNext = portTuple.Item2;
|
|
|
|
|
|
|
|
|
|
|
|
if (portParent != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
diagramViewModel.AddNextCommand.Execute(new BlockNextPara() { Item = portParent.DataItem as BlockDesignerItemViewModel, Next = item.Items.FirstOrDefault() });
|
|
|
|
|
|
portParent.BeAttachTo = false;
|
|
|
|
|
|
portParent.DisableAttachTo = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
if (item.Items.FirstOrDefault().Parent != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
diagramViewModel.RemoveNextCommand.Execute(new BlockNextPara() { Item = item.Items.FirstOrDefault().Parent as BlockDesignerItemViewModel, Next = (item.Items.FirstOrDefault().Parent as BlockDesignerItemViewModel)?.Next });
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (portNext != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
diagramViewModel.AddNextCommand.Execute(new BlockNextPara() { Item = item.Items.LastOrDefault(), Next = portNext.DataItem as BlockDesignerItemViewModel });
|
|
|
|
|
|
portNext.BeAttachTo = false;
|
|
|
|
|
|
portNext.DisableAttachTo = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
if (item.Items.LastOrDefault().Next != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
diagramViewModel.RemoveNextCommand.Execute(new BlockNextPara() { Item = item.Items.LastOrDefault(), Next = item.Items.LastOrDefault()?.Next });
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|