PIbd-23_Dolgova_D.N._Warmly.../WarmlyShip/FormWarmlyShip.cs

175 lines
5.9 KiB
C#
Raw Normal View History

2023-12-01 18:41:28 +04:00
using WarmlyShip.DrawningObjects;
using WarmlyShip.MovementStrategy;
2023-11-24 22:12:36 +04:00
namespace WarmlyShip
{
public partial class FormWarmlyShip : Form
{
/// <summary>
2023-12-01 18:41:28 +04:00
/// Форма работы с объектом "Теплоход"
2023-11-24 22:12:36 +04:00
/// </summary>
2023-12-01 18:41:28 +04:00
private DrawningShip? _drawningShip;
/// <summary>
/// Стратегия перемещения
/// </summary>
2023-12-01 21:18:58 +04:00
private AbstractStrategy? _strategy;
/// <summary>
/// Выбранный корабль
/// </summary>
public DrawningShip? SelectedShip { get; private set; }
2023-11-24 22:12:36 +04:00
/// <summary>
/// Инициализация формы
/// </summary>
public FormWarmlyShip()
{
InitializeComponent();
2023-12-01 21:18:58 +04:00
_strategy = null;
SelectedShip = null;
2023-11-24 22:12:36 +04:00
}
/// <summary>
2023-12-01 18:41:28 +04:00
/// Метод прорисовки корабля
2023-11-24 22:12:36 +04:00
/// </summary>
private void Draw()
{
2023-12-01 18:41:28 +04:00
if (_drawningShip == null)
2023-11-24 22:12:36 +04:00
{
return;
}
Bitmap bmp = new(pictureBoxWarmlyShip.Width, pictureBoxWarmlyShip.Height);
Graphics gr = Graphics.FromImage(bmp);
2023-12-01 18:41:28 +04:00
_drawningShip.DrawTransport(gr);
2023-11-24 22:12:36 +04:00
pictureBoxWarmlyShip.Image = bmp;
}
/// <summary>
2023-12-01 18:41:28 +04:00
/// Обработка нажатия кнопки "Создать теплоход"
2023-11-24 22:12:36 +04:00
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
2023-12-01 18:41:28 +04:00
private void buttonCreateWarmlyShip_Click(object sender, EventArgs e)
2023-11-24 22:12:36 +04:00
{
Random random = new();
2023-12-01 21:18:58 +04:00
Color color = Color.FromArgb(random.Next(0, 256),
random.Next(0, 256), random.Next(0, 256));
ColorDialog dialogColor = new();
if (dialogColor.ShowDialog() == DialogResult.OK)
{
color = dialogColor.Color;
}
Color dopColor = Color.FromArgb(random.Next(0, 256),
random.Next(0, 256), random.Next(0, 256));
ColorDialog dialogDopColor = new();
if (dialogDopColor.ShowDialog() == DialogResult.OK)
{
dopColor = dialogDopColor.Color;
}
2023-12-01 18:41:28 +04:00
_drawningShip = new DrawningWarmlyShip(random.Next(100, 300),
2023-12-01 21:18:58 +04:00
random.Next(1000, 3000), color, dopColor, Convert.ToBoolean(random.Next(0, 2)),
2023-11-24 22:12:36 +04:00
Convert.ToBoolean(random.Next(0, 2)),
pictureBoxWarmlyShip.Width, pictureBoxWarmlyShip.Height);
2023-12-01 18:41:28 +04:00
_drawningShip.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw();
}
/// <summary>
/// Обработка нажатия кнопки "Создать корабль"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonCreateShip_Click(object sender, EventArgs e)
{
Random random = new();
2023-12-01 21:18:58 +04:00
Color color = Color.FromArgb(random.Next(0, 256),
random.Next(0, 256), random.Next(0, 256));
ColorDialog dialogColor = new();
if (dialogColor.ShowDialog() == DialogResult.OK)
{
color = dialogColor.Color;
}
2023-12-01 18:41:28 +04:00
_drawningShip = new DrawningShip(random.Next(100, 300), random.Next(1000, 3000),
2023-12-01 21:18:58 +04:00
color, pictureBoxWarmlyShip.Width, pictureBoxWarmlyShip.Height);
2023-12-01 18:41:28 +04:00
_drawningShip.SetPosition(random.Next(10, 100), random.Next(10, 100));
2023-11-24 22:12:36 +04:00
Draw();
}
2023-12-01 18:41:28 +04:00
2023-11-24 22:12:36 +04:00
/// <summary>
2023-12-01 18:41:28 +04:00
/// Изменение положения корабля
2023-11-24 22:12:36 +04:00
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonMove_Click(object sender, EventArgs e)
{
2023-12-01 18:41:28 +04:00
if (_drawningShip == null)
2023-11-24 22:12:36 +04:00
{
return;
}
string name = ((Button)sender)?.Name ?? string.Empty;
switch (name)
{
case "buttonUp":
2023-12-01 18:41:28 +04:00
_drawningShip.MoveTransport(DirectionType.Up);
2023-11-24 22:12:36 +04:00
break;
case "buttonDown":
2023-12-01 18:41:28 +04:00
_drawningShip.MoveTransport(DirectionType.Down);
2023-11-24 22:12:36 +04:00
break;
case "buttonLeft":
2023-12-01 18:41:28 +04:00
_drawningShip.MoveTransport(DirectionType.Left);
2023-11-24 22:12:36 +04:00
break;
case "buttonRight":
2023-12-01 18:41:28 +04:00
_drawningShip.MoveTransport(DirectionType.Right);
2023-11-24 22:12:36 +04:00
break;
}
Draw();
}
2023-12-01 18:41:28 +04:00
/// <summary>
/// Обработка нажатия кнопки "Шаг"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonStep_Click(object sender, EventArgs e)
{
if (_drawningShip == null)
{
return;
}
if (comboBoxStrategy.Enabled)
{
2023-12-01 21:18:58 +04:00
_strategy = comboBoxStrategy.SelectedIndex switch
2023-12-01 18:41:28 +04:00
{
0 => new MoveToCenter(),
1 => new MoveToBorder(),
_ => null,
};
2023-12-01 21:18:58 +04:00
if (_strategy == null)
2023-12-01 18:41:28 +04:00
{
return;
}
2023-12-01 21:18:58 +04:00
_strategy.SetData(_drawningShip.GetMoveableObject,
pictureBoxWarmlyShip.Width, pictureBoxWarmlyShip.Height);
2023-12-01 18:41:28 +04:00
}
2023-12-01 21:18:58 +04:00
if (_strategy == null)
2023-12-01 18:41:28 +04:00
{
return;
}
2023-12-01 21:18:58 +04:00
comboBoxStrategy.Enabled = false;
_strategy.MakeStep();
2023-12-01 18:41:28 +04:00
Draw();
2023-12-01 21:18:58 +04:00
if (_strategy.GetStatus() == Status.Finish)
2023-12-01 18:41:28 +04:00
{
comboBoxStrategy.Enabled = true;
2023-12-01 21:18:58 +04:00
_strategy = null;
2023-12-01 18:41:28 +04:00
}
}
2023-12-01 21:18:58 +04:00
private void ButtonSelectedShip_Click(object sender, EventArgs e)
{
SelectedShip = _drawningShip;
DialogResult = DialogResult.OK;
}
2023-11-24 22:12:36 +04:00
}
}