using AntiAircraftGun.Drawnings; using AntiAircraftGun.Entities; namespace AntiAircraftGun; /// /// Форма конфигурации объекта /// public partial class FormCarConfig : Form { /// /// Объект - прорисовка бронемашины /// private DrawningArmoredCar? _armoredCar; /// /// Событие для передачи объекта /// private event Action? _armoredCarDelegate; public FormCarConfig() { 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 += (sender, e) => Close(); } /// /// Привязка внешнего метода к событию /// /// public void AddEvent(Action carDelegate) { _armoredCarDelegate += carDelegate; } /// /// Прорисовка объекта /// private void DrawObject() { Bitmap bmp = new(pictureBoxObject.Width, pictureBoxObject.Height); Graphics gr = Graphics.FromImage(bmp); _armoredCar?.SetPictureSize(pictureBoxObject.Width, pictureBoxObject.Height); _armoredCar?.SetPosition(15, 15); _armoredCar?.DrawTransport(gr); pictureBoxObject.Image = bmp; } /// /// Передаем информацию при нажатии на Label /// /// /// private void labelObject_MouseDown(object sender, MouseEventArgs e) { (sender as Label)?.DoDragDrop((sender as Label)?.Name ?? string.Empty, DragDropEffects.Move | DragDropEffects.Copy); } /// /// Проверка получаемой информации (ее типа на соответствие требуемому) /// /// /// private void panelObject_DragEnter(object sender, DragEventArgs e) { e.Effect = e.Data?.GetDataPresent(DataFormats.Text) ?? false ? DragDropEffects.Copy : DragDropEffects.None; } /// /// Действия при приеме перетаскиваемой информации /// /// /// private void panelObject_DragDrop(object sender, DragEventArgs e) { switch (e.Data?.GetData(DataFormats.Text)?.ToString()) { case "labelSimpleObject": _armoredCar = new DrawningArmoredCar((int)numericUpDownSpeed.Value, (double)numericUpDownWeight.Value, Color.White); break; case "labelModifiedObject": _armoredCar = new DrawningAntiAircraftGun((int)numericUpDownSpeed.Value, (double)numericUpDownWeight.Value, Color.White, Color.Black, checkBoxTower.Checked, checkBoxRadar.Checked); break; } DrawObject(); } /// /// Передаем информацию при нажатии на Panel /// /// /// private void Panel_MouseDown(object? sender, MouseEventArgs e) { (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 (_armoredCar != null) { _armoredCar.EntityAircraftGun?.setBodyColor((Color)e.Data.GetData(typeof(Color))); DrawObject(); } } /// /// Прорисовка основным цветом /// /// /// private void labelAdditionalColor_DragDrop(object sender, DragEventArgs e) { if (_armoredCar?.EntityAircraftGun is EntityAntiAircraftGun _car) { _car.setAdditionalColor((Color)e.Data.GetData(typeof(Color))); } DrawObject(); } /// /// Передача дополнительного цвета /// /// /// private void labelAdditionalColor_DragEnter(object sender, DragEventArgs e) { if (_armoredCar is DrawningAntiAircraftGun) { if (e.Data.GetDataPresent(typeof(Color))) { e.Effect = DragDropEffects.Copy; } else { e.Effect = DragDropEffects.None; } } } /// /// Передача объекта /// /// /// private void buttonAdd_Click_1(object sender, EventArgs e) { if (_armoredCar != null) { _armoredCarDelegate?.Invoke(_armoredCar); Close(); } } }