public void Add(Polygon p)
 {
     polyList.Add(p);
 }
 public void Remove(Polygon p)
 {
     polyList.Remove(p);
     selectedIndex = -1;
 }
 private Range MinMaxRangeFromPolygon(Polygon poly, bool X)
 {
     Range range = new Range(Math.Max(ClientRectangle.Width, ClientRectangle.Height), -1);
     if (X)
     {
         foreach (Point p in poly.Corners)
         {
             if (p.X > range.Max) range.Max = p.X;
             if (p.X < range.Min) range.Min = p.X;
         }
     }
     else
     {
         foreach (Point p in poly.Corners)
         {
             if (p.Y > range.Max) range.Max = p.Y;
             if (p.Y < range.Min) range.Min = p.Y;
         }
     }
     return range;
 }
 private int FindIndexFromPolygon(Polygon poly)
 {
     for (int i = 0; i < polyList.Count; i++)
     {
         if (polyList[i] == poly) return i;
     }
     return -1;
 }
 private void ChangeZIndex(Polygon selected)
 {
     polyList.Remove(selected);
     polyList.Add(selected);
     this.Invalidate();
 }
        private void Canvas_MouseUp(Object sender, MouseEventArgs e)
        {
            if (Mode == Modes.DRAWMODE && drawRect != null)
            {
                drawRect.EndPoint = e.Location;
                Polygon poly = new Polygon(drawRect.StartPoint, Settings.DefaultColor);
                Point[] corners = new Point[4];
                corners[0] = drawRect.StartPoint;
                corners[1] = new Point(drawRect.StartPoint.X, drawRect.EndPoint.Y);
                corners[2] = drawRect.EndPoint;
                corners[3] = new Point(drawRect.EndPoint.X, drawRect.StartPoint.Y);
                poly.Corners = corners;
                polyList.Add(poly);
            }
            Mode = Modes.NONE;
            mouseClicked = false;
            newNodeCreated = false;
            if (selectedIndex != -1)
                polyList[selectedIndex].SelectedCorner = -1;

            this.Invalidate();
        }
        private void Canvas_MouseMove(Object sender, MouseEventArgs e)
        {
            if (Mode == Modes.SHAPEMODE)
                if (e.Button == MouseButtons.Right)
                {
                    if (!newNodeCreated)
                    {
                        AddCornerToList(polyList[selectedIndex].Corners[polyList[selectedIndex].SelectedCorner], polyList[selectedIndex].Corners[polyList[selectedIndex].SelectedCorner]);
                        newNodeCreated = true;
                    }
                    polyList[selectedIndex].Corners[polyList[selectedIndex].SelectedCorner + 1] = new Point(e.X, e.Y);
                }
                else if (e.Button == MouseButtons.Left)
                    polyList[selectedIndex].Corners[polyList[selectedIndex].SelectedCorner] = new Point(e.X, e.Y);
            if (Mode == Modes.DRAGMODE)
            {
                if (e.Button == MouseButtons.Left)
                {
                    Point oldDragPoint = polyList[selectedIndex].DragPoint;
                    polyList[selectedIndex].DragPoint = new Point(e.X, e.Y);
                    for (int i = 0; i < polyList[selectedIndex].Corners.Length; i++)
                    {
                        polyList[selectedIndex].Corners[i].X += (polyList[selectedIndex].DragPoint.X - oldDragPoint.X);
                        polyList[selectedIndex].Corners[i].Y += (polyList[selectedIndex].DragPoint.Y - oldDragPoint.Y);
                    }
                }
                else if (e.Button == MouseButtons.Middle)
                {
                    Remove(polyList[selectedIndex]);
                }
            }
            if (Mode == Modes.DRAWMODE && drawRect != null)
            {
                drawRect.EndPoint = e.Location;
                drawPolygon = new Polygon(drawRect.StartPoint, Settings.DefaultColor);
                Point[] corners = new Point[4];
                corners[0] = drawRect.StartPoint;
                corners[1] = new Point(drawRect.StartPoint.X, drawRect.EndPoint.Y);
                corners[2] = drawRect.EndPoint;
                corners[3] = new Point(drawRect.EndPoint.X, drawRect.StartPoint.Y);
                drawPolygon.Corners = corners;
            }
            if (Mode != Modes.DRAWMODE)
            {
                if (GetPolygonFromPoint(e.Location) != null)
                {
                    this.Cursor = Cursors.SizeAll;

                }
                else this.Cursor = Cursors.Arrow;
            }

            this.Invalidate();
        }