private static Point GetNearestNeighborSource(ConnectionNodeInfo source, Point endPoint, Rect rectSource, Rect rectTarget, out bool flag)
        {
            Point n1, n2; // neighbors

            GetNeighborCorners(source.Orientation, rectSource, out n1, out n2);

            if (rectTarget.Contains(n1))
            {
                flag = false;
                return(n2);
            }

            if (rectTarget.Contains(n2))
            {
                flag = true;
                return(n1);
            }

            if ((Distance(n1, endPoint) <= Distance(n2, endPoint)))
            {
                flag = true;
                return(n1);
            }
            else
            {
                flag = false;
                return(n2);
            }
        }
        private static Point GetOffsetPoint(ConnectionNodeInfo ConnectionNode, Rect rect)
        {
            Point offsetPoint = new Point();

            switch (ConnectionNode.Orientation)
            {
            case ConnectionNodeOrientation.Left:
                offsetPoint = new Point(rect.Left, ConnectionNode.Position.Y);
                break;

            case ConnectionNodeOrientation.Top:
                offsetPoint = new Point(ConnectionNode.Position.X, rect.Top);
                break;

            case ConnectionNodeOrientation.Right:
                offsetPoint = new Point(rect.Right, ConnectionNode.Position.Y);
                break;

            case ConnectionNodeOrientation.Bottom:
                offsetPoint = new Point(ConnectionNode.Position.X, rect.Bottom);
                break;

            default:
                break;
            }

            return(offsetPoint);
        }
        private static void CheckPathEnd(ConnectionNodeInfo source, ConnectionNodeInfo target, bool showLastLine, List <Point> linePoints)
        {
            if (showLastLine)
            {
                Point  startPoint = new Point(0, 0);
                Point  endPoint   = new Point(0, 0);
                double marginPath = 15;
                switch (source.Orientation)
                {
                case ConnectionNodeOrientation.Left:
                    startPoint = new Point(source.Position.X - marginPath, source.Position.Y);
                    break;

                case ConnectionNodeOrientation.Top:
                    startPoint = new Point(source.Position.X, source.Position.Y - marginPath);
                    break;

                case ConnectionNodeOrientation.Right:
                    startPoint = new Point(source.Position.X + marginPath, source.Position.Y);
                    break;

                case ConnectionNodeOrientation.Bottom:
                    startPoint = new Point(source.Position.X, source.Position.Y + marginPath);
                    break;

                default:
                    break;
                }

                switch (target.Orientation)
                {
                case ConnectionNodeOrientation.Left:
                    endPoint = new Point(target.Position.X - marginPath, target.Position.Y);
                    break;

                case ConnectionNodeOrientation.Top:
                    endPoint = new Point(target.Position.X, target.Position.Y - marginPath);
                    break;

                case ConnectionNodeOrientation.Right:
                    endPoint = new Point(target.Position.X + marginPath, target.Position.Y);
                    break;

                case ConnectionNodeOrientation.Bottom:
                    endPoint = new Point(target.Position.X, target.Position.Y + marginPath);
                    break;

                default:
                    break;
                }
                linePoints.Insert(0, startPoint);
                linePoints.Add(endPoint);
            }
            else
            {
                linePoints.Insert(0, source.Position);
                linePoints.Add(target.Position);
            }
        }
        private static Rect GetRectWithMargin(ConnectionNodeInfo ConnectionNodeThumb, double margin)
        {
            Rect rect = new Rect(ConnectionNodeThumb.DiagramElementLeft,
                                 ConnectionNodeThumb.DiagramElementTop,
                                 ConnectionNodeThumb.DiagramElementSize.Width,
                                 ConnectionNodeThumb.DiagramElementSize.Height);

            rect.Inflate(margin, margin);

            return(rect);
        }
Beispiel #5
0
        internal ConnectionNodeInfo GetInfo()
        {
            ConnectionNodeInfo info = new ConnectionNodeInfo();

            info.DiagramElementLeft = DiagramCanvas.GetLeft(this.ParentDiagramElement);
            info.DiagramElementTop  = DiagramCanvas.GetTop(this.ParentDiagramElement);
            info.DiagramElementSize = new Size(this.ParentDiagramElement.ActualWidth, this.ParentDiagramElement.ActualHeight);
            info.Orientation        = this.Orientation;
            info.Position           = this.Position;
            return(info);
        }
        private static Point GetNearestVisibleNeighborTarget(Point currentPoint, Point endPoint, ConnectionNodeInfo target, Rect rectSource, Rect rectTarget)
        {
            Point s1, s2; // neighbors on target side

            GetNeighborCorners(target.Orientation, rectTarget, out s1, out s2);

            bool flag1 = IsPointVisible(currentPoint, s1, new Rect[] { rectSource, rectTarget });
            bool flag2 = IsPointVisible(currentPoint, s2, new Rect[] { rectSource, rectTarget });

            if (flag1)     // s1 visible
            {
                if (flag2) // s1 and s2 visible
                {
                    if (rectTarget.Contains(s1))
                    {
                        return(s2);
                    }

                    if (rectTarget.Contains(s2))
                    {
                        return(s1);
                    }

                    if ((Distance(s1, endPoint) <= Distance(s2, endPoint)))
                    {
                        return(s1);
                    }
                    else
                    {
                        return(s2);
                    }
                }
                else
                {
                    return(s1);
                }
            }
            else // s1 not visible
            {
                if (flag2) // only s2 visible
                {
                    return(s2);
                }
                else // s1 and s2 not visible
                {
                    return(new Point(double.NaN, double.NaN));
                }
            }
        }
        internal static List <Point> GetConnectionLine(ConnectionNodeInfo source, Point targetPoint, ConnectionNodeOrientation preferredOrientation)
        {
            List <Point> linePoints = new List <Point>();
            Rect         rectSource = GetRectWithMargin(source, 10);
            Point        startPoint = GetOffsetPoint(source, rectSource);
            Point        endPoint   = targetPoint;

            linePoints.Add(startPoint);
            Point currentPoint = startPoint;

            if (!rectSource.Contains(endPoint))
            {
                while (true)
                {
                    if (IsPointVisible(currentPoint, endPoint, new Rect[] { rectSource }))
                    {
                        linePoints.Add(endPoint);
                        break;
                    }

                    bool  sideFlag;
                    Point n = GetNearestNeighborSource(source, endPoint, rectSource, out sideFlag);
                    linePoints.Add(n);
                    currentPoint = n;

                    if (IsPointVisible(currentPoint, endPoint, new Rect[] { rectSource }))
                    {
                        linePoints.Add(endPoint);
                        break;
                    }
                    else
                    {
                        Point n1, n2;
                        GetOppositeCorners(source.Orientation, rectSource, out n1, out n2);
                        if (sideFlag)
                        {
                            linePoints.Add(n1);
                        }
                        else
                        {
                            linePoints.Add(n2);
                        }

                        linePoints.Add(endPoint);
                        break;
                    }
                }
            }
            else
            {
                linePoints.Add(endPoint);
            }

            if (preferredOrientation != ConnectionNodeOrientation.None)
            {
                linePoints = OptimizeLinePoints(linePoints, new Rect[] { rectSource }, source.Orientation, preferredOrientation);
            }
            else
            {
                linePoints = OptimizeLinePoints(linePoints, new Rect[] { rectSource }, source.Orientation, GetOpositeOrientation(source.Orientation));
            }

            return(linePoints);
        }
        internal static List <Point> GetConnectionLine(ConnectionNodeInfo source, ConnectionNodeInfo target, bool showLastLine)
        {
            List <Point> linePoints = new List <Point>();

            Rect rectSource = GetRectWithMargin(source, margin);
            Rect rectTarget = GetRectWithMargin(target, margin);

            Point startPoint = GetOffsetPoint(source, rectSource);
            Point endPoint   = GetOffsetPoint(target, rectTarget);

            linePoints.Add(startPoint);
            Point currentPoint = startPoint;

            if (!rectTarget.Contains(currentPoint) && !rectSource.Contains(endPoint))
            {
                while (true)
                {
                    #region source node

                    if (IsPointVisible(currentPoint, endPoint, new Rect[] { rectSource, rectTarget }))
                    {
                        linePoints.Add(endPoint);
                        currentPoint = endPoint;
                        break;
                    }

                    Point neighbour = GetNearestVisibleNeighborTarget(currentPoint, endPoint, target, rectSource, rectTarget);
                    if (!double.IsNaN(neighbour.X))
                    {
                        linePoints.Add(neighbour);
                        linePoints.Add(endPoint);
                        currentPoint = endPoint;
                        break;
                    }

                    if (currentPoint == startPoint)
                    {
                        bool  flag;
                        Point n = GetNearestNeighborSource(source, endPoint, rectSource, rectTarget, out flag);
                        linePoints.Add(n);
                        currentPoint = n;

                        if (!IsRectVisible(currentPoint, rectTarget, new Rect[] { rectSource }))
                        {
                            Point n1, n2;
                            GetOppositeCorners(source.Orientation, rectSource, out n1, out n2);
                            if (flag)
                            {
                                linePoints.Add(n1);
                                currentPoint = n1;
                            }
                            else
                            {
                                linePoints.Add(n2);
                                currentPoint = n2;
                            }
                            if (!IsRectVisible(currentPoint, rectTarget, new Rect[] { rectSource }))
                            {
                                if (flag)
                                {
                                    linePoints.Add(n2);
                                    currentPoint = n2;
                                }
                                else
                                {
                                    linePoints.Add(n1);
                                    currentPoint = n1;
                                }
                            }
                        }
                    }
                    #endregion

                    #region target node

                    else // from here on we jump to the target node
                    {
                        Point n1, n2; // neighbour corner
                        Point s1, s2; // opposite corner
                        GetNeighborCorners(target.Orientation, rectTarget, out s1, out s2);
                        GetOppositeCorners(target.Orientation, rectTarget, out n1, out n2);

                        bool n1Visible = IsPointVisible(currentPoint, n1, new Rect[] { rectSource, rectTarget });
                        bool n2Visible = IsPointVisible(currentPoint, n2, new Rect[] { rectSource, rectTarget });

                        if (n1Visible && n2Visible)
                        {
                            if (rectSource.Contains(n1))
                            {
                                linePoints.Add(n2);
                                if (rectSource.Contains(s2))
                                {
                                    linePoints.Add(n1);
                                    linePoints.Add(s1);
                                }
                                else
                                {
                                    linePoints.Add(s2);
                                }

                                linePoints.Add(endPoint);
                                currentPoint = endPoint;
                                break;
                            }

                            if (rectSource.Contains(n2))
                            {
                                linePoints.Add(n1);
                                if (rectSource.Contains(s1))
                                {
                                    linePoints.Add(n2);
                                    linePoints.Add(s2);
                                }
                                else
                                {
                                    linePoints.Add(s1);
                                }

                                linePoints.Add(endPoint);
                                currentPoint = endPoint;
                                break;
                            }

                            if ((Distance(n1, endPoint) <= Distance(n2, endPoint)))
                            {
                                linePoints.Add(n1);
                                if (rectSource.Contains(s1))
                                {
                                    linePoints.Add(n2);
                                    linePoints.Add(s2);
                                }
                                else
                                {
                                    linePoints.Add(s1);
                                }
                                linePoints.Add(endPoint);
                                currentPoint = endPoint;
                                break;
                            }
                            else
                            {
                                linePoints.Add(n2);
                                if (rectSource.Contains(s2))
                                {
                                    linePoints.Add(n1);
                                    linePoints.Add(s1);
                                }
                                else
                                {
                                    linePoints.Add(s2);
                                }
                                linePoints.Add(endPoint);
                                currentPoint = endPoint;
                                break;
                            }
                        }
                        else if (n1Visible)
                        {
                            linePoints.Add(n1);
                            if (rectSource.Contains(s1))
                            {
                                linePoints.Add(n2);
                                linePoints.Add(s2);
                            }
                            else
                            {
                                linePoints.Add(s1);
                            }
                            linePoints.Add(endPoint);
                            currentPoint = endPoint;
                            break;
                        }
                        else
                        {
                            linePoints.Add(n2);
                            if (rectSource.Contains(s2))
                            {
                                linePoints.Add(n1);
                                linePoints.Add(s1);
                            }
                            else
                            {
                                linePoints.Add(s2);
                            }
                            linePoints.Add(endPoint);
                            currentPoint = endPoint;
                            break;
                        }
                    }
                    #endregion
                }
            }
            else
            {
                linePoints.Add(endPoint);
            }

            linePoints = OptimizeLinePoints(linePoints, new Rect[] { rectSource, rectTarget }, source.Orientation, target.Orientation);

            CheckPathEnd(source, target, showLastLine, linePoints);
            return(linePoints);
        }