diff --git a/ProjectPlane/ProjectPlane/AbstractMap.cs b/ProjectPlane/ProjectPlane/AbstractMap.cs index 4b9b93e..ab0c1a1 100644 --- a/ProjectPlane/ProjectPlane/AbstractMap.cs +++ b/ProjectPlane/ProjectPlane/AbstractMap.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace ProjectPlane { - internal abstract class AbstractMap + internal abstract class AbstractMap : IEquatable { private IDrawingObject _drawingObject = null; protected int[,] _map = null; @@ -156,7 +156,27 @@ namespace ProjectPlane _drawingObject.DrawingObject(gr); return bmp; } + public bool Equals(AbstractMap? other) + { + if (other == null) + { + return false; + } + + if (_width != other._width) return false; + if (_height != other._height) return false; + if (_size_x != other._size_x) return false; + if (_size_y != other._size_y) 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/ProjectPlane/ProjectPlane/DrawingObject.cs b/ProjectPlane/ProjectPlane/DrawingObject.cs index d228a40..c6a6b3f 100644 --- a/ProjectPlane/ProjectPlane/DrawingObject.cs +++ b/ProjectPlane/ProjectPlane/DrawingObject.cs @@ -17,6 +17,7 @@ namespace ProjectPlane public float Step => _plane?.Plane?.Step ?? 0; + public DrawingPlane GetPlane => _plane; public (float Left, float Right, float Top, float Bottom) GetCurrentPosition() { return _plane?.GetCurrentPosition() ?? default; @@ -40,6 +41,52 @@ namespace ProjectPlane public string GetInfo() => _plane?.GetDataForSave(); public static IDrawingObject Create(string data) => new DrawingObject(data.CreateDrawingPlane()); + public bool Equals(IDrawingObject? other) + { + if (other == null) + { + return false; + } + var otherPlane = other as DrawingObject; + if (otherPlane == null) + { + return false; + } + var plane = _plane.Plane; + var otherPlanePlane = otherPlane._plane.Plane; + if (plane.Speed != otherPlanePlane.Speed) + { + return false; + } + if (plane.Weight != otherPlanePlane.Weight) + { + return false; + } + if (plane.BodyColor != otherPlanePlane.BodyColor) + { + return false; + } + if (plane.GetType() != otherPlanePlane.GetType()) + { + return false; + } + if (plane is EntityWarPlane advanced && otherPlanePlane is EntityWarPlane otheradvanced) + { + if (advanced.DopColor != otheradvanced.DopColor) + { + return false; + } + if (advanced.extraCell != otheradvanced.extraCell) + { + return false; + } + if (advanced.SuperTurbine != otheradvanced.SuperTurbine) + { + return false; + } + } + return true; + } } } diff --git a/ProjectPlane/ProjectPlane/FormMapWithSetPlanes.Designer.cs b/ProjectPlane/ProjectPlane/FormMapWithSetPlanes.Designer.cs index 7a40ffc..ee13032 100644 --- a/ProjectPlane/ProjectPlane/FormMapWithSetPlanes.Designer.cs +++ b/ProjectPlane/ProjectPlane/FormMapWithSetPlanes.Designer.cs @@ -52,6 +52,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.groupBoxTools.SuspendLayout(); this.groupBoxMaps.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit(); @@ -60,6 +62,8 @@ // // groupBoxTools // + this.groupBoxTools.Controls.Add(this.ButtonSortByColor); + this.groupBoxTools.Controls.Add(this.ButtonSortByType); this.groupBoxTools.Controls.Add(this.buttonRight); this.groupBoxTools.Controls.Add(this.buttonDown); this.groupBoxTools.Controls.Add(this.buttonLeft); @@ -78,6 +82,26 @@ this.groupBoxTools.TabStop = false; this.groupBoxTools.Text = "Tools"; // + // ButtonSortByType + // + this.ButtonSortByType.Location = new System.Drawing.Point(17, 506); + this.ButtonSortByType.Name = "ButtonSortByType"; + this.ButtonSortByType.Size = new System.Drawing.Size(176, 30); + this.ButtonSortByType.TabIndex = 9; + this.ButtonSortByType.Text = "Sort By Type"; + this.ButtonSortByType.UseVisualStyleBackColor = true; + this.ButtonSortByType.Click += new System.EventHandler(this.ButtonSortByType_Click); + // + // ButtonSortByColor + // + this.ButtonSortByColor.Location = new System.Drawing.Point(17, 541); + this.ButtonSortByColor.Name = "ButtonSortByColor"; + this.ButtonSortByColor.Size = new System.Drawing.Size(176, 30); + this.ButtonSortByColor.TabIndex = 10; + this.ButtonSortByColor.Text = "Sort By Color"; + this.ButtonSortByColor.UseVisualStyleBackColor = true; + this.ButtonSortByColor.Click += new System.EventHandler(this.ButtonSortByColor_Click); + // // buttonRight // this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); @@ -289,7 +313,7 @@ // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(884, 590); + this.ClientSize = new System.Drawing.Size(884, 740); this.Controls.Add(this.pictureBox); this.Controls.Add(this.groupBoxTools); this.Controls.Add(this.menuStrip); @@ -333,5 +357,7 @@ private ToolStripMenuItem loadToolStripMenuItem; private OpenFileDialog openFileDialog; private SaveFileDialog saveFileDialog; + private Button ButtonSortByType; + private Button ButtonSortByColor; } } \ No newline at end of file diff --git a/ProjectPlane/ProjectPlane/FormMapWithSetPlanes.cs b/ProjectPlane/ProjectPlane/FormMapWithSetPlanes.cs index b9f2a19..772a101 100644 --- a/ProjectPlane/ProjectPlane/FormMapWithSetPlanes.cs +++ b/ProjectPlane/ProjectPlane/FormMapWithSetPlanes.cs @@ -306,5 +306,24 @@ namespace ProjectPlane } } } + 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/ProjectPlane/ProjectPlane/IDrawingObject.cs b/ProjectPlane/ProjectPlane/IDrawingObject.cs index 097c15a..ad79269 100644 --- a/ProjectPlane/ProjectPlane/IDrawingObject.cs +++ b/ProjectPlane/ProjectPlane/IDrawingObject.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace ProjectPlane { - internal interface IDrawingObject + internal interface IDrawingObject : IEquatable { /// /// Шаг перемещения объекта diff --git a/ProjectPlane/ProjectPlane/MapWithSetPlanesGeneric.cs b/ProjectPlane/ProjectPlane/MapWithSetPlanesGeneric.cs index d03b30f..cb94c4f 100644 --- a/ProjectPlane/ProjectPlane/MapWithSetPlanesGeneric.cs +++ b/ProjectPlane/ProjectPlane/MapWithSetPlanesGeneric.cs @@ -7,7 +7,7 @@ using System.Threading.Tasks; namespace ProjectPlane { internal class MapWithSetPlanesGeneric - where T : class, IDrawingObject + where T : class, IDrawingObject, IEquatable where U : AbstractMap { /// @@ -132,6 +132,11 @@ namespace ProjectPlane _setPlanes.Insert(DrawingObject.Create(rec) as T); } } + public void Sort(IComparer comparer) + { + _setPlanes.SortSet(comparer); + } + /// /// "Взбалтываем" набор, чтобы все элементы оказались в начале /// diff --git a/ProjectPlane/ProjectPlane/PlaneCompareByColor.cs b/ProjectPlane/ProjectPlane/PlaneCompareByColor.cs new file mode 100644 index 0000000..a0d592b --- /dev/null +++ b/ProjectPlane/ProjectPlane/PlaneCompareByColor.cs @@ -0,0 +1,78 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectPlane +{ + internal class PlaneCompareByColor : IComparer + { + 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 xPlane = x as DrawingObject; + var yPlane = y as DrawingObject; + if (xPlane == null && yPlane == null) + { + return 0; + } + if (xPlane == null && yPlane != null) + { + return 1; + } + if (xPlane != null && yPlane == null) + { + return -1; + } + if (xPlane.GetPlane.Plane.BodyColor.R.CompareTo(yPlane.GetPlane.Plane.BodyColor.R) != 0) + { + return xPlane.GetPlane.Plane.BodyColor.R.CompareTo(yPlane.GetPlane.Plane.BodyColor.R); + } + if (xPlane.GetPlane.Plane.BodyColor.G.CompareTo(yPlane.GetPlane.Plane.BodyColor.G) != 0) + { + return xPlane.GetPlane.Plane.BodyColor.G.CompareTo(yPlane.GetPlane.Plane.BodyColor.G); + } + if (xPlane.GetPlane.Plane.BodyColor.B.CompareTo(yPlane.GetPlane.Plane.BodyColor.B) != 0) + { + return xPlane.GetPlane.Plane.BodyColor.B.CompareTo(yPlane.GetPlane.Plane.BodyColor.B); + } + + if (xPlane.GetPlane.Plane is EntityWarPlane xWarmlyEntity && yPlane.GetPlane.Plane is EntityWarPlane yWarmlyEntity) + { + if (xWarmlyEntity.DopColor.R.CompareTo(yWarmlyEntity.DopColor.R) != 0) + { + return xWarmlyEntity.DopColor.R.CompareTo(yWarmlyEntity.DopColor.R); + } + if (xWarmlyEntity.DopColor.G.CompareTo(yWarmlyEntity.DopColor.G) != 0) + { + return xWarmlyEntity.DopColor.G.CompareTo(yWarmlyEntity.DopColor.G); + } + if (xWarmlyEntity.DopColor.B.CompareTo(yWarmlyEntity.DopColor.B) != 0) + { + return xWarmlyEntity.DopColor.B.CompareTo(yWarmlyEntity.DopColor.B); + } + } + + var speedCompare = xPlane.GetPlane.Plane.Speed.CompareTo(yPlane.GetPlane.Plane.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return xPlane.GetPlane.Plane.Weight.CompareTo(yPlane.GetPlane.Plane.Weight); + + } + } +} diff --git a/ProjectPlane/ProjectPlane/PlaneCompareByType.cs b/ProjectPlane/ProjectPlane/PlaneCompareByType.cs new file mode 100644 index 0000000..fab5e33 --- /dev/null +++ b/ProjectPlane/ProjectPlane/PlaneCompareByType.cs @@ -0,0 +1,56 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectPlane +{ + internal class PlaneCompareByType : IComparer + { + 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 xPlane = x as DrawingObject; + var yPlane = y as DrawingObject; + 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 == "DrawingPlane") + { + return -1; + } + return 1; + } + var speedCompare = + xPlane.GetPlane.Plane.Speed.CompareTo(yPlane.GetPlane.Plane.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return xPlane.GetPlane.Plane.Weight.CompareTo(yPlane.GetPlane.Plane.Weight); + } + } +} diff --git a/ProjectPlane/ProjectPlane/SetPlanesGeneric.cs b/ProjectPlane/ProjectPlane/SetPlanesGeneric.cs index 58869f1..3212255 100644 --- a/ProjectPlane/ProjectPlane/SetPlanesGeneric.cs +++ b/ProjectPlane/ProjectPlane/SetPlanesGeneric.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace ProjectPlane { - internal class SetPlanesGeneric where T : class + internal class SetPlanesGeneric where T : class, IEquatable { /// /// Список объектов, которые храним @@ -43,6 +43,8 @@ namespace ProjectPlane /// public int Insert(T plane, int position) { + if (_places.Contains(plane)) return -1; + if (position < 0 || position >= _maxCount) throw new StorageOverflowException(_maxCount); _places.Insert(position, plane); return position; @@ -99,5 +101,14 @@ namespace ProjectPlane } } } + public void SortSet(IComparer comparer) + { + if (comparer == null) + { + return; + } + _places.Sort(comparer); + } + } }