using AirBomber.Exceptions; using AirBomber.Generics; using AirBomber.Rendering; using Microsoft.Extensions.Logging; namespace AirBomber { public partial class FormEntityCollection : Form { private readonly EntitiesGenericStorage _storage; private readonly ILogger _logger; public FormEntityCollection(ILogger Logger) { InitializeComponent(); _storage = new EntitiesGenericStorage(CollectionPictureBox.Width, CollectionPictureBox.Height); _logger = Logger; } private void ReloadObjects() { int index = StorageListBox.SelectedIndex; StorageListBox.Items.Clear(); for (int i = 0; i < _storage.Keys.Count; i++) StorageListBox.Items.Add(_storage.Keys[i]); if (StorageListBox.Items.Count > 0 && (index == -1 || index >= StorageListBox.Items.Count)) StorageListBox.SelectedIndex = 0; else if (StorageListBox.Items.Count > 0 && index > -1 && index < StorageListBox.Items.Count) StorageListBox.SelectedIndex = index; } public void ButtonAddEntity_Click(object sender, EventArgs e) { if (StorageListBox.SelectedIndex == -1) return; FormBomberConfig ConfigForm = new FormBomberConfig(); ConfigForm.AddEvent(OnEntityAdded); ConfigForm.Show(); } public void OnEntityAdded(BomberRendererBase Renderer) { var obj = _storage[StorageListBox.SelectedItem.ToString() ?? string.Empty]; if (obj is null) return; if (obj + (Renderer) != -1) { MessageBox.Show("Объект добавлен"); CollectionPictureBox.Image = obj.ShowEntities(); } else { MessageBox.Show("Не удалось добавить объект"); } } public void ButtonRemoveEntity_Click(object sender, EventArgs e) { if (StorageListBox.SelectedIndex == -1) return; var obj = _storage[StorageListBox.SelectedItem.ToString() ?? string.Empty]; if (obj is null) return; if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) return; int Pos = Convert.ToInt32(NumberMaskedTextBox.Text); try { if (obj - Pos == true) { MessageBox.Show("Объект удален"); CollectionPictureBox.Image = obj.ShowEntities(); } else { MessageBox.Show("Не удалось удалить объект"); } } catch (EntityNotFoundException ex) { MessageBox.Show(ex.Message); } } public void ButtonRefreshCollection_Click(object sender, EventArgs e) { if (StorageListBox.SelectedIndex == -1) return; var obj = _storage[StorageListBox.SelectedItem.ToString() ?? string.Empty]; if (obj is null) return; CollectionPictureBox.Image = obj.ShowEntities(); } private void ButtonAddSet_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(SetNameTextBox.Text)) { MessageBox.Show("Не все данные заполнены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } _storage.AddSet(SetNameTextBox.Text); ReloadObjects(); _logger.LogInformation($"Добавлен набор: {SetNameTextBox.Text}"); } private void ButtonRemoveSet_Click(object sender, EventArgs e) { if (StorageListBox.SelectedIndex == -1) return; string SetName = StorageListBox.SelectedItem.ToString() ?? string.Empty; if (MessageBox.Show( $"Удалить объект{SetName}?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question ) == DialogResult.Yes) { _storage.RemoveSet(SetName); ReloadObjects(); _logger.LogInformation($"Удален набор: {SetName}"); } } private void StorageListBoxIndexChanged(object sender, EventArgs e) { CollectionPictureBox.Image = _storage[StorageListBox.SelectedItem?.ToString() ?? string.Empty]?.ShowEntities(); } 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) { 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); } catch (Exception ex) { MessageBox.Show($"Не загрузилось: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); } } ReloadObjects(); } } }