private void DrawingField_MouseUp(object sender, MouseEventArgs e) { switch (CurrentMode) { case modes.AddCircle: IsMouseDown = false; CircleAction ca = actions[actions.Count - 1] as CircleAction; ca.radiusA = circles[circles.Count - 1].radius; break; case modes.AddPolygon: AddAction(new PolygonAddVerticeAction("Dodanie wierzcholka", polygons.Count - 1, polygons[polygons.Count - 1].points.Count - 1, Point.Empty, e.Location)); IsMouseDown = false; break; case modes.Move: IsMouseDown = false; break; } Refresh(); }
private void DrawingField_MouseDown(object sender, MouseEventArgs e) { switch (CurrentMode) { case modes.Delete: Circle sc1 = Selected as Circle; if (sc1 != null) { if (sc1.IsInside(e.Location)) { CircleAction ca1 = new CircleAction("Usuniecie kola", modes.Delete, moveModes.None, sc1.SolidBrush, circles.FindIndex(cc => sc1 == cc), sc1.center, Point.Empty, sc1.radius, 0); AddAction(ca1); circles.Remove(sc1); Selected = null; } } Polygon p2 = Selected as Polygon; if (p2 != null) { bool Found2 = false; for (int i = 0; i < p2.points.Count && !Found2; i++) { if ((Math.Abs(e.X - p2.points[i].X) < 10 && Math.Abs(e.Y - p2.points[i].Y) < 10)) { Found2 = true; if (p2.points.Count == 3) { AddAction(new PolygonDeleteAction("Usuniecie wielokata", polygons.FindIndex(cc1 => p2 == cc1), p2.SolidBrush, p2.points)); polygons.Remove(p2); Selected = null; } else { AddAction(new PolygonDeleteVerticeAction("Usuniecie wierzcholka", polygons.FindIndex(cc2 => p2 == cc2), i, p2.points[i], Point.Empty)); p2.points.RemoveAt(i); } } } if (!Found2 && p2.IsInside(e.Location)) { AddAction(new PolygonDeleteAction("Usuniecie wielokata", polygons.FindIndex(cc3 => p2 == cc3), p2.SolidBrush, p2.points)); polygons.Remove(p2); Selected = null; } } break; case modes.AddCircle: IsMouseDown = true; circles.Add(new Circle(e.Location, 1) { SolidBrush = SelectedColor }); CircleAction ca = new CircleAction("Dodanie kola", modes.AddCircle, moveModes.None, SelectedColor, circles.Count - 1, Point.Empty, e.Location, 0, 1); AddAction(ca); break; case modes.AddPolygon: IsMouseDown = true; CompletePoly.Enabled = true; if (polygons.Count == 0 || polygons[polygons.Count - 1].Completed) { polygons.Add(new Polygon(e.Location) { SolidBrush = SelectedColor }); AddAction(new PolygonCreatePolygonAction("Utworzenie wielokata", SelectedColor, Point.Empty, e.Location, CompletePoly, polygons.Count - 1)); } polygons[polygons.Count - 1].points.Add(e.Location); break; case modes.Select: Selected = null; bool Found = false; for (int i = 0; i < polygons.Count && !Found; i++) { if (polygons[i].IsInside(e.Location)) { Found = true; Selected = polygons[i]; } } for (int i = 0; i < circles.Count && !Found; i++) { if (circles[i].IsInside(e.Location)) { Found = true; Selected = circles[i]; } } break; case modes.Move: IsMouseDown = true; CurrentMoveMode = moveModes.None; Circle sc = Selected as Circle; if (sc != null) { if ((Math.Abs(e.X - sc.center.X) < 10 && Math.Abs(e.Y - sc.center.Y) < 10)) { AddAction(new CircleAction("Przesuniecie kola", modes.Move, moveModes.Point, sc.SolidBrush, circles.FindIndex(cc4 => cc4 == sc), sc.center, Point.Empty, 0, 0)); CurrentMoveMode = moveModes.Point; } else { AddAction(new CircleAction("Zmiana promienia kola", modes.Move, moveModes.Radius, sc.SolidBrush, circles.FindIndex(cc5 => cc5 == sc), Point.Empty, Point.Empty, sc.radius, 0)); CurrentMoveMode = moveModes.Radius; } } Polygon p = Selected as Polygon; if (p != null) { bool Found1 = false; for (int i = 0; i < p.points.Count && !Found1; i++) //przesuwanie wierzcholka { if ((Math.Abs(e.X - p.points[i].X) < 10 && Math.Abs(e.Y - p.points[i].Y) < 10)) { Found1 = true; AddAction(new PolygonMovePointAction("Przesuniecie wierzcholka", polygons.FindIndex(cc6 => cc6 == p), i, p.points[i], Point.Empty)); CurrentMoveMode = moveModes.Point; WhichPoint = i; } } if (!Found1) { bool FoundEdge = false; for (int i = 0; i < p.points.Count && !FoundEdge; i++) //przesuwanie krawedzi { if (Math.Abs( Math.Sqrt( (e.X - p.points[i].X) * (e.X - p.points[i].X) + (e.Y - p.points[i].Y) * (e.Y - p.points[i].Y) ) + Math.Sqrt( (e.X - p.points[(i + 1) % p.points.Count].X) * (e.X - p.points[(i + 1) % p.points.Count].X) + (e.Y - p.points[(i + 1) % p.points.Count].Y) * (e.Y - p.points[(i + 1) % p.points.Count].Y) ) - Math.Sqrt( (p.points[i].X - p.points[(i + 1) % p.points.Count].X) * (p.points[i].X - p.points[(i + 1) % p.points.Count].X) + (p.points[i].Y - p.points[(i + 1) % p.points.Count].Y) * (p.points[i].Y - p.points[(i + 1) % p.points.Count].Y) ) ) < 10) { FoundEdge = true; AddAction(new PolygonMoveEdgeAction("Przesuniecie krawedzi", polygons.FindIndex(cc7 => cc7 == p), i, p.points[i], Point.Empty, p.points[(i + 1) % p.points.Count], Point.Empty)); CurrentMoveMode = moveModes.Edge; WhichPoint = i; } } if (!FoundEdge) //przesuwanie wielokata { AddAction(new PolygonMoveWholeAction("Przesuniecie wielokata", polygons.FindIndex(cc8 => cc8 == p), p.Middle(), Point.Empty)); CurrentMoveMode = moveModes.Polygon; } } } break; case modes.AddVertice: Polygon p1 = Selected as Polygon; if (p1 != null) { bool FoundEdge = false; for (int i = 0; i < p1.points.Count && !FoundEdge; i++) { int a = (int)Math.Abs( Math.Sqrt( (e.X - p1.points[i].X) * (e.X - p1.points[i].X) + (e.Y - p1.points[i].Y) * (e.Y - p1.points[i].Y) ) + Math.Sqrt( (e.X - p1.points[(i + 1) % p1.points.Count].X) * (e.X - p1.points[(i + 1) % p1.points.Count].X) + (e.Y - p1.points[(i + 1) % p1.points.Count].Y) * (e.Y - p1.points[(i + 1) % p1.points.Count].Y) ) - Math.Sqrt( (p1.points[i].X - p1.points[(i + 1) % p1.points.Count].X) * (p1.points[i].X - p1.points[(i + 1) % p1.points.Count].X) + (p1.points[i].Y - p1.points[(i + 1) % p1.points.Count].Y) * (p1.points[i].Y - p1.points[(i + 1) % p1.points.Count].Y) ) ); if (a < 10) { FoundEdge = true; p1.points.Insert(i + 1, e.Location); AddAction(new PolygonAddVerticeAction("Dodanie wierzcholka", polygons.FindIndex(cc9 => cc9 == p1), i + 1, Point.Empty, e.Location)); } } } break; } Refresh(); }