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

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;
_airplaneHeight = airplaneHeight;
}
public void SetColor(Color color)
{
Airplane.BodyColor = color;
}
/// <summary>
/// Установка позиции самолета
/// </summary>

View File

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

View File

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

View File

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

View File

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

View File

@ -20,6 +20,10 @@ namespace AirplaneWithRadar
/// </summary>
DrawingAirplane _airplane = null;
/// <summary>
/// Событие
/// </summary>
private event Action<DrawingAirplane> EventAddAirplane;
/// <summary>
/// Конструктор
/// </summary>
public FormAirplaneConfig()
@ -34,6 +38,7 @@ namespace AirplaneWithRadar
panelWhite.MouseDown += PanelColor_MouseDown;
panelBlack.MouseDown += PanelColor_MouseDown;
// TODO buttonCancel.Click with lambda
buttonCancel.Click += (object sender, EventArgs e) => Close();
}
/// <summary>
@ -48,6 +53,21 @@ namespace AirplaneWithRadar
pictureBoxObject.Image = bmp;
}
/// <summary>
/// Добавление события
/// </summary>
/// <param name="ev"></param>
public void AddEvent(Action<DrawingAirplane> ev)
{
if (EventAddAirplane == null)
{
EventAddAirplane = ev;
}
else
{
EventAddAirplane += ev;
}
}
/// <summary>
/// Передаем информацию при нажатии на Label
/// </summary>
/// <param name="sender"></param>
@ -98,8 +118,7 @@ namespace AirplaneWithRadar
/// <param name="e"></param>
private void PanelColor_MouseDown(object sender, MouseEventArgs e)
{
(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>
/// Проверка получаемой информации (ее типа на соответствие требуемому)
@ -125,6 +144,9 @@ namespace AirplaneWithRadar
private void LabelBaseColor_DragDrop(object sender, DragEventArgs e)
{
// TODO Call method from object _car and set color
Color BodyColor = (Color)e.Data.GetData(typeof(Color));
_airplane.SetColor(BodyColor);
DrawAirplane();
}
/// <summary>
/// Принимаем дополнительный цвет
@ -134,6 +156,22 @@ namespace AirplaneWithRadar
private void LabelDopColor_DragDrop(object sender, DragEventArgs e)
{
// 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>
private readonly MapsCollection _mapsCollection;
private Action<DrawingAirplane> AddAction;
/// <summary>
/// Конструктор
/// </summary>
@ -102,24 +103,28 @@ namespace AirplaneWithRadar
/// <param name="sender"></param>
/// <param name="e"></param>
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)
{
return;
}
FormAirplaneWithRadar form = new();
if (form.ShowDialog() == DialogResult.OK)
DrawingObjectAirplane airplane = new(drawingAirplane);
if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + airplane != -1)
{
DrawingObjectAirplane airplane = new(form.SelectedAirplane);
if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + airplane != -1)
{
MessageBox.Show("Объект добавлен");
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
else
{
MessageBox.Show("Не удалось добавить объект");
}
MessageBox.Show("Объект добавлен");
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
else
{
MessageBox.Show("Не удалось добавить объект");
}
}
/// <summary>