261 lines
8.8 KiB
C#
261 lines
8.8 KiB
C#
using ProjectCruiser.CollectionGenericObjects;
|
||
using ProjectCruiser.Drawnings;
|
||
|
||
namespace ProjectCruiser
|
||
{
|
||
public partial class FormCruisersCollection : Form
|
||
{
|
||
/// <summary>
|
||
/// Хранилише коллекций
|
||
/// </summary>
|
||
private readonly StorageCollection<DrawningCruiser> _storageCollection;
|
||
|
||
/// <summary>
|
||
/// Компания
|
||
/// </summary>
|
||
private AbstractCompany? _company = null;
|
||
|
||
/// <summary>
|
||
/// Конструктор
|
||
/// </summary>
|
||
public FormCruisersCollection()
|
||
{
|
||
InitializeComponent();
|
||
_storageCollection = new();
|
||
}
|
||
|
||
/// <summary>
|
||
///
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void comboBoxSelectorCompany_SelectedIndexChanged_1(object sender, EventArgs e)
|
||
{
|
||
panelCompanyTools.Enabled = false;
|
||
}
|
||
|
||
/// <summary>
|
||
/// Создание объекта класса-перемещения
|
||
/// </summary>
|
||
/// <param name="type">Тип создаваемого объекта</param>
|
||
private void CreateObject(string type)
|
||
{
|
||
if (_company == null)
|
||
{
|
||
return;
|
||
}
|
||
Random random = new();
|
||
DrawningCruiser drawningCruiser;
|
||
switch (type)
|
||
{
|
||
case nameof(DrawningCruiser):
|
||
drawningCruiser = new DrawningCruiser(random.Next(100, 300), random.Next(1000, 3000), GetColor(random));
|
||
break;
|
||
case nameof(DrawningMilitaryCruiser):
|
||
// TODO вызов диалогового окна для выбора цвета
|
||
drawningCruiser = new DrawningMilitaryCruiser(random.Next(100, 300), random.Next(1000, 3000),
|
||
GetColor(random), GetColor(random),
|
||
Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)));
|
||
break;
|
||
default:
|
||
return;
|
||
}
|
||
if (_company + drawningCruiser != -1)
|
||
{
|
||
MessageBox.Show("Объект добавлен");
|
||
pictureBoxCruiser.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;
|
||
}
|
||
|
||
//private void ButtonAddCruiser_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningCruiser));
|
||
|
||
//private void ButtonAddMilitaryCruiser_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningMilitaryCruiser));
|
||
private void ButtonAddCruiser_Click(object sender, EventArgs e)
|
||
{
|
||
CreateObject(nameof(DrawningCruiser));
|
||
}
|
||
|
||
private void ButtonAddMilitaryCruiser_Click(object sender, EventArgs e)
|
||
{
|
||
CreateObject(nameof(DrawningMilitaryCruiser));
|
||
}
|
||
|
||
private void ButtonRemoveCruiser_Click(object sender, EventArgs e)
|
||
{
|
||
if (string.IsNullOrEmpty(maskedTextBoxPosision.Text) || _company == null)
|
||
{
|
||
return;
|
||
}
|
||
if (MessageBox.Show("удалить объект?", "удаление",
|
||
MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes)
|
||
{
|
||
return;
|
||
}
|
||
int pos = Convert.ToInt32(maskedTextBoxPosision.Text);
|
||
if (_company - pos != null)
|
||
{
|
||
MessageBox.Show("объект удален");
|
||
pictureBoxCruiser.Image = _company.Show();
|
||
}
|
||
else
|
||
{
|
||
MessageBox.Show("не удалось удалить объект");
|
||
}
|
||
}
|
||
|
||
private void ButtonGetToTest_Click(object sender, EventArgs e)
|
||
{
|
||
if (_company == null)
|
||
{
|
||
return;
|
||
}
|
||
|
||
DrawningCruiser? cruiser = null;
|
||
int counter = 100;
|
||
while (cruiser == null)
|
||
{
|
||
cruiser = _company.GetRandomObject();
|
||
counter--;
|
||
if (counter <= 0)
|
||
{
|
||
break;
|
||
}
|
||
}
|
||
if (cruiser == null)
|
||
{
|
||
return;
|
||
}
|
||
FormCruiser form = new()
|
||
{
|
||
SetCruiser = cruiser
|
||
};
|
||
form.ShowDialog();
|
||
|
||
}
|
||
|
||
private void ButtonRefresh_Click(object sender, EventArgs e)
|
||
{
|
||
if (_company == null)
|
||
{
|
||
return;
|
||
}
|
||
|
||
pictureBoxCruiser.Image = _company.Show();
|
||
}
|
||
|
||
/// <summary>
|
||
/// Добавление коллекции
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void ButtonCollecctionAdd_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>
|
||
/// Удаленние коллекции
|
||
/// </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>
|
||
/// Обновление списка в 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 ButtonCreateCompany_Click(object sender, EventArgs e)
|
||
{
|
||
if (listBoxCollection.SelectedIndex < 0 || listBoxCollection.SelectedItem == null)
|
||
{
|
||
MessageBox.Show("Коллекция не выбрана");
|
||
return;
|
||
}
|
||
ICollectionGenericObjects<DrawningCruiser>? collection =
|
||
_storageCollection[listBoxCollection.SelectedItem.ToString() ?? string.Empty];
|
||
if (collection == null)
|
||
{
|
||
MessageBox.Show("Коллекция не проинициализирована");
|
||
return;
|
||
}
|
||
switch (comboBoxSelectorCompany.Text)
|
||
{
|
||
case "Хранилище":
|
||
_company = new CruiserDockingService(pictureBoxCruiser.Width, pictureBoxCruiser.Height, collection);
|
||
break;
|
||
}
|
||
panelCompanyTools.Enabled = true;
|
||
|
||
}
|
||
|
||
|
||
}
|
||
}
|