diff --git a/Hydroplane/FormHydroplaneCollection.Designer.cs b/Hydroplane/FormHydroplaneCollection.Designer.cs index 75355af..71512e7 100644 --- a/Hydroplane/FormHydroplaneCollection.Designer.cs +++ b/Hydroplane/FormHydroplaneCollection.Designer.cs @@ -29,33 +29,104 @@ private void InitializeComponent() { panel1 = new Panel(); + panel2 = new Panel(); + DeleteCollectButton = new Button(); + CollectionListBox = new ListBox(); + AddCollectButton = new Button(); + SetTextBox = new TextBox(); + label2 = new Label(); UpdateButton = new Button(); DeleteButton = new Button(); AddButton = new Button(); - InputNum = new TextBox(); + PlaneTextBox = new TextBox(); label1 = new Label(); DrawPlane = new PictureBox(); panel1.SuspendLayout(); + panel2.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)DrawPlane).BeginInit(); SuspendLayout(); // // panel1 // + panel1.Controls.Add(panel2); panel1.Controls.Add(UpdateButton); panel1.Controls.Add(DeleteButton); panel1.Controls.Add(AddButton); - panel1.Controls.Add(InputNum); + panel1.Controls.Add(PlaneTextBox); panel1.Controls.Add(label1); panel1.Dock = DockStyle.Right; - panel1.Location = new Point(577, 0); + panel1.Location = new Point(646, 0); panel1.Margin = new Padding(3, 2, 3, 2); panel1.Name = "panel1"; - panel1.Size = new Size(219, 338); + panel1.Size = new Size(219, 386); panel1.TabIndex = 0; // + // panel2 + // + panel2.Controls.Add(DeleteCollectButton); + panel2.Controls.Add(CollectionListBox); + panel2.Controls.Add(AddCollectButton); + panel2.Controls.Add(SetTextBox); + panel2.Controls.Add(label2); + panel2.Location = new Point(14, 28); + panel2.Margin = new Padding(3, 2, 3, 2); + panel2.Name = "panel2"; + panel2.Size = new Size(187, 165); + panel2.TabIndex = 5; + // + // DeleteCollectButton + // + DeleteCollectButton.Location = new Point(3, 136); + DeleteCollectButton.Margin = new Padding(3, 2, 3, 2); + DeleteCollectButton.Name = "DeleteCollectButton"; + DeleteCollectButton.Size = new Size(182, 22); + DeleteCollectButton.TabIndex = 4; + DeleteCollectButton.Text = "Удалить набор"; + DeleteCollectButton.UseVisualStyleBackColor = true; + DeleteCollectButton.Click += ButtonDelObject_Click; + // + // CollectionListBox + // + CollectionListBox.FormattingEnabled = true; + CollectionListBox.ItemHeight = 15; + CollectionListBox.Location = new Point(3, 68); + CollectionListBox.Margin = new Padding(3, 2, 3, 2); + CollectionListBox.Name = "CollectionListBox"; + CollectionListBox.Size = new Size(182, 64); + CollectionListBox.TabIndex = 3; + CollectionListBox.SelectedIndexChanged += ListBoxObjects_SelectedIndexChanged; + // + // AddCollectButton + // + AddCollectButton.Location = new Point(3, 42); + AddCollectButton.Margin = new Padding(3, 2, 3, 2); + AddCollectButton.Name = "AddCollectButton"; + AddCollectButton.Size = new Size(182, 22); + AddCollectButton.TabIndex = 2; + AddCollectButton.Text = "Добавить набор"; + AddCollectButton.UseVisualStyleBackColor = true; + AddCollectButton.Click += ButtonAddObject_Click; + // + // SetTextBox + // + SetTextBox.Location = new Point(2, 17); + SetTextBox.Margin = new Padding(3, 2, 3, 2); + SetTextBox.Name = "SetTextBox"; + SetTextBox.Size = new Size(183, 23); + SetTextBox.TabIndex = 1; + // + // label2 + // + label2.AutoSize = true; + label2.Location = new Point(3, 0); + label2.Name = "label2"; + label2.Size = new Size(52, 15); + label2.TabIndex = 0; + label2.Text = "Наборы"; + // // UpdateButton // - UpdateButton.Location = new Point(9, 181); + UpdateButton.Location = new Point(9, 301); UpdateButton.Margin = new Padding(3, 2, 3, 2); UpdateButton.Name = "UpdateButton"; UpdateButton.Size = new Size(200, 28); @@ -66,33 +137,33 @@ // // DeleteButton // - DeleteButton.Location = new Point(9, 132); + DeleteButton.Location = new Point(9, 268); DeleteButton.Margin = new Padding(3, 2, 3, 2); DeleteButton.Name = "DeleteButton"; DeleteButton.Size = new Size(200, 28); DeleteButton.TabIndex = 3; DeleteButton.Text = "Удалить самолёт"; DeleteButton.UseVisualStyleBackColor = true; - DeleteButton.Click += ButtonRemoveCar_Click; + DeleteButton.Click += ButtonRemovePlane_Click; // // AddButton // - AddButton.Location = new Point(9, 40); + AddButton.Location = new Point(9, 212); AddButton.Margin = new Padding(3, 2, 3, 2); AddButton.Name = "AddButton"; AddButton.Size = new Size(200, 28); AddButton.TabIndex = 2; AddButton.Text = "Добавить самолёт"; AddButton.UseVisualStyleBackColor = true; - AddButton.Click += ButtonAddTank_Click; + AddButton.Click += ButtonAddPlane_Click; // - // InputNum + // PlaneTextBox // - InputNum.Location = new Point(9, 100); - InputNum.Margin = new Padding(3, 2, 3, 2); - InputNum.Name = "InputNum"; - InputNum.Size = new Size(200, 23); - InputNum.TabIndex = 1; + PlaneTextBox.Location = new Point(9, 244); + PlaneTextBox.Margin = new Padding(3, 2, 3, 2); + PlaneTextBox.Name = "PlaneTextBox"; + PlaneTextBox.Size = new Size(200, 23); + PlaneTextBox.TabIndex = 1; // // label1 // @@ -109,7 +180,7 @@ DrawPlane.Location = new Point(0, 0); DrawPlane.Margin = new Padding(3, 2, 3, 2); DrawPlane.Name = "DrawPlane"; - DrawPlane.Size = new Size(577, 338); + DrawPlane.Size = new Size(646, 386); DrawPlane.TabIndex = 1; DrawPlane.TabStop = false; // @@ -117,14 +188,16 @@ // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(796, 338); + ClientSize = new Size(865, 386); Controls.Add(DrawPlane); Controls.Add(panel1); Margin = new Padding(3, 2, 3, 2); Name = "FormHydroplaneCollection"; - Text = "Form1"; + Text = "Гаражи гидропланов"; panel1.ResumeLayout(false); panel1.PerformLayout(); + panel2.ResumeLayout(false); + panel2.PerformLayout(); ((System.ComponentModel.ISupportInitialize)DrawPlane).EndInit(); ResumeLayout(false); } @@ -135,8 +208,14 @@ private Button UpdateButton; private Button DeleteButton; private Button AddButton; - private TextBox InputNum; + private TextBox PlaneTextBox; private Label label1; private PictureBox DrawPlane; + private Panel panel2; + private Button DeleteCollectButton; + private ListBox CollectionListBox; + private Button AddCollectButton; + private TextBox SetTextBox; + private Label label2; } } \ No newline at end of file diff --git a/Hydroplane/FormHydroplaneCollection.cs b/Hydroplane/FormHydroplaneCollection.cs index 5f94f71..5c51f8e 100644 --- a/Hydroplane/FormHydroplaneCollection.cs +++ b/Hydroplane/FormHydroplaneCollection.cs @@ -15,23 +15,107 @@ namespace Hydroplane { public partial class FormHydroplaneCollection : Form { - private readonly PlanesGenericCollection _planes; - + private readonly PlanesGenericStorage _storage; + /// + /// Конструктор + /// public FormHydroplaneCollection() { InitializeComponent(); - _planes = new PlanesGenericCollection(DrawPlane.Width, DrawPlane.Height); + _storage = new PlanesGenericStorage(DrawPlane.Width, DrawPlane.Height); } - - private void ButtonAddTank_Click(object sender, EventArgs e) + /// + /// Заполнение listBoxObjects + /// + private void ReloadObjects() { - FormHydroplane form = new FormHydroplane(); + int index = CollectionListBox.SelectedIndex; + CollectionListBox.Items.Clear(); + for (int i = 0; i < _storage.Keys.Count; i++) + { + CollectionListBox.Items.Add(_storage.Keys[i]); + } + if (CollectionListBox.Items.Count > 0 && (index == -1 || index + >= CollectionListBox.Items.Count)) + { + CollectionListBox.SelectedIndex = 0; + } + else if (CollectionListBox.Items.Count > 0 && index > -1 && + index < CollectionListBox.Items.Count) + { + CollectionListBox.SelectedIndex = index; + } + + } + /// + /// Добавление набора в коллекцию + /// + /// + /// + private void ButtonAddObject_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(SetTextBox.Text)) + { + MessageBox.Show("Не все данные заполнены", "Ошибка", + MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _storage.AddSet(SetTextBox.Text); + ReloadObjects(); + } + /// + /// Выбор набора + /// + /// + /// + private void ListBoxObjects_SelectedIndexChanged(object sender, + EventArgs e) + { + DrawPlane.Image = + _storage[CollectionListBox.SelectedItem?.ToString() ?? string.Empty]?.ShowPlanes(); + } + /// + /// Удаление набора + /// + /// + /// + private void ButtonDelObject_Click(object sender, EventArgs e) + { + if (CollectionListBox.SelectedIndex == -1) + { + return; + } + if (MessageBox.Show($"Удалить объект {CollectionListBox.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo, + MessageBoxIcon.Question) == DialogResult.Yes) + { + _storage.DelSet(CollectionListBox.SelectedItem.ToString() + ?? string.Empty); + ReloadObjects(); + } + } + /// + /// Добавление объекта в набор + /// + /// + /// + private void ButtonAddPlane_Click(object sender, EventArgs e) + { + if (CollectionListBox.SelectedIndex == -1) + { + return; + } + var obj = _storage[CollectionListBox.SelectedItem.ToString() ?? string.Empty]; + if (obj == null) + { + return; + } + FormHydroplane form = new(); if (form.ShowDialog() == DialogResult.OK) { - if (_planes + form.SelectedPlane != -1) + if (obj + form.SelectedPlane) { MessageBox.Show("Объект добавлен"); - DrawPlane.Image = _planes.ShowPlanes(); + DrawPlane.Image = obj.ShowPlanes(); } else { @@ -39,34 +123,60 @@ namespace Hydroplane } } } - - private void ButtonRemoveCar_Click(object sender, EventArgs e) + /// + /// Удаление объекта из набора + /// + /// + /// + private void ButtonRemovePlane_Click(object sender, EventArgs e) { + if (CollectionListBox.SelectedIndex == -1) + { + return; + } + var obj = _storage[CollectionListBox.SelectedItem.ToString() ?? + string.Empty]; + if (obj == null) + { + return; + } if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) { return; } - int pos = -1; - try - { - pos = Convert.ToInt32(InputNum.Text); - } - catch (Exception ex) { } - if (_planes - pos) + int pos = 0; + if (PlaneTextBox != null) + pos = Convert.ToInt32(PlaneTextBox.Text); + if (obj - pos != null) { MessageBox.Show("Объект удален"); - DrawPlane.Image = _planes.ShowPlanes(); + DrawPlane.Image = obj.ShowPlanes(); } else { MessageBox.Show("Не удалось удалить объект"); } } - - private void ButtonRefreshCollection_Click(object sender, EventArgs e) + /// + /// Обновление рисунка по набору + /// + /// + /// + private void ButtonRefreshCollection_Click(object sender, EventArgs + e) { - DrawPlane.Image = _planes.ShowPlanes(); + if (CollectionListBox.SelectedIndex == -1) + { + return; + } + var obj = _storage[CollectionListBox.SelectedItem.ToString() ?? + string.Empty]; + if (obj == null) + { + return; + } + DrawPlane.Image = obj.ShowPlanes(); } } } diff --git a/Hydroplane/PlanesGenericCollection.cs b/Hydroplane/PlanesGenericCollection.cs index 7077420..f84dbf6 100644 --- a/Hydroplane/PlanesGenericCollection.cs +++ b/Hydroplane/PlanesGenericCollection.cs @@ -27,30 +27,29 @@ namespace Hydroplane.Generics _collection = new SetGeneric(width * height); } - public static int? operator +(PlanesGenericCollection collect, T? - obj) + public static bool operator +(PlanesGenericCollection collect, T obj) { - if (obj == null) - { - return -1; - } - return collect?._collection.Insert(obj); - } - - public static bool operator -(PlanesGenericCollection collect, int - pos) - { - T? obj = collect._collection.Get(pos); if (obj == null) { return false; } - return collect._collection.Remove(pos); + return (bool)collect._collection.Insert(obj); + } + + public static T? operator -(PlanesGenericCollection collect, int + pos) + { + T? obj = collect._collection[pos]; + if (obj != null) + { + collect._collection.Remove(pos); + } + return obj; } public U? GetU(int pos) { - return (U?)_collection.Get(pos)?.GetMoveableObject; + return (U?)_collection[pos]?.GetMoveableObject; } public Bitmap ShowPlanes() { @@ -80,7 +79,7 @@ namespace Hydroplane.Generics { for (int i = 0; i < _collection.Count; i++) { - T? t = _collection.Get(i); + T? t = _collection[i]; if (t != null) { t.SetPosition((i % (_pictureWidth / _placeSizeWidth)) * _placeSizeWidth, (i / (_pictureWidth / _placeSizeWidth)) * _placeSizeHeight); diff --git a/Hydroplane/PlanesGenericStorage.cs b/Hydroplane/PlanesGenericStorage.cs new file mode 100644 index 0000000..a569a1f --- /dev/null +++ b/Hydroplane/PlanesGenericStorage.cs @@ -0,0 +1,45 @@ +using Hydroplane.DrawningObjects; +using Hydroplane.MovementStrategy; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Hydroplane.Generics +{ + internal class PlanesGenericStorage + { + readonly Dictionary> _planeStorages; + public List Keys => _planeStorages.Keys.ToList(); + private readonly int _pictureWidth; + private readonly int _pictureHeight; + public PlanesGenericStorage(int pictureWidth, int pictureHeight) + { + _planeStorages = new Dictionary>(); + _pictureWidth = pictureWidth; + _pictureHeight = pictureHeight; + } + public void AddSet(string name) + { + if (_planeStorages.ContainsKey(name)) return; + _planeStorages[name] = new PlanesGenericCollection(_pictureWidth, _pictureHeight); + } + + public void DelSet(string name) + { + if (!_planeStorages.ContainsKey(name)) return; + _planeStorages.Remove(name); + } + + public PlanesGenericCollection? + this[string ind] + { + get + { + if (_planeStorages.ContainsKey(ind)) return _planeStorages[ind]; + return null; + } + } + } +} \ No newline at end of file diff --git a/Hydroplane/SetGeneric.cs b/Hydroplane/SetGeneric.cs index d5e3b98..3c90c01 100644 --- a/Hydroplane/SetGeneric.cs +++ b/Hydroplane/SetGeneric.cs @@ -8,58 +8,68 @@ namespace Hydroplane.Generics { internal class SetGeneric where T : class { - private readonly T?[] _places; + private readonly List _places; + public int Count => _places.Count; - public int Count => _places.Length; + private readonly int _maxCount; public SetGeneric(int count) { - _places = new T?[count]; + _maxCount = count; + _places = new List(_maxCount); } - public int Insert(T plane) + public bool Insert(T plane) { - if (_places[Count - 1] != null) - return -1; return Insert(plane, 0); } - - public int Insert(T plane, int position) + public bool Insert(T plane, int position) { - if (position < 0 || position >= Count) - return -1; - if (_places[position] != null) - { - int indexEnd = position + 1; - while (_places[indexEnd] != null) - { - indexEnd++; - } - for (int i = indexEnd + 1; i > position; i--) - { - _places[i] = _places[i - 1]; - } - - } - _places[position] = plane; - return position; - } - - public bool Remove(int position) - { - if (position < 0 || position >= _places.Count()) + if (position < 0 || position >= _maxCount) return false; - _places[position] = null; - + if (Count >= _maxCount) + return false; + _places.Insert(0, plane); return true; } - - public T? Get(int position) + + public bool Remove(int position) { - if (position < 0 || position >= _places.Count()) - return null; - return _places[position]; + if (position < 0 || position > _maxCount) + return false; + if (position >= Count) + return false; + _places.RemoveAt(position); + return true; + } + public T? this[int position] + { + get + { + if (position < 0 || position > _maxCount) + return null; + return _places[position]; + } + set + { + if (position < 0 || position > _maxCount) + return; + _places[position] = value; + } + } + + + public IEnumerable GetPlanes(int? maxPlanes = null) + { + for (int i = 0; i < _places.Count; ++i) + { + yield return _places[i]; + if (maxPlanes.HasValue && i == maxPlanes.Value) + { + yield break; + } + } } } }