From 2523d4f151cedd81faf509666a450cc430c96c40 Mon Sep 17 00:00:00 2001 From: sardq Date: Sun, 12 Nov 2023 11:51:51 +0400 Subject: [PATCH] =?UTF-8?q?=D0=BB=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D0=BD=D0=B0=D1=8F=204?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../HoistingCrane/CranesGenericCollection.cs | 28 +++--- .../HoistingCrane/CranesGenericStorage.cs | 52 +++++++++++ HoistingCrane/HoistingCrane/DrawingCrane.cs | 1 + .../FormCraneCollection.Designer.cs | 80 ++++++++++++++-- .../HoistingCrane/FormCraneCollection.cs | 91 +++++++++++++++++-- HoistingCrane/HoistingCrane/SetGeneric.cs | 72 ++++++++------- 6 files changed, 260 insertions(+), 64 deletions(-) create mode 100644 HoistingCrane/HoistingCrane/CranesGenericStorage.cs diff --git a/HoistingCrane/HoistingCrane/CranesGenericCollection.cs b/HoistingCrane/HoistingCrane/CranesGenericCollection.cs index 162531f..bd24e44 100644 --- a/HoistingCrane/HoistingCrane/CranesGenericCollection.cs +++ b/HoistingCrane/HoistingCrane/CranesGenericCollection.cs @@ -39,7 +39,7 @@ namespace HoistingCrane.Generics /// Перегрузка оператора вычитания public static bool operator -(CranesGenericCollection collect, int pos) { - T? obj = collect._collection.Get(pos); + T? obj = collect._collection[pos]; if (obj == null) { return false; @@ -49,7 +49,7 @@ namespace HoistingCrane.Generics /// Получение объекта IMoveableObject public U? GetU(int pos) { - return (U?)_collection.Get(pos)?.GetMoveableObject; + return (U?)_collection[pos]?.GetMoveableObject; } /// Вывод всего набора объектов public Bitmap ShowCars() @@ -77,24 +77,22 @@ namespace HoistingCrane.Generics private void DrawObjects(Graphics g) { T? obj; - int j = 3; - int k = 1; - for (int i = 0; i < _collection.Count; i++) + int j = _pictureWidth/_placeSizeWidth-1; + int k = _pictureHeight/_placeSizeHeight-1; + foreach (var crane in _collection.GetCranes()) { - if(_collection.Get(i) == null) - continue; - if (j < 0) + if (crane != null) { - j += 4; - k--; - } - if (_collection.Get(i) != null) - { - obj = _collection.Get(i); + if (j < 0) + { + j += _pictureWidth / _placeSizeWidth; + k--; + } + obj = crane; obj.SetPosition(_placeSizeWidth * j, _placeSizeHeight * k); obj.DrawTransport(g); + j--; } - j--; } } } diff --git a/HoistingCrane/HoistingCrane/CranesGenericStorage.cs b/HoistingCrane/HoistingCrane/CranesGenericStorage.cs new file mode 100644 index 0000000..6465166 --- /dev/null +++ b/HoistingCrane/HoistingCrane/CranesGenericStorage.cs @@ -0,0 +1,52 @@ +using HoistingCrane.DrawningObjects; +using HoistingCrane.Generics; +using HoistingCrane.MovementStrategy; + +namespace HoistingCrane +{ + internal class CranesGenericStorage + { + /// Словарь (хранилище) + readonly Dictionary> _craneStorages; + /// Возвращение списка названий наборов + public List Keys => _craneStorages.Keys.ToList(); + /// Ширина окна отрисовки + private readonly int _pictureWidth; + /// Высота окна отрисовки + private readonly int _pictureHeight; + /// Конструктор + public CranesGenericStorage(int pictureWidth, int pictureHeight) + { + _craneStorages = new Dictionary>(); + _pictureWidth = pictureWidth; + _pictureHeight = pictureHeight; + } + /// Добавление набора + public void AddSet(string name) + { + if (_craneStorages.ContainsKey(name)) + { + return; + } + _craneStorages.Add(name, new CranesGenericCollection(_pictureWidth, _pictureHeight)); + } + /// Удаление набора + public void DelSet(string name) + { + if (_craneStorages.ContainsKey(name)) + { + _craneStorages.Remove(name); + } + } + /// Доступ к набору + public CranesGenericCollection? this[string ind] + { + get + { + if (_craneStorages.ContainsKey(ind)) + return _craneStorages[ind]; + return null; + } + } + } +} diff --git a/HoistingCrane/HoistingCrane/DrawingCrane.cs b/HoistingCrane/HoistingCrane/DrawingCrane.cs index 7fe469e..75085b8 100644 --- a/HoistingCrane/HoistingCrane/DrawingCrane.cs +++ b/HoistingCrane/HoistingCrane/DrawingCrane.cs @@ -36,6 +36,7 @@ namespace HoistingCrane.DrawningObjects } public void SetPosition(int x, int y) { + while (x + _craneWidth > _pictureWidth) { x -= _pictureWidth; diff --git a/HoistingCrane/HoistingCrane/FormCraneCollection.Designer.cs b/HoistingCrane/HoistingCrane/FormCraneCollection.Designer.cs index ceb8413..0899d7c 100644 --- a/HoistingCrane/HoistingCrane/FormCraneCollection.Designer.cs +++ b/HoistingCrane/HoistingCrane/FormCraneCollection.Designer.cs @@ -34,33 +34,40 @@ this.buttonDelete = new System.Windows.Forms.Button(); this.buttonAdd = new System.Windows.Forms.Button(); this.pictureBoxCollection = new System.Windows.Forms.PictureBox(); + this.groupBoxSet = new System.Windows.Forms.GroupBox(); + this.textBoxStorageName = new System.Windows.Forms.TextBox(); + this.buttonAddSet = new System.Windows.Forms.Button(); + this.listBoxStorages = new System.Windows.Forms.ListBox(); + this.buttonRemoveSet = new System.Windows.Forms.Button(); this.groupBoxInstruments.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).BeginInit(); + this.groupBoxSet.SuspendLayout(); this.SuspendLayout(); // // groupBoxInstruments // + this.groupBoxInstruments.Controls.Add(this.groupBoxSet); this.groupBoxInstruments.Controls.Add(this.maskedTextBoxNumber); this.groupBoxInstruments.Controls.Add(this.buttonReload); this.groupBoxInstruments.Controls.Add(this.buttonDelete); this.groupBoxInstruments.Controls.Add(this.buttonAdd); this.groupBoxInstruments.Location = new System.Drawing.Point(811, 0); this.groupBoxInstruments.Name = "groupBoxInstruments"; - this.groupBoxInstruments.Size = new System.Drawing.Size(185, 451); + this.groupBoxInstruments.Size = new System.Drawing.Size(185, 464); this.groupBoxInstruments.TabIndex = 0; this.groupBoxInstruments.TabStop = false; this.groupBoxInstruments.Text = "Инструменты"; // // maskedTextBoxNumber // - this.maskedTextBoxNumber.Location = new System.Drawing.Point(11, 71); + this.maskedTextBoxNumber.Location = new System.Drawing.Point(23, 319); this.maskedTextBoxNumber.Name = "maskedTextBoxNumber"; this.maskedTextBoxNumber.Size = new System.Drawing.Size(150, 31); this.maskedTextBoxNumber.TabIndex = 9; // // buttonReload // - this.buttonReload.Location = new System.Drawing.Point(6, 172); + this.buttonReload.Location = new System.Drawing.Point(12, 397); this.buttonReload.Name = "buttonReload"; this.buttonReload.Size = new System.Drawing.Size(167, 35); this.buttonReload.TabIndex = 8; @@ -70,7 +77,7 @@ // // buttonDelete // - this.buttonDelete.Location = new System.Drawing.Point(6, 108); + this.buttonDelete.Location = new System.Drawing.Point(12, 356); this.buttonDelete.Name = "buttonDelete"; this.buttonDelete.Size = new System.Drawing.Size(167, 35); this.buttonDelete.TabIndex = 7; @@ -80,7 +87,7 @@ // // buttonAdd // - this.buttonAdd.Location = new System.Drawing.Point(6, 30); + this.buttonAdd.Location = new System.Drawing.Point(12, 278); this.buttonAdd.Name = "buttonAdd"; this.buttonAdd.Size = new System.Drawing.Size(167, 35); this.buttonAdd.TabIndex = 6; @@ -93,22 +100,74 @@ this.pictureBoxCollection.Dock = System.Windows.Forms.DockStyle.Fill; this.pictureBoxCollection.Location = new System.Drawing.Point(0, 0); this.pictureBoxCollection.Name = "pictureBoxCollection"; - this.pictureBoxCollection.Size = new System.Drawing.Size(996, 444); + this.pictureBoxCollection.Size = new System.Drawing.Size(996, 434); this.pictureBoxCollection.TabIndex = 5; this.pictureBoxCollection.TabStop = false; // + // groupBoxSet + // + this.groupBoxSet.Controls.Add(this.textBoxStorageName); + this.groupBoxSet.Controls.Add(this.buttonAddSet); + this.groupBoxSet.Controls.Add(this.listBoxStorages); + this.groupBoxSet.Controls.Add(this.buttonRemoveSet); + this.groupBoxSet.Location = new System.Drawing.Point(6, 21); + this.groupBoxSet.Name = "groupBoxSet"; + this.groupBoxSet.Size = new System.Drawing.Size(179, 257); + this.groupBoxSet.TabIndex = 6; + this.groupBoxSet.TabStop = false; + this.groupBoxSet.Text = "Наборы"; + // + // textBoxStorageName + // + this.textBoxStorageName.Location = new System.Drawing.Point(6, 30); + this.textBoxStorageName.Name = "textBoxStorageName"; + this.textBoxStorageName.Size = new System.Drawing.Size(150, 31); + this.textBoxStorageName.TabIndex = 14; + // + // buttonAddSet + // + this.buttonAddSet.Location = new System.Drawing.Point(6, 65); + this.buttonAddSet.Name = "buttonAddSet"; + this.buttonAddSet.Size = new System.Drawing.Size(167, 35); + this.buttonAddSet.TabIndex = 12; + this.buttonAddSet.Text = "Добавить набор"; + this.buttonAddSet.UseVisualStyleBackColor = true; + this.buttonAddSet.Click += new System.EventHandler(this.ButtonAddObject_Click); + // + // listBoxStorages + // + this.listBoxStorages.FormattingEnabled = true; + this.listBoxStorages.ItemHeight = 25; + this.listBoxStorages.Location = new System.Drawing.Point(6, 106); + this.listBoxStorages.Name = "listBoxStorages"; + this.listBoxStorages.Size = new System.Drawing.Size(167, 104); + this.listBoxStorages.TabIndex = 13; + this.listBoxStorages.SelectedIndexChanged += new System.EventHandler(this.ListBoxObjects_SelectedIndexChanged); + // + // buttonRemoveSet + // + this.buttonRemoveSet.Location = new System.Drawing.Point(6, 216); + this.buttonRemoveSet.Name = "buttonRemoveSet"; + this.buttonRemoveSet.Size = new System.Drawing.Size(167, 35); + this.buttonRemoveSet.TabIndex = 11; + this.buttonRemoveSet.Text = "Удалить набор"; + this.buttonRemoveSet.UseVisualStyleBackColor = true; + this.buttonRemoveSet.Click += new System.EventHandler(this.ButtonDelObject_Click); + // // FormCraneCollection // this.AutoScaleDimensions = new System.Drawing.SizeF(10F, 25F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(996, 444); + this.ClientSize = new System.Drawing.Size(996, 434); this.Controls.Add(this.groupBoxInstruments); this.Controls.Add(this.pictureBoxCollection); this.Name = "FormCraneCollection"; - this.Text = "FormCraneCollection"; + this.Text = "Набор кранов"; this.groupBoxInstruments.ResumeLayout(false); this.groupBoxInstruments.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).EndInit(); + this.groupBoxSet.ResumeLayout(false); + this.groupBoxSet.PerformLayout(); this.ResumeLayout(false); } @@ -121,5 +180,10 @@ private Button buttonDelete; private Button buttonAdd; private PictureBox pictureBoxCollection; + private GroupBox groupBoxSet; + private TextBox textBoxStorageName; + private Button buttonAddSet; + private ListBox listBoxStorages; + private Button buttonRemoveSet; } } \ No newline at end of file diff --git a/HoistingCrane/HoistingCrane/FormCraneCollection.cs b/HoistingCrane/HoistingCrane/FormCraneCollection.cs index 5d71394..d5e5a98 100644 --- a/HoistingCrane/HoistingCrane/FormCraneCollection.cs +++ b/HoistingCrane/HoistingCrane/FormCraneCollection.cs @@ -6,21 +6,76 @@ namespace HoistingCrane { public partial class FormCraneCollection : Form { - private readonly CranesGenericCollection _cranes; + private readonly CranesGenericStorage _storage; public FormCraneCollection() { InitializeComponent(); - _cranes = new CranesGenericCollection(pictureBoxCollection.Width, pictureBoxCollection.Height); + _storage = new CranesGenericStorage(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 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]?.ShowCars(); + } + 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 ButtonAddCrane_Click(object sender, EventArgs e) { + if (listBoxStorages.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty]; + if (obj == null) + { + return; + } CraneVisual form = new(); if (form.ShowDialog() == DialogResult.OK) { - if (_cranes + form.SelectedCrane != -1) + if (obj + form.SelectedCrane != -1) { MessageBox.Show("Объект добавлен"); - pictureBoxCollection.Image = _cranes.ShowCars(); + pictureBoxCollection.Image = obj.ShowCars(); } else { @@ -32,16 +87,25 @@ namespace HoistingCrane private void ButtonRemoveCrane_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 (_cranes - pos) + if (obj - pos) { MessageBox.Show("Объект удален"); - pictureBoxCollection.Image = _cranes.ShowCars(); + pictureBoxCollection.Image = obj.ShowCars(); } else { @@ -49,10 +113,19 @@ namespace HoistingCrane } } - private void ButtonRefreshCollection_Click(object sender, EventArgs -e) + private void ButtonRefreshCollection_Click(object sender, EventArgs e) { - pictureBoxCollection.Image = _cranes.ShowCars(); + if (listBoxStorages.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? + string.Empty]; + if (obj == null) + { + return; + } + pictureBoxCollection.Image = obj.ShowCars(); } } diff --git a/HoistingCrane/HoistingCrane/SetGeneric.cs b/HoistingCrane/HoistingCrane/SetGeneric.cs index a08ab89..df483fb 100644 --- a/HoistingCrane/HoistingCrane/SetGeneric.cs +++ b/HoistingCrane/HoistingCrane/SetGeneric.cs @@ -6,55 +6,35 @@ { /// Массив объектов, которые храним - 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 crane) { - int pos = -1; - for (int i = 0; i < Count; i++) - { - if (_places[i] == null) - { - pos = i; - break; - } - } - if (pos != -1) - { - for (int i = pos; i > 0; i--) - { - _places[i] = _places[i - 1]; - } - } - _places[0] = crane; + _places.Insert(0, crane); return 0; } public int Insert(T crane, int position) { - if ((position < 0) && (position > Count)) return -1; - if (_places[position] != null) - { - for (int i = Count - 1; i >= position; i--) - { - if (_places[i - 1] != null) - _places[i] = _places[i - 1]; - } - } - _places[position] = crane; + if (position < 0 || position >= _maxCount|| Count >= _maxCount) + return -1; + _places.Insert(position, crane); return position; } /// Удаление объекта из набора с конкретной позиции public bool Remove(int position) { - if ((position < 0) || (position > Count)) return false; - _places[position] = null; + if ((position < 0) || (position > _maxCount)) return false; + _places.RemoveAt(position); return true; } /// Получение объекта из набора по позиции @@ -63,6 +43,34 @@ if ((position < 0) || (position > Count)) return null; return _places[position]; } + /// Получение объекта из набора по позиции + public T? this[int position] + { + get + { + if (position < 0 || position > _maxCount) + return null; + return _places[position]; + } + set + { + if (position < 0 || position > _maxCount) + return; + _places[position] = value; + } + } + /// Проход по списку + public IEnumerable GetCranes(int? maxCranes = null) + { + for (int i = 0; i < _places.Count; ++i) + { + yield return _places[i]; + if (maxCranes.HasValue && i == maxCranes.Value) + { + yield break; + } + } + } } }