diff --git a/DumpTruck/DumpTruck/DrawningDumpTruck.cs b/DumpTruck/DumpTruck/DrawningDumpTruck.cs index 6ecb5ee..1fd16b9 100644 --- a/DumpTruck/DumpTruck/DrawningDumpTruck.cs +++ b/DumpTruck/DumpTruck/DrawningDumpTruck.cs @@ -40,7 +40,7 @@ namespace DumpTruck.DrawningObjects if (dumpTruck.Trailer) { //прицеп - Brush trailer = new SolidBrush(Color.Black); + Brush trailer = new SolidBrush(EntityTruck.AdditionalColor); g.FillRectangle(trailer, _startPosX, _startPosY, 5, 38); g.FillRectangle(trailer, _startPosX + 5, _startPosY + 33, 70, 5); g.FillRectangle(trailer, _startPosX + 70, _startPosY, 5, 38); diff --git a/DumpTruck/DumpTruck/DrawningTruck.cs b/DumpTruck/DumpTruck/DrawningTruck.cs index bc1d030..fff37be 100644 --- a/DumpTruck/DumpTruck/DrawningTruck.cs +++ b/DumpTruck/DumpTruck/DrawningTruck.cs @@ -203,7 +203,7 @@ namespace DumpTruck.DrawningObjects //кузов Brush br = new SolidBrush(EntityTruck.BodyColor); g.FillRectangle(br, _startPosX + 81, _startPosY + 1, 29, 40); - Brush br1 = new SolidBrush(EntityTruck.AdditionalColor); + Brush br1 = new SolidBrush(EntityTruck.BodyColor); g.FillRectangle(br1, _startPosX + 1, _startPosY + 41, 109, 19); //колеса Brush wheels = new SolidBrush(Color.Black); diff --git a/DumpTruck/DumpTruck/FormDumpTruck.cs b/DumpTruck/DumpTruck/FormDumpTruck.cs index c99f10d..bf9d4f7 100644 --- a/DumpTruck/DumpTruck/FormDumpTruck.cs +++ b/DumpTruck/DumpTruck/FormDumpTruck.cs @@ -73,7 +73,7 @@ namespace DumpTruck } Color color2 = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)); ColorDialog dialog2 = new(); - if (dialog.ShowDialog() == DialogResult.OK) + if (dialog2.ShowDialog() == DialogResult.OK) { color2 = dialog2.Color; } diff --git a/DumpTruck/DumpTruck/FormTruckCollection.Designer.cs b/DumpTruck/DumpTruck/FormTruckCollection.Designer.cs index ba3a3e5..dd2855d 100644 --- a/DumpTruck/DumpTruck/FormTruckCollection.Designer.cs +++ b/DumpTruck/DumpTruck/FormTruckCollection.Designer.cs @@ -29,17 +29,24 @@ private void InitializeComponent() { groupBox1 = new GroupBox(); + groupBox2 = new GroupBox(); + listBoxStorages = new ListBox(); + buttonRemoveObject = new Button(); + buttonAddObject = new Button(); maskedTextBoxNumber = new MaskedTextBox(); buttonRefreshCollection = new Button(); buttonRemoveTruck = new Button(); buttonAddTruck = new Button(); pictureBoxCollection = new PictureBox(); + textBoxStorageName = new TextBox(); groupBox1.SuspendLayout(); + groupBox2.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); SuspendLayout(); // // groupBox1 // + groupBox1.Controls.Add(groupBox2); groupBox1.Controls.Add(maskedTextBoxNumber); groupBox1.Controls.Add(buttonRefreshCollection); groupBox1.Controls.Add(buttonRemoveTruck); @@ -53,9 +60,52 @@ groupBox1.TabStop = false; groupBox1.Text = "Инструменты"; // + // groupBox2 + // + groupBox2.Controls.Add(textBoxStorageName); + groupBox2.Controls.Add(listBoxStorages); + groupBox2.Controls.Add(buttonRemoveObject); + groupBox2.Controls.Add(buttonAddObject); + groupBox2.Location = new Point(3, 24); + groupBox2.Name = "groupBox2"; + groupBox2.Size = new Size(220, 331); + groupBox2.TabIndex = 5; + groupBox2.TabStop = false; + groupBox2.Text = "Наборы"; + // + // listBoxStorages + // + listBoxStorages.FormattingEnabled = true; + listBoxStorages.ItemHeight = 20; + listBoxStorages.Location = new Point(30, 135); + listBoxStorages.Name = "listBoxStorages"; + listBoxStorages.Size = new Size(171, 104); + listBoxStorages.TabIndex = 2; + listBoxStorages.SelectedIndexChanged += listBoxStorages_SelectedIndexChanged; + // + // buttonRemoveObject + // + buttonRemoveObject.Location = new Point(30, 258); + buttonRemoveObject.Name = "buttonRemoveObject"; + buttonRemoveObject.Size = new Size(171, 39); + buttonRemoveObject.TabIndex = 1; + buttonRemoveObject.Text = "Удалить набор"; + buttonRemoveObject.UseVisualStyleBackColor = true; + buttonRemoveObject.Click += buttonDelObject_Click; + // + // buttonAddObject + // + buttonAddObject.Location = new Point(30, 86); + buttonAddObject.Name = "buttonAddObject"; + buttonAddObject.Size = new Size(171, 33); + buttonAddObject.TabIndex = 0; + buttonAddObject.Text = "Добавить набор"; + buttonAddObject.UseVisualStyleBackColor = true; + buttonAddObject.Click += buttonAddObject_Click; + // // maskedTextBoxNumber // - maskedTextBoxNumber.Location = new Point(59, 135); + maskedTextBoxNumber.Location = new Point(7, 428); maskedTextBoxNumber.Margin = new Padding(3, 4, 3, 4); maskedTextBoxNumber.Name = "maskedTextBoxNumber"; maskedTextBoxNumber.Size = new Size(114, 27); @@ -63,7 +113,7 @@ // // buttonRefreshCollection // - buttonRefreshCollection.Location = new Point(7, 299); + buttonRefreshCollection.Location = new Point(0, 516); buttonRefreshCollection.Margin = new Padding(3, 4, 3, 4); buttonRefreshCollection.Name = "buttonRefreshCollection"; buttonRefreshCollection.Size = new Size(215, 44); @@ -74,7 +124,7 @@ // // buttonRemoveTruck // - buttonRemoveTruck.Location = new Point(7, 173); + buttonRemoveTruck.Location = new Point(0, 463); buttonRemoveTruck.Margin = new Padding(3, 4, 3, 4); buttonRemoveTruck.Name = "buttonRemoveTruck"; buttonRemoveTruck.Size = new Size(215, 45); @@ -85,7 +135,7 @@ // // buttonAddTruck // - buttonAddTruck.Location = new Point(7, 29); + buttonAddTruck.Location = new Point(0, 379); buttonAddTruck.Margin = new Padding(3, 4, 3, 4); buttonAddTruck.Name = "buttonAddTruck"; buttonAddTruck.Size = new Size(215, 41); @@ -103,6 +153,13 @@ pictureBoxCollection.TabIndex = 1; pictureBoxCollection.TabStop = false; // + // textBoxStorageName + // + textBoxStorageName.Location = new Point(30, 50); + textBoxStorageName.Name = "textBoxStorageName"; + textBoxStorageName.Size = new Size(171, 27); + textBoxStorageName.TabIndex = 3; + // // FormTruckCollection // AutoScaleDimensions = new SizeF(8F, 20F); @@ -115,6 +172,8 @@ Text = "Набор грузовиков"; groupBox1.ResumeLayout(false); groupBox1.PerformLayout(); + groupBox2.ResumeLayout(false); + groupBox2.PerformLayout(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit(); ResumeLayout(false); } @@ -127,5 +186,10 @@ private Button buttonRefreshCollection; private PictureBox pictureBoxCollection; private MaskedTextBox maskedTextBoxNumber; + private GroupBox groupBox2; + private Button buttonRemoveObject; + private Button buttonAddObject; + private ListBox listBoxStorages; + private TextBox textBoxStorageName; } } \ No newline at end of file diff --git a/DumpTruck/DumpTruck/FormTruckCollection.cs b/DumpTruck/DumpTruck/FormTruckCollection.cs index 9cb1184..f11552f 100644 --- a/DumpTruck/DumpTruck/FormTruckCollection.cs +++ b/DumpTruck/DumpTruck/FormTruckCollection.cs @@ -20,58 +20,140 @@ namespace DumpTruck /// /// Набор объектов /// - private readonly TrucksGenericCollection _trucks; + private readonly TrucksGenericStorage _storage; public FormTruckCollection() { InitializeComponent(); - _trucks = new TrucksGenericCollection(pictureBoxCollection.Width, pictureBoxCollection.Height); + _storage = new TrucksGenericStorage(pictureBoxCollection.Width, pictureBoxCollection.Height); } + 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 buttonAddTruck_Click(object sender, EventArgs e) { + if (listBoxStorages.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? + string.Empty]; + if (obj == null) + { + return; + } FormDumpTruck form = new(); if (form.ShowDialog() == DialogResult.OK) { - if (_trucks + form.selectedTruck != -1) + if (obj + form.selectedTruck != -1) { MessageBox.Show("Объект добавлен"); - pictureBoxCollection.Image = _trucks.ShowTrucks(); + pictureBoxCollection.Image = obj.ShowTrucks(); } else { MessageBox.Show("Не удалось добавить объект"); } } - } private void buttonRemoveTruck_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 (_trucks - pos) + if (obj - pos) { MessageBox.Show("Объект удален"); - pictureBoxCollection.Image = _trucks.ShowTrucks(); + pictureBoxCollection.Image = obj.ShowTrucks(); } else { MessageBox.Show("Не удалось удалить объект"); } - } - private void buttonRefreshCollection_Click(object sender, EventArgs e) { - pictureBoxCollection.Image = _trucks.ShowTrucks(); + + if (listBoxStorages.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? + string.Empty]; + if (obj == null) + { + return; + } + pictureBoxCollection.Image = obj.ShowTrucks(); } + + 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 listBoxStorages_SelectedIndexChanged(object sender, EventArgs e) + { + pictureBoxCollection.Image = + _storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowTrucks(); + } + + 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(); + } + } + } } diff --git a/DumpTruck/DumpTruck/SetGeneric.cs b/DumpTruck/DumpTruck/SetGeneric.cs index 2f3cdb8..6acc2b3 100644 --- a/DumpTruck/DumpTruck/SetGeneric.cs +++ b/DumpTruck/DumpTruck/SetGeneric.cs @@ -6,7 +6,7 @@ using System.Text; using System.Threading.Tasks; namespace DumpTruck.Generics -{ +{ /// /// Параметризованный набор объектов /// @@ -17,18 +17,23 @@ namespace DumpTruck.Generics /// /// Массив объектов, которые храним /// - 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); } /// /// Добавление объекта в набор @@ -47,39 +52,13 @@ namespace DumpTruck.Generics /// public int Insert(T truck, int position) { - // TODO проверка позиции - if (position < 0 || position > Count) + // Проверка позиции + if (position < 0 || position >= _maxCount) { return -1; } - // TODO проверка, что элемент массива по этой позиции пустой, если нет, то - if (_places[position] != null) - { - // проверка, что после вставляемого элемента в массиве есть пустой элемент - int nullIndex = -1; - for (int i = position + 1; i < Count; i++) - { - if (_places[i] == null) - { - nullIndex = i; - break; - } - } - // Если пустого элемента нет, то выходим - if (nullIndex < 0) - { - return -1; - } - // сдвиг всех объектов, находящихся справа от позиции до первого пустого элемента - int j = nullIndex - 1; - while (j >= position) - { - _places[j + 1] = _places[j]; - j--; - } - } - // TODO вставка по позиции - _places[position] = truck; + // Вставка по позиции + _places.Insert(position, truck); return position; } /// @@ -100,11 +79,43 @@ namespace DumpTruck.Generics /// /// /// - public T? Get(int position) + public T? this[int position] { - // TODO проверка позиции - 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 >= _maxCount || Count == _maxCount) + { + return; + } + // Вставка в список по позиции + _places.Insert(position, value); + } + } + /// + /// Проход по списку + /// + /// + public IEnumerable GetTrucks(int? maxTrucks = null) + { + for (int i = 0; i < _places.Count; ++i) + { + yield return _places[i]; + if (maxTrucks.HasValue && i == maxTrucks.Value) + { + yield break; + } + } } } } \ No newline at end of file diff --git a/DumpTruck/DumpTruck/TruckGenericStorage.cs b/DumpTruck/DumpTruck/TruckGenericStorage.cs new file mode 100644 index 0000000..b7b3277 --- /dev/null +++ b/DumpTruck/DumpTruck/TruckGenericStorage.cs @@ -0,0 +1,78 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using DumpTruck.DrawningObjects; +using DumpTruck.Generics; +using DumpTruck.MovementStrategy; + + +namespace DumpTruck.Generics +{ + internal class TrucksGenericStorage + { + /// + /// Словарь (хранилище) + /// + readonly Dictionary> _truckStorages; + /// + /// Возвращение списка названий наборов + /// + public List Keys => _truckStorages.Keys.ToList(); + /// + /// Ширина окна отрисовки + /// + private readonly int _pictureWidth; + /// + /// Высота окна отрисовки + /// + private readonly int _pictureHeight; + /// + /// Конструктор + /// + /// + /// /// + public TrucksGenericStorage(int pictureWidth, int pictureHeight) + { + _truckStorages = new Dictionary>(); + _pictureWidth = pictureWidth; + _pictureHeight = pictureHeight; + } + /// + /// Добавление набора + /// + /// Название набора + public void AddSet(string name) + { + // TODO Прописать логику для добавления + _truckStorages.Add(name, new TrucksGenericCollection(_pictureWidth, _pictureHeight)); + } + /// + /// Удаление набора + /// + /// Название набора + public void DelSet(string name) + { + // TODO Прописать логику для удаления + if (_truckStorages.ContainsKey(name)) + _truckStorages.Remove(name); + } + /// + /// Доступ к набору + /// + /// + /// + public TrucksGenericCollection? this[string ind] + { + get + { + // TODO Продумать логику получения набора + if (_truckStorages.ContainsKey(ind)) + return _truckStorages[ind]; + return null; + } + } + + } +} \ No newline at end of file diff --git a/DumpTruck/DumpTruck/TrucksGenericCollection.cs b/DumpTruck/DumpTruck/TrucksGenericCollection.cs index d56e408..6a9a999 100644 --- a/DumpTruck/DumpTruck/TrucksGenericCollection.cs +++ b/DumpTruck/DumpTruck/TrucksGenericCollection.cs @@ -51,8 +51,7 @@ namespace DumpTruck.Generics /// /// /// - public static int operator +(TrucksGenericCollection collect, T? - obj) + public static int operator +(TrucksGenericCollection collect, T? obj) { if (obj == null) { @@ -68,7 +67,7 @@ namespace DumpTruck.Generics /// public static bool operator -(TrucksGenericCollection collect, int pos) { - T? obj = collect._collection.Get(pos); + T? obj = collect._collection[pos]; if (obj != null) { return collect._collection.Remove(pos); @@ -82,7 +81,7 @@ namespace DumpTruck.Generics /// public U? GetU(int pos) { - return (U?)_collection.Get(pos)?.GetMoveableObject; + return (U?)_collection[pos]?.GetMoveableObject; } /// /// Вывод всего набора объектов @@ -107,7 +106,8 @@ namespace DumpTruck.Generics { for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j) - {//линия рамзетки места + //рамзеткa места + { g.DrawLine(pen, i * _placeSizeWidth, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight); @@ -123,13 +123,9 @@ namespace DumpTruck.Generics private void DrawObjects(Graphics g) { int width = _pictureWidth / _placeSizeWidth; - for (int i = 0; i < _collection.Count; i++) { - // TODO получение объекта - // TODO установка позиции - // TODO прорисовка объекта - DrawningTruck? truck = _collection.Get(i); + DrawningTruck? truck = _collection[i]; if (truck == null) continue; truck.SetPosition(i % width * _placeSizeWidth, i / width * _placeSizeHeight);