PIbd-22. Kozlova A.A. Lab work 04 #5

Closed
Nastya_Kozlova wants to merge 5 commits from laba4 into laba3
8 changed files with 318 additions and 71 deletions

View File

@ -46,7 +46,7 @@
pictureBoxAirFighter.Dock = DockStyle.Fill;
pictureBoxAirFighter.Location = new Point(0, 0);
pictureBoxAirFighter.Name = "pictureBoxAirFighter";
pictureBoxAirFighter.Size = new Size(882, 453);
pictureBoxAirFighter.Size = new Size(815, 619);
pictureBoxAirFighter.SizeMode = PictureBoxSizeMode.AutoSize;
pictureBoxAirFighter.TabIndex = 0;
pictureBoxAirFighter.TabStop = false;
@ -56,7 +56,7 @@
buttonUp.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonUp.BackgroundImage = Properties.Resources.up;
buttonUp.BackgroundImageLayout = ImageLayout.Zoom;
buttonUp.Location = new Point(691, 366);
buttonUp.Location = new Point(624, 532);
buttonUp.Name = "buttonUp";
buttonUp.Size = new Size(30, 30);
buttonUp.TabIndex = 2;
@ -68,7 +68,7 @@
buttonLeft.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonLeft.BackgroundImage = Properties.Resources.left;
buttonLeft.BackgroundImageLayout = ImageLayout.Zoom;
buttonLeft.Location = new Point(655, 402);
buttonLeft.Location = new Point(588, 568);
buttonLeft.Name = "buttonLeft";
buttonLeft.Size = new Size(30, 30);
buttonLeft.TabIndex = 3;
@ -80,7 +80,7 @@
buttonDown.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonDown.BackgroundImage = Properties.Resources.down;
buttonDown.BackgroundImageLayout = ImageLayout.Zoom;
buttonDown.Location = new Point(691, 402);
buttonDown.Location = new Point(624, 568);
buttonDown.Name = "buttonDown";
buttonDown.Size = new Size(30, 30);
buttonDown.TabIndex = 4;
@ -92,7 +92,7 @@
buttonRight.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonRight.BackgroundImage = Properties.Resources.right;
buttonRight.BackgroundImageLayout = ImageLayout.Zoom;
buttonRight.Location = new Point(727, 402);
buttonRight.Location = new Point(660, 568);
buttonRight.Name = "buttonRight";
buttonRight.Size = new Size(30, 30);
buttonRight.TabIndex = 5;
@ -105,7 +105,7 @@
comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList;
comboBoxStrategy.FormattingEnabled = true;
comboBoxStrategy.Items.AddRange(new object[] { "MoveToCenter", "MoveToBorder" });
comboBoxStrategy.Location = new Point(691, 22);
comboBoxStrategy.Location = new Point(624, 22);
comboBoxStrategy.Name = "comboBoxStrategy";
comboBoxStrategy.Size = new Size(160, 28);
comboBoxStrategy.TabIndex = 6;
@ -113,7 +113,7 @@
// ButtonCreateAirplane
//
ButtonCreateAirplane.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
ButtonCreateAirplane.Location = new Point(62, 389);
ButtonCreateAirplane.Location = new Point(62, 555);
ButtonCreateAirplane.Name = "ButtonCreateAirplane";
ButtonCreateAirplane.Size = new Size(197, 43);
ButtonCreateAirplane.TabIndex = 7;
@ -124,7 +124,7 @@
// ButtonCreateAirFighter
//
ButtonCreateAirFighter.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
ButtonCreateAirFighter.Location = new Point(297, 389);
ButtonCreateAirFighter.Location = new Point(297, 555);
ButtonCreateAirFighter.Name = "ButtonCreateAirFighter";
ButtonCreateAirFighter.Size = new Size(197, 43);
ButtonCreateAirFighter.TabIndex = 8;
@ -135,7 +135,7 @@
// buttonStep
//
buttonStep.Anchor = AnchorStyles.Top | AnchorStyles.Right;
buttonStep.Location = new Point(727, 71);
buttonStep.Location = new Point(660, 71);
buttonStep.Name = "buttonStep";
buttonStep.Size = new Size(94, 43);
buttonStep.TabIndex = 9;
@ -145,7 +145,7 @@
//
// buttonSelectAirplane
//
buttonSelectAirplane.Location = new Point(691, 138);
buttonSelectAirplane.Location = new Point(624, 133);
buttonSelectAirplane.Name = "buttonSelectAirplane";
buttonSelectAirplane.Size = new Size(160, 43);
buttonSelectAirplane.TabIndex = 10;
@ -157,7 +157,7 @@
//
AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(882, 453);
ClientSize = new Size(815, 619);
Controls.Add(buttonSelectAirplane);
Controls.Add(buttonStep);
Controls.Add(ButtonCreateAirFighter);

View File

@ -155,6 +155,6 @@ namespace AirFighter
SelectedAirplane = _drawningAirplane;
DialogResult = DialogResult.OK;
}
}
}

View File

@ -14,9 +14,9 @@ namespace AirFighter.Generics
where T : DrawningAirplane
where U : IMoveableObject
{
// Ширина окна прорисовки
private readonly int _pictureWidth;
// Высота окна прорисовки
private readonly int _pictureHeight;
@ -38,7 +38,7 @@ namespace AirFighter.Generics
_pictureHeight = picHeight;
_collection = new SetGeneric<T>(width * height);
}
public static int operator +(AirplanesGenericCollection<T, U> collect, T? obj)
{
if (obj == null)
@ -51,18 +51,20 @@ namespace AirFighter.Generics
// Перегрузка оператора вычитания
public static bool operator -(AirplanesGenericCollection<T, U> collect, int pos)
{
T? obj = collect._collection.Get(pos);
T? obj = collect._collection[pos];
if (obj != null)
{
return collect._collection.Remove(pos);
}
return false;
}
// Получение объекта IMoveableObject
public U? GetU(int pos)
{
return (U?)_collection.Get(pos)?.GetMoveableObject;
return (U?)_collection[pos]?.GetMoveableObject;
}
// Вывод всего набора объектов
public Bitmap ShowAirplanes()
{
@ -74,7 +76,6 @@ namespace AirFighter.Generics
}
// Метод отрисовки фона
private void DrawBackground(Graphics g)
{
Pen pen = new(Color.Black, 3);
@ -94,15 +95,18 @@ namespace AirFighter.Generics
// Метод прорисовки объектов
private void DrawObjects(Graphics g)
{
for (int i = 0; i < _collection.Count; i++)
int Width = _pictureWidth / _placeSizeWidth;
int Height = _pictureHeight / _placeSizeHeight;
int i = 0;
foreach (var airplane in _collection.GetAirplanes())
{
DrawningAirplane 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.SetPosition(((inRow - 1 - (i % inRow)) * _placeSizeWidth), (Height - i / Width - 1) * _placeSizeHeight);
airplane.DrawTransport(g);
}
i++;
}
}
}

View File

@ -0,0 +1,65 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AirFighter.DrawningObjects;
using AirFighter.Generics;
using AirFighter.MovementStrategy;
namespace AirFighter.Generics
{
// Класс для хранения коллекции
internal class AirplanesGenericStorage
{
// Словарь (хранилище)
readonly Dictionary<string, AirplanesGenericCollection<DrawningAirplane,
DrawningObjectAirplane>> _AirplaneStorages;
// Возвращение списка названий наборов
public List<string> Keys => _AirplaneStorages.Keys.ToList();
// Ширина окна отрисовки
private readonly int _pictureWidth;
// Высота окна отрисовки
private readonly int _pictureHeight;
// Конструктор
public AirplanesGenericStorage(int pictureWidth, int pictureHeight)
{
_AirplaneStorages = new Dictionary<string,
AirplanesGenericCollection<DrawningAirplane, DrawningObjectAirplane>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
// Добавление набора
public void AddSet(string name)
{
if (!_AirplaneStorages.ContainsKey(name))
_AirplaneStorages.Add(name, new AirplanesGenericCollection<DrawningAirplane,
DrawningObjectAirplane>(_pictureWidth, _pictureHeight));
}
// Удаление набора
public void DelSet(string name)
{
if (_AirplaneStorages.ContainsKey(name))
_AirplaneStorages.Remove(name);
}
// Доступ к набору
public AirplanesGenericCollection<DrawningAirplane, DrawningObjectAirplane>?
this[string ind]
{
get
{
if (_AirplaneStorages.ContainsKey(ind))
return _AirplaneStorages[ind];
return null;
}
}
}
}

View File

@ -85,6 +85,7 @@ namespace AirFighter.DrawningObjects
_startPosY = 50;
}
}
// Изменение направления перемещения
public void MoveTransport(DirectionType direction)
{

View File

@ -30,40 +30,99 @@
{
pictureBoxCollection = new PictureBox();
panelTools = new Panel();
panelSets = new Panel();
ButtonRemoveObject = new Button();
listBoxStorage = new ListBox();
ButtonAddObject = new Button();
labelSets = new Label();
ButtonAddAirplane = new Button();
ButtonRefreshCollection = new Button();
buttonRemoveAirplane = new Button();
maskedTextBoxNumber = new MaskedTextBox();
labelTools = new Label();
textBoxStorageName = new TextBox();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit();
panelTools.SuspendLayout();
panelSets.SuspendLayout();
SuspendLayout();
//
// pictureBoxCollection
//
pictureBoxCollection.Location = new Point(1, 0);
pictureBoxCollection.Name = "pictureBoxCollection";
pictureBoxCollection.Size = new Size(544, 446);
pictureBoxCollection.Size = new Size(778, 591);
pictureBoxCollection.TabIndex = 0;
pictureBoxCollection.TabStop = false;
//
// panelTools
//
panelTools.BorderStyle = BorderStyle.FixedSingle;
panelTools.Controls.Add(panelSets);
panelTools.Controls.Add(ButtonAddAirplane);
panelTools.Controls.Add(ButtonRefreshCollection);
panelTools.Controls.Add(buttonRemoveAirplane);
panelTools.Controls.Add(maskedTextBoxNumber);
panelTools.Location = new Point(577, 12);
panelTools.Name = "panelTools";
panelTools.Size = new Size(202, 426);
panelTools.Size = new Size(202, 579);
panelTools.TabIndex = 1;
//
// panelSets
//
panelSets.Controls.Add(textBoxStorageName);
panelSets.Controls.Add(ButtonRemoveObject);
panelSets.Controls.Add(listBoxStorage);
panelSets.Controls.Add(ButtonAddObject);
panelSets.Controls.Add(labelSets);
panelSets.Location = new Point(13, 24);
panelSets.Name = "panelSets";
panelSets.Size = new Size(178, 296);
panelSets.TabIndex = 8;
//
// ButtonRemoveObject
//
ButtonRemoveObject.Location = new Point(10, 254);
ButtonRemoveObject.Name = "ButtonRemoveObject";
ButtonRemoveObject.Size = new Size(160, 29);
ButtonRemoveObject.TabIndex = 4;
ButtonRemoveObject.Text = "Удалить набор";
ButtonRemoveObject.UseVisualStyleBackColor = true;
ButtonRemoveObject.Click += ButtonRemoveObject_Click;
//
// listBoxStorage
//
listBoxStorage.FormattingEnabled = true;
listBoxStorage.ItemHeight = 20;
listBoxStorage.Location = new Point(19, 135);
listBoxStorage.Name = "listBoxStorage";
listBoxStorage.Size = new Size(145, 104);
listBoxStorage.TabIndex = 3;
listBoxStorage.SelectedIndexChanged += listBoxStorage_SelectedIndexChanged;
//
// ButtonAddObject
//
ButtonAddObject.Location = new Point(10, 87);
ButtonAddObject.Name = "ButtonAddObject";
ButtonAddObject.Size = new Size(160, 29);
ButtonAddObject.TabIndex = 2;
ButtonAddObject.Text = "Добавить набор";
ButtonAddObject.UseVisualStyleBackColor = true;
ButtonAddObject.Click += ButtonAddObject_Click;
//
// labelSets
//
labelSets.AutoSize = true;
labelSets.Location = new Point(53, 10);
labelSets.Name = "labelSets";
labelSets.Size = new Size(66, 20);
labelSets.TabIndex = 0;
labelSets.Text = "Наборы";
//
// ButtonAddAirplane
//
ButtonAddAirplane.Location = new Point(12, 37);
ButtonAddAirplane.Location = new Point(12, 381);
ButtonAddAirplane.Name = "ButtonAddAirplane";
ButtonAddAirplane.Size = new Size(174, 40);
ButtonAddAirplane.Size = new Size(174, 31);
ButtonAddAirplane.TabIndex = 7;
ButtonAddAirplane.Text = "добавить самолёт";
ButtonAddAirplane.UseVisualStyleBackColor = true;
@ -71,9 +130,9 @@
//
// ButtonRefreshCollection
//
ButtonRefreshCollection.Location = new Point(12, 250);
ButtonRefreshCollection.Location = new Point(12, 532);
ButtonRefreshCollection.Name = "ButtonRefreshCollection";
ButtonRefreshCollection.Size = new Size(174, 40);
ButtonRefreshCollection.Size = new Size(174, 33);
ButtonRefreshCollection.TabIndex = 6;
ButtonRefreshCollection.Text = "Обновить коллекцию";
ButtonRefreshCollection.UseVisualStyleBackColor = true;
@ -81,9 +140,9 @@
//
// buttonRemoveAirplane
//
buttonRemoveAirplane.Location = new Point(12, 182);
buttonRemoveAirplane.Location = new Point(12, 480);
buttonRemoveAirplane.Name = "buttonRemoveAirplane";
buttonRemoveAirplane.Size = new Size(174, 40);
buttonRemoveAirplane.Size = new Size(174, 30);
buttonRemoveAirplane.TabIndex = 5;
buttonRemoveAirplane.Text = "Удалить самолёт";
buttonRemoveAirplane.UseVisualStyleBackColor = true;
@ -91,7 +150,7 @@
//
// maskedTextBoxNumber
//
maskedTextBoxNumber.Location = new Point(12, 117);
maskedTextBoxNumber.Location = new Point(12, 432);
maskedTextBoxNumber.Name = "maskedTextBoxNumber";
maskedTextBoxNumber.Size = new Size(174, 27);
maskedTextBoxNumber.TabIndex = 4;
@ -106,11 +165,18 @@
labelTools.TabIndex = 2;
labelTools.Text = "Инструменты";
//
// textBoxStorageName
//
textBoxStorageName.Location = new Point(10, 44);
textBoxStorageName.Name = "textBoxStorageName";
textBoxStorageName.Size = new Size(160, 27);
textBoxStorageName.TabIndex = 5;
//
// FormAirPlaneCollection
//
AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(800, 450);
ClientSize = new Size(789, 597);
Controls.Add(labelTools);
Controls.Add(panelTools);
Controls.Add(pictureBoxCollection);
@ -119,6 +185,8 @@
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit();
panelTools.ResumeLayout(false);
panelTools.PerformLayout();
panelSets.ResumeLayout(false);
panelSets.PerformLayout();
ResumeLayout(false);
PerformLayout();
}
@ -132,5 +200,11 @@
private MaskedTextBox maskedTextBoxNumber;
private Button ButtonRefreshCollection;
private Button ButtonAddAirplane;
private Panel panelSets;
private Label labelSets;
private Button ButtonAddObject;
private Button ButtonRemoveObject;
private ListBox listBoxStorage;
private TextBox textBoxStorageName;
}
}

View File

@ -16,25 +16,96 @@ namespace AirFighter
public partial class FormAirPlaneCollection : Form
{
// Набор объектов
private readonly AirplanesGenericCollection<DrawningAirplane,
DrawningObjectAirplane> _airplanes;
private readonly AirplanesGenericStorage _storage;
public FormAirPlaneCollection()
{
InitializeComponent();
_airplanes = new AirplanesGenericCollection<DrawningAirplane,
DrawningObjectAirplane>(pictureBoxCollection.Width, pictureBoxCollection.Height);
_storage = new AirplanesGenericStorage(pictureBoxCollection.Width,
pictureBoxCollection.Height);
}
// Заполнение listBoxObjects
private void ReloadObjects()
{
int index = listBoxStorage.SelectedIndex;
listBoxStorage.Items.Clear();
for (int i = 0; i < _storage.Keys.Count; i++)
{
listBoxStorage.Items.Add(_storage.Keys[i]);
}
if (listBoxStorage.Items.Count > 0 && (index == -1 || index
>= listBoxStorage.Items.Count))
{
listBoxStorage.SelectedIndex = 0;
}
else if (listBoxStorage.Items.Count > 0 && index > -1 &&
index < listBoxStorage.Items.Count)
{
listBoxStorage.SelectedIndex = index;
}
}
// Добавление набора в коллекцию
private void ButtonAddObject_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBoxStorageName.Text))
{
MessageBox.Show("Не все данные заполнены", "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
_storage.AddSet(textBoxStorageName.Text);
ReloadObjects();
}
// Выбор набора
private void listBoxStorage_SelectedIndexChanged(object sender, EventArgs e)
{
pictureBoxCollection.Image =
_storage[listBoxStorage.SelectedItem?.ToString() ?? string.Empty]?.ShowAirplanes();
}
// Удаление набора
private void ButtonRemoveObject_Click(object sender, EventArgs e)
{
if (listBoxStorage.SelectedIndex == -1)
{
return;
}
if (MessageBox.Show($"Удалить объект {listBoxStorage.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo,
MessageBoxIcon.Question) == DialogResult.Yes)
{
_storage.DelSet(listBoxStorage.SelectedItem.ToString()
?? string.Empty);
ReloadObjects();
}
}
// Добавление объекта в набор
private void ButtonAddAirplane_Click(object sender, EventArgs e)
{
if (listBoxStorage.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorage.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
AirFighterForm form = new();
if (form.ShowDialog() == DialogResult.OK)
{
if (_airplanes + form.SelectedAirplane != -1)
if (obj + form.SelectedAirplane != -1)
{
MessageBox.Show("Объект добавлен");
pictureBoxCollection.Image = _airplanes.ShowAirplanes();
pictureBoxCollection.Image = obj.ShowAirplanes();
}
else
{
@ -45,16 +116,26 @@ namespace AirFighter
private void buttonRemoveAirplane_Click(object sender, EventArgs e)
{
if (listBoxStorage.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorage.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
if (MessageBox.Show("Удалить объект?", "Удаление",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
return;
}
int pos = Convert.ToInt32(maskedTextBoxNumber.Text);
if (_airplanes - pos)
if (obj - pos != null)
{
MessageBox.Show("Объект удален");
pictureBoxCollection.Image = _airplanes.ShowAirplanes();
pictureBoxCollection.Image = obj.ShowAirplanes();
}
else
{
@ -64,7 +145,17 @@ namespace AirFighter
private void ButtonRefreshCollection_Click(object sender, EventArgs e)
{
pictureBoxCollection.Image = _airplanes.ShowAirplanes();
if (listBoxStorage.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorage.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
pictureBoxCollection.Image = obj.ShowAirplanes();
}
}
}

View File

@ -9,15 +9,20 @@ namespace AirFighter.Generics
// Параметризованный набор объектов
internal class SetGeneric<T> where T : class
{
// Массив объектов, которые храним
private readonly T?[] _places;
// Список объектов, которые храним
private readonly List<T?> _places;
// Количество объектов в массиве
public int Count => _places.Length;
public int Count => _places.Count;
// Максимальное количество объектов в списке
public readonly int _maxCount;
// Конструктор
public SetGeneric(int count)
{
_places = new T?[count];
_maxCount = count;
_places = new List<T?>(count);
}
// Добавление объекта в набор
@ -29,45 +34,52 @@ namespace AirFighter.Generics
// Добавление объекта в набор на конкретную позицию
public int Insert(T airplane, int position)
{
int nullIndex = -1, i;
if (position < 0 || position >= Count)
return -1;
for (i = position; i < Count; i++)
{
if (_places[i] == null)
{
nullIndex = i;
break;
}
}
if (nullIndex < 0)
if (position < 0 || position > Count || Count >= _maxCount)
return -1;
for (i = nullIndex; i > position; i--)
{
_places[i] = _places[i - 1];
}
_places[position] = airplane;
_places.Insert(position, airplane);
return position;
}
}
// Удаление объекта из набора с конкретной позиции
public bool Remove(int position)
{
if (position < 0 || position >= Count)
return false;
_places[position] = null;
_places.RemoveAt(position);
return true;
}
// Получение объекта из набора по позиции
public T? Get(int position)
public T? this[int position]
{
if (position < 0 || position >= Count)
return null;
return _places[position];
get
{
if (position < 0 || position >= Count)
return null;
return _places[position];
}
set
{
if (position < 0 || position > Count || Count >= _maxCount)
return;
_places.Insert(position, value);
}
}
// Проход по списку
public IEnumerable<T?> GetAirplanes(int? maxAirplanes = null)
{
for (int i = 0; i < _places.Count; ++i)
{
yield return _places[i];
if (maxAirplanes.HasValue && i == maxAirplanes.Value)
{
yield break;
}
}
}
}
}