PIbd-23_Vrazhkin_S_A_Electr.../lab1/MainForm.cs

98 lines
3.2 KiB
C#
Raw Normal View History

2023-09-19 19:04:33 +04:00
using ElectricLocomotive;
namespace ElectricLocomotive
{
public partial class MainForm : Form
{
private DrawingLocomotiv? _drawingLocomotiv;
2023-10-03 11:53:51 +04:00
private AbstractStrategy _abstractStrategy;
2023-09-19 19:04:33 +04:00
public MainForm()
{
InitializeComponent();
}
private void Draw()
{
if (_drawingLocomotiv == null) return;
Bitmap bmp = new(locoBox.Width, locoBox.Height);
Graphics g = Graphics.FromImage(bmp);
_drawingLocomotiv.DrawLoco(g);
locoBox.Image = bmp;
}
2023-10-03 11:53:51 +04:00
private void PaintLocoButton_click(object sender, EventArgs e)
2023-09-19 19:04:33 +04:00
{
Random random = new();
2023-10-03 11:53:51 +04:00
_drawingLocomotiv = new DrawingLocomotiv(random.Next(100, 300), random.Next(1000, 3000), locoBox.Width, locoBox.Height);
2023-09-19 19:04:33 +04:00
_drawingLocomotiv.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw();
}
2023-10-03 11:53:51 +04:00
private void PaintElectricLocoButton_click(object sender, EventArgs e)
{
Random random = new();
_drawingLocomotiv = new DrawingElectricLocomotiv(random.Next(100, 300), random.Next(1000, 3000), locoBox.Width, locoBox.Height);
_drawingLocomotiv.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw();
}
private void MoveSideButton_Click(object sender, EventArgs e)
2023-09-19 19:04:33 +04:00
{
if (_drawingLocomotiv == null) return;
Control button = sender as Control;
string name = button.Name;
switch (name)
{
case "moveUpButton":
_drawingLocomotiv.MoveTransport(DirectionType.Up);
break;
case "moveDownButton":
_drawingLocomotiv.MoveTransport(DirectionType.Down);
break;
case "moveLeftButton":
_drawingLocomotiv.MoveTransport(DirectionType.Left);
break;
case "moveRightButton":
_drawingLocomotiv.MoveTransport(DirectionType.Right);
break;
}
Draw();
}
2023-10-03 11:53:51 +04:00
private void MoveButton_Click(object sender, EventArgs e)
{
if (_drawingLocomotiv == null)
return;
if (movesBox.Enabled)
{
_abstractStrategy = movesBox.SelectedIndex
switch
{
0 => new MoveToEdge(),
1 => new MoveToCenter(),
_ => null,
} ;
if (_abstractStrategy == null)
{
return;
}
_abstractStrategy.SetData(new
DrawingObjectLocomotiv(_drawingLocomotiv), locoBox.Width,
locoBox.Height);
movesBox.Enabled = false;
}
if (_abstractStrategy == null)
{
return;
}
_abstractStrategy.MakeStep();
Draw();
if (_abstractStrategy.GetStatus() == Status.Finish)
{
movesBox.Enabled = true;
_abstractStrategy = null;
}
}
2023-09-19 19:04:33 +04:00
}
}