PIbd-22_Smirnov_A_A_RoadTra.../RoadTrain/RoadTrain/RoadTrain.cs

140 lines
4.8 KiB
C#

using System;
using System.Collections.Generic;
using RoadTrain.DrawingObjects;
using RoadTrain.MovementStrategy;
namespace RoadTrain
{
public partial class RoadTrain : Form
{
private DrawingRoadTrain? _drawingRoadTrain;
private AbstractStrategy? _abstractStrategy;
public DrawingRoadTrain? SelectedTrain { get; private set; }
public RoadTrain()
{
InitializeComponent();
_abstractStrategy = null;
SelectedTrain = null;
}
private void Draw()
{
if (_drawingRoadTrain == null)
{
return;
}
Bitmap bmp = new(pictureBoxRoadTrain.Width,
pictureBoxRoadTrain.Height);
Graphics gr = Graphics.FromImage(bmp);
_drawingRoadTrain.DrawTransport(gr);
pictureBoxRoadTrain.Image = bmp;
}
private void buttonCreate_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;
}
_drawingRoadTrain = new DrawingRoadTrain(random.Next(100, 300),
random.Next(1000, 3000),
color,
pictureBoxRoadTrain.Width, pictureBoxRoadTrain.Height);
_drawingRoadTrain.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw();
}
private void buttonCreateRoadTrain_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 addColor = Color.FromArgb(random.Next(0, 256),
random.Next(0, 256), random.Next(0, 256));
ColorDialog dialogDopColor = new();
if (dialogDopColor.ShowDialog() == DialogResult.OK)
{
addColor = dialogDopColor.Color;
}
_drawingRoadTrain = new DrawingRoadTrainWithTank(random.Next(100, 300),
random.Next(1000, 3000),
color, addColor,
Convert.ToBoolean(random.Next(0, 2)),
Convert.ToBoolean(random.Next(0, 2)),
pictureBoxRoadTrain.Width, pictureBoxRoadTrain.Height);
_drawingRoadTrain.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw();
}
private void buttonMove_Click(object sender, EventArgs e)
{
if (_drawingRoadTrain == null)
{
return;
}
string name = ((Button)sender)?.Name ?? string.Empty;
switch (name)
{
case "buttonUp":
_drawingRoadTrain.MoveTransport(DirectionType.Up);
break;
case "buttonDown":
_drawingRoadTrain.MoveTransport(DirectionType.Down);
break;
case "buttonLeft":
_drawingRoadTrain.MoveTransport(DirectionType.Left);
break;
case "buttonRight":
_drawingRoadTrain.MoveTransport(DirectionType.Right);
break;
}
Draw();
}
private void buttonStep_Click(object sender, EventArgs e)
{
if (_drawingRoadTrain == null)
{
return;
}
if (comboBoxStrategy.Enabled)
{
_abstractStrategy = comboBoxStrategy.SelectedIndex
switch
{
0 => new MoveToCenter(),
1 => new MoveToBorder(),
_ => null,
};
if (_abstractStrategy == null)
{
return;
}
_abstractStrategy.SetData(_drawingRoadTrain.GetMoveableObject, pictureBoxRoadTrain.Width,
pictureBoxRoadTrain.Height);
comboBoxStrategy.Enabled = false;
}
if (_abstractStrategy == null)
{
return;
}
_abstractStrategy.MakeStep();
Draw();
if (_abstractStrategy.GetStatus() == Status.Finish)
{
comboBoxStrategy.Enabled = true;
_abstractStrategy = null;
}
}
private void ButtonSelectTrain_Click(object sender, EventArgs e)
{
SelectedTrain = _drawingRoadTrain;
DialogResult = DialogResult.OK;
}
}
}