132 lines
4.3 KiB
C#
132 lines
4.3 KiB
C#
using Liner.Drawing;
|
|
using Liner.Entities;
|
|
using Liner.MovingStrategies;
|
|
|
|
namespace Liner
|
|
{
|
|
public partial class MainScreen : Form
|
|
{
|
|
/// <summary>
|
|
/// Ïîëå-îáúåêò äëÿ ïðîðèñîâêè îáúåêòà
|
|
/// </summary>
|
|
private DrawingLiner? _drawingLiner;
|
|
private Bitmap bmp;
|
|
/// <summary>
|
|
/// Ñòðàòåãèÿ ïåðåìåùåíèÿ
|
|
/// </summary>
|
|
private AbstractStrategy? _abstractStrategy;
|
|
/// <summary>
|
|
/// Èíèöèàëèçàöèÿ ôîðìû
|
|
/// </summary>
|
|
public MainScreen()
|
|
{
|
|
InitializeComponent();
|
|
bmp = new(pictureBoxLiner.Width, pictureBoxLiner.Height);
|
|
}
|
|
/// <summary>
|
|
/// Ìåòîä ïðîðèñîâêè ëàéíåðà
|
|
/// </summary>
|
|
private void Draw()
|
|
{
|
|
if (_drawingLiner == null)
|
|
{
|
|
return;
|
|
}
|
|
Graphics gr = Graphics.FromImage(bmp);
|
|
gr.Clear(Color.White);
|
|
_drawingLiner.DrawTransport(gr);
|
|
pictureBoxLiner.Image = bmp;
|
|
gr.Dispose();
|
|
}
|
|
private void buttonCreateLiner_Click(object sender, EventArgs e)
|
|
{
|
|
Random random = new();
|
|
_drawingLiner = new DrawingLiner(random.Next(100, 300),
|
|
random.Next(1000, 3000),
|
|
Color.FromArgb(random.Next(0, 256), random.Next(0, 256),
|
|
random.Next(0, 256)),
|
|
Color.FromArgb(random.Next(0, 256), random.Next(0, 256),
|
|
random.Next(0, 256)),
|
|
pictureBoxLiner.Width, pictureBoxLiner.Height);
|
|
_drawingLiner.SetPosition(random.Next(10, 100), random.Next(10, 100));
|
|
Draw();
|
|
}
|
|
|
|
private void buttonMove_Click(object sender, EventArgs e)
|
|
{
|
|
if (_drawingLiner == null)
|
|
{
|
|
return;
|
|
}
|
|
string name = ((Button)sender)?.Name ?? string.Empty;
|
|
switch (name)
|
|
{
|
|
case "buttonUp":
|
|
_drawingLiner.MoveTransport(DirectionType.Up);
|
|
break;
|
|
case "buttonDown":
|
|
_drawingLiner.MoveTransport(DirectionType.Down);
|
|
break;
|
|
case "buttonLeft":
|
|
_drawingLiner.MoveTransport(DirectionType.Left);
|
|
break;
|
|
case "buttonRight":
|
|
_drawingLiner.MoveTransport(DirectionType.Right);
|
|
break;
|
|
}
|
|
Draw();
|
|
}
|
|
|
|
private void buttonCreateBigLiner_Click(object sender, EventArgs e)
|
|
{
|
|
Random random = new();
|
|
_drawingLiner = new DrawingBigLiner(random.Next(100, 300),
|
|
random.Next(1000, 3000),
|
|
Color.FromArgb(random.Next(0, 256), random.Next(0, 256),
|
|
random.Next(0, 256)),
|
|
Color.FromArgb(random.Next(0, 256), random.Next(0, 256),
|
|
random.Next(0, 256)),
|
|
Convert.ToBoolean(random.Next(0, 2)),
|
|
Convert.ToBoolean(random.Next(1, 2)),
|
|
pictureBoxLiner.Width, pictureBoxLiner.Height);
|
|
_drawingLiner.SetPosition(random.Next(10, 100), random.Next(10, 100));
|
|
Draw();
|
|
}
|
|
|
|
private void buttonStep_Click(object sender, EventArgs e)
|
|
{
|
|
if (_drawingLiner == null)
|
|
{
|
|
return;
|
|
}
|
|
if (comboBoxStrategy.Enabled)
|
|
{
|
|
_abstractStrategy = comboBoxStrategy.SelectedIndex
|
|
switch
|
|
{
|
|
0 => new MoveToCenter(),
|
|
1 => new MoveToBorder(),
|
|
_ => null,
|
|
};
|
|
if (_abstractStrategy == null)
|
|
{
|
|
return;
|
|
}
|
|
_abstractStrategy.SetData(new DrawingObjectLiner(_drawingLiner), pictureBoxLiner.Width,
|
|
pictureBoxLiner.Height);
|
|
comboBoxStrategy.Enabled = false;
|
|
}
|
|
if (_abstractStrategy == null)
|
|
{
|
|
return;
|
|
}
|
|
_abstractStrategy.MakeStep();
|
|
Draw();
|
|
if (_abstractStrategy.GetStatus() == Status.Finish)
|
|
{
|
|
comboBoxStrategy.Enabled = true;
|
|
_abstractStrategy = null;
|
|
}
|
|
}
|
|
}
|
|
} |