diff --git a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/FormSPAUCollection.Designer.cs b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/FormSPAUCollection.Designer.cs index 493c480..689a1dd 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.Items.AddRange(new object[] { "Ans", "Second", "Dry", "Quadro" }); + listBoxStorages.Location = new Point(18, 87); + listBoxStorages.Name = "listBoxStorages"; + listBoxStorages.Size = new Size(143, 64); + listBoxStorages.TabIndex = 7; + // + // 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 fe671dc..30ed011 100644 --- a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/FormSPAUCollection.cs +++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/FormSPAUCollection.cs @@ -23,14 +23,77 @@ 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(listBoxStorages.Text)) + { + MessageBox.Show("Не все данные заполнены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _storage.AddSet(listBoxStorages.Text); + ReloadObjects(); + + } + /// + /// Выбор набора + /// + /// + /// + private void ListBoxObjects_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,16 +102,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; - //MessageBox.Show(addedIndex.ToString()); + int addedIndex = obj + form.SelectedSPAU; if (addedIndex != -1 && addedIndex <= countPlaces) - + { MessageBox.Show("Объект добавлен"); - pictureBoxCollection.Image = _SPAUs.ShowSPAUs(); + pictureBoxCollection.Image = obj.ShowSPAUs(); } else { @@ -56,6 +127,8 @@ namespace SelfPropelledArtilleryUnit } } } + + /// /// Удаление объекта из набора /// @@ -63,24 +136,36 @@ 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 { @@ -94,7 +179,17 @@ 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/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; + } + } + } + } + +}