ISEbd-12_Sinelnikova_A.V._S.../ProjectCruiser/FormCruiserConfing.cs

177 lines
6.3 KiB
C#
Raw Permalink Normal View History

using ProjectCruiser.Drawnings;
using ProjectCruiser.Entities;
namespace ProjectCruiser
{
/// <summary>
/// Форма конфигурации объекта
/// </summary>
public partial class FormCruiserConfing : Form
{
/// <summary>
/// Объект - прорисовка крейсера
/// </summary>
private DrawningCruiser _cruiser;
/// <summary>
/// Событие для передачи объекта
/// </summary>
private event Action<DrawningCruiser>? CruiserDelegate;
/// <summary>
/// Привязка внешнего метода к событию
/// </summary>
/// <param name="carDelegate"></param>
public void AddEvent(Action<DrawningCruiser> cruiserDelegate)
{
CruiserDelegate += cruiserDelegate;
}
/// <summary>
/// Конструктор
/// </summary>
public FormCruiserConfing()
{
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 with lambda с закрытием формы
buttonCancel.Click += (sender, e) => Close();
}
/// <summary>
/// Прорисовка объекта
/// </summary>
private void DrawObject()
{
Bitmap bmp = new(pictureBoxObject.Width, pictureBoxObject.Height);
Graphics gr = Graphics.FromImage(bmp);
_cruiser?.SetPictureSize(pictureBoxObject.Width, pictureBoxObject.Height);
_cruiser?.SetPosition(15, 15);
_cruiser?.DrawTransport(gr);
pictureBoxObject.Image = bmp;
}
/// <summary>
/// Передаем информацию при нажатии на Label
/// </summary>
/// <param name="sender"></param>labelSimpleObject
/// <param name="e"></param>labelSimpleObject
private void labelObject_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)
{
e.Effect = e.Data?.GetDataPresent(DataFormats.Text) ?? false ? DragDropEffects.Copy : 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":
_cruiser = new DrawningCruiser((int)numericUpDownSpeed.Value, (double)numericUpDownWeight.Value, Color.White);
break;
case "labelModifiedObject":
_cruiser = new
DrawningMilitaryCruiser((int)numericUpDownSpeed.Value, (double)numericUpDownWeight.Value, Color.White,
Color.Black, checkBoxHelicopterArea.Checked, checkBoxBoat.Checked, checkBoxWeapon.Checked);
break;
}
DrawObject();
}
/// <summary>
/// Передаем информацию при нажатии на Panel
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Panel_MouseDown(object sender, MouseEventArgs e)
{
//TODO отправка цвета в Drag&Drop
(sender as Control)?.DoDragDrop((sender as Control).BackColor, DragDropEffects.Move | DragDropEffects.Copy);
}
// TODO Реализовать логику смены цветов: основного и дополнительного (для продвинутого объекта)
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 (_cruiser != null)
{
_cruiser.EntityCruiser.setBodyColor((Color)e.Data.GetData(typeof(Color)));
DrawObject();
}
}
private void labelAdditionalColor_DragEnter(object sender, DragEventArgs e)
{
if (_cruiser is DrawningCruiser)
{
if (e.Data.GetDataPresent(typeof(Color)))
{
e.Effect = DragDropEffects.Copy;
}
else
{
e.Effect = DragDropEffects.None;
}
}
}
private void labelAdditionalColor_DragDrop(object sender, DragEventArgs e)
{
if (_cruiser.EntityCruiser is EntityMilitaryCruiser militaryCruiser)
{
militaryCruiser.setAdditionalColor((Color)e.Data.GetData(typeof(Color)));
}
DrawObject();
}
/// <summary>
/// Передача объекта
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonAdd_Click(object sender, EventArgs e)
{
if (_cruiser != null)
{
CruiserDelegate?.Invoke(_cruiser);
Close();
}
}
}
}