Pibd-22_Presnyakova.V.V_Cat.../Catamaran/FormMapWithSetBoats.cs

239 lines
8.3 KiB
C#
Raw Normal View History

2022-11-06 23:24:34 +04:00
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 Catamaran
{
public partial class FormMapWithSetBoats : Form
{
2022-11-06 23:39:21 +04:00
/// <summary>
/// Словарь для выпадающего списка
/// </summary>
private readonly Dictionary<string, AbstractMap> _mapsDict = new Dictionary<string, AbstractMap>()
{
{ "SimpleMap", new SimpleMap() },
{"SecondMap", new SecondMap() }
};
/// <summary>
/// Объект от коллекции карт
/// </summary>
private readonly MapsCollection _mapsCollection;
2022-11-06 23:24:34 +04:00
2022-11-06 23:39:21 +04:00
/// <summary>
/// Конструктор
/// </summary>
2022-11-06 23:24:34 +04:00
public FormMapWithSetBoats()
{
InitializeComponent();
2022-11-06 23:39:21 +04:00
_mapsCollection = new MapsCollection(pictureBox.Width, pictureBox.Height);
comboBoxSelectorMap.Items.Clear();
foreach (var elem in _mapsDict)
{
comboBoxSelectorMap.Items.Add(elem.Key);
}
}
/// <summary>
/// Заполнение listBoxMaps
/// </summary>
private void ReloadMaps()
{
int index = listBoxMaps.SelectedIndex;
listBoxMaps.Items.Clear();
for (int i = 0; i < _mapsCollection.Keys.Count; i++)
{
listBoxMaps.Items.Add(_mapsCollection.Keys[i]);
}
2022-11-06 23:24:34 +04:00
2022-11-06 23:39:21 +04:00
if (listBoxMaps.Items.Count > 0 && (index == -1 || index >= listBoxMaps.Items.Count))
{
listBoxMaps.SelectedIndex = 0;
}
else if (listBoxMaps.Items.Count > 0 && index > -1 && index < listBoxMaps.Items.Count)
{
listBoxMaps.SelectedIndex = index;
}
2022-11-06 23:24:34 +04:00
}
/// <summary>
2022-11-06 23:39:21 +04:00
/// Добавление карты
2022-11-06 23:24:34 +04:00
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
2022-11-06 23:39:21 +04:00
private void ButtonAddMap_Click(object sender, EventArgs e)
2022-11-06 23:24:34 +04:00
{
2022-11-06 23:39:21 +04:00
if (comboBoxSelectorMap.SelectedIndex == -1 || string.IsNullOrEmpty(textBoxNewMapName.Text))
2022-11-06 23:24:34 +04:00
{
2022-11-06 23:39:21 +04:00
MessageBox.Show("Не все данные заполнены", "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
2022-11-06 23:24:34 +04:00
}
2022-11-06 23:39:21 +04:00
if (!_mapsDict.ContainsKey(comboBoxSelectorMap.Text))
2022-11-06 23:24:34 +04:00
{
2022-11-06 23:39:21 +04:00
MessageBox.Show("Нет такой карты", "Ошибка", MessageBoxButtons.OK,
MessageBoxIcon.Error);
return;
2022-11-06 23:24:34 +04:00
}
2022-11-06 23:39:21 +04:00
_mapsCollection.AddMap(textBoxNewMapName.Text,
_mapsDict[comboBoxSelectorMap.Text]);
ReloadMaps();
2022-11-06 23:24:34 +04:00
}
/// <summary>
2022-11-06 23:39:21 +04:00
/// Выбор карты
2022-11-06 23:24:34 +04:00
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
2022-11-06 23:39:21 +04:00
private void ListBoxMaps_SelectedIndexChanged(object sender, EventArgs e)
{
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
/// <summary>
/// Удаление карты
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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-11-06 23:24:34 +04:00
/// <summary>
/// Удаление объекта
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonRemoveBoat_Click(object sender, EventArgs e)
{
2022-11-06 23:39:21 +04:00
if (listBoxMaps.SelectedIndex == -1)
{
return;
}
2022-11-06 23:24:34 +04:00
if (string.IsNullOrEmpty(maskedTextBoxPosition.Text))
{
return;
}
if (MessageBox.Show("Удалить объект?", "Удаление",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
return;
}
int pos = Convert.ToInt32(maskedTextBoxPosition.Text);
2022-11-06 23:39:21 +04:00
if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] - pos != null)
2022-11-06 23:24:34 +04:00
{
MessageBox.Show("Объект удален");
2022-11-06 23:39:21 +04:00
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
2022-11-06 23:24:34 +04:00
}
else
{
MessageBox.Show("Не удалось удалить объект");
}
}
/// <summary>
/// Вывод набора
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonShowStorage_Click(object sender, EventArgs e)
{
2022-11-06 23:39:21 +04:00
if (listBoxMaps.SelectedIndex == -1)
2022-11-06 23:24:34 +04:00
{
return;
}
2022-11-06 23:39:21 +04:00
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
2022-11-06 23:24:34 +04:00
}
/// <summary>
/// Вывод карты
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonShowOnMap_Click(object sender, EventArgs e)
{
2022-11-06 23:39:21 +04:00
if (listBoxMaps.SelectedIndex == -1)
2022-11-06 23:24:34 +04:00
{
return;
}
2022-11-06 23:39:21 +04:00
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowOnMap();
2022-11-06 23:24:34 +04:00
2022-11-06 23:39:21 +04:00
}
2022-11-06 23:24:34 +04:00
/// <summary>
/// Перемещение
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonMove_Click(object sender, EventArgs e)
{
2022-11-06 23:39:21 +04:00
if (listBoxMaps.SelectedIndex == -1)
2022-11-06 23:24:34 +04:00
{
return;
}
//получаем имя кнопки
string name = ((Button)sender)?.Name ?? string.Empty;
Direction dir = Direction.None;
switch (name)
{
case "buttonUp":
dir = Direction.Up;
break;
case "buttonDown":
dir = Direction.Down;
break;
case "buttonLeft":
dir = Direction.Left;
break;
case "buttonRight":
dir = Direction.Right;
break;
}
2022-11-06 23:39:21 +04:00
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].MoveObject(dir);
2022-11-06 23:24:34 +04:00
2022-11-06 23:39:21 +04:00
}
/// <summary>
/// Добавление объекта
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
2022-11-06 23:24:34 +04:00
private void buttonAddBoat_Click_1(object sender, EventArgs e)
2022-11-07 01:27:11 +04:00
{
var formBoatConfig = new FormBoatConfig();
formBoatConfig.AddEvent(new Action<DrawingBoat>(AddBoat));
2022-11-20 20:46:00 +04:00
// TODO Call method AddEvent from formBoatConfig
2022-11-07 01:27:11 +04:00
formBoatConfig.Show();
}
private void AddBoat(DrawingBoat boat)
2022-11-06 23:24:34 +04:00
{
2022-11-06 23:39:21 +04:00
if (listBoxMaps.SelectedIndex == -1)
2022-11-06 23:24:34 +04:00
{
2022-11-07 01:27:11 +04:00
MessageBox.Show("Перед добавлением объекта необходимо создать карту");
2022-11-06 23:24:34 +04:00
}
2022-11-07 01:27:11 +04:00
else if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + new DrawingObjectBoat(boat) != -1)
{
MessageBox.Show("Объект добавлен");
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
else
{
MessageBox.Show("Не удалось добавить объект");
2022-11-06 23:24:34 +04:00
}
}
}
}