PIbd-14_Rachek_A.S._Base/lab_0/FormBus.cs
2024-04-25 10:17:26 +04:00

154 lines
3.6 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using ProjectBus.Drawnings;
using ProjectBus.MovementStrategy;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ProjectBus;
/// <summary>
/// форма работы с объектом "Автобус"
/// </summary>
public partial class FormBus : Form
{
/// <summary>
/// поле-объект для прорисовки объекта
/// </summary>
private DrawningSimpleBus? _drawningSimpleBus;
/// <summary>
/// Стратегия перемещения
/// </summary>
private AbstractStrategy? _strategy;
/// <summary>
/// Получение объекта
/// </summary>
public DrawningSimpleBus SetSimpleBus
{
set
{
_drawningSimpleBus = value;
_drawningSimpleBus.SetPictureSize(pictureBoxBus.Width, pictureBoxBus.Height);
comboBoxStrategy.Enabled = true;
_strategy = null;
Draw();
}
}
/// <summary>
/// конструктор формы
/// </summary>
public FormBus()
{
InitializeComponent();
_strategy = null;
}
/// <summary>
/// метод прорисовки машины
/// </summary>
private void Draw()
{
if (_drawningSimpleBus == null)
{
return;
}
Bitmap bmp = new(pictureBoxBus.Width, pictureBoxBus.Height);
Graphics gr = Graphics.FromImage(bmp);
_drawningSimpleBus.DrawTransport(gr);
pictureBoxBus.Image = bmp;
}
private void ButtonMove_Click(object sender, EventArgs e)
{
if (_drawningSimpleBus == null)
{
return;
}
string name = ((Button)sender)?.Name ?? string.Empty;
bool result = false;
switch (name)
{
case "buttonUp":
result =
_drawningSimpleBus.MoveTransport(DirectionType.Up);
break;
case "buttonDown":
result =
_drawningSimpleBus.MoveTransport(DirectionType.Down);
break;
case "buttonLeft":
result =
_drawningSimpleBus.MoveTransport(DirectionType.Left);
break;
case "buttonRight":
result =
_drawningSimpleBus.MoveTransport(DirectionType.Right);
break;
}
if (result)
{
Draw();
}
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonStrategyStep_Click(object sender, EventArgs e)
{
if (_drawningSimpleBus == null)
{
return;
}
if (comboBoxStrategy.Enabled)
{
_strategy = comboBoxStrategy.SelectedIndex switch
{
0 => new MoveToCenter(),
1 => new MoveToBorder(),
_ => null,
};
if (_strategy == null)
{
return;
}
_strategy.SetData(new MoveableSimpleBus(_drawningSimpleBus), pictureBoxBus.Width, pictureBoxBus.Height);
}
if (_strategy == null)
{
return;
}
comboBoxStrategy.Enabled = false;
_strategy.MakeStep();
Draw();
if (_strategy.GetStatus() == StrategyStatus.Finish)
{
comboBoxStrategy.Enabled = true;
_strategy = null;
}
}
}