using ProjectGasolineTanker.Drawings;
using ProjectGasolineTanker.Entities;
using ProjectGasolineTanker.MovementStratg;
namespace ProjectGasolineTanker
{
///
/// Форма работы с объектом Газовоз
///
public partial class FormGasolineTanker : Form
{
///
/// Поле-объект для прорисовки объекта
///
private DrawingTruck? _drawingTruck;
///
/// Стратегия перемещения
///
private AbstractStrategy? _abstractStrategy;
///
/// Выбранный автомобиль
///
public DrawingTruck? SelectedTruck { get; private set; }
public FormGasolineTanker()
{
InitializeComponent();
_abstractStrategy = null;
SelectedTruck = null;
}
///
/// Метод прорисовки грузовика
///
private void Draw()
{
if (_drawingTruck == null)
{
return;
}
Bitmap bmp = new(pictureBoxGasolineTanker.Width, pictureBoxGasolineTanker.Height);
Graphics gr = Graphics.FromImage(bmp);
_drawingTruck.DrawTransport(gr);
pictureBoxGasolineTanker.Image = bmp;
}
///
/// Обработка нажатия кнопки "Создать газовоз"
///
///
///
private void buttonCreateProjectGasolineTanker_Click(object sender, EventArgs e)
{
Random random = new();
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;
}
_drawingTruck = new DrawingGasolineTanker(random.Next(100, 300),
random.Next(1000, 3000),
color, color1,
true, true,
pictureBoxGasolineTanker.Width, pictureBoxGasolineTanker.Height);
_drawingTruck.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw();
}
///
/// Обработка нажатия кнопки "Создать ProjectGasolineTanker"
///
///
///
private void buttonCreateTruck_Click(object sender, EventArgs e)
{
Random random = new();
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;
}
_drawingTruck = new DrawingTruck(random.Next(100, 300),
random.Next(1000, 3000),
color,
pictureBoxGasolineTanker.Width, pictureBoxGasolineTanker.Height);
_drawingTruck.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw();
}
///
/// Изменение положения объекта
///
///
///
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();
}
///
/// Обработка нажатия кнопки "Шаг"
///
///
///
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;
}
}
private void ButtonSelectTruck_Click(object sender, EventArgs e)
{
SelectedTruck = _drawingTruck;
DialogResult = DialogResult.OK;
}
}
}