diff --git a/ProjectExcavator/ProjectExcavator/ExcavatorGenericCollection.cs b/ProjectExcavator/ProjectExcavator/ExcavatorGenericCollection.cs index 12fa8de..a904509 100644 --- a/ProjectExcavator/ProjectExcavator/ExcavatorGenericCollection.cs +++ b/ProjectExcavator/ProjectExcavator/ExcavatorGenericCollection.cs @@ -56,30 +56,31 @@ namespace ProjectExcavator.Generics /// /// /// - public static int operator +(ExcavatorGenericCollection collect, T? obj) + public static bool operator +(ExcavatorGenericCollection collect, T? obj) { if (obj == null) { - return -1; + return false; } - return collect?._collection.Insert(obj) ?? -1; + return collect?._collection.Insert(obj) ?? false; } + /// /// Перегрузка оператора вычитания /// /// /// /// - public static bool operator -(ExcavatorGenericCollection collect, int pos) + public static T? operator -(ExcavatorGenericCollection collect, int pos) { - T? obj = collect._collection.Get(pos); + T? obj = collect._collection[pos]; if (obj != null) { collect._collection.Remove(pos); - return true; } - return false; + return obj; } + /// /// Получение объекта IMoveableObject /// @@ -87,7 +88,8 @@ namespace ProjectExcavator.Generics /// public U? GetU(int pos) { - return (U?)_collection.Get(pos)?.GetMoveableObject; + return (U?)_collection[pos]?.GetMoveableObject; + } /// /// Вывод всего набора объектов @@ -110,7 +112,8 @@ namespace ProjectExcavator.Generics Pen pen = new(Color.Black, 3); for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++) { - for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j) + for (int j = 0; j < _pictureHeight / _placeSizeHeight + + 1; ++j) {//линия разметки места g.DrawLine(pen, i * _placeSizeWidth, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth, j * @@ -130,7 +133,7 @@ namespace ProjectExcavator.Generics int height = _pictureHeight / _placeSizeHeight; for (int i = 0; i < _collection.Count; i++) { - DrawingExcavator? excavator = _collection.Get(i); + T? excavator = _collection[i]; if (excavator == null) continue; int r = i / width; diff --git a/ProjectExcavator/ProjectExcavator/ExcavatorGenericStorage.cs b/ProjectExcavator/ProjectExcavator/ExcavatorGenericStorage.cs new file mode 100644 index 0000000..996d492 --- /dev/null +++ b/ProjectExcavator/ProjectExcavator/ExcavatorGenericStorage.cs @@ -0,0 +1,87 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ProjectExcavator.DrawingObjects; +using ProjectExcavator.MovementStrategy; + +namespace ProjectExcavator.Generics +{ + /// + /// Класс для хранения коллекции + /// + internal class ExcavatorGenericStorage + { + /// + /// Словарь (хранилище) + /// + readonly Dictionary> _excavatorStorages; + /// + /// Возвращение списка названий наборов + /// + public List Keys => _excavatorStorages.Keys.ToList(); + /// + /// Ширина окна отрисовки + /// + private readonly int _pictureWidth; + /// + /// Высота окна отрисовки + /// + private readonly int _pictureHeight; + /// + /// Конструктор + /// + /// + /// + public ExcavatorGenericStorage(int pictureWidth, int pictureHeight) + { + _excavatorStorages = new Dictionary>(); + _pictureWidth = pictureWidth; + _pictureHeight = pictureHeight; + } + /// + /// Добавление набора + /// + /// Название набора + public void AddSet(string name) + { + if (!_excavatorStorages.ContainsKey(name)) + { + ExcavatorGenericCollection newSet = new ExcavatorGenericCollection(_pictureWidth, _pictureHeight); + _excavatorStorages.Add(name, newSet); + } + } + /// + /// Удаление набора + /// + /// Название набора + public void DelSet(string name) + { + { + if (_excavatorStorages.ContainsKey(name)) + { + _excavatorStorages.Remove(name); + } + } + } + /// + /// Доступ к набору + /// + /// + /// + public ExcavatorGenericCollection? this[string ind] + { + get + { + if (_excavatorStorages.ContainsKey(ind)) + { + return _excavatorStorages[ind]; + } + return null; + } + } + } +} diff --git a/ProjectExcavator/ProjectExcavator/FormExcavatorCollection.Designer.cs b/ProjectExcavator/ProjectExcavator/FormExcavatorCollection.Designer.cs index 4c94b90..a038e1d 100644 --- a/ProjectExcavator/ProjectExcavator/FormExcavatorCollection.Designer.cs +++ b/ProjectExcavator/ProjectExcavator/FormExcavatorCollection.Designer.cs @@ -34,8 +34,14 @@ this.buttonRemoveEx = new System.Windows.Forms.Button(); this.buttonRefreshCollection = new System.Windows.Forms.Button(); this.groupBox1 = new System.Windows.Forms.GroupBox(); + this.groupBox2 = new System.Windows.Forms.GroupBox(); + this.textBoxStorageName = new System.Windows.Forms.TextBox(); + this.buttonDelObject = new System.Windows.Forms.Button(); + this.listBoxStorages = new System.Windows.Forms.ListBox(); + this.buttonAddObject = new System.Windows.Forms.Button(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).BeginInit(); this.groupBox1.SuspendLayout(); + this.groupBox2.SuspendLayout(); this.SuspendLayout(); // // pictureBoxCollection @@ -49,14 +55,14 @@ // // maskedTextBoxNumber // - this.maskedTextBoxNumber.Location = new System.Drawing.Point(26, 125); + this.maskedTextBoxNumber.Location = new System.Drawing.Point(35, 339); this.maskedTextBoxNumber.Name = "maskedTextBoxNumber"; this.maskedTextBoxNumber.Size = new System.Drawing.Size(100, 23); this.maskedTextBoxNumber.TabIndex = 1; // // buttonAddEx // - this.buttonAddEx.Location = new System.Drawing.Point(6, 57); + this.buttonAddEx.Location = new System.Drawing.Point(13, 310); this.buttonAddEx.Name = "buttonAddEx"; this.buttonAddEx.Size = new System.Drawing.Size(150, 23); this.buttonAddEx.TabIndex = 2; @@ -66,7 +72,7 @@ // // buttonRemoveEx // - this.buttonRemoveEx.Location = new System.Drawing.Point(6, 166); + this.buttonRemoveEx.Location = new System.Drawing.Point(13, 368); this.buttonRemoveEx.Name = "buttonRemoveEx"; this.buttonRemoveEx.Size = new System.Drawing.Size(150, 23); this.buttonRemoveEx.TabIndex = 3; @@ -76,7 +82,7 @@ // // buttonRefreshCollection // - this.buttonRefreshCollection.Location = new System.Drawing.Point(6, 230); + this.buttonRefreshCollection.Location = new System.Drawing.Point(13, 432); this.buttonRefreshCollection.Name = "buttonRefreshCollection"; this.buttonRefreshCollection.Size = new System.Drawing.Size(148, 23); this.buttonRefreshCollection.TabIndex = 4; @@ -86,17 +92,68 @@ // // groupBox1 // + this.groupBox1.Controls.Add(this.groupBox2); this.groupBox1.Controls.Add(this.buttonAddEx); this.groupBox1.Controls.Add(this.buttonRefreshCollection); this.groupBox1.Controls.Add(this.maskedTextBoxNumber); this.groupBox1.Controls.Add(this.buttonRemoveEx); - this.groupBox1.Location = new System.Drawing.Point(749, 0); + this.groupBox1.Location = new System.Drawing.Point(741, 0); this.groupBox1.Name = "groupBox1"; - this.groupBox1.Size = new System.Drawing.Size(160, 461); + this.groupBox1.Size = new System.Drawing.Size(168, 461); this.groupBox1.TabIndex = 5; this.groupBox1.TabStop = false; this.groupBox1.Text = "Инструменты"; // + // groupBox2 + // + this.groupBox2.Controls.Add(this.textBoxStorageName); + this.groupBox2.Controls.Add(this.buttonDelObject); + this.groupBox2.Controls.Add(this.listBoxStorages); + this.groupBox2.Controls.Add(this.buttonAddObject); + this.groupBox2.Location = new System.Drawing.Point(13, 28); + this.groupBox2.Name = "groupBox2"; + this.groupBox2.Size = new System.Drawing.Size(143, 214); + this.groupBox2.TabIndex = 5; + this.groupBox2.TabStop = false; + this.groupBox2.Text = "Наборы"; + // + // textBoxStorageName + // + this.textBoxStorageName.Location = new System.Drawing.Point(15, 30); + this.textBoxStorageName.Name = "textBoxStorageName"; + this.textBoxStorageName.Size = new System.Drawing.Size(122, 23); + this.textBoxStorageName.TabIndex = 6; + // + // buttonDelObject + // + this.buttonDelObject.Location = new System.Drawing.Point(15, 185); + this.buttonDelObject.Name = "buttonDelObject"; + this.buttonDelObject.Size = new System.Drawing.Size(122, 23); + this.buttonDelObject.TabIndex = 8; + this.buttonDelObject.Text = "Удалить набор"; + this.buttonDelObject.UseVisualStyleBackColor = true; + this.buttonDelObject.Click += new System.EventHandler(this.ButtonDelObject_Click); + // + // listBoxStorages + // + this.listBoxStorages.FormattingEnabled = true; + this.listBoxStorages.ItemHeight = 15; + this.listBoxStorages.Location = new System.Drawing.Point(15, 88); + this.listBoxStorages.Name = "listBoxStorages"; + this.listBoxStorages.Size = new System.Drawing.Size(122, 79); + this.listBoxStorages.TabIndex = 6; + this.listBoxStorages.Click += new System.EventHandler(this.ListBoxObjects_SelectedIndexChanged); + // + // buttonAddObject + // + this.buttonAddObject.Location = new System.Drawing.Point(15, 59); + this.buttonAddObject.Name = "buttonAddObject"; + this.buttonAddObject.Size = new System.Drawing.Size(122, 23); + this.buttonAddObject.TabIndex = 7; + this.buttonAddObject.Text = "Добавить набор"; + this.buttonAddObject.UseVisualStyleBackColor = true; + this.buttonAddObject.Click += new System.EventHandler(this.ButtonAddObject_Click); + // // FormExcavatorCollection // this.ClientSize = new System.Drawing.Size(909, 461); @@ -106,6 +163,8 @@ ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).EndInit(); this.groupBox1.ResumeLayout(false); this.groupBox1.PerformLayout(); + this.groupBox2.ResumeLayout(false); + this.groupBox2.PerformLayout(); this.ResumeLayout(false); } @@ -118,5 +177,10 @@ private Button buttonRemoveEx; private Button buttonRefreshCollection; private GroupBox groupBox1; + private GroupBox groupBox2; + private TextBox textBoxStorageName; + private Button buttonDelObject; + private ListBox listBoxStorages; + private Button buttonAddObject; } } \ No newline at end of file diff --git a/ProjectExcavator/ProjectExcavator/FormExcavatorCollection.cs b/ProjectExcavator/ProjectExcavator/FormExcavatorCollection.cs index 9475e75..faae433 100644 --- a/ProjectExcavator/ProjectExcavator/FormExcavatorCollection.cs +++ b/ProjectExcavator/ProjectExcavator/FormExcavatorCollection.cs @@ -5,23 +5,95 @@ using ProjectExcavator.MovementStrategy; namespace ProjectExcavator { /// - /// Форма для работы с набором объектов класса DrawingExcavator + /// Форма для работы с набором объектов класса DrawningExcavator /// public partial class FormExcavatorCollection : Form { /// /// Набор объектов /// - private readonly ExcavatorGenericCollection _excavator; + private readonly ExcavatorGenericStorage _storage; /// /// Конструктор /// public FormExcavatorCollection() { InitializeComponent(); - _excavator = new ExcavatorGenericCollection(pictureBoxCollection.Width, pictureBoxCollection.Height); + _storage = new ExcavatorGenericStorage(pictureBoxCollection.Width, pictureBoxCollection.Height); + } + /// + /// Заполнение listBoxObjects + /// + 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 ListBoxObjects_SelectedIndexChanged(object sender, + EventArgs e) + { + pictureBoxCollection.Image = + _storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowExcavator(); + } + /// + /// Удаление набора + /// + /// + /// + 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(); + var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty]; + if (obj == null) + { + return; + } + pictureBoxCollection.Image = obj.ShowExcavator(); + } } /// /// Добавление объекта в набор @@ -30,14 +102,23 @@ namespace ProjectExcavator /// private void ButtonAddEx_Click(object sender, EventArgs e) { + if (listBoxStorages.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? + string.Empty]; + if (obj == null) + { + return; + } FormExcavator form = new(); if (form.ShowDialog() == DialogResult.OK) { - int result = _excavator + form.SelectedExcavator; - if (result != -2) + if (obj + form.SelectedExcavator) { MessageBox.Show("Объект добавлен"); - pictureBoxCollection.Image = _excavator.ShowExcavator(); + pictureBoxCollection.Image = obj.ShowExcavator(); } else { @@ -52,16 +133,26 @@ namespace ProjectExcavator /// private void ButtonRemoveEx_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) { return; } int pos = Convert.ToInt32(maskedTextBoxNumber.Text); - if (_excavator - pos != null) + if (obj - pos != null) { MessageBox.Show("Объект удален"); - pictureBoxCollection.Image = _excavator.ShowExcavator(); + pictureBoxCollection.Image = obj.ShowExcavator(); } else { @@ -75,7 +166,17 @@ namespace ProjectExcavator /// private void ButtonRefreshCollection_Click(object sender, EventArgs e) { - pictureBoxCollection.Image = _excavator.ShowExcavator(); + if (listBoxStorages.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? + string.Empty]; + if (obj == null) + { + return; + } + pictureBoxCollection.Image = obj.ShowExcavator(); } } } diff --git a/ProjectExcavator/ProjectExcavator/SetGeneric.cs b/ProjectExcavator/ProjectExcavator/SetGeneric.cs index 310a7a2..4e2bfba 100644 --- a/ProjectExcavator/ProjectExcavator/SetGeneric.cs +++ b/ProjectExcavator/ProjectExcavator/SetGeneric.cs @@ -14,34 +14,40 @@ namespace ProjectExcavator.Generics where T : class { /// - /// Массив объектов, которые храним + /// Список объектов, которые храним /// - private readonly T?[] _places; + private readonly List _places; /// /// Количество объектов в массиве /// - public int Count => _places.Length; + 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 excavator) + public bool Insert(T excavator) { - for (int i = _places.Length - 1; i > 0; i--) + // TODO вставка в начало набора + if (_places.Count >= _maxCount) { - _places[i] = _places[i - 1]; + return false; } - _places[0] = excavator; - return Insert(excavator, 0); + _places.Insert(0, excavator); + return true; } /// /// Добавление объекта в набор на конкретную позицию @@ -49,39 +55,21 @@ namespace ProjectExcavator.Generics /// Добавляемый экскаватор /// Позиция /// - public int Insert(T excavator, int position) + public bool Insert(T excavator, int position) { - if (position < 0 || position >= _places.Length) + // TODO проверка позиции + if (position < 0 || position >= _places.Count) { - return -1; + return false; } - - if (_places[position] != null) + // TODO проверка, что есть место для вставки + if (_places.Count >= _maxCount) { - return -1; + return false; } - - int NullIndex = 0; - for (int i = position; i < _places.Length; i++) - { - if (_places[i] == null) - { - NullIndex = i; - break; - } - } - - if (NullIndex == 0) - { - return -1; - } - - for (int i = NullIndex; i >= position; i--) - { - _places[i + 1] = _places[i]; - } - _places[position] = excavator; - return position; + // TODO вставка по позиции + _places.Insert(position, excavator); + return true; } /// /// Удаление объекта из набора с конкретной позиции @@ -90,11 +78,13 @@ namespace ProjectExcavator.Generics /// public bool Remove(int position) { - if (position < 0 || position >= _places.Length) + // TODO проверка позиции + if (position < 0 || position >= _places.Count) { return false; } - _places[position] = null; + // TODO удаление объекта из списка + _places.RemoveAt(position); return true; } /// @@ -102,13 +92,47 @@ namespace ProjectExcavator.Generics /// /// /// - public T? Get(int position) + public T? this[int position] { - if (position < 0 || position >= _places.Length) + get { - return null; + // TODO проверка позиции + if (position < 0 || position >= _places.Count) + { + return null; + } + return _places[position]; + } + set + { + // TODO проверка позиции + if (position < 0 || position >= _places.Count) + { + return; + } + // TODO проверка свободных мест в списке + if (_places.Count >= _maxCount) + { + return; + } + // TODO вставка в список по позиции + _places.Insert(position, value); + } + } + /// + /// Проход по списку + /// + /// + public IEnumerable GetExcavators(int? maxExcavators = null) + { + for (int i = 0; i < _places.Count; ++i) + { + yield return _places[i]; + if (maxExcavators.HasValue && i == maxExcavators.Value) + { + yield break; + } } - return _places[position]; } } }