PIbd-13_Ladyagin_P.D._Simple/AirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplaneWithRadar.cs

141 lines
4.2 KiB
C#
Raw Permalink Normal View History

2024-03-23 15:26:35 +04:00
using ProjectAirplaneWithRadar.Drawnings;
2024-03-23 15:58:03 +04:00
using ProjectAirplaneWithRadar.MovementStrategy;
2024-03-23 15:11:25 +04:00
namespace ProjectAirplaneWithRadar
2024-02-10 18:11:15 +04:00
{
/// <summary>
/// Форма работы с объектом "Самолет с радаром"
/// </summary>
2024-02-10 18:11:15 +04:00
public partial class FormAirplaneWithRadar : Form
{
/// <summary>
/// Поле-объект для происовки объект
/// </summary>
2024-03-23 15:58:03 +04:00
private DrawningAirplane? _drawingAirplane;
/// <summary>
/// Стратегия перемещения
/// </summary>
private AbstractStrategy? _strategy;
2024-03-23 18:42:24 +04:00
public DrawningAirplane SetAirplane
2024-02-10 18:11:15 +04:00
{
2024-03-23 18:42:24 +04:00
set
{
_drawingAirplane = value;
_drawingAirplane.SetPictureSize(pictureBoxAirplaneWithRadar.Width, pictureBoxAirplaneWithRadar.Height);
comboBoxStrategy.Enabled = true;
_strategy = null;
UpdatePlane();
}
2024-02-10 18:11:15 +04:00
}
/// <summary>
2024-03-23 18:42:24 +04:00
/// Конструктор формы
/// </summary>
2024-03-23 18:42:24 +04:00
public FormAirplaneWithRadar()
2024-02-10 18:11:15 +04:00
{
2024-03-23 18:42:24 +04:00
InitializeComponent();
2024-03-23 15:58:03 +04:00
_strategy = null;
2024-02-10 18:11:15 +04:00
}
2024-03-23 15:58:03 +04:00
/// <summary>
/// Метод прорисовки самолета
/// </summary>
2024-02-10 18:11:15 +04:00
private void UpdatePlane()
{
2024-03-23 15:58:03 +04:00
if (_drawingAirplane == null)
{
return;
}
2024-02-10 18:11:15 +04:00
Bitmap bmp = new(pictureBoxAirplaneWithRadar.Width, pictureBoxAirplaneWithRadar.Height);
Graphics gr = Graphics.FromImage(bmp);
2024-03-23 15:58:03 +04:00
_drawingAirplane?.DrawTransport(gr);
2024-02-10 18:11:15 +04:00
pictureBoxAirplaneWithRadar.Image = bmp;
}
/// <summary>
/// Перемещение объекта по форме (нажатие кнопок навигации)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
2024-02-10 18:11:15 +04:00
private void ButtonMove_Click(object sender, EventArgs e)
{
2024-03-23 15:58:03 +04:00
if (_drawingAirplane == null)
2024-02-10 18:11:15 +04:00
{
return;
}
if (sender is Button button)
{
string name = button.Name;
DirectionType result;
switch (name)
{
case "buttonUp":
result = DirectionType.Up;
break;
case "buttonDown":
result = DirectionType.Down;
break;
case "buttonLeft":
result = DirectionType.Left;
break;
case "buttonRight":
result = DirectionType.Right;
break;
default:
return;
}
2024-03-23 15:58:03 +04:00
_drawingAirplane.MoveTransport(result);
2024-02-10 18:11:15 +04:00
UpdatePlane();
}
2024-03-23 15:58:03 +04:00
}
/// <summary>
/// Обработка нажатия кнопки "Шаг"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonStrategyStep_Click(object sender, EventArgs e)
{
if (_drawingAirplane == null)
{
return;
}
if (comboBoxStrategy.Enabled)
{
_strategy = comboBoxStrategy.SelectedIndex switch
{
0 => new MoveToCenter(),
1 => new MoveToBorder(),
_ => null,
};
if (_strategy == null)
{
return;
}
_strategy.SetData(new MoveablePlane(_drawingAirplane), pictureBoxAirplaneWithRadar.Width, pictureBoxAirplaneWithRadar.Height);
}
if (_strategy == null)
{
return;
}
comboBoxStrategy.Enabled = false;
_strategy.MakeStep();
UpdatePlane();
if (_strategy.GetStatus() == StrategyStatus.Finish)
{
comboBoxStrategy.Enabled = true;
_strategy = null;
}
}
2024-02-10 18:11:15 +04:00
}
2024-03-23 15:58:03 +04:00
}