Лабораторная работа №5(new)
This commit is contained in:
parent
40c8209de7
commit
477bdcf3d4
@ -12,30 +12,83 @@ public class AircraftSharingService : AbstractCompany
|
||||
public AircraftSharingService(int picWidth, int picHeight, ICollectionGenericObjects<DrawningAircraft> 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -11,13 +11,13 @@ namespace ProectMilitaryAircraft.CollectionGenericObjects;
|
||||
/// Параметризованный набор объектов
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Параметр : ограничение - ссылочный тип</typeparam>
|
||||
public class ListgenericObjects<T>
|
||||
public class ListgenericObjects<T> : ICollectionGenericObjects<T>
|
||||
where T : class
|
||||
{
|
||||
/// <summary>
|
||||
/// Список объектов, которые храниим
|
||||
/// </summary>
|
||||
private readonly List<T>? _collection;
|
||||
/// Список объектов, которые храним
|
||||
/// </summary>
|
||||
private readonly List<T?> _collection;
|
||||
|
||||
/// <summary>
|
||||
/// Максимально допустимое число объектов в списке
|
||||
@ -28,91 +28,51 @@ public class ListgenericObjects<T>
|
||||
|
||||
public int SetMaxCount { set { if (value > 0) { _maxCount = value; } } }
|
||||
|
||||
public CollectionType GetCollectionType => CollectionType.List;
|
||||
|
||||
/// <summary>
|
||||
/// Конструктор
|
||||
/// </summary>
|
||||
public ListgenericObjects(int count)
|
||||
public ListgenericObjects()
|
||||
{
|
||||
_maxCount = count;
|
||||
_collection = new();
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Получение объекта из набора позиции
|
||||
/// </summary>
|
||||
/// <param name="position"></param>
|
||||
/// <returns></returns>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Проход по списку
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public IEnumerable<T?> 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;
|
||||
}
|
||||
}
|
||||
|
@ -16,71 +16,103 @@ namespace ProectMilitaryAircraft.CollectionGenericObjects
|
||||
/// <summary>
|
||||
/// Массив объектов, которые храним
|
||||
/// </summary>
|
||||
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];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Конструктор
|
||||
/// </summary>
|
||||
public MassiveGenericObjects()
|
||||
{
|
||||
_massive = Array.Empty<T>();
|
||||
_collection = Array.Empty<T?>();
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -30,7 +30,7 @@ public class StorageCollection<T>
|
||||
}
|
||||
if (collectionType == CollectionType.List)
|
||||
{
|
||||
_storages.Add(name, new MassiveGenericObjects<T>());
|
||||
_storages.Add(name, new ListgenericObjects<T>());
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -120,8 +120,10 @@ public class DrawningAircraft
|
||||
/// <returns>true - границы заданы, false - проверка не пройдена, нельзя разместить объект в этих размерах</returns>
|
||||
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;
|
||||
|
@ -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);
|
||||
|
||||
//Ракеты
|
||||
|
@ -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<DrawningAircraft>? collection = _storageCollection[listBoxCollection.SelectedItem.ToString()?? string.Empty];
|
||||
ICollectionGenericObjects<DrawningAircraft>? 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<DrawningAircraft>());
|
||||
_company = new AircraftSharingService(pictureBox.Width, pictureBox.Height, collection);
|
||||
pictureBox.Image = _company.Show();
|
||||
break;
|
||||
}
|
||||
|
||||
panelCompanyTools.Enabled = true;
|
||||
RefreshListBoxItems();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user