PIbd-14_Pruidze_I.K_Cruiser.../WinF_Lab1_Cruiser/CruiserMech/DrawingCanvas.cs

118 lines
4.1 KiB
C#

using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CruiserMech.Drawing_tools;
using CruiserMech.Entity_Folder;
using CruiserMech.Move_Strategy;
namespace CruiserMech
{
public partial class DrawingCanvas : Form
{
private DrawingBase? _DrCruiser;
private AbstractStrategy _strategy;
public DrawingCanvas()
{
InitializeComponent();
_strategy = null;
}
//private DrawingCanvas_Load(object sender, EventArgs e);
private void Draw()
{
if (_DrCruiser == null)
{
return;
}
Bitmap bmp = new(pictureBox1.Width, pictureBox1.Height);
Graphics gr = Graphics.FromImage(bmp); _DrCruiser.DrawTransport(gr);
pictureBox1.Image = bmp;
}
private void CreateObj(string type)
{
Random rn = new Random();
switch (type)
{
case nameof(DrawingBase):
_DrCruiser = new DrawingBase(rn.Next(100, 300), rn.Next(1000, 3000), Color.FromArgb(rn.Next(0, 256), rn.Next(0, 256), rn.Next(0, 256)));
break;
case nameof(DrawingCruiser):
Class_CruiserEntity Obj = new Class_CruiserEntity(rn.Next(100, 300), rn.Next(1000, 3000),
Color.FromArgb(rn.Next(0, 256), rn.Next(0, 256), rn.Next(0, 256)), Color.FromArgb(rn.Next(0, 256), rn.Next(0, 256), rn.Next(0, 256)),
Convert.ToBoolean(rn.Next(0, 2)), Convert.ToBoolean(rn.Next(0, 2)), rn.Next(1, 4), rn.Next(5, 10), rn.Next(1, 3));
_DrCruiser = new DrawingCruiser(Obj);
break;
default:
return;
}
_DrCruiser.SetPicSize(pictureBox1.Width, pictureBox1.Height);
_DrCruiser.SetPos(rn.Next(pictureBox1.Width - 600, pictureBox1.Width - _DrCruiser._CruiserMaxW), rn.Next(0, 100)); _strategy = null;
comboBox.Enabled = true;
Draw();
}
private void comboBox_Activate(object sender, EventArgs e)
{
if (_DrCruiser == null) return;
if (comboBox.Enabled)
{
_strategy = comboBox.SelectedIndex switch
{
0 => new Center(),
1 => new Boarder()
};
if (_strategy == null) return;
_strategy.SetData(new MovableCruiser(_DrCruiser), pictureBox1.Width, pictureBox1.Height);
}
if (_strategy == null) return;
comboBox.Enabled = false;
_strategy.MakeStep();
Draw();
if (_strategy.GetStatus() == Status.Finish)
{
comboBox.Enabled = true;
_strategy = null;
}
}
private void buttonCr2_Click(object sender, EventArgs e) => CreateObj(nameof(DrawingCruiser));
private void buttonCr_Click(object sender, EventArgs e) => CreateObj(nameof(DrawingBase));
private void ButtonMove_Click(object sender, EventArgs e)
{
if (_DrCruiser == null)
{
return;
}
string name = ((Button)sender)?.Name ?? string.Empty;
bool result = false;
switch (name)
{
case "buttonUp":
result = _DrCruiser.MoveTransport(DirectionType.Up);
break;
case "buttonDown":
result = _DrCruiser.MoveTransport(DirectionType.Down);
break;
case "buttonLeft":
result = _DrCruiser.MoveTransport(DirectionType.Left);
break;
case "buttonRight":
result = _DrCruiser.MoveTransport(DirectionType.Right);
break;
}
if (result)
{
Draw();
}
}
}
}