From a0f252b9e97e7e2b489c797f9a9bf4f80cf42879 Mon Sep 17 00:00:00 2001 From: Efi Date: Fri, 21 Apr 2023 01:24:52 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9B=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D0=BD=D0=B0=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=B0=208?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Monorail/Monorail/DrawingObjectLocomotive.cs | 30 +++++++++ .../FormMapWithSetLocomotive.Designer.cs | 58 ++++++++++++----- Monorail/Monorail/FormMapWithSetLocomotive.cs | 51 +++++++-------- Monorail/Monorail/IDrawingObject.cs | 2 +- Monorail/Monorail/LocomotiveCompareByColor.cs | 62 +++++++++++++++++++ Monorail/Monorail/LocomotiveCompareByType.cs | 56 +++++++++++++++++ .../Monorail/MapWithSetLocomotiveGeneric.cs | 13 ++-- Monorail/Monorail/SetLocomotiveGeneric.cs | 36 +++++++---- 8 files changed, 249 insertions(+), 59 deletions(-) create mode 100644 Monorail/Monorail/LocomotiveCompareByColor.cs create mode 100644 Monorail/Monorail/LocomotiveCompareByType.cs diff --git a/Monorail/Monorail/DrawingObjectLocomotive.cs b/Monorail/Monorail/DrawingObjectLocomotive.cs index 8f82693..c792c39 100644 --- a/Monorail/Monorail/DrawingObjectLocomotive.cs +++ b/Monorail/Monorail/DrawingObjectLocomotive.cs @@ -10,6 +10,8 @@ namespace Monorail { private DrawingLocomotive _locomotive = null; + public DrawingLocomotive GetLocomotive => _locomotive; + public DrawingObjectLocomotive(DrawingLocomotive locomotive) { _locomotive = locomotive; @@ -36,6 +38,34 @@ namespace Monorail { _locomotive.DrawTransport(g); } + + + public bool Equals(IDrawingObject? other) + { + if (other is not DrawingObjectLocomotive otherLocomotive) + { + return false; + } + var entity = _locomotive.Locomotive; + var otherEntity = otherLocomotive._locomotive.Locomotive; + if (entity.GetType() != otherEntity.GetType() || + entity.Speed != otherEntity.Speed || + entity.Weight != otherEntity.Weight || + entity.BodyColor != otherEntity.BodyColor) + { + return false; + } + if (entity is EntityMonorailLocomotive entityMonorailLocomotive && + otherEntity is EntityMonorailLocomotive otherEntityMonorailLocomotive && ( + entityMonorailLocomotive.Monorail != otherEntityMonorailLocomotive.Monorail || + entityMonorailLocomotive.DopColor != otherEntityMonorailLocomotive.DopColor || + entityMonorailLocomotive.DopCabin != otherEntityMonorailLocomotive.DopCabin)) + { + return false; + } + return true; + } + public string GetInfo() => _locomotive?.GetDataForSave(); public static IDrawingObject Create(string data) => new DrawingObjectLocomotive(data.CreateDrawingLocomotive()); diff --git a/Monorail/Monorail/FormMapWithSetLocomotive.Designer.cs b/Monorail/Monorail/FormMapWithSetLocomotive.Designer.cs index 57ab501..5db2f9e 100644 --- a/Monorail/Monorail/FormMapWithSetLocomotive.Designer.cs +++ b/Monorail/Monorail/FormMapWithSetLocomotive.Designer.cs @@ -40,6 +40,8 @@ buttonDown = new Button(); buttonRight = new Button(); groupBoxTools = new GroupBox(); + buttonSortByColor = new Button(); + buttonSortByType = new Button(); groupBoxMaps = new GroupBox(); textBoxNewMapName = new TextBox(); buttonDeleteMap = new Button(); @@ -62,7 +64,7 @@ pictureBoxLocomotive.Dock = DockStyle.Left; pictureBoxLocomotive.Location = new Point(0, 24); pictureBoxLocomotive.Name = "pictureBoxLocomotive"; - pictureBoxLocomotive.Size = new Size(579, 537); + pictureBoxLocomotive.Size = new Size(579, 623); pictureBoxLocomotive.TabIndex = 0; pictureBoxLocomotive.TabStop = false; // @@ -78,7 +80,7 @@ // // buttonAddCar // - buttonAddCar.Location = new Point(23, 288); + buttonAddCar.Location = new Point(23, 358); buttonAddCar.Name = "buttonAddCar"; buttonAddCar.Size = new Size(169, 30); buttonAddCar.TabIndex = 3; @@ -88,7 +90,7 @@ // // buttonRemoveCar // - buttonRemoveCar.Location = new Point(23, 353); + buttonRemoveCar.Location = new Point(23, 421); buttonRemoveCar.Name = "buttonRemoveCar"; buttonRemoveCar.Size = new Size(169, 30); buttonRemoveCar.TabIndex = 4; @@ -98,7 +100,7 @@ // // buttonShowStorage // - buttonShowStorage.Location = new Point(25, 406); + buttonShowStorage.Location = new Point(25, 470); buttonShowStorage.Name = "buttonShowStorage"; buttonShowStorage.Size = new Size(169, 30); buttonShowStorage.TabIndex = 5; @@ -108,7 +110,7 @@ // // buttonShowOnMap // - buttonShowOnMap.Location = new Point(25, 443); + buttonShowOnMap.Location = new Point(25, 505); buttonShowOnMap.Name = "buttonShowOnMap"; buttonShowOnMap.Size = new Size(169, 30); buttonShowOnMap.TabIndex = 6; @@ -118,7 +120,7 @@ // // maskedTextBoxPosition // - maskedTextBoxPosition.Location = new Point(24, 324); + maskedTextBoxPosition.Location = new Point(24, 393); maskedTextBoxPosition.Name = "maskedTextBoxPosition"; maskedTextBoxPosition.Size = new Size(169, 23); maskedTextBoxPosition.TabIndex = 7; @@ -127,7 +129,7 @@ // buttonUp.BackgroundImage = Properties.Resources.arrowUp; buttonUp.BackgroundImageLayout = ImageLayout.Zoom; - buttonUp.Location = new Point(96, 483); + buttonUp.Location = new Point(96, 547); buttonUp.Name = "buttonUp"; buttonUp.Size = new Size(30, 30); buttonUp.TabIndex = 8; @@ -138,7 +140,7 @@ // buttonLeft.BackgroundImage = Properties.Resources.arrowLeft; buttonLeft.BackgroundImageLayout = ImageLayout.Zoom; - buttonLeft.Location = new Point(60, 519); + buttonLeft.Location = new Point(60, 583); buttonLeft.Name = "buttonLeft"; buttonLeft.Size = new Size(30, 30); buttonLeft.TabIndex = 9; @@ -149,7 +151,7 @@ // buttonDown.BackgroundImage = Properties.Resources.arrowDown; buttonDown.BackgroundImageLayout = ImageLayout.Zoom; - buttonDown.Location = new Point(96, 519); + buttonDown.Location = new Point(96, 583); buttonDown.Name = "buttonDown"; buttonDown.Size = new Size(30, 30); buttonDown.TabIndex = 10; @@ -160,7 +162,7 @@ // buttonRight.BackgroundImage = Properties.Resources.arrowRight; buttonRight.BackgroundImageLayout = ImageLayout.Zoom; - buttonRight.Location = new Point(132, 519); + buttonRight.Location = new Point(132, 583); buttonRight.Name = "buttonRight"; buttonRight.Size = new Size(30, 30); buttonRight.TabIndex = 11; @@ -170,6 +172,8 @@ // groupBoxTools // groupBoxTools.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; + groupBoxTools.Controls.Add(buttonSortByColor); + groupBoxTools.Controls.Add(buttonSortByType); groupBoxTools.Controls.Add(groupBoxMaps); groupBoxTools.Controls.Add(buttonRight); groupBoxTools.Controls.Add(buttonDown); @@ -180,13 +184,33 @@ groupBoxTools.Controls.Add(buttonShowStorage); groupBoxTools.Controls.Add(buttonRemoveCar); groupBoxTools.Controls.Add(buttonAddCar); - groupBoxTools.Location = new Point(585, 0); + groupBoxTools.Location = new Point(585, 24); groupBoxTools.Name = "groupBoxTools"; - groupBoxTools.Size = new Size(212, 560); + groupBoxTools.Size = new Size(212, 623); groupBoxTools.TabIndex = 12; groupBoxTools.TabStop = false; groupBoxTools.Text = "Инструменты"; // + // buttonSortByColor + // + buttonSortByColor.Location = new Point(25, 311); + buttonSortByColor.Name = "buttonSortByColor"; + buttonSortByColor.Size = new Size(169, 30); + buttonSortByColor.TabIndex = 18; + buttonSortByColor.Text = "Сортировать по цвету"; + buttonSortByColor.UseVisualStyleBackColor = true; + buttonSortByColor.Click += ButtonSortByColor_Click; + // + // buttonSortByType + // + buttonSortByType.Location = new Point(24, 277); + buttonSortByType.Name = "buttonSortByType"; + buttonSortByType.Size = new Size(169, 30); + buttonSortByType.TabIndex = 17; + buttonSortByType.Text = "Сортировать по типу"; + buttonSortByType.UseVisualStyleBackColor = true; + buttonSortByType.Click += ButtonSortByType_Click; + // // groupBoxMaps // groupBoxMaps.Controls.Add(textBoxNewMapName); @@ -194,7 +218,7 @@ groupBoxMaps.Controls.Add(buttonAddMap); groupBoxMaps.Controls.Add(listBoxMaps); groupBoxMaps.Controls.Add(comboBoxSelectorMap); - groupBoxMaps.Location = new Point(8, 21); + groupBoxMaps.Location = new Point(9, 22); groupBoxMaps.Name = "groupBoxMaps"; groupBoxMaps.Size = new Size(197, 249); groupBoxMaps.TabIndex = 16; @@ -257,14 +281,14 @@ // SaveToolStripMenuItem // SaveToolStripMenuItem.Name = "SaveToolStripMenuItem"; - SaveToolStripMenuItem.Size = new Size(180, 22); + SaveToolStripMenuItem.Size = new Size(141, 22); SaveToolStripMenuItem.Text = "Сохранение"; SaveToolStripMenuItem.Click += SaveToolStripMenuItem_Click; // // LoadToolStripMenuItem // LoadToolStripMenuItem.Name = "LoadToolStripMenuItem"; - LoadToolStripMenuItem.Size = new Size(180, 22); + LoadToolStripMenuItem.Size = new Size(141, 22); LoadToolStripMenuItem.Text = "Загрузка"; LoadToolStripMenuItem.Click += LoadToolStripMenuItem_Click; // @@ -280,7 +304,7 @@ // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(800, 561); + ClientSize = new Size(800, 647); Controls.Add(groupBoxTools); Controls.Add(pictureBoxLocomotive); Controls.Add(menuStrip); @@ -323,5 +347,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/Monorail/Monorail/FormMapWithSetLocomotive.cs b/Monorail/Monorail/FormMapWithSetLocomotive.cs index 494133b..7f0deb7 100644 --- a/Monorail/Monorail/FormMapWithSetLocomotive.cs +++ b/Monorail/Monorail/FormMapWithSetLocomotive.cs @@ -14,7 +14,7 @@ namespace Monorail { public partial class FormMapWithSetLocomotive : Form { - private MapWithSetLocomotiveGeneric _mapLocomotiveCollectionGeneric; + private readonly Dictionary _mapDict = new() { {"Простая карта", new SimpleMap() }, @@ -53,32 +53,6 @@ namespace Monorail 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 FieldMap(); - break; - case "Карта с кустами": - map = new BushesMap(); - break; - } - if (map != null) - { - _mapLocomotiveCollectionGeneric = new MapWithSetLocomotiveGeneric( - pictureBoxLocomotive.Width, pictureBoxLocomotive.Height, map); - } - else - { - _mapLocomotiveCollectionGeneric = null; - } - } private void ButtonAddLocomotive_Click(object sender, EventArgs e) { var FormLocmotiveConfig = new FormLocomotiveConfig(); @@ -272,5 +246,28 @@ namespace Monorail } } } + + private void ButtonSortByType_Click(object sender, EventArgs e) + { + if (listBoxMaps.SelectedIndex == -1) + { + return; + } + _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? + string.Empty].Sort(new LocomotiveCompareByType()); + pictureBoxLocomotive.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 LocomotiveCompareByColor()); + pictureBoxLocomotive.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); + } + } } diff --git a/Monorail/Monorail/IDrawingObject.cs b/Monorail/Monorail/IDrawingObject.cs index a2c1fb2..6acb767 100644 --- a/Monorail/Monorail/IDrawingObject.cs +++ b/Monorail/Monorail/IDrawingObject.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace Monorail { - internal interface IDrawingObject + internal interface IDrawingObject : IEquatable { /// /// Шаг перемещения объекта diff --git a/Monorail/Monorail/LocomotiveCompareByColor.cs b/Monorail/Monorail/LocomotiveCompareByColor.cs new file mode 100644 index 0000000..76e3483 --- /dev/null +++ b/Monorail/Monorail/LocomotiveCompareByColor.cs @@ -0,0 +1,62 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Monorail +{ + internal class LocomotiveCompareByColor : 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 xLocomotive = x as DrawingObjectLocomotive; + var yLocomotive = y as DrawingObjectLocomotive; + if (xLocomotive == null && yLocomotive == null) + { + return 0; + } + if (xLocomotive == null && yLocomotive != null) + { + return 1; + } + if (xLocomotive != null && yLocomotive == null) + { + return -1; + } + var xEntityLocomotive = xLocomotive.GetLocomotive.Locomotive; + var yEntityLocomotive = yLocomotive.GetLocomotive.Locomotive; + var baseColorCompare = xEntityLocomotive.BodyColor.ToArgb().CompareTo(yEntityLocomotive.BodyColor.ToArgb()); + if (baseColorCompare != 0) + { + return baseColorCompare; + } + if (xEntityLocomotive is EntityMonorailLocomotive xMonorailLocomotive && yEntityLocomotive is EntityMonorailLocomotive yMonorailLocomotive) + { + var dopColorCompare = xMonorailLocomotive.DopColor.ToArgb().CompareTo(yMonorailLocomotive.DopColor.ToArgb()); + if (dopColorCompare != 0) + { + return dopColorCompare; + } + } + var speedCompare = xLocomotive.GetLocomotive.Locomotive.Speed.CompareTo(yLocomotive.GetLocomotive.Locomotive.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return xLocomotive.GetLocomotive.Locomotive.Weight.CompareTo(yLocomotive.GetLocomotive.Locomotive.Weight); + } + } +} diff --git a/Monorail/Monorail/LocomotiveCompareByType.cs b/Monorail/Monorail/LocomotiveCompareByType.cs new file mode 100644 index 0000000..ee170f3 --- /dev/null +++ b/Monorail/Monorail/LocomotiveCompareByType.cs @@ -0,0 +1,56 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Monorail +{ + internal class LocomotiveCompareByType : 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 xLocomotive = x as DrawingObjectLocomotive; + var yLocomotive = y as DrawingObjectLocomotive; + if (xLocomotive == null && yLocomotive == null) + { + return 0; + } + if (xLocomotive == null && yLocomotive != null) + { + return 1; + } + if (xLocomotive != null && yLocomotive == null) + { + return -1; + } + if (xLocomotive.GetLocomotive.GetType().Name != yLocomotive.GetLocomotive.GetType().Name) + { + if (xLocomotive.GetLocomotive.GetType().Name == "DrawingLocomotive") + { + return -1; + } + return 1; + } + var speedCompare = + xLocomotive.GetLocomotive.Locomotive.Speed.CompareTo(yLocomotive.GetLocomotive.Locomotive.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return xLocomotive.GetLocomotive.Locomotive.Weight.CompareTo(yLocomotive.GetLocomotive.Locomotive.Weight); + } + } +} diff --git a/Monorail/Monorail/MapWithSetLocomotiveGeneric.cs b/Monorail/Monorail/MapWithSetLocomotiveGeneric.cs index d09ff7a..0e22186 100644 --- a/Monorail/Monorail/MapWithSetLocomotiveGeneric.cs +++ b/Monorail/Monorail/MapWithSetLocomotiveGeneric.cs @@ -7,7 +7,7 @@ using System.Threading.Tasks; namespace Monorail { internal class MapWithSetLocomotiveGeneric - where T : class, IDrawingObject + where T : class, IDrawingObject, IEquatable where U : AbstractMap { // Ширина окна отрисовки @@ -55,7 +55,7 @@ namespace Monorail public Bitmap ShowOnMap() { Shaking(); - foreach (var locomotive in _setLocomotive.GetLocomotives()) + foreach (var locomotive in _setLocomotive.GetLocomotive()) { return _map.CreateMap(_pictureWidth, _pictureHeight, locomotive); } @@ -118,7 +118,7 @@ namespace Monorail int xPosition = _pictureWidth - _placeSizeWidth; int yPosition = 12; - foreach (var locomotive in _setLocomotive.GetLocomotives()) + foreach (var locomotive in _setLocomotive.GetLocomotive()) { locomotive.SetObject(xPosition, yPosition, _pictureWidth, _pictureHeight); locomotive.DrawingObject(g); @@ -135,7 +135,7 @@ namespace Monorail public string GetData(char separatorType, char separatorData) { string data = $"{_map.GetType().Name}{separatorType}"; - foreach (var boat in _setLocomotive.GetLocomotives()) + foreach (var boat in _setLocomotive.GetLocomotive()) { data += $"{boat.GetInfo()}{separatorData}"; } @@ -148,5 +148,10 @@ namespace Monorail _setLocomotive.Insert(DrawingObjectLocomotive.Create(rec) as T); } } + + public void Sort(IComparer comparer) + { + _setLocomotive.SortSet(comparer); + } } } diff --git a/Monorail/Monorail/SetLocomotiveGeneric.cs b/Monorail/Monorail/SetLocomotiveGeneric.cs index c2c7146..1140128 100644 --- a/Monorail/Monorail/SetLocomotiveGeneric.cs +++ b/Monorail/Monorail/SetLocomotiveGeneric.cs @@ -7,29 +7,30 @@ using System.Threading.Tasks; namespace Monorail { internal class SetLocomotiveGeneric - where T : class + where T : class, IEquatable { private readonly List _places; public int Count => _places.Count; private readonly int _maxCount; - public SetLocomotiveGeneric(int count) { _maxCount = count; _places = new List(); } - public int Insert(T locomotive) { return Insert(locomotive, 0); } public int Insert(T locomotive, int position) { - if (Count == _maxCount) + if (_places.Contains(locomotive)) + { + throw new ArgumentException($"Объект {locomotive} уже присутствует в наборе"); + } + if (position < 0 || position > Count || _maxCount == Count) { throw new StorageOverflowException(_maxCount); } - if (position < 0 || position > _maxCount) return -1; _places.Insert(position, locomotive); return position; } @@ -43,23 +44,27 @@ namespace Monorail _places.RemoveAt(position); return ship; } - - public T this[int position] { get { - if (position < _places.Count && position >= 0) - return _places[position]; - else + if (position >= Count || position < 0) + { return null; + } + return _places[position]; } set { + if (position >= Count || position < 0) + { + return; + } Insert(value, position); + } } - public IEnumerable GetLocomotives() + public IEnumerable GetLocomotive() { foreach (var locomotive in _places) { @@ -73,5 +78,14 @@ namespace Monorail } } } + public void SortSet(IComparer comparer) + { + if (comparer == null) + { + return; + } + _places.Sort(comparer); + } + } }