2022-12-23 00:48:39 +04:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using System;
|
2022-11-28 22:39:41 +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;
|
|
|
|
|
using WinFormsApp1;
|
|
|
|
|
|
|
|
|
|
namespace WinFormsApp1
|
|
|
|
|
{
|
|
|
|
|
public partial class FormMapWithSetTraktor : Form
|
|
|
|
|
{
|
|
|
|
|
private MapWithSetTraktorGeneric<DrawningObjectTractor, AbstractMap> _mapTraktorCollectionGeneric;
|
|
|
|
|
|
2022-11-29 02:16:22 +04:00
|
|
|
|
private readonly Dictionary<string, AbstractMap> _mapsDict = new()
|
|
|
|
|
{
|
|
|
|
|
{"Простая карта", new SimpleMap() },
|
|
|
|
|
{"Поле", new FieldMap() }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private readonly MapsCollection _mapsCollection;
|
2022-11-28 22:39:41 +04:00
|
|
|
|
|
2022-12-23 00:48:39 +04:00
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
|
|
|
|
|
public FormMapWithSetTraktor(ILogger<FormMapWithSetTraktor> logger)
|
2022-11-28 22:39:41 +04:00
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
2022-12-23 00:48:39 +04:00
|
|
|
|
_logger = logger;
|
2022-11-29 02:16:22 +04:00
|
|
|
|
_mapsCollection = new MapsCollection(pictureBox.Width, pictureBox.Height);
|
|
|
|
|
comboBoxSelectorMap.Items.Clear();
|
|
|
|
|
foreach (var item in _mapsDict)
|
|
|
|
|
{
|
|
|
|
|
comboBoxSelectorMap.Items.Add(item.Key);
|
|
|
|
|
}
|
2022-12-23 00:48:39 +04:00
|
|
|
|
}
|
2022-11-29 02:16:22 +04:00
|
|
|
|
|
2022-12-23 00:48:39 +04:00
|
|
|
|
public FormMapWithSetTraktor()
|
|
|
|
|
{
|
2022-11-29 02:16:22 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ReloadMaps()
|
|
|
|
|
{
|
|
|
|
|
int index = listBoxMaps.SelectedIndex;
|
|
|
|
|
|
|
|
|
|
listBoxMaps.Items.Clear();
|
|
|
|
|
for (int i = 0; i < _mapsCollection.Keys.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
listBoxMaps.Items.Add(_mapsCollection.Keys[i]);
|
|
|
|
|
}
|
2022-11-28 22:39:41 +04:00
|
|
|
|
|
2022-11-29 02:16:22 +04:00
|
|
|
|
if (listBoxMaps.Items.Count > 0 && (index == -1 || index >= listBoxMaps.Items.Count))
|
|
|
|
|
{
|
|
|
|
|
listBoxMaps.SelectedIndex = 0;
|
|
|
|
|
}
|
|
|
|
|
else if (listBoxMaps.Items.Count > 0 && index > -1 && index < listBoxMaps.Items.Count)
|
|
|
|
|
{
|
|
|
|
|
listBoxMaps.SelectedIndex = index;
|
|
|
|
|
}
|
2022-11-28 22:39:41 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ComboBoxSelectorMap_SelectedIndexChanged(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
AbstractMap map = null;
|
|
|
|
|
switch (comboBoxSelectorMap.Text)
|
|
|
|
|
{
|
|
|
|
|
case "Простая карта":
|
|
|
|
|
map = new SimpleMap();
|
|
|
|
|
break;
|
|
|
|
|
case "Поле":
|
|
|
|
|
map = new FieldMap();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (map != null)
|
|
|
|
|
{
|
|
|
|
|
_mapTraktorCollectionGeneric = new MapWithSetTraktorGeneric<DrawningObjectTractor, AbstractMap>(pictureBox.Width, pictureBox.Height, map);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_mapTraktorCollectionGeneric = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ButtonAddTraktor_Click(object sender, EventArgs e)
|
2022-12-12 23:38:47 +04:00
|
|
|
|
{
|
|
|
|
|
var formBusConfig = new FormTraktorConfig();
|
|
|
|
|
formBusConfig.AddEvent(AddTraktor);
|
|
|
|
|
formBusConfig.Show();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void AddTraktor(TractorDraw traktor)
|
2022-11-28 22:39:41 +04:00
|
|
|
|
{
|
2022-12-23 00:48:39 +04:00
|
|
|
|
try
|
2022-11-28 22:39:41 +04:00
|
|
|
|
{
|
2022-12-23 00:48:39 +04:00
|
|
|
|
if (listBoxMaps.SelectedIndex == -1)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
DrawningObjectTractor boat = new(traktor);
|
|
|
|
|
if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + boat >= 0)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("Объект добавлен");
|
|
|
|
|
_logger.LogInformation("Объект добавлен");
|
|
|
|
|
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("Не удалось добавить объект");
|
|
|
|
|
_logger.LogInformation("Не удалось добавить объект");
|
|
|
|
|
}
|
2022-11-28 22:39:41 +04:00
|
|
|
|
}
|
2022-12-23 00:48:39 +04:00
|
|
|
|
catch (StorageOverflowException ex)
|
2022-11-28 22:39:41 +04:00
|
|
|
|
{
|
2022-12-23 00:48:39 +04:00
|
|
|
|
_logger.LogWarning($"Ошибка, переполнение хранилища: {0}", ex.Message);
|
|
|
|
|
MessageBox.Show($"Ошибка, хранилище переполнено: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
2022-12-12 23:38:47 +04:00
|
|
|
|
}
|
2022-12-23 00:48:39 +04:00
|
|
|
|
catch (ArgumentException ex)
|
2022-12-12 23:38:47 +04:00
|
|
|
|
{
|
2022-12-23 00:48:39 +04:00
|
|
|
|
_logger.LogWarning("Ошибка добавления");
|
|
|
|
|
MessageBox.Show(ex.Message, "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
2022-11-28 22:39:41 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-12-23 00:48:39 +04:00
|
|
|
|
|
2022-11-28 22:39:41 +04:00
|
|
|
|
private void ButtonRemoveTraktor_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
2022-11-29 02:16:22 +04:00
|
|
|
|
if (listBoxMaps.SelectedIndex == -1)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-28 22:39:41 +04:00
|
|
|
|
if (string.IsNullOrEmpty(maskedTextBoxPosition.Text))
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-11-29 02:16:22 +04:00
|
|
|
|
|
2022-11-28 22:39:41 +04:00
|
|
|
|
if (MessageBox.Show("Удалить объект?", "Удаление",
|
|
|
|
|
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-11-29 02:16:22 +04:00
|
|
|
|
|
2022-11-28 22:39:41 +04:00
|
|
|
|
int pos = Convert.ToInt32(maskedTextBoxPosition.Text);
|
2022-12-23 00:48:39 +04:00
|
|
|
|
try
|
2022-11-28 22:39:41 +04:00
|
|
|
|
{
|
2022-12-23 00:48:39 +04:00
|
|
|
|
var deletedTraktor = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] - pos;
|
|
|
|
|
if (deletedTraktor != null)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("Объект удален");
|
2022-12-23 01:28:21 +04:00
|
|
|
|
_logger.LogInformation("Из текущей карты удален объект {@traktor}", deletedTraktor);
|
2022-12-23 00:48:39 +04:00
|
|
|
|
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-12-23 01:28:21 +04:00
|
|
|
|
_logger.LogWarning("Не удалось добавить объект по позиции {0} равен null", pos);
|
2022-12-23 00:48:39 +04:00
|
|
|
|
MessageBox.Show("Не удалось удалить объект");
|
|
|
|
|
}
|
2022-11-28 22:39:41 +04:00
|
|
|
|
}
|
2022-12-23 01:13:22 +04:00
|
|
|
|
catch (TraktorNotFoundException ex)
|
2022-11-28 22:39:41 +04:00
|
|
|
|
{
|
2022-12-23 00:48:39 +04:00
|
|
|
|
_logger.LogWarning("Ошибка удаления: {0}", ex.Message);
|
|
|
|
|
MessageBox.Show($"Ошибка удаления: {ex.Message}");
|
2022-11-28 22:39:41 +04:00
|
|
|
|
}
|
2022-12-23 01:31:29 +04:00
|
|
|
|
catch (Exception ex)
|
2022-12-23 01:13:22 +04:00
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Неизвестная ошибка удаления: {0}", ex.Message);
|
|
|
|
|
MessageBox.Show($"Неизвестная ошибка: {ex.Message}");
|
|
|
|
|
}
|
2022-11-28 22:39:41 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ButtonShowStorage_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (_mapTraktorCollectionGeneric == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-11-29 02:16:22 +04:00
|
|
|
|
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
|
2022-11-28 22:39:41 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ButtonShowOnMap_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (_mapTraktorCollectionGeneric == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-11-29 02:16:22 +04:00
|
|
|
|
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowOnMap();
|
2022-11-28 22:39:41 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ButtonMove_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (_mapTraktorCollectionGeneric == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string name = ((Button)sender)?.Name ?? string.Empty;
|
|
|
|
|
Direction dir = Direction.None;
|
|
|
|
|
switch (name)
|
|
|
|
|
{
|
|
|
|
|
case "buttonUp":
|
|
|
|
|
dir = Direction.Up;
|
|
|
|
|
break;
|
|
|
|
|
case "buttonDown":
|
|
|
|
|
dir = Direction.Down;
|
|
|
|
|
break;
|
|
|
|
|
case "buttonLeft":
|
|
|
|
|
dir = Direction.Left;
|
|
|
|
|
break;
|
|
|
|
|
case "buttonRight":
|
|
|
|
|
dir = Direction.Right;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
pictureBox.Image = _mapTraktorCollectionGeneric.MoveObject(dir);
|
|
|
|
|
}
|
2022-11-29 02:16:22 +04:00
|
|
|
|
|
|
|
|
|
private void buttonAddMap_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (comboBoxSelectorMap.SelectedIndex == -1 || string.IsNullOrEmpty(textBoxNewMapName.Text))
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("Не все данные заполнены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
2022-12-23 00:48:39 +04:00
|
|
|
|
_logger.LogInformation("При добавлении карты {0}", comboBoxSelectorMap.SelectedIndex == -1 ? "Не была выбрана карта" : "Не была названа карта");
|
2022-11-29 02:16:22 +04:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (!_mapsDict.ContainsKey(comboBoxSelectorMap.Text))
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("Нет такой карты", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
2022-12-23 00:48:39 +04:00
|
|
|
|
_logger.LogInformation("При добавлении карты {0}", comboBoxSelectorMap.SelectedIndex == -1 ? "Не была выбрана карта" : "Не была названа карта");
|
2022-11-29 02:16:22 +04:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
_mapsCollection.AddMap(textBoxNewMapName.Text, _mapsDict[comboBoxSelectorMap.Text]);
|
|
|
|
|
ReloadMaps();
|
2022-12-23 00:48:39 +04:00
|
|
|
|
_logger.LogInformation("Добавлена карта {0}", textBoxNewMapName.Text);
|
2022-11-29 02:16:22 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void listBoxMaps_SelectedIndexChanged(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
|
2022-12-23 00:48:39 +04:00
|
|
|
|
_logger.LogInformation("Осуществлён переход на карту под названием {0}", listBoxMaps.SelectedItem?.ToString() ?? string.Empty);
|
2022-11-29 02:16:22 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void buttonDeleteMap_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (listBoxMaps.SelectedIndex == -1)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (MessageBox.Show($"Удалить карту {listBoxMaps.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
|
|
|
|
|
{
|
|
|
|
|
_mapsCollection.DelMap(listBoxMaps.SelectedItem?.ToString() ?? string.Empty);
|
|
|
|
|
ReloadMaps();
|
2022-12-23 00:48:39 +04:00
|
|
|
|
_logger.LogInformation("Удалена карта {0}", listBoxMaps.SelectedItem?.ToString() ?? string.Empty);
|
2022-11-29 02:16:22 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-12-23 00:48:39 +04:00
|
|
|
|
|
2022-12-13 01:49:12 +04:00
|
|
|
|
private void SaveToolStripMenuItem_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (saveFileDialog.ShowDialog() == DialogResult.OK)
|
|
|
|
|
{
|
2022-12-23 00:48:39 +04:00
|
|
|
|
try
|
2022-12-13 01:49:12 +04:00
|
|
|
|
{
|
2022-12-23 00:48:39 +04:00
|
|
|
|
_mapsCollection.SaveData(saveFileDialog.FileName);
|
|
|
|
|
_logger.LogInformation("Загрузка данных из файла '{0}' прошла успешно", openFileDialog.FileName);
|
2022-12-13 01:49:12 +04:00
|
|
|
|
MessageBox.Show("Сохранение прошло успешно!", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
|
|
|
|
}
|
2022-12-23 00:48:39 +04:00
|
|
|
|
catch (Exception ex)
|
2022-12-13 01:49:12 +04:00
|
|
|
|
{
|
2022-12-23 00:48:39 +04:00
|
|
|
|
MessageBox.Show($"Не сохранилось: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
_logger.LogInformation("Не удалось загрузить файл '{0}'. Текст ошибки: {1}", openFileDialog.FileName, ex.Message);
|
2022-12-13 01:49:12 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void LoadToolStripMenuItem_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (openFileDialog.ShowDialog() == DialogResult.OK)
|
|
|
|
|
{
|
2022-12-23 00:48:39 +04:00
|
|
|
|
try
|
2022-12-13 01:49:12 +04:00
|
|
|
|
{
|
2022-12-23 00:48:39 +04:00
|
|
|
|
_mapsCollection.LoadData(openFileDialog.FileName);
|
|
|
|
|
MessageBox.Show("Загрузка данных прошла успешно", "Результат",
|
|
|
|
|
MessageBoxButtons.OK, MessageBoxIcon.Information);
|
2022-12-13 01:49:12 +04:00
|
|
|
|
ReloadMaps();
|
|
|
|
|
}
|
2022-12-23 00:48:39 +04:00
|
|
|
|
catch (Exception ex)
|
2022-12-13 01:49:12 +04:00
|
|
|
|
{
|
2022-12-23 00:48:39 +04:00
|
|
|
|
MessageBox.Show($"Не загрузилось: {ex.Message}", "Результат",
|
|
|
|
|
MessageBoxButtons.OK, MessageBoxIcon.Error);
|
2022-12-13 01:49:12 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-11-28 22:39:41 +04:00
|
|
|
|
}
|
|
|
|
|
}
|