diff --git a/DrawFigureLibrary/Figure.cs b/DrawFigureLibrary/Figure.cs index b9d4f7d..b56f8f8 100644 --- a/DrawFigureLibrary/Figure.cs +++ b/DrawFigureLibrary/Figure.cs @@ -28,8 +28,19 @@ namespace DrawFigureLibrary public override string ToString() => Name ?? ""; - public static bool Check(Figure fig, int dX, int dY, Image img) => fig.x + dX >= 0 && fig.y + dY >= 0 && fig.x + dX + fig.w <= img.ActualWidth && fig.y + dY + fig.h <= img.ActualHeight; - + public static bool Check(Figure fig, int dX, int dY, Image img) + { + long nx = (long)fig.x + dX; + long ny = (long)fig.y + dY; + long nr = nx + fig.w; + long nb = ny + fig.h; + + return nx >= 0 && + ny >= 0 && + nr <= (long)img.ActualWidth && + nb <= (long)img.ActualHeight; + } + public virtual bool HitTest(Point p) => p.X >= x && p.X <= x + w && p.Y >= y && p.Y <= y + h; public virtual bool MoveByChecked(int dX, int dY) diff --git a/Main/MainWindow.xaml.cs b/Main/MainWindow.xaml.cs index 97194ee..6f7b3f4 100644 --- a/Main/MainWindow.xaml.cs +++ b/Main/MainWindow.xaml.cs @@ -1,6 +1,7 @@ using DrawFigureLibrary; using DrawFigureLibrary.Figures; using System.Windows; +using System.Windows.Controls; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; @@ -18,18 +19,19 @@ namespace Main private DrawFigureLibrary.Figure? _mouseSelected ; private bool _isDragging ; private Point _lastMouse ; + private int CanvasW => _renderTarget?.PixelWidth ?? 800; + private int CanvasH => _renderTarget?.PixelHeight ?? 400; private void ClickAddRectangle(object sender, RoutedEventArgs e) { try { - string name = RectName.Text.Trim(); - if (!NameIsValid(name)) throw new Exception("Имя пустое или уже занято."); + if (!TryReadName(RectName, out string name)) return; - int x = int.Parse(RectX.Text); - int y = int.Parse(RectY.Text); - int w = int.Parse(RectW.Text); - int h = int.Parse(RectH.Text); + if (!TryReadInt(RectX.Text, "X", 0, CanvasW - 1, out int x)) return; + if (!TryReadInt(RectY.Text, "Y", 0, CanvasH - 1, out int y)) return; + if (!TryReadInt(RectW.Text, "W", 1, CanvasW, out int w)) return; + if (!TryReadInt(RectH.Text, "H", 1, CanvasH, out int h)) return; ShapeContainer.AddFigure(new RectangleFigure(name, x, y, w, h)); @@ -49,12 +51,11 @@ namespace Main { try { - string name = SquareName.Text.Trim(); - if (!NameIsValid(name)) throw new Exception("Имя пустое или уже занято."); + if (!TryReadName(SquareName, out string name)) return; - int x = int.Parse(SquareX.Text) ; - int y = int.Parse(SquareY.Text) ; - int side = int.Parse(SquareSide.Text) ; + if (!TryReadInt(SquareX.Text, "X", 0, CanvasW - 1, out int x)) return; + if (!TryReadInt(SquareY.Text, "Y", 0, CanvasH - 1, out int y)) return; + if (!TryReadInt(SquareSide.Text, "Side", 1, Math.Min(CanvasW, CanvasH), out int side)) return; ShapeContainer.AddFigure(new SquareFigure(name, x, y, side)); @@ -74,13 +75,12 @@ namespace Main { try { - string name = EllipseName.Text.Trim(); - if (!NameIsValid(name)) throw new Exception("Имя пустое или уже занято."); + if (!TryReadName(EllipseName, out string name)) return; - int x = int.Parse(EllipseX.Text); - int y = int.Parse(EllipseY.Text); - int w = int.Parse(EllipseW.Text); - int h = int.Parse(EllipseH.Text); + if (!TryReadInt(EllipseX.Text, "X", 0, CanvasW - 1, out int x)) return; + if (!TryReadInt(EllipseY.Text, "Y", 0, CanvasH - 1, out int y)) return; + if (!TryReadInt(EllipseW.Text, "W", 1, CanvasW, out int w)) return; + if (!TryReadInt(EllipseH.Text, "H", 1, CanvasH, out int h)) return; ShapeContainer.AddFigure(new EllipseFigure(name, x, y, w, h)); @@ -100,12 +100,11 @@ namespace Main { try { - string name = CircleName.Text.Trim(); - if (!NameIsValid(name)) throw new Exception("Имя пустое или уже занято."); + if (!TryReadName(CircleName, out string name)) return; - int x = int.Parse(CircleX.Text); - int y = int.Parse(CircleY.Text); - int d = int.Parse(CircleD.Text); + if (!TryReadInt(CircleX.Text, "X", 0, CanvasW - 1, out int x)) return; + if (!TryReadInt(CircleY.Text, "Y", 0, CanvasH - 1, out int y)) return; + if (!TryReadInt(CircleD.Text, "Диаметр", 1, Math.Min(CanvasW, CanvasH), out int d)) return; ShapeContainer.AddFigure(new CircleFigure(name, x, y, d)); @@ -416,5 +415,49 @@ namespace Main DrawingImage.ReleaseMouseCapture(); e.Handled = true; } + private bool TryReadInt(string raw, string field, int min, int max, out int value) + { + if (!int.TryParse(raw?.Trim(), out value)) + { + MessageBox.Show($"Поле \"{field}\": нужно целое число."); + return false; + } + + if (value < min || value > max) + { + MessageBox.Show($"Поле \"{field}\": допустимо [{min}..{max}]."); + return false; + } + + return true; + } + + + private bool TryReadName(TextBox tb, out string name) + { + name = (tb.Text ?? "").Trim(); + + if (string.IsNullOrWhiteSpace(name)) + { + MessageBox.Show("Имя фигуры не должно быть пустым."); + return false; + } + + if (name.Length > 40) + { + MessageBox.Show("Имя фигуры слишком длинное (максимум 40 символов)."); + return false; + } + + string kostil = name; + + if (ShapeContainer.FigureList.Any(f => string.Equals(f.Name, kostil, StringComparison.OrdinalIgnoreCase))) + { + MessageBox.Show("Фигура с таким именем уже существует."); + return false; + } + + return true; + } } } \ No newline at end of file