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

128 lines
3.7 KiB
C#
Raw Normal View History

2024-02-19 09:25:06 +04:00
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;
using ProjectElectricLocomotive.MovementStrategy;
2024-02-19 09:25:06 +04:00
namespace ProjectElectricLocomotive
{
public partial class FormlectricLocomotive : Form
{
/// <summary>
/// Стратегия перемещения
/// </summary>
private AbstractStrategy? _strategy;
2024-03-14 14:26:53 +04:00
public DrawningLocomotive SetLocomotive
{
set
{
_drawnningLocomotive = value;
_drawnningLocomotive.SetPictureSize(pictureBoxElectricLocomotive.Width, pictureBoxElectricLocomotive.Height);
comboBoxStrategy.Enabled = true;
_strategy = null;
Draw();
}
}
public FormlectricLocomotive()
{
InitializeComponent();
_strategy = null;
}
2024-02-19 09:25:06 +04:00
private void Draw()
{
if (_drawnningLocomotive == null)
2024-02-19 09:25:06 +04:00
{
return;
}
Bitmap bmp = new(pictureBoxElectricLocomotive.Width, pictureBoxElectricLocomotive.Height);
Graphics gr = Graphics.FromImage(bmp);
_drawnningLocomotive.DrawTransport(gr);
2024-02-19 09:25:06 +04:00
pictureBoxElectricLocomotive.Image = bmp;
}
private DrawningLocomotive? _drawnningLocomotive;
2024-02-19 09:25:06 +04:00
private void ButtonMove_Click(object sender, EventArgs e)
{
if (_drawnningLocomotive == null)
2024-02-19 09:25:06 +04:00
{
return;
}
string name = ((Button)sender)?.Name ?? string.Empty;
bool result = false;
switch (name)
{
case "buttonUp":
result =
_drawnningLocomotive.MoveTransport(DirectionType.Up);
2024-02-19 09:25:06 +04:00
break;
case "buttonDown":
result =
_drawnningLocomotive.MoveTransport(DirectionType.Down);
2024-02-19 09:25:06 +04:00
break;
case "buttonLeft":
result =
_drawnningLocomotive.MoveTransport(DirectionType.Left);
2024-02-19 09:25:06 +04:00
break;
case "buttonRight":
result =
_drawnningLocomotive.MoveTransport(DirectionType.Right);
2024-02-19 09:25:06 +04:00
break;
}
if (result)
{
Draw();
}
}
2024-03-14 14:26:53 +04:00
private void buttonStrategyStep_Click(object sender, EventArgs e)
{
if (_drawnningLocomotive == null)
{
return;
}
if (comboBoxStrategy.Enabled)
{
_strategy = comboBoxStrategy.SelectedIndex switch
{
0 => new MoveToCenter(),
1 => new MoveToBorder(),
_ => null,
};
if (_strategy == null)
{
return;
}
_strategy.SetData(new MoveableLocomotive(_drawnningLocomotive),
pictureBoxElectricLocomotive.Width, pictureBoxElectricLocomotive.Height);
}
if (_strategy == null)
{
return;
}
comboBoxStrategy.Enabled = false;
_strategy.MakeStep();
Draw();
if (_strategy.GetStatus() == StrategyStatus.Finish)
{
comboBoxStrategy.Enabled = true;
_strategy = null;
}
}
2024-02-19 09:25:06 +04:00
}
}