Реализация функции DragAndDrop для основного и дополнительного цвета

This commit is contained in:
Данияр Аглиуллов 2022-10-18 02:38:41 +04:00
parent d52ab91927
commit 017d1885d9
3 changed files with 57 additions and 41 deletions

View File

@ -40,7 +40,6 @@
this.panelGreen = new System.Windows.Forms.Panel();
this.panelWhite = new System.Windows.Forms.Panel();
this.panelRed = new System.Windows.Forms.Panel();
this.checkBoxSportLine = new System.Windows.Forms.CheckBox();
this.checkBoxHasFuelTanks = new System.Windows.Forms.CheckBox();
this.checkBoxHasBombs = new System.Windows.Forms.CheckBox();
this.numericUpDownWeight = new System.Windows.Forms.NumericUpDown();
@ -66,7 +65,6 @@
this.groupBoxConfig.Controls.Add(this.labelModifiedObject);
this.groupBoxConfig.Controls.Add(this.labelSimpleObject);
this.groupBoxConfig.Controls.Add(this.groupBoxColors);
this.groupBoxConfig.Controls.Add(this.checkBoxSportLine);
this.groupBoxConfig.Controls.Add(this.checkBoxHasFuelTanks);
this.groupBoxConfig.Controls.Add(this.checkBoxHasBombs);
this.groupBoxConfig.Controls.Add(this.numericUpDownWeight);
@ -183,34 +181,25 @@
this.panelRed.Size = new System.Drawing.Size(40, 40);
this.panelRed.TabIndex = 0;
//
// checkBoxSportLine
//
this.checkBoxSportLine.AutoSize = true;
this.checkBoxSportLine.Location = new System.Drawing.Point(22, 185);
this.checkBoxSportLine.Name = "checkBoxSportLine";
this.checkBoxSportLine.Size = new System.Drawing.Size(226, 19);
this.checkBoxSportLine.TabIndex = 13;
this.checkBoxSportLine.Text = "Признак наличия гоночной полосы";
this.checkBoxSportLine.UseVisualStyleBackColor = true;
//
// checkBoxHasFuelTanks
//
this.checkBoxHasFuelTanks.AutoSize = true;
this.checkBoxHasFuelTanks.Location = new System.Drawing.Point(22, 149);
this.checkBoxHasFuelTanks.Location = new System.Drawing.Point(22, 165);
this.checkBoxHasFuelTanks.Name = "checkBoxHasFuelTanks";
this.checkBoxHasFuelTanks.Size = new System.Drawing.Size(186, 19);
this.checkBoxHasFuelTanks.RightToLeft = System.Windows.Forms.RightToLeft.No;
this.checkBoxHasFuelTanks.Size = new System.Drawing.Size(222, 19);
this.checkBoxHasFuelTanks.TabIndex = 12;
this.checkBoxHasFuelTanks.Text = "Признак наличия антикрыла";
this.checkBoxHasFuelTanks.Text = "Признак наличия топливных баков";
this.checkBoxHasFuelTanks.UseVisualStyleBackColor = true;
//
// checkBoxHasBombs
//
this.checkBoxHasBombs.AutoSize = true;
this.checkBoxHasBombs.Location = new System.Drawing.Point(22, 115);
this.checkBoxHasBombs.Location = new System.Drawing.Point(22, 130);
this.checkBoxHasBombs.Name = "checkBoxHasBombs";
this.checkBoxHasBombs.Size = new System.Drawing.Size(164, 19);
this.checkBoxHasBombs.Size = new System.Drawing.Size(156, 19);
this.checkBoxHasBombs.TabIndex = 11;
this.checkBoxHasBombs.Text = "Признак наличия обвеса";
this.checkBoxHasBombs.Text = "Признак наличия бомб";
this.checkBoxHasBombs.UseVisualStyleBackColor = true;
//
// numericUpDownWeight
@ -298,6 +287,8 @@
this.labelDopColor.TabIndex = 2;
this.labelDopColor.Text = "Доп. цвет";
this.labelDopColor.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.labelDopColor.DragDrop += new System.Windows.Forms.DragEventHandler(this.LabelDopColor_DragDrop);
this.labelDopColor.DragEnter += new System.Windows.Forms.DragEventHandler(this.LabelDopColor_DragEnter);
//
// labelBaseColor
//
@ -309,6 +300,8 @@
this.labelBaseColor.TabIndex = 1;
this.labelBaseColor.Text = "Цвет";
this.labelBaseColor.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.labelBaseColor.DragDrop += new System.Windows.Forms.DragEventHandler(this.LabelBaseColor_DragDrop);
this.labelBaseColor.DragEnter += new System.Windows.Forms.DragEventHandler(this.LabelBaseColor_DragEnter);
//
// pictureBoxObject
//
@ -362,7 +355,6 @@
#endregion
private GroupBox groupBoxConfig;
private CheckBox checkBoxSportLine;
private CheckBox checkBoxHasFuelTanks;
private CheckBox checkBoxHasBombs;
private NumericUpDown numericUpDownWeight;

View File

@ -6,13 +6,13 @@
public partial class FormAirplaneConfig : Form
{
/// <summary>
/// Переменная-выбранная машина
/// Переменная-выбранная самолет
/// </summary>
DrawningAirplane _car = null;
DrawningAirplane _airplane = null;
/// <summary>
/// Событие
/// </summary>
private event AirplaneDelegate EventAddAirplane;
private event Action<DrawningAirplane> EventAddAirplane;
/// <summary>
/// Конструктор
/// </summary>
@ -28,28 +28,28 @@
panelYellow.MouseDown += PanelColor_MouseDown;
panelBlue.MouseDown += PanelColor_MouseDown;
// TODO buttonCancel.Click with lambda
buttonCancel.Click += (s, e) => Close();
}
/// <summary>
/// Отрисовать машину
/// Отрисовать самолет
/// </summary>
private void DrawAirplane()
{
Bitmap bmp = new(pictureBoxObject.Width, pictureBoxObject.Height);
Graphics gr = Graphics.FromImage(bmp);
_car?.SetPosition(5, 5, pictureBoxObject.Width, pictureBoxObject.Height);
_car?.DrawTransport(gr);
_airplane?.SetPosition(5, 5, pictureBoxObject.Width, pictureBoxObject.Height);
_airplane?.DrawTransport(gr);
pictureBoxObject.Image = bmp;
}
/// <summary>
/// Добавление события
/// </summary>
/// <param name="ev"></param>
public void AddEvent(AirplaneDelegate ev)
public void AddEvent(Action<DrawningAirplane> ev)
{
if (EventAddAirplane == null)
{
EventAddAirplane = new AirplaneDelegate(ev);
EventAddAirplane = new(ev);
}
else
{
@ -91,10 +91,10 @@
switch (e.Data.GetData(DataFormats.Text).ToString())
{
case "labelSimpleObject":
_car = new DrawningAirplane((int)numericUpDownSpeed.Value, (int)numericUpDownWeight.Value, Color.White);
_airplane = new DrawningAirplane((int)numericUpDownSpeed.Value, (int)numericUpDownWeight.Value, Color.White);
break;
case "labelModifiedObject":
_car = new DrawningAirBomber((int)numericUpDownSpeed.Value, (int)numericUpDownWeight.Value, Color.White, Color.Black,
_airplane = new DrawningAirBomber((int)numericUpDownSpeed.Value, (int)numericUpDownWeight.Value, Color.White, Color.Black,
checkBoxHasBombs.Checked, checkBoxHasFuelTanks.Checked);
break;
}
@ -110,13 +110,14 @@
(sender as Control).DoDragDrop((sender as Control).BackColor, DragDropEffects.Move | DragDropEffects.Copy);
}
/// <summary>
/// Проверка получаемой информации (ее типа на соответствие требуемому)
/// Установливает статус перемещаемых данных
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void LabelBaseColor_DragEnter(object sender, DragEventArgs e)
/// <param name="needTypeData">Тип которого должны соответствовать перемещаемые данные</param>
/// <param name="condition">условие на возможность копирования данных</param>
private void setDragEffect(DragEventArgs e, Type needTypeData, bool condition)
{
if (e.Data.GetDataPresent(typeof(Color)))
if (e.Data.GetDataPresent(needTypeData) && condition)
{
e.Effect = DragDropEffects.Copy;
}
@ -126,13 +127,30 @@
}
}
/// <summary>
/// Проверка получаемой информации (ее типа на соответствие требуемому)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void LabelBaseColor_DragEnter(object sender, DragEventArgs e) => setDragEffect(e, typeof(Color), _airplane != null);
private void LabelDopColor_DragEnter(object sender, DragEventArgs e) => setDragEffect(e, typeof(Color), _airplane != null && _airplane is DrawningAirBomber);
/// <summary>
/// Принимаем основной цвет
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void LabelBaseColor_DragDrop(object sender, DragEventArgs e)
{
// TODO Call method from object _car and set color
var color = (Color)e.Data.GetData(typeof(Color));
if (_airplane is DrawningAirBomber)
{
var a = (EntityAirBomber)_airplane.Airplane;
_airplane = new DrawningAirBomber(a.Speed, a.Weight, color, a.DopColor, a.HasBombs, a.HasFuelTanks);
}
else if (_airplane is DrawningAirplane)
{
_airplane = new DrawningAirplane(_airplane.Airplane.Speed, _airplane.Airplane.Weight, color);
}
DrawAirplane();
}
/// <summary>
/// Принимаем дополнительный цвет
@ -141,16 +159,22 @@
/// <param name="e"></param>
private void LabelDopColor_DragDrop(object sender, DragEventArgs e)
{
// TODO Call method from object _car if _car is DrawningSportAirplane and set dop color
var color = (Color)e.Data.GetData(typeof(Color));
if (_airplane is DrawningAirBomber)
{
var a = (EntityAirBomber)_airplane.Airplane;
_airplane = new DrawningAirBomber(a.Speed, a.Weight, a.BodyColor, color, a.HasBombs, a.HasFuelTanks);
}
DrawAirplane();
}
/// <summary>
/// Добавление машины
/// Добавление самолета
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonOk_Click(object sender, EventArgs e)
{
EventAddAirplane?.Invoke(_car);
EventAddAirplane?.Invoke(_airplane);
Close();
}
}

View File

@ -104,7 +104,7 @@
private void ButtonAddAirplane_Click(object sender, EventArgs e)
{
FormAirplaneConfig formAirplane = new();
formAirplane.AddEvent(new AirplaneDelegate(AddAirplane));
formAirplane.AddEvent(new(AddAirplane));
formAirplane.Show();
}
/// <summary>
@ -115,9 +115,9 @@
{
if (listBoxMaps.SelectedIndex == -1)
{
return;
MessageBox.Show("Перед добавлением объекта необходимо создать карту");
}
if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + new DrawningObject(airplane) != -1)
else if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + new DrawningObject(airplane) != -1)
{
MessageBox.Show("Объект добавлен");
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();