lab_4
This commit is contained in:
parent
ce8e5ace33
commit
4de997dcb5
@ -37,7 +37,7 @@ namespace AirFighter.Generics
|
|||||||
}
|
}
|
||||||
public static bool operator -(AirFighterGenericCollection<T, U> collect, int pos)
|
public static bool operator -(AirFighterGenericCollection<T, U> collect, int pos)
|
||||||
{
|
{
|
||||||
T? obj = collect._collection.Get(pos);
|
T? obj = collect._collection[pos];
|
||||||
if (obj != null)
|
if (obj != null)
|
||||||
{
|
{
|
||||||
return collect._collection.Remove(pos);
|
return collect._collection.Remove(pos);
|
||||||
@ -46,7 +46,7 @@ namespace AirFighter.Generics
|
|||||||
}
|
}
|
||||||
public U? GetU(int pos)
|
public U? GetU(int pos)
|
||||||
{
|
{
|
||||||
return (U?)_collection.Get(pos)?.GetMoveableObject;
|
return (U?)_collection[pos]?.GetMoveableObject;
|
||||||
}
|
}
|
||||||
public Bitmap ShowAirFighter()
|
public Bitmap ShowAirFighter()
|
||||||
{
|
{
|
||||||
@ -77,18 +77,16 @@ namespace AirFighter.Generics
|
|||||||
int width = _pictureWidth / _placeSizeWidth;
|
int width = _pictureWidth / _placeSizeWidth;
|
||||||
int height = _pictureHeight / _placeSizeHeight;
|
int height = _pictureHeight / _placeSizeHeight;
|
||||||
|
|
||||||
for (int i = 0; i < _collection.Count; i++)
|
foreach (var fighter in _collection.GetAirFighter())
|
||||||
{
|
{
|
||||||
DrawningAirFighter? fighter = _collection.Get(i);
|
if (fighter != null)
|
||||||
|
{
|
||||||
if (fighter == null)
|
int index = _collection.GetAirFighter().ToList().IndexOf(fighter);
|
||||||
continue;
|
int row = height - 1 - (index / width);
|
||||||
|
int col = width - 1 - (index % width);
|
||||||
int row = height - 1 - (i / width);
|
|
||||||
int col = width - 1 - (i % width);
|
|
||||||
|
|
||||||
fighter.SetPosition(col * _placeSizeWidth + 10, row * _placeSizeHeight + 5);
|
|
||||||
|
|
||||||
|
fighter.SetPosition(col * _placeSizeWidth + 10, row * _placeSizeHeight + 5);
|
||||||
|
}
|
||||||
fighter.DrawTransport(g);
|
fighter.DrawTransport(g);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
55
AirFighter/AirFighter/AirFighterGenericStorage.cs
Normal file
55
AirFighter/AirFighter/AirFighterGenericStorage.cs
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
using AirFighter.Drawnings;
|
||||||
|
using AirFighter.MovementStrategy;
|
||||||
|
using AirFighter.DrawningObjects;
|
||||||
|
|
||||||
|
namespace AirFighter.Generics
|
||||||
|
{
|
||||||
|
internal class AirFighterGenericStorage
|
||||||
|
{
|
||||||
|
readonly Dictionary<string, AirFighterGenericCollection<DrawningAirFighter, DrawningObjectAirFighter>> _AirFighterStorages;
|
||||||
|
public List<string> Keys => _AirFighterStorages.Keys.ToList();
|
||||||
|
|
||||||
|
private readonly int _pictureWidth;
|
||||||
|
|
||||||
|
private readonly int _pictureHeight;
|
||||||
|
public AirFighterGenericStorage(int pictureWidth, int pictureHeight)
|
||||||
|
{
|
||||||
|
_AirFighterStorages = new Dictionary<string, AirFighterGenericCollection<DrawningAirFighter, DrawningObjectAirFighter>>();
|
||||||
|
_pictureWidth = pictureWidth;
|
||||||
|
_pictureHeight = pictureHeight;
|
||||||
|
}
|
||||||
|
public void AddSet(string name)
|
||||||
|
{
|
||||||
|
if (!_AirFighterStorages.ContainsKey(name))
|
||||||
|
{
|
||||||
|
var fighterCollection = new AirFighterGenericCollection<DrawningAirFighter, DrawningObjectAirFighter>(_pictureWidth, _pictureHeight);
|
||||||
|
_AirFighterStorages.Add(name, fighterCollection);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public void DelSet(string name)
|
||||||
|
{
|
||||||
|
if (_AirFighterStorages.ContainsKey(name))
|
||||||
|
{
|
||||||
|
_AirFighterStorages.Remove(name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public AirFighterGenericCollection<DrawningAirFighter, DrawningObjectAirFighter>? this[string ind]
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
if (_AirFighterStorages.ContainsKey(ind))
|
||||||
|
{
|
||||||
|
return _AirFighterStorages[ind];
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,6 +1,6 @@
|
|||||||
namespace AirFighter
|
namespace AirFighter
|
||||||
{
|
{
|
||||||
partial class FormFighterCollection
|
partial class FormAirFighterCollection
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Required designer variable.
|
/// Required designer variable.
|
||||||
@ -29,17 +29,24 @@
|
|||||||
private void InitializeComponent()
|
private void InitializeComponent()
|
||||||
{
|
{
|
||||||
groupBox1 = new GroupBox();
|
groupBox1 = new GroupBox();
|
||||||
|
groupBox2 = new GroupBox();
|
||||||
|
buttonDelObject = new Button();
|
||||||
|
listBoxStorage = new ListBox();
|
||||||
|
buttonAddObject = new Button();
|
||||||
|
textBoxStorageName = new TextBox();
|
||||||
maskedTextBoxNumber = new MaskedTextBox();
|
maskedTextBoxNumber = new MaskedTextBox();
|
||||||
buttonRemoveFighter = new Button();
|
buttonRemoveFighter = new Button();
|
||||||
buttonRefreshCollection = new Button();
|
buttonRefreshCollection = new Button();
|
||||||
buttonAddFighter = new Button();
|
buttonAddFighter = new Button();
|
||||||
pictureBoxCollection = new PictureBox();
|
pictureBoxCollection = new PictureBox();
|
||||||
groupBox1.SuspendLayout();
|
groupBox1.SuspendLayout();
|
||||||
|
groupBox2.SuspendLayout();
|
||||||
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit();
|
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit();
|
||||||
SuspendLayout();
|
SuspendLayout();
|
||||||
//
|
//
|
||||||
// groupBox1
|
// groupBox1
|
||||||
//
|
//
|
||||||
|
groupBox1.Controls.Add(groupBox2);
|
||||||
groupBox1.Controls.Add(maskedTextBoxNumber);
|
groupBox1.Controls.Add(maskedTextBoxNumber);
|
||||||
groupBox1.Controls.Add(buttonRemoveFighter);
|
groupBox1.Controls.Add(buttonRemoveFighter);
|
||||||
groupBox1.Controls.Add(buttonRefreshCollection);
|
groupBox1.Controls.Add(buttonRefreshCollection);
|
||||||
@ -51,10 +58,60 @@
|
|||||||
groupBox1.TabStop = false;
|
groupBox1.TabStop = false;
|
||||||
groupBox1.Text = "Инструменты";
|
groupBox1.Text = "Инструменты";
|
||||||
//
|
//
|
||||||
|
// groupBox2
|
||||||
|
//
|
||||||
|
groupBox2.Controls.Add(buttonDelObject);
|
||||||
|
groupBox2.Controls.Add(listBoxStorage);
|
||||||
|
groupBox2.Controls.Add(buttonAddObject);
|
||||||
|
groupBox2.Controls.Add(textBoxStorageName);
|
||||||
|
groupBox2.Location = new Point(3, 19);
|
||||||
|
groupBox2.Name = "groupBox2";
|
||||||
|
groupBox2.Size = new Size(200, 284);
|
||||||
|
groupBox2.TabIndex = 6;
|
||||||
|
groupBox2.TabStop = false;
|
||||||
|
groupBox2.Text = "Наборы";
|
||||||
|
//
|
||||||
|
// buttonDelObject
|
||||||
|
//
|
||||||
|
buttonDelObject.Location = new Point(18, 244);
|
||||||
|
buttonDelObject.Name = "buttonDelObject";
|
||||||
|
buttonDelObject.Size = new Size(159, 34);
|
||||||
|
buttonDelObject.TabIndex = 2;
|
||||||
|
buttonDelObject.Text = "Удалить набор";
|
||||||
|
buttonDelObject.UseVisualStyleBackColor = true;
|
||||||
|
buttonDelObject.Click += ButtonDelObject_Click;
|
||||||
|
//
|
||||||
|
// listBoxStorage
|
||||||
|
//
|
||||||
|
listBoxStorage.FormattingEnabled = true;
|
||||||
|
listBoxStorage.ItemHeight = 15;
|
||||||
|
listBoxStorage.Location = new Point(15, 88);
|
||||||
|
listBoxStorage.Name = "listBoxStorage";
|
||||||
|
listBoxStorage.Size = new Size(162, 139);
|
||||||
|
listBoxStorage.TabIndex = 2;
|
||||||
|
listBoxStorage.SelectedIndexChanged += listBoxStorage_SelectedIndexChanged;
|
||||||
|
//
|
||||||
|
// buttonAddObject
|
||||||
|
//
|
||||||
|
buttonAddObject.Location = new Point(18, 48);
|
||||||
|
buttonAddObject.Name = "buttonAddObject";
|
||||||
|
buttonAddObject.Size = new Size(159, 34);
|
||||||
|
buttonAddObject.TabIndex = 1;
|
||||||
|
buttonAddObject.Text = "Добавить набор";
|
||||||
|
buttonAddObject.UseVisualStyleBackColor = true;
|
||||||
|
buttonAddObject.Click += ButtonAddObject_Click;
|
||||||
|
//
|
||||||
|
// textBoxStorageName
|
||||||
|
//
|
||||||
|
textBoxStorageName.Location = new Point(3, 19);
|
||||||
|
textBoxStorageName.Name = "textBoxStorageName";
|
||||||
|
textBoxStorageName.Size = new Size(191, 23);
|
||||||
|
textBoxStorageName.TabIndex = 0;
|
||||||
|
//
|
||||||
// maskedTextBoxNumber
|
// maskedTextBoxNumber
|
||||||
//
|
//
|
||||||
maskedTextBoxNumber.Font = new Font("Showcard Gothic", 9F, FontStyle.Regular, GraphicsUnit.Point);
|
maskedTextBoxNumber.Font = new Font("Showcard Gothic", 9F, FontStyle.Regular, GraphicsUnit.Point);
|
||||||
maskedTextBoxNumber.Location = new Point(37, 103);
|
maskedTextBoxNumber.Location = new Point(39, 346);
|
||||||
maskedTextBoxNumber.Mask = "00";
|
maskedTextBoxNumber.Mask = "00";
|
||||||
maskedTextBoxNumber.Name = "maskedTextBoxNumber";
|
maskedTextBoxNumber.Name = "maskedTextBoxNumber";
|
||||||
maskedTextBoxNumber.Size = new Size(100, 22);
|
maskedTextBoxNumber.Size = new Size(100, 22);
|
||||||
@ -62,7 +119,7 @@
|
|||||||
//
|
//
|
||||||
// buttonRemoveFighter
|
// buttonRemoveFighter
|
||||||
//
|
//
|
||||||
buttonRemoveFighter.Location = new Point(37, 155);
|
buttonRemoveFighter.Location = new Point(17, 374);
|
||||||
buttonRemoveFighter.Name = "buttonRemoveFighter";
|
buttonRemoveFighter.Name = "buttonRemoveFighter";
|
||||||
buttonRemoveFighter.Size = new Size(166, 31);
|
buttonRemoveFighter.Size = new Size(166, 31);
|
||||||
buttonRemoveFighter.TabIndex = 3;
|
buttonRemoveFighter.TabIndex = 3;
|
||||||
@ -72,7 +129,7 @@
|
|||||||
//
|
//
|
||||||
// buttonRefreshCollection
|
// buttonRefreshCollection
|
||||||
//
|
//
|
||||||
buttonRefreshCollection.Location = new Point(37, 209);
|
buttonRefreshCollection.Location = new Point(17, 411);
|
||||||
buttonRefreshCollection.Name = "buttonRefreshCollection";
|
buttonRefreshCollection.Name = "buttonRefreshCollection";
|
||||||
buttonRefreshCollection.Size = new Size(166, 31);
|
buttonRefreshCollection.Size = new Size(166, 31);
|
||||||
buttonRefreshCollection.TabIndex = 4;
|
buttonRefreshCollection.TabIndex = 4;
|
||||||
@ -82,7 +139,7 @@
|
|||||||
//
|
//
|
||||||
// buttonAddFighter
|
// buttonAddFighter
|
||||||
//
|
//
|
||||||
buttonAddFighter.Location = new Point(37, 36);
|
buttonAddFighter.Location = new Point(17, 309);
|
||||||
buttonAddFighter.Name = "buttonAddFighter";
|
buttonAddFighter.Name = "buttonAddFighter";
|
||||||
buttonAddFighter.Size = new Size(166, 31);
|
buttonAddFighter.Size = new Size(166, 31);
|
||||||
buttonAddFighter.TabIndex = 2;
|
buttonAddFighter.TabIndex = 2;
|
||||||
@ -111,6 +168,8 @@
|
|||||||
Text = "Набор истребителя";
|
Text = "Набор истребителя";
|
||||||
groupBox1.ResumeLayout(false);
|
groupBox1.ResumeLayout(false);
|
||||||
groupBox1.PerformLayout();
|
groupBox1.PerformLayout();
|
||||||
|
groupBox2.ResumeLayout(false);
|
||||||
|
groupBox2.PerformLayout();
|
||||||
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit();
|
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit();
|
||||||
ResumeLayout(false);
|
ResumeLayout(false);
|
||||||
}
|
}
|
||||||
@ -123,5 +182,10 @@
|
|||||||
private Button buttonRefreshCollection;
|
private Button buttonRefreshCollection;
|
||||||
private Button buttonAddFighter;
|
private Button buttonAddFighter;
|
||||||
private MaskedTextBox maskedTextBoxNumber;
|
private MaskedTextBox maskedTextBoxNumber;
|
||||||
|
private GroupBox groupBox2;
|
||||||
|
private TextBox textBoxStorageName;
|
||||||
|
private Button buttonDelObject;
|
||||||
|
private ListBox listBoxStorage;
|
||||||
|
private Button buttonAddObject;
|
||||||
}
|
}
|
||||||
}
|
}
|
187
AirFighter/AirFighter/FormAirFighterCollection.cs
Normal file
187
AirFighter/AirFighter/FormAirFighterCollection.cs
Normal file
@ -0,0 +1,187 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.ComponentModel;
|
||||||
|
using System.Data;
|
||||||
|
using System.Drawing;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using System.Windows.Forms;
|
||||||
|
using AirFighter.DrawningObjects;
|
||||||
|
using AirFighter.Drawnings;
|
||||||
|
using AirFighter.Generics;
|
||||||
|
using AirFighter.MovementStrategy;
|
||||||
|
|
||||||
|
namespace AirFighter
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Форма для работы с набором объектов класса DrawningCar
|
||||||
|
/// </summary>
|
||||||
|
public partial class FormAirFighterCollection : Form
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Набор объектов
|
||||||
|
/// </summary>
|
||||||
|
private readonly AirFighterGenericStorage _storage;
|
||||||
|
/// <summary>
|
||||||
|
/// Конструктор
|
||||||
|
/// </summary>
|
||||||
|
public FormAirFighterCollection()
|
||||||
|
{
|
||||||
|
InitializeComponent();
|
||||||
|
_storage = new AirFighterGenericStorage(pictureBoxCollection.Width, pictureBoxCollection.Height);
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// Заполнение listBoxObjects
|
||||||
|
/// </summary>
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// Добавление набора в коллекцию
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="sender"></param>
|
||||||
|
/// <param name="e"></param>
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// Выбор набора
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="sender"></param>
|
||||||
|
/// <param name="e"></param>
|
||||||
|
private void listBoxStorage_SelectedIndexChanged(object sender,
|
||||||
|
EventArgs e)
|
||||||
|
{
|
||||||
|
pictureBoxCollection.Image =
|
||||||
|
_storage[listBoxStorage.SelectedItem?.ToString() ?? string.Empty]?.ShowAirFighter();
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// Удаление набора
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="sender"></param>
|
||||||
|
/// <param name="e"></param>
|
||||||
|
private void ButtonDelObject_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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// Добавление объекта в набор
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="sender"></param>
|
||||||
|
/// <param name="e"></param>
|
||||||
|
private void ButtonAddAirFighter_Click(object sender, EventArgs e)
|
||||||
|
{
|
||||||
|
if (listBoxStorage.SelectedIndex == -1)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
var obj = _storage[listBoxStorage.SelectedItem.ToString() ??
|
||||||
|
string.Empty];
|
||||||
|
if (obj == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
FormAirFighter form = new();
|
||||||
|
if (form.ShowDialog() == DialogResult.OK)
|
||||||
|
{
|
||||||
|
if (obj + form.SelectedAirFighter > -1)
|
||||||
|
{
|
||||||
|
MessageBox.Show("Объект добавлен");
|
||||||
|
pictureBoxCollection.Image = obj.ShowAirFighter();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
MessageBox.Show("Не удалось добавить объект");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// Удаление объекта из набора
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="sender"></param>
|
||||||
|
/// <param name="e"></param>
|
||||||
|
private void ButtonRemoveAirFighter_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 (obj - pos != null)
|
||||||
|
{
|
||||||
|
MessageBox.Show("Объект удален");
|
||||||
|
pictureBoxCollection.Image = obj.ShowAirFighter();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
MessageBox.Show("Не удалось удалить объект");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// Обновление рисунка по набору
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="sender"></param>
|
||||||
|
/// <param name="e"></param>
|
||||||
|
private void ButtonRefreshCollection_Click(object sender, EventArgs e)
|
||||||
|
{
|
||||||
|
if (listBoxStorage.SelectedIndex == -1)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
var obj = _storage[listBoxStorage.SelectedItem.ToString() ??
|
||||||
|
string.Empty];
|
||||||
|
if (obj == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
pictureBoxCollection.Image = obj.ShowAirFighter();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,69 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.ComponentModel;
|
|
||||||
using System.Data;
|
|
||||||
using System.Drawing;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using System.Windows.Forms;
|
|
||||||
using AirFighter.DrawningObjects;
|
|
||||||
using AirFighter.Drawnings;
|
|
||||||
using AirFighter.Generics;
|
|
||||||
using AirFighter.MovementStrategy;
|
|
||||||
|
|
||||||
namespace AirFighter
|
|
||||||
{
|
|
||||||
public partial class FormFighterCollection : Form
|
|
||||||
{
|
|
||||||
private readonly AirFighterGenericCollection<DrawningAirFighter, DrawningObjectAirFighter> _fighter;
|
|
||||||
public FormFighterCollection()
|
|
||||||
{
|
|
||||||
InitializeComponent();
|
|
||||||
_fighter = new AirFighterGenericCollection<DrawningAirFighter, DrawningObjectAirFighter>(pictureBoxCollection.Width, pictureBoxCollection.Height);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void ButtonAddAirFighter_Click(object sender, EventArgs e)
|
|
||||||
{
|
|
||||||
FormAirFighter form = new();
|
|
||||||
if (form.ShowDialog() == DialogResult.OK)
|
|
||||||
{
|
|
||||||
if (_fighter + form.SelectedAirFighter != null)
|
|
||||||
{
|
|
||||||
MessageBox.Show("Объект добавлен");
|
|
||||||
pictureBoxCollection.Image = _fighter.ShowAirFighter();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
MessageBox.Show("Не удалось добавить объект");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private void ButtonRemoveAirFighter_Click(object sender, EventArgs e)
|
|
||||||
{
|
|
||||||
if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
int pos = Convert.ToInt32(maskedTextBoxNumber.Text);
|
|
||||||
if (_fighter - pos != null)
|
|
||||||
{
|
|
||||||
MessageBox.Show("Объект удален");
|
|
||||||
pictureBoxCollection.Image = _fighter.ShowAirFighter();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
MessageBox.Show("Не удалось удалить объект");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private void ButtonRefreshCollection_Click(object sender, EventArgs e)
|
|
||||||
{
|
|
||||||
pictureBoxCollection.Image = _fighter.ShowAirFighter();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -11,7 +11,7 @@ namespace AirFighter
|
|||||||
// To customize application configuration such as set high DPI settings or default font,
|
// To customize application configuration such as set high DPI settings or default font,
|
||||||
// see https://aka.ms/applicationconfiguration.
|
// see https://aka.ms/applicationconfiguration.
|
||||||
ApplicationConfiguration.Initialize();
|
ApplicationConfiguration.Initialize();
|
||||||
Application.Run(new FormFighterCollection());
|
Application.Run(new FormAirFighterCollection());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -3,53 +3,63 @@
|
|||||||
internal class SetGeneric<T>
|
internal class SetGeneric<T>
|
||||||
where T : class
|
where T : class
|
||||||
{
|
{
|
||||||
private readonly T[] _places;
|
private readonly List<T?> _places;
|
||||||
public int Count => _places.Length;
|
public int Count => _places.Count;
|
||||||
|
private readonly int _maxCount;
|
||||||
public SetGeneric(int count)
|
public SetGeneric(int count)
|
||||||
{
|
{
|
||||||
_places = new T[count];
|
_maxCount = count;
|
||||||
|
_places = new List<T?>(count);
|
||||||
}
|
}
|
||||||
public int Insert(T fighter)
|
public int Insert(T fighter)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < _places.Length; i++)
|
return Insert(fighter, 0);
|
||||||
{
|
|
||||||
if (_places[i] == null)
|
|
||||||
{
|
|
||||||
_places[i] = fighter;
|
|
||||||
return i;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
private bool isCorrectPosition(int position)
|
||||||
|
{
|
||||||
|
return 0 <= position && position < _maxCount;
|
||||||
|
}
|
||||||
public int Insert(T fighter, int position)
|
public int Insert(T fighter, int position)
|
||||||
{
|
{
|
||||||
int index = position;
|
if (!isCorrectPosition(position))
|
||||||
|
{
|
||||||
while (_places[index] != null && index < _places.Length) index++;
|
return -1;
|
||||||
|
}
|
||||||
if (index == _places.Length) return -1;
|
_places.Insert(position, fighter);
|
||||||
for (int i = index; i > position; --i) _places[i] = _places[i - 1];
|
|
||||||
|
|
||||||
_places[position] = fighter;
|
|
||||||
return position;
|
return position;
|
||||||
}
|
}
|
||||||
public bool Remove(int position)
|
public bool Remove(int position)
|
||||||
{
|
{
|
||||||
if (position < 0 || position >= _places.Length)
|
if ((position < 0) || (position > _maxCount)) return false;
|
||||||
{
|
_places.RemoveAt(position);
|
||||||
return false;
|
|
||||||
}
|
|
||||||
_places[position] = null;
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
public T? Get(int position)
|
public T? this[int position]
|
||||||
{
|
{
|
||||||
if (position < 0 || position >= _places.Length)
|
get
|
||||||
{
|
{
|
||||||
return null;
|
if (position < 0 || position > _maxCount)
|
||||||
|
return null;
|
||||||
|
return _places[position];
|
||||||
|
}
|
||||||
|
set
|
||||||
|
{
|
||||||
|
if (position < 0 || position > _maxCount)
|
||||||
|
return;
|
||||||
|
_places[position] = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public IEnumerable<T?> GetAirFighter(int? maxAirFighter = null)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < _places.Count; ++i)
|
||||||
|
{
|
||||||
|
yield return _places[i];
|
||||||
|
if (maxAirFighter.HasValue && i == maxAirFighter.Value)
|
||||||
|
{
|
||||||
|
yield break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return _places[position];
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user