mirror of
https://gitee.com/akwkevin/aistudio.-wpf.-diagram
synced 2026-03-20 00:16:36 +08:00
180 lines
7.3 KiB
C#
180 lines
7.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using AIStudio.Wpf.DiagramDesigner.Geometry;
|
|
|
|
namespace AIStudio.Wpf.DiagramDesigner
|
|
{
|
|
public class BoundaryPathFinder : IPathFinder
|
|
{
|
|
public List<ConnectorPoint> UpdateConnectionPoints(IDiagramViewModel diagramViewModel, PointBase sourceA, PointBase sourceB, FullyCreatedConnectorInfo sourceConnectorInfo, ConnectorInfoBase sinkConnectorInfo)
|
|
{
|
|
List<ConnectorPoint> connectionPoints;
|
|
var isFullConnection = sinkConnectorInfo is FullyCreatedConnectorInfo;
|
|
|
|
var points = new List<PointBase>()
|
|
{
|
|
sourceA,
|
|
sourceB
|
|
};
|
|
|
|
ConnectorInfo sourceInfo = ConnectorInfo(sourceConnectorInfo.Orientation,
|
|
points[0].X,
|
|
points[0].Y,
|
|
sourceConnectorInfo.DataItem.ItemWidth,
|
|
sourceConnectorInfo.DataItem.ItemHeight,
|
|
points[0]);
|
|
|
|
|
|
//StartPoint = points[0];
|
|
if (isFullConnection)
|
|
{
|
|
|
|
ConnectorInfo sinkInfo = ConnectorInfo(sinkConnectorInfo.Orientation,
|
|
points[1].X,
|
|
points[1].Y,
|
|
((FullyCreatedConnectorInfo)sinkConnectorInfo).DataItem.ItemWidth,
|
|
((FullyCreatedConnectorInfo)sinkConnectorInfo).DataItem.ItemHeight,
|
|
points[1]);
|
|
|
|
connectionPoints = ConnectorPoint.ToList(GetConnectionLine(diagramViewModel, sourceInfo, sinkInfo, false, sourceConnectorInfo.IsInnerPoint));
|
|
//EndPoint = ConnectionPoints.Last();
|
|
}
|
|
else
|
|
{
|
|
connectionPoints = ConnectorPoint.ToList(GetConnectionLine(diagramViewModel, sourceInfo, points[1], sourceConnectorInfo.Orientation, false, sourceConnectorInfo.IsInnerPoint));
|
|
//EndPoint = new PointBase();
|
|
}
|
|
|
|
return connectionPoints;
|
|
}
|
|
|
|
public ConnectorInfo ConnectorInfo(ConnectorOrientation orientation, double left, double top, double width, double height, PointBase position)
|
|
{
|
|
return new ConnectorInfo()
|
|
{
|
|
Orientation = orientation,
|
|
DesignerItemSize = new SizeBase(width, height),
|
|
DesignerItemLeft = left,
|
|
DesignerItemTop = top,
|
|
Position = position
|
|
};
|
|
}
|
|
|
|
public List<PointBase> GetConnectionLine(IDiagramViewModel diagramViewModel, ConnectorInfo source, ConnectorInfo sink, bool showLastLine, bool sourceInnerPoint = false)
|
|
{
|
|
var points = new List<PointBase>();
|
|
var ends = new List<PointBase> { source.Position, sink.Position };
|
|
|
|
points.Add(ends[0]);
|
|
points.AddRange(GetMiddlePoints(source, sink, diagramViewModel.GridCellSize, diagramViewModel.GridMargin, true));
|
|
points.Add(ends[1]);
|
|
var res = points.ToArray();
|
|
DoShift(res);
|
|
return res.ToList();
|
|
}
|
|
|
|
public List<PointBase> GetConnectionLine(IDiagramViewModel diagramViewModel, ConnectorInfo source, PointBase sinkPoint, ConnectorOrientation preferredOrientation, bool showLastLine, bool isInnerPoint = false)
|
|
{
|
|
var points = new List<PointBase>();
|
|
var ends = new List<PointBase> { source.Position, sinkPoint };
|
|
|
|
points.Add(ends[0]);
|
|
points.AddRange(GetMiddlePoints(source, new ConnectorInfo() { Orientation = ConnectorOrientation.Top, Position = sinkPoint }, diagramViewModel.GridCellSize, diagramViewModel.GridMargin, false));
|
|
points.Add(ends[1]);
|
|
var res = points.ToArray();
|
|
DoShift(res);
|
|
return res.ToList();
|
|
}
|
|
|
|
private IEnumerable<PointBase> GetMiddlePoints(ConnectorInfo source, ConnectorInfo sink, SizeBase gridCellSize, double gridMargin, bool isFullConnection)
|
|
{
|
|
var points = new List<PointBase>();
|
|
if (isFullConnection)
|
|
{
|
|
var p0 = GetFirstSegment(source.Orientation, source.Position, gridCellSize, gridMargin);
|
|
var p1 = GetFirstSegment(sink.Orientation, sink.Position, gridCellSize, gridMargin);
|
|
|
|
if (p0 == p1)
|
|
return points;
|
|
|
|
|
|
var p2 = new PointBase(GetNearestCross(p0.X, p1.X), GetNearestCross(p0.Y, p1.Y));
|
|
var p3 = new PointBase(GetNearestCross(p1.X, p0.X), GetNearestCross(p1.Y, p0.Y));
|
|
if (p2 == p3)
|
|
{
|
|
points.Add(p0);
|
|
points.Add(p2);
|
|
points.Add(p1);
|
|
}
|
|
else
|
|
{
|
|
points.Add(p0);
|
|
points.Add(p2);
|
|
if (!(Math.Abs(p2.X - p3.X) < 0.0001) && !(Math.Abs(p2.Y - p3.Y) < 0.0001))
|
|
points.Add(new PointBase(p2.X, p3.Y));
|
|
points.Add(p3);
|
|
points.Add(p1);
|
|
}
|
|
DoScale(points, gridCellSize, gridMargin);
|
|
}
|
|
return points;
|
|
}
|
|
|
|
private PointBase GetFirstSegment(ConnectorOrientation orientation, PointBase point, SizeBase cellSize, double margin)
|
|
{
|
|
double x = (int)((point.X - margin) / cellSize.Width) + 0.5;
|
|
double y = (int)((point.Y - margin) / cellSize.Height) + 0.5;
|
|
if (orientation == ConnectorOrientation.Top)
|
|
return new PointBase(x, y - 0.5);
|
|
else if (orientation == ConnectorOrientation.Bottom)
|
|
return new PointBase(x, y + 0.5);
|
|
else if (orientation == ConnectorOrientation.Left)
|
|
return new PointBase(x - 0.5, y);
|
|
else
|
|
return new PointBase(x + 0.5, y);
|
|
}
|
|
|
|
public static double GetNearestCross(double a, double b)
|
|
{
|
|
if (Math.Abs(a - b) < 0.0001 && (int)a == a)
|
|
return a;
|
|
else if (a < b)
|
|
return Math.Ceiling(a);
|
|
else
|
|
return Math.Floor(a);
|
|
}
|
|
|
|
public static PointBase SegmentMiddlePoint(PointBase p1, PointBase p2)
|
|
{
|
|
return new PointBase((p1.X + p2.X) / 2, (p1.Y + p2.Y) / 2);
|
|
}
|
|
|
|
|
|
private void DoScale(List<PointBase> points, SizeBase cellSize, double margin)
|
|
{
|
|
for (int i = 0; i < points.Count; i++)
|
|
{
|
|
points[i] = new PointBase(points[i].X * cellSize.Width + margin,
|
|
points[i].Y * cellSize.Height + margin);
|
|
}
|
|
}
|
|
|
|
private void DoShift(PointBase[] points)
|
|
{
|
|
double left = new PointBase[] { points.FirstOrDefault(), points.LastOrDefault() }.Min(p => p.X);
|
|
double top = new PointBase[] { points.FirstOrDefault(), points.LastOrDefault() }.Min(p => p.Y);
|
|
|
|
for (int i = 0; i < points.Length; i++)
|
|
{
|
|
points[i].X = points[i].X - left;
|
|
points[i].Y = points[i].Y - top;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
}
|