From 9b5dfadc815438e3864c5a6bb54c9f2b200c9099 Mon Sep 17 00:00:00 2001 From: the Date: Tue, 6 Dec 2022 00:13:52 +0400 Subject: [PATCH] Done --- Ship/Ship/DrawingObject.cs | 41 +++++++++--- Ship/Ship/FormMapWithSetShips.Designer.cs | 54 ++++++++++++---- Ship/Ship/FormMapWithSetShips.cs | 26 ++++++-- Ship/Ship/MapWithSetShipsGeneric.cs | 9 +++ Ship/Ship/SetShipsGeneric.cs | 13 ++++ Ship/Ship/ShipCompareByColor.cs | 79 +++++++++++++++++++++++ Ship/Ship/ShipCompareByType.cs | 55 ++++++++++++++++ 7 files changed, 251 insertions(+), 26 deletions(-) create mode 100644 Ship/Ship/ShipCompareByColor.cs create mode 100644 Ship/Ship/ShipCompareByType.cs diff --git a/Ship/Ship/DrawingObject.cs b/Ship/Ship/DrawingObject.cs index c1d0797..fb3d6bc 100644 --- a/Ship/Ship/DrawingObject.cs +++ b/Ship/Ship/DrawingObject.cs @@ -17,6 +17,8 @@ namespace Ship public float Step => _ship?.Ship?.Step ?? 0; + public DrawingShip GetShip => _ship; + public (float Left, float Right, float Top, float Bottom) GetCurrentPosition() { return _ship?.GetCurrentPosition() ?? default; @@ -47,26 +49,49 @@ namespace Ship { return false; } - var otherCar = other as DrawingObject; - if (otherCar == null) + var otherShip = other as DrawingObject; + if (otherShip == null) { return false; } - var car = _ship.Ship; - var otherCarCar = otherCar._ship.Ship; - if (car.Speed != otherCarCar.Speed) + var ship = _ship.Ship; + var otherShipShip = otherShip._ship.Ship; + if (ship.GetType() != otherShipShip.GetType()) { return false; } - if (car.Weight != otherCarCar.Weight) + + if (ship.Speed != otherShipShip.Speed) { return false; } - if (car.BodyColor != otherCarCar.BodyColor) + if (ship.Weight != otherShipShip.Weight) { return false; } - // TODO доделать проверки в случае продвинутого объекта + if (ship.BodyColor != otherShipShip.BodyColor) + { + return false; + } + + if (ship is EntityMotorShip motor && otherShipShip is EntityMotorShip otherMotor) + { + if (motor.DopColor != otherMotor.DopColor) + { + return false; + } + + if (motor.Pipes != otherMotor.Pipes) + { + return false; + } + + if (motor.Fueltank != otherMotor.Fueltank) + { + return false; + } + } + return true; } } diff --git a/Ship/Ship/FormMapWithSetShips.Designer.cs b/Ship/Ship/FormMapWithSetShips.Designer.cs index 431ea79..107678b 100644 --- a/Ship/Ship/FormMapWithSetShips.Designer.cs +++ b/Ship/Ship/FormMapWithSetShips.Designer.cs @@ -29,6 +29,8 @@ private void InitializeComponent() { this.groupBoxTools = new System.Windows.Forms.GroupBox(); + this.buttonSortByColor = new System.Windows.Forms.Button(); + this.buttonSortByType = new System.Windows.Forms.Button(); this.groupBoxMaps = new System.Windows.Forms.GroupBox(); this.buttonDeleteMap = new System.Windows.Forms.Button(); this.listBoxMaps = new System.Windows.Forms.ListBox(); @@ -59,6 +61,8 @@ // // groupBoxTools // + this.groupBoxTools.Controls.Add(this.buttonSortByColor); + this.groupBoxTools.Controls.Add(this.buttonSortByType); this.groupBoxTools.Controls.Add(this.groupBoxMaps); this.groupBoxTools.Controls.Add(this.buttonRight); this.groupBoxTools.Controls.Add(this.buttonDown); @@ -72,11 +76,31 @@ this.groupBoxTools.Dock = System.Windows.Forms.DockStyle.Right; this.groupBoxTools.Location = new System.Drawing.Point(767, 24); this.groupBoxTools.Name = "groupBoxTools"; - this.groupBoxTools.Size = new System.Drawing.Size(200, 595); + this.groupBoxTools.Size = new System.Drawing.Size(200, 713); this.groupBoxTools.TabIndex = 0; this.groupBoxTools.TabStop = false; this.groupBoxTools.Text = "Инструменты"; // + // buttonSortByColor + // + this.buttonSortByColor.Location = new System.Drawing.Point(19, 361); + this.buttonSortByColor.Name = "buttonSortByColor"; + this.buttonSortByColor.Size = new System.Drawing.Size(162, 38); + this.buttonSortByColor.TabIndex = 13; + 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, 317); + this.buttonSortByType.Name = "buttonSortByType"; + this.buttonSortByType.Size = new System.Drawing.Size(162, 38); + this.buttonSortByType.TabIndex = 12; + this.buttonSortByType.Text = "Сортировать по типу"; + this.buttonSortByType.UseVisualStyleBackColor = true; + this.buttonSortByType.Click += new System.EventHandler(this.buttonSortByType_Click); + // // groupBoxMaps // this.groupBoxMaps.Controls.Add(this.buttonDeleteMap); @@ -145,7 +169,7 @@ this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonRight.BackgroundImage = global::Ship.Properties.Resources.arrowRight; this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonRight.Location = new System.Drawing.Point(124, 553); + this.buttonRight.Location = new System.Drawing.Point(124, 671); this.buttonRight.Name = "buttonRight"; this.buttonRight.Size = new System.Drawing.Size(30, 30); this.buttonRight.TabIndex = 10; @@ -157,7 +181,7 @@ this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonDown.BackgroundImage = global::Ship.Properties.Resources.arrowDown; this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonDown.Location = new System.Drawing.Point(88, 553); + this.buttonDown.Location = new System.Drawing.Point(88, 671); this.buttonDown.Name = "buttonDown"; this.buttonDown.Size = new System.Drawing.Size(30, 30); this.buttonDown.TabIndex = 9; @@ -169,7 +193,7 @@ this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonLeft.BackgroundImage = global::Ship.Properties.Resources.arrowLeft; this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonLeft.Location = new System.Drawing.Point(52, 553); + this.buttonLeft.Location = new System.Drawing.Point(52, 671); this.buttonLeft.Name = "buttonLeft"; this.buttonLeft.Size = new System.Drawing.Size(30, 30); this.buttonLeft.TabIndex = 8; @@ -181,7 +205,7 @@ this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonUp.BackgroundImage = global::Ship.Properties.Resources.arrowUp; this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonUp.Location = new System.Drawing.Point(88, 517); + this.buttonUp.Location = new System.Drawing.Point(88, 635); this.buttonUp.Name = "buttonUp"; this.buttonUp.Size = new System.Drawing.Size(30, 30); this.buttonUp.TabIndex = 7; @@ -190,7 +214,7 @@ // // buttonShowOnMap // - this.buttonShowOnMap.Location = new System.Drawing.Point(19, 498); + this.buttonShowOnMap.Location = new System.Drawing.Point(19, 592); this.buttonShowOnMap.Name = "buttonShowOnMap"; this.buttonShowOnMap.Size = new System.Drawing.Size(169, 37); this.buttonShowOnMap.TabIndex = 5; @@ -200,7 +224,7 @@ // // buttonShowStorage // - this.buttonShowStorage.Location = new System.Drawing.Point(19, 455); + this.buttonShowStorage.Location = new System.Drawing.Point(19, 549); this.buttonShowStorage.Name = "buttonShowStorage"; this.buttonShowStorage.Size = new System.Drawing.Size(169, 37); this.buttonShowStorage.TabIndex = 4; @@ -210,7 +234,7 @@ // // buttonRemoveShip // - this.buttonRemoveShip.Location = new System.Drawing.Point(19, 412); + this.buttonRemoveShip.Location = new System.Drawing.Point(19, 506); this.buttonRemoveShip.Name = "buttonRemoveShip"; this.buttonRemoveShip.Size = new System.Drawing.Size(169, 37); this.buttonRemoveShip.TabIndex = 3; @@ -220,7 +244,7 @@ // // maskedTextBoxPosition // - this.maskedTextBoxPosition.Location = new System.Drawing.Point(19, 383); + this.maskedTextBoxPosition.Location = new System.Drawing.Point(19, 477); this.maskedTextBoxPosition.Mask = "00"; this.maskedTextBoxPosition.Name = "maskedTextBoxPosition"; this.maskedTextBoxPosition.Size = new System.Drawing.Size(169, 23); @@ -228,7 +252,7 @@ // // buttonAddShip // - this.buttonAddShip.Location = new System.Drawing.Point(19, 340); + this.buttonAddShip.Location = new System.Drawing.Point(19, 434); this.buttonAddShip.Name = "buttonAddShip"; this.buttonAddShip.Size = new System.Drawing.Size(169, 37); this.buttonAddShip.TabIndex = 1; @@ -241,7 +265,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(767, 595); + this.pictureBox.Size = new System.Drawing.Size(767, 713); this.pictureBox.TabIndex = 1; this.pictureBox.TabStop = false; // @@ -266,14 +290,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); // @@ -290,7 +314,7 @@ // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(967, 619); + this.ClientSize = new System.Drawing.Size(967, 737); this.Controls.Add(this.pictureBox); this.Controls.Add(this.groupBoxTools); this.Controls.Add(this.menuStrip); @@ -335,5 +359,7 @@ private ToolStripMenuItem LoadToolStripMenuItem; private OpenFileDialog openFileDialog; private SaveFileDialog saveFileDialog; + private Button buttonSortByColor; + private Button buttonSortByType; } } \ No newline at end of file diff --git a/Ship/Ship/FormMapWithSetShips.cs b/Ship/Ship/FormMapWithSetShips.cs index ff294a2..77bdea9 100644 --- a/Ship/Ship/FormMapWithSetShips.cs +++ b/Ship/Ship/FormMapWithSetShips.cs @@ -32,10 +32,6 @@ namespace Ship /// private readonly ILogger _logger; /// - /// Объект от класса карты с набором объектов - /// - private MapWithSetShipsGeneric _mapShipsCollectionGeneric; - /// /// Конструктор /// public FormMapWithSetShips(ILogger logger) @@ -317,5 +313,27 @@ namespace Ship { } + + private void buttonSortByType_Click(object sender, EventArgs e) + { + if (listBoxMaps.SelectedIndex == -1) + { + return; + } + _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? + string.Empty].Sort(new ShipCompareByType()); + 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 ShipCompareByColor()); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); + + } } } diff --git a/Ship/Ship/MapWithSetShipsGeneric.cs b/Ship/Ship/MapWithSetShipsGeneric.cs index 61d6634..1846f82 100644 --- a/Ship/Ship/MapWithSetShipsGeneric.cs +++ b/Ship/Ship/MapWithSetShipsGeneric.cs @@ -207,5 +207,14 @@ namespace Ship i++; } } + + /// + /// Сортировка + /// + /// + public void Sort(IComparer comparer) + { + _setShips.SortSet(comparer); + } } } diff --git a/Ship/Ship/SetShipsGeneric.cs b/Ship/Ship/SetShipsGeneric.cs index 71a4add..23aa759 100644 --- a/Ship/Ship/SetShipsGeneric.cs +++ b/Ship/Ship/SetShipsGeneric.cs @@ -123,5 +123,18 @@ namespace Ship } } } + + /// + /// Сортировка набора объектов + /// + /// + public void SortSet(IComparer comparer) + { + if (comparer == null) + { + return; + } + _places.Sort(comparer); + } } } diff --git a/Ship/Ship/ShipCompareByColor.cs b/Ship/Ship/ShipCompareByColor.cs new file mode 100644 index 0000000..ba48e97 --- /dev/null +++ b/Ship/Ship/ShipCompareByColor.cs @@ -0,0 +1,79 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Ship +{ + internal class ShipCompareByColor : 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 xShip = x as DrawingObject; + var yShip = y as DrawingObject; + if (xShip == null && yShip == null) + { + return 0; + } + if (xShip == null && yShip != null) + { + return 1; + } + if (xShip != null && yShip == null) + { + return -1; + } + + if (xShip.GetShip.Ship.BodyColor.R.CompareTo(yShip.GetShip.Ship.BodyColor.R) != 0) + { + return xShip.GetShip.Ship.BodyColor.R.CompareTo(yShip.GetShip.Ship.BodyColor.R); + } + if (xShip.GetShip.Ship.BodyColor.G.CompareTo(yShip.GetShip.Ship.BodyColor.G) != 0) + { + return xShip.GetShip.Ship.BodyColor.G.CompareTo(yShip.GetShip.Ship.BodyColor.G); + } + if (xShip.GetShip.Ship.BodyColor.B.CompareTo(yShip.GetShip.Ship.BodyColor.B) != 0) + { + return xShip.GetShip.Ship.BodyColor.B.CompareTo(yShip.GetShip.Ship.BodyColor.B); + } + + if (xShip.GetShip.Ship is EntityMotorShip xMotorEntity && yShip.GetShip.Ship is EntityMotorShip yMotorEntity) + { + if (xMotorEntity.DopColor.R.CompareTo(yMotorEntity.DopColor.R) != 0) + { + return xMotorEntity.DopColor.R.CompareTo(yMotorEntity.DopColor.R); + } + if (xMotorEntity.DopColor.G.CompareTo(yMotorEntity.DopColor.G) != 0) + { + return xMotorEntity.DopColor.G.CompareTo(yMotorEntity.DopColor.G); + } + if (xMotorEntity.DopColor.B.CompareTo(yMotorEntity.DopColor.B) != 0) + { + return xMotorEntity.DopColor.B.CompareTo(yMotorEntity.DopColor.B); + } + } + + var speedCompare = xShip.GetShip.Ship.Speed.CompareTo(yShip.GetShip.Ship.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return xShip.GetShip.Ship.Weight.CompareTo(yShip.GetShip.Ship.Weight); + + } + + } +} diff --git a/Ship/Ship/ShipCompareByType.cs b/Ship/Ship/ShipCompareByType.cs new file mode 100644 index 0000000..b001436 --- /dev/null +++ b/Ship/Ship/ShipCompareByType.cs @@ -0,0 +1,55 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Ship +{ + internal class ShipCompareByType : 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 xShip = x as DrawingObject; + var yShip = y as DrawingObject; + if (xShip == null && yShip == null) + { + return 0; + } + if (xShip == null && yShip != null) + { + return 1; + } + if (xShip != null && yShip == null) + { + return -1; + } + if (xShip.GetShip.GetType().Name != yShip.GetShip.GetType().Name) + { + if (xShip.GetShip.GetType().Name == "DrawingShip") + { + return -1; + } + return 1; + } + var speedCompare = xShip.GetShip.Ship.Speed.CompareTo(yShip.GetShip.Ship.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return xShip.GetShip.Ship.Weight.CompareTo(yShip.GetShip.Ship.Weight); + } + } +}