This commit is contained in:
GokaPek 2023-12-14 00:48:38 +04:00
parent 86566fe526
commit 2955c29f45
11 changed files with 281 additions and 28 deletions

View File

@ -0,0 +1,18 @@
namespace SelfPropelledArtilleryUnit.Generics
{
internal class SPAUCollectionInfo : IEquatable<CarsCollectionInfo>
{
public string Name { get; private set; }
public string Description { get; private set; }
public CarsCollectionInfo(string name, string description)
{
Name = name;
Description = description;
}
public bool Equals(CarsCollectionInfo? other)
{
// TODO прописать логику сравнения по свойству Name
throw new NotImplementedException();
}
}
}

View File

@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.7.34024.191
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SelfPropelledArtilleryUnit", "SelfPropelledArtilleryUnit\SelfPropelledArtilleryUnit.csproj", "{82A37504-B0B3-4518-AF90-0433F4E332E4}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SelfPropelledArtilleryUnit", "SelfPropelledArtilleryUnit\SelfPropelledArtilleryUnit.csproj", "{82A37504-B0B3-4518-AF90-0433F4E332E4}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution

View File

@ -0,0 +1,58 @@
using SelfPropelledArtilleryUnit.DrawningObjects;
using SelfPropelledArtilleryUnit.Entities;
using System.Diagnostics.CodeAnalysis;
namespace SelfPropelledArtilleryUnit.Generics
{
internal class DrawiningSPAUEqutables : IEqualityComparer<DrawningSPAU?>
{
public bool Equals(DrawningSPAU? x, DrawningSPAU? y)
{
if (x == null || x.EntitySPAU == null)
{
throw new ArgumentNullException(nameof(x));
}
if (y == null || y.EntitySPAU == null)
{
throw new ArgumentNullException(nameof(y));
}
if (x.GetType().Name != y.GetType().Name)
{
return false;
}
if (x.EntitySPAU.Speed != y.EntitySPAU.Speed)
{
return false;
}
if (x.EntitySPAU.Weight != y.EntitySPAU.Weight)
{
return false;
}
if (x.EntitySPAU.BodyColor != y.EntitySPAU.BodyColor)
{
return false;
}
if (x is DrawningSPAUchild && y is DrawningSPAUchild)
{
EntitySPAUchild xChild = (EntitySPAUchild)x.EntitySPAU;
EntitySPAUchild yChild = (EntitySPAUchild)y.EntitySPAU;
if (xChild.Ballon != yChild.Ballon)
{
return false;
}
if (xChild.AdditionalColor != yChild.AdditionalColor)
{
return false;
}
if (xChild.BodyKit != yChild.BodyKit)
{
return false;
}
}
return true;
}
public int GetHashCode([DisallowNull] DrawningSPAU obj)
{
return obj.GetHashCode();
}
}
}

View File

@ -35,6 +35,8 @@
ButtonRefreshCollection = new Button();
panel1 = new Panel();
panel2 = new Panel();
ButtonSortByColor = new Button();
ButtonSortByType = new Button();
listBoxStorages = new ListBox();
ButtonDelObject = new Button();
ButtonAddObject = new Button();
@ -61,14 +63,14 @@
//
// maskedTextBoxNumber
//
maskedTextBoxNumber.Location = new Point(18, 71);
maskedTextBoxNumber.Location = new Point(18, 42);
maskedTextBoxNumber.Name = "maskedTextBoxNumber";
maskedTextBoxNumber.Size = new Size(143, 27);
maskedTextBoxNumber.TabIndex = 1;
//
// ButtonAddSPAU
//
ButtonAddSPAU.Location = new Point(44, 19);
ButtonAddSPAU.Location = new Point(44, 3);
ButtonAddSPAU.Name = "ButtonAddSPAU";
ButtonAddSPAU.Size = new Size(102, 33);
ButtonAddSPAU.TabIndex = 2;
@ -78,7 +80,7 @@
//
// ButtonRemoveSPAU
//
ButtonRemoveSPAU.Location = new Point(44, 118);
ButtonRemoveSPAU.Location = new Point(44, 75);
ButtonRemoveSPAU.Name = "ButtonRemoveSPAU";
ButtonRemoveSPAU.Size = new Size(100, 37);
ButtonRemoveSPAU.TabIndex = 3;
@ -88,7 +90,7 @@
//
// ButtonRefreshCollection
//
ButtonRefreshCollection.Location = new Point(44, 172);
ButtonRefreshCollection.Location = new Point(44, 118);
ButtonRefreshCollection.Name = "ButtonRefreshCollection";
ButtonRefreshCollection.Size = new Size(100, 35);
ButtonRefreshCollection.TabIndex = 4;
@ -102,22 +104,44 @@
panel1.Controls.Add(ButtonRemoveSPAU);
panel1.Controls.Add(ButtonAddSPAU);
panel1.Controls.Add(maskedTextBoxNumber);
panel1.Location = new Point(711, 245);
panel1.Location = new Point(711, 305);
panel1.Name = "panel1";
panel1.Size = new Size(176, 227);
panel1.Size = new Size(176, 167);
panel1.TabIndex = 5;
//
// panel2
//
panel2.Controls.Add(ButtonSortByColor);
panel2.Controls.Add(ButtonSortByType);
panel2.Controls.Add(listBoxStorages);
panel2.Controls.Add(ButtonDelObject);
panel2.Controls.Add(ButtonAddObject);
panel2.Controls.Add(textBoxStorageName);
panel2.Location = new Point(711, 40);
panel2.Name = "panel2";
panel2.Size = new Size(174, 199);
panel2.Size = new Size(174, 259);
panel2.TabIndex = 6;
//
// ButtonSortByColor
//
ButtonSortByColor.Location = new Point(3, 227);
ButtonSortByColor.Name = "ButtonSortByColor";
ButtonSortByColor.Size = new Size(168, 29);
ButtonSortByColor.TabIndex = 9;
ButtonSortByColor.Text = "Сортировка по цвету";
ButtonSortByColor.UseVisualStyleBackColor = true;
ButtonSortByColor.Click += ButtonSortByColor_Click;
//
// ButtonSortByType
//
ButtonSortByType.Location = new Point(3, 182);
ButtonSortByType.Name = "ButtonSortByType";
ButtonSortByType.Size = new Size(168, 39);
ButtonSortByType.TabIndex = 8;
ButtonSortByType.Text = "Сортировка по типу";
ButtonSortByType.UseVisualStyleBackColor = true;
ButtonSortByType.Click += ButtonSortByType_Click;
//
// listBoxStorages
//
listBoxStorages.FormattingEnabled = true;
@ -130,7 +154,7 @@
//
// ButtonDelObject
//
ButtonDelObject.Location = new Point(18, 168);
ButtonDelObject.Location = new Point(18, 153);
ButtonDelObject.Name = "ButtonDelObject";
ButtonDelObject.Size = new Size(143, 28);
ButtonDelObject.TabIndex = 3;
@ -175,14 +199,14 @@
// openFileDialog
//
openFileDialog.Name = "openFileDialog";
openFileDialog.Size = new Size(224, 26);
openFileDialog.Size = new Size(205, 26);
openFileDialog.Text = "Открыть файл";
openFileDialog.Click += LoadToolStripMenuItem_Click;
//
// saveFileDialog
//
saveFileDialog.Name = "saveFileDialog";
saveFileDialog.Size = new Size(224, 26);
saveFileDialog.Size = new Size(205, 26);
saveFileDialog.Text = "Сохранить файл";
saveFileDialog.Click += SaveToolStripMenuItem_Click;
//
@ -238,5 +262,7 @@
private ToolStripMenuItem saveFileDialog;
private OpenFileDialog openFileDialog_;
private OpenFileDialog saveFileDialog_;
private Button ButtonSortByColor;
private Button ButtonSortByType;
}
}

View File

@ -50,7 +50,7 @@ namespace SelfPropelledArtilleryUnit
listBoxStorages.Items.Clear();
for (int i = 0; i < _storage.Keys.Count; i++)
{
listBoxStorages.Items.Add(_storage.Keys[i]);
listBoxStorages.Items.Add(_storage.Keys[i].Name);
}
if (listBoxStorages.Items.Count > 0 && (index == -1 || index
>= listBoxStorages.Items.Count))
@ -169,7 +169,7 @@ namespace SelfPropelledArtilleryUnit
MessageBox.Show("Не удалось добавить объект");
_logger.LogWarning($"Неудачная попытка добавления: {ex}");
}
}
@ -200,7 +200,7 @@ namespace SelfPropelledArtilleryUnit
pos = Convert.ToInt32(maskedTextBoxNumber.Text);
}
catch(Exception ex)
catch (Exception ex)
{
MessageBox.Show("Не удалось удалить объект");
_logger.LogWarning($"Неудачная попытка удаления: {ex}");
@ -221,7 +221,7 @@ namespace SelfPropelledArtilleryUnit
_logger.LogWarning($"Неудачная попытка удаления c позиции {pos}");
}
}
catch(SPAUNotFoundException ex)
catch (SPAUNotFoundException ex)
{
MessageBox.Show(ex.Message);
_logger.LogWarning($"Неудачная попытка удаления: {ex}");
@ -258,11 +258,11 @@ namespace SelfPropelledArtilleryUnit
try
{
_storage.SaveData(saveFileDialog_.FileName);
MessageBox.Show("Сохранение прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
_logger.LogInformation("Успешное сохранение");
}
catch(Exception ex)
catch (Exception ex)
{
MessageBox.Show($"Не сохранилось: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
_logger.LogWarning($"Не сохранилось: {ex.Message}");
@ -291,5 +291,38 @@ namespace SelfPropelledArtilleryUnit
}
}
}
/// <summary>
/// Сортировка по типу
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonSortByType_Click(object sender, EventArgs e) => CompareSPAU(new SPAUCompareByType());
/// <summary>
/// Сортировка по цвету
/// </summary>
/// <param name="sender"></param>
private void ButtonSortByColor_Click(object sender, EventArgs e) => CompareSPAU(new SPAUCompareByColor());
/// <summary>
/// Сортировка по сравнителю
/// </summary>
/// <param name="comparer"></param>
private void CompareSPAU(IComparer<DrawningSPAU?> comparer)
{
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
if (obj == null)
{
return;
}
obj.Sort(comparer);
pictureBoxCollection.Image = obj.ShowSPAUs();
}
}
}

View File

@ -0,0 +1,23 @@
namespace SelfPropelledArtilleryUnit.Generics
{
internal class SPAUCollectionInfo : IEquatable<SPAUCollectionInfo>
{
public string Name { get; private set; }
public string Description { get; private set; }
public SPAUCollectionInfo(string name, string description)
{
Name = name;
Description = description;
}
public bool Equals(SPAUCollectionInfo? other)
{
return Name == other?.Name;
throw new NotImplementedException();
}
public override int GetHashCode()
{
return this.Name.GetHashCode();
}
}
}

View File

@ -0,0 +1,46 @@
using SelfPropelledArtilleryUnit.DrawningObjects;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SelfPropelledArtilleryUnit.Generics
{
internal class SPAUCompareByColor : IComparer<DrawningSPAU?>
{
public int Compare(DrawningSPAU? x, DrawningSPAU? y)
{
if (x == null || x.EntitySPAU == null)
{
throw new ArgumentNullException(nameof(x));
}
if (y == null || y.EntitySPAU == null)
{
throw new ArgumentNullException(nameof(y));
}
if (x.EntitySPAU.BodyColor == y.EntitySPAU.BodyColor)
{
return 0;
}
else
{
if (x.EntitySPAU.BodyColor.R.CompareTo(y.EntitySPAU.BodyColor.R) == 0)
{
if (x.EntitySPAU.BodyColor.G.CompareTo(y.EntitySPAU.BodyColor.G) == 0)
{
return x.EntitySPAU.BodyColor.B.CompareTo(y.EntitySPAU.BodyColor.B);
}
else
{
return x.EntitySPAU.BodyColor.G.CompareTo(y.EntitySPAU.BodyColor.G);
}
}
else {
return x.EntitySPAU.BodyColor.R.CompareTo(y.EntitySPAU.BodyColor.R);
}
}
}
}
}

View File

@ -0,0 +1,35 @@
using SelfPropelledArtilleryUnit.DrawningObjects;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SelfPropelledArtilleryUnit.Generics
{
internal class SPAUCompareByType : IComparer<DrawningSPAU?>
{
public int Compare(DrawningSPAU? x, DrawningSPAU? y)
{
if (x == null || x.EntitySPAU == null)
{
throw new ArgumentNullException(nameof(x));
}
if (y == null || y.EntitySPAU == null)
{
throw new ArgumentNullException(nameof(y));
}
if (x.GetType().Name != y.GetType().Name)
{
return x.GetType().Name.CompareTo(y.GetType().Name);
}
var speedCompare = x.EntitySPAU.Speed.CompareTo(y.EntitySPAU.Speed);
if (speedCompare != 0)
{
return speedCompare;
}
return x.EntitySPAU.Weight.CompareTo(y.EntitySPAU.Weight);
}
}
}

View File

@ -150,6 +150,14 @@ namespace SelfPropelledArtilleryUnit.Generics
current?.DrawTransport(g);
}
}
// <summary>
/// Сортировка
/// </summary>
/// <param name="comparer"></param>
public void Sort(IComparer<T?> comparer) =>
_collection.SortSet(comparer);
}
}

View File

@ -16,11 +16,11 @@ namespace SelfPropelledArtilleryUnit.Generics
/// <summary>
/// Словарь (хранилище)
/// </summary>
readonly Dictionary<string, SPAUGenericCollection<DrawningSPAU, DrawningObjectSPAU>> _SPAUStorages;
readonly Dictionary<SPAUCollectionInfo, SPAUGenericCollection<DrawningSPAU, DrawningObjectSPAU>> _SPAUStorages;
/// <summary>
/// Возвращение списка названий наборов
/// </summary>
public List<string> Keys => _SPAUStorages.Keys.ToList();
public List<SPAUCollectionInfo> Keys => _SPAUStorages.Keys.ToList();
/// <summary>
/// Ширина окна отрисовки
/// </summary>
@ -36,7 +36,7 @@ namespace SelfPropelledArtilleryUnit.Generics
/// <param name="pictureHeight"></param>
public SPAUGenericStorage(int pictureWidth, int pictureHeight)
{
_SPAUStorages = new Dictionary<string, SPAUGenericCollection<DrawningSPAU, DrawningObjectSPAU>>();
_SPAUStorages = new Dictionary<SPAUCollectionInfo, SPAUGenericCollection<DrawningSPAU, DrawningObjectSPAU>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
@ -48,7 +48,7 @@ namespace SelfPropelledArtilleryUnit.Generics
{
try
{
_SPAUStorages.Add(name, new SPAUGenericCollection<DrawningSPAU, DrawningObjectSPAU>(_pictureWidth, _pictureHeight));
_SPAUStorages.Add(new SPAUCollectionInfo(name, string.Empty), new SPAUGenericCollection<DrawningSPAU, DrawningObjectSPAU>(_pictureWidth, _pictureHeight));
}
catch (Exception) { return; }
}
@ -58,7 +58,7 @@ namespace SelfPropelledArtilleryUnit.Generics
/// <param name="name">Название набора</param>
public void DelSet(string name)
{
try { _SPAUStorages.Remove(name); } catch (Exception) { return; }
try { _SPAUStorages.Remove(new SPAUCollectionInfo(name, string.Empty)); } catch (Exception) { return; }
}
/// <summary>
/// Доступ к набору
@ -69,9 +69,9 @@ namespace SelfPropelledArtilleryUnit.Generics
{
get
{
if (_SPAUStorages.ContainsKey(ind))
if (_SPAUStorages.ContainsKey(new SPAUCollectionInfo(ind, string.Empty)))
{
return _SPAUStorages[ind];
return _SPAUStorages[new SPAUCollectionInfo(ind, string.Empty)];
}
else
{
@ -105,7 +105,7 @@ namespace SelfPropelledArtilleryUnit.Generics
File.Delete(filename);
}
StringBuilder data = new();
foreach (KeyValuePair<string, SPAUGenericCollection<DrawningSPAU, DrawningObjectSPAU>> record in _SPAUStorages)
foreach (KeyValuePair<SPAUCollectionInfo, SPAUGenericCollection<DrawningSPAU, DrawningObjectSPAU>> record in _SPAUStorages)
{
StringBuilder records = new();
foreach (DrawningSPAU? elem in record.Value.GetCars.Reverse())
@ -178,7 +178,7 @@ namespace SelfPropelledArtilleryUnit.Generics
}
}
}
_SPAUStorages.Add(record[0], collection);
_SPAUStorages.Add(new SPAUCollectionInfo(record[0], string.Empty), collection);
}
return true;
}

View File

@ -42,7 +42,7 @@ namespace SelfPropelledArtilleryUnit.Generics
/// </summary>
/// <param name="spau">Добавляемый автомобиль</param>
/// <returns></returns>
public int Insert(T spau)
public int Insert(T spau, IEqualityComparer<T?>? equal = null)
{
return Insert(spau, 0);
}
@ -52,7 +52,7 @@ namespace SelfPropelledArtilleryUnit.Generics
/// <param name="car">Добавляемый автомобиль</param>
/// <param name="position">Позиция</param>
/// <returns></returns>
public int Insert(T spau, int position)
public int Insert(T spau, int position, IEqualityComparer<T?>? equal = null)
{
if (Count == _maxCount)
throw new StorageOverflowException(Count);
@ -124,6 +124,12 @@ namespace SelfPropelledArtilleryUnit.Generics
}
}
/// <summary>
/// Сортировка набора объектов
/// </summary>
/// <param name="comparer"></param>
public void SortSet(IComparer<T?> comparer) =>
_places.Sort(comparer);
}
}