using ProjectElectricLocomotive.DrawingObjects; using ProjectElectricLocomotive.MovementStrategy; using ProjectElectricLocomotive; using System; namespace ElectricLocomotive { public partial class FormElectricLocomotive : Form { private DrawingLocomotive? _drawingLocomotive; private AbstractStrategy? _abstractStrategy; public DrawingLocomotive? SelectedLocomotive { get; private set; } public FormElectricLocomotive() { InitializeComponent(); _abstractStrategy = null; SelectedLocomotive = null; } private void Draw() { if (_drawingLocomotive == null) { return; } Bitmap bmp = new(pictureBoxElectricLocomotive.Width, pictureBoxElectricLocomotive.Height); Graphics gr = Graphics.FromImage(bmp); _drawingLocomotive.DrawTransport(gr); pictureBoxElectricLocomotive.Image = bmp; } private void buttonCreateElectricLocomotive_Click(object sender, EventArgs e) { Random random = new Random(); Color color = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)); ColorDialog colorDialog = new ColorDialog(); if (colorDialog.ShowDialog() == DialogResult.OK) { color = colorDialog.Color; } Color dopColor = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)); if (colorDialog.ShowDialog() == DialogResult.OK) { dopColor = colorDialog.Color; } _drawingLocomotive = new DrawingElectricLocomotive(random.Next(100, 300), random.Next(1000, 3000), color, dopColor, Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)), pictureBoxElectricLocomotive.Width, pictureBoxElectricLocomotive.Height); _drawingLocomotive.SetPosition(random.Next(10, 100), random.Next(10, 100)); Draw(); } private void buttonCreateLocomotive_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; } _drawingLocomotive = new DrawingLocomotive(random.Next(100, 300), random.Next(1000, 3000), color, pictureBoxElectricLocomotive.Width, pictureBoxElectricLocomotive.Height); _drawingLocomotive.SetPosition(random.Next(10, 100), random.Next(10, 100)); Draw(); } private void buttonMove_Click(object sender, EventArgs e) { if (_drawingLocomotive == null) { return; } string name = ((Button)sender)?.Name ?? string.Empty; switch (name) { case "buttonUp": _drawingLocomotive.MoveTransport(Direction.Up); break; case "buttonDown": _drawingLocomotive.MoveTransport(Direction.Down); break; case "buttonLeft": _drawingLocomotive.MoveTransport(Direction.Left); break; case "buttonRight": _drawingLocomotive.MoveTransport(Direction.Right); break; } Draw(); } private void buttonStrategyStep_Click(object sender, EventArgs e) { if (_drawingLocomotive == null) { return; } if (comboBoxStrategy.Enabled) { _abstractStrategy = comboBoxStrategy.SelectedIndex switch { 0 => new MoveToCenter(), 1 => new MoveToRightEdge(), _ => null, }; if (_abstractStrategy == null) { return; } _abstractStrategy.SetData(new DrawingObjectLocomotive(_drawingLocomotive), pictureBoxElectricLocomotive.Width, pictureBoxElectricLocomotive.Height); comboBoxStrategy.Enabled = false; } if (_abstractStrategy == null) { return; } _abstractStrategy.MakeStep(); Draw(); if (_abstractStrategy.GetStatus() == Status.Finish) { comboBoxStrategy.Enabled = true; _abstractStrategy = null; } } private void ButtonSelectLocomotive_Click(object sender, EventArgs e) { SelectedLocomotive = _drawingLocomotive; DialogResult = DialogResult.OK; } } }