PIbd-21_Anisin_R.S._DumpTru.../DumpTruck/FormTruckConfig.cs

178 lines
6.2 KiB
C#
Raw Normal View History

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 DumpTruck.DrawingObjects;
using DumpTruck.Entities;
namespace DumpTruck
{
public partial class FormTruckConfig : Form
{
/// <summary>
/// Переменная-выбранный грузовик
/// </summary>
DrawingTruck? _truck = null;
/// <summary>
/// Событие
/// </summary>
private event Action<DrawingTruck>? EventAddTruck;
/// <summary>
/// Конструктор
/// </summary>
public FormTruckConfig()
{
InitializeComponent();
panelBlack.MouseDown += PanelColor_MouseDown;
panelPurple.MouseDown += PanelColor_MouseDown;
panelGray.MouseDown += PanelColor_MouseDown;
panelGreen.MouseDown += PanelColor_MouseDown;
panelRed.MouseDown += PanelColor_MouseDown;
panelWhite.MouseDown += PanelColor_MouseDown;
panelYellow.MouseDown += PanelColor_MouseDown;
panelBlue.MouseDown += PanelColor_MouseDown;
buttonCancel.Click += (object sender, EventArgs e) => Close();
}
/// <summary>
/// Отрисовать грузовик
/// </summary>
private void DrawTruck()
{
Bitmap bmp = new(pictureBoxObject.Width, pictureBoxObject.Height);
Graphics gr = Graphics.FromImage(bmp);
_truck?.SetPosition(5, 5);
_truck?.DrawTransport(gr);
pictureBoxObject.Image = bmp;
}
/// <summary>
/// Добавление события
/// </summary>
/// <param name="ev">Привязанный метод</param>
public void AddEvent(Action<DrawingTruck> ev)
{
if (EventAddTruck == null)
{
EventAddTruck = ev;
}
else
{
EventAddTruck += ev;
}
}
/// <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, 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,
(int)numericUpDownWeight.Value, Color.White, pictureBoxObject.Width,
pictureBoxObject.Height);
break;
case "labelModifiedObject":
_truck = new DrawingDumpTruck((int)numericUpDownSpeed.Value,
(int)numericUpDownWeight.Value, Color.White, checkBoxTent.Checked,
checkBoxDumpBox.Checked, Color.Black, Color.Gray, pictureBoxObject.Width,
pictureBoxObject.Height);
break;
}
DrawTruck();
}
private void PanelColor_MouseDown(object sender, MouseEventArgs e)
{
(sender as Panel)?.DoDragDrop((sender as Panel)?.BackColor, DragDropEffects.Move | DragDropEffects.Copy);
}
private void labelColor_DragEnter(object sender, DragEventArgs e)
{
if (e.Data?.GetDataPresent(typeof(Color)) ?? false)
{
e.Effect = DragDropEffects.Copy;
}
else
{
e.Effect = DragDropEffects.None;
}
}
private void labelBaseColor_DragDrop(object sender, DragEventArgs e)
{
if (_truck != null)
{
if (e.Data.GetDataPresent(typeof(Color)))
{
_truck.EntityTruck.BodyColor = (Color)e.Data.GetData(typeof(Color));
}
DrawTruck();
}
}
private void labelDumpBoxColor_DragDrop(object sender, DragEventArgs e)
{
if (_truck != null && _truck.EntityTruck is EntityDumpTruck entityDumpTruck)
{
if (e.Data.GetDataPresent(typeof(Color)))
{
entityDumpTruck.DumpBoxColor = (Color)e.Data.GetData(typeof(Color));
}
DrawTruck();
}
}
private void labelTentColor_DragDrop(object sender, DragEventArgs e)
{
if (_truck != null && _truck.EntityTruck is EntityDumpTruck entityDumpTruck)
{
if (e.Data.GetDataPresent(typeof(Color)))
{
entityDumpTruck.TentColor = (Color)e.Data.GetData(typeof(Color));
}
DrawTruck();
}
}
/// <summary>
/// Добавление грузовика
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonAdd_Click(object sender, EventArgs e)
{
EventAddTruck?.Invoke(_truck);
Close();
}
}
}