полуготовая лаба4
This commit is contained in:
parent
6be2956699
commit
88833f3f18
@ -43,24 +43,22 @@ namespace AircraftCarrier.Generics
|
||||
return collect._collection.Insert(obj);
|
||||
}
|
||||
/// Перегрузка оператора вычитания
|
||||
public static T? operator -(AircraftsGenericCollection<T, U> collect, int pos)
|
||||
public static bool operator -(AircraftsGenericCollection<T, U> collect, int pos)
|
||||
{
|
||||
T? obj = collect._collection[pos];
|
||||
if (obj != null)
|
||||
{
|
||||
collect._collection.Remove(pos);
|
||||
return collect._collection.Remove(pos);
|
||||
}
|
||||
return obj;
|
||||
return false;
|
||||
}
|
||||
|
||||
/// Получение объекта IMoveableObject
|
||||
public U? GetU(int pos)
|
||||
{
|
||||
return (U?)_collection[pos]?.GetMoveableObject;
|
||||
}
|
||||
|
||||
/// Вывод всего набора объектов
|
||||
public Bitmap ShowAircraft()
|
||||
public Bitmap ShowAircrafts()
|
||||
{
|
||||
Bitmap bmp = new(_pictureWidth, _pictureHeight);
|
||||
Graphics gr = Graphics.FromImage(bmp);
|
||||
@ -89,15 +87,18 @@ namespace AircraftCarrier.Generics
|
||||
/// Метод прорисовки объектов
|
||||
private void DrawObjects(Graphics g)
|
||||
{
|
||||
foreach (var car in _collection.GetAircrafts())
|
||||
int Width = _pictureWidth / _placeSizeWidth;
|
||||
int Height = _pictureHeight / _placeSizeHeight;
|
||||
int i = 0;
|
||||
foreach (var aircraft in _collection.GetAircrafts())
|
||||
{
|
||||
if (car != null)
|
||||
if (aircraft != null)
|
||||
{
|
||||
// TODO получение объекта
|
||||
// TODO установка позиции
|
||||
// TODO прорисовка объекта
|
||||
car.DrawTransport(g);
|
||||
|
||||
aircraft.SetPosition(i % Width * _placeSizeWidth + 3, 600);
|
||||
aircraft.DrawTransport(g);
|
||||
}
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -30,12 +30,18 @@ namespace AircraftCarrier.Generics
|
||||
/// Добавление набора
|
||||
public void AddSet(string name)
|
||||
{
|
||||
// TODO Прописать логику для добавления
|
||||
if (!_AircraftStorages.ContainsKey(name))
|
||||
{
|
||||
_AircraftStorages.Add(name, new AircraftsGenericCollection<DrawningAircraft, DrawningObjectAircraft>(_pictureWidth, _pictureHeight));
|
||||
}
|
||||
}
|
||||
/// Удаление набора
|
||||
public void DelSet(string name)
|
||||
{
|
||||
// TODO Прописать логику для удаления
|
||||
if (_AircraftStorages.ContainsKey(name))
|
||||
{
|
||||
_AircraftStorages.Remove(name);
|
||||
}
|
||||
}
|
||||
/// Доступ к набору
|
||||
public AircraftsGenericCollection<DrawningAircraft, DrawningObjectAircraft>?
|
||||
@ -43,7 +49,10 @@ namespace AircraftCarrier.Generics
|
||||
{
|
||||
get
|
||||
{
|
||||
// TODO Продумать логику получения набора
|
||||
if (_AircraftStorages.ContainsKey(ind))
|
||||
{
|
||||
return _AircraftStorages[ind];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
@ -50,7 +50,6 @@ namespace AircraftCarrier
|
||||
}
|
||||
Draw();
|
||||
}
|
||||
|
||||
private void ButtonCreateAircraft_Click(object sender, EventArgs e)
|
||||
{
|
||||
Random random = new();
|
||||
@ -67,7 +66,6 @@ namespace AircraftCarrier
|
||||
100));
|
||||
Draw();
|
||||
}
|
||||
|
||||
private void ButtonCreateAircraftCarrier_Click(object sender, EventArgs e)
|
||||
{
|
||||
Random random = new();
|
||||
@ -90,7 +88,6 @@ namespace AircraftCarrier
|
||||
_drawingAircraft.SetPosition(random.Next(10, 100), random.Next(10, 100));
|
||||
Draw();
|
||||
}
|
||||
|
||||
private void buttonStep_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (_drawingAircraft == null)
|
||||
|
@ -28,31 +28,31 @@
|
||||
/// </summary>
|
||||
private void InitializeComponent()
|
||||
{
|
||||
PictureBoxCollection = new PictureBox();
|
||||
pictureBoxCollection = new PictureBox();
|
||||
PanelTools = new Panel();
|
||||
labelTools = new Label();
|
||||
PanelSets = new Panel();
|
||||
ButtonRemoveObject = new Button();
|
||||
listBoxStorage = new ListBox();
|
||||
ButtonAddObject = new Button();
|
||||
textBoxStorageName = new TextBox();
|
||||
labelSets = new Label();
|
||||
ButtonRefreshCollection = new Button();
|
||||
ButtonRemoveAircraft = new Button();
|
||||
MaskedTextBoxNumber = new MaskedTextBox();
|
||||
ButtonAddAircraft = new Button();
|
||||
labelTools = new Label();
|
||||
labelSets = new Label();
|
||||
textBoxStorageName = new TextBox();
|
||||
ButtonAddObject = new Button();
|
||||
listBoxStorage = new ListBox();
|
||||
ButtonRemoveObject = new Button();
|
||||
((System.ComponentModel.ISupportInitialize)PictureBoxCollection).BeginInit();
|
||||
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit();
|
||||
PanelTools.SuspendLayout();
|
||||
PanelSets.SuspendLayout();
|
||||
SuspendLayout();
|
||||
//
|
||||
// PictureBoxCollection
|
||||
// pictureBoxCollection
|
||||
//
|
||||
PictureBoxCollection.Location = new Point(0, 0);
|
||||
PictureBoxCollection.Name = "PictureBoxCollection";
|
||||
PictureBoxCollection.Size = new Size(768, 651);
|
||||
PictureBoxCollection.TabIndex = 0;
|
||||
PictureBoxCollection.TabStop = false;
|
||||
pictureBoxCollection.Location = new Point(0, 0);
|
||||
pictureBoxCollection.Name = "pictureBoxCollection";
|
||||
pictureBoxCollection.Size = new Size(793, 724);
|
||||
pictureBoxCollection.TabIndex = 0;
|
||||
pictureBoxCollection.TabStop = false;
|
||||
//
|
||||
// PanelTools
|
||||
//
|
||||
@ -63,11 +63,20 @@
|
||||
PanelTools.Controls.Add(ButtonRemoveAircraft);
|
||||
PanelTools.Controls.Add(MaskedTextBoxNumber);
|
||||
PanelTools.Controls.Add(ButtonAddAircraft);
|
||||
PanelTools.Location = new Point(774, 12);
|
||||
PanelTools.Location = new Point(799, 12);
|
||||
PanelTools.Name = "PanelTools";
|
||||
PanelTools.Size = new Size(232, 639);
|
||||
PanelTools.Size = new Size(232, 712);
|
||||
PanelTools.TabIndex = 1;
|
||||
//
|
||||
// labelTools
|
||||
//
|
||||
labelTools.AutoSize = true;
|
||||
labelTools.Location = new Point(16, -4);
|
||||
labelTools.Name = "labelTools";
|
||||
labelTools.Size = new Size(103, 20);
|
||||
labelTools.TabIndex = 5;
|
||||
labelTools.Text = "Инструменты";
|
||||
//
|
||||
// PanelSets
|
||||
//
|
||||
PanelSets.Controls.Add(ButtonRemoveObject);
|
||||
@ -80,6 +89,52 @@
|
||||
PanelSets.Size = new Size(204, 339);
|
||||
PanelSets.TabIndex = 4;
|
||||
//
|
||||
// ButtonRemoveObject
|
||||
//
|
||||
ButtonRemoveObject.Location = new Point(20, 286);
|
||||
ButtonRemoveObject.Name = "ButtonRemoveObject";
|
||||
ButtonRemoveObject.Size = new Size(168, 41);
|
||||
ButtonRemoveObject.TabIndex = 4;
|
||||
ButtonRemoveObject.Text = "Удалить набор";
|
||||
ButtonRemoveObject.UseVisualStyleBackColor = true;
|
||||
ButtonRemoveObject.Click += ButtonRemoveObject_Click;
|
||||
//
|
||||
// listBoxStorage
|
||||
//
|
||||
listBoxStorage.FormattingEnabled = true;
|
||||
listBoxStorage.ItemHeight = 20;
|
||||
listBoxStorage.Location = new Point(29, 147);
|
||||
listBoxStorage.Name = "listBoxStorage";
|
||||
listBoxStorage.Size = new Size(150, 124);
|
||||
listBoxStorage.TabIndex = 3;
|
||||
listBoxStorage.SelectedIndexChanged += listBoxStorage_SelectedIndexChanged;
|
||||
//
|
||||
// ButtonAddObject
|
||||
//
|
||||
ButtonAddObject.Location = new Point(20, 83);
|
||||
ButtonAddObject.Name = "ButtonAddObject";
|
||||
ButtonAddObject.Size = new Size(168, 41);
|
||||
ButtonAddObject.TabIndex = 2;
|
||||
ButtonAddObject.Text = "Добавить набор";
|
||||
ButtonAddObject.UseVisualStyleBackColor = true;
|
||||
ButtonAddObject.Click += ButtonAddObject_Click;
|
||||
//
|
||||
// textBoxStorageName
|
||||
//
|
||||
textBoxStorageName.Location = new Point(41, 41);
|
||||
textBoxStorageName.Name = "textBoxStorageName";
|
||||
textBoxStorageName.Size = new Size(125, 27);
|
||||
textBoxStorageName.TabIndex = 1;
|
||||
//
|
||||
// labelSets
|
||||
//
|
||||
labelSets.AutoSize = true;
|
||||
labelSets.Location = new Point(11, 9);
|
||||
labelSets.Name = "labelSets";
|
||||
labelSets.Size = new Size(55, 20);
|
||||
labelSets.TabIndex = 0;
|
||||
labelSets.Text = "Набор";
|
||||
//
|
||||
// ButtonRefreshCollection
|
||||
//
|
||||
ButtonRefreshCollection.Location = new Point(36, 572);
|
||||
@ -118,68 +173,16 @@
|
||||
ButtonAddAircraft.UseVisualStyleBackColor = true;
|
||||
ButtonAddAircraft.Click += ButtonAddAircraft_Click;
|
||||
//
|
||||
// labelTools
|
||||
//
|
||||
labelTools.AutoSize = true;
|
||||
labelTools.Location = new Point(16, -4);
|
||||
labelTools.Name = "labelTools";
|
||||
labelTools.Size = new Size(103, 20);
|
||||
labelTools.TabIndex = 5;
|
||||
labelTools.Text = "Инструменты";
|
||||
//
|
||||
// labelSets
|
||||
//
|
||||
labelSets.AutoSize = true;
|
||||
labelSets.Location = new Point(11, 9);
|
||||
labelSets.Name = "labelSets";
|
||||
labelSets.Size = new Size(55, 20);
|
||||
labelSets.TabIndex = 0;
|
||||
labelSets.Text = "Набор";
|
||||
//
|
||||
// textBoxStorageName
|
||||
//
|
||||
textBoxStorageName.Location = new Point(41, 41);
|
||||
textBoxStorageName.Name = "textBoxStorageName";
|
||||
textBoxStorageName.Size = new Size(125, 27);
|
||||
textBoxStorageName.TabIndex = 1;
|
||||
//
|
||||
// ButtonAddObject
|
||||
//
|
||||
ButtonAddObject.Location = new Point(20, 83);
|
||||
ButtonAddObject.Name = "ButtonAddObject";
|
||||
ButtonAddObject.Size = new Size(168, 41);
|
||||
ButtonAddObject.TabIndex = 2;
|
||||
ButtonAddObject.Text = "Добавить набор";
|
||||
ButtonAddObject.UseVisualStyleBackColor = true;
|
||||
//
|
||||
// listBoxStorage
|
||||
//
|
||||
listBoxStorage.FormattingEnabled = true;
|
||||
listBoxStorage.ItemHeight = 20;
|
||||
listBoxStorage.Location = new Point(29, 147);
|
||||
listBoxStorage.Name = "listBoxStorage";
|
||||
listBoxStorage.Size = new Size(150, 124);
|
||||
listBoxStorage.TabIndex = 3;
|
||||
//
|
||||
// ButtonRemoveObject
|
||||
//
|
||||
ButtonRemoveObject.Location = new Point(20, 286);
|
||||
ButtonRemoveObject.Name = "ButtonRemoveObject";
|
||||
ButtonRemoveObject.Size = new Size(168, 41);
|
||||
ButtonRemoveObject.TabIndex = 4;
|
||||
ButtonRemoveObject.Text = "Удалить набор";
|
||||
ButtonRemoveObject.UseVisualStyleBackColor = true;
|
||||
//
|
||||
// FormAircraftCollection
|
||||
//
|
||||
AutoScaleDimensions = new SizeF(8F, 20F);
|
||||
AutoScaleMode = AutoScaleMode.Font;
|
||||
ClientSize = new Size(1007, 651);
|
||||
ClientSize = new Size(1033, 724);
|
||||
Controls.Add(PanelTools);
|
||||
Controls.Add(PictureBoxCollection);
|
||||
Controls.Add(pictureBoxCollection);
|
||||
Name = "FormAircraftCollection";
|
||||
Text = "FormAircraftCollection";
|
||||
((System.ComponentModel.ISupportInitialize)PictureBoxCollection).EndInit();
|
||||
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit();
|
||||
PanelTools.ResumeLayout(false);
|
||||
PanelTools.PerformLayout();
|
||||
PanelSets.ResumeLayout(false);
|
||||
@ -189,7 +192,7 @@
|
||||
|
||||
#endregion
|
||||
|
||||
private PictureBox PictureBoxCollection;
|
||||
private PictureBox pictureBoxCollection;
|
||||
private Panel PanelTools;
|
||||
private MaskedTextBox MaskedTextBoxNumber;
|
||||
private Button ButtonAddAircraft;
|
||||
|
@ -14,40 +14,107 @@ namespace AircraftCarrier
|
||||
{
|
||||
public partial class FormAircraftCollection : Form
|
||||
{
|
||||
private readonly AircraftsGenericCollection<DrawningAircraft, DrawningObjectAircraft> _Aircrafts;
|
||||
private readonly AircraftsGenericStorage _storage;
|
||||
public FormAircraftCollection()
|
||||
{
|
||||
InitializeComponent();
|
||||
_Aircrafts = new AircraftsGenericCollection<DrawningAircraft, DrawningObjectAircraft>(PictureBoxCollection.Width, PictureBoxCollection.Height);
|
||||
_storage = new AircraftsGenericStorage(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]?.ShowAircrafts();
|
||||
}
|
||||
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 ButtonAddAircraft_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (listBoxStorage.SelectedIndex == -1)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var obj = _storage[listBoxStorage.SelectedItem.ToString() ?? string.Empty];
|
||||
if (obj == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
FormAircraftCarrier form = new();
|
||||
if (form.ShowDialog() == DialogResult.OK)
|
||||
{
|
||||
if (_Aircrafts + form.SelectedAircraft != -1)
|
||||
if (obj + form.SelectedAircraft != -1)
|
||||
{
|
||||
MessageBox.Show("Объект добавлен");
|
||||
PictureBoxCollection.Image = _Aircrafts.ShowAircraft();
|
||||
pictureBoxCollection.Image = obj.ShowAircrafts();
|
||||
}
|
||||
else
|
||||
{
|
||||
MessageBox.Show("Не удалось добавить объект");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
private void ButtonRemoveAircraft_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
|
||||
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 (_Aircrafts - pos != null)
|
||||
if (obj - pos != null)
|
||||
{
|
||||
MessageBox.Show("Объект удален");
|
||||
PictureBoxCollection.Image = _Aircrafts.ShowAircraft();
|
||||
pictureBoxCollection.Image = obj.ShowAircrafts();
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -56,9 +123,17 @@ namespace AircraftCarrier
|
||||
}
|
||||
private void ButtonRefreshCollection_Click(object sender, EventArgs e)
|
||||
{
|
||||
PictureBoxCollection.Image = _Aircrafts.ShowAircraft();
|
||||
if (listBoxStorage.SelectedIndex == -1)
|
||||
{
|
||||
return;
|
||||
}
|
||||
var obj = _storage[listBoxStorage.SelectedItem.ToString() ??
|
||||
string.Empty];
|
||||
if (obj == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
pictureBoxCollection.Image = obj.ShowAircrafts();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -21,25 +21,25 @@ namespace AircraftCarrier.Generics
|
||||
_places = new List<T?>(count);
|
||||
}
|
||||
/// Добавление объекта в набор
|
||||
public bool Insert(T Aircraft)
|
||||
public int Insert(T Aircraft)
|
||||
{
|
||||
// TODO вставка в начало набора
|
||||
return true;
|
||||
return Insert(Aircraft, 0);
|
||||
}
|
||||
/// Добавление объекта в набор на конкретную позицию
|
||||
public bool Insert(T Aircraft, int position)
|
||||
public int Insert(T Aircraft, int position)
|
||||
{
|
||||
// TODO проверка позиции
|
||||
// TODO проверка, что есть место для вставки
|
||||
// TODO вставка по позиции
|
||||
_places[position] = Aircraft;
|
||||
return true;
|
||||
if (position < 0 || position > Count || Count >= _maxCount)
|
||||
return -1;
|
||||
|
||||
_places.Insert(position, Aircraft);
|
||||
return position;
|
||||
}
|
||||
/// Удаление объекта из набора с конкретной позиции
|
||||
public bool Remove(int position)
|
||||
{
|
||||
// TODO проверка позиции
|
||||
// TODO удаление объекта из списка
|
||||
if (position < 0 || position >= Count)
|
||||
return false;
|
||||
_places.RemoveAt(position);
|
||||
return true;
|
||||
}
|
||||
/// Получение объекта из набора по позиции
|
||||
@ -47,14 +47,16 @@ namespace AircraftCarrier.Generics
|
||||
{
|
||||
get
|
||||
{
|
||||
// TODO проверка позиции
|
||||
if (position < 0 || position >= Count)
|
||||
return null;
|
||||
return _places[position];
|
||||
}
|
||||
set
|
||||
{
|
||||
// TODO проверка позиции
|
||||
// TODO проверка свободных мест в списке
|
||||
// TODO вставка в список по позиции
|
||||
if (position < 0 || position > Count || Count >= _maxCount)
|
||||
return;
|
||||
|
||||
_places.Insert(position, value);
|
||||
}
|
||||
}
|
||||
/// Проход по списку
|
||||
|
Loading…
Reference in New Issue
Block a user