using ProjectElectricLocomotive.Drawnings;
using ProjectElectricLocomotive.Entities;
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;
namespace ProjectElectricLocomotive;
///
/// Форма конфигурации объекта
///
public partial class FormElectricLocomotiveConfig : Form
{
///
/// Объект - прорисовка
///
private DrawningLocomotive? _locomotive;
///
/// Событие для передачи объекта
///
private event Action _locomotiveDelegate;
///
/// Конструктор
///
public FormElectricLocomotiveConfig()
{
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;
//TODO buttonCancel.Click привязать анонимный метод через лямбда с закрытием формы
buttonCancel.Click += (sender, e) => Close();
}
///
/// Привязка внешнего метода к событию
///
///
public void AddEvent(Action locomotiveDelegate)
{
if (_locomotiveDelegate == null)
{
_locomotiveDelegate = locomotiveDelegate;
}
else
{
_locomotiveDelegate += locomotiveDelegate;
}
}
///
/// Прорисовка объекта
///
private void DrawObject()
{
Bitmap bmp = new(pictureBoxObject.Width, pictureBoxObject.Height);
Graphics gr = Graphics.FromImage(bmp);
_locomotive?.SetPictureSize(pictureBoxObject.Width,
pictureBoxObject.Height);
_locomotive?.SetPosition(15, 15);
_locomotive?.DrawTransport(gr);
pictureBoxObject.Image = bmp;
}
///
/// передаем информацию при нажатии на Label
///
///
///
private void labelSimpleObject_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)
{
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":
_locomotive = new DrawningLocomotive((int)numericUpDownSpeed.Value,
(double)numericUpDownWeight.Value, Color.White);
break;
case "labelModifiedObject":
_locomotive = new
DrawningElectricLocomotive((int)numericUpDownSpeed.Value, (double)numericUpDownWeight.Value,
Color.White,
Color.Black, checkBoxPantograph.Checked,
checkBoxBatteryStorage.Checked);
break;
}
labelBodyColor.BackColor = Color.Empty;
labelAdditionalColor.BackColor = Color.Empty;
DrawObject();
}
private void panel_MouseDown(object sender, MouseEventArgs e)
{
(sender as Control).DoDragDrop((sender as Control).BackColor, DragDropEffects.Move | DragDropEffects.Copy);
//TODO отпавка цвета в Dragdrop
}
//TODO реализовать логику смены цветов основного и дополнительного(для продвинутого)
private void labelBodyColor_DragEnter(object sender, DragEventArgs e)
{
if (_locomotive != null)
{
if (e.Data.GetDataPresent(typeof(Color)))
{
e.Effect = DragDropEffects.Copy;
}
else
{
e.Effect = DragDropEffects.None;
}
}
}
private void labelBodyColor_DragDrop(object sender, DragEventArgs e)
{
if (_locomotive != null)
{
_locomotive.EntityLocomotive.setBodyColor((Color)e.Data.GetData(typeof(Color)));
DrawObject();
}
}
private void labelAdditionalColor_DragEnter(object sender, DragEventArgs e)
{
if (_locomotive != null && _locomotive.EntityLocomotive is EntityElectricLocomotive entityElectricLocomotive)
{
if (e.Data.GetDataPresent(typeof(Color)))
{
e.Effect = DragDropEffects.Copy;
}
else
{
e.Effect = DragDropEffects.None;
}
}
}
private void labelAdditionalColor_DragDrop(object sender, DragEventArgs e)
{
if (_locomotive != null && _locomotive.EntityLocomotive is EntityElectricLocomotive entityElectricLocomotive)
{
entityElectricLocomotive.setAdditionalColor((Color)e.Data.GetData(typeof(Color)));
DrawObject();
}
DrawObject();
}
///
/// Передача объекта
///
///
///
private void buttonAdd_Click(object sender, EventArgs e)
{
if (_locomotive != null)
{
_locomotiveDelegate?.Invoke(_locomotive);
Close();
}
}
}