From 3394c1cf58889a83e64e49d265f01821e6324cb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=98=D0=B3=D0=BE=D1=80=D1=8C=20=D0=93=D0=BE=D1=80=D0=B4?= =?UTF-8?q?=D0=B5=D0=B5=D0=B2?= <89176335310x@gmail.com> Date: Thu, 14 Dec 2023 20:42:45 +0400 Subject: [PATCH] 70% --- Bulldoser/Bulldoser/Form2.Designer.cs | 73 +++++++++++++++- Bulldoser/Bulldoser/Form2.cs | 84 +++++++++++++++++-- .../Generic/BulldoserGenericCollection.cs | 26 +++--- Bulldoser/Bulldoser/Generic/SetGeneric.cs | 68 +++++++-------- .../Generic/TractorGenericStorage.cs | 47 +++++++++++ Bulldoser/Bulldoser/Status.cs | 2 +- 6 files changed, 239 insertions(+), 61 deletions(-) create mode 100644 Bulldoser/Bulldoser/Generic/TractorGenericStorage.cs diff --git a/Bulldoser/Bulldoser/Form2.Designer.cs b/Bulldoser/Bulldoser/Form2.Designer.cs index 397ad1f..66aeba0 100644 --- a/Bulldoser/Bulldoser/Form2.Designer.cs +++ b/Bulldoser/Bulldoser/Form2.Designer.cs @@ -30,12 +30,18 @@ { pictureBoxCollections = new PictureBox(); groupBoxTools = new GroupBox(); + groupBox1 = new GroupBox(); + listBoxStorage = new ListBox(); + DelNabor = new Button(); + AddNabor = new Button(); + InputNabor = new MaskedTextBox(); maskedTextBoxNumbers = new MaskedTextBox(); buttonUpdateCollection = new Button(); buttonRemoveTransport = new Button(); buttonAddTransport = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollections).BeginInit(); groupBoxTools.SuspendLayout(); + groupBox1.SuspendLayout(); SuspendLayout(); // // pictureBoxCollections @@ -48,6 +54,7 @@ // // groupBoxTools // + groupBoxTools.Controls.Add(groupBox1); groupBoxTools.Controls.Add(maskedTextBoxNumbers); groupBoxTools.Controls.Add(buttonUpdateCollection); groupBoxTools.Controls.Add(buttonRemoveTransport); @@ -59,9 +66,60 @@ groupBoxTools.TabStop = false; groupBoxTools.Text = "Инструменты"; // + // groupBox1 + // + groupBox1.Controls.Add(listBoxStorage); + groupBox1.Controls.Add(DelNabor); + groupBox1.Controls.Add(AddNabor); + groupBox1.Controls.Add(InputNabor); + groupBox1.Location = new Point(6, 22); + groupBox1.Name = "groupBox1"; + groupBox1.Size = new Size(188, 223); + groupBox1.TabIndex = 6; + groupBox1.TabStop = false; + groupBox1.Text = "Наборы"; + // + // listBoxStorage + // + listBoxStorage.FormattingEnabled = true; + listBoxStorage.ItemHeight = 15; + listBoxStorage.Location = new Point(6, 85); + listBoxStorage.Name = "listBoxStorage"; + listBoxStorage.Size = new Size(176, 94); + listBoxStorage.TabIndex = 10; + // + // DelNabor + // + DelNabor.Location = new Point(6, 187); + DelNabor.Name = "DelNabor"; + DelNabor.Size = new Size(176, 30); + DelNabor.TabIndex = 9; + DelNabor.Text = "Удалить набор"; + DelNabor.UseVisualStyleBackColor = true; + DelNabor.Click += ButtonRemoveObject_Click; + // + // AddNabor + // + AddNabor.Location = new Point(6, 49); + AddNabor.Name = "AddNabor"; + AddNabor.Size = new Size(176, 30); + AddNabor.TabIndex = 8; + AddNabor.Text = "Добавить набор"; + AddNabor.UseVisualStyleBackColor = true; + AddNabor.Click += ButtonAddObject_Click; + // + // InputNabor + // + InputNabor.Location = new Point(6, 21); + InputNabor.Margin = new Padding(3, 2, 3, 2); + InputNabor.Mask = "0"; + InputNabor.Name = "InputNabor"; + InputNabor.Size = new Size(176, 23); + InputNabor.TabIndex = 7; + // // maskedTextBoxNumbers // - maskedTextBoxNumbers.Location = new Point(6, 106); + maskedTextBoxNumbers.Location = new Point(6, 312); maskedTextBoxNumbers.Margin = new Padding(3, 2, 3, 2); maskedTextBoxNumbers.Mask = "0"; maskedTextBoxNumbers.Name = "maskedTextBoxNumbers"; @@ -70,7 +128,7 @@ // // buttonUpdateCollection // - buttonUpdateCollection.Location = new Point(6, 220); + buttonUpdateCollection.Location = new Point(6, 386); buttonUpdateCollection.Name = "buttonUpdateCollection"; buttonUpdateCollection.Size = new Size(188, 40); buttonUpdateCollection.TabIndex = 3; @@ -80,7 +138,7 @@ // // buttonRemoveTransport // - buttonRemoveTransport.Location = new Point(6, 152); + buttonRemoveTransport.Location = new Point(6, 340); buttonRemoveTransport.Name = "buttonRemoveTransport"; buttonRemoveTransport.Size = new Size(188, 40); buttonRemoveTransport.TabIndex = 2; @@ -90,7 +148,7 @@ // // buttonAddTransport // - buttonAddTransport.Location = new Point(6, 39); + buttonAddTransport.Location = new Point(6, 260); buttonAddTransport.Name = "buttonAddTransport"; buttonAddTransport.Size = new Size(188, 47); buttonAddTransport.TabIndex = 0; @@ -110,6 +168,8 @@ ((System.ComponentModel.ISupportInitialize)pictureBoxCollections).EndInit(); groupBoxTools.ResumeLayout(false); groupBoxTools.PerformLayout(); + groupBox1.ResumeLayout(false); + groupBox1.PerformLayout(); ResumeLayout(false); } @@ -121,5 +181,10 @@ private Button buttonRemoveTransport; private Button buttonAddTransport; private MaskedTextBox maskedTextBoxNumbers; + private GroupBox groupBox1; + private ListBox listBoxStorage; + private Button DelNabor; + private Button AddNabor; + private MaskedTextBox InputNabor; } } \ No newline at end of file diff --git a/Bulldoser/Bulldoser/Form2.cs b/Bulldoser/Bulldoser/Form2.cs index 7163f12..d4f932f 100644 --- a/Bulldoser/Bulldoser/Form2.cs +++ b/Bulldoser/Bulldoser/Form2.cs @@ -1,6 +1,7 @@ using Bulldoser.Drawings; using Bulldoser.Generic; using Bulldoser.MovementStrategy; +using Bulldozer.Generics; using System; using System.Collections.Generic; using System.ComponentModel; @@ -15,24 +16,75 @@ namespace Bulldoser { public partial class FormTractorCollection : Form { - private readonly TractorGenericCollection _Tractors; + private readonly TractorGenericStorage _storage; public FormTractorCollection() { InitializeComponent(); - _Tractors = new TractorGenericCollection(pictureBoxCollections.Width, - pictureBoxCollections.Height); + _storage = new TractorGenericStorage(pictureBoxCollections.Width, pictureBoxCollections.Height); } + 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(InputNabor.Text)) + { + MessageBox.Show("Не всё заполнено", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _storage.AddSet(InputNabor.Text); + ReloadObjects(); + } + private void listBoxStorage_SelectedIndexChanged(object sender, EventArgs e) + { + pictureBoxCollections.Image = _storage[listBoxStorage.SelectedItem?.ToString() ?? string.Empty]?.ShowTractors(); + } + private void ButtonRemoveObject_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 ButtonAddTractor_Click(object sender, EventArgs e) { - if (_Tractors == null) return; + if (listBoxStorage.SelectedIndex == -1) return; + + var obj = _storage[listBoxStorage.SelectedItem.ToString() ?? string.Empty]; + if (obj == null) + { + return; + } FormBulldoser form = new(); if (form.ShowDialog() == DialogResult.OK) { //проверяем, удалось ли нам загрузить объект - if (_Tractors + form.SelectedTractor != -1) + if (obj + form.SelectedTractor > -1) { MessageBox.Show("Объект добавлен"); - pictureBoxCollections.Image = _Tractors.ShowTractors(); + pictureBoxCollections.Image = obj.ShowTractors(); } else { @@ -40,17 +92,25 @@ namespace Bulldoser } } } + private void ButtonRemoveTractor_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(maskedTextBoxNumbers.Text); - if (_Tractors - pos != null) + if (obj - pos != null) { MessageBox.Show("Объект удален"); - pictureBoxCollections.Image = _Tractors.ShowTractors(); + pictureBoxCollections.Image = obj.ShowTractors(); } else { @@ -59,7 +119,13 @@ namespace Bulldoser } private void ButtonRefreshCollection_Click(object sender, EventArgs e) { - pictureBoxCollections.Image = _Tractors.ShowTractors(); + if (listBoxStorage.SelectedIndex == -1) return; + var obj = _storage[listBoxStorage.SelectedItem.ToString() ?? string.Empty]; + if (obj == null) + { + return; + } + pictureBoxCollections.Image = obj.ShowTractors(); } } } diff --git a/Bulldoser/Bulldoser/Generic/BulldoserGenericCollection.cs b/Bulldoser/Bulldoser/Generic/BulldoserGenericCollection.cs index c62bfa5..9e3a8bf 100644 --- a/Bulldoser/Bulldoser/Generic/BulldoserGenericCollection.cs +++ b/Bulldoser/Bulldoser/Generic/BulldoserGenericCollection.cs @@ -40,7 +40,7 @@ namespace Bulldoser.Generic public static T? operator -(TractorGenericCollection collect, int pos) { - T obj = collect._collection.Get(pos); + T obj = collect._collection[pos]; if (obj != null) { return collect._collection.Remove(pos); @@ -50,7 +50,7 @@ namespace Bulldoser.Generic // получение объекта imoveableObj public U? GetU(int pos) { - return (U?)_collection.Get(pos)?.GetMoveableObject; + return (U?)_collection[pos]?.GetMoveableObject; } /// Вывод всего набора объектов public Bitmap ShowTractors() @@ -76,19 +76,19 @@ namespace Bulldoser.Generic } private void DrawObjects(Graphics g) { - int HeightObjCount = _pictureHeight / _placeSizeHeight; - int WidthObjCount = _pictureWidth / _placeSizeWidth; + int width = _pictureWidth / _placeSizeWidth; + int height = _pictureHeight / _placeSizeHeight; for (int i = 0; i < _collection.Count; i++) { - T t = _collection.Get(i); - if (t != null) - { - { - t.SetPosition((_collection.Count - i - 1) % (_pictureWidth / _placeSizeWidth) * _placeSizeWidth, - (_collection.Count - i - 1) / (_pictureWidth / _placeSizeWidth) * _placeSizeHeight); - t.DrawTransport(g); - } - } + // Получение объекта + var obj = _collection[i]; + // Установка позиции + obj?.SetPosition( + (int)((width - 1) * _placeSizeWidth - (i % width * _placeSizeWidth)), + (int)((height - 1) * _placeSizeHeight - (i / width * _placeSizeHeight)) + ); + // Прорисовка объекта + obj?.DrawTransport(g); } } } diff --git a/Bulldoser/Bulldoser/Generic/SetGeneric.cs b/Bulldoser/Bulldoser/Generic/SetGeneric.cs index 22ea4f6..560b2b2 100644 --- a/Bulldoser/Bulldoser/Generic/SetGeneric.cs +++ b/Bulldoser/Bulldoser/Generic/SetGeneric.cs @@ -6,13 +6,15 @@ using System.Threading.Tasks; namespace Bulldoser.Generic { - public class SetGeneric where T : class + 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]; + _places = new List(count); + _maxCount = count; } public int Insert(T tract) { @@ -20,32 +22,9 @@ namespace Bulldoser.Generic } public int Insert(T tract, int position) { - int NoEmpty = 0, temp = 0; - for (int i = position; i < Count; i++) - { - if (_places[i] != null) NoEmpty++; - } - if (NoEmpty == Count - position - 1) return -1; - - if (position < Count && position >= 0) - { - for (int j = position; j < Count; j++) - { - if (_places[j] == null) - { - temp = j; - break; - } - } - // shift right - for (int i = temp; i > position; i--) - { - _places[i] = _places[i - 1]; - } - _places[position] = tract; - return position; - } - return -1; + if (position < 0 || position >= _maxCount) return -1; + _places.Insert(position, tract); + return position; } public T? Remove(int position) @@ -53,15 +32,36 @@ namespace Bulldoser.Generic if (position >= Count || position < 0) return null; - T tmp = _places[position]; + T? tmp = _places[position]; _places[position] = null; return tmp; } //Получение объекта из набора по позиции - 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 || Count == _maxCount) return; + _places.Insert(position, value); + + + } + } + public IEnumerable GetTractors(int? maxTracts = null) + { + for (int i = 0; i < _places.Count; ++i) + { + yield return _places[i]; + if (maxTracts.HasValue && i == maxTracts.Value) + { + yield break; + } + } } } } diff --git a/Bulldoser/Bulldoser/Generic/TractorGenericStorage.cs b/Bulldoser/Bulldoser/Generic/TractorGenericStorage.cs new file mode 100644 index 0000000..396bd72 --- /dev/null +++ b/Bulldoser/Bulldoser/Generic/TractorGenericStorage.cs @@ -0,0 +1,47 @@ + +using Bulldoser.Drawings; +using Bulldoser.Generic; +using Bulldoser.MovementStrategy; + +namespace Bulldozer.Generics +{ + internal class TractorGenericStorage + { + readonly Dictionary> _TractorsStorage; + public List Keys => _TractorsStorage.Keys.ToList(); + private readonly int _pictureWidth; + private readonly int _pictureHeight; + public TractorGenericStorage(int pictureWidth, int pictureHeight) + { + _TractorsStorage = new Dictionary>(); + _pictureWidth = pictureWidth; + _pictureHeight = pictureHeight; + } + public void AddSet(string name) + { + if (!_TractorsStorage.ContainsKey(name)) + { + _TractorsStorage.Add(name, new TractorGenericCollection(_pictureWidth, _pictureHeight)); + } + } + public void DelSet(string name) + { + if (_TractorsStorage.ContainsKey(name)) + { + _TractorsStorage.Remove(name); + } + } + public TractorGenericCollection? + this[string ind] + { + get + { + if (_TractorsStorage.ContainsKey(ind)) + { + return _TractorsStorage[ind]; + } + return null; + } + } + } +} diff --git a/Bulldoser/Bulldoser/Status.cs b/Bulldoser/Bulldoser/Status.cs index a846e28..bbe086d 100644 --- a/Bulldoser/Bulldoser/Status.cs +++ b/Bulldoser/Bulldoser/Status.cs @@ -4,7 +4,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace Bulldoser +namespace Bulldoser.MovementStrategy { /// /// Статус выполнения операции перемещения