This commit is contained in:
zum 2024-05-07 16:02:49 +04:00
parent 96cd694efd
commit 21ab932b58
9 changed files with 258 additions and 29 deletions

View File

@ -64,7 +64,7 @@ public abstract class AbstractCompany
/// <returns></returns> /// <returns></returns>
public static int operator +(AbstractCompany company, DrawningWarPlane plane) public static int operator +(AbstractCompany company, DrawningWarPlane plane)
{ {
if (company._collection.Insert(plane)) if (company._collection.Insert(plane, new DrawiningWarPlaneEqutables()))
{ {
return 1; return 1;
} }
@ -120,7 +120,11 @@ public abstract class AbstractCompany
return bitmap; return bitmap;
} }
/// <summary>
/// Сортировка
/// </summary>
/// <param name="comparer">Сравнитель объектов</param>
public void Sort(IComparer<DrawningWarPlane?> comparer) => _collection?.CollectionSort(comparer);
/// <summary> /// <summary>
/// Вывод заднего фона /// Вывод заднего фона
/// </summary> /// </summary>

View File

@ -23,7 +23,7 @@ public interface ICollectionGenericObjects<T>
/// </summary> /// </summary>
/// <param name="obj">Добавляемый объект</param> /// <param name="obj">Добавляемый объект</param>
/// <returns>true - вставка прошла удачно, false - вставка не удалась</returns> /// <returns>true - вставка прошла удачно, false - вставка не удалась</returns>
bool Insert(T obj); bool Insert(T obj, IEqualityComparer<T?>? comparer = null);
/// <summary> /// <summary>
/// Добавление объекта в коллекцию на конкретную позицию /// Добавление объекта в коллекцию на конкретную позицию
@ -31,7 +31,7 @@ public interface ICollectionGenericObjects<T>
/// <param name="obj">Добавляемый объект</param> /// <param name="obj">Добавляемый объект</param>
/// <param name="position">Позиция</param> /// <param name="position">Позиция</param>
/// <returns>true - вставка прошла удачно, false - вставка не удалась</returns> /// <returns>true - вставка прошла удачно, false - вставка не удалась</returns>
bool Insert(T obj, int position); bool Insert(T obj, int position, IEqualityComparer<T?>? comparer = null);
/// <summary> /// <summary>
/// Удаление объекта из коллекции с конкретной позиции /// Удаление объекта из коллекции с конкретной позиции
@ -57,4 +57,10 @@ public interface ICollectionGenericObjects<T>
/// </summary> /// </summary>
/// <returns>Поэлементый вывод элементов коллекции</returns> /// <returns>Поэлементый вывод элементов коллекции</returns>
IEnumerable<T?> GetItems(); IEnumerable<T?> GetItems();
/// <summary>
/// Сортировка коллекции
/// </summary>
/// <param name="comparer">Сравнитель объектов</param>
void CollectionSort(IComparer<T?> comparer);
} }

View File

@ -55,7 +55,7 @@ public class ListGenericObjects<T> : ICollectionGenericObjects<T>
return _collection[position]; return _collection[position];
} }
public bool Insert(T obj) public bool Insert(T obj, IEqualityComparer<T?>? comparer = null)
{ {
if (Count + 1 > MaxCount) if (Count + 1 > MaxCount)
{ {
@ -66,7 +66,7 @@ public class ListGenericObjects<T> : ICollectionGenericObjects<T>
return true; return true;
} }
public bool Insert(T obj, int position) public bool Insert(T obj, int position, IEqualityComparer<T?>? comparer = null)
{ {
if (_collection.Count + 1 < _maxCount) if (_collection.Count + 1 < _maxCount)
{ {
@ -98,4 +98,9 @@ public class ListGenericObjects<T> : ICollectionGenericObjects<T>
yield return _collection[i]; yield return _collection[i];
} }
} }
public void CollectionSort(IComparer<T?> comparer)
{
_collection.Sort(comparer);
}
} }

View File

@ -62,7 +62,7 @@ public class MassiveGenericObjects<T> : ICollectionGenericObjects<T>
return _collection[position]; return _collection[position];
} }
public bool Insert(T obj) public bool Insert(T obj, IEqualityComparer<T?>? comparer = null)
{ {
int index = Array.IndexOf(_collection, null); int index = Array.IndexOf(_collection, null);
@ -75,7 +75,7 @@ public class MassiveGenericObjects<T> : ICollectionGenericObjects<T>
} }
public bool Insert(T obj, int position) public bool Insert(T obj, int position, IEqualityComparer<T?>? comparer = null)
{ {
if(position < 0 || position > _collection.Length -1) if(position < 0 || position > _collection.Length -1)
@ -125,4 +125,14 @@ public class MassiveGenericObjects<T> : ICollectionGenericObjects<T>
yield return _collection[i]; yield return _collection[i];
} }
} }
public void CollectionSort(IComparer<T?> comparer)
{
if(_collection?.Length > 0)
{
Array.Sort(_collection, comparer);
//_collection = _collection.OrderBy(warPlane => warPlane == null).ToArray();
}
}
} }

View File

@ -0,0 +1,71 @@
using ProjectStormTrooper.Entities;
using System.Diagnostics.CodeAnalysis;
namespace ProjectStormTrooper.Drawnings;
/// <summary>
/// Реализация сравнения двух объектов класса-прорисовки
/// </summary>
public class DrawiningWarPlaneEqutables : IEqualityComparer<DrawningWarPlane?>
{
public bool Equals(DrawningWarPlane? x, DrawningWarPlane? y)
{
if (x == null || x.EntityWarPlane == null)
{
return false;
}
if (y == null || y.EntityWarPlane == null)
{
return false;
}
if (x.GetType().Name != y.GetType().Name)
{
return false;
}
if (x.EntityWarPlane.Speed != y.EntityWarPlane.Speed)
{
return false;
}
if (x.EntityWarPlane.Weight != y.EntityWarPlane.Weight)
{
return false;
}
if (x.EntityWarPlane.BodyColor != y.EntityWarPlane.BodyColor)
{
return false;
}
if(x.EntityWarPlane.ColorOfHead != y.EntityWarPlane.ColorOfHead)
{
return false;
}
if (x is DrawningStormTrooper && y is DrawningStormTrooper)
{
EntityStormTrooper entityX = (EntityStormTrooper)x.EntityWarPlane;
EntityStormTrooper entityY = (EntityStormTrooper)y.EntityWarPlane;
if(entityX.Rockets != entityY.Rockets)
{
return false;
}
if(entityX.Bombs != entityY.Bombs)
{
return false;
}
if(entityX.AdditionalColor != entityY.AdditionalColor)
{
return false;
}
}
return true;
}
public int GetHashCode([DisallowNull] DrawningWarPlane obj)
{
return obj.GetHashCode();
}
}

View File

@ -0,0 +1,34 @@
namespace ProjectStormTrooper.Drawnings;
/// <summary>
/// Сравнение по цвету, скорости, весу
/// </summary>
public class DrawningWarPlaneCompareByColor : IComparer<DrawningWarPlane?>
{
public int Compare(DrawningWarPlane? x, DrawningWarPlane? y)
{
if (x == null && y == null)
{
return 0;
}
if (x == null || x.EntityWarPlane == null)
{
return -1;
}
if (y == null || y.EntityWarPlane == null)
{
return 1;
}
if(x.EntityWarPlane.BodyColor.Name != y.EntityWarPlane.BodyColor.Name)
{
return x.EntityWarPlane.BodyColor.Name.CompareTo(y.EntityWarPlane.BodyColor.Name);
}
var speedCompare = x.EntityWarPlane.Speed.CompareTo(y.EntityWarPlane.Speed);
if (speedCompare != 0)
{
return speedCompare;
}
return x.EntityWarPlane.Weight.CompareTo(y.EntityWarPlane.Weight);
}
}

View File

@ -0,0 +1,39 @@

namespace ProjectStormTrooper.Drawnings;
/// <summary>
/// Сравнение по типу, скорости, весу
/// </summary>
public class DrawningWarPlaneCompareByType : IComparer<DrawningWarPlane?>
{
public int Compare(DrawningWarPlane? x, DrawningWarPlane? y)
{
if (x == null && y == null)
{
return 0;
}
if (x == null || x.EntityWarPlane == null)
{
return -1;
}
if (y == null || y.EntityWarPlane == null)
{
return 1;
}
if (!x.GetType().Name.Equals(y.GetType().Name))
{
return x.GetType().Name.CompareTo(y.GetType().Name);
}
var speedCompare = x.EntityWarPlane.Speed.CompareTo(y.EntityWarPlane.Speed);
if (speedCompare != 0)
{
return speedCompare;
}
return x.EntityWarPlane.Weight.CompareTo(y.EntityWarPlane.Weight);
}
}

View File

@ -39,6 +39,8 @@
labelCollectionName = new Label(); labelCollectionName = new Label();
buttonCreateCompany = new Button(); buttonCreateCompany = new Button();
panelCompanyTools = new Panel(); panelCompanyTools = new Panel();
buttonSortByColor = new Button();
buttonSortByType = new Button();
buttonGoToCheck = new Button(); buttonGoToCheck = new Button();
buttonRefresh = new Button(); buttonRefresh = new Button();
buttonRemoveWarPlane = new Button(); buttonRemoveWarPlane = new Button();
@ -68,7 +70,7 @@
groupBox1.Dock = DockStyle.Right; groupBox1.Dock = DockStyle.Right;
groupBox1.Location = new Point(616, 28); groupBox1.Location = new Point(616, 28);
groupBox1.Name = "groupBox1"; groupBox1.Name = "groupBox1";
groupBox1.Size = new Size(197, 782); groupBox1.Size = new Size(197, 737);
groupBox1.TabIndex = 0; groupBox1.TabIndex = 0;
groupBox1.TabStop = false; groupBox1.TabStop = false;
groupBox1.Text = "Инструменты"; groupBox1.Text = "Инструменты";
@ -157,7 +159,7 @@
// //
// buttonCreateCompany // buttonCreateCompany
// //
buttonCreateCompany.Location = new Point(8, 441); buttonCreateCompany.Location = new Point(6, 399);
buttonCreateCompany.Name = "buttonCreateCompany"; buttonCreateCompany.Name = "buttonCreateCompany";
buttonCreateCompany.Size = new Size(172, 29); buttonCreateCompany.Size = new Size(172, 29);
buttonCreateCompany.TabIndex = 8; buttonCreateCompany.TabIndex = 8;
@ -167,6 +169,8 @@
// //
// panelCompanyTools // panelCompanyTools
// //
panelCompanyTools.Controls.Add(buttonSortByColor);
panelCompanyTools.Controls.Add(buttonSortByType);
panelCompanyTools.Controls.Add(buttonGoToCheck); panelCompanyTools.Controls.Add(buttonGoToCheck);
panelCompanyTools.Controls.Add(buttonRefresh); panelCompanyTools.Controls.Add(buttonRefresh);
panelCompanyTools.Controls.Add(buttonRemoveWarPlane); panelCompanyTools.Controls.Add(buttonRemoveWarPlane);
@ -174,14 +178,34 @@
panelCompanyTools.Controls.Add(buttonAddWarPlane); panelCompanyTools.Controls.Add(buttonAddWarPlane);
panelCompanyTools.Dock = DockStyle.Bottom; panelCompanyTools.Dock = DockStyle.Bottom;
panelCompanyTools.Enabled = false; panelCompanyTools.Enabled = false;
panelCompanyTools.Location = new Point(3, 521); panelCompanyTools.Location = new Point(3, 449);
panelCompanyTools.Name = "panelCompanyTools"; panelCompanyTools.Name = "panelCompanyTools";
panelCompanyTools.Size = new Size(191, 258); panelCompanyTools.Size = new Size(191, 285);
panelCompanyTools.TabIndex = 7; panelCompanyTools.TabIndex = 7;
// //
// buttonSortByColor
//
buttonSortByColor.Location = new Point(5, 249);
buttonSortByColor.Name = "buttonSortByColor";
buttonSortByColor.Size = new Size(172, 27);
buttonSortByColor.TabIndex = 8;
buttonSortByColor.Text = "Сортировка по цвету";
buttonSortByColor.UseVisualStyleBackColor = true;
buttonSortByColor.Click += ButtonSortByColor_Click;
//
// buttonSortByType
//
buttonSortByType.Location = new Point(5, 218);
buttonSortByType.Name = "buttonSortByType";
buttonSortByType.Size = new Size(172, 27);
buttonSortByType.TabIndex = 7;
buttonSortByType.Text = "Сортировка по типу";
buttonSortByType.UseVisualStyleBackColor = true;
buttonSortByType.Click += ButtonSortByType_Click;
//
// buttonGoToCheck // buttonGoToCheck
// //
buttonGoToCheck.Location = new Point(5, 185); buttonGoToCheck.Location = new Point(5, 150);
buttonGoToCheck.Name = "buttonGoToCheck"; buttonGoToCheck.Name = "buttonGoToCheck";
buttonGoToCheck.Size = new Size(172, 27); buttonGoToCheck.Size = new Size(172, 27);
buttonGoToCheck.TabIndex = 6; buttonGoToCheck.TabIndex = 6;
@ -191,7 +215,7 @@
// //
// buttonRefresh // buttonRefresh
// //
buttonRefresh.Location = new Point(5, 218); buttonRefresh.Location = new Point(5, 183);
buttonRefresh.Name = "buttonRefresh"; buttonRefresh.Name = "buttonRefresh";
buttonRefresh.Size = new Size(172, 29); buttonRefresh.Size = new Size(172, 29);
buttonRefresh.TabIndex = 5; buttonRefresh.TabIndex = 5;
@ -201,7 +225,7 @@
// //
// buttonRemoveWarPlane // buttonRemoveWarPlane
// //
buttonRemoveWarPlane.Location = new Point(5, 128); buttonRemoveWarPlane.Location = new Point(3, 93);
buttonRemoveWarPlane.Name = "buttonRemoveWarPlane"; buttonRemoveWarPlane.Name = "buttonRemoveWarPlane";
buttonRemoveWarPlane.Size = new Size(172, 51); buttonRemoveWarPlane.Size = new Size(172, 51);
buttonRemoveWarPlane.TabIndex = 4; buttonRemoveWarPlane.TabIndex = 4;
@ -211,7 +235,7 @@
// //
// maskedTextBoxPosition // maskedTextBoxPosition
// //
maskedTextBoxPosition.Location = new Point(5, 95); maskedTextBoxPosition.Location = new Point(5, 60);
maskedTextBoxPosition.Mask = "00"; maskedTextBoxPosition.Mask = "00";
maskedTextBoxPosition.Name = "maskedTextBoxPosition"; maskedTextBoxPosition.Name = "maskedTextBoxPosition";
maskedTextBoxPosition.Size = new Size(172, 27); maskedTextBoxPosition.Size = new Size(172, 27);
@ -232,7 +256,7 @@
comboBoxSelectorCompany.DropDownStyle = ComboBoxStyle.DropDownList; comboBoxSelectorCompany.DropDownStyle = ComboBoxStyle.DropDownList;
comboBoxSelectorCompany.FormattingEnabled = true; comboBoxSelectorCompany.FormattingEnabled = true;
comboBoxSelectorCompany.Items.AddRange(new object[] { "Ангар" }); comboBoxSelectorCompany.Items.AddRange(new object[] { "Ангар" });
comboBoxSelectorCompany.Location = new Point(8, 396); comboBoxSelectorCompany.Location = new Point(6, 365);
comboBoxSelectorCompany.Name = "comboBoxSelectorCompany"; comboBoxSelectorCompany.Name = "comboBoxSelectorCompany";
comboBoxSelectorCompany.Size = new Size(172, 28); comboBoxSelectorCompany.Size = new Size(172, 28);
comboBoxSelectorCompany.TabIndex = 0; comboBoxSelectorCompany.TabIndex = 0;
@ -243,7 +267,7 @@
pictureBox.Dock = DockStyle.Fill; pictureBox.Dock = DockStyle.Fill;
pictureBox.Location = new Point(0, 28); pictureBox.Location = new Point(0, 28);
pictureBox.Name = "pictureBox"; pictureBox.Name = "pictureBox";
pictureBox.Size = new Size(616, 782); pictureBox.Size = new Size(616, 737);
pictureBox.TabIndex = 1; pictureBox.TabIndex = 1;
pictureBox.TabStop = false; pictureBox.TabStop = false;
// //
@ -292,7 +316,7 @@
// //
AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font; AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(813, 810); ClientSize = new Size(813, 765);
Controls.Add(pictureBox); Controls.Add(pictureBox);
Controls.Add(groupBox1); Controls.Add(groupBox1);
Controls.Add(menuStrip); Controls.Add(menuStrip);
@ -337,5 +361,7 @@
private ToolStripMenuItem LoadToolStripMenuItem; private ToolStripMenuItem LoadToolStripMenuItem;
private OpenFileDialog openFileDialog; private OpenFileDialog openFileDialog;
private SaveFileDialog saveFileDialog; private SaveFileDialog saveFileDialog;
private Button buttonSortByType;
private Button buttonSortByColor;
} }
} }

View File

@ -323,5 +323,39 @@ namespace ProjectStormTrooper
} }
} }
/// <summary>
/// Сортировка по типу
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonSortByType_Click(object sender, EventArgs e)
{
CompareCars(new DrawningWarPlaneCompareByType());
}
/// <summary>
/// Сортировка по цвету
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonSortByColor_Click(object sender, EventArgs e)
{
CompareCars(new DrawningWarPlaneCompareByColor());
}
/// <summary>
/// Сортировка по сравнителю
/// </summary>
/// <param name="comparer">Сравнитель объектов</param>
private void CompareCars(IComparer<DrawningWarPlane?> comparer)
{
if (_company == null)
{
return;
}
_company.Sort(comparer);
pictureBox.Image = _company.Show();
}
} }
} }