PIbd-21_Putintsev_D.M._Road.../RoadTrain/FormRoadTrain.cs

139 lines
4.5 KiB
C#

using RoadTrain.MovementStrategy;
using RoadTrain.DrawningObjects;
namespace RoadTrain
{
public partial class FormRoadTrain : Form
{
/// <summary>
/// Ïîëå-îáúåêò äëÿ ïðîðèñîâêè îáúåêòà
/// </summary>
private DrawningRoadTrain? _drawningRoadTrain;
private AbstractStrategy? _abstractStrategy;
/// <summary>
/// Èíèöèàëèçàöèÿ ôîðìû
/// </summary>
public FormRoadTrain()
{
InitializeComponent();
}
/// <summary>
/// Ìåòîä ïðîðèñîâêè ìàøèíû
/// </summary>
private void Draw()
{
if (_drawningRoadTrain == null)
{
return;
}
Bitmap bmp = new(pictureBoxRoadTrain.Width,
pictureBoxRoadTrain.Height);
Graphics gr = Graphics.FromImage(bmp);
_drawningRoadTrain.DrawTransport(gr);
pictureBoxRoadTrain.Image = bmp;
}
/// <summary>
/// Îáðàáîòêà íàæàòèÿ êíîïêè "Ñîçäàòü"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
/// <summary>
/// Èçìåíåíèå ðàçìåðîâ ôîðìû
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonMove_Click(object sender, EventArgs e)
{
if (_drawningRoadTrain == null)
{
return;
}
string name = ((Button)sender)?.Name ?? string.Empty;
switch (name)
{
case "buttonUp":
_drawningRoadTrain.MoveTransport(DirectionType.Up);
break;
case "buttonDown":
_drawningRoadTrain.MoveTransport(DirectionType.Down);
break;
case "buttonLeft":
_drawningRoadTrain.MoveTransport(DirectionType.Left);
break;
case "buttonRight":
_drawningRoadTrain.MoveTransport(DirectionType.Right);
break;
}
Draw();
}
private void buttonCreate_Click(object sender, EventArgs e)
{
Random random = new();
_drawningRoadTrain = new DrawningRoadTrain(random.Next(100, 300), random.Next(1000, 3000),
Color.FromArgb(random.Next(0, 256), random.Next(0, 256),
random.Next(0, 256)),
pictureBoxRoadTrain.Width, pictureBoxRoadTrain.Height);
_drawningRoadTrain.SetPosition(random.Next(10, 100),
random.Next(10, 100));
Draw();
}
private void button1_Click(object sender, EventArgs e)
{
Random random = new();
_drawningRoadTrain = new DrawningTrain(random.Next(100, 300), random.Next(1000, 3000),
Color.FromArgb(random.Next(0, 256), random.Next(0, 256),
random.Next(0, 256)),
Color.FromArgb(random.Next(0, 256), random.Next(0, 256),
random.Next(0, 256)),
Convert.ToBoolean(random.Next(0, 2)),
Convert.ToBoolean(random.Next(0, 2)),
pictureBoxRoadTrain.Width, pictureBoxRoadTrain.Height);
_drawningRoadTrain.SetPosition(random.Next(10, 100),
random.Next(10, 100));
Draw();
}
private void ButtonStep_Click_1(object sender, EventArgs e)
{
if (_drawningRoadTrain == null)
{
return;
}
if (comboBoxStrategy.Enabled)
{
_abstractStrategy = comboBoxStrategy.SelectedIndex
switch
{
0 => new MoveToCenter(),
1 => new MoveToBorder(),
_ => null,
};
if (_abstractStrategy == null)
{
return;
}
_abstractStrategy.SetData(new
DrawningObjectTrain(_drawningRoadTrain), pictureBoxRoadTrain.Width,
pictureBoxRoadTrain.Height);
comboBoxStrategy.Enabled = false;
}
if (_abstractStrategy == null)
{
return;
}
_abstractStrategy.MakeStep();
Draw();
if (_abstractStrategy.GetStatus() == Status.Finish)
{
comboBoxStrategy.Enabled = true;
_abstractStrategy = null;
}
}
}
}