diff --git a/Main/MainWindow.xaml.cs b/Main/MainWindow.xaml.cs index 6f7b3f4..2835851 100644 --- a/Main/MainWindow.xaml.cs +++ b/Main/MainWindow.xaml.cs @@ -22,7 +22,7 @@ namespace Main private int CanvasW => _renderTarget?.PixelWidth ?? 800; private int CanvasH => _renderTarget?.PixelHeight ?? 400; - private void ClickAddRectangle(object sender, RoutedEventArgs e) + private void ClickAddRectangle(object sender, RoutedEventArgs e) { try { @@ -33,14 +33,15 @@ namespace Main 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)); + if (x + w > CanvasW || y + h > CanvasH) + { + MessageBox.Show("Фигура не помещается в область рисования."); + return; + } + ShapeContainer.AddFigure(new RectangleFigure(name, x, y, w, h)); Upd(); } - catch (FormatException) - { - MessageBox.Show("Некорректный формат числа"); - } catch (Exception ex) { MessageBox.Show(ex.Message); @@ -55,23 +56,26 @@ namespace Main 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; + + int maxSide = Math.Min(CanvasW, CanvasH); + if (!TryReadInt(SquareSide.Text, "Side", 1, maxSide, out int side)) return; + + if (x + side > CanvasW || y + side > CanvasH) + { + MessageBox.Show("Фигура не помещается в область рисования."); + return; + } ShapeContainer.AddFigure(new SquareFigure(name, x, y, side)); - Upd(); } - catch (FormatException) - { - MessageBox.Show("Некорректный формат числа"); - } catch (Exception ex) { MessageBox.Show(ex.Message); } } - private void ClickAddEllipse(object sender, RoutedEventArgs e) + private void ClickAddEllipse(object sender, RoutedEventArgs e) { try { @@ -82,14 +86,15 @@ namespace Main 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)); + if (x + w > CanvasW || y + h > CanvasH) + { + MessageBox.Show("Фигура не помещается в область рисования."); + return; + } + ShapeContainer.AddFigure(new EllipseFigure(name, x, y, w, h)); Upd(); } - catch (FormatException) - { - MessageBox.Show("Некорректный формат числа"); - } catch (Exception ex) { MessageBox.Show(ex.Message); @@ -104,16 +109,19 @@ namespace Main 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; + + int maxD = Math.Min(CanvasW, CanvasH); + if (!TryReadInt(CircleD.Text, "Диаметр", 1, maxD, out int d)) return; + + if (x + d > CanvasW || y + d > CanvasH) + { + MessageBox.Show("Фигура не помещается в область рисования."); + return; + } ShapeContainer.AddFigure(new CircleFigure(name, x, y, d)); - Upd(); } - catch (FormatException) - { - MessageBox.Show("Некорректный формат числа"); - } catch (Exception ex) { MessageBox.Show(ex.Message); @@ -124,13 +132,12 @@ namespace Main { try { - string name = PolyName.Text.Trim(); - if (!NameIsValid(name)) throw new Exception("Имя пустое или уже занято."); + if (!TryReadName(PolyName, out string name)) return; - _polyExpected = int.Parse(PolyCount.Text); - if (_polyExpected < 3) throw new Exception("Минимум 3 вершины."); + if (!TryReadInt(PolyCount.Text, "Кол-во вершин", 3, 50, out _polyExpected)) return; _polyPoints = new List(); + PolyCount.IsEnabled = false; PolyX.IsEnabled = true; PolyY.IsEnabled = true; @@ -139,11 +146,8 @@ namespace Main PolyStatus.Text = $"Точек: 0 / {_polyExpected}"; } - catch (FormatException) + catch (Exception ex) { - MessageBox.Show("Некорректный формат числа"); - } - catch (Exception ex) { MessageBox.Show(ex.Message); } } @@ -152,10 +156,14 @@ namespace Main { try { - int x = int.Parse(PolyX.Text); - int y = int.Parse(PolyY.Text); + if (!TryReadInt(PolyX.Text, "X точки", 0, CanvasW - 1, out int x)) return; + if (!TryReadInt(PolyY.Text, "Y точки", 0, CanvasH - 1, out int y)) return; - if (_polyPoints.Count >= _polyExpected) throw new Exception("Все точки уже введены."); + if (_polyPoints.Count >= _polyExpected) + { + MessageBox.Show("Все точки уже введены."); + return; + } _polyPoints.Add(new Point(x, y)); PolyStatus.Text = $"Точек: {_polyPoints.Count} / {_polyExpected}"; @@ -166,11 +174,8 @@ namespace Main PolyFinishBtn.IsEnabled = true; } } - catch (FormatException) + catch (Exception ex) { - MessageBox.Show("Некорректный формат числа"); - } - catch (Exception ex) { MessageBox.Show(ex.Message); } } @@ -179,9 +184,19 @@ namespace Main { try { - string name = PolyName.Text.Trim(); - if (!NameIsValid(name)) throw new Exception("Имя пустое или уже занято."); - if (_polyPoints.Count != _polyExpected) throw new Exception("Не все точки введены."); + if (!TryReadName(PolyName, out string name)) return; + + if (_polyPoints.Count != _polyExpected) + { + MessageBox.Show("Не все точки введены."); + return; + } + + if (_polyPoints.Count < 3) + { + MessageBox.Show("Минимум 3 точки."); + return; + } ShapeContainer.AddFigure(new PolygonFigure(name, _polyPoints.ToArray())); Upd(); @@ -203,21 +218,24 @@ namespace Main { try { - string name = TriName.Text.Trim(); - if (!NameIsValid(name)) throw new Exception("Имя пустое или уже занято."); + if (!TryReadName(TriName, out string name)) return; - var a = new Point(int.Parse(Ax.Text), int.Parse(Ay.Text)); - var b = new Point(int.Parse(Bx.Text), int.Parse(By.Text)); - var c = new Point(int.Parse(Cx.Text), int.Parse(Cy.Text)); + if (!TryReadInt(Ax.Text, "Ax", 0, CanvasW - 1, out int ax)) return; + if (!TryReadInt(Ay.Text, "Ay", 0, CanvasH - 1, out int ay)) return; + + if (!TryReadInt(Bx.Text, "Bx", 0, CanvasW - 1, out int bx)) return; + if (!TryReadInt(By.Text, "By", 0, CanvasH - 1, out int by)) return; + + if (!TryReadInt(Cx.Text, "Cx", 0, CanvasW - 1, out int cx)) return; + if (!TryReadInt(Cy.Text, "Cy", 0, CanvasH - 1, out int cy)) return; + + var a = new Point(ax, ay); + var b = new Point(bx, by); + var c = new Point(cx, cy); ShapeContainer.AddFigure(new Triangle(name, a, b, c)); - Upd(); } - catch (FormatException) - { - MessageBox.Show("Некорректный формат числа"); - } catch (Exception ex) { MessageBox.Show(ex.Message); @@ -228,22 +246,23 @@ namespace Main { try { - string name = SceneName.Text.Trim(); - if (!NameIsValid(name)) throw new Exception("Имя пустое или уже занято."); + if (!TryReadName(SceneName, out string name)) return; - int x = int.Parse(SceneX.Text); - int y = int.Parse(SceneY.Text); - int sceneW = int.Parse(SceneW.Text); - int sceneH = int.Parse(SceneH.Text); + if (!TryReadInt(SceneX.Text, "X", 0, CanvasW - 1, out int x)) return; + if (!TryReadInt(SceneY.Text, "Y", 0, CanvasH - 1, out int y)) return; + + if (!TryReadInt(SceneW.Text, "W", 1, CanvasW, out int sceneW)) return; + if (!TryReadInt(SceneH.Text, "H", 1, CanvasH, out int sceneH)) return; + + if (x + sceneW > CanvasW || y + sceneH > CanvasH) + { + MessageBox.Show("Сцена не помещается в область рисования."); + return; + } ShapeContainer.AddFigure(new TerrainPointSunPointHumanPointText(name, x, y, sceneW, sceneH)); - Upd(); } - catch (FormatException) - { - MessageBox.Show("Некорректный формат числа"); - } catch (Exception ex) { MessageBox.Show(ex.Message); @@ -255,21 +274,19 @@ namespace Main try { var fig = GetSelectedFigureOrThrow(); - int dx = int.Parse(MoveDx.Text); - int dy = int.Parse(MoveDy.Text); - fig.MoveBy(dx, dy); - Redraw(); - } - catch (FormatException) - { - MessageBox.Show("Некорректный формат числа"); + if (!TryReadInt(MoveDx.Text, "dX", -CanvasW, CanvasW, out int dx)) return; + if (!TryReadInt(MoveDy.Text, "dY", -CanvasH, CanvasH, out int dy)) return; + + if (fig.MoveByChecked(dx, dy)) + Redraw(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } + private void ClickDeleteSelected(object sender, RoutedEventArgs e) { try @@ -345,12 +362,6 @@ namespace Main RefreshCombo(); } - private bool NameIsValid(string? name) - { - if (string.IsNullOrWhiteSpace(name)) return false; - if (ShapeContainer.FigureList.Any(f => f.Name == name)) return false; - return true; - } private Figure GetSelectedFigureOrThrow() { if (FigureCombo.SelectedItem == null) throw new Exception("Фигура не выбрана.");