PIbd-22. Shabunov O.A. Lab work 04 #4

Closed
olshab wants to merge 1 commits from Lab4 into Lab3
5 changed files with 247 additions and 50 deletions

View File

@ -30,12 +30,18 @@
{
CollectionPictureBox = new PictureBox();
ToolGroupBox = new GroupBox();
groupBox1 = new GroupBox();
SetNameTextBox = new TextBox();
StorageListBox = new ListBox();
AddSetButton = new Button();
RemoveSetButton = new Button();
RefreshCollectionButton = new Button();
RemoveBomberButton = new Button();
NumberMaskedTextBox = new MaskedTextBox();
AddBomberButton = new Button();
((System.ComponentModel.ISupportInitialize)CollectionPictureBox).BeginInit();
ToolGroupBox.SuspendLayout();
groupBox1.SuspendLayout();
SuspendLayout();
//
// CollectionPictureBox
@ -50,6 +56,7 @@
// ToolGroupBox
//
ToolGroupBox.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Right;
ToolGroupBox.Controls.Add(groupBox1);
ToolGroupBox.Controls.Add(RefreshCollectionButton);
ToolGroupBox.Controls.Add(RemoveBomberButton);
ToolGroupBox.Controls.Add(NumberMaskedTextBox);
@ -61,9 +68,60 @@
ToolGroupBox.TabStop = false;
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.Location = new Point(6, 190);
RefreshCollectionButton.Location = new Point(6, 543);
RefreshCollectionButton.Name = "RefreshCollectionButton";
RefreshCollectionButton.Size = new Size(164, 30);
RefreshCollectionButton.TabIndex = 3;
@ -73,7 +131,7 @@
//
// RemoveBomberButton
//
RemoveBomberButton.Location = new Point(6, 141);
RemoveBomberButton.Location = new Point(6, 467);
RemoveBomberButton.Name = "RemoveBomberButton";
RemoveBomberButton.Size = new Size(164, 30);
RemoveBomberButton.TabIndex = 2;
@ -83,7 +141,7 @@
//
// NumberMaskedTextBox
//
NumberMaskedTextBox.Location = new Point(6, 102);
NumberMaskedTextBox.Location = new Point(6, 428);
NumberMaskedTextBox.Mask = "00000";
NumberMaskedTextBox.Name = "NumberMaskedTextBox";
NumberMaskedTextBox.Size = new Size(164, 23);
@ -92,7 +150,7 @@
//
// AddBomberButton
//
AddBomberButton.Location = new Point(6, 32);
AddBomberButton.Location = new Point(6, 358);
AddBomberButton.Name = "AddBomberButton";
AddBomberButton.Size = new Size(164, 43);
AddBomberButton.TabIndex = 0;
@ -112,6 +170,8 @@
((System.ComponentModel.ISupportInitialize)CollectionPictureBox).EndInit();
ToolGroupBox.ResumeLayout(false);
ToolGroupBox.PerformLayout();
groupBox1.ResumeLayout(false);
groupBox1.PerformLayout();
ResumeLayout(false);
}
@ -123,5 +183,10 @@
private Button RemoveBomberButton;
private MaskedTextBox NumberMaskedTextBox;
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.MovementStrategy;
using AirBomber.Rendering;
namespace AirBomber
{
public partial class FormEntityCollection : Form
{
private readonly EntitiesGenericCollection<BomberRendererBase, ObjectEntityRenderer> _entities;
private readonly EntitiesGenericStorage _storage;
public FormEntityCollection()
{
InitializeComponent();
_entities = new EntitiesGenericCollection<BomberRendererBase, ObjectEntityRenderer>(
CollectionPictureBox.Width, CollectionPictureBox.Height
);
_storage = new EntitiesGenericStorage(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)
{
if (StorageListBox.SelectedIndex == -1)
return;
var obj = _storage[StorageListBox.SelectedItem.ToString() ?? string.Empty];
if (obj is null)
return;
BomberForm Form = new BomberForm();
if (Form.ShowDialog() == DialogResult.OK)
{
if (_entities + Form.SelectedRenderer != -1)
if (obj + Form.SelectedRenderer != -1)
{
MessageBox.Show("Объект добавлен");
CollectionPictureBox.Image = _entities.ShowEntities();
CollectionPictureBox.Image = obj.ShowEntities();
}
else
{
MessageBox.Show("Не удалось добавить объект");
@ -38,14 +56,21 @@ namespace AirBomber
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)
return;
int Pos = Convert.ToInt32(NumberMaskedTextBox.Text);
if (_entities - Pos == true)
if (obj - Pos == true)
{
MessageBox.Show("Объект удален");
CollectionPictureBox.Image = _entities.ShowEntities();
CollectionPictureBox.Image = obj.ShowEntities();
}
else
{
@ -55,7 +80,48 @@ namespace AirBomber
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)
{
T? obj = collect._collection.Get(pos);
T? obj = collect._collection[pos];
if (obj != null)
return collect._collection.Remove(pos);
@ -44,7 +44,7 @@ namespace AirBomber.Generics
public U? GetU(int pos)
{
return (U?)_collection.Get(pos)?.MovableObject;
return (U?)_collection[pos]?.MovableObject;
}
public Bitmap ShowEntities()
@ -81,10 +81,10 @@ namespace AirBomber.Generics
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)
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
{
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)
{
_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)
{
for (int i = 0; i < Count; i++)
/** Вставка в начало набора */
for (int i = 0; i < _maxCount; i++)
if (_objects[i] is null)
{
_objects[i] = Entity;
@ -25,7 +32,7 @@
public int Insert(T Entity, int Position)
{
if (Position >= Count)
if (Position >= _maxCount)
return -1;
if (_objects[Position] is null)
@ -34,42 +41,48 @@
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;
/** Сдвиг */
for (int i = EmptyPos; i > Position; i--)
_objects[i] = _objects[i - 1];
_objects[Position] = Entity;
return Position;
return -1;
}
public bool Remove(int Position)
{
if (Position >= Count)
if (Position >= _maxCount)
return false;
_objects[Position] = default(T);
return true;
}
public T? Get(int Position)
public T? this[int Position]
{
if (Position >= Count)
return default(T);
get
{
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;
}
}
}
}