diff --git a/ProjectTank/FormTankCollection.Designer.cs b/ProjectTank/FormTankCollection.Designer.cs
index dfa4a0d..a34b5a1 100644
--- a/ProjectTank/FormTankCollection.Designer.cs
+++ b/ProjectTank/FormTankCollection.Designer.cs
@@ -34,6 +34,11 @@
button2 = new Button();
button4 = new Button();
maskedTextBoxNumber = new TextBox();
+ label2 = new Label();
+ textBoxStorageName = new TextBox();
+ button1 = new Button();
+ listBoxStorages = new ListBox();
+ button5 = new Button();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit();
SuspendLayout();
//
@@ -56,7 +61,7 @@
//
// button3
//
- button3.Location = new Point(775, 247);
+ button3.Location = new Point(775, 448);
button3.Name = "button3";
button3.Size = new Size(139, 29);
button3.TabIndex = 4;
@@ -66,7 +71,7 @@
//
// button2
//
- button2.Location = new Point(775, 166);
+ button2.Location = new Point(775, 393);
button2.Name = "button2";
button2.Size = new Size(139, 29);
button2.TabIndex = 5;
@@ -76,7 +81,7 @@
//
// button4
//
- button4.Location = new Point(775, 32);
+ button4.Location = new Point(775, 306);
button4.Name = "button4";
button4.Size = new Size(139, 29);
button4.TabIndex = 6;
@@ -86,16 +91,67 @@
//
// maskedTextBoxNumber
//
- maskedTextBoxNumber.Location = new Point(775, 133);
+ maskedTextBoxNumber.Location = new Point(775, 360);
maskedTextBoxNumber.Name = "maskedTextBoxNumber";
maskedTextBoxNumber.Size = new Size(139, 27);
maskedTextBoxNumber.TabIndex = 7;
//
+ // label2
+ //
+ label2.AutoSize = true;
+ label2.Location = new Point(775, 51);
+ label2.Name = "label2";
+ label2.Size = new Size(66, 20);
+ label2.TabIndex = 8;
+ label2.Text = "Наборы";
+ //
+ // textBoxStorageName
+ //
+ textBoxStorageName.Location = new Point(775, 74);
+ textBoxStorageName.Name = "textBoxStorageName";
+ textBoxStorageName.Size = new Size(139, 27);
+ textBoxStorageName.TabIndex = 9;
+ //
+ // button1
+ //
+ button1.Location = new Point(775, 107);
+ button1.Name = "button1";
+ button1.Size = new Size(139, 29);
+ button1.TabIndex = 10;
+ button1.Text = "Добавить набор";
+ button1.UseVisualStyleBackColor = true;
+ button1.Click += ButtonAddObject_Click;
+ //
+ // listBoxStorages
+ //
+ listBoxStorages.FormattingEnabled = true;
+ listBoxStorages.ItemHeight = 20;
+ listBoxStorages.Location = new Point(775, 142);
+ listBoxStorages.Name = "listBoxStorages";
+ listBoxStorages.Size = new Size(139, 84);
+ listBoxStorages.TabIndex = 11;
+ listBoxStorages.SelectedIndexChanged += ListBoxObjects_SelectedIndexChanged;
+ //
+ // button5
+ //
+ button5.Location = new Point(775, 232);
+ button5.Name = "button5";
+ button5.Size = new Size(139, 29);
+ button5.TabIndex = 12;
+ button5.Text = "Удалить набор";
+ button5.UseVisualStyleBackColor = true;
+ button5.Click += ButtonDelObject_Click;
+ //
// FormTankCollection
//
AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(926, 486);
+ Controls.Add(button5);
+ Controls.Add(listBoxStorages);
+ Controls.Add(button1);
+ Controls.Add(textBoxStorageName);
+ Controls.Add(label2);
Controls.Add(maskedTextBoxNumber);
Controls.Add(button4);
Controls.Add(button2);
@@ -117,5 +173,10 @@
private Button button2;
private Button button4;
private TextBox maskedTextBoxNumber;
+ private Label label2;
+ private TextBox textBoxStorageName;
+ private Button button1;
+ private ListBox listBoxStorages;
+ private Button button5;
}
}
\ No newline at end of file
diff --git a/ProjectTank/FormTankCollection.cs b/ProjectTank/FormTankCollection.cs
index 56bfb36..68f2196 100644
--- a/ProjectTank/FormTankCollection.cs
+++ b/ProjectTank/FormTankCollection.cs
@@ -6,19 +6,73 @@ namespace ProjectTank
{
public partial class FormTankCollection : Form
{
- ///
- /// Набор объектов
- ///
- private readonly TanksGenericCollection _tanks;
+ private readonly TanksGenericStorage _storage;
///
/// Конструктор
///
public FormTankCollection()
{
InitializeComponent();
- _tanks = new TanksGenericCollection(pictureBoxCollection.Width,
+ _storage = new TanksGenericStorage(pictureBoxCollection.Width,
pictureBoxCollection.Height);
}
+
+ private void ReloadObjects()
+ {
+ int index = listBoxStorages.SelectedIndex;
+ listBoxStorages.Items.Clear();
+
+ for (int i = 0; i < _storage.Keys.Count; i++)
+ {
+ listBoxStorages.Items.Add(_storage.Keys[i]);
+ }
+
+ if (listBoxStorages.Items.Count > 0 && (index == -1 || index
+ >= listBoxStorages.Items.Count))
+ {
+ listBoxStorages.SelectedIndex = 0;
+ }
+ else if (listBoxStorages.Items.Count > 0 && index > -1 &&
+ index < listBoxStorages.Items.Count)
+ {
+ listBoxStorages.SelectedIndex = index;
+ }
+ }
+
+ private void ButtonAddObject_Click(object sender, EventArgs e)
+ {
+ string objName = textBoxStorageName.Text;
+ if (string.IsNullOrEmpty(objName))
+ {
+ MessageBox.Show("Не все данные заполнены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
+ return;
+ }
+ _storage.AddSet(textBoxStorageName.Text);
+ ReloadObjects();
+ }
+
+ private void ListBoxObjects_SelectedIndexChanged(object sender, EventArgs e)
+ {
+ pictureBoxCollection.Image = _storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowTanks();
+ }
+ ///
+ /// Удаление набора
+ ///
+ ///
+ ///
+ private void ButtonDelObject_Click(object sender, EventArgs e)
+ {
+ if (listBoxStorages.SelectedIndex == -1) return;
+
+ if (MessageBox.Show($"Удалить объект {listBoxStorages.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo,
+ MessageBoxIcon.Question) == DialogResult.Yes)
+ {
+ _storage.DelSet(listBoxStorages.SelectedItem.ToString()
+ ?? string.Empty);
+ ReloadObjects();
+ }
+ }
+
///
/// Добавление объекта в набор
///
@@ -26,18 +80,20 @@ namespace ProjectTank
///
private void ButtonAddTank_Click(object sender, EventArgs e)
{
+ if (listBoxStorages.SelectedIndex == -1) return;
+
+ var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
+ if (obj == null) return;
+
FormTank form = new();
if (form.ShowDialog() == DialogResult.OK)
{
- if (_tanks + form.SelectedTank != null)
+ if (obj + form.SelectedTank)
{
MessageBox.Show("Объект добавлен");
- pictureBoxCollection.Image = _tanks.ShowTanks();
- }
- else
- {
- MessageBox.Show("Не удалось добавить объект");
+ pictureBoxCollection.Image = obj.ShowTanks();
}
+ else MessageBox.Show("Не удалось добавить объект");
}
}
///
@@ -47,32 +103,41 @@ namespace ProjectTank
///
private void ButtonRemoveTank_Click(object sender, EventArgs e)
{
+ if (listBoxStorages.SelectedIndex == -1) return;
+
+ var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
+ if (obj == null) return;
+
if (MessageBox.Show("Удалить объект?", "Удаление",
- MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
+ MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
return;
}
int pos = Convert.ToInt32(maskedTextBoxNumber.Text);
- if (_tanks - pos != null)
+ if (obj - pos != null)
{
MessageBox.Show("Объект удален");
- pictureBoxCollection.Image = _tanks.ShowTanks();
- maskedTextBoxNumber.Text = "";
- }
- else
- {
- MessageBox.Show("Не удалось удалить объект");
+ pictureBoxCollection.Image = obj.ShowTanks();
}
+ else MessageBox.Show("Не удалось удалить объект");
}
+
///
/// Обновление рисунка по набору
///
///
///
+
private void ButtonRefreshCollection_Click(object sender, EventArgs e)
{
- pictureBoxCollection.Image = _tanks.ShowTanks();
+ if (listBoxStorages.SelectedIndex == -1) return;
+
+ var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
+ if (obj == null) return;
+
+ pictureBoxCollection.Image = obj.ShowTanks();
}
+
}
}
diff --git a/ProjectTank/Generics/SetGeneric.cs b/ProjectTank/Generics/SetGeneric.cs
index 68491e6..f9d21e4 100644
--- a/ProjectTank/Generics/SetGeneric.cs
+++ b/ProjectTank/Generics/SetGeneric.cs
@@ -9,28 +9,31 @@ namespace ProjectTank.Generics
///
/// Массив объектов, которые храним
///
- private readonly T?[] _places;
+ private readonly List _places;
///
/// Количество объектов в массиве
///
- public int Count => _places.Length;
+ public int Count => _places.Count;
+
///
/// Конструктор
///
///
+ private readonly int _maxCount;
public SetGeneric(int count)
{
- _places = new T?[count];
+ _maxCount = count;
+ _places = new List(count);
}
///
/// Добавление объекта в набор
///
/// Добавляемый автомобиль
///
- public int Insert(T tank)
+ public bool Insert(T tank)
{
- if (_places[Count-1] != null)
- return -1;
+ if (_places.Count == _maxCount)
+ return false;
return Insert(tank, 0);
}
///
@@ -39,22 +42,12 @@ namespace ProjectTank.Generics
/// Добавляемый автомобиль
/// Позиция
///
- public int Insert(T tank, int position)
+ public bool Insert(T tank, int position)
{
- if (!(position >= 0 && position < Count)) return -1;
-
- if (_places[position] != null)
- {
- int ind = position;
- while (ind < Count && _places[ind] != null)
- ind++;
- if (ind == Count)
- return -1;
- for (int i = ind - 1; i >= position; i--)
- _places[i + 1] = _places[i];
- }
- _places[position] = tank;
- return position;
+ if (!(position >= 0 && position <= Count && _places.Count < _maxCount))
+ return false;
+ _places.Insert(position, tank);
+ return true;
}
///
/// Удаление объекта из набора с конкретной позиции
@@ -63,20 +56,40 @@ namespace ProjectTank.Generics
///
public bool Remove(int position)
{
- if (!(position >= 0 && position < Count) || _places[position] == null)
+ if (!(position >= 0 && position < Count))
return false;
- _places[position] = null;
+ _places.RemoveAt(position);
return true;
}
+
+ public T? this[int position]
+ {
+ get {
+ if (!(position >= 0 && position < Count))
+ return null;
+ return _places[position];
+ }
+ set {
+ if (!(position >= 0 && position < Count && _places.Count < _maxCount))
+ return;
+ _places.Insert(position, value);
+ }
+ }
///
/// Получение объекта из набора по позиции
///
///
///
- public T? Get(int position)
+ public IEnumerable GetTanks(int? maxTanks = null)
{
- if (!(position >= 0 && position < Count)) return null;
- return _places[position];
+ for (int i = 0; i < _places.Count; ++i)
+ {
+ yield return _places[i];
+ if (maxTanks.HasValue && i == maxTanks.Value)
+ {
+ yield break;
+ }
+ }
}
}
}
\ No newline at end of file
diff --git a/ProjectTank/Generics/TanksGenericCollection.cs b/ProjectTank/Generics/TanksGenericCollection.cs
index 2c7d75f..acd1c4d 100644
--- a/ProjectTank/Generics/TanksGenericCollection.cs
+++ b/ProjectTank/Generics/TanksGenericCollection.cs
@@ -49,10 +49,12 @@ namespace ProjectTank.Generics
///
///
///
- public static int operator +(TanksGenericCollection collect, T? obj)
+ public static bool operator +(TanksGenericCollection collect, T? obj)
{
- if (obj == null) return -1;
- return collect?._collection.Insert(obj) ?? -1;
+ if (obj == null)
+ return false;
+
+ return collect?._collection.Insert(obj) ?? false;
}
///
/// Перегрузка оператора вычитания
@@ -60,13 +62,17 @@ namespace ProjectTank.Generics
///
///
///
- public static bool operator -(TanksGenericCollection collect, int pos)
+ public static T? operator -(TanksGenericCollection collect, int
+ pos)
{
- T? obj = collect._collection.Get(pos);
-
- if (obj != null) return collect._collection.Remove(pos);
- return false;
+ T? obj = collect._collection[pos];
+ if (obj != null)
+ {
+ collect._collection.Remove(pos);
+ }
+ return obj;
}
+
///
/// Получение объекта IMoveableObject
@@ -75,7 +81,7 @@ namespace ProjectTank.Generics
///
public U? GetU(int pos)
{
- return (U?)_collection.Get(pos)?.GetMoveableObject;
+ return (U?)_collection[pos]?.GetMoveableObject;
}
///
/// Вывод всего набора объектов
@@ -113,9 +119,9 @@ namespace ProjectTank.Generics
///
private void DrawObjects(Graphics g)
{
- for (int i = 0; i < _collection.Count; i++)
+ int i = 0;
+ foreach (var tank in _collection.GetTanks())
{
- DrawningTankBase tank = _collection.Get(i);
if (tank != null)
{
int columnsCount = _pictureHeight / _placeSizeHeight + 1;
@@ -125,6 +131,8 @@ namespace ProjectTank.Generics
tank.SetPosition(colIndex * _placeSizeWidth + 2, rowIndex * _placeSizeHeight + 2);
tank.DrawTransport(g);
+
+ i++;
}
}
}
diff --git a/ProjectTank/Generics/TanksGenericStorage.cs b/ProjectTank/Generics/TanksGenericStorage.cs
new file mode 100644
index 0000000..b0f23f3
--- /dev/null
+++ b/ProjectTank/Generics/TanksGenericStorage.cs
@@ -0,0 +1,75 @@
+using ProjectTank.DrawningObjects;
+using ProjectTank.MovementStrategy;
+
+namespace ProjectTank.Generics
+{
+ ///
+ /// Класс для хранения коллекции
+ ///
+ internal class TanksGenericStorage
+ {
+ ///
+ /// Словарь (хранилище)
+ ///
+ readonly Dictionary> _tankStorages;
+ ///
+ /// Возвращение списка названий наборов
+ ///
+ public List Keys => _tankStorages.Keys.ToList();
+ ///
+ /// Ширина окна отрисовки
+ ///
+ private readonly int _pictureWidth;
+ ///
+ /// Высота окна отрисовки
+ ///
+ private readonly int _pictureHeight;
+ ///
+ /// Конструктор
+ ///
+ ///
+ ///
+ public TanksGenericStorage(int pictureWidth, int pictureHeight)
+ {
+ _tankStorages = new Dictionary>();
+ _pictureWidth = pictureWidth;
+ _pictureHeight = pictureHeight;
+ }
+ ///
+ /// Добавление набора
+ ///
+ /// Название набора
+
+ public void AddSet(string name)
+ {
+ _tankStorages.Add(name, new TanksGenericCollection (_pictureWidth, _pictureHeight));
+ }
+ ///
+ /// Удаление набора
+ ///
+ /// Название набора
+ public void DelSet(string name)
+ {
+ if (!_tankStorages.ContainsKey(name))
+ return;
+ _tankStorages.Remove(name);
+ }
+
+ ///
+ /// Доступ к набору
+ ///
+ ///
+ ///
+ public TanksGenericCollection? this[string ind]
+ {
+ get
+ {
+ if (_tankStorages.ContainsKey(ind))
+ return _tankStorages[ind];
+ return null;
+ }
+ }
+ }
+}
\ No newline at end of file