PIbd-11_Tarasov_V.D._Simple/ProjectMonorail/FormMonorail.cs
2024-03-10 19:05:31 +04:00

126 lines
3.9 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 ProjectMonorail.Scripts.Monorail.Drawnings;
using ProjectMonorail.Scripts.Monorail.MovementStrategy;
namespace ProjectMonorail
{
/// <summary>
/// Форма работы с объектом "Монорельс"
/// </summary>
public partial class FormMonorail : Form
{
/// <summary>
/// Поле-объект для прорисовки объекта
/// </summary>
private DrawingMonorail? _drawningMonorail;
/// <summary>
/// Стратегия перемещения
/// </summary>
private AbstractStrategy? _strategy;
public DrawingMonorail SetMonorail
{
set
{
_drawningMonorail = value;
_drawningMonorail.SetPictureSize(pictureBoxMonorail.Width, pictureBoxMonorail.Height);
Random random = new Random();
_drawningMonorail.SetPosition(random.Next(0, 100), random.Next(0, 100));
comboBoxStrategy.Enabled = true;
_strategy = null;
Draw();
}
}
/// <summary>
/// Конструктор формы
/// </summary>
public FormMonorail()
{
InitializeComponent();
_strategy = null;
}
/// <summary>
/// Метод для прорисовки автомобиля
/// </summary>
private void Draw()
{
if (_drawningMonorail == null) return;
Bitmap bmp = new(pictureBoxMonorail.Width, pictureBoxMonorail.Height);
Graphics gr = Graphics.FromImage(bmp);
_drawningMonorail.DrawTransport(gr);
pictureBoxMonorail.Image = bmp;
}
/// <summary>
/// Обработка кнопок перемешения
/// </summary>
private void ButtonMove_Click(object sender, EventArgs e)
{
if (_drawningMonorail == null) return;
string name = ((Button)sender).Name ?? string.Empty;
bool result = false;
switch (name)
{
case "buttonMove_Up":
result = _drawningMonorail.MoveTransport(DirectionType.Up);
break;
case "buttonMove_Down":
result = _drawningMonorail.MoveTransport(DirectionType.Down);
break;
case "buttonMove_Right":
result = _drawningMonorail.MoveTransport(DirectionType.Right);
break;
case "buttonMove_Left":
result = _drawningMonorail.MoveTransport(DirectionType.Left);
break;
}
if (result) Draw();
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonStrategyStep_Click(object sender, EventArgs e)
{
if (_drawningMonorail == null) return;
if (comboBoxStrategy.Enabled)
{
_strategy = comboBoxStrategy.SelectedIndex switch
{
0 => new MoveToCenter(),
1 => new MoveToRightDownBorder(),
_ => null,
};
if (_strategy == null)
{
return;
}
_strategy.SetData(new MoveableMonorail(_drawningMonorail), pictureBoxMonorail.Width, pictureBoxMonorail.Height);
}
if (_strategy == null)
{
return;
}
comboBoxStrategy.Enabled = false;
_strategy.MakeStep();
Draw();
if (_strategy.GetStatus() == StrategyStatus.Finish)
{
comboBoxStrategy.Enabled = true;
_strategy = null;
}
}
}
}