mirror of
https://gitee.com/akwkevin/aistudio.-wpf.-diagram
synced 2026-04-06 09:16:37 +08:00
block
This commit is contained in:
@@ -83,12 +83,34 @@ namespace AIStudio.Wpf.DiagramDesigner
|
||||
|
||||
public double DistanceTo(BlockConnectorInfo port)
|
||||
{
|
||||
var leftleftdistance = LeftPosition.DistanceTo(port.LeftPosition);
|
||||
var leftrightdistance = LeftPosition.DistanceTo(port.RightPosition);
|
||||
var rightleftdistance = RightPosition.DistanceTo(port.LeftPosition);
|
||||
var rightrightdistance = RightPosition.DistanceTo(port.RightPosition);
|
||||
PointBase p0 = port.LeftPosition;
|
||||
PointBase p1 = port.RightPosition;
|
||||
PointBase p2 = LeftPosition;
|
||||
PointBase p3 = RightPosition;
|
||||
var distance1 = DistanceTo(p0, p1, p2);
|
||||
var distance2 = DistanceTo(p0, p1, p3);
|
||||
var distance3 = DistanceTo(p2, p3, p0);
|
||||
var distance4 = DistanceTo(p2, p3, p1);
|
||||
|
||||
return Math.Min(Math.Min(leftleftdistance, leftrightdistance), Math.Min(rightleftdistance, rightrightdistance));
|
||||
return Math.Min(Math.Min(distance1, distance2), Math.Min(distance3, distance4));
|
||||
}
|
||||
|
||||
double DistanceTo(PointBase A, PointBase B, PointBase P) //点P到线段AB的最短距离
|
||||
{
|
||||
double r = ((P.X - A.X) * (B.X - A.X) + (P.Y - A.Y) * (B.Y - A.Y)) / DistanceToPow(A, B);
|
||||
if (r <= 0) return Math.Sqrt(DistanceToPow(A, P));
|
||||
else if (r >= 1) return Math.Sqrt(DistanceToPow(B, P));
|
||||
else
|
||||
{
|
||||
double AC = r * Math.Sqrt(DistanceToPow(A, B));
|
||||
return Math.Sqrt(DistanceToPow(A, P) - AC * AC);
|
||||
}
|
||||
}
|
||||
|
||||
double DistanceToPow(PointBase a, PointBase b) //点a、b距离的平方
|
||||
{
|
||||
return (a.X - b.X) * (a.X - b.X) + (a.Y - b.Y) * (a.Y - b.Y);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -776,12 +776,12 @@ namespace AIStudio.Wpf.DiagramDesigner
|
||||
|
||||
public double GetItemWidth()
|
||||
{
|
||||
return double.IsNaN(ItemWidth) ? ActualItemWidth : ItemWidth;
|
||||
return double.IsNaN(ItemWidth) ? Math.Max(ActualItemWidth, MinItemWidth) : ItemWidth;
|
||||
}
|
||||
|
||||
public double GetItemHeight()
|
||||
{
|
||||
return double.IsNaN(ItemHeight) ? ActualItemHeight : ItemHeight;
|
||||
return double.IsNaN(ItemHeight) ? Math.Max(ActualItemHeight, MinItemHeight) : ItemHeight;
|
||||
}
|
||||
|
||||
public IShape GetShape() => ShapeDefiner(this);
|
||||
|
||||
@@ -130,9 +130,34 @@ namespace AIStudio.Wpf.DiagramDesigner
|
||||
{
|
||||
this.RemoveChild(oldchildren, container);
|
||||
}
|
||||
Root.Items.Remove(child);
|
||||
if (child.Prev != null)
|
||||
{
|
||||
child.Prev.RemoveNext();
|
||||
}
|
||||
if (child.Next != null)
|
||||
{
|
||||
child.RemoveNext();
|
||||
}
|
||||
container.InsertChild(child, index);
|
||||
}
|
||||
else
|
||||
{
|
||||
var list = child.GetNexts(true);
|
||||
list.Reverse();
|
||||
list.ForEach(p => {
|
||||
Root.Items.Remove(p);
|
||||
if (p.Prev != null)
|
||||
{
|
||||
p.Prev.RemoveNext();
|
||||
}
|
||||
if (p.Next != null)
|
||||
{
|
||||
p.RemoveNext();
|
||||
}
|
||||
container.InsertChild(p, index);
|
||||
});
|
||||
}
|
||||
Root.Items.Remove(child);
|
||||
container.InsertChild(child, index);
|
||||
|
||||
child.RemoveFromSelection();
|
||||
this.GetRootContainItem.AddToSelection(true, true);
|
||||
@@ -237,28 +262,44 @@ namespace AIStudio.Wpf.DiagramDesigner
|
||||
|
||||
public BlockDesignerItemViewModel GetFirst()
|
||||
{
|
||||
var parent = this.Next;
|
||||
if (parent != null)
|
||||
var parent = this.Prev;
|
||||
while (parent?.Prev != null)
|
||||
{
|
||||
while (parent.Prev != null)
|
||||
{
|
||||
parent = Prev;
|
||||
}
|
||||
parent = parent.Prev;
|
||||
}
|
||||
|
||||
return parent;
|
||||
}
|
||||
|
||||
public BlockDesignerItemViewModel GetLast()
|
||||
{
|
||||
var next = this;
|
||||
if (next != null)
|
||||
while (next.Next != null)
|
||||
{
|
||||
while (next.Next != null)
|
||||
next = next.Next;
|
||||
}
|
||||
|
||||
return next;
|
||||
}
|
||||
|
||||
public List<BlockDesignerItemViewModel> GetNexts(bool self)
|
||||
{
|
||||
List<BlockDesignerItemViewModel> blockDesignerItemViewModels = new List<BlockDesignerItemViewModel>();
|
||||
if (self)
|
||||
{
|
||||
blockDesignerItemViewModels.Add(this);
|
||||
}
|
||||
var next = this;
|
||||
while (next != null)
|
||||
{
|
||||
next = next.Next;
|
||||
if (next != null)
|
||||
{
|
||||
next = next.Next;
|
||||
blockDesignerItemViewModels.Add(next);
|
||||
}
|
||||
}
|
||||
return next;
|
||||
|
||||
return blockDesignerItemViewModels;
|
||||
}
|
||||
|
||||
public void AddContainer(BlockItemsContainerInfo container)
|
||||
@@ -404,12 +445,12 @@ namespace AIStudio.Wpf.DiagramDesigner
|
||||
{
|
||||
List<BlockDesignerItemTempLink> links = new List<BlockDesignerItemTempLink>();
|
||||
foreach (var block in blocks.OrderBy(p => p.BlockLevel).ToList())
|
||||
{
|
||||
{
|
||||
bool success = false;
|
||||
foreach (var link in links)
|
||||
{
|
||||
if (link.Items.LastOrDefault() == block.Prev)
|
||||
{
|
||||
{
|
||||
link.Items.Add(block);
|
||||
success = true;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
@@ -34,30 +35,32 @@ namespace AIStudio.Wpf.DiagramDesigner
|
||||
FullyCreatedConnectorInfo parent = null;
|
||||
FullyCreatedConnectorInfo next = null;
|
||||
|
||||
foreach (var item in items)
|
||||
|
||||
foreach (var port2 in blockDesignerItemTempLink.Connectors.OfType<BlockConnectorInfo>())
|
||||
{
|
||||
bool success = false;
|
||||
foreach (var port in item.Connectors.OfType<BlockConnectorInfo>())
|
||||
{
|
||||
//已经被连接的不允许在顶部吸附了
|
||||
if ((port.Orientation == ConnectorOrientation.Top || port.Orientation == ConnectorOrientation.Left) && port.DataItem.Prev != null)
|
||||
foreach (var item in items)
|
||||
{
|
||||
foreach (var port in item.Connectors.OfType<BlockConnectorInfo>())
|
||||
{
|
||||
continue;
|
||||
}
|
||||
//已经被连接的不允许在顶部吸附了
|
||||
if ((port.Orientation == ConnectorOrientation.Top || port.Orientation == ConnectorOrientation.Left) && port.DataItem.Prev != null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach (var port2 in blockDesignerItemTempLink.Connectors.OfType<BlockConnectorInfo>())
|
||||
{
|
||||
//parent
|
||||
if (parent == null)
|
||||
{
|
||||
if ((port.Orientation == ConnectorOrientation.Right && port2.Orientation == ConnectorOrientation.Left)
|
||||
|| (port.Orientation == ConnectorOrientation.Bottom && port2.Orientation == ConnectorOrientation.Top))
|
||||
{
|
||||
|
||||
if (port.DistanceTo(port2) < diagramViewModel.DiagramOption.SnappingOption.BlockSnappingRadius)
|
||||
var dis = port.DistanceTo(port2);
|
||||
Debug.WriteLine($"{port.DataItem.Name}-{port2.DataItem.Name}:{dis}");
|
||||
if (dis < diagramViewModel.DiagramOption.SnappingOption.BlockSnappingRadius)
|
||||
{
|
||||
port.DataItem.ShowConnectors = true;
|
||||
if (port2.CanAttachTo(port) == true)
|
||||
if (port.CanAttachTo(port2) == true)
|
||||
{
|
||||
diagramViewModel.AddAttachTo(port, true);
|
||||
parent = port;
|
||||
@@ -79,11 +82,12 @@ namespace AIStudio.Wpf.DiagramDesigner
|
||||
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.BlockSnappingRadius)
|
||||
var dis = port.DistanceTo(port2);
|
||||
Debug.WriteLine($"{port.DataItem.Name}-{port2.DataItem.Name}:{dis}");
|
||||
if (dis < diagramViewModel.DiagramOption.SnappingOption.BlockSnappingRadius)
|
||||
{
|
||||
port.DataItem.ShowConnectors = true;
|
||||
if (port2.CanAttachTo(port) == true)
|
||||
if (port.CanAttachTo(port2) == true)
|
||||
{
|
||||
diagramViewModel.AddAttachTo(port, true);
|
||||
next = port;
|
||||
@@ -105,6 +109,11 @@ namespace AIStudio.Wpf.DiagramDesigner
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (success)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return new Tuple<FullyCreatedConnectorInfo, FullyCreatedConnectorInfo>(parent, next);
|
||||
@@ -125,17 +134,17 @@ namespace AIStudio.Wpf.DiagramDesigner
|
||||
{
|
||||
var innerport = container.GetAllContainers(container.Children, false).Where(p => p.GetBounds().IntersectsWith(blockDesignerItemTempLink.GetBounds())).OrderByDescending(p => p.ContainerLevel).FirstOrDefault();
|
||||
if (innerport != null)
|
||||
{
|
||||
{
|
||||
if (innerport.CanAttachTo(blockDesignerItemTempLink.Items.FirstOrDefault()) == true)
|
||||
{
|
||||
innerport.DataItem.ShowConnectors = true;
|
||||
innerport.DataItem.ShowConnectors = true;
|
||||
if (innerport.OnlyOneChild || innerport.Children.Count == 0)
|
||||
{
|
||||
diagramViewModel.AddAttachTo(innerport, true);
|
||||
}
|
||||
else
|
||||
{
|
||||
diagramViewModel.FindNearPortToAttachTo(innerport.Children.ToList(), blockDesignerItemTempLink);
|
||||
diagramViewModel.FindNearPortToAttachTo(innerport.Children.ToList(), blockDesignerItemTempLink);
|
||||
}
|
||||
return innerport;
|
||||
}
|
||||
@@ -148,7 +157,7 @@ namespace AIStudio.Wpf.DiagramDesigner
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
|
||||
if (container.CanAttachTo(blockDesignerItemTempLink.Items.FirstOrDefault()) == true)
|
||||
{
|
||||
container.DataItem.ShowConnectors = true;
|
||||
@@ -158,7 +167,7 @@ namespace AIStudio.Wpf.DiagramDesigner
|
||||
}
|
||||
else
|
||||
{
|
||||
diagramViewModel.FindNearPortToAttachTo(container.Children.ToList(), blockDesignerItemTempLink);
|
||||
diagramViewModel.FindNearPortToAttachTo(container.Children.ToList(), blockDesignerItemTempLink);
|
||||
}
|
||||
return container;
|
||||
}
|
||||
@@ -209,11 +218,11 @@ namespace AIStudio.Wpf.DiagramDesigner
|
||||
return;
|
||||
|
||||
if (blocks.Any())
|
||||
{
|
||||
{
|
||||
var links = BlockDesignerItemTempLink.Build(blocks);
|
||||
|
||||
blocks.ToList().ForEach(p => {
|
||||
p.ZIndex = diagramViewModel.Items.Any() ? diagramViewModel.Items.Where(q => q.ZIndex != int.MaxValue).Max(r => r.ZIndex) + 1 : 0;
|
||||
p.ZIndex = diagramViewModel.Items.Where(q => q.ZIndex != int.MaxValue).Any() ? diagramViewModel.Items.Where(q => q.ZIndex != int.MaxValue).Max(r => r.ZIndex) + 1 : 0;
|
||||
});
|
||||
|
||||
foreach (BlockDesignerItemTempLink item in links)
|
||||
@@ -228,10 +237,10 @@ namespace AIStudio.Wpf.DiagramDesigner
|
||||
index = container.Children.IndexOf(child);
|
||||
if (child.RightConnector?.BeAttachTo == true || child.BottomConnector?.BeAttachTo == true)
|
||||
{
|
||||
index ++;
|
||||
index++;
|
||||
}
|
||||
}
|
||||
diagramViewModel.InsertChildCommand.Execute(new BlockContainerPara() { Item = container.DataItem, Child = item.Items.FirstOrDefault(), Container = container, Index = index });
|
||||
diagramViewModel.InsertChildCommand.Execute(new BlockContainerPara() { Item = container.DataItem, Child = item.Items.FirstOrDefault(), Container = container, Index = index });
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -267,7 +276,7 @@ namespace AIStudio.Wpf.DiagramDesigner
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
diagramViewModel.ClearAttachTo();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user