コード例 #1
0
        /// <summary>
        /// Automaticaly update edge label position
        /// </summary>
        public virtual void UpdatePosition()
        {
            if (double.IsNaN(DesiredSize.Width) || DesiredSize.Width == 0)
            {
                return;
            }

            if (EdgeControl == null)
            {
                return;
            }
            if (EdgeControl.Source == null || EdgeControl.Target == null)
            {
                Debug.WriteLine("EdgeLabelControl_LayoutUpdated() -> Got empty edgecontrol!");
                return;
            }
            //if hidden
            if (Visibility != Visibility.Visible)
            {
                return;
            }

            if (EdgeControl.IsSelfLooped)
            {
                var idesiredSize = DesiredSize;
                var pt           = EdgeControl.Source.GetCenterPosition();
                SetSelfLoopedSize(pt, idesiredSize);
                Arrange(LastKnownRectSize);
                return;
            }

            var p1 = EdgeControl.SourceConnectionPoint.GetValueOrDefault();
            var p2 = EdgeControl.TargetConnectionPoint.GetValueOrDefault();

            double edgeLength  = 0;
            var    routingInfo = EdgeControl.Edge as IRoutingInfo;

            if (routingInfo != null)
            {
                var routePoints = routingInfo.RoutingPoints == null ? null : routingInfo.RoutingPoints.ToWindows();

                if (routePoints == null || routePoints.Length == 0)
                {
                    // the edge is a single segment (p1,p2)
                    edgeLength = GetLabelDistance(MathHelper.GetDistanceBetweenPoints(p1, p2));
                }
                else
                {
                    // the edge has one or more segments
                    // compute the total length of all the segments
                    edgeLength = 0;
                    var rplen = routePoints.Length;
                    for (var i = 0; i <= rplen; ++i)
                    {
                        if (i == 0)
                        {
                            edgeLength += MathHelper.GetDistanceBetweenPoints(p1, routePoints[0]);
                        }
                        else if (i == rplen)
                        {
                            edgeLength += MathHelper.GetDistanceBetweenPoints(routePoints[rplen - 1], p2);
                        }
                        else
                        {
                            edgeLength += MathHelper.GetDistanceBetweenPoints(routePoints[i - 1], routePoints[i]);
                        }
                    }
                    // find the line segment where the half distance is located
                    edgeLength = GetLabelDistance(edgeLength);
                    var newp1 = p1;
                    var newp2 = p2;
                    for (var i = 0; i <= rplen; ++i)
                    {
                        double lengthOfSegment;
                        if (i == 0)
                        {
                            lengthOfSegment = MathHelper.GetDistanceBetweenPoints(newp1 = p1, newp2 = routePoints[0]);
                        }
                        else if (i == rplen)
                        {
                            lengthOfSegment = MathHelper.GetDistanceBetweenPoints(newp1 = routePoints[rplen - 1], newp2 = p2);
                        }
                        else
                        {
                            lengthOfSegment = MathHelper.GetDistanceBetweenPoints(newp1 = routePoints[i - 1], newp2 = routePoints[i]);
                        }
                        if (lengthOfSegment >= edgeLength)
                        {
                            break;
                        }
                        edgeLength -= lengthOfSegment;
                    }
                    // redefine our edge points
                    p1 = newp1;
                    p2 = newp2;
                }
            }
            // The label control should be laid out on a rectangle, in the middle of the edge
            var  angleBetweenPoints = MathHelper.GetAngleBetweenPoints(p1, p2);
            var  desiredSize        = DesiredSize;
            bool flipAxis           = p1.X > p2.X; // Flip axis if source is "after" target

            ApplyLabelHorizontalOffset(edgeLength, LabelHorizontalOffset);

            // Calculate the center point of the edge
            var centerPoint = new Point(p1.X + edgeLength * Math.Cos(angleBetweenPoints), p1.Y - edgeLength * Math.Sin(angleBetweenPoints));

            if (AlignToEdge)
            {
                // If we're aligning labels to the edges make sure add the label vertical offset
                var yEdgeOffset = LabelVerticalOffset;
                if (FlipOnRotation && flipAxis && !EdgeControl.IsParallel) // If we've flipped axis, move the offset to the other side of the edge
                {
                    yEdgeOffset = -yEdgeOffset;
                }

                // Adjust offset for rotation. Remember, the offset is perpendicular from the edge tangent.
                // Slap on 90 degrees to the angle between the points, to get the direction of the offset.
                centerPoint.Y -= yEdgeOffset * Math.Sin(angleBetweenPoints + Math.PI / 2);
                centerPoint.X += yEdgeOffset * Math.Cos(angleBetweenPoints + Math.PI / 2);

                // Angle is in degrees
                Angle = -angleBetweenPoints * 180 / Math.PI;
                if (flipAxis)
                {
                    Angle += 180; // Reorient the label so that it's always "pointing north"
                }
            }

            UpdateFinalPosition(centerPoint, desiredSize);

            #if METRO
            GraphAreaBase.SetX(this, LastKnownRectSize.X, true);
            GraphAreaBase.SetY(this, LastKnownRectSize.Y, true);
            #else
            Arrange(LastKnownRectSize);
            #endif
        }
コード例 #2
0
        private Point UpdateTargetEpData(Point from, Point to)
        {
            var dir    = MathHelper.GetDirection(from, to);
            var result = EdgePointerForTarget.Update(from, dir, EdgePointerForTarget.NeedRotation ? (-MathHelper.GetAngleBetweenPoints(from, to).ToDegrees()) : 0);

            return(EdgePointerForTarget.Visibility == Visibility.Visible ? result : new Point());
        }
コード例 #3
0
ファイル: EdgeControlBase.cs プロジェクト: slate56/GraphXExt
        private Point UpdateTargetEpData(Point from, Point to, bool allowUnsuppress = true)
        {
            var dir = MathHelper.GetDirection(from, to);

            if (from == to)
            {
                if (HideEdgePointerOnVertexOverlap)
                {
                    EdgePointerForTarget.Suppress();
                }
                else
                {
                    dir = new Vector(0, 0);
                }
            }
            else if (allowUnsuppress)
            {
                EdgePointerForTarget.UnSuppress();
            }
            var result = EdgePointerForTarget.Update(from, dir, EdgePointerForTarget.NeedRotation ? (-MathHelper.GetAngleBetweenPoints(from, to).ToDegrees()) : 0);

            return(EdgePointerForTarget.Visibility == Visibility.Visible ? result : new Point());
        }