142 lines
3.6 KiB
C#
142 lines
3.6 KiB
C#
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 ProjectDumpTruck.Drawnings;
|
||
using ProjectDumpTruck.MovementStrategy;
|
||
|
||
namespace ProjectDumpTruck;
|
||
|
||
public partial class FormDumpTruck : Form
|
||
{
|
||
/// <summary>
|
||
/// Поле
|
||
/// объект для прорисовки объекта
|
||
/// </summary>
|
||
private DrawningTruck? _drawningTruck;
|
||
/// <summary>
|
||
/// Стратегия перемещения
|
||
/// </summary>
|
||
private AbstractStrategy? _strategy;
|
||
|
||
/// <summary>
|
||
/// Получение объекта
|
||
/// </summary>
|
||
public DrawningTruck SetTruck
|
||
{
|
||
set
|
||
{
|
||
_drawningTruck = value;
|
||
_drawningTruck.SetPictureSize(pictureBoxDumpTruck.Width, pictureBoxDumpTruck.Height);
|
||
comboBoxStrategy.Enabled = true;
|
||
_strategy = null;
|
||
Draw();
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// Конструктор формы
|
||
/// </summary>
|
||
public FormDumpTruck()
|
||
{
|
||
InitializeComponent();
|
||
_strategy = null;
|
||
}
|
||
/// <summary>
|
||
/// Метод прорисовки машины
|
||
/// </summary>
|
||
private void Draw()
|
||
{
|
||
if (_drawningTruck == null)
|
||
{
|
||
return;
|
||
}
|
||
|
||
Bitmap bmp = new(pictureBoxDumpTruck.Width, pictureBoxDumpTruck.Height);
|
||
Graphics gr = Graphics.FromImage(bmp);
|
||
_drawningTruck.DrawTransport(gr);
|
||
pictureBoxDumpTruck.Image = bmp;
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// Перемещение объекта по форме (нажатие кнопок навигации)
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void ButtonMove_Click(object sender, EventArgs e)
|
||
{
|
||
if (_drawningTruck == null)
|
||
{
|
||
return;
|
||
}
|
||
string name = ((Button)sender)?.Name ?? string.Empty;
|
||
bool result = false;
|
||
switch (name)
|
||
{
|
||
case "buttonUp":
|
||
result =
|
||
_drawningTruck.MoveTransport(DirectionType.Up);
|
||
break;
|
||
case "buttonDown":
|
||
result =
|
||
_drawningTruck.MoveTransport(DirectionType.Down);
|
||
break;
|
||
case "buttonLeft":
|
||
result =
|
||
_drawningTruck.MoveTransport(DirectionType.Left);
|
||
break;
|
||
case "buttonRight":
|
||
result =
|
||
_drawningTruck.MoveTransport(DirectionType.Right);
|
||
break;
|
||
}
|
||
if (result)
|
||
{
|
||
Draw();
|
||
}
|
||
}
|
||
|
||
private void ВuttonStrategyStep_Click(object sender, EventArgs e)
|
||
{
|
||
if (_drawningTruck == null)
|
||
{
|
||
return;
|
||
}
|
||
if (comboBoxStrategy.Enabled)
|
||
{
|
||
_strategy = comboBoxStrategy.SelectedIndex switch
|
||
{
|
||
0 => new MoveToCenter(),
|
||
1 => new MoveToBorder(),
|
||
_ => null,
|
||
};
|
||
if (_strategy == null)
|
||
{
|
||
return;
|
||
}
|
||
_strategy.SetData(new MoveableTruck(_drawningTruck), pictureBoxDumpTruck.Width, pictureBoxDumpTruck.Height);
|
||
}
|
||
if (_strategy == null)
|
||
{
|
||
return;
|
||
}
|
||
comboBoxStrategy.Enabled = false;
|
||
_strategy.MakeStep();
|
||
Draw();
|
||
if (_strategy.GetStatus() == StrategyStatus.Finish)
|
||
{
|
||
comboBoxStrategy.Enabled = true;
|
||
_strategy = null;
|
||
}
|
||
}
|
||
|
||
}
|
||
|
||
|