не работает добавление

This commit is contained in:
Макс Бондаренко 2022-10-30 00:49:41 +04:00
parent f5435fbcce
commit 6e509b6d07
3 changed files with 25 additions and 61 deletions

View File

@ -132,7 +132,7 @@
this.comboBoxSelectorMap.Name = "comboBoxSelectorMap"; this.comboBoxSelectorMap.Name = "comboBoxSelectorMap";
this.comboBoxSelectorMap.Size = new System.Drawing.Size(176, 23); this.comboBoxSelectorMap.Size = new System.Drawing.Size(176, 23);
this.comboBoxSelectorMap.TabIndex = 0; this.comboBoxSelectorMap.TabIndex = 0;
this.comboBoxSelectorMap.SelectedIndexChanged += new System.EventHandler(this.ComboBoxSelectorMap_SelectedIndexChanged); //this.comboBoxSelectorMap.SelectedIndexChanged += new System.EventHandler(this.ComboBoxSelectorMap_SelectedIndexChanged);
// //
// maskedTextBoxPosition // maskedTextBoxPosition
// //

View File

@ -8,15 +8,14 @@ using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Forms; using System.Windows.Forms;
using WarmlyShip; using WarmlyShip;
using static System.Windows.Forms.DataFormats;
namespace WarmlyShip namespace WarmlyShip
{ {
public partial class FormMapWithSetShip : Form public partial class FormMapWithSetShip : Form
{ {
private MapWithSetShipGeneric<DrawningObjectShip, AbstractMap> _mapShipCollectionGeneric; private readonly Dictionary<string, AbstractMap> _mapsDict = new()
public bool ShipOnMap = false; {
private readonly Dictionary<string, AbstractMap> _mapsDict = new() {
{ "Простая карта", new SimpleMap() }, { "Простая карта", new SimpleMap() },
{ "Вторая карта", new SecondMap() }, { "Вторая карта", new SecondMap() },
{ "Последняя карта", new LastMap() } { "Последняя карта", new LastMap() }
@ -33,7 +32,6 @@ namespace WarmlyShip
{ {
comboBoxSelectorMap.Items.Add(elem.Key); comboBoxSelectorMap.Items.Add(elem.Key);
} }
} }
private void ReloadMaps() private void ReloadMaps()
@ -54,25 +52,6 @@ namespace WarmlyShip
} }
} }
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 void ButtonAddMap_Click(object sender, EventArgs e) private void ButtonAddMap_Click(object sender, EventArgs e)
{ {
if (comboBoxSelectorMap.SelectedIndex == -1 || string.IsNullOrEmpty(textBoxNewMapName.Text)) if (comboBoxSelectorMap.SelectedIndex == -1 || string.IsNullOrEmpty(textBoxNewMapName.Text))
@ -89,31 +68,25 @@ namespace WarmlyShip
ReloadMaps(); ReloadMaps();
} }
private void ListBoxMaps_SelectedIndexChanged(object sender, EventArgs e)
{
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
private void ComboBoxSelectorMap_SelectedIndexChanged(object sender, EventArgs e) private void ButtonDeleteMap_Click(object sender, EventArgs e)
{ {
AbstractMap map = null; if (listBoxMaps.SelectedIndex == -1)
switch (comboBoxSelectorMap.Text)
{ {
case "Простая карта": return;
map = new SimpleMap();
break;
case "Вторая карта":
map = new SecondMap();
break;
case "Последняя карта":
map = new LastMap();
break;
} }
if (map != null) if (MessageBox.Show($"Удалить карту {listBoxMaps.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{ {
_mapShipCollectionGeneric = new MapWithSetShipGeneric<DrawningObjectShip, AbstractMap>(pictureBox.Width, pictureBox.Height, map); _mapsCollection.DelMap(listBoxMaps.SelectedItem?.ToString() ??
} string.Empty);
else ReloadMaps();
{
_mapShipCollectionGeneric = null;
} }
} }
private void ButtonAddShip_Click(object sender, EventArgs e) private void ButtonAddShip_Click(object sender, EventArgs e)
{ {
if (listBoxMaps.SelectedIndex == -1) if (listBoxMaps.SelectedIndex == -1)
@ -124,10 +97,10 @@ namespace WarmlyShip
if (form.ShowDialog() == DialogResult.OK) if (form.ShowDialog() == DialogResult.OK)
{ {
DrawningObjectShip ship = new(form.SelectedShip); DrawningObjectShip ship = new(form.SelectedShip);
if (_mapShipCollectionGeneric + ship == 1) if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + ship == 1)
{ {
MessageBox.Show("Объект добавлен"); MessageBox.Show("Объект добавлен");
pictureBox.Image = _mapShipCollectionGeneric.ShowSet(); pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
} }
else else
{ {
@ -154,13 +127,13 @@ namespace WarmlyShip
if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] - pos == 1) if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] - pos == 1)
{ {
MessageBox.Show("Объект удален"); MessageBox.Show("Объект удален");
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); pictureBox.Image =
_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
} }
else else
{ {
MessageBox.Show("Не удалось удалить объект"); MessageBox.Show("Не удалось удалить объект");
} }
} }
private void ButtonShowStorage_Click(object sender, EventArgs e) private void ButtonShowStorage_Click(object sender, EventArgs e)

View File

@ -24,21 +24,15 @@ namespace WarmlyShip
_pictureHeight = pictureHeight; _pictureHeight = pictureHeight;
} }
private bool inDir(string name)
{
foreach(var key in Keys)
if (key == name) return true;
return false;
}
public void AddMap(string name, AbstractMap map) public void AddMap(string name, AbstractMap map)
{ {
if (inDir(name)) return; if (_mapStorages.ContainsKey(name)) return;
_mapStorages.Add(name, new MapWithSetShipGeneric<DrawningObjectShip, AbstractMap>(_pictureWidth, _pictureHeight, map)); _mapStorages.Add(name, new MapWithSetShipGeneric<DrawningObjectShip, AbstractMap>(_pictureWidth, _pictureHeight, map));
} }
public void DelMap(string name) public void DelMap(string name)
{ {
if (!inDir(name)) return; if (_mapStorages.ContainsKey(name)) return;
_mapStorages.Remove(name); _mapStorages.Remove(name);
} }
@ -46,10 +40,7 @@ namespace WarmlyShip
{ {
get get
{ {
foreach (var map in _mapStorages) if(_mapStorages.ContainsKey(ind)) return _mapStorages[ind];
{
if(map.Key == ind) return map.Value;
}
return null; return null;
} }
} }