diff --git a/ProjStormtrooper/ProjStormtrooper/DrawingPlaneEqutables.cs b/ProjStormtrooper/ProjStormtrooper/DrawingPlaneEqutables.cs new file mode 100644 index 0000000..171033b --- /dev/null +++ b/ProjStormtrooper/ProjStormtrooper/DrawingPlaneEqutables.cs @@ -0,0 +1,63 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjStormtrooper +{ + public class DrawingPlaneEqutables : IEqualityComparer + { + public bool Equals(DrawingPlane? x, DrawingPlane? y) + { + if (x == null || x.EntityPlane == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.EntityPlane == null) + { + throw new ArgumentNullException(nameof(y)); + } + if (x.GetType().Name != y.GetType().Name) + { + return false; + } + if (x.EntityPlane.Speed != y.EntityPlane.Speed) + { + return false; + } + if (x.EntityPlane.Weight != y.EntityPlane.Weight) + { + return false; + } + if (x.EntityPlane.BodyColor != y.EntityPlane.BodyColor) + { + return false; + } + if (x is DrawingStormtrooper && y is DrawingStormtrooper) + { + var xStormtrooper = (x.EntityPlane as EntityStormtrooper); + var yStormtrooper = (y.EntityPlane as EntityStormtrooper); + if (xStormtrooper?.AdditionalColor != yStormtrooper?.AdditionalColor) + { + return false; + } + if (xStormtrooper?.Bombs != yStormtrooper?.Bombs) + { + return false; + } + if (xStormtrooper?.Rockets != yStormtrooper?.Rockets) + { + return false; + } + } + return true; + } + + public int GetHashCode([DisallowNull] DrawingPlane obj) + { + return obj.GetHashCode(); + } + } +} diff --git a/ProjStormtrooper/ProjStormtrooper/PlaneCollection.Designer.cs b/ProjStormtrooper/ProjStormtrooper/PlaneCollection.Designer.cs index dafbfb6..c98f95f 100644 --- a/ProjStormtrooper/ProjStormtrooper/PlaneCollection.Designer.cs +++ b/ProjStormtrooper/ProjStormtrooper/PlaneCollection.Designer.cs @@ -29,6 +29,8 @@ private void InitializeComponent() { groupBoxTools = new GroupBox(); + buttonSortByType = new Button(); + buttonSortByColor = new Button(); groupBoxStorages = new GroupBox(); buttonRemoveStorage = new Button(); listBoxStorages = new ListBox(); @@ -53,42 +55,59 @@ // // groupBoxTools // + groupBoxTools.Controls.Add(buttonSortByType); + groupBoxTools.Controls.Add(buttonSortByColor); groupBoxTools.Controls.Add(groupBoxStorages); groupBoxTools.Controls.Add(maskedTextBoxNumber); groupBoxTools.Controls.Add(buttonRefreshCollection); groupBoxTools.Controls.Add(buttonRemovePlane); groupBoxTools.Controls.Add(buttonAddPlane); groupBoxTools.Dock = DockStyle.Right; - groupBoxTools.Location = new Point(689, 24); - groupBoxTools.Margin = new Padding(3, 2, 3, 2); + groupBoxTools.Location = new Point(787, 28); groupBoxTools.Name = "groupBoxTools"; - groupBoxTools.Padding = new Padding(3, 2, 3, 2); - groupBoxTools.Size = new Size(201, 380); + groupBoxTools.Size = new Size(230, 510); groupBoxTools.TabIndex = 0; groupBoxTools.TabStop = false; groupBoxTools.Text = "Инструменты"; // + // buttonSortByType + // + buttonSortByType.Location = new Point(6, 273); + buttonSortByType.Name = "buttonSortByType"; + buttonSortByType.Size = new Size(218, 29); + buttonSortByType.TabIndex = 7; + buttonSortByType.Text = "Сортировка по типу"; + buttonSortByType.UseVisualStyleBackColor = true; + buttonSortByType.Click += buttonSortByType_Click; + // + // buttonSortByColor + // + buttonSortByColor.Location = new Point(6, 305); + buttonSortByColor.Name = "buttonSortByColor"; + buttonSortByColor.Size = new Size(218, 29); + buttonSortByColor.TabIndex = 6; + buttonSortByColor.Text = "Сортировка по цвету"; + buttonSortByColor.UseVisualStyleBackColor = true; + buttonSortByColor.Click += buttonSortByColor_Click; + // // groupBoxStorages // groupBoxStorages.Controls.Add(buttonRemoveStorage); groupBoxStorages.Controls.Add(listBoxStorages); groupBoxStorages.Controls.Add(buttonAddStorage); groupBoxStorages.Controls.Add(textBoxStorageName); - groupBoxStorages.Location = new Point(5, 20); - groupBoxStorages.Margin = new Padding(3, 2, 3, 2); + groupBoxStorages.Location = new Point(6, 26); groupBoxStorages.Name = "groupBoxStorages"; - groupBoxStorages.Padding = new Padding(3, 2, 3, 2); - groupBoxStorages.Size = new Size(191, 181); + groupBoxStorages.Size = new Size(218, 241); groupBoxStorages.TabIndex = 5; groupBoxStorages.TabStop = false; groupBoxStorages.Text = "Наборы"; // // buttonRemoveStorage // - buttonRemoveStorage.Location = new Point(5, 153); - buttonRemoveStorage.Margin = new Padding(3, 2, 3, 2); + buttonRemoveStorage.Location = new Point(6, 204); buttonRemoveStorage.Name = "buttonRemoveStorage"; - buttonRemoveStorage.Size = new Size(180, 22); + buttonRemoveStorage.Size = new Size(206, 29); buttonRemoveStorage.TabIndex = 3; buttonRemoveStorage.Text = "Удалить набор"; buttonRemoveStorage.UseVisualStyleBackColor = true; @@ -97,20 +116,18 @@ // listBoxStorages // listBoxStorages.FormattingEnabled = true; - listBoxStorages.ItemHeight = 15; - listBoxStorages.Location = new Point(5, 70); - listBoxStorages.Margin = new Padding(3, 2, 3, 2); + listBoxStorages.ItemHeight = 20; + listBoxStorages.Location = new Point(6, 94); listBoxStorages.Name = "listBoxStorages"; - listBoxStorages.Size = new Size(181, 79); + listBoxStorages.Size = new Size(206, 104); listBoxStorages.TabIndex = 2; listBoxStorages.SelectedIndexChanged += listBoxStorages_SelectedIndexChanged; // // buttonAddStorage // - buttonAddStorage.Location = new Point(5, 44); - buttonAddStorage.Margin = new Padding(3, 2, 3, 2); + buttonAddStorage.Location = new Point(6, 59); buttonAddStorage.Name = "buttonAddStorage"; - buttonAddStorage.Size = new Size(180, 22); + buttonAddStorage.Size = new Size(206, 29); buttonAddStorage.TabIndex = 1; buttonAddStorage.Text = "Добавить набор"; buttonAddStorage.UseVisualStyleBackColor = true; @@ -118,29 +135,26 @@ // // textBoxStorageName // - textBoxStorageName.Location = new Point(5, 20); - textBoxStorageName.Margin = new Padding(3, 2, 3, 2); + textBoxStorageName.Location = new Point(6, 26); textBoxStorageName.Name = "textBoxStorageName"; - textBoxStorageName.Size = new Size(181, 23); + textBoxStorageName.Size = new Size(206, 27); textBoxStorageName.TabIndex = 0; // // maskedTextBoxNumber // - maskedTextBoxNumber.Location = new Point(5, 303); - maskedTextBoxNumber.Margin = new Padding(3, 2, 3, 2); + maskedTextBoxNumber.Location = new Point(6, 404); maskedTextBoxNumber.Mask = "00"; maskedTextBoxNumber.Name = "maskedTextBoxNumber"; - maskedTextBoxNumber.Size = new Size(191, 23); + maskedTextBoxNumber.Size = new Size(218, 27); maskedTextBoxNumber.TabIndex = 4; maskedTextBoxNumber.TextAlign = HorizontalAlignment.Center; maskedTextBoxNumber.ValidatingType = typeof(int); // // buttonRefreshCollection // - buttonRefreshCollection.Location = new Point(5, 373); - buttonRefreshCollection.Margin = new Padding(3, 2, 3, 2); + buttonRefreshCollection.Location = new Point(6, 497); buttonRefreshCollection.Name = "buttonRefreshCollection"; - buttonRefreshCollection.Size = new Size(191, 22); + buttonRefreshCollection.Size = new Size(218, 29); buttonRefreshCollection.TabIndex = 3; buttonRefreshCollection.Text = "Обновить коллекцию"; buttonRefreshCollection.UseVisualStyleBackColor = true; @@ -148,10 +162,9 @@ // // buttonRemovePlane // - buttonRemovePlane.Location = new Point(5, 328); - buttonRemovePlane.Margin = new Padding(3, 2, 3, 2); + buttonRemovePlane.Location = new Point(6, 437); buttonRemovePlane.Name = "buttonRemovePlane"; - buttonRemovePlane.Size = new Size(191, 22); + buttonRemovePlane.Size = new Size(218, 29); buttonRemovePlane.TabIndex = 2; buttonRemovePlane.Text = "Удалить самолет"; buttonRemovePlane.UseVisualStyleBackColor = true; @@ -159,10 +172,9 @@ // // buttonAddPlane // - buttonAddPlane.Location = new Point(5, 262); - buttonAddPlane.Margin = new Padding(3, 2, 3, 2); + buttonAddPlane.Location = new Point(6, 349); buttonAddPlane.Name = "buttonAddPlane"; - buttonAddPlane.Size = new Size(191, 22); + buttonAddPlane.Size = new Size(218, 29); buttonAddPlane.TabIndex = 0; buttonAddPlane.Text = "Добавить самолет"; buttonAddPlane.UseVisualStyleBackColor = true; @@ -171,10 +183,9 @@ // pictureBoxCollection // pictureBoxCollection.Dock = DockStyle.Fill; - pictureBoxCollection.Location = new Point(0, 24); - pictureBoxCollection.Margin = new Padding(3, 2, 3, 2); + pictureBoxCollection.Location = new Point(0, 28); pictureBoxCollection.Name = "pictureBoxCollection"; - pictureBoxCollection.Size = new Size(689, 380); + pictureBoxCollection.Size = new Size(787, 510); pictureBoxCollection.TabIndex = 1; pictureBoxCollection.TabStop = false; // @@ -184,8 +195,7 @@ menuStrip.Items.AddRange(new ToolStripItem[] { файлToolStripMenuItem }); menuStrip.Location = new Point(0, 0); menuStrip.Name = "menuStrip"; - menuStrip.Padding = new Padding(5, 2, 0, 2); - menuStrip.Size = new Size(890, 24); + menuStrip.Size = new Size(1017, 28); menuStrip.TabIndex = 2; menuStrip.Text = "menuStrip1"; // @@ -193,21 +203,20 @@ // файлToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { сохранитьToolStripMenuItem, загрузитьToolStripMenuItem }); файлToolStripMenuItem.Name = "файлToolStripMenuItem"; - файлToolStripMenuItem.Size = new Size(48, 20); + файлToolStripMenuItem.Size = new Size(59, 24); файлToolStripMenuItem.Text = "Файл"; - файлToolStripMenuItem.Click += файлToolStripMenuItem_Click; // // сохранитьToolStripMenuItem // сохранитьToolStripMenuItem.Name = "сохранитьToolStripMenuItem"; - сохранитьToolStripMenuItem.Size = new Size(180, 22); + сохранитьToolStripMenuItem.Size = new Size(166, 26); сохранитьToolStripMenuItem.Text = "Сохранить"; сохранитьToolStripMenuItem.Click += saveToFileToolStripMenuItem_Click; // // загрузитьToolStripMenuItem // загрузитьToolStripMenuItem.Name = "загрузитьToolStripMenuItem"; - загрузитьToolStripMenuItem.Size = new Size(180, 22); + загрузитьToolStripMenuItem.Size = new Size(166, 26); загрузитьToolStripMenuItem.Text = "Загрузить"; загрузитьToolStripMenuItem.Click += loadFromFileToolStripMenuItem_Click; // @@ -220,17 +229,16 @@ // saveFileDialog.Filter = "txt file | *.txt"; // - // PlaneCollection + // FormPlaneCollection // - AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(890, 404); + ClientSize = new Size(1017, 538); Controls.Add(pictureBoxCollection); Controls.Add(groupBoxTools); Controls.Add(menuStrip); MainMenuStrip = menuStrip; - Margin = new Padding(3, 2, 3, 2); - Name = "PlaneCollection"; + Name = "FormPlaneCollection"; Text = "Набор самолетов"; groupBoxTools.ResumeLayout(false); groupBoxTools.PerformLayout(); @@ -262,5 +270,7 @@ private ToolStripMenuItem загрузитьToolStripMenuItem; private OpenFileDialog openFileDialog; private SaveFileDialog saveFileDialog; + private Button buttonSortByType; + private Button buttonSortByColor; } } \ No newline at end of file diff --git a/ProjStormtrooper/ProjStormtrooper/PlaneCollection.cs b/ProjStormtrooper/ProjStormtrooper/PlaneCollection.cs index e794db9..7aa17b9 100644 --- a/ProjStormtrooper/ProjStormtrooper/PlaneCollection.cs +++ b/ProjStormtrooper/ProjStormtrooper/PlaneCollection.cs @@ -140,7 +140,12 @@ namespace ProjStormtrooper } catch (StorageOverflowException ex) { - MessageBox.Show(ex.Message); + MessageBox.Show(ex.Message, "Ошибка добавления", MessageBoxButtons.OK, MessageBoxIcon.Error); + _logger.LogWarning("Ошибка добавления: " + ex.Message); + } + catch (ApplicationException ex) + { + MessageBox.Show(ex.Message, "Ошибка добавления", MessageBoxButtons.OK, MessageBoxIcon.Error); _logger.LogWarning("Ошибка добавления: " + ex.Message); } } @@ -246,5 +251,26 @@ namespace ProjStormtrooper } } } + /// + /// Сортировка по сравнителю + /// + /// + private void ComparePlanes(IComparer comparer) + { + if (listBoxStorages.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty]; + if (obj == null) + { + return; + } + obj.Sort(comparer); + pictureBoxCollection.Image = obj.ShowPlanes(); + } + private void buttonSortByType_Click(object sender, EventArgs e) => ComparePlanes(new PlaneCompareByType()); + + private void buttonSortByColor_Click(object sender, EventArgs e) => ComparePlanes(new PlaneCompareByColor()); } } diff --git a/ProjStormtrooper/ProjStormtrooper/PlaneCollection.resx b/ProjStormtrooper/ProjStormtrooper/PlaneCollection.resx index 6df6d7f..ad03251 100644 --- a/ProjStormtrooper/ProjStormtrooper/PlaneCollection.resx +++ b/ProjStormtrooper/ProjStormtrooper/PlaneCollection.resx @@ -18,7 +18,7 @@ System.Resources.ResXResourceReader, System.Windows.Forms, ... System.Resources.ResXResourceWriter, System.Windows.Forms, ... this is my long stringthis is a comment - Blue + Blue [base64 mime encoded serialized .NET Framework object] diff --git a/ProjStormtrooper/ProjStormtrooper/PlaneCompareByColor.cs b/ProjStormtrooper/ProjStormtrooper/PlaneCompareByColor.cs new file mode 100644 index 0000000..154b98d --- /dev/null +++ b/ProjStormtrooper/ProjStormtrooper/PlaneCompareByColor.cs @@ -0,0 +1,35 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjStormtrooper +{ + public class PlaneCompareByColor : IComparer + { + public int Compare(DrawingPlane? x, DrawingPlane? y) + { + if (x == null || x.EntityPlane == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.EntityPlane == null) + { + throw new ArgumentNullException(nameof(y)); + } + if (x.EntityPlane.BodyColor != y.EntityPlane.BodyColor) + { + return x.EntityPlane.BodyColor.Name.CompareTo(y.EntityPlane.BodyColor.Name); + } + if (x.EntityPlane is EntityStormtrooper xStormtrooper && y.EntityPlane is EntityStormtrooper yStormtrooper) + { + if (xStormtrooper.AdditionalColor != yStormtrooper.AdditionalColor) + { + return xStormtrooper.AdditionalColor.Name.CompareTo(yStormtrooper.AdditionalColor.Name); + } + } + return 0; + } + } +} diff --git a/ProjStormtrooper/ProjStormtrooper/PlaneCompareByType.cs b/ProjStormtrooper/ProjStormtrooper/PlaneCompareByType.cs new file mode 100644 index 0000000..a8bea3e --- /dev/null +++ b/ProjStormtrooper/ProjStormtrooper/PlaneCompareByType.cs @@ -0,0 +1,33 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjStormtrooper +{ + public class PlaneCompareByType : IComparer + { + public int Compare(DrawingPlane? x, DrawingPlane? y) + { + if (x == null || x.EntityPlane == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.EntityPlane == null) + { + throw new ArgumentNullException(nameof(y)); + } + if (x.GetType().Name != y.GetType().Name) + { + return x.GetType().Name.CompareTo(y.GetType().Name); + } + var speedCompare = x.EntityPlane.Speed.CompareTo(y.EntityPlane.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return x.EntityPlane.Weight.CompareTo(y.EntityPlane.Weight); + } + } +} diff --git a/ProjStormtrooper/ProjStormtrooper/PlaneConfig.cs b/ProjStormtrooper/ProjStormtrooper/PlaneConfig.cs index 5cd4f51..f06554a 100644 --- a/ProjStormtrooper/ProjStormtrooper/PlaneConfig.cs +++ b/ProjStormtrooper/ProjStormtrooper/PlaneConfig.cs @@ -123,15 +123,5 @@ namespace ProjStormtrooper (_plane.EntityPlane as EntityStormtrooper).SetAdditionalColor((Color)e.Data.GetData(typeof(Color).ToString())); DrawPlane(); } - - private void panelColorPurple_Paint(object sender, PaintEventArgs e) - { - - } - - private void panelColorPink_Paint(object sender, PaintEventArgs e) - { - - } } } diff --git a/ProjStormtrooper/ProjStormtrooper/PlanesCollectionInfo.cs b/ProjStormtrooper/ProjStormtrooper/PlanesCollectionInfo.cs new file mode 100644 index 0000000..330e028 --- /dev/null +++ b/ProjStormtrooper/ProjStormtrooper/PlanesCollectionInfo.cs @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjStormtrooper +{ + public class PlanesCollectionInfo : IEquatable + { + public string Name { get; private set; } + public string Description { get; private set; } + public PlanesCollectionInfo(string name, string description) + { + Name = name; + Description = description; + } + public bool Equals(PlanesCollectionInfo? other) + { + return Name == other.Name; + } + public override int GetHashCode() + { + return Name.GetHashCode(); + } + public override string ToString() + { + return Name; + } + } +} diff --git a/ProjStormtrooper/ProjStormtrooper/PlanesGenericCollection.cs b/ProjStormtrooper/ProjStormtrooper/PlanesGenericCollection.cs index aed534c..6bb393b 100644 --- a/ProjStormtrooper/ProjStormtrooper/PlanesGenericCollection.cs +++ b/ProjStormtrooper/ProjStormtrooper/PlanesGenericCollection.cs @@ -12,8 +12,8 @@ namespace ProjStormtrooper /// /// internal class PlanesGenericCollection - where T : DrawingPlane - where U : IMoveableObject + where T : DrawingPlane + where U : IMoveableObject { /// /// Ширина окна прорисовки @@ -49,6 +49,11 @@ namespace ProjStormtrooper _collection = new SetGeneric(horizontalObjectsCount * verticalObjectsCount); } /// + /// Сортировка + /// + /// + public void Sort(IComparer comparer) => _collection.SortSet(comparer); + /// /// Перегрузка оператора сложения /// /// @@ -60,7 +65,7 @@ namespace ProjStormtrooper { return -1; } - return collect?._collection.Insert(obj) ?? -1; + return collect?._collection.Insert(obj, new DrawingPlaneEqutables()) ?? -1; } /// /// Перегрузка оператора вычитания @@ -71,10 +76,7 @@ namespace ProjStormtrooper public static T? operator -(PlanesGenericCollection collect, int pos) { T? obj = collect._collection[pos]; - if (obj != null) - { - collect._collection.Remove(pos); - } + collect._collection.Remove(pos); return obj; } /// @@ -102,45 +104,45 @@ namespace ProjStormtrooper /// Метод отрисовки фона /// /// - private void DrawBackground(Graphics g) { Pen pen = new(Color.Black, 3); for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++) { - for (int j = 0; j < _pictureHeight / _placeSizeHeight + - 1; ++j) - {//линия разметки места - g.DrawLine(pen, i * _placeSizeWidth, j * - _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j * - _placeSizeHeight); + for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j) + { + // Линия разметки места + g.DrawLine(pen, i * _placeSizeWidth, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight); } - g.DrawLine(pen, i * _placeSizeWidth, 0, i * - _placeSizeWidth, _pictureHeight / _placeSizeHeight * _placeSizeHeight); + g.DrawLine(pen, i * _placeSizeWidth, 0, i * _placeSizeWidth, _pictureHeight / _placeSizeHeight * _placeSizeHeight); } } /// Метод прорисовки объектов /// /// - - private void DrawObjects(Graphics g) { - int width = _pictureWidth / _placeSizeWidth; - int height = _pictureHeight / _placeSizeHeight; - - for (int i = 0; i < _collection.Count; i++) + int placesColumnCount = _pictureHeight / _placeSizeHeight; + int placesRowCount = _pictureWidth / _placeSizeWidth; + int i = 0; + foreach (var plane in _collection.GetPlanes()) { - var obj = _collection[i]; - obj?.SetPosition( - (int)((width - 1) * _placeSizeWidth - (i % width * _placeSizeWidth)), - (int)((i / width) * _placeSizeHeight) - ); - obj?.DrawTransport(g); + if (plane != null) + { + // установка позиции + plane.SetPosition( + (int)((placesRowCount - 1) * _placeSizeWidth - (i % placesColumnCount * _placeSizeWidth) + (_placeSizeWidth - plane.GetWidth) / 2), + (int)(i / placesColumnCount * _placeSizeHeight + (_placeSizeHeight - plane.GetHeight) / 2) + ); + // прорисовка объекта + plane.DrawTransport(g); + } + i++; } } - + /// + /// Получение объектов коллекции + /// public IEnumerable GetPlanes => _collection.GetPlanes(); - } } diff --git a/ProjStormtrooper/ProjStormtrooper/PlanesGenericStorage.cs b/ProjStormtrooper/ProjStormtrooper/PlanesGenericStorage.cs index 8aae045..a1cb014 100644 --- a/ProjStormtrooper/ProjStormtrooper/PlanesGenericStorage.cs +++ b/ProjStormtrooper/ProjStormtrooper/PlanesGenericStorage.cs @@ -23,11 +23,11 @@ namespace ProjStormtrooper /// /// Словарь (хранилище) /// - readonly Dictionary> _planeStorages; + readonly Dictionary> _planeStorages; /// /// Возвращение списка названий наборов /// - public List Keys => _planeStorages.Keys.ToList(); + public List Keys => _planeStorages.Keys.ToList(); /// /// Ширина окна отрисовки /// @@ -43,7 +43,7 @@ namespace ProjStormtrooper /// public PlanesGenericStorage(int pictureWidth, int pictureHeight) { - _planeStorages = new Dictionary>(); + _planeStorages = new Dictionary>(); _pictureWidth = pictureWidth; _pictureHeight = pictureHeight; } @@ -53,7 +53,7 @@ namespace ProjStormtrooper /// Название набора public void AddSet(string name) { - _planeStorages.Add(name, new PlanesGenericCollection(_pictureWidth, _pictureHeight)); + _planeStorages.Add(new PlanesCollectionInfo(name, ""), new PlanesGenericCollection(_pictureWidth, _pictureHeight)); } /// /// Удаление набора @@ -61,8 +61,9 @@ namespace ProjStormtrooper /// Название набора public void DelSet(string name) { - if (_planeStorages.ContainsKey(name)) - _planeStorages.Remove(name); + var info = new PlanesCollectionInfo(name, ""); + if (_planeStorages.ContainsKey(info)) + _planeStorages.Remove(info); } /// /// Доступ к набору @@ -73,8 +74,9 @@ namespace ProjStormtrooper { get { - if (_planeStorages.ContainsKey(ind)) - return _planeStorages[ind]; + var info = new PlanesCollectionInfo(ind, ""); + if (_planeStorages.ContainsKey(info)) + return _planeStorages[info]; return null; } } @@ -104,7 +106,7 @@ namespace ProjStormtrooper { throw new Exception("Невалиданя операция, нет данных для сохранения"); } - sw.WriteLine($"{storage.Key}{_separatorForKeyValue}{storageString}"); + sw.WriteLine($"{storage.Key.Name}{_separatorForKeyValue}{storageString}"); } } } @@ -152,7 +154,7 @@ namespace ProjStormtrooper } } } - _planeStorages.Add(record[0], collection); + _planeStorages.Add(new PlanesCollectionInfo(record[0], ""), collection); currentLine = sr.ReadLine(); } } diff --git a/ProjStormtrooper/ProjStormtrooper/SetGeneric.cs b/ProjStormtrooper/ProjStormtrooper/SetGeneric.cs index 30fae9c..f5a3fec 100644 --- a/ProjStormtrooper/ProjStormtrooper/SetGeneric.cs +++ b/ProjStormtrooper/ProjStormtrooper/SetGeneric.cs @@ -34,13 +34,18 @@ namespace ProjStormtrooper _places = new List(count); } /// + /// Сортировка набора объектов + /// + /// + public void SortSet(IComparer comparer) => _places.Sort(comparer); + /// /// Добавления объекта в набор /// /// /// - public int Insert(T plane) - { - return Insert(plane, 0); + public int Insert(T plane, IEqualityComparer? equal = null) + { + return Insert(plane, 0, equal); } /// /// Добавление объекта в набор на конкретную позицию @@ -48,7 +53,7 @@ namespace ProjStormtrooper /// /// /// - public int Insert(T plane, int position) + public int Insert(T plane, int position, IEqualityComparer? equal = null) { if (_places.Count == _maxCount) { @@ -59,6 +64,16 @@ namespace ProjStormtrooper { return -1; } + if (equal != null) + { + foreach (var otherPlane in _places) + { + if (equal.Equals(otherPlane, plane)) + { + throw new ApplicationException("Такой объект уже есть в коллекции!"); + } + } + } // Вставка по позиции _places.Insert(position, plane); return position;