ISEbd-12_Sinelnikova_A.V._S.../ProjectCruiser/FormCruiser.cs
2024-03-21 10:52:11 +04:00

140 lines
4.0 KiB
C#

using ProjectCruiser.Drawnings;
using ProjectCruiser.MovementStrategy;
namespace ProjectCruiser
{
public partial class FormCruiser : Form
{
/// <summary>
/// Поле-объект для прорисовки объекта
/// </summary>
private DrawningCruiser? _drawningCruiser;
/// <summary>
/// Стратегия перемещения
/// </summary>
private AbstractStrategy? _strategy;
/// <summary>
/// Получение объекта
/// </summary>
public DrawningCruiser SetCruiser
{
set
{
_drawningCruiser = value;
_drawningCruiser.SetPictureSize(pictureBoxCruiser.Width,
pictureBoxCruiser.Height);
comboBoxStrategy.Enabled = true;
_strategy = null;
Draw();
}
}
/// <summary>
/// Конструктор формы
/// </summary>
public FormCruiser()
{
InitializeComponent();
_strategy = null;
}
/// <summary>
/// Метод прорисовки круисера
/// </summary>
private void Draw()
{
if (_drawningCruiser == null)
{
return;
}
Bitmap bmp = new(pictureBoxCruiser.Width,
pictureBoxCruiser.Height);
Graphics gr = Graphics.FromImage(bmp);
_drawningCruiser.DrawTransport(gr);
pictureBoxCruiser.Image = bmp;
}
/// <summary>
/// Перемещение объекта по форме (нажатие кнопок навигации)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonMove_Click(object sender, EventArgs e)
{
if (_drawningCruiser == null)
{
return;
}
string name = ((Button)sender)?.Name ?? string.Empty;
bool result = false;
switch (name)
{
case "buttonUp":
result = _drawningCruiser.MoveTransport(DirectionType.Up);
break;
case "buttonDown":
result = _drawningCruiser.MoveTransport(DirectionType.Down);
break;
case "buttonLeft":
result = _drawningCruiser.MoveTransport(DirectionType.Left);
break;
case "buttonRight":
result =
_drawningCruiser.MoveTransport(DirectionType.Right);
break;
}
if (result)
{
Draw();
}
}
/// <summary>
/// обработка нажатия кнопки шаг
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonStrategyStep_Click(object sender, EventArgs e)
{
if (_drawningCruiser == null)
{
return;
}
if (comboBoxStrategy.Enabled)
{
_strategy = comboBoxStrategy.SelectedIndex switch
{
0 => new MoveToCenter(),
1 => new MoveToBorder(),
_ => null,
};
if (_strategy == null)
{
return;
}
_strategy.SetData(new MoveableCruiser(_drawningCruiser), pictureBoxCruiser.Width, pictureBoxCruiser.Height);
}
if (_strategy == null)
{
return;
}
comboBoxStrategy.Enabled = false;
_strategy.MakeStep();
Draw();
if (_strategy.GetStatus() == StrategyStatus.Finish)
{
comboBoxStrategy.Enabled = true;
_strategy = null;
}
}
}
}