2023-04-21 23:04:27 +04:00
|
|
|
|
using System;
|
2023-04-14 14:36:46 +04:00
|
|
|
|
using System.Collections.Generic;
|
2023-04-21 23:04:27 +04:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2023-04-14 14:36:46 +04:00
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
using System.Data;
|
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
|
|
|
|
|
namespace Sailboat
|
|
|
|
|
{
|
|
|
|
|
public partial class FormMapWithSetBoats : Form
|
|
|
|
|
{
|
2023-04-21 23:04:27 +04:00
|
|
|
|
private readonly Dictionary<string, AbstractMap> _mapsDict = new Dictionary<string, AbstractMap>()
|
|
|
|
|
{
|
2023-04-14 22:41:19 +04:00
|
|
|
|
{ "Простая карта", new SimpleMap() },
|
|
|
|
|
{ "Водная карта", new WaterMap() }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private readonly MapsCollection _mapsCollection;
|
2023-04-21 23:04:27 +04:00
|
|
|
|
private ILogger _logger;
|
2023-04-14 14:36:46 +04:00
|
|
|
|
|
2023-04-21 23:04:27 +04:00
|
|
|
|
public FormMapWithSetBoats(ILogger<FormMapWithSetBoats> logger)
|
2023-04-14 14:36:46 +04:00
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
2023-04-21 23:04:27 +04:00
|
|
|
|
_logger = logger;
|
2023-04-14 22:41:19 +04:00
|
|
|
|
_mapsCollection = new MapsCollection(pictureBox.Width, pictureBox.Height);
|
|
|
|
|
comboBoxMapSelector.Items.Clear();
|
|
|
|
|
foreach (var item in _mapsDict)
|
|
|
|
|
{
|
|
|
|
|
comboBoxMapSelector.Items.Add(item.Key);
|
|
|
|
|
}
|
2023-04-14 14:36:46 +04:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-14 22:41:19 +04:00
|
|
|
|
private void ReloadMaps()
|
2023-04-14 14:36:46 +04:00
|
|
|
|
{
|
2023-04-14 22:41:19 +04:00
|
|
|
|
int index = listBoxMaps.SelectedIndex;
|
|
|
|
|
listBoxMaps.Items.Clear();
|
|
|
|
|
for (int i = 0; i < _mapsCollection.Keys.Count; i++)
|
2023-04-14 14:36:46 +04:00
|
|
|
|
{
|
2023-04-14 22:41:19 +04:00
|
|
|
|
listBoxMaps.Items.Add(_mapsCollection.Keys[i]);
|
2023-04-14 14:36:46 +04:00
|
|
|
|
}
|
2023-04-14 22:41:19 +04:00
|
|
|
|
if (listBoxMaps.Items.Count > 0 && (index == -1 || index >= listBoxMaps.Items.Count))
|
2023-04-14 14:36:46 +04:00
|
|
|
|
{
|
2023-04-14 22:41:19 +04:00
|
|
|
|
listBoxMaps.SelectedIndex = 0;
|
2023-04-14 14:36:46 +04:00
|
|
|
|
}
|
2023-04-14 22:41:19 +04:00
|
|
|
|
else if (listBoxMaps.Items.Count > 0 && index > -1 && index < listBoxMaps.Items.Count)
|
2023-04-14 14:36:46 +04:00
|
|
|
|
{
|
2023-04-14 22:41:19 +04:00
|
|
|
|
listBoxMaps.SelectedIndex = index;
|
2023-04-14 14:36:46 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void btn_add_boat_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
2023-04-21 00:50:18 +04:00
|
|
|
|
var formBoatConfig = new FormBoatConfig();
|
|
|
|
|
formBoatConfig.AddEvent(InsertBoatCheck);
|
|
|
|
|
formBoatConfig.Show();
|
2023-04-14 14:36:46 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void btn_remove_boat_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(maskedTextBoxPosition.Text))
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
int pos = Convert.ToInt32(maskedTextBoxPosition.Text);
|
2023-04-21 23:04:27 +04:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var deletedBoat = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] - pos;
|
|
|
|
|
if (deletedBoat != null)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("Объект удален");
|
|
|
|
|
_logger.LogInformation("Из текущей карты удалён объект {@Tanker}", deletedBoat);
|
|
|
|
|
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Не удалось удалить объект по позиции {0}. Объект равен null", pos);
|
|
|
|
|
MessageBox.Show("Не удалось удалить объект");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (BoatNotFoundException ex)
|
2023-04-14 14:36:46 +04:00
|
|
|
|
{
|
2023-04-21 23:04:27 +04:00
|
|
|
|
_logger.LogWarning("Ошибка удаления: {0}", ex.Message);
|
|
|
|
|
MessageBox.Show($"Ошибка удаления: {ex.Message}");
|
2023-04-14 14:36:46 +04:00
|
|
|
|
}
|
2023-04-21 23:04:27 +04:00
|
|
|
|
catch (Exception ex)
|
2023-04-14 14:36:46 +04:00
|
|
|
|
{
|
2023-04-21 23:04:27 +04:00
|
|
|
|
_logger.LogWarning("Неизвестная ошибка удаления: {0}", ex.Message);
|
|
|
|
|
MessageBox.Show($"Неизвестная ошибка: {ex.Message}");
|
2023-04-14 14:36:46 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void btn_show_storage_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
2023-04-14 22:41:19 +04:00
|
|
|
|
if (listBoxMaps.SelectedIndex == -1)
|
2023-04-14 14:36:46 +04:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-04-14 22:41:19 +04:00
|
|
|
|
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
|
2023-04-14 14:36:46 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void btn_show_map_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
2023-04-14 22:41:19 +04:00
|
|
|
|
if (listBoxMaps.SelectedIndex == -1)
|
2023-04-14 14:36:46 +04:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-04-14 22:41:19 +04:00
|
|
|
|
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowOnMap();
|
2023-04-14 14:36:46 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void btn_move_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
2023-04-14 22:41:19 +04:00
|
|
|
|
if (listBoxMaps.SelectedIndex == -1)
|
2023-04-14 14:36:46 +04:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string name = ((Button)sender)?.Name ?? string.Empty;
|
|
|
|
|
Direction dir = Direction.None;
|
|
|
|
|
switch (name)
|
|
|
|
|
{
|
|
|
|
|
case "btn_up":
|
|
|
|
|
dir = Direction.Up;
|
|
|
|
|
break;
|
|
|
|
|
case "btn_down":
|
|
|
|
|
dir = Direction.Down;
|
|
|
|
|
break;
|
|
|
|
|
case "btn_left":
|
|
|
|
|
dir = Direction.Left;
|
|
|
|
|
break;
|
|
|
|
|
case "btn_right":
|
|
|
|
|
dir = Direction.Right;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2023-04-14 22:41:19 +04:00
|
|
|
|
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].MoveObject(dir);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void btn_add_map_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (comboBoxMapSelector.SelectedIndex == -1 || string.IsNullOrEmpty(textBoxNewMapName.Text))
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("Не все данные заполнены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
2023-04-21 23:04:27 +04:00
|
|
|
|
_logger.LogWarning("При добавлении карты {0}", comboBoxMapSelector.SelectedIndex == -1 ? "Не была выбрана карта" : "Не была названа карта");
|
2023-04-14 22:41:19 +04:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (!_mapsDict.ContainsKey(comboBoxMapSelector.Text))
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("Нет такой карты", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
2023-04-21 23:04:27 +04:00
|
|
|
|
_logger.LogWarning("Отсутствует карта с типом {0}", comboBoxMapSelector.Text);
|
2023-04-14 22:41:19 +04:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
_mapsCollection.AddMap(textBoxNewMapName.Text, _mapsDict[comboBoxMapSelector.Text]);
|
|
|
|
|
ReloadMaps();
|
2023-04-21 23:04:27 +04:00
|
|
|
|
_logger.LogInformation($"Добавлена карта: {textBoxNewMapName.Text}");
|
2023-04-14 22:41:19 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void listBoxMaps_SelectedIndexChanged(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
|
2023-04-21 23:04:27 +04:00
|
|
|
|
_logger.LogInformation("Осуществлён переход на карту под названием {0}", listBoxMaps.SelectedItem?.ToString() ?? string.Empty);
|
2023-04-14 22:41:19 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void btn_delete_map_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (listBoxMaps.SelectedIndex == -1)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (MessageBox.Show($"Удалить карту {listBoxMaps.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
|
|
|
|
|
{
|
2023-04-21 23:04:27 +04:00
|
|
|
|
_logger.LogInformation("Удалена карта {0}", listBoxMaps.SelectedItem?.ToString() ?? string.Empty);
|
2023-04-14 22:41:19 +04:00
|
|
|
|
_mapsCollection.DelMap(listBoxMaps.SelectedItem?.ToString() ?? string.Empty);
|
|
|
|
|
ReloadMaps();
|
|
|
|
|
}
|
2023-04-14 14:36:46 +04:00
|
|
|
|
}
|
2023-04-21 00:50:18 +04:00
|
|
|
|
private void InsertBoatCheck(DrawingBoat _boat)
|
|
|
|
|
{
|
2023-04-21 23:04:27 +04:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (listBoxMaps.SelectedIndex == -1)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
DrawingObjectBoat boat = new(_boat);
|
|
|
|
|
if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + boat >= 0)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("Объект добавлен");
|
|
|
|
|
_logger.LogInformation("Добавлен объект {@Tanker}", _boat);
|
|
|
|
|
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("Не удалось добавить объект");
|
|
|
|
|
_logger.LogWarning("Не удалось добавить объект");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (StorageOverflowException ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Ошибка, переполнение хранилища :{0}", ex.Message);
|
|
|
|
|
MessageBox.Show($"Ошибка хранилище переполнено: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
2023-04-21 00:50:18 +04:00
|
|
|
|
}
|
2023-04-21 23:04:27 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SaveToolStripMenuItem_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (saveFileDialog.ShowDialog() == DialogResult.OK)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_mapsCollection.SaveData(saveFileDialog.FileName);
|
|
|
|
|
_logger.LogInformation("Сохранение прошло успешно. Расположение файла: {0}", saveFileDialog.FileName);
|
|
|
|
|
MessageBox.Show("Сохранение прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show($"Не сохранилось:{ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
_logger.LogWarning("Не удалось сохранить файл '{0}'. Текст ошибки: {1}", saveFileDialog.FileName, ex.Message);
|
|
|
|
|
}
|
2023-04-21 00:50:18 +04:00
|
|
|
|
}
|
2023-04-21 23:04:27 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void LoadToolStripMenuItem_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (openFileDialog.ShowDialog() == DialogResult.OK)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_mapsCollection.LoadData(openFileDialog.FileName);
|
|
|
|
|
_logger.LogInformation("Загрузка данных из файла '{0}' прошла успешно", openFileDialog.FileName);
|
|
|
|
|
ReloadMaps();
|
|
|
|
|
MessageBox.Show("Загрузка прошла успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Не удалось загрузить файл '{0}'. Текст ошибки: {1}", openFileDialog.FileName, ex.Message);
|
|
|
|
|
MessageBox.Show($"Не загрузилось:{ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
}
|
2023-04-21 00:50:18 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-04-22 01:09:06 +04:00
|
|
|
|
|
|
|
|
|
private void buttonSortByType_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (listBoxMaps.SelectedIndex == -1)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].Sort(new BoatCompareByType());
|
|
|
|
|
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void buttonSortByColor_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (listBoxMaps.SelectedIndex == -1)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].Sort(new BoatCompareByColor());
|
|
|
|
|
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
|
|
|
|
|
|
|
|
|
|
}
|
2023-04-14 14:36:46 +04:00
|
|
|
|
}
|
|
|
|
|
}
|