diff --git a/ProjectExcavator/ProjectExcavator/ExcavatorGenericCollection.cs b/ProjectExcavator/ProjectExcavator/ExcavatorGenericCollection.cs index b457d0a..a3b73e4 100644 --- a/ProjectExcavator/ProjectExcavator/ExcavatorGenericCollection.cs +++ b/ProjectExcavator/ProjectExcavator/ExcavatorGenericCollection.cs @@ -26,17 +26,17 @@ namespace ProjectExcavator.Generic _pictureHeight = picHeight; _collection = new SetGeneric(width * height); } - 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 T? operator -(ExcavatorGenericCollection collect, int pos) { - T? obj = collect._collection.Get(pos); + T? obj = collect._collection[pos]; if (obj != null) { collect._collection.Remove(pos); @@ -44,7 +44,7 @@ namespace ProjectExcavator.Generic return obj; } - public U? GetU(int pos) => (U)_collection.Get(pos)?.GetMoveableObject; + public U? GetU(int pos) => (U?)_collection[pos]?.GetMoveableObject; /// /// Вывод всего набора объектов /// @@ -72,22 +72,18 @@ namespace ProjectExcavator.Generic } private void DrawObjects(Graphics g) { - for(int i = 0; i < _collection.Count;i++) + int i = 0; + foreach (var excavator in _collection.GetExcavators()) { - T t = _collection.Get(i); - if(t != null) + if (excavator != null) { - t.SetPosition((i % (_pictureWidth / _placeSizeWidth)) * _placeSizeWidth, (i / (_pictureWidth / _placeSizeWidth)) * _placeSizeHeight); - if (t is DrawningExcavator) - { - //тут преведение избыточно - t.DrawTransport(g); - } + excavator.SetPosition((i % (_pictureWidth / _placeSizeWidth)) * _placeSizeWidth, (i / (_pictureWidth / _placeSizeWidth)) * _placeSizeHeight); + if (excavator is DrawningExcavator) + (excavator as DrawningExcavator).DrawTransport(g); else - { - t.DrawTransport(g); - } + excavator.DrawTransport(g); } + i++; } } } diff --git a/ProjectExcavator/ProjectExcavator/ExcavatorsGenericStorage.cs b/ProjectExcavator/ProjectExcavator/ExcavatorsGenericStorage.cs new file mode 100644 index 0000000..d81afcd --- /dev/null +++ b/ProjectExcavator/ProjectExcavator/ExcavatorsGenericStorage.cs @@ -0,0 +1,81 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ProjectExcavator.DrawningObjects; +using ProjectExcavator.Generic; +using ProjectExcavator.MovementStrategy; + +namespace ProjectExcavator.Generic +{ + internal class ExcavatorsGenericStorage + { + /// + /// Словарь (хранилище) + /// + readonly Dictionary> _excavatorStorages; + /// + /// Возвращение списка названий наборов + /// + public List Keys => _excavatorStorages.Keys.ToList(); + /// + /// Ширина окна отрисовки + /// + private readonly int _pictureWidth; + /// + /// Высота окна отрисовки + /// + private readonly int _pictureHeight; + /// + /// Конструктор + /// + /// + /// + public ExcavatorsGenericStorage(int pictureWidth, int pictureHeight) + { + _excavatorStorages = new Dictionary>(); + _pictureWidth = pictureWidth; + _pictureHeight = pictureHeight; + } + /// + /// Добавление набора + /// + /// Название набора + public void AddSet(string name) + { + if(_excavatorStorages.ContainsKey(name)) + { + return; + } + _excavatorStorages[name] = new ExcavatorGenericCollection(_pictureWidth,_pictureHeight); + } + /// + /// Удаление набора + /// + /// Название набора + public void DelSet(string name) + { + if (!_excavatorStorages.ContainsKey(name)) return; + _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 8edecb0..e9d2d89 100644 --- a/ProjectExcavator/ProjectExcavator/FormExcavatorCollection.Designer.cs +++ b/ProjectExcavator/ProjectExcavator/FormExcavatorCollection.Designer.cs @@ -33,6 +33,10 @@ maskedTextBoxNumber = new MaskedTextBox(); buttonRemoveExcavator = new Button(); buttonRefreshCollection = new Button(); + listBoxStorages = new ListBox(); + AddCollectButton = new Button(); + DeleteCollectButton = new Button(); + textBoxStorageName = new TextBox(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); SuspendLayout(); // @@ -46,7 +50,7 @@ // // buttonAddExcavator // - buttonAddExcavator.Location = new Point(623, 26); + buttonAddExcavator.Location = new Point(623, 244); buttonAddExcavator.Name = "buttonAddExcavator"; buttonAddExcavator.Size = new Size(174, 51); buttonAddExcavator.TabIndex = 1; @@ -56,14 +60,14 @@ // // maskedTextBoxNumber // - maskedTextBoxNumber.Location = new Point(623, 83); + maskedTextBoxNumber.Location = new Point(623, 301); maskedTextBoxNumber.Name = "maskedTextBoxNumber"; maskedTextBoxNumber.Size = new Size(174, 23); maskedTextBoxNumber.TabIndex = 2; // // buttonRemoveExcavator // - buttonRemoveExcavator.Location = new Point(623, 128); + buttonRemoveExcavator.Location = new Point(623, 330); buttonRemoveExcavator.Name = "buttonRemoveExcavator"; buttonRemoveExcavator.Size = new Size(174, 38); buttonRemoveExcavator.TabIndex = 3; @@ -73,19 +77,60 @@ // // buttonRefreshCollection // - buttonRefreshCollection.Location = new Point(625, 172); + buttonRefreshCollection.Location = new Point(623, 374); buttonRefreshCollection.Name = "buttonRefreshCollection"; - buttonRefreshCollection.Size = new Size(172, 33); + buttonRefreshCollection.Size = new Size(174, 33); buttonRefreshCollection.TabIndex = 4; buttonRefreshCollection.Text = "Обновить коллекцию"; buttonRefreshCollection.UseVisualStyleBackColor = true; buttonRefreshCollection.Click += buttonRefreshCollection_Click; // + // listBoxStorages + // + listBoxStorages.FormattingEnabled = true; + listBoxStorages.ItemHeight = 15; + listBoxStorages.Location = new Point(623, 90); + listBoxStorages.Name = "listBoxStorages"; + listBoxStorages.Size = new Size(174, 79); + listBoxStorages.TabIndex = 5; + listBoxStorages.SelectedIndexChanged += ListBoxObjects_SelectedIndexChanged; + // + // AddCollectButton + // + AddCollectButton.Location = new Point(623, 53); + AddCollectButton.Name = "AddCollectButton"; + AddCollectButton.Size = new Size(174, 31); + AddCollectButton.TabIndex = 6; + AddCollectButton.Text = "Добавить набор"; + AddCollectButton.UseVisualStyleBackColor = true; + AddCollectButton.Click += ButtonAddObject_Click; + // + // DeleteCollectButton + // + DeleteCollectButton.Location = new Point(623, 175); + DeleteCollectButton.Name = "DeleteCollectButton"; + DeleteCollectButton.Size = new Size(174, 28); + DeleteCollectButton.TabIndex = 7; + DeleteCollectButton.Text = "Удалить набор"; + DeleteCollectButton.UseVisualStyleBackColor = true; + DeleteCollectButton.Click += ButtonDelObject_Click; + // + // textBoxStorageName + // + textBoxStorageName.Location = new Point(623, 24); + textBoxStorageName.Name = "textBoxStorageName"; + textBoxStorageName.Size = new Size(174, 23); + textBoxStorageName.TabIndex = 8; + // // FormExcavatorCollection // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(800, 450); + Controls.Add(textBoxStorageName); + Controls.Add(DeleteCollectButton); + Controls.Add(AddCollectButton); + Controls.Add(listBoxStorages); Controls.Add(buttonRefreshCollection); Controls.Add(buttonRemoveExcavator); Controls.Add(maskedTextBoxNumber); @@ -105,5 +150,9 @@ private MaskedTextBox maskedTextBoxNumber; private Button buttonRemoveExcavator; private Button buttonRefreshCollection; + private ListBox listBoxStorages; + private Button AddCollectButton; + private Button DeleteCollectButton; + private TextBox textBoxStorageName; } } \ No newline at end of file diff --git a/ProjectExcavator/ProjectExcavator/FormExcavatorCollection.cs b/ProjectExcavator/ProjectExcavator/FormExcavatorCollection.cs index 29ea4c3..2680200 100644 --- a/ProjectExcavator/ProjectExcavator/FormExcavatorCollection.cs +++ b/ProjectExcavator/ProjectExcavator/FormExcavatorCollection.cs @@ -16,49 +16,146 @@ namespace ProjectExcavator public partial class FormExcavatorCollection : Form { private readonly ExcavatorGenericCollection _excavators; - + /// + /// Набор объектов + /// + private readonly ExcavatorsGenericStorage _storage; public FormExcavatorCollection() { InitializeComponent(); - _excavators = new ExcavatorGenericCollection(pictureBoxCollection.Width, pictureBoxCollection.Height); + _storage = new ExcavatorsGenericStorage(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(); + } + } + private void buttonAddExcavator_Click(object sender, EventArgs e) { + if (listBoxStorages.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty]; + if (obj == null) + { + return; + } ExcavatorForm form = new(); if (form.ShowDialog() == DialogResult.OK) { - if (_excavators + form.SelectedExcavator != -1) + if (obj + form.SelectedExcavator) { - MessageBox.Show("Объект добален"); - pictureBoxCollection.Image = _excavators.ShowExcavator(); + MessageBox.Show("Объект добавлен"); + pictureBoxCollection.Image = obj.ShowExcavator(); } else { - MessageBox.Show("Не удалось добавить новый обьект"); + MessageBox.Show("Не удалось добавить объект"); } } } + private void buttonRemoveExcavator_Click(object sender, EventArgs e) { - if (MessageBox.Show("Удалить объект?", "Удаление...", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) + 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(_excavators - pos != null) + if (obj - pos != null) { MessageBox.Show("Объект удален"); - pictureBoxCollection.Image = _excavators.ShowExcavator(); + pictureBoxCollection.Image = obj.ShowExcavator(); } else { - MessageBox.Show("Не удалось удалить обхект"); + MessageBox.Show("Не удалось удалить объект"); } + } - } - private void buttonRefreshCollection_Click(object sender, EventArgs e) + private void buttonRefreshCollection_Click(object sender, EventArgs +e) { - pictureBoxCollection.Image = _excavators.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 e0c83ee..82813df 100644 --- a/ProjectExcavator/ProjectExcavator/SetGeneric.cs +++ b/ProjectExcavator/ProjectExcavator/SetGeneric.cs @@ -4,54 +4,70 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace ProjectExcavator +namespace ProjectExcavator.Generic { 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]; + _maxCount = count; + _places = new List(_maxCount); } - public int Insert(T excavator) + public bool Insert(T excavator) { - return Insert(excavator,0); - } - public int Insert(T excavator, int position) - { - if (position < 0 || position >= _places.Length) - return -1; - for (; position < _places.Length; position++) + if(_places.Count >= _maxCount) { - if (_places[position] == null) - { - break; - } - } - if (position == _places.Length) - return -1; - for (; position > 0; position--) - { - _places[position] = _places[position - 1]; - } - _places[position] = excavator; - return position; - } - public bool Remove(int position) { - if (position >= _places.Length || position<0) return false; - _places[position] = null; + } + _places.Insert(0, excavator); return true; } - public T? Get(int position) + public bool Insert(T excavator, int position) { - if(position >= _places.Length || position < 0) + if (position < 0 || position >= _maxCount) + return false; + if (Count >= _maxCount) + return false; + _places.Insert(0,excavator); + return true; + } + public bool Remove(int position) { + if (position > _places.Count || position<0) + return false; + if (position >= Count) + return false; + _places.RemoveAt(position); + return true; + } + public T? this[int position] + { + get { - return null; + if (position < 0 || position > _maxCount) + return null; + return _places[position]; + } + set + { + if (position < 0 || position > _maxCount) + return; + _places[position] = value; + } + } + public IEnumerable GetExcavators(int? maxCars = null) + { + for (int i = 0; i < _places.Count; ++i) + { + yield return _places[i]; + if (maxCars.HasValue && i == maxCars.Value) + { + yield break; + } } - return _places[position]; } } }