Сортировка

This commit is contained in:
Макс Бондаренко 2022-12-12 02:04:19 +04:00
parent a2d2543486
commit 22a548b7c1
7 changed files with 186 additions and 7 deletions

View File

@ -17,6 +17,8 @@ namespace WarmlyShip
public float Step => _warmlyShip?.warmlyShip?.Step ?? 0;
public DrawingWarmlyShip GetWarmlyShip => _warmlyShip;
public void DrawningObject(Graphics g)
{
_warmlyShip?.DrawTransport(g);

View File

@ -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.maskedTextBoxPosition);
this.groupBox1.Controls.Add(this.buttonDown);
@ -142,7 +146,7 @@
//
// maskedTextBoxPosition
//
this.maskedTextBoxPosition.Location = new System.Drawing.Point(6, 345);
this.maskedTextBoxPosition.Location = new System.Drawing.Point(6, 375);
this.maskedTextBoxPosition.Mask = "00";
this.maskedTextBoxPosition.Name = "maskedTextBoxPosition";
this.maskedTextBoxPosition.Size = new System.Drawing.Size(188, 23);
@ -198,7 +202,7 @@
//
// buttonShowOnMap
//
this.buttonShowOnMap.Location = new System.Drawing.Point(6, 432);
this.buttonShowOnMap.Location = new System.Drawing.Point(6, 462);
this.buttonShowOnMap.Name = "buttonShowOnMap";
this.buttonShowOnMap.Size = new System.Drawing.Size(188, 23);
this.buttonShowOnMap.TabIndex = 5;
@ -208,7 +212,7 @@
//
// buttonShowStorage
//
this.buttonShowStorage.Location = new System.Drawing.Point(6, 403);
this.buttonShowStorage.Location = new System.Drawing.Point(6, 433);
this.buttonShowStorage.Name = "buttonShowStorage";
this.buttonShowStorage.Size = new System.Drawing.Size(188, 23);
this.buttonShowStorage.TabIndex = 4;
@ -218,7 +222,7 @@
//
// buttonRemoveShip
//
this.buttonRemoveShip.Location = new System.Drawing.Point(6, 374);
this.buttonRemoveShip.Location = new System.Drawing.Point(6, 404);
this.buttonRemoveShip.Name = "buttonRemoveShip";
this.buttonRemoveShip.Size = new System.Drawing.Size(188, 23);
this.buttonRemoveShip.TabIndex = 3;
@ -228,7 +232,7 @@
//
// buttonAddShip
//
this.buttonAddShip.Location = new System.Drawing.Point(6, 316);
this.buttonAddShip.Location = new System.Drawing.Point(6, 346);
this.buttonAddShip.Name = "buttonAddShip";
this.buttonAddShip.Size = new System.Drawing.Size(188, 23);
this.buttonAddShip.TabIndex = 1;
@ -266,14 +270,14 @@
// SaveToolStripMenuItem
//
this.SaveToolStripMenuItem.Name = "SaveToolStripMenuItem";
this.SaveToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
this.SaveToolStripMenuItem.Size = new System.Drawing.Size(141, 22);
this.SaveToolStripMenuItem.Text = "Сохранение";
this.SaveToolStripMenuItem.Click += new System.EventHandler(this.SaveToolStripMenuItem_Click);
//
// LoadToolStripMenuItem
//
this.LoadToolStripMenuItem.Name = "LoadToolStripMenuItem";
this.LoadToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
this.LoadToolStripMenuItem.Size = new System.Drawing.Size(141, 22);
this.LoadToolStripMenuItem.Text = "Загрузка";
this.LoadToolStripMenuItem.Click += new System.EventHandler(this.LoadToolStripMenuItem_Click);
//
@ -285,6 +289,26 @@
//
this.saveFileDialog.Filter = "txt file | *.txt";
//
// buttonSortByType
//
this.buttonSortByType.Location = new System.Drawing.Point(6, 272);
this.buttonSortByType.Name = "buttonSortByType";
this.buttonSortByType.Size = new System.Drawing.Size(188, 23);
this.buttonSortByType.TabIndex = 13;
this.buttonSortByType.Text = "Сортировка по типу";
this.buttonSortByType.UseVisualStyleBackColor = true;
this.buttonSortByType.Click += new System.EventHandler(this.ButtonSortByType_Click);
//
// buttonSortByColor
//
this.buttonSortByColor.Location = new System.Drawing.Point(6, 301);
this.buttonSortByColor.Name = "buttonSortByColor";
this.buttonSortByColor.Size = new System.Drawing.Size(188, 23);
this.buttonSortByColor.TabIndex = 14;
this.buttonSortByColor.Text = "Сортировка по цвету";
this.buttonSortByColor.UseVisualStyleBackColor = true;
this.buttonSortByColor.Click += new System.EventHandler(this.ButtonSortByColor_Click);
//
// FormMapWithSetShip
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
@ -333,5 +357,7 @@
private ToolStripMenuItem LoadToolStripMenuItem;
private OpenFileDialog openFileDialog;
private SaveFileDialog saveFileDialog;
private Button buttonSortByColor;
private Button buttonSortByType;
}
}

View File

@ -250,5 +250,25 @@ namespace WarmlyShip
}
}
}
private void ButtonSortByType_Click(object sender, EventArgs e)
{
if (listBoxMaps.SelectedIndex == -1)
{
return;
}
_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].Sort(new ShipCompareByType());
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 ShipCompareByColor());
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
}
}

View File

@ -83,6 +83,11 @@ namespace WarmlyShip
}
}
public void Sort(IComparer<T> comparer)
{
_setShips.SortSet(comparer);
}
private void Shaking()
{
int j = _setShips.Count - 1;

View File

@ -73,5 +73,13 @@ namespace WarmlyShip
}
}
public void SortSet(IComparer<T> comparer)
{
if (comparer == null)
{
return;
}
_places.Sort(comparer);
}
}
}

View File

@ -0,0 +1,63 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WarmlyShip
{
internal class ShipCompareByColor : IComparer<IDrawningObject>
{
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 xShip = x as DrawningObjectShip;
var yShip = y as DrawningObjectShip;
if (xShip == null && yShip == null)
{
return 0;
}
if (xShip == null && yShip != null)
{
return 1;
}
if (xShip != null && yShip == null)
{
return -1;
}
string xAirplaneColor = xShip.GetWarmlyShip.warmlyShip.BodyColor.Name;
string yAirplaneColor = yShip.GetWarmlyShip.warmlyShip.BodyColor.Name;
if (xAirplaneColor != yAirplaneColor)
{
return xAirplaneColor.CompareTo(yAirplaneColor);
}
if (xShip.GetWarmlyShip.warmlyShip is EntityMotorShip xAirbus && yShip.GetWarmlyShip.warmlyShip is EntityMotorShip yAirbus)
{
string xAirplaneDopColor = xAirbus.DopColor.Name;
string yAirplaneDopColor = yAirbus.DopColor.Name;
var dopColorCompare = xAirplaneDopColor.CompareTo(yAirplaneDopColor);
if (dopColorCompare != 0)
{
return dopColorCompare;
}
}
var speedCompare = xShip.GetWarmlyShip.warmlyShip.Speed.CompareTo(yShip.GetWarmlyShip.warmlyShip.Speed);
if (speedCompare != 0)
{
return speedCompare;
}
return xShip.GetWarmlyShip.warmlyShip.Weight.CompareTo(yShip.GetWarmlyShip.warmlyShip.Weight);
}
}
}

View File

@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WarmlyShip
{
internal class ShipCompareByType : IComparer<IDrawningObject>
{
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 xShip = x as DrawningObjectShip;
var yShip = y as DrawningObjectShip;
if (xShip == null && yShip == null)
{
return 0;
}
if (xShip == null && yShip != null)
{
return 1;
}
if (xShip != null && yShip == null)
{
return -1;
}
if (xShip.GetWarmlyShip.GetType().Name != yShip.GetWarmlyShip.GetType().Name)
{
if (xShip.GetWarmlyShip.GetType().Name == "DrawingWarmlyShip")
{
return -1;
}
return 1;
}
var speedCompare = xShip.GetWarmlyShip.warmlyShip.Speed.CompareTo(yShip.GetWarmlyShip.warmlyShip.Speed);
if (speedCompare != 0)
{
return speedCompare;
}
return xShip.GetWarmlyShip.warmlyShip.Weight.CompareTo(yShip.GetWarmlyShip.warmlyShip.Weight);
}
}
}