229 lines
8.3 KiB
C#
229 lines
8.3 KiB
C#
using ProjectAirFighter.DrawningObjects;
|
||
using ProjectAirFighter.Generics;
|
||
using Microsoft.Extensions.Logging;
|
||
using ProjectAirFighter.Exceptions;
|
||
using ProjectAirFighter.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 Serilog;
|
||
using Microsoft.VisualBasic.Logging;
|
||
using Log = Serilog.Log;
|
||
|
||
namespace ProjectAirFighter
|
||
{
|
||
public partial class FormAirplaneCollection : Form
|
||
{
|
||
private readonly AirplanesGenericStorage _storage;
|
||
|
||
public FormAirplaneCollection()
|
||
{
|
||
InitializeComponent();
|
||
_storage = new AirplanesGenericStorage(pictureBoxCollection.Width,
|
||
pictureBoxCollection.Height);
|
||
}
|
||
|
||
private void SaveToolStripMenuItem_Click(object sender, EventArgs e)
|
||
{
|
||
if (saveFileDialog.ShowDialog() == DialogResult.OK)
|
||
{
|
||
try
|
||
{
|
||
_storage.SaveData(saveFileDialog.FileName);
|
||
MessageBox.Show("Сохранение прошло успешно",
|
||
"Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Log.Warning("Не удалось сохранить");
|
||
MessageBox.Show($"Не сохранилось: {ex.Message}",
|
||
"Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
}
|
||
}
|
||
}
|
||
private void LoadToolStripMenuItem_Click(object sender, EventArgs e)
|
||
{
|
||
if (openFileDialog.ShowDialog() == DialogResult.OK)
|
||
{
|
||
try
|
||
{
|
||
_storage.LoadData(openFileDialog.FileName);
|
||
MessageBox.Show("Загрузка прошла успешно",
|
||
"Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||
foreach (var collection in _storage.Keys)
|
||
{
|
||
listBoxStorages.Items.Add(collection);
|
||
}
|
||
ReloadObjects();
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Log.Warning("Не удалось загрузить");
|
||
MessageBox.Show($"Не загрузилось: {ex.Message}",
|
||
"Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
}
|
||
}
|
||
}
|
||
private void ReloadObjects()
|
||
{
|
||
int index = listBoxStorages.SelectedIndex;
|
||
|
||
listBoxStorages.Items.Clear();
|
||
for (int i = 0; i < _storage.Keys.Count; i++)
|
||
{
|
||
listBoxStorages.Items.Add(_storage.Keys[i]);
|
||
}
|
||
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;
|
||
}
|
||
|
||
if (listBoxStorages.SelectedIndex == -1)
|
||
{
|
||
return;
|
||
}
|
||
var obj = _storage[listBoxStorages.SelectedItem.ToString() ??
|
||
string.Empty];
|
||
if (obj == null)
|
||
{
|
||
return;
|
||
}
|
||
pictureBoxCollection.Image = obj.ShowAirplanes();
|
||
}
|
||
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}");
|
||
|
||
}
|
||
private void deleteAirplaneButton_Click(object sender, EventArgs e)
|
||
{
|
||
if (listBoxStorages.SelectedIndex == -1)
|
||
{
|
||
return;
|
||
}
|
||
var obj = _storage[listBoxStorages.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 q = obj - pos;
|
||
MessageBox.Show("Объект удален");
|
||
Log.Information($"Удален объект из коллекции {listBoxStorages.SelectedItem.ToString() ?? string.Empty} по номеру {pos}");
|
||
pictureBoxCollection.Image = obj.ShowAirplanes();
|
||
}
|
||
catch (AirplaneNotFoundException ex)
|
||
{
|
||
Log.Warning($"Не получилось удалить объект из коллекции {listBoxStorages.SelectedItem.ToString() ?? string.Empty}");
|
||
MessageBox.Show(ex.Message);
|
||
}
|
||
catch (FormatException ex)
|
||
{
|
||
Log.Warning($"Было введено не число");
|
||
MessageBox.Show("Введите число");
|
||
}
|
||
}
|
||
|
||
private void updateCollectionButton_Click(object sender, EventArgs e)
|
||
{
|
||
if (listBoxStorages.SelectedIndex == -1)
|
||
{
|
||
return;
|
||
}
|
||
var obj = _storage[listBoxStorages.SelectedItem.ToString() ??
|
||
string.Empty];
|
||
if (obj == null)
|
||
{
|
||
return;
|
||
}
|
||
pictureBoxCollection.Image = obj.ShowAirplanes();
|
||
|
||
}
|
||
private void addAirplaneButton_Click(object sender, EventArgs e)
|
||
{
|
||
if (listBoxStorages.SelectedIndex == -1)
|
||
{
|
||
return;
|
||
}
|
||
var obj = _storage[listBoxStorages.SelectedItem.ToString() ??
|
||
string.Empty];
|
||
if (obj == null)
|
||
{
|
||
return;
|
||
}
|
||
FormAirplaneConfig form = new();
|
||
form.Show();
|
||
Action<DrawningAirplane>? airplaneDelegate = new((m) =>
|
||
{
|
||
try
|
||
{
|
||
bool q = obj + m;
|
||
MessageBox.Show("Объект добавлен");
|
||
Log.Information($"Добавлен объект в коллекцию {listBoxStorages.SelectedItem.ToString() ?? string.Empty}");
|
||
pictureBoxCollection.Image = obj.ShowAirplanes();
|
||
}
|
||
catch (StorageOverflowException ex)
|
||
{
|
||
MessageBox.Show("Не удалось добавить объект");
|
||
Log.Warning($"Коллекция {listBoxStorages.SelectedItem.ToString() ?? string.Empty} переполнена");
|
||
MessageBox.Show(ex.Message);
|
||
}
|
||
});
|
||
form.AddEvent(airplaneDelegate);
|
||
}
|
||
private void listBoxStorages_SelectedIndexChanged(object sender, EventArgs e)
|
||
{
|
||
pictureBoxCollection.Image =
|
||
_storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowAirplanes();
|
||
|
||
}
|
||
|
||
private void ButtonDelObject_Click(object sender, EventArgs e)
|
||
{
|
||
if (listBoxStorages.SelectedIndex == -1)
|
||
{
|
||
return;
|
||
}
|
||
if (MessageBox.Show($"Удалить объект {listBoxStorages.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo,
|
||
MessageBoxIcon.Question) == DialogResult.Yes)
|
||
{
|
||
string name = listBoxStorages.SelectedItem.ToString()
|
||
?? string.Empty;
|
||
_storage.DelSet(name);
|
||
ReloadObjects();
|
||
Log.Information($"Удален набор: {name}");
|
||
}
|
||
|
||
}
|
||
}
|
||
}
|