From 489f116e75eb47522d4a9c838450421bdb8600d2 Mon Sep 17 00:00:00 2001 From: gg12 darfren Date: Sun, 15 Oct 2023 20:12:28 +0400 Subject: [PATCH] I think it's done --- .../FormMonorailCollection.Designer.cs | 82 ++++++++++++-- Monorail/Monorail/FormMonorailCollection.cs | 104 ++++++++++++++++-- Monorail/Monorail/MonorailForm.Designer.cs | 20 ++-- .../Monorail/MonorailGenericCollection.cs | 32 +++--- Monorail/Monorail/MonorailGenericStorage.cs | 51 +++++++++ Monorail/Monorail/SetGeneric.cs | 72 +++++++----- 6 files changed, 289 insertions(+), 72 deletions(-) create mode 100644 Monorail/Monorail/MonorailGenericStorage.cs diff --git a/Monorail/Monorail/FormMonorailCollection.Designer.cs b/Monorail/Monorail/FormMonorailCollection.Designer.cs index e3013e0..8e0899a 100644 --- a/Monorail/Monorail/FormMonorailCollection.Designer.cs +++ b/Monorail/Monorail/FormMonorailCollection.Designer.cs @@ -29,38 +29,96 @@ private void InitializeComponent() { toolGroupBox = new System.Windows.Forms.GroupBox(); + storageGroupBox = new System.Windows.Forms.GroupBox(); + listBoxStorages = new System.Windows.Forms.ListBox(); + delStorageButton = new System.Windows.Forms.Button(); + addStorageButton = new System.Windows.Forms.Button(); + storageAddNameBox = new System.Windows.Forms.MaskedTextBox(); maskedTextBox = new System.Windows.Forms.MaskedTextBox(); updateCollectionButton = new System.Windows.Forms.Button(); deleteMonorailButton = new System.Windows.Forms.Button(); addMonorailButton = new System.Windows.Forms.Button(); pictureBoxCollection = new System.Windows.Forms.PictureBox(); toolGroupBox.SuspendLayout(); + storageGroupBox.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); SuspendLayout(); // // toolGroupBox // + toolGroupBox.Anchor = System.Windows.Forms.AnchorStyles.Right; + toolGroupBox.Controls.Add(storageGroupBox); toolGroupBox.Controls.Add(maskedTextBox); toolGroupBox.Controls.Add(updateCollectionButton); toolGroupBox.Controls.Add(deleteMonorailButton); toolGroupBox.Controls.Add(addMonorailButton); - toolGroupBox.Location = new System.Drawing.Point(623, 12); + toolGroupBox.Location = new System.Drawing.Point(743, 12); toolGroupBox.Name = "toolGroupBox"; - toolGroupBox.Size = new System.Drawing.Size(227, 426); + toolGroupBox.Size = new System.Drawing.Size(227, 529); toolGroupBox.TabIndex = 0; toolGroupBox.TabStop = false; toolGroupBox.Text = "Инструменты"; // + // storageGroupBox + // + storageGroupBox.Controls.Add(listBoxStorages); + storageGroupBox.Controls.Add(delStorageButton); + storageGroupBox.Controls.Add(addStorageButton); + storageGroupBox.Controls.Add(storageAddNameBox); + storageGroupBox.Location = new System.Drawing.Point(0, 26); + storageGroupBox.Name = "storageGroupBox"; + storageGroupBox.Size = new System.Drawing.Size(227, 317); + storageGroupBox.TabIndex = 5; + storageGroupBox.TabStop = false; + storageGroupBox.Text = "Наборы"; + // + // listBoxStorages + // + listBoxStorages.FormattingEnabled = true; + listBoxStorages.ItemHeight = 20; + listBoxStorages.Location = new System.Drawing.Point(6, 94); + listBoxStorages.Name = "listBoxStorages"; + listBoxStorages.Size = new System.Drawing.Size(215, 184); + listBoxStorages.TabIndex = 4; + listBoxStorages.SelectedIndexChanged += listBoxStorages_SelectedIndexChanged; + // + // delStorageButton + // + delStorageButton.Location = new System.Drawing.Point(6, 282); + delStorageButton.Name = "delStorageButton"; + delStorageButton.Size = new System.Drawing.Size(215, 29); + delStorageButton.TabIndex = 3; + delStorageButton.Text = "Удалить набор"; + delStorageButton.UseVisualStyleBackColor = true; + delStorageButton.Click += delStorageButton_Click; + // + // addStorageButton + // + addStorageButton.Location = new System.Drawing.Point(6, 59); + addStorageButton.Name = "addStorageButton"; + addStorageButton.Size = new System.Drawing.Size(215, 29); + addStorageButton.TabIndex = 1; + addStorageButton.Text = "Добавить набор"; + addStorageButton.UseVisualStyleBackColor = true; + addStorageButton.Click += addStorageButton_Click; + // + // storageAddNameBox + // + storageAddNameBox.Location = new System.Drawing.Point(6, 26); + storageAddNameBox.Name = "storageAddNameBox"; + storageAddNameBox.Size = new System.Drawing.Size(215, 27); + storageAddNameBox.TabIndex = 0; + // // maskedTextBox // - maskedTextBox.Location = new System.Drawing.Point(6, 72); + maskedTextBox.Location = new System.Drawing.Point(6, 404); maskedTextBox.Name = "maskedTextBox"; maskedTextBox.Size = new System.Drawing.Size(125, 27); maskedTextBox.TabIndex = 4; // // updateCollectionButton // - updateCollectionButton.Location = new System.Drawing.Point(6, 151); + updateCollectionButton.Location = new System.Drawing.Point(6, 483); updateCollectionButton.Name = "updateCollectionButton"; updateCollectionButton.Size = new System.Drawing.Size(215, 40); updateCollectionButton.TabIndex = 3; @@ -70,7 +128,7 @@ // // deleteMonorailButton // - deleteMonorailButton.Location = new System.Drawing.Point(6, 105); + deleteMonorailButton.Location = new System.Drawing.Point(6, 437); deleteMonorailButton.Name = "deleteMonorailButton"; deleteMonorailButton.Size = new System.Drawing.Size(215, 40); deleteMonorailButton.TabIndex = 2; @@ -80,7 +138,7 @@ // // addMonorailButton // - addMonorailButton.Location = new System.Drawing.Point(6, 26); + addMonorailButton.Location = new System.Drawing.Point(6, 358); addMonorailButton.Name = "addMonorailButton"; addMonorailButton.Size = new System.Drawing.Size(215, 40); addMonorailButton.TabIndex = 0; @@ -92,7 +150,7 @@ // pictureBoxCollection.Location = new System.Drawing.Point(12, 12); pictureBoxCollection.Name = "pictureBoxCollection"; - pictureBoxCollection.Size = new System.Drawing.Size(605, 426); + pictureBoxCollection.Size = new System.Drawing.Size(725, 529); pictureBoxCollection.TabIndex = 1; pictureBoxCollection.TabStop = false; // @@ -100,13 +158,15 @@ // AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - ClientSize = new System.Drawing.Size(862, 450); + ClientSize = new System.Drawing.Size(982, 553); Controls.Add(pictureBoxCollection); Controls.Add(toolGroupBox); Name = "FormMonorailCollection"; Text = "FormMonorailCollection"; toolGroupBox.ResumeLayout(false); toolGroupBox.PerformLayout(); + storageGroupBox.ResumeLayout(false); + storageGroupBox.PerformLayout(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit(); ResumeLayout(false); } @@ -119,5 +179,11 @@ private System.Windows.Forms.Button addMonorailButton; private System.Windows.Forms.PictureBox pictureBoxCollection; private System.Windows.Forms.MaskedTextBox maskedTextBox; + private System.Windows.Forms.GroupBox storageGroupBox; + private System.Windows.Forms.MaskedTextBox maskedTextBox1; + private System.Windows.Forms.Button addStorageButton; + private System.Windows.Forms.MaskedTextBox storageAddNameBox; + private System.Windows.Forms.Button delStorageButton; + private System.Windows.Forms.ListBox listBoxStorages; } } \ No newline at end of file diff --git a/Monorail/Monorail/FormMonorailCollection.cs b/Monorail/Monorail/FormMonorailCollection.cs index 136d0df..0c09b96 100644 --- a/Monorail/Monorail/FormMonorailCollection.cs +++ b/Monorail/Monorail/FormMonorailCollection.cs @@ -15,24 +15,56 @@ namespace Monorail { public partial class FormMonorailCollection : Form { - private readonly MonorailGenericCollection _monorails; + private readonly MonorailGenericStorage _storage; public FormMonorailCollection() { InitializeComponent(); - _monorails = new MonorailGenericCollection(pictureBoxCollection.Width, pictureBoxCollection.Height); + _storage = new MonorailGenericStorage(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 addMonorailButton_Click(object sender, EventArgs e) { + if (listBoxStorages.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? + string.Empty]; + if (obj == null) + { + return; + } MonorailForm form = new(); if (form.ShowDialog() == DialogResult.OK) { - if (_monorails + form.SelectedMonorail != null) + if (obj + form.SelectedMonorail) { MessageBox.Show("Объект добавлен"); - pictureBoxCollection.Image = _monorails.ShowMonorails(); + pictureBoxCollection.Image = obj.ShowMonorails(); } else { @@ -44,16 +76,26 @@ DrawningObjectMonorail>(pictureBoxCollection.Width, pictureBoxCollection.Height) private void deleteMonorailButton_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) + MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) { return; } int pos = Convert.ToInt32(maskedTextBox.Text); - if (_monorails - pos != null) + if (obj - pos != null) { MessageBox.Show("Объект удален"); - pictureBoxCollection.Image = _monorails.ShowMonorails(); + pictureBoxCollection.Image = obj.ShowMonorails(); } else { @@ -63,8 +105,52 @@ MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) private void updateCollectionButton_Click(object sender, EventArgs e) { - pictureBoxCollection.Image = _monorails.ShowMonorails(); + if (listBoxStorages.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? + string.Empty]; + if (obj == null) + { + return; + } + pictureBoxCollection.Image = obj.ShowMonorails(); } + + private void listBoxStorages_SelectedIndexChanged(object sender, EventArgs e) + { + pictureBoxCollection.Image = +_storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowMonorails(); + } + + private void delStorageButton_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 addStorageButton_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(storageAddNameBox.Text)) + { + MessageBox.Show("Не все данные заполнены", "Ошибка", + MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _storage.AddSet(storageAddNameBox.Text); + ReloadObjects(); + } } } diff --git a/Monorail/Monorail/MonorailForm.Designer.cs b/Monorail/Monorail/MonorailForm.Designer.cs index 78aec86..c955859 100644 --- a/Monorail/Monorail/MonorailForm.Designer.cs +++ b/Monorail/Monorail/MonorailForm.Designer.cs @@ -48,7 +48,7 @@ namespace Monorail pictureBoxMonorail.Dock = System.Windows.Forms.DockStyle.Fill; pictureBoxMonorail.Location = new System.Drawing.Point(0, 0); pictureBoxMonorail.Name = "pictureBoxMonorail"; - pictureBoxMonorail.Size = new System.Drawing.Size(882, 453); + pictureBoxMonorail.Size = new System.Drawing.Size(725, 529); pictureBoxMonorail.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize; pictureBoxMonorail.TabIndex = 0; pictureBoxMonorail.TabStop = false; @@ -56,7 +56,7 @@ namespace Monorail // buttonCreate // buttonCreate.Anchor = System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left; - buttonCreate.Location = new System.Drawing.Point(198, 401); + buttonCreate.Location = new System.Drawing.Point(198, 477); buttonCreate.Name = "buttonCreate"; buttonCreate.Size = new System.Drawing.Size(180, 40); buttonCreate.TabIndex = 1; @@ -69,7 +69,7 @@ namespace Monorail buttonLeft.Anchor = System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right; buttonLeft.BackgroundImage = (System.Drawing.Image)resources.GetObject("buttonLeft.BackgroundImage"); buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; - buttonLeft.Location = new System.Drawing.Point(768, 411); + buttonLeft.Location = new System.Drawing.Point(611, 487); buttonLeft.Name = "buttonLeft"; buttonLeft.Size = new System.Drawing.Size(30, 30); buttonLeft.TabIndex = 2; @@ -81,7 +81,7 @@ namespace Monorail buttonRight.Anchor = System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right; buttonRight.BackgroundImage = (System.Drawing.Image)resources.GetObject("buttonRight.BackgroundImage"); buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; - buttonRight.Location = new System.Drawing.Point(840, 411); + buttonRight.Location = new System.Drawing.Point(683, 487); buttonRight.Name = "buttonRight"; buttonRight.Size = new System.Drawing.Size(30, 30); buttonRight.TabIndex = 3; @@ -93,7 +93,7 @@ namespace Monorail buttonUp.Anchor = System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right; buttonUp.BackgroundImage = (System.Drawing.Image)resources.GetObject("buttonUp.BackgroundImage"); buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; - buttonUp.Location = new System.Drawing.Point(804, 375); + buttonUp.Location = new System.Drawing.Point(647, 451); buttonUp.Name = "buttonUp"; buttonUp.Size = new System.Drawing.Size(30, 30); buttonUp.TabIndex = 4; @@ -105,7 +105,7 @@ namespace Monorail buttonDown.Anchor = System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right; buttonDown.BackgroundImage = (System.Drawing.Image)resources.GetObject("buttonDown.BackgroundImage"); buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; - buttonDown.Location = new System.Drawing.Point(804, 411); + buttonDown.Location = new System.Drawing.Point(647, 487); buttonDown.Name = "buttonDown"; buttonDown.Size = new System.Drawing.Size(30, 30); buttonDown.TabIndex = 5; @@ -134,7 +134,8 @@ namespace Monorail // // buttonCreateAdvanced // - buttonCreateAdvanced.Location = new System.Drawing.Point(12, 401); + buttonCreateAdvanced.Anchor = System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left; + buttonCreateAdvanced.Location = new System.Drawing.Point(12, 477); buttonCreateAdvanced.Name = "buttonCreateAdvanced"; buttonCreateAdvanced.Size = new System.Drawing.Size(180, 40); buttonCreateAdvanced.TabIndex = 8; @@ -144,7 +145,8 @@ namespace Monorail // // buttonSelectMonorail // - buttonSelectMonorail.Location = new System.Drawing.Point(384, 401); + buttonSelectMonorail.Anchor = System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left; + buttonSelectMonorail.Location = new System.Drawing.Point(384, 477); buttonSelectMonorail.Name = "buttonSelectMonorail"; buttonSelectMonorail.Size = new System.Drawing.Size(180, 40); buttonSelectMonorail.TabIndex = 9; @@ -156,7 +158,7 @@ namespace Monorail // AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - ClientSize = new System.Drawing.Size(882, 453); + ClientSize = new System.Drawing.Size(725, 529); Controls.Add(buttonSelectMonorail); Controls.Add(buttonCreateAdvanced); Controls.Add(buttonStep); diff --git a/Monorail/Monorail/MonorailGenericCollection.cs b/Monorail/Monorail/MonorailGenericCollection.cs index 7361407..2b566e2 100644 --- a/Monorail/Monorail/MonorailGenericCollection.cs +++ b/Monorail/Monorail/MonorailGenericCollection.cs @@ -32,25 +32,26 @@ namespace Monorail.Generics _collection = new SetGeneric(width * height); } - public static int operator +(MonorailGenericCollection collect, T? obj) + public static bool operator +(MonorailGenericCollection collect, T? obj) { if (obj == null) - return -1; - return collect?._collection.Insert(obj) ?? -1; + return false; + + return collect?._collection.Insert(obj) ?? false; } - public static bool operator -(MonorailGenericCollection collect, int + public static T? operator -(MonorailGenericCollection collect, int pos) { - T? obj = collect._collection.Get(pos); + T? obj = collect._collection[pos]; if (obj != null) - return collect._collection.Remove(pos); - return false; + collect._collection.Remove(pos); + return obj; } public U? GetU(int pos) { - return (U?)_collection.Get(pos)?.GetMoveableObject; + return (U?)_collection[pos]?.GetMoveableObject; } public Bitmap ShowMonorails() @@ -81,18 +82,15 @@ pos) private void DrawObjects(Graphics g) { - for (int i = 0; i < _collection.Count; i++) + int i = 0; + foreach(var monorail in _collection.GetMonorails()) { - DrawningMonorail monorail = _collection.Get(i); - if (monorail != null) - { - int inRow = _pictureWidth / _placeSizeWidth; - monorail.SetPosition(i % inRow * _placeSizeWidth, (_collection.Count / inRow - 1 - i / inRow) * _placeSizeHeight ); - monorail.DrawTransport(g); - } + int inRow = _pictureWidth / _placeSizeWidth; + monorail.SetPosition(i % inRow * _placeSizeWidth, _pictureHeight - _pictureHeight % _placeSizeHeight - (i / inRow +1) *_placeSizeHeight ); + monorail.DrawTransport(g); + i++; } } - } } diff --git a/Monorail/Monorail/MonorailGenericStorage.cs b/Monorail/Monorail/MonorailGenericStorage.cs new file mode 100644 index 0000000..88b1715 --- /dev/null +++ b/Monorail/Monorail/MonorailGenericStorage.cs @@ -0,0 +1,51 @@ +using Monorail.DrawningObjects; +using Monorail.Generics; +using Monorail.MovementStrategy; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Monorail +{ + internal class MonorailGenericStorage + { + readonly Dictionary> _monorailStorages; + public List Keys => _monorailStorages.Keys.ToList(); + + private readonly int _pictureWidth; + + private readonly int _pictureHeight; + + public MonorailGenericStorage(int pictureWidth, int pictureHeight) + { + _monorailStorages = new Dictionary>(); + _pictureWidth = pictureWidth; + _pictureHeight = pictureHeight; + } + + public void AddSet(string name) + { + _monorailStorages.Add(name, new MonorailGenericCollection (_pictureWidth, _pictureHeight)); + } + + public void DelSet(string name) + { + if (_monorailStorages.ContainsKey(name) == null) + return; + _monorailStorages.Remove(name); + } + + public MonorailGenericCollection? this[string ind] + { + get + { + if (_monorailStorages.ContainsKey(ind)) + return _monorailStorages[ind]; + return null; + } + } + } +} diff --git a/Monorail/Monorail/SetGeneric.cs b/Monorail/Monorail/SetGeneric.cs index 5e314c2..421b2ef 100644 --- a/Monorail/Monorail/SetGeneric.cs +++ b/Monorail/Monorail/SetGeneric.cs @@ -9,52 +9,66 @@ namespace Monorail.Generics internal class SetGeneric 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 monorail) + public bool Insert(T monorail) { - if (_places[Count-1] != null) - return -1; - return Insert(monorail, 0); + if (_places.Count == _maxCount) + return false; + Insert(monorail, 0); + return true; + } - public int Insert(T monorail, int position) + public bool Insert(T monorail, int position) { - if (!(position >= 0 && position < Count)) - return -1; - if (_places[position] != null) - { - int ind = position; - while (ind < Count && _places[ind] != null) - ind++; - if (ind == Count) - return -1; - for (int i = ind - 1; i >= position; i--) - _places[i + 1] = _places[i]; - } - _places[position] = monorail; - return position; + if (!(position >= 0 && position <= Count && _places.Count < _maxCount)) + return false; + _places.Insert(position, monorail); + return true; } public bool Remove(int position) { - if (!(position >= 0 && position < Count) || _places[position] == null) + if (!(position >= 0 && position < Count)) return false; - _places[position] = null; + _places.RemoveAt(position); return true; } - - public T? Get(int position) + public T? this[int position] { - 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 < Count && _places.Count < _maxCount)) + return; + _places.Insert(position, value); + return; + } } + + public IEnumerable GetMonorails(int? maxMonorails = null) + { + for (int i = 0; i < _places.Count; ++i) + { + yield return _places[i]; + if (maxMonorails.HasValue && i == maxMonorails.Value) + { + yield break; + } + } + } + } }