From f510c85dc4e7a380607e13c6968533ddd56d0315 Mon Sep 17 00:00:00 2001 From: Sem730 Date: Tue, 6 Dec 2022 09:48:20 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9B=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D0=BD=D0=B0=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=B0=208?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ProjectLocomotive/AbstractMap.cs | 25 +++++- .../ProjectLocomotive/DrawningObject.cs | 46 +++++++++-- .../FormLocomotiveConfig.Designer.cs | 3 +- .../FormMapWithSetLocomotives.Designer.cs | 50 +++++++++--- .../FormMapWithSetLocomotives.cs | 23 ++++++ .../ProjectLocomotive/IDrawningObject.cs | 2 +- .../LocomotiveCompareByColor.cs | 78 +++++++++++++++++++ .../LocomotiveCompareByType.cs | 55 +++++++++++++ .../MapWithSetLocomotivesGeneric.cs | 6 +- .../SetLocomotivesGeneric.cs | 11 ++- 10 files changed, 277 insertions(+), 22 deletions(-) create mode 100644 ProjectLocomotive/ProjectLocomotive/LocomotiveCompareByColor.cs create mode 100644 ProjectLocomotive/ProjectLocomotive/LocomotiveCompareByType.cs diff --git a/ProjectLocomotive/ProjectLocomotive/AbstractMap.cs b/ProjectLocomotive/ProjectLocomotive/AbstractMap.cs index c7d0a44..6101ae0 100644 --- a/ProjectLocomotive/ProjectLocomotive/AbstractMap.cs +++ b/ProjectLocomotive/ProjectLocomotive/AbstractMap.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace ProjectLocomotive { - internal abstract class AbstractMap + internal abstract class AbstractMap : IEquatable { private IDrawningObject _drawningObject = null; protected int[,] _map = null; @@ -137,5 +137,28 @@ namespace ProjectLocomotive 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; + } } } diff --git a/ProjectLocomotive/ProjectLocomotive/DrawningObject.cs b/ProjectLocomotive/ProjectLocomotive/DrawningObject.cs index 2fb578b..357912b 100644 --- a/ProjectLocomotive/ProjectLocomotive/DrawningObject.cs +++ b/ProjectLocomotive/ProjectLocomotive/DrawningObject.cs @@ -14,6 +14,8 @@ namespace ProjectLocomotive _loc = loc; } public float Step => _loc?.Locomotivе?.Step ?? 0; + + public DrawningLocomotive GetLocomotive => _loc; public (float Left, float Right, float Top, float Bottom) GetCurrentPosition() { return _loc?.GetCurrentPosition() ?? default; @@ -26,12 +28,46 @@ namespace ProjectLocomotive { _loc.SetPosition(x, y, width, height); } - void IDrawningObject.DrawningObject(Graphics g) - { - // TODO - _loc.DrawTransport(g); - } public string getInfo() => _loc?.getDataForSave(); public static IDrawningObject Create(string data) => new DrawningObject(data.createDrawningLocomotive()); + public bool Equals(IDrawningObject? other) + { + if (other == null) + { + return false; + } + var otherLocomotive = other as DrawningObject; + if (otherLocomotive == null) + { + return false; + } + var locomotive = _loc.Locomotivе; + var otherLocomotiveLocomotive = otherLocomotive._loc.Locomotivе; + if (locomotive.GetType().Name != otherLocomotiveLocomotive.GetType().Name) return false; + if (locomotive.Speed != otherLocomotiveLocomotive.Speed) + { + return false; + } + if (locomotive.Weight != otherLocomotiveLocomotive.Weight) + { + return false; + } + if (locomotive.BodyColor != otherLocomotiveLocomotive.BodyColor) + { + return false; + } + // проверка в случае продвинутого объекта + if (locomotive is EntityElectricLocomotive entityWarmlyLocomotive && otherLocomotiveLocomotive is EntityElectricLocomotive otherEntityWarmlyLocomotive) + { + if (entityWarmlyLocomotive.DopColor != otherEntityWarmlyLocomotive.DopColor) return false; + if (entityWarmlyLocomotive.ElectroLines != otherEntityWarmlyLocomotive.ElectroLines) return false; + if (entityWarmlyLocomotive.ElectroBattery != otherEntityWarmlyLocomotive.ElectroBattery) return false; + } + return true; + } + void IDrawningObject.DrawningObject(Graphics g) + { + _loc.DrawTransport(g); + } } } diff --git a/ProjectLocomotive/ProjectLocomotive/FormLocomotiveConfig.Designer.cs b/ProjectLocomotive/ProjectLocomotive/FormLocomotiveConfig.Designer.cs index 21246c9..60344dd 100644 --- a/ProjectLocomotive/ProjectLocomotive/FormLocomotiveConfig.Designer.cs +++ b/ProjectLocomotive/ProjectLocomotive/FormLocomotiveConfig.Designer.cs @@ -1,4 +1,5 @@ -namespace ProjectLocomotive + +namespace ProjectLocomotive { partial class FormLocomotiveConfig { diff --git a/ProjectLocomotive/ProjectLocomotive/FormMapWithSetLocomotives.Designer.cs b/ProjectLocomotive/ProjectLocomotive/FormMapWithSetLocomotives.Designer.cs index 0d17b61..9808edf 100644 --- a/ProjectLocomotive/ProjectLocomotive/FormMapWithSetLocomotives.Designer.cs +++ b/ProjectLocomotive/ProjectLocomotive/FormMapWithSetLocomotives.Designer.cs @@ -28,6 +28,8 @@ private void InitializeComponent() { this.groupBoxTools = new System.Windows.Forms.GroupBox(); + this.ButtonSortByColor = new System.Windows.Forms.Button(); + this.ButtonSortByType = new System.Windows.Forms.Button(); this.groupBox1 = new System.Windows.Forms.GroupBox(); this.buttonDeleteMap = new System.Windows.Forms.Button(); this.listBoxMaps = new System.Windows.Forms.ListBox(); @@ -58,6 +60,8 @@ // // groupBoxTools // + this.groupBoxTools.Controls.Add(this.ButtonSortByColor); + this.groupBoxTools.Controls.Add(this.ButtonSortByType); this.groupBoxTools.Controls.Add(this.groupBox1); this.groupBoxTools.Controls.Add(this.buttonLeft); this.groupBoxTools.Controls.Add(this.buttonRight); @@ -69,15 +73,35 @@ this.groupBoxTools.Controls.Add(this.maskedTextBoxPosition); this.groupBoxTools.Controls.Add(this.buttonAddLocomotive); this.groupBoxTools.Dock = System.Windows.Forms.DockStyle.Right; - this.groupBoxTools.Location = new System.Drawing.Point(577, 33); + this.groupBoxTools.Location = new System.Drawing.Point(585, 33); this.groupBoxTools.Margin = new System.Windows.Forms.Padding(4); this.groupBoxTools.Name = "groupBoxTools"; this.groupBoxTools.Padding = new System.Windows.Forms.Padding(4); - this.groupBoxTools.Size = new System.Drawing.Size(220, 510); + this.groupBoxTools.Size = new System.Drawing.Size(220, 481); this.groupBoxTools.TabIndex = 0; this.groupBoxTools.TabStop = false; this.groupBoxTools.Text = "Инструменты"; // + // ButtonSortByColor + // + this.ButtonSortByColor.Location = new System.Drawing.Point(7, 524); + this.ButtonSortByColor.Name = "ButtonSortByColor"; + this.ButtonSortByColor.Size = new System.Drawing.Size(207, 37); + this.ButtonSortByColor.TabIndex = 10; + this.ButtonSortByColor.Text = "Сортировка по цвету"; + this.ButtonSortByColor.UseVisualStyleBackColor = true; + this.ButtonSortByColor.Click += new System.EventHandler(this.ButtonSortByColor_Click); + // + // ButtonSortByType + // + this.ButtonSortByType.Location = new System.Drawing.Point(6, 567); + this.ButtonSortByType.Name = "ButtonSortByType"; + this.ButtonSortByType.Size = new System.Drawing.Size(207, 39); + this.ButtonSortByType.TabIndex = 9; + this.ButtonSortByType.Text = "Сортировка по типу"; + this.ButtonSortByType.UseVisualStyleBackColor = true; + this.ButtonSortByType.Click += new System.EventHandler(this.ButtonSortByType_Click); + // // groupBox1 // this.groupBox1.Controls.Add(this.buttonDeleteMap); @@ -145,7 +169,7 @@ // this.buttonLeft.BackgroundImage = global::ProjectLocomotive.Properties.Resources.left; this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonLeft.Location = new System.Drawing.Point(22, 591); + this.buttonLeft.Location = new System.Drawing.Point(42, 661); this.buttonLeft.Margin = new System.Windows.Forms.Padding(4); this.buttonLeft.Name = "buttonLeft"; this.buttonLeft.Size = new System.Drawing.Size(50, 50); @@ -157,7 +181,7 @@ // this.buttonRight.BackgroundImage = global::ProjectLocomotive.Properties.Resources.right; this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonRight.Location = new System.Drawing.Point(138, 591); + this.buttonRight.Location = new System.Drawing.Point(157, 661); this.buttonRight.Margin = new System.Windows.Forms.Padding(4); this.buttonRight.Name = "buttonRight"; this.buttonRight.Size = new System.Drawing.Size(50, 50); @@ -169,7 +193,7 @@ // this.buttonDown.BackgroundImage = global::ProjectLocomotive.Properties.Resources.down; this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonDown.Location = new System.Drawing.Point(80, 591); + this.buttonDown.Location = new System.Drawing.Point(100, 661); this.buttonDown.Margin = new System.Windows.Forms.Padding(4); this.buttonDown.Name = "buttonDown"; this.buttonDown.Size = new System.Drawing.Size(50, 50); @@ -181,7 +205,7 @@ // this.buttonUp.BackgroundImage = global::ProjectLocomotive.Properties.Resources.up; this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonUp.Location = new System.Drawing.Point(80, 533); + this.buttonUp.Location = new System.Drawing.Point(100, 613); this.buttonUp.Margin = new System.Windows.Forms.Padding(4); this.buttonUp.Name = "buttonUp"; this.buttonUp.Size = new System.Drawing.Size(50, 50); @@ -191,7 +215,7 @@ // // buttonShowOnMap // - this.buttonShowOnMap.Location = new System.Drawing.Point(0, 489); + this.buttonShowOnMap.Location = new System.Drawing.Point(0, 481); this.buttonShowOnMap.Margin = new System.Windows.Forms.Padding(4); this.buttonShowOnMap.Name = "buttonShowOnMap"; this.buttonShowOnMap.Size = new System.Drawing.Size(220, 36); @@ -202,7 +226,7 @@ // // buttonShowStorage // - this.buttonShowStorage.Location = new System.Drawing.Point(7, 418); + this.buttonShowStorage.Location = new System.Drawing.Point(7, 410); this.buttonShowStorage.Margin = new System.Windows.Forms.Padding(4); this.buttonShowStorage.Name = "buttonShowStorage"; this.buttonShowStorage.Size = new System.Drawing.Size(208, 63); @@ -213,7 +237,7 @@ // // buttonRemoveLocomotive // - this.buttonRemoveLocomotive.Location = new System.Drawing.Point(7, 364); + this.buttonRemoveLocomotive.Location = new System.Drawing.Point(7, 356); this.buttonRemoveLocomotive.Margin = new System.Windows.Forms.Padding(4); this.buttonRemoveLocomotive.Name = "buttonRemoveLocomotive"; this.buttonRemoveLocomotive.Size = new System.Drawing.Size(207, 46); @@ -248,7 +272,7 @@ this.pictureBox.Location = new System.Drawing.Point(0, 33); this.pictureBox.Margin = new System.Windows.Forms.Padding(4); this.pictureBox.Name = "pictureBox"; - this.pictureBox.Size = new System.Drawing.Size(577, 510); + this.pictureBox.Size = new System.Drawing.Size(585, 481); this.pictureBox.TabIndex = 1; this.pictureBox.TabStop = false; // @@ -259,7 +283,7 @@ this.fileToolStripMenuItem}); this.menuStrip.Location = new System.Drawing.Point(0, 0); this.menuStrip.Name = "menuStrip"; - this.menuStrip.Size = new System.Drawing.Size(797, 33); + this.menuStrip.Size = new System.Drawing.Size(805, 33); this.menuStrip.TabIndex = 2; // // fileToolStripMenuItem @@ -297,7 +321,7 @@ // this.AutoScaleDimensions = new System.Drawing.SizeF(10F, 25F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(797, 543); + this.ClientSize = new System.Drawing.Size(805, 514); this.Controls.Add(this.pictureBox); this.Controls.Add(this.groupBoxTools); this.Controls.Add(this.menuStrip); @@ -340,5 +364,7 @@ private ToolStripMenuItem loadToolStripMenuItem; private OpenFileDialog loadFileDialog; private SaveFileDialog saveFileDialog; + private Button ButtonSortByType; + private Button ButtonSortByColor; } } \ No newline at end of file diff --git a/ProjectLocomotive/ProjectLocomotive/FormMapWithSetLocomotives.cs b/ProjectLocomotive/ProjectLocomotive/FormMapWithSetLocomotives.cs index 1ad7f37..0502bed 100644 --- a/ProjectLocomotive/ProjectLocomotive/FormMapWithSetLocomotives.cs +++ b/ProjectLocomotive/ProjectLocomotive/FormMapWithSetLocomotives.cs @@ -90,6 +90,11 @@ namespace ProjectLocomotive _logger.LogWarning("Ошибка переполнения хранилища: {0}", ex.Message); MessageBox.Show($"Ошибка переполнения хранилища: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); } + catch (ArgumentException ex) + { + _logger.LogWarning("Ошибка добавления: {0}. Объект: {@Ship}", ex.Message, locomotive); + MessageBox.Show(ex.Message, "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); + } } /// Удаление объекта private void buttonRemoveLocomotive_Click(object sender, EventArgs e) @@ -249,5 +254,23 @@ namespace ProjectLocomotive } } } + 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/ProjectLocomotive/ProjectLocomotive/IDrawningObject.cs b/ProjectLocomotive/ProjectLocomotive/IDrawningObject.cs index 50c50b6..ccc2d0f 100644 --- a/ProjectLocomotive/ProjectLocomotive/IDrawningObject.cs +++ b/ProjectLocomotive/ProjectLocomotive/IDrawningObject.cs @@ -9,7 +9,7 @@ namespace ProjectLocomotive /// /// Интерфейс для работы с объектом, прорисовываемым на форме /// - internal interface IDrawningObject + internal interface IDrawningObject : IEquatable { /// /// Шаг перемещения объекта diff --git a/ProjectLocomotive/ProjectLocomotive/LocomotiveCompareByColor.cs b/ProjectLocomotive/ProjectLocomotive/LocomotiveCompareByColor.cs new file mode 100644 index 0000000..8df21b0 --- /dev/null +++ b/ProjectLocomotive/ProjectLocomotive/LocomotiveCompareByColor.cs @@ -0,0 +1,78 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectLocomotive +{ + 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 DrawningObject; + var yLocomotive = y as DrawningObject; + if (xLocomotive == null && yLocomotive == null) + { + return 0; + } + if (xLocomotive == null && yLocomotive != null) + { + return 1; + } + if (xLocomotive != null && yLocomotive == null) + { + return -1; + } + + if (xLocomotive.GetLocomotive.Locomotivе.BodyColor.R.CompareTo(yLocomotive.GetLocomotive.Locomotivе.BodyColor.R) != 0) + { + return xLocomotive.GetLocomotive.Locomotivе.BodyColor.R.CompareTo(yLocomotive.GetLocomotive.Locomotivе.BodyColor.R); + } + if (xLocomotive.GetLocomotive.Locomotivе.BodyColor.G.CompareTo(yLocomotive.GetLocomotive.Locomotivе.BodyColor.G) != 0) + { + return xLocomotive.GetLocomotive.Locomotivе.BodyColor.G.CompareTo(yLocomotive.GetLocomotive.Locomotivе.BodyColor.G); + } + if (xLocomotive.GetLocomotive.Locomotivе.BodyColor.B.CompareTo(yLocomotive.GetLocomotive.Locomotivе.BodyColor.B) != 0) + { + return xLocomotive.GetLocomotive.Locomotivе.BodyColor.B.CompareTo(yLocomotive.GetLocomotive.Locomotivе.BodyColor.B); + } + + if (xLocomotive.GetLocomotive.Locomotivе is EntityElectricLocomotive xWarmlyEntity && yLocomotive.GetLocomotive.Locomotivе is EntityElectricLocomotive yWarmlyEntity) + { + if (xWarmlyEntity.DopColor.R.CompareTo(yWarmlyEntity.DopColor.R) != 0) + { + return xWarmlyEntity.DopColor.R.CompareTo(yWarmlyEntity.DopColor.R); + } + if (xWarmlyEntity.DopColor.G.CompareTo(yWarmlyEntity.DopColor.G) != 0) + { + return xWarmlyEntity.DopColor.G.CompareTo(yWarmlyEntity.DopColor.G); + } + if (xWarmlyEntity.DopColor.B.CompareTo(yWarmlyEntity.DopColor.B) != 0) + { + return xWarmlyEntity.DopColor.B.CompareTo(yWarmlyEntity.DopColor.B); + } + } + + var speedCompare = xLocomotive.GetLocomotive.Locomotivе.Speed.CompareTo(yLocomotive.GetLocomotive.Locomotivе.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return xLocomotive.GetLocomotive.Locomotivе.Weight.CompareTo(yLocomotive.GetLocomotive.Locomotivе.Weight); + + } + } +} diff --git a/ProjectLocomotive/ProjectLocomotive/LocomotiveCompareByType.cs b/ProjectLocomotive/ProjectLocomotive/LocomotiveCompareByType.cs new file mode 100644 index 0000000..2d9025a --- /dev/null +++ b/ProjectLocomotive/ProjectLocomotive/LocomotiveCompareByType.cs @@ -0,0 +1,55 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectLocomotive +{ + 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 DrawningObject; + var yLocomotive = y as DrawningObject; + 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.Locomotivе.Speed.CompareTo(yLocomotive.GetLocomotive.Locomotivе.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return xLocomotive.GetLocomotive.Locomotivе.Weight.CompareTo(yLocomotive.GetLocomotive.Locomotivе.Weight); + } + } +} diff --git a/ProjectLocomotive/ProjectLocomotive/MapWithSetLocomotivesGeneric.cs b/ProjectLocomotive/ProjectLocomotive/MapWithSetLocomotivesGeneric.cs index a6aa393..c85dddf 100644 --- a/ProjectLocomotive/ProjectLocomotive/MapWithSetLocomotivesGeneric.cs +++ b/ProjectLocomotive/ProjectLocomotive/MapWithSetLocomotivesGeneric.cs @@ -7,7 +7,7 @@ using System.Threading.Tasks; namespace ProjectLocomotive { internal class MapWithSetLocomotivesGeneric - where T : class, IDrawningObject + where T : class, IDrawningObject, IEquatable where U : AbstractMap { /// Ширина окна отрисовки @@ -165,5 +165,9 @@ namespace ProjectLocomotive _setLocomotives.Insert(DrawningObject.Create(rec) as T); } } + public void Sort(IComparer comparer) + { + _setLocomotives.SortSet(comparer); + } } } diff --git a/ProjectLocomotive/ProjectLocomotive/SetLocomotivesGeneric.cs b/ProjectLocomotive/ProjectLocomotive/SetLocomotivesGeneric.cs index 6b0e991..f5c820e 100644 --- a/ProjectLocomotive/ProjectLocomotive/SetLocomotivesGeneric.cs +++ b/ProjectLocomotive/ProjectLocomotive/SetLocomotivesGeneric.cs @@ -11,7 +11,7 @@ namespace ProjectLocomotive /// /// internal class SetLocomotivesGeneric - where T : class + where T : class, IEquatable { /// Список хранимых объектов private readonly List _places; @@ -32,6 +32,7 @@ namespace ProjectLocomotive /// Добавление объекта в набор на конкретную позицию public int Insert(T locomotive, int position) { + if (_places.Contains(locomotive)) return -1; // Проверка на уникальность if (position < 0) return -1; if (Count >= _maxCount) throw new StorageOverflowException(_maxCount); _places.Insert(position, locomotive); @@ -70,5 +71,13 @@ namespace ProjectLocomotive } } } + public void SortSet(IComparer comparer) + { + if (comparer == null) + { + return; + } + _places.Sort(comparer); + } } }