108 lines
4.5 KiB
C#
108 lines
4.5 KiB
C#
using ElectricLocomotive;
|
|
|
|
namespace ElectricLocomotive {
|
|
public partial class FormLocomotiv : Form {
|
|
private DrawingLocomotiv? _drawingLocomotiv;
|
|
private AbstractStrategy? _abstractStrategy;
|
|
public DrawingLocomotiv? SelectedLocomotiv { get; private set; }
|
|
public FormLocomotiv() {
|
|
InitializeComponent();
|
|
_abstractStrategy = null;
|
|
SelectedLocomotiv = null;
|
|
}
|
|
|
|
private void Draw() {
|
|
if (_drawingLocomotiv == null) return;
|
|
Bitmap bmp = new(locoBox.Width, locoBox.Height);
|
|
Graphics g = Graphics.FromImage(bmp);
|
|
_drawingLocomotiv.DrawLoco(g);
|
|
locoBox.Image = bmp;
|
|
}
|
|
private void PaintLocoButton_click(object sender, EventArgs e) {
|
|
Random random = new();
|
|
Color color = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256));
|
|
Color dopColor = 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;
|
|
_drawingLocomotiv = new DrawingLocomotiv(random.Next(100, 300), random.Next(1000, 3000), locoBox.Width, locoBox.Height, color, dopColor);
|
|
_drawingLocomotiv.SetPosition(random.Next(10, 100), random.Next(10, 100));
|
|
|
|
Draw();
|
|
}
|
|
|
|
private void PaintElectricLocoButton_click(object sender, EventArgs e) {
|
|
Random random = new();
|
|
Color color = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256));
|
|
Color dopColor = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256));
|
|
Color batteryColor = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256));
|
|
Color rogaColor = 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;
|
|
if (dialog.ShowDialog() == DialogResult.OK)
|
|
dopColor = dialog.Color;
|
|
|
|
_drawingLocomotiv = new DrawingElectricLocomotiv(random.Next(100, 300), random.Next(1000, 3000), locoBox.Width, locoBox.Height, color, dopColor, batteryColor, rogaColor);
|
|
_drawingLocomotiv.SetPosition(random.Next(10, 100), random.Next(10, 100));
|
|
Draw();
|
|
}
|
|
|
|
private void MoveSideButton_Click(object sender, EventArgs e) {
|
|
if (_drawingLocomotiv == null) return;
|
|
|
|
Control button = sender as Control;
|
|
|
|
string name = button.Name;
|
|
switch (name) {
|
|
case "moveUpButton":
|
|
_drawingLocomotiv.MoveTransport(DirectionType.Up);
|
|
break;
|
|
case "moveDownButton":
|
|
_drawingLocomotiv.MoveTransport(DirectionType.Down);
|
|
break;
|
|
case "moveLeftButton":
|
|
_drawingLocomotiv.MoveTransport(DirectionType.Left);
|
|
break;
|
|
case "moveRightButton":
|
|
_drawingLocomotiv.MoveTransport(DirectionType.Right);
|
|
break;
|
|
}
|
|
Draw();
|
|
}
|
|
|
|
private void MoveButton_Click(object sender, EventArgs e) {
|
|
if (_drawingLocomotiv == null)
|
|
return;
|
|
if (movesBox.Enabled) {
|
|
_abstractStrategy = movesBox.SelectedIndex
|
|
switch {
|
|
0 => new MoveToEdge(),
|
|
1 => new MoveToCenter(),
|
|
_ => null,
|
|
};
|
|
if (_abstractStrategy == null) {
|
|
return;
|
|
}
|
|
_abstractStrategy.SetData(new
|
|
DrawingObjectLocomotiv(_drawingLocomotiv), locoBox.Width,
|
|
locoBox.Height);
|
|
movesBox.Enabled = false;
|
|
}
|
|
if (_abstractStrategy == null) {
|
|
return;
|
|
}
|
|
_abstractStrategy.MakeStep();
|
|
Draw();
|
|
if (_abstractStrategy.GetStatus() == Status.Finish) {
|
|
movesBox.Enabled = true;
|
|
_abstractStrategy = null;
|
|
}
|
|
}
|
|
|
|
private void selectLocoButton_Click(object sender, EventArgs e) {
|
|
SelectedLocomotiv = _drawingLocomotiv;
|
|
DialogResult = DialogResult.OK;
|
|
}
|
|
}
|
|
} |