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

118 lines
4.1 KiB
C#
Raw Normal View History

2024-04-16 08:45:59 +04:00
using System.ComponentModel;
2024-03-03 16:11:54 +04:00
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
2024-04-16 08:45:59 +04:00
using CruiserMech.Drawing_tools;
using CruiserMech.Entity_Folder;
using CruiserMech.Move_Strategy;
2024-03-03 16:11:54 +04:00
namespace CruiserMech
{
public partial class DrawingCanvas : Form
{
2024-04-16 08:45:59 +04:00
private DrawingBase? _DrCruiser;
private AbstractStrategy _strategy;
2024-03-03 16:11:54 +04:00
public DrawingCanvas()
{
InitializeComponent();
2024-04-16 08:45:59 +04:00
_strategy = null;
2024-03-03 16:11:54 +04:00
}
//private DrawingCanvas_Load(object sender, EventArgs e);
private void Draw()
{
if (_DrCruiser == null)
{
return;
}
Bitmap bmp = new(pictureBox1.Width, pictureBox1.Height);
2024-04-16 08:45:59 +04:00
Graphics gr = Graphics.FromImage(bmp); _DrCruiser.DrawTransport(gr);
2024-03-03 16:11:54 +04:00
pictureBox1.Image = bmp;
2024-04-16 08:45:59 +04:00
}
2024-03-03 16:11:54 +04:00
2024-04-16 08:45:59 +04:00
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();
2024-03-03 16:11:54 +04:00
}
2024-04-16 08:45:59 +04:00
private void comboBox_Activate(object sender, EventArgs e)
2024-03-03 16:11:54 +04:00
{
2024-04-16 08:45:59 +04:00
if (_DrCruiser == null) return;
2024-03-03 16:11:54 +04:00
2024-04-16 08:45:59 +04:00
if (comboBox.Enabled)
{
_strategy = comboBox.SelectedIndex switch
{
0 => new Center(),
1 => new Boarder()
};
2024-03-03 16:11:54 +04:00
2024-04-16 08:45:59 +04:00
if (_strategy == null) return;
_strategy.SetData(new MovableCruiser(_DrCruiser), pictureBox1.Width, pictureBox1.Height);
}
if (_strategy == null) return;
comboBox.Enabled = false;
2024-03-03 16:11:54 +04:00
2024-04-16 08:45:59 +04:00
_strategy.MakeStep();
2024-03-03 16:11:54 +04:00
Draw();
2024-04-16 08:45:59 +04:00
if (_strategy.GetStatus() == Status.Finish)
{
comboBox.Enabled = true;
_strategy = null;
}
2024-03-03 16:11:54 +04:00
}
2024-04-16 08:45:59 +04:00
private void buttonCr2_Click(object sender, EventArgs e) => CreateObj(nameof(DrawingCruiser));
private void buttonCr_Click(object sender, EventArgs e) => CreateObj(nameof(DrawingBase));
2024-03-03 16:11:54 +04:00
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();
}
}
}
}