mirror of
https://gitee.com/akwkevin/aistudio.-wpf.-diagram
synced 2026-03-03 00:00:57 +08:00
253 lines
11 KiB
C#
253 lines
11 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace AIStudio.Wpf.DiagramDesigner
|
||
{
|
||
public static class BlockDesignerItemViewModelHelper
|
||
{
|
||
/// <summary>
|
||
/// type=0最近且没有依附;=1
|
||
/// </summary>
|
||
/// <param name="blockDesignerItemViewModel"></param>
|
||
/// <param name="type"></param>
|
||
/// <returns></returns>
|
||
public static Tuple<FullyCreatedConnectorInfo, FullyCreatedConnectorInfo> FindNearPortToAttachTo(this IDiagramViewModel diagramViewModel, BlockDesignerItemTempLink blockDesignerItemTempLink, bool isExist)
|
||
{
|
||
if (blockDesignerItemTempLink == null || blockDesignerItemTempLink.Items == null || blockDesignerItemTempLink.Items.Count == 0)
|
||
return new Tuple<FullyCreatedConnectorInfo, FullyCreatedConnectorInfo>(null, null);
|
||
|
||
List<BlockDesignerItemViewModel> items;
|
||
if (isExist == false)
|
||
{
|
||
items = diagramViewModel.Items.OfType<BlockDesignerItemViewModel>().Where(p => !blockDesignerItemTempLink.Items.Contains(p)).ToList();
|
||
}
|
||
else
|
||
{
|
||
items = diagramViewModel.Items.OfType<BlockDesignerItemViewModel>().Where(p => !blockDesignerItemTempLink.Items.Contains(p)).ToList();
|
||
}
|
||
|
||
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)
|
||
{
|
||
diagramViewModel.Items.OfType<BlockDesignerItemViewModel>().ToList().SelectMany(n => n.GetAllContainers()).Where(p => p.BeAttachTo == true || p.DisableAttachTo == true).ToList()
|
||
.ForEach(p => {
|
||
p.DisableAttachTo = false;
|
||
p.BeAttachTo = false;
|
||
});
|
||
}
|
||
|
||
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();
|
||
|
||
foreach (var container in items.SelectMany(n => n.Containers))
|
||
{
|
||
if (container.GetBounds().IntersectsWith(blockDesignerItemTempLink.GetBounds())) //如果两个位置相交
|
||
{
|
||
var innerport = container.GetAllContainers(container.Children, false).Where(p => p.GetBounds().IntersectsWith(blockDesignerItemTempLink.GetBounds())).OrderByDescending(p => p.ContainerLevel).FirstOrDefault();
|
||
if (innerport != null)
|
||
{
|
||
innerport.DataItem.ShowConnectors = true;
|
||
if (innerport.CanAttachTo(blockDesignerItemTempLink.Items.FirstOrDefault()) == true)
|
||
{
|
||
if (innerport.OnlyOneChild)
|
||
{
|
||
innerport.BeAttachTo = true;
|
||
return innerport;
|
||
}
|
||
else
|
||
{
|
||
innerport.BeAttachTo = true;
|
||
return innerport;
|
||
}
|
||
}
|
||
//else
|
||
//{
|
||
// innerport.DisableAttachTo = true;
|
||
// return null;
|
||
//}
|
||
|
||
}
|
||
else
|
||
{
|
||
container.DataItem.ShowConnectors = true;
|
||
if (container.CanAttachTo(blockDesignerItemTempLink.Items.FirstOrDefault()) == true)
|
||
{
|
||
container.BeAttachTo = true;
|
||
return container;
|
||
}
|
||
//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;
|
||
}
|
||
diagramViewModel.FindNearPortToAttachTo(item, false);
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
|
||
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)
|
||
{
|
||
diagramViewModel.AddChildCommand.Execute(new BlockContainerPara() { Item = container.DataItem, Child = item.Items.FirstOrDefault(), Container = container });//待完善
|
||
container.BeAttachTo = false;
|
||
container.DisableAttachTo = false;
|
||
continue;
|
||
}
|
||
|
||
var portTuple = diagramViewModel.FindNearPortToAttachTo(item, true);
|
||
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
|
||
}
|
||
}
|