PIbd-23_Dolgova_D.N._Warmly.../WarmlyShip/FormWarmlyShip.cs
2023-12-01 21:18:58 +04:00

175 lines
5.9 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 WarmlyShip.DrawningObjects;
using WarmlyShip.MovementStrategy;
namespace WarmlyShip
{
public partial class FormWarmlyShip : Form
{
/// <summary>
/// Форма работы с объектом "Теплоход"
/// </summary>
private DrawningShip? _drawningShip;
/// <summary>
/// Стратегия перемещения
/// </summary>
private AbstractStrategy? _strategy;
/// <summary>
/// Выбранный корабль
/// </summary>
public DrawningShip? SelectedShip { get; private set; }
/// <summary>
/// Инициализация формы
/// </summary>
public FormWarmlyShip()
{
InitializeComponent();
_strategy = null;
SelectedShip = null;
}
/// <summary>
/// Метод прорисовки корабля
/// </summary>
private void Draw()
{
if (_drawningShip == null)
{
return;
}
Bitmap bmp = new(pictureBoxWarmlyShip.Width, pictureBoxWarmlyShip.Height);
Graphics gr = Graphics.FromImage(bmp);
_drawningShip.DrawTransport(gr);
pictureBoxWarmlyShip.Image = bmp;
}
/// <summary>
/// Обработка нажатия кнопки "Создать теплоход"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonCreateWarmlyShip_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 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;
}
_drawningShip = new DrawningWarmlyShip(random.Next(100, 300),
random.Next(1000, 3000), color, dopColor, Convert.ToBoolean(random.Next(0, 2)),
Convert.ToBoolean(random.Next(0, 2)),
pictureBoxWarmlyShip.Width, pictureBoxWarmlyShip.Height);
_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();
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;
}
_drawningShip = new DrawningShip(random.Next(100, 300), random.Next(1000, 3000),
color, pictureBoxWarmlyShip.Width, pictureBoxWarmlyShip.Height);
_drawningShip.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw();
}
/// <summary>
/// Изменение положения корабля
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonMove_Click(object sender, EventArgs e)
{
if (_drawningShip == null)
{
return;
}
string name = ((Button)sender)?.Name ?? string.Empty;
switch (name)
{
case "buttonUp":
_drawningShip.MoveTransport(DirectionType.Up);
break;
case "buttonDown":
_drawningShip.MoveTransport(DirectionType.Down);
break;
case "buttonLeft":
_drawningShip.MoveTransport(DirectionType.Left);
break;
case "buttonRight":
_drawningShip.MoveTransport(DirectionType.Right);
break;
}
Draw();
}
/// <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)
{
_strategy = comboBoxStrategy.SelectedIndex switch
{
0 => new MoveToCenter(),
1 => new MoveToBorder(),
_ => null,
};
if (_strategy == null)
{
return;
}
_strategy.SetData(_drawningShip.GetMoveableObject,
pictureBoxWarmlyShip.Width, pictureBoxWarmlyShip.Height);
}
if (_strategy == null)
{
return;
}
comboBoxStrategy.Enabled = false;
_strategy.MakeStep();
Draw();
if (_strategy.GetStatus() == Status.Finish)
{
comboBoxStrategy.Enabled = true;
_strategy = null;
}
}
private void ButtonSelectedShip_Click(object sender, EventArgs e)
{
SelectedShip = _drawningShip;
DialogResult = DialogResult.OK;
}
}
}