diff --git a/ProjectExcavator/ProjectExcavator/DrawingObjectExcavator.cs b/ProjectExcavator/ProjectExcavator/DrawingObjectExcavator.cs index 4fc56be..638ad85 100644 --- a/ProjectExcavator/ProjectExcavator/DrawingObjectExcavator.cs +++ b/ProjectExcavator/ProjectExcavator/DrawingObjectExcavator.cs @@ -12,7 +12,7 @@ namespace ProjectExcavator.MovementStrategy /// public class DrawingObjectExcavator : IMoveableObject { - private readonly DrawingExcavator? _drawingExcavator = null; + public DrawingExcavator _drawingExcavator { get; private set; } public DrawingObjectExcavator(DrawingExcavator drawingExcavator) { _drawingExcavator = drawingExcavator; diff --git a/ProjectExcavator/ProjectExcavator/ExcavatorGenericCollection.cs b/ProjectExcavator/ProjectExcavator/ExcavatorGenericCollection.cs index e19b378..b93a70b 100644 --- a/ProjectExcavator/ProjectExcavator/ExcavatorGenericCollection.cs +++ b/ProjectExcavator/ProjectExcavator/ExcavatorGenericCollection.cs @@ -56,32 +56,33 @@ namespace ProjectExcavator.Generics /// /// /// - public static int operator +(ExcavatorGenericCollection collect, T? + 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 /// @@ -89,7 +90,8 @@ namespace ProjectExcavator.Generics /// public U? GetU(int pos) { - return (U?)_collection.Get(pos)?.GetMoveableObject; + return (U?)_collection[pos]?.GetMoveableObject; + } /// /// Вывод всего набора объектов @@ -133,12 +135,15 @@ namespace ProjectExcavator.Generics int height = _pictureHeight / _placeSizeHeight; for (int i = 0; i < _collection.Count; i++) { - DrawingExcavator? excavator = _collection.Get(i); + // TODO получение объекта + T? excavator = _collection[i]; if (excavator == null) continue; int r = i / width; int s = width - 1 - (i % width); + // TODO установка позиции excavator.SetPosition(s * _placeSizeWidth, r * _placeSizeHeight); + // TODO прорисовка объекта excavator.DrawTransport(g); } } diff --git a/ProjectExcavator/ProjectExcavator/ExcavatorGenericStorage.cs b/ProjectExcavator/ProjectExcavator/ExcavatorGenericStorage.cs new file mode 100644 index 0000000..a8be07f --- /dev/null +++ b/ProjectExcavator/ProjectExcavator/ExcavatorGenericStorage.cs @@ -0,0 +1,83 @@ +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; + _removedObjects = new LinkedList(); + } + 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; + } + } + public DrawingObjectExcavator? this[string dictIndex, int objIndex] + { + get + { + if (_excavatorStorages.ContainsKey(dictIndex)) + { + var selectedDictElement = _excavatorStorages[dictIndex]; + var selectedObject = selectedDictElement.GetU(objIndex); + return selectedObject; + } + return null; + } + } + private LinkedList _removedObjects; + + public DrawingObjectExcavator RemovedObject + { + set + { + _removedObjects.AddLast(value); + } + get + { + if (_removedObjects == null || _removedObjects.Count == 0) + { + return null; + } + var removedObject = _removedObjects.Last(); + _removedObjects.RemoveLast(); + return removedObject; + } + } + } +} diff --git a/ProjectExcavator/ProjectExcavator/FormExcavator.cs b/ProjectExcavator/ProjectExcavator/FormExcavator.cs index e7f0867..8c214ac 100644 --- a/ProjectExcavator/ProjectExcavator/FormExcavator.cs +++ b/ProjectExcavator/ProjectExcavator/FormExcavator.cs @@ -15,12 +15,13 @@ namespace ProjectExcavator public FormExcavator() { InitializeComponent(); - comboBoxStrategy.Items.Add(""); - comboBoxStrategy.Items.Add(""); - } - - private void Draw() + public FormExcavator(DrawingExcavator drawingObject) + { + _drawingExcavator = drawingObject; + InitializeComponent(); + } + public void Draw() { if (_drawingExcavator == null) { diff --git a/ProjectExcavator/ProjectExcavator/FormExcavatorCollection.Designer.cs b/ProjectExcavator/ProjectExcavator/FormExcavatorCollection.Designer.cs index 50d106a..c73cb8a 100644 --- a/ProjectExcavator/ProjectExcavator/FormExcavatorCollection.Designer.cs +++ b/ProjectExcavator/ProjectExcavator/FormExcavatorCollection.Designer.cs @@ -34,9 +34,16 @@ 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(); this.buttonGeneration = new System.Windows.Forms.Button(); + this.buttonRemoveobj = new System.Windows.Forms.Button(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).BeginInit(); this.groupBox1.SuspendLayout(); + this.groupBox2.SuspendLayout(); this.SuspendLayout(); // // pictureBoxCollection @@ -50,14 +57,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; @@ -67,36 +74,89 @@ // // 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; this.buttonRemoveEx.Text = "Удалить экскаватор"; this.buttonRemoveEx.UseVisualStyleBackColor = true; + this.buttonRemoveEx.Click += new System.EventHandler(this.ButtonRemoveEx_Click); // // 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; this.buttonRefreshCollection.Text = "Обновить коллекцию"; this.buttonRefreshCollection.UseVisualStyleBackColor = true; + this.buttonRefreshCollection.Click += new System.EventHandler(this.ButtonRefreshCollection_Click); // // groupBox1 // - this.groupBox1.Controls.Add(this.buttonGeneration); + this.groupBox1.Controls.Add(this.buttonRemoveobj); + 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); + // // buttonGeneration // this.buttonGeneration.Location = new System.Drawing.Point(6, 332); @@ -107,6 +167,16 @@ this.buttonGeneration.UseVisualStyleBackColor = true; this.buttonGeneration.Click += new System.EventHandler(this.buttonGeneration_Click); // + // buttonRemoveobj + // + this.buttonRemoveobj.Location = new System.Drawing.Point(16, 265); + this.buttonRemoveobj.Name = "buttonRemoveobj"; + this.buttonRemoveobj.Size = new System.Drawing.Size(145, 23); + this.buttonRemoveobj.TabIndex = 6; + this.buttonRemoveobj.Text = "Удалённые обьекты"; + this.buttonRemoveobj.UseVisualStyleBackColor = true; + this.buttonRemoveobj.Click += new System.EventHandler(this.buttonRemoveobj_Click); + // // FormExcavatorCollection // this.ClientSize = new System.Drawing.Size(909, 461); @@ -116,6 +186,8 @@ ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).EndInit(); this.groupBox1.ResumeLayout(false); this.groupBox1.PerformLayout(); + this.groupBox2.ResumeLayout(false); + this.groupBox2.PerformLayout(); this.ResumeLayout(false); } @@ -128,6 +200,12 @@ private Button buttonRemoveEx; private Button buttonRefreshCollection; private GroupBox groupBox1; + private GroupBox groupBox2; + private TextBox textBoxStorageName; + private Button buttonDelObject; + private ListBox listBoxStorages; + private Button buttonAddObject; private Button buttonGeneration; + private Button buttonRemoveobj; } } \ No newline at end of file diff --git a/ProjectExcavator/ProjectExcavator/FormExcavatorCollection.cs b/ProjectExcavator/ProjectExcavator/FormExcavatorCollection.cs index f2aec94..c94c145 100644 --- a/ProjectExcavator/ProjectExcavator/FormExcavatorCollection.cs +++ b/ProjectExcavator/ProjectExcavator/FormExcavatorCollection.cs @@ -12,16 +12,105 @@ namespace ProjectExcavator /// /// Набор объектов /// - 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(); + if (listBoxStorages.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? + string.Empty]; + if (obj == null) + { + return; + } + pictureBoxCollection.Image = obj.ShowExcavator(); + } + /// + /// Выбор набора + /// + /// + /// + 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(); + if (listBoxStorages.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? + string.Empty]; + if (obj == null) + { + return; + } + MessageBox.Show("Набор удален"); + pictureBoxCollection.Image = obj.ShowExcavator(); + } } /// /// Добавление объекта в набор @@ -30,14 +119,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 +150,28 @@ 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) + var removableObject = _storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty, pos]; + if (_storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty] - pos != null) { MessageBox.Show("Объект удален"); - pictureBoxCollection.Image = _excavator.ShowExcavator(); + _storage.RemovedObject = (DrawingObjectExcavator)removableObject; + pictureBoxCollection.Image = obj.ShowExcavator(); } else { @@ -75,7 +185,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(); } private void buttonGeneration_Click(object sender, EventArgs e) @@ -83,5 +203,18 @@ namespace ProjectExcavator RandGeneration form = new(); form.ShowDialog(); } + + private void buttonRemoveobj_Click(object sender, EventArgs e) + { + DrawingObjectExcavator removedObject = _storage.RemovedObject; + if (removedObject == null) + { + MessageBox.Show("Не удалось показать удаленные объекты"); + return; + } + FormExcavator formRemovedObject = new FormExcavator(removedObject._drawingExcavator); + formRemovedObject.Show(); + formRemovedObject.Draw(); + } } } diff --git a/ProjectExcavator/ProjectExcavator/SetGeneric.cs b/ProjectExcavator/ProjectExcavator/SetGeneric.cs index 883a270..cbfe8bd 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,66 +55,83 @@ 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; } /// /// Удаление объекта из набора с конкретной позиции /// /// /// - public bool Remove(int position) + public T Remove(int position) { - if (position < 0 || position >= _places.Length) + if (position > Count || _places[position] == null) { - return false; + return null; } - - _places[position] = null; - return true; + var result = _places[position]; + _places.RemoveAt(position); + return result; } /// /// Получение объекта из набора по позиции /// /// /// - public T? Get(int position) + public T? this[int position] { - if (position < 0 || position >= _places.Length) - return null; - - return _places[position]; + get + { + // 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; + } + } } } }