diff --git a/AirBomber/AirBomber/AbstractMap.cs b/AirBomber/AirBomber/AbstractMap.cs index 62dbec5..b9104c6 100644 --- a/AirBomber/AirBomber/AbstractMap.cs +++ b/AirBomber/AirBomber/AbstractMap.cs @@ -7,7 +7,7 @@ using System.Threading.Tasks; namespace AirBomber { - internal abstract class AbstractMap + internal abstract class AbstractMap : IEquatable { private IDrawningObject _drawningObject = null; private Bitmap? _staticBitMap; @@ -174,5 +174,32 @@ namespace AirBomber protected abstract void GenerateMap(); protected abstract void DrawRoadPart(Graphics g, int i, int j); protected abstract void DrawBarrierPart(Graphics g, int i, int j); + + public bool Equals(AbstractMap? other) + { + if (other == null || + _map != other._map || + _width != other._width || + _size_x != other._size_x || + _size_y != other._size_y || + _height != other._height || + 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; + } } } \ No newline at end of file diff --git a/AirBomber/AirBomber/AirplaneCompareByColor.cs b/AirBomber/AirBomber/AirplaneCompareByColor.cs new file mode 100644 index 0000000..35b0020 --- /dev/null +++ b/AirBomber/AirBomber/AirplaneCompareByColor.cs @@ -0,0 +1,33 @@ +namespace AirBomber +{ + internal class AirplaneCompareByColor : IComparer + { + public int Compare(IDrawningObject? x, IDrawningObject? y) + { + var xAirplane = x as DrawningObject; + var yAirplane = y as DrawningObject; + if (xAirplane == yAirplane) + { + return 0; + } + if (xAirplane == null) + { + return 1; + } + if (yAirplane == null) + { + return -1; + } + var xEntity = xAirplane.Airplane.Airplane; + var yEntity = yAirplane.Airplane.Airplane; + var colorWeight = xEntity.BodyColor.ToArgb().CompareTo(yEntity.BodyColor.ToArgb()); + if (colorWeight != 0 || + xEntity is not EntityAirBomber xEntityAirBomber || + yEntity is not EntityAirBomber yEntityAirBomber) + { + return colorWeight; + } + return xEntityAirBomber.DopColor.ToArgb().CompareTo(yEntityAirBomber.DopColor.ToArgb()); + } + } +} \ No newline at end of file diff --git a/AirBomber/AirBomber/AirplaneCompareByType.cs b/AirBomber/AirBomber/AirplaneCompareByType.cs new file mode 100644 index 0000000..43bc7ce --- /dev/null +++ b/AirBomber/AirBomber/AirplaneCompareByType.cs @@ -0,0 +1,37 @@ +namespace AirBomber +{ + internal class AirplaneCompareByType : IComparer + { + public int Compare(IDrawningObject? x, IDrawningObject? y) + { + var xAirplane = x as DrawningObject; + var yAirplane = y as DrawningObject; + if (xAirplane == yAirplane) + { + return 0; + } + if (xAirplane == null) + { + return 1; + } + if (yAirplane == null) + { + return -1; + } + if (xAirplane.Airplane.GetType().Name != yAirplane.Airplane.GetType().Name) + { + if (xAirplane.Airplane.GetType() == typeof(DrawningAirplane)) + { + return -1; + } + return 1; + } + var speedCompare = xAirplane.Airplane.Airplane.Speed.CompareTo(yAirplane.Airplane.Airplane.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return xAirplane.Airplane.Airplane.Weight.CompareTo(yAirplane.Airplane.Airplane.Weight); + } + } +} \ No newline at end of file diff --git a/AirBomber/AirBomber/DrawningObject.cs b/AirBomber/AirBomber/DrawningObject.cs index 4bb3594..45790d7 100644 --- a/AirBomber/AirBomber/DrawningObject.cs +++ b/AirBomber/AirBomber/DrawningObject.cs @@ -8,37 +8,63 @@ namespace AirBomber { internal class DrawningObject : IDrawningObject { - private DrawningAirplane _airplane = null; - public DrawningObject(DrawningAirplane airplane) { - _airplane = airplane; + Airplane = airplane; } - public float Step => _airplane?.Airplane?.Step ?? 0; + public float Step => Airplane?.Airplane?.Step ?? 0; + + public DrawningAirplane Airplane { get; private set; } public RectangleF GetCurrentPosition() { - return _airplane.GetCurrentPosition(); + return Airplane.GetCurrentPosition(); } public void MoveObject(Direction direction) { - _airplane?.MoveTransport(direction); + Airplane?.MoveTransport(direction); } public void SetObject(int x, int y, int width, int height) { - _airplane?.SetPosition(x, y, width, height); + Airplane?.SetPosition(x, y, width, height); } public void DrawObject(Graphics g) { - _airplane?.DrawTransport(g); + Airplane?.DrawTransport(g); } - public string GetInfo() => _airplane?.GetDataForSave(); + public string GetInfo() => Airplane?.GetDataForSave() ?? string.Empty; public static IDrawningObject Create(string data) => new DrawningObject(data.CreateDrawningAirplane()); + + public bool Equals(IDrawningObject? other) + { + if (other is not DrawningObject otherAirplane) + { + return false; + } + var entity = Airplane.Airplane; + var otherEntity = otherAirplane.Airplane.Airplane; + if (entity.GetType() != otherEntity.GetType() || + entity.Speed != otherEntity.Speed || + entity.Weight != otherEntity.Weight || + entity.BodyColor != otherEntity.BodyColor) + { + return false; + } + if (entity is EntityAirBomber entityAirBomber && + otherEntity is EntityAirBomber otherEntityAirBomber && ( + entityAirBomber.HasBombs != otherEntityAirBomber.HasBombs || + entityAirBomber.DopColor != otherEntityAirBomber.DopColor || + entityAirBomber.HasFuelTanks != otherEntityAirBomber.HasFuelTanks)) + { + return false; + } + return true; + } } } diff --git a/AirBomber/AirBomber/FormMapWithSetAirplanes.Designer.cs b/AirBomber/AirBomber/FormMapWithSetAirplanes.Designer.cs index 9bd368d..52a27f9 100644 --- a/AirBomber/AirBomber/FormMapWithSetAirplanes.Designer.cs +++ b/AirBomber/AirBomber/FormMapWithSetAirplanes.Designer.cs @@ -51,13 +51,18 @@ this.LoadToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.openFileDialog = new System.Windows.Forms.OpenFileDialog(); this.saveFileDialog = new System.Windows.Forms.SaveFileDialog(); + this.buttonSortByColor = new System.Windows.Forms.Button(); + this.buttonSortByType = new System.Windows.Forms.Button(); this.groupBoxTools.SuspendLayout(); this.groupBoxMaps.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit(); + this.menuStrip.SuspendLayout(); this.SuspendLayout(); // // groupBoxTools // + this.groupBoxTools.Controls.Add(this.buttonSortByType); + this.groupBoxTools.Controls.Add(this.buttonSortByColor); this.groupBoxTools.Controls.Add(this.groupBoxMaps); this.groupBoxTools.Controls.Add(this.maskedTextBoxPosition); this.groupBoxTools.Controls.Add(this.buttonRemoveAirplane); @@ -69,9 +74,9 @@ this.groupBoxTools.Controls.Add(this.buttonShowOnMap); this.groupBoxTools.Controls.Add(this.buttonAddAirplane); this.groupBoxTools.Dock = System.Windows.Forms.DockStyle.Right; - this.groupBoxTools.Location = new System.Drawing.Point(811, 0); + this.groupBoxTools.Location = new System.Drawing.Point(811, 24); this.groupBoxTools.Name = "groupBoxTools"; - this.groupBoxTools.Size = new System.Drawing.Size(204, 632); + this.groupBoxTools.Size = new System.Drawing.Size(204, 625); this.groupBoxTools.TabIndex = 0; this.groupBoxTools.TabStop = false; this.groupBoxTools.Text = "Инструменты"; @@ -140,7 +145,7 @@ // // maskedTextBoxPosition // - this.maskedTextBoxPosition.Location = new System.Drawing.Point(17, 355); + this.maskedTextBoxPosition.Location = new System.Drawing.Point(17, 399); this.maskedTextBoxPosition.Mask = "00"; this.maskedTextBoxPosition.Name = "maskedTextBoxPosition"; this.maskedTextBoxPosition.Size = new System.Drawing.Size(175, 23); @@ -149,7 +154,7 @@ // // buttonRemoveAirplane // - this.buttonRemoveAirplane.Location = new System.Drawing.Point(17, 384); + this.buttonRemoveAirplane.Location = new System.Drawing.Point(17, 428); this.buttonRemoveAirplane.Name = "buttonRemoveAirplane"; this.buttonRemoveAirplane.Size = new System.Drawing.Size(175, 35); this.buttonRemoveAirplane.TabIndex = 3; @@ -159,7 +164,7 @@ // // buttonShowStorage // - this.buttonShowStorage.Location = new System.Drawing.Point(17, 437); + this.buttonShowStorage.Location = new System.Drawing.Point(17, 472); this.buttonShowStorage.Name = "buttonShowStorage"; this.buttonShowStorage.Size = new System.Drawing.Size(175, 35); this.buttonShowStorage.TabIndex = 4; @@ -172,7 +177,7 @@ this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonDown.BackgroundImage = global::AirBomber.Properties.Resources.arrowDown; this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonDown.Location = new System.Drawing.Point(91, 582); + this.buttonDown.Location = new System.Drawing.Point(90, 588); this.buttonDown.Name = "buttonDown"; this.buttonDown.Size = new System.Drawing.Size(30, 30); this.buttonDown.TabIndex = 10; @@ -184,7 +189,7 @@ this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonRight.BackgroundImage = global::AirBomber.Properties.Resources.arrowRight; this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonRight.Location = new System.Drawing.Point(127, 582); + this.buttonRight.Location = new System.Drawing.Point(126, 588); this.buttonRight.Name = "buttonRight"; this.buttonRight.Size = new System.Drawing.Size(30, 30); this.buttonRight.TabIndex = 9; @@ -196,7 +201,7 @@ this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonLeft.BackgroundImage = global::AirBomber.Properties.Resources.arrowLeft; this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonLeft.Location = new System.Drawing.Point(55, 582); + this.buttonLeft.Location = new System.Drawing.Point(54, 588); this.buttonLeft.Name = "buttonLeft"; this.buttonLeft.Size = new System.Drawing.Size(30, 30); this.buttonLeft.TabIndex = 8; @@ -208,7 +213,7 @@ this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonUp.BackgroundImage = global::AirBomber.Properties.Resources.arrowUp; this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonUp.Location = new System.Drawing.Point(91, 546); + this.buttonUp.Location = new System.Drawing.Point(90, 552); this.buttonUp.Name = "buttonUp"; this.buttonUp.Size = new System.Drawing.Size(30, 30); this.buttonUp.TabIndex = 7; @@ -217,7 +222,7 @@ // // buttonShowOnMap // - this.buttonShowOnMap.Location = new System.Drawing.Point(17, 487); + this.buttonShowOnMap.Location = new System.Drawing.Point(17, 513); this.buttonShowOnMap.Name = "buttonShowOnMap"; this.buttonShowOnMap.Size = new System.Drawing.Size(175, 35); this.buttonShowOnMap.TabIndex = 5; @@ -227,7 +232,7 @@ // // buttonAddAirplane // - this.buttonAddAirplane.Location = new System.Drawing.Point(17, 314); + this.buttonAddAirplane.Location = new System.Drawing.Point(17, 358); this.buttonAddAirplane.Name = "buttonAddAirplane"; this.buttonAddAirplane.Size = new System.Drawing.Size(175, 35); this.buttonAddAirplane.TabIndex = 1; @@ -238,9 +243,9 @@ // pictureBox // this.pictureBox.Dock = System.Windows.Forms.DockStyle.Fill; - this.pictureBox.Location = new System.Drawing.Point(0, 0); + this.pictureBox.Location = new System.Drawing.Point(0, 24); this.pictureBox.Name = "pictureBox"; - this.pictureBox.Size = new System.Drawing.Size(811, 632); + this.pictureBox.Size = new System.Drawing.Size(811, 625); this.pictureBox.TabIndex = 1; this.pictureBox.TabStop = false; // @@ -265,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); // @@ -284,11 +289,31 @@ // this.saveFileDialog.Filter = "txt file | *.txt"; // + // buttonSortByColor + // + this.buttonSortByColor.Location = new System.Drawing.Point(17, 317); + this.buttonSortByColor.Name = "buttonSortByColor"; + this.buttonSortByColor.Size = new System.Drawing.Size(175, 35); + this.buttonSortByColor.TabIndex = 11; + this.buttonSortByColor.Text = "Отсортировать по цвету"; + this.buttonSortByColor.UseVisualStyleBackColor = true; + this.buttonSortByColor.Click += new System.EventHandler(this.ButtonSortByColor_Click); + // + // buttonSortByType + // + this.buttonSortByType.Location = new System.Drawing.Point(17, 276); + this.buttonSortByType.Name = "buttonSortByType"; + this.buttonSortByType.Size = new System.Drawing.Size(175, 35); + this.buttonSortByType.TabIndex = 12; + this.buttonSortByType.Text = "Отсортировать по типу"; + this.buttonSortByType.UseVisualStyleBackColor = true; + this.buttonSortByType.Click += new System.EventHandler(this.ButtonSortByType_Click); + // // FormMapWithSetAirplanes // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(1015, 632); + this.ClientSize = new System.Drawing.Size(1015, 649); this.Controls.Add(this.pictureBox); this.Controls.Add(this.groupBoxTools); this.Controls.Add(this.menuStrip); @@ -303,6 +328,7 @@ this.menuStrip.ResumeLayout(false); this.menuStrip.PerformLayout(); this.ResumeLayout(false); + this.PerformLayout(); } @@ -331,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/AirBomber/AirBomber/FormMapWithSetAirplanes.cs b/AirBomber/AirBomber/FormMapWithSetAirplanes.cs index c4f6072..d822bab 100644 --- a/AirBomber/AirBomber/FormMapWithSetAirplanes.cs +++ b/AirBomber/AirBomber/FormMapWithSetAirplanes.cs @@ -149,6 +149,11 @@ namespace AirBomber _logger.LogWarning("Ошибка переполнения хранилища: {0}", ex.Message); MessageBox.Show($"Ошибка переполнения хранилища: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); } + catch (ArgumentException ex) + { + _logger.LogWarning("Ошибка добавления: {0}. Объект: {@Airplane}", ex.Message, airplane); + MessageBox.Show(ex.Message, "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); + } } /// @@ -289,5 +294,18 @@ namespace AirBomber } } } + + private void SortBy(IComparer comparer) + { + if (listBoxMaps.SelectedIndex == -1) + { + return; + } + _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].Sort(comparer); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); + } + + private void ButtonSortByType_Click (object sender, EventArgs e) => SortBy(new AirplaneCompareByType()); + private void ButtonSortByColor_Click(object sender, EventArgs e) => SortBy(new AirplaneCompareByColor()); } } \ No newline at end of file diff --git a/AirBomber/AirBomber/FormMapWithSetAirplanes.resx b/AirBomber/AirBomber/FormMapWithSetAirplanes.resx index f298a7b..1d824c7 100644 --- a/AirBomber/AirBomber/FormMapWithSetAirplanes.resx +++ b/AirBomber/AirBomber/FormMapWithSetAirplanes.resx @@ -57,4 +57,13 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + 17, 17 + + + 125, 17 + + + 258, 17 + \ No newline at end of file diff --git a/AirBomber/AirBomber/IDrawningObject.cs b/AirBomber/AirBomber/IDrawningObject.cs index 87584d8..139757c 100644 --- a/AirBomber/AirBomber/IDrawningObject.cs +++ b/AirBomber/AirBomber/IDrawningObject.cs @@ -9,7 +9,7 @@ namespace AirBomber /// /// Интерфейс для работы с объектом, прорисовываемым на форме /// - internal interface IDrawningObject + internal interface IDrawningObject : IEquatable { /// /// Шаг перемещения объекта diff --git a/AirBomber/AirBomber/MapWithSetAirplanesGeneric.cs b/AirBomber/AirBomber/MapWithSetAirplanesGeneric.cs index 6a18d52..96b94fb 100644 --- a/AirBomber/AirBomber/MapWithSetAirplanesGeneric.cs +++ b/AirBomber/AirBomber/MapWithSetAirplanesGeneric.cs @@ -7,7 +7,7 @@ using System.Threading.Tasks; namespace AirBomber { internal class MapWithSetAirplanesGeneric - where T : class, IDrawningObject + where T : class, IEquatable, IDrawningObject where U : AbstractMap { /// @@ -208,5 +208,14 @@ namespace AirBomber _setAirplanes.Insert(DrawningObject.Create(rec) as T); } } + + /// + /// Сортировка + /// + /// + public void Sort(IComparer comparer) + { + _setAirplanes.SortSet(comparer); + } } } diff --git a/AirBomber/AirBomber/SetAirplanesGeneric.cs b/AirBomber/AirBomber/SetAirplanesGeneric.cs index 986ccf7..af5bc85 100644 --- a/AirBomber/AirBomber/SetAirplanesGeneric.cs +++ b/AirBomber/AirBomber/SetAirplanesGeneric.cs @@ -11,7 +11,7 @@ namespace AirBomber /// /// internal class SetAirplanesGeneric - where T : class + where T : class, IEquatable { /// /// Список объектов, которые храним @@ -51,9 +51,13 @@ namespace AirBomber /// /// Добавляемый самолет /// Позиция + /// Исключения генерируется при попытке добавить дубликат в набор + /// /// Возвращает позицию вставленного объекта, либо -1 если его не удалось вставить public int Insert(T airplane, int position) { + if (_places.Contains(airplane)) + throw new ArgumentException($"Объект {airplane} уже есть в наборе"); if (Count == _maxcount) throw new StorageOverflowException(_maxcount); if (!isCorrectPosition(position)) @@ -113,5 +117,17 @@ namespace AirBomber } } + /// + /// Сортировка набора объектов + /// + /// + public void SortSet(IComparer comparer) + { + if (comparer == null) + { + return; + } + _places.Sort(comparer); + } } }