Merge branch 'PrevLab8' into Lab8

This commit is contained in:
Данияр Аглиуллов 2022-11-02 23:41:29 +04:00
commit 4a7b7a0df9
10 changed files with 232 additions and 29 deletions

View File

@ -7,7 +7,7 @@ using System.Threading.Tasks;
namespace AirBomber
{
internal abstract class AbstractMap
internal abstract class AbstractMap : IEquatable<AbstractMap>
{
private IDrawningObject _drawningObject = null;
private Bitmap? _staticBitMap;
@ -174,5 +174,32 @@ namespace AirBomber
protected abstract void GenerateMap();
protected abstract void DrawRoadPart(Graphics g, int i, int j);
protected abstract void DrawBarrierPart(Graphics g, int i, int j);
public bool Equals(AbstractMap? other)
{
if (other == null ||
_map != other._map ||
_width != other._width ||
_size_x != other._size_x ||
_size_y != other._size_y ||
_height != other._height ||
GetType() != other.GetType() ||
_map.GetLength(0) != other._map.GetLength(0) ||
_map.GetLength(1) != other._map.GetLength(1))
{
return false;
}
for (int i = 0; i < _map.GetLength(0); i++)
{
for (int j = 0; j < _map.GetLength(1); j++)
{
if (_map[i, j] != other._map[i, j])
{
return false;
}
}
}
return true;
}
}
}

View File

@ -0,0 +1,33 @@
namespace AirBomber
{
internal class AirplaneCompareByColor : IComparer<IDrawningObject>
{
public int Compare(IDrawningObject? x, IDrawningObject? y)
{
var xAirplane = x as DrawningObject;
var yAirplane = y as DrawningObject;
if (xAirplane == yAirplane)
{
return 0;
}
if (xAirplane == null)
{
return 1;
}
if (yAirplane == null)
{
return -1;
}
var xEntity = xAirplane.Airplane.Airplane;
var yEntity = yAirplane.Airplane.Airplane;
var colorWeight = xEntity.BodyColor.ToArgb().CompareTo(yEntity.BodyColor.ToArgb());
if (colorWeight != 0 ||
xEntity is not EntityAirBomber xEntityAirBomber ||
yEntity is not EntityAirBomber yEntityAirBomber)
{
return colorWeight;
}
return xEntityAirBomber.DopColor.ToArgb().CompareTo(yEntityAirBomber.DopColor.ToArgb());
}
}
}

View File

@ -0,0 +1,37 @@
namespace AirBomber
{
internal class AirplaneCompareByType : IComparer<IDrawningObject>
{
public int Compare(IDrawningObject? x, IDrawningObject? y)
{
var xAirplane = x as DrawningObject;
var yAirplane = y as DrawningObject;
if (xAirplane == yAirplane)
{
return 0;
}
if (xAirplane == null)
{
return 1;
}
if (yAirplane == null)
{
return -1;
}
if (xAirplane.Airplane.GetType().Name != yAirplane.Airplane.GetType().Name)
{
if (xAirplane.Airplane.GetType() == typeof(DrawningAirplane))
{
return -1;
}
return 1;
}
var speedCompare = xAirplane.Airplane.Airplane.Speed.CompareTo(yAirplane.Airplane.Airplane.Speed);
if (speedCompare != 0)
{
return speedCompare;
}
return xAirplane.Airplane.Airplane.Weight.CompareTo(yAirplane.Airplane.Airplane.Weight);
}
}
}

View File

@ -8,37 +8,63 @@ namespace AirBomber
{
internal class DrawningObject : IDrawningObject
{
private DrawningAirplane _airplane = null;
public DrawningObject(DrawningAirplane airplane)
{
_airplane = airplane;
Airplane = airplane;
}
public float Step => _airplane?.Airplane?.Step ?? 0;
public float Step => Airplane?.Airplane?.Step ?? 0;
public DrawningAirplane Airplane { get; private set; }
public RectangleF GetCurrentPosition()
{
return _airplane.GetCurrentPosition();
return Airplane.GetCurrentPosition();
}
public void MoveObject(Direction direction)
{
_airplane?.MoveTransport(direction);
Airplane?.MoveTransport(direction);
}
public void SetObject(int x, int y, int width, int height)
{
_airplane?.SetPosition(x, y, width, height);
Airplane?.SetPosition(x, y, width, height);
}
public void DrawObject(Graphics g)
{
_airplane?.DrawTransport(g);
Airplane?.DrawTransport(g);
}
public string GetInfo() => _airplane?.GetDataForSave();
public string GetInfo() => Airplane?.GetDataForSave() ?? string.Empty;
public static IDrawningObject Create(string data) => new DrawningObject(data.CreateDrawningAirplane());
public bool Equals(IDrawningObject? other)
{
if (other is not DrawningObject otherAirplane)
{
return false;
}
var entity = Airplane.Airplane;
var otherEntity = otherAirplane.Airplane.Airplane;
if (entity.GetType() != otherEntity.GetType() ||
entity.Speed != otherEntity.Speed ||
entity.Weight != otherEntity.Weight ||
entity.BodyColor != otherEntity.BodyColor)
{
return false;
}
if (entity is EntityAirBomber entityAirBomber &&
otherEntity is EntityAirBomber otherEntityAirBomber && (
entityAirBomber.HasBombs != otherEntityAirBomber.HasBombs ||
entityAirBomber.DopColor != otherEntityAirBomber.DopColor ||
entityAirBomber.HasFuelTanks != otherEntityAirBomber.HasFuelTanks))
{
return false;
}
return true;
}
}
}

View File

@ -51,13 +51,18 @@
this.LoadToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.openFileDialog = new System.Windows.Forms.OpenFileDialog();
this.saveFileDialog = new System.Windows.Forms.SaveFileDialog();
this.buttonSortByColor = new System.Windows.Forms.Button();
this.buttonSortByType = new System.Windows.Forms.Button();
this.groupBoxTools.SuspendLayout();
this.groupBoxMaps.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit();
this.menuStrip.SuspendLayout();
this.SuspendLayout();
//
// groupBoxTools
//
this.groupBoxTools.Controls.Add(this.buttonSortByType);
this.groupBoxTools.Controls.Add(this.buttonSortByColor);
this.groupBoxTools.Controls.Add(this.groupBoxMaps);
this.groupBoxTools.Controls.Add(this.maskedTextBoxPosition);
this.groupBoxTools.Controls.Add(this.buttonRemoveAirplane);
@ -69,9 +74,9 @@
this.groupBoxTools.Controls.Add(this.buttonShowOnMap);
this.groupBoxTools.Controls.Add(this.buttonAddAirplane);
this.groupBoxTools.Dock = System.Windows.Forms.DockStyle.Right;
this.groupBoxTools.Location = new System.Drawing.Point(811, 0);
this.groupBoxTools.Location = new System.Drawing.Point(811, 24);
this.groupBoxTools.Name = "groupBoxTools";
this.groupBoxTools.Size = new System.Drawing.Size(204, 632);
this.groupBoxTools.Size = new System.Drawing.Size(204, 625);
this.groupBoxTools.TabIndex = 0;
this.groupBoxTools.TabStop = false;
this.groupBoxTools.Text = "Инструменты";
@ -140,7 +145,7 @@
//
// maskedTextBoxPosition
//
this.maskedTextBoxPosition.Location = new System.Drawing.Point(17, 355);
this.maskedTextBoxPosition.Location = new System.Drawing.Point(17, 399);
this.maskedTextBoxPosition.Mask = "00";
this.maskedTextBoxPosition.Name = "maskedTextBoxPosition";
this.maskedTextBoxPosition.Size = new System.Drawing.Size(175, 23);
@ -149,7 +154,7 @@
//
// buttonRemoveAirplane
//
this.buttonRemoveAirplane.Location = new System.Drawing.Point(17, 384);
this.buttonRemoveAirplane.Location = new System.Drawing.Point(17, 428);
this.buttonRemoveAirplane.Name = "buttonRemoveAirplane";
this.buttonRemoveAirplane.Size = new System.Drawing.Size(175, 35);
this.buttonRemoveAirplane.TabIndex = 3;
@ -159,7 +164,7 @@
//
// buttonShowStorage
//
this.buttonShowStorage.Location = new System.Drawing.Point(17, 437);
this.buttonShowStorage.Location = new System.Drawing.Point(17, 472);
this.buttonShowStorage.Name = "buttonShowStorage";
this.buttonShowStorage.Size = new System.Drawing.Size(175, 35);
this.buttonShowStorage.TabIndex = 4;
@ -172,7 +177,7 @@
this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonDown.BackgroundImage = global::AirBomber.Properties.Resources.arrowDown;
this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonDown.Location = new System.Drawing.Point(91, 582);
this.buttonDown.Location = new System.Drawing.Point(90, 588);
this.buttonDown.Name = "buttonDown";
this.buttonDown.Size = new System.Drawing.Size(30, 30);
this.buttonDown.TabIndex = 10;
@ -184,7 +189,7 @@
this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonRight.BackgroundImage = global::AirBomber.Properties.Resources.arrowRight;
this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonRight.Location = new System.Drawing.Point(127, 582);
this.buttonRight.Location = new System.Drawing.Point(126, 588);
this.buttonRight.Name = "buttonRight";
this.buttonRight.Size = new System.Drawing.Size(30, 30);
this.buttonRight.TabIndex = 9;
@ -196,7 +201,7 @@
this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonLeft.BackgroundImage = global::AirBomber.Properties.Resources.arrowLeft;
this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonLeft.Location = new System.Drawing.Point(55, 582);
this.buttonLeft.Location = new System.Drawing.Point(54, 588);
this.buttonLeft.Name = "buttonLeft";
this.buttonLeft.Size = new System.Drawing.Size(30, 30);
this.buttonLeft.TabIndex = 8;
@ -208,7 +213,7 @@
this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonUp.BackgroundImage = global::AirBomber.Properties.Resources.arrowUp;
this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonUp.Location = new System.Drawing.Point(91, 546);
this.buttonUp.Location = new System.Drawing.Point(90, 552);
this.buttonUp.Name = "buttonUp";
this.buttonUp.Size = new System.Drawing.Size(30, 30);
this.buttonUp.TabIndex = 7;
@ -217,7 +222,7 @@
//
// buttonShowOnMap
//
this.buttonShowOnMap.Location = new System.Drawing.Point(17, 487);
this.buttonShowOnMap.Location = new System.Drawing.Point(17, 513);
this.buttonShowOnMap.Name = "buttonShowOnMap";
this.buttonShowOnMap.Size = new System.Drawing.Size(175, 35);
this.buttonShowOnMap.TabIndex = 5;
@ -227,7 +232,7 @@
//
// buttonAddAirplane
//
this.buttonAddAirplane.Location = new System.Drawing.Point(17, 314);
this.buttonAddAirplane.Location = new System.Drawing.Point(17, 358);
this.buttonAddAirplane.Name = "buttonAddAirplane";
this.buttonAddAirplane.Size = new System.Drawing.Size(175, 35);
this.buttonAddAirplane.TabIndex = 1;
@ -238,9 +243,9 @@
// pictureBox
//
this.pictureBox.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBox.Location = new System.Drawing.Point(0, 0);
this.pictureBox.Location = new System.Drawing.Point(0, 24);
this.pictureBox.Name = "pictureBox";
this.pictureBox.Size = new System.Drawing.Size(811, 632);
this.pictureBox.Size = new System.Drawing.Size(811, 625);
this.pictureBox.TabIndex = 1;
this.pictureBox.TabStop = false;
//
@ -265,14 +270,14 @@
// SaveToolStripMenuItem
//
this.SaveToolStripMenuItem.Name = "SaveToolStripMenuItem";
this.SaveToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
this.SaveToolStripMenuItem.Size = new System.Drawing.Size(141, 22);
this.SaveToolStripMenuItem.Text = "Сохранение";
this.SaveToolStripMenuItem.Click += new System.EventHandler(this.SaveToolStripMenuItem_Click);
//
// LoadToolStripMenuItem
//
this.LoadToolStripMenuItem.Name = "LoadToolStripMenuItem";
this.LoadToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
this.LoadToolStripMenuItem.Size = new System.Drawing.Size(141, 22);
this.LoadToolStripMenuItem.Text = "Загрузка";
this.LoadToolStripMenuItem.Click += new System.EventHandler(this.LoadToolStripMenuItem_Click);
//
@ -284,11 +289,31 @@
//
this.saveFileDialog.Filter = "txt file | *.txt";
//
// buttonSortByColor
//
this.buttonSortByColor.Location = new System.Drawing.Point(17, 317);
this.buttonSortByColor.Name = "buttonSortByColor";
this.buttonSortByColor.Size = new System.Drawing.Size(175, 35);
this.buttonSortByColor.TabIndex = 11;
this.buttonSortByColor.Text = "Отсортировать по цвету";
this.buttonSortByColor.UseVisualStyleBackColor = true;
this.buttonSortByColor.Click += new System.EventHandler(this.ButtonSortByColor_Click);
//
// buttonSortByType
//
this.buttonSortByType.Location = new System.Drawing.Point(17, 276);
this.buttonSortByType.Name = "buttonSortByType";
this.buttonSortByType.Size = new System.Drawing.Size(175, 35);
this.buttonSortByType.TabIndex = 12;
this.buttonSortByType.Text = "Отсортировать по типу";
this.buttonSortByType.UseVisualStyleBackColor = true;
this.buttonSortByType.Click += new System.EventHandler(this.ButtonSortByType_Click);
//
// FormMapWithSetAirplanes
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1015, 632);
this.ClientSize = new System.Drawing.Size(1015, 649);
this.Controls.Add(this.pictureBox);
this.Controls.Add(this.groupBoxTools);
this.Controls.Add(this.menuStrip);
@ -303,6 +328,7 @@
this.menuStrip.ResumeLayout(false);
this.menuStrip.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
@ -331,5 +357,7 @@
private ToolStripMenuItem LoadToolStripMenuItem;
private OpenFileDialog openFileDialog;
private SaveFileDialog saveFileDialog;
private Button buttonSortByType;
private Button buttonSortByColor;
}
}

View File

@ -149,6 +149,11 @@ namespace AirBomber
_logger.LogWarning("Ошибка переполнения хранилища: {0}", ex.Message);
MessageBox.Show($"Ошибка переполнения хранилища: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (ArgumentException ex)
{
_logger.LogWarning("Ошибка добавления: {0}. Объект: {@Airplane}", ex.Message, airplane);
MessageBox.Show(ex.Message, "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
/// <summary>
@ -289,5 +294,18 @@ namespace AirBomber
}
}
}
private void SortBy(IComparer<IDrawningObject> comparer)
{
if (listBoxMaps.SelectedIndex == -1)
{
return;
}
_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].Sort(comparer);
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
private void ButtonSortByType_Click (object sender, EventArgs e) => SortBy(new AirplaneCompareByType());
private void ButtonSortByColor_Click(object sender, EventArgs e) => SortBy(new AirplaneCompareByColor());
}
}

View File

@ -57,4 +57,13 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<metadata name="menuStrip.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
<metadata name="openFileDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>125, 17</value>
</metadata>
<metadata name="saveFileDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>258, 17</value>
</metadata>
</root>

View File

@ -9,7 +9,7 @@ namespace AirBomber
/// <summary>
/// Интерфейс для работы с объектом, прорисовываемым на форме
/// </summary>
internal interface IDrawningObject
internal interface IDrawningObject : IEquatable<IDrawningObject>
{
/// <summary>
/// Шаг перемещения объекта

View File

@ -7,7 +7,7 @@ using System.Threading.Tasks;
namespace AirBomber
{
internal class MapWithSetAirplanesGeneric<T, U>
where T : class, IDrawningObject
where T : class, IEquatable<T>, IDrawningObject
where U : AbstractMap
{
/// <summary>
@ -208,5 +208,14 @@ namespace AirBomber
_setAirplanes.Insert(DrawningObject.Create(rec) as T);
}
}
/// <summary>
/// Сортировка
/// </summary>
/// <param name="comparer"></param>
public void Sort(IComparer<T> comparer)
{
_setAirplanes.SortSet(comparer);
}
}
}

View File

@ -11,7 +11,7 @@ namespace AirBomber
/// </summary>
/// <typeparam name="T"></typeparam>
internal class SetAirplanesGeneric<T>
where T : class
where T : class, IEquatable<T>
{
/// <summary>
/// Список объектов, которые храним
@ -51,9 +51,13 @@ namespace AirBomber
/// </summary>
/// <param name="airplane">Добавляемый самолет</param>
/// <param name="position">Позиция</param>
/// <exception cref="ArgumentException"> Исключения генерируется при попытке добавить дубликат в набор</exception>
/// <exception cref="StorageOverflowException"> </exception>
/// <returns>Возвращает позицию вставленного объекта, либо -1 если его не удалось вставить</returns>
public int Insert(T airplane, int position)
{
if (_places.Contains(airplane))
throw new ArgumentException($"Объект {airplane} уже есть в наборе");
if (Count == _maxcount)
throw new StorageOverflowException(_maxcount);
if (!isCorrectPosition(position))
@ -113,5 +117,17 @@ namespace AirBomber
}
}
/// <summary>
/// Сортировка набора объектов
/// </summary>
/// <param name="comparer"></param>
public void SortSet(IComparer<T> comparer)
{
if (comparer == null)
{
return;
}
_places.Sort(comparer);
}
}
}