PIBD-11_Basalov_A.D_Simple/ProjectElectricLocomotive/FormElectricLocomotiveConfig.cs

212 lines
6.5 KiB
C#
Raw Normal View History

2024-04-01 20:21:57 +04:00
using ProjectElectricLocomotive.Drawnings;
2024-04-09 18:48:06 +04:00
using ProjectElectricLocomotive.Entities;
2024-04-01 20:21:57 +04:00
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>
2024-04-09 18:48:06 +04:00
/// Объект - прорисовка
2024-04-01 20:21:57 +04:00
/// </summary>
private DrawningLocomotive? _locomotive;
/// <summary>
/// Событие для передачи объекта
/// </summary>
2024-04-15 09:42:20 +04:00
private event Action<DrawningLocomotive> _locomotiveDelegate;
2024-04-01 20:21:57 +04:00
/// <summary>
/// Конструктор
/// </summary>
public FormElectricLocomotiveConfig()
{
2024-04-09 18:48:06 +04:00
InitializeComponent();
2024-04-01 20:21:57 +04:00
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 привязать анонимный метод через лямбда с закрытием формы
2024-04-09 18:48:06 +04:00
buttonCancel.Click += (sender, e) => Close();
2024-04-01 20:21:57 +04:00
}
/// <summary>
/// Привязка внешнего метода к событию
/// </summary>
/// <param name="locomotiveDelegate"></param>
2024-04-15 09:42:20 +04:00
public void AddEvent(Action<DrawningLocomotive> locomotiveDelegate)
2024-04-01 20:21:57 +04:00
{
2024-04-09 18:48:06 +04:00
if (_locomotiveDelegate == null)
{
2024-04-01 20:21:57 +04:00
_locomotiveDelegate = locomotiveDelegate;
}
else
{
_locomotiveDelegate += locomotiveDelegate;
}
}
2024-04-15 10:37:58 +04:00
2024-04-19 18:16:07 +04:00
2024-04-15 10:37:58 +04:00
2024-04-15 09:42:20 +04:00
2024-04-01 20:21:57 +04:00
/// <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;
}
2024-04-09 18:48:06 +04:00
labelBodyColor.BackColor = Color.Empty;
labelAdditionalColor.BackColor = Color.Empty;
2024-04-01 20:21:57 +04:00
DrawObject();
}
private void panel_MouseDown(object sender, MouseEventArgs e)
{
2024-04-09 18:48:06 +04:00
(sender as Control).DoDragDrop((sender as Control).BackColor, DragDropEffects.Move | DragDropEffects.Copy);
2024-04-01 20:21:57 +04:00
//TODO отпавка цвета в Dragdrop
}
//TODO реализовать логику смены цветов основного и дополнительного(для продвинутого)
2024-04-09 18:48:06 +04:00
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();
}
2024-04-01 20:21:57 +04:00
/// <summary>
/// Передача объекта
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonAdd_Click(object sender, EventArgs e)
{
2024-04-09 18:48:06 +04:00
if (_locomotive != null)
2024-04-01 20:21:57 +04:00
{
_locomotiveDelegate?.Invoke(_locomotive);
Close();
}
}
2024-04-09 18:48:06 +04:00
2024-04-01 20:21:57 +04:00
}