PIbd-22_Fedorenko_G.Y._Hydr.../Hydroplane/FormPlaneConfig.cs

170 lines
5.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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;
using Hydroplane.DrawningObjects;
using Hydroplane.Entities;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
namespace Hydroplane
{
public partial class FormPlaneConfig : Form
{
/// <summary>
/// Переменная-выбранная машина
/// </summary>
DrawningPlane? _plane = null;
/// <summary>
/// Событие
/// </summary>
public event Action<DrawningPlane>? EventAddPlane;
/// <summary>
/// Конструктор
/// </summary>
public FormPlaneConfig()
{
InitializeComponent();
panelBlack.MouseDown += panelColor_MouseDown;
panelPurple.MouseDown += panelColor_MouseDown;
panelGray.MouseDown += panelColor_MouseDown;
panelGreen.MouseDown += panelColor_MouseDown;
panelRed.MouseDown += panelColor_MouseDown;
panelCian.MouseDown += panelColor_MouseDown;
panelYellow.MouseDown += panelColor_MouseDown;
panelBlue.MouseDown += panelColor_MouseDown;
button_close.Click += (s, e) => Close();
}
/// <summary>
/// Отрисовать машину
/// </summary>
private void DrawPlane()
{
Bitmap bmp = new(pictureBox.Width, pictureBox.Height);
Graphics gr = Graphics.FromImage(bmp);
_plane?.SetPosition(5, 5);
_plane?.DrawTransport(gr);
pictureBox.Image = bmp;
}
/// <summary>
/// Добавление события
/// </summary>
/// <param name="ev">Привязанный метод</param>
public void AddEvent(Action<DrawningPlane> ev)
{
if (EventAddPlane == null)
{
EventAddPlane = ev;
}
else
{
EventAddPlane += ev;
}
}
/// <summary>
/// Передаем информацию при нажатии на Label
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Label_MouseDown(object sender, MouseEventArgs e)
{
(sender as Label)?.DoDragDrop((sender as Label)?.Name, DragDropEffects.Move | DragDropEffects.Copy);
}
/// <summary>
/// Проверка получаемой информации (ее типа на соответствие требуемому)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void panel_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 panel_dragDrop(object sender, DragEventArgs e)
{
ILogger<FormHydroplaneCollection> logger = new NullLogger<FormHydroplaneCollection>();
switch (e.Data?.GetData(DataFormats.Text).ToString())
{
case "labelOriginalObject":
_plane = new DrawningPlane((int)numericUpDownSpeed.Value, (int)numericUpDownWeight.Value, Color.White, pictureBox.Width, pictureBox.Height);
break;
case "labelModifiedObject":
_plane = new DrawningHydroplane((int)numericUpDownSpeed.Value, (int)numericUpDownWeight.Value, Color.White, Color.Black, checkBox_boat.Checked, checkBox_bobber.Checked, pictureBox.Width, pictureBox.Height);
break;
}
DrawPlane();
}
public void panelColor_MouseDown(object sender, MouseEventArgs e)
{
(sender as Panel)?.DoDragDrop((sender as Panel)?.BackColor, DragDropEffects.Move | DragDropEffects.Copy);
}
private void labelColor_dragEnter(object sender, DragEventArgs e)
{
if (e.Data?.GetDataPresent(typeof(Color)) ?? false)
{
e.Effect = DragDropEffects.Copy;
}
else
{
e.Effect = DragDropEffects.None;
}
}
private void labelColor_dragDrop(object sender, DragEventArgs e)
{
if (_plane == null)
return;
switch (((Panel)sender).Name)
{
case "panel_color":
_plane?.EntityPlane?.setBodyColor((Color)e.Data.GetData(typeof(Color)));
break;
case "panel_addit_color":
if (!(_plane is DrawningHydroplane))
return;
(_plane.EntityPlane as EntityHydroplane)?.setAdditionalColor((Color)e.Data.GetData(typeof(Color)));
break;
}
DrawPlane();
}
/// <summary>
/// Добавление машины
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button_add_Click(object sender, EventArgs e)
{
EventAddPlane?.Invoke(_plane);
Close();
}
}
}