From 5510581b53fa9f8ad23678ed0ec2879e877956c6 Mon Sep 17 00:00:00 2001 From: olshab Date: Sun, 19 Nov 2023 17:56:27 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9B=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D0=BD=D0=B0=D1=8F=20=E2=84=964=20=D0=B3=D0=BE?= =?UTF-8?q?=D1=82=D0=BE=D0=B2=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AirBomber/FormEntityCollection.Designer.cs | 73 ++++++++++++++- AirBomber/FormEntityCollection.cs | 90 ++++++++++++++++--- .../Generics/EntitiesGenericCollection.cs | 10 +-- AirBomber/Generics/EntitiesGenericStorage.cs | 53 +++++++++++ AirBomber/Generics/SetGeneric.cs | 71 +++++++++------ 5 files changed, 247 insertions(+), 50 deletions(-) create mode 100644 AirBomber/Generics/EntitiesGenericStorage.cs diff --git a/AirBomber/FormEntityCollection.Designer.cs b/AirBomber/FormEntityCollection.Designer.cs index ffb0a01..885b63f 100644 --- a/AirBomber/FormEntityCollection.Designer.cs +++ b/AirBomber/FormEntityCollection.Designer.cs @@ -30,12 +30,18 @@ { CollectionPictureBox = new PictureBox(); ToolGroupBox = new GroupBox(); + groupBox1 = new GroupBox(); + SetNameTextBox = new TextBox(); + StorageListBox = new ListBox(); + AddSetButton = new Button(); + RemoveSetButton = new Button(); RefreshCollectionButton = new Button(); RemoveBomberButton = new Button(); NumberMaskedTextBox = new MaskedTextBox(); AddBomberButton = new Button(); ((System.ComponentModel.ISupportInitialize)CollectionPictureBox).BeginInit(); ToolGroupBox.SuspendLayout(); + groupBox1.SuspendLayout(); SuspendLayout(); // // CollectionPictureBox @@ -50,6 +56,7 @@ // ToolGroupBox // ToolGroupBox.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Right; + ToolGroupBox.Controls.Add(groupBox1); ToolGroupBox.Controls.Add(RefreshCollectionButton); ToolGroupBox.Controls.Add(RemoveBomberButton); ToolGroupBox.Controls.Add(NumberMaskedTextBox); @@ -61,9 +68,60 @@ ToolGroupBox.TabStop = false; ToolGroupBox.Text = "Инструменты"; // + // groupBox1 + // + groupBox1.Controls.Add(SetNameTextBox); + groupBox1.Controls.Add(StorageListBox); + groupBox1.Controls.Add(AddSetButton); + groupBox1.Controls.Add(RemoveSetButton); + groupBox1.Location = new Point(6, 22); + groupBox1.Name = "groupBox1"; + groupBox1.Size = new Size(164, 232); + groupBox1.TabIndex = 8; + groupBox1.TabStop = false; + groupBox1.Text = "Наборы"; + // + // SetNameTextBox + // + SetNameTextBox.Location = new Point(6, 22); + SetNameTextBox.Name = "SetNameTextBox"; + SetNameTextBox.Size = new Size(152, 23); + SetNameTextBox.TabIndex = 4; + // + // StorageListBox + // + StorageListBox.FormattingEnabled = true; + StorageListBox.ItemHeight = 15; + StorageListBox.Items.AddRange(new object[] { "Наборы здесь..." }); + StorageListBox.Location = new Point(6, 96); + StorageListBox.Name = "StorageListBox"; + StorageListBox.Size = new Size(152, 79); + StorageListBox.TabIndex = 7; + StorageListBox.SelectedIndexChanged += StorageListBoxIndexChanged; + // + // AddSetButton + // + AddSetButton.Location = new Point(6, 51); + AddSetButton.Name = "AddSetButton"; + AddSetButton.Size = new Size(152, 30); + AddSetButton.TabIndex = 5; + AddSetButton.Text = "Добавить набор"; + AddSetButton.UseVisualStyleBackColor = true; + AddSetButton.Click += ButtonAddSet_Click; + // + // RemoveSetButton + // + RemoveSetButton.Location = new Point(6, 190); + RemoveSetButton.Name = "RemoveSetButton"; + RemoveSetButton.Size = new Size(152, 30); + RemoveSetButton.TabIndex = 6; + RemoveSetButton.Text = "Удалить набор"; + RemoveSetButton.UseVisualStyleBackColor = true; + RemoveSetButton.Click += ButtonRemoveSet_Click; + // // RefreshCollectionButton // - RefreshCollectionButton.Location = new Point(6, 190); + RefreshCollectionButton.Location = new Point(6, 543); RefreshCollectionButton.Name = "RefreshCollectionButton"; RefreshCollectionButton.Size = new Size(164, 30); RefreshCollectionButton.TabIndex = 3; @@ -73,7 +131,7 @@ // // RemoveBomberButton // - RemoveBomberButton.Location = new Point(6, 141); + RemoveBomberButton.Location = new Point(6, 467); RemoveBomberButton.Name = "RemoveBomberButton"; RemoveBomberButton.Size = new Size(164, 30); RemoveBomberButton.TabIndex = 2; @@ -83,7 +141,7 @@ // // NumberMaskedTextBox // - NumberMaskedTextBox.Location = new Point(6, 102); + NumberMaskedTextBox.Location = new Point(6, 428); NumberMaskedTextBox.Mask = "00000"; NumberMaskedTextBox.Name = "NumberMaskedTextBox"; NumberMaskedTextBox.Size = new Size(164, 23); @@ -92,7 +150,7 @@ // // AddBomberButton // - AddBomberButton.Location = new Point(6, 32); + AddBomberButton.Location = new Point(6, 358); AddBomberButton.Name = "AddBomberButton"; AddBomberButton.Size = new Size(164, 43); AddBomberButton.TabIndex = 0; @@ -112,6 +170,8 @@ ((System.ComponentModel.ISupportInitialize)CollectionPictureBox).EndInit(); ToolGroupBox.ResumeLayout(false); ToolGroupBox.PerformLayout(); + groupBox1.ResumeLayout(false); + groupBox1.PerformLayout(); ResumeLayout(false); } @@ -123,5 +183,10 @@ private Button RemoveBomberButton; private MaskedTextBox NumberMaskedTextBox; private Button AddBomberButton; + private GroupBox groupBox1; + private TextBox SetNameTextBox; + private ListBox StorageListBox; + private Button AddSetButton; + private Button RemoveSetButton; } } \ No newline at end of file diff --git a/AirBomber/FormEntityCollection.cs b/AirBomber/FormEntityCollection.cs index 387477c..d6a3fd5 100644 --- a/AirBomber/FormEntityCollection.cs +++ b/AirBomber/FormEntityCollection.cs @@ -1,34 +1,52 @@ using AirBomber.Generics; -using AirBomber.MovementStrategy; -using AirBomber.Rendering; namespace AirBomber { public partial class FormEntityCollection : Form { - private readonly EntitiesGenericCollection _entities; - + private readonly EntitiesGenericStorage _storage; public FormEntityCollection() { InitializeComponent(); - _entities = new EntitiesGenericCollection( - CollectionPictureBox.Width, CollectionPictureBox.Height - ); + _storage = new EntitiesGenericStorage(CollectionPictureBox.Width, CollectionPictureBox.Height); + } + + private void ReloadObjects() + { + int index = StorageListBox.SelectedIndex; + StorageListBox.Items.Clear(); + + for (int i = 0; i < _storage.Keys.Count; i++) + StorageListBox.Items.Add(_storage.Keys[i]); + + if (StorageListBox.Items.Count > 0 && (index == -1 || index >= StorageListBox.Items.Count)) + StorageListBox.SelectedIndex = 0; + + else if (StorageListBox.Items.Count > 0 && index > -1 && index < StorageListBox.Items.Count) + StorageListBox.SelectedIndex = index; } public void ButtonAddEntity_Click(object sender, EventArgs e) { + if (StorageListBox.SelectedIndex == -1) + return; + + var obj = _storage[StorageListBox.SelectedItem.ToString() ?? string.Empty]; + if (obj is null) + return; + BomberForm Form = new BomberForm(); if (Form.ShowDialog() == DialogResult.OK) { - if (_entities + Form.SelectedRenderer != -1) + if (obj + Form.SelectedRenderer != -1) { MessageBox.Show("Объект добавлен"); - CollectionPictureBox.Image = _entities.ShowEntities(); + CollectionPictureBox.Image = obj.ShowEntities(); } + else { MessageBox.Show("Не удалось добавить объект"); @@ -38,14 +56,21 @@ namespace AirBomber public void ButtonRemoveEntity_Click(object sender, EventArgs e) { + if (StorageListBox.SelectedIndex == -1) + return; + + var obj = _storage[StorageListBox.SelectedItem.ToString() ?? string.Empty]; + if (obj is null) + return; + if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) return; int Pos = Convert.ToInt32(NumberMaskedTextBox.Text); - if (_entities - Pos == true) + if (obj - Pos == true) { MessageBox.Show("Объект удален"); - CollectionPictureBox.Image = _entities.ShowEntities(); + CollectionPictureBox.Image = obj.ShowEntities(); } else { @@ -55,7 +80,48 @@ namespace AirBomber public void ButtonRefreshCollection_Click(object sender, EventArgs e) { - CollectionPictureBox.Image = _entities.ShowEntities(); + if (StorageListBox.SelectedIndex == -1) + return; + + var obj = _storage[StorageListBox.SelectedItem.ToString() ?? string.Empty]; + if (obj is null) + return; + + CollectionPictureBox.Image = obj.ShowEntities(); + } + + private void ButtonAddSet_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(SetNameTextBox.Text)) + { + MessageBox.Show("Не все данные заполнены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + + _storage.AddSet(SetNameTextBox.Text); + ReloadObjects(); + } + + private void ButtonRemoveSet_Click(object sender, EventArgs e) + { + if (StorageListBox.SelectedIndex == -1) + return; + + if (MessageBox.Show( + $"Удалить объект{StorageListBox.SelectedItem}?", + "Удаление", + MessageBoxButtons.YesNo, + MessageBoxIcon.Question + ) == DialogResult.Yes) + { + _storage.RemoveSet(StorageListBox.SelectedItem.ToString() ?? string.Empty); + ReloadObjects(); + } + } + + private void StorageListBoxIndexChanged(object sender, EventArgs e) + { + CollectionPictureBox.Image = _storage[StorageListBox.SelectedItem?.ToString() ?? string.Empty]?.ShowEntities(); } } } diff --git a/AirBomber/Generics/EntitiesGenericCollection.cs b/AirBomber/Generics/EntitiesGenericCollection.cs index 5867cb3..f42b3a1 100644 --- a/AirBomber/Generics/EntitiesGenericCollection.cs +++ b/AirBomber/Generics/EntitiesGenericCollection.cs @@ -34,7 +34,7 @@ namespace AirBomber.Generics public static bool operator -(EntitiesGenericCollection collect, int pos) { - T? obj = collect._collection.Get(pos); + T? obj = collect._collection[pos]; if (obj != null) return collect._collection.Remove(pos); @@ -44,7 +44,7 @@ namespace AirBomber.Generics public U? GetU(int pos) { - return (U?)_collection.Get(pos)?.MovableObject; + return (U?)_collection[pos]?.MovableObject; } public Bitmap ShowEntities() @@ -81,10 +81,10 @@ namespace AirBomber.Generics private void DrawObjects(Graphics g) { - for (int i = 0; i < _collection.Count; i++) + int i = -1; + foreach (T? Entity in _collection.GetEntities()) { - T? Entity = _collection.Get(i); - + i++; if (Entity is null) continue; diff --git a/AirBomber/Generics/EntitiesGenericStorage.cs b/AirBomber/Generics/EntitiesGenericStorage.cs new file mode 100644 index 0000000..af2fa34 --- /dev/null +++ b/AirBomber/Generics/EntitiesGenericStorage.cs @@ -0,0 +1,53 @@ +using AirBomber.MovementStrategy; +using AirBomber.Rendering; + +namespace AirBomber.Generics +{ + internal class EntitiesGenericStorage + { + readonly Dictionary> _entityStorages; + + public List Keys => _entityStorages.Keys.ToList(); + + private readonly int _pictureWidth; + private readonly int _pictureHeight; + + public EntitiesGenericStorage(int PictureWidth, int PictureHeight) + { + _entityStorages = new Dictionary>(); + + _pictureWidth = PictureWidth; + _pictureHeight = PictureHeight; + } + + public void AddSet(string Name) + { + var NewCollection = + new EntitiesGenericCollection(_pictureWidth, _pictureHeight); + + if (Keys.Contains(Name)) + { + MessageBox.Show("Набор с таким именем уже существует", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + + _entityStorages.Add(Name, NewCollection); + } + + public void RemoveSet(string Name) + { + _entityStorages.Remove(Name); + } + + public EntitiesGenericCollection? this[string Index] + { + get + { + if (!_entityStorages.ContainsKey(Index)) + return null; + + return _entityStorages[Index]; + } + } + } +} diff --git a/AirBomber/Generics/SetGeneric.cs b/AirBomber/Generics/SetGeneric.cs index 1fe3b22..7aa044d 100644 --- a/AirBomber/Generics/SetGeneric.cs +++ b/AirBomber/Generics/SetGeneric.cs @@ -1,19 +1,26 @@ namespace AirBomber.Generics { internal class SetGeneric + where T : class { - private readonly T?[] _objects; + private readonly List _objects; + private readonly int _maxCount; - public int Count => _objects.Length; + public int Count => _objects.Count; public SetGeneric(int Count) { - _objects = new T?[Count]; + _maxCount = Count; + _objects = new List(_maxCount); + + for (int i = 0; i < _maxCount; i++) + _objects.Add(null); } public int Insert(T Entity) { - for (int i = 0; i < Count; i++) + /** Вставка в начало набора */ + for (int i = 0; i < _maxCount; i++) if (_objects[i] is null) { _objects[i] = Entity; @@ -25,7 +32,7 @@ public int Insert(T Entity, int Position) { - if (Position >= Count) + if (Position >= _maxCount) return -1; if (_objects[Position] is null) @@ -34,42 +41,48 @@ return Position; } - /** Сдвиг элементов вправо начиная с Position до ближайшего пустого места */ - int EmptyPos = -1; - - for (int i = Position + 1; i < Count; i++) - if (_objects[i] is null) - { - EmptyPos = i; - break; - } - - if (EmptyPos == -1) - return -1; - - /** Сдвиг */ - for (int i = EmptyPos; i > Position; i--) - _objects[i] = _objects[i - 1]; - - _objects[Position] = Entity; - return Position; + return -1; } public bool Remove(int Position) { - if (Position >= Count) + if (Position >= _maxCount) return false; _objects[Position] = default(T); return true; } - public T? Get(int Position) + public T? this[int Position] { - if (Position >= Count) - return default(T); + get + { + if (Position >= _maxCount) + return null; - return _objects[Position]; + return _objects[Position]; + } + set + { + if (Position >= _maxCount) + return; + + if (_objects[Position] is not null) + return; + + _objects[Position] = value; + } + } + + public IEnumerable GetEntities(int? MaxEntities = null) + { + for (int i = 0; i < _objects.Count; ++i) + { + yield return _objects[i]; + + if (MaxEntities.HasValue && i == MaxEntities.Value) + yield break; + } } } }