PIbd_23_Kislitsa_E.D_AirFig.../AirFighter/FormAirplaneConfig.cs
2023-11-18 23:11:44 +03:00

142 lines
4.5 KiB
C#

using ProjectAirFighter.DrawningObjects;
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 ProjectAirFighter
{
public partial class FormAirplaneConfig : Form
{
DrawningAirplane? _airplane = null;
private event Action<DrawningAirplane>? EventAddAirplane;
public void AddEvent(Action<DrawningAirplane> ev)
{
if (EventAddAirplane == null)
{
EventAddAirplane = ev;
}
else
{
EventAddAirplane += ev;
}
}
public FormAirplaneConfig()
{
InitializeComponent();
blackPanel.MouseDown += PanelColor_MouseDown;
greenPanel.MouseDown += PanelColor_MouseDown;
redPanel.MouseDown += PanelColor_MouseDown;
bluePanel.MouseDown += PanelColor_MouseDown;
greyPanel.MouseDown += PanelColor_MouseDown;
yellowPanel.MouseDown += PanelColor_MouseDown;
purplePanel.MouseDown += PanelColor_MouseDown;
whitePanel.MouseDown += PanelColor_MouseDown;
airplaneLabel.MouseDown += LabelObject_MouseDown;
airfighterLabel.MouseDown += LabelObject_MouseDown;
cancelButton.Click += (s, e) => Close();
}
public void DrawAirplane()
{
Bitmap bmp = new Bitmap(pictureBox.Width, pictureBox.Height);
Graphics gr = Graphics.FromImage(bmp);
_airplane?.SetPosition(5, 70);
_airplane?.DrawTransport(gr);
pictureBox.Image = bmp;
}
private void PanelColor_MouseDown(object sender, MouseEventArgs e)
{
(sender as Panel)?.DoDragDrop((sender as Panel)?.BackColor,
DragDropEffects.Move | DragDropEffects.Copy);
}
private void allowPanel_DragDrop(object sender, DragEventArgs e)
{
switch (e.Data?.GetData(DataFormats.Text).ToString())
{
case "airplaneLabel":
_airplane = new DrawningAirplane((int)numericSpeed.Value,
(int)numericWeight.Value, Color.White, pictureBox.Width,
pictureBox.Height);
break;
case "airfighterLabel":
_airplane = new DrawningAirFighter((int)numericSpeed.Value,
(int)numericWeight.Value, Color.White, Color.Silver, checkracket.Checked, checkwing.Checked, pictureBox.Width,
pictureBox.Height);
break;
}
colorLabel.BackColor = Color.Empty;
addColorLabel.BackColor = Color.Empty;
DrawAirplane();
}
private void allowPanel_DragEnter(object sender, DragEventArgs e)
{
if (e.Data?.GetDataPresent(DataFormats.Text) ?? false)
{
e.Effect = DragDropEffects.Copy;
}
else
{
e.Effect = DragDropEffects.None;
}
}
private void addButton_Click(object sender, EventArgs e)
{
EventAddAirplane?.Invoke(_airplane);
Close();
}
private void LabelObject_MouseDown(object sender, MouseEventArgs e)
{
(sender as Label)?.DoDragDrop((sender as Label)?.Name, DragDropEffects.Move | DragDropEffects.Copy);
}
private void colorLabel_DragDrop(object sender, DragEventArgs e)
{
if (_airplane == null)
return;
colorLabel.BackColor = (Color)e.Data.GetData(typeof(Color));
_airplane.ChangeColor(colorLabel.BackColor);
DrawAirplane();
}
private void colorLabel_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(typeof(Color)))
{
e.Effect = DragDropEffects.Copy;
}
else
{
e.Effect = DragDropEffects.None;
}
}
private void addColorLabel_DragDrop(object sender, DragEventArgs e)
{
if ((_airplane == null) || (_airplane is DrawningAirFighter == false))
return;
addColorLabel.BackColor = (Color)e.Data.GetData(typeof(Color));
((DrawningAirFighter)_airplane).ChangeAddColor(addColorLabel.BackColor);
DrawAirplane();
}
}
}