From 6550575acd24e3c9ae25d1b07b2c54519b80189e Mon Sep 17 00:00:00 2001 From: Danil Kargin Date: Wed, 2 Nov 2022 22:00:08 +0400 Subject: [PATCH 1/3] =?UTF-8?q?=D0=AD=D1=82=D0=B0=D0=BF=201.=20=D0=A1?= =?UTF-8?q?=D0=BC=D0=B5=D0=BD=D0=B0=20=D0=BC=D0=B0=D1=81=D1=81=D0=B8=D0=B2?= =?UTF-8?q?=D0=B0=20=D0=BD=D0=B0=20=D1=81=D0=BF=D0=B8=D1=81=D0=BE=D0=BA.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../MapWithSetAirFightersGeneric.cs | 25 ++-- .../AirFighter/SetAirFightersGeneric.cs | 124 ++++++++---------- 2 files changed, 67 insertions(+), 82 deletions(-) diff --git a/AirFighter/AirFighter/MapWithSetAirFightersGeneric.cs b/AirFighter/AirFighter/MapWithSetAirFightersGeneric.cs index 3f8684e..3fc878b 100644 --- a/AirFighter/AirFighter/MapWithSetAirFightersGeneric.cs +++ b/AirFighter/AirFighter/MapWithSetAirFightersGeneric.cs @@ -94,13 +94,9 @@ namespace AirFighter public Bitmap ShowOnMap() { Shaking(); - for (int i = 0; i < _setAirFighters.Count; i++) + foreach (var airFighter in _setAirFighters.GetAirFighters()) { - var airFighter = _setAirFighters.Get(i); - if (airFighter != null) - { - return _map.CreateMap(_pictureWidth, _pictureHeight, airFighter); - } + return _map.CreateMap(_pictureWidth, _pictureHeight, airFighter); } return new(_pictureWidth, _pictureHeight); } @@ -125,11 +121,11 @@ namespace AirFighter int j = _setAirFighters.Count - 1; for (int i = 0; i < _setAirFighters.Count; i++) { - if (_setAirFighters.Get(i) == null) + if (_setAirFighters[i] == null) { for (; j > i; j--) { - var airFighter = _setAirFighters.Get(j); + var airFighter = _setAirFighters[j]; if (airFighter != null) { _setAirFighters.Insert(airFighter, i); @@ -179,22 +175,23 @@ namespace AirFighter int currentWidth = _pictureWidth / _placeSizeWidth - 1; int currentHeight = _pictureHeight / _placeSizeHeight - 1; - for (int i = 0; i < _setAirFighters.Count; i++) + foreach (var airFighter in _setAirFighters.GetAirFighters()) { - _setAirFighters.Get(i)?.SetObject(currentWidth * _placeSizeWidth + 50, currentHeight * _placeSizeHeight + 10, _pictureWidth, _pictureHeight); - _setAirFighters.Get(i)?.DrawningObject(g); + airFighter?.SetObject(currentWidth * _placeSizeWidth + 50, currentHeight * _placeSizeHeight + 10, _pictureWidth, _pictureHeight); + airFighter?.DrawningObject(g); - if(currentWidth > 0) + if (currentWidth > 0) { currentWidth -= 1; } else { - if(currentHeight > 0) + if (currentHeight > 0) { currentHeight -= 1; currentWidth = _pictureWidth / _placeSizeWidth - 1; - }else return; + } + else return; } } } diff --git a/AirFighter/AirFighter/SetAirFightersGeneric.cs b/AirFighter/AirFighter/SetAirFightersGeneric.cs index 4496e04..2800138 100644 --- a/AirFighter/AirFighter/SetAirFightersGeneric.cs +++ b/AirFighter/AirFighter/SetAirFightersGeneric.cs @@ -15,40 +15,25 @@ namespace AirFighter where T: class { /// - /// Массив объектов, которые храним + /// Список объектов, которые храним /// - private readonly T[] _places; + private readonly List _places; /// - /// Количество объектов в массиве + /// Количество объектов в списке /// - public int Count => _places.Length; + public int Count => _places.Count; + /// + /// Максимальная длина списка + /// + private readonly int _maxCount; /// /// Конструктор /// /// public SetAirFightersGeneric(int count) { - _places = new T[count]; - } - /// - /// Проверка на наличие пустых мест - /// - /// - /// - private bool CheckNullPosition(int firstIndex) - { - if(firstIndex >= _places.Length && firstIndex < 0) - { - return false; - } - for(int i = firstIndex; i < _places.Length; i++) - { - if(_places[i] == null) - { - return true; - } - } - return false; + _places = new List(); + _maxCount = count; } /// /// Добавление объекта в набор @@ -67,40 +52,15 @@ namespace AirFighter /// public int Insert(T airFighter, int position) { - if(airFighter == null) + if (position < 0 && position > _maxCount) { return -1; } - if (_places[position] == null) - { - _places[position] = airFighter; - } else { - if(CheckNullPosition(position + 1)) - { - T tempMain = airFighter; - for (int i = position; i < _places.Length; i++) - { - if (_places[i] == null) - { - _places[i] = tempMain; - break; - } - else - { - T temp2 = _places[i]; - _places[i] = tempMain; - tempMain = temp2; - } - } - } - else - { - return -1; - } + _places.Insert(position, airFighter); + return position; } - return position; } /// /// Удаление объекта из набора с конкретной позиции @@ -109,28 +69,56 @@ namespace AirFighter /// public T Remove(int position) { - if (position >= 0 && position < _places.Length && _places[position] != null) + if (position >= 0 && position < _maxCount && _places[position] != null) { T temp = _places[position]; - _places[position] = null; + _places.RemoveAt(position); return temp; } else return null; - } - /// - /// Получение объекта из набора по позиции - /// - /// - /// - public T Get(int position) - { - if (_places[position] != null) + } + /// + /// Получение объекта из набора по позиции + /// + /// + /// + public T this[int position] + { + get { - return _places[position]; + if (position >= 0 && position < _maxCount) + { + return _places[position]; + } + else + return null; } - else - return null; + set + { + if (position >= 0 && position < _maxCount) + { + _places.Insert(position, value); + } + } + } + /// + /// Проход по набору до первого пустого + /// + /// + public IEnumerable GetAirFighters() + { + foreach (var airFighter in _places) + { + if (airFighter != null) + { + yield return airFighter; + } + else + { + yield break; + } + } + } } } -} -- 2.25.1 From 66cf50c25a23011ae649665713bbc569c9e41131 Mon Sep 17 00:00:00 2001 From: Danil Kargin Date: Wed, 2 Nov 2022 22:04:03 +0400 Subject: [PATCH 2/3] =?UTF-8?q?=D0=AD=D1=82=D0=B0=D0=BF=202.=20=D0=9A?= =?UTF-8?q?=D0=BB=D0=B0=D1=81=D1=81=20MapsCollection?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AirFighter/AirFighter/MapsCollection.cs | 83 +++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 AirFighter/AirFighter/MapsCollection.cs diff --git a/AirFighter/AirFighter/MapsCollection.cs b/AirFighter/AirFighter/MapsCollection.cs new file mode 100644 index 0000000..ff24c26 --- /dev/null +++ b/AirFighter/AirFighter/MapsCollection.cs @@ -0,0 +1,83 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AirFighter +{ + /// + /// Класс для хранения коллекции карт + /// + internal class MapsCollection + { + /// + /// Словарь (хранилище) с картами + /// + readonly Dictionary> _mapStorages; + /// + /// Возвращение списка названий карт + /// + public List Keys => _mapStorages.Keys.ToList(); + /// + /// Ширина окна отрисовки + /// + private readonly int _pictureWidth; + /// + /// Высота окна отрисовки + /// + private readonly int _pictureHeight; + /// + /// Конструктор + /// + /// + /// + public MapsCollection(int pictureWidth, int pictureHeight) + { + _mapStorages = new Dictionary>(); + _pictureWidth = pictureWidth; + _pictureHeight = pictureHeight; + } + /// + /// Добавление карты + /// + /// Название карты + /// Карта + public void AddMap(string name, AbstractMap map) + { + if (!string.IsNullOrEmpty(name) && map != null && !Keys.Contains(name)) + { + _mapStorages.Add(name, new MapWithSetAirFightersGeneric(_pictureWidth, _pictureHeight, map)); + } + } + /// + /// Удаление карты + /// + /// Название карты + public void DelMap(string name) + { + if (!string.IsNullOrEmpty(name)) + { + _mapStorages.Remove(name); + } + } + /// + /// Доступ к парковке + /// + /// + /// + public MapWithSetAirFightersGeneric this[string ind] + { + get + { + MapWithSetAirFightersGeneric _map; + if (_mapStorages.TryGetValue(ind, out _map)) + { + return _map; + } + else + return null; + } + } + } +} -- 2.25.1 From 50baf3b05b3c63e70ca72336a2b565375d171e41 Mon Sep 17 00:00:00 2001 From: Danil Kargin Date: Wed, 2 Nov 2022 22:20:29 +0400 Subject: [PATCH 3/3] =?UTF-8?q?=D0=AD=D1=82=D0=B0=D0=BF=203.=20=D0=A4?= =?UTF-8?q?=D0=BE=D1=80=D0=BC=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../FormMapWithSetAirFighters.Designer.cs | 99 +++++++++++--- .../AirFighter/FormMapWithSetAirFighters.cs | 124 +++++++++++++----- 2 files changed, 175 insertions(+), 48 deletions(-) diff --git a/AirFighter/AirFighter/FormMapWithSetAirFighters.Designer.cs b/AirFighter/AirFighter/FormMapWithSetAirFighters.Designer.cs index d4ce11e..04e373c 100644 --- a/AirFighter/AirFighter/FormMapWithSetAirFighters.Designer.cs +++ b/AirFighter/AirFighter/FormMapWithSetAirFighters.Designer.cs @@ -40,12 +40,19 @@ this.buttonAddAirFighter = new System.Windows.Forms.Button(); this.comboBoxSelectorMap = new System.Windows.Forms.ComboBox(); this.pictureBox = new System.Windows.Forms.PictureBox(); + this.groupBoxMaps = new System.Windows.Forms.GroupBox(); + this.buttonDeleteMap = new System.Windows.Forms.Button(); + this.listBoxMaps = new System.Windows.Forms.ListBox(); + this.buttonAddMap = new System.Windows.Forms.Button(); + this.textBoxNewMapName = new System.Windows.Forms.TextBox(); this.groupBox.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit(); + this.groupBoxMaps.SuspendLayout(); this.SuspendLayout(); // // groupBox // + this.groupBox.Controls.Add(this.groupBoxMaps); this.groupBox.Controls.Add(this.buttonUp); this.groupBox.Controls.Add(this.buttonDown); this.groupBox.Controls.Add(this.buttonLeft); @@ -55,20 +62,20 @@ this.groupBox.Controls.Add(this.buttonRemoveAirFighter); this.groupBox.Controls.Add(this.maskedTextBoxPosition); this.groupBox.Controls.Add(this.buttonAddAirFighter); - this.groupBox.Controls.Add(this.comboBoxSelectorMap); this.groupBox.Dock = System.Windows.Forms.DockStyle.Right; - this.groupBox.Location = new System.Drawing.Point(550, 0); + this.groupBox.Location = new System.Drawing.Point(732, 0); this.groupBox.Name = "groupBox"; - this.groupBox.Size = new System.Drawing.Size(250, 450); + this.groupBox.Size = new System.Drawing.Size(250, 555); this.groupBox.TabIndex = 0; this.groupBox.TabStop = false; this.groupBox.Text = "Инструменты"; // // buttonUp // + this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonUp.BackgroundImage = global::AirFighter.Properties.Resources.Up; this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonUp.Location = new System.Drawing.Point(135, 370); + this.buttonUp.Location = new System.Drawing.Point(135, 475); this.buttonUp.Name = "buttonUp"; this.buttonUp.Size = new System.Drawing.Size(30, 30); this.buttonUp.TabIndex = 2; @@ -77,9 +84,10 @@ // // buttonDown // + this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonDown.BackgroundImage = global::AirFighter.Properties.Resources.Down; this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonDown.Location = new System.Drawing.Point(135, 408); + this.buttonDown.Location = new System.Drawing.Point(135, 513); this.buttonDown.Name = "buttonDown"; this.buttonDown.Size = new System.Drawing.Size(30, 30); this.buttonDown.TabIndex = 3; @@ -88,9 +96,10 @@ // // buttonLeft // + this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonLeft.BackgroundImage = global::AirFighter.Properties.Resources.Left; this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonLeft.Location = new System.Drawing.Point(99, 408); + this.buttonLeft.Location = new System.Drawing.Point(99, 513); this.buttonLeft.Name = "buttonLeft"; this.buttonLeft.Size = new System.Drawing.Size(30, 30); this.buttonLeft.TabIndex = 4; @@ -99,9 +108,10 @@ // // buttonRight // + this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonRight.BackgroundImage = global::AirFighter.Properties.Resources.Right; this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonRight.Location = new System.Drawing.Point(171, 408); + this.buttonRight.Location = new System.Drawing.Point(171, 513); this.buttonRight.Name = "buttonRight"; this.buttonRight.Size = new System.Drawing.Size(30, 30); this.buttonRight.TabIndex = 5; @@ -110,7 +120,7 @@ // // buttonShowOnMap // - this.buttonShowOnMap.Location = new System.Drawing.Point(28, 296); + this.buttonShowOnMap.Location = new System.Drawing.Point(28, 443); this.buttonShowOnMap.Name = "buttonShowOnMap"; this.buttonShowOnMap.Size = new System.Drawing.Size(210, 29); this.buttonShowOnMap.TabIndex = 5; @@ -120,7 +130,7 @@ // // buttonShowStorage // - this.buttonShowStorage.Location = new System.Drawing.Point(28, 252); + this.buttonShowStorage.Location = new System.Drawing.Point(28, 408); this.buttonShowStorage.Name = "buttonShowStorage"; this.buttonShowStorage.Size = new System.Drawing.Size(210, 29); this.buttonShowStorage.TabIndex = 4; @@ -130,7 +140,7 @@ // // buttonRemoveAirFighter // - this.buttonRemoveAirFighter.Location = new System.Drawing.Point(28, 194); + this.buttonRemoveAirFighter.Location = new System.Drawing.Point(28, 373); this.buttonRemoveAirFighter.Name = "buttonRemoveAirFighter"; this.buttonRemoveAirFighter.Size = new System.Drawing.Size(210, 29); this.buttonRemoveAirFighter.TabIndex = 3; @@ -140,7 +150,7 @@ // // maskedTextBoxPosition // - this.maskedTextBoxPosition.Location = new System.Drawing.Point(28, 145); + this.maskedTextBoxPosition.Location = new System.Drawing.Point(28, 340); this.maskedTextBoxPosition.Mask = "00"; this.maskedTextBoxPosition.Name = "maskedTextBoxPosition"; this.maskedTextBoxPosition.Size = new System.Drawing.Size(210, 27); @@ -148,7 +158,7 @@ // // buttonAddAirFighter // - this.buttonAddAirFighter.Location = new System.Drawing.Point(28, 96); + this.buttonAddAirFighter.Location = new System.Drawing.Point(28, 296); this.buttonAddAirFighter.Name = "buttonAddAirFighter"; this.buttonAddAirFighter.Size = new System.Drawing.Size(210, 29); this.buttonAddAirFighter.TabIndex = 1; @@ -163,26 +173,76 @@ this.comboBoxSelectorMap.Items.AddRange(new object[] { "Простая карта", "Карта шторма"}); - this.comboBoxSelectorMap.Location = new System.Drawing.Point(28, 45); + this.comboBoxSelectorMap.Location = new System.Drawing.Point(17, 75); this.comboBoxSelectorMap.Name = "comboBoxSelectorMap"; this.comboBoxSelectorMap.Size = new System.Drawing.Size(210, 28); this.comboBoxSelectorMap.TabIndex = 0; - this.comboBoxSelectorMap.SelectedIndexChanged += new System.EventHandler(this.ComboBoxSelectorMap_SelectedIndexChanged); // // pictureBox // this.pictureBox.Dock = System.Windows.Forms.DockStyle.Fill; this.pictureBox.Location = new System.Drawing.Point(0, 0); this.pictureBox.Name = "pictureBox"; - this.pictureBox.Size = new System.Drawing.Size(550, 450); + this.pictureBox.Size = new System.Drawing.Size(732, 555); this.pictureBox.TabIndex = 1; this.pictureBox.TabStop = false; // + // groupBoxMaps + // + this.groupBoxMaps.Controls.Add(this.buttonDeleteMap); + this.groupBoxMaps.Controls.Add(this.listBoxMaps); + this.groupBoxMaps.Controls.Add(this.buttonAddMap); + this.groupBoxMaps.Controls.Add(this.textBoxNewMapName); + this.groupBoxMaps.Controls.Add(this.comboBoxSelectorMap); + this.groupBoxMaps.Location = new System.Drawing.Point(0, 12); + this.groupBoxMaps.Name = "groupBoxMaps"; + this.groupBoxMaps.Size = new System.Drawing.Size(250, 278); + this.groupBoxMaps.TabIndex = 6; + this.groupBoxMaps.TabStop = false; + this.groupBoxMaps.Text = "Карты"; + // + // buttonDeleteMap + // + this.buttonDeleteMap.Location = new System.Drawing.Point(17, 244); + this.buttonDeleteMap.Name = "buttonDeleteMap"; + this.buttonDeleteMap.Size = new System.Drawing.Size(210, 29); + this.buttonDeleteMap.TabIndex = 4; + this.buttonDeleteMap.Text = "Удалить карту"; + this.buttonDeleteMap.UseVisualStyleBackColor = true; + this.buttonDeleteMap.Click += new System.EventHandler(this.ButtonDeleteMap_Click); + // + // listBoxMaps + // + this.listBoxMaps.FormattingEnabled = true; + this.listBoxMaps.ItemHeight = 20; + this.listBoxMaps.Location = new System.Drawing.Point(17, 154); + this.listBoxMaps.Name = "listBoxMaps"; + this.listBoxMaps.Size = new System.Drawing.Size(210, 84); + this.listBoxMaps.TabIndex = 3; + this.listBoxMaps.SelectedIndexChanged += new System.EventHandler(this.ListBoxMaps_SelectedIndexChanged); + // + // buttonAddMap + // + this.buttonAddMap.Location = new System.Drawing.Point(17, 119); + this.buttonAddMap.Name = "buttonAddMap"; + this.buttonAddMap.Size = new System.Drawing.Size(210, 29); + this.buttonAddMap.TabIndex = 2; + this.buttonAddMap.Text = "Добавить карту"; + this.buttonAddMap.UseVisualStyleBackColor = true; + this.buttonAddMap.Click += new System.EventHandler(this.ButtonAddMap_Click); + // + // textBoxNewMapName + // + this.textBoxNewMapName.Location = new System.Drawing.Point(17, 36); + this.textBoxNewMapName.Name = "textBoxNewMapName"; + this.textBoxNewMapName.Size = new System.Drawing.Size(210, 27); + this.textBoxNewMapName.TabIndex = 1; + // // FormMapWithSetAirFighters // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(800, 450); + this.ClientSize = new System.Drawing.Size(982, 555); this.Controls.Add(this.pictureBox); this.Controls.Add(this.groupBox); this.Name = "FormMapWithSetAirFighters"; @@ -190,6 +250,8 @@ this.groupBox.ResumeLayout(false); this.groupBox.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit(); + this.groupBoxMaps.ResumeLayout(false); + this.groupBoxMaps.PerformLayout(); this.ResumeLayout(false); } @@ -208,5 +270,10 @@ private Button buttonDown; private Button buttonLeft; private Button buttonRight; + private GroupBox groupBoxMaps; + private Button buttonDeleteMap; + private ListBox listBoxMaps; + private Button buttonAddMap; + private TextBox textBoxNewMapName; } } \ No newline at end of file diff --git a/AirFighter/AirFighter/FormMapWithSetAirFighters.cs b/AirFighter/AirFighter/FormMapWithSetAirFighters.cs index 0694cf9..78e5763 100644 --- a/AirFighter/AirFighter/FormMapWithSetAirFighters.cs +++ b/AirFighter/AirFighter/FormMapWithSetAirFighters.cs @@ -14,42 +14,49 @@ namespace AirFighter public partial class FormMapWithSetAirFighters : Form { /// - /// Объект от класса карты с набором объектов + /// Словарь для выпадающего списка /// - private MapWithSetAirFightersGeneric _mapAirFightersCollectionGeneric; + private readonly Dictionary _mapsDict = new() + { + { "Простая карта", new SimpleMap() }, + { "Карта шторма", new StormMap() } + }; + /// + /// Объект от коллекции карт + /// + private readonly MapsCollection _mapsCollection; /// /// Конструктор public FormMapWithSetAirFighters() { InitializeComponent(); + _mapsCollection = new MapsCollection(pictureBox.Width, pictureBox.Height); + comboBoxSelectorMap.Items.Clear(); + foreach (var elem in _mapsDict) + { + comboBoxSelectorMap.Items.Add(elem.Key); + } } /// - /// Выбор карты + /// Заполнение listBoxMaps /// - /// - /// - private void ComboBoxSelectorMap_SelectedIndexChanged(object sender, - EventArgs e) + private void ReloadMaps() { - AbstractMap map = null; - switch (comboBoxSelectorMap.Text) + int index = listBoxMaps.SelectedIndex; + listBoxMaps.Items.Clear(); + for (int i = 0; i < _mapsCollection.Keys.Count; i++) { - case "Простая карта": - map = new SimpleMap(); - break; - case "Карта шторма": - map = new StormMap(); - break; + listBoxMaps.Items.Add(_mapsCollection.Keys[i]); } - if (map != null) + if (listBoxMaps.Items.Count > 0 && (index == -1 || index >= + listBoxMaps.Items.Count)) { - _mapAirFightersCollectionGeneric = new - MapWithSetAirFightersGeneric( - pictureBox.Width, pictureBox.Height, map); + listBoxMaps.SelectedIndex = 0; } - else + else if (listBoxMaps.Items.Count > 0 && index > -1 && index < + listBoxMaps.Items.Count) { - _mapAirFightersCollectionGeneric = null; + listBoxMaps.SelectedIndex = index; } } /// @@ -59,7 +66,7 @@ namespace AirFighter /// private void ButtonAddAirFighter_Click(object sender, EventArgs e) { - if (_mapAirFightersCollectionGeneric == null) + if (listBoxMaps.SelectedIndex == -1) { return; } @@ -67,10 +74,10 @@ namespace AirFighter if (form.ShowDialog() == DialogResult.OK) { DrawningObjectAirFighter airFighter = new(form.SelectedAirFighter); - if ((_mapAirFightersCollectionGeneric + airFighter) == 0) + if ((_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + airFighter) == 0) { MessageBox.Show("Объект добавлен"); - pictureBox.Image = _mapAirFightersCollectionGeneric.ShowSet(); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } else { @@ -85,6 +92,10 @@ namespace AirFighter /// private void ButtonRemoveAirFighter_Click(object sender, EventArgs e) { + if (listBoxMaps.SelectedIndex == -1) + { + return; + } if (string.IsNullOrEmpty(maskedTextBoxPosition.Text)) { return; @@ -95,10 +106,10 @@ namespace AirFighter return; } int pos = Convert.ToInt32(maskedTextBoxPosition.Text); - if ((_mapAirFightersCollectionGeneric - pos) != null) + if ((_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] - pos) != null) { MessageBox.Show("Объект удален"); - pictureBox.Image = _mapAirFightersCollectionGeneric.ShowSet(); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } else { @@ -112,11 +123,11 @@ namespace AirFighter /// private void ButtonShowStorage_Click(object sender, EventArgs e) { - if (_mapAirFightersCollectionGeneric == null) + if (listBoxMaps.SelectedIndex == -1) { return; } - pictureBox.Image = _mapAirFightersCollectionGeneric.ShowSet(); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } /// /// Вывод карты @@ -125,11 +136,11 @@ namespace AirFighter /// private void ButtonShowOnMap_Click(object sender, EventArgs e) { - if (_mapAirFightersCollectionGeneric == null) + if (listBoxMaps.SelectedIndex == -1) { return; } - pictureBox.Image = _mapAirFightersCollectionGeneric.ShowOnMap(); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowOnMap(); } /// /// Перемещение @@ -138,7 +149,7 @@ namespace AirFighter /// private void ButtonMove_Click(object sender, EventArgs e) { - if (_mapAirFightersCollectionGeneric == null) + if (listBoxMaps.SelectedIndex == -1) { return; } @@ -160,7 +171,56 @@ namespace AirFighter dir = Direction.Right; break; } - pictureBox.Image = _mapAirFightersCollectionGeneric.MoveObject(dir); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].MoveObject(dir); + } + /// + /// Добавление карты + /// + /// + /// + 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(); + } } } } -- 2.25.1