using Hydroplane.DrawningObjects;
using Hydroplane.Generics;
using Hydroplane.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;
namespace Hydroplane
{
public partial class FormHydroplaneCollection : Form
{
private readonly PlanesGenericStorage _storage;
///
/// Конструктор
///
public FormHydroplaneCollection()
{
InitializeComponent();
_storage = new PlanesGenericStorage(DrawPlane.Width, DrawPlane.Height);
}
///
/// Заполнение listBoxObjects
///
private void ReloadObjects()
{
int index = CollectionListBox.SelectedIndex;
CollectionListBox.Items.Clear();
for (int i = 0; i < _storage.Keys.Count; i++)
{
CollectionListBox.Items.Add(_storage.Keys[i]);
}
if (CollectionListBox.Items.Count > 0 && (index == -1 || index
>= CollectionListBox.Items.Count))
{
CollectionListBox.SelectedIndex = 0;
}
else if (CollectionListBox.Items.Count > 0 && index > -1 &&
index < CollectionListBox.Items.Count)
{
CollectionListBox.SelectedIndex = index;
}
}
///
/// Добавление набора в коллекцию
///
///
///
private void ButtonAddObject_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(SetTextBox.Text))
{
MessageBox.Show("Не все данные заполнены", "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
_storage.AddSet(SetTextBox.Text);
ReloadObjects();
}
///
/// Выбор набора
///
///
///
private void ListBoxObjects_SelectedIndexChanged(object sender,
EventArgs e)
{
DrawPlane.Image =
_storage[CollectionListBox.SelectedItem?.ToString() ?? string.Empty]?.ShowPlanes();
}
///
/// Удаление набора
///
///
///
private void ButtonDelObject_Click(object sender, EventArgs e)
{
if (CollectionListBox.SelectedIndex == -1)
{
return;
}
if (MessageBox.Show($"Удалить объект {CollectionListBox.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo,
MessageBoxIcon.Question) == DialogResult.Yes)
{
_storage.DelSet(CollectionListBox.SelectedItem.ToString()
?? string.Empty);
ReloadObjects();
}
}
private void ButtonAddPlane_Click(object sender, EventArgs e)
{
if (CollectionListBox.SelectedIndex == -1)
{
return;
}
var formPlaneConfig = new FormPlaneConfig();
formPlaneConfig.AddEvent(plane =>
{
if (CollectionListBox.SelectedIndex != -1)
{
var obj = _storage[CollectionListBox.SelectedItem?.ToString() ?? string.Empty];
if (obj != null)
{
if (obj + plane)
{
MessageBox.Show("Объект добавлен");
DrawPlane.Image = obj.ShowPlanes();
}
else
{
MessageBox.Show("Не удалось добавить объект");
}
}
}
});
formPlaneConfig.Show();
}
///
/// Удаление объекта из набора
///
///
///
private void ButtonRemovePlane_Click(object sender, EventArgs e)
{
if (CollectionListBox.SelectedIndex == -1)
{
return;
}
var obj = _storage[CollectionListBox.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
if (MessageBox.Show("Удалить объект?", "Удаление",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
return;
}
int pos = 0;
if (PlaneTextBox != null)
pos = Convert.ToInt32(PlaneTextBox.Text);
if (obj - pos != null)
{
MessageBox.Show("Объект удален");
DrawPlane.Image = obj.ShowPlanes();
}
else
{
MessageBox.Show("Не удалось удалить объект");
}
}
///
/// Обновление рисунка по набору
///
///
///
private void ButtonRefreshCollection_Click(object sender, EventArgs
e)
{
if (CollectionListBox.SelectedIndex == -1)
{
return;
}
var obj = _storage[CollectionListBox.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
DrawPlane.Image = obj.ShowPlanes();
}
}
}