private void Canvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
 {
     if (ProgramMode == Mode.Pointer)
     {
         Canvas currentCanvas = sender as Canvas;
         CurrentDragObjectType = DragObjectType.Nothing;
         IsDraggingOn          = false;
         DragPolygonId         = -1;
         DragObject            = null;
         DragStartingPoint     = new Point();
         currentCanvas.Cursor  = Cursors.Arrow;
     }
 }
        private void ClearVariables()
        {
            Polygons.Clear();
            ProgramMode = Mode.Pointer;
            //Pointer Variables
            IsDraggingOn          = false;
            CurrentDragObjectType = DragObjectType.Nothing;
            DragStartingPoint     = new Point();
            DragPolygonId         = -1;
            DragObject            = null;

            //Drawing Variables
            PolygonDrawing          = false;
            CurrentlyDrawingPolygon = null;
            PolygonNumber           = 0;
            CurrentLine             = null;

            //Adding Relation Variables
            RelationPolygonId    = -1;
            RelationSelectedEdge = null;
        }
        private void Canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Canvas  currentCanvas        = sender as Canvas;
            Point   CurrentMousePosition = e.GetPosition(currentCanvas);
            MyPoint p = new MyPoint(CurrentMousePosition.X, CurrentMousePosition.Y);

            switch (ProgramMode)
            {
            case Mode.Pointer:
            {
                foreach (var pol in Polygons)
                {
                    foreach (var edge in pol.Value.Edges)
                    {
                        //check if point hit
                        if (MyPoint.AreNear(edge.first, CurrentMousePosition, Globals.VerticleClickRadiusSize) == true)
                        {
                            CurrentDragObjectType = DragObjectType.Verticle;
                            IsDraggingOn          = true;
                            DragPolygonId         = pol.Key;
                            DragObject            = edge.first;
                            return;
                        }
                    }
                    foreach (var edge in pol.Value.Edges)
                    {
                        //check if edge hit
                        if (edge.IsNearPoint(CurrentMousePosition, Globals.LineClickDistance) == true)
                        {
                            CurrentDragObjectType = DragObjectType.Edge;
                            IsDraggingOn          = true;
                            DragPolygonId         = pol.Key;
                            DragObject            = edge;
                            DragStartingPoint     = CurrentMousePosition;
                            return;
                        }
                    }
                    //check if inside Polygon
                    if (pol.Value.IsPointInside(CurrentMousePosition) == true)
                    {
                        CurrentDragObjectType = DragObjectType.Polygon;
                        IsDraggingOn          = true;
                        DragPolygonId         = pol.Key;
                        DragObject            = pol.Value;
                        DragStartingPoint     = CurrentMousePosition;
                        return;
                    }
                }
                CurrentDragObjectType = DragObjectType.Nothing;
            }
            break;

            case Mode.Draw:
            {
                if (PolygonDrawing == false)
                {
                    MyPolygon polygon = new MyPolygon(p, currentCanvas);
                    CurrentlyDrawingPolygon = polygon;
                    PolygonDrawing          = true;
                    CurrentLine?.DeleteDrawing();
                    CurrentLine = Draw.SimpleEdge(CurrentMousePosition, CurrentMousePosition, currentCanvas);
                }
                else
                {
                    var DrawResult = CurrentlyDrawingPolygon.AddVerticleAndDraw(p);
                    switch (DrawResult)
                    {
                    case PolygonDrawResult.DrawFinished:
                        CurrentLine.SetFirstPoint(CurrentMousePosition);

                        Polygons.Add(PolygonNumber, CurrentlyDrawingPolygon);

                        CurrentLine.DeleteDrawing();
                        PolygonDrawing          = false;
                        CurrentlyDrawingPolygon = null;
                        PolygonNumber++;
                        break;

                    case PolygonDrawResult.NotEnoughEdges:
                        MessageBox.Show("Not enough edges to finish polygon", Globals.WindowName, MessageBoxButton.OK, MessageBoxImage.Warning);
                        break;

                    case PolygonDrawResult.DrawInProgress:
                        CurrentLine.SetFirstPoint(CurrentMousePosition);
                        break;

                    default:
                        break;
                    }
                }
            }
            break;

            case Mode.AddMiddleVerticle:
            {
                foreach (var pol in Polygons)
                {
                    foreach (var edge in pol.Value.Edges)
                    {
                        //check if edge hit
                        if (edge.IsNearPoint(CurrentMousePosition, Globals.LineClickDistance) == true)
                        {
                            pol.Value.AddMiddleVerticleOnEdge(edge);
                            return;
                        }
                    }
                }
            }
            break;

            case Mode.AddEqualRelation:
            case Mode.AddPerpendicularRelation:
            {
                RelationType relationType;
                if (ProgramMode == Mode.AddEqualRelation)
                {
                    relationType = RelationType.Equal;
                }
                else
                {
                    relationType = RelationType.Perpendicular;
                }
                if (RelationPolygonId == -1)
                {
                    foreach (var pol in Polygons)
                    {
                        foreach (var edge in pol.Value.Edges)
                        {
                            //check if edge hit
                            if (edge.relationType == RelationType.None)
                            {
                                if (edge.IsNearPoint(CurrentMousePosition, Globals.LineClickDistance) == true)
                                {
                                    edge.SelectEdge();
                                    RelationSelectedEdge = edge;
                                    RelationPolygonId    = pol.Key;
                                    return;
                                }
                            }
                        }
                    }
                }
                else
                {
                    foreach (var edge in Polygons[RelationPolygonId].Edges)
                    {
                        //check if edge hit
                        if (edge.relationType == RelationType.None)
                        {
                            if (edge.IsNearPoint(CurrentMousePosition, Globals.LineClickDistance) == true)
                            {
                                if (Object.ReferenceEquals(edge, RelationSelectedEdge) == false)
                                {
                                    edge.relationEdge = RelationSelectedEdge;
                                    RelationSelectedEdge.relationEdge = edge;
                                    edge.relationType = relationType;
                                    RelationSelectedEdge.relationType = relationType;
                                    var result = Polygons[RelationPolygonId].ApplyRelationChanges(edge);
                                    if (result == true)
                                    {
                                        edge.relationIcon = new RelationIcon(edge, relationType, RelationPolygonId, Polygons[RelationPolygonId], currentCanvas);
                                        RelationSelectedEdge.relationIcon = new RelationIcon(RelationSelectedEdge, relationType, RelationPolygonId, Polygons[RelationPolygonId], currentCanvas);
                                    }
                                    else
                                    {
                                        edge.relationEdge = null;
                                        RelationSelectedEdge.relationEdge = null;
                                        edge.relationType = RelationType.None;
                                        RelationSelectedEdge.relationType = RelationType.None;
                                    }
                                }
                                RelationSelectedEdge.UnselectEdge();
                                RelationSelectedEdge = null;
                                RelationPolygonId    = -1;
                                return;
                            }
                        }
                    }
                }
            }
            break;

            case Mode.DeleteVerticleOrRelation:
            {
                foreach (var pol in Polygons)
                {
                    foreach (var edge in pol.Value.Edges)
                    {
                        //check if point hit
                        if (MyPoint.AreNear(edge.first, CurrentMousePosition, Globals.VerticleClickRadiusSize) == true)
                        {
                            if (Polygons[pol.Key].DeleteVerticle(edge.first) == true)
                            {
                                Polygons[pol.Key] = null;
                                Polygons.Remove(pol.Key);
                            }
                            return;
                        }
                    }
                    foreach (var edge in pol.Value.Edges)
                    {
                        //check if edge hit
                        if (edge.IsNearPoint(CurrentMousePosition, Globals.LineClickDistance) == true)
                        {
                            edge.DeleteRelation();
                            return;
                        }
                    }
                }
            }
            break;

            default:
                break;
            }
            return;
        }