2024-04-15 00:21:36 +04:00

116 lines
3.1 KiB
C#

using ProjectSeaplane.Drawings;
using ProjectSeaplane.MovementStrategy;
namespace ProjectSeaplane
{
public partial class FormSeaplane : Form
{
private DrawingPlane? _drawingPlane;
private AbstractStrategy? _strategy;
public DrawingPlane SetPlane
{
set
{
_drawingPlane = value;
_drawingPlane.SetPictureSize(pictureBoxSeaplane.Width, pictureBoxSeaplane.Height);
comboBoxStrategy.Enabled = true;
_strategy = null;
Draw();
}
}
/// <summary>
/// Конструктор формы
/// </summary>
public FormSeaplane()
{
InitializeComponent();
_strategy = null;
}
private void Draw()
{
if (_drawingPlane == null)
{
return;
}
Bitmap bmp = new(pictureBoxSeaplane.Width, pictureBoxSeaplane.Height);
Graphics gr = Graphics.FromImage(bmp);
_drawingPlane.DrawTransport(gr);
pictureBoxSeaplane.Image = bmp;
}
private void ButtonMove_Click(object sender, EventArgs e)
{
if (_drawingPlane == null)
{
return;
}
string name = ((Button)sender)?.Name ?? string.Empty;
bool result = false;
switch (name)
{
case "buttonUp":
result = _drawingPlane.MoveTransport(DirectionType.Up);
break;
case "buttonDown":
result = _drawingPlane.MoveTransport(DirectionType.Down);
break;
case "buttonLeft":
result = _drawingPlane.MoveTransport(DirectionType.Left);
break;
case "buttonRight":
result = _drawingPlane.MoveTransport(DirectionType.Right);
break;
}
if (result)
{
Draw();
}
}
private void buttonStrategyStep_Click(object sender, EventArgs e)
{
if (_drawingPlane == null)
{
return;
}
if (comboBoxStrategy.Enabled)
{
_strategy = comboBoxStrategy.SelectedIndex switch
{
0 => new MoveToCenter(),
1 => new MoveToBorder(),
_ => null,
};
if (_strategy == null)
{
return;
}
_strategy.SetData(new MoveablePlane(_drawingPlane), pictureBoxSeaplane.Width, pictureBoxSeaplane.Height);
}
if (_strategy == null)
{
return;
}
comboBoxStrategy.Enabled = false;
_strategy.MakeStep();
Draw();
if (_strategy.GetStatus() == StrategyStatus.Finish)
{
comboBoxStrategy.Enabled = true;
_strategy = null;
}
}
}
}