PIbd-14_Pruidze_I.K_Simple_.../ProjectCruiser/ServiceForm2.cs
2024-06-15 09:05:36 +04:00

229 lines
6.6 KiB
C#

using System.Collections.Generic;
using System.Windows.Forms;
using System.Xml.Linq;
using ProjectCruiser.CollectionGenericObj;
using ProjectCruiser.DrawningSamples;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
namespace ProjectCruiser;
public partial class ServiceForm2 : Form
{
// Компания
private AbstractCompany? _company = null;
private readonly StorageCollection<DrawningBase> _storageCollection;
public ServiceForm2()
{
InitializeComponent();
_storageCollection = new();
}
// Выбор компании
private void SelectorCompany_SelectedIndexChanged(object sender, EventArgs e)
{
/*
switch (comboBoxArrList.Text)
{
case "Storage":
_company = new ShipSharingService(pictureBox.Width, pictureBox.Height,
new ArrayGenObj<DrawningBase>());
break;
}
*/
toolPanel.Enabled = false;
}
// Color picker (default : random)
private static Color pickColor(Random r)
{
Color cl = new Color();
ColorDialog dialog = new();
if (dialog.ShowDialog() == DialogResult.OK) cl = dialog.Color;
else Color.FromArgb(r.Next(0, 256), r.Next(0, 256), r.Next(0, 256));
return cl;
}
// Добавление обычного корабля
private void btnAddBase_Click(object sender, EventArgs e) =>
CreateObject(nameof(DrawningBase));
// Добавление продвинутого
private void btnAddAdvanced_Click(object sender, EventArgs e) =>
CreateObject(nameof(DrawningCruiser));
// Создание объекта класса-перемещения
private void CreateObject(string type)
{
if (_company == null)
{
return;
}
Random random = new();
DrawningBase drawningCar;
switch (type)
{
case nameof(DrawningBase):
drawningCar = new DrawningBase(random.Next(100, 300),
random.Next(1000, 3000), pickColor(random));
break;
case nameof(DrawningCruiser):
drawningCar = new DrawningCruiser(random.Next(100, 300),
random.Next(1000, 3000), pickColor(random), pickColor(random),
Convert.ToBoolean(random.Next(0, 2)));
break;
default:
return;
}
if (_company + drawningCar != -1)
{
MessageBox.Show("> Object was added");
pictureBox.Image = _company.Show();
}
else
{
MessageBox.Show("[!] Failed to add object");
}
}
// Удаление объекта
private void btnRemoveCar_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(maskedTextBoxPosition.Text)
|| _company == null) return;
if (MessageBox.Show("[*] Remove object: Are you sure?", "Remove",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes) return;
int pos = Convert.ToInt32(maskedTextBoxPosition.Text);
if (_company - Convert.ToInt32(maskedTextBoxPosition.Text) != null)
{
MessageBox.Show("> Object was removed");
pictureBox.Image = _company.Show();
}
else MessageBox.Show("[!] Failed to remove object");
}
// Передача объекта в другую форму
private void btnChooseforTest_Click(object sender, EventArgs e)
{
if (_company == null)
{
return;
}
DrawningBase? car = null;
int counter = 100;
while (car == null)
{
car = _company.GetRandomObject();
counter--;
if (counter <= 0)
{
break;
}
}
if (car == null)
{
return;
}
OceanForm1 form = new() { SetShip = car };
form.ShowDialog();
}
// Перерисовка коллекции
private void btnRefresh_Click(object sender, EventArgs e)
{
if (_company == null)
{
return;
}
pictureBox.Image = _company.Show();
}
private void btnCollectionAdd_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(maskedTxtBoxCName.Text) || (!rBtnList.Checked && !rBtnArray.Checked))
{
MessageBox.Show("Enter correct data or choose an option", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
CollectionType collType = CollectionType.None;
if (rBtnArray.Checked)
{
collType = CollectionType.Array;
}
else if (rBtnList.Checked)
{
collType = CollectionType.List;
}
_storageCollection.AddCollection(maskedTxtBoxCName.Text, collType);
RefreshListBoxItems();
}
private void btnCollectionDel_Click(object sender, EventArgs e)
{
if (listBox.SelectedItem == null || listBox.SelectedIndex < 0)
{
MessageBox.Show("Collection was not choosed");
return;
}
if (MessageBox.Show("Are you sure?", "Removing", MessageBoxButtons.OK, MessageBoxIcon.Question) != DialogResult.OK)
{
return;
}
_storageCollection.DelCollection(listBox.SelectedItem.ToString());
RefreshListBoxItems();
}
private void RefreshListBoxItems()
{
listBox.Items.Clear();
for (int i = 0; i < _storageCollection.Keys?.Count; ++i)
{
string? collName = _storageCollection.Keys?[i];
if (!string.IsNullOrEmpty(collName))
{
listBox.Items.Add(collName);
}
}
}
private void btnCreateCompany_Click(object sender, EventArgs e)
{
if (listBox.SelectedIndex < 0 || listBox.SelectedItem == null)
{
MessageBox.Show("Collection wasn't choosed");
return;
}
ICollectionGenObj<DrawningBase>? collection =
_storageCollection[listBox.SelectedItem.ToString() ?? string.Empty];
if (collection == null)
{
MessageBox.Show("Collection wasn't initialized");
return;
}
switch (comboBoxArrList.Text)
{
case "Storage":
_company = new ShipSharingService(pictureBox.Width,
pictureBox.Height, collection);
break;
}
toolPanel.Enabled = true; // block of buttons at the right bottom
RefreshListBoxItems();
}
}