Exemple #1
0
        public void LeftMouseDownEventHandler(CanvasPoint mouseCoordinate)
        {
            if (!ViewModelMediator.MovingMode && ViewModelMediator.SelectedShapeType == ShapeType.FreeHand)
            {
                ViewModelMediator.RaiseViewModelEvent(this, ViewModelMediator.ViewModelEvent.DrawingStarted);
                drawn = false;  // new drawing started
                ShapeViewModel shapeVM = CreateShapeVM();
                if (shapeVM.Shape is Polyline polyLine)
                {
                    polyLine.Points.Add(new System.Windows.Point(mouseCoordinate.AbsolutePoint.X, mouseCoordinate.AbsolutePoint.Y));
                }

                Shapes.Add(shapeVM);
            }
        }
Exemple #2
0
        public void LeftMouseUpEventHandler(CanvasPoint mouseCoordinate)
        {
            if (!ViewModelMediator.MovingMode && ViewModelMediator.SelectedShapeType != ShapeType.FreeHand)
            {
                if (firstPoint == null)
                {
                    ViewModelMediator.RaiseViewModelEvent(this, ViewModelMediator.ViewModelEvent.DrawingStarted);
                    drawn = false;  // new drawing started
                    ShapeViewModel shapeVM = CreateShapeVM();
                    firstPoint = mouseCoordinate.AbsolutePoint;

                    if (shapeVM.Shape is Line line)
                    {
                        line.X1 = firstPoint.X;
                        line.Y1 = firstPoint.Y;
                        line.X2 = firstPoint.X;
                        line.Y2 = firstPoint.Y;
                        Shapes.Add(shapeVM);
                    }
                    else
                    {
                        shapeVM.Shape.Width           = 0;
                        shapeVM.Shape.Height          = 2;
                        shapeVM.Shape.RenderTransform = new TranslateTransform(firstPoint.X, firstPoint.Y);
                        Shapes.Add(shapeVM);

                        ShapeViewModel tempLineVM = CreateShapeVM(ShapeType.Line);
                        Line           tempLine   = tempLineVM.Shape as Line;
                        tempLine.X1 = firstPoint.X;
                        tempLine.Y1 = firstPoint.Y;
                        tempLine.X2 = firstPoint.X;
                        tempLine.Y2 = firstPoint.Y;
                        Shapes.Add(tempLineVM);
                    }
                }
                else if (secondPoint == null)
                {
                    secondPoint = mouseCoordinate.AbsolutePoint;
                    if (ViewModelMediator.SelectedShapeType == ShapeType.Line)
                    {
                        Shape shape = Shapes.Last().Shape;
                        (shape as Line).X2 = secondPoint.X;
                        (shape as Line).Y2 = secondPoint.Y;
                        drawn      = true;
                        firstPoint = secondPoint = null;
                        ViewModelMediator.RaiseViewModelEvent(this,
                                                              ViewModelMediator.ViewModelEvent.DrawingEnded);
                    }
                    else
                    {
                        Shapes.Remove(Shapes.Last());
                        Shape shape  = Shapes.Last().Shape;
                        var   angle  = Math.Atan((secondPoint.Y - firstPoint.Y) / (secondPoint.X - firstPoint.X));
                        var   rotate = new RotateTransform(angle * 180 / 3.14);
                        rotate.CenterX        = (firstPoint.X + secondPoint.X) / 2;
                        rotate.CenterY        = (firstPoint.Y + secondPoint.Y) / 2;
                        shape.RenderTransform = rotate;
                    }
                }
                else
                {
                    drawn      = true;
                    firstPoint = secondPoint = null;
                    ViewModelMediator.RaiseViewModelEvent(this,
                                                          ViewModelMediator.ViewModelEvent.DrawingEnded);
                }
            }
        }