120 lines
4.3 KiB
C#
120 lines
4.3 KiB
C#
using SailBoat.DrawningObjects;
|
|
using SailBoat.MovementStrategy;
|
|
using System.Drawing;
|
|
|
|
namespace SailBoat
|
|
{
|
|
public partial class FormSailBoat : Form
|
|
{
|
|
private DrawningBoat? _drawningBoat;
|
|
|
|
private AbstractStrategy? _abstractStrategy;
|
|
|
|
public DrawningBoat? SelectedBoat { get; private set; }
|
|
|
|
public FormSailBoat()
|
|
{
|
|
InitializeComponent();
|
|
_abstractStrategy = null;
|
|
SelectedBoat = null;
|
|
}
|
|
private void Draw()
|
|
{
|
|
if (_drawningBoat == null) return;
|
|
Bitmap bmp = new(pictureBoxSailBoat.Width, pictureBoxSailBoat.Height);
|
|
Graphics gr = Graphics.FromImage(bmp);
|
|
_drawningBoat.DrawTransport(gr);
|
|
pictureBoxSailBoat.Image = bmp;
|
|
|
|
}
|
|
|
|
private void buttonCreateSailBoat_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;
|
|
}
|
|
Color dopcolor = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256));
|
|
if (dialog.ShowDialog() == DialogResult.OK)
|
|
{
|
|
dopcolor = dialog.Color;
|
|
}
|
|
_drawningBoat = new DrawningSailBoat(random.Next(100, 300), random.Next(1000, 3000), color, dopcolor, Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)), pictureBoxSailBoat.Width, pictureBoxSailBoat.Height);
|
|
_drawningBoat.SetPosition(random.Next(10, 100), random.Next(10, 100));
|
|
Draw();
|
|
}
|
|
|
|
private void buttonCreateBoat_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;
|
|
}
|
|
|
|
_drawningBoat = new DrawningBoat(random.Next(300, 700), random.Next(2000, 4000), color, pictureBoxSailBoat.Width, pictureBoxSailBoat.Height);
|
|
_drawningBoat.SetPosition(random.Next(10, 100), random.Next(10, 100));
|
|
Draw();
|
|
}
|
|
|
|
private void buttonMove_Click(object sender, EventArgs e)
|
|
{
|
|
if (_drawningBoat == null) { return; }
|
|
string name = ((Button)sender)?.Name ?? string.Empty;
|
|
switch (name)
|
|
{
|
|
case "buttonUp":
|
|
_drawningBoat.MoveTransport(DirectionType.Up);
|
|
break;
|
|
case "buttonDown":
|
|
_drawningBoat.MoveTransport(DirectionType.Down);
|
|
break;
|
|
case "buttonRight":
|
|
_drawningBoat.MoveTransport(DirectionType.Right);
|
|
break;
|
|
case "buttonLeft":
|
|
_drawningBoat.MoveTransport(DirectionType.Left);
|
|
break;
|
|
}
|
|
Draw();
|
|
}
|
|
|
|
private void buttonStep_Click(object sender, EventArgs e)
|
|
{
|
|
if (_drawningBoat == null) return;
|
|
if (comboBoxStrategy.Enabled)
|
|
{
|
|
_abstractStrategy = comboBoxStrategy.SelectedIndex switch
|
|
{
|
|
0 => new MoveToCenter(),
|
|
1 => new MoveToBorder(),
|
|
_ => null,
|
|
|
|
};
|
|
if (_abstractStrategy == null) return;
|
|
_abstractStrategy.SetData(new DrawningObjectBoat(_drawningBoat), pictureBoxSailBoat.Width, pictureBoxSailBoat.Height);
|
|
comboBoxStrategy.Enabled = false;
|
|
}
|
|
if (_abstractStrategy == null) return;
|
|
comboBoxStrategy.Enabled = false;
|
|
_abstractStrategy.MakeStep();
|
|
Draw();
|
|
if (_abstractStrategy.GetStatus() == Status.Finish)
|
|
{
|
|
comboBoxStrategy.Enabled = true;
|
|
_abstractStrategy = null;
|
|
}
|
|
}
|
|
|
|
private void buttonSelectBoat_Click(object sender, EventArgs e)
|
|
{
|
|
SelectedBoat = _drawningBoat;
|
|
DialogResult = DialogResult.OK;
|
|
}
|
|
}
|
|
} |