PIbd-21_Valitov_D.F_Sailboa.../Sailboat/FormMapWithSetBoats.cs

183 lines
6.4 KiB
C#
Raw Normal View History

2023-04-14 14:36:46 +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 Sailboat
{
public partial class FormMapWithSetBoats : Form
{
2023-04-14 22:41:19 +04:00
private readonly Dictionary<string, AbstractMap> _mapsDict = new Dictionary<string, AbstractMap>() {
{ "Простая карта", new SimpleMap() },
{ "Водная карта", new WaterMap() }
};
private readonly MapsCollection _mapsCollection;
2023-04-14 14:36:46 +04:00
public FormMapWithSetBoats()
{
InitializeComponent();
2023-04-14 22:41:19 +04:00
_mapsCollection = new MapsCollection(pictureBox.Width, pictureBox.Height);
comboBoxMapSelector.Items.Clear();
foreach (var item in _mapsDict)
{
comboBoxMapSelector.Items.Add(item.Key);
}
2023-04-14 14:36:46 +04:00
}
2023-04-14 22:41:19 +04:00
private void ReloadMaps()
2023-04-14 14:36:46 +04:00
{
2023-04-14 22:41:19 +04:00
int index = listBoxMaps.SelectedIndex;
listBoxMaps.Items.Clear();
for (int i = 0; i < _mapsCollection.Keys.Count; i++)
2023-04-14 14:36:46 +04:00
{
2023-04-14 22:41:19 +04:00
listBoxMaps.Items.Add(_mapsCollection.Keys[i]);
2023-04-14 14:36:46 +04:00
}
2023-04-14 22:41:19 +04:00
if (listBoxMaps.Items.Count > 0 && (index == -1 || index >= listBoxMaps.Items.Count))
2023-04-14 14:36:46 +04:00
{
2023-04-14 22:41:19 +04:00
listBoxMaps.SelectedIndex = 0;
2023-04-14 14:36:46 +04:00
}
2023-04-14 22:41:19 +04:00
else if (listBoxMaps.Items.Count > 0 && index > -1 && index < listBoxMaps.Items.Count)
2023-04-14 14:36:46 +04:00
{
2023-04-14 22:41:19 +04:00
listBoxMaps.SelectedIndex = index;
2023-04-14 14:36:46 +04:00
}
}
private void btn_add_boat_Click(object sender, EventArgs e)
{
2023-04-14 22:41:19 +04:00
if (listBoxMaps.SelectedIndex == -1)
2023-04-14 14:36:46 +04:00
{
return;
}
BoatForm form = new();
if (form.ShowDialog() == DialogResult.OK)
{
if (form.SelectedBoat == null)
{
MessageBox.Show("Вы не создали объект");
return;
}
2023-04-14 21:28:29 +04:00
DrawingObjectBoat boat = new(form.SelectedBoat);
2023-04-14 14:36:46 +04:00
2023-04-14 22:41:19 +04:00
if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + boat != -1)
2023-04-14 14:36:46 +04:00
{
MessageBox.Show("Объект добавлен");
2023-04-14 22:41:19 +04:00
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
2023-04-14 14:36:46 +04:00
}
else
{
MessageBox.Show("Не удалось добавить объект");
}
}
}
private void btn_remove_boat_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(maskedTextBoxPosition.Text))
{
return;
}
if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
return;
}
int pos = Convert.ToInt32(maskedTextBoxPosition.Text);
2023-04-14 22:41:19 +04:00
if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] - pos != null)
2023-04-14 14:36:46 +04:00
{
MessageBox.Show("Объект удален");
2023-04-14 22:41:19 +04:00
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
2023-04-14 14:36:46 +04:00
}
else
{
MessageBox.Show("Не удалось удалить объект");
}
}
private void btn_show_storage_Click(object sender, EventArgs e)
{
2023-04-14 22:41:19 +04:00
if (listBoxMaps.SelectedIndex == -1)
2023-04-14 14:36:46 +04:00
{
return;
}
2023-04-14 22:41:19 +04:00
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
2023-04-14 14:36:46 +04:00
}
private void btn_show_map_Click(object sender, EventArgs e)
{
2023-04-14 22:41:19 +04:00
if (listBoxMaps.SelectedIndex == -1)
2023-04-14 14:36:46 +04:00
{
return;
}
2023-04-14 22:41:19 +04:00
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowOnMap();
2023-04-14 14:36:46 +04:00
}
private void btn_move_Click(object sender, EventArgs e)
{
2023-04-14 22:41:19 +04:00
if (listBoxMaps.SelectedIndex == -1)
2023-04-14 14:36:46 +04:00
{
return;
}
string name = ((Button)sender)?.Name ?? string.Empty;
Direction dir = Direction.None;
switch (name)
{
case "btn_up":
dir = Direction.Up;
break;
case "btn_down":
dir = Direction.Down;
break;
case "btn_left":
dir = Direction.Left;
break;
case "btn_right":
dir = Direction.Right;
break;
}
2023-04-14 22:41:19 +04:00
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].MoveObject(dir);
}
private void btn_add_map_Click(object sender, EventArgs e)
{
if (comboBoxMapSelector.SelectedIndex == -1 || string.IsNullOrEmpty(textBoxNewMapName.Text))
{
MessageBox.Show("Не все данные заполнены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (!_mapsDict.ContainsKey(comboBoxMapSelector.Text))
{
MessageBox.Show("Нет такой карты", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
_mapsCollection.AddMap(textBoxNewMapName.Text, _mapsDict[comboBoxMapSelector.Text]);
ReloadMaps();
}
private void listBoxMaps_SelectedIndexChanged(object sender, EventArgs e)
{
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
private void btn_delete_map_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();
}
2023-04-14 14:36:46 +04:00
}
}
}