diff --git a/AirBomber/AirBomber/AbstractMap.cs b/AirBomber/AirBomber/AbstractMap.cs index 8cc655f..e11d0ee 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; @@ -172,5 +172,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..d9915bf --- /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 = (DrawningAirplane?)(x as DrawningObject); + var yAirplane = (DrawningAirplane?)(y as DrawningObject); + if (xAirplane == yAirplane) + { + return 0; + } + if (xAirplane == null) + { + return 1; + } + if (yAirplane == null) + { + return -1; + } + var xEntity = xAirplane.Airplane; + var yEntity = yAirplane.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..f237f9a --- /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 = (DrawningAirplane?)(x as DrawningObject); + var yAirplane = (DrawningAirplane?)(y as DrawningObject); + if (xAirplane == yAirplane) + { + return 0; + } + if (xAirplane == null) + { + return 1; + } + if (yAirplane == null) + { + return -1; + } + if (xAirplane.GetType().Name != yAirplane.GetType().Name) + { + if (xAirplane.Airplane.GetType() == typeof(DrawningAirplane)) + { + return -1; + } + return 1; + } + var speedCompare = xAirplane.Airplane.Speed.CompareTo(yAirplane.Airplane.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return xAirplane.Airplane.Weight.CompareTo(yAirplane.Airplane.Weight); + } + } +} \ No newline at end of file diff --git a/AirBomber/AirBomber/DrawningAirBomber.cs b/AirBomber/AirBomber/DrawningAirBomber.cs index f4af376..b240427 100644 --- a/AirBomber/AirBomber/DrawningAirBomber.cs +++ b/AirBomber/AirBomber/DrawningAirBomber.cs @@ -80,5 +80,23 @@ namespace AirBomber g.DrawLine(pen, baseTail, new PointF(r.Right, r.Top)); g.DrawLine(pen, baseTail, new PointF(r.Right, r.Bottom)); } + + /// + /// Возвращает итератор со свойствами самолета в порядке: + /// Скорость -> Вес -> Цвет корпуса -> Тип двигателя, либо null если их нет -> Количество двигателей -> Дополнительный цвет -> Наличие бомб -> Наличие топливных баков + /// + public override IEnumerator GetEnumerator() + { + IEnumerator enumBase = base.GetEnumerator(); + while (enumBase.MoveNext()) + { + yield return enumBase.Current; + } + var entity = (EntityAirBomber)Airplane; + yield return entity.DopColor; + yield return entity.HasBombs; + yield return entity.HasFuelTanks; + yield break; + } } } diff --git a/AirBomber/AirBomber/DrawningAirplane.cs b/AirBomber/AirBomber/DrawningAirplane.cs index 4c3c0c4..abeabfc 100644 --- a/AirBomber/AirBomber/DrawningAirplane.cs +++ b/AirBomber/AirBomber/DrawningAirplane.cs @@ -1,16 +1,24 @@ -namespace AirBomber +using Microsoft.CodeAnalysis.CSharp.Syntax; +using System.Collections; + +namespace AirBomber { /// /// Класс, отвечающий за прорисовку и перемещение объекта-сущности /// - public class DrawningAirplane + public class DrawningAirplane : IEnumerator, IEnumerable { /// /// Класс-сущность /// public EntityAirplane Airplane { get; protected set; } - + public IAirplaneEngines? DrawningEngines { get; protected set; } + + private IEnumerator? _machineStateEnum = null; + + public object Current => _machineStateEnum?.Current; + /// /// Левая координата отрисовки самолета /// @@ -97,7 +105,7 @@ _pictureHeight = height; // Проверка на нахождение предмета полностью в границах окна if (width <= _airplaneWidth || height <= _airplaneHeight - || _startPosX < 0 || _startPosX + _airplaneWidth > width || + || _startPosX < 0 || _startPosX + _airplaneWidth > width || _startPosY < 0 || _startPosY + _airplaneHeight > height) { _pictureWidth = null; @@ -163,14 +171,14 @@ var h = _airplaneHeight; SolidBrush BodyColorBrush = new(Airplane.BodyColor); // Треугольник самолета - g.FillPolygon(BodyColorBrush, new PointF[] { + g.FillPolygon(BodyColorBrush, new PointF[] { new PointF(x , y + h / 2), new PointF(x + 15, y + h / 2 - 7), new PointF(x + 15, y + h / 2 + 8), }); // Тело самолета g.FillRectangle(BodyColorBrush, x + 15, y + h / 2 - 7, w - 35, 15); - DrawWing(g, BodyColorBrush, new PointF(x + w / 2 - 20, y + h / 2 - 7), new PointF(x + w / 2, y + h / 2 - 7), true , h / 2 - 7); + DrawWing(g, BodyColorBrush, new PointF(x + w / 2 - 20, y + h / 2 - 7), new PointF(x + w / 2, y + h / 2 - 7), true, h / 2 - 7); DrawWing(g, BodyColorBrush, new PointF(x + w / 2 - 20, y + h / 2 + 7), new PointF(x + w / 2, y + h / 2 + 7), false, h / 2 - 7); // Линия примыкающая к хвосту слева @@ -241,5 +249,34 @@ { return new(_startPosX, _startPosY, _airplaneWidth, _airplaneHeight); } + + public bool MoveNext() + { + if (_machineStateEnum == null) + { + _machineStateEnum = GetEnumerator(); + } + return _machineStateEnum.MoveNext(); + } + + public void Reset() => throw new NotSupportedException(); + + public void Dispose() => GC.SuppressFinalize(this); + + /// + /// Возвращает итератор со свойствами самолета в порядке: + /// Скорость -> Вес -> Цвет корпуса -> Тип двигателя, либо null если их нет -> Количество двигателей + /// + virtual public IEnumerator GetEnumerator() + { + yield return Airplane.Speed; + yield return Airplane.Weight; + yield return Airplane.BodyColor; + yield return DrawningEngines; + yield return DrawningEngines?.CountEngines ?? 0; + yield break; + } + + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); } } \ No newline at end of file diff --git a/AirBomber/AirBomber/DrawningObject.cs b/AirBomber/AirBomber/DrawningObject.cs index f2d1a16..36a618a 100644 --- a/AirBomber/AirBomber/DrawningObject.cs +++ b/AirBomber/AirBomber/DrawningObject.cs @@ -42,10 +42,38 @@ namespace AirBomber /// /// The drawning object. /// - public static explicit operator DrawningAirplane(DrawningObject drawningObject) => drawningObject._airplane; + public static explicit operator DrawningAirplane?(DrawningObject? drawningObject) => drawningObject?._airplane ?? null; - 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 || + _airplane.DrawningEngines?.GetType() != otherAirplane._airplane.DrawningEngines?.GetType() || + _airplane.DrawningEngines?.CountEngines != otherAirplane._airplane.DrawningEngines?.CountEngines) + { + 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 073c34e..1a5d698 100644 --- a/AirBomber/AirBomber/FormMapWithSetAirplanes.Designer.cs +++ b/AirBomber/AirBomber/FormMapWithSetAirplanes.Designer.cs @@ -30,6 +30,8 @@ { this.comboTypeEngines = new System.Windows.Forms.ComboBox(); this.groupBoxTools = new System.Windows.Forms.GroupBox(); + this.buttonSortByColor = new System.Windows.Forms.Button(); + this.buttonSortByType = new System.Windows.Forms.Button(); this.btnAddDeletedObject = new System.Windows.Forms.Button(); this.groupBoxMaps = new System.Windows.Forms.GroupBox(); this.buttonAddMap = new System.Windows.Forms.Button(); @@ -60,10 +62,10 @@ this.menuStrip = new System.Windows.Forms.MenuStrip(); this.файлToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.SaveToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.SaveMapToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.LoadToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.openFileDialog = new System.Windows.Forms.OpenFileDialog(); this.saveFileDialog = new System.Windows.Forms.SaveFileDialog(); - this.SaveMapToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.groupBoxTools.SuspendLayout(); this.groupBoxMaps.SuspendLayout(); this.groupBoxGenerate.SuspendLayout(); @@ -89,6 +91,8 @@ // // groupBoxTools // + this.groupBoxTools.Controls.Add(this.buttonSortByColor); + this.groupBoxTools.Controls.Add(this.buttonSortByType); this.groupBoxTools.Controls.Add(this.btnAddDeletedObject); this.groupBoxTools.Controls.Add(this.groupBoxMaps); this.groupBoxTools.Controls.Add(this.maskedTextBoxPosition); @@ -102,16 +106,36 @@ this.groupBoxTools.Controls.Add(this.buttonLeft); this.groupBoxTools.Controls.Add(this.buttonUp); this.groupBoxTools.Dock = System.Windows.Forms.DockStyle.Right; - this.groupBoxTools.Location = new System.Drawing.Point(811, 24); + this.groupBoxTools.Location = new System.Drawing.Point(840, 24); this.groupBoxTools.Name = "groupBoxTools"; - this.groupBoxTools.Size = new System.Drawing.Size(204, 786); + this.groupBoxTools.Size = new System.Drawing.Size(409, 522); this.groupBoxTools.TabIndex = 0; this.groupBoxTools.TabStop = false; this.groupBoxTools.Text = "Инструменты"; // + // buttonSortByColor + // + this.buttonSortByColor.Location = new System.Drawing.Point(223, 413); + this.buttonSortByColor.Name = "buttonSortByColor"; + this.buttonSortByColor.Size = new System.Drawing.Size(175, 35); + 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(223, 372); + this.buttonSortByType.Name = "buttonSortByType"; + this.buttonSortByType.Size = new System.Drawing.Size(175, 35); + this.buttonSortByType.TabIndex = 28; + this.buttonSortByType.Text = "Отсортировать по типу"; + this.buttonSortByType.UseVisualStyleBackColor = true; + this.buttonSortByType.Click += new System.EventHandler(this.ButtonSortByType_Click); + // // btnAddDeletedObject // - this.btnAddDeletedObject.Location = new System.Drawing.Point(17, 689); + this.btnAddDeletedObject.Location = new System.Drawing.Point(25, 317); this.btnAddDeletedObject.Name = "btnAddDeletedObject"; this.btnAddDeletedObject.Size = new System.Drawing.Size(175, 45); this.btnAddDeletedObject.TabIndex = 27; @@ -126,9 +150,9 @@ this.groupBoxMaps.Controls.Add(this.listBoxMaps); this.groupBoxMaps.Controls.Add(this.textBoxNewMapName); this.groupBoxMaps.Controls.Add(this.comboBoxSelectorMap); - this.groupBoxMaps.Location = new System.Drawing.Point(6, 267); + this.groupBoxMaps.Location = new System.Drawing.Point(212, 14); this.groupBoxMaps.Name = "groupBoxMaps"; - this.groupBoxMaps.Size = new System.Drawing.Size(192, 248); + this.groupBoxMaps.Size = new System.Drawing.Size(192, 253); this.groupBoxMaps.TabIndex = 26; this.groupBoxMaps.TabStop = false; this.groupBoxMaps.Text = "Карты"; @@ -137,7 +161,7 @@ // this.buttonAddMap.Location = new System.Drawing.Point(11, 80); this.buttonAddMap.Name = "buttonAddMap"; - this.buttonAddMap.Size = new System.Drawing.Size(175, 35); + this.buttonAddMap.Size = new System.Drawing.Size(175, 47); this.buttonAddMap.TabIndex = 2; this.buttonAddMap.Text = "Добавить карту"; this.buttonAddMap.UseVisualStyleBackColor = true; @@ -145,9 +169,9 @@ // // buttonDeleteMap // - this.buttonDeleteMap.Location = new System.Drawing.Point(11, 206); + this.buttonDeleteMap.Location = new System.Drawing.Point(11, 218); this.buttonDeleteMap.Name = "buttonDeleteMap"; - this.buttonDeleteMap.Size = new System.Drawing.Size(175, 35); + this.buttonDeleteMap.Size = new System.Drawing.Size(175, 29); this.buttonDeleteMap.TabIndex = 4; this.buttonDeleteMap.Text = "Удалить карту"; this.buttonDeleteMap.UseVisualStyleBackColor = true; @@ -157,7 +181,7 @@ // this.listBoxMaps.FormattingEnabled = true; this.listBoxMaps.ItemHeight = 15; - this.listBoxMaps.Location = new System.Drawing.Point(11, 121); + this.listBoxMaps.Location = new System.Drawing.Point(11, 133); this.listBoxMaps.Name = "listBoxMaps"; this.listBoxMaps.Size = new System.Drawing.Size(175, 79); this.listBoxMaps.TabIndex = 3; @@ -184,7 +208,7 @@ // // maskedTextBoxPosition // - this.maskedTextBoxPosition.Location = new System.Drawing.Point(17, 565); + this.maskedTextBoxPosition.Location = new System.Drawing.Point(25, 379); this.maskedTextBoxPosition.Mask = "00"; this.maskedTextBoxPosition.Name = "maskedTextBoxPosition"; this.maskedTextBoxPosition.Size = new System.Drawing.Size(175, 23); @@ -193,9 +217,9 @@ // // buttonRemoveAirplane // - this.buttonRemoveAirplane.Location = new System.Drawing.Point(17, 594); + this.buttonRemoveAirplane.Location = new System.Drawing.Point(25, 413); this.buttonRemoveAirplane.Name = "buttonRemoveAirplane"; - this.buttonRemoveAirplane.Size = new System.Drawing.Size(175, 26); + this.buttonRemoveAirplane.Size = new System.Drawing.Size(175, 35); this.buttonRemoveAirplane.TabIndex = 23; this.buttonRemoveAirplane.Text = "Удалить самолет"; this.buttonRemoveAirplane.UseVisualStyleBackColor = true; @@ -203,9 +227,9 @@ // // buttonShowStorage // - this.buttonShowStorage.Location = new System.Drawing.Point(17, 626); + this.buttonShowStorage.Location = new System.Drawing.Point(223, 273); this.buttonShowStorage.Name = "buttonShowStorage"; - this.buttonShowStorage.Size = new System.Drawing.Size(175, 26); + this.buttonShowStorage.Size = new System.Drawing.Size(175, 35); this.buttonShowStorage.TabIndex = 24; this.buttonShowStorage.Text = "Посмотреть хранилище"; this.buttonShowStorage.UseVisualStyleBackColor = true; @@ -213,9 +237,9 @@ // // buttonShowOnMap // - this.buttonShowOnMap.Location = new System.Drawing.Point(17, 658); + this.buttonShowOnMap.Location = new System.Drawing.Point(223, 317); this.buttonShowOnMap.Name = "buttonShowOnMap"; - this.buttonShowOnMap.Size = new System.Drawing.Size(175, 25); + this.buttonShowOnMap.Size = new System.Drawing.Size(175, 45); this.buttonShowOnMap.TabIndex = 25; this.buttonShowOnMap.Text = "Посмотреть карту"; this.buttonShowOnMap.UseVisualStyleBackColor = true; @@ -223,7 +247,7 @@ // // buttonAddAirplane // - this.buttonAddAirplane.Location = new System.Drawing.Point(17, 521); + this.buttonAddAirplane.Location = new System.Drawing.Point(23, 273); this.buttonAddAirplane.Name = "buttonAddAirplane"; this.buttonAddAirplane.Size = new System.Drawing.Size(175, 35); this.buttonAddAirplane.TabIndex = 21; @@ -333,7 +357,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(90, 750); + this.buttonDown.Location = new System.Drawing.Point(57, 486); this.buttonDown.Name = "buttonDown"; this.buttonDown.Size = new System.Drawing.Size(30, 30); this.buttonDown.TabIndex = 10; @@ -345,7 +369,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(126, 750); + this.buttonRight.Location = new System.Drawing.Point(93, 486); this.buttonRight.Name = "buttonRight"; this.buttonRight.Size = new System.Drawing.Size(30, 30); this.buttonRight.TabIndex = 9; @@ -357,7 +381,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(54, 750); + this.buttonLeft.Location = new System.Drawing.Point(21, 486); this.buttonLeft.Name = "buttonLeft"; this.buttonLeft.Size = new System.Drawing.Size(30, 30); this.buttonLeft.TabIndex = 8; @@ -369,7 +393,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(90, 714); + this.buttonUp.Location = new System.Drawing.Point(57, 450); this.buttonUp.Name = "buttonUp"; this.buttonUp.Size = new System.Drawing.Size(30, 30); this.buttonUp.TabIndex = 7; @@ -381,7 +405,7 @@ this.pictureBox.Dock = System.Windows.Forms.DockStyle.Fill; this.pictureBox.Location = new System.Drawing.Point(0, 24); this.pictureBox.Name = "pictureBox"; - this.pictureBox.Size = new System.Drawing.Size(811, 786); + this.pictureBox.Size = new System.Drawing.Size(840, 522); this.pictureBox.TabIndex = 1; this.pictureBox.TabStop = false; // @@ -391,7 +415,7 @@ this.файлToolStripMenuItem}); this.menuStrip.Location = new System.Drawing.Point(0, 0); this.menuStrip.Name = "menuStrip"; - this.menuStrip.Size = new System.Drawing.Size(1015, 24); + this.menuStrip.Size = new System.Drawing.Size(1249, 24); this.menuStrip.TabIndex = 2; // // файлToolStripMenuItem @@ -411,6 +435,13 @@ this.SaveToolStripMenuItem.Text = "Сохранение"; this.SaveToolStripMenuItem.Click += new System.EventHandler(this.SaveToolStripMenuItem_Click); // + // SaveMapToolStripMenuItem + // + this.SaveMapToolStripMenuItem.Name = "SaveMapToolStripMenuItem"; + this.SaveMapToolStripMenuItem.Size = new System.Drawing.Size(280, 22); + this.SaveMapToolStripMenuItem.Text = "Сохранение Выбранного хранилища"; + this.SaveMapToolStripMenuItem.Click += new System.EventHandler(this.SaveMapToolStripMenuItem_Click); + // // LoadToolStripMenuItem // this.LoadToolStripMenuItem.Name = "LoadToolStripMenuItem"; @@ -426,18 +457,11 @@ // this.saveFileDialog.Filter = "txt file | *.txt"; // - // SaveMapToolStripMenuItem - // - this.SaveMapToolStripMenuItem.Name = "SaveMapToolStripMenuItem"; - this.SaveMapToolStripMenuItem.Size = new System.Drawing.Size(280, 22); - this.SaveMapToolStripMenuItem.Text = "Сохранение Выбранного хранилища"; - this.SaveMapToolStripMenuItem.Click += new System.EventHandler(this.SaveMapToolStripMenuItem_Click); - // // FormMapWithSetAirplanes // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(1015, 810); + this.ClientSize = new System.Drawing.Size(1249, 546); this.Controls.Add(this.pictureBox); this.Controls.Add(this.groupBoxTools); this.Controls.Add(this.menuStrip); @@ -499,5 +523,7 @@ private OpenFileDialog openFileDialog; private SaveFileDialog saveFileDialog; private ToolStripMenuItem SaveMapToolStripMenuItem; + private Button buttonSortByColor; + private Button buttonSortByType; } } \ No newline at end of file diff --git a/AirBomber/AirBomber/FormMapWithSetAirplanes.cs b/AirBomber/AirBomber/FormMapWithSetAirplanes.cs index 66ebb84..09867fb 100644 --- a/AirBomber/AirBomber/FormMapWithSetAirplanes.cs +++ b/AirBomber/AirBomber/FormMapWithSetAirplanes.cs @@ -143,7 +143,7 @@ namespace AirBomber { MessageBox.Show("Объект добавлен"); pictureBox.Image = _mapsCollection[NameMap].ShowSet(); - _logger.LogInformation("Добавлен объект {@Airplane}", (DrawningAirplane)airplane); + _logger.LogInformation("Добавлен объект {@Airplane}", (DrawningAirplane?)airplane); } } catch (StorageOverflowException ex) @@ -151,6 +151,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, (DrawningAirplane?)airplane); + MessageBox.Show(ex.Message, "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); + } } /// /// Перемещение @@ -401,5 +406,23 @@ namespace AirBomber } } + + private void maskedTextBoxPosition_MaskInputRejected(object sender, MaskInputRejectedEventArgs e) + { + + } + + 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/IDrawningObject.cs b/AirBomber/AirBomber/IDrawningObject.cs index 2394173..ef71303 100644 --- a/AirBomber/AirBomber/IDrawningObject.cs +++ b/AirBomber/AirBomber/IDrawningObject.cs @@ -9,7 +9,7 @@ namespace AirBomber /// /// Интерфейс для работы с объектом, прорисовываемым на форме /// - public interface IDrawningObject + public interface IDrawningObject : IEquatable { /// /// Шаг перемещения объекта diff --git a/AirBomber/AirBomber/MapWithSetAirplanesGeneric.cs b/AirBomber/AirBomber/MapWithSetAirplanesGeneric.cs index 387b58d..b116807 100644 --- a/AirBomber/AirBomber/MapWithSetAirplanesGeneric.cs +++ b/AirBomber/AirBomber/MapWithSetAirplanesGeneric.cs @@ -6,8 +6,8 @@ using System.Threading.Tasks; namespace AirBomber { - internal class MapWithSetAirplanesGeneric - where T : class, IDrawningObject + internal class MapWithSetAirplanesGeneric : IComparable> + where T : class, IEquatable, IDrawningObject where U : AbstractMap { /// @@ -210,5 +210,27 @@ namespace AirBomber SetAirplanes.Insert(DrawningObject.Create(rec) as T); } } + + /// + /// Сортировка + /// + /// + public void Sort(IComparer comparer) + { + SetAirplanes.SortSet(comparer); + } + + public int CompareTo(MapWithSetAirplanesGeneric? other) + { + if (other == null) + { + return 1; + } + if (this == other) + { + return 0; + } + return SetAirplanes.Count.CompareTo(other.SetAirplanes.Count); + } } } diff --git a/AirBomber/AirBomber/SetAirplanesGeneric.cs b/AirBomber/AirBomber/SetAirplanesGeneric.cs index 69c97ee..7049d7b 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)) @@ -111,7 +115,23 @@ namespace AirBomber yield break; } } + } + /// + /// Сортировка набора объектов + /// + /// + public void SortSet(IComparer comparer) + { + if (comparer == null) + { + return; + } + _places.Sort(comparer); + } + public void Clear() + { + _places.Clear(); } public void Clear()