193 lines
6.1 KiB
C#
193 lines
6.1 KiB
C#
using ProjectGasolineTanker.Drawings;
|
||
using ProjectGasolineTanker.Entities;
|
||
|
||
namespace ProjectGasolineTanker;
|
||
/// <summary>
|
||
/// Форма конфигурации объекта
|
||
/// </summary>
|
||
public partial class FormTruckConfig : Form
|
||
{
|
||
/// <summary>
|
||
/// Объект - прорисовка грузовика
|
||
/// </summary>
|
||
private DrawingTruck _truck;
|
||
|
||
/// <summary>
|
||
/// Событие для передачи объекта
|
||
/// </summary>
|
||
private event Action<DrawingTruck>? TruckDelegate;
|
||
/// <summary>
|
||
/// Конструктор
|
||
/// </summary>
|
||
public FormTruckConfig()
|
||
{
|
||
|
||
InitializeComponent();
|
||
panelRed.MouseDown += Panel_MouseDown;
|
||
panelGreen.MouseDown += Panel_MouseDown;
|
||
panelBlue.MouseDown += Panel_MouseDown;
|
||
panelYellow.MouseDown += Panel_MouseDown;
|
||
panelWhite.MouseDown += Panel_MouseDown;
|
||
panelGray.MouseDown += Panel_MouseDown;
|
||
panelBlack.MouseDown += Panel_MouseDown;
|
||
panelPurple.MouseDown += Panel_MouseDown;
|
||
|
||
//к buttonCancel.Click привязать анонимный метод через lambda с закрытием формы
|
||
buttonCancel.Click += (sender, e) => Close();
|
||
|
||
}
|
||
|
||
/// <summary>
|
||
/// Привязка внешнего метода к событию
|
||
/// </summary>
|
||
/// <param name="truckDelegate"></param>
|
||
public void AddEvent(Action<DrawingTruck> truckDelegate)
|
||
{
|
||
if (TruckDelegate == null)
|
||
{
|
||
TruckDelegate = truckDelegate;
|
||
}
|
||
else
|
||
{
|
||
TruckDelegate += truckDelegate;
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// Прорисовка объекта
|
||
/// </summary>
|
||
private void DrawObject()
|
||
{
|
||
Bitmap bmp = new(pictureBoxObject.Width, pictureBoxObject.Height);
|
||
Graphics gr = Graphics.FromImage(bmp);
|
||
_truck?.SetPictureSize(pictureBoxObject.Width, pictureBoxObject.Height);
|
||
_truck?.SetPosition(5, 5);
|
||
_truck?.DrawTransport(gr);
|
||
pictureBoxObject.Image = bmp;
|
||
}
|
||
|
||
/// <summary>
|
||
/// Передаем информацию при нажатии на Label
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void LabelObject_MouseDown(object sender, MouseEventArgs e)
|
||
{
|
||
(sender as Label)?.DoDragDrop((sender as Label)?.Name ?? string.Empty, DragDropEffects.Move | DragDropEffects.Copy);
|
||
}
|
||
|
||
/// <summary>
|
||
/// Проверка получаемой информации (ее типа на соответствие требуемому)
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void PanelObject_DragEnter(object sender, DragEventArgs e)
|
||
{
|
||
if (e.Data?.GetDataPresent(DataFormats.Text) ?? false)
|
||
{
|
||
e.Effect = DragDropEffects.Copy;
|
||
}
|
||
else
|
||
{
|
||
e.Effect = DragDropEffects.None;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// Действия при приеме перетаскиваемой информации
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void PanelObject_DragDrop(object sender, DragEventArgs e)
|
||
{
|
||
switch (e.Data?.GetData(DataFormats.Text)?.ToString())
|
||
{
|
||
case "labelSimpleObject":
|
||
_truck = new DrawingTruck((int)numericUpDownSpeed.Value, (double)numericUpDownWeight.Value, Color.White);
|
||
break;
|
||
case "labelModifiedObject":
|
||
_truck = new DrawingGasolineTanker((int)numericUpDownSpeed.Value, (double)numericUpDownWeight.Value, Color.White,
|
||
Color.Black, checkBoxGasTank.Checked, checkBoxSignalBeacon.Checked);
|
||
break;
|
||
}
|
||
DrawObject();
|
||
}
|
||
|
||
/// <summary>
|
||
/// Передаем информацию при нажатии на Panel
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void Panel_MouseDown(object? sender, MouseEventArgs e)
|
||
{
|
||
//отправка цвета в Drag&Drop
|
||
(sender as Control).DoDragDrop((sender as Control).BackColor, DragDropEffects.Move | DragDropEffects.Copy);
|
||
}
|
||
|
||
// Логика смены цветов: основного и дополнительного (для продвинутого объекта)
|
||
private void LabelBodyColor_DragEnter(object sender, DragEventArgs e)
|
||
{
|
||
if (e.Data.GetDataPresent(typeof(Color)))
|
||
{
|
||
e.Effect = DragDropEffects.Copy;
|
||
}
|
||
else
|
||
{
|
||
e.Effect = DragDropEffects.None;
|
||
}
|
||
}
|
||
|
||
|
||
private void LabelBodyColor_DragDrop(object sender, DragEventArgs e)
|
||
{
|
||
if (_truck != null)
|
||
{
|
||
if (e.Data.GetDataPresent(typeof(Color)))
|
||
{
|
||
_truck.EntityTruck.SetBodyColor((Color)e.Data.GetData(typeof(Color)));
|
||
|
||
}
|
||
DrawObject();
|
||
}
|
||
}
|
||
private void LabelAdditionalColor_DragEnter(object sender, DragEventArgs e)
|
||
{
|
||
if (_truck != null && _truck is DrawingGasolineTanker)
|
||
{
|
||
if (e.Data.GetDataPresent(typeof(Color)))
|
||
{
|
||
e.Effect = DragDropEffects.Copy;
|
||
}
|
||
else
|
||
{
|
||
e.Effect = DragDropEffects.None;
|
||
}
|
||
}
|
||
}
|
||
|
||
private void LabelAdditionalColor_DragDrop(object sender, DragEventArgs e)
|
||
{
|
||
if (_truck != null && _truck.EntityTruck is EntityGasolineTanker _gasolineTanker)
|
||
{
|
||
if (e.Data.GetDataPresent(typeof(Color)))
|
||
{
|
||
_gasolineTanker.SetAdditionalColor((Color)e.Data.GetData(typeof(Color)));
|
||
|
||
}
|
||
DrawObject();
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// Передача объекта
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void ButtonAdd_Click(object sender, EventArgs e)
|
||
{
|
||
if (_truck != null)
|
||
{
|
||
TruckDelegate?.Invoke(_truck);
|
||
Close();
|
||
}
|
||
}
|
||
}
|