using ProjectAirplaneWithRadar.Drawnings; using ProjectAirplaneWithRadar.MovementStrategy; namespace ProjectAirplaneWithRadar { /// /// Форма работы с объектом "Самолет с радаром" /// public partial class FormAirplaneWithRadar : Form { /// /// Поле-объект для происовки объект /// private DrawningAirplane? _drawingAirplane; /// /// Стратегия перемещения /// private AbstractStrategy? _strategy; public DrawningAirplane SetAirplane { set { _drawingAirplane = value; _drawingAirplane.SetPictureSize(pictureBoxAirplaneWithRadar.Width, pictureBoxAirplaneWithRadar.Height); comboBoxStrategy.Enabled = true; _strategy = null; UpdatePlane(); } } /// /// Конструктор формы /// public FormAirplaneWithRadar() { InitializeComponent(); _strategy = null; } /// /// Метод прорисовки самолета /// private void UpdatePlane() { if (_drawingAirplane == null) { return; } Bitmap bmp = new(pictureBoxAirplaneWithRadar.Width, pictureBoxAirplaneWithRadar.Height); Graphics gr = Graphics.FromImage(bmp); _drawingAirplane?.DrawTransport(gr); pictureBoxAirplaneWithRadar.Image = bmp; } /// /// Перемещение объекта по форме (нажатие кнопок навигации) /// /// /// private void ButtonMove_Click(object sender, EventArgs e) { if (_drawingAirplane == null) { return; } if (sender is Button button) { string name = button.Name; DirectionType result; switch (name) { case "buttonUp": result = DirectionType.Up; break; case "buttonDown": result = DirectionType.Down; break; case "buttonLeft": result = DirectionType.Left; break; case "buttonRight": result = DirectionType.Right; break; default: return; } _drawingAirplane.MoveTransport(result); UpdatePlane(); } } /// /// Обработка нажатия кнопки "Шаг" /// /// /// private void buttonStrategyStep_Click(object sender, EventArgs e) { if (_drawingAirplane == null) { return; } if (comboBoxStrategy.Enabled) { _strategy = comboBoxStrategy.SelectedIndex switch { 0 => new MoveToCenter(), 1 => new MoveToBorder(), _ => null, }; if (_strategy == null) { return; } _strategy.SetData(new MoveablePlane(_drawingAirplane), pictureBoxAirplaneWithRadar.Width, pictureBoxAirplaneWithRadar.Height); } if (_strategy == null) { return; } comboBoxStrategy.Enabled = false; _strategy.MakeStep(); UpdatePlane(); if (_strategy.GetStatus() == StrategyStatus.Finish) { comboBoxStrategy.Enabled = true; _strategy = null; } } } }