From 402fb1d315cbe2bb61923359c7e3879784f3933d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=B5=D1=80=D0=B3=D0=B5=D0=B9=20=D0=9F=D0=BE=D0=BB?= =?UTF-8?q?=D0=B5=D0=B2=D0=BE=D0=B9?= Date: Mon, 21 Nov 2022 20:27:15 +0400 Subject: [PATCH] =?UTF-8?q?=D0=92=D1=82=D0=BE=D1=80=D0=BE=D0=B9=20=D1=88?= =?UTF-8?q?=D0=B0=D0=B3=20=D0=B3=D0=BE=D1=82=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ArtilleryCompareByColor.cs | 74 +++++++++++++++++++ .../ArtilleryCompareByType.cs | 64 ++++++++++++++++ .../DrawingObjectArtillery.cs | 2 + .../FormMapWithSetArtilleries.Designer.cs | 50 ++++++++++--- .../FormMapWithSetArtilleries.cs | 20 +++++ .../MapWithSetArtilleriesGeneric.cs | 7 +- .../SetArtilleriesGeneric.cs | 10 +++ 7 files changed, 214 insertions(+), 13 deletions(-) create mode 100644 SelfPropelledArtilleryUnit/ArtilleryCompareByColor.cs create mode 100644 SelfPropelledArtilleryUnit/ArtilleryCompareByType.cs diff --git a/SelfPropelledArtilleryUnit/ArtilleryCompareByColor.cs b/SelfPropelledArtilleryUnit/ArtilleryCompareByColor.cs new file mode 100644 index 0000000..7d7730d --- /dev/null +++ b/SelfPropelledArtilleryUnit/ArtilleryCompareByColor.cs @@ -0,0 +1,74 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Artilleries +{ + internal class ArtilleryCompareByColor : IComparer + { + public int Compare(IDrawingObject? first, IDrawingObject? second) + { + if (first == null && second == null) + { + return 0; + } + + if (first == null && second != null) + { + return 1; + } + + if (first != null && second == null) + { + return -1; + } + + var firstArtillery = first as DrawingObjectArtillery; + var secondArtillery = second as DrawingObjectArtillery; + + if (firstArtillery == null && secondArtillery == null) + { + return 0; + } + + if (firstArtillery == null && secondArtillery != null) + { + return 1; + } + + if (firstArtillery != null && secondArtillery == null) + { + return -1; + } + + int firstColor = firstArtillery.Artillery.Artillery.BodyColor.ToArgb(); + int secondColor = secondArtillery.Artillery.Artillery.BodyColor.ToArgb(); + + if (firstColor != secondColor) + { + return firstColor.CompareTo(secondColor); + } + + if (firstArtillery.Artillery.Artillery is EntityAdvancedArtillery firstAdvanced && secondArtillery.Artillery.Artillery is EntityAdvancedArtillery secondAdvanced) + { + firstColor = firstAdvanced.DopColor.ToArgb(); + secondColor = secondAdvanced.DopColor.ToArgb(); + + if (firstColor != secondColor) + { + return firstColor.CompareTo(secondColor); + } + } + + var speedCompare = firstArtillery.Artillery.Artillery.Speed.CompareTo(secondArtillery.Artillery.Artillery.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + + return firstArtillery.Artillery.Artillery.Weight.CompareTo(secondArtillery.Artillery.Artillery.Weight); + } + } +} diff --git a/SelfPropelledArtilleryUnit/ArtilleryCompareByType.cs b/SelfPropelledArtilleryUnit/ArtilleryCompareByType.cs new file mode 100644 index 0000000..a04a385 --- /dev/null +++ b/SelfPropelledArtilleryUnit/ArtilleryCompareByType.cs @@ -0,0 +1,64 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Artilleries +{ + internal class ArtilleryCompareByType : IComparer + { + public int Compare(IDrawingObject? first, IDrawingObject? second) + { + if (first == null && second == null) + { + return 0; + } + + if (first == null && second != null) + { + return 1; + } + + if (first != null && second == null) + { + return -1; + } + + var firstArtillery = first as DrawingObjectArtillery; + var secondArtillery = second as DrawingObjectArtillery; + + if (firstArtillery == null && secondArtillery == null) + { + return 0; + } + + if (firstArtillery == null && secondArtillery != null) + { + return 1; + } + + if (firstArtillery != null && secondArtillery == null) + { + return -1; + } + + if (firstArtillery.Artillery.GetType() != secondArtillery.Artillery.GetType()) + { + if (firstArtillery.Artillery.GetType().Name == "DrawningArtillery") + { + return -1; + } + return 1; + } + + var speedCompare = firstArtillery.Artillery.Artillery.Speed.CompareTo(secondArtillery.Artillery.Artillery.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + + return firstArtillery.Artillery.Artillery.Weight.CompareTo(secondArtillery.Artillery.Artillery.Weight); + } + } +} diff --git a/SelfPropelledArtilleryUnit/DrawingObjectArtillery.cs b/SelfPropelledArtilleryUnit/DrawingObjectArtillery.cs index bb7441b..633435d 100644 --- a/SelfPropelledArtilleryUnit/DrawingObjectArtillery.cs +++ b/SelfPropelledArtilleryUnit/DrawingObjectArtillery.cs @@ -10,6 +10,8 @@ namespace Artilleries { private DrawingArtillery _artillery = null; + public DrawingArtillery Artillery => _artillery; + public DrawingObjectArtillery(DrawingArtillery artillery) { _artillery = artillery; diff --git a/SelfPropelledArtilleryUnit/FormMapWithSetArtilleries.Designer.cs b/SelfPropelledArtilleryUnit/FormMapWithSetArtilleries.Designer.cs index 9ffe300..23ee848 100644 --- a/SelfPropelledArtilleryUnit/FormMapWithSetArtilleries.Designer.cs +++ b/SelfPropelledArtilleryUnit/FormMapWithSetArtilleries.Designer.cs @@ -29,6 +29,8 @@ private void InitializeComponent() { this.toolsGroupBox = new System.Windows.Forms.GroupBox(); + this.buttonSortByColor = new System.Windows.Forms.Button(); + this.buttonSortByType = new System.Windows.Forms.Button(); this.mapsGroupBox = new System.Windows.Forms.GroupBox(); this.buttonDeleteMap = new System.Windows.Forms.Button(); this.listBoxMaps = new System.Windows.Forms.ListBox(); @@ -59,6 +61,8 @@ // // toolsGroupBox // + this.toolsGroupBox.Controls.Add(this.buttonSortByColor); + this.toolsGroupBox.Controls.Add(this.buttonSortByType); this.toolsGroupBox.Controls.Add(this.mapsGroupBox); this.toolsGroupBox.Controls.Add(this.buttonShowOnMap); this.toolsGroupBox.Controls.Add(this.buttonShowStorage); @@ -72,11 +76,31 @@ this.toolsGroupBox.Dock = System.Windows.Forms.DockStyle.Right; this.toolsGroupBox.Location = new System.Drawing.Point(600, 24); this.toolsGroupBox.Name = "toolsGroupBox"; - this.toolsGroupBox.Size = new System.Drawing.Size(200, 589); + this.toolsGroupBox.Size = new System.Drawing.Size(200, 657); this.toolsGroupBox.TabIndex = 0; this.toolsGroupBox.TabStop = false; this.toolsGroupBox.Text = "Инструменты"; // + // buttonSortByColor + // + this.buttonSortByColor.Location = new System.Drawing.Point(13, 324); + this.buttonSortByColor.Name = "buttonSortByColor"; + this.buttonSortByColor.Size = new System.Drawing.Size(175, 32); + this.buttonSortByColor.TabIndex = 27; + this.buttonSortByColor.Text = "Сортировать по цвету"; + this.buttonSortByColor.UseVisualStyleBackColor = true; + this.buttonSortByColor.Click += new System.EventHandler(this.buttonSortByColor_Click); + // + // buttonSortByType + // + this.buttonSortByType.Location = new System.Drawing.Point(13, 286); + this.buttonSortByType.Name = "buttonSortByType"; + this.buttonSortByType.Size = new System.Drawing.Size(175, 32); + this.buttonSortByType.TabIndex = 26; + this.buttonSortByType.Text = "Сортировать по типу"; + this.buttonSortByType.UseVisualStyleBackColor = true; + this.buttonSortByType.Click += new System.EventHandler(this.buttonSortByType_Click); + // // mapsGroupBox // this.mapsGroupBox.Controls.Add(this.buttonDeleteMap); @@ -142,7 +166,7 @@ // // buttonShowOnMap // - this.buttonShowOnMap.Location = new System.Drawing.Point(13, 470); + this.buttonShowOnMap.Location = new System.Drawing.Point(13, 526); this.buttonShowOnMap.Name = "buttonShowOnMap"; this.buttonShowOnMap.Size = new System.Drawing.Size(175, 32); this.buttonShowOnMap.TabIndex = 24; @@ -152,7 +176,7 @@ // // buttonShowStorage // - this.buttonShowStorage.Location = new System.Drawing.Point(13, 423); + this.buttonShowStorage.Location = new System.Drawing.Point(13, 488); this.buttonShowStorage.Name = "buttonShowStorage"; this.buttonShowStorage.Size = new System.Drawing.Size(175, 32); this.buttonShowStorage.TabIndex = 23; @@ -162,7 +186,7 @@ // // buttonRemoveArtillery // - this.buttonRemoveArtillery.Location = new System.Drawing.Point(13, 376); + this.buttonRemoveArtillery.Location = new System.Drawing.Point(13, 441); this.buttonRemoveArtillery.Name = "buttonRemoveArtillery"; this.buttonRemoveArtillery.Size = new System.Drawing.Size(175, 32); this.buttonRemoveArtillery.TabIndex = 22; @@ -172,7 +196,7 @@ // // maskedTextBoxPosition // - this.maskedTextBoxPosition.Location = new System.Drawing.Point(13, 347); + this.maskedTextBoxPosition.Location = new System.Drawing.Point(13, 412); this.maskedTextBoxPosition.Mask = "00"; this.maskedTextBoxPosition.Name = "maskedTextBoxPosition"; this.maskedTextBoxPosition.Size = new System.Drawing.Size(175, 23); @@ -180,7 +204,7 @@ // // buttonAddArtillery // - this.buttonAddArtillery.Location = new System.Drawing.Point(13, 309); + this.buttonAddArtillery.Location = new System.Drawing.Point(13, 374); this.buttonAddArtillery.Name = "buttonAddArtillery"; this.buttonAddArtillery.Size = new System.Drawing.Size(175, 32); this.buttonAddArtillery.TabIndex = 20; @@ -193,7 +217,7 @@ this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonRight.BackgroundImage = global::SelfPropelledArtilleryUnit.Properties.Resources.ArrowRight; this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; - this.buttonRight.Location = new System.Drawing.Point(124, 538); + this.buttonRight.Location = new System.Drawing.Point(124, 606); this.buttonRight.Name = "buttonRight"; this.buttonRight.Size = new System.Drawing.Size(30, 30); this.buttonRight.TabIndex = 18; @@ -205,7 +229,7 @@ this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonDown.BackgroundImage = global::SelfPropelledArtilleryUnit.Properties.Resources.ArrowDown; this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; - this.buttonDown.Location = new System.Drawing.Point(88, 538); + this.buttonDown.Location = new System.Drawing.Point(88, 606); this.buttonDown.Name = "buttonDown"; this.buttonDown.Size = new System.Drawing.Size(30, 30); this.buttonDown.TabIndex = 17; @@ -217,7 +241,7 @@ this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonUp.BackgroundImage = global::SelfPropelledArtilleryUnit.Properties.Resources.ArrowUp; this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; - this.buttonUp.Location = new System.Drawing.Point(88, 505); + this.buttonUp.Location = new System.Drawing.Point(88, 573); this.buttonUp.Name = "buttonUp"; this.buttonUp.Size = new System.Drawing.Size(30, 30); this.buttonUp.TabIndex = 16; @@ -229,7 +253,7 @@ this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonLeft.BackgroundImage = global::SelfPropelledArtilleryUnit.Properties.Resources.ArrowLeft; this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; - this.buttonLeft.Location = new System.Drawing.Point(54, 538); + this.buttonLeft.Location = new System.Drawing.Point(54, 606); this.buttonLeft.Name = "buttonLeft"; this.buttonLeft.Size = new System.Drawing.Size(30, 30); this.buttonLeft.TabIndex = 15; @@ -241,7 +265,7 @@ this.pictureBoxArtilleries.Dock = System.Windows.Forms.DockStyle.Fill; this.pictureBoxArtilleries.Location = new System.Drawing.Point(0, 24); this.pictureBoxArtilleries.Name = "pictureBoxArtilleries"; - this.pictureBoxArtilleries.Size = new System.Drawing.Size(600, 589); + this.pictureBoxArtilleries.Size = new System.Drawing.Size(600, 657); this.pictureBoxArtilleries.TabIndex = 1; this.pictureBoxArtilleries.TabStop = false; // @@ -291,7 +315,7 @@ // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(800, 613); + this.ClientSize = new System.Drawing.Size(800, 681); this.Controls.Add(this.pictureBoxArtilleries); this.Controls.Add(this.toolsGroupBox); this.Controls.Add(this.menuStrip); @@ -335,5 +359,7 @@ private ToolStripMenuItem loadToolStripMenuItem; private OpenFileDialog loadFileDialog; private SaveFileDialog saveFileDialog; + private Button buttonSortByColor; + private Button buttonSortByType; } } \ No newline at end of file diff --git a/SelfPropelledArtilleryUnit/FormMapWithSetArtilleries.cs b/SelfPropelledArtilleryUnit/FormMapWithSetArtilleries.cs index bd06995..dde5fda 100644 --- a/SelfPropelledArtilleryUnit/FormMapWithSetArtilleries.cs +++ b/SelfPropelledArtilleryUnit/FormMapWithSetArtilleries.cs @@ -250,5 +250,25 @@ namespace Artilleries } } } + + private void buttonSortByType_Click(object sender, EventArgs e) + { + if (listBoxMaps.SelectedIndex == -1) + { + return; + } + _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].Sort(new ArtilleryCompareByType()); + pictureBoxArtilleries.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 ArtilleryCompareByColor()); + pictureBoxArtilleries.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); + } } } diff --git a/SelfPropelledArtilleryUnit/MapWithSetArtilleriesGeneric.cs b/SelfPropelledArtilleryUnit/MapWithSetArtilleriesGeneric.cs index 0af1465..f9ac5a5 100644 --- a/SelfPropelledArtilleryUnit/MapWithSetArtilleriesGeneric.cs +++ b/SelfPropelledArtilleryUnit/MapWithSetArtilleriesGeneric.cs @@ -7,7 +7,7 @@ using System.Threading.Tasks; namespace Artilleries { internal class MapWithSetArtilleriesGeneric - where T : class, IDrawingObject + where T : class, IDrawingObject, IEquatable where U : AbstractMap { private readonly int _pictureWidth; @@ -142,5 +142,10 @@ namespace Artilleries _setArtilleries.Insert(DrawingObjectArtillery.Create(record) as T); } } + + public void Sort(IComparer comparer) + { + _setArtilleries.SortSet(comparer); + } } } diff --git a/SelfPropelledArtilleryUnit/SetArtilleriesGeneric.cs b/SelfPropelledArtilleryUnit/SetArtilleriesGeneric.cs index aae0c0e..c2152a8 100644 --- a/SelfPropelledArtilleryUnit/SetArtilleriesGeneric.cs +++ b/SelfPropelledArtilleryUnit/SetArtilleriesGeneric.cs @@ -98,5 +98,15 @@ namespace Artilleries } } } + + public void SortSet(IComparer comparer) + { + if (comparer == null) + { + return; + } + + _places.Sort(comparer); + } } }