using DoubleDeckerbus.DrawingObjects; using DoubleDeckerbus.MovementStrategy; namespace DoubleDeckerbus { public partial class FormDoubleDeckerbus : Form { private DrawingBus? _drawingBus; private AbstractStrategy? _abstractStrategy; public DrawingBus? SelectedBus { get; private set; } public FormDoubleDeckerbus() { InitializeComponent(); _abstractStrategy = null; SelectedBus = null; } private void Draw() { if (_drawingBus == null) { return; } Bitmap bmp = new(pictureBoxDoubleDeckerbus.Width, pictureBoxDoubleDeckerbus.Height); Graphics gr = Graphics.FromImage(bmp); _drawingBus.DrawTransport(gr); pictureBoxDoubleDeckerbus.Image = bmp; } private void buttonCreateDoubleDeckerbus_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; } _drawingBus = new DrawingDoubleDeckerbus(random.Next(100, 300), random.Next(1000, 3000), color, color1, true, true, pictureBoxDoubleDeckerbus.Width, pictureBoxDoubleDeckerbus.Height); _drawingBus.SetPosition(random.Next(10, 100), random.Next(10, 100)); Draw(); } private void buttonCreateBus_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; } _drawingBus = new DrawingBus(random.Next(100, 300), random.Next(1000, 3000), color, pictureBoxDoubleDeckerbus.Width, pictureBoxDoubleDeckerbus.Height); _drawingBus.SetPosition(random.Next(10, 100), random.Next(10, 100)); Draw(); } private void buttonMove_Click(object sender, EventArgs e) { if (_drawingBus == null) { return; } string name = ((Button)sender)?.Name ?? string.Empty; switch (name) { case "buttonUp": _drawingBus.MoveTransport(DirectionType.Up); break; case "buttonDown": _drawingBus.MoveTransport(DirectionType.Down); break; case "buttonLeft": _drawingBus.MoveTransport(DirectionType.Left); break; case "buttonRight": _drawingBus.MoveTransport(DirectionType.Right); break; } Draw(); } private void buttonStep_Click(object sender, EventArgs e) { if (_drawingBus == null) { return; } if (comboBoxStrategy.Enabled) { _abstractStrategy = comboBoxStrategy.SelectedIndex switch { 0 => new MoveToCenter(), 1 => new MoveToBorder(), _ => null, }; if (_abstractStrategy == null) { return; } _abstractStrategy.SetData(new DrawingObjectBus(_drawingBus), pictureBoxDoubleDeckerbus.Width, pictureBoxDoubleDeckerbus.Height); comboBoxStrategy.Enabled = false; } if (_abstractStrategy == null) { return; } _abstractStrategy.MakeStep(); Draw(); if (_abstractStrategy.GetStatus() == Status.Finish) { comboBoxStrategy.Enabled = true; _abstractStrategy = null; } } private void ButtonSelectBus_Click(object sender, EventArgs e) { SelectedBus = _drawingBus; DialogResult = DialogResult.OK; } } }