290 lines
8.5 KiB
C#
290 lines
8.5 KiB
C#
using ProjectMonorail.CollectionGenericObject;
|
||
using ProjectMonorail.CollectionGenericObjects;
|
||
using ProjectMonorail.Drawings;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.ComponentModel;
|
||
using System.Data;
|
||
using System.Drawing;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
using System.Windows.Forms;
|
||
|
||
namespace ProjectMonorail;
|
||
|
||
/// <summary>
|
||
/// Форма работы с компанией и её коллекцией
|
||
/// </summary>
|
||
public partial class FormTrainCollection : Form
|
||
{
|
||
/// <summary>
|
||
/// Хранилище коллекций
|
||
/// </summary>
|
||
private readonly StorageCollection<DrawingTrain> _storageCollection;
|
||
|
||
/// <summary>
|
||
/// Компания
|
||
/// </summary>
|
||
private AbstractCompany? _company = null;
|
||
|
||
/// <summary>
|
||
/// Конструктор
|
||
/// </summary>
|
||
public FormTrainCollection()
|
||
{
|
||
InitializeComponent();
|
||
_storageCollection = new();
|
||
}
|
||
|
||
/// <summary>
|
||
/// Выбор компании
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void ComboBoxSelectorCompany_SelectedIndexChanged(object sender, EventArgs e)
|
||
{
|
||
panelCompanyTools.Enabled = false;
|
||
}
|
||
|
||
/// <summary>
|
||
/// Добавление поезда
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void ButtonAddTrain_Click(object sender, EventArgs e) => CreateObject(nameof(DrawingTrain));
|
||
|
||
/// <summary>
|
||
/// Добавление монорельса
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void ButtonAddMonorail_Click(object sender, EventArgs e) => CreateObject(nameof(DrawingMonorail));
|
||
|
||
/// <summary>
|
||
/// Создание объекта класа - перемещение
|
||
/// </summary>
|
||
/// <param name="type"></param>
|
||
private void CreateObject(string type)
|
||
{
|
||
if (_company == null)
|
||
{
|
||
return;
|
||
}
|
||
|
||
Random random = new();
|
||
DrawingTrain drawingTrain;
|
||
switch (type)
|
||
{
|
||
case nameof(DrawingTrain):
|
||
drawingTrain = new DrawingTrain(random.Next(100, 300), random.Next(1000, 3000), GetColor(random));
|
||
break;
|
||
case nameof(DrawingMonorail):
|
||
drawingTrain = new DrawingMonorail(random.Next(100, 300), random.Next(1000, 3000), random.Next(2, 5),
|
||
GetColor(random), GetColor(random), Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)));
|
||
break;
|
||
default: return;
|
||
}
|
||
|
||
if (_company + drawingTrain != -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 ButtonRemoveTrain_Click(object sender, EventArgs e)
|
||
{
|
||
if (string.IsNullOrEmpty(maskedTextBoxPosition.Text) || _company == null)
|
||
{
|
||
return;
|
||
}
|
||
|
||
if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
|
||
{
|
||
return;
|
||
}
|
||
|
||
int pos = Convert.ToInt32(maskedTextBoxPosition.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;
|
||
}
|
||
|
||
DrawingTrain? train = null;
|
||
int counter = 100;
|
||
while (train == null)
|
||
{
|
||
train = _company.GetRandomObject();
|
||
counter--;
|
||
if (counter <= 0)
|
||
{
|
||
break;
|
||
}
|
||
}
|
||
|
||
if (train == null)
|
||
{
|
||
return;
|
||
}
|
||
|
||
FormMonorail form = new()
|
||
{
|
||
SetTrain = train
|
||
};
|
||
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;
|
||
}
|
||
if (radioButtonList.Checked)
|
||
{
|
||
collectionType = CollectionType.List;
|
||
}
|
||
|
||
_storageCollection.AddCollection(textBoxCollectionName.Text, collectionType);
|
||
RefreshListBoxItems();
|
||
}
|
||
|
||
/// <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());
|
||
RefreshListBoxItems();
|
||
}
|
||
|
||
/// <summary>
|
||
/// Обновление списка в listBoxCollection
|
||
/// </summary>
|
||
private void RefreshListBoxItems()
|
||
{
|
||
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<DrawingTrain>? collection = _storageCollection[listBoxCollection.SelectedItem.ToString() ?? string.Empty];
|
||
if (collection == null)
|
||
{
|
||
MessageBox.Show("Коллекция не проинициализирована");
|
||
return;
|
||
}
|
||
|
||
switch (comboBoxSelectorCompany.Text)
|
||
{
|
||
case "Хранилище":
|
||
_company = new TrainSharingService(pictureBox.Width, pictureBox.Height, collection);
|
||
break;
|
||
}
|
||
|
||
panelCompanyTools.Enabled = true;
|
||
RefreshListBoxItems();
|
||
}
|
||
}
|