280 lines
10 KiB
C#
280 lines
10 KiB
C#
using WarmlyShip.Generics;
|
||
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 WarmlyShip.DrawningObjects;
|
||
using WarmlyShip.MovementStrategy;
|
||
using Microsoft.Extensions.Logging;
|
||
using WarmlyShip.Exceptions;
|
||
using System.Xml.Linq;
|
||
using Serilog;
|
||
|
||
namespace WarmlyShip
|
||
{
|
||
/// <summary>
|
||
/// Форма для работы с набором объектов класса DrawningShip
|
||
/// </summary>
|
||
public partial class FormShipCollection : Form
|
||
{
|
||
/// <summary>
|
||
/// Набор объектов
|
||
/// </summary>
|
||
private readonly ShipsGenericStorage _storage;
|
||
|
||
/// <summary>
|
||
/// Конструктор
|
||
/// </summary>
|
||
public FormShipCollection()
|
||
{
|
||
InitializeComponent();
|
||
_storage = new ShipsGenericStorage(pictureBoxCollection.Width, pictureBoxCollection.Height);
|
||
}
|
||
|
||
/// <summary>
|
||
/// Заполнение ListBoxObjects
|
||
/// </summary>
|
||
private void ReloadObjects()
|
||
{
|
||
int index = ListBoxObjects.SelectedIndex;
|
||
|
||
ListBoxObjects.Items.Clear();
|
||
for (int i = 0; i < _storage.Keys.Count; i++)
|
||
{
|
||
ListBoxObjects.Items.Add(_storage.Keys[i].Name);
|
||
}
|
||
|
||
if (ListBoxObjects.Items.Count > 0 && (index == -1 || index >= ListBoxObjects.Items.Count))
|
||
{
|
||
ListBoxObjects.SelectedIndex = 0;
|
||
}
|
||
|
||
else if (ListBoxObjects.Items.Count > 0 && index > -1 && index < ListBoxObjects.Items.Count)
|
||
{
|
||
ListBoxObjects.SelectedIndex = index;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// Добавление набора в коллекцию
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void ButtonAddObject_Click(object sender, EventArgs e)
|
||
{
|
||
if (string.IsNullOrEmpty(textBoxStorageName.Text))
|
||
{
|
||
MessageBox.Show("Не все данные заполнены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
return;
|
||
}
|
||
_storage.AddSet(textBoxStorageName.Text);
|
||
ReloadObjects();
|
||
Log.Information($"Добавлен набор: {textBoxStorageName.Text}");
|
||
}
|
||
|
||
/// <summary>
|
||
/// Выбор набора
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void ListBoxObjects_SelectedIndexChanged(object sender, EventArgs e)
|
||
{
|
||
pictureBoxCollection.Image = _storage[ListBoxObjects.SelectedItem?.ToString() ?? string.Empty]?.ShowShips();
|
||
}
|
||
|
||
/// <summary>
|
||
/// Удаление набора
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void ButtonDelObject_Click(object sender, EventArgs e)
|
||
{
|
||
if (ListBoxObjects.SelectedIndex == -1)
|
||
{
|
||
return;
|
||
}
|
||
if (MessageBox.Show($"Удалить объект {ListBoxObjects.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo,
|
||
MessageBoxIcon.Question) == DialogResult.Yes)
|
||
{
|
||
string name = (ListBoxObjects.SelectedItem.ToString() ?? string.Empty);
|
||
_storage.DelSet(name);
|
||
ReloadObjects();
|
||
Log.Information($"Удален набор: {name}");
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// Добавление объекта в набор
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void ButtonAddShip_Click(object sender, EventArgs e)
|
||
{
|
||
if (ListBoxObjects.SelectedIndex == -1)
|
||
{
|
||
return;
|
||
}
|
||
var obj = _storage[ListBoxObjects.SelectedItem.ToString() ?? string.Empty];
|
||
if (obj == null)
|
||
{
|
||
return;
|
||
}
|
||
|
||
FormShipConfig form = new();
|
||
form.Show();
|
||
Action<DrawningShip>? shipDelegate = new((ship) =>
|
||
{
|
||
try
|
||
{
|
||
bool isAdditionSuccessful = obj + ship;
|
||
MessageBox.Show("Объект добавлен");
|
||
ship.ChangePictureBoxSize(pictureBoxCollection.Width, pictureBoxCollection.Height);
|
||
pictureBoxCollection.Image = obj.ShowShips();
|
||
Log.Information($"Добавлен объект в коллекцию {ListBoxObjects.SelectedItem.ToString() ?? string.Empty}");
|
||
}
|
||
catch (ArgumentException ex)
|
||
{
|
||
Log.Warning($"Добавляемый объект уже существует в коллекции {ListBoxObjects.SelectedItem.ToString() ?? string.Empty}");
|
||
MessageBox.Show("Добавляемый объект уже сущесвует в коллекции");
|
||
}
|
||
});
|
||
form.AddEvent(shipDelegate);
|
||
}
|
||
|
||
/// <summary>
|
||
/// Удаление объекта из набора
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void ButtonDeleteShip_Click(object sender, EventArgs e)
|
||
{
|
||
if (ListBoxObjects.SelectedIndex == -1)
|
||
{
|
||
return;
|
||
}
|
||
var obj = _storage[ListBoxObjects.SelectedItem.ToString() ??
|
||
string.Empty];
|
||
if (obj == null)
|
||
{
|
||
return;
|
||
}
|
||
if (MessageBox.Show("Удалить объект?", "Удаление",
|
||
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
|
||
{
|
||
return;
|
||
}
|
||
try
|
||
{
|
||
int pos = Convert.ToInt32(maskedTextBoxNumber.Text);
|
||
var isAdditionSuccessful = obj - pos;
|
||
MessageBox.Show("Объект удален");
|
||
Log.Information($"Удален объект из коллекции {ListBoxObjects.SelectedItem.ToString() ?? string.Empty} по номеру {pos}");
|
||
pictureBoxCollection.Image = obj.ShowShips();
|
||
}
|
||
catch (ShipNotFoundException ex)
|
||
{
|
||
Log.Warning($"Не получилось удалить объект из коллекции {ListBoxObjects.SelectedItem.ToString() ?? string.Empty}");
|
||
MessageBox.Show(ex.Message);
|
||
}
|
||
catch (FormatException)
|
||
{
|
||
Log.Warning($"Было введено не число");
|
||
MessageBox.Show("Введите число");
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// Обновление рисунка по набору
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void ButtonRefreshCollection_Click(object sender, EventArgs e)
|
||
{
|
||
if (ListBoxObjects.SelectedIndex == -1)
|
||
{
|
||
return;
|
||
}
|
||
var obj = _storage[ListBoxObjects.SelectedItem.ToString() ??
|
||
string.Empty];
|
||
if (obj == null)
|
||
{
|
||
return;
|
||
}
|
||
pictureBoxCollection.Image = obj.ShowShips();
|
||
}
|
||
|
||
/// <summary>
|
||
/// Обработка нажатия "Сохранение"
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void SaveToolStripMenuItem_Click(object sender, EventArgs e)
|
||
{
|
||
if (saveFileDialog.ShowDialog() == DialogResult.OK)
|
||
{
|
||
try
|
||
{
|
||
_storage.SaveData(saveFileDialog.FileName);
|
||
MessageBox.Show("Сохранение прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||
Log.Information($"Файл {saveFileDialog.FileName} успешно сохранен");
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Log.Warning("Не удалось сохранить");
|
||
MessageBox.Show($"Не сохранилось: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// Обработка нажатия "Загрузка"
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void LoadToolStripMenuItem_Click(object sender, EventArgs e)
|
||
{
|
||
if (openFileDialog.ShowDialog() == DialogResult.OK)
|
||
{
|
||
try
|
||
{
|
||
_storage.LoadData(openFileDialog.FileName);
|
||
MessageBox.Show("Загрузка прошла успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||
Log.Information($"Файл {openFileDialog.FileName} успешно загружен");
|
||
foreach (var collection in _storage.Keys)
|
||
{
|
||
ListBoxObjects.Items.Add(collection);
|
||
}
|
||
ReloadObjects();
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Log.Warning("Не удалось загрузить");
|
||
MessageBox.Show($"Не загрузилось: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
}
|
||
}
|
||
}
|
||
|
||
private void ButtonSortByType_Click(object sender, EventArgs e) => CompareShips(new ShipCompareByType());
|
||
private void ButtonSortByColor_Click(object sender, EventArgs e) => CompareShips(new ShipCompareByColor());
|
||
private void CompareShips(IComparer<DrawningShip?> comparer)
|
||
{
|
||
if (ListBoxObjects.SelectedIndex == -1)
|
||
{
|
||
return;
|
||
}
|
||
var obj = _storage[ListBoxObjects.SelectedItem.ToString() ?? string.Empty];
|
||
if (obj == null)
|
||
{
|
||
return;
|
||
}
|
||
obj.Sort(comparer);
|
||
pictureBoxCollection.Image = obj.ShowShips();
|
||
}
|
||
}
|
||
}
|