mirror of
https://gitee.com/akwkevin/aistudio.-wpf.-diagram
synced 2026-04-04 16:16:34 +08:00
Block图鉴
This commit is contained in:
@@ -64,5 +64,31 @@ namespace AIStudio.Wpf.DiagramDesigner
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public PointBase LeftPosition
|
||||
{
|
||||
get
|
||||
{
|
||||
return new PointBase(MiddlePosition.X - DataItem.GetItemWidth() / 2, MiddlePosition.Y);
|
||||
}
|
||||
}
|
||||
|
||||
public PointBase RightPosition
|
||||
{
|
||||
get
|
||||
{
|
||||
return new PointBase(MiddlePosition.X + DataItem.GetItemWidth() / 2, MiddlePosition.Y);
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
return Math.Min(Math.Min(leftleftdistance, leftrightdistance), Math.Min(rightleftdistance, rightrightdistance));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -382,6 +382,10 @@ namespace AIStudio.Wpf.DiagramDesigner
|
||||
{
|
||||
get; set;
|
||||
} = 50;
|
||||
public double BlockSnappingRadius
|
||||
{
|
||||
get; set;
|
||||
} = 30;
|
||||
}
|
||||
|
||||
public class ShortcutOption
|
||||
|
||||
@@ -37,30 +37,35 @@ namespace AIStudio.Wpf.DiagramDesigner
|
||||
IsReadOnlyText = true;
|
||||
}
|
||||
|
||||
public void AddNext(BlockDesignerItemViewModel next)
|
||||
public void AddNext(BlockDesignerItemViewModel next, bool first = true)
|
||||
{
|
||||
if (this.Next == next)
|
||||
{
|
||||
AlignNext(next);
|
||||
return;
|
||||
}
|
||||
var oldnext = this.Next;
|
||||
RemoveNext();
|
||||
|
||||
if (next.Prev != null)
|
||||
{
|
||||
next.Prev.RemoveNext();
|
||||
}
|
||||
|
||||
var oldnext = RemoveNext();
|
||||
|
||||
next.Left = this.Left;
|
||||
next.Top = this.Top + this.GetItemHeight();
|
||||
next.ParentId = this.Id;
|
||||
next.Parent = this;
|
||||
next.Prev = this;
|
||||
this.Next = next;
|
||||
if (next.Next != null)
|
||||
{
|
||||
next.AlignNext(next.Next);
|
||||
}
|
||||
if (oldnext != null)
|
||||
if (oldnext != null && first == true)
|
||||
{
|
||||
System.Windows.Application.Current?.Dispatcher.BeginInvoke(new Action(async () => {
|
||||
await Task.Delay(10);
|
||||
GetLast().AddNext(oldnext);
|
||||
GetLast().AddNext(oldnext, false);
|
||||
}));
|
||||
|
||||
}
|
||||
@@ -79,14 +84,19 @@ namespace AIStudio.Wpf.DiagramDesigner
|
||||
}
|
||||
}
|
||||
|
||||
public void RemoveNext()
|
||||
public BlockDesignerItemViewModel RemoveNext()
|
||||
{
|
||||
var next = this.Next;
|
||||
if (next != null)
|
||||
{
|
||||
next.ParentId = new Guid();
|
||||
next.Parent = null;
|
||||
next.Prev = null;
|
||||
this.Next = null;
|
||||
return next;
|
||||
}
|
||||
else
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -177,6 +187,13 @@ namespace AIStudio.Wpf.DiagramDesigner
|
||||
{
|
||||
return Parent as BlockDesignerItemViewModel;
|
||||
}
|
||||
set
|
||||
{
|
||||
if (Parent != value)
|
||||
{
|
||||
Parent = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public BlockDesignerItemViewModel Next
|
||||
@@ -223,9 +240,9 @@ namespace AIStudio.Wpf.DiagramDesigner
|
||||
var parent = this.Next;
|
||||
if (parent != null)
|
||||
{
|
||||
while (parent.Parent != null)
|
||||
while (parent.Prev != null)
|
||||
{
|
||||
parent = parent.Parent as BlockDesignerItemViewModel;
|
||||
parent = Prev;
|
||||
}
|
||||
}
|
||||
return parent;
|
||||
@@ -387,12 +404,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;
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using System.Windows.Documents;
|
||||
|
||||
namespace AIStudio.Wpf.DiagramDesigner
|
||||
{
|
||||
@@ -33,64 +34,75 @@ namespace AIStudio.Wpf.DiagramDesigner
|
||||
FullyCreatedConnectorInfo parent = null;
|
||||
FullyCreatedConnectorInfo next = null;
|
||||
|
||||
foreach (var port in items.SelectMany(n => n.Connectors).OfType<BlockConnectorInfo>())
|
||||
foreach (var item in items)
|
||||
{
|
||||
//已经被连接的不允许在顶部吸附了
|
||||
if ((port.Orientation == ConnectorOrientation.Top || port.Orientation == ConnectorOrientation.Left) && port.DataItem.Prev != null)
|
||||
bool success = false;
|
||||
foreach (var port in item.Connectors.OfType<BlockConnectorInfo>())
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach (var port2 in blockDesignerItemTempLink.Connectors)
|
||||
{
|
||||
//parent
|
||||
if (parent == null)
|
||||
//已经被连接的不允许在顶部吸附了
|
||||
if ((port.Orientation == ConnectorOrientation.Top || port.Orientation == ConnectorOrientation.Left) && port.DataItem.Prev != null)
|
||||
{
|
||||
if ((port.Orientation == ConnectorOrientation.Right && port2.Orientation == ConnectorOrientation.Left)
|
||||
|| (port.Orientation == ConnectorOrientation.Bottom && port2.Orientation == ConnectorOrientation.Top))
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach (var port2 in blockDesignerItemTempLink.Connectors.OfType<BlockConnectorInfo>())
|
||||
{
|
||||
//parent
|
||||
if (parent == null)
|
||||
{
|
||||
|
||||
if (port.Position.DistanceTo(port2.Position) < diagramViewModel.DiagramOption.SnappingOption.SnappingRadius)
|
||||
if ((port.Orientation == ConnectorOrientation.Right && port2.Orientation == ConnectorOrientation.Left)
|
||||
|| (port.Orientation == ConnectorOrientation.Bottom && port2.Orientation == ConnectorOrientation.Top))
|
||||
{
|
||||
port.DataItem.ShowConnectors = true;
|
||||
if (port2.CanAttachTo(port) == true)
|
||||
{
|
||||
diagramViewModel.AddAttachTo(port, true);
|
||||
parent = port;
|
||||
continue;
|
||||
}
|
||||
else
|
||||
{
|
||||
diagramViewModel.AddAttachTo(port, false);
|
||||
}
|
||||
|
||||
if (port.DistanceTo(port2) < diagramViewModel.DiagramOption.SnappingOption.BlockSnappingRadius)
|
||||
{
|
||||
port.DataItem.ShowConnectors = true;
|
||||
if (port2.CanAttachTo(port) == true)
|
||||
{
|
||||
diagramViewModel.AddAttachTo(port, true);
|
||||
parent = port;
|
||||
success = true;
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
diagramViewModel.AddAttachTo(port, false);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//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.BlockSnappingRadius)
|
||||
{
|
||||
port.DataItem.ShowConnectors = true;
|
||||
if (port2.CanAttachTo(port) == true)
|
||||
{
|
||||
diagramViewModel.AddAttachTo(port, true);
|
||||
next = port;
|
||||
success = true;
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
diagramViewModel.AddAttachTo(port, false);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//next
|
||||
if (next == null)
|
||||
if (success)
|
||||
{
|
||||
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)
|
||||
{
|
||||
diagramViewModel.AddAttachTo(port, true);
|
||||
next = port;
|
||||
continue;
|
||||
}
|
||||
else
|
||||
{
|
||||
diagramViewModel.AddAttachTo(port, false);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -172,6 +184,11 @@ namespace AIStudio.Wpf.DiagramDesigner
|
||||
{
|
||||
diagramViewModel.ClearAttachTo();
|
||||
var links = BlockDesignerItemTempLink.Build(blocks);
|
||||
|
||||
blocks.ToList().ForEach(p => {
|
||||
p.ZIndex = int.MaxValue;
|
||||
});
|
||||
|
||||
foreach (BlockDesignerItemTempLink item in links)
|
||||
{
|
||||
var container = diagramViewModel.FindNearContainerToAttachTo(item);
|
||||
@@ -195,6 +212,10 @@ namespace AIStudio.Wpf.DiagramDesigner
|
||||
{
|
||||
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;
|
||||
});
|
||||
|
||||
foreach (BlockDesignerItemTempLink item in links)
|
||||
{
|
||||
var container = diagramViewModel.FindNearContainerToAttachTo(item);
|
||||
@@ -246,7 +267,7 @@ namespace AIStudio.Wpf.DiagramDesigner
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
diagramViewModel.ClearAttachTo();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user