Реализация функции 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.panelGreen = new System.Windows.Forms.Panel();
this.panelWhite = new System.Windows.Forms.Panel(); this.panelWhite = new System.Windows.Forms.Panel();
this.panelRed = 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.checkBoxHasFuelTanks = new System.Windows.Forms.CheckBox();
this.checkBoxHasBombs = new System.Windows.Forms.CheckBox(); this.checkBoxHasBombs = new System.Windows.Forms.CheckBox();
this.numericUpDownWeight = new System.Windows.Forms.NumericUpDown(); this.numericUpDownWeight = new System.Windows.Forms.NumericUpDown();
@ -66,7 +65,6 @@
this.groupBoxConfig.Controls.Add(this.labelModifiedObject); this.groupBoxConfig.Controls.Add(this.labelModifiedObject);
this.groupBoxConfig.Controls.Add(this.labelSimpleObject); this.groupBoxConfig.Controls.Add(this.labelSimpleObject);
this.groupBoxConfig.Controls.Add(this.groupBoxColors); this.groupBoxConfig.Controls.Add(this.groupBoxColors);
this.groupBoxConfig.Controls.Add(this.checkBoxSportLine);
this.groupBoxConfig.Controls.Add(this.checkBoxHasFuelTanks); this.groupBoxConfig.Controls.Add(this.checkBoxHasFuelTanks);
this.groupBoxConfig.Controls.Add(this.checkBoxHasBombs); this.groupBoxConfig.Controls.Add(this.checkBoxHasBombs);
this.groupBoxConfig.Controls.Add(this.numericUpDownWeight); this.groupBoxConfig.Controls.Add(this.numericUpDownWeight);
@ -183,34 +181,25 @@
this.panelRed.Size = new System.Drawing.Size(40, 40); this.panelRed.Size = new System.Drawing.Size(40, 40);
this.panelRed.TabIndex = 0; 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 // checkBoxHasFuelTanks
// //
this.checkBoxHasFuelTanks.AutoSize = true; 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.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.TabIndex = 12;
this.checkBoxHasFuelTanks.Text = "Признак наличия антикрыла"; this.checkBoxHasFuelTanks.Text = "Признак наличия топливных баков";
this.checkBoxHasFuelTanks.UseVisualStyleBackColor = true; this.checkBoxHasFuelTanks.UseVisualStyleBackColor = true;
// //
// checkBoxHasBombs // checkBoxHasBombs
// //
this.checkBoxHasBombs.AutoSize = true; 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.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.TabIndex = 11;
this.checkBoxHasBombs.Text = "Признак наличия обвеса"; this.checkBoxHasBombs.Text = "Признак наличия бомб";
this.checkBoxHasBombs.UseVisualStyleBackColor = true; this.checkBoxHasBombs.UseVisualStyleBackColor = true;
// //
// numericUpDownWeight // numericUpDownWeight
@ -298,6 +287,8 @@
this.labelDopColor.TabIndex = 2; this.labelDopColor.TabIndex = 2;
this.labelDopColor.Text = "Доп. цвет"; this.labelDopColor.Text = "Доп. цвет";
this.labelDopColor.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; 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 // labelBaseColor
// //
@ -309,6 +300,8 @@
this.labelBaseColor.TabIndex = 1; this.labelBaseColor.TabIndex = 1;
this.labelBaseColor.Text = "Цвет"; this.labelBaseColor.Text = "Цвет";
this.labelBaseColor.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; 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 // pictureBoxObject
// //
@ -362,7 +355,6 @@
#endregion #endregion
private GroupBox groupBoxConfig; private GroupBox groupBoxConfig;
private CheckBox checkBoxSportLine;
private CheckBox checkBoxHasFuelTanks; private CheckBox checkBoxHasFuelTanks;
private CheckBox checkBoxHasBombs; private CheckBox checkBoxHasBombs;
private NumericUpDown numericUpDownWeight; private NumericUpDown numericUpDownWeight;

View File

@ -6,13 +6,13 @@
public partial class FormAirplaneConfig : Form public partial class FormAirplaneConfig : Form
{ {
/// <summary> /// <summary>
/// Переменная-выбранная машина /// Переменная-выбранная самолет
/// </summary> /// </summary>
DrawningAirplane _car = null; DrawningAirplane _airplane = null;
/// <summary> /// <summary>
/// Событие /// Событие
/// </summary> /// </summary>
private event AirplaneDelegate EventAddAirplane; private event Action<DrawningAirplane> EventAddAirplane;
/// <summary> /// <summary>
/// Конструктор /// Конструктор
/// </summary> /// </summary>
@ -28,28 +28,28 @@
panelYellow.MouseDown += PanelColor_MouseDown; panelYellow.MouseDown += PanelColor_MouseDown;
panelBlue.MouseDown += PanelColor_MouseDown; panelBlue.MouseDown += PanelColor_MouseDown;
// TODO buttonCancel.Click with lambda buttonCancel.Click += (s, e) => Close();
} }
/// <summary> /// <summary>
/// Отрисовать машину /// Отрисовать самолет
/// </summary> /// </summary>
private void DrawAirplane() private void DrawAirplane()
{ {
Bitmap bmp = new(pictureBoxObject.Width, pictureBoxObject.Height); Bitmap bmp = new(pictureBoxObject.Width, pictureBoxObject.Height);
Graphics gr = Graphics.FromImage(bmp); Graphics gr = Graphics.FromImage(bmp);
_car?.SetPosition(5, 5, pictureBoxObject.Width, pictureBoxObject.Height); _airplane?.SetPosition(5, 5, pictureBoxObject.Width, pictureBoxObject.Height);
_car?.DrawTransport(gr); _airplane?.DrawTransport(gr);
pictureBoxObject.Image = bmp; pictureBoxObject.Image = bmp;
} }
/// <summary> /// <summary>
/// Добавление события /// Добавление события
/// </summary> /// </summary>
/// <param name="ev"></param> /// <param name="ev"></param>
public void AddEvent(AirplaneDelegate ev) public void AddEvent(Action<DrawningAirplane> ev)
{ {
if (EventAddAirplane == null) if (EventAddAirplane == null)
{ {
EventAddAirplane = new AirplaneDelegate(ev); EventAddAirplane = new(ev);
} }
else else
{ {
@ -91,10 +91,10 @@
switch (e.Data.GetData(DataFormats.Text).ToString()) switch (e.Data.GetData(DataFormats.Text).ToString())
{ {
case "labelSimpleObject": 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; break;
case "labelModifiedObject": 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); checkBoxHasBombs.Checked, checkBoxHasFuelTanks.Checked);
break; break;
} }
@ -110,13 +110,14 @@
(sender as Control).DoDragDrop((sender as Control).BackColor, DragDropEffects.Move | DragDropEffects.Copy); (sender as Control).DoDragDrop((sender as Control).BackColor, DragDropEffects.Move | DragDropEffects.Copy);
} }
/// <summary> /// <summary>
/// Проверка получаемой информации (ее типа на соответствие требуемому) /// Установливает статус перемещаемых данных
/// </summary> /// </summary>
/// <param name="sender"></param>
/// <param name="e"></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; e.Effect = DragDropEffects.Copy;
} }
@ -126,13 +127,30 @@
} }
} }
/// <summary> /// <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> /// </summary>
/// <param name="sender"></param> /// <param name="sender"></param>
/// <param name="e"></param> /// <param name="e"></param>
private void LabelBaseColor_DragDrop(object sender, DragEventArgs e) 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> /// <summary>
/// Принимаем дополнительный цвет /// Принимаем дополнительный цвет
@ -141,16 +159,22 @@
/// <param name="e"></param> /// <param name="e"></param>
private void LabelDopColor_DragDrop(object sender, DragEventArgs e) 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>
/// Добавление машины /// Добавление самолета
/// </summary> /// </summary>
/// <param name="sender"></param> /// <param name="sender"></param>
/// <param name="e"></param> /// <param name="e"></param>
private void ButtonOk_Click(object sender, EventArgs e) private void ButtonOk_Click(object sender, EventArgs e)
{ {
EventAddAirplane?.Invoke(_car); EventAddAirplane?.Invoke(_airplane);
Close(); Close();
} }
} }

View File

@ -104,7 +104,7 @@
private void ButtonAddAirplane_Click(object sender, EventArgs e) private void ButtonAddAirplane_Click(object sender, EventArgs e)
{ {
FormAirplaneConfig formAirplane = new(); FormAirplaneConfig formAirplane = new();
formAirplane.AddEvent(new AirplaneDelegate(AddAirplane)); formAirplane.AddEvent(new(AddAirplane));
formAirplane.Show(); formAirplane.Show();
} }
/// <summary> /// <summary>
@ -115,9 +115,9 @@
{ {
if (listBoxMaps.SelectedIndex == -1) 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("Объект добавлен"); MessageBox.Show("Объект добавлен");
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();