diff --git a/lainer/Lainer1/FormLainerCollection.Designer.cs b/lainer/Lainer1/FormLainerCollection.Designer.cs index 7bc9105..e4f5a11 100644 --- a/lainer/Lainer1/FormLainerCollection.Designer.cs +++ b/lainer/Lainer1/FormLainerCollection.Designer.cs @@ -33,6 +33,10 @@ ButtonRemove = new Button(); ButtonRefresh = new Button(); maskedTextBoxNumber = new MaskedTextBox(); + ButtonAddObject = new Button(); + textBoxStorageName = new TextBox(); + listBoxStorages = new ListBox(); + ButtonDelObject = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); SuspendLayout(); // @@ -40,15 +44,15 @@ // pictureBoxCollection.Location = new Point(16, 14); pictureBoxCollection.Name = "pictureBoxCollection"; - pictureBoxCollection.Size = new Size(781, 297); + pictureBoxCollection.Size = new Size(815, 434); pictureBoxCollection.TabIndex = 0; pictureBoxCollection.TabStop = false; // // ButtonAddLainer // - ButtonAddLainer.Location = new Point(813, 40); + ButtonAddLainer.Location = new Point(872, 282); ButtonAddLainer.Name = "ButtonAddLainer"; - ButtonAddLainer.Size = new Size(154, 52); + ButtonAddLainer.Size = new Size(154, 32); ButtonAddLainer.TabIndex = 1; ButtonAddLainer.Text = "добавить лайнер"; ButtonAddLainer.UseVisualStyleBackColor = true; @@ -56,9 +60,9 @@ // // ButtonRemove // - ButtonRemove.Location = new Point(812, 158); + ButtonRemove.Location = new Point(887, 384); ButtonRemove.Name = "ButtonRemove"; - ButtonRemove.Size = new Size(139, 65); + ButtonRemove.Size = new Size(139, 37); ButtonRemove.TabIndex = 2; ButtonRemove.Text = "удалить лайнер"; ButtonRemove.UseVisualStyleBackColor = true; @@ -66,26 +70,67 @@ // // ButtonRefresh // - ButtonRefresh.Location = new Point(803, 247); + ButtonRefresh.Location = new Point(849, 449); ButtonRefresh.Name = "ButtonRefresh"; - ButtonRefresh.Size = new Size(164, 54); + ButtonRefresh.Size = new Size(164, 29); ButtonRefresh.TabIndex = 3; ButtonRefresh.Text = "обновить коллекцию"; ButtonRefresh.UseVisualStyleBackColor = true; - ButtonRefresh.Click += ButtonRefresh_Click; + ButtonRefresh.Click += ButtonRefreshCollection_Click; // // maskedTextBoxNumber // - maskedTextBoxNumber.Location = new Point(826, 111); + maskedTextBoxNumber.Location = new Point(888, 351); maskedTextBoxNumber.Name = "maskedTextBoxNumber"; maskedTextBoxNumber.Size = new Size(125, 27); maskedTextBoxNumber.TabIndex = 4; // + // ButtonAddObject + // + ButtonAddObject.Location = new Point(856, 55); + ButtonAddObject.Name = "ButtonAddObject"; + ButtonAddObject.Size = new Size(170, 29); + ButtonAddObject.TabIndex = 5; + ButtonAddObject.Text = "добавить набор"; + ButtonAddObject.UseVisualStyleBackColor = true; + ButtonAddObject.Click += ButtonAddObject_Click; + // + // textBoxStorageName + // + textBoxStorageName.Location = new Point(892, 22); + textBoxStorageName.Name = "textBoxStorageName"; + textBoxStorageName.Size = new Size(125, 27); + textBoxStorageName.TabIndex = 6; + // + // listBoxStorages + // + listBoxStorages.FormattingEnabled = true; + listBoxStorages.ItemHeight = 20; + listBoxStorages.Location = new Point(876, 90); + listBoxStorages.Name = "listBoxStorages"; + listBoxStorages.Size = new Size(150, 104); + listBoxStorages.TabIndex = 7; + listBoxStorages.SelectedIndexChanged += listBoxStorages_SelectedIndexChanged; + // + // ButtonDelObject + // + ButtonDelObject.Location = new Point(888, 210); + ButtonDelObject.Name = "ButtonDelObject"; + ButtonDelObject.Size = new Size(138, 29); + ButtonDelObject.TabIndex = 8; + ButtonDelObject.Text = "удалить набор"; + ButtonDelObject.UseVisualStyleBackColor = true; + ButtonDelObject.Click += ButtonDelObject_Click; + // // FormLainerCollection // AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(1003, 450); + ClientSize = new Size(1038, 490); + Controls.Add(ButtonDelObject); + Controls.Add(listBoxStorages); + Controls.Add(textBoxStorageName); + Controls.Add(ButtonAddObject); Controls.Add(maskedTextBoxNumber); Controls.Add(ButtonRefresh); Controls.Add(ButtonRemove); @@ -105,5 +150,9 @@ private Button ButtonRemove; private Button ButtonRefresh; private MaskedTextBox maskedTextBoxNumber; + private Button ButtonAddObject; + private TextBox textBoxStorageName; + private ListBox listBoxStorages; + private Button ButtonDelObject; } } \ No newline at end of file diff --git a/lainer/Lainer1/FormLainerCollection.cs b/lainer/Lainer1/FormLainerCollection.cs index c153019..aa5b9d3 100644 --- a/lainer/Lainer1/FormLainerCollection.cs +++ b/lainer/Lainer1/FormLainerCollection.cs @@ -1,4 +1,14 @@ -using ProjectLainer.DrawningObjects; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +using ProjectLainer.DrawningObjects; using ProjectLainer.Generics; using ProjectLainer.MovementStrategy; @@ -6,21 +16,73 @@ namespace ProjectLainer { public partial class FormLainerCollection : Form { - private readonly LainersGenericCollection _lainers; + private readonly LainersGenericStorage _storage; public FormLainerCollection() { InitializeComponent(); - _lainers = new LainersGenericCollection(pictureBoxCollection.Width, pictureBoxCollection.Height); + _storage = new LainersGenericStorage(pictureBoxCollection.Width, + pictureBoxCollection.Height); + } + private void ReloadObjects() + { + int index = listBoxStorages.SelectedIndex; + listBoxStorages.Items.Clear(); + + foreach (string key in _storage.Keys) + { + listBoxStorages.Items.Add(key); + 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 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 ButtonAddLainer_Click(object sender, EventArgs e) { + if (listBoxStorages.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty]; + if (obj == null) + { + return; + } LainerForm form = new(); if (form.ShowDialog() == DialogResult.OK) { - if (_lainers + form.SelectedLainer) + if (obj + form.SelectedLainer) { MessageBox.Show("Объект добавлен"); - pictureBoxCollection.Image = _lainers.ShowLainers(); + pictureBoxCollection.Image = obj.ShowLainers(); } else { @@ -30,30 +92,49 @@ namespace ProjectLainer } private void ButtonRemove_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; } - try + int pos = Convert.ToInt32(maskedTextBoxNumber.Text); + if (obj - pos != null) { - int pos = Convert.ToInt32(maskedTextBoxNumber.Text); - if (_lainers - pos == true) - { - MessageBox.Show("Объект удален"); - pictureBoxCollection.Image = _lainers.ShowLainers(); - } - else - { - MessageBox.Show("Не удалось удалить объект"); - } + MessageBox.Show("Объект удален"); + pictureBoxCollection.Image = obj.ShowLainers(); + } + else + { + MessageBox.Show("Не удалось удалить объект"); } - catch { MessageBox.Show("Не удалось удалить объект"); } - } - private void ButtonRefresh_Click(object sender, EventArgs e) + private void ButtonRefreshCollection_Click(object sender, EventArgs e) { - pictureBoxCollection.Image = _lainers.ShowLainers(); + if (listBoxStorages.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? + string.Empty]; + if (obj == null) + { + return; + } + pictureBoxCollection.Image = obj.ShowLainers(); + } + private void listBoxStorages_SelectedIndexChanged(object sender, EventArgs e) + { + pictureBoxCollection.Image = _storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowLainers(); } } } diff --git a/lainer/Lainer1/LainerGenericCollection.cs b/lainer/Lainer1/LainerGenericCollection.cs index 37a90d6..3939af8 100644 --- a/lainer/Lainer1/LainerGenericCollection.cs +++ b/lainer/Lainer1/LainerGenericCollection.cs @@ -1,24 +1,17 @@ using ProjectLainer.MovementStrategy; using ProjectLainer.DrawningObjects; -using System.Runtime.CompilerServices; namespace ProjectLainer.Generics { internal class LainersGenericCollection - where T : DrawingLainer + where T : DrawingEntity where U : IMoveableObject { - private readonly int _pictureWidth; - private readonly int _pictureHeight; - private readonly int _placeSizeWidth = 210; - private readonly int _placeSizeHeight = 90; - - public readonly SetGeneric _collection; - + private readonly SetGeneric _collection; public LainersGenericCollection(int picWidth, int picHeight) { int width = picWidth / _placeSizeWidth; @@ -27,40 +20,25 @@ namespace ProjectLainer.Generics _pictureHeight = picHeight; _collection = new SetGeneric(width * height); } - public static bool operator +(LainersGenericCollection collect, T? obj) { if (obj != null && collect != null) - { - var value = collect._collection.Insert(obj); - if(value <= collect._collection.Count && value >= 0) - { - return true; - } - else { return false; } - } + return collect._collection.Insert(obj); return false; - } - public static bool? operator -(LainersGenericCollection collect, int - pos) + public static T? operator -(LainersGenericCollection collect, int pos) { - T? obj = collect?._collection.Get(pos); - if (obj != null && collect != null) + T? obj = collect._collection[pos]; + if (obj != null) { - return collect._collection.Remove(pos); + collect._collection.Remove(pos); } - else - { - return null; - } - } - + return obj; + } public U? GetU(int pos) { - return (U?)_collection.Get(pos)?.GetMoveableObject; + return (U?)_collection[pos]?.GetMoveableObject; } - public Bitmap ShowLainers() { Bitmap bmp = new(_pictureWidth, _pictureHeight); @@ -69,7 +47,6 @@ namespace ProjectLainer.Generics DrawObjects(gr); return bmp; } - private void DrawBackground(Graphics g) { Pen pen = new(Color.Black, 3); @@ -77,7 +54,7 @@ namespace ProjectLainer.Generics { for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j) - { + {//линия рамзетки места g.DrawLine(pen, i * _placeSizeWidth, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight); @@ -88,17 +65,19 @@ namespace ProjectLainer.Generics } private void DrawObjects(Graphics g) { - - for (int i = 0; i < _collection.Count; i++) + int i = 0; + foreach (var lainer in _collection.GetLainers()) { - DrawingLainer? lainer = _collection.Get(i); if (lainer != null) { - int inRow = _pictureWidth / _placeSizeWidth; - + int inRow = _pictureWidth / _placeSizeWidth; + lainer.SetPosition((inRow - 1 - (i % inRow)) * _placeSizeWidth, i / inRow * _placeSizeHeight); lainer.DrawTransport(g); + + } + i++; } } } diff --git a/lainer/Lainer1/LainersGenericStorage.cs b/lainer/Lainer1/LainersGenericStorage.cs new file mode 100644 index 0000000..f5cbae6 --- /dev/null +++ b/lainer/Lainer1/LainersGenericStorage.cs @@ -0,0 +1,47 @@ +using ProjectLainer.DrawningObjects; +using ProjectLainer.MovementStrategy; +namespace ProjectLainer.Generics +{ + /// + /// Класс для хранения коллекции + /// + internal class LainersGenericStorage + { + readonly Dictionary> _lainerStorages; + public List Keys => _lainerStorages.Keys.ToList(); + private readonly int _pictureWidth; + private readonly int _pictureHeight; + public LainersGenericStorage(int pictureWidth, int pictureHeight) + { + _lainerStorages = new Dictionary>(); + _pictureWidth = pictureWidth; + _pictureHeight = pictureHeight; + } + public void AddSet(string name) + { + if (!_lainerStorages.ContainsKey(name)) + { + _lainerStorages.Add(name, new LainersGenericCollection(_pictureWidth, _pictureHeight)); + } + } + public void DelSet(string name) + { + if (_lainerStorages.ContainsKey(name)) + { + _lainerStorages.Remove(name); + } + } + public LainersGenericCollection? this[string ind] + { + get + { + if (_lainerStorages.ContainsKey(ind)) + { + return _lainerStorages[ind]; + } + return null; + } + } + } +} + diff --git a/lainer/Lainer1/SetGeneric.cs b/lainer/Lainer1/SetGeneric.cs index 6433123..fd73178 100644 --- a/lainer/Lainer1/SetGeneric.cs +++ b/lainer/Lainer1/SetGeneric.cs @@ -1,60 +1,74 @@ -namespace ProjectLainer.Generics +using System.Numerics; + +namespace ProjectLainer.Generics { 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(count); } - - public int Insert(T lainer) + public bool Insert(T lainer) { - if (_places[Count - 1] != null) - return Count; - return Insert(lainer, 0); + if (_places.Count == _maxCount) + { + return false; + } + Insert(lainer, 0); + return true; } - public int Insert(T lainer, int position) + public bool Insert(T lainer, int position) { - if (position < 0 || position >= Count) + if (position < 0 || position > Count || _places.Count >= _maxCount) { - return position; + return false; } - if (_places[position] != null) - { - int ind = position; - while (ind < Count && _places[ind] != null) - ind++; - if (ind == Count) - return Count; - for (int i = ind - 1; i >= position; i--) - _places[i + 1] = _places[i]; - } - _places[position] = lainer; - return position; + _places.Insert(position, lainer); + return true; } public bool Remove(int position) { - if (position < 0 ) + if (position < 0 || position >= Count) { return false; } - if(position >= Count) - { - return false; - } - _places[position] = null; + _places.RemoveAt(position); return true; } - public T? Get(int position) + public T? this[int position] { - if(position < Count && position >= 0) - { + get + { + if(position < 0 || position >= Count) + { + return null; + } return _places[position]; } - return null; + set + { + if (position < 0 || position >= Count || Count >= _maxCount) + { + return; + } + _places.Insert(position, value); + } + } + public IEnumerable GetLainers(int? maxLainers = null) + { + for (int i = 0; i < _places.Count; ++i) + { + yield return _places[i]; + if (maxLainers.HasValue && i == maxLainers.Value) + { + yield break; + } + } } } }