PIbd-23-Kondratev-N.D.-Gaso.../GasolineTanker/ProjectGasolineTanker/FormGasolineTanker.cs
2024-10-03 00:21:27 +03:00

171 lines
5.8 KiB
C#

using ProjectGasolineTanker.Drawings;
using ProjectGasolineTanker.Entities;
using ProjectGasolineTanker.MovementStratg;
namespace ProjectGasolineTanker
{
/// <summary>
/// Ôîðìà ðàáîòû ñ îáúåêòîì Ãàçîâîç
/// </summary>
public partial class FormGasolineTanker : Form
{
/// <summary>
/// Ïîëå-îáúåêò äëÿ ïðîðèñîâêè îáúåêòà
/// </summary>
private DrawingTruck? _drawingTruck;
/// <summary>
/// Ñòðàòåãèÿ ïåðåìåùåíèÿ
/// </summary>
private AbstractStrategy? _abstractStrategy;
/// <summary>
/// Âûáðàííûé àâòîìîáèëü
/// </summary>
public DrawingTruck? SelectedTruck { get; private set; }
public FormGasolineTanker()
{
InitializeComponent();
_abstractStrategy = null;
SelectedTruck = null;
}
/// <summary>
/// Ìåòîä ïðîðèñîâêè ãðóçîâèêà
/// </summary>
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;
}
/// <summary>
/// Îáðàáîòêà íàæàòèÿ êíîïêè "Ñîçäàòü ãàçîâîç"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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();
}
/// <summary>
/// Îáðàáîòêà íàæàòèÿ êíîïêè "Ñîçäàòü ProjectGasolineTanker"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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();
}
/// <summary>
/// Èçìåíåíèå ïîëîæåíèÿ îáúåêòà
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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();
}
/// <summary>
/// Îáðàáîòêà íàæàòèÿ êíîïêè "Øàã"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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;
}
}
}