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-07-01 21:38:23 +08:00
|
|
|
|
using System.Windows.Documents;
|
2023-06-20 13:46:26 +08:00
|
|
|
|
|
|
|
|
|
|
namespace AIStudio.Wpf.DiagramDesigner
|
|
|
|
|
|
{
|
2023-06-28 12:06:58 +08:00
|
|
|
|
public static partial class BlockDesignerItemViewModelHelper
|
2023-06-20 13:46:26 +08:00
|
|
|
|
{
|
|
|
|
|
|
/// <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;
|
|
|
|
|
|
|
2023-07-01 21:38:23 +08:00
|
|
|
|
foreach (var item in items)
|
2023-06-20 13:46:26 +08:00
|
|
|
|
{
|
2023-07-01 21:38:23 +08:00
|
|
|
|
bool success = false;
|
|
|
|
|
|
foreach (var port in item.Connectors.OfType<BlockConnectorInfo>())
|
2023-06-20 13:46:26 +08:00
|
|
|
|
{
|
2023-07-01 21:38:23 +08:00
|
|
|
|
//已经被连接的不允许在顶部吸附了
|
|
|
|
|
|
if ((port.Orientation == ConnectorOrientation.Top || port.Orientation == ConnectorOrientation.Left) && port.DataItem.Prev != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
2023-06-20 13:46:26 +08:00
|
|
|
|
|
2023-07-01 21:38:23 +08:00
|
|
|
|
foreach (var port2 in blockDesignerItemTempLink.Connectors.OfType<BlockConnectorInfo>())
|
2023-06-20 13:46:26 +08:00
|
|
|
|
{
|
2023-07-01 21:38:23 +08:00
|
|
|
|
//parent
|
|
|
|
|
|
if (parent == null)
|
2023-06-20 13:46:26 +08:00
|
|
|
|
{
|
2023-07-01 21:38:23 +08:00
|
|
|
|
if ((port.Orientation == ConnectorOrientation.Right && port2.Orientation == ConnectorOrientation.Left)
|
|
|
|
|
|
|| (port.Orientation == ConnectorOrientation.Bottom && port2.Orientation == ConnectorOrientation.Top))
|
2023-06-20 13:46:26 +08:00
|
|
|
|
{
|
2023-07-01 21:38:23 +08:00
|
|
|
|
|
|
|
|
|
|
if (port.DistanceTo(port2) < diagramViewModel.DiagramOption.SnappingOption.BlockSnappingRadius)
|
2023-06-20 13:46:26 +08:00
|
|
|
|
{
|
2023-07-01 21:38:23 +08:00
|
|
|
|
port.DataItem.ShowConnectors = true;
|
|
|
|
|
|
if (port2.CanAttachTo(port) == true)
|
|
|
|
|
|
{
|
|
|
|
|
|
diagramViewModel.AddAttachTo(port, true);
|
|
|
|
|
|
parent = port;
|
|
|
|
|
|
success = true;
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
diagramViewModel.AddAttachTo(port, false);
|
|
|
|
|
|
}
|
2023-06-20 13:46:26 +08:00
|
|
|
|
|
2023-07-01 21:38:23 +08:00
|
|
|
|
}
|
2023-06-20 13:46:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-07-01 21:38:23 +08:00
|
|
|
|
//next
|
|
|
|
|
|
if (next == null)
|
2023-06-20 13:46:26 +08:00
|
|
|
|
{
|
2023-07-01 21:38:23 +08:00
|
|
|
|
if ((port.Orientation == ConnectorOrientation.Left && port2.Orientation == ConnectorOrientation.Right)
|
|
|
|
|
|
|| (port.Orientation == ConnectorOrientation.Top && port2.Orientation == ConnectorOrientation.Bottom))
|
2023-06-20 13:46:26 +08:00
|
|
|
|
{
|
2023-07-01 21:38:23 +08:00
|
|
|
|
|
|
|
|
|
|
if (port.Position.DistanceTo(port2.Position) < diagramViewModel.DiagramOption.SnappingOption.BlockSnappingRadius)
|
2023-06-20 13:46:26 +08:00
|
|
|
|
{
|
2023-07-01 21:38:23 +08:00
|
|
|
|
port.DataItem.ShowConnectors = true;
|
|
|
|
|
|
if (port2.CanAttachTo(port) == true)
|
|
|
|
|
|
{
|
|
|
|
|
|
diagramViewModel.AddAttachTo(port, true);
|
|
|
|
|
|
next = port;
|
|
|
|
|
|
success = true;
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
diagramViewModel.AddAttachTo(port, false);
|
|
|
|
|
|
}
|
2023-06-20 13:46:26 +08:00
|
|
|
|
|
2023-07-01 21:38:23 +08:00
|
|
|
|
}
|
2023-06-20 13:46:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2023-07-01 21:38:23 +08:00
|
|
|
|
|
|
|
|
|
|
if (success)
|
|
|
|
|
|
{
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
2023-06-20 13:46:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return new Tuple<FullyCreatedConnectorInfo, FullyCreatedConnectorInfo>(parent, next);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
2023-06-28 12:06:58 +08:00
|
|
|
|
{
|
2023-06-20 13:46:26 +08:00
|
|
|
|
if (innerport.CanAttachTo(blockDesignerItemTempLink.Items.FirstOrDefault()) == true)
|
|
|
|
|
|
{
|
2023-06-28 12:06:58 +08:00
|
|
|
|
innerport.DataItem.ShowConnectors = true;
|
2023-06-27 19:58:08 +08:00
|
|
|
|
if (innerport.OnlyOneChild || innerport.Children.Count == 0)
|
2023-06-26 16:45:48 +08:00
|
|
|
|
{
|
2023-06-28 12:06:58 +08:00
|
|
|
|
diagramViewModel.AddAttachTo(innerport, true);
|
2023-06-26 16:45:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2023-06-28 12:06:58 +08:00
|
|
|
|
diagramViewModel.FindNearPortToAttachTo(innerport.Children.ToList(), blockDesignerItemTempLink);
|
2023-06-26 16:45:48 +08:00
|
|
|
|
}
|
2023-06-28 12:06:58 +08:00
|
|
|
|
return innerport;
|
2023-06-20 13:46:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
//else
|
|
|
|
|
|
//{
|
|
|
|
|
|
// innerport.DisableAttachTo = true;
|
|
|
|
|
|
// return null;
|
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2023-06-28 12:06:58 +08:00
|
|
|
|
|
2023-06-26 16:45:48 +08:00
|
|
|
|
if (container.CanAttachTo(blockDesignerItemTempLink.Items.FirstOrDefault()) == true)
|
2023-06-20 13:46:26 +08:00
|
|
|
|
{
|
2023-06-28 12:06:58 +08:00
|
|
|
|
container.DataItem.ShowConnectors = true;
|
2023-06-27 22:57:08 +08:00
|
|
|
|
if (container.OnlyOneChild || container.Children.Count == 0)
|
|
|
|
|
|
{
|
2023-06-28 12:06:58 +08:00
|
|
|
|
diagramViewModel.AddAttachTo(container, true);
|
2023-06-27 22:57:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2023-06-28 12:06:58 +08:00
|
|
|
|
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())
|
|
|
|
|
|
{
|
2023-06-28 12:06:58 +08:00
|
|
|
|
diagramViewModel.ClearAttachTo();
|
2023-06-20 13:46:26 +08:00
|
|
|
|
var links = BlockDesignerItemTempLink.Build(blocks);
|
2023-07-01 21:38:23 +08:00
|
|
|
|
|
|
|
|
|
|
blocks.ToList().ForEach(p => {
|
|
|
|
|
|
p.ZIndex = int.MaxValue;
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2023-06-20 13:46:26 +08:00
|
|
|
|
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())
|
2023-06-28 12:06:58 +08:00
|
|
|
|
{
|
2023-06-20 13:46:26 +08:00
|
|
|
|
var links = BlockDesignerItemTempLink.Build(blocks);
|
|
|
|
|
|
|
2023-07-01 21:38:23 +08:00
|
|
|
|
blocks.ToList().ForEach(p => {
|
|
|
|
|
|
p.ZIndex = diagramViewModel.Items.Any() ? diagramViewModel.Items.Where(q => q.ZIndex != int.MaxValue).Max(r => r.ZIndex) + 1 : 0;
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2023-06-20 13:46:26 +08:00
|
|
|
|
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 ++;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2023-06-28 12:06:58 +08:00
|
|
|
|
diagramViewModel.InsertChildCommand.Execute(new BlockContainerPara() { Item = container.DataItem, Child = item.Items.FirstOrDefault(), Container = container, Index = index });
|
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 });
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2023-07-01 21:38:23 +08:00
|
|
|
|
|
2023-06-28 12:06:58 +08:00
|
|
|
|
diagramViewModel.ClearAttachTo();
|
2023-06-20 13:46:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|