212 lines
6.5 KiB
C#
212 lines
6.5 KiB
C#
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;
|
||
/// <summary>
|
||
/// Форма конфигурации объекта
|
||
/// </summary>
|
||
public partial class FormElectricLocomotiveConfig : Form
|
||
{
|
||
|
||
/// <summary>
|
||
/// Объект - прорисовка
|
||
/// </summary>
|
||
private DrawningLocomotive? _locomotive;
|
||
|
||
/// <summary>
|
||
/// Событие для передачи объекта
|
||
/// </summary>
|
||
private event Action<DrawningLocomotive> _locomotiveDelegate;
|
||
/// <summary>
|
||
/// Конструктор
|
||
/// </summary>
|
||
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();
|
||
|
||
}
|
||
|
||
/// <summary>
|
||
/// Привязка внешнего метода к событию
|
||
/// </summary>
|
||
/// <param name="locomotiveDelegate"></param>
|
||
public void AddEvent(Action<DrawningLocomotive> locomotiveDelegate)
|
||
{
|
||
if (_locomotiveDelegate == null)
|
||
{
|
||
_locomotiveDelegate = locomotiveDelegate;
|
||
}
|
||
else
|
||
{
|
||
_locomotiveDelegate += locomotiveDelegate;
|
||
}
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
/// <summary>
|
||
/// Прорисовка объекта
|
||
/// </summary>
|
||
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;
|
||
}
|
||
|
||
/// <summary>
|
||
/// передаем информацию при нажатии на Label
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void labelSimpleObject_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":
|
||
_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();
|
||
}
|
||
|
||
/// <summary>
|
||
/// Передача объекта
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void buttonAdd_Click(object sender, EventArgs e)
|
||
{
|
||
if (_locomotive != null)
|
||
{
|
||
_locomotiveDelegate?.Invoke(_locomotive);
|
||
Close();
|
||
}
|
||
}
|
||
|
||
|
||
}
|