PIbd-21_MalafeevL.S._Cruise.../Cruiser/FormCruiserCollection.cs

189 lines
6.4 KiB
C#
Raw Normal View History

2023-10-21 14:10:32 +04:00
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection.Metadata.Ecma335;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Cruiser.Drawing;
using Cruiser.Generics;
using Cruiser.MovementStrategy;
namespace Cruiser
{
/// <summary>
/// Форма для работы с набором объектов класса DrawingCruiser
/// </summary>
public partial class FormCruiserCollection : Form
{
/// <summary>
/// Набор объектов
/// </summary>
2023-11-24 11:41:50 +04:00
private readonly CruisersGenericStorage _storage;
2023-10-21 14:10:32 +04:00
/// <summary>
/// Конструктор
/// </summary>
public FormCruiserCollection()
{
InitializeComponent();
2023-11-24 11:41:50 +04:00
_storage = new CruisersGenericStorage(pictureBoxCollection.Width, pictureBoxCollection.Height);
2023-10-21 14:10:32 +04:00
}
2023-11-24 11:41:50 +04:00
/// <summary>
/// Заполнение listBoxObjects
/// </summary>
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;
}
}
/// <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 ButtonDelObject_Click(object sender, EventArgs e)
{
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
if (MessageBox.Show($"Удалить объект{listBoxStorages.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
_storage.DelSet(listBoxStorages.SelectedItem.ToString() ?? string.Empty);
ReloadObjects();
}
}
/// <summary>
/// Выбор набора
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ListBoxObjects_SelectedIndexChanged(object sender,
EventArgs e)
{
pictureBoxCollection.Image = _storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowCruiser();
}
2023-10-21 14:10:32 +04:00
/// <summary>
/// Добавление объекта в набор
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
2023-11-24 12:12:46 +04:00
private void AddCruiser(DrawingCruiser cruiser)
2023-10-21 14:10:32 +04:00
{
2023-11-24 11:41:50 +04:00
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
if (obj == null)
{
return;
}
2023-11-24 12:12:46 +04:00
if ((obj + cruiser))
2023-10-21 14:10:32 +04:00
{
2023-11-24 12:12:46 +04:00
MessageBox.Show("Объект добавлен");
pictureBoxCollection.Image = obj.ShowCruiser();
}
else
{
MessageBox.Show("Не удалось добавить объект");
2023-10-21 14:10:32 +04:00
}
}
/// <summary>
/// Удаление объекта из набора
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonRemoveCar_Click(object sender, EventArgs e)
{
2023-11-24 11:41:50 +04:00
if (listBoxStorages.SelectedIndex == -1)
2023-10-21 14:10:32 +04:00
{
return;
}
2023-11-24 15:30:37 +04:00
var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
2023-11-24 11:41:50 +04:00
if (obj == null)
{
return;
}
if (MessageBox.Show("Удалить объект?", "Удаление",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
2023-10-21 14:10:32 +04:00
{
2023-11-24 11:41:50 +04:00
return;
2023-10-21 14:10:32 +04:00
}
int pos = Convert.ToInt32(textBoxNumber.Text);
2023-11-24 11:41:50 +04:00
if (obj - pos != null)
2023-10-21 14:10:32 +04:00
{
MessageBox.Show("Объект удален");
2023-11-24 11:41:50 +04:00
pictureBoxCollection.Image = obj.ShowCruiser();
2023-10-21 14:10:32 +04:00
}
else
{
MessageBox.Show("Не удалось удалить объект");
}
}
/// <summary>
/// Обновление рисунка по набору
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonRefreshCollection_Click(object sender, EventArgs e)
{
2023-11-24 11:41:50 +04:00
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
pictureBoxCollection.Image = obj.ShowCruiser();
2023-10-21 14:10:32 +04:00
}
2023-11-24 12:12:46 +04:00
/// <summary>
/// Добавление объекта
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonAddCruiser_Click(object sender, EventArgs e)
2023-10-21 14:10:32 +04:00
{
2023-11-24 12:12:46 +04:00
var formCruiserConfig = new FormCruiserConfig();
formCruiserConfig.Show();
formCruiserConfig.AddEvent(AddCruiser);
2023-10-21 14:10:32 +04:00
}
}
}