Лабораторная №4 готова

This commit is contained in:
ShabOl 2023-11-19 17:56:27 +04:00
parent 0922c3809f
commit 5510581b53
5 changed files with 247 additions and 50 deletions

View File

@ -30,12 +30,18 @@
{ {
CollectionPictureBox = new PictureBox(); CollectionPictureBox = new PictureBox();
ToolGroupBox = new GroupBox(); ToolGroupBox = new GroupBox();
groupBox1 = new GroupBox();
SetNameTextBox = new TextBox();
StorageListBox = new ListBox();
AddSetButton = new Button();
RemoveSetButton = new Button();
RefreshCollectionButton = new Button(); RefreshCollectionButton = new Button();
RemoveBomberButton = new Button(); RemoveBomberButton = new Button();
NumberMaskedTextBox = new MaskedTextBox(); NumberMaskedTextBox = new MaskedTextBox();
AddBomberButton = new Button(); AddBomberButton = new Button();
((System.ComponentModel.ISupportInitialize)CollectionPictureBox).BeginInit(); ((System.ComponentModel.ISupportInitialize)CollectionPictureBox).BeginInit();
ToolGroupBox.SuspendLayout(); ToolGroupBox.SuspendLayout();
groupBox1.SuspendLayout();
SuspendLayout(); SuspendLayout();
// //
// CollectionPictureBox // CollectionPictureBox
@ -50,6 +56,7 @@
// ToolGroupBox // ToolGroupBox
// //
ToolGroupBox.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Right; ToolGroupBox.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Right;
ToolGroupBox.Controls.Add(groupBox1);
ToolGroupBox.Controls.Add(RefreshCollectionButton); ToolGroupBox.Controls.Add(RefreshCollectionButton);
ToolGroupBox.Controls.Add(RemoveBomberButton); ToolGroupBox.Controls.Add(RemoveBomberButton);
ToolGroupBox.Controls.Add(NumberMaskedTextBox); ToolGroupBox.Controls.Add(NumberMaskedTextBox);
@ -61,9 +68,60 @@
ToolGroupBox.TabStop = false; ToolGroupBox.TabStop = false;
ToolGroupBox.Text = "Инструменты"; ToolGroupBox.Text = "Инструменты";
// //
// groupBox1
//
groupBox1.Controls.Add(SetNameTextBox);
groupBox1.Controls.Add(StorageListBox);
groupBox1.Controls.Add(AddSetButton);
groupBox1.Controls.Add(RemoveSetButton);
groupBox1.Location = new Point(6, 22);
groupBox1.Name = "groupBox1";
groupBox1.Size = new Size(164, 232);
groupBox1.TabIndex = 8;
groupBox1.TabStop = false;
groupBox1.Text = "Наборы";
//
// SetNameTextBox
//
SetNameTextBox.Location = new Point(6, 22);
SetNameTextBox.Name = "SetNameTextBox";
SetNameTextBox.Size = new Size(152, 23);
SetNameTextBox.TabIndex = 4;
//
// StorageListBox
//
StorageListBox.FormattingEnabled = true;
StorageListBox.ItemHeight = 15;
StorageListBox.Items.AddRange(new object[] { "Наборы здесь..." });
StorageListBox.Location = new Point(6, 96);
StorageListBox.Name = "StorageListBox";
StorageListBox.Size = new Size(152, 79);
StorageListBox.TabIndex = 7;
StorageListBox.SelectedIndexChanged += StorageListBoxIndexChanged;
//
// AddSetButton
//
AddSetButton.Location = new Point(6, 51);
AddSetButton.Name = "AddSetButton";
AddSetButton.Size = new Size(152, 30);
AddSetButton.TabIndex = 5;
AddSetButton.Text = "Добавить набор";
AddSetButton.UseVisualStyleBackColor = true;
AddSetButton.Click += ButtonAddSet_Click;
//
// RemoveSetButton
//
RemoveSetButton.Location = new Point(6, 190);
RemoveSetButton.Name = "RemoveSetButton";
RemoveSetButton.Size = new Size(152, 30);
RemoveSetButton.TabIndex = 6;
RemoveSetButton.Text = "Удалить набор";
RemoveSetButton.UseVisualStyleBackColor = true;
RemoveSetButton.Click += ButtonRemoveSet_Click;
//
// RefreshCollectionButton // RefreshCollectionButton
// //
RefreshCollectionButton.Location = new Point(6, 190); RefreshCollectionButton.Location = new Point(6, 543);
RefreshCollectionButton.Name = "RefreshCollectionButton"; RefreshCollectionButton.Name = "RefreshCollectionButton";
RefreshCollectionButton.Size = new Size(164, 30); RefreshCollectionButton.Size = new Size(164, 30);
RefreshCollectionButton.TabIndex = 3; RefreshCollectionButton.TabIndex = 3;
@ -73,7 +131,7 @@
// //
// RemoveBomberButton // RemoveBomberButton
// //
RemoveBomberButton.Location = new Point(6, 141); RemoveBomberButton.Location = new Point(6, 467);
RemoveBomberButton.Name = "RemoveBomberButton"; RemoveBomberButton.Name = "RemoveBomberButton";
RemoveBomberButton.Size = new Size(164, 30); RemoveBomberButton.Size = new Size(164, 30);
RemoveBomberButton.TabIndex = 2; RemoveBomberButton.TabIndex = 2;
@ -83,7 +141,7 @@
// //
// NumberMaskedTextBox // NumberMaskedTextBox
// //
NumberMaskedTextBox.Location = new Point(6, 102); NumberMaskedTextBox.Location = new Point(6, 428);
NumberMaskedTextBox.Mask = "00000"; NumberMaskedTextBox.Mask = "00000";
NumberMaskedTextBox.Name = "NumberMaskedTextBox"; NumberMaskedTextBox.Name = "NumberMaskedTextBox";
NumberMaskedTextBox.Size = new Size(164, 23); NumberMaskedTextBox.Size = new Size(164, 23);
@ -92,7 +150,7 @@
// //
// AddBomberButton // AddBomberButton
// //
AddBomberButton.Location = new Point(6, 32); AddBomberButton.Location = new Point(6, 358);
AddBomberButton.Name = "AddBomberButton"; AddBomberButton.Name = "AddBomberButton";
AddBomberButton.Size = new Size(164, 43); AddBomberButton.Size = new Size(164, 43);
AddBomberButton.TabIndex = 0; AddBomberButton.TabIndex = 0;
@ -112,6 +170,8 @@
((System.ComponentModel.ISupportInitialize)CollectionPictureBox).EndInit(); ((System.ComponentModel.ISupportInitialize)CollectionPictureBox).EndInit();
ToolGroupBox.ResumeLayout(false); ToolGroupBox.ResumeLayout(false);
ToolGroupBox.PerformLayout(); ToolGroupBox.PerformLayout();
groupBox1.ResumeLayout(false);
groupBox1.PerformLayout();
ResumeLayout(false); ResumeLayout(false);
} }
@ -123,5 +183,10 @@
private Button RemoveBomberButton; private Button RemoveBomberButton;
private MaskedTextBox NumberMaskedTextBox; private MaskedTextBox NumberMaskedTextBox;
private Button AddBomberButton; private Button AddBomberButton;
private GroupBox groupBox1;
private TextBox SetNameTextBox;
private ListBox StorageListBox;
private Button AddSetButton;
private Button RemoveSetButton;
} }
} }

View File

@ -1,34 +1,52 @@
using AirBomber.Generics; using AirBomber.Generics;
using AirBomber.MovementStrategy;
using AirBomber.Rendering;
namespace AirBomber namespace AirBomber
{ {
public partial class FormEntityCollection : Form public partial class FormEntityCollection : Form
{ {
private readonly EntitiesGenericCollection<BomberRendererBase, ObjectEntityRenderer> _entities; private readonly EntitiesGenericStorage _storage;
public FormEntityCollection() public FormEntityCollection()
{ {
InitializeComponent(); InitializeComponent();
_entities = new EntitiesGenericCollection<BomberRendererBase, ObjectEntityRenderer>( _storage = new EntitiesGenericStorage(CollectionPictureBox.Width, CollectionPictureBox.Height);
CollectionPictureBox.Width, CollectionPictureBox.Height }
);
private void ReloadObjects()
{
int index = StorageListBox.SelectedIndex;
StorageListBox.Items.Clear();
for (int i = 0; i < _storage.Keys.Count; i++)
StorageListBox.Items.Add(_storage.Keys[i]);
if (StorageListBox.Items.Count > 0 && (index == -1 || index >= StorageListBox.Items.Count))
StorageListBox.SelectedIndex = 0;
else if (StorageListBox.Items.Count > 0 && index > -1 && index < StorageListBox.Items.Count)
StorageListBox.SelectedIndex = index;
} }
public void ButtonAddEntity_Click(object sender, EventArgs e) public void ButtonAddEntity_Click(object sender, EventArgs e)
{ {
if (StorageListBox.SelectedIndex == -1)
return;
var obj = _storage[StorageListBox.SelectedItem.ToString() ?? string.Empty];
if (obj is null)
return;
BomberForm Form = new BomberForm(); BomberForm Form = new BomberForm();
if (Form.ShowDialog() == DialogResult.OK) if (Form.ShowDialog() == DialogResult.OK)
{ {
if (_entities + Form.SelectedRenderer != -1) if (obj + Form.SelectedRenderer != -1)
{ {
MessageBox.Show("Объект добавлен"); MessageBox.Show("Объект добавлен");
CollectionPictureBox.Image = _entities.ShowEntities(); CollectionPictureBox.Image = obj.ShowEntities();
} }
else else
{ {
MessageBox.Show("Не удалось добавить объект"); MessageBox.Show("Не удалось добавить объект");
@ -38,14 +56,21 @@ namespace AirBomber
public void ButtonRemoveEntity_Click(object sender, EventArgs e) public void ButtonRemoveEntity_Click(object sender, EventArgs e)
{ {
if (StorageListBox.SelectedIndex == -1)
return;
var obj = _storage[StorageListBox.SelectedItem.ToString() ?? string.Empty];
if (obj is null)
return;
if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
return; return;
int Pos = Convert.ToInt32(NumberMaskedTextBox.Text); int Pos = Convert.ToInt32(NumberMaskedTextBox.Text);
if (_entities - Pos == true) if (obj - Pos == true)
{ {
MessageBox.Show("Объект удален"); MessageBox.Show("Объект удален");
CollectionPictureBox.Image = _entities.ShowEntities(); CollectionPictureBox.Image = obj.ShowEntities();
} }
else else
{ {
@ -55,7 +80,48 @@ namespace AirBomber
public void ButtonRefreshCollection_Click(object sender, EventArgs e) public void ButtonRefreshCollection_Click(object sender, EventArgs e)
{ {
CollectionPictureBox.Image = _entities.ShowEntities(); if (StorageListBox.SelectedIndex == -1)
return;
var obj = _storage[StorageListBox.SelectedItem.ToString() ?? string.Empty];
if (obj is null)
return;
CollectionPictureBox.Image = obj.ShowEntities();
}
private void ButtonAddSet_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(SetNameTextBox.Text))
{
MessageBox.Show("Не все данные заполнены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
_storage.AddSet(SetNameTextBox.Text);
ReloadObjects();
}
private void ButtonRemoveSet_Click(object sender, EventArgs e)
{
if (StorageListBox.SelectedIndex == -1)
return;
if (MessageBox.Show(
$"Удалить объект{StorageListBox.SelectedItem}?",
"Удаление",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question
) == DialogResult.Yes)
{
_storage.RemoveSet(StorageListBox.SelectedItem.ToString() ?? string.Empty);
ReloadObjects();
}
}
private void StorageListBoxIndexChanged(object sender, EventArgs e)
{
CollectionPictureBox.Image = _storage[StorageListBox.SelectedItem?.ToString() ?? string.Empty]?.ShowEntities();
} }
} }
} }

View File

@ -34,7 +34,7 @@ namespace AirBomber.Generics
public static bool operator -(EntitiesGenericCollection<T, U> collect, int pos) public static bool operator -(EntitiesGenericCollection<T, U> collect, int pos)
{ {
T? obj = collect._collection.Get(pos); T? obj = collect._collection[pos];
if (obj != null) if (obj != null)
return collect._collection.Remove(pos); return collect._collection.Remove(pos);
@ -44,7 +44,7 @@ namespace AirBomber.Generics
public U? GetU(int pos) public U? GetU(int pos)
{ {
return (U?)_collection.Get(pos)?.MovableObject; return (U?)_collection[pos]?.MovableObject;
} }
public Bitmap ShowEntities() public Bitmap ShowEntities()
@ -81,10 +81,10 @@ namespace AirBomber.Generics
private void DrawObjects(Graphics g) private void DrawObjects(Graphics g)
{ {
for (int i = 0; i < _collection.Count; i++) int i = -1;
foreach (T? Entity in _collection.GetEntities())
{ {
T? Entity = _collection.Get(i); i++;
if (Entity is null) if (Entity is null)
continue; continue;

View File

@ -0,0 +1,53 @@
using AirBomber.MovementStrategy;
using AirBomber.Rendering;
namespace AirBomber.Generics
{
internal class EntitiesGenericStorage
{
readonly Dictionary<string, EntitiesGenericCollection<BomberRendererBase, ObjectEntityRenderer>> _entityStorages;
public List<string> Keys => _entityStorages.Keys.ToList();
private readonly int _pictureWidth;
private readonly int _pictureHeight;
public EntitiesGenericStorage(int PictureWidth, int PictureHeight)
{
_entityStorages = new Dictionary<string, EntitiesGenericCollection<BomberRendererBase, ObjectEntityRenderer>>();
_pictureWidth = PictureWidth;
_pictureHeight = PictureHeight;
}
public void AddSet(string Name)
{
var NewCollection =
new EntitiesGenericCollection<BomberRendererBase, ObjectEntityRenderer>(_pictureWidth, _pictureHeight);
if (Keys.Contains(Name))
{
MessageBox.Show("Набор с таким именем уже существует", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
_entityStorages.Add(Name, NewCollection);
}
public void RemoveSet(string Name)
{
_entityStorages.Remove(Name);
}
public EntitiesGenericCollection<BomberRendererBase, ObjectEntityRenderer>? this[string Index]
{
get
{
if (!_entityStorages.ContainsKey(Index))
return null;
return _entityStorages[Index];
}
}
}
}

View File

@ -1,19 +1,26 @@
namespace AirBomber.Generics namespace AirBomber.Generics
{ {
internal class SetGeneric<T> internal class SetGeneric<T>
where T : class
{ {
private readonly T?[] _objects; private readonly List<T?> _objects;
private readonly int _maxCount;
public int Count => _objects.Length; public int Count => _objects.Count;
public SetGeneric(int Count) public SetGeneric(int Count)
{ {
_objects = new T?[Count]; _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)
{ {
for (int i = 0; i < Count; i++) /** Вставка в начало набора */
for (int i = 0; i < _maxCount; i++)
if (_objects[i] is null) if (_objects[i] is null)
{ {
_objects[i] = Entity; _objects[i] = Entity;
@ -25,7 +32,7 @@
public int Insert(T Entity, int Position) public int Insert(T Entity, int Position)
{ {
if (Position >= Count) if (Position >= _maxCount)
return -1; return -1;
if (_objects[Position] is null) if (_objects[Position] is null)
@ -34,42 +41,48 @@
return Position; return Position;
} }
/** Сдвиг элементов вправо начиная с Position до ближайшего пустого места */
int EmptyPos = -1;
for (int i = Position + 1; i < Count; i++)
if (_objects[i] is null)
{
EmptyPos = i;
break;
}
if (EmptyPos == -1)
return -1; return -1;
/** Сдвиг */
for (int i = EmptyPos; i > Position; i--)
_objects[i] = _objects[i - 1];
_objects[Position] = Entity;
return Position;
} }
public bool Remove(int Position) public bool Remove(int Position)
{ {
if (Position >= Count) if (Position >= _maxCount)
return false; return false;
_objects[Position] = default(T); _objects[Position] = default(T);
return true; return true;
} }
public T? Get(int Position) public T? this[int Position]
{ {
if (Position >= Count) get
return default(T); {
if (Position >= _maxCount)
return null;
return _objects[Position]; return _objects[Position];
} }
set
{
if (Position >= _maxCount)
return;
if (_objects[Position] is not null)
return;
_objects[Position] = value;
}
}
public IEnumerable<T?> GetEntities(int? MaxEntities = null)
{
for (int i = 0; i < _objects.Count; ++i)
{
yield return _objects[i];
if (MaxEntities.HasValue && i == MaxEntities.Value)
yield break;
}
}
} }
} }