Пример #1
0
 public StrokeAnchorPointThumb(ShapeStroke stroke, CustomInkCanvas canvas, int number) : base()
 {
     this.stroke = stroke;
     this.canvas = canvas;
     this.number = number;
 }
        void dragHandle_DragCompleted(object sender,
                                      DragCompletedEventArgs e)
        {
            Point actualPos = Mouse.GetPosition(this);

            if (actualPos.X < 0 || actualPos.Y < 0)
            {
                visualChildren.Remove(linkPreview);
                InvalidateArrange();
                return;
            }
            ShapeStroke strokeTo = null;
            int         number   = 0;

            foreach (UIElement thumb in canvas.Children)
            {
                if (thumb.GetType() == typeof(StrokeAnchorPointThumb))
                {
                    Point thumbPosition = thumb.TransformToAncestor(canvas).Transform(new Point(0, 0));

                    StrokeAnchorPointThumb cheatThumb = thumb as StrokeAnchorPointThumb;
                    double y = thumbPosition.Y - actualPos.Y;
                    double x = thumbPosition.X - actualPos.X;

                    double distBetweenPoints = (Math.Sqrt(Math.Pow(x, 2) + Math.Pow(y, 2)));
                    if (distBetweenPoints <= 10)
                    {
                        strokeTo  = cheatThumb.stroke as ShapeStroke;
                        actualPos = thumbPosition;
                        number    = cheatThumb.number;
                    }
                }
            }


            int linkAnchorNumber = 0;

            if (sender as Thumb == anchors[1])
            {
                linkAnchorNumber = 1;
            }
            if (sender as Thumb == anchors[2])
            {
                linkAnchorNumber = 2;
            }
            if (sender as Thumb == anchors[3])
            {
                linkAnchorNumber = 3;
            }
            Point pos = (sender as Thumb).TransformToAncestor(canvas).Transform(new Point(0, 0));

            pos.X += 5;
            pos.Y += 5;

            if (shapeStroke is ActorStroke)
            {
                if (strokeTo is ActivityStroke)
                {
                    CreateLink(actualPos, strokeTo, number, linkAnchorNumber, LinkTypes.ONE_WAY_ASSOCIATION, pos);
                }
                else
                {
                    ShowMessage("A role can only be linked to an activity.");
                }
            }
            else if (shapeStroke is ArtifactStroke)
            {
                if (strokeTo is ActivityStroke)
                {
                    CreateLink(actualPos, strokeTo, number, linkAnchorNumber, LinkTypes.ONE_WAY_ASSOCIATION, pos);
                }
                else
                {
                    ShowMessage("An artifact can only be linked to an activity.");
                }
            }
            else if (shapeStroke is ActivityStroke)
            {
                if (strokeTo is ArtifactStroke)
                {
                    CreateLink(actualPos, strokeTo, number, linkAnchorNumber, LinkTypes.ONE_WAY_ASSOCIATION, pos);
                }
                else
                {
                    ShowMessage("An activity can only be linked to an artifact.");
                }
            }
            else if (strokeTo != null && strokeTo.isProccessStroke())
            {
                ShowMessage("A role, an artifact or a role cannot be linked to nothing.");
            }
            else
            {
                CreateLink(actualPos, strokeTo, number, linkAnchorNumber, LinkTypes.LINE, pos);
            }

            visualChildren.Remove(linkPreview);
            StrokeCollection selectedStrokes = new StrokeCollection {
                canvas.GetSelectedStrokes()
            };

            canvas.Select(new StrokeCollection {
            });
            canvas.Select(selectedStrokes);

            InvalidateArrange();
        }