284 lines
8.6 KiB
C#
284 lines
8.6 KiB
C#
using ProjectSeaplane.CollectionGenericObjects;
|
||
using ProjectSeaplane.Drawnings;
|
||
namespace ProjectSeaplane;
|
||
|
||
/// <summary>
|
||
/// Форма работы с компанией и ее коллекцией
|
||
/// </summary>
|
||
public partial class FormPlaneCollection : Form
|
||
{
|
||
|
||
/// <summary>
|
||
/// Хранилище коллекций
|
||
/// </summary>
|
||
private readonly StorageCollection<DrawingBasicSeaplane> _storageCollection;
|
||
/// <summary>
|
||
/// Компания
|
||
/// </summary>
|
||
AbstractCompany? _company = null;
|
||
|
||
/// <summary>
|
||
/// Конструктор
|
||
/// </summary>
|
||
public FormPlaneCollection()
|
||
{
|
||
InitializeComponent();
|
||
_storageCollection = new();
|
||
}
|
||
|
||
/// <summary>
|
||
/// Выбор компании
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void СomboBoxSelectorCompany_SelectedIndexChanged(object sender, EventArgs e)
|
||
{
|
||
panelCompanyTools.Enabled = false;
|
||
|
||
}
|
||
|
||
/// <summary>
|
||
/// Добавление обычного самолета
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void ButtonAddBasicSeaplane_Click(object sender, EventArgs e) => CreateObject(nameof(DrawingBasicSeaplane));
|
||
|
||
/// <summary>
|
||
/// Добавление гидросамолета
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void ButtonAddSeaplane_Click(object sender, EventArgs e) => CreateObject(nameof(DrawingSeaplane));
|
||
|
||
/// <summary>
|
||
/// Создание объекта класса-перемещения
|
||
/// </summary>
|
||
/// <param name="type">Тип создаваемого объекта</param>
|
||
private void CreateObject(string type)
|
||
{
|
||
if (_company == null)
|
||
{
|
||
return;
|
||
}
|
||
|
||
Random random = new();
|
||
DrawingBasicSeaplane drawingBasicSeaplane;
|
||
switch (type)
|
||
{
|
||
case nameof(DrawingBasicSeaplane):
|
||
drawingBasicSeaplane = new DrawingBasicSeaplane(random.Next(100, 300), random.Next(1000, 3000), GetColor(random));
|
||
break;
|
||
case nameof(DrawingSeaplane):
|
||
// TODO вызов диалогового окна для выбора цвета
|
||
drawingBasicSeaplane = new DrawingSeaplane(random.Next(100, 300), random.Next(1000, 3000),
|
||
GetColor(random), GetColor(random),
|
||
Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)));
|
||
break;
|
||
default:
|
||
return;
|
||
}
|
||
|
||
if (_company + drawingBasicSeaplane != -1)
|
||
{
|
||
MessageBox.Show("Объект добавлен");
|
||
pictureBox.Image = _company.Show();
|
||
}
|
||
else
|
||
{
|
||
MessageBox.Show("Не удалось добавить объект");
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// Получение цвета
|
||
/// </summary>
|
||
/// <param name="random">Генератор случайных чисел</param>
|
||
/// <returns></returns>
|
||
private static Color GetColor(Random random)
|
||
{
|
||
Color color = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256));
|
||
ColorDialog dialog = new();
|
||
if (dialog.ShowDialog() == DialogResult.OK)
|
||
{
|
||
color = dialog.Color;
|
||
}
|
||
|
||
return color;
|
||
}
|
||
|
||
/// <summary>
|
||
/// Удаление объекта
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void ButtonnDelSeaplane_Click(object sender, EventArgs e)
|
||
{
|
||
if (string.IsNullOrEmpty(maskedTextBox.Text) || _company == null)
|
||
{
|
||
return;
|
||
}
|
||
|
||
if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes)
|
||
{
|
||
return;
|
||
}
|
||
|
||
int pos = Convert.ToInt32(maskedTextBox.Text);
|
||
if (_company - pos != null)
|
||
{
|
||
MessageBox.Show("Объект удален");
|
||
pictureBox.Image = _company.Show();
|
||
}
|
||
else
|
||
{
|
||
MessageBox.Show("Не удалось удалить объект");
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// Передача объекта в другую форму
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void ButtonGoToCheck_Click(object sender, EventArgs e)
|
||
{
|
||
if (_company == null)
|
||
{
|
||
return;
|
||
}
|
||
|
||
DrawingBasicSeaplane? seaplane = null;
|
||
int counter = 100;
|
||
while (seaplane == null)
|
||
{
|
||
seaplane = _company.GetRandomObject();
|
||
counter--;
|
||
if (counter <= 0)
|
||
{
|
||
break;
|
||
}
|
||
}
|
||
|
||
if (seaplane == null)
|
||
{
|
||
return;
|
||
}
|
||
|
||
FormSeaplane form = new()
|
||
{
|
||
SetSeaplane = seaplane
|
||
};
|
||
form.ShowDialog();
|
||
}
|
||
|
||
/// <summary>
|
||
/// Перерисовка коллекции
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void ButtonRefresh_Click(object sender, EventArgs e)
|
||
{
|
||
if (_company == null)
|
||
{
|
||
return;
|
||
}
|
||
|
||
pictureBox.Image = _company.Show();
|
||
}
|
||
|
||
|
||
|
||
/// <summary>
|
||
/// добавление коллекции
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void buttonCollectionAdd_Click(object sender, EventArgs e)
|
||
{
|
||
if (string.IsNullOrEmpty(textBoxCollectionName.Text) || (!radioButtonList.Checked && !radioButtonMassive.Checked))
|
||
{
|
||
MessageBox.Show("Не все данные заполнены", "Ошибка",
|
||
MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
return;
|
||
}
|
||
CollectionType collectionType = CollectionType.None;
|
||
if (radioButtonMassive.Checked)
|
||
{
|
||
collectionType = CollectionType.Massive;
|
||
}
|
||
else if (radioButtonList.Checked)
|
||
{
|
||
collectionType = CollectionType.List;
|
||
}
|
||
_storageCollection.AddCollection(textBoxCollectionName.Text, collectionType);
|
||
RerfreshListBoxItems();
|
||
}
|
||
/// <summary>
|
||
/// Обновление списка в ListboxCollection
|
||
/// </summary>
|
||
private void RerfreshListBoxItems()
|
||
{
|
||
listBoxCollection.Items.Clear();
|
||
for (int i = 0; i < _storageCollection.Keys?.Count; ++i)
|
||
{
|
||
string? colName = _storageCollection.Keys?[i];
|
||
if (!string.IsNullOrEmpty(colName))
|
||
{
|
||
listBoxCollection.Items.Add(colName);
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// удаление коллекции
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void buttonCollectionDel_Click(object sender, EventArgs e)
|
||
{
|
||
if (listBoxCollection.SelectedIndex < 0 || listBoxCollection.SelectedItem == null)
|
||
{
|
||
MessageBox.Show("Коллекция не выбрана");
|
||
return;
|
||
}
|
||
if (MessageBox.Show("Удалить коллекцию?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
|
||
{
|
||
return;
|
||
}
|
||
_storageCollection.DelCollection(listBoxCollection.SelectedItem.ToString());
|
||
RerfreshListBoxItems();
|
||
}
|
||
/// <summary>
|
||
/// создание компании
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void buttonCreateCompany_Click(object sender, EventArgs e)
|
||
{
|
||
if (listBoxCollection.SelectedIndex < 0 || listBoxCollection.SelectedItem == null)
|
||
{
|
||
MessageBox.Show("Коллекция не выбрана");
|
||
return;
|
||
}
|
||
ICollectionGenericObjects<DrawingBasicSeaplane>? collection =
|
||
_storageCollection[listBoxCollection.SelectedItem.ToString() ?? string.Empty];
|
||
if (collection == null)
|
||
{
|
||
MessageBox.Show("Коллекция не проинициализирована");
|
||
return;
|
||
}
|
||
switch (СomboBoxSelectorCompany.Text)
|
||
{
|
||
case "Хранилище":
|
||
_company = new PlanePark(pictureBox.Width, pictureBox.Height, collection);
|
||
break;
|
||
}
|
||
panelCompanyTools.Enabled = true;
|
||
RerfreshListBoxItems();
|
||
|
||
}
|
||
|
||
|
||
}
|