PIbd-23_Vrazhkin_S_A_Electr.../lab1/FormLocomotiv.cs

108 lines
4.5 KiB
C#
Raw Permalink Normal View History

2023-09-19 19:04:33 +04:00
using ElectricLocomotive;
2023-10-17 13:49:31 +04:00
namespace ElectricLocomotive {
public partial class FormLocomotiv : Form {
2023-09-19 19:04:33 +04:00
private DrawingLocomotiv? _drawingLocomotiv;
2023-10-17 13:49:31 +04:00
private AbstractStrategy? _abstractStrategy;
public DrawingLocomotiv? SelectedLocomotiv { get; private set; }
public FormLocomotiv() {
2023-09-19 19:04:33 +04:00
InitializeComponent();
2023-10-17 13:49:31 +04:00
_abstractStrategy = null;
SelectedLocomotiv = null;
2023-09-19 19:04:33 +04:00
}
2023-10-17 13:49:31 +04:00
private void Draw() {
2023-09-19 19:04:33 +04:00
if (_drawingLocomotiv == null) return;
Bitmap bmp = new(locoBox.Width, locoBox.Height);
Graphics g = Graphics.FromImage(bmp);
_drawingLocomotiv.DrawLoco(g);
locoBox.Image = bmp;
}
2023-10-17 13:49:31 +04:00
private void PaintLocoButton_click(object sender, EventArgs e) {
2023-09-19 19:04:33 +04:00
Random random = new();
2023-10-17 13:49:31 +04:00
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);
2023-09-19 19:04:33 +04:00
_drawingLocomotiv.SetPosition(random.Next(10, 100), random.Next(10, 100));
2023-10-17 13:49:31 +04:00
2023-09-19 19:04:33 +04:00
Draw();
}
2023-10-17 13:49:31 +04:00
private void PaintElectricLocoButton_click(object sender, EventArgs e) {
2023-10-03 11:53:51 +04:00
Random random = new();
2023-10-17 13:49:31 +04:00
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;
2023-11-28 12:26:51 +04:00
_drawingLocomotiv = new DrawingElectricLocomotiv(random.Next(100, 300), random.Next(1000, 3000), locoBox.Width, locoBox.Height, color, dopColor, batteryColor, rogaColor,true, true);
2023-10-03 11:53:51 +04:00
_drawingLocomotiv.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw();
}
2023-10-17 13:49:31 +04:00
private void MoveSideButton_Click(object sender, EventArgs e) {
2023-09-19 19:04:33 +04:00
if (_drawingLocomotiv == null) return;
Control button = sender as Control;
string name = button.Name;
2023-10-17 13:49:31 +04:00
switch (name) {
2023-09-19 19:04:33 +04:00
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();
}
2023-10-03 11:53:51 +04:00
2023-10-17 13:49:31 +04:00
private void MoveButton_Click(object sender, EventArgs e) {
2023-10-03 11:53:51 +04:00
if (_drawingLocomotiv == null)
return;
2023-10-17 13:49:31 +04:00
if (movesBox.Enabled) {
2023-10-03 11:53:51 +04:00
_abstractStrategy = movesBox.SelectedIndex
2023-10-17 13:49:31 +04:00
switch {
2023-10-03 11:53:51 +04:00
0 => new MoveToEdge(),
1 => new MoveToCenter(),
_ => null,
2023-10-17 13:49:31 +04:00
};
if (_abstractStrategy == null) {
2023-10-03 11:53:51 +04:00
return;
}
_abstractStrategy.SetData(new
DrawingObjectLocomotiv(_drawingLocomotiv), locoBox.Width,
locoBox.Height);
movesBox.Enabled = false;
}
2023-10-17 13:49:31 +04:00
if (_abstractStrategy == null) {
2023-10-03 11:53:51 +04:00
return;
}
_abstractStrategy.MakeStep();
Draw();
2023-10-17 13:49:31 +04:00
if (_abstractStrategy.GetStatus() == Status.Finish) {
2023-10-03 11:53:51 +04:00
movesBox.Enabled = true;
_abstractStrategy = null;
}
}
2023-10-17 13:49:31 +04:00
private void selectLocoButton_Click(object sender, EventArgs e) {
SelectedLocomotiv = _drawingLocomotiv;
DialogResult = DialogResult.OK;
}
2023-09-19 19:04:33 +04:00
}
}