private void OnMouseDown(object sender, MouseButtonEventArgs e)
 {
     if (e.ButtonState == MouseButtonState.Pressed)
     {
         currentPoint = Mouse.GetMousePosition(CanvasImage);
     }
 }
 private void Container_LeftClick(object sender, MouseButtonEventArgs e)
 {
     if (isDrawingPolygon)
     {
         var p = Mouse.GetMousePosition(CanvasImage);
         polygonVertices.Add(p);
     }
 }
        private void OnMouseMove(object sender, MouseEventArgs e)
        {
            if (isDrawingPolygon)
            {
                return;
            }

            if (e.LeftButton == MouseButtonState.Pressed)
            {
                var to = Mouse.GetMousePosition(CanvasImage);
                canvas.DrawLine((int)currentPoint.X, (int)currentPoint.Y, (int)to.X, (int)to.Y);

                currentPoint = to;
            }
        }
        private void Container_RightClick(object sender, MouseEventArgs e)
        {
            Container.ContextMenu = null;

            var p = Mouse.GetMousePosition(CanvasImage);

            if (canvas.IsPointInsideSomePolygon(p.X, p.Y))
            {
                showFillPolygonMenu = true;
            }
            else
            {
                showFillPolygonMenu = false;
            }

            var drawContextMenu = new ContextMenu();
            var drawLine        = new MenuItem();
            var drawRect        = new MenuItem();
            var drawPolygon     = new MenuItem();
            var drawCircle      = new MenuItem();

            if (!isDrawingLine)
            {
                drawLine.Header = "Start line";
                drawLine.Click += (s, ee) =>
                {
                    drawingLineStart = p;

                    isDrawingLine = true;
                };
            }
            else
            {
                drawCircle.IsEnabled  = false;
                drawPolygon.IsEnabled = false;
                drawRect.IsEnabled    = false;

                drawLine.Header = "End line";

                drawLine.Click += (s, ee) =>
                {
                    canvas.DrawLine((int)drawingLineStart.X, (int)drawingLineStart.Y,
                                    (int)p.X, (int)p.Y);

                    isDrawingLine = false;
                };
            }

            if (!isDrawingRect)
            {
                drawRect.Header = "Start clipping rect";
                drawRect.Click += (s, ee) =>
                {
                    drawingRectStart = p;

                    isDrawingRect = true;
                };
            }
            else
            {
                drawLine.IsEnabled    = false;
                drawPolygon.IsEnabled = false;
                drawCircle.IsEnabled  = false;

                drawRect.Header = "End clipping rect";

                drawRect.Click += (s, ee) =>
                {
                    canvas.DrawClippingRect(
                        (int)Math.Min(drawingRectStart.X, p.X), (int)Math.Min(drawingRectStart.Y, p.Y),
                        (int)Math.Max(drawingRectStart.X, p.X), (int)Math.Max(drawingRectStart.Y, p.Y));

                    isDrawingRect = false;
                };
            }

            if (!isDrawingPolygon)
            {
                drawPolygon.Header = "Start polygon";
                drawPolygon.Click += (s, ee) =>
                {
                    isDrawingPolygon = true;
                    polygonVertices.Add(p);
                };
            }
            else
            {
                drawLine.IsEnabled   = false;
                drawRect.IsEnabled   = false;
                drawCircle.IsEnabled = false;

                drawPolygon.Header = "End polygon";

                drawPolygon.Click += (s, ee) =>
                {
                    isDrawingPolygon = false;

                    canvas.DrawPolygon(polygonVertices);
                    polygonVertices.Clear();
                };
            }


            drawCircle.Header = "Draw circle";
            drawCircle.Click += (s, ee) =>
            {
                canvas.DrawCircle((int)p.X, (int)p.Y, 50);
            };

            drawContextMenu.Items.Add(drawLine);
            drawContextMenu.Items.Add(drawRect);
            drawContextMenu.Items.Add(drawPolygon);
            drawContextMenu.Items.Add(new Separator());
            drawContextMenu.Items.Add(drawCircle);

            if (showFillPolygonMenu)
            {
                var fillPolygon = new MenuItem();
                fillPolygon.Header = "Fill polygon";
                fillPolygon.Click += (s, ee) =>
                {
                    canvas.FillRectangleUnderCursor();
                };

                drawContextMenu.Items.Add(new Separator());
                drawContextMenu.Items.Add(fillPolygon);
            }

            Container.ContextMenu = drawContextMenu;
        }