PIbd-23_Dyakonov_R_R_Tank/ProjectTank/FormTankConfig.cs
2023-11-18 16:18:44 +04:00

155 lines
5.8 KiB
C#
Raw Permalink 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 ProjectTank.DrawningObjects;
using System.Windows.Forms;
namespace ProjectTank
{
/// <summary>
/// Форма создания объекта
/// </summary>
public partial class FormTankConfig : Form
{
/// <summary>
/// Переменная-выбранный танк
/// </summary>
DrawningTankBase? _tank = null;
/// <summary>
/// Событие
/// </summary>
private event Action<DrawningTankBase>? EventAddTank;
/// <summary>
/// Конструктор
/// </summary>
public FormTankConfig()
{
InitializeComponent();
yellowPanel.MouseDown += PanelColor_MouseDown;
cyanPanel.MouseDown += PanelColor_MouseDown;
purplePanel.MouseDown += PanelColor_MouseDown;
redPanel.MouseDown += PanelColor_MouseDown;
greenPanel.MouseDown += PanelColor_MouseDown;
orangePanel.MouseDown += PanelColor_MouseDown;
blackPanel.MouseDown += PanelColor_MouseDown;
bluePanel.MouseDown += PanelColor_MouseDown;
labelSimpleObject.MouseDown += LabelObject_MouseDown;
labelModifiedObject.MouseDown += LabelObject_MouseDown;
cancelButton.Click += (s, e) => Close();
}
/// <summary>
/// Отрисовать танк
/// </summary>
private void DrawTank()
{
Bitmap bmp = new(pictureBoxObject.Width, pictureBoxObject.Height);
Graphics gr = Graphics.FromImage(bmp);
_tank?.SetPosition(5, 5);
_tank?.DrawTransport(gr);
pictureBoxObject.Image = bmp;
}
/// <summary>
/// Добавление события
/// </summary>
/// <param name="ev">Привязанный метод</param>
public void AddEvent(Action<DrawningTankBase> ev)
{
if (EventAddTank == null) EventAddTank = ev;
else EventAddTank += ev;
}
private void PanelColor_MouseDown(object sender, MouseEventArgs e)
{
(sender as Panel)?.DoDragDrop((sender as Panel)?.BackColor, DragDropEffects.Move | DragDropEffects.Copy);
}
/// <summary>
/// Передаем информацию при нажатии на Label
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void LabelObject_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 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":
_tank = new DrawningTankBase((int)numericUpDownSpeed.Value,
(int)numericUpDownWeight.Value, Color.White, pictureBoxObject.Width,
pictureBoxObject.Height);
break;
case "labelModifiedObject":
_tank = new DrawningTank((int)numericUpDownSpeed.Value,
(int)numericUpDownWeight.Value, Color.White, Color.Black, checkBoxTankTower.Checked,
checkBoxAntiAirforceGun.Checked, pictureBoxObject.Width, pictureBoxObject.Height);
break;
}
mainColorLabel.BackColor = Color.Empty;
additionalColorLabel.BackColor = Color.Empty;
DrawTank();
}
/// <summary>
/// Добавление танка
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonOk_Click(object sender, EventArgs e)
{
EventAddTank?.Invoke(_tank);
Close();
}
private void ColorLabel_DragDrop(object sender, DragEventArgs e)
{
string senderName = ((Label)sender).Name;
if (_tank == null || e.Data.GetDataPresent(typeof(Color)) == false)
return;
Color droppedColor = (Color)e.Data.GetData(typeof(Color));
if (senderName == "mainColorLabel")
{
mainColorLabel.BackColor = droppedColor;
((DrawningTankBase)_tank).ChangeColor(mainColorLabel.BackColor);
}
else if (senderName == "additionalColorLabel" && _tank is DrawningTank)
{
additionalColorLabel.BackColor = droppedColor;
((DrawningTank)_tank).ChangeAdditionalColor(additionalColorLabel.BackColor);
}
DrawTank();
}
private void ColorLabel_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(typeof(Color)))
{
e.Effect = DragDropEffects.Copy;
}
else
{
e.Effect = DragDropEffects.None;
}
}
}
}