From d7b2497caa3fbe179823c053bc66ff45d997e383 Mon Sep 17 00:00:00 2001
From: zw1st <144824777+zw1st@users.noreply.github.com>
Date: Wed, 21 Feb 2024 16:36:38 +0400
Subject: [PATCH] =?UTF-8?q?=D0=9B=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82?=
=?UTF-8?q?=D0=BE=D1=80=D0=BD=D0=B0=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82?=
=?UTF-8?q?=D0=B0=20=E2=84=961?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
Cruiser/Cruiser/DrawingCruiser.cs | 36 +++++++++++++++++++++++++++----
Cruiser/Cruiser/FormCruiser.cs | 2 +-
2 files changed, 33 insertions(+), 5 deletions(-)
diff --git a/Cruiser/Cruiser/DrawingCruiser.cs b/Cruiser/Cruiser/DrawingCruiser.cs
index 9bf2a50..393ef09 100644
--- a/Cruiser/Cruiser/DrawingCruiser.cs
+++ b/Cruiser/Cruiser/DrawingCruiser.cs
@@ -67,7 +67,19 @@ public class DrawingCruiser
///
public bool SetPictureSize(int width, int height)
{
- //TODO проверка, что объект влезает в границы поля
+ if (_pictureWidth < _drawingCruiserWidth || _pictureHeight < _drawingCruiserHeight)
+ {
+ if (_startPosX != null && _startPosY != null)
+ {
+ if (width < 0) width = 0;
+ else if (_startPosX + _drawingCruiserWidth > width) _startPosX = width - _drawingCruiserWidth;
+ if (height < 0) height = 0;
+ else if (_startPosY + _drawingCruiserHeight > height) _startPosY = height - _drawingCruiserHeight;
+ }
+ }
+
+ // TODO проверка, что объект "влезает" в размеры поля
+ // если влезает, сохраняем границы и корректируем позицию объекта, если она была уже установлена
_pictureHeight = height;
_pictureWidth = width;
return true;
@@ -80,15 +92,31 @@ public class DrawingCruiser
///
public void SetPosition(int x, int y)
{
+ _startPosX = x;
+ _startPosY = y;
if (!_pictureHeight.HasValue || !_pictureWidth.HasValue)
{
return;
-
+ }
+ if (_startPosX + _drawingCruiserWidth > _pictureWidth)
+ {
+ _startPosX = _pictureWidth - _drawingCruiserWidth;
+ }
+ else if (_startPosX < 0)
+ {
+ _startPosX = 0;
+ }
+ if (_startPosY + _drawingCruiserHeight > _pictureHeight)
+ {
+ _startPosY = _pictureHeight - _drawingCruiserHeight;
+ }
+
+ else if (_startPosY < 0)
+ {
+ _startPosY = 0;
}
//TODO если при установке объекта в эти координаты, он будет выходить за границы поля,
//то нужно изменить координаты, чтобы он остался в нужных размерах
- _startPosX = x;
- _startPosY = y;
}
///
diff --git a/Cruiser/Cruiser/FormCruiser.cs b/Cruiser/Cruiser/FormCruiser.cs
index 40f03cf..fd1222b 100644
--- a/Cruiser/Cruiser/FormCruiser.cs
+++ b/Cruiser/Cruiser/FormCruiser.cs
@@ -30,7 +30,7 @@ public partial class FormCruiser : Form
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)),
Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)));
_drawingCruiser.SetPictureSize(pictureBoxCruiser.Width, pictureBoxCruiser.Height);
- _drawingCruiser.SetPosition(random.Next(10, 100), random.Next(10, 100));
+ _drawingCruiser.SetPosition(random.Next(pictureBoxCruiser.Right - 200, pictureBoxCruiser.Right - 160), random.Next(pictureBoxCruiser.Bottom - 150, pictureBoxCruiser.Bottom - 100));
Draw();
}