PIbd-14_Rachek_A.S._Base/lab_0/FormBus.cs

119 lines
3.9 KiB
C#
Raw Normal View History

using ProjectBus.Drawings;
using ProjectBus.Drawnings;
using System;
2024-02-29 10:45:00 +04:00
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;
namespace ProjectBus
{
public partial class FormBus : Form
{
private DrawningSimpleBus? _drawningSimpleBus;
2024-02-29 10:45:00 +04:00
public FormBus()
{
InitializeComponent();
}
private void Draw()
{
if (_drawningSimpleBus == null)
2024-02-29 10:45:00 +04:00
{
return;
}
Bitmap bmp = new(pictureBoxBus.Width, pictureBoxBus.Height);
Graphics gr = Graphics.FromImage(bmp);
_drawningSimpleBus.DrawTransport(gr);
2024-02-29 10:45:00 +04:00
pictureBoxBus.Image = bmp;
}
/// <summary>
/// Создание объекта класса-перемещения
/// </summary>
/// <param name="type">Тип создаваемого объекта</param>
private void CreateObject(string type)
2024-02-29 10:45:00 +04:00
{
Random random = new();
switch (type)
{
case nameof(DrawningSimpleBus):
_drawningSimpleBus = new DrawningSimpleBus(random.Next(100, 300), random.Next(1000, 3000),
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)));
break;
case nameof(DrawningBus):
_drawningSimpleBus = new DrawningBus(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(0, 2)));
break;
default:
return;
}
2024-02-29 10:45:00 +04:00
_drawningSimpleBus.SetPictureSize(pictureBoxBus.Width, pictureBoxBus.Height);
_drawningSimpleBus.SetPosition(random.Next(10, 100), random.Next(10, 100));
2024-02-29 10:45:00 +04:00
Draw();
2024-02-29 10:45:00 +04:00
}
/// <summary>
/// Обработка нажатия кнопки "Создать автобус с доп отсеком"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonCreate_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningBus));
/// <summary>
/// Обработка нажатия кнопки "Создать обычный автобус"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonCreateSimpleBus_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningSimpleBus));
2024-02-29 10:45:00 +04:00
private void ButtonMove_Click(object sender, EventArgs e)
{
if (_drawningSimpleBus == null)
2024-02-29 10:45:00 +04:00
{
return;
}
string name = ((Button)sender)?.Name ?? string.Empty;
bool result = false;
switch (name)
{
case "buttonUp":
result =
_drawningSimpleBus.MoveTransport(DirectionType.Up);
2024-02-29 10:45:00 +04:00
break;
case "buttonDown":
result =
_drawningSimpleBus.MoveTransport(DirectionType.Down);
2024-02-29 10:45:00 +04:00
break;
case "buttonLeft":
result =
_drawningSimpleBus.MoveTransport(DirectionType.Left);
2024-02-29 10:45:00 +04:00
break;
case "buttonRight":
result =
_drawningSimpleBus.MoveTransport(DirectionType.Right);
2024-02-29 10:45:00 +04:00
break;
}
if (result)
{
Draw();
}
}
}
}