PIBD14-BOYKO-M.S.SuperEasyS.../ProjectElectroTrans/FormElectroTrans.cs
2024-03-13 19:49:07 +04:00

139 lines
3.5 KiB
C#

using ProjectElectroTrans.MovementStrategy;
using ProjectElectroTrans.Drawnings;
namespace ProjectElectroTrans;
/// <summary>
/// Ôîðìà ðàáîòû ñ îáúåêòîì "Ýëåêòðîïîåçä"
/// </summary>
public partial class FormElectroTrans : Form
{
/// <summary>
/// Ïîëå-îáúåêò äëÿ ïðîðèñîâêè îáúåêòà
/// </summary>
private DrawingTrans? _drawningTrans;
/// <summary>
/// Ñòðàòåãèÿ ïåðåìåùåíèÿ
/// </summary>
private AbstractStrategy? _strategy;
/// <summary>
/// Ïîëó÷åíèå îáúåêòà
/// </summary>
public DrawingTrans SetCar
{
set
{
_drawningTrans = value;
_drawningTrans.SetPictureSize(pictureBoxElectroTrans.Width, pictureBoxElectroTrans.Height);
comboBoxStrategy.Enabled = true;
_strategy = null;
Draw();
}
}
/// <summary>
/// Êîíñòðóêòîð ôîðìû
/// </summary>
public FormElectroTrans()
{
InitializeComponent();
_strategy = null;
}
/// <summary>
/// Ìåòîä ïðîðèñîâêè ìàøèíû
/// </summary>
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;
}
/// <summary>
/// Ïåðåìåùåíèå îáúåêòà ïî ôîðìå (íàæàòèå êíîïîê íàâèãàöèè)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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();
}
}
/// <summary>
/// Îáðàáîòêà íàæàòèÿ êíîïêè "Øàã"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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;
}
}
}