Сортировка

This commit is contained in:
Павел Сорокин 2022-12-02 11:37:42 +04:00
parent c67d41e1ab
commit 27cbe93ede
7 changed files with 187 additions and 12 deletions

View File

@ -18,6 +18,7 @@ namespace Liner
{
return _ship?.GetCurrentPosition() ?? default;
}
public DrawingShip GetShip => _ship;
public void MoveObject(Direction direction)
{
_ship?.MoveTransport(direction);

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.buttonSortType = new System.Windows.Forms.Button();
this.buttonSortColor = new System.Windows.Forms.Button();
this.groupBox.SuspendLayout();
this.groupBox1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit();
@ -59,6 +61,8 @@
//
// groupBox
//
this.groupBox.Controls.Add(this.buttonSortColor);
this.groupBox.Controls.Add(this.buttonSortType);
this.groupBox.Controls.Add(this.groupBox1);
this.groupBox.Controls.Add(this.buttonRight);
this.groupBox.Controls.Add(this.buttonDown);
@ -72,7 +76,7 @@
this.groupBox.Dock = System.Windows.Forms.DockStyle.Right;
this.groupBox.Location = new System.Drawing.Point(696, 28);
this.groupBox.Name = "groupBox";
this.groupBox.Size = new System.Drawing.Size(279, 640);
this.groupBox.Size = new System.Drawing.Size(279, 746);
this.groupBox.TabIndex = 0;
this.groupBox.TabStop = false;
this.groupBox.Text = "Инструменты";
@ -146,7 +150,7 @@
this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonRight.BackgroundImage = global::Liner.Properties.Resources._3;
this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonRight.Location = new System.Drawing.Point(233, 598);
this.buttonRight.Location = new System.Drawing.Point(233, 704);
this.buttonRight.Name = "buttonRight";
this.buttonRight.Size = new System.Drawing.Size(30, 30);
this.buttonRight.TabIndex = 17;
@ -158,7 +162,7 @@
this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonDown.BackgroundImage = global::Liner.Properties.Resources._2;
this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonDown.Location = new System.Drawing.Point(197, 598);
this.buttonDown.Location = new System.Drawing.Point(197, 704);
this.buttonDown.Name = "buttonDown";
this.buttonDown.Size = new System.Drawing.Size(30, 30);
this.buttonDown.TabIndex = 16;
@ -170,7 +174,7 @@
this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonUp.BackgroundImage = global::Liner.Properties.Resources._4;
this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonUp.Location = new System.Drawing.Point(197, 562);
this.buttonUp.Location = new System.Drawing.Point(197, 668);
this.buttonUp.Name = "buttonUp";
this.buttonUp.Size = new System.Drawing.Size(30, 30);
this.buttonUp.TabIndex = 15;
@ -182,7 +186,7 @@
this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonLeft.BackgroundImage = global::Liner.Properties.Resources._1;
this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonLeft.Location = new System.Drawing.Point(161, 598);
this.buttonLeft.Location = new System.Drawing.Point(161, 704);
this.buttonLeft.Name = "buttonLeft";
this.buttonLeft.Size = new System.Drawing.Size(30, 30);
this.buttonLeft.TabIndex = 14;
@ -191,7 +195,7 @@
//
// ButtonShowOnMap
//
this.ButtonShowOnMap.Location = new System.Drawing.Point(16, 527);
this.ButtonShowOnMap.Location = new System.Drawing.Point(16, 633);
this.ButtonShowOnMap.Name = "ButtonShowOnMap";
this.ButtonShowOnMap.Size = new System.Drawing.Size(222, 29);
this.ButtonShowOnMap.TabIndex = 13;
@ -201,7 +205,7 @@
//
// ButtonShowStorage
//
this.ButtonShowStorage.Location = new System.Drawing.Point(16, 472);
this.ButtonShowStorage.Location = new System.Drawing.Point(16, 578);
this.ButtonShowStorage.Name = "ButtonShowStorage";
this.ButtonShowStorage.Size = new System.Drawing.Size(222, 29);
this.ButtonShowStorage.TabIndex = 12;
@ -211,7 +215,7 @@
//
// ButtonRemoveShip
//
this.ButtonRemoveShip.Location = new System.Drawing.Point(16, 437);
this.ButtonRemoveShip.Location = new System.Drawing.Point(16, 543);
this.ButtonRemoveShip.Name = "ButtonRemoveShip";
this.ButtonRemoveShip.Size = new System.Drawing.Size(222, 29);
this.ButtonRemoveShip.TabIndex = 11;
@ -221,7 +225,7 @@
//
// maskedTextBoxPosition
//
this.maskedTextBoxPosition.Location = new System.Drawing.Point(16, 404);
this.maskedTextBoxPosition.Location = new System.Drawing.Point(16, 510);
this.maskedTextBoxPosition.Mask = "00";
this.maskedTextBoxPosition.Name = "maskedTextBoxPosition";
this.maskedTextBoxPosition.Size = new System.Drawing.Size(222, 27);
@ -229,7 +233,7 @@
//
// ButtonAddShip
//
this.ButtonAddShip.Location = new System.Drawing.Point(16, 356);
this.ButtonAddShip.Location = new System.Drawing.Point(16, 462);
this.ButtonAddShip.Name = "ButtonAddShip";
this.ButtonAddShip.Size = new System.Drawing.Size(222, 29);
this.ButtonAddShip.TabIndex = 2;
@ -242,7 +246,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(696, 640);
this.pictureBox.Size = new System.Drawing.Size(696, 746);
this.pictureBox.TabIndex = 1;
this.pictureBox.TabStop = false;
//
@ -283,11 +287,31 @@
//
this.saveFileDialog.Filter = "txt file | *.txt";
//
// buttonSortType
//
this.buttonSortType.Location = new System.Drawing.Point(16, 355);
this.buttonSortType.Name = "buttonSortType";
this.buttonSortType.Size = new System.Drawing.Size(222, 29);
this.buttonSortType.TabIndex = 19;
this.buttonSortType.Text = "Сортировать по типу";
this.buttonSortType.UseVisualStyleBackColor = true;
this.buttonSortType.Click += new System.EventHandler(this.ButtonSortType_Click);
//
// buttonSortColor
//
this.buttonSortColor.Location = new System.Drawing.Point(16, 390);
this.buttonSortColor.Name = "buttonSortColor";
this.buttonSortColor.Size = new System.Drawing.Size(222, 29);
this.buttonSortColor.TabIndex = 20;
this.buttonSortColor.Text = "Сортировать по цвету";
this.buttonSortColor.UseVisualStyleBackColor = true;
this.buttonSortColor.Click += new System.EventHandler(this.ButtonSortColor_Click);
//
// FormMapWithSetShips
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(975, 668);
this.ClientSize = new System.Drawing.Size(975, 774);
this.Controls.Add(this.pictureBox);
this.Controls.Add(this.groupBox);
this.Controls.Add(this.menuStrip);
@ -331,5 +355,7 @@
private ToolStripMenuItem LoadToolStripMenuItem;
private OpenFileDialog openFileDialog;
private SaveFileDialog saveFileDialog;
private Button buttonSortColor;
private Button buttonSortType;
}
}

View File

@ -242,5 +242,23 @@ namespace Liner
}
}
}
private void ButtonSortType_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 ButtonSortColor_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

@ -74,6 +74,10 @@ namespace Liner
_setShips.Insert(DrawingObjectShip.Create(rec) as T);
}
}
public void Sort(IComparer<T> comparer)
{
_setShips.SortSet(comparer);
}
private void Shaking()
{
int j = _setShips.Count - 1;

View File

@ -79,5 +79,13 @@ namespace Liner
}
}
}
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 Liner
{
internal class ShipCompareByColor : IComparer<IDrawingObject>
{
public int Compare(IDrawingObject? x, IDrawingObject? 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 DrawingObjectShip;
var yShip = y as DrawingObjectShip;
if (xShip == null && yShip == null)
{
return 0;
}
if (xShip == null && yShip != null)
{
return 1;
}
if (xShip != null && yShip == null)
{
return -1;
}
string xShipColor = xShip.GetShip.Ship.BodyColor.Name;
string yShipColor = yShip.GetShip.Ship.BodyColor.Name;
if (xShipColor != yShipColor)
{
return xShipColor.CompareTo(yShipColor);
}
if (xShip.GetShip.Ship is EntityLiner xLiner && yShip.GetShip.Ship is EntityLiner yLiner)
{
string xLocomotiveDopColor = xLiner.DopColor.Name;
string yLocomotiveDopColor = yLiner.DopColor.Name;
var dopColorCompare = xShipColor.CompareTo(yShipColor);
if (dopColorCompare != 0)
{
return dopColorCompare;
}
}
var speedCompare = xShip.GetShip.Ship.Speed.CompareTo(yShip.GetShip.Ship.Speed);
if (speedCompare != 0)
{
return speedCompare;
}
return xShip.GetShip.Ship.Weight.CompareTo(yShip.GetShip.Ship.Weight);
}
}
}

View File

@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Liner
{
internal class ShipCompareByType : IComparer<IDrawingObject>
{
public int Compare(IDrawingObject? x, IDrawingObject? 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 DrawingObjectShip;
var yShip = y as DrawingObjectShip;
if (xShip == null && yShip == null)
{
return 0;
}
if (xShip == null && yShip != null)
{
return 1;
}
if (xShip != null && yShip == null)
{
return -1;
}
if (xShip.GetShip.GetType().Name != yShip.GetShip.GetType().Name)
{
if (xShip.GetShip.GetType().Name == "DrawingShip")
{
return -1;
}
return 1;
}
var speedCompare = xShip.GetShip.Ship.Speed.CompareTo(yShip.GetShip.Ship.Speed);
if (speedCompare != 0)
{
return speedCompare;
}
return xShip.GetShip.Ship.Weight.CompareTo(yShip.GetShip.Ship.Weight);
}
}
}