diff --git a/Airbus/Airbus/AbstractMap.cs b/Airbus/Airbus/AbstractMap.cs index 916a451..a120000 100644 --- a/Airbus/Airbus/AbstractMap.cs +++ b/Airbus/Airbus/AbstractMap.cs @@ -7,7 +7,7 @@ using System.Threading.Tasks; namespace Airbus { - internal abstract class AbstractMap + internal abstract class AbstractMap : IEquatable { private IDrawningObject _drawningObject = null; protected int[,] _map = null; @@ -166,6 +166,30 @@ namespace Airbus return bmp; } + //сравнение + public bool Equals(AbstractMap? other) + { + if (other == null || _map != other._map || _height != other._height || _width != other._width + || _size_x != other._size_x || _size_y != other._size_y || GetType() != other.GetType() + || _map.GetLength(0) != other._map.GetLength(0) || _map.GetLength(1) != other._map.GetLength(1)) + { + 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; + } + protected abstract void GenerateMap(); protected abstract void DrawRoadPart(Graphics g, int i, int j); protected abstract void DrawBarrierPart(Graphics g, int i, int j); diff --git a/Airbus/Airbus/DrawningObjectPlane.cs b/Airbus/Airbus/DrawningObjectPlane.cs index 695be57..1d57aec 100644 --- a/Airbus/Airbus/DrawningObjectPlane.cs +++ b/Airbus/Airbus/DrawningObjectPlane.cs @@ -17,6 +17,8 @@ namespace Airbus _airbus = airbus; } + public DrawningAirbus GetPlane => _airbus; + void IDrawningObject.DrawningObject(Graphics g) { _airbus.DrawTransport(g); @@ -46,5 +48,62 @@ namespace Airbus public static IDrawningObject Create(string data) => new DrawningObjectPlane(data.CreateDrawningPlane()); + public bool Equals(IDrawningObject? other) + { + if(other == null) + { + return false; + } + + var otherPlane = other as DrawningObjectPlane; + + if(otherPlane == null) + { + return false; + } + + var plane = _airbus.Airbus; + var otherPlanePlane = otherPlane._airbus.Airbus; + + if(plane.Speed != otherPlanePlane.Speed) + { + return false; + } + + if(plane.Weight != otherPlanePlane.Weight) + { + return false; + } + + if (plane.CorpusColor != otherPlanePlane.CorpusColor) + { + return false; + } + + if(plane.GetType().Name != otherPlanePlane.GetType().Name) + { + return false; + } + + if (plane is EntitySuperAirbus entitySuperAirbus && otherPlanePlane is EntitySuperAirbus otherEntitySuperAirbus) + { + if(entitySuperAirbus.AddEngine != otherEntitySuperAirbus.AddEngine) + { + return false; + } + + if(entitySuperAirbus.AddСompartment != otherEntitySuperAirbus.AddСompartment) + { + return false; + } + + if(entitySuperAirbus.AddColor.Name != otherEntitySuperAirbus.AddColor.Name) + { + return false; + } + } + + return true; + } } } diff --git a/Airbus/Airbus/FormMapWithSetPlanes.Designer.cs b/Airbus/Airbus/FormMapWithSetPlanes.Designer.cs index fd70732..43df6fd 100644 --- a/Airbus/Airbus/FormMapWithSetPlanes.Designer.cs +++ b/Airbus/Airbus/FormMapWithSetPlanes.Designer.cs @@ -29,6 +29,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.groupBoxMaps = new System.Windows.Forms.GroupBox(); this.buttonDeleteMap = new System.Windows.Forms.Button(); this.listBoxMaps = new System.Windows.Forms.ListBox(); @@ -59,6 +61,8 @@ // // groupBoxTools // + this.groupBoxTools.Controls.Add(this.buttonSortByColor); + this.groupBoxTools.Controls.Add(this.buttonSortByType); this.groupBoxTools.Controls.Add(this.groupBoxMaps); this.groupBoxTools.Controls.Add(this.buttonRight); this.groupBoxTools.Controls.Add(this.buttonDown); @@ -72,11 +76,31 @@ this.groupBoxTools.Dock = System.Windows.Forms.DockStyle.Right; this.groupBoxTools.Location = new System.Drawing.Point(843, 28); this.groupBoxTools.Name = "groupBoxTools"; - this.groupBoxTools.Size = new System.Drawing.Size(250, 769); + this.groupBoxTools.Size = new System.Drawing.Size(250, 805); this.groupBoxTools.TabIndex = 0; this.groupBoxTools.TabStop = false; this.groupBoxTools.Text = "Инструменты"; // + // buttonSortByColor + // + this.buttonSortByColor.Location = new System.Drawing.Point(20, 416); + this.buttonSortByColor.Name = "buttonSortByColor"; + this.buttonSortByColor.Size = new System.Drawing.Size(218, 29); + this.buttonSortByColor.TabIndex = 12; + this.buttonSortByColor.Text = "Сортировка по цвету"; + this.buttonSortByColor.UseVisualStyleBackColor = true; + this.buttonSortByColor.Click += new System.EventHandler(this.ButtonSortByColor_Click); + // + // buttonSortByType + // + this.buttonSortByType.Location = new System.Drawing.Point(20, 381); + this.buttonSortByType.Name = "buttonSortByType"; + this.buttonSortByType.Size = new System.Drawing.Size(218, 29); + this.buttonSortByType.TabIndex = 11; + this.buttonSortByType.Text = "Сортировка по типу"; + this.buttonSortByType.UseVisualStyleBackColor = true; + this.buttonSortByType.Click += new System.EventHandler(this.ButtonSortByType_Click); + // // groupBoxMaps // this.groupBoxMaps.Controls.Add(this.buttonDeleteMap); @@ -145,7 +169,7 @@ this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonRight.BackgroundImage = global::Airbus.Properties.Resources.Right; this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; - this.buttonRight.Location = new System.Drawing.Point(158, 704); + this.buttonRight.Location = new System.Drawing.Point(158, 740); this.buttonRight.Name = "buttonRight"; this.buttonRight.Size = new System.Drawing.Size(45, 45); this.buttonRight.TabIndex = 9; @@ -157,7 +181,7 @@ this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonDown.BackgroundImage = global::Airbus.Properties.Resources.Down; this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; - this.buttonDown.Location = new System.Drawing.Point(107, 704); + this.buttonDown.Location = new System.Drawing.Point(107, 740); this.buttonDown.Name = "buttonDown"; this.buttonDown.Size = new System.Drawing.Size(45, 45); this.buttonDown.TabIndex = 8; @@ -169,7 +193,7 @@ this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonLeft.BackgroundImage = global::Airbus.Properties.Resources.Left; this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; - this.buttonLeft.Location = new System.Drawing.Point(56, 704); + this.buttonLeft.Location = new System.Drawing.Point(56, 740); this.buttonLeft.Name = "buttonLeft"; this.buttonLeft.Size = new System.Drawing.Size(45, 45); this.buttonLeft.TabIndex = 7; @@ -181,7 +205,7 @@ this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonUp.BackgroundImage = global::Airbus.Properties.Resources.Up; this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; - this.buttonUp.Location = new System.Drawing.Point(107, 653); + this.buttonUp.Location = new System.Drawing.Point(107, 689); this.buttonUp.Name = "buttonUp"; this.buttonUp.Size = new System.Drawing.Size(45, 45); this.buttonUp.TabIndex = 6; @@ -190,7 +214,7 @@ // // buttonShowOnMap // - this.buttonShowOnMap.Location = new System.Drawing.Point(20, 553); + this.buttonShowOnMap.Location = new System.Drawing.Point(20, 642); this.buttonShowOnMap.Name = "buttonShowOnMap"; this.buttonShowOnMap.Size = new System.Drawing.Size(218, 29); this.buttonShowOnMap.TabIndex = 5; @@ -200,7 +224,7 @@ // // buttonShowStorage // - this.buttonShowStorage.Location = new System.Drawing.Point(20, 500); + this.buttonShowStorage.Location = new System.Drawing.Point(20, 589); this.buttonShowStorage.Name = "buttonShowStorage"; this.buttonShowStorage.Size = new System.Drawing.Size(218, 29); this.buttonShowStorage.TabIndex = 4; @@ -210,7 +234,7 @@ // // buttonRemovePlane // - this.buttonRemovePlane.Location = new System.Drawing.Point(20, 446); + this.buttonRemovePlane.Location = new System.Drawing.Point(20, 535); this.buttonRemovePlane.Name = "buttonRemovePlane"; this.buttonRemovePlane.Size = new System.Drawing.Size(218, 29); this.buttonRemovePlane.TabIndex = 3; @@ -220,7 +244,7 @@ // // maskedTextBoxPosition // - this.maskedTextBoxPosition.Location = new System.Drawing.Point(20, 413); + this.maskedTextBoxPosition.Location = new System.Drawing.Point(20, 502); this.maskedTextBoxPosition.Mask = "00"; this.maskedTextBoxPosition.Name = "maskedTextBoxPosition"; this.maskedTextBoxPosition.Size = new System.Drawing.Size(218, 27); @@ -228,7 +252,7 @@ // // buttonAddPlane // - this.buttonAddPlane.Location = new System.Drawing.Point(20, 378); + this.buttonAddPlane.Location = new System.Drawing.Point(20, 467); this.buttonAddPlane.Name = "buttonAddPlane"; this.buttonAddPlane.Size = new System.Drawing.Size(218, 29); this.buttonAddPlane.TabIndex = 1; @@ -241,7 +265,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(843, 769); + this.pictureBox.Size = new System.Drawing.Size(843, 805); this.pictureBox.TabIndex = 1; this.pictureBox.TabStop = false; // @@ -291,7 +315,7 @@ // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(1093, 797); + this.ClientSize = new System.Drawing.Size(1093, 833); this.Controls.Add(this.pictureBox); this.Controls.Add(this.groupBoxTools); this.Controls.Add(this.menuStrip); @@ -335,5 +359,7 @@ private ToolStripMenuItem LoadToolStripMenuItem; private OpenFileDialog openFileDialog; private OpenFileDialog saveFileDialog; + private Button buttonSortByColor; + private Button buttonSortByType; } } \ No newline at end of file diff --git a/Airbus/Airbus/FormMapWithSetPlanes.cs b/Airbus/Airbus/FormMapWithSetPlanes.cs index 18f4ffd..6eb55ad 100644 --- a/Airbus/Airbus/FormMapWithSetPlanes.cs +++ b/Airbus/Airbus/FormMapWithSetPlanes.cs @@ -292,5 +292,29 @@ namespace Airbus } } } + + //сортировка по типу + private void ButtonSortByType_Click(object sender, EventArgs e) + { + if(listBoxMaps.SelectedIndex == -1) + { + return; + } + + _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].Sort(new PlaneCompareByType()); + 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 PlaneCompareByColor()); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); + } } } diff --git a/Airbus/Airbus/IDrawningObject.cs b/Airbus/Airbus/IDrawningObject.cs index ae2dff5..076a89c 100644 --- a/Airbus/Airbus/IDrawningObject.cs +++ b/Airbus/Airbus/IDrawningObject.cs @@ -7,7 +7,7 @@ using System.Threading.Tasks; namespace Airbus { - internal interface IDrawningObject + internal interface IDrawningObject : IEquatable { //шаг перемещения объекта public float Step { get; } diff --git a/Airbus/Airbus/MapWithSetPlanesGeneric.cs b/Airbus/Airbus/MapWithSetPlanesGeneric.cs index 4917a05..a67988d 100644 --- a/Airbus/Airbus/MapWithSetPlanesGeneric.cs +++ b/Airbus/Airbus/MapWithSetPlanesGeneric.cs @@ -10,7 +10,7 @@ namespace Airbus { //карта с набором объектов под неё internal class MapWithSetPlanesGeneric - where T : class, IDrawningObject + where T : class, IDrawningObject, IEquatable where U : AbstractMap { //ширина окна отрисовки @@ -108,6 +108,12 @@ namespace Airbus } } + //сортировка + public void Sort(IComparer comparer) + { + _setPlanes.SortSet(comparer); + } + //"взламываем" набор, чтобы все элементы оказались в начале private void Shaking() { diff --git a/Airbus/Airbus/PlaneCompareByColor.cs b/Airbus/Airbus/PlaneCompareByColor.cs new file mode 100644 index 0000000..0606000 --- /dev/null +++ b/Airbus/Airbus/PlaneCompareByColor.cs @@ -0,0 +1,75 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Airbus +{ + internal class PlaneCompareByColor : 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 xPlane = x as DrawningObjectPlane; + var yPlane = y as DrawningObjectPlane; + + if (xPlane == null && yPlane == null) + { + return 0; + } + + if (xPlane == null && yPlane != null) + { + return 1; + } + + if (xPlane != null && yPlane == null) + { + return -1; + } + + var xEntityPlane = xPlane.GetPlane.Airbus; + var yEntityPlane = yPlane.GetPlane.Airbus; + var colorCompare = xEntityPlane.CorpusColor.ToArgb().CompareTo(yEntityPlane.CorpusColor.ToArgb()); + + if (colorCompare != 0) + { + return colorCompare; + } + + if (xEntityPlane is EntitySuperAirbus xAirbus && yEntityPlane is EntitySuperAirbus yAirbus) + { + var dopColorCompare = xAirbus.AddColor.ToArgb().CompareTo(yAirbus.AddColor.ToArgb()); + + if (dopColorCompare != 0) + { + return dopColorCompare; + } + } + + var speedCompare = xPlane.GetPlane.Airbus.Speed.CompareTo(yPlane.GetPlane.Airbus.Speed); + + if (speedCompare != 0) + { + return speedCompare; + } + + return xPlane.GetPlane.Airbus.Weight.CompareTo(yPlane.GetPlane.Airbus.Weight); + } + } +} diff --git a/Airbus/Airbus/PlaneCompareByType.cs b/Airbus/Airbus/PlaneCompareByType.cs new file mode 100644 index 0000000..bf93c01 --- /dev/null +++ b/Airbus/Airbus/PlaneCompareByType.cs @@ -0,0 +1,67 @@ +using Microsoft.Extensions.Logging.Abstractions; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Airbus +{ + internal class PlaneCompareByType : 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 xPlane = x as DrawningObjectPlane; + var yPlane = y as DrawningObjectPlane; + + if(xPlane == null && yPlane == null) + { + return 0; + } + + if(xPlane == null && yPlane != null) + { + return 1; + } + + if(xPlane != null && yPlane == null) + { + return -1; + } + + if(xPlane.GetPlane.GetType().Name != yPlane.GetPlane.GetType().Name) + { + if(xPlane.GetPlane.GetType().Name == "DrawningAirbus") + { + return -1; + } + + return 1; + } + + var speedCompare = xPlane.GetPlane.Airbus.Speed.CompareTo(yPlane.GetPlane.Airbus.Speed); + + if(speedCompare != 0) + { + return speedCompare; + } + + return xPlane.GetPlane.Airbus.Weight.CompareTo(yPlane.GetPlane.Airbus.Weight); + } + } +} diff --git a/Airbus/Airbus/SaveData.txt b/Airbus/Airbus/SaveData.txt index c96b589..608f1fd 100644 --- a/Airbus/Airbus/SaveData.txt +++ b/Airbus/Airbus/SaveData.txt @@ -1,2 +1,2 @@ MapsCollection -123|StarWarsMap|1000:750:Red;1000:750:DeepPink:Control:False:False; +345|DesertStormMap|1000:750:White:Gray:True:True;1000:750:DeepPink:Yellow:False:True;1001:751:Red;1000:750:Red;1005:755:Lime;1010:760:Yellow;1000:750:Black:Yellow:True:False; diff --git a/Airbus/Airbus/SetPlanesGeneric.cs b/Airbus/Airbus/SetPlanesGeneric.cs index 70d5559..f5db532 100644 --- a/Airbus/Airbus/SetPlanesGeneric.cs +++ b/Airbus/Airbus/SetPlanesGeneric.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Numerics; using System.Reflection.PortableExecutable; using System.Text; using System.Threading.Tasks; @@ -8,7 +9,7 @@ using System.Threading.Tasks; namespace Airbus { internal class SetPlanesGeneric - where T: class + where T: class, IEquatable { //список объектов, которые храним private readonly List _places; @@ -42,16 +43,16 @@ namespace Airbus //добавление объекта в набор на конкретную позицию public int Insert(T plane, int position) { - if (position > _maxCount && position < 0) - { - return -1; - } - if (_places.Contains(plane)) { throw new ArgumentException($"Объект {plane} уже есть в наборе"); } + if (position > _maxCount && position < 0) + { + return -1; + } + _places.Insert(position, plane); return position; @@ -112,5 +113,16 @@ namespace Airbus } } } + + //сортировка набора объектов + public void SortSet(IComparer comparer) + { + if(comparer == null) + { + return; + } + + _places.Sort(comparer); + } } }