From 477bdcf3d41d63357323df3aac92e469935a1ca8 Mon Sep 17 00:00:00 2001 From: MatveyPetrov Date: Fri, 16 Feb 2024 18:41:40 +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=965(new)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AircraftSharingService.cs | 77 ++++++++++++--- .../ListgenericObjects.cs | 84 +++++----------- .../MassiveGenericObjects.cs | 98 ++++++++++++------- .../StorageCollection.cs | 2 +- .../Draw/DrawningAircraft.cs | 31 +++--- .../Draw/DrawningMilitaryAircraft.cs | 27 ----- .../FormAircraftCollection.cs | 12 +-- 7 files changed, 176 insertions(+), 155 deletions(-) diff --git a/ProectMilitaryAircraft/ProectMilitaryAircraft/CollectionGenericObjects/AircraftSharingService.cs b/ProectMilitaryAircraft/ProectMilitaryAircraft/CollectionGenericObjects/AircraftSharingService.cs index 06acff7..3854cb3 100644 --- a/ProectMilitaryAircraft/ProectMilitaryAircraft/CollectionGenericObjects/AircraftSharingService.cs +++ b/ProectMilitaryAircraft/ProectMilitaryAircraft/CollectionGenericObjects/AircraftSharingService.cs @@ -12,30 +12,83 @@ public class AircraftSharingService : AbstractCompany public AircraftSharingService(int picWidth, int picHeight, ICollectionGenericObjects collection) : base(picWidth, picHeight, collection) { } + private int? _startPosX; + private int? _startPosY; + private int? ObjPositionX; + private int? ObjPositionY; + + private void DrawPlace(Graphics g) + { + Pen pen = new(Color.Black); + g.DrawRectangle(pen, _startPosX.Value, _startPosY.Value, _placeSizeWidth, _placeSizeHeight); + } + + private void DrawPosition(Graphics g) + { + Pen pen = new(Color.Black); + g.DrawRectangle(pen, _startPosX.Value, _startPosY.Value, 2, 2); + } protected override void DrawBackGround(Graphics g) { - Pen pen = new(Color.Black, 3); - for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++) + _startPosX = 0; + _startPosY = 0; + for (int x = 0; x <= _pictureWidth; x = x + _placeSizeWidth) { - for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; j++) + if ((_pictureWidth - _placeSizeWidth) > _startPosX) { - g.DrawLine(pen, i * _placeSizeWidth, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight); + for (int y = 0; y <= _pictureHeight; y = y + _placeSizeHeight) + { + if ((_pictureHeight - _placeSizeHeight) > _startPosY) + { + DrawPlace(g); + _startPosY = _startPosY + _placeSizeHeight; + } + } + _startPosX = _startPosX + _placeSizeWidth; + _startPosY = 0; } - g.DrawLine(pen, i * _placeSizeWidth, 0, i * _placeSizeWidth, _pictureHeight / _placeSizeHeight * _placeSizeHeight); } } protected override void SetObjectPosition(Graphics g) { - for(int i = 0; i < _collection?.Count; i++) + _startPosX = 5; + _startPosY = 5; + int i = 0; + + for (int x = 0; x <= _pictureWidth; x = x + _placeSizeWidth) + { + if ((_pictureWidth - _placeSizeWidth) > _startPosX) { - DrawningAircraft airplane = _collection?.Get(i); - if (airplane != null) - { - int inRow = _pictureWidth / _placeSizeWidth; - airplane.SetPosition(((inRow - 1 - (i % inRow)) * _placeSizeWidth), ((_collection.Count / inRow - 1 - i / inRow) * _placeSizeHeight)); - airplane.DrawTransport(g); + ObjPositionX = _startPosX; + + for (int y = 0; y <= _pictureHeight; y = y + _placeSizeHeight) + { + if ((_pictureHeight - _placeSizeHeight) > _startPosY) + { + ObjPositionY = _startPosY; + if (i < (_collection?.Count)) + { + DrawningAircraft obj = _collection.Get(i); + + if (obj != null) + { + obj.SetpictureSize(_pictureWidth, _pictureHeight); + obj.SetPosition(Convert.ToInt32(ObjPositionX), Convert.ToInt32(ObjPositionY)); + + } + i++; + } + + _startPosY = _startPosY + _placeSizeHeight; + + + } + } + _startPosX = _startPosX + _placeSizeWidth; + + _startPosY = 5; } } } diff --git a/ProectMilitaryAircraft/ProectMilitaryAircraft/CollectionGenericObjects/ListgenericObjects.cs b/ProectMilitaryAircraft/ProectMilitaryAircraft/CollectionGenericObjects/ListgenericObjects.cs index 58c3ef8..b9e1580 100644 --- a/ProectMilitaryAircraft/ProectMilitaryAircraft/CollectionGenericObjects/ListgenericObjects.cs +++ b/ProectMilitaryAircraft/ProectMilitaryAircraft/CollectionGenericObjects/ListgenericObjects.cs @@ -11,13 +11,13 @@ namespace ProectMilitaryAircraft.CollectionGenericObjects; /// Параметризованный набор объектов /// /// Параметр : ограничение - ссылочный тип -public class ListgenericObjects +public class ListgenericObjects : ICollectionGenericObjects where T : class { /// - /// Список объектов, которые храниим - /// - private readonly List? _collection; + /// Список объектов, которые храним + /// + private readonly List _collection; /// /// Максимально допустимое число объектов в списке @@ -28,91 +28,51 @@ public class ListgenericObjects public int SetMaxCount { set { if (value > 0) { _maxCount = value; } } } + public CollectionType GetCollectionType => CollectionType.List; + /// /// Конструктор /// - public ListgenericObjects(int count) + public ListgenericObjects() { - _maxCount = count; _collection = new(); } - - /// - /// Получение объекта из набора позиции - /// - /// - /// - public T? this[int position] + public T? Get(int position) { - get - { - if (position < 0 || position >= _maxCount) - { - return null; - } - return _collection?[position]; - } + if (position < 0 || position >= Count) return null; + return _collection[position]; - set - { - if (!(position >= 0 && position < Count && _collection?.Count < _maxCount)) - { - return; - } - - _collection?.Insert(position, value); - return; - } } public bool Insert(T obj) { - if (_collection?.Count == _maxCount) + if (Count != _maxCount) { - return false; + _collection.Add(obj); + return true; } - - Insert(obj, 0); - return true; + return false; } public bool Insert(T obj, int position) { - if (_collection?.Count == _maxCount) + if (position > 0 && position <= _maxCount && Count != _maxCount) { - return false; + _collection.Insert(position, obj); + return true; } - - Insert(obj, 0); - return true; + return false; } public bool Remove(int position) { - if (position < 0 || position >= Count) + if (_collection[position] != null) { - return false; - } - - _collection?.RemoveAt(position); - return true; - } - - /// - /// Проход по списку - /// - /// - public IEnumerable GetTheAirplanes(int? maxTheAirplanes = null) - { - for (int i = 0; i < _collection?.Count; ++i) - { - yield return _collection?[i]; - if (maxTheAirplanes.HasValue && i == maxTheAirplanes.Value) - { - yield break; - } + _collection.RemoveAt(position); + return true; } + return false; } } diff --git a/ProectMilitaryAircraft/ProectMilitaryAircraft/CollectionGenericObjects/MassiveGenericObjects.cs b/ProectMilitaryAircraft/ProectMilitaryAircraft/CollectionGenericObjects/MassiveGenericObjects.cs index 578626e..2b95842 100644 --- a/ProectMilitaryAircraft/ProectMilitaryAircraft/CollectionGenericObjects/MassiveGenericObjects.cs +++ b/ProectMilitaryAircraft/ProectMilitaryAircraft/CollectionGenericObjects/MassiveGenericObjects.cs @@ -16,71 +16,103 @@ namespace ProectMilitaryAircraft.CollectionGenericObjects /// /// Массив объектов, которые храним /// - private T?[] _massive; - public int Count => _massive.Length; + private T?[] _collection; - public int SetMaxCount { set { if (value > 0) { _massive = new T?[value]; } } } + public int Count => _collection.Length; + + public int SetMaxCount + { + set + { + if (value > 0) + { + if (_collection.Length > 0) + { + Array.Resize(ref _collection, value); + } + else + { + _collection = new T?[value]; + } + } + } + } /// /// Конструктор /// public MassiveGenericObjects() { - _massive = Array.Empty(); + _collection = Array.Empty(); } public T? Get(int position) { - if (position < 0 || position >= Count) return null; - return _massive[position]; + if (_collection[position] != null) + { + return _collection[position]; + } + else + { + return null; + } } public bool Insert(T obj) { - int index = 0; - while (_massive[index] != null) + for (int i = 0; i < _collection.Length; i++) { - index++; - if (index == Count) { return true; } // false? + if (_collection[i] == null) + { + _collection[i] = obj; + return true; + } } - - while (index != 0) - { - _massive[index] = _massive[index - 1]; - index--; - } - _massive[0] = obj; return false; } public bool Insert(T obj, int position) { - if (position < 0 || position >= Count) + if (_collection[position] == null) { - return false; - } - if (_massive[position] == null) - { - _massive[position] = obj; + _collection[position] = obj; return true; } - int index = position; - while (_massive[index] != null) index++; - if (index == Count) return false; - for (int i = index; i > position; i--) + if (_collection[position] != null) { - _massive[i] = _massive[i - 1]; + for (int i = position; i < _collection.Length; i++) + { + if (_collection[i] == null) + { + _collection[i] = obj; + return true; + } + break; + } + + for (int i = position; i <= 0; i--) + { + if (_collection[i] == null) + { + _collection[i] = obj; + return true; + } + break; + } } - _massive[position] = obj; - return true; + return false; } public bool Remove(int position) { - if (position < 0 || position >= Count) return false; - _massive[position] = null; - return true; + if (_collection[position] != null) + { + _collection[position] = null; + return true; + } + return false; } + } } diff --git a/ProectMilitaryAircraft/ProectMilitaryAircraft/CollectionGenericObjects/StorageCollection.cs b/ProectMilitaryAircraft/ProectMilitaryAircraft/CollectionGenericObjects/StorageCollection.cs index 0e2a472..e99a26b 100644 --- a/ProectMilitaryAircraft/ProectMilitaryAircraft/CollectionGenericObjects/StorageCollection.cs +++ b/ProectMilitaryAircraft/ProectMilitaryAircraft/CollectionGenericObjects/StorageCollection.cs @@ -30,7 +30,7 @@ public class StorageCollection } if (collectionType == CollectionType.List) { - _storages.Add(name, new MassiveGenericObjects()); + _storages.Add(name, new ListgenericObjects()); } } diff --git a/ProectMilitaryAircraft/ProectMilitaryAircraft/Draw/DrawningAircraft.cs b/ProectMilitaryAircraft/ProectMilitaryAircraft/Draw/DrawningAircraft.cs index 51b60c9..6365ced 100644 --- a/ProectMilitaryAircraft/ProectMilitaryAircraft/Draw/DrawningAircraft.cs +++ b/ProectMilitaryAircraft/ProectMilitaryAircraft/Draw/DrawningAircraft.cs @@ -120,8 +120,10 @@ public class DrawningAircraft /// true - границы заданы, false - проверка не пройдена, нельзя разместить объект в этих размерах public bool SetpictureSize(int width, int height) { - // TODO провека, что объект "влезает" в размеры поля - // если влезает, сохраняем границы и корректируем позицию объекта, если она была установлена + if (width <= _drawningMilitaryAircraftWidth || height <= _drawingMilitaryAircraftHeight) + { + return false; + } _pictureWidth = width; _pictureHeight = height; return true; @@ -138,8 +140,12 @@ public class DrawningAircraft { return; } - //TODO если при установке объекта в эти координаты, он будет "выходить" за границы формы - // то надо изменить координаты, чтобы он оставался в этих границах + + if (x > _pictureWidth || x < 0 || y > _pictureHeight || y < 0) + { + x = 0; + y = 0; + } _startPosX = x; _startPosY = y; } @@ -169,24 +175,21 @@ public class DrawningAircraft return true; //Вправо case DirectionType.Right: -#pragma warning disable CS8629 // Тип значения, допускающего NULL, может быть NULL. - if (_startPosX.Value + EntityAircraft.Step <= _pictureWidth.Value) + + if (_startPosX.Value + _drawningMilitaryAircraftWidth + EntityAircraft.Step < _pictureWidth) { - if (_startPosX + 98 <= _pictureWidth) - _startPosX += (int)EntityAircraft.Step; + _startPosX += (int)EntityAircraft.Step; } -#pragma warning restore CS8629 // Тип значения, допускающего NULL, может быть NULL. + return true; //Влево case DirectionType.Down: -#pragma warning disable CS8629 // Тип значения, допускающего NULL, может быть NULL. - if (_startPosY.Value + EntityAircraft.Step <= _pictureHeight.Value) + + if (_startPosY.Value + _drawingMilitaryAircraftHeight + EntityAircraft.Step < _pictureHeight) { - if (_startPosY + 90 <= _pictureHeight) - _startPosY += (int)EntityAircraft.Step; + _startPosY += (int)EntityAircraft.Step; } -#pragma warning restore CS8629 // Тип значения, допускающего NULL, может быть NULL. return true; default: return false; diff --git a/ProectMilitaryAircraft/ProectMilitaryAircraft/Draw/DrawningMilitaryAircraft.cs b/ProectMilitaryAircraft/ProectMilitaryAircraft/Draw/DrawningMilitaryAircraft.cs index 0713114..0d0429c 100644 --- a/ProectMilitaryAircraft/ProectMilitaryAircraft/Draw/DrawningMilitaryAircraft.cs +++ b/ProectMilitaryAircraft/ProectMilitaryAircraft/Draw/DrawningMilitaryAircraft.cs @@ -44,33 +44,6 @@ public class DrawningMilitaryAircraft : DrawningAircraft Pen pen = new(Color.Black); Brush abr = new SolidBrush(airCraft.AdditionalColor); - Brush br = new SolidBrush(airCraft.BodyColor); - - //крыло - g.FillRectangle(br, _startPosX.Value + 40, _startPosY.Value, 10, 80); - g.DrawRectangle(pen, _startPosX.Value + 40, _startPosY.Value, 10, 80); - - //хвост - g.FillRectangle(br, _startPosX.Value + 5, _startPosY.Value + 27, 10, 5); - g.DrawRectangle(pen, _startPosX.Value + 5, _startPosY.Value + 27, 10, 5); - g.FillRectangle(br, _startPosX.Value + 5, _startPosY.Value + 47, 10, 5); - g.DrawRectangle(pen, _startPosX.Value + 5, _startPosY.Value + 47, 10, 5); - - //Границы Самолета - - g.FillRectangle(br, _startPosX.Value + 10, _startPosY.Value + 30, 50, 20); - g.DrawRectangle(pen, _startPosX.Value + 10, _startPosY.Value + 30, 50, 20); - - //Хвост (центр) - g.FillRectangle(br, _startPosX.Value + 2, _startPosY.Value + 37, 10, 5); - g.DrawRectangle(pen, _startPosX.Value + 2, _startPosY.Value + 37, 10, 5); - - //Кабина - g.DrawLine(pen, _startPosX.Value + 60, _startPosY.Value + 40, _startPosX.Value + 60, _startPosY.Value + 25); - g.DrawLine(pen, _startPosX.Value + 60, _startPosY.Value + 25, _startPosX.Value + 80, _startPosY.Value + 40); - g.DrawLine(pen, _startPosX.Value + 80, _startPosY.Value + 40, _startPosX.Value + 60, _startPosY.Value + 55); - g.DrawLine(pen, _startPosX.Value + 60, _startPosY.Value + 50, _startPosX.Value + 60, _startPosY.Value + 55); - base.DrawTransport(g); //Ракеты diff --git a/ProectMilitaryAircraft/ProectMilitaryAircraft/FormAircraftCollection.cs b/ProectMilitaryAircraft/ProectMilitaryAircraft/FormAircraftCollection.cs index 7d23d8b..478f7c0 100644 --- a/ProectMilitaryAircraft/ProectMilitaryAircraft/FormAircraftCollection.cs +++ b/ProectMilitaryAircraft/ProectMilitaryAircraft/FormAircraftCollection.cs @@ -76,14 +76,13 @@ public partial class FormAircraftCollection : Form if (_company + aircraft) { - - MessageBox.Show("не удалось добавить объект"); + MessageBox.Show("Объект добавлен"); pictureBox.Image = _company.Show(); + } else { - MessageBox.Show("Объект добавлен"); - pictureBox.Image = _company.Show(); + MessageBox.Show("не удалось добавить объект"); } } @@ -236,7 +235,7 @@ public partial class FormAircraftCollection : Form return; } - ICollectionGenericObjects? collection = _storageCollection[listBoxCollection.SelectedItem.ToString()?? string.Empty]; + ICollectionGenericObjects? collection = _storageCollection[listBoxCollection.SelectedItem?.ToString()?? string.Empty]; if (collection == null) { MessageBox.Show("Коллкция не проиницилизирована"); @@ -246,11 +245,12 @@ public partial class FormAircraftCollection : Form switch (comboBoxSelectorCompany.Text) { case "Хранилище": - _company = new AircraftSharingService(pictureBox.Width, pictureBox.Height, new MassiveGenericObjects()); + _company = new AircraftSharingService(pictureBox.Width, pictureBox.Height, collection); pictureBox.Image = _company.Show(); break; } panelCompanyTools.Enabled = true; + RefreshListBoxItems(); } }