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