diff --git a/AntiAircraftGun/AntiAircraftGun/FormAircraftGunCollection.Designer.cs b/AntiAircraftGun/AntiAircraftGun/FormAircraftGunCollection.Designer.cs index f1933d9..2db2e84 100644 --- a/AntiAircraftGun/AntiAircraftGun/FormAircraftGunCollection.Designer.cs +++ b/AntiAircraftGun/AntiAircraftGun/FormAircraftGunCollection.Designer.cs @@ -28,96 +28,161 @@ /// private void InitializeComponent() { - this.panel1 = new System.Windows.Forms.Panel(); - this.maskedTextBoxNumber = new System.Windows.Forms.TextBox(); - this.ButtonRefreshCollection = new System.Windows.Forms.Button(); + this.pictureBoxCollection = new System.Windows.Forms.PictureBox(); + this.groupBox1 = new System.Windows.Forms.GroupBox(); + this.maskedTextBoxNumber = new System.Windows.Forms.MaskedTextBox(); + this.listBoxStorages = new System.Windows.Forms.ListBox(); + this.textBoxStorageName = new System.Windows.Forms.TextBox(); + this.ButtonAddObject = new System.Windows.Forms.Button(); + this.ButtonDelObject = new System.Windows.Forms.Button(); this.ButtonAddAircraftGun = new System.Windows.Forms.Button(); this.ButtonRemoveAircraftGun = new System.Windows.Forms.Button(); - this.pictureBoxCollection = new System.Windows.Forms.PictureBox(); - this.panel1.SuspendLayout(); + this.ButtonRefreshCollection = new System.Windows.Forms.Button(); + this.groupBox2 = new System.Windows.Forms.GroupBox(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).BeginInit(); + this.groupBox1.SuspendLayout(); + this.groupBox2.SuspendLayout(); this.SuspendLayout(); // - // panel1 + // pictureBoxCollection // - this.panel1.Controls.Add(this.maskedTextBoxNumber); - this.panel1.Controls.Add(this.ButtonRefreshCollection); - this.panel1.Controls.Add(this.ButtonAddAircraftGun); - this.panel1.Controls.Add(this.ButtonRemoveAircraftGun); - this.panel1.Location = new System.Drawing.Point(667, 12); - this.panel1.Name = "panel1"; - this.panel1.Size = new System.Drawing.Size(180, 436); - this.panel1.TabIndex = 0; + this.pictureBoxCollection.Location = new System.Drawing.Point(7, 6); + this.pictureBoxCollection.Name = "pictureBoxCollection"; + this.pictureBoxCollection.Size = new System.Drawing.Size(634, 439); + this.pictureBoxCollection.TabIndex = 0; + this.pictureBoxCollection.TabStop = false; + // + // groupBox1 + // + this.groupBox1.Controls.Add(this.groupBox2); + this.groupBox1.Controls.Add(this.maskedTextBoxNumber); + this.groupBox1.Controls.Add(this.ButtonAddAircraftGun); + this.groupBox1.Controls.Add(this.ButtonRemoveAircraftGun); + this.groupBox1.Controls.Add(this.ButtonRefreshCollection); + this.groupBox1.Location = new System.Drawing.Point(658, 6); + this.groupBox1.Name = "groupBox1"; + this.groupBox1.Size = new System.Drawing.Size(236, 437); + this.groupBox1.TabIndex = 1; + this.groupBox1.TabStop = false; + this.groupBox1.Text = "Инструменты"; // // maskedTextBoxNumber // - this.maskedTextBoxNumber.Location = new System.Drawing.Point(24, 80); + this.maskedTextBoxNumber.Location = new System.Drawing.Point(49, 318); this.maskedTextBoxNumber.Name = "maskedTextBoxNumber"; - this.maskedTextBoxNumber.Size = new System.Drawing.Size(134, 23); - this.maskedTextBoxNumber.TabIndex = 3; + this.maskedTextBoxNumber.Size = new System.Drawing.Size(156, 23); + this.maskedTextBoxNumber.TabIndex = 5; // - // ButtonRefreshCollection + // listBoxStorages // - this.ButtonRefreshCollection.Location = new System.Drawing.Point(3, 210); - this.ButtonRefreshCollection.Name = "ButtonRefreshCollection"; - this.ButtonRefreshCollection.Size = new System.Drawing.Size(174, 44); - this.ButtonRefreshCollection.TabIndex = 2; - this.ButtonRefreshCollection.Text = "Обновить коллекцию"; - this.ButtonRefreshCollection.UseVisualStyleBackColor = true; - this.ButtonRefreshCollection.Click += new System.EventHandler(this.ButtonRefreshCollection_Click); + this.listBoxStorages.FormattingEnabled = true; + this.listBoxStorages.ItemHeight = 15; + this.listBoxStorages.Location = new System.Drawing.Point(30, 86); + this.listBoxStorages.Name = "listBoxStorages"; + this.listBoxStorages.Size = new System.Drawing.Size(156, 79); + this.listBoxStorages.TabIndex = 4; + this.listBoxStorages.SelectedIndexChanged += new System.EventHandler(this.listBoxStorages_SelectedIndexChanged); + // + // textBoxStorageName + // + this.textBoxStorageName.Location = new System.Drawing.Point(30, 26); + this.textBoxStorageName.Name = "textBoxStorageName"; + this.textBoxStorageName.Size = new System.Drawing.Size(156, 23); + this.textBoxStorageName.TabIndex = 3; + // + // ButtonAddObject + // + this.ButtonAddObject.Location = new System.Drawing.Point(30, 55); + this.ButtonAddObject.Name = "ButtonAddObject"; + this.ButtonAddObject.Size = new System.Drawing.Size(156, 25); + this.ButtonAddObject.TabIndex = 2; + this.ButtonAddObject.Text = "Добавить набор"; + this.ButtonAddObject.UseVisualStyleBackColor = true; + this.ButtonAddObject.Click += new System.EventHandler(this.ButtonAddObject_Click); + // + // ButtonDelObject + // + this.ButtonDelObject.Location = new System.Drawing.Point(30, 171); + this.ButtonDelObject.Name = "ButtonDelObject"; + this.ButtonDelObject.Size = new System.Drawing.Size(156, 25); + this.ButtonDelObject.TabIndex = 2; + this.ButtonDelObject.Text = "Удалить набор"; + this.ButtonDelObject.UseVisualStyleBackColor = true; + this.ButtonDelObject.Click += new System.EventHandler(this.ButtonDelObject_Click); // // ButtonAddAircraftGun // - this.ButtonAddAircraftGun.Location = new System.Drawing.Point(3, 12); + this.ButtonAddAircraftGun.Location = new System.Drawing.Point(49, 285); this.ButtonAddAircraftGun.Name = "ButtonAddAircraftGun"; - this.ButtonAddAircraftGun.Size = new System.Drawing.Size(174, 44); - this.ButtonAddAircraftGun.TabIndex = 0; + this.ButtonAddAircraftGun.Size = new System.Drawing.Size(156, 27); + this.ButtonAddAircraftGun.TabIndex = 2; this.ButtonAddAircraftGun.Text = "Добавить зенитку"; this.ButtonAddAircraftGun.UseVisualStyleBackColor = true; this.ButtonAddAircraftGun.Click += new System.EventHandler(this.ButtonAddAircraftGun_Click); // // ButtonRemoveAircraftGun // - this.ButtonRemoveAircraftGun.Location = new System.Drawing.Point(3, 135); + this.ButtonRemoveAircraftGun.Location = new System.Drawing.Point(49, 347); this.ButtonRemoveAircraftGun.Name = "ButtonRemoveAircraftGun"; - this.ButtonRemoveAircraftGun.Size = new System.Drawing.Size(174, 44); - this.ButtonRemoveAircraftGun.TabIndex = 1; + this.ButtonRemoveAircraftGun.Size = new System.Drawing.Size(156, 26); + this.ButtonRemoveAircraftGun.TabIndex = 2; this.ButtonRemoveAircraftGun.Text = "Удалить зенитку"; this.ButtonRemoveAircraftGun.UseVisualStyleBackColor = true; this.ButtonRemoveAircraftGun.Click += new System.EventHandler(this.ButtonRemoveAircraftGun_Click); // - // pictureBoxCollection + // ButtonRefreshCollection // - this.pictureBoxCollection.Location = new System.Drawing.Point(2, 2); - this.pictureBoxCollection.Name = "pictureBoxCollection"; - this.pictureBoxCollection.Size = new System.Drawing.Size(659, 490); - this.pictureBoxCollection.TabIndex = 1; - this.pictureBoxCollection.TabStop = false; + this.ButtonRefreshCollection.Location = new System.Drawing.Point(49, 379); + this.ButtonRefreshCollection.Name = "ButtonRefreshCollection"; + this.ButtonRefreshCollection.Size = new System.Drawing.Size(156, 27); + this.ButtonRefreshCollection.TabIndex = 2; + this.ButtonRefreshCollection.Text = "Обновить коллекцию"; + this.ButtonRefreshCollection.UseVisualStyleBackColor = true; + this.ButtonRefreshCollection.Click += new System.EventHandler(this.ButtonRefreshCollection_Click); + // + // groupBox2 + // + this.groupBox2.Controls.Add(this.listBoxStorages); + this.groupBox2.Controls.Add(this.textBoxStorageName); + this.groupBox2.Controls.Add(this.ButtonAddObject); + this.groupBox2.Controls.Add(this.ButtonDelObject); + this.groupBox2.Location = new System.Drawing.Point(19, 28); + this.groupBox2.Name = "groupBox2"; + this.groupBox2.Size = new System.Drawing.Size(209, 222); + this.groupBox2.TabIndex = 6; + this.groupBox2.TabStop = false; + this.groupBox2.Text = "Наборы"; // // FormAircraftGunCollection // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(850, 504); + this.ClientSize = new System.Drawing.Size(906, 450); + this.Controls.Add(this.groupBox1); this.Controls.Add(this.pictureBoxCollection); - this.Controls.Add(this.panel1); this.Name = "FormAircraftGunCollection"; this.Text = "Набор зениток"; - this.Load += new System.EventHandler(this.FormAircraftGunCollection_Load); - this.panel1.ResumeLayout(false); - this.panel1.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).EndInit(); + this.groupBox1.ResumeLayout(false); + this.groupBox1.PerformLayout(); + this.groupBox2.ResumeLayout(false); + this.groupBox2.PerformLayout(); this.ResumeLayout(false); } #endregion - private Panel panel1; - private Button ButtonRefreshCollection; - private Button ButtonAddAircraftGun; - private Button ButtonRemoveAircraftGun; private PictureBox pictureBoxCollection; - private TextBox maskedTextBoxNumber; + private GroupBox groupBox1; + private Button ButtonRefreshCollection; + private Button ButtonRemoveAircraftGun; + private Button ButtonAddAircraftGun; + private Button ButtonDelObject; + private Button ButtonAddObject; + private TextBox textBoxStorageName; + private ListBox listBoxStorages; + private MaskedTextBox maskedTextBoxNumber; + private GroupBox groupBox2; } } \ No newline at end of file diff --git a/AntiAircraftGun/AntiAircraftGun/FormAircraftGunCollection.cs b/AntiAircraftGun/AntiAircraftGun/FormAircraftGunCollection.cs index 0c0e569..66a8bb9 100644 --- a/AntiAircraftGun/AntiAircraftGun/FormAircraftGunCollection.cs +++ b/AntiAircraftGun/AntiAircraftGun/FormAircraftGunCollection.cs @@ -1,4 +1,7 @@ -using System; +using AntiAircraftGun.DrawningObjects; +using AntiAircraftGun.Generics; +using AntiAircraftGun.MovementStrategy; +using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; @@ -7,37 +10,100 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; -using AntiAircraftGun.DrawningObjects; -using AntiAircraftGun.MovementStrategy; -using AntiAircraftGun.Generics; namespace AntiAircraftGun { public partial class FormAircraftGunCollection : Form { - - private readonly AntiAircraftGunsGenericCollection _aircraftguns; + private readonly AntiAircraftGunsGenericStorage _storage; + //private readonly AntiAircraftGunsGenericCollection _aircraftguns; public FormAircraftGunCollection() { InitializeComponent(); - _aircraftguns = new AntiAircraftGunsGenericCollection(pictureBoxCollection.Width, pictureBoxCollection.Height); + _storage = new AntiAircraftGunsGenericStorage(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 FormAircraftGunCollection_Load(object sender, EventArgs e) + // Обновление рисунка по набору + private void ButtonRefreshCollection_Click(object sender, EventArgs e) { + if (listBoxStorages.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty]; + if (obj == null) + { + return; + } + pictureBoxCollection.Image = obj.ShowAntiAircraftGuns(); + } + // Удаление объекта из набора + private void ButtonRemoveAircraftGun_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 = Convert.ToInt32(maskedTextBoxNumber.Text); + if (obj - pos != null) + { + MessageBox.Show("Объект удалён"); + pictureBoxCollection.Image = obj.ShowAntiAircraftGuns(); + } + else + { + MessageBox.Show("Не удалось удалить объект"); + } } // Добавление объекта в набор private void ButtonAddAircraftGun_Click(object sender, EventArgs e) { + if (listBoxStorages.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty]; + if (obj == null) + { + return; + } FormAntiAircraftGun form = new(); if (form.ShowDialog() == DialogResult.OK) { - if ((_aircraftguns + form.SelectedAircraftGun) == 0) + if (obj + form.SelectedAircraftGun) { MessageBox.Show("Объект добавлен"); - pictureBoxCollection.Image = _aircraftguns.ShowAntiAircraftGuns(); + pictureBoxCollection.Image = obj.ShowAntiAircraftGuns(); } else { @@ -46,29 +112,36 @@ namespace AntiAircraftGun } } - // Удаление объекта из набора - private void ButtonRemoveAircraftGun_Click(object sender, EventArgs e) + // Удаление набора + private void ButtonDelObject_Click(object sender, EventArgs e) { - if (MessageBox.Show("удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) + if (listBoxStorages.SelectedIndex == -1) { return; } - int pos = Convert.ToInt32(maskedTextBoxNumber.Text); - if (_aircraftguns - pos != null) + if (MessageBox.Show($"удалить объект {listBoxStorages.SelectedItem}?", "удаление", MessageBoxButtons.YesNo, + MessageBoxIcon.Question) == DialogResult.Yes) { - MessageBox.Show("Объект удалён"); - pictureBoxCollection.Image = _aircraftguns.ShowAntiAircraftGuns(); - } - else - { - MessageBox.Show("Не удалось удалить объект"); + _storage.DelSet(listBoxStorages.SelectedItem.ToString() ?? string.Empty); + ReloadObjects(); } } - // Обновление рисунка по набору - private void ButtonRefreshCollection_Click(object sender, EventArgs e) + // Добавление набора в коллекцию + private void ButtonAddObject_Click(object sender, EventArgs e) { - pictureBoxCollection.Image = _aircraftguns.ShowAntiAircraftGuns(); + 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]?.ShowAntiAircraftGuns(); } } } diff --git a/AntiAircraftGun/AntiAircraftGun/Generics/AntiAircraftGunsGenericCollection.cs b/AntiAircraftGun/AntiAircraftGun/Generics/AntiAircraftGunsGenericCollection.cs index ff91aff..e09ccae 100644 --- a/AntiAircraftGun/AntiAircraftGun/Generics/AntiAircraftGunsGenericCollection.cs +++ b/AntiAircraftGun/AntiAircraftGun/Generics/AntiAircraftGunsGenericCollection.cs @@ -34,19 +34,19 @@ namespace AntiAircraftGun.Generics } // Перегрузка оператора сложения - public static int? operator +(AntiAircraftGunsGenericCollection collect, T? obj) + public static bool operator +(AntiAircraftGunsGenericCollection collect, T? obj) { if (obj == null) { - return -1; + return false; } - return collect?._collection.Insert(obj); + return collect._collection.Insert(obj); } // Перегрузка оператора вычитания public static bool operator -(AntiAircraftGunsGenericCollection collect, int pos) { - T? obj = collect._collection.Get(pos); + T? obj = collect._collection[pos]; if (obj == null) { return false; @@ -58,7 +58,7 @@ namespace AntiAircraftGun.Generics // Получение объекта IMoveableObject public U? GetU(int pos) { - return (U?)_collection.Get(pos)?.GetMoveableObject; + return (U?)_collection[pos]?.GetMoveableObject; } // Вывод всего набора объектов @@ -90,15 +90,15 @@ namespace AntiAircraftGun.Generics { int height = _pictureHeight / _placeSizeHeight; int width = _pictureWidth / _placeSizeWidth; - for (int i = 0; i < _collection.Count; i++) + int i = 0; + foreach (var antiaircraftgun in _collection.GetAntiAircraftGuns()) { - DrawningAircraftGun aircraftGun = _collection.Get(i); - if(aircraftGun == null) + if (antiaircraftgun != null) { - continue; + antiaircraftgun.SetPosition((i % width) * _placeSizeWidth, (i / height) * _placeSizeHeight); + antiaircraftgun.DrawZenitka(g); } - aircraftGun.SetPosition((i % width) * _placeSizeWidth, (i / height) * _placeSizeHeight); - aircraftGun.DrawZenitka(g); + i++; } } } diff --git a/AntiAircraftGun/AntiAircraftGun/Generics/AntiAircraftGunsGenericStorage.cs b/AntiAircraftGun/AntiAircraftGun/Generics/AntiAircraftGunsGenericStorage.cs new file mode 100644 index 0000000..074664c --- /dev/null +++ b/AntiAircraftGun/AntiAircraftGun/Generics/AntiAircraftGunsGenericStorage.cs @@ -0,0 +1,64 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using AntiAircraftGun.MovementStrategy; +using AntiAircraftGun.DrawningObjects; + +namespace AntiAircraftGun.Generics +{ + internal class AntiAircraftGunsGenericStorage + { + // Словарь + readonly Dictionary> _aircraftgunStorages; + + // Возвращение списка названий наборов + public List Keys => _aircraftgunStorages.Keys.ToList(); + // Ширина окна отрисовки + private readonly int _pictureWidth; + // Высота окна отрисовки + private readonly int _pictureHeight; + + // Конструктор + public AntiAircraftGunsGenericStorage(int pictureWidth, int pictureHeight) + { + _aircraftgunStorages = new Dictionary>(); + _pictureWidth = pictureWidth; + _pictureHeight = pictureHeight; + } + + // Добавление набора + public void AddSet(string name) + { + if(_aircraftgunStorages.ContainsKey(name)) + { + return; + } + _aircraftgunStorages[name] = new AntiAircraftGunsGenericCollection(_pictureWidth, _pictureHeight); + } + + // Удаление набора + public void DelSet(string name) + { + if (!_aircraftgunStorages.ContainsKey(name)) + { + return; + } + _aircraftgunStorages.Remove(name); + } + + // Доступ к набору + public AntiAircraftGunsGenericCollection?this[string ind] + { + get + { + if(_aircraftgunStorages.ContainsKey(ind)) + { + return _aircraftgunStorages[ind]; + } + return null; + } + } + } +} diff --git a/AntiAircraftGun/AntiAircraftGun/Generics/SetGeneric.cs b/AntiAircraftGun/AntiAircraftGun/Generics/SetGeneric.cs index 50373e9..8d8ba6d 100644 --- a/AntiAircraftGun/AntiAircraftGun/Generics/SetGeneric.cs +++ b/AntiAircraftGun/AntiAircraftGun/Generics/SetGeneric.cs @@ -1,4 +1,7 @@ -using System; +using AntiAircraftGun.DrawningObjects; +using AntiAircraftGun.Generics; +using AntiAircraftGun.MovementStrategy; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -9,87 +12,90 @@ namespace AntiAircraftGun.Generics internal class SetGeneric where T : class { - // Массив - 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 antiaircraftgun) + public bool Insert(T antiaircraftgun) { - int i = 0; - while(_places[i] != null) - { - i++; - if (i == Count) - { - return -1; - } - } - while (i != 0) - { - _places[i] = _places[i - 1]; - i--; - } - _places[0] = antiaircraftgun; - return 0; + return Insert(antiaircraftgun, 0); } // Добавление объекта на конкретную позицию public bool Insert(T antiaircraftgun, int position) { - if (position < 0 || position >= Count) + if (position < 0 || position >= _maxCount) { return false; } - if (_places[position] == null) - { - _places[position] = antiaircraftgun; - return true; - } - int index = position; - while (_places[index] != null) - { - index++; - } - if (index == Count) + if (Count >= _maxCount) { return false; } - for (int i = index; i > position; i--) - { - _places[i] = _places[i - 1]; - } - _places[position] = antiaircraftgun; + _places.Insert(0, antiaircraftgun); return true; } // Удаление объекта из набора с конкретной позиции public bool Remove(int position) { - if (position < 0 || position >= Count) + if (position < 0 || position >= _maxCount) { return false; } - _places[position] = null; + if (position >= Count) + { + return false; + } + _places.RemoveAt(position); return true; } // Получение объекта из набора по позиции - public T? Get(int position) + public T? this[int position] { - if (position < 0 || position >= Count) + 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 GetAntiAircraftGuns(int? maxAntiAircraftGuns = null) + { + for (int i = 0; i < _places.Count; i++) + { + yield return _places[i]; + if (maxAntiAircraftGuns.HasValue && i == maxAntiAircraftGuns.Value) + { + yield break; + } } - return _places[position]; } } }