190 lines
5.3 KiB
C#
190 lines
5.3 KiB
C#
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;
|
||
using Tank.CollectionGenericObjects;
|
||
using Tank.Drowings;
|
||
|
||
namespace Tank;
|
||
|
||
/// <summary>
|
||
///
|
||
/// </summary>
|
||
public partial class FormTankCollection : Form
|
||
|
||
{
|
||
/// <summary>
|
||
///
|
||
/// </summary>
|
||
private AbstractCompany? _company = null;
|
||
|
||
/// <summary>
|
||
///
|
||
/// </summary>
|
||
public FormTankCollection()
|
||
{
|
||
InitializeComponent();
|
||
}
|
||
|
||
/// <summary>
|
||
///
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void ComboBoxSelectorCompany_SelectedIndexChanged(object sender, EventArgs e)
|
||
{
|
||
switch (comboBoxSelectorCompany.Text)
|
||
{
|
||
case "Хранилище":
|
||
_company = new TankSharingService(pictureBox.Width, pictureBox.Height, new MassiveGenericObjects<DrawningMachine>());
|
||
break;
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// Добавление обычного автомобиля
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void ButtonAddMachine_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningMachine));
|
||
/// <summary>
|
||
/// Добавление спортивного автомобиля
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void ButtonAddTank_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningTank));
|
||
///<summary>
|
||
///Создание объекта класса-перемещения
|
||
///</summary>
|
||
/// <param name="type">Тип создаваемого объётека</param>
|
||
|
||
private void CreateObject(string type)
|
||
{
|
||
if (_company == null)
|
||
{
|
||
return;
|
||
}
|
||
|
||
Random rnd = new Random();
|
||
DrawningMachine drawningMachine;
|
||
switch (type)
|
||
{
|
||
case nameof(DrawningMachine):
|
||
drawningMachine = new DrawningMachine(rnd.Next(100, 300), rnd.Next(1000, 3000), GetColor(rnd));
|
||
break;
|
||
case nameof(DrawningTank):
|
||
drawningMachine = new DrawningTank(rnd.Next(650, 700), rnd.Next(15760, 16130),
|
||
GetColor(rnd),
|
||
GetColor(rnd),
|
||
Convert.ToBoolean(rnd.Next(0, 2)),
|
||
Convert.ToBoolean(rnd.Next(0, 2)));
|
||
break;
|
||
default:
|
||
return;
|
||
}
|
||
|
||
if (_company + drawningMachine != -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 ButtonRemoveCar_Click(object sender, EventArgs e)
|
||
{
|
||
if (string.IsNullOrEmpty(maskedTextBoxPosition.Text) || _company == null)
|
||
{
|
||
return;
|
||
}
|
||
if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes)
|
||
{
|
||
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;
|
||
}
|
||
DrawningMachine? tank = null;
|
||
int counter = 100;
|
||
while (tank == null)
|
||
{
|
||
tank = _company.GetRandomObject();
|
||
counter--;
|
||
if (counter <= 0)
|
||
{
|
||
break;
|
||
}
|
||
}
|
||
if (tank == null)
|
||
{
|
||
return;
|
||
}
|
||
FormTank form = new()
|
||
{
|
||
SetMachine = tank
|
||
};
|
||
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();
|
||
}
|
||
} |