PIbd-23_Dolgova_D.N._Warmly.../WarmlyShip/FormShipCollection.cs

196 lines
6.4 KiB
C#
Raw Normal View History

2023-12-01 21:18:58 +04:00
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;
namespace WarmlyShip
{
/// <summary>
/// Форма для работы с набором объектов класса DrawningShip
/// </summary>
public partial class FormShipCollection : Form
{
/// <summary>
/// Набор объектов
/// </summary>
2023-12-02 00:46:08 +04:00
private readonly ShipsGenericStorage _storage;
2023-12-01 21:18:58 +04:00
/// <summary>
/// Конструктор
/// </summary>
public FormShipCollection()
{
InitializeComponent();
2023-12-02 00:46:08 +04:00
_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]);
}
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();
}
/// <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)
{
_storage.DelSet(ListBoxObjects.SelectedItem.ToString()
?? string.Empty);
ReloadObjects();
}
2023-12-01 21:18:58 +04:00
}
/// <summary>
/// Добавление объекта в набор
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonAddShip_Click(object sender, EventArgs e)
{
2023-12-02 00:46:08 +04:00
if (ListBoxObjects.SelectedIndex == -1)
{
return;
}
var obj = _storage[ListBoxObjects.SelectedItem.ToString() ?? string.Empty];
if (obj == null)
{
return;
}
2023-12-10 23:51:40 +04:00
FormShipConfig form = new();
form.Show();
Action<DrawningShip>? shipDelegate = new((m) =>
2023-12-01 21:18:58 +04:00
{
2023-12-10 23:51:40 +04:00
bool isAdditionSuccessful = (obj + m);
if (isAdditionSuccessful)
2023-12-01 21:18:58 +04:00
{
MessageBox.Show("Объект добавлен");
2023-12-10 23:51:40 +04:00
m.ChangePictureBoxSize(pictureBoxCollection.Width, pictureBoxCollection.Height);
2023-12-02 00:46:08 +04:00
pictureBoxCollection.Image = obj.ShowShips();
2023-12-01 21:18:58 +04:00
}
else
{
MessageBox.Show("Не удалось добавить объект");
}
2023-12-10 23:51:40 +04:00
});
form.AddEvent(shipDelegate);
2023-12-01 21:18:58 +04:00
}
/// <summary>
/// Удаление объекта из набора
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonDeleteShip_Click(object sender, EventArgs e)
{
2023-12-02 00:46:08 +04:00
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)
2023-12-01 21:18:58 +04:00
{
return;
}
int pos = Convert.ToInt32(maskedTextBoxNumber.Text);
2023-12-02 00:46:08 +04:00
if (obj - pos != null)
2023-12-01 21:18:58 +04:00
{
MessageBox.Show("Объект удален");
2023-12-02 00:46:08 +04:00
pictureBoxCollection.Image = obj.ShowShips();
2023-12-01 21:18:58 +04:00
}
else
{
MessageBox.Show("Не удалось удалить объект");
}
}
/// <summary>
/// Обновление рисунка по набору
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonRefreshCollection_Click(object sender, EventArgs e)
{
2023-12-02 00:46:08 +04:00
if (ListBoxObjects.SelectedIndex == -1)
{
return;
}
var obj = _storage[ListBoxObjects.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
pictureBoxCollection.Image = obj.ShowShips();
2023-12-01 21:18:58 +04:00
}
}
}