This commit is contained in:
ShabOl 2023-12-10 16:40:20 +04:00
parent f49703596b
commit 57f42e5abd
9 changed files with 216 additions and 21 deletions

View File

@ -30,6 +30,8 @@
{
CollectionPictureBox = new PictureBox();
ToolGroupBox = new GroupBox();
SortByColorButton = new Button();
SortByTypeButton = new Button();
SetsGroupBox = new GroupBox();
SetNameTextBox = new TextBox();
StorageListBox = new ListBox();
@ -63,6 +65,8 @@
// ToolGroupBox
//
ToolGroupBox.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Right;
ToolGroupBox.Controls.Add(SortByColorButton);
ToolGroupBox.Controls.Add(SortByTypeButton);
ToolGroupBox.Controls.Add(SetsGroupBox);
ToolGroupBox.Controls.Add(RefreshCollectionButton);
ToolGroupBox.Controls.Add(RemoveBomberButton);
@ -75,6 +79,26 @@
ToolGroupBox.TabStop = false;
ToolGroupBox.Text = "Инструменты";
//
// SortByColorButton
//
SortByColorButton.Location = new Point(6, 305);
SortByColorButton.Name = "SortByColorButton";
SortByColorButton.Size = new Size(164, 30);
SortByColorButton.TabIndex = 10;
SortByColorButton.Text = "Сортировка по цвету";
SortByColorButton.UseVisualStyleBackColor = true;
SortByColorButton.Click += SortByColorButton_Click;
//
// SortByTypeButton
//
SortByTypeButton.Location = new Point(6, 269);
SortByTypeButton.Name = "SortByTypeButton";
SortByTypeButton.Size = new Size(164, 30);
SortByTypeButton.TabIndex = 9;
SortByTypeButton.Text = "Сортировка по типу";
SortByTypeButton.UseVisualStyleBackColor = true;
SortByTypeButton.Click += SortByTypeButton_Click;
//
// SetsGroupBox
//
SetsGroupBox.Controls.Add(SetNameTextBox);
@ -244,5 +268,7 @@
private ToolStripMenuItem LoadToolStripMenuItem;
private OpenFileDialog OpenFileDialog;
private SaveFileDialog SaveFileDialog;
private Button SortByTypeButton;
private Button SortByColorButton;
}
}

View File

@ -25,7 +25,7 @@ namespace AirBomber
StorageListBox.Items.Clear();
for (int i = 0; i < _storage.Keys.Count; i++)
StorageListBox.Items.Add(_storage.Keys[i]);
StorageListBox.Items.Add(_storage.Keys[i].Name);
if (StorageListBox.Items.Count > 0 && (index == -1 || index >= StorageListBox.Items.Count))
StorageListBox.SelectedIndex = 0;
@ -72,6 +72,11 @@ namespace AirBomber
MessageBox.Show(ex.Message);
_logger.LogWarning(ex.Message);
}
catch (ArgumentException ex)
{
MessageBox.Show(ex.Message);
_logger.LogWarning(ex.Message);
}
}
public void ButtonRemoveEntity_Click(object sender, EventArgs e)
@ -106,7 +111,7 @@ namespace AirBomber
{
MessageBox.Show(ex.Message);
_logger.LogWarning(ex.Message);
}
}
}
public void ButtonRefreshCollection_Click(object sender, EventArgs e)
@ -193,7 +198,7 @@ namespace AirBomber
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}");
@ -202,5 +207,23 @@ namespace AirBomber
ReloadObjects();
}
private void SortByTypeButton_Click(object sender, EventArgs e) => CompareBombers(new BomberCompareByType());
private void SortByColorButton_Click(object sender, EventArgs e) => CompareBombers(new BomberCompareByColor());
private void CompareBombers(IComparer<BomberRendererBase?> Comparer)
{
if (StorageListBox.SelectedIndex == -1)
return;
var obj = _storage[StorageListBox.SelectedItem.ToString() ?? string.Empty];
if (obj == null)
return;
obj.Sort(Comparer);
CollectionPictureBox.Image = obj.ShowEntities();
}
}
}

View File

@ -0,0 +1,26 @@
using AirBomber.Entities;
using AirBomber.Rendering;
namespace AirBomber.Generics
{
internal class BomberCompareByColor : IComparer<BomberRendererBase?>
{
public int Compare(BomberRendererBase? lhs, BomberRendererBase? rhs)
{
if (lhs == null || lhs.EntityBomber == null)
throw new ArgumentNullException(nameof(lhs));
if (rhs == null || rhs.EntityBomber == null)
throw new ArgumentNullException(nameof(rhs));
var BaseColorCompare = lhs.EntityBomber.BodyColor.Name.CompareTo(rhs.EntityBomber.BodyColor.Name);
if ( !(lhs is BomberRenderer && rhs is BomberRenderer) )
return BaseColorCompare;
// Если оба объекта - продвинутого типа, сравниваем еще и по дополнительному цвету
return
((BomberEntity)lhs.EntityBomber).AdditionalColor.Name.CompareTo(((BomberEntity)rhs.EntityBomber).AdditionalColor.Name);
}
}
}

View File

@ -0,0 +1,25 @@
using AirBomber.Rendering;
namespace AirBomber.Generics
{
internal class BomberCompareByType : IComparer<BomberRendererBase?>
{
public int Compare(BomberRendererBase? lhs, BomberRendererBase? rhs)
{
if (lhs == null || lhs.EntityBomber == null)
throw new ArgumentNullException(nameof(lhs));
if (rhs == null || rhs.EntityBomber == null)
throw new ArgumentNullException(nameof(rhs));
if (lhs.GetType().Name != rhs.GetType().Name)
return lhs.GetType().Name.CompareTo(rhs.GetType().Name);
var SpeedCompare = lhs.EntityBomber.Speed.CompareTo(rhs.EntityBomber.Speed);
if (SpeedCompare != 0)
return SpeedCompare;
return lhs.EntityBomber.Weight.CompareTo(rhs.EntityBomber.Weight);
}
}
}

View File

@ -0,0 +1,52 @@
using AirBomber.Entities;
using AirBomber.Rendering;
using System.Diagnostics.CodeAnalysis;
namespace AirBomber.Generics
{
internal class BomberRendererEquatables : IEqualityComparer<BomberRendererBase?>
{
public bool Equals(BomberRendererBase? lhs, BomberRendererBase? rhs)
{
if (lhs is null || lhs.EntityBomber is null)
throw new ArgumentNullException(nameof(lhs));
if (rhs is null || rhs.EntityBomber is null)
throw new ArgumentNullException(nameof(rhs));
if (lhs.GetType().Name != rhs.GetType().Name)
return false;
if (lhs.EntityBomber.Speed != rhs.EntityBomber.Speed)
return false;
if (lhs.EntityBomber.Weight != rhs.EntityBomber.Weight)
return false;
if (lhs.EntityBomber.BodyColor != rhs.EntityBomber.BodyColor)
return false;
if (lhs is BomberRenderer && rhs is BomberRenderer)
{
BomberEntity lhsEntity = (BomberEntity)lhs.EntityBomber;
BomberEntity rhsEntity = (BomberEntity)rhs.EntityBomber;
if (lhsEntity.AdditionalColor != rhsEntity.AdditionalColor)
return false;
if (lhsEntity.Bombs != rhsEntity.Bombs)
return false;
if (lhsEntity.FuelTanks != rhsEntity.FuelTanks)
return false;
}
return true;
}
public int GetHashCode([DisallowNull] BomberRendererBase obj)
{
return obj.GetHashCode();
}
}
}

View File

@ -0,0 +1,27 @@
namespace AirBomber.Generics
{
internal class BombersCollectionInfo : IEquatable<BombersCollectionInfo>
{
public string Name { get; private set; }
public string Description { get; private set; }
public BombersCollectionInfo(string Name, string Description)
{
this.Name = Name;
this.Description = Description;
}
public bool Equals(BombersCollectionInfo? rhs)
{
if (this.Name == rhs?.Name)
return true;
return false;
}
public override int GetHashCode()
{
return Name.GetHashCode();
}
}
}

View File

@ -32,7 +32,7 @@ namespace AirBomber.Generics
if (obj is null || collect is null)
return -1;
return collect._collection.Insert(obj);
return collect._collection.Insert(obj, new BomberRendererEquatables());
}
public static bool operator -(EntitiesGenericCollection<T, U> collect, int pos)
@ -96,5 +96,7 @@ namespace AirBomber.Generics
Entity.DrawEntity(g);
}
}
public void Sort(IComparer<T?> Comparer) => _collection.SortSet(Comparer);
}
}

View File

@ -7,9 +7,9 @@ namespace AirBomber.Generics
{
internal class EntitiesGenericStorage
{
readonly Dictionary<string, EntitiesGenericCollection<BomberRendererBase, ObjectEntityRenderer>> _entityStorages;
readonly Dictionary<BombersCollectionInfo, EntitiesGenericCollection<BomberRendererBase, ObjectEntityRenderer>> _entityStorages;
public List<string> Keys => _entityStorages.Keys.ToList();
public List<BombersCollectionInfo> Keys => _entityStorages.Keys.ToList();
private readonly int _pictureWidth;
private readonly int _pictureHeight;
@ -20,7 +20,7 @@ namespace AirBomber.Generics
public EntitiesGenericStorage(int PictureWidth, int PictureHeight)
{
_entityStorages = new Dictionary<string, EntitiesGenericCollection<BomberRendererBase, ObjectEntityRenderer>>();
_entityStorages = new Dictionary<BombersCollectionInfo, EntitiesGenericCollection<BomberRendererBase, ObjectEntityRenderer>>();
_pictureWidth = PictureWidth;
_pictureHeight = PictureHeight;
@ -31,28 +31,28 @@ namespace AirBomber.Generics
var NewCollection =
new EntitiesGenericCollection<BomberRendererBase, ObjectEntityRenderer>(_pictureWidth, _pictureHeight);
if (Keys.Contains(Name))
if (Keys.Contains(new BombersCollectionInfo(Name, string.Empty)))
{
MessageBox.Show("Набор с таким именем уже существует", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
_entityStorages.Add(Name, NewCollection);
_entityStorages.Add(new BombersCollectionInfo(Name, string.Empty), NewCollection);
}
public void RemoveSet(string Name)
{
_entityStorages.Remove(Name);
_entityStorages.Remove(new BombersCollectionInfo(Name, string.Empty));
}
public EntitiesGenericCollection<BomberRendererBase, ObjectEntityRenderer>? this[string Index]
{
get
{
if (!_entityStorages.ContainsKey(Index))
if (!_entityStorages.ContainsKey(new BombersCollectionInfo(Index, string.Empty)))
return null;
return _entityStorages[Index];
return _entityStorages[new BombersCollectionInfo(Index, string.Empty)];
}
}
@ -65,14 +65,14 @@ namespace AirBomber.Generics
{
writer.WriteLine("BomberStorage");
foreach (KeyValuePair<string, EntitiesGenericCollection<BomberRendererBase, ObjectEntityRenderer>> Record in _entityStorages)
foreach (KeyValuePair<BombersCollectionInfo, EntitiesGenericCollection<BomberRendererBase, ObjectEntityRenderer>> Record in _entityStorages)
{
StringBuilder Records = new StringBuilder();
foreach (BomberRendererBase? Element in Record.Value.Entities)
Records.Append($"{Element?.SerializeRenderer(_entityDelimiter)}{_recordsDelimiter}");
writer.WriteLine($"{Record.Key}{_keyValueDelimiter}{Records}");
writer.WriteLine($"{Record.Key.Name}{_keyValueDelimiter}{Records}");
}
}
}
@ -111,7 +111,7 @@ namespace AirBomber.Generics
}
}
_entityStorages.Add(Record[0], Collection);
_entityStorages.Add(new BombersCollectionInfo(Record[0], string.Empty), Collection);
}
}
}

View File

@ -14,31 +14,43 @@ namespace AirBomber.Generics
{
_maxCount = Count;
_objects = new List<T?>(_maxCount);
for (int i = 0; i < _maxCount; i++)
_objects.Add(null);
}
public int Insert(T Entity)
public int Insert(T Entity, IEqualityComparer<T?>? EqualityComparer = null)
{
/** Вставка в начало набора */
for (int i = 0; i < _maxCount; i++)
for (int i = 0; i < Count; i++)
if (_objects[i] is null)
{
if (EqualityComparer is not null && _objects.Contains(Entity, EqualityComparer))
throw new ArgumentException("Объект уже существует");
_objects[i] = Entity;
return i;
}
if (Count < _maxCount)
{
if (EqualityComparer is not null && _objects.Contains(Entity, EqualityComparer))
throw new ArgumentException("Объект уже существует");
_objects.Add(Entity);
return Count - 1;
}
throw new StorageOverflowException(_maxCount);
}
public int Insert(T Entity, int Position)
public int Insert(T Entity, int Position, IEqualityComparer<T?>? EqualityComparer = null)
{
if (Position >= _maxCount)
throw new StorageOverflowException(_maxCount);
if (_objects[Position] is null)
{
if (EqualityComparer is not null && _objects.Contains(Entity, EqualityComparer))
throw new ArgumentException("Объект уже существует");
_objects[Position] = Entity;
return Position;
}
@ -89,5 +101,7 @@ namespace AirBomber.Generics
yield break;
}
}
public void SortSet(IComparer<T?> Comparer) => _objects.Sort(Comparer);
}
}