PIbd-22_Fedorenko_G.Y._Hydr.../Hydroplane/FormHydroplane.cs

141 lines
4.8 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Hydroplane.Entities;
using Hydroplane.DrawningObjects;
using Hydroplane.MovementStrategy;
2023-09-22 18:54:14 +04:00
namespace Hydroplane
{
public partial class FormHydroplane : Form
2023-09-22 18:54:14 +04:00
{
private DrawningPlane? _drawingPlane;
private AbstractStrategy? _abstractStrategy;
2023-10-25 02:19:13 +04:00
public DrawningPlane? SelectedPlane { get; private set; }
public FormHydroplane()
2023-09-22 18:54:14 +04:00
{
InitializeComponent();
2023-10-25 02:19:13 +04:00
_abstractStrategy = null;
SelectedPlane = null;
2023-09-22 18:54:14 +04:00
}
private void Draw()
{
if (_drawingPlane == null)
{
return;
}
Bitmap bmp = new(pictureBoxHydroplane.Width,
pictureBoxHydroplane.Height);
Graphics gr = Graphics.FromImage(bmp);
_drawingPlane.DrawTransport(gr);
pictureBoxHydroplane.Image = bmp;
}
private void buttonCreatePlane_Click(object sender, EventArgs e)
{
Random random = new();
2023-10-25 02:19:13 +04:00
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;
}
_drawingPlane = new DrawningPlane(random.Next(100, 300), random.Next(1000, 3000), color, pictureBoxHydroplane.Width, pictureBoxHydroplane.Height);
_drawingPlane.SetPosition(random.Next(10, 100), random.Next(10,
100));
Draw();
}
private void buttonCreateHydroplane_Click(object sender, EventArgs e)
{
Random random = new();
2023-10-25 02:19:13 +04:00
Color mainColor = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256));
ColorDialog dialog = new();
if (dialog.ShowDialog() == DialogResult.OK)
{
mainColor = dialog.Color;
}
Color addColor = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256));
if (dialog.ShowDialog() == DialogResult.OK)
{
addColor = dialog.Color;
}
_drawingPlane = new DrawningHydroplane(random.Next(100, 300), random.Next(1000, 3000), mainColor, addColor,
Convert.ToBoolean(random.Next(0, 2)),
Convert.ToBoolean(random.Next(0, 2)), pictureBoxHydroplane.Width, pictureBoxHydroplane.Height);
_drawingPlane.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw();
}
private void buttonMove_Click(object sender, EventArgs e)
{
if (_drawingPlane == null)
{
return;
}
string name = ((Button)sender)?.Name ?? string.Empty;
switch (name)
{
case "buttonUp":
_drawingPlane.MoveTransport(DirectionType.Up);
break;
case "buttonDown":
_drawingPlane.MoveTransport(DirectionType.Down);
break;
case "buttonLeft":
_drawingPlane.MoveTransport(DirectionType.Left);
break;
case "buttonRight":
_drawingPlane.MoveTransport(DirectionType.Right);
break;
}
Draw();
}
private void buttonStep_Click(object sender, EventArgs e)
{
if (_drawingPlane == null)
{
return;
}
if (comboBoxStrategy.Enabled)
{
_abstractStrategy = comboBoxStrategy.SelectedIndex
switch
{
0 => new MoveToCenter(),
1 => new MoveToBorder(),
_ => null,
};
if (_abstractStrategy == null)
{
return;
}
2023-10-25 02:19:13 +04:00
_abstractStrategy.SetData(_drawingPlane.GetMoveableObject, pictureBoxHydroplane.Width, pictureBoxHydroplane.Height);
comboBoxStrategy.Enabled = false;
}
if (_abstractStrategy == null)
{
return;
}
_abstractStrategy.MakeStep();
Draw();
if (_abstractStrategy.GetStatus() == Status.Finish)
{
comboBoxStrategy.Enabled = true;
_abstractStrategy = null;
}
}
2023-10-25 02:19:13 +04:00
private void ButtonSelectPLane_Click(object sender, EventArgs e)
{
SelectedPlane = _drawingPlane;
DialogResult = DialogResult.OK;
}
2023-09-22 18:54:14 +04:00
}
}