PIbd-23_Vrazhkin_S_A_Electr.../lab1/FormLocoConfig.cs
2023-11-14 12:18:28 +04:00

127 lines
4.8 KiB
C#

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 ElectricLocomotive {
public partial class FormLocoConfig : Form {
DrawingLocomotiv? _loco = null;
private event Action<DrawingLocomotiv>? EventAddLoco;
public void AddEvent(Action<DrawingLocomotiv> ev) {
if (EventAddLoco == null) {
EventAddLoco = ev;
}
else {
EventAddLoco += ev;
}
}
public FormLocoConfig() {
InitializeComponent();
panelBlack.MouseDown += PanelColor_MouseDown;
panelGreen.MouseDown += PanelColor_MouseDown;
panelRed.MouseDown += PanelColor_MouseDown;
panelBlue.MouseDown += PanelColor_MouseDown;
panelGray.MouseDown += PanelColor_MouseDown;
panelYellow.MouseDown += PanelColor_MouseDown;
panelPurple.MouseDown += PanelColor_MouseDown;
panelWhite.MouseDown += PanelColor_MouseDown;
labelSimple.MouseDown += LabelObject_MouseDown;
labelAdvanced.MouseDown += LabelObject_MouseDown;
buttonCancel.Click += (s, e) => Close();
}
private void PanelColor_MouseDown(object sender, MouseEventArgs e) {
(sender as Panel)?.DoDragDrop((sender as Panel)?.BackColor,
DragDropEffects.Move | DragDropEffects.Copy);
}
private void LabelObject_MouseDown(object sender, MouseEventArgs e) {
(sender as Label)?.DoDragDrop((sender as Label)?.Name, DragDropEffects.Move | DragDropEffects.Copy);
}
private void DrawLoco() {
Bitmap bmp = new(pictureBox.Width, pictureBox.Height);
Graphics gr = Graphics.FromImage(bmp);
_loco?.SetPosition(5, 5);
_loco?.DrawLoco(gr);
pictureBox.Image = bmp;
}
private void labelSimple_MouseDown(object sender, MouseEventArgs e) {
(sender as Label)?.DoDragDrop((sender as Label)?.Name,
DragDropEffects.Move | DragDropEffects.Copy);
}
private void rightPanel_DragEnter(object sender, DragEventArgs e) {
if (e.Data?.GetDataPresent(DataFormats.Text) ?? false) {
e.Effect = DragDropEffects.Copy;
}
else {
e.Effect = DragDropEffects.None;
}
}
private void rightPanel_DragDrop(object sender, DragEventArgs e) {
switch (e.Data?.GetData(DataFormats.Text).ToString()) {
case "labelSimple":
_loco = new DrawingLocomotiv((int)numericUpDownSpeed.Value,
(int)numericUpDownWeight.Value, pictureBox.Width,
pictureBox.Height, Color.Black, Color.Yellow);
break;
case "labelAdvanced":
_loco = new DrawingElectricLocomotiv(checkBoxRoga.Checked, checkBoxBattery.Checked,
(int)numericUpDownSpeed.Value, (int)numericUpDownWeight.Value, pictureBox.Width,
pictureBox.Height, Color.Black, Color.Yellow, Color.Aqua, Color.Black);
break;
}
labelColor.BackColor = Color.Empty;
labelAddColor.BackColor = Color.Empty;
DrawLoco();
}
private void buttonAdd_Click(object sender, EventArgs e) {
EventAddLoco?.Invoke(_loco);
Close();
}
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) {
if (_loco == null)
return;
labelColor.BackColor = (Color)e.Data.GetData(typeof(Color));
_loco.ChangeColor(labelColor.BackColor);
DrawLoco();
}
private void labelAddColor_DragEnter(object sender, DragEventArgs e) {
if (e.Data.GetDataPresent(typeof(Color))) {
e.Effect = DragDropEffects.Copy;
}
else {
e.Effect = DragDropEffects.None;
}
}
private void labelAddColor_DragDrop(object sender, DragEventArgs e) {
if ((_loco == null) || (_loco is DrawingElectricLocomotiv == false))
return;
labelAddColor.BackColor = (Color)e.Data.GetData(typeof(Color));
((DrawingElectricLocomotiv)_loco).ChangeAddColor(labelAddColor.BackColor);
DrawLoco();
}
}
}