diff --git a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/FormSPAUCollection.Designer.cs b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/FormSPAUCollection.Designer.cs index 493c480..f2322bf 100644 --- a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/FormSPAUCollection.Designer.cs +++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/FormSPAUCollection.Designer.cs @@ -33,7 +33,15 @@ ButtonAddSPAU = new Button(); ButtonRemoveSPAU = new Button(); ButtonRefreshCollection = new Button(); + panel1 = new Panel(); + panel2 = new Panel(); + listBoxStorages = new ListBox(); + ButtonDelObject = new Button(); + ButtonAddObject = new Button(); + textBoxStorageName = new TextBox(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); + panel1.SuspendLayout(); + panel2.SuspendLayout(); SuspendLayout(); // // pictureBoxCollection @@ -46,14 +54,14 @@ // // maskedTextBoxNumber // - maskedTextBoxNumber.Location = new Point(729, 51); + maskedTextBoxNumber.Location = new Point(18, 71); maskedTextBoxNumber.Name = "maskedTextBoxNumber"; maskedTextBoxNumber.Size = new Size(143, 27); maskedTextBoxNumber.TabIndex = 1; // // ButtonAddSPAU // - ButtonAddSPAU.Location = new Point(748, 102); + ButtonAddSPAU.Location = new Point(44, 19); ButtonAddSPAU.Name = "ButtonAddSPAU"; ButtonAddSPAU.Size = new Size(102, 33); ButtonAddSPAU.TabIndex = 2; @@ -63,7 +71,7 @@ // // ButtonRemoveSPAU // - ButtonRemoveSPAU.Location = new Point(750, 141); + ButtonRemoveSPAU.Location = new Point(44, 118); ButtonRemoveSPAU.Name = "ButtonRemoveSPAU"; ButtonRemoveSPAU.Size = new Size(100, 37); ButtonRemoveSPAU.TabIndex = 3; @@ -73,7 +81,7 @@ // // ButtonRefreshCollection // - ButtonRefreshCollection.Location = new Point(750, 195); + ButtonRefreshCollection.Location = new Point(44, 172); ButtonRefreshCollection.Name = "ButtonRefreshCollection"; ButtonRefreshCollection.Size = new Size(100, 35); ButtonRefreshCollection.TabIndex = 4; @@ -81,21 +89,81 @@ ButtonRefreshCollection.UseVisualStyleBackColor = true; ButtonRefreshCollection.Click += ButtonRefreshCollection_Click; // + // panel1 + // + panel1.Controls.Add(ButtonRefreshCollection); + panel1.Controls.Add(ButtonRemoveSPAU); + panel1.Controls.Add(ButtonAddSPAU); + panel1.Controls.Add(maskedTextBoxNumber); + panel1.Location = new Point(711, 217); + panel1.Name = "panel1"; + panel1.Size = new Size(176, 227); + panel1.TabIndex = 5; + // + // panel2 + // + panel2.Controls.Add(listBoxStorages); + panel2.Controls.Add(ButtonDelObject); + panel2.Controls.Add(ButtonAddObject); + panel2.Controls.Add(textBoxStorageName); + panel2.Location = new Point(711, 12); + panel2.Name = "panel2"; + panel2.Size = new Size(174, 198); + panel2.TabIndex = 6; + // + // listBoxStorages + // + listBoxStorages.FormattingEnabled = true; + listBoxStorages.ItemHeight = 20; + listBoxStorages.Location = new Point(18, 87); + listBoxStorages.Name = "listBoxStorages"; + listBoxStorages.Size = new Size(143, 64); + listBoxStorages.TabIndex = 7; + listBoxStorages.SelectedIndexChanged += listBoxStorages_SelectedIndexChanged; + // + // ButtonDelObject + // + ButtonDelObject.Location = new Point(18, 168); + ButtonDelObject.Name = "ButtonDelObject"; + ButtonDelObject.Size = new Size(143, 28); + ButtonDelObject.TabIndex = 3; + ButtonDelObject.Text = "Удалить набор"; + ButtonDelObject.UseVisualStyleBackColor = true; + ButtonDelObject.Click += ButtonDelObject_Click; + // + // ButtonAddObject + // + ButtonAddObject.Location = new Point(18, 53); + ButtonAddObject.Name = "ButtonAddObject"; + ButtonAddObject.Size = new Size(143, 28); + ButtonAddObject.TabIndex = 1; + ButtonAddObject.Text = "Добавить набор"; + ButtonAddObject.UseVisualStyleBackColor = true; + ButtonAddObject.Click += ButtonAddObject_Click; + // + // textBoxStorageName + // + textBoxStorageName.Location = new Point(18, 20); + textBoxStorageName.Name = "textBoxStorageName"; + textBoxStorageName.Size = new Size(143, 27); + textBoxStorageName.TabIndex = 0; + // // FormSPAUCollection // AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(899, 456); - Controls.Add(ButtonRefreshCollection); - Controls.Add(ButtonRemoveSPAU); - Controls.Add(ButtonAddSPAU); - Controls.Add(maskedTextBoxNumber); + Controls.Add(panel2); + Controls.Add(panel1); Controls.Add(pictureBoxCollection); Name = "FormSPAUCollection"; Text = "FormSPAUCollection"; ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit(); + panel1.ResumeLayout(false); + panel1.PerformLayout(); + panel2.ResumeLayout(false); + panel2.PerformLayout(); ResumeLayout(false); - PerformLayout(); } #endregion @@ -105,5 +173,11 @@ private Button ButtonAddSPAU; private Button ButtonRemoveSPAU; private Button ButtonRefreshCollection; + private Panel panel1; + private Panel panel2; + private Button ButtonAddObject; + private TextBox textBoxStorageName; + private Button ButtonDelObject; + private ListBox listBoxStorages; } } \ No newline at end of file diff --git a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/FormSPAUCollection.cs b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/FormSPAUCollection.cs index fe93be1..beac829 100644 --- a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/FormSPAUCollection.cs +++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/FormSPAUCollection.cs @@ -23,14 +23,81 @@ namespace SelfPropelledArtilleryUnit /// /// Набор объектов /// - private readonly SPAUGenericCollection _SPAUs; + //private readonly SPAUGenericCollection _SPAUs; + private readonly SPAUGenericStorage _storage; /// /// Конструктор /// public FormSPAUCollection() { InitializeComponent(); - _SPAUs = new SPAUGenericCollection(pictureBoxCollection.Width, pictureBoxCollection.Height); + _storage = new SPAUGenericStorage(pictureBoxCollection.Width, pictureBoxCollection.Height); + } + /// + /// Заполнение listBoxObjects + /// + 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) + { + if (string.IsNullOrEmpty(textBoxStorageName.Text)) + { + MessageBox.Show("Не все данные заполнены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _storage.AddSet(textBoxStorageName.Text); + ReloadObjects(); + + } + /// + /// Выбор набора + /// + /// + /// + + private void listBoxStorages_SelectedIndexChanged(object sender, EventArgs e) + { + pictureBoxCollection.Image = _storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowSPAUs(); + } + /// + /// Удаление набора + /// + /// + /// + + 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(); + } } /// /// Добавление объекта в набор @@ -39,15 +106,24 @@ namespace SelfPropelledArtilleryUnit /// private void ButtonAddSPAU_Click(object sender, EventArgs e) { + if (listBoxStorages.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty]; + if (obj == null) + { + return; + } FormSPAU form = new(); if (form.ShowDialog() == DialogResult.OK) { - int addedIndex = _SPAUs + form.SelectedSPAU; + int addedIndex = obj + form.SelectedSPAU; if (addedIndex != -1 && addedIndex <= countPlaces) - + { MessageBox.Show("Объект добавлен"); - pictureBoxCollection.Image = _SPAUs.ShowSPAUs(); + pictureBoxCollection.Image = obj.ShowSPAUs(); } else { @@ -55,6 +131,8 @@ namespace SelfPropelledArtilleryUnit } } } + + /// /// Удаление объекта из набора /// @@ -62,24 +140,35 @@ namespace SelfPropelledArtilleryUnit /// private void ButtonRemoveSPAU_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) { return; } int pos; - try { - - pos = Convert.ToInt32(maskedTextBoxNumber.Text); + try + { + + pos = Convert.ToInt32(maskedTextBoxNumber.Text); } - catch { + catch + { MessageBox.Show("Не удалось удалить объект"); return; } - if (_SPAUs - pos) + if (obj - pos) { MessageBox.Show("Объект удален"); - pictureBoxCollection.Image = _SPAUs.ShowSPAUs(); + pictureBoxCollection.Image = obj.ShowSPAUs(); } else { @@ -93,7 +182,19 @@ namespace SelfPropelledArtilleryUnit /// private void ButtonRefreshCollection_Click(object sender, EventArgs e) { - pictureBoxCollection.Image = _SPAUs.ShowSPAUs(); + if (listBoxStorages.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? + string.Empty]; + if (obj == null) + { + return; + } + pictureBoxCollection.Image = obj.ShowSPAUs(); } + + } } diff --git a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/SPAUGenericCollection.cs b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/SPAUGenericCollection.cs index 11a18a7..0a4a07f 100644 --- a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/SPAUGenericCollection.cs +++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/SPAUGenericCollection.cs @@ -72,7 +72,7 @@ namespace SelfPropelledArtilleryUnit.Generics /// public static bool operator -(SPAUGenericCollection collect, int pos) { - T? obj = collect._collection.Get(pos); + T? obj = collect._collection[pos]; if (obj != null) { collect._collection.Remove(pos); @@ -90,7 +90,7 @@ namespace SelfPropelledArtilleryUnit.Generics /// public U? GetU(int pos) { - return (U?)_collection.Get(pos)?.GetMoveableObject; + return (U?)_collection[pos]?.GetMoveableObject; } /// /// Вывод всего набора объектов @@ -135,8 +135,8 @@ namespace SelfPropelledArtilleryUnit.Generics DrawningSPAU current; for (int i = 0; i < _collection.Count; i++) { - current = _collection.Get(i); - current?.SetPosition(stringCount * 200, 280 - j * 100); + current = _collection[i]; + current?.SetPosition(stringCount * _placeSizeWidth, 280 - j * _placeSizeHeight); stringCount++; if (stringCount >= 3) { diff --git a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/SPAUGenericStorage.cs b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/SPAUGenericStorage.cs new file mode 100644 index 0000000..fb2db48 --- /dev/null +++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/SPAUGenericStorage.cs @@ -0,0 +1,84 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using SelfPropelledArtilleryUnit.DrawningObjects; +using SelfPropelledArtilleryUnit.MovementStrategy; + +namespace SelfPropelledArtilleryUnit.Generics +{ + /// + /// Класс для хранения коллекции + /// + internal class SPAUGenericStorage + { + /// + /// Словарь (хранилище) + /// + readonly Dictionary> _SPAUStorages; + /// + /// Возвращение списка названий наборов + /// + public List Keys => _SPAUStorages.Keys.ToList(); + /// + /// Ширина окна отрисовки + /// + private readonly int _pictureWidth; + /// + /// Высота окна отрисовки + /// + private readonly int _pictureHeight; + /// + /// Конструктор + /// + /// + /// + public SPAUGenericStorage(int pictureWidth, int pictureHeight) + { + _SPAUStorages = new Dictionary>(); + _pictureWidth = pictureWidth; + _pictureHeight = pictureHeight; + } + /// + /// Добавление набора + /// + /// Название набора + public void AddSet(string name) + { + try + { + _SPAUStorages.Add(name, new SPAUGenericCollection(_pictureWidth, _pictureHeight)); + } + catch (Exception) { return; } + } + /// + /// Удаление набора + /// + /// Название набора + public void DelSet(string name) + { + try { _SPAUStorages.Remove(name); } catch (Exception) { return; } + } + /// + /// Доступ к набору + /// + /// + /// + public SPAUGenericCollection? this[string ind] + { + get + { + if (_SPAUStorages.ContainsKey(ind)) + { + return _SPAUStorages[ind]; + } + else + { + return null; + } + } + } + } + +} diff --git a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/SetGeneric.cs b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/SetGeneric.cs index b8ecae8..f07b832 100644 --- a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/SetGeneric.cs +++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/SetGeneric.cs @@ -1,4 +1,6 @@ -using System; +using SelfPropelledArtilleryUnit.DrawningObjects; +using SelfPropelledArtilleryUnit.MovementStrategy; +using System; using System.Collections.Generic; using System.Configuration; using System.Linq; @@ -17,18 +19,23 @@ namespace SelfPropelledArtilleryUnit.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); } /// /// Добавление объекта в набор @@ -47,35 +54,25 @@ namespace SelfPropelledArtilleryUnit.Generics /// public int Insert(T spau, int position) { - if (position < 0 || spau == null) - { + if (Count == _maxCount) return -1; - } - if (position >= Count) - { - return -1; - } - // Ищем первую пустую позицию начиная с указанной позиции - int positionNull = Array.FindIndex(_places, position, x => x == null); - if (positionNull == -1 && _places[Count - 1] != null) - { - // Если пустых позиций нет и последняя позиция в массиве занята + if (position < 0 || spau == null) return -1; - } - else if (positionNull == -1) + + if (position >= _maxCount) + return -1; + + + if (Count == 0) { - // Если позиция для вставки пустая, а пустых позиций больше нет - positionNull = Count - 1; + _places.Add(spau); } - // Сдвигаем элементы вправо, начиная с первой пустой позиции и заканчивая указанной позицией - for (int i = positionNull; i > position; i--) + else { - _places[i] = _places[i - 1]; + _places.Insert(position, spau); } - // Вставка по позиции - _places[position] = spau; - return position; + return Count; } /// /// Удаление объекта из набора с конкретной позиции @@ -87,7 +84,7 @@ namespace SelfPropelledArtilleryUnit.Generics if (position < 0 || position >= Count) return false; - _places[position] = null; + _places.RemoveAt(position); return true; } @@ -96,12 +93,37 @@ namespace SelfPropelledArtilleryUnit.Generics /// /// /// - 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 >= _maxCount) + return null; + return _places[position]; + } + set + { + if (position < 0 || position >= _maxCount) + return; + _places[position] = value; + } } + /// + /// Проход по списку + /// + /// + public IEnumerable GetSPAUs(int? maxSPAUs = null) + { + for (int i = 0; i < _places.Count; ++i) + { + yield return _places[i]; + if (maxSPAUs.HasValue && i == maxSPAUs.Value) + { + yield break; + } + } + } + } }