using ProjectTank.DrawningObjects;
using ProjectTank.Generics;
using ProjectTank.MovementStrategy;
namespace ProjectTank
{
public partial class FormTankCollection : Form
{
private readonly TanksGenericStorage _storage;
///
/// Конструктор
///
public FormTankCollection()
{
InitializeComponent();
_storage = new TanksGenericStorage(pictureBoxCollection.Width,
pictureBoxCollection.Height);
}
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;
}
}
private void ButtonAddObject_Click(object sender, EventArgs e)
{
string objName = textBoxStorageName.Text;
if (string.IsNullOrEmpty(objName))
{
MessageBox.Show("Не все данные заполнены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
_storage.AddSet(textBoxStorageName.Text);
ReloadObjects();
}
private void ListBoxObjects_SelectedIndexChanged(object sender, EventArgs e)
{
pictureBoxCollection.Image = _storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowTanks();
}
///
/// Удаление набора
///
///
///
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();
}
}
///
/// Добавление объекта в набор
///
///
///
private void ButtonAddTank_Click(object sender, EventArgs e)
{
if (listBoxStorages.SelectedIndex == -1) return;
var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
if (obj == null) return;
FormTank form = new();
if (form.ShowDialog() == DialogResult.OK)
{
if (obj + form.SelectedTank)
{
MessageBox.Show("Объект добавлен");
pictureBoxCollection.Image = obj.ShowTanks();
}
else MessageBox.Show("Не удалось добавить объект");
}
}
///
/// Удаление объекта из набора
///
///
///
private void ButtonRemoveTank_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 (obj - pos != null)
{
MessageBox.Show("Объект удален");
pictureBoxCollection.Image = obj.ShowTanks();
}
else MessageBox.Show("Не удалось удалить объект");
}
///
/// Обновление рисунка по набору
///
///
///
private void ButtonRefreshCollection_Click(object sender, EventArgs e)
{
if (listBoxStorages.SelectedIndex == -1) return;
var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
if (obj == null) return;
pictureBoxCollection.Image = obj.ShowTanks();
}
}
}