134 lines
3.5 KiB
C#
Raw 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 ProjectGasolineTanker.Drawings;
using ProjectGasolineTanker.MovementStrategy;
namespace ProjectGasolineTanker;
/// <summary>
/// Форма работы с объектом "Бензовоз"
/// </summary>
public partial class FormGasolineTanker : Form
{
/// <summary>
/// Поле-объект для прорисовки объекта
/// </summary>
private DrawingTruck? _drawingTruck;
/// <summary>
/// Стратегия перемещения
/// </summary>
private AbstractStrategy? _strategy;
/// <summary>
/// Получение объекта
/// </summary>
public DrawingTruck SetTruck
{
set
{
_drawingTruck = value;
_drawingTruck.SetPictureSize(pictureBoxGasolineTanker.Width, pictureBoxGasolineTanker.Height);
comboBoxStrategy.Enabled = true;
_strategy = null;
Draw();
}
}
/// <summary>
/// Конструктор формы
/// </summary>
public FormGasolineTanker()
{
InitializeComponent();
_strategy = null;
}
/// <summary>
/// Метод отрисовки бензовоза
/// </summary>
private void Draw()
{
if (_drawingTruck == null)
{
return;
}
Bitmap bmp = new(pictureBoxGasolineTanker.Width, pictureBoxGasolineTanker.Height);
Graphics gr = Graphics.FromImage(bmp);
_drawingTruck.DrawTransport(gr);
pictureBoxGasolineTanker.Image = bmp;
}
/// <summary>
/// Перемещение объекта по форме (кнопки навигации)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonMove_Click(object sender, EventArgs e)
{
if (_drawingTruck == null)
{
return;
}
string name = ((Button)sender)?.Name ?? string.Empty;
bool result = false;
switch (name)
{
case "buttonUp":
result = _drawingTruck.MoveTransport(DirectionType.Up);
break;
case "buttonDown":
result = _drawingTruck.MoveTransport(DirectionType.Down);
break;
case "buttonRight":
result = _drawingTruck.MoveTransport(DirectionType.Right);
break;
case "buttonLeft":
result = _drawingTruck.MoveTransport(DirectionType.Left);
break;
}
if (result)
{
Draw();
}
}
private void ButtonStrategyStep_Click(object sender, EventArgs e)
{
if (_drawingTruck == null)
{
return;
}
if (comboBoxStrategy.Enabled)
{
_strategy = comboBoxStrategy.SelectedIndex switch
{
0 => new MoveToCenter(),
1 => new MoveToBorder(),
_ => null,
};
if (_strategy == null)
{
return;
}
_strategy.SetData(new MoveableTruck(_drawingTruck), pictureBoxGasolineTanker.Width, pictureBoxGasolineTanker.Height);
}
if (_strategy == null)
{
return;
}
comboBoxStrategy.Enabled = false;
_strategy.MakeStep();
Draw();
if (_strategy.GetStatus() == StrategyStatus.Finish)
{
comboBoxStrategy.Enabled = true;
_strategy = null;
}
}
}