Работа с событиями.

This commit is contained in:
Anastasia 2022-11-14 16:28:46 +04:00
parent 8a6db0f091
commit 6a1814926f
8 changed files with 96 additions and 34 deletions

View File

@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AirplaneWithRadar
{
/// <summary>
/// Делегат для передачи объекта-самолета
/// </summary>
/// <param name="airplane"></param>
public delegate void AirplaneDelegate(DrawingAirplane airplane);
}

View File

@ -63,6 +63,12 @@ namespace AirplaneWithRadar
_airplaneWidth = airplaneWidth; _airplaneWidth = airplaneWidth;
_airplaneHeight = airplaneHeight; _airplaneHeight = airplaneHeight;
} }
public void SetColor(Color color)
{
Airplane.BodyColor = color;
}
/// <summary> /// <summary>
/// Установка позиции самолета /// Установка позиции самолета
/// </summary> /// </summary>

View File

@ -21,6 +21,12 @@ namespace AirplaneWithRadar
{ {
Airplane = new EntityAirplaneWithRadar(speed, weight, bodyColor, dopColor, radar, extraFuelTank); Airplane = new EntityAirplaneWithRadar(speed, weight, bodyColor, dopColor, radar, extraFuelTank);
} }
public void SetDopColor(Color dopColor)
{
((EntityAirplaneWithRadar)Airplane).DopColor = dopColor;
}
public override void DrawTransport(Graphics g) public override void DrawTransport(Graphics g)
{ {
if (Airplane is not EntityAirplaneWithRadar airplaneWithRadar) if (Airplane is not EntityAirplaneWithRadar airplaneWithRadar)

View File

@ -23,7 +23,7 @@ namespace AirplaneWithRadar
/// <summary> /// <summary>
/// Цвет кузова /// Цвет кузова
/// </summary> /// </summary>
public Color BodyColor { get; private set; } public Color BodyColor { get; set; }
/// <summary> /// <summary>
/// Шаг перемещения самолета /// Шаг перемещения самолета
/// </summary> /// </summary>

View File

@ -11,7 +11,7 @@ namespace AirplaneWithRadar
/// <summary> /// <summary>
/// Дополнительный цвет /// Дополнительный цвет
/// </summary> /// </summary>
public Color DopColor { get; private set; } public Color DopColor { get; set; }
/// <summary> /// <summary>
/// Признак наличия радара /// Признак наличия радара
/// </summary> /// </summary>

View File

@ -51,7 +51,7 @@
this.labelDopColor = new System.Windows.Forms.Label(); this.labelDopColor = new System.Windows.Forms.Label();
this.labelBaseColor = new System.Windows.Forms.Label(); this.labelBaseColor = new System.Windows.Forms.Label();
this.buttonOk = new System.Windows.Forms.Button(); this.buttonOk = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button(); this.buttonCancel = new System.Windows.Forms.Button();
this.groupBoxConfig.SuspendLayout(); this.groupBoxConfig.SuspendLayout();
this.groupBoxColors.SuspendLayout(); this.groupBoxColors.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.numericUpDownWeight)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.numericUpDownWeight)).BeginInit();
@ -124,7 +124,6 @@
this.panelBlack.Name = "panelBlack"; this.panelBlack.Name = "panelBlack";
this.panelBlack.Size = new System.Drawing.Size(50, 50); this.panelBlack.Size = new System.Drawing.Size(50, 50);
this.panelBlack.TabIndex = 1; this.panelBlack.TabIndex = 1;
this.panelBlack.MouseDown += new System.Windows.Forms.MouseEventHandler(this.PanelColor_MouseDown);
// //
// panelWhite // panelWhite
// //
@ -133,7 +132,6 @@
this.panelWhite.Name = "panelWhite"; this.panelWhite.Name = "panelWhite";
this.panelWhite.Size = new System.Drawing.Size(50, 50); this.panelWhite.Size = new System.Drawing.Size(50, 50);
this.panelWhite.TabIndex = 1; this.panelWhite.TabIndex = 1;
this.panelWhite.MouseDown += new System.Windows.Forms.MouseEventHandler(this.PanelColor_MouseDown);
// //
// panelHotPink // panelHotPink
// //
@ -142,7 +140,6 @@
this.panelHotPink.Name = "panelHotPink"; this.panelHotPink.Name = "panelHotPink";
this.panelHotPink.Size = new System.Drawing.Size(50, 50); this.panelHotPink.Size = new System.Drawing.Size(50, 50);
this.panelHotPink.TabIndex = 1; this.panelHotPink.TabIndex = 1;
this.panelHotPink.MouseDown += new System.Windows.Forms.MouseEventHandler(this.PanelColor_MouseDown);
// //
// panelBlueViolet // panelBlueViolet
// //
@ -151,7 +148,6 @@
this.panelBlueViolet.Name = "panelBlueViolet"; this.panelBlueViolet.Name = "panelBlueViolet";
this.panelBlueViolet.Size = new System.Drawing.Size(50, 50); this.panelBlueViolet.Size = new System.Drawing.Size(50, 50);
this.panelBlueViolet.TabIndex = 1; this.panelBlueViolet.TabIndex = 1;
this.panelBlueViolet.MouseDown += new System.Windows.Forms.MouseEventHandler(this.PanelColor_MouseDown);
// //
// panelDodgerBlue // panelDodgerBlue
// //
@ -160,7 +156,6 @@
this.panelDodgerBlue.Name = "panelDodgerBlue"; this.panelDodgerBlue.Name = "panelDodgerBlue";
this.panelDodgerBlue.Size = new System.Drawing.Size(50, 50); this.panelDodgerBlue.Size = new System.Drawing.Size(50, 50);
this.panelDodgerBlue.TabIndex = 1; this.panelDodgerBlue.TabIndex = 1;
this.panelDodgerBlue.MouseDown += new System.Windows.Forms.MouseEventHandler(this.PanelColor_MouseDown);
// //
// panelYellowGreen // panelYellowGreen
// //
@ -169,7 +164,6 @@
this.panelYellowGreen.Name = "panelYellowGreen"; this.panelYellowGreen.Name = "panelYellowGreen";
this.panelYellowGreen.Size = new System.Drawing.Size(50, 50); this.panelYellowGreen.Size = new System.Drawing.Size(50, 50);
this.panelYellowGreen.TabIndex = 1; this.panelYellowGreen.TabIndex = 1;
this.panelYellowGreen.MouseDown += new System.Windows.Forms.MouseEventHandler(this.PanelColor_MouseDown);
// //
// panelGold // panelGold
// //
@ -178,7 +172,6 @@
this.panelGold.Name = "panelGold"; this.panelGold.Name = "panelGold";
this.panelGold.Size = new System.Drawing.Size(50, 50); this.panelGold.Size = new System.Drawing.Size(50, 50);
this.panelGold.TabIndex = 1; this.panelGold.TabIndex = 1;
this.panelGold.MouseDown += new System.Windows.Forms.MouseEventHandler(this.PanelColor_MouseDown);
// //
// panelRed // panelRed
// //
@ -187,7 +180,6 @@
this.panelRed.Name = "panelRed"; this.panelRed.Name = "panelRed";
this.panelRed.Size = new System.Drawing.Size(50, 50); this.panelRed.Size = new System.Drawing.Size(50, 50);
this.panelRed.TabIndex = 0; this.panelRed.TabIndex = 0;
this.panelRed.MouseDown += new System.Windows.Forms.MouseEventHandler(this.PanelColor_MouseDown);
// //
// checkBoxExtraFuelTank // checkBoxExtraFuelTank
// //
@ -306,22 +298,23 @@
this.buttonOk.TabIndex = 3; this.buttonOk.TabIndex = 3;
this.buttonOk.Text = "Добавить"; this.buttonOk.Text = "Добавить";
this.buttonOk.UseVisualStyleBackColor = true; this.buttonOk.UseVisualStyleBackColor = true;
this.buttonOk.Click += new System.EventHandler(this.ButtonOk_Click);
// //
// button2 // buttonCancel
// //
this.button2.Location = new System.Drawing.Point(822, 284); this.buttonCancel.Location = new System.Drawing.Point(822, 284);
this.button2.Name = "button2"; this.buttonCancel.Name = "buttonCancel";
this.button2.Size = new System.Drawing.Size(112, 34); this.buttonCancel.Size = new System.Drawing.Size(112, 34);
this.button2.TabIndex = 4; this.buttonCancel.TabIndex = 4;
this.button2.Text = "Отмена"; this.buttonCancel.Text = "Отмена";
this.button2.UseVisualStyleBackColor = true; this.buttonCancel.UseVisualStyleBackColor = true;
// //
// FormAirplaneConfig // FormAirplaneConfig
// //
this.AutoScaleDimensions = new System.Drawing.SizeF(10F, 25F); this.AutoScaleDimensions = new System.Drawing.SizeF(10F, 25F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1025, 330); this.ClientSize = new System.Drawing.Size(1025, 330);
this.Controls.Add(this.button2); this.Controls.Add(this.buttonCancel);
this.Controls.Add(this.buttonOk); this.Controls.Add(this.buttonOk);
this.Controls.Add(this.panelObject); this.Controls.Add(this.panelObject);
this.Controls.Add(this.groupBoxConfig); this.Controls.Add(this.groupBoxConfig);
@ -363,6 +356,6 @@
private Label labelDopColor; private Label labelDopColor;
private Label labelBaseColor; private Label labelBaseColor;
private Button buttonOk; private Button buttonOk;
private Button button2; private Button buttonCancel;
} }
} }

View File

@ -20,6 +20,10 @@ namespace AirplaneWithRadar
/// </summary> /// </summary>
DrawingAirplane _airplane = null; DrawingAirplane _airplane = null;
/// <summary> /// <summary>
/// Событие
/// </summary>
private event Action<DrawingAirplane> EventAddAirplane;
/// <summary>
/// Конструктор /// Конструктор
/// </summary> /// </summary>
public FormAirplaneConfig() public FormAirplaneConfig()
@ -34,6 +38,7 @@ namespace AirplaneWithRadar
panelWhite.MouseDown += PanelColor_MouseDown; panelWhite.MouseDown += PanelColor_MouseDown;
panelBlack.MouseDown += PanelColor_MouseDown; panelBlack.MouseDown += PanelColor_MouseDown;
// TODO buttonCancel.Click with lambda // TODO buttonCancel.Click with lambda
buttonCancel.Click += (object sender, EventArgs e) => Close();
} }
/// <summary> /// <summary>
@ -48,6 +53,21 @@ namespace AirplaneWithRadar
pictureBoxObject.Image = bmp; pictureBoxObject.Image = bmp;
} }
/// <summary> /// <summary>
/// Добавление события
/// </summary>
/// <param name="ev"></param>
public void AddEvent(Action<DrawingAirplane> ev)
{
if (EventAddAirplane == null)
{
EventAddAirplane = ev;
}
else
{
EventAddAirplane += ev;
}
}
/// <summary>
/// Передаем информацию при нажатии на Label /// Передаем информацию при нажатии на Label
/// </summary> /// </summary>
/// <param name="sender"></param> /// <param name="sender"></param>
@ -98,8 +118,7 @@ namespace AirplaneWithRadar
/// <param name="e"></param> /// <param name="e"></param>
private void PanelColor_MouseDown(object sender, MouseEventArgs e) private void PanelColor_MouseDown(object sender, MouseEventArgs e)
{ {
(sender as Control).DoDragDrop((sender as Control).BackColor, (sender as Control).DoDragDrop((sender as Control).BackColor, DragDropEffects.Move | DragDropEffects.Copy);
DragDropEffects.Move | DragDropEffects.Copy);
} }
/// <summary> /// <summary>
/// Проверка получаемой информации (ее типа на соответствие требуемому) /// Проверка получаемой информации (ее типа на соответствие требуемому)
@ -125,6 +144,9 @@ namespace AirplaneWithRadar
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 // TODO Call method from object _car and set color
Color BodyColor = (Color)e.Data.GetData(typeof(Color));
_airplane.SetColor(BodyColor);
DrawAirplane();
} }
/// <summary> /// <summary>
/// Принимаем дополнительный цвет /// Принимаем дополнительный цвет
@ -134,6 +156,22 @@ namespace AirplaneWithRadar
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 DrawningSportCar and set dop color // TODO Call method from object _car if _car is DrawningSportCar and set dop color
Color ModifColor = (Color)e.Data.GetData(typeof(Color));
if (_airplane is DrawingAirplaneWithRadar airplaneWithRadar)
{
airplaneWithRadar.SetDopColor(ModifColor);
DrawAirplane();
}
}
/// <summary>
/// Добавление машины
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonOk_Click(object sender, EventArgs e)
{
EventAddAirplane?.Invoke(_airplane);
Close();
} }
} }

View File

@ -17,6 +17,7 @@ namespace AirplaneWithRadar
/// Объект от коллекции карт /// Объект от коллекции карт
/// </summary> /// </summary>
private readonly MapsCollection _mapsCollection; private readonly MapsCollection _mapsCollection;
private Action<DrawingAirplane> AddAction;
/// <summary> /// <summary>
/// Конструктор /// Конструктор
/// </summary> /// </summary>
@ -102,24 +103,28 @@ namespace AirplaneWithRadar
/// <param name="sender"></param> /// <param name="sender"></param>
/// <param name="e"></param> /// <param name="e"></param>
private void ButtonAddAirplane_Click(object sender, EventArgs e) private void ButtonAddAirplane_Click(object sender, EventArgs e)
{
// TODO Call method AddEvent from formCarConfig
var formAirplaneConfig = new FormAirplaneConfig();
formAirplaneConfig.AddEvent(AddAirplaneOnMap);
formAirplaneConfig.Show();
}
private void AddAirplaneOnMap(DrawingAirplane drawingAirplane)
{ {
if (listBoxMaps.SelectedIndex == -1) if (listBoxMaps.SelectedIndex == -1)
{ {
return; return;
} }
FormAirplaneWithRadar form = new(); DrawingObjectAirplane airplane = new(drawingAirplane);
if (form.ShowDialog() == DialogResult.OK) if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + airplane != -1)
{ {
DrawingObjectAirplane airplane = new(form.SelectedAirplane); MessageBox.Show("Объект добавлен");
if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + airplane != -1) pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
{ }
MessageBox.Show("Объект добавлен"); else
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); {
} MessageBox.Show("Не удалось добавить объект");
else
{
MessageBox.Show("Не удалось добавить объект");
}
} }
} }
/// <summary> /// <summary>