diff --git a/AirplaneWithRadar/AirplaneWithRadar/AbstractMap.cs b/AirplaneWithRadar/AirplaneWithRadar/AbstractMap.cs index 2dfb97e..68b31f1 100644 --- a/AirplaneWithRadar/AirplaneWithRadar/AbstractMap.cs +++ b/AirplaneWithRadar/AirplaneWithRadar/AbstractMap.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace AirplaneWithRadar { - internal abstract class AbstractMap + internal abstract class AbstractMap : IEquatable { private IDrawningObject _drawningObject = null; protected int[,] _map = null; @@ -135,5 +135,44 @@ namespace AirplaneWithRadar 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) + { + return false; + } + var otherMap = other as AbstractMap; + if (otherMap == null) + { + return false; + } + if (_width != otherMap._width) + { + return false; + } + if (_height != otherMap._height) + { + return false; + } + if (_size_x != otherMap._size_x) + { + return false; + } + if (_size_y != otherMap._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] != otherMap._map[i, j]) + { + return false; + } + } + } + return true; + } } } diff --git a/AirplaneWithRadar/AirplaneWithRadar/AirplaneCompareByColor.cs b/AirplaneWithRadar/AirplaneWithRadar/AirplaneCompareByColor.cs new file mode 100644 index 0000000..8e9a94c --- /dev/null +++ b/AirplaneWithRadar/AirplaneWithRadar/AirplaneCompareByColor.cs @@ -0,0 +1,62 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AirplaneWithRadar +{ + internal class AirplaneCompareByColor : 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 xAirplane = x as DrawningObjectAirplane; + var yAirplane = y as DrawningObjectAirplane; + if (xAirplane == null && yAirplane == null) + { + return 0; + } + if (xAirplane == null && yAirplane != null) + { + return 1; + } + if (xAirplane != null && yAirplane == null) + { + return -1; + } + var xEntityAirplane = xAirplane._airplane.Airplane; + var yEntityAirplane = yAirplane._airplane.Airplane; + var baseColorCompare = xEntityAirplane.BodyColor.ToArgb().CompareTo(yEntityAirplane.BodyColor.ToArgb()); + if (baseColorCompare != 0) + { + return baseColorCompare; + } + if (xEntityAirplane is EntityAirplaneWithRadar xAirplaneWithRadar && yEntityAirplane is EntityAirplaneWithRadar yAirplaneWithRadar) + { + var dopColorCompare = xAirplaneWithRadar.DopColor.ToArgb().CompareTo(yAirplaneWithRadar.DopColor.ToArgb()); + if (dopColorCompare != 0) + { + return dopColorCompare; + } + } + 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); + } + } +} diff --git a/AirplaneWithRadar/AirplaneWithRadar/AirplaneCompareByType.cs b/AirplaneWithRadar/AirplaneWithRadar/AirplaneCompareByType.cs new file mode 100644 index 0000000..e7c33a7 --- /dev/null +++ b/AirplaneWithRadar/AirplaneWithRadar/AirplaneCompareByType.cs @@ -0,0 +1,55 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AirplaneWithRadar +{ + internal class AirplaneCompareByType : 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 xAirplane = x as DrawningObjectAirplane; + var yAirplane = y as DrawningObjectAirplane; + if (xAirplane == null && yAirplane == null) + { + return 0; + } + if (xAirplane == null && yAirplane != null) + { + return 1; + } + if (xAirplane != null && yAirplane == null) + { + return -1; + } + if (xAirplane.GetAirplane.GetType().Name != yAirplane.GetAirplane.GetType().Name) + { + if (xAirplane.GetAirplane.GetType().Name == "DrawningAirplane") + { + return -1; + } + return 1; + } + var speedCompare = xAirplane.GetAirplane.Airplane.Speed.CompareTo(yAirplane.GetAirplane.Airplane.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return xAirplane.GetAirplane.Airplane.Weight.CompareTo(yAirplane.GetAirplane.Airplane.Weight); + } + } +} diff --git a/AirplaneWithRadar/AirplaneWithRadar/DrawningObjectAirplane.cs b/AirplaneWithRadar/AirplaneWithRadar/DrawningObjectAirplane.cs index 4b7c5a0..4eb2896 100644 --- a/AirplaneWithRadar/AirplaneWithRadar/DrawningObjectAirplane.cs +++ b/AirplaneWithRadar/AirplaneWithRadar/DrawningObjectAirplane.cs @@ -8,7 +8,7 @@ namespace AirplaneWithRadar { internal class DrawningObjectAirplane : IDrawningObject { - private DrawningAirplane _airplane = null; + public DrawningAirplane _airplane { get; private set; } public DrawningObjectAirplane(DrawningAirplane airplane) { @@ -36,7 +36,33 @@ namespace AirplaneWithRadar { _airplane.DrawTransport(g); } - public string GetInfo() => _airplane?.GetDataForSave(); + public string GetInfo() => _airplane?.GetDataForSave() ?? string.Empty; public static IDrawningObject Create(string data) => new DrawningObjectAirplane(data.CreateDrawningAirplane()); + public bool Equals(IDrawningObject? other) + { + if (other is not DrawningObjectAirplane 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 EntityAirplaneWithRadar entityAirplaneWithRadar && + otherEntity is EntityAirplaneWithRadar otherEntityAirplaneWithRadar && ( + entityAirplaneWithRadar.Ladder != otherEntityAirplaneWithRadar.Ladder || + entityAirplaneWithRadar.DopColor != otherEntityAirplaneWithRadar.DopColor || + entityAirplaneWithRadar.Window != otherEntityAirplaneWithRadar.Window || + entityAirplaneWithRadar.Radar != otherEntityAirplaneWithRadar.Radar)) + { + return false; + } + return true; + } } } diff --git a/AirplaneWithRadar/AirplaneWithRadar/FormMap.Designer.cs b/AirplaneWithRadar/AirplaneWithRadar/FormMap.Designer.cs deleted file mode 100644 index 4185cf7..0000000 --- a/AirplaneWithRadar/AirplaneWithRadar/FormMap.Designer.cs +++ /dev/null @@ -1,209 +0,0 @@ -namespace AirplaneWithRadar -{ - partial class FormMap - { - /// - /// Required designer variable. - /// - private System.ComponentModel.IContainer components = null; - - /// - /// Clean up any resources being used. - /// - /// true if managed resources should be disposed; otherwise, false. - protected override void Dispose(bool disposing) - { - if (disposing && (components != null)) - { - components.Dispose(); - } - base.Dispose(disposing); - } - - #region Windows Form Designer generated code - - /// - /// Required method for Designer support - do not modify - /// the contents of this method with the code editor. - /// - private void InitializeComponent() - { - this.pictureBoxAirplane = new System.Windows.Forms.PictureBox(); - this.statusStrip1 = new System.Windows.Forms.StatusStrip(); - this.toolStripStatusLabelSpeed = new System.Windows.Forms.ToolStripStatusLabel(); - this.toolStripStatusLabelWeight = new System.Windows.Forms.ToolStripStatusLabel(); - this.toolStripStatusLabelBodyColor = new System.Windows.Forms.ToolStripStatusLabel(); - this.buttonCreate = new System.Windows.Forms.Button(); - this.buttonUp = new System.Windows.Forms.Button(); - this.buttonLeft = new System.Windows.Forms.Button(); - this.buttonDown = new System.Windows.Forms.Button(); - this.buttonRight = new System.Windows.Forms.Button(); - this.buttonCreateModify = new System.Windows.Forms.Button(); - this.comboBoxSelectorMap = new System.Windows.Forms.ComboBox(); - ((System.ComponentModel.ISupportInitialize)(this.pictureBoxAirplane)).BeginInit(); - this.statusStrip1.SuspendLayout(); - this.SuspendLayout(); - // - // pictureBoxAirplane - // - this.pictureBoxAirplane.Dock = System.Windows.Forms.DockStyle.Fill; - this.pictureBoxAirplane.Location = new System.Drawing.Point(0, 0); - this.pictureBoxAirplane.Name = "pictureBoxAirplane"; - this.pictureBoxAirplane.Size = new System.Drawing.Size(800, 450); - this.pictureBoxAirplane.TabIndex = 0; - this.pictureBoxAirplane.TabStop = false; - // - // statusStrip1 - // - this.statusStrip1.ImageScalingSize = new System.Drawing.Size(20, 20); - this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { - this.toolStripStatusLabelSpeed, - this.toolStripStatusLabelWeight, - this.toolStripStatusLabelBodyColor}); - this.statusStrip1.Location = new System.Drawing.Point(0, 428); - this.statusStrip1.Name = "statusStrip1"; - this.statusStrip1.Size = new System.Drawing.Size(800, 22); - this.statusStrip1.TabIndex = 1; - this.statusStrip1.Text = "Скорость"; - // - // toolStripStatusLabelSpeed - // - this.toolStripStatusLabelSpeed.Name = "toolStripStatusLabelSpeed"; - this.toolStripStatusLabelSpeed.Size = new System.Drawing.Size(59, 17); - this.toolStripStatusLabelSpeed.Text = "Скорость"; - // - // toolStripStatusLabelWeight - // - this.toolStripStatusLabelWeight.Name = "toolStripStatusLabelWeight"; - this.toolStripStatusLabelWeight.Size = new System.Drawing.Size(26, 17); - this.toolStripStatusLabelWeight.Text = "Вес"; - // - // toolStripStatusLabelBodyColor - // - this.toolStripStatusLabelBodyColor.Name = "toolStripStatusLabelBodyColor"; - this.toolStripStatusLabelBodyColor.Size = new System.Drawing.Size(33, 17); - this.toolStripStatusLabelBodyColor.Text = "Цвет"; - // - // buttonCreate - // - this.buttonCreate.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); - this.buttonCreate.Location = new System.Drawing.Point(12, 402); - this.buttonCreate.Name = "buttonCreate"; - this.buttonCreate.Size = new System.Drawing.Size(75, 23); - this.buttonCreate.TabIndex = 2; - this.buttonCreate.Text = "Создать"; - this.buttonCreate.UseVisualStyleBackColor = true; - this.buttonCreate.Click += new System.EventHandler(this.buttonCreate_Click); - // - // buttonUp - // - this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.buttonUp.BackgroundImage = global::AirplaneWithRadar.Properties.Resources.arrowUp; - this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonUp.Location = new System.Drawing.Point(687, 357); - this.buttonUp.Name = "buttonUp"; - this.buttonUp.Size = new System.Drawing.Size(30, 30); - this.buttonUp.TabIndex = 3; - this.buttonUp.UseVisualStyleBackColor = true; - this.buttonUp.Click += new System.EventHandler(this.buttonMove_Click); - // - // buttonLeft - // - this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.buttonLeft.BackgroundImage = global::AirplaneWithRadar.Properties.Resources.arrowLeft; - this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonLeft.Location = new System.Drawing.Point(651, 395); - this.buttonLeft.Name = "buttonLeft"; - this.buttonLeft.Size = new System.Drawing.Size(30, 30); - this.buttonLeft.TabIndex = 4; - this.buttonLeft.UseVisualStyleBackColor = true; - this.buttonLeft.Click += new System.EventHandler(this.buttonMove_Click); - // - // buttonDown - // - this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.buttonDown.BackgroundImage = global::AirplaneWithRadar.Properties.Resources.arrowDown; - this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonDown.Location = new System.Drawing.Point(687, 395); - this.buttonDown.Name = "buttonDown"; - this.buttonDown.Size = new System.Drawing.Size(30, 30); - this.buttonDown.TabIndex = 5; - this.buttonDown.UseVisualStyleBackColor = true; - this.buttonDown.Click += new System.EventHandler(this.buttonMove_Click); - // - // buttonRight - // - this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.buttonRight.BackgroundImage = global::AirplaneWithRadar.Properties.Resources.arrowRight; - this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonRight.Location = new System.Drawing.Point(723, 395); - this.buttonRight.Name = "buttonRight"; - this.buttonRight.Size = new System.Drawing.Size(30, 30); - this.buttonRight.TabIndex = 6; - this.buttonRight.UseVisualStyleBackColor = true; - this.buttonRight.Click += new System.EventHandler(this.buttonMove_Click); - // - // buttonCreateModify - // - this.buttonCreateModify.Location = new System.Drawing.Point(93, 402); - this.buttonCreateModify.Name = "buttonCreateModify"; - this.buttonCreateModify.Size = new System.Drawing.Size(108, 23); - this.buttonCreateModify.TabIndex = 7; - this.buttonCreateModify.Text = "Модификация"; - this.buttonCreateModify.UseVisualStyleBackColor = true; - this.buttonCreateModify.Click += new System.EventHandler(this.buttonCreateModify_Click); - // - // comboBoxSelectorMap - // - this.comboBoxSelectorMap.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; - this.comboBoxSelectorMap.FormattingEnabled = true; - this.comboBoxSelectorMap.Items.AddRange(new object[] { - "Простая карта", - "Карта неба с облаками", - "Карта туманного неба с грозой"}); - this.comboBoxSelectorMap.Location = new System.Drawing.Point(12, 12); - this.comboBoxSelectorMap.Name = "comboBoxSelectorMap"; - this.comboBoxSelectorMap.Size = new System.Drawing.Size(121, 23); - this.comboBoxSelectorMap.TabIndex = 8; - this.comboBoxSelectorMap.SelectedIndexChanged += new System.EventHandler(this.ComboBoxSelectorMap_SelectedIndexChanged); - // - // FormMap - // - this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); - this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(800, 450); - this.Controls.Add(this.comboBoxSelectorMap); - this.Controls.Add(this.buttonCreateModify); - this.Controls.Add(this.buttonRight); - this.Controls.Add(this.buttonDown); - this.Controls.Add(this.buttonLeft); - this.Controls.Add(this.buttonUp); - this.Controls.Add(this.buttonCreate); - this.Controls.Add(this.statusStrip1); - this.Controls.Add(this.pictureBoxAirplane); - this.Name = "FormMap"; - this.Text = "FormMap"; - ((System.ComponentModel.ISupportInitialize)(this.pictureBoxAirplane)).EndInit(); - this.statusStrip1.ResumeLayout(false); - this.statusStrip1.PerformLayout(); - this.ResumeLayout(false); - this.PerformLayout(); - - } - - #endregion - - private PictureBox pictureBoxAirplane; - private StatusStrip statusStrip1; - private ToolStripStatusLabel toolStripStatusLabelSpeed; - private ToolStripStatusLabel toolStripStatusLabelWeight; - private ToolStripStatusLabel toolStripStatusLabelBodyColor; - private Button buttonCreate; - private Button buttonUp; - private Button buttonLeft; - private Button buttonDown; - private Button buttonRight; - private Button buttonCreateModify; - private ComboBox comboBoxSelectorMap; - } -} \ No newline at end of file diff --git a/AirplaneWithRadar/AirplaneWithRadar/FormMap.cs b/AirplaneWithRadar/AirplaneWithRadar/FormMap.cs deleted file mode 100644 index bae95d5..0000000 --- a/AirplaneWithRadar/AirplaneWithRadar/FormMap.cs +++ /dev/null @@ -1,107 +0,0 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Data; -using System.Drawing; -using System.Linq; -using System.Numerics; -using System.Text; -using System.Threading.Tasks; -using System.Windows.Forms; - -namespace AirplaneWithRadar -{ - public partial class FormMap : Form - { - private AbstractMap _abstractMap; - public FormMap() - { - InitializeComponent(); - _abstractMap = new SimpleMap(); - } - /// - /// Заполнение информации по объекту - /// - /// - private void SetData(DrawningAirplane airplane) - { - toolStripStatusLabelSpeed.Text = $"Скорость: {airplane.Airplane.Speed}"; - toolStripStatusLabelWeight.Text = $"Вес: {airplane.Airplane.Weight}"; - toolStripStatusLabelBodyColor.Text = $"Цвет: {airplane.Airplane.BodyColor.Name}"; - pictureBoxAirplane.Image = _abstractMap.CreateMap(pictureBoxAirplane.Width, pictureBoxAirplane.Height, - new DrawningObjectAirplane(airplane)); - } - /// - /// Обработка нажатия кнопки "Создать" - /// - /// - /// - private void buttonCreate_Click(object sender, EventArgs e) - { - Random rnd = new(); - var ship = new DrawningAirplane(rnd.Next(100, 300), rnd.Next(1000, 2000), Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256))); - SetData(ship); - } - /// - /// Изменение размеров формы - /// - /// - /// - private void buttonMove_Click(object sender, EventArgs e) - { - //получаем имя кнопки - string name = ((Button)sender)?.Name ?? string.Empty; - Direction dir = Direction.None; - switch (name) - { - case "buttonUp": - dir = Direction.Up; - break; - case "buttonDown": - dir = Direction.Down; - break; - case "buttonLeft": - dir = Direction.Left; - break; - case "buttonRight": - dir = Direction.Right; - break; - } - pictureBoxAirplane.Image = _abstractMap?.MoveObject(dir); - } - /// - /// Обработка нажатия кнопки "Модификация" - /// - /// - /// - private void buttonCreateModify_Click(object sender, EventArgs e) - { - Random rnd = new(); - var ship = new DrawningAirplaneWithRadar(rnd.Next(100, 300), rnd.Next(1000, 2000), - Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)), - Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)), - Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0, 2))); - SetData(ship); - } - /// - /// Смена карты - /// - /// - /// - private void ComboBoxSelectorMap_SelectedIndexChanged(object sender, EventArgs e) - { - switch (comboBoxSelectorMap.Text) - { - case "Простая карта": - _abstractMap = new SimpleMap(); - break; - case "Карта неба с облаками": - _abstractMap = new SkyMap(); - break; - case "Карта туманного неба с грозой": - _abstractMap = new FoggySkyMap(); - break; - } - } - } -} diff --git a/AirplaneWithRadar/AirplaneWithRadar/FormMap.resx b/AirplaneWithRadar/AirplaneWithRadar/FormMap.resx deleted file mode 100644 index 5cb320f..0000000 --- a/AirplaneWithRadar/AirplaneWithRadar/FormMap.resx +++ /dev/null @@ -1,63 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - text/microsoft-resx - - - 2.0 - - - System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - 17, 17 - - \ No newline at end of file diff --git a/AirplaneWithRadar/AirplaneWithRadar/FormMapWithSetAirplane.Designer.cs b/AirplaneWithRadar/AirplaneWithRadar/FormMapWithSetAirplane.Designer.cs index f2db7f6..cc481e7 100644 --- a/AirplaneWithRadar/AirplaneWithRadar/FormMapWithSetAirplane.Designer.cs +++ b/AirplaneWithRadar/AirplaneWithRadar/FormMapWithSetAirplane.Designer.cs @@ -28,6 +28,8 @@ /// private void InitializeComponent() { + this.buttonSortByColor = new System.Windows.Forms.Button(); + this.buttonSortByType = new System.Windows.Forms.Button(); this.groupBox = new System.Windows.Forms.GroupBox(); this.buttonDown = new System.Windows.Forms.Button(); this.buttonRight = new System.Windows.Forms.Button(); @@ -57,8 +59,30 @@ this.menuStrip.SuspendLayout(); this.SuspendLayout(); // + // buttonSortByColor + // + this.buttonSortByColor.Location = new System.Drawing.Point(19, 279); + this.buttonSortByColor.Name = "buttonSortByColor"; + this.buttonSortByColor.Size = new System.Drawing.Size(169, 30); + 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(19, 243); + this.buttonSortByType.Name = "buttonSortByType"; + this.buttonSortByType.Size = new System.Drawing.Size(169, 30); + this.buttonSortByType.TabIndex = 11; + this.buttonSortByType.Text = "Сортировать по типу"; + this.buttonSortByType.UseVisualStyleBackColor = true; + this.buttonSortByType.Click += new System.EventHandler(this.ButtonSortByType_Click); + // // groupBox // + this.groupBox.Controls.Add(this.buttonSortByColor); + this.groupBox.Controls.Add(this.buttonSortByType); this.groupBox.Controls.Add(this.buttonDown); this.groupBox.Controls.Add(this.buttonRight); this.groupBox.Controls.Add(this.buttonLeft); @@ -72,7 +96,7 @@ this.groupBox.Dock = System.Windows.Forms.DockStyle.Right; this.groupBox.Location = new System.Drawing.Point(600, 24); this.groupBox.Name = "groupBox"; - this.groupBox.Size = new System.Drawing.Size(200, 492); + this.groupBox.Size = new System.Drawing.Size(200, 590); this.groupBox.TabIndex = 0; this.groupBox.TabStop = false; this.groupBox.Text = "Инструменты"; @@ -82,7 +106,7 @@ this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonDown.BackgroundImage = global::AirplaneWithRadar.Properties.Resources.arrowDown; this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonDown.Location = new System.Drawing.Point(86, 456); + this.buttonDown.Location = new System.Drawing.Point(86, 554); this.buttonDown.Name = "buttonDown"; this.buttonDown.Size = new System.Drawing.Size(30, 30); this.buttonDown.TabIndex = 9; @@ -94,7 +118,7 @@ this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonRight.BackgroundImage = global::AirplaneWithRadar.Properties.Resources.arrowRight; this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonRight.Location = new System.Drawing.Point(122, 456); + this.buttonRight.Location = new System.Drawing.Point(122, 554); this.buttonRight.Name = "buttonRight"; this.buttonRight.Size = new System.Drawing.Size(30, 30); this.buttonRight.TabIndex = 8; @@ -106,7 +130,7 @@ this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonLeft.BackgroundImage = global::AirplaneWithRadar.Properties.Resources.arrowLeft; this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonLeft.Location = new System.Drawing.Point(50, 456); + this.buttonLeft.Location = new System.Drawing.Point(50, 554); this.buttonLeft.Name = "buttonLeft"; this.buttonLeft.Size = new System.Drawing.Size(30, 30); this.buttonLeft.TabIndex = 7; @@ -118,7 +142,7 @@ this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonUp.BackgroundImage = global::AirplaneWithRadar.Properties.Resources.arrowUp; this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonUp.Location = new System.Drawing.Point(86, 420); + this.buttonUp.Location = new System.Drawing.Point(86, 518); this.buttonUp.Name = "buttonUp"; this.buttonUp.Size = new System.Drawing.Size(30, 30); this.buttonUp.TabIndex = 6; @@ -128,7 +152,7 @@ // buttonShowOnMap // this.buttonShowOnMap.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.buttonShowOnMap.Location = new System.Drawing.Point(19, 384); + this.buttonShowOnMap.Location = new System.Drawing.Point(19, 482); this.buttonShowOnMap.Name = "buttonShowOnMap"; this.buttonShowOnMap.Size = new System.Drawing.Size(169, 30); this.buttonShowOnMap.TabIndex = 5; @@ -139,7 +163,7 @@ // buttonShowStorage // this.buttonShowStorage.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.buttonShowStorage.Location = new System.Drawing.Point(19, 348); + this.buttonShowStorage.Location = new System.Drawing.Point(19, 446); this.buttonShowStorage.Name = "buttonShowStorage"; this.buttonShowStorage.Size = new System.Drawing.Size(169, 30); this.buttonShowStorage.TabIndex = 4; @@ -150,7 +174,7 @@ // buttonRemoveAirplane // this.buttonRemoveAirplane.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.buttonRemoveAirplane.Location = new System.Drawing.Point(19, 312); + this.buttonRemoveAirplane.Location = new System.Drawing.Point(19, 410); this.buttonRemoveAirplane.Name = "buttonRemoveAirplane"; this.buttonRemoveAirplane.Size = new System.Drawing.Size(169, 30); this.buttonRemoveAirplane.TabIndex = 3; @@ -161,7 +185,7 @@ // maskedTextBoxPosition // this.maskedTextBoxPosition.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.maskedTextBoxPosition.Location = new System.Drawing.Point(19, 286); + this.maskedTextBoxPosition.Location = new System.Drawing.Point(19, 384); this.maskedTextBoxPosition.Mask = "00"; this.maskedTextBoxPosition.Name = "maskedTextBoxPosition"; this.maskedTextBoxPosition.Size = new System.Drawing.Size(169, 23); @@ -170,7 +194,7 @@ // buttonAddAirplane // this.buttonAddAirplane.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.buttonAddAirplane.Location = new System.Drawing.Point(19, 250); + this.buttonAddAirplane.Location = new System.Drawing.Point(19, 348); this.buttonAddAirplane.Name = "buttonAddAirplane"; this.buttonAddAirplane.Size = new System.Drawing.Size(169, 30); this.buttonAddAirplane.TabIndex = 1; @@ -185,21 +209,21 @@ this.groupBox1.Controls.Add(this.ButtonAddMap); this.groupBox1.Controls.Add(this.textBoxNewMapName); this.groupBox1.Controls.Add(this.comboBoxSelectorMap); - this.groupBox1.Location = new System.Drawing.Point(5, 20); + this.groupBox1.Location = new System.Drawing.Point(6, 21); this.groupBox1.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); this.groupBox1.Name = "groupBox1"; this.groupBox1.Padding = new System.Windows.Forms.Padding(3, 2, 3, 2); - this.groupBox1.Size = new System.Drawing.Size(219, 225); + this.groupBox1.Size = new System.Drawing.Size(194, 204); this.groupBox1.TabIndex = 18; this.groupBox1.TabStop = false; this.groupBox1.Text = "Карты"; // // ButtonDeleteMap // - this.ButtonDeleteMap.Location = new System.Drawing.Point(9, 191); + this.ButtonDeleteMap.Location = new System.Drawing.Point(6, 182); this.ButtonDeleteMap.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); this.ButtonDeleteMap.Name = "ButtonDeleteMap"; - this.ButtonDeleteMap.Size = new System.Drawing.Size(181, 22); + this.ButtonDeleteMap.Size = new System.Drawing.Size(182, 22); this.ButtonDeleteMap.TabIndex = 13; this.ButtonDeleteMap.Text = "Удалить карту"; this.ButtonDeleteMap.UseVisualStyleBackColor = true; @@ -209,7 +233,7 @@ // this.ListBoxMaps.FormattingEnabled = true; this.ListBoxMaps.ItemHeight = 15; - this.ListBoxMaps.Location = new System.Drawing.Point(9, 102); + this.ListBoxMaps.Location = new System.Drawing.Point(6, 102); this.ListBoxMaps.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); this.ListBoxMaps.Name = "ListBoxMaps"; this.ListBoxMaps.Size = new System.Drawing.Size(182, 79); @@ -218,7 +242,7 @@ // // ButtonAddMap // - this.ButtonAddMap.Location = new System.Drawing.Point(9, 76); + this.ButtonAddMap.Location = new System.Drawing.Point(6, 76); this.ButtonAddMap.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); this.ButtonAddMap.Name = "ButtonAddMap"; this.ButtonAddMap.Size = new System.Drawing.Size(181, 22); @@ -229,7 +253,7 @@ // // textBoxNewMapName // - this.textBoxNewMapName.Location = new System.Drawing.Point(9, 26); + this.textBoxNewMapName.Location = new System.Drawing.Point(6, 20); this.textBoxNewMapName.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); this.textBoxNewMapName.Name = "textBoxNewMapName"; this.textBoxNewMapName.Size = new System.Drawing.Size(182, 23); @@ -243,19 +267,18 @@ "Простая карта", "Карта неба с облаками", "Карта туманного неба с грозой"}); - this.comboBoxSelectorMap.Location = new System.Drawing.Point(9, 50); + this.comboBoxSelectorMap.Location = new System.Drawing.Point(6, 49); this.comboBoxSelectorMap.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); this.comboBoxSelectorMap.Name = "comboBoxSelectorMap"; this.comboBoxSelectorMap.Size = new System.Drawing.Size(182, 23); this.comboBoxSelectorMap.TabIndex = 9; - this.comboBoxSelectorMap.SelectedIndexChanged += new System.EventHandler(this.ComboBoxSelectorMap_SelectedIndexChanged); // // pictureBox // 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(600, 492); + this.pictureBox.Size = new System.Drawing.Size(600, 590); this.pictureBox.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize; this.pictureBox.TabIndex = 1; this.pictureBox.TabStop = false; @@ -282,14 +305,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); // @@ -306,7 +329,7 @@ // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(800, 516); + this.ClientSize = new System.Drawing.Size(800, 614); this.Controls.Add(this.pictureBox); this.Controls.Add(this.groupBox); this.Controls.Add(this.menuStrip); @@ -327,6 +350,8 @@ #endregion + private Button buttonSortByColor; + private Button buttonSortByType; private GroupBox groupBox; private Button buttonDown; private Button buttonRight; diff --git a/AirplaneWithRadar/AirplaneWithRadar/FormMapWithSetAirplane.cs b/AirplaneWithRadar/AirplaneWithRadar/FormMapWithSetAirplane.cs index b905e95..148c264 100644 --- a/AirplaneWithRadar/AirplaneWithRadar/FormMapWithSetAirplane.cs +++ b/AirplaneWithRadar/AirplaneWithRadar/FormMapWithSetAirplane.cs @@ -14,10 +14,6 @@ namespace AirplaneWithRadar { public partial class FormMapWithSetAirplane : Form { - /// - /// Объект от класса карты с набором объектов - /// - private MapWithSetAirplaneGeneric _mapAirplaneCollectionGeneric; private readonly Dictionary _mapDict = new() { {"Простая карта", new SimpleMap() }, @@ -56,36 +52,6 @@ namespace AirplaneWithRadar ListBoxMaps.SelectedIndex = index; } } - /// - /// Выбор карты - /// - /// - /// - private void ComboBoxSelectorMap_SelectedIndexChanged(object sender, EventArgs e) - { - AbstractMap map = null; - switch (comboBoxSelectorMap.Text) - { - case "Простая карта": - map = new SimpleMap(); - break; - case "Карта неба с облаками": - map = new SkyMap(); - break; - case "Карта туманного неба с грозой": - map = new FoggySkyMap(); - break; - } - if (map != null) - { - _mapAirplaneCollectionGeneric = new MapWithSetAirplaneGeneric( - pictureBox.Width, pictureBox.Height, map); - } - else - { - _mapAirplaneCollectionGeneric = null; - } - } private void AddAirplane(DrawningAirplane airplane) { try @@ -320,5 +286,33 @@ namespace AirplaneWithRadar } } } + /// + /// Сортировка по типу + /// + /// + /// + private void ButtonSortByType_Click(object sender, EventArgs e) + { + if (ListBoxMaps.SelectedIndex == -1) + { + return; + } + _mapsCollection[ListBoxMaps.SelectedItem?.ToString() ?? string.Empty].Sort(new AirplaneCompareByType()); + 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 AirplaneCompareByColor()); + pictureBox.Image = _mapsCollection[ListBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); + } } } diff --git a/AirplaneWithRadar/AirplaneWithRadar/IDrawningObject.cs b/AirplaneWithRadar/AirplaneWithRadar/IDrawningObject.cs index 15ad11e..902ef56 100644 --- a/AirplaneWithRadar/AirplaneWithRadar/IDrawningObject.cs +++ b/AirplaneWithRadar/AirplaneWithRadar/IDrawningObject.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace AirplaneWithRadar { - internal interface IDrawningObject + internal interface IDrawningObject : IEquatable { /// /// Шаг перемещения объекта diff --git a/AirplaneWithRadar/AirplaneWithRadar/MapWithSetAirplaneGeneric.cs b/AirplaneWithRadar/AirplaneWithRadar/MapWithSetAirplaneGeneric.cs index ca34620..e98e2cb 100644 --- a/AirplaneWithRadar/AirplaneWithRadar/MapWithSetAirplaneGeneric.cs +++ b/AirplaneWithRadar/AirplaneWithRadar/MapWithSetAirplaneGeneric.cs @@ -2,7 +2,7 @@ { // Карта с набром объектов под нее internal class MapWithSetAirplaneGeneric - where T : class, IDrawningObject + where T : class, IDrawningObject, IEquatable where U : AbstractMap { // Ширина окна отрисовки @@ -90,6 +90,14 @@ _setAirplane.Insert(DrawningObjectAirplane.Create(rec) as T); } } + /// + /// Сортировка + /// + /// + public void Sort(IComparer comparer) + { + _setAirplane.SortSet(comparer); + } // "Взбалтываем" набор, чтобы все элементы оказались в начале private void Shaking() { @@ -123,7 +131,7 @@ g.FillRectangle(brush, 0, 0, _pictureWidth, _pictureHeight); for (int i = 0; i <= _pictureWidth / _placeSizeWidth; i++) { - for (int j = 0; j <= _pictureHeight / _placeSizeHeight + 1; ++j) + for (int j = 0; j <= (_pictureHeight / _placeSizeHeight); ++j) {//линия разметки места g.DrawLine(pen, i * _placeSizeWidth, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth, j * _placeSizeHeight); g.DrawLine(pen, i * _placeSizeWidth, j * _placeSizeHeight + 10, i * _placeSizeWidth + _placeSizeWidth, j * _placeSizeHeight + 10); diff --git a/AirplaneWithRadar/AirplaneWithRadar/SetAirplaneGeneric.cs b/AirplaneWithRadar/AirplaneWithRadar/SetAirplaneGeneric.cs index b51b9cc..d97a4b0 100644 --- a/AirplaneWithRadar/AirplaneWithRadar/SetAirplaneGeneric.cs +++ b/AirplaneWithRadar/AirplaneWithRadar/SetAirplaneGeneric.cs @@ -2,8 +2,8 @@ { // Параметризованный набор объектов internal class SetAirplaneGeneric - where T : class - { + where T : class, IEquatable + { private readonly List _places; public int Count => _places.Count; private readonly int _maxCount; @@ -26,11 +26,14 @@ // Добавление объекта в набор на конкретную позицию public int Insert(T airplane, int position) { - if (Count == _maxCount) + if (_places.Contains(airplane)) + { + throw new ArgumentException($"Объект {airplane} уже присутствует в наборе"); + } + if (position < 0 || position > Count || _maxCount == Count) { throw new StorageOverflowException(_maxCount); } - if (position < 0 || position > _maxCount) return -1; _places.Insert(position, airplane); return position; } @@ -81,5 +84,17 @@ } } } + /// + /// Сортировка набора объектов + /// + /// + public void SortSet(IComparer comparer) + { + if (comparer == null) + { + return; + } + _places.Sort(comparer); + } } } \ No newline at end of file