234 lines
9.0 KiB
C#
234 lines
9.0 KiB
C#
using AirBomber.DrawningObjects;
|
||
using AirBomber.Generics;
|
||
using AirBomber.MovementStrategy;
|
||
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 System.Xml.Linq;
|
||
using AirBomber.Exceptions;
|
||
using Microsoft.Extensions.Logging;
|
||
|
||
namespace AirBomber
|
||
{
|
||
public partial class FormBomberCollection : Form
|
||
{
|
||
private readonly BomberGenericStorage _bomber;
|
||
|
||
private readonly ILogger _logger;
|
||
|
||
public FormBomberCollection(ILogger<FormBomberCollection> logger)
|
||
{
|
||
InitializeComponent();
|
||
_bomber = new BomberGenericStorage(PicBoxBomberCollection.Width, PicBoxBomberCollection.Height);
|
||
_logger = logger;
|
||
}
|
||
|
||
private void ReloadObjects()
|
||
{
|
||
int index = listBoxStorages.SelectedIndex;
|
||
listBoxStorages.Items.Clear();
|
||
for (int i = 0; i < _bomber.Keys.Count; i++)
|
||
{
|
||
listBoxStorages.Items.Add(_bomber.Keys[i].Name);
|
||
}
|
||
if (listBoxStorages.Items.Count > 0 && (index == -1 || index
|
||
>= listBoxStorages.Items.Count))
|
||
{
|
||
listBoxStorages.SelectedIndex = 0;
|
||
}
|
||
else if (listBoxStorages.Items.Count > 0 && index > -1 &&
|
||
index < listBoxStorages.Items.Count)
|
||
{
|
||
listBoxStorages.SelectedIndex = index;
|
||
}
|
||
}
|
||
|
||
private void ButtonRefreshCollection_Click(object sender, EventArgs e)
|
||
{
|
||
if (listBoxStorages.SelectedIndex == -1)
|
||
{
|
||
return;
|
||
}
|
||
var obj = _bomber[listBoxStorages.SelectedItem.ToString() ??
|
||
string.Empty];
|
||
if (obj == null)
|
||
{
|
||
return;
|
||
}
|
||
PicBoxBomberCollection.Image = obj.ShowBomber();
|
||
}
|
||
private void AddBomber(DrawningBomber bomber)
|
||
{
|
||
|
||
var obj = _bomber[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
|
||
if (obj == null)
|
||
{
|
||
_logger.LogWarning("Добавление пустого объекта");
|
||
return;
|
||
}
|
||
try
|
||
{
|
||
_ = obj + bomber;
|
||
|
||
MessageBox.Show("Объект добавлен");
|
||
PicBoxBomberCollection.Image = obj.ShowBomber();
|
||
_logger.LogInformation($"Добавлен объект в набор {listBoxStorages.SelectedItem.ToString()}");
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
MessageBox.Show("Не удалось добавить объект");
|
||
_logger.LogWarning($"{ex.Message} в наборе {listBoxStorages.SelectedItem.ToString()}");
|
||
}
|
||
}
|
||
private void ButtonAddBomber_Click(object sender, EventArgs e)
|
||
{
|
||
if (listBoxStorages.SelectedIndex == -1)
|
||
{
|
||
return;
|
||
}
|
||
FormBomberConfig form = new FormBomberConfig();
|
||
form.Show();
|
||
form.AddEvent(AddBomber);
|
||
}
|
||
|
||
private void ButtonRemoveBomber_Click(object sender, EventArgs e)
|
||
{
|
||
if (listBoxStorages.SelectedIndex == -1)
|
||
{
|
||
_logger.LogWarning("Удаление объекта из несуществующего набора");
|
||
return;
|
||
}
|
||
var obj = _bomber[listBoxStorages.SelectedItem.ToString() ??
|
||
string.Empty];
|
||
if (obj == null)
|
||
{
|
||
return;
|
||
}
|
||
if (MessageBox.Show("Удалить объект?", "Удаление",
|
||
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
|
||
{
|
||
return;
|
||
}
|
||
int pos = Convert.ToInt32(MessageBoxBomber.Text);
|
||
try
|
||
{
|
||
if (obj - pos != null)
|
||
{
|
||
MessageBox.Show("Объект удален");
|
||
PicBoxBomberCollection.Image = obj.ShowBomber();
|
||
_logger.LogInformation($"Удален объект из набора {listBoxStorages.SelectedItem.ToString()}");
|
||
}
|
||
else
|
||
{
|
||
MessageBox.Show("Не удалось удалить объект");
|
||
_logger.LogWarning($"Не удалось удалить объект из набора {listBoxStorages.SelectedItem.ToString()}");
|
||
}
|
||
}
|
||
catch (BomberNotFoundException ex)
|
||
{
|
||
MessageBox.Show(ex.Message);
|
||
_logger.LogWarning($"{ex.Message} из набора {listBoxStorages.SelectedItem.ToString()}");
|
||
}
|
||
}
|
||
|
||
private void AddKit_Click(object sender, EventArgs e)
|
||
{
|
||
if (string.IsNullOrEmpty(KitTextbox.Text))
|
||
{
|
||
MessageBox.Show("Не все данные заполнены", "Ошибка",
|
||
MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
_logger.LogWarning("Пустое название набора");
|
||
return;
|
||
}
|
||
_bomber.AddSet(KitTextbox.Text);
|
||
ReloadObjects();
|
||
_logger.LogInformation($"Добавлен набор: {KitTextbox.Text}");
|
||
}
|
||
|
||
private void RemoveKit_Click(object sender, EventArgs e)
|
||
{
|
||
if (listBoxStorages.SelectedIndex == -1)
|
||
{
|
||
_logger.LogWarning("Удаление невыбранного набора");
|
||
return;
|
||
}
|
||
string name = listBoxStorages.SelectedItem.ToString() ?? string.Empty;
|
||
if (MessageBox.Show($"Удалить объект {listBoxStorages.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
|
||
{
|
||
_bomber.DelSet(listBoxStorages.SelectedItem.ToString()
|
||
?? string.Empty);
|
||
ReloadObjects();
|
||
_logger.LogInformation($"Удален набор: {name}");
|
||
}
|
||
}
|
||
private void ListBoxObjects_SelectedIndexChanged(object sender, EventArgs e)
|
||
{
|
||
PicBoxBomberCollection.Image =
|
||
_bomber[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowBomber();
|
||
}
|
||
|
||
private void SaveToolStripMenuItem_Click(object sender, EventArgs e)
|
||
{
|
||
if (saveFileDialog.ShowDialog() == DialogResult.OK)
|
||
{
|
||
try
|
||
{
|
||
_bomber.SaveData(saveFileDialog.FileName);
|
||
MessageBox.Show("Сохранение прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||
_logger.LogInformation($"Сохранение наборов в файл {saveFileDialog.FileName}");
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
MessageBox.Show($"Не сохранилось: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
_logger.LogWarning($"Не удалось сохранить наборы с ошибкой: {ex.Message}");
|
||
}
|
||
}
|
||
}
|
||
|
||
private void LoadToolStripMenuItem_Click(object sender, EventArgs e)
|
||
{
|
||
if (openFileDialog.ShowDialog() == DialogResult.OK)
|
||
{
|
||
try
|
||
{
|
||
_bomber.LoadData(openFileDialog.FileName);
|
||
ReloadObjects();
|
||
MessageBox.Show("Загрузка прошла успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||
_logger.LogInformation($"Загрузились наборы из файла {openFileDialog.FileName}");
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
MessageBox.Show($"Не загрузилось: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
_logger.LogWarning($"Не удалось сохранить наборы с ошибкой: {ex.Message}");
|
||
}
|
||
}
|
||
}
|
||
|
||
private void ButtonSortByType_Click(object sender, EventArgs e) => CompareBombers(new BomberCompareByType());
|
||
|
||
private void ButtonSortByColor_Click(object sender, EventArgs e) => CompareBombers(new BomberCompareByColor());
|
||
|
||
private void CompareBombers(IComparer<DrawningBomber?> comparer)
|
||
{
|
||
if (listBoxStorages.SelectedIndex == -1)
|
||
{
|
||
return;
|
||
}
|
||
var obj = _bomber[listBoxStorages.SelectedItem.ToString() ??
|
||
string.Empty];
|
||
if (obj == null)
|
||
{
|
||
return;
|
||
}
|
||
obj.Sort(comparer);
|
||
PicBoxBomberCollection.Image = obj.ShowBomber();
|
||
}
|
||
}
|
||
}
|