250 lines
8.7 KiB
C#
Raw Permalink Normal View History

2022-10-05 23:08:51 +04:00
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
2022-10-11 11:25:31 +04:00
using System.Reflection;
2022-10-05 23:08:51 +04:00
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Airbus
{
public partial class FormMapWithSetPlanes : Form
{
2022-10-11 11:25:31 +04:00
//словарь для выпадающего списка
private readonly Dictionary<string, AbstractMap> _mapsDict = new()
{
{"Простая карта", new SimpleMap() },
{"Буря в пустыне", new DesertStormMap() },
{"Звёздные войны", new StarWarsMap() }
};
//объект от коллекции карт
private readonly MapsCollection _mapsCollection;
2022-10-05 23:08:51 +04:00
2022-11-02 22:24:43 +04:00
//конструктор
2022-10-05 23:08:51 +04:00
public FormMapWithSetPlanes()
{
InitializeComponent();
2022-10-11 11:25:31 +04:00
_mapsCollection = new MapsCollection(pictureBox.Width, pictureBox.Height);
comboBoxSelectorMap.Items.Clear();
foreach (var element in _mapsDict)
{
comboBoxSelectorMap.Items.Add(element.Key);
}
2022-10-05 23:08:51 +04:00
}
2022-10-11 11:25:31 +04:00
//заполнение ListBoxMaps
private void ReloadMaps()
2022-10-05 23:08:51 +04:00
{
2022-10-11 11:25:31 +04:00
int index = listBoxMaps.SelectedIndex;
2022-10-05 23:08:51 +04:00
2022-10-11 11:25:31 +04:00
listBoxMaps.Items.Clear();
for (int i = 0; i < _mapsCollection.Keys.Count; i++)
2022-10-05 23:08:51 +04:00
{
2022-10-11 11:25:31 +04:00
listBoxMaps.Items.Add(_mapsCollection.Keys[i]);
2022-10-05 23:08:51 +04:00
}
2022-10-11 11:25:31 +04:00
if (listBoxMaps.Items.Count > 0 && (index == -1 || index >= listBoxMaps.Items.Count))
2022-10-05 23:08:51 +04:00
{
2022-10-11 11:25:31 +04:00
listBoxMaps.SelectedIndex = 0;
2022-10-05 23:08:51 +04:00
}
2022-10-11 11:25:31 +04:00
else if (listBoxMaps.Items.Count > 0 && index > -1 && index < listBoxMaps.Items.Count)
2022-10-05 23:08:51 +04:00
{
2022-10-11 11:25:31 +04:00
listBoxMaps.SelectedIndex = index;
}
}
//добавление карты
private void ButtonAddMap_Click_1(object sender, EventArgs e)
{
if (comboBoxSelectorMap.SelectedIndex == -1 || string.IsNullOrEmpty(textBoxNewMapName.Text))
{
MessageBox.Show("Не все данные заполнены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (!_mapsDict.ContainsKey(comboBoxSelectorMap.Text))
{
MessageBox.Show("Нет такой карты", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
_mapsCollection.AddMap(textBoxNewMapName.Text,
_mapsDict[comboBoxSelectorMap.Text]);
ReloadMaps();
}
//Выбор карты
private void ListBoxMaps_SelectedIndexChanged(object sender, EventArgs e)
{
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
// Удаление карты
private void buttonDeleteMap_Click(object sender, EventArgs e)
{
if (listBoxMaps.SelectedIndex == -1)
{
return;
}
if (MessageBox.Show($"Удалить карту {listBoxMaps.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
_mapsCollection.DelMap(listBoxMaps.SelectedItem?.ToString() ?? string.Empty);
ReloadMaps();
2022-10-05 23:08:51 +04:00
}
}
2022-11-02 22:24:43 +04:00
//отрисовка добавленного объекта в хранилище
private void AddPlane(DrawningAirbus plane)
{
if (listBoxMaps.SelectedIndex == -1)
{
return;
}
if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + new DrawningObjectPlane(plane) != -1)
{
MessageBox.Show("Объект добавлен");
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
else
{
MessageBox.Show("Не удалось добавить объект");
}
}
2022-10-05 23:08:51 +04:00
//добавление объекта
private void ButtonAddPlane_Click(object sender, EventArgs e)
{
2022-10-20 11:35:08 +04:00
var formPlaneConfig = new FormPlaneConfig();
formPlaneConfig.AddEvent(AddPlane);
2022-10-20 11:35:08 +04:00
formPlaneConfig.Show();
2022-10-05 23:08:51 +04:00
}
//удаление объекта
2022-10-05 23:08:51 +04:00
private void ButtonRemovePlane_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(maskedTextBoxPosition.Text))
{
return;
}
if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo,
2022-10-05 23:08:51 +04:00
MessageBoxIcon.Question) == DialogResult.No)
{
return;
}
int pos = Convert.ToInt32(maskedTextBoxPosition.Text);
if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? String.Empty] - pos != null)
2022-10-05 23:08:51 +04:00
{
MessageBox.Show("Объект удалён");
2022-10-11 11:25:31 +04:00
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? String.Empty].ShowSet();
}
2022-10-05 23:08:51 +04:00
else
{
MessageBox.Show("Не удалось удалить объект");
}
}
//вывод набора
private void ButtonShowStorage_Click(object sender, EventArgs e)
2022-10-05 23:08:51 +04:00
{
if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? String.Empty] == null)
2022-10-05 23:08:51 +04:00
{
return;
}
2022-10-11 11:25:31 +04:00
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? String.Empty].ShowSet();
2022-10-05 23:08:51 +04:00
}
//вывод карты
private void ButtonShowOnMap_Click(object sender, EventArgs e)
2022-10-05 23:08:51 +04:00
{
2022-10-11 11:25:31 +04:00
if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? String.Empty] == null)
2022-10-05 23:08:51 +04:00
{
return;
}
2022-10-11 11:25:31 +04:00
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? String.Empty].ShowOnMap();
2022-10-05 23:08:51 +04:00
}
//перемещение
private void ButtonMove_Click(object sender, EventArgs e)
{
if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? String.Empty] == null)
2022-10-05 23:08:51 +04:00
{
return;
}
//получаем имя кнопки
string name = ((Button)sender)?.Name ?? string.Empty;
Direction dir = Direction.None;
switch (name)
{
case "buttonUp":
dir = Direction.Up;
break;
case "buttonLeft":
dir = Direction.Left;
break;
case "buttonDown":
dir = Direction.Down;
break;
case "buttonRight":
dir = Direction.Right;
break;
}
2022-10-11 11:25:31 +04:00
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? String.Empty].MoveObject(dir);
2022-10-05 23:08:51 +04:00
}
//обработка нажатия сохранения
private void SaveToolStripMenuItem_Click(object sender, EventArgs e)
{
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
if (_mapsCollection.SaveData(saveFileDialog.FileName))
{
MessageBox.Show("Сохранение прошло успешно", "Результат",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("Не сохранилось", "Результат",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
//обработка нажатия загрузки
private void LoadToolStripMenuItem_Click(object sender, EventArgs e)
{
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
if (_mapsCollection.LoadData(openFileDialog.FileName))
{
ReloadMaps();
MessageBox.Show("Загрузка данных прошла успешно", "Результат",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("Ошибка загрузки данных", "Результат",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
2022-10-05 23:08:51 +04:00
}
}