121 lines
4.2 KiB
C#
Raw Normal View History

2023-09-26 19:32:10 +04:00
using Lab.DrawningObjects;
using Lab.MovementStrategy;
using Lab;
2023-10-04 12:52:06 +04:00
using System.Drawing;
2023-09-26 19:32:10 +04:00
2023-09-26 19:15:17 +04:00
namespace Lab
{
public partial class Frame : Form
{
2023-09-26 19:32:10 +04:00
private DrawTanker? _drawingTanker;
private AbstractStrategy? _abstractStrategy;
2023-10-04 12:52:06 +04:00
public DrawTanker? SelectedCar { get; private set; }
2023-09-26 19:15:17 +04:00
public Frame()
{
InitializeComponent();
2023-10-04 12:52:06 +04:00
_abstractStrategy = null;
SelectedCar = null;
2023-09-26 19:15:17 +04:00
}
2023-10-04 12:52:06 +04:00
2023-09-26 19:15:17 +04:00
private void Draw()
{
2023-09-26 19:32:10 +04:00
if (_drawingTanker == null)
2023-09-26 19:15:17 +04:00
return;
Bitmap bitmap = new(DrawCar.Width, DrawCar.Height);
Graphics g = Graphics.FromImage(bitmap);
2023-09-26 19:32:10 +04:00
_drawingTanker.DrawTransport(g);
2023-09-26 19:15:17 +04:00
DrawCar.Image = bitmap;
}
2023-09-26 19:32:10 +04:00
private void CreateGasolineTankerButton_Click(object sender, EventArgs e)
2023-09-26 19:15:17 +04:00
{
Random rnd = new();
2023-10-04 12:52:06 +04:00
Color mainColor = Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256));
ColorDialog dialog = new();
if (dialog.ShowDialog() == DialogResult.OK)
{
mainColor = dialog.Color;
}
Color addColor = Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256));
if (dialog.ShowDialog() == DialogResult.OK)
{
addColor = dialog.Color;
}
2023-09-26 19:32:10 +04:00
_drawingTanker = new DrawGasolineTanker(rnd.Next(100, 200), rnd.Next(2000, 4000),
2023-10-04 12:52:06 +04:00
mainColor, addColor,
2023-09-26 19:15:17 +04:00
Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0, 2)),
DrawCar.Width, DrawCar.Height);
2023-09-26 19:32:10 +04:00
_drawingTanker.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100));
Draw();
}
private void CreateCarButton_Click(object sender, EventArgs e)
{
Random rnd = new();
2023-10-04 12:52:06 +04:00
Color color = Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256));
ColorDialog dialog = new();
if (dialog.ShowDialog() == DialogResult.OK)
{
color = dialog.Color;
}
_drawingTanker = new DrawTanker(rnd.Next(100, 200), rnd.Next(2000, 4000), color,
2023-09-26 19:32:10 +04:00
DrawCar.Width, DrawCar.Height);
_drawingTanker.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100));
2023-09-26 19:15:17 +04:00
Draw();
}
private void ButtonMove_Click(object sender, EventArgs e)
{
2023-09-26 19:32:10 +04:00
if (_drawingTanker == null)
2023-09-26 19:15:17 +04:00
return;
string name = ((Button)sender)?.Name ?? string.Empty;
switch (name)
{
case "Up":
2023-09-26 19:32:10 +04:00
_drawingTanker.MoveTransport(Direction.Up); break;
2023-09-26 19:15:17 +04:00
case "Down":
2023-09-26 19:32:10 +04:00
_drawingTanker.MoveTransport(Direction.Down); break;
2023-09-26 19:15:17 +04:00
case "Left":
2023-09-26 19:32:10 +04:00
_drawingTanker.MoveTransport(Direction.Left); break;
2023-09-26 19:15:17 +04:00
case "Right":
2023-09-26 19:32:10 +04:00
_drawingTanker.MoveTransport(Direction.Right); break;
2023-09-26 19:15:17 +04:00
}
Draw();
}
2023-09-26 19:32:10 +04:00
private void ButtonStep_Click(object sender, EventArgs e)
{
if (_drawingTanker == null)
return;
if (comboBoxStrategy.Enabled)
{
_abstractStrategy = comboBoxStrategy.SelectedIndex switch
{
0 => new MoveToCenter(),
1 => new MoveToBorder(),
_ => null
};
if (_abstractStrategy == null)
return;
2023-10-04 12:52:06 +04:00
_abstractStrategy.SetData(_drawingTanker.GetMoveableObject, DrawCar.Width, DrawCar.Height);
2023-09-26 19:32:10 +04:00
comboBoxStrategy.Enabled = false;
}
if (_abstractStrategy == null)
return;
_abstractStrategy.MakeStep();
Draw();
if (_abstractStrategy.GetStatus() == Status.Finish)
{
comboBoxStrategy.Enabled = true;
_abstractStrategy = null;
}
}
2023-10-04 12:52:06 +04:00
private void ButtonSelectTank_Click(object sender, EventArgs e)
{
SelectedCar = _drawingTanker;
DialogResult = DialogResult.OK;
}
2023-09-26 19:15:17 +04:00
}
}