diff --git a/WarmlyLocomotive/WarmlyLocomotive/AbstractMap.cs b/WarmlyLocomotive/WarmlyLocomotive/AbstractMap.cs index d808c44..ecc82a1 100644 --- a/WarmlyLocomotive/WarmlyLocomotive/AbstractMap.cs +++ b/WarmlyLocomotive/WarmlyLocomotive/AbstractMap.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace WarmlyLocomotive { - internal abstract class AbstractMap + internal abstract class AbstractMap : IEquatable { private IDrawningObject _drawningObject = null; protected int[,] _map = null; @@ -136,5 +136,44 @@ namespace WarmlyLocomotive 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; + } + var otherMap = other as AbstractMap; + if (otherMap == null) + { + return false; + } + if (_width != otherMap._width) + { + return false; + } + if (_height != otherMap._height) + { + return false; + } + if (_size_x != otherMap._size_x) + { + return false; + } + if (_size_y != otherMap._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] != otherMap._map[i, j]) + { + return false; + } + } + } + return true; + } } } diff --git a/WarmlyLocomotive/WarmlyLocomotive/DrawningObjectLocomotive.cs b/WarmlyLocomotive/WarmlyLocomotive/DrawningObjectLocomotive.cs index ad0c7d7..2e317c8 100644 --- a/WarmlyLocomotive/WarmlyLocomotive/DrawningObjectLocomotive.cs +++ b/WarmlyLocomotive/WarmlyLocomotive/DrawningObjectLocomotive.cs @@ -10,6 +10,7 @@ namespace WarmlyLocomotive { private DrawningLocomotive _locomotive = null; public float Step => _locomotive?.Locomotive?.Step ?? 0; + public DrawningLocomotive GetLocomotive => _locomotive; public DrawningObjectLocomotive(DrawningLocomotive locomotive) { _locomotive = locomotive; @@ -32,5 +33,55 @@ namespace WarmlyLocomotive } public string GetInfo() => _locomotive?.GetDataForSave(); public static IDrawningObject Create(string data) => new DrawningObjectLocomotive(data.CreateDrawningLocomotive()); + public bool Equals(IDrawningObject? other) + { + if (other == null) + { + return false; + } + var otherLocomotive = other as DrawningObjectLocomotive; + if (otherLocomotive == null) + { + return false; + } + var locomotive = _locomotive.Locomotive; + var otherLocomotiveLocomotive = otherLocomotive._locomotive.Locomotive; + if (locomotive.Speed != otherLocomotiveLocomotive.Speed) + { + return false; + } + if (locomotive.Weight != otherLocomotiveLocomotive.Weight) + { + return false; + } + if (locomotive.BodyColor != otherLocomotiveLocomotive.BodyColor) + { + return false; + } + if(locomotive is EntityWarmlyLocomotive warmlyLocomotive && otherLocomotiveLocomotive is EntityWarmlyLocomotive otherWarmlyLocomotive) + { + if(warmlyLocomotive.Pipe != otherWarmlyLocomotive.Pipe) + { + return false; + } + if (warmlyLocomotive.FuelCompartment != otherWarmlyLocomotive.FuelCompartment) + { + return false; + } + if (warmlyLocomotive.WarmlyLines != otherWarmlyLocomotive.WarmlyLines) + { + return false; + } + if (warmlyLocomotive.DopColor != otherWarmlyLocomotive.DopColor) + { + return false; + } + } + else if(locomotive is EntityWarmlyLocomotive || otherLocomotiveLocomotive is EntityWarmlyLocomotive) + { + return false; + } + return true; + } } } diff --git a/WarmlyLocomotive/WarmlyLocomotive/FormMapWithSetLocomotives.Designer.cs b/WarmlyLocomotive/WarmlyLocomotive/FormMapWithSetLocomotives.Designer.cs index dedcd57..cc81862 100644 --- a/WarmlyLocomotive/WarmlyLocomotive/FormMapWithSetLocomotives.Designer.cs +++ b/WarmlyLocomotive/WarmlyLocomotive/FormMapWithSetLocomotives.Designer.cs @@ -29,6 +29,8 @@ private void InitializeComponent() { this.groupBox = new System.Windows.Forms.GroupBox(); + this.buttonSortByColor = new System.Windows.Forms.Button(); + this.buttonSortByType = new System.Windows.Forms.Button(); this.groupBoxMaps = new System.Windows.Forms.GroupBox(); this.buttonDeleteMap = new System.Windows.Forms.Button(); this.listBoxMaps = new System.Windows.Forms.ListBox(); @@ -59,6 +61,8 @@ // // groupBox // + this.groupBox.Controls.Add(this.buttonSortByColor); + this.groupBox.Controls.Add(this.buttonSortByType); this.groupBox.Controls.Add(this.groupBoxMaps); this.groupBox.Controls.Add(this.buttonDown); this.groupBox.Controls.Add(this.buttonRight); @@ -72,11 +76,31 @@ this.groupBox.Dock = System.Windows.Forms.DockStyle.Right; this.groupBox.Location = new System.Drawing.Point(668, 24); this.groupBox.Name = "groupBox"; - this.groupBox.Size = new System.Drawing.Size(200, 638); + this.groupBox.Size = new System.Drawing.Size(200, 693); this.groupBox.TabIndex = 0; this.groupBox.TabStop = false; this.groupBox.Text = "Инструменты"; // + // buttonSortByColor + // + this.buttonSortByColor.Location = new System.Drawing.Point(19, 340); + this.buttonSortByColor.Name = "buttonSortByColor"; + this.buttonSortByColor.Size = new System.Drawing.Size(169, 30); + this.buttonSortByColor.TabIndex = 12; + this.buttonSortByColor.Text = "Сортировать по цвету"; + this.buttonSortByColor.UseVisualStyleBackColor = true; + this.buttonSortByColor.Click += new System.EventHandler(this.ButtonSortByColor_Click); + // + // buttonSortByType + // + this.buttonSortByType.Location = new System.Drawing.Point(19, 304); + this.buttonSortByType.Name = "buttonSortByType"; + this.buttonSortByType.Size = new System.Drawing.Size(169, 30); + this.buttonSortByType.TabIndex = 11; + this.buttonSortByType.Text = "Сортировать по типу"; + this.buttonSortByType.UseVisualStyleBackColor = true; + this.buttonSortByType.Click += new System.EventHandler(this.ButtonSortByType_Click); + // // groupBoxMaps // this.groupBoxMaps.Controls.Add(this.buttonDeleteMap); @@ -147,7 +171,7 @@ this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonDown.BackgroundImage = global::WarmlyLocomotive.Properties.Resources.arrowDown; this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonDown.Location = new System.Drawing.Point(87, 591); + this.buttonDown.Location = new System.Drawing.Point(87, 646); this.buttonDown.Name = "buttonDown"; this.buttonDown.Size = new System.Drawing.Size(30, 30); this.buttonDown.TabIndex = 9; @@ -159,7 +183,7 @@ this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonRight.BackgroundImage = global::WarmlyLocomotive.Properties.Resources.arrowRight; this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonRight.Location = new System.Drawing.Point(123, 591); + this.buttonRight.Location = new System.Drawing.Point(123, 646); this.buttonRight.Name = "buttonRight"; this.buttonRight.Size = new System.Drawing.Size(30, 30); this.buttonRight.TabIndex = 8; @@ -171,7 +195,7 @@ this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonLeft.BackgroundImage = global::WarmlyLocomotive.Properties.Resources.arrowLeft; this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonLeft.Location = new System.Drawing.Point(51, 591); + this.buttonLeft.Location = new System.Drawing.Point(51, 646); this.buttonLeft.Name = "buttonLeft"; this.buttonLeft.Size = new System.Drawing.Size(30, 30); this.buttonLeft.TabIndex = 7; @@ -183,7 +207,7 @@ this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonUp.BackgroundImage = global::WarmlyLocomotive.Properties.Resources.arrowUp; this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonUp.Location = new System.Drawing.Point(87, 555); + this.buttonUp.Location = new System.Drawing.Point(87, 610); this.buttonUp.Name = "buttonUp"; this.buttonUp.Size = new System.Drawing.Size(30, 30); this.buttonUp.TabIndex = 6; @@ -193,7 +217,7 @@ // buttonShowOnMap // this.buttonShowOnMap.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.buttonShowOnMap.Location = new System.Drawing.Point(19, 500); + this.buttonShowOnMap.Location = new System.Drawing.Point(19, 554); this.buttonShowOnMap.Name = "buttonShowOnMap"; this.buttonShowOnMap.Size = new System.Drawing.Size(169, 30); this.buttonShowOnMap.TabIndex = 5; @@ -204,7 +228,7 @@ // buttonShowStorage // this.buttonShowStorage.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.buttonShowStorage.Location = new System.Drawing.Point(19, 452); + this.buttonShowStorage.Location = new System.Drawing.Point(19, 506); this.buttonShowStorage.Name = "buttonShowStorage"; this.buttonShowStorage.Size = new System.Drawing.Size(169, 30); this.buttonShowStorage.TabIndex = 4; @@ -215,7 +239,7 @@ // buttonRemoveLocomotive // this.buttonRemoveLocomotive.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.buttonRemoveLocomotive.Location = new System.Drawing.Point(19, 406); + this.buttonRemoveLocomotive.Location = new System.Drawing.Point(19, 460); this.buttonRemoveLocomotive.Name = "buttonRemoveLocomotive"; this.buttonRemoveLocomotive.Size = new System.Drawing.Size(169, 30); this.buttonRemoveLocomotive.TabIndex = 3; @@ -226,7 +250,7 @@ // maskedTextBoxPosition // this.maskedTextBoxPosition.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.maskedTextBoxPosition.Location = new System.Drawing.Point(19, 377); + this.maskedTextBoxPosition.Location = new System.Drawing.Point(19, 431); this.maskedTextBoxPosition.Mask = "00"; this.maskedTextBoxPosition.Name = "maskedTextBoxPosition"; this.maskedTextBoxPosition.Size = new System.Drawing.Size(169, 23); @@ -235,7 +259,7 @@ // buttonAddLocomotive // this.buttonAddLocomotive.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.buttonAddLocomotive.Location = new System.Drawing.Point(19, 341); + this.buttonAddLocomotive.Location = new System.Drawing.Point(19, 395); this.buttonAddLocomotive.Name = "buttonAddLocomotive"; this.buttonAddLocomotive.Size = new System.Drawing.Size(169, 30); this.buttonAddLocomotive.TabIndex = 1; @@ -248,7 +272,7 @@ this.pictureBox.Dock = System.Windows.Forms.DockStyle.Fill; this.pictureBox.Location = new System.Drawing.Point(0, 24); this.pictureBox.Name = "pictureBox"; - this.pictureBox.Size = new System.Drawing.Size(668, 638); + this.pictureBox.Size = new System.Drawing.Size(668, 693); this.pictureBox.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize; this.pictureBox.TabIndex = 1; this.pictureBox.TabStop = false; @@ -297,7 +321,7 @@ // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(868, 662); + this.ClientSize = new System.Drawing.Size(868, 717); this.Controls.Add(this.pictureBox); this.Controls.Add(this.groupBox); this.Controls.Add(this.menuStrip); @@ -341,5 +365,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/WarmlyLocomotive/WarmlyLocomotive/FormMapWithSetLocomotives.cs b/WarmlyLocomotive/WarmlyLocomotive/FormMapWithSetLocomotives.cs index 933cd2a..1a13137 100644 --- a/WarmlyLocomotive/WarmlyLocomotive/FormMapWithSetLocomotives.cs +++ b/WarmlyLocomotive/WarmlyLocomotive/FormMapWithSetLocomotives.cs @@ -102,6 +102,11 @@ namespace WarmlyLocomotive _logger.LogWarning($"Ошибка переполнения хранилища: {ex.Message}"); MessageBox.Show($"Ошибка переполнения хранилища: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); } + catch(ArgumentException ex) + { + _logger.LogWarning($"Ошибка при добавлении: {ex.Message}"); + MessageBox.Show($"Ошибка при добавлении: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); + } catch (Exception ex) { _logger.LogWarning($"Неизвестная ошибка: {ex.Message}"); @@ -314,5 +319,33 @@ namespace WarmlyLocomotive } } } + /// + /// Сортировка по типу + /// + /// + /// + private void ButtonSortByType_Click(object sender, EventArgs e) + { + if (listBoxMaps.SelectedIndex == -1) + { + return; + } + _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].Sort(new LocomotiveCompareByType()); + 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 LocomotiveCompareByColor()); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); + } } } diff --git a/WarmlyLocomotive/WarmlyLocomotive/IDrawningObject.cs b/WarmlyLocomotive/WarmlyLocomotive/IDrawningObject.cs index 57e5e4a..46bed5e 100644 --- a/WarmlyLocomotive/WarmlyLocomotive/IDrawningObject.cs +++ b/WarmlyLocomotive/WarmlyLocomotive/IDrawningObject.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace WarmlyLocomotive { - internal interface IDrawningObject + internal interface IDrawningObject : IEquatable { /// /// Шаг перемещения объекта diff --git a/WarmlyLocomotive/WarmlyLocomotive/LocomotiveCompareByColor.cs b/WarmlyLocomotive/WarmlyLocomotive/LocomotiveCompareByColor.cs new file mode 100644 index 0000000..f956ec0 --- /dev/null +++ b/WarmlyLocomotive/WarmlyLocomotive/LocomotiveCompareByColor.cs @@ -0,0 +1,63 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml; + +namespace WarmlyLocomotive +{ + internal class LocomotiveCompareByColor : 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 xLocomotive = x as DrawningObjectLocomotive; + var yLocomotive = y as DrawningObjectLocomotive; + if (xLocomotive == null && yLocomotive == null) + { + return 0; + } + if (xLocomotive == null && yLocomotive != null) + { + return 1; + } + if (xLocomotive != null && yLocomotive == null) + { + return -1; + } + var xEntityLocomotive = xLocomotive.GetLocomotive.Locomotive; + var yEntityLocomotive = yLocomotive.GetLocomotive.Locomotive; + var baseColorCompare = xEntityLocomotive.BodyColor.ToArgb().CompareTo(yEntityLocomotive.BodyColor.ToArgb()); + if(baseColorCompare != 0) + { + return baseColorCompare; + } + if(xEntityLocomotive is EntityWarmlyLocomotive xWarmlyLocomotive && yEntityLocomotive is EntityWarmlyLocomotive yWarmlyLocomotive) + { + var dopColorCompare = xWarmlyLocomotive.DopColor.ToArgb().CompareTo(yWarmlyLocomotive.DopColor.ToArgb()); + if (dopColorCompare != 0) + { + return dopColorCompare; + } + } + var speedCompare = xLocomotive.GetLocomotive.Locomotive.Speed.CompareTo(yLocomotive.GetLocomotive.Locomotive.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return xLocomotive.GetLocomotive.Locomotive.Weight.CompareTo(yLocomotive.GetLocomotive.Locomotive.Weight); + } + } +} diff --git a/WarmlyLocomotive/WarmlyLocomotive/LocomotiveCompareByType.cs b/WarmlyLocomotive/WarmlyLocomotive/LocomotiveCompareByType.cs new file mode 100644 index 0000000..de241d7 --- /dev/null +++ b/WarmlyLocomotive/WarmlyLocomotive/LocomotiveCompareByType.cs @@ -0,0 +1,55 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace WarmlyLocomotive +{ + internal class LocomotiveCompareByType : 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 xLocomotive = x as DrawningObjectLocomotive; + var yLocomotive = y as DrawningObjectLocomotive; + if (xLocomotive == null && yLocomotive == null) + { + return 0; + } + if (xLocomotive == null && yLocomotive != null) + { + return 1; + } + if (xLocomotive != null && yLocomotive == null) + { + return -1; + } + if (xLocomotive.GetLocomotive.GetType().Name != yLocomotive.GetLocomotive.GetType().Name) + { + if (xLocomotive.GetLocomotive.GetType().Name == "DrawningLocomotive") + { + return -1; + } + return 1; + } + var speedCompare = xLocomotive.GetLocomotive.Locomotive.Speed.CompareTo(yLocomotive.GetLocomotive.Locomotive.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return xLocomotive.GetLocomotive.Locomotive.Weight.CompareTo(yLocomotive.GetLocomotive.Locomotive.Weight); + } + } +} diff --git a/WarmlyLocomotive/WarmlyLocomotive/MapWithSetLocomotivesGeneric.cs b/WarmlyLocomotive/WarmlyLocomotive/MapWithSetLocomotivesGeneric.cs index ef78bcd..9519875 100644 --- a/WarmlyLocomotive/WarmlyLocomotive/MapWithSetLocomotivesGeneric.cs +++ b/WarmlyLocomotive/WarmlyLocomotive/MapWithSetLocomotivesGeneric.cs @@ -7,7 +7,7 @@ using System.Threading.Tasks; namespace WarmlyLocomotive { internal class MapWithSetLocomotivesGeneric - where T : class, IDrawningObject + where T : class, IDrawningObject, IEquatable where U : AbstractMap { /// @@ -133,6 +133,14 @@ namespace WarmlyLocomotive } } /// + /// Сортировка + /// + /// + public void Sort(IComparer comparer) + { + _setLocomotives.SortSet(comparer); + } + /// /// "Взбалтываем" набор, чтобы все элементы оказались в начале /// private void Shaking() diff --git a/WarmlyLocomotive/WarmlyLocomotive/SetLocomotivesGeneric.cs b/WarmlyLocomotive/WarmlyLocomotive/SetLocomotivesGeneric.cs index b579117..2739ebc 100644 --- a/WarmlyLocomotive/WarmlyLocomotive/SetLocomotivesGeneric.cs +++ b/WarmlyLocomotive/WarmlyLocomotive/SetLocomotivesGeneric.cs @@ -7,7 +7,7 @@ using System.Threading.Tasks; namespace WarmlyLocomotive { internal class SetLocomotivesGeneric - where T : class + where T : class, IEquatable { /// /// Список объектов, которые храним @@ -44,6 +44,10 @@ namespace WarmlyLocomotive /// public int Insert(T locomotive, int position) { + if (_places.Contains(locomotive)) + { + throw new ArgumentException($"Объект {locomotive} уже присутствует в наборе"); + } if (position < 0 || position > Count || _maxCount == Count) { throw new StorageOverflowException(_maxCount); @@ -107,5 +111,17 @@ namespace WarmlyLocomotive } } } + /// + /// Сортировка набора объектов + /// + /// + public void SortSet(IComparer comparer) + { + if (comparer == null) + { + return; + } + _places.Sort(comparer); + } } }