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

141 lines
4.2 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using ProjectAirplaneWithRadar.Drawnings;
using ProjectAirplaneWithRadar.MovementStrategy;
namespace ProjectAirplaneWithRadar
{
/// <summary>
/// Форма работы с объектом "Самолет с радаром"
/// </summary>
public partial class FormAirplaneWithRadar : Form
{
/// <summary>
/// Поле-объект для происовки объект
/// </summary>
private DrawningAirplane? _drawingAirplane;
/// <summary>
/// Стратегия перемещения
/// </summary>
private AbstractStrategy? _strategy;
public DrawningAirplane SetAirplane
{
set
{
_drawingAirplane = value;
_drawingAirplane.SetPictureSize(pictureBoxAirplaneWithRadar.Width, pictureBoxAirplaneWithRadar.Height);
comboBoxStrategy.Enabled = true;
_strategy = null;
UpdatePlane();
}
}
/// <summary>
/// Конструктор формы
/// </summary>
public FormAirplaneWithRadar()
{
InitializeComponent();
_strategy = null;
}
/// <summary>
/// Метод прорисовки самолета
/// </summary>
private void UpdatePlane()
{
if (_drawingAirplane == null)
{
return;
}
Bitmap bmp = new(pictureBoxAirplaneWithRadar.Width, pictureBoxAirplaneWithRadar.Height);
Graphics gr = Graphics.FromImage(bmp);
_drawingAirplane?.DrawTransport(gr);
pictureBoxAirplaneWithRadar.Image = bmp;
}
/// <summary>
/// Перемещение объекта по форме (нажатие кнопок навигации)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonMove_Click(object sender, EventArgs e)
{
if (_drawingAirplane == null)
{
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;
}
_drawingAirplane.MoveTransport(result);
UpdatePlane();
}
}
/// <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;
}
}
}
}