PIbd-21_Krasnikov_Lab1.base/WinFormsApp1/FormTraktorConfig.cs

149 lines
4.6 KiB
C#
Raw Permalink Normal View History

2022-12-12 23:38:47 +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 WinFormsApp1
{
public partial class FormTraktorConfig : Form
{
TractorDraw _traktor = null;
private event TraktorDelegate EventAddTraktor;
public FormTraktorConfig()
{
InitializeComponent();
panelBlack.MouseDown += PanelColor_MouseDown;
panelPurple.MouseDown += PanelColor_MouseDown;
panelGray.MouseDown += PanelColor_MouseDown;
panelGreen.MouseDown += PanelColor_MouseDown;
panelRed.MouseDown += PanelColor_MouseDown;
panelWhite.MouseDown += PanelColor_MouseDown;
panelYellow.MouseDown += PanelColor_MouseDown;
panelBlue.MouseDown += PanelColor_MouseDown;
buttonCancel.Click += (object sender, EventArgs a) => Close();
}
private void labelObject_MouseDown(object sender, MouseEventArgs e)
{
(sender as Label).DoDragDrop((sender as Label).Name, DragDropEffects.Move | DragDropEffects.Copy);
}
private void DrawTraktor()
{
Bitmap bmp = new(pictureBoxObject.Width, pictureBoxObject.Height);
Graphics gr = Graphics.FromImage(bmp);
_traktor?.SetPosition(5, 5, pictureBoxObject.Width, pictureBoxObject.Height);
_traktor?.DrawEntity(gr);
pictureBoxObject.Image = bmp;
}
public void AddEvent(TraktorDelegate ev)
{
if (EventAddTraktor == null)
{
EventAddTraktor = new TraktorDelegate(ev);
}
else
{
EventAddTraktor += ev;
}
}
private void Panel1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.Text))
{
e.Effect = DragDropEffects.Copy;
}
else
{
e.Effect = DragDropEffects.None;
}
}
private void Panel1_DragDrop(object sender, DragEventArgs e)
{
switch (e.Data.GetData(DataFormats.Text).ToString())
{
case "labelSimpleObject":
_traktor = new TractorDraw((int)numericUpDownSpeed.Value, (int)numericUpDownWeight.Value, labelColor.BackColor);
break;
case "labelHardObject":
_traktor = new MultiTraktorDraw((int)numericUpDownSpeed.Value, (int)numericUpDownWeight.Value, labelColor.BackColor, labelDopColor.BackColor,
checkBoxAhead.Checked, checkBoxBehind.Checked);
break;
}
DrawTraktor();
}
private void LabelColor_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(typeof(Color)))
{
e.Effect = DragDropEffects.Copy;
}
else
{
e.Effect = DragDropEffects.None;
}
}
private void LabelColor_DragDrop(object sender, DragEventArgs e)
{
var color = e.Data.GetData(typeof(Color));
if (color != null)
{
(sender as Label).BackColor = (Color)color;
}
if (_traktor != null)
{
_traktor.Tractor.BodyColor = (Color)color;
DrawTraktor();
}
}
private void PanelColor_MouseDown(object sender, MouseEventArgs e)
{
(sender as Control).DoDragDrop((sender as Control).BackColor, DragDropEffects.Move | DragDropEffects.Copy);
}
private void LabelDopColor_DragDrop(object sender, DragEventArgs e)
{
if (_traktor != null && _traktor.Tractor is MultiTraktor entityBulldozer)
{
entityBulldozer.DopColor = (Color)e.Data.GetData(typeof(Color));
DrawTraktor();
}
}
private void LabelDopColor_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(typeof(Color)))
{
e.Effect = DragDropEffects.Copy;
}
else
{
e.Effect = DragDropEffects.None;
}
}
private void buttonAdd_Click(object sender, EventArgs e)
{
EventAddTraktor?.Invoke(_traktor);
Close();
}
}
}