From d53c9971d2ce7505a4217c7d9cc33dbf227c9c42 Mon Sep 17 00:00:00 2001 From: dlopatin Date: Sat, 17 Feb 2024 14:35:37 +0300 Subject: [PATCH] =?UTF-8?q?=D0=93=D0=BE=D1=82=D0=BE=D0=B2=D0=B0=D1=8F=20?= =?UTF-8?q?=D0=BB=D0=B0=D0=B1=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- WarmlyLocomotive/DrawningWarmlyLocomotive.cs | 42 +++++++++++++++----- WarmlyLocomotive/FormWarmlyLocomotive.cs | 8 ++-- 2 files changed, 35 insertions(+), 15 deletions(-) diff --git a/WarmlyLocomotive/DrawningWarmlyLocomotive.cs b/WarmlyLocomotive/DrawningWarmlyLocomotive.cs index 5cdc203..354ec17 100644 --- a/WarmlyLocomotive/DrawningWarmlyLocomotive.cs +++ b/WarmlyLocomotive/DrawningWarmlyLocomotive.cs @@ -59,11 +59,13 @@ public class DrawningWarmlyLocomotive /// true - границы заданы, false - проверка не пройдена, нельзя разместить объект в этих размерах public bool SetPictureSize(int width, int height) { - // TODO проверка, что объект "влезает" в размеры поля - // если влезает, сохраняем границы и корректируем позицию объекта, если она была уже установлена - _pictureWidth = width; - _pictureHeight = height; - return true; + if (width >= _drawningLocomotiveWidth && height >= _drawningLocomotiveHeight) { + _pictureWidth = width; + _pictureHeight = height; + return true; + } + return false; + } /// /// Установка позиции @@ -76,10 +78,28 @@ public class DrawningWarmlyLocomotive { return; } - // TODO если при установке объекта в эти координаты, он будет "выходить" за границы формы - // то надо изменить координаты, чтобы он оставался в этих границах - _startPosX = x; - _startPosY = y; + + if((x + _drawningLocomotiveWidth > _pictureWidth ) && (y + _drawningLocomotiveHeight > _pictureHeight)) + { + _startPosX = x - _drawningLocomotiveWidth; + _startPosY = y - _drawningLocomotiveHeight; + } + else if((x + _drawningLocomotiveWidth <= _pictureWidth) && (y + _drawningLocomotiveHeight > _pictureHeight)) + { + _startPosX = x; + _startPosY = y - _drawningLocomotiveHeight; + } + else if ((x + _drawningLocomotiveWidth > _pictureWidth) && (y + _drawningLocomotiveHeight <= _pictureHeight)) + { + _startPosX = x - _drawningLocomotiveWidth; + _startPosY = y; + } + else + { + _startPosX = x; + _startPosY = y; + } + } /// /// Изменение направления перемещения @@ -111,14 +131,14 @@ public class DrawningWarmlyLocomotive return true; // вправо case DirectionType.Right: - if (_startPosX.Value + EntityWarmlyLocomotive.Step < _pictureWidth) + if (_startPosX.Value + EntityWarmlyLocomotive.Step + _drawningLocomotiveWidth < _pictureWidth) { _startPosX += (int)EntityWarmlyLocomotive.Step; } return true; //вниз case DirectionType.Down: - if (_startPosY.Value + EntityWarmlyLocomotive.Step < _pictureHeight) + if (_startPosY.Value + EntityWarmlyLocomotive.Step + _drawningLocomotiveHeight < _pictureHeight) { _startPosY += (int)EntityWarmlyLocomotive.Step; } diff --git a/WarmlyLocomotive/FormWarmlyLocomotive.cs b/WarmlyLocomotive/FormWarmlyLocomotive.cs index 03fb16d..943d472 100644 --- a/WarmlyLocomotive/FormWarmlyLocomotive.cs +++ b/WarmlyLocomotive/FormWarmlyLocomotive.cs @@ -43,10 +43,10 @@ namespace WarmlyLocomotive 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))); - _drawningWarmlyLocomotive.SetPictureSize(pictureBoxWarmlyLocomotive.Width, - pictureBoxWarmlyLocomotive.Height); - _drawningWarmlyLocomotive.SetPosition(random.Next(10, 100), random.Next(10, - 100)); + _drawningWarmlyLocomotive.SetPictureSize( + pictureBoxWarmlyLocomotive.Width, + pictureBoxWarmlyLocomotive.Height); + _drawningWarmlyLocomotive.SetPosition(random.Next(10, 100), random.Next(10, 100)); Draw(); }