From 4de997dcb5c10b9b71f52f6801788d80c11c9776 Mon Sep 17 00:00:00 2001 From: Whoisthatjulia Date: Wed, 13 Dec 2023 02:57:47 +0400 Subject: [PATCH] lab_4 --- .../AirFighter/AirFighterGenericCollection.cs | 22 +-- .../AirFighter/AirFighterGenericStorage.cs | 55 ++++++ ...s => FormAirFighterCollection.Designer.cs} | 74 ++++++- .../AirFighter/FormAirFighterCollection.cs | 187 ++++++++++++++++++ ...ion.resx => FormAirFighterCollection.resx} | 0 .../AirFighter/FormFighterCollection.cs | 69 ------- AirFighter/AirFighter/Program.cs | 2 +- AirFighter/AirFighter/SetGeneric.cs | 68 ++++--- 8 files changed, 361 insertions(+), 116 deletions(-) create mode 100644 AirFighter/AirFighter/AirFighterGenericStorage.cs rename AirFighter/AirFighter/{FormFighterCollection.Designer.cs => FormAirFighterCollection.Designer.cs} (62%) create mode 100644 AirFighter/AirFighter/FormAirFighterCollection.cs rename AirFighter/AirFighter/{FormFighterCollection.resx => FormAirFighterCollection.resx} (100%) delete mode 100644 AirFighter/AirFighter/FormFighterCollection.cs diff --git a/AirFighter/AirFighter/AirFighterGenericCollection.cs b/AirFighter/AirFighter/AirFighterGenericCollection.cs index 1d7a5bc..11152ff 100644 --- a/AirFighter/AirFighter/AirFighterGenericCollection.cs +++ b/AirFighter/AirFighter/AirFighterGenericCollection.cs @@ -37,7 +37,7 @@ namespace AirFighter.Generics } public static bool operator -(AirFighterGenericCollection collect, int pos) { - T? obj = collect._collection.Get(pos); + T? obj = collect._collection[pos]; if (obj != null) { return collect._collection.Remove(pos); @@ -46,7 +46,7 @@ namespace AirFighter.Generics } public U? GetU(int pos) { - return (U?)_collection.Get(pos)?.GetMoveableObject; + return (U?)_collection[pos]?.GetMoveableObject; } public Bitmap ShowAirFighter() { @@ -77,18 +77,16 @@ namespace AirFighter.Generics int width = _pictureWidth / _placeSizeWidth; int height = _pictureHeight / _placeSizeHeight; - for (int i = 0; i < _collection.Count; i++) + foreach (var fighter in _collection.GetAirFighter()) { - DrawningAirFighter? fighter = _collection.Get(i); - - if (fighter == null) - continue; - - int row = height - 1 - (i / width); - int col = width - 1 - (i % width); - - fighter.SetPosition(col * _placeSizeWidth + 10, row * _placeSizeHeight + 5); + if (fighter != null) + { + int index = _collection.GetAirFighter().ToList().IndexOf(fighter); + int row = height - 1 - (index / width); + int col = width - 1 - (index % width); + fighter.SetPosition(col * _placeSizeWidth + 10, row * _placeSizeHeight + 5); + } fighter.DrawTransport(g); } } diff --git a/AirFighter/AirFighter/AirFighterGenericStorage.cs b/AirFighter/AirFighter/AirFighterGenericStorage.cs new file mode 100644 index 0000000..848bf4b --- /dev/null +++ b/AirFighter/AirFighter/AirFighterGenericStorage.cs @@ -0,0 +1,55 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +using AirFighter.Drawnings; +using AirFighter.MovementStrategy; +using AirFighter.DrawningObjects; + +namespace AirFighter.Generics +{ + internal class AirFighterGenericStorage + { + readonly Dictionary> _AirFighterStorages; + public List Keys => _AirFighterStorages.Keys.ToList(); + + private readonly int _pictureWidth; + + private readonly int _pictureHeight; + public AirFighterGenericStorage(int pictureWidth, int pictureHeight) + { + _AirFighterStorages = new Dictionary>(); + _pictureWidth = pictureWidth; + _pictureHeight = pictureHeight; + } + public void AddSet(string name) + { + if (!_AirFighterStorages.ContainsKey(name)) + { + var fighterCollection = new AirFighterGenericCollection(_pictureWidth, _pictureHeight); + _AirFighterStorages.Add(name, fighterCollection); + } + } + public void DelSet(string name) + { + if (_AirFighterStorages.ContainsKey(name)) + { + _AirFighterStorages.Remove(name); + } + } + public AirFighterGenericCollection? this[string ind] + { + get + { + if (_AirFighterStorages.ContainsKey(ind)) + { + return _AirFighterStorages[ind]; + } + return null; + } + } + } +} + diff --git a/AirFighter/AirFighter/FormFighterCollection.Designer.cs b/AirFighter/AirFighter/FormAirFighterCollection.Designer.cs similarity index 62% rename from AirFighter/AirFighter/FormFighterCollection.Designer.cs rename to AirFighter/AirFighter/FormAirFighterCollection.Designer.cs index 0cd3e7c..b440b6d 100644 --- a/AirFighter/AirFighter/FormFighterCollection.Designer.cs +++ b/AirFighter/AirFighter/FormAirFighterCollection.Designer.cs @@ -1,6 +1,6 @@ namespace AirFighter { - partial class FormFighterCollection + partial class FormAirFighterCollection { /// /// Required designer variable. @@ -29,17 +29,24 @@ private void InitializeComponent() { groupBox1 = new GroupBox(); + groupBox2 = new GroupBox(); + buttonDelObject = new Button(); + listBoxStorage = new ListBox(); + buttonAddObject = new Button(); + textBoxStorageName = new TextBox(); maskedTextBoxNumber = new MaskedTextBox(); buttonRemoveFighter = new Button(); buttonRefreshCollection = new Button(); buttonAddFighter = new Button(); pictureBoxCollection = new PictureBox(); groupBox1.SuspendLayout(); + groupBox2.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); SuspendLayout(); // // groupBox1 // + groupBox1.Controls.Add(groupBox2); groupBox1.Controls.Add(maskedTextBoxNumber); groupBox1.Controls.Add(buttonRemoveFighter); groupBox1.Controls.Add(buttonRefreshCollection); @@ -51,10 +58,60 @@ groupBox1.TabStop = false; groupBox1.Text = "Инструменты"; // + // groupBox2 + // + groupBox2.Controls.Add(buttonDelObject); + groupBox2.Controls.Add(listBoxStorage); + groupBox2.Controls.Add(buttonAddObject); + groupBox2.Controls.Add(textBoxStorageName); + groupBox2.Location = new Point(3, 19); + groupBox2.Name = "groupBox2"; + groupBox2.Size = new Size(200, 284); + groupBox2.TabIndex = 6; + groupBox2.TabStop = false; + groupBox2.Text = "Наборы"; + // + // buttonDelObject + // + buttonDelObject.Location = new Point(18, 244); + buttonDelObject.Name = "buttonDelObject"; + buttonDelObject.Size = new Size(159, 34); + buttonDelObject.TabIndex = 2; + buttonDelObject.Text = "Удалить набор"; + buttonDelObject.UseVisualStyleBackColor = true; + buttonDelObject.Click += ButtonDelObject_Click; + // + // listBoxStorage + // + listBoxStorage.FormattingEnabled = true; + listBoxStorage.ItemHeight = 15; + listBoxStorage.Location = new Point(15, 88); + listBoxStorage.Name = "listBoxStorage"; + listBoxStorage.Size = new Size(162, 139); + listBoxStorage.TabIndex = 2; + listBoxStorage.SelectedIndexChanged += listBoxStorage_SelectedIndexChanged; + // + // buttonAddObject + // + buttonAddObject.Location = new Point(18, 48); + buttonAddObject.Name = "buttonAddObject"; + buttonAddObject.Size = new Size(159, 34); + buttonAddObject.TabIndex = 1; + buttonAddObject.Text = "Добавить набор"; + buttonAddObject.UseVisualStyleBackColor = true; + buttonAddObject.Click += ButtonAddObject_Click; + // + // textBoxStorageName + // + textBoxStorageName.Location = new Point(3, 19); + textBoxStorageName.Name = "textBoxStorageName"; + textBoxStorageName.Size = new Size(191, 23); + textBoxStorageName.TabIndex = 0; + // // maskedTextBoxNumber // maskedTextBoxNumber.Font = new Font("Showcard Gothic", 9F, FontStyle.Regular, GraphicsUnit.Point); - maskedTextBoxNumber.Location = new Point(37, 103); + maskedTextBoxNumber.Location = new Point(39, 346); maskedTextBoxNumber.Mask = "00"; maskedTextBoxNumber.Name = "maskedTextBoxNumber"; maskedTextBoxNumber.Size = new Size(100, 22); @@ -62,7 +119,7 @@ // // buttonRemoveFighter // - buttonRemoveFighter.Location = new Point(37, 155); + buttonRemoveFighter.Location = new Point(17, 374); buttonRemoveFighter.Name = "buttonRemoveFighter"; buttonRemoveFighter.Size = new Size(166, 31); buttonRemoveFighter.TabIndex = 3; @@ -72,7 +129,7 @@ // // buttonRefreshCollection // - buttonRefreshCollection.Location = new Point(37, 209); + buttonRefreshCollection.Location = new Point(17, 411); buttonRefreshCollection.Name = "buttonRefreshCollection"; buttonRefreshCollection.Size = new Size(166, 31); buttonRefreshCollection.TabIndex = 4; @@ -82,7 +139,7 @@ // // buttonAddFighter // - buttonAddFighter.Location = new Point(37, 36); + buttonAddFighter.Location = new Point(17, 309); buttonAddFighter.Name = "buttonAddFighter"; buttonAddFighter.Size = new Size(166, 31); buttonAddFighter.TabIndex = 2; @@ -111,6 +168,8 @@ Text = "Набор истребителя"; groupBox1.ResumeLayout(false); groupBox1.PerformLayout(); + groupBox2.ResumeLayout(false); + groupBox2.PerformLayout(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit(); ResumeLayout(false); } @@ -123,5 +182,10 @@ private Button buttonRefreshCollection; private Button buttonAddFighter; private MaskedTextBox maskedTextBoxNumber; + private GroupBox groupBox2; + private TextBox textBoxStorageName; + private Button buttonDelObject; + private ListBox listBoxStorage; + private Button buttonAddObject; } } \ No newline at end of file diff --git a/AirFighter/AirFighter/FormAirFighterCollection.cs b/AirFighter/AirFighter/FormAirFighterCollection.cs new file mode 100644 index 0000000..3380862 --- /dev/null +++ b/AirFighter/AirFighter/FormAirFighterCollection.cs @@ -0,0 +1,187 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; +using AirFighter.DrawningObjects; +using AirFighter.Drawnings; +using AirFighter.Generics; +using AirFighter.MovementStrategy; + +namespace AirFighter +{ + /// + /// Форма для работы с набором объектов класса DrawningCar + /// + public partial class FormAirFighterCollection : Form + { + /// + /// Набор объектов + /// + private readonly AirFighterGenericStorage _storage; + /// + /// Конструктор + /// + public FormAirFighterCollection() + { + InitializeComponent(); + _storage = new AirFighterGenericStorage(pictureBoxCollection.Width, pictureBoxCollection.Height); + } + /// + /// Заполнение listBoxObjects + /// + private void ReloadObjects() + { + int index = listBoxStorage.SelectedIndex; + listBoxStorage.Items.Clear(); + for (int i = 0; i < _storage.Keys.Count; i++) + { + listBoxStorage.Items.Add(_storage.Keys[i]); + } + if (listBoxStorage.Items.Count > 0 && (index == -1 || index + >= listBoxStorage.Items.Count)) + { + listBoxStorage.SelectedIndex = 0; + } + else if (listBoxStorage.Items.Count > 0 && index > -1 && + index < listBoxStorage.Items.Count) + { + listBoxStorage.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 listBoxStorage_SelectedIndexChanged(object sender, + EventArgs e) + { + pictureBoxCollection.Image = + _storage[listBoxStorage.SelectedItem?.ToString() ?? string.Empty]?.ShowAirFighter(); + } + /// + /// Удаление набора + /// + /// + /// + private void ButtonDelObject_Click(object sender, EventArgs e) + { + if (listBoxStorage.SelectedIndex == -1) + { + return; + } + if (MessageBox.Show($"Удалить объект {listBoxStorage.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) + + { + _storage.DelSet(listBoxStorage.SelectedItem.ToString() + ?? string.Empty); + ReloadObjects(); + } + } + /// + /// Добавление объекта в набор + /// + /// + /// + private void ButtonAddAirFighter_Click(object sender, EventArgs e) + { + if (listBoxStorage.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxStorage.SelectedItem.ToString() ?? + string.Empty]; + if (obj == null) + { + return; + } + FormAirFighter form = new(); + if (form.ShowDialog() == DialogResult.OK) + { + if (obj + form.SelectedAirFighter > -1) + { + MessageBox.Show("Объект добавлен"); + pictureBoxCollection.Image = obj.ShowAirFighter(); + } + else + { + MessageBox.Show("Не удалось добавить объект"); + } + } + + } + /// + /// Удаление объекта из набора + /// + /// + /// + private void ButtonRemoveAirFighter_Click(object sender, EventArgs e) + { + if (listBoxStorage.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxStorage.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.ShowAirFighter(); + } + else + { + MessageBox.Show("Не удалось удалить объект"); + } + } + /// + /// Обновление рисунка по набору + /// + /// + /// + private void ButtonRefreshCollection_Click(object sender, EventArgs e) + { + if (listBoxStorage.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxStorage.SelectedItem.ToString() ?? + string.Empty]; + if (obj == null) + { + return; + } + pictureBoxCollection.Image = obj.ShowAirFighter(); + } + } +} diff --git a/AirFighter/AirFighter/FormFighterCollection.resx b/AirFighter/AirFighter/FormAirFighterCollection.resx similarity index 100% rename from AirFighter/AirFighter/FormFighterCollection.resx rename to AirFighter/AirFighter/FormAirFighterCollection.resx diff --git a/AirFighter/AirFighter/FormFighterCollection.cs b/AirFighter/AirFighter/FormFighterCollection.cs deleted file mode 100644 index 339ac16..0000000 --- a/AirFighter/AirFighter/FormFighterCollection.cs +++ /dev/null @@ -1,69 +0,0 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Data; -using System.Drawing; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Windows.Forms; -using AirFighter.DrawningObjects; -using AirFighter.Drawnings; -using AirFighter.Generics; -using AirFighter.MovementStrategy; - -namespace AirFighter -{ - public partial class FormFighterCollection : Form - { - private readonly AirFighterGenericCollection _fighter; - public FormFighterCollection() - { - InitializeComponent(); - _fighter = new AirFighterGenericCollection(pictureBoxCollection.Width, pictureBoxCollection.Height); - } - - private void ButtonAddAirFighter_Click(object sender, EventArgs e) - { - FormAirFighter form = new(); - if (form.ShowDialog() == DialogResult.OK) - { - if (_fighter + form.SelectedAirFighter != null) - { - MessageBox.Show("Объект добавлен"); - pictureBoxCollection.Image = _fighter.ShowAirFighter(); - } - else - { - MessageBox.Show("Не удалось добавить объект"); - } - - } - - } - - private void ButtonRemoveAirFighter_Click(object sender, EventArgs e) - { - if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) - { - return; - } - int pos = Convert.ToInt32(maskedTextBoxNumber.Text); - if (_fighter - pos != null) - { - MessageBox.Show("Объект удален"); - pictureBoxCollection.Image = _fighter.ShowAirFighter(); - } - else - { - MessageBox.Show("Не удалось удалить объект"); - } - - } - - private void ButtonRefreshCollection_Click(object sender, EventArgs e) - { - pictureBoxCollection.Image = _fighter.ShowAirFighter(); - } - } -} diff --git a/AirFighter/AirFighter/Program.cs b/AirFighter/AirFighter/Program.cs index 0020ca3..1dd7fab 100644 --- a/AirFighter/AirFighter/Program.cs +++ b/AirFighter/AirFighter/Program.cs @@ -11,7 +11,7 @@ namespace AirFighter // To customize application configuration such as set high DPI settings or default font, // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); - Application.Run(new FormFighterCollection()); + Application.Run(new FormAirFighterCollection()); } } } \ No newline at end of file diff --git a/AirFighter/AirFighter/SetGeneric.cs b/AirFighter/AirFighter/SetGeneric.cs index 5546ae5..318e7f5 100644 --- a/AirFighter/AirFighter/SetGeneric.cs +++ b/AirFighter/AirFighter/SetGeneric.cs @@ -3,53 +3,63 @@ internal class SetGeneric where T : class { - private readonly T[] _places; - public int Count => _places.Length; + private readonly List _places; + 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 fighter) { - for (int i = 0; i < _places.Length; i++) - { - if (_places[i] == null) - { - _places[i] = fighter; - return i; - } - } - return -1; + return Insert(fighter, 0); } + private bool isCorrectPosition(int position) + { + return 0 <= position && position < _maxCount; + } public int Insert(T fighter, int position) { - int index = position; - - while (_places[index] != null && index < _places.Length) index++; - - if (index == _places.Length) return -1; - for (int i = index; i > position; --i) _places[i] = _places[i - 1]; - - _places[position] = fighter; + if (!isCorrectPosition(position)) + { + return -1; + } + _places.Insert(position, fighter); return position; } public bool Remove(int position) { - if (position < 0 || position >= _places.Length) - { - return false; - } - _places[position] = null; + if ((position < 0) || (position > _maxCount)) return false; + _places.RemoveAt(position); return true; } - public T? Get(int position) + public T? this[int position] { - if (position < 0 || position >= _places.Length) + 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 GetAirFighter(int? maxAirFighter = null) + { + for (int i = 0; i < _places.Count; ++i) + { + yield return _places[i]; + if (maxAirFighter.HasValue && i == maxAirFighter.Value) + { + yield break; + } } - return _places[position]; } } }