using Lab.DrawningObjects; using Lab.MovementStrategy; using Lab; using System.Drawing; namespace Lab { public partial class Frame : Form { private DrawTanker? _drawingTanker; private AbstractStrategy? _abstractStrategy; public DrawTanker? SelectedCar { get; private set; } public Frame() { InitializeComponent(); _abstractStrategy = null; SelectedCar = null; } private void Draw() { if (_drawingTanker == null) return; Bitmap bitmap = new(DrawCar.Width, DrawCar.Height); Graphics g = Graphics.FromImage(bitmap); _drawingTanker.DrawTransport(g); DrawCar.Image = bitmap; } private void CreateGasolineTankerButton_Click(object sender, EventArgs e) { Random rnd = new(); 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; } _drawingTanker = new DrawGasolineTanker(rnd.Next(100, 200), rnd.Next(2000, 4000), mainColor, addColor, Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0, 2)), DrawCar.Width, DrawCar.Height); _drawingTanker.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100)); Draw(); } private void CreateCarButton_Click(object sender, EventArgs e) { Random rnd = new(); 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, DrawCar.Width, DrawCar.Height); _drawingTanker.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100)); Draw(); } private void ButtonMove_Click(object sender, EventArgs e) { if (_drawingTanker == null) return; string name = ((Button)sender)?.Name ?? string.Empty; switch (name) { case "Up": _drawingTanker.MoveTransport(Direction.Up); break; case "Down": _drawingTanker.MoveTransport(Direction.Down); break; case "Left": _drawingTanker.MoveTransport(Direction.Left); break; case "Right": _drawingTanker.MoveTransport(Direction.Right); break; } Draw(); } 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; _abstractStrategy.SetData(_drawingTanker.GetMoveableObject, DrawCar.Width, DrawCar.Height); comboBoxStrategy.Enabled = false; } if (_abstractStrategy == null) return; _abstractStrategy.MakeStep(); Draw(); if (_abstractStrategy.GetStatus() == Status.Finish) { comboBoxStrategy.Enabled = true; _abstractStrategy = null; } } private void ButtonSelectTank_Click(object sender, EventArgs e) { SelectedCar = _drawingTanker; DialogResult = DialogResult.OK; } } }