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 RoadTrain.MovementStrategy; using RoadTrain.DrawningObjects; using RoadTrain.Entities; namespace RoadTrain { public partial class FormTrainConfig : Form { /// /// Переменная-выбранный пеозд /// DrawningRoadTrain? _train = null; /// /// Событие /// private event Action EventAddTrain; public FormTrainConfig() { InitializeComponent(); ButtonCancel.Click += (s, e) => Close(); 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; } /// /// Отрисовать машину /// private void DrawTrain() { Bitmap bmp = new(pictureBoxObject.Width, pictureBoxObject.Height); Graphics gr = Graphics.FromImage(bmp); _train?.SetPosition(5, 5); _train?.DrawTransport(gr); pictureBoxObject.Image = bmp; } /// /// Добавление события /// /// Привязанный метод public void AddEvent(Action ev) { if (EventAddTrain == null) { EventAddTrain = ev; } else { EventAddTrain += ev; } } /// /// Передаем информацию при нажатии на Label /// /// /// private void LabelObject_MouseDown(object sender, MouseEventArgs e) { (sender as Label)?.DoDragDrop((sender as Label)?.Name, DragDropEffects.Move | DragDropEffects.Copy); } /// /// Проверка получаемой информации (ее типа на соответствие требуемому) /// /// /// private void PanelObject_DragEnter(object sender, DragEventArgs e) { if (e.Data?.GetDataPresent(DataFormats.Text) ?? false) { e.Effect = DragDropEffects.Copy; } else { e.Effect = DragDropEffects.None; } } /// /// Действия при приеме перетаскиваемой информации /// /// /// private void PanelObject_DragDrop(object sender, DragEventArgs e) { switch (e.Data?.GetData(DataFormats.Text).ToString()) { case "labelSimpleObject": _train = new DrawningRoadTrain((int)numericUpDownSpeed.Value, (int)numericUpDownWeight.Value, Color.White, pictureBoxObject.Width, pictureBoxObject.Height); break; case "labelModifiedObject": _train = new DrawningTrain((int)numericUpDownSpeed.Value, (int)numericUpDownWeight.Value, Color.White, Color.Black, checkBoxWaterContainer.Checked, checkBoxSweepingBrush.Checked, pictureBoxObject.Width, pictureBoxObject.Height); break; } DrawTrain(); } private void ButtonOk_Click(object sender, EventArgs e) { EventAddTrain?.Invoke(_train); Close(); } 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 labelColor_DragDrop(object sender, DragEventArgs e) { if (_train == null) return; ((Label)sender).BackColor = (Color)e.Data.GetData(typeof(Color)); switch (((Label)sender).Name) { case "labelColor": _train.SetBodyColor((Color)e.Data.GetData(typeof(Color))); break; case "labelAdditionalColor": if (!(_train is DrawningTrain)) { return; } (_train as DrawningTrain).SetAdditionalColor((Color)e.Data.GetData(typeof(Color))); break; } DrawTrain(); } } }