From e57dd95b3424493674d2f3702549e2f1408eeb65 Mon Sep 17 00:00:00 2001 From: goblinrf Date: Thu, 26 Oct 2023 23:17:04 +0300 Subject: [PATCH] Complete --- AirFighter/AirplaneGenericCollection.cs | 35 +++--- AirFighter/AirplanesGenericStorage.cs | 50 ++++++++ AirFighter/FormAirFighter.Designer.cs | 22 ++-- AirFighter/FormAirplaneCollection.Designer.cs | 98 +++++++++++++--- AirFighter/FormAirplaneCollection.cs | 107 ++++++++++++++++-- AirFighter/SetGeneric.cs | 72 +++++++----- 6 files changed, 298 insertions(+), 86 deletions(-) create mode 100644 AirFighter/AirplanesGenericStorage.cs diff --git a/AirFighter/AirplaneGenericCollection.cs b/AirFighter/AirplaneGenericCollection.cs index 265f7ea..a762584 100644 --- a/AirFighter/AirplaneGenericCollection.cs +++ b/AirFighter/AirplaneGenericCollection.cs @@ -25,27 +25,28 @@ namespace ProjectAirFighter.Generics _pictureWidth = picWidth; _pictureHeight = picHeight; _collection = new SetGeneric(width * height); + } - public static int operator +(AirplaneslGenericCollection collect, T? obj) + public static bool operator +(AirplaneslGenericCollection collect, T? obj) { if (obj == null) - return -1; - return collect?._collection.Insert(obj) ?? -1; + return false; + return collect?._collection.Insert(obj) ?? false; } - public static bool operator -(AirplaneslGenericCollection collect, int + public static T? operator -(AirplaneslGenericCollection collect, int pos) { - T? obj = collect._collection.Get(pos); + T? obj = collect._collection[pos]; if (obj != null) - return collect._collection.Remove(pos); - return false; + collect._collection.Remove(pos); + return obj; } public U? GetU(int pos) { - return (U?)_collection.Get(pos)?.GetMoveableObject; + return (U?)_collection[pos]?.GetMoveableObject; } public Bitmap ShowAirplanes() @@ -76,25 +77,19 @@ pos) private void DrawObjects(Graphics g) { - int c = 0,r = _pictureHeight/_placeSizeHeight; - - for (int i = 0; i < _collection.Count; i++) + int i = 0; + foreach (var airplane in _collection.GetAirplanes()) { - DrawningAirplane airplane = _collection.Get(i); + if (airplane != null) { int inRow = _pictureWidth / _placeSizeWidth; - airplane.SetPosition(_pictureWidth - _placeSizeWidth - (i % inRow * _placeSizeWidth) - _placeSizeWidth /10 * 2 , (_collection.Count / inRow - 1 - i / inRow) * _placeSizeHeight + (_placeSizeHeight - _placeSizeHeight * 170 / 1000) / 2); + airplane.SetPosition(_pictureWidth - _placeSizeWidth - (i % inRow * _placeSizeWidth) - _placeSizeWidth / 8 * 3, _pictureHeight - _pictureHeight % _placeSizeHeight - (i / inRow + 1) * _placeSizeHeight + (_placeSizeHeight - _placeSizeHeight * 170 / 1000) / 2); airplane.DrawTransport(g); - c++; - if (c - 1 == inRow) - { - c = 0; - r--; - } + } - + i++; } } diff --git a/AirFighter/AirplanesGenericStorage.cs b/AirFighter/AirplanesGenericStorage.cs new file mode 100644 index 0000000..c723fbf --- /dev/null +++ b/AirFighter/AirplanesGenericStorage.cs @@ -0,0 +1,50 @@ +using ProjectAirFighter.DrawningObjects; +using ProjectAirFighter.Generics; +using ProjectAirFighter.MovementStrategy; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectAirFighter.Generics +{ + internal class AirplanesGenericStorage + { + readonly Dictionary> _airplaneStorages; + public List Keys => _airplaneStorages.Keys.ToList(); + + private readonly int _pictureWidth; + + private readonly int _pictureHeight; + + public AirplanesGenericStorage(int pictureWidth, int pictureHeight) + { + _airplaneStorages = new Dictionary>(); + _pictureWidth = pictureWidth; + _pictureHeight = pictureHeight; + } + + public void AddSet(string name) + { + _airplaneStorages.Add(name, new AirplaneslGenericCollection(_pictureWidth, _pictureHeight)); + } + + public void DelSet(string name) + { + if (!_airplaneStorages.ContainsKey(name)) + return; + _airplaneStorages.Remove(name); + } + + public AirplaneslGenericCollection? this[string ind] + { + get + { + if (_airplaneStorages.ContainsKey(ind)) + return _airplaneStorages[ind]; + return null; + } + } + } +} diff --git a/AirFighter/FormAirFighter.Designer.cs b/AirFighter/FormAirFighter.Designer.cs index 24df854..32f8502 100644 --- a/AirFighter/FormAirFighter.Designer.cs +++ b/AirFighter/FormAirFighter.Designer.cs @@ -47,7 +47,7 @@ namespace ProjectAirFighter this.pictureBoxAirFighter.Dock = System.Windows.Forms.DockStyle.Fill; this.pictureBoxAirFighter.Location = new System.Drawing.Point(0, 0); this.pictureBoxAirFighter.Name = "pictureBoxAirFighter"; - this.pictureBoxAirFighter.Size = new System.Drawing.Size(882, 453); + this.pictureBoxAirFighter.Size = new System.Drawing.Size(1015, 736); this.pictureBoxAirFighter.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize; this.pictureBoxAirFighter.TabIndex = 0; this.pictureBoxAirFighter.TabStop = false; @@ -55,7 +55,7 @@ namespace ProjectAirFighter // buttonAirplane // this.buttonAirplane.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); - this.buttonAirplane.Location = new System.Drawing.Point(186, 411); + this.buttonAirplane.Location = new System.Drawing.Point(186, 694); this.buttonAirplane.Name = "buttonAirplane"; this.buttonAirplane.Size = new System.Drawing.Size(180, 40); this.buttonAirplane.TabIndex = 1; @@ -68,7 +68,7 @@ namespace ProjectAirFighter this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonLeft.BackgroundImage = global::AirFighter.Properties.Resources.png_clipart_computer_icons_graphics_arrow_symbol_arrow_angle_desktop_wallpaper; this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; - this.buttonLeft.Location = new System.Drawing.Point(768, 411); + this.buttonLeft.Location = new System.Drawing.Point(901, 694); this.buttonLeft.Name = "buttonLeft"; this.buttonLeft.Size = new System.Drawing.Size(30, 30); this.buttonLeft.TabIndex = 2; @@ -80,7 +80,7 @@ namespace ProjectAirFighter this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonRight.BackgroundImage = global::AirFighter.Properties.Resources.png_transparent_grammatical_person_paper_narration_direzione_didattica_statale_gestione_scuola_elementare_copy_print_right_arrow_miscellaneous_game_angle; this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; - this.buttonRight.Location = new System.Drawing.Point(840, 411); + this.buttonRight.Location = new System.Drawing.Point(973, 694); this.buttonRight.Name = "buttonRight"; this.buttonRight.Size = new System.Drawing.Size(30, 30); this.buttonRight.TabIndex = 3; @@ -92,7 +92,7 @@ namespace ProjectAirFighter this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonUp.BackgroundImage = global::AirFighter.Properties.Resources.kisspng_up_arrow_computer_icons_arrow_down_clip_art_5af6157c473cb4_0747815015260767962918; this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; - this.buttonUp.Location = new System.Drawing.Point(804, 375); + this.buttonUp.Location = new System.Drawing.Point(937, 658); this.buttonUp.Name = "buttonUp"; this.buttonUp.Size = new System.Drawing.Size(30, 30); this.buttonUp.TabIndex = 4; @@ -104,7 +104,7 @@ namespace ProjectAirFighter this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonDown.BackgroundImage = global::AirFighter.Properties.Resources.png_clipart_computer_icons_uma_musume_pretty_derby_fate_grand_order_saber_kemono_friends_three_arrow_game_angle; this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; - this.buttonDown.Location = new System.Drawing.Point(804, 411); + this.buttonDown.Location = new System.Drawing.Point(937, 694); this.buttonDown.Name = "buttonDown"; this.buttonDown.Size = new System.Drawing.Size(30, 30); this.buttonDown.TabIndex = 5; @@ -118,14 +118,14 @@ namespace ProjectAirFighter this.comboBoxStrategy.Items.AddRange(new object[] { "Довести до центра", "Довести до края"}); - this.comboBoxStrategy.Location = new System.Drawing.Point(719, 12); + this.comboBoxStrategy.Location = new System.Drawing.Point(864, 0); this.comboBoxStrategy.Name = "comboBoxStrategy"; this.comboBoxStrategy.Size = new System.Drawing.Size(151, 28); this.comboBoxStrategy.TabIndex = 6; // // buttonStep // - this.buttonStep.Location = new System.Drawing.Point(768, 46); + this.buttonStep.Location = new System.Drawing.Point(921, 43); this.buttonStep.Name = "buttonStep"; this.buttonStep.Size = new System.Drawing.Size(94, 29); this.buttonStep.TabIndex = 7; @@ -135,7 +135,7 @@ namespace ProjectAirFighter // // buttonCreateAirFighter // - this.buttonCreateAirFighter.Location = new System.Drawing.Point(0, 411); + this.buttonCreateAirFighter.Location = new System.Drawing.Point(0, 696); this.buttonCreateAirFighter.Name = "buttonCreateAirFighter"; this.buttonCreateAirFighter.Size = new System.Drawing.Size(180, 40); this.buttonCreateAirFighter.TabIndex = 8; @@ -145,7 +145,7 @@ namespace ProjectAirFighter // // buttonSelectAirplane // - this.buttonSelectAirplane.Location = new System.Drawing.Point(372, 411); + this.buttonSelectAirplane.Location = new System.Drawing.Point(372, 696); this.buttonSelectAirplane.Name = "buttonSelectAirplane"; this.buttonSelectAirplane.Size = new System.Drawing.Size(180, 40); this.buttonSelectAirplane.TabIndex = 9; @@ -157,7 +157,7 @@ namespace ProjectAirFighter // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(882, 453); + this.ClientSize = new System.Drawing.Size(1015, 736); this.Controls.Add(this.buttonSelectAirplane); this.Controls.Add(this.buttonCreateAirFighter); this.Controls.Add(this.buttonStep); diff --git a/AirFighter/FormAirplaneCollection.Designer.cs b/AirFighter/FormAirplaneCollection.Designer.cs index 2d4653f..879ade1 100644 --- a/AirFighter/FormAirplaneCollection.Designer.cs +++ b/AirFighter/FormAirplaneCollection.Designer.cs @@ -29,38 +29,95 @@ private void InitializeComponent() { this.toolGroupBox = new System.Windows.Forms.GroupBox(); - this.maskedTextBox = new System.Windows.Forms.MaskedTextBox(); + this.maskedTextBoxNumber = new System.Windows.Forms.MaskedTextBox(); + this.kitGroupbox = new System.Windows.Forms.GroupBox(); + this.textBoxStorageName = new System.Windows.Forms.TextBox(); + this.ButtonDelObject = new System.Windows.Forms.Button(); + this.ButtonAddObject = new System.Windows.Forms.Button(); + this.listBoxStorages = new System.Windows.Forms.ListBox(); this.updateCollectionButton = new System.Windows.Forms.Button(); this.deleteAirplaneButton = new System.Windows.Forms.Button(); this.addAirplaneButton = new System.Windows.Forms.Button(); this.pictureBoxCollection = new System.Windows.Forms.PictureBox(); this.toolGroupBox.SuspendLayout(); + this.kitGroupbox.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).BeginInit(); this.SuspendLayout(); // // toolGroupBox // - this.toolGroupBox.Controls.Add(this.maskedTextBox); + this.toolGroupBox.Controls.Add(this.maskedTextBoxNumber); + this.toolGroupBox.Controls.Add(this.kitGroupbox); this.toolGroupBox.Controls.Add(this.updateCollectionButton); this.toolGroupBox.Controls.Add(this.deleteAirplaneButton); this.toolGroupBox.Controls.Add(this.addAirplaneButton); - this.toolGroupBox.Location = new System.Drawing.Point(716, 12); + this.toolGroupBox.Location = new System.Drawing.Point(744, 12); this.toolGroupBox.Name = "toolGroupBox"; - this.toolGroupBox.Size = new System.Drawing.Size(223, 426); + this.toolGroupBox.Size = new System.Drawing.Size(223, 530); this.toolGroupBox.TabIndex = 0; this.toolGroupBox.TabStop = false; this.toolGroupBox.Text = "Инструменты"; // - // maskedTextBox + // maskedTextBoxNumber // - this.maskedTextBox.Location = new System.Drawing.Point(8, 72); - this.maskedTextBox.Name = "maskedTextBox"; - this.maskedTextBox.Size = new System.Drawing.Size(125, 27); - this.maskedTextBox.TabIndex = 4; + this.maskedTextBoxNumber.Location = new System.Drawing.Point(-2, 367); + this.maskedTextBoxNumber.Name = "maskedTextBoxNumber"; + this.maskedTextBoxNumber.Size = new System.Drawing.Size(215, 27); + this.maskedTextBoxNumber.TabIndex = 7; + // + // kitGroupbox + // + this.kitGroupbox.Controls.Add(this.textBoxStorageName); + this.kitGroupbox.Controls.Add(this.ButtonDelObject); + this.kitGroupbox.Controls.Add(this.ButtonAddObject); + this.kitGroupbox.Controls.Add(this.listBoxStorages); + this.kitGroupbox.Location = new System.Drawing.Point(6, 26); + this.kitGroupbox.Name = "kitGroupbox"; + this.kitGroupbox.Size = new System.Drawing.Size(217, 280); + this.kitGroupbox.TabIndex = 5; + this.kitGroupbox.TabStop = false; + this.kitGroupbox.Text = "Наборы"; + // + // textBoxStorageName + // + this.textBoxStorageName.Location = new System.Drawing.Point(23, 26); + this.textBoxStorageName.Name = "textBoxStorageName"; + this.textBoxStorageName.Size = new System.Drawing.Size(184, 27); + this.textBoxStorageName.TabIndex = 9; + // + // ButtonDelObject + // + this.ButtonDelObject.Location = new System.Drawing.Point(23, 233); + this.ButtonDelObject.Name = "ButtonDelObject"; + this.ButtonDelObject.Size = new System.Drawing.Size(184, 29); + this.ButtonDelObject.TabIndex = 8; + this.ButtonDelObject.Text = "Удалить набор"; + this.ButtonDelObject.UseVisualStyleBackColor = true; + this.ButtonDelObject.Click += new System.EventHandler(this.ButtonDelObject_Click); + // + // ButtonAddObject + // + this.ButtonAddObject.Location = new System.Drawing.Point(23, 59); + this.ButtonAddObject.Name = "ButtonAddObject"; + this.ButtonAddObject.Size = new System.Drawing.Size(184, 29); + this.ButtonAddObject.TabIndex = 7; + this.ButtonAddObject.Text = "Добавить набор"; + this.ButtonAddObject.UseVisualStyleBackColor = true; + this.ButtonAddObject.Click += new System.EventHandler(this.ButtonAddObject_Click); + // + // listBoxStorages + // + this.listBoxStorages.FormattingEnabled = true; + this.listBoxStorages.ItemHeight = 20; + this.listBoxStorages.Location = new System.Drawing.Point(23, 106); + this.listBoxStorages.Name = "listBoxStorages"; + this.listBoxStorages.Size = new System.Drawing.Size(184, 104); + this.listBoxStorages.TabIndex = 6; + this.listBoxStorages.Click += new System.EventHandler(this.listBoxStorages_SelectedIndexChanged); // // updateCollectionButton // - this.updateCollectionButton.Location = new System.Drawing.Point(6, 151); + this.updateCollectionButton.Location = new System.Drawing.Point(-2, 471); this.updateCollectionButton.Name = "updateCollectionButton"; this.updateCollectionButton.Size = new System.Drawing.Size(215, 40); this.updateCollectionButton.TabIndex = 3; @@ -70,21 +127,21 @@ // // deleteAirplaneButton // - this.deleteAirplaneButton.Location = new System.Drawing.Point(6, 105); + this.deleteAirplaneButton.Location = new System.Drawing.Point(-2, 409); this.deleteAirplaneButton.Name = "deleteAirplaneButton"; this.deleteAirplaneButton.Size = new System.Drawing.Size(215, 40); this.deleteAirplaneButton.TabIndex = 2; - this.deleteAirplaneButton.Text = "Удалить"; + this.deleteAirplaneButton.Text = "Удалить самолёт"; this.deleteAirplaneButton.UseVisualStyleBackColor = true; this.deleteAirplaneButton.Click += new System.EventHandler(this.deleteAirplaneButton_Click); // // addAirplaneButton // - this.addAirplaneButton.Location = new System.Drawing.Point(8, 26); + this.addAirplaneButton.Location = new System.Drawing.Point(0, 312); this.addAirplaneButton.Name = "addAirplaneButton"; this.addAirplaneButton.Size = new System.Drawing.Size(215, 40); this.addAirplaneButton.TabIndex = 0; - this.addAirplaneButton.Text = "Добавить"; + this.addAirplaneButton.Text = "Добавить самолёт"; this.addAirplaneButton.UseVisualStyleBackColor = true; this.addAirplaneButton.Click += new System.EventHandler(this.addAirplaneButton_Click); // @@ -92,7 +149,7 @@ // this.pictureBoxCollection.Location = new System.Drawing.Point(12, 12); this.pictureBoxCollection.Name = "pictureBoxCollection"; - this.pictureBoxCollection.Size = new System.Drawing.Size(698, 426); + this.pictureBoxCollection.Size = new System.Drawing.Size(726, 524); this.pictureBoxCollection.TabIndex = 1; this.pictureBoxCollection.TabStop = false; // @@ -100,13 +157,15 @@ // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(951, 446); + this.ClientSize = new System.Drawing.Size(980, 548); this.Controls.Add(this.pictureBoxCollection); this.Controls.Add(this.toolGroupBox); this.Name = "FormAirplaneCollection"; this.Text = "FormMonorailCollection"; this.toolGroupBox.ResumeLayout(false); this.toolGroupBox.PerformLayout(); + this.kitGroupbox.ResumeLayout(false); + this.kitGroupbox.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).EndInit(); this.ResumeLayout(false); @@ -119,6 +178,11 @@ private System.Windows.Forms.Button deleteAirplaneButton; private System.Windows.Forms.Button addAirplaneButton; private System.Windows.Forms.PictureBox pictureBoxCollection; - private System.Windows.Forms.MaskedTextBox maskedTextBox; + private GroupBox kitGroupbox; + private Button ButtonAddObject; + private ListBox listBoxStorages; + private Button ButtonDelObject; + private TextBox textBoxStorageName; + private MaskedTextBox maskedTextBoxNumber; } } \ No newline at end of file diff --git a/AirFighter/FormAirplaneCollection.cs b/AirFighter/FormAirplaneCollection.cs index e44e628..a52dfec 100644 --- a/AirFighter/FormAirplaneCollection.cs +++ b/AirFighter/FormAirplaneCollection.cs @@ -15,26 +15,69 @@ namespace ProjectAirFighter { public partial class FormAirplaneCollection : Form { - private readonly AirplaneslGenericCollection _airplanes; + private readonly AirplanesGenericStorage _storage; public FormAirplaneCollection() { InitializeComponent(); - _airplanes = new AirplaneslGenericCollection(pictureBoxCollection.Width, pictureBoxCollection.Height); + _storage = new AirplanesGenericStorage(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) + { + if (string.IsNullOrEmpty(textBoxStorageName.Text)) + { + MessageBox.Show("Не все данные заполнены", "Ошибка", + MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _storage.AddSet(textBoxStorageName.Text); + ReloadObjects(); + } private void deleteAirplaneButton_Click(object sender, EventArgs e) { - if (MessageBox.Show("Удалить объект?", "Удаление", -MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) + if (listBoxStorages.SelectedIndex == -1) { return; } - int pos = Convert.ToInt32(maskedTextBox.Text); - if (_airplanes - pos != null) + 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 = _airplanes.ShowAirplanes(); + pictureBoxCollection.Image = obj.ShowAirplanes(); } else { @@ -44,19 +87,39 @@ MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) private void updateCollectionButton_Click(object sender, EventArgs e) { - pictureBoxCollection.Image = _airplanes.ShowAirplanes(); + if (listBoxStorages.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? + string.Empty]; + if (obj == null) + { + return; + } + pictureBoxCollection.Image = obj.ShowAirplanes(); } private void addAirplaneButton_Click(object sender, EventArgs e) { + if (listBoxStorages.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? + string.Empty]; + if (obj == null) + { + return; + } FormAirFighter form = new(); if (form.ShowDialog() == DialogResult.OK) { - if (_airplanes + form.SelectedAirplane != null) + if (obj + form.SelectedAirplane) { MessageBox.Show("Объект добавлен"); - pictureBoxCollection.Image = _airplanes.ShowAirplanes(); + pictureBoxCollection.Image = obj.ShowAirplanes(); } else { @@ -65,5 +128,27 @@ MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) } } + private void listBoxStorages_SelectedIndexChanged(object sender, EventArgs e) + { + pictureBoxCollection.Image = +_storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowAirplanes(); + } + + 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(); + } + + } + } } diff --git a/AirFighter/SetGeneric.cs b/AirFighter/SetGeneric.cs index b0b4df6..5c2063d 100644 --- a/AirFighter/SetGeneric.cs +++ b/AirFighter/SetGeneric.cs @@ -9,50 +9,68 @@ namespace ProjectAirFighter.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 airplane) + public bool Insert(T airplane) { - return Insert(airplane, 0); + if (_places.Count == _maxCount) + return false; + Insert(airplane, 0); + return true; + } - public int Insert(T airplane, int position) + public bool Insert(T airplane, 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] = airplane; - return position; + if (!(position >= 0 && position <= Count && _places.Count < _maxCount)) + return false; + _places.Insert(position, airplane); + return true; } 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? Get(int position) + public T? this[int position] { - if (!(position >= 0 && position < Count)) - return null; - return _places[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); + return; + } } + + public IEnumerable GetAirplanes(int? maxAirplanes = null) + { + for (int i = 0; i < _places.Count; ++i) + { + yield return _places[i]; + if (maxAirplanes.HasValue && i == maxAirplanes.Value) + { + yield break; + } + } + } + } }