Compare commits

...

3 Commits

9 changed files with 321 additions and 16 deletions

View File

@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace Artilleries
{
internal abstract class AbstractMap
internal abstract class AbstractMap : IEquatable<AbstractMap>
{
private IDrawingObject _drawingObject = null;
protected int[,] _map = null;
@ -122,5 +122,46 @@ namespace Artilleries
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)
{
return false;
}
if (_width != other._width)
{
return false;
}
if (_height != other._height)
{
return false;
}
if (_size_x != other._size_x)
{
return false;
}
if (_size_y != other._size_y)
{
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,74 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Artilleries
{
internal class ArtilleryCompareByColor : IComparer<IDrawingObject>
{
public int Compare(IDrawingObject? first, IDrawingObject? second)
{
if (first == null && second == null)
{
return 0;
}
if (first == null && second != null)
{
return 1;
}
if (first != null && second == null)
{
return -1;
}
var firstArtillery = first as DrawingObjectArtillery;
var secondArtillery = second as DrawingObjectArtillery;
if (firstArtillery == null && secondArtillery == null)
{
return 0;
}
if (firstArtillery == null && secondArtillery != null)
{
return 1;
}
if (firstArtillery != null && secondArtillery == null)
{
return -1;
}
int firstColor = firstArtillery.Artillery.Artillery.BodyColor.ToArgb();
int secondColor = secondArtillery.Artillery.Artillery.BodyColor.ToArgb();
if (firstColor != secondColor)
{
return firstColor.CompareTo(secondColor);
}
if (firstArtillery.Artillery.Artillery is EntityAdvancedArtillery firstAdvanced && secondArtillery.Artillery.Artillery is EntityAdvancedArtillery secondAdvanced)
{
firstColor = firstAdvanced.DopColor.ToArgb();
secondColor = secondAdvanced.DopColor.ToArgb();
if (firstColor != secondColor)
{
return firstColor.CompareTo(secondColor);
}
}
var speedCompare = firstArtillery.Artillery.Artillery.Speed.CompareTo(secondArtillery.Artillery.Artillery.Speed);
if (speedCompare != 0)
{
return speedCompare;
}
return firstArtillery.Artillery.Artillery.Weight.CompareTo(secondArtillery.Artillery.Artillery.Weight);
}
}
}

View File

@ -0,0 +1,64 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Artilleries
{
internal class ArtilleryCompareByType : IComparer<IDrawingObject>
{
public int Compare(IDrawingObject? first, IDrawingObject? second)
{
if (first == null && second == null)
{
return 0;
}
if (first == null && second != null)
{
return 1;
}
if (first != null && second == null)
{
return -1;
}
var firstArtillery = first as DrawingObjectArtillery;
var secondArtillery = second as DrawingObjectArtillery;
if (firstArtillery == null && secondArtillery == null)
{
return 0;
}
if (firstArtillery == null && secondArtillery != null)
{
return 1;
}
if (firstArtillery != null && secondArtillery == null)
{
return -1;
}
if (firstArtillery.Artillery.GetType() != secondArtillery.Artillery.GetType())
{
if (firstArtillery.Artillery.GetType().Name == "DrawningArtillery")
{
return -1;
}
return 1;
}
var speedCompare = firstArtillery.Artillery.Artillery.Speed.CompareTo(secondArtillery.Artillery.Artillery.Speed);
if (speedCompare != 0)
{
return speedCompare;
}
return firstArtillery.Artillery.Artillery.Weight.CompareTo(secondArtillery.Artillery.Artillery.Weight);
}
}
}

View File

@ -10,6 +10,8 @@ namespace Artilleries
{
private DrawingArtillery _artillery = null;
public DrawingArtillery Artillery => _artillery;
public DrawingObjectArtillery(DrawingArtillery artillery)
{
_artillery = artillery;
@ -40,5 +42,63 @@ namespace Artilleries
public string GetInfo() => _artillery?.GetDateForSave();
public static IDrawingObject Create(string data) => new DrawingObjectArtillery(data.CreateDrawingArtillery());
public bool Equals(IDrawingObject? other)
{
if (other == null)
{
return false;
}
var otherArtillery = other as DrawingObjectArtillery;
if (otherArtillery == null)
{
return false;
}
var artillery = _artillery.Artillery;
var otherArtilleryArtillery = otherArtillery._artillery.Artillery;
if (artillery.GetType() != otherArtilleryArtillery.GetType())
{
return false;
}
if (artillery.Speed != otherArtilleryArtillery.Speed)
{
return false;
}
if (artillery.Weight != otherArtilleryArtillery.Weight)
{
return false;
}
if (artillery.BodyColor != otherArtilleryArtillery.BodyColor)
{
return false;
}
if (artillery is EntityAdvancedArtillery advanced && otherArtilleryArtillery is EntityAdvancedArtillery otherAdvanced)
{
if (advanced.DopColor != otherAdvanced.DopColor)
{
return false;
}
if (advanced.Weapon != otherAdvanced.Weapon)
{
return false;
}
if (advanced.SalvoBattery != otherAdvanced.SalvoBattery)
{
return false;
}
}
return true;
}
}
}

View File

@ -29,6 +29,8 @@
private void InitializeComponent()
{
this.toolsGroupBox = new System.Windows.Forms.GroupBox();
this.buttonSortByColor = new System.Windows.Forms.Button();
this.buttonSortByType = new System.Windows.Forms.Button();
this.mapsGroupBox = new System.Windows.Forms.GroupBox();
this.buttonDeleteMap = new System.Windows.Forms.Button();
this.listBoxMaps = new System.Windows.Forms.ListBox();
@ -59,6 +61,8 @@
//
// toolsGroupBox
//
this.toolsGroupBox.Controls.Add(this.buttonSortByColor);
this.toolsGroupBox.Controls.Add(this.buttonSortByType);
this.toolsGroupBox.Controls.Add(this.mapsGroupBox);
this.toolsGroupBox.Controls.Add(this.buttonShowOnMap);
this.toolsGroupBox.Controls.Add(this.buttonShowStorage);
@ -72,11 +76,31 @@
this.toolsGroupBox.Dock = System.Windows.Forms.DockStyle.Right;
this.toolsGroupBox.Location = new System.Drawing.Point(600, 24);
this.toolsGroupBox.Name = "toolsGroupBox";
this.toolsGroupBox.Size = new System.Drawing.Size(200, 589);
this.toolsGroupBox.Size = new System.Drawing.Size(200, 657);
this.toolsGroupBox.TabIndex = 0;
this.toolsGroupBox.TabStop = false;
this.toolsGroupBox.Text = "Инструменты";
//
// buttonSortByColor
//
this.buttonSortByColor.Location = new System.Drawing.Point(13, 324);
this.buttonSortByColor.Name = "buttonSortByColor";
this.buttonSortByColor.Size = new System.Drawing.Size(175, 32);
this.buttonSortByColor.TabIndex = 27;
this.buttonSortByColor.Text = "Сортировать по цвету";
this.buttonSortByColor.UseVisualStyleBackColor = true;
this.buttonSortByColor.Click += new System.EventHandler(this.buttonSortByColor_Click);
//
// buttonSortByType
//
this.buttonSortByType.Location = new System.Drawing.Point(13, 286);
this.buttonSortByType.Name = "buttonSortByType";
this.buttonSortByType.Size = new System.Drawing.Size(175, 32);
this.buttonSortByType.TabIndex = 26;
this.buttonSortByType.Text = "Сортировать по типу";
this.buttonSortByType.UseVisualStyleBackColor = true;
this.buttonSortByType.Click += new System.EventHandler(this.buttonSortByType_Click);
//
// mapsGroupBox
//
this.mapsGroupBox.Controls.Add(this.buttonDeleteMap);
@ -142,7 +166,7 @@
//
// buttonShowOnMap
//
this.buttonShowOnMap.Location = new System.Drawing.Point(13, 470);
this.buttonShowOnMap.Location = new System.Drawing.Point(13, 526);
this.buttonShowOnMap.Name = "buttonShowOnMap";
this.buttonShowOnMap.Size = new System.Drawing.Size(175, 32);
this.buttonShowOnMap.TabIndex = 24;
@ -152,7 +176,7 @@
//
// buttonShowStorage
//
this.buttonShowStorage.Location = new System.Drawing.Point(13, 423);
this.buttonShowStorage.Location = new System.Drawing.Point(13, 488);
this.buttonShowStorage.Name = "buttonShowStorage";
this.buttonShowStorage.Size = new System.Drawing.Size(175, 32);
this.buttonShowStorage.TabIndex = 23;
@ -162,7 +186,7 @@
//
// buttonRemoveArtillery
//
this.buttonRemoveArtillery.Location = new System.Drawing.Point(13, 376);
this.buttonRemoveArtillery.Location = new System.Drawing.Point(13, 441);
this.buttonRemoveArtillery.Name = "buttonRemoveArtillery";
this.buttonRemoveArtillery.Size = new System.Drawing.Size(175, 32);
this.buttonRemoveArtillery.TabIndex = 22;
@ -172,7 +196,7 @@
//
// maskedTextBoxPosition
//
this.maskedTextBoxPosition.Location = new System.Drawing.Point(13, 347);
this.maskedTextBoxPosition.Location = new System.Drawing.Point(13, 412);
this.maskedTextBoxPosition.Mask = "00";
this.maskedTextBoxPosition.Name = "maskedTextBoxPosition";
this.maskedTextBoxPosition.Size = new System.Drawing.Size(175, 23);
@ -180,7 +204,7 @@
//
// buttonAddArtillery
//
this.buttonAddArtillery.Location = new System.Drawing.Point(13, 309);
this.buttonAddArtillery.Location = new System.Drawing.Point(13, 374);
this.buttonAddArtillery.Name = "buttonAddArtillery";
this.buttonAddArtillery.Size = new System.Drawing.Size(175, 32);
this.buttonAddArtillery.TabIndex = 20;
@ -193,7 +217,7 @@
this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonRight.BackgroundImage = global::SelfPropelledArtilleryUnit.Properties.Resources.ArrowRight;
this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.buttonRight.Location = new System.Drawing.Point(124, 538);
this.buttonRight.Location = new System.Drawing.Point(124, 606);
this.buttonRight.Name = "buttonRight";
this.buttonRight.Size = new System.Drawing.Size(30, 30);
this.buttonRight.TabIndex = 18;
@ -205,7 +229,7 @@
this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonDown.BackgroundImage = global::SelfPropelledArtilleryUnit.Properties.Resources.ArrowDown;
this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.buttonDown.Location = new System.Drawing.Point(88, 538);
this.buttonDown.Location = new System.Drawing.Point(88, 606);
this.buttonDown.Name = "buttonDown";
this.buttonDown.Size = new System.Drawing.Size(30, 30);
this.buttonDown.TabIndex = 17;
@ -217,7 +241,7 @@
this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonUp.BackgroundImage = global::SelfPropelledArtilleryUnit.Properties.Resources.ArrowUp;
this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.buttonUp.Location = new System.Drawing.Point(88, 505);
this.buttonUp.Location = new System.Drawing.Point(88, 573);
this.buttonUp.Name = "buttonUp";
this.buttonUp.Size = new System.Drawing.Size(30, 30);
this.buttonUp.TabIndex = 16;
@ -229,7 +253,7 @@
this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonLeft.BackgroundImage = global::SelfPropelledArtilleryUnit.Properties.Resources.ArrowLeft;
this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.buttonLeft.Location = new System.Drawing.Point(54, 538);
this.buttonLeft.Location = new System.Drawing.Point(54, 606);
this.buttonLeft.Name = "buttonLeft";
this.buttonLeft.Size = new System.Drawing.Size(30, 30);
this.buttonLeft.TabIndex = 15;
@ -241,7 +265,7 @@
this.pictureBoxArtilleries.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBoxArtilleries.Location = new System.Drawing.Point(0, 24);
this.pictureBoxArtilleries.Name = "pictureBoxArtilleries";
this.pictureBoxArtilleries.Size = new System.Drawing.Size(600, 589);
this.pictureBoxArtilleries.Size = new System.Drawing.Size(600, 657);
this.pictureBoxArtilleries.TabIndex = 1;
this.pictureBoxArtilleries.TabStop = false;
//
@ -291,7 +315,7 @@
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 613);
this.ClientSize = new System.Drawing.Size(800, 681);
this.Controls.Add(this.pictureBoxArtilleries);
this.Controls.Add(this.toolsGroupBox);
this.Controls.Add(this.menuStrip);
@ -335,5 +359,7 @@
private ToolStripMenuItem loadToolStripMenuItem;
private OpenFileDialog loadFileDialog;
private SaveFileDialog saveFileDialog;
private Button buttonSortByColor;
private Button buttonSortByType;
}
}

View File

@ -250,5 +250,25 @@ namespace Artilleries
}
}
}
private void buttonSortByType_Click(object sender, EventArgs e)
{
if (listBoxMaps.SelectedIndex == -1)
{
return;
}
_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].Sort(new ArtilleryCompareByType());
pictureBoxArtilleries.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 ArtilleryCompareByColor());
pictureBoxArtilleries.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
}
}

View File

@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace Artilleries
{
internal interface IDrawingObject
internal interface IDrawingObject : IEquatable<IDrawingObject>
{
public float Step { get; }
void SetObject(int x, int y, int width, int height);

View File

@ -7,7 +7,7 @@ using System.Threading.Tasks;
namespace Artilleries
{
internal class MapWithSetArtilleriesGeneric<T, U>
where T : class, IDrawingObject
where T : class, IDrawingObject, IEquatable<T>
where U : AbstractMap
{
private readonly int _pictureWidth;
@ -142,5 +142,10 @@ namespace Artilleries
_setArtilleries.Insert(DrawingObjectArtillery.Create(record) as T);
}
}
public void Sort(IComparer<T> comparer)
{
_setArtilleries.SortSet(comparer);
}
}
}

View File

@ -7,7 +7,7 @@ using System.Threading.Tasks;
namespace Artilleries
{
internal class SetArtilleriesGeneric<T>
where T : class
where T : class, IEquatable<T>
{
private readonly List<T> _places;
public int Count => _places.Count;
@ -36,6 +36,11 @@ namespace Artilleries
return -1;
}
if (_places.Contains(artillery))
{
return -1;
}
_places.Insert(position, artillery);
return position;
@ -93,5 +98,15 @@ namespace Artilleries
}
}
}
public void SortSet(IComparer<T> comparer)
{
if (comparer == null)
{
return;
}
_places.Sort(comparer);
}
}
}