Files
aistudio-wpf-diagram/AIStudio.Wpf.DiagramDesigner/Helpers/PointHelper.cs

82 lines
4.2 KiB
C#
Raw Normal View History

2023-02-11 10:03:06 +08:00
using System;
using AIStudio.Wpf.DiagramDesigner.Geometrys;
2021-07-23 09:42:22 +08:00
2022-10-28 22:45:39 +08:00
namespace AIStudio.Wpf.DiagramDesigner
2021-07-23 09:42:22 +08:00
{
public class PointHelper
{
2023-02-11 10:03:06 +08:00
public static PointBase GetPointForConnector(FullyCreatedConnectorInfo connector, bool middle = false)
2021-07-23 09:42:22 +08:00
{
2023-01-08 09:22:37 +08:00
PointBase point = new PointBase();
2023-02-11 10:03:06 +08:00
if (connector == null || connector.DataItem == null)
2023-01-08 09:22:37 +08:00
{
return point;
}
var connectorWidth = double.IsNaN(connector.ConnectorWidth) ? 0 : connector.ConnectorWidth;
var connectorHeight = double.IsNaN(connector.ConnectorHeight) ? 0 : connector.ConnectorHeight;
var left = connector.DataItem.Left;
var top = connector.DataItem.Top;
var itemWidth = connector.DataItem.GetItemWidth();
var itemHeight = connector.DataItem.GetItemHeight();
2021-07-23 09:42:22 +08:00
if (connector.IsInnerPoint)
{
point = new PointBase(left + itemWidth * connector.XRatio - connectorWidth / 2,
top + itemHeight * connector.YRatio - connectorHeight / 2);
2021-07-23 09:42:22 +08:00
}
2023-01-27 20:10:17 +08:00
else if (connector.IsPortless)
{
2023-02-11 10:03:06 +08:00
point = connector.DataItem.MiddlePosition;
2023-01-27 20:10:17 +08:00
}
2021-07-23 09:42:22 +08:00
else
{
switch (connector.Orientation)
{
2023-01-27 20:10:17 +08:00
case ConnectorOrientation.Left:
point = new PointBase(left - connectorWidth / 2, top + (itemHeight / 2) - connectorHeight / 2);
2023-01-27 20:10:17 +08:00
break;
case ConnectorOrientation.TopLeft:
point = new PointBase(left - connectorWidth / 2, top - connectorHeight / 2);
2023-01-27 20:10:17 +08:00
break;
2021-07-23 09:42:22 +08:00
case ConnectorOrientation.Top:
point = new PointBase(left + (itemWidth / 2) - connectorWidth / 2, top - connectorHeight / 2);
2021-07-23 09:42:22 +08:00
break;
2023-01-27 20:10:17 +08:00
case ConnectorOrientation.TopRight:
point = new PointBase(left + itemWidth - connectorWidth / 2, top - connectorHeight / 2);
2021-07-23 09:42:22 +08:00
break;
case ConnectorOrientation.Right:
point = new PointBase(left + itemWidth - connectorWidth / 2, top + (itemHeight / 2) - connectorHeight / 2);
2021-07-23 09:42:22 +08:00
break;
2023-01-27 20:10:17 +08:00
case ConnectorOrientation.BottomRight:
point = new PointBase(left + itemWidth - connectorWidth / 2, top + itemHeight - connectorHeight / 2);
2023-01-27 20:10:17 +08:00
break;
case ConnectorOrientation.Bottom:
point = new PointBase(left + (itemWidth / 2) - connectorWidth / 2, top + itemHeight - connectorHeight / 2);
2023-01-27 20:10:17 +08:00
break;
case ConnectorOrientation.BottomLeft:
point = new PointBase(left - connectorWidth / 2, top + itemHeight - connectorHeight / 2);
2023-01-27 20:10:17 +08:00
break;
default:
point = new PointBase(left + (itemWidth / 2) - connectorWidth / 2, top + (itemHeight / 2) - connectorHeight / 2);
2021-07-23 09:42:22 +08:00
break;
}
}
2023-02-11 10:03:06 +08:00
if (middle)
{
point.X = point.X + connectorWidth / 2;
point.Y = point.Y + connectorHeight / 2;
2023-02-11 10:03:06 +08:00
}
//旋转后的坐标
var newX = (point.X - connector.DataItem.MiddlePosition.X) * Math.Cos(connector.DataItem.Angle * Math.PI / 180) - (point.Y - connector.DataItem.MiddlePosition.Y) * Math.Sin(connector.DataItem.Angle * Math.PI / 180) + connector.DataItem.MiddlePosition.X;
var newY = (point.Y - connector.DataItem.MiddlePosition.Y) * Math.Cos(connector.DataItem.Angle * Math.PI / 180) - (point.X - connector.DataItem.MiddlePosition.X) * Math.Sin(connector.DataItem.Angle * Math.PI / 180) + connector.DataItem.MiddlePosition.Y;
//放大缩小后的坐标
newX = (newX - connector.DataItem.MiddlePosition.X) * connector.DataItem.ScaleX + connector.DataItem.MiddlePosition.X;
newY = (newY - connector.DataItem.MiddlePosition.Y) * connector.DataItem.ScaleY + connector.DataItem.MiddlePosition.Y;
return new PointBase(newX, newY);
2021-07-23 09:42:22 +08:00
}
}
}