235 lines
8.8 KiB
C#
235 lines
8.8 KiB
C#
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 Trolleybus
|
||
{
|
||
public partial class FormMapWithSetTrolleybus : Form
|
||
{
|
||
private readonly Dictionary<string, AbstractMap> _mapsDict = new()
|
||
{
|
||
{ "Простая карта", new SimpleMap() },
|
||
{ "Сложная карта", new AutoStopMap() },
|
||
};
|
||
private readonly MapsCollection _mapsCollection;
|
||
public FormMapWithSetTrolleybus()
|
||
{
|
||
InitializeComponent();
|
||
_mapsCollection = new MapsCollection(pictureBox.Width, pictureBox.Height);
|
||
comboBoxSelectorMap.Items.Clear();
|
||
foreach (var elem in _mapsDict)
|
||
{
|
||
comboBoxSelectorMap.Items.Add(elem.Key);
|
||
}
|
||
}
|
||
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]);
|
||
}
|
||
|
||
foreach (string map in _mapsCollection.Keys)
|
||
{
|
||
listBoxMaps.Items.Add(map);
|
||
}
|
||
|
||
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;
|
||
}
|
||
}
|
||
private void ButtonAddMap_Click(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();
|
||
}
|
||
}
|
||
private MapWithSetTrolleybusGeneric<DrawningObjectTrolleybus, AbstractMap> _mapTrolleybusCollectionGeneric;
|
||
private void ComboBoxSelectorMap_SelectedIndexChanged(object sender, EventArgs e)
|
||
{
|
||
AbstractMap map = null;
|
||
switch (comboBoxSelectorMap.Text)
|
||
{
|
||
case "Простая карта":
|
||
map = new SimpleMap();
|
||
break;
|
||
case "Сложная карта":
|
||
map = new AutoStopMap();
|
||
break;
|
||
}
|
||
if (map != null)
|
||
{
|
||
_mapTrolleybusCollectionGeneric = new MapWithSetTrolleybusGeneric<DrawningObjectTrolleybus, AbstractMap>(
|
||
pictureBox.Width, pictureBox.Height, map);
|
||
}
|
||
else
|
||
{
|
||
_mapTrolleybusCollectionGeneric = null;
|
||
}
|
||
}
|
||
private void AddTrolleybus(DrawingTrolleybus trolleybus)
|
||
{
|
||
if (listBoxMaps.SelectedIndex == -1)
|
||
{
|
||
return;
|
||
}
|
||
if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + new DrawningObjectTrolleybus(trolleybus) != -1)
|
||
{
|
||
MessageBox.Show("Объект добавлен");
|
||
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
|
||
}
|
||
else
|
||
{
|
||
MessageBox.Show("Не удалось добавить объект");
|
||
}
|
||
|
||
}
|
||
|
||
private void ButtonAddTrolleybus_Click(object sender, EventArgs e)
|
||
{
|
||
var formTrolleybusConfig = new FormTrolleybusConfig();
|
||
|
||
formTrolleybusConfig.AddEvent(AddTrolleybus);
|
||
formTrolleybusConfig.Show();
|
||
/*if (listBoxMaps.SelectedIndex == -1)
|
||
{
|
||
return;
|
||
}
|
||
Form1 form = new();
|
||
if (form.ShowDialog() == DialogResult.OK)
|
||
{
|
||
DrawningObjectTrolleybus tractor = new(form.SelectedTrolleybus);
|
||
if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + tractor != -1)
|
||
{
|
||
MessageBox.Show("Объект добавлен");
|
||
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
|
||
}
|
||
else
|
||
{
|
||
MessageBox.Show("Не удалось добавить объект");
|
||
}
|
||
}*/
|
||
}
|
||
/// <summary>
|
||
/// Удаление объекта
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void ButtonRemoveTrolleybus_Click(object sender, EventArgs e)
|
||
{
|
||
if (listBoxMaps.SelectedIndex == -1 || string.IsNullOrEmpty(maskedTextBoxPosition.Text) ||
|
||
MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
|
||
{
|
||
return;
|
||
}
|
||
int pos = Convert.ToInt32(maskedTextBoxPosition.Text);
|
||
if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] - pos != null)
|
||
{
|
||
MessageBox.Show("Объект удален");
|
||
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
|
||
}
|
||
else
|
||
{
|
||
MessageBox.Show("Не удалось удалить объект");
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// Вывод набора
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void ButtonShowStorage_Click(object sender, EventArgs e)
|
||
{
|
||
if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? String.Empty] == null)
|
||
{
|
||
return;
|
||
}
|
||
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
|
||
}
|
||
/// <summary>
|
||
/// Вывод карты
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void ButtonShowOnMap_Click(object sender, EventArgs e)
|
||
{
|
||
if (listBoxMaps.SelectedIndex == -1)
|
||
{
|
||
return;
|
||
}
|
||
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowOnMap();
|
||
}
|
||
/// <summary>
|
||
/// Перемещение
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void ButtonMove_Click(object sender, EventArgs e)
|
||
{
|
||
if (listBoxMaps.SelectedIndex == -1)
|
||
{
|
||
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;
|
||
}
|
||
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].MoveObject(dir);
|
||
}
|
||
}
|
||
}
|