From b7d80bf20fe71e2ae3bab163f68c6e962b444588 Mon Sep 17 00:00:00 2001 From: Marselchi Date: Fri, 24 Nov 2023 12:49:48 +0400 Subject: [PATCH] =?UTF-8?q?=D0=A4=D1=83=D0=BB=D0=BB=20=D0=BB=D0=B0=D0=B1?= =?UTF-8?q?=D0=B0=204?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Liner/FormLinerCollection.Designer.cs | 78 ++++++++++-- Liner/FormLinerCollection.cs | 125 +++++++++++++++++-- Liner/Generics/LinerGenericCollection.cs | 15 ++- Liner/Generics/LinersGenericStorage.cs | 80 ++++++++++++ Liner/Generics/SetGeneric.cs | 88 +++++++------ Liner/MainScreen.cs | 9 +- Liner/MovingStrategies/DrawingObjectLiner.cs | 1 - Liner/Program.cs | 2 +- 8 files changed, 328 insertions(+), 70 deletions(-) create mode 100644 Liner/Generics/LinersGenericStorage.cs diff --git a/Liner/FormLinerCollection.Designer.cs b/Liner/FormLinerCollection.Designer.cs index 06a7a37..8e30369 100644 --- a/Liner/FormLinerCollection.Designer.cs +++ b/Liner/FormLinerCollection.Designer.cs @@ -29,17 +29,24 @@ private void InitializeComponent() { groupBoxTools = new GroupBox(); + Storages = new GroupBox(); + listBoxStorages = new ListBox(); + buttonDeleteSet = new Button(); + textBoxStorageName = new TextBox(); + buttonAddStorage = new Button(); buttonRefreshCollection = new Button(); buttonDeleteLiner = new Button(); textBoxNumber = new TextBox(); buttonAddLiner = new Button(); pictureBoxCollection = new PictureBox(); groupBoxTools.SuspendLayout(); + Storages.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); SuspendLayout(); // // groupBoxTools // + groupBoxTools.Controls.Add(Storages); groupBoxTools.Controls.Add(buttonRefreshCollection); groupBoxTools.Controls.Add(buttonDeleteLiner); groupBoxTools.Controls.Add(textBoxNumber); @@ -51,42 +58,92 @@ groupBoxTools.TabStop = false; groupBoxTools.Text = "Tools"; // + // Storages + // + Storages.Controls.Add(listBoxStorages); + Storages.Controls.Add(buttonDeleteSet); + Storages.Controls.Add(textBoxStorageName); + Storages.Controls.Add(buttonAddStorage); + Storages.Location = new Point(7, 19); + Storages.Name = "Storages"; + Storages.Size = new Size(200, 276); + Storages.TabIndex = 4; + Storages.TabStop = false; + Storages.Text = "Sets"; + // + // listBoxStorages + // + listBoxStorages.FormattingEnabled = true; + listBoxStorages.ItemHeight = 15; + listBoxStorages.Location = new Point(6, 113); + listBoxStorages.Name = "listBoxStorages"; + listBoxStorages.Size = new Size(187, 109); + listBoxStorages.TabIndex = 4; + listBoxStorages.SelectedIndexChanged += ListBoxObjects_SelectedIndexChanged; + // + // buttonDeleteSet + // + buttonDeleteSet.Location = new Point(10, 228); + buttonDeleteSet.Name = "buttonDeleteSet"; + buttonDeleteSet.Size = new Size(184, 30); + buttonDeleteSet.TabIndex = 3; + buttonDeleteSet.Text = "Delete Set"; + buttonDeleteSet.UseVisualStyleBackColor = true; + buttonDeleteSet.Click += ButtonRemoveObject_Click; + // + // textBoxStorageName + // + textBoxStorageName.Location = new Point(34, 39); + textBoxStorageName.Name = "textBoxStorageName"; + textBoxStorageName.Size = new Size(139, 23); + textBoxStorageName.TabIndex = 2; + // + // buttonAddStorage + // + buttonAddStorage.Location = new Point(10, 68); + buttonAddStorage.Name = "buttonAddStorage"; + buttonAddStorage.Size = new Size(184, 30); + buttonAddStorage.TabIndex = 1; + buttonAddStorage.Text = "Add Set"; + buttonAddStorage.UseVisualStyleBackColor = true; + buttonAddStorage.Click += ButtonAddObject_Click; + // // buttonRefreshCollection // - buttonRefreshCollection.Location = new Point(11, 258); + buttonRefreshCollection.Location = new Point(11, 501); buttonRefreshCollection.Name = "buttonRefreshCollection"; buttonRefreshCollection.Size = new Size(194, 40); buttonRefreshCollection.TabIndex = 3; buttonRefreshCollection.Text = "Refresh Collection"; buttonRefreshCollection.UseVisualStyleBackColor = true; - buttonRefreshCollection.Click += buttonRefreshCollection_Click; + buttonRefreshCollection.Click += ButtonRefreshCollection_Click; // // buttonDeleteLiner // - buttonDeleteLiner.Location = new Point(11, 117); + buttonDeleteLiner.Location = new Point(11, 408); buttonDeleteLiner.Name = "buttonDeleteLiner"; buttonDeleteLiner.Size = new Size(194, 40); buttonDeleteLiner.TabIndex = 2; buttonDeleteLiner.Text = "Delete Liner"; buttonDeleteLiner.UseVisualStyleBackColor = true; - buttonDeleteLiner.Click += buttonRemoveLiner_Click; + buttonDeleteLiner.Click += ButtonRemoveLiner_Click; // // textBoxNumber // - textBoxNumber.Location = new Point(41, 77); + textBoxNumber.Location = new Point(41, 368); textBoxNumber.Name = "textBoxNumber"; textBoxNumber.Size = new Size(139, 23); textBoxNumber.TabIndex = 1; // // buttonAddLiner // - buttonAddLiner.Location = new Point(11, 22); + buttonAddLiner.Location = new Point(11, 313); buttonAddLiner.Name = "buttonAddLiner"; buttonAddLiner.Size = new Size(194, 40); buttonAddLiner.TabIndex = 0; buttonAddLiner.Text = "Add Liner"; buttonAddLiner.UseVisualStyleBackColor = true; - buttonAddLiner.Click += buttonAddLiner_Click; + buttonAddLiner.Click += ButtonAddLiner_Click; // // pictureBoxCollection // @@ -107,6 +164,8 @@ Text = "Liner Collection"; groupBoxTools.ResumeLayout(false); groupBoxTools.PerformLayout(); + Storages.ResumeLayout(false); + Storages.PerformLayout(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit(); ResumeLayout(false); } @@ -119,5 +178,10 @@ private Button buttonRefreshCollection; private Button buttonDeleteLiner; private PictureBox pictureBoxCollection; + private GroupBox Storages; + private ListBox listBoxStorages; + private Button buttonDeleteSet; + private TextBox textBoxStorageName; + private Button buttonAddStorage; } } \ No newline at end of file diff --git a/Liner/FormLinerCollection.cs b/Liner/FormLinerCollection.cs index 3972175..5683fcb 100644 --- a/Liner/FormLinerCollection.cs +++ b/Liner/FormLinerCollection.cs @@ -21,30 +21,39 @@ namespace Liner /// /// Набор объектов /// - private readonly LinerGenericCollection _liners; + private readonly LinersGenericStorage _storage; /// /// Конструктор /// public FormLinerCollection() { InitializeComponent(); - _liners = new LinerGenericCollection(pictureBoxCollection.Width, pictureBoxCollection.Height); + _storage = new LinersGenericStorage(pictureBoxCollection.Width, pictureBoxCollection.Height); } /// /// Добавление объекта в набор /// /// /// - private void buttonAddLiner_Click(object sender, EventArgs e) + private void ButtonAddLiner_Click(object sender, EventArgs e) { + if (listBoxStorages.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? + string.Empty]; + if (obj == null) + { + return; + } MainScreen form = new(); if (form.ShowDialog() == DialogResult.OK) { - if (_liners + form.SelectedLiner != -1) + if (obj + form.SelectedLiner != -1) { MessageBox.Show("Объект добавлен"); - pictureBoxCollection.Image = _liners.ShowLiners(); + pictureBoxCollection.Image = obj.ShowLiners(); } else { @@ -57,18 +66,37 @@ namespace Liner /// /// /// - private void buttonRemoveLiner_Click(object sender, EventArgs e) + private void ButtonRemoveLiner_Click(object sender, EventArgs e) { + int pos; + 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(textBoxNumber.Text); - if (_liners - pos) + if (textBoxNumber.Text != "") + { + pos = Convert.ToInt32(textBoxNumber.Text); + } + else + { + MessageBox.Show("Не удалось удалить объект"); + return; + } + pos = 0; + if (obj - pos) { MessageBox.Show("Объект удален"); - pictureBoxCollection.Image = _liners.ShowLiners(); + pictureBoxCollection.Image = obj.ShowLiners(); } else { @@ -76,13 +104,86 @@ namespace Liner } } /// + /// Добавление набора в коллекцию + /// + /// + /// + 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]?.ShowLiners(); + } + /// + /// Удаление набора + /// + /// + /// + private void ButtonRemoveObject_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 buttonRefreshCollection_Click(object sender, EventArgs e) + private void ButtonRefreshCollection_Click(object sender, EventArgs e) { - pictureBoxCollection.Image = _liners.ShowLiners(); + if (listBoxStorages.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty]; + if (obj == null) + { + return; + } + pictureBoxCollection.Image = obj.ShowLiners(); + } + /// + /// Заполнение 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; + } } } } diff --git a/Liner/Generics/LinerGenericCollection.cs b/Liner/Generics/LinerGenericCollection.cs index 42a0005..ce412f2 100644 --- a/Liner/Generics/LinerGenericCollection.cs +++ b/Liner/Generics/LinerGenericCollection.cs @@ -72,8 +72,7 @@ namespace Liner.Generics /// public static bool operator -(LinerGenericCollection collect, int pos) { - T? obj = collect._collection.Get(pos); - if (obj != null) + if (collect._collection[pos] != null) { collect._collection.Remove(pos); return true; @@ -87,7 +86,7 @@ namespace Liner.Generics /// public U? GetU(int pos) { - return (U?)_collection.Get(pos)?.GetMoveableObject; + return (U?)_collection[pos]?.GetMoveableObject; } /// /// Вывод всего набора объектов @@ -97,8 +96,8 @@ namespace Liner.Generics { Bitmap bmp = new(_pictureWidth, _pictureHeight); Graphics gr = Graphics.FromImage(bmp); - DrawObjects(gr); DrawBackground(gr); + DrawObjects(gr); return bmp; } /// @@ -124,12 +123,12 @@ namespace Liner.Generics { int iX = ((_pictureWidth / _placeSizeWidth) - 1) * _placeSizeWidth; int iY = 0; - for (int i = 0; i < _collection.Count; i++) + foreach (var liner in _collection.GetLiners()) { - _collection.Get(i)?.SetPosition(iX,iY); - _collection.Get(i)?.DrawTransport(g); + liner?.SetPosition(iX, iY); + liner?.DrawTransport(g); iX -= _placeSizeWidth; - if(iX < 0) + if (iX < 0) { iX = ((_pictureWidth / _placeSizeWidth) - 1) * _placeSizeWidth; iY += _placeSizeHeight; diff --git a/Liner/Generics/LinersGenericStorage.cs b/Liner/Generics/LinersGenericStorage.cs new file mode 100644 index 0000000..72feb94 --- /dev/null +++ b/Liner/Generics/LinersGenericStorage.cs @@ -0,0 +1,80 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Liner.Drawing; +using Liner.MovingStrategies; +namespace Liner.Generics +{ + /// + /// Класс для хранения коллекции + /// + public class LinersGenericStorage + { + + /// + /// Словарь (хранилище) + /// + readonly Dictionary> _linerStorages; + /// + /// Возвращение списка названий наборов + /// + public List Keys => _linerStorages.Keys.ToList(); + /// + /// Ширина окна отрисовки + /// + private readonly int _pictureWidth; + /// + /// Высота окна отрисовки + /// + private readonly int _pictureHeight; + /// + /// Конструктор + /// + /// + /// + public LinersGenericStorage(int pictureWidth, int pictureHeight) + { + _linerStorages = new Dictionary>(); + _pictureWidth = pictureWidth; + _pictureHeight = pictureHeight; + } + /// + /// Добавление набора + /// + /// Название набора + public void AddSet(string name) + { + if(_linerStorages.ContainsKey(name)) + { + return; + } + _linerStorages.Add(name, new LinerGenericCollection(_pictureWidth,_pictureHeight)); + } + /// + /// Удаление набора + /// + /// Название набора + public void DelSet(string name) + { + if (_linerStorages.ContainsKey(name)) + { + _linerStorages.Remove(name); + } + } + /// + /// Доступ к набору + /// + /// + /// + public LinerGenericCollection? this[string ind] + { + get + { + if (_linerStorages.ContainsKey(ind)) { return _linerStorages[ind]; } + return null; + } + } + } +} diff --git a/Liner/Generics/SetGeneric.cs b/Liner/Generics/SetGeneric.cs index 93c1bfe..0e78153 100644 --- a/Liner/Generics/SetGeneric.cs +++ b/Liner/Generics/SetGeneric.cs @@ -16,18 +16,23 @@ namespace Liner.Generics /// /// Массив объектов, которые храним /// - private readonly T?[] _places; + private readonly List _places; /// /// Количество объектов в массиве /// - public int Count => _places.Length; + public int Count => _places.Capacity; + /// + /// Максимальное количество объектов в списке + /// + private readonly int _maxCount; /// /// Конструктор /// /// public SetGeneric(int count) { - _places = new T?[count]; + _maxCount = count; + _places = new List(count); } /// /// Добавление объекта в набор @@ -36,22 +41,12 @@ namespace Liner.Generics /// public int Insert(T liner) { - int nulli = 0; - while (_places[nulli] != null) + if (_places.Count + 1 <= _maxCount) { - nulli++; - if(nulli == _places.Length) - { - return -1; // нет пустого места - } + _places.Insert(0, liner); + return 0; } - //сдвиг - for(int i = nulli; i > 0; i--) - { - _places[i] = _places[i - 1]; - } - _places[0] = liner; - return 0; + return -1; } /// /// Добавление объекта в набор на конкретную позицию @@ -61,24 +56,12 @@ namespace Liner.Generics /// public int Insert(T liner, int position) { - if (position < 0 || position > _places.Length) + if (_places.Count + 1 <= _maxCount && _places.Count >= position) { - return -1; + _places.Insert(position, liner); + return position; } - if (_places[position] != null) - { - int nulli = position; - while (_places[nulli] != null) - { - nulli++; - if (nulli == _places.Length) - { - return -1; // нет пустого места - } - } - } - _places[position] = liner; - return position; + return -1; } /// /// Удаление объекта из набора с конкретной позиции @@ -87,25 +70,50 @@ namespace Liner.Generics /// public bool Remove(int position) { - if (position < 0 || position > _places.Length) + if(_places.Count >= position && position >= 0) { - return false; + _places.RemoveAt(position); + return true; } - _places[position] = null; - return true; + return false; } /// /// Получение объекта из набора по позиции /// /// /// - public T? Get(int position) + public T? this[int position] { - if (position < 0 || position > _places.Length) + get { + if (_places.Count >= position && position >= 0) + { + return _places[position]; + } return null; } - return _places[position]; + set + { + if(_places.Count + 1 <= _maxCount && position >= 0 && _places.Count > position) + { + _places[position] = value; + } + } + } + /// + /// Проход по списку + /// + /// + public IEnumerable GetLiners(int? maxLiners = null) + { + for (int i = 0; i < _places.Count; ++i) + { + yield return _places[i]; + if (maxLiners.HasValue && i == maxLiners.Value) + { + yield break; + } + } } } } diff --git a/Liner/MainScreen.cs b/Liner/MainScreen.cs index 4e176e4..0c71647 100644 --- a/Liner/MainScreen.cs +++ b/Liner/MainScreen.cs @@ -2,7 +2,6 @@ using Liner.Drawing; using Liner.Entities; using Liner.MovingStrategies; using System.Drawing; -using static System.Windows.Forms.VisualStyles.VisualStyleElement.Window; namespace Liner { @@ -51,6 +50,10 @@ namespace Liner Color mainColor = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)); Color addColor = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)); ColorDialog dialog = new(); + if (dialog.ShowDialog() == DialogResult.OK) + { + addColor = dialog.Color; + } _drawingLiner = new DrawingLiner(random.Next(100, 300), random.Next(1000, 3000), mainColor, addColor, pictureBoxLiner.Width, pictureBoxLiner.Height); @@ -93,6 +96,10 @@ namespace Liner { addColor = dialog.Color; } + if (dialog.ShowDialog() == DialogResult.OK) + { + mainColor = dialog.Color; + } _drawingLiner = new DrawingBigLiner(random.Next(100, 300), random.Next(1000, 3000), mainColor, addColor, Convert.ToBoolean(random.Next(0, 2)), diff --git a/Liner/MovingStrategies/DrawingObjectLiner.cs b/Liner/MovingStrategies/DrawingObjectLiner.cs index aa00f5c..0afe82d 100644 --- a/Liner/MovingStrategies/DrawingObjectLiner.cs +++ b/Liner/MovingStrategies/DrawingObjectLiner.cs @@ -35,5 +35,4 @@ namespace Liner.MovingStrategies public void MoveObject(DirectionType direction) => _drawingLiner?.MoveTransport(direction); } - } diff --git a/Liner/Program.cs b/Liner/Program.cs index ba99bab..0cdd27f 100644 --- a/Liner/Program.cs +++ b/Liner/Program.cs @@ -14,4 +14,4 @@ namespace Liner Application.Run(new FormLinerCollection()); } } -} +} \ No newline at end of file