PIbd-23-Kondratev-N.D.-Gaso.../GasolineTanker/ProjectGasolineTanker/FormGasolineTanker.cs

144 lines
4.7 KiB
C#
Raw Normal View History

2024-10-03 01:40:53 +04:00
using ProjectGasolineTanker.Drawings;
using ProjectGasolineTanker.Entities;
using ProjectGasolineTanker.MovementStratg;
2024-10-03 01:16:11 +04:00
namespace ProjectGasolineTanker
{
2024-10-03 01:40:53 +04:00
2024-10-03 01:16:11 +04:00
public partial class FormGasolineTanker : Form
{
2024-10-03 01:40:53 +04:00
2024-10-03 01:16:11 +04:00
private DrawingTruck? _drawingTruck;
2024-10-03 01:40:53 +04:00
2024-10-03 01:16:11 +04:00
private AbstractStrategy? _abstractStrategy;
2024-10-03 01:40:53 +04:00
public DrawingTruck? SelectedTruck { get; private set; }
2024-10-03 01:16:11 +04:00
public FormGasolineTanker()
{
InitializeComponent();
2024-10-03 01:40:53 +04:00
_abstractStrategy = null;
SelectedTruck = null;
2024-10-03 01:16:11 +04:00
}
2024-10-03 01:40:53 +04:00
2024-10-03 01:16:11 +04:00
private void Draw()
{
if (_drawingTruck == null)
{
return;
}
Bitmap bmp = new(pictureBoxGasolineTanker.Width, pictureBoxGasolineTanker.Height);
Graphics gr = Graphics.FromImage(bmp);
_drawingTruck.DrawTransport(gr);
2024-10-03 01:40:53 +04:00
pictureBoxGasolineTanker.Image = bmp;
2024-10-03 01:16:11 +04:00
}
2024-10-03 01:40:53 +04:00
private void buttonCreateProjectGasolineTanker_Click(object sender, EventArgs e)
2024-10-03 01:16:11 +04:00
{
Random random = new();
2024-10-03 01:40:53 +04:00
Color color = Color.FromArgb(random.Next(0, 256),
random.Next(0, 256), random.Next(0, 256));
Color color1 = Color.FromArgb(random.Next(0, 256),
random.Next(0, 256), random.Next(0, 256));
ColorDialog dialog = new();
ColorDialog dialog1 = new();
if (dialog.ShowDialog() == DialogResult.OK && dialog1.ShowDialog() == DialogResult.OK)
{
color = dialog.Color;
color1 = dialog1.Color;
}
2024-10-03 01:16:11 +04:00
_drawingTruck = new DrawingGasolineTanker(random.Next(100, 300),
random.Next(1000, 3000),
2024-10-03 01:40:53 +04:00
color, color1,
true, true,
2024-10-03 01:16:11 +04:00
pictureBoxGasolineTanker.Width, pictureBoxGasolineTanker.Height);
_drawingTruck.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw();
}
2024-10-03 01:40:53 +04:00
2024-10-03 01:16:11 +04:00
private void buttonCreateTruck_Click(object sender, EventArgs e)
{
Random random = new();
2024-10-03 01:40:53 +04:00
Color color = Color.FromArgb(random.Next(0, 256),
random.Next(0, 256), random.Next(0, 256));
ColorDialog dialog = new();
if (dialog.ShowDialog() == DialogResult.OK)
{
color = dialog.Color;
}
2024-10-03 01:16:11 +04:00
_drawingTruck = new DrawingTruck(random.Next(100, 300),
random.Next(1000, 3000),
2024-10-03 01:40:53 +04:00
color,
2024-10-03 01:16:11 +04:00
pictureBoxGasolineTanker.Width, pictureBoxGasolineTanker.Height);
_drawingTruck.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw();
}
2024-10-03 01:40:53 +04:00
2024-10-03 01:16:11 +04:00
private void buttonMove_Click(object sender, EventArgs e)
{
if (_drawingTruck == null)
{
return;
}
string name = ((Button)sender)?.Name ?? string.Empty;
switch (name)
{
case "buttonUp":
_drawingTruck.MoveTransport(DirectionType.Up);
break;
case "buttonDown":
_drawingTruck.MoveTransport(DirectionType.Down);
break;
case "buttonLeft":
_drawingTruck.MoveTransport(DirectionType.Left);
break;
case "buttonRight":
_drawingTruck.MoveTransport(DirectionType.Right);
break;
}
Draw();
}
2024-10-03 01:40:53 +04:00
2024-10-03 01:16:11 +04:00
private void buttonStep_Click(object sender, EventArgs e)
{
if (_drawingTruck == null)
{
return;
}
if (comboBoxStrategy.Enabled)
{
_abstractStrategy = comboBoxStrategy.SelectedIndex
switch
{
0 => new MoveToCenter(),
1 => new MoveToBorder(),
_ => null,
};
if (_abstractStrategy == null)
{
return;
}
_abstractStrategy.SetData(new DrawingObjectTruck(_drawingTruck), pictureBoxGasolineTanker.Width, pictureBoxGasolineTanker.Height);
comboBoxStrategy.Enabled = false;
}
if (_abstractStrategy == null)
{
return;
}
_abstractStrategy.MakeStep();
Draw();
if (_abstractStrategy.GetStatus() == Status.Finish)
{
comboBoxStrategy.Enabled = true;
_abstractStrategy = null;
}
}
2024-10-03 01:40:53 +04:00
private void ButtonSelectTruck_Click(object sender, EventArgs e)
{
SelectedTruck = _drawingTruck;
DialogResult = DialogResult.OK;
}
2024-10-03 01:16:11 +04:00
}
}