Lab4 Done

This commit is contained in:
Артём Яшин 2023-10-21 17:01:06 +04:00
parent 80868253a2
commit f13ef15b80
6 changed files with 288 additions and 64 deletions

View File

@ -30,12 +30,18 @@
{
pictureBoxCollection = new PictureBox();
groupBox1 = new GroupBox();
Sets = new GroupBox();
buttonDelObject = new Button();
buttonAddObject = new Button();
listBoxStorages = new ListBox();
textBoxStorageName = new TextBox();
ButtonRefreshCollection = new Button();
ButtonRemovePlane = new Button();
maskedTextBoxNumber = new MaskedTextBox();
ButtonAddPlane = new Button();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit();
groupBox1.SuspendLayout();
Sets.SuspendLayout();
SuspendLayout();
//
// pictureBoxCollection
@ -50,6 +56,7 @@
// groupBox1
//
groupBox1.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Right;
groupBox1.Controls.Add(Sets);
groupBox1.Controls.Add(ButtonRefreshCollection);
groupBox1.Controls.Add(ButtonRemovePlane);
groupBox1.Controls.Add(maskedTextBoxNumber);
@ -61,9 +68,59 @@
groupBox1.TabStop = false;
groupBox1.Text = "Инструменты";
//
// Sets
//
Sets.Controls.Add(buttonDelObject);
Sets.Controls.Add(buttonAddObject);
Sets.Controls.Add(listBoxStorages);
Sets.Controls.Add(textBoxStorageName);
Sets.Location = new Point(6, 31);
Sets.Name = "Sets";
Sets.Size = new Size(201, 290);
Sets.TabIndex = 4;
Sets.TabStop = false;
Sets.Text = "Наборы";
//
// buttonDelObject
//
buttonDelObject.Location = new Point(9, 239);
buttonDelObject.Name = "buttonDelObject";
buttonDelObject.Size = new Size(186, 35);
buttonDelObject.TabIndex = 3;
buttonDelObject.Text = "Удалить набор";
buttonDelObject.UseVisualStyleBackColor = true;
buttonDelObject.Click += ButtonDelObject_Click;
//
// buttonAddObject
//
buttonAddObject.Location = new Point(6, 82);
buttonAddObject.Name = "buttonAddObject";
buttonAddObject.Size = new Size(186, 35);
buttonAddObject.TabIndex = 2;
buttonAddObject.Text = "Добавить набор";
buttonAddObject.UseVisualStyleBackColor = true;
buttonAddObject.Click += ButtonAddObject_Click;
//
// listBoxStorages
//
listBoxStorages.FormattingEnabled = true;
listBoxStorages.ItemHeight = 20;
listBoxStorages.Location = new Point(9, 137);
listBoxStorages.Name = "listBoxStorages";
listBoxStorages.Size = new Size(185, 84);
listBoxStorages.TabIndex = 1;
listBoxStorages.SelectedIndexChanged += ListBoxObjects_SelectedIndexChanged;
//
// textBoxStorageName
//
textBoxStorageName.Location = new Point(5, 30);
textBoxStorageName.Name = "textBoxStorageName";
textBoxStorageName.Size = new Size(190, 27);
textBoxStorageName.TabIndex = 0;
//
// ButtonRefreshCollection
//
ButtonRefreshCollection.Location = new Point(6, 269);
ButtonRefreshCollection.Location = new Point(11, 581);
ButtonRefreshCollection.Name = "ButtonRefreshCollection";
ButtonRefreshCollection.Size = new Size(190, 50);
ButtonRefreshCollection.TabIndex = 3;
@ -73,7 +130,7 @@
//
// ButtonRemovePlane
//
ButtonRemovePlane.Location = new Point(6, 213);
ButtonRemovePlane.Location = new Point(11, 525);
ButtonRemovePlane.Name = "ButtonRemovePlane";
ButtonRemovePlane.Size = new Size(190, 50);
ButtonRemovePlane.TabIndex = 2;
@ -83,14 +140,14 @@
//
// maskedTextBoxNumber
//
maskedTextBoxNumber.Location = new Point(23, 123);
maskedTextBoxNumber.Location = new Point(28, 435);
maskedTextBoxNumber.Name = "maskedTextBoxNumber";
maskedTextBoxNumber.Size = new Size(162, 27);
maskedTextBoxNumber.TabIndex = 1;
//
// ButtonAddPlane
//
ButtonAddPlane.Location = new Point(6, 26);
ButtonAddPlane.Location = new Point(11, 338);
ButtonAddPlane.Name = "ButtonAddPlane";
ButtonAddPlane.Size = new Size(190, 50);
ButtonAddPlane.TabIndex = 0;
@ -110,6 +167,8 @@
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit();
groupBox1.ResumeLayout(false);
groupBox1.PerformLayout();
Sets.ResumeLayout(false);
Sets.PerformLayout();
ResumeLayout(false);
}
@ -121,5 +180,10 @@
private Button ButtonRemovePlane;
private MaskedTextBox maskedTextBoxNumber;
private Button ButtonAddPlane;
private GroupBox Sets;
private ListBox listBoxStorages;
private TextBox textBoxStorageName;
private Button buttonDelObject;
private Button buttonAddObject;
}
}

View File

@ -18,17 +18,83 @@ namespace ProjectAirBomber
/// <summary>
/// Набор объектов
/// </summary>
private readonly PlanesGenericCollection<DrawingPlane,
DrawingObjectPlane> _planes;
private readonly PlanesGenericStorage _storage;
/// <summary>
/// Конструктор
/// </summary>
public FormPlaneCollection()
{
InitializeComponent();
_planes = new PlanesGenericCollection<DrawingPlane,
DrawingObjectPlane>(pictureBoxCollection.Width, pictureBoxCollection.Height);
_storage = new PlanesGenericStorage(pictureBoxCollection.Width,
pictureBoxCollection.Height);
}
/// <summary>
/// Заполнение listBoxObjects
/// </summary>
private void ReloadObjects()
{
int index = listBoxStorages.SelectedIndex;
listBoxStorages.Items.Clear();
for (int i = 0; i < _storage.Keys.Count; i++)
{
listBoxStorages.Items.Add(_storage.Keys[i]);
}
if (listBoxStorages.Items.Count > 0 && (index == -1 || index
>= listBoxStorages.Items.Count))
{
listBoxStorages.SelectedIndex = 0;
}
else if (listBoxStorages.Items.Count > 0 && index > -1 &&
index < listBoxStorages.Items.Count)
{
listBoxStorages.SelectedIndex = index;
}
}
/// <summary>
/// Добавление набора в коллекцию
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonAddObject_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBoxStorageName.Text))
{
MessageBox.Show("Не все данные заполнены", "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
_storage.AddSet(textBoxStorageName.Text);
ReloadObjects();
}
/// <summary>
/// Выбор набора
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ListBoxObjects_SelectedIndexChanged(object sender,
EventArgs e)
{
pictureBoxCollection.Image =
_storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowPlanes();
}
/// <summary>
/// Удаление набора
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonDelObject_Click(object sender, EventArgs e)
{
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
if (MessageBox.Show($"Удалить объект {listBoxStorages.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
_storage.DelSet(listBoxStorages.SelectedItem.ToString() ?? string.Empty);
ReloadObjects();
}
}
/// <summary>
/// Добавление объекта в набор
/// </summary>
@ -36,13 +102,22 @@ namespace ProjectAirBomber
/// <param name="e"></param>
private void ButtonAddPlane_Click(object sender, EventArgs e)
{
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
if (obj == null)
{
return;
}
FormAirBomber form = new();
if (form.ShowDialog() == DialogResult.OK)
{
if (_planes + form.SelectedPlane != -1)
if (obj + form.SelectedPlane)
{
MessageBox.Show("Объект добавлен");
pictureBoxCollection.Image = _planes.ShowPlanes();
pictureBoxCollection.Image = obj.ShowPlanes();
}
else
{
@ -57,21 +132,32 @@ namespace ProjectAirBomber
/// <param name="e"></param>
private void ButtonRemovePlane_Click(object sender, EventArgs e)
{
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
if (MessageBox.Show("Удалить объект?", "Удаление",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
return;
}
int pos = Convert.ToInt32(maskedTextBoxNumber.Text);
if (_planes - pos)
if (obj - pos != null)
{
MessageBox.Show("Объект удален");
pictureBoxCollection.Image = _planes.ShowPlanes();
pictureBoxCollection.Image = obj.ShowPlanes();
}
else
{
MessageBox.Show("Не удалось удалить объект");
}
}
/// <summary>
/// Обновление рисунка по набору
@ -80,8 +166,18 @@ namespace ProjectAirBomber
/// <param name="e"></param>
private void ButtonRefreshCollection_Click(object sender, EventArgs e)
{
pictureBoxCollection.Image = _planes.ShowPlanes();
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
pictureBoxCollection.Image = obj.ShowPlanes();
}
}
}

View File

@ -53,10 +53,10 @@ namespace ProjectAirBomber.Generics
/// <param name="collect"></param>
/// <param name="obj"></param>
/// <returns></returns>
public static int operator +(PlanesGenericCollection<T, U>? collect, T? obj)
public static bool operator +(PlanesGenericCollection<T, U>? collect, T? obj)
{
if (obj == null || collect == null)
return -1;
return false;
return collect._collection.Insert(obj);
}
/// <summary>
@ -65,12 +65,12 @@ namespace ProjectAirBomber.Generics
/// <param name="collect"></param>
/// <param name="pos"></param>
/// <returns></returns>
public static bool operator -(PlanesGenericCollection<T, U> collect, int pos)
public static T? operator -(PlanesGenericCollection<T, U> collect, int pos)
{
T? obj = collect?._collection.Get(pos);
if (obj == null || collect == null)
return false;
return collect._collection.Remove(pos);
T? obj = collect._collection[pos];
if (obj != null)
collect._collection.Remove(pos);
return obj;
}
/// <summary>
/// Получение объекта IMoveableObject
@ -79,7 +79,7 @@ namespace ProjectAirBomber.Generics
/// <returns></returns>
public U? GetU(int pos)
{
return (U?)_collection.Get(pos)?.GetMoveableObject;
return (U?)_collection[pos]?.GetMoveableObject;
}
/// <summary>
/// Вывод всего набора объектов
@ -119,16 +119,16 @@ namespace ProjectAirBomber.Generics
/// <param name="g"></param>
private void DrawObjects(Graphics g)
{
for (int i = 0; i < _collection.Count; i++)
int i = 0;
foreach(var plane in _collection.GetPlanes())
{
DrawingPlane? plane = _collection.Get(i);
if (plane != null)
{
int inRow = _pictureWidth / _placeSizeWidth;
plane.SetPosition(_pictureWidth - _placeSizeWidth - (i % inRow * _placeSizeWidth) - _placeSizeHeight / 2 - 8, i / inRow * _placeSizeHeight + 20);
plane.DrawTransport(g);
}
i++;
}
}
}

View File

@ -0,0 +1,51 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ProjectAirBomber.DrawingObjects;
using ProjectAirBomber.MovementStrategy;
namespace ProjectAirBomber.Generics
{
internal class PlanesGenericStorage
{
readonly Dictionary<string, PlanesGenericCollection<DrawingPlane,
DrawingObjectPlane>> _planeStorage;
public List<string> Keys => _planeStorage.Keys.ToList();
private readonly int _pictureWidth;
private readonly int _pictureHeight;
public PlanesGenericStorage(int pictureWidth, int pictureHeight)
{
_planeStorage = new Dictionary<string, PlanesGenericCollection<DrawingPlane,
DrawingObjectPlane>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
public void AddSet(string name)
{
_planeStorage.Add(name,
new PlanesGenericCollection<DrawingPlane,
DrawingObjectPlane>(_pictureWidth, _pictureHeight));
}
public void DelSet(string name)
{
if (!_planeStorage.ContainsKey(name))
return;
_planeStorage.Remove(name);
}
public PlanesGenericCollection<DrawingPlane, DrawingObjectPlane>?this[string ind]
{
get
{
if(_planeStorage.ContainsKey(ind))
return _planeStorage[ind];
return null;
}
}
}
}

View File

@ -118,16 +118,16 @@
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<assembly alias="System.Windows.Forms" name="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<data name="Left" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\Left.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
<data name="Up" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\Up.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
<data name="Down" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\Down.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
<data name="Left" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\Left.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
<data name="Right" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\Right.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
<data name="Up" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\Up.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
</root>

View File

@ -4,58 +4,71 @@ using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Schema;
namespace ProjectAirBomber.Generics
{
internal class SetGeneric<T>
where T: class
{
private readonly T?[] _places;
private readonly List<T?> _places;
public int Count => _places.Length;
public int Count => _places.Count;
private readonly int _maxCount;
public SetGeneric(int count)
{
_places = new T?[count];
_maxCount = count;
_places = new List<T?>(count);
}
public int Insert(T plane)
public bool Insert(T plane)
{
if (_places[Count - 1] != null)
return -1;
return Insert(plane, 0);
}
public int Insert(T plane, int position)
{
if (!(position >= 0 && position < Count))
return -1;
if (_places[position] != null)
{
int ind = position;
while (ind < Count && _places[ind] != null)
ind++;
if (ind == Count)
return -1;
for (int i = ind - 1; i >= position; i--)
_places[i + 1] = _places[i];
}
_places[position] = plane;
return position;
}
public bool Remove(int position)
{
if (!(position >= 0 && position < Count) || _places[position] == null)
if (_places.Count == _maxCount)
return false;
_places[position] = null;
Insert(plane, 0);
return true;
}
public T? Get(int position)
public bool Insert(T plane, int position)
{
if (!(position >= 0 && position <= Count && _places.Count < _maxCount))
return false;
_places.Insert(position, plane);
return true;
}
public bool Remove(int position)
{
if (!(position >= 0 && position < Count))
return null;
return _places[position];
return false;
_places.RemoveAt(position);
return true;
}
public T? this[int position]
{
get
{
if (!(position >= 0 && position <= Count))
return null;
return _places[position];
}
set
{
if (!(position >= 0 && position <= Count))
return;
_places.Insert(position, value);
}
}
public IEnumerable<T?> GetPlanes(int? maxPlanes = null)
{
for (int i = 0; i < _places.Count; ++i)
{
yield return _places[i];
if (maxPlanes.HasValue && i == maxPlanes.Value)
yield break;
}
}
}
}