Сортировка

This commit is contained in:
Данила Мочалов 2022-11-21 19:27:25 +04:00
parent 38def0fbc5
commit 5a1cdcd4b2
9 changed files with 223 additions and 12 deletions

View File

@ -17,6 +17,8 @@ namespace Locomotive
public float Step => _locomotive?.Locomotive?.Step ?? 0; public float Step => _locomotive?.Locomotive?.Step ?? 0;
public DrawningLocomotive GetLocomotive => _locomotive;
public void DrawningObject(Graphics g) public void DrawningObject(Graphics g)
{ {
_locomotive?.DrawTransport(g); _locomotive?.DrawTransport(g);
@ -54,7 +56,7 @@ namespace Locomotive
var locomotive = _locomotive.Locomotive; var locomotive = _locomotive.Locomotive;
var otherLocomotiveLocomotive = otherLocomotive._locomotive.Locomotive; var otherLocomotiveLocomotive = otherLocomotive._locomotive.Locomotive;
if (locomotive.GetType() != otherLocomotiveLocomotive.GetType()) return false; if (locomotive.GetType().Name != otherLocomotiveLocomotive.GetType().Name) return false;
if (locomotive.Speed != otherLocomotiveLocomotive.Speed) if (locomotive.Speed != otherLocomotiveLocomotive.Speed)
{ {

View File

@ -29,6 +29,7 @@
private void InitializeComponent() private void InitializeComponent()
{ {
this.groupBoxTools = new System.Windows.Forms.GroupBox(); this.groupBoxTools = new System.Windows.Forms.GroupBox();
this.ButtonSortByType = new System.Windows.Forms.Button();
this.groupBox1 = new System.Windows.Forms.GroupBox(); this.groupBox1 = new System.Windows.Forms.GroupBox();
this.buttonDeleteMap = new System.Windows.Forms.Button(); this.buttonDeleteMap = new System.Windows.Forms.Button();
this.listBoxMaps = new System.Windows.Forms.ListBox(); this.listBoxMaps = new System.Windows.Forms.ListBox();
@ -51,6 +52,7 @@
this.loadToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.loadToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.loadFileDialog = new System.Windows.Forms.OpenFileDialog(); this.loadFileDialog = new System.Windows.Forms.OpenFileDialog();
this.saveFileDialog = new System.Windows.Forms.SaveFileDialog(); this.saveFileDialog = new System.Windows.Forms.SaveFileDialog();
this.ButtonSortByColor = new System.Windows.Forms.Button();
this.groupBoxTools.SuspendLayout(); this.groupBoxTools.SuspendLayout();
this.groupBox1.SuspendLayout(); this.groupBox1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit();
@ -59,6 +61,8 @@
// //
// groupBoxTools // groupBoxTools
// //
this.groupBoxTools.Controls.Add(this.ButtonSortByColor);
this.groupBoxTools.Controls.Add(this.ButtonSortByType);
this.groupBoxTools.Controls.Add(this.groupBox1); this.groupBoxTools.Controls.Add(this.groupBox1);
this.groupBoxTools.Controls.Add(this.buttonLeft); this.groupBoxTools.Controls.Add(this.buttonLeft);
this.groupBoxTools.Controls.Add(this.buttonRight); this.groupBoxTools.Controls.Add(this.buttonRight);
@ -72,11 +76,21 @@
this.groupBoxTools.Dock = System.Windows.Forms.DockStyle.Right; this.groupBoxTools.Dock = System.Windows.Forms.DockStyle.Right;
this.groupBoxTools.Location = new System.Drawing.Point(580, 28); this.groupBoxTools.Location = new System.Drawing.Point(580, 28);
this.groupBoxTools.Name = "groupBoxTools"; this.groupBoxTools.Name = "groupBoxTools";
this.groupBoxTools.Size = new System.Drawing.Size(220, 540); this.groupBoxTools.Size = new System.Drawing.Size(220, 637);
this.groupBoxTools.TabIndex = 0; this.groupBoxTools.TabIndex = 0;
this.groupBoxTools.TabStop = false; this.groupBoxTools.TabStop = false;
this.groupBoxTools.Text = "Tools"; this.groupBoxTools.Text = "Tools";
// //
// ButtonSortByType
//
this.ButtonSortByType.Location = new System.Drawing.Point(7, 456);
this.ButtonSortByType.Name = "ButtonSortByType";
this.ButtonSortByType.Size = new System.Drawing.Size(207, 29);
this.ButtonSortByType.TabIndex = 9;
this.ButtonSortByType.Text = "Sort By Type";
this.ButtonSortByType.UseVisualStyleBackColor = true;
this.ButtonSortByType.Click += new System.EventHandler(this.ButtonSortByType_Click);
//
// groupBox1 // groupBox1
// //
this.groupBox1.Controls.Add(this.buttonDeleteMap); this.groupBox1.Controls.Add(this.buttonDeleteMap);
@ -144,7 +158,7 @@
// //
this.buttonLeft.BackgroundImage = global::Locomotive.Properties.Resources.left_arrow; this.buttonLeft.BackgroundImage = global::Locomotive.Properties.Resources.left_arrow;
this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonLeft.Location = new System.Drawing.Point(42, 502); this.buttonLeft.Location = new System.Drawing.Point(48, 591);
this.buttonLeft.Name = "buttonLeft"; this.buttonLeft.Name = "buttonLeft";
this.buttonLeft.Size = new System.Drawing.Size(40, 40); this.buttonLeft.Size = new System.Drawing.Size(40, 40);
this.buttonLeft.TabIndex = 7; this.buttonLeft.TabIndex = 7;
@ -155,7 +169,7 @@
// //
this.buttonRight.BackgroundImage = global::Locomotive.Properties.Resources.right_arrow; this.buttonRight.BackgroundImage = global::Locomotive.Properties.Resources.right_arrow;
this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonRight.Location = new System.Drawing.Point(134, 502); this.buttonRight.Location = new System.Drawing.Point(140, 591);
this.buttonRight.Name = "buttonRight"; this.buttonRight.Name = "buttonRight";
this.buttonRight.Size = new System.Drawing.Size(40, 40); this.buttonRight.Size = new System.Drawing.Size(40, 40);
this.buttonRight.TabIndex = 7; this.buttonRight.TabIndex = 7;
@ -166,7 +180,7 @@
// //
this.buttonDown.BackgroundImage = global::Locomotive.Properties.Resources.down_arrow; this.buttonDown.BackgroundImage = global::Locomotive.Properties.Resources.down_arrow;
this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonDown.Location = new System.Drawing.Point(88, 502); this.buttonDown.Location = new System.Drawing.Point(94, 591);
this.buttonDown.Name = "buttonDown"; this.buttonDown.Name = "buttonDown";
this.buttonDown.Size = new System.Drawing.Size(40, 40); this.buttonDown.Size = new System.Drawing.Size(40, 40);
this.buttonDown.TabIndex = 7; this.buttonDown.TabIndex = 7;
@ -177,7 +191,7 @@
// //
this.buttonUp.BackgroundImage = global::Locomotive.Properties.Resources.up_arrow; this.buttonUp.BackgroundImage = global::Locomotive.Properties.Resources.up_arrow;
this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonUp.Location = new System.Drawing.Point(88, 456); this.buttonUp.Location = new System.Drawing.Point(94, 545);
this.buttonUp.Name = "buttonUp"; this.buttonUp.Name = "buttonUp";
this.buttonUp.Size = new System.Drawing.Size(40, 40); this.buttonUp.Size = new System.Drawing.Size(40, 40);
this.buttonUp.TabIndex = 6; this.buttonUp.TabIndex = 6;
@ -237,7 +251,7 @@
this.pictureBox.Dock = System.Windows.Forms.DockStyle.Fill; this.pictureBox.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBox.Location = new System.Drawing.Point(0, 28); this.pictureBox.Location = new System.Drawing.Point(0, 28);
this.pictureBox.Name = "pictureBox"; this.pictureBox.Name = "pictureBox";
this.pictureBox.Size = new System.Drawing.Size(580, 540); this.pictureBox.Size = new System.Drawing.Size(580, 637);
this.pictureBox.TabIndex = 1; this.pictureBox.TabIndex = 1;
this.pictureBox.TabStop = false; this.pictureBox.TabStop = false;
// //
@ -263,14 +277,14 @@
// saveToolStripMenuItem // saveToolStripMenuItem
// //
this.saveToolStripMenuItem.Name = "saveToolStripMenuItem"; this.saveToolStripMenuItem.Name = "saveToolStripMenuItem";
this.saveToolStripMenuItem.Size = new System.Drawing.Size(224, 26); this.saveToolStripMenuItem.Size = new System.Drawing.Size(125, 26);
this.saveToolStripMenuItem.Text = "Save"; this.saveToolStripMenuItem.Text = "Save";
this.saveToolStripMenuItem.Click += new System.EventHandler(this.saveToolStripMenuItem_Click); this.saveToolStripMenuItem.Click += new System.EventHandler(this.saveToolStripMenuItem_Click);
// //
// loadToolStripMenuItem // loadToolStripMenuItem
// //
this.loadToolStripMenuItem.Name = "loadToolStripMenuItem"; this.loadToolStripMenuItem.Name = "loadToolStripMenuItem";
this.loadToolStripMenuItem.Size = new System.Drawing.Size(224, 26); this.loadToolStripMenuItem.Size = new System.Drawing.Size(125, 26);
this.loadToolStripMenuItem.Text = "Load"; this.loadToolStripMenuItem.Text = "Load";
this.loadToolStripMenuItem.Click += new System.EventHandler(this.loadToolStripMenuItem_Click); this.loadToolStripMenuItem.Click += new System.EventHandler(this.loadToolStripMenuItem_Click);
// //
@ -282,11 +296,21 @@
// //
this.saveFileDialog.Filter = "txt file | *.txt"; this.saveFileDialog.Filter = "txt file | *.txt";
// //
// ButtonSortByColor
//
this.ButtonSortByColor.Location = new System.Drawing.Point(7, 491);
this.ButtonSortByColor.Name = "ButtonSortByColor";
this.ButtonSortByColor.Size = new System.Drawing.Size(207, 29);
this.ButtonSortByColor.TabIndex = 10;
this.ButtonSortByColor.Text = "Sort By Color";
this.ButtonSortByColor.UseVisualStyleBackColor = true;
this.ButtonSortByColor.Click += new System.EventHandler(this.ButtonSortByColor_Click);
//
// FormMapWithSetLocomotives // FormMapWithSetLocomotives
// //
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 568); this.ClientSize = new System.Drawing.Size(800, 665);
this.Controls.Add(this.pictureBox); this.Controls.Add(this.pictureBox);
this.Controls.Add(this.groupBoxTools); this.Controls.Add(this.groupBoxTools);
this.Controls.Add(this.menuStrip); this.Controls.Add(this.menuStrip);
@ -330,5 +354,7 @@
private ToolStripMenuItem loadToolStripMenuItem; private ToolStripMenuItem loadToolStripMenuItem;
private OpenFileDialog loadFileDialog; private OpenFileDialog loadFileDialog;
private SaveFileDialog saveFileDialog; private SaveFileDialog saveFileDialog;
private Button ButtonSortByType;
private Button ButtonSortByColor;
} }
} }

View File

@ -254,5 +254,25 @@ namespace Locomotive
} }
} }
} }
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();
}
} }
} }

View File

@ -67,6 +67,6 @@
<value>311, 17</value> <value>311, 17</value>
</metadata> </metadata>
<metadata name="$this.TrayHeight" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> <metadata name="$this.TrayHeight" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>33</value> <value>25</value>
</metadata> </metadata>
</root> </root>

View File

@ -0,0 +1,78 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Locomotive
{
internal class LocomotiveCompareByColor : 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 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.Locomotive.BodyColor.R.CompareTo(yLocomotive.GetLocomotive.Locomotive.BodyColor.R) != 0)
{
return xLocomotive.GetLocomotive.Locomotive.BodyColor.R.CompareTo(yLocomotive.GetLocomotive.Locomotive.BodyColor.R);
}
if (xLocomotive.GetLocomotive.Locomotive.BodyColor.G.CompareTo(yLocomotive.GetLocomotive.Locomotive.BodyColor.G) != 0)
{
return xLocomotive.GetLocomotive.Locomotive.BodyColor.G.CompareTo(yLocomotive.GetLocomotive.Locomotive.BodyColor.G);
}
if (xLocomotive.GetLocomotive.Locomotive.BodyColor.B.CompareTo(yLocomotive.GetLocomotive.Locomotive.BodyColor.B) != 0)
{
return xLocomotive.GetLocomotive.Locomotive.BodyColor.B.CompareTo(yLocomotive.GetLocomotive.Locomotive.BodyColor.B);
}
if (xLocomotive.GetLocomotive.Locomotive is EntityWarmlyLocomotive xWarmlyEntity && yLocomotive.GetLocomotive.Locomotive is EntityWarmlyLocomotive yWarmlyEntity)
{
if (xWarmlyEntity.ExtraColor.R.CompareTo(yWarmlyEntity.ExtraColor.R) != 0)
{
return xWarmlyEntity.ExtraColor.R.CompareTo(yWarmlyEntity.ExtraColor.R);
}
if (xWarmlyEntity.ExtraColor.G.CompareTo(yWarmlyEntity.ExtraColor.G) != 0)
{
return xWarmlyEntity.ExtraColor.G.CompareTo(yWarmlyEntity.ExtraColor.G);
}
if (xWarmlyEntity.ExtraColor.B.CompareTo(yWarmlyEntity.ExtraColor.B) != 0)
{
return xWarmlyEntity.ExtraColor.B.CompareTo(yWarmlyEntity.ExtraColor.B);
}
}
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);
}
}
}

View File

@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Locomotive
{
internal class LocomotiveCompareByType : 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 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);
}
}
}

View File

@ -178,6 +178,9 @@ namespace Locomotive
} }
} }
public void Sort(IComparer<T> comparer)
{
_setLocomotives.SortSet(comparer);
}
} }
} }

View File

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

17
Locomotive/log.clef Normal file
View File

@ -0,0 +1,17 @@
{"@t":"2022-11-21T15:23:13.2775821Z","@mt":"Map lalaland added"}
{"@t":"2022-11-21T15:23:13.3466012Z","@mt":"Map switched to lalaland"}
{"@t":"2022-11-21T15:23:31.6565447Z","@mt":"Object Locomotive.DrawningLocomotive added"}
{"@t":"2022-11-21T15:23:57.7770111Z","@mt":"Object Locomotive.DrawningLocomotive added"}
{"@t":"2022-11-21T15:25:01.0533266Z","@mt":"Map lalaland added"}
{"@t":"2022-11-21T15:25:01.0794891Z","@mt":"Map switched to lalaland"}
{"@t":"2022-11-21T15:25:01.4998647Z","@mt":"Map switched to lalaland"}
{"@t":"2022-11-21T15:25:06.1595618Z","@mt":"Object Locomotive.DrawningLocomotive added"}
{"@t":"2022-11-21T15:25:18.9994738Z","@mt":"Object Locomotive.DrawningLocomotive added"}
{"@t":"2022-11-21T15:25:31.8129464Z","@mt":"Map lalaland added"}
{"@t":"2022-11-21T15:25:31.8344280Z","@mt":"Map switched to lalaland"}
{"@t":"2022-11-21T15:25:37.1362070Z","@mt":"Object Locomotive.DrawningLocomotive added"}
{"@t":"2022-11-21T15:25:46.1930394Z","@mt":"Object Locomotive.DrawningLocomotive added"}
{"@t":"2022-11-21T15:26:02.0790751Z","@mt":"Object Locomotive.DrawningLocomotive added"}
{"@t":"2022-11-21T15:26:24.6631252Z","@mt":"Object Locomotive.DrawningWarmlyLocomotive added"}
{"@t":"2022-11-21T15:26:31.0236570Z","@mt":"Object Locomotive.DrawningLocomotive added"}
{"@t":"2022-11-21T15:26:36.0331011Z","@mt":"Object Locomotive.DrawningWarmlyLocomotive added"}