diff --git a/ProjectMachine/ProjectMachine/DrawningObject.cs b/ProjectMachine/ProjectMachine/DrawningObject.cs index 0e7e774..f93f305 100644 --- a/ProjectMachine/ProjectMachine/DrawningObject.cs +++ b/ProjectMachine/ProjectMachine/DrawningObject.cs @@ -8,7 +8,7 @@ namespace ProjectMachine { internal class DrawningObject : IDrawningObject { - private DrawningMachine _machine = null; + public DrawningMachine _machine = null; public DrawningObject(DrawningMachine machine) { @@ -17,6 +17,8 @@ namespace ProjectMachine public float Step => _machine?.Machine?.Step ?? 0; + public DrawningMachine GetTank => _machine; + public (float Left, float Right, float Top, float Bottom) GetCurrentPosition() { return _machine?.GetCurrentPosition() ?? default; diff --git a/ProjectMachine/ProjectMachine/FormMapWithSetTank.Designer.cs b/ProjectMachine/ProjectMachine/FormMapWithSetTank.Designer.cs index c4d9479..2024d15 100644 --- a/ProjectMachine/ProjectMachine/FormMapWithSetTank.Designer.cs +++ b/ProjectMachine/ProjectMachine/FormMapWithSetTank.Designer.cs @@ -51,6 +51,8 @@ this.LoadToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.openFileDialog = new System.Windows.Forms.OpenFileDialog(); this.saveFileDialog = new System.Windows.Forms.SaveFileDialog(); + this.buttonSortByType = new System.Windows.Forms.Button(); + this.buttonSortByColor = new System.Windows.Forms.Button(); this.groupBoxTools.SuspendLayout(); this.groupBoxMaps.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit(); @@ -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.buttonLeft); this.groupBoxTools.Controls.Add(this.buttonUp); @@ -70,9 +74,9 @@ this.groupBoxTools.Controls.Add(this.maskedTextBoxPosition); this.groupBoxTools.Controls.Add(this.buttonAddTank); this.groupBoxTools.Dock = System.Windows.Forms.DockStyle.Right; - this.groupBoxTools.Location = new System.Drawing.Point(799, 28); + this.groupBoxTools.Location = new System.Drawing.Point(812, 28); this.groupBoxTools.Name = "groupBoxTools"; - this.groupBoxTools.Size = new System.Drawing.Size(233, 758); + this.groupBoxTools.Size = new System.Drawing.Size(233, 843); this.groupBoxTools.TabIndex = 0; this.groupBoxTools.TabStop = false; this.groupBoxTools.Text = "Инструменты"; @@ -150,7 +154,7 @@ this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonLeft.BackgroundImage = global::ProjectMachine.Properties.Resources.лево; this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonLeft.Location = new System.Drawing.Point(69, 703); + this.buttonLeft.Location = new System.Drawing.Point(69, 788); this.buttonLeft.Name = "buttonLeft"; this.buttonLeft.Size = new System.Drawing.Size(34, 30); this.buttonLeft.TabIndex = 13; @@ -163,7 +167,7 @@ this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonUp.BackgroundImage = global::ProjectMachine.Properties.Resources.вверх; this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonUp.Location = new System.Drawing.Point(105, 667); + this.buttonUp.Location = new System.Drawing.Point(105, 752); this.buttonUp.Name = "buttonUp"; this.buttonUp.Size = new System.Drawing.Size(34, 30); this.buttonUp.TabIndex = 12; @@ -176,7 +180,7 @@ this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonRight.BackgroundImage = global::ProjectMachine.Properties.Resources.право; this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonRight.Location = new System.Drawing.Point(141, 703); + this.buttonRight.Location = new System.Drawing.Point(141, 788); this.buttonRight.Name = "buttonRight"; this.buttonRight.Size = new System.Drawing.Size(34, 30); this.buttonRight.TabIndex = 11; @@ -189,7 +193,7 @@ this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonDown.BackgroundImage = global::ProjectMachine.Properties.Resources.вниз; this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonDown.Location = new System.Drawing.Point(105, 703); + this.buttonDown.Location = new System.Drawing.Point(105, 788); this.buttonDown.Name = "buttonDown"; this.buttonDown.Size = new System.Drawing.Size(34, 30); this.buttonDown.TabIndex = 10; @@ -199,7 +203,7 @@ // // buttonShowOnMap // - this.buttonShowOnMap.Location = new System.Drawing.Point(21, 611); + this.buttonShowOnMap.Location = new System.Drawing.Point(20, 689); this.buttonShowOnMap.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.buttonShowOnMap.Name = "buttonShowOnMap"; this.buttonShowOnMap.Size = new System.Drawing.Size(200, 47); @@ -210,7 +214,7 @@ // // buttonShowStorage // - this.buttonShowStorage.Location = new System.Drawing.Point(21, 556); + this.buttonShowStorage.Location = new System.Drawing.Point(20, 634); this.buttonShowStorage.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.buttonShowStorage.Name = "buttonShowStorage"; this.buttonShowStorage.Size = new System.Drawing.Size(200, 47); @@ -221,7 +225,7 @@ // // buttonRemoveTank // - this.buttonRemoveTank.Location = new System.Drawing.Point(21, 501); + this.buttonRemoveTank.Location = new System.Drawing.Point(20, 579); this.buttonRemoveTank.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.buttonRemoveTank.Name = "buttonRemoveTank"; this.buttonRemoveTank.Size = new System.Drawing.Size(200, 47); @@ -232,7 +236,7 @@ // // maskedTextBoxPosition // - this.maskedTextBoxPosition.Location = new System.Drawing.Point(21, 466); + this.maskedTextBoxPosition.Location = new System.Drawing.Point(22, 544); this.maskedTextBoxPosition.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.maskedTextBoxPosition.Mask = "00"; this.maskedTextBoxPosition.Name = "maskedTextBoxPosition"; @@ -242,7 +246,7 @@ // // buttonAddTank // - this.buttonAddTank.Location = new System.Drawing.Point(21, 411); + this.buttonAddTank.Location = new System.Drawing.Point(20, 489); this.buttonAddTank.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.buttonAddTank.Name = "buttonAddTank"; this.buttonAddTank.Size = new System.Drawing.Size(200, 47); @@ -256,7 +260,7 @@ this.pictureBox.Dock = System.Windows.Forms.DockStyle.Fill; this.pictureBox.Location = new System.Drawing.Point(0, 28); this.pictureBox.Name = "pictureBox"; - this.pictureBox.Size = new System.Drawing.Size(799, 758); + this.pictureBox.Size = new System.Drawing.Size(812, 843); this.pictureBox.TabIndex = 1; this.pictureBox.TabStop = false; // @@ -267,7 +271,7 @@ this.файлToolStripMenuItem}); this.menuStrip.Location = new System.Drawing.Point(0, 0); this.menuStrip.Name = "menuStrip"; - this.menuStrip.Size = new System.Drawing.Size(1032, 28); + this.menuStrip.Size = new System.Drawing.Size(1045, 28); this.menuStrip.TabIndex = 2; this.menuStrip.Text = "menuStrip1"; // @@ -302,11 +306,33 @@ // this.saveFileDialog.Filter = "txt file | *.txt"; // + // buttonSortByType + // + this.buttonSortByType.Location = new System.Drawing.Point(20, 366); + this.buttonSortByType.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); + this.buttonSortByType.Name = "buttonSortByType"; + this.buttonSortByType.Size = new System.Drawing.Size(200, 47); + this.buttonSortByType.TabIndex = 15; + this.buttonSortByType.Text = "Сортировать по типу"; + this.buttonSortByType.UseVisualStyleBackColor = true; + this.buttonSortByType.Click += new System.EventHandler(this.buttonSortByType_Click); + // + // buttonSortByColor + // + this.buttonSortByColor.Location = new System.Drawing.Point(22, 421); + this.buttonSortByColor.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); + this.buttonSortByColor.Name = "buttonSortByColor"; + this.buttonSortByColor.Size = new System.Drawing.Size(200, 47); + this.buttonSortByColor.TabIndex = 16; + this.buttonSortByColor.Text = "Сортировать по цвету"; + this.buttonSortByColor.UseVisualStyleBackColor = true; + this.buttonSortByColor.Click += new System.EventHandler(this.buttonSortByColor_Click); + // // FormMapWithSetTank // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(1032, 786); + this.ClientSize = new System.Drawing.Size(1045, 871); this.Controls.Add(this.pictureBox); this.Controls.Add(this.groupBoxTools); this.Controls.Add(this.menuStrip); @@ -350,5 +376,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/ProjectMachine/ProjectMachine/FormMapWithSetTank.cs b/ProjectMachine/ProjectMachine/FormMapWithSetTank.cs index d668f69..f95e34b 100644 --- a/ProjectMachine/ProjectMachine/FormMapWithSetTank.cs +++ b/ProjectMachine/ProjectMachine/FormMapWithSetTank.cs @@ -294,6 +294,34 @@ namespace ProjectMachine } } } + /// + /// Сортировка по типу + /// + /// + /// + private void buttonSortByType_Click(object sender, EventArgs e) + { + if (listBoxMaps.SelectedIndex == -1) + { + return; + } + _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].Sort(new TankCompareByType()); + 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 TankCompareByColor()); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); + } } } diff --git a/ProjectMachine/ProjectMachine/MapWithSetTankGeneric.cs b/ProjectMachine/ProjectMachine/MapWithSetTankGeneric.cs index 38c6253..86e76d9 100644 --- a/ProjectMachine/ProjectMachine/MapWithSetTankGeneric.cs +++ b/ProjectMachine/ProjectMachine/MapWithSetTankGeneric.cs @@ -139,6 +139,14 @@ namespace ProjectMachine } } /// + /// Сортировка + /// + /// + public void Sort(IComparer comparer) + { + _setTank.SortSet(comparer); + } + /// /// "Взбалтываем" набор, чтобы все элементы оказались в начале /// private void Shaking() diff --git a/ProjectMachine/ProjectMachine/SetTankGeneric.cs b/ProjectMachine/ProjectMachine/SetTankGeneric.cs index 9d3a8d7..c8268cb 100644 --- a/ProjectMachine/ProjectMachine/SetTankGeneric.cs +++ b/ProjectMachine/ProjectMachine/SetTankGeneric.cs @@ -51,7 +51,7 @@ namespace ProjectMachine public int Insert(T tank, int position) { if (_places.Contains(tank)) - throw new ArgumentException($"Объект {tank} уже есть"); + throw new ArgumentException($"Такой объект уже есть"); if (Count >= _maxCount) throw new StorageOverflowException(Count); if (position < 0 || position >= _maxCount) @@ -112,6 +112,18 @@ namespace ProjectMachine } } } + /// + /// Сортировка набора объектов + /// + /// + public void SortSet(IComparer comparer) + { + if (comparer == null) + { + return; + } + _places.Sort(comparer); + } } } diff --git a/ProjectMachine/ProjectMachine/TankCompareByColor.cs b/ProjectMachine/ProjectMachine/TankCompareByColor.cs new file mode 100644 index 0000000..17580a0 --- /dev/null +++ b/ProjectMachine/ProjectMachine/TankCompareByColor.cs @@ -0,0 +1,45 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectMachine +{ + internal class TankCompareByColor : 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 xTank = x as DrawningObject; + var yTank = y as DrawningObject; + if (xTank == null && yTank == null) + { + return 0; + } + if (xTank == null && yTank != null) + { + return 1; + } + if (xTank != null && yTank == null) + { + return -1; + } + var xColorName = xTank._machine.Machine.BodyColor.Name; + var yColorName = yTank._machine.Machine.BodyColor.Name; + return xColorName.CompareTo(yColorName); + } + } +} diff --git a/ProjectMachine/ProjectMachine/TankCompareByType.cs b/ProjectMachine/ProjectMachine/TankCompareByType.cs new file mode 100644 index 0000000..aaeeebd --- /dev/null +++ b/ProjectMachine/ProjectMachine/TankCompareByType.cs @@ -0,0 +1,57 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectMachine +{ + internal class TankCompareByType : 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 xTank = x as DrawningObject; + var yTank = y as DrawningObject; + if (xTank == null && yTank == null) + { + return 0; + } + if (xTank == null && yTank != null) + { + return 1; + } + if (xTank != null && yTank == null) + { + return -1; + } + if (xTank.GetTank.GetType().Name != yTank.GetTank.GetType().Name) + { + if (xTank.GetTank.GetType().Name == "DrawningMachine") + { + return -1; + } + return 1; + } + var speedCompare = + xTank.GetTank.Machine.Speed.CompareTo(yTank.GetTank.Machine.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return xTank.GetTank.Machine.Weight.CompareTo(yTank.GetTank.Machine.Weight); + } + } +}