From e85c542e3f71ace0e3ac0e50633214352beb5045 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?10=D0=93=20=D0=95=D0=B3=D0=BE=D1=80=20=D0=A0=D0=BE=D0=BC?= =?UTF-8?q?=D0=B0=D0=BD=D0=BE=D0=B2?= Date: Mon, 5 Dec 2022 01:25:25 +0400 Subject: [PATCH] =?UTF-8?q?8=20=D0=BB=D0=B0=D0=B1=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HoistingCrane/HoistingCrane/App.config | 58 +++++++++---------- .../DrawingAdvancedHoistingCrane.cs | 2 +- .../DrawingObjectHoistingCrane.cs | 39 ++++++++++++- .../EntityAdvancedHoistingCrane.cs | 4 +- .../HoistingCrane/ExtentionHoistingCrane.cs | 2 +- .../FormMapWithSetHoistingCrane.Designer.cs | 51 +++++++++++----- .../FormMapWithSetHoistingCrane.cs | 45 +++++++------- .../HoistingCrane/HoistingCompareByType.cs | 56 ++++++++++++++++++ .../HoistingCraneCompareByColor.cs | 52 +++++++++++++++++ HoistingCrane/HoistingCrane/IDrawingObject.cs | 2 +- .../MapWithSetHoistingCraneGeneric.cs | 10 +++- .../HoistingCrane/SetHoistingCraneGeneric.cs | 11 +++- 12 files changed, 256 insertions(+), 76 deletions(-) create mode 100644 HoistingCrane/HoistingCrane/HoistingCompareByType.cs create mode 100644 HoistingCrane/HoistingCrane/HoistingCraneCompareByColor.cs diff --git a/HoistingCrane/HoistingCrane/App.config b/HoistingCrane/HoistingCrane/App.config index bc02e6b..7f97274 100644 --- a/HoistingCrane/HoistingCrane/App.config +++ b/HoistingCrane/HoistingCrane/App.config @@ -1,65 +1,65 @@  - + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + diff --git a/HoistingCrane/HoistingCrane/DrawingAdvancedHoistingCrane.cs b/HoistingCrane/HoistingCrane/DrawingAdvancedHoistingCrane.cs index faae436..76d34e0 100644 --- a/HoistingCrane/HoistingCrane/DrawingAdvancedHoistingCrane.cs +++ b/HoistingCrane/HoistingCrane/DrawingAdvancedHoistingCrane.cs @@ -21,7 +21,7 @@ namespace HoistingCrane } Pen pen = new(Color.Black, 3); Brush dopBrush = new SolidBrush(advancedHoistingCrane.DopColor); - if (advancedHoistingCrane.Сounterweight) + if (advancedHoistingCrane.Counterweight) { g.DrawLine(pen, _startPosX + 15, _startPosY + 60, _startPosX + 40, _startPosY + 75); g.DrawLine(pen, _startPosX + 15, _startPosY + 45, _startPosX + 45, _startPosY + 65); diff --git a/HoistingCrane/HoistingCrane/DrawingObjectHoistingCrane.cs b/HoistingCrane/HoistingCrane/DrawingObjectHoistingCrane.cs index cca8e8f..55ea613 100644 --- a/HoistingCrane/HoistingCrane/DrawingObjectHoistingCrane.cs +++ b/HoistingCrane/HoistingCrane/DrawingObjectHoistingCrane.cs @@ -8,16 +8,51 @@ namespace HoistingCrane { _hoistingCrane = hoistingCrane; } + public DrawingHoistingCrane GetHoistingCrane => _hoistingCrane; public float Step => _hoistingCrane?.HoistingCrane?.Step ?? 0; public (float Left, float Right, float Top, float Bottom) GetCurrentPosition() { return _hoistingCrane?.GetCurrentPosition() ?? default; } - public void MoveObject(Direction direction) + public bool Equals(IDrawingObject? other) + { + if (other == null) + { + return false; + } + var otherHoistingCrane = other as DrawingObjectHoistingCrane; + if (otherHoistingCrane == null) + { + return false; + } + var hoistingCrane = _hoistingCrane.HoistingCrane; + var otherHoistingCraneHoistingCrane = otherHoistingCrane._hoistingCrane.HoistingCrane; + if (hoistingCrane.Speed != otherHoistingCraneHoistingCrane.Speed) + { + return false; + } + if (hoistingCrane.Weight != otherHoistingCraneHoistingCrane.Weight) + { + return false; + } + if (hoistingCrane.BodyColor != otherHoistingCraneHoistingCrane.BodyColor) + { + return false; + } + var hoistingcrane = hoistingCrane as EntityAdvancedHoistingCrane; + var otherhoistingcrane = otherHoistingCraneHoistingCrane as EntityAdvancedHoistingCrane; + if (hoistingcrane == null && otherhoistingcrane == null) return true; + if (hoistingcrane == null || otherhoistingcrane == null) return false; + if (hoistingcrane.DopColor != otherhoistingcrane.DopColor) return false; + if (hoistingcrane.Counterweight != otherhoistingcrane.Counterweight) return false; + if (hoistingcrane.Crane != otherhoistingcrane.Crane) return false; + return true; + } + public void MoveObject(Direction direction) { _hoistingCrane?.MoveTransport(direction); } - public void SetObject(int x, int y, int width, int height) + public void SetObject(int x, int y, int width, int height) { _hoistingCrane?.SetPosition(x, y, width, height); } diff --git a/HoistingCrane/HoistingCrane/EntityAdvancedHoistingCrane.cs b/HoistingCrane/HoistingCrane/EntityAdvancedHoistingCrane.cs index 8cbfa2b..723fb12 100644 --- a/HoistingCrane/HoistingCrane/EntityAdvancedHoistingCrane.cs +++ b/HoistingCrane/HoistingCrane/EntityAdvancedHoistingCrane.cs @@ -11,14 +11,14 @@ namespace HoistingCrane // наличие крана public bool Crane { get; private set; } // наличие противовеса - public bool Сounterweight { get; private set; } + public bool Counterweight { get; private set; } public Color DopColor { get; set; } public EntityAdvancedHoistingCrane(int speed, float weight, Color bodyColor, Color dopColor, bool crane, bool counterWeight) : base(speed, weight, bodyColor) { Crane = crane; - Сounterweight = counterWeight; + Counterweight = counterWeight; DopColor = dopColor; } } diff --git a/HoistingCrane/HoistingCrane/ExtentionHoistingCrane.cs b/HoistingCrane/HoistingCrane/ExtentionHoistingCrane.cs index 89f1b80..4520153 100644 --- a/HoistingCrane/HoistingCrane/ExtentionHoistingCrane.cs +++ b/HoistingCrane/HoistingCrane/ExtentionHoistingCrane.cs @@ -47,7 +47,7 @@ namespace HoistingCrane { return str; } - return $"{str}{_separatorForObject}{HoistingCrane.DopColor.Name}{_separatorForObject}{HoistingCrane.Crane}{_separatorForObject}{HoistingCrane.Сounterweight}"; + return $"{str}{_separatorForObject}{HoistingCrane.DopColor.Name}{_separatorForObject}{HoistingCrane.Crane}{_separatorForObject}{HoistingCrane.Counterweight}"; } } } diff --git a/HoistingCrane/HoistingCrane/FormMapWithSetHoistingCrane.Designer.cs b/HoistingCrane/HoistingCrane/FormMapWithSetHoistingCrane.Designer.cs index 73f94b7..cd0e470 100644 --- a/HoistingCrane/HoistingCrane/FormMapWithSetHoistingCrane.Designer.cs +++ b/HoistingCrane/HoistingCrane/FormMapWithSetHoistingCrane.Designer.cs @@ -37,6 +37,8 @@ this.buttonAddMap = new System.Windows.Forms.Button(); this.comboBoxSelectorMap = new System.Windows.Forms.ComboBox(); this.textBoxNewMapName = new System.Windows.Forms.TextBox(); + this.ButtonSortByColor = new System.Windows.Forms.Button(); + this.ButtonSortByType = new System.Windows.Forms.Button(); this.groupBoxTools = new System.Windows.Forms.GroupBox(); this.maskedTextBoxPosition = new System.Windows.Forms.MaskedTextBox(); this.buttonRemoveHoistingCrane = new System.Windows.Forms.Button(); @@ -67,11 +69,11 @@ this.groupBoxMaps.Controls.Add(this.buttonAddMap); this.groupBoxMaps.Controls.Add(this.comboBoxSelectorMap); this.groupBoxMaps.Controls.Add(this.textBoxNewMapName); - this.groupBoxMaps.Location = new System.Drawing.Point(13, 28); + this.groupBoxMaps.Location = new System.Drawing.Point(0, 0); this.groupBoxMaps.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.groupBoxMaps.Name = "groupBoxMaps"; this.groupBoxMaps.Padding = new System.Windows.Forms.Padding(3, 4, 3, 4); - this.groupBoxMaps.Size = new System.Drawing.Size(229, 326); + this.groupBoxMaps.Size = new System.Drawing.Size(213, 267); this.groupBoxMaps.TabIndex = 11; this.groupBoxMaps.TabStop = false; this.groupBoxMaps.Text = "Карты"; @@ -121,7 +123,6 @@ this.comboBoxSelectorMap.Name = "comboBoxSelectorMap"; this.comboBoxSelectorMap.Size = new System.Drawing.Size(200, 28); this.comboBoxSelectorMap.TabIndex = 0; - this.comboBoxSelectorMap.SelectedIndexChanged += new System.EventHandler(this.ComboBoxSelectorMap_SelectedIndexChanged); // // textBoxNewMapName // @@ -131,8 +132,30 @@ this.textBoxNewMapName.Size = new System.Drawing.Size(200, 27); this.textBoxNewMapName.TabIndex = 0; // + // ButtonSortByColor + // + this.ButtonSortByColor.Location = new System.Drawing.Point(7, 316); + this.ButtonSortByColor.Name = "ButtonSortByColor"; + this.ButtonSortByColor.Size = new System.Drawing.Size(206, 26); + this.ButtonSortByColor.TabIndex = 7; + this.ButtonSortByColor.Text = "SortByColor"; + this.ButtonSortByColor.UseVisualStyleBackColor = true; + this.ButtonSortByColor.Click += new System.EventHandler(this.ButtonSortByColor_Click); + // + // ButtonSortByType + // + this.ButtonSortByType.Location = new System.Drawing.Point(7, 279); + this.ButtonSortByType.Name = "ButtonSortByType"; + this.ButtonSortByType.Size = new System.Drawing.Size(206, 31); + this.ButtonSortByType.TabIndex = 6; + this.ButtonSortByType.Text = "SortByType"; + this.ButtonSortByType.UseVisualStyleBackColor = true; + this.ButtonSortByType.Click += new System.EventHandler(this.ButtonSortByType_Click); + // // groupBoxTools // + this.groupBoxTools.Controls.Add(this.ButtonSortByColor); + this.groupBoxTools.Controls.Add(this.ButtonSortByType); this.groupBoxTools.Controls.Add(this.groupBoxMaps); this.groupBoxTools.Controls.Add(this.maskedTextBoxPosition); this.groupBoxTools.Controls.Add(this.buttonRemoveHoistingCrane); @@ -155,20 +178,20 @@ // // maskedTextBoxPosition // - this.maskedTextBoxPosition.Location = new System.Drawing.Point(21, 436); + this.maskedTextBoxPosition.Location = new System.Drawing.Point(7, 424); this.maskedTextBoxPosition.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.maskedTextBoxPosition.Mask = "00"; this.maskedTextBoxPosition.Name = "maskedTextBoxPosition"; - this.maskedTextBoxPosition.Size = new System.Drawing.Size(199, 27); + this.maskedTextBoxPosition.Size = new System.Drawing.Size(206, 27); this.maskedTextBoxPosition.TabIndex = 2; this.maskedTextBoxPosition.ValidatingType = typeof(int); // // buttonRemoveHoistingCrane // - this.buttonRemoveHoistingCrane.Location = new System.Drawing.Point(20, 471); + this.buttonRemoveHoistingCrane.Location = new System.Drawing.Point(7, 471); this.buttonRemoveHoistingCrane.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.buttonRemoveHoistingCrane.Name = "buttonRemoveHoistingCrane"; - this.buttonRemoveHoistingCrane.Size = new System.Drawing.Size(199, 47); + this.buttonRemoveHoistingCrane.Size = new System.Drawing.Size(206, 47); this.buttonRemoveHoistingCrane.TabIndex = 3; this.buttonRemoveHoistingCrane.Text = "Удалить подъёмный кран"; this.buttonRemoveHoistingCrane.UseVisualStyleBackColor = true; @@ -176,10 +199,10 @@ // // buttonShowStorage // - this.buttonShowStorage.Location = new System.Drawing.Point(20, 526); + this.buttonShowStorage.Location = new System.Drawing.Point(7, 526); this.buttonShowStorage.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.buttonShowStorage.Name = "buttonShowStorage"; - this.buttonShowStorage.Size = new System.Drawing.Size(199, 47); + this.buttonShowStorage.Size = new System.Drawing.Size(206, 47); this.buttonShowStorage.TabIndex = 4; this.buttonShowStorage.Text = "Посмотреть хранилище"; this.buttonShowStorage.UseVisualStyleBackColor = true; @@ -239,10 +262,10 @@ // // buttonShowOnMap // - this.buttonShowOnMap.Location = new System.Drawing.Point(21, 584); + this.buttonShowOnMap.Location = new System.Drawing.Point(7, 584); this.buttonShowOnMap.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.buttonShowOnMap.Name = "buttonShowOnMap"; - this.buttonShowOnMap.Size = new System.Drawing.Size(198, 47); + this.buttonShowOnMap.Size = new System.Drawing.Size(206, 47); this.buttonShowOnMap.TabIndex = 5; this.buttonShowOnMap.Text = "Посмотреть карту"; this.buttonShowOnMap.UseVisualStyleBackColor = true; @@ -250,10 +273,10 @@ // // buttonAddHoistingCrane // - this.buttonAddHoistingCrane.Location = new System.Drawing.Point(20, 381); + this.buttonAddHoistingCrane.Location = new System.Drawing.Point(7, 349); this.buttonAddHoistingCrane.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.buttonAddHoistingCrane.Name = "buttonAddHoistingCrane"; - this.buttonAddHoistingCrane.Size = new System.Drawing.Size(199, 47); + this.buttonAddHoistingCrane.Size = new System.Drawing.Size(206, 67); this.buttonAddHoistingCrane.TabIndex = 1; this.buttonAddHoistingCrane.Text = "Добавить подъёмный кран"; this.buttonAddHoistingCrane.UseVisualStyleBackColor = true; @@ -356,5 +379,7 @@ private System.Windows.Forms.ToolStripMenuItem loadToolStripMenuItem; private System.Windows.Forms.OpenFileDialog openFileDialog; private System.Windows.Forms.SaveFileDialog saveFileDialog; + private System.Windows.Forms.Button ButtonSortByColor; + private System.Windows.Forms.Button ButtonSortByType; } } \ No newline at end of file diff --git a/HoistingCrane/HoistingCrane/FormMapWithSetHoistingCrane.cs b/HoistingCrane/HoistingCrane/FormMapWithSetHoistingCrane.cs index 1485f78..10dd3b8 100644 --- a/HoistingCrane/HoistingCrane/FormMapWithSetHoistingCrane.cs +++ b/HoistingCrane/HoistingCrane/FormMapWithSetHoistingCrane.cs @@ -29,7 +29,6 @@ namespace HoistingCrane /// private readonly MapsCollection _mapsCollection; /// - private MapWithSetHoistingCraneGeneric _mapHoistingCraneCollectionGeneric; public FormMapWithSetHoistingCrane(ILogger logger) { InitializeComponent(); @@ -63,29 +62,6 @@ namespace HoistingCrane 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 SecondMap(); - break; - - } - if (map != null) - { - _mapHoistingCraneCollectionGeneric = new MapWithSetHoistingCraneGeneric( - pictureBox.Width, pictureBox.Height, map); - } - else - { - _mapHoistingCraneCollectionGeneric = null; - } - } /// /// Добавление карты /// @@ -245,7 +221,7 @@ namespace HoistingCrane enums = Direction.Right; break; } - pictureBox.Image = _mapHoistingCraneCollectionGeneric.MoveObject(enums); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].MoveObject(enums); } private void saveToolStripMenuItem_Click(object sender, EventArgs e) { @@ -287,5 +263,24 @@ namespace HoistingCrane } } } + + private void ButtonSortByType_Click(object sender, EventArgs e) + { + if (listBoxMaps.SelectedIndex == -1) + { + return; + } + _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].Sort(new HoistingCraneCompareByType()); + 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 HoistingCraneCompareByColor()); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); + } } } diff --git a/HoistingCrane/HoistingCrane/HoistingCompareByType.cs b/HoistingCrane/HoistingCrane/HoistingCompareByType.cs new file mode 100644 index 0000000..d76e983 --- /dev/null +++ b/HoistingCrane/HoistingCrane/HoistingCompareByType.cs @@ -0,0 +1,56 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace HoistingCrane +{ + internal class HoistingCraneCompareByType : 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 xHoistingCrane = x as DrawingObjectHoistingCrane; + var yHoistingCrane = y as DrawingObjectHoistingCrane; + if (xHoistingCrane == null && yHoistingCrane == null) + { + return 0; + } + if (xHoistingCrane == null && yHoistingCrane != null) + { + return 1; + } + if (xHoistingCrane != null && yHoistingCrane == null) + { + return -1; + } + if (xHoistingCrane.GetHoistingCrane.GetType().Name != yHoistingCrane.GetHoistingCrane.GetType().Name) + { + if (xHoistingCrane.GetHoistingCrane.GetType().Name == "DrawingHoistingCrane") + { + return -1; + } + return 1; + } + var speedCompare = + xHoistingCrane.GetHoistingCrane.HoistingCrane.Speed.CompareTo(yHoistingCrane.GetHoistingCrane.HoistingCrane.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return xHoistingCrane.GetHoistingCrane.HoistingCrane.Weight.CompareTo(yHoistingCrane.GetHoistingCrane.HoistingCrane.Weight); + } + } +} diff --git a/HoistingCrane/HoistingCrane/HoistingCraneCompareByColor.cs b/HoistingCrane/HoistingCrane/HoistingCraneCompareByColor.cs new file mode 100644 index 0000000..ce31eb3 --- /dev/null +++ b/HoistingCrane/HoistingCrane/HoistingCraneCompareByColor.cs @@ -0,0 +1,52 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace HoistingCrane +{ + internal class HoistingCraneCompareByColor : 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 xHoistingCrane = x as DrawingObjectHoistingCrane; + var yHoistingCrane = y as DrawingObjectHoistingCrane; + if (xHoistingCrane == null && yHoistingCrane == null) + { + return 0; + } + if (xHoistingCrane == null && yHoistingCrane != null) + { + return 1; + } + if (xHoistingCrane != null && yHoistingCrane == null) + { + return -1; + } + int colorCompare = string.Compare(xHoistingCrane.GetHoistingCrane.HoistingCrane.BodyColor.Name, yHoistingCrane.GetHoistingCrane.HoistingCrane.BodyColor.Name); + if (colorCompare != 0) + { + return colorCompare; + } + var speedCompare = xHoistingCrane.GetHoistingCrane.HoistingCrane.Speed.CompareTo(yHoistingCrane.GetHoistingCrane.HoistingCrane.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return xHoistingCrane.GetHoistingCrane.HoistingCrane.Weight.CompareTo(yHoistingCrane.GetHoistingCrane.HoistingCrane.Weight); + } + } +} diff --git a/HoistingCrane/HoistingCrane/IDrawingObject.cs b/HoistingCrane/HoistingCrane/IDrawingObject.cs index 9c0c409..39d52b6 100644 --- a/HoistingCrane/HoistingCrane/IDrawingObject.cs +++ b/HoistingCrane/HoistingCrane/IDrawingObject.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace HoistingCrane { - internal interface IDrawingObject + internal interface IDrawingObject : IEquatable { public float Step { get; } void SetObject(int x, int y, int width, int height); diff --git a/HoistingCrane/HoistingCrane/MapWithSetHoistingCraneGeneric.cs b/HoistingCrane/HoistingCrane/MapWithSetHoistingCraneGeneric.cs index d288a7c..110be21 100644 --- a/HoistingCrane/HoistingCrane/MapWithSetHoistingCraneGeneric.cs +++ b/HoistingCrane/HoistingCrane/MapWithSetHoistingCraneGeneric.cs @@ -7,7 +7,7 @@ using System.Threading.Tasks; namespace HoistingCrane { internal class MapWithSetHoistingCraneGeneric - where T : class, IDrawingObject + where T : class, IDrawingObject, IEquatable where U : AbstractMap { private readonly int _pictureWidth; @@ -140,5 +140,13 @@ namespace HoistingCrane _setHoistingCranes.Insert(DrawingObjectHoistingCrane.Create(rec) as T); } } + /// + /// Сортировка + /// + /// + public void Sort(IComparer comparer) + { + _setHoistingCranes.SortSet(comparer); + } } } diff --git a/HoistingCrane/HoistingCrane/SetHoistingCraneGeneric.cs b/HoistingCrane/HoistingCrane/SetHoistingCraneGeneric.cs index 710d61a..54dffbe 100644 --- a/HoistingCrane/HoistingCrane/SetHoistingCraneGeneric.cs +++ b/HoistingCrane/HoistingCrane/SetHoistingCraneGeneric.cs @@ -7,7 +7,7 @@ using System.Threading.Tasks; namespace HoistingCrane { internal class SetHoistingCraneGeneric - where T : class + where T : class, IEquatable { private readonly List _places; public int Count => _places.Count; @@ -19,6 +19,7 @@ namespace HoistingCrane } public int Insert(T hoistingCrane) { + if (_places.Contains(hoistingCrane)) return -1; for (int i = 0; i < _maxCount; i++) { if (i == Count) @@ -95,5 +96,13 @@ namespace HoistingCrane } } } + public void SortSet(IComparer comparer) + { + if (comparer == null) + { + return; + } + _places.Sort(comparer); + } } } -- 2.25.1