PIBD-11_Basalov_A.D_Simple/ProjectElectricLocomotive/FormlectricLocomotive.cs

110 lines
3.9 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ProjectElectricLocomotive.Drawnings;
namespace ProjectElectricLocomotive
{
public partial class FormlectricLocomotive : Form
{
private void Draw()
{
if (_drawnningLocomotive == null)
{
return;
}
Bitmap bmp = new(pictureBoxElectricLocomotive.Width, pictureBoxElectricLocomotive.Height);
Graphics gr = Graphics.FromImage(bmp);
_drawnningLocomotive.DrawTransport(gr);
pictureBoxElectricLocomotive.Image = bmp;
}
private void CreateObject(string type)
{
Random random = new();
switch (type)
{
case nameof(DrawningLocomotive):
_drawnningLocomotive = new DrawningLocomotive(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(DrawningElectricLocomotive):
_drawnningLocomotive = new DrawningElectricLocomotive(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;
}
_drawnningLocomotive.SetPictureSize(pictureBoxElectricLocomotive.Width, pictureBoxElectricLocomotive.Height);
_drawnningLocomotive.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw();
}
private DrawningLocomotive? _drawnningLocomotive;
public FormlectricLocomotive()
{
InitializeComponent();
}
/// <summary>
/// Обработка кнопки создать "ЭлектроЛокомотив"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonCreate_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningElectricLocomotive));
/// <summary>
/// Обработка нажатия кнопки создать "Локомотив"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonCreateLocomotive_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningLocomotive));
private void ButtonMove_Click(object sender, EventArgs e)
{
if (_drawnningLocomotive == null)
{
return;
}
string name = ((Button)sender)?.Name ?? string.Empty;
bool result = false;
switch (name)
{
case "buttonUp":
result =
_drawnningLocomotive.MoveTransport(DirectionType.Up);
break;
case "buttonDown":
result =
_drawnningLocomotive.MoveTransport(DirectionType.Down);
break;
case "buttonLeft":
result =
_drawnningLocomotive.MoveTransport(DirectionType.Left);
break;
case "buttonRight":
result =
_drawnningLocomotive.MoveTransport(DirectionType.Right);
break;
}
if (result)
{
Draw();
}
}
}
}