From 86f46e95681e03514c1859d1f387f439de165f4e Mon Sep 17 00:00:00 2001 From: Efi Date: Thu, 7 Dec 2023 21:24:31 +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=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=B0=204?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../FormWarAirplaneCollection.Designer.cs | 104 +++++++++++++---- .../AirBomber/FormWarAirplaneCollection.cs | 109 ++++++++++++++++-- AirBomber/AirBomber/SetGeneric.cs | 88 ++++++++------ .../AirBomber/WarAirplaneGenericCollection.cs | 32 +++-- .../AirBomber/WarAirplaneGenericStorage.cs | 55 +++++++++ 5 files changed, 305 insertions(+), 83 deletions(-) create mode 100644 AirBomber/AirBomber/WarAirplaneGenericStorage.cs diff --git a/AirBomber/AirBomber/FormWarAirplaneCollection.Designer.cs b/AirBomber/AirBomber/FormWarAirplaneCollection.Designer.cs index 92c0ee2..0ffa662 100644 --- a/AirBomber/AirBomber/FormWarAirplaneCollection.Designer.cs +++ b/AirBomber/AirBomber/FormWarAirplaneCollection.Designer.cs @@ -28,29 +28,84 @@ /// private void InitializeComponent() { - panel1 = new Panel(); + panelCollection = new Panel(); + panelObject = new Panel(); + textBoxStorageName = new TextBox(); + buttonAddObject = new Button(); + buttonDelObject = new Button(); + listBoxStorages = new ListBox(); Tools = new Label(); buttonRefreshCollection = new Button(); buttonRemove = new Button(); buttonAdd = new Button(); maskedTextBoxNumber = new MaskedTextBox(); pictureBoxCollection = new PictureBox(); - panel1.SuspendLayout(); + panelCollection.SuspendLayout(); + panelObject.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); SuspendLayout(); // - // panel1 + // panelCollection // - panel1.Controls.Add(Tools); - panel1.Controls.Add(buttonRefreshCollection); - panel1.Controls.Add(buttonRemove); - panel1.Controls.Add(buttonAdd); - panel1.Controls.Add(maskedTextBoxNumber); - panel1.Dock = DockStyle.Right; - panel1.Location = new Point(617, 0); - panel1.Name = "panel1"; - panel1.Size = new Size(183, 450); - panel1.TabIndex = 0; + panelCollection.Controls.Add(panelObject); + panelCollection.Controls.Add(Tools); + panelCollection.Controls.Add(buttonRefreshCollection); + panelCollection.Controls.Add(buttonRemove); + panelCollection.Controls.Add(buttonAdd); + panelCollection.Controls.Add(maskedTextBoxNumber); + panelCollection.Dock = DockStyle.Right; + panelCollection.Location = new Point(617, 0); + panelCollection.Name = "panelCollection"; + panelCollection.Size = new Size(183, 450); + panelCollection.TabIndex = 0; + // + // panelObject + // + panelObject.Controls.Add(textBoxStorageName); + panelObject.Controls.Add(buttonAddObject); + panelObject.Controls.Add(buttonDelObject); + panelObject.Controls.Add(listBoxStorages); + panelObject.Location = new Point(6, 27); + panelObject.Name = "panelObject"; + panelObject.Size = new Size(171, 214); + panelObject.TabIndex = 4; + // + // textBoxStorageName + // + textBoxStorageName.Location = new Point(7, 19); + textBoxStorageName.Name = "textBoxStorageName"; + textBoxStorageName.Size = new Size(158, 23); + textBoxStorageName.TabIndex = 2; + // + // buttonAddObject + // + buttonAddObject.Location = new Point(7, 48); + buttonAddObject.Name = "buttonAddObject"; + buttonAddObject.Size = new Size(158, 31); + buttonAddObject.TabIndex = 2; + buttonAddObject.Text = "Добавить объект"; + buttonAddObject.UseVisualStyleBackColor = true; + buttonAddObject.Click += ButtonAddObject_Click; + // + // buttonDelObject + // + buttonDelObject.Location = new Point(7, 167); + buttonDelObject.Name = "buttonDelObject"; + buttonDelObject.Size = new Size(158, 31); + buttonDelObject.TabIndex = 2; + buttonDelObject.Text = "Удалить объект"; + buttonDelObject.UseVisualStyleBackColor = true; + buttonDelObject.Click += ButtonDelObject__Click; + // + // listBoxStorages + // + listBoxStorages.FormattingEnabled = true; + listBoxStorages.ItemHeight = 15; + listBoxStorages.Location = new Point(7, 85); + listBoxStorages.Name = "listBoxStorages"; + listBoxStorages.Size = new Size(158, 64); + listBoxStorages.TabIndex = 2; + listBoxStorages.SelectedIndexChanged += ListBoxObjects_SelectedIndexChanged; // // Tools // @@ -63,7 +118,7 @@ // // buttonRefreshCollection // - buttonRefreshCollection.Location = new Point(6, 179); + buttonRefreshCollection.Location = new Point(3, 395); buttonRefreshCollection.Name = "buttonRefreshCollection"; buttonRefreshCollection.Size = new Size(174, 43); buttonRefreshCollection.TabIndex = 2; @@ -73,7 +128,7 @@ // // buttonRemove // - buttonRemove.Location = new Point(6, 120); + buttonRemove.Location = new Point(3, 336); buttonRemove.Name = "buttonRemove"; buttonRemove.Size = new Size(174, 31); buttonRemove.TabIndex = 2; @@ -83,7 +138,7 @@ // // buttonAdd // - buttonAdd.Location = new Point(6, 39); + buttonAdd.Location = new Point(3, 255); buttonAdd.Name = "buttonAdd"; buttonAdd.Size = new Size(174, 31); buttonAdd.TabIndex = 2; @@ -93,7 +148,7 @@ // // maskedTextBoxNumber // - maskedTextBoxNumber.Location = new Point(6, 91); + maskedTextBoxNumber.Location = new Point(3, 307); maskedTextBoxNumber.Name = "maskedTextBoxNumber"; maskedTextBoxNumber.Size = new Size(174, 23); maskedTextBoxNumber.TabIndex = 0; @@ -113,23 +168,30 @@ AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(800, 450); Controls.Add(pictureBoxCollection); - Controls.Add(panel1); + Controls.Add(panelCollection); Name = "FormWarAirplaneCollection"; Text = "Набор военных самолётов"; - panel1.ResumeLayout(false); - panel1.PerformLayout(); + panelCollection.ResumeLayout(false); + panelCollection.PerformLayout(); + panelObject.ResumeLayout(false); + panelObject.PerformLayout(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit(); ResumeLayout(false); } #endregion - private Panel panel1; + private Panel panelCollection; private PictureBox pictureBoxCollection; private Label Tools; private Button buttonRefreshCollection; private Button buttonRemove; private Button buttonAdd; private MaskedTextBox maskedTextBoxNumber; + private ListBox listBoxStorages; + private Button buttonAddObject; + private Button buttonDelObject; + private Panel panelObject; + private TextBox textBoxStorageName; } } \ No newline at end of file diff --git a/AirBomber/AirBomber/FormWarAirplaneCollection.cs b/AirBomber/AirBomber/FormWarAirplaneCollection.cs index 219e714..c13e9f4 100644 --- a/AirBomber/AirBomber/FormWarAirplaneCollection.cs +++ b/AirBomber/AirBomber/FormWarAirplaneCollection.cs @@ -15,21 +15,86 @@ namespace AirBomber { public partial class FormWarAirplaneCollection : Form { - private readonly WarAirplaneGenericCollection _warAirplane; + private readonly WarAirplaneGenericStorage _storage; public FormWarAirplaneCollection() { InitializeComponent(); - _warAirplane = new WarAirplaneGenericCollection(pictureBoxCollection.Width, pictureBoxCollection.Height); + _storage = new WarAirplaneGenericStorage(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 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(); + } + + } + private void ListBoxObjects_SelectedIndexChanged(object sender, EventArgs e) + { + + pictureBoxCollection.Image = + _storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowWarAirplane(); + } + + private void ButtonAddWarAirplane_Click(object sender, EventArgs e) { - FormAirBomber form = new(); - if(form.ShowDialog() == DialogResult.OK) + if (listBoxStorages.SelectedIndex == -1) { - if (_warAirplane + form.SelectedWarAirplane != null) + return; + } + var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty]; + if (obj == null) + { + return; + } + FormAirBomber form = new(); + if (form.ShowDialog() == DialogResult.OK) + { + if (obj + form.SelectedWarAirplane) { MessageBox.Show("Объект добавлен"); - pictureBoxCollection.Image = _warAirplane.ShowWarAirplane(); + pictureBoxCollection.Image = obj.ShowWarAirplane(); } else { @@ -39,25 +104,45 @@ namespace AirBomber } private void ButtonRemoveWarAirplane_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) + MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) { return; } int pos = Convert.ToInt32(maskedTextBoxNumber.Text); - if (_warAirplane - pos != null) + if (obj - pos != null) { - MessageBox.Show("Объект удалён"); - pictureBoxCollection.Image = _warAirplane.ShowWarAirplane(); + MessageBox.Show("Объект удален"); + pictureBoxCollection.Image = obj.ShowWarAirplane(); } else { - MessageBox.Show("Не удалось добавить объект"); + MessageBox.Show("Не удалось удалить объект"); } } private void ButtonRefreshCollection_Click(object sender, EventArgs e) { - pictureBoxCollection.Image = _warAirplane.ShowWarAirplane(); + if (listBoxStorages.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? + string.Empty]; + if (obj == null) + { + return; + } + pictureBoxCollection.Image = obj.ShowWarAirplane(); } } diff --git a/AirBomber/AirBomber/SetGeneric.cs b/AirBomber/AirBomber/SetGeneric.cs index 81cdd85..365ea4c 100644 --- a/AirBomber/AirBomber/SetGeneric.cs +++ b/AirBomber/AirBomber/SetGeneric.cs @@ -9,58 +9,80 @@ namespace AirBomber.Generics internal class SetGeneric where T : class { - private readonly T?[] _places; - public int Count => _places.Length; + private readonly List _places; + public int Count => _places.Count; public int startPointer = 0; + public readonly int _maxCount; public SetGeneric(int count) { - _places = new T?[count]; + _maxCount = count; + _places = new List(count); } - public int Insert(T WarAirplane) + public bool Insert(T WarAirplane) { - if (_places[Count - 1] != null) - return -1; - return Insert(WarAirplane, 0); - } - public int Insert(T WarAirplane, int position) - { - if (!(position >= 0 && position < Count)) - return -1; - if (_places[position] != null) + if (_places.Count == _maxCount) { - int indexEnd = position + 1; - while (_places[indexEnd] != null) - { - indexEnd++; - } - for (int i = indexEnd + 1; i > position; i--) - { - _places[i] = _places[i - 1]; - } - + return false; } - _places[position] = WarAirplane; - return position; + Insert(WarAirplane, 0); + return true; + } + public bool Insert(T WarAirplane, int position) + { + if (!(position >= 0 && position <= Count && _places.Count < _maxCount)) + { + return false; + } + + _places.Insert(position, WarAirplane); + return true; } public bool Remove(int position) { - if (position < Count && position >= 0) + if (position < 0 || position >= Count) { - _places[position] = null; - return true; + return false; } - return false; + + _places.RemoveAt(position); + return true; } - public T? Get(int position) + public T? this[int position] { - if (position < Count && position >= 0) + get { - return _places[position]; + if (position < 0 || position >= _maxCount) + { + return null; + } + return _places[position]; } - return null; + set + { + if (!(position >= 0 && position < Count && _places.Count < _maxCount)) + { + return; + } + _places.Insert(position, value); + return; + + + } } + public IEnumerable GetWarAirplane(int? maxWarAirplane = null) + { + for (int i = 0; i < _places.Count; ++i) + { + yield return _places[i]; + if (maxWarAirplane.HasValue && i == maxWarAirplane.Value) + { + yield break; + } + } + } + } } diff --git a/AirBomber/AirBomber/WarAirplaneGenericCollection.cs b/AirBomber/AirBomber/WarAirplaneGenericCollection.cs index 014eab4..56d034e 100644 --- a/AirBomber/AirBomber/WarAirplaneGenericCollection.cs +++ b/AirBomber/AirBomber/WarAirplaneGenericCollection.cs @@ -26,19 +26,19 @@ namespace AirBomber.Generics _pictureHeight = picHeight; _collection = new SetGeneric(width * height); } - public static int operator +(WarAirplaneGenericCollection collect, T? + public static bool operator +(WarAirplaneGenericCollection collect, T? obj) { if (obj == null) { - return -1; + return false; } - return collect._collection.Insert(obj); + return collect?._collection.Insert(obj) ?? false; } public static bool operator -(WarAirplaneGenericCollection collect, int pos) { - T? obj = collect._collection.Get(pos); + T? obj = collect._collection[pos]; if (obj != null) { collect._collection.Remove(pos); @@ -47,7 +47,7 @@ namespace AirBomber.Generics } public U? GetU(int pos) { - return (U?)_collection.Get(pos)?.GetMoveableObject; + return (U?)_collection[pos]?.GetMoveableObject; } public Bitmap ShowWarAirplane() { @@ -75,20 +75,18 @@ namespace AirBomber.Generics } private void DrawObjects(Graphics g) { - DrawingWarAirplane warAirplane; - - for (int i = 0; i < _collection.Count; i++) - { - warAirplane = _collection.Get(i); - if (warAirplane != null) - { - int numPlacesInRow = _pictureWidth / _placeSizeWidth; - warAirplane.SetPosition((numPlacesInRow - 1 - (i % numPlacesInRow)) * _placeSizeWidth, - i / numPlacesInRow * _placeSizeHeight); - warAirplane.DrawTransport(g); + int i = 0; + foreach (var warAirplane in _collection.GetWarAirplane()) { + if (warAirplane != null) + { + int numPlacesInRow = _pictureWidth / _placeSizeWidth; + warAirplane.SetPosition((numPlacesInRow - 1 - (i % numPlacesInRow)) * _placeSizeWidth, + i / numPlacesInRow * _placeSizeHeight); + warAirplane.DrawTransport(g); + } + i++; } - } } } } diff --git a/AirBomber/AirBomber/WarAirplaneGenericStorage.cs b/AirBomber/AirBomber/WarAirplaneGenericStorage.cs new file mode 100644 index 0000000..c3c21eb --- /dev/null +++ b/AirBomber/AirBomber/WarAirplaneGenericStorage.cs @@ -0,0 +1,55 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using AirBomber.DrawingObjects; +using AirBomber.MovementStrategy; + +namespace AirBomber.Generics +{ + internal class WarAirplaneGenericStorage + { + readonly Dictionary> _warAirplaneStorages; + public List Keys => _warAirplaneStorages.Keys.ToList(); + private readonly int _pictureWidth; + private readonly int _pictureHeight; + public WarAirplaneGenericStorage(int pictureWidth, int pictureHeight) + { + _warAirplaneStorages = new Dictionary>(); + _pictureWidth = pictureWidth; + _pictureHeight = pictureHeight; + } + public void AddSet(string name) + { + _warAirplaneStorages.Add(name, new WarAirplaneGenericCollection(_pictureWidth, _pictureHeight)); + } + public void DelSet(string name) + { + if (!_warAirplaneStorages.ContainsKey(name)) + { + return; + } + + _warAirplaneStorages.Remove(name); + } + + /// + /// Доступ к набору + /// + /// + /// + public WarAirplaneGenericCollection? this[string ind] + { + get + { + if (_warAirplaneStorages.ContainsKey(ind)) + { + return _warAirplaneStorages[ind]; + } + return null; + } + } + } +}