2022-11-08 02:36:45 +04:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using System;
|
2022-10-02 16:55:05 +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 static System.Windows.Forms.DataFormats;
|
|
|
|
|
|
|
|
|
|
namespace Artilleries
|
|
|
|
|
{
|
|
|
|
|
public partial class FormMapWithSetArtilleries : Form
|
|
|
|
|
{
|
2022-10-10 00:00:51 +04:00
|
|
|
|
private readonly Dictionary<string, AbstractMap> _mapsDict = new()
|
|
|
|
|
{
|
|
|
|
|
{ "Простая карта", new SimpleMap() },
|
|
|
|
|
{ "Лесная карта", new ForestMap() }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private readonly MapsCollection _mapsCollection;
|
2022-10-02 16:55:05 +04:00
|
|
|
|
|
2022-11-08 02:36:45 +04:00
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
|
|
|
|
|
public FormMapWithSetArtilleries(ILogger<FormMapWithSetArtilleries> logger)
|
2022-10-02 16:55:05 +04:00
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
2022-10-10 00:00:51 +04:00
|
|
|
|
_mapsCollection = new MapsCollection(pictureBoxArtilleries.Width, pictureBoxArtilleries.Height);
|
2022-11-08 02:36:45 +04:00
|
|
|
|
_logger = logger;
|
2022-10-10 00:00:51 +04:00
|
|
|
|
comboBoxSelectorMap.Items.Clear();
|
|
|
|
|
foreach(var element in _mapsDict)
|
|
|
|
|
{
|
|
|
|
|
comboBoxSelectorMap.Items.Add(element.Key);
|
|
|
|
|
}
|
2022-10-02 16:55:05 +04:00
|
|
|
|
}
|
|
|
|
|
|
2022-10-10 00:00:51 +04:00
|
|
|
|
private void ReloadMaps()
|
2022-10-02 16:55:05 +04:00
|
|
|
|
{
|
2022-10-10 00:00:51 +04:00
|
|
|
|
int index = listBoxMaps.SelectedIndex;
|
|
|
|
|
|
|
|
|
|
listBoxMaps.Items.Clear();
|
|
|
|
|
for (int i = 0; i < _mapsCollection.Keys.Count; i++)
|
2022-10-02 16:55:05 +04:00
|
|
|
|
{
|
2022-10-10 00:00:51 +04:00
|
|
|
|
listBoxMaps.Items.Add(_mapsCollection.Keys[i]);
|
2022-10-02 16:55:05 +04:00
|
|
|
|
}
|
2022-10-10 00:00:51 +04:00
|
|
|
|
if (listBoxMaps.Items.Count > 0 && (index == -1 || index >= listBoxMaps.Items.Count))
|
2022-10-02 16:55:05 +04:00
|
|
|
|
{
|
2022-10-10 00:00:51 +04:00
|
|
|
|
listBoxMaps.SelectedIndex = 0;
|
2022-10-02 16:55:05 +04:00
|
|
|
|
}
|
2022-10-10 00:00:51 +04:00
|
|
|
|
else if (listBoxMaps.Items.Count > 0 && index > -1 && index < listBoxMaps.Items.Count)
|
2022-10-02 16:55:05 +04:00
|
|
|
|
{
|
2022-10-10 00:00:51 +04:00
|
|
|
|
listBoxMaps.SelectedIndex = index;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void buttonAddMap_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (comboBoxSelectorMap.SelectedIndex == -1 || string.IsNullOrEmpty(textBoxNewMapName.Text))
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("Не все данные заполнены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
2022-11-08 02:36:45 +04:00
|
|
|
|
_logger.LogWarning("При добавлении карты {0}", comboBoxSelectorMap.SelectedIndex == -1 ? "не выбрали карту" : "не указали её название");
|
2022-10-10 00:00:51 +04:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (!_mapsDict.ContainsKey(comboBoxSelectorMap.Text))
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("Нет такой карты", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
2022-11-08 02:36:45 +04:00
|
|
|
|
_logger.LogWarning("Нет карты с названием: {0}", textBoxNewMapName.Text);
|
2022-10-10 00:00:51 +04:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
_mapsCollection.AddMap(textBoxNewMapName.Text, _mapsDict[comboBoxSelectorMap.Text]);
|
2022-11-08 02:36:45 +04:00
|
|
|
|
_logger.LogInformation("Добавлена карта \"{1}\" с названием \"{0}\"", textBoxNewMapName.Text, comboBoxSelectorMap.Text);
|
2022-10-10 00:00:51 +04:00
|
|
|
|
ReloadMaps();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void listBoxMaps_SelectedIndexChanged(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
pictureBoxArtilleries.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? String.Empty].ShowSet();
|
2022-11-08 02:36:45 +04:00
|
|
|
|
_logger.LogInformation("Переход на карту \"{0}\"", listBoxMaps.SelectedItem?.ToString() ?? String.Empty);
|
2022-10-10 00:00:51 +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);
|
2022-11-08 02:36:45 +04:00
|
|
|
|
_logger.LogInformation("Удалена карта \"{0}\"", listBoxMaps.SelectedItem?.ToString() ?? string.Empty);
|
2022-10-10 00:00:51 +04:00
|
|
|
|
ReloadMaps();
|
2022-10-02 16:55:05 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void buttonAddArtillery_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
2022-10-24 21:49:26 +04:00
|
|
|
|
var formArtilleryConfig = new FormArtilleryConfig();
|
2022-10-25 01:57:21 +04:00
|
|
|
|
formArtilleryConfig.AddEvent((artillery) => {
|
|
|
|
|
if (listBoxMaps.SelectedIndex == -1)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-08 00:55:42 +04:00
|
|
|
|
try
|
2022-10-02 16:55:05 +04:00
|
|
|
|
{
|
2022-11-08 02:36:45 +04:00
|
|
|
|
var result = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? String.Empty] + new DrawingObjectArtillery(artillery);
|
|
|
|
|
if (result != -1)
|
2022-11-08 00:55:42 +04:00
|
|
|
|
{
|
2022-11-08 02:36:45 +04:00
|
|
|
|
_logger.LogInformation("Добавлен объект на позицию {0}", result);
|
2022-11-08 00:55:42 +04:00
|
|
|
|
MessageBox.Show("Объект добавлен");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-11-08 02:36:45 +04:00
|
|
|
|
_logger.LogWarning("Не удалось добавить объект");
|
2022-11-08 00:55:42 +04:00
|
|
|
|
MessageBox.Show("Не удалось добавить объект");
|
|
|
|
|
}
|
|
|
|
|
pictureBoxArtilleries.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? String.Empty].ShowSet();
|
|
|
|
|
}
|
|
|
|
|
catch (StorageOverflowException ex)
|
|
|
|
|
{
|
2022-11-08 02:36:45 +04:00
|
|
|
|
_logger.LogWarning("Ошибка переполнения хранилища: {0}", ex.Message);
|
2022-11-08 00:55:42 +04:00
|
|
|
|
MessageBox.Show($"Ошибка переполнения хранилища: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
2022-10-02 16:55:05 +04:00
|
|
|
|
{
|
2022-11-08 02:36:45 +04:00
|
|
|
|
_logger.LogWarning("Неизвестная ошибка: {0}", ex.Message);
|
2022-11-08 00:55:42 +04:00
|
|
|
|
MessageBox.Show($"Неизвестная ошибка: {ex.Message}");
|
2022-10-02 16:55:05 +04:00
|
|
|
|
}
|
2022-10-24 21:49:26 +04:00
|
|
|
|
});
|
|
|
|
|
formArtilleryConfig.Show();
|
2022-10-02 16:55:05 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void buttonRemoveArtillery_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
2022-10-10 00:00:51 +04:00
|
|
|
|
if (listBoxMaps.SelectedIndex == -1)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-10-02 16:55:05 +04:00
|
|
|
|
if (string.IsNullOrEmpty(maskedTextBoxPosition.Text))
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-10-02 22:19:25 +04:00
|
|
|
|
if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
|
2022-10-02 16:55:05 +04:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
int pos = Convert.ToInt32(maskedTextBoxPosition.Text);
|
2022-11-08 00:55:42 +04:00
|
|
|
|
try
|
2022-10-02 16:55:05 +04:00
|
|
|
|
{
|
2022-11-08 00:55:42 +04:00
|
|
|
|
if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] - pos != null)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("Объект удален");
|
2022-11-08 02:36:45 +04:00
|
|
|
|
_logger.LogInformation("Удалён объект на позиции {0}", pos);
|
2022-11-08 00:55:42 +04:00
|
|
|
|
pictureBoxArtilleries.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-11-08 02:36:45 +04:00
|
|
|
|
_logger.LogWarning("Не удалось удалить объект по позиции {0}. Объект равен null", pos);
|
2022-11-08 00:55:42 +04:00
|
|
|
|
MessageBox.Show("Не удалось удалить объект");
|
|
|
|
|
}
|
2022-10-02 16:55:05 +04:00
|
|
|
|
}
|
2022-11-08 00:55:42 +04:00
|
|
|
|
catch (ArtilleryNotFoundException ex)
|
2022-10-02 16:55:05 +04:00
|
|
|
|
{
|
2022-11-08 02:36:45 +04:00
|
|
|
|
_logger.LogWarning("Ошибка удаления: {0}", ex.Message);
|
2022-11-08 00:55:42 +04:00
|
|
|
|
MessageBox.Show($"Ошибка удаления: {ex.Message}");
|
2022-10-02 16:55:05 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void buttonShowStorage_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
2022-10-10 00:00:51 +04:00
|
|
|
|
if (listBoxMaps.SelectedIndex == -1)
|
2022-10-02 16:55:05 +04:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-10-10 00:00:51 +04:00
|
|
|
|
pictureBoxArtilleries.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
|
2022-10-02 16:55:05 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void buttonShowOnMap_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
2022-10-10 00:00:51 +04:00
|
|
|
|
if (listBoxMaps.SelectedIndex == -1)
|
2022-10-02 16:55:05 +04:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-10-10 00:00:51 +04:00
|
|
|
|
pictureBoxArtilleries.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowOnMap();
|
2022-10-02 16:55:05 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void buttonMove_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
2022-10-10 00:00:51 +04:00
|
|
|
|
if (listBoxMaps.SelectedIndex == -1)
|
2022-10-02 16:55:05 +04:00
|
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
}
|
2022-10-10 00:00:51 +04:00
|
|
|
|
pictureBoxArtilleries.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].MoveObject(dir);
|
2022-10-02 16:55:05 +04:00
|
|
|
|
}
|
2022-11-04 16:50:18 +04:00
|
|
|
|
|
|
|
|
|
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (saveFileDialog.ShowDialog() == DialogResult.OK)
|
|
|
|
|
{
|
2022-11-08 00:55:42 +04:00
|
|
|
|
try
|
2022-11-04 16:50:18 +04:00
|
|
|
|
{
|
2022-11-08 00:55:42 +04:00
|
|
|
|
_mapsCollection.SaveData(saveFileDialog.FileName);
|
2022-11-08 02:36:45 +04:00
|
|
|
|
_logger.LogInformation("Сохранение в файл \"{0}\"", saveFileDialog.FileName);
|
2022-11-04 16:50:18 +04:00
|
|
|
|
MessageBox.Show("Сохранение прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
|
|
|
|
}
|
2022-11-08 00:55:42 +04:00
|
|
|
|
catch (Exception ex)
|
2022-11-04 16:50:18 +04:00
|
|
|
|
{
|
2022-11-08 02:36:45 +04:00
|
|
|
|
_logger.LogWarning("Не сохранить файл \"{0}\": {1}", saveFileDialog.FileName, ex.Message);
|
2022-11-08 00:55:42 +04:00
|
|
|
|
MessageBox.Show($"Не сохранилось: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
2022-11-04 16:50:18 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void loadToolStripMenuItem_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (loadFileDialog.ShowDialog() == DialogResult.OK)
|
|
|
|
|
{
|
2022-11-08 00:55:42 +04:00
|
|
|
|
try
|
2022-11-04 16:50:18 +04:00
|
|
|
|
{
|
2022-11-08 00:55:42 +04:00
|
|
|
|
_mapsCollection.LoadData(loadFileDialog.FileName);
|
2022-11-08 02:36:45 +04:00
|
|
|
|
_logger.LogInformation("Загрузка из файла \"{0}\"", loadFileDialog.FileName);
|
2022-11-04 16:50:18 +04:00
|
|
|
|
MessageBox.Show("Загрузка прошла успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
2022-11-08 00:55:42 +04:00
|
|
|
|
ReloadMaps();
|
|
|
|
|
pictureBoxArtilleries.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
|
2022-11-04 16:50:18 +04:00
|
|
|
|
}
|
2022-11-08 00:55:42 +04:00
|
|
|
|
catch (Exception ex)
|
2022-11-04 16:50:18 +04:00
|
|
|
|
{
|
2022-11-08 02:36:45 +04:00
|
|
|
|
_logger.LogWarning("Не удалось загрузить файл \"{0}\": {1}", loadFileDialog.FileName, ex.Message);
|
2022-11-08 00:55:42 +04:00
|
|
|
|
MessageBox.Show($"Не загрузилось: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
2022-11-04 16:50:18 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-10-02 16:55:05 +04:00
|
|
|
|
}
|
|
|
|
|
}
|