diff --git a/AccordionBus/AccordionBus/AbstractMap.cs b/AccordionBus/AccordionBus/AbstractMap.cs index 437ac7f..494b6cd 100644 --- a/AccordionBus/AccordionBus/AbstractMap.cs +++ b/AccordionBus/AccordionBus/AbstractMap.cs @@ -7,7 +7,7 @@ using System.Threading.Tasks; namespace AccordionBus { - internal abstract class AbstractMap + internal abstract class AbstractMap : IEquatable { private IDrawningObject _drawningObject = null; protected int[,] _map = null; @@ -158,5 +158,44 @@ namespace AccordionBus protected abstract void GenerateMap(); protected abstract void DrawRoadPart(Graphics g, int i, int j); protected abstract void DrawBarrierPart(Graphics g, int i, int j); + + public bool Equals(AbstractMap? other) + { + if (other == null) + { + return false; + } + if (other == null) + { + return false; + } + if (_width != other._width) + { + return false; + } + if (_height != other._height) + { + return false; + } + if (_size_x != other._size_x) + { + return false; + } + if (_size_y != other._size_y) + { + return false; + } + for (int i = 0; i < _map.GetLength(0); i++) + { + for (int j = 0; j < _map.GetLength(1); j++) + { + if (_map[i, j] != other._map[i, j]) + { + return false; + } + } + } + return true; + } } } \ No newline at end of file diff --git a/AccordionBus/AccordionBus/BusCompareByColor.cs b/AccordionBus/AccordionBus/BusCompareByColor.cs new file mode 100644 index 0000000..4bb3c14 --- /dev/null +++ b/AccordionBus/AccordionBus/BusCompareByColor.cs @@ -0,0 +1,77 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AccordionBus +{ + internal class BusCompareByColor : IComparer + { + public int Compare(IDrawningObject? x, IDrawningObject? y) + { + if (x == null && y == null) + { + return 0; + } + if (x == null && y != null) + { + return 1; + } + if (x != null && y == null) + { + return -1; + } + var xBus = x as DrawningObjectBus; + var yBus = y as DrawningObjectBus; + if (xBus == null && yBus == null) + { + return 0; + } + if (xBus == null && yBus != null) + { + return 1; + } + if (xBus != null && yBus == null) + { + return -1; + } + + if (xBus.GetBus.Bus.BodyColor.R.CompareTo(yBus.GetBus.Bus.BodyColor.R) != 0) + { + return xBus.GetBus.Bus.BodyColor.R.CompareTo(yBus.GetBus.Bus.BodyColor.R); + } + if (xBus.GetBus.Bus.BodyColor.G.CompareTo(yBus.GetBus.Bus.BodyColor.G) != 0) + { + return xBus.GetBus.Bus.BodyColor.G.CompareTo(yBus.GetBus.Bus.BodyColor.G); + } + if (xBus.GetBus.Bus.BodyColor.B.CompareTo(yBus.GetBus.Bus.BodyColor.B) != 0) + { + return xBus.GetBus.Bus.BodyColor.B.CompareTo(yBus.GetBus.Bus.BodyColor.B); + } + + if (xBus.GetBus.Bus is EntityAccordionBus xAccordionBus && yBus.GetBus.Bus is EntityAccordionBus yAccordionBus) + { + if (xAccordionBus.DopColor.R.CompareTo(yAccordionBus.DopColor.R) != 0) + { + return xAccordionBus.DopColor.R.CompareTo(yAccordionBus.DopColor.R); + } + if (xAccordionBus.DopColor.G.CompareTo(yAccordionBus.DopColor.G) != 0) + { + return xAccordionBus.DopColor.G.CompareTo(yAccordionBus.DopColor.G); + } + if (xAccordionBus.DopColor.B.CompareTo(yAccordionBus.DopColor.B) != 0) + { + return xAccordionBus.DopColor.B.CompareTo(yAccordionBus.DopColor.B); + } + } + + var speedCompare = xBus.GetBus.Bus.Speed.CompareTo(yBus.GetBus.Bus.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return xBus.GetBus.Bus.Weight.CompareTo(yBus.GetBus.Bus.Weight); + } + } +} \ No newline at end of file diff --git a/AccordionBus/AccordionBus/BusCompareByType.cs b/AccordionBus/AccordionBus/BusCompareByType.cs new file mode 100644 index 0000000..ab0d3fb --- /dev/null +++ b/AccordionBus/AccordionBus/BusCompareByType.cs @@ -0,0 +1,55 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AccordionBus +{ + internal class BusCompareByType : IComparer + { + public int Compare(IDrawningObject? x, IDrawningObject? y) + { + if (x == null && y == null) + { + return 0; + } + if (x == null && y != null) + { + return 1; + } + if (x != null && y == null) + { + return -1; + } + var xBus = x as DrawningObjectBus; + var yBus = y as DrawningObjectBus; + if (xBus == null && yBus == null) + { + return 0; + } + if (xBus == null && yBus != null) + { + return 1; + } + if (xBus != null && yBus == null) + { + return -1; + } + if (xBus.GetBus.GetType().Name != yBus.GetBus.GetType().Name) + { + if (xBus.GetBus.GetType().Name == "DrawningBus") + { + return -1; + } + return 1; + } + var speedCompare = xBus.GetBus.Bus.Speed.CompareTo(yBus.GetBus.Bus.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return xBus.GetBus.Bus.Weight.CompareTo(yBus.GetBus.Bus.Weight); + } + } +} \ No newline at end of file diff --git a/AccordionBus/AccordionBus/DrawningObjectBus.cs b/AccordionBus/AccordionBus/DrawningObjectBus.cs index 82be0ff..c4b3d77 100644 --- a/AccordionBus/AccordionBus/DrawningObjectBus.cs +++ b/AccordionBus/AccordionBus/DrawningObjectBus.cs @@ -16,6 +16,8 @@ namespace AccordionBus } public float Step => _bus?.Bus?.Step ?? 0; + public DrawningBus GetBus => _bus; + public (float Left, float Right, float Top, float Bottom) GetCurrentPosition() { return _bus?.GetCurrentPosition() ?? default; @@ -39,5 +41,57 @@ namespace AccordionBus public string GetInfo() => _bus?.GetDataForSave(); public static IDrawningObject Create(string data) => new DrawningObjectBus(data.CreateDrawningBus()); + + public bool Equals(IDrawningObject? other) + { + if (other == null) + { + return false; + } + var otherBus = other as DrawningObjectBus; + if (otherBus == null) + { + return false; + } + var bus = _bus.Bus; + var otherBusBus = otherBus._bus.Bus; + if (bus.GetType().Name != otherBusBus.GetType().Name) + { + return false; + } + if (bus.Speed != otherBusBus.Speed) + { + return false; + } + if (bus.Weight != otherBusBus.Weight) + { + return false; + } + if (bus.BodyColor != otherBusBus.BodyColor) + { + return false; + } + // проверка в случае продвинутого объекта + if (bus is EntityAccordionBus entityAccordionBus && otherBusBus is EntityAccordionBus otherEntityAccordionBus) + { + if (entityAccordionBus.DopColor != otherEntityAccordionBus.DopColor) + { + return false; + } + if (entityAccordionBus.Compartment != otherEntityAccordionBus.Compartment) + { + return false; + } + if (entityAccordionBus.RearviewMirror != otherEntityAccordionBus.RearviewMirror) + { + return false; + } + if (entityAccordionBus.BusNumber != otherEntityAccordionBus.BusNumber) + { + return false; + } + } + return true; + } } } diff --git a/AccordionBus/AccordionBus/FormMapWithSetBuses.Designer.cs b/AccordionBus/AccordionBus/FormMapWithSetBuses.Designer.cs index 3c20478..5beb0b4 100644 --- a/AccordionBus/AccordionBus/FormMapWithSetBuses.Designer.cs +++ b/AccordionBus/AccordionBus/FormMapWithSetBuses.Designer.cs @@ -51,6 +51,8 @@ this.LoadToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.openFileDialog = new System.Windows.Forms.OpenFileDialog(); this.saveFileDialog = new System.Windows.Forms.SaveFileDialog(); + this.buttonSortByType = new System.Windows.Forms.Button(); + this.buttonSortByColor = new System.Windows.Forms.Button(); this.groupBox1.SuspendLayout(); this.groupBoxMaps.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit(); @@ -59,6 +61,8 @@ // // groupBox1 // + this.groupBox1.Controls.Add(this.buttonSortByColor); + this.groupBox1.Controls.Add(this.buttonSortByType); this.groupBox1.Controls.Add(this.groupBoxMaps); this.groupBox1.Controls.Add(this.buttonRight); this.groupBox1.Controls.Add(this.buttonDown); @@ -72,7 +76,7 @@ this.groupBox1.Dock = System.Windows.Forms.DockStyle.Right; this.groupBox1.Location = new System.Drawing.Point(719, 28); this.groupBox1.Name = "groupBox1"; - this.groupBox1.Size = new System.Drawing.Size(250, 593); + this.groupBox1.Size = new System.Drawing.Size(250, 676); this.groupBox1.TabIndex = 0; this.groupBox1.TabStop = false; this.groupBox1.Text = "Инструменты"; @@ -145,7 +149,7 @@ this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonRight.BackgroundImage = global::AccordionBus.Properties.Resources.arrowRight; this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonRight.Location = new System.Drawing.Point(155, 530); + this.buttonRight.Location = new System.Drawing.Point(151, 621); this.buttonRight.Name = "buttonRight"; this.buttonRight.Size = new System.Drawing.Size(30, 30); this.buttonRight.TabIndex = 10; @@ -157,7 +161,7 @@ this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonDown.BackgroundImage = global::AccordionBus.Properties.Resources.arrowDown; this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonDown.Location = new System.Drawing.Point(119, 530); + this.buttonDown.Location = new System.Drawing.Point(115, 621); this.buttonDown.Name = "buttonDown"; this.buttonDown.Size = new System.Drawing.Size(30, 30); this.buttonDown.TabIndex = 9; @@ -169,7 +173,7 @@ this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonLeft.BackgroundImage = global::AccordionBus.Properties.Resources.arrowLeft; this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonLeft.Location = new System.Drawing.Point(83, 530); + this.buttonLeft.Location = new System.Drawing.Point(79, 621); this.buttonLeft.Name = "buttonLeft"; this.buttonLeft.Size = new System.Drawing.Size(30, 30); this.buttonLeft.TabIndex = 8; @@ -181,7 +185,7 @@ this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonUp.BackgroundImage = global::AccordionBus.Properties.Resources.arrowUp; this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonUp.Location = new System.Drawing.Point(119, 494); + this.buttonUp.Location = new System.Drawing.Point(115, 585); this.buttonUp.Name = "buttonUp"; this.buttonUp.Size = new System.Drawing.Size(30, 30); this.buttonUp.TabIndex = 7; @@ -190,7 +194,7 @@ // // maskedTextBoxPosition // - this.maskedTextBoxPosition.Location = new System.Drawing.Point(12, 338); + this.maskedTextBoxPosition.Location = new System.Drawing.Point(12, 408); this.maskedTextBoxPosition.Mask = "00"; this.maskedTextBoxPosition.Name = "maskedTextBoxPosition"; this.maskedTextBoxPosition.Size = new System.Drawing.Size(226, 27); @@ -198,7 +202,7 @@ // // buttonShowOnMap // - this.buttonShowOnMap.Location = new System.Drawing.Point(12, 461); + this.buttonShowOnMap.Location = new System.Drawing.Point(12, 531); this.buttonShowOnMap.Name = "buttonShowOnMap"; this.buttonShowOnMap.Size = new System.Drawing.Size(226, 29); this.buttonShowOnMap.TabIndex = 5; @@ -208,7 +212,7 @@ // // buttonShowStorage // - this.buttonShowStorage.Location = new System.Drawing.Point(12, 416); + this.buttonShowStorage.Location = new System.Drawing.Point(12, 486); this.buttonShowStorage.Name = "buttonShowStorage"; this.buttonShowStorage.Size = new System.Drawing.Size(226, 29); this.buttonShowStorage.TabIndex = 4; @@ -218,7 +222,7 @@ // // buttonRemoveBus // - this.buttonRemoveBus.Location = new System.Drawing.Point(12, 371); + this.buttonRemoveBus.Location = new System.Drawing.Point(12, 441); this.buttonRemoveBus.Name = "buttonRemoveBus"; this.buttonRemoveBus.Size = new System.Drawing.Size(226, 29); this.buttonRemoveBus.TabIndex = 3; @@ -228,7 +232,7 @@ // // buttonAddBus // - this.buttonAddBus.Location = new System.Drawing.Point(12, 303); + this.buttonAddBus.Location = new System.Drawing.Point(12, 373); this.buttonAddBus.Name = "buttonAddBus"; this.buttonAddBus.Size = new System.Drawing.Size(226, 29); this.buttonAddBus.TabIndex = 1; @@ -241,7 +245,7 @@ this.pictureBox.Dock = System.Windows.Forms.DockStyle.Fill; this.pictureBox.Location = new System.Drawing.Point(0, 28); this.pictureBox.Name = "pictureBox"; - this.pictureBox.Size = new System.Drawing.Size(719, 593); + this.pictureBox.Size = new System.Drawing.Size(719, 676); this.pictureBox.TabIndex = 1; this.pictureBox.TabStop = false; // @@ -267,14 +271,14 @@ // SaveToolStripMenuItem // this.SaveToolStripMenuItem.Name = "SaveToolStripMenuItem"; - this.SaveToolStripMenuItem.Size = new System.Drawing.Size(224, 26); + this.SaveToolStripMenuItem.Size = new System.Drawing.Size(177, 26); this.SaveToolStripMenuItem.Text = "Сохранение"; this.SaveToolStripMenuItem.Click += new System.EventHandler(this.SaveToolStripMenuItem_Click); // // LoadToolStripMenuItem // this.LoadToolStripMenuItem.Name = "LoadToolStripMenuItem"; - this.LoadToolStripMenuItem.Size = new System.Drawing.Size(224, 26); + this.LoadToolStripMenuItem.Size = new System.Drawing.Size(177, 26); this.LoadToolStripMenuItem.Text = "Загрузка"; this.LoadToolStripMenuItem.Click += new System.EventHandler(this.LoadToolStripMenuItem_Click); // @@ -286,11 +290,31 @@ // this.saveFileDialog.Filter = "txt file | *.txt"; // + // buttonSortByType + // + this.buttonSortByType.Location = new System.Drawing.Point(12, 291); + this.buttonSortByType.Name = "buttonSortByType"; + this.buttonSortByType.Size = new System.Drawing.Size(226, 29); + this.buttonSortByType.TabIndex = 12; + this.buttonSortByType.Text = "Сортировать по типу"; + this.buttonSortByType.UseVisualStyleBackColor = true; + this.buttonSortByType.Click += new System.EventHandler(this.ButtonSortByType_Click); + // + // buttonSortByColor + // + this.buttonSortByColor.Location = new System.Drawing.Point(12, 326); + this.buttonSortByColor.Name = "buttonSortByColor"; + this.buttonSortByColor.Size = new System.Drawing.Size(226, 29); + this.buttonSortByColor.TabIndex = 13; + this.buttonSortByColor.Text = "Сортировать по цвету"; + this.buttonSortByColor.UseVisualStyleBackColor = true; + this.buttonSortByColor.Click += new System.EventHandler(this.ButtonSortByColor_Click); + // // FormMapWithSetBuses // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(969, 621); + this.ClientSize = new System.Drawing.Size(969, 704); this.Controls.Add(this.pictureBox); this.Controls.Add(this.groupBox1); this.Controls.Add(this.menuStrip); @@ -334,5 +358,7 @@ private ToolStripMenuItem LoadToolStripMenuItem; private OpenFileDialog openFileDialog; private SaveFileDialog saveFileDialog; + private Button buttonSortByColor; + private Button buttonSortByType; } } \ No newline at end of file diff --git a/AccordionBus/AccordionBus/FormMapWithSetBuses.cs b/AccordionBus/AccordionBus/FormMapWithSetBuses.cs index e4ab379..e83a7dc 100644 --- a/AccordionBus/AccordionBus/FormMapWithSetBuses.cs +++ b/AccordionBus/AccordionBus/FormMapWithSetBuses.cs @@ -157,6 +157,11 @@ namespace AccordionBus _logger.LogWarning($"Ошибка переполнения хранилища: {ex.Message}"); MessageBox.Show($"Ошибка переполнения хранилища: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); } + catch (ArgumentException ex) + { + _logger.LogWarning($"Ошибка добавления: {ex.Message}"); + MessageBox.Show($"Ошибка добавления: {ex.Message}"); + } catch (Exception ex) { _logger.LogWarning($"Неизвестная ошибка: {ex.Message}"); @@ -310,5 +315,33 @@ namespace AccordionBus } } } + /// + /// Сортировка по типу + /// + /// + /// + private void ButtonSortByType_Click(object sender, EventArgs e) + { + if (listBoxMaps.SelectedIndex == -1) + { + return; + } + _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].Sort(new BusCompareByType()); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); + } + /// + /// Сортировка по цвету + /// + /// + /// + private void ButtonSortByColor_Click(object sender, EventArgs e) + { + if (listBoxMaps.SelectedIndex == -1) + { + return; + } + _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].Sort(new BusCompareByColor()); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); + } } } diff --git a/AccordionBus/AccordionBus/FormMapWithSetBuses.resx b/AccordionBus/AccordionBus/FormMapWithSetBuses.resx index 985a3e4..77f1fe0 100644 --- a/AccordionBus/AccordionBus/FormMapWithSetBuses.resx +++ b/AccordionBus/AccordionBus/FormMapWithSetBuses.resx @@ -66,4 +66,7 @@ 311, 17 + + 25 + \ No newline at end of file diff --git a/AccordionBus/AccordionBus/IDrawningObject.cs b/AccordionBus/AccordionBus/IDrawningObject.cs index d5a475e..8857f2b 100644 --- a/AccordionBus/AccordionBus/IDrawningObject.cs +++ b/AccordionBus/AccordionBus/IDrawningObject.cs @@ -10,7 +10,7 @@ namespace AccordionBus /// /// Интерфейс для работы с объектом, прорисовываемым на форме /// - internal interface IDrawningObject + internal interface IDrawningObject : IEquatable { /// /// Шаг перемещения объекта diff --git a/AccordionBus/AccordionBus/MapWithSetBusesGeneric.cs b/AccordionBus/AccordionBus/MapWithSetBusesGeneric.cs index 9f71cf2..498a6f2 100644 --- a/AccordionBus/AccordionBus/MapWithSetBusesGeneric.cs +++ b/AccordionBus/AccordionBus/MapWithSetBusesGeneric.cs @@ -13,7 +13,7 @@ namespace AccordionBus /// /// internal class MapWithSetBusesGeneric - where T : class, IDrawningObject + where T : class, IDrawningObject, IEquatable where U : AbstractMap { /// @@ -139,6 +139,14 @@ namespace AccordionBus } } /// + /// Сортировка + /// + /// + public void Sort(IComparer comparer) + { + _setBuses.SortSet(comparer); + } + /// /// "Взбалтываем" набор, чтобы все элементы оказались в начале /// private void Shaking() diff --git a/AccordionBus/AccordionBus/SetBusesGeneric.cs b/AccordionBus/AccordionBus/SetBusesGeneric.cs index ac8ebe7..4e9cc11 100644 --- a/AccordionBus/AccordionBus/SetBusesGeneric.cs +++ b/AccordionBus/AccordionBus/SetBusesGeneric.cs @@ -11,7 +11,7 @@ namespace AccordionBus /// /// internal class SetBusesGeneric - where T : class + where T : class, IEquatable { /// /// Список объектов, которые храним @@ -49,6 +49,11 @@ namespace AccordionBus /// Возвращает позицию вставленного объекта, либо -1 если его не удалось вставить public int Insert(T bus, int position) { + // Проверка на уникальность + if (_places.Contains(bus)) + { + throw new ArgumentException("Объект с такими характеристиками уже существует в наборе."); + } //проверка позиции if (Count == _maxCount) { @@ -117,5 +122,17 @@ namespace AccordionBus } } } + /// + /// Сортировка набора объектов + /// + /// + public void SortSet(IComparer comparer) + { + if (comparer == null) + { + return; + } + _places.Sort(comparer); + } } } \ No newline at end of file