diff --git a/Seaplane/DrawingSeaplane.cs b/Seaplane/DrawingSeaplane.cs index 8bad07c..336d5c2 100644 --- a/Seaplane/DrawingSeaplane.cs +++ b/Seaplane/DrawingSeaplane.cs @@ -55,21 +55,27 @@ public class DrawingSeaplane { // TODO проверка, что объект "влезает" в размеры поля // если влезает, сохраняем границы и корректируем позицию объекта,если она была уже установлена - if (width < _drawningPlaneWidth || height < _drawningPlaneHeight) return false; + if (width < _drawningPlaneWidth || height < _drawningPlaneHeight) { return false; }; _pictureWidth = width; _pictureHeight = height; if (_startPosX != null || _startPosY != null) { if (_startPosX + _drawningPlaneWidth > _pictureWidth) { - _startPosX = _startPosX - (_startPosX + _drawningPlaneWidth - _pictureWidth); + _startPosX = -_drawningPlaneWidth + _pictureWidth; + } + else if (_startPosX < 0) + { + _startPosX = 0; } - else if (_startPosX < 0) _startPosX = 0; if (_startPosY + _drawningPlaneHeight > _pictureHeight) { - _startPosY = _startPosY - (_startPosY + _drawningPlaneHeight - _pictureHeight); + _startPosY = -_drawningPlaneHeight + _pictureHeight; + } + else if (_startPosY < 0) + { + _startPosY = 0; } - else if (_startPosY < 0) _startPosY = 0; } return true; } @@ -88,16 +94,29 @@ public class DrawingSeaplane // то надо изменить координаты, чтобы он оставался в этих границах if (x + _drawningPlaneWidth > _pictureWidth) { - _startPosX = x - (x + _drawningPlaneWidth - _pictureWidth); + _startPosX = _pictureWidth - _drawningPlaneWidth; } - else if (x < 0) _startPosX = 0; - else _startPosX = x; + else if (x < 0) + { + _startPosX = 0; + } + else + { + _startPosX = x; + } + if (y + _drawningPlaneHeight > _pictureHeight) { - _startPosY = y - (y + _drawningPlaneHeight - _pictureHeight); + _startPosY = _pictureHeight - _drawningPlaneHeight; + } + else if (y < 0) + { + _startPosY = 0; + } + else + { + _startPosY = y; } - else if (y < 0) _startPosY = 0; - else _startPosY = y; } /// /// Изменение направления перемещения diff --git a/Seaplane/FormSeaplane.cs b/Seaplane/FormSeaplane.cs index b0afe24..8fc7920 100644 --- a/Seaplane/FormSeaplane.cs +++ b/Seaplane/FormSeaplane.cs @@ -17,7 +17,22 @@ namespace Seaplane { InitializeComponent(); } - + private void Draw() + { + if (_drawningSeaplane == null) + { + return; + } + Bitmap bmp = new(pictureBoxSeaplane.Width, pictureBoxSeaplane.Height); + Graphics gr = Graphics.FromImage(bmp); + _drawningSeaplane.DrawTransport(gr); + pictureBoxSeaplane.Image = bmp; + } + /// + /// Обработка кнопик Создать + /// + /// + /// private void ButtonCreate_Click(object sender, EventArgs e) { Random random = new(); @@ -28,10 +43,40 @@ namespace Seaplane _drawningSeaplane.SetPictureSize(pictureBoxSeaplane.Width, pictureBoxSeaplane.Height); _drawningSeaplane.SetPosition(random.Next(10,100),random.Next(10,100)); - Bitmap bmp = new(pictureBoxSeaplane.Width, pictureBoxSeaplane.Height); - Graphics gr = Graphics.FromImage(bmp); - _drawningSeaplane.DrawTransport(gr); - pictureBoxSeaplane.Image = bmp; + Draw(); + } + /// + /// Перемещение объекта по форме + /// + /// + /// + private void ButtonMove_Click(object sender, EventArgs e) + { + if (_drawningSeaplane == null) + { + return; + } + string name = ((Button)sender)?.Name ?? string.Empty; + bool result = false; + switch (name) + { + case "buttonUp": + result = _drawningSeaplane.MoveTransport(DirectionType.Up); + break; + case "buttonDown": + result = _drawningSeaplane.MoveTransport(DirectionType.Down); + break; + case "buttonRight": + result = _drawningSeaplane.MoveTransport(DirectionType.Right); + break; + case "buttonLeft": + result = _drawningSeaplane.MoveTransport(DirectionType.Left); + break; + } + if (result) + { + Draw(); + } } } }