diff --git a/ContainerShip/ContainerShip/FormMapWithSetShips.Designer.cs b/ContainerShip/ContainerShip/FormMapWithSetShips.Designer.cs index 4e94da7..bce3b84 100644 --- a/ContainerShip/ContainerShip/FormMapWithSetShips.Designer.cs +++ b/ContainerShip/ContainerShip/FormMapWithSetShips.Designer.cs @@ -29,8 +29,13 @@ private void InitializeComponent() { this.groupBox1 = new System.Windows.Forms.GroupBox(); - this.ButtonAddShip = new System.Windows.Forms.Button(); + this.groupBoxMaps = new System.Windows.Forms.GroupBox(); + this.ButtonDeleteMap = new System.Windows.Forms.Button(); + this.listBoxMaps = new System.Windows.Forms.ListBox(); + this.textBoxNewMapName = new System.Windows.Forms.TextBox(); + this.ButtonAddMap = new System.Windows.Forms.Button(); this.ComboBoxSelectorMap = new System.Windows.Forms.ComboBox(); + this.ButtonAddShip = new System.Windows.Forms.Button(); this.buttonUp = new System.Windows.Forms.Button(); this.buttonRight = new System.Windows.Forms.Button(); this.buttonDown = new System.Windows.Forms.Button(); @@ -41,13 +46,14 @@ this.maskedTextBoxPosition = new System.Windows.Forms.MaskedTextBox(); this.pictureBox = new System.Windows.Forms.PictureBox(); this.groupBox1.SuspendLayout(); + this.groupBoxMaps.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit(); this.SuspendLayout(); // // groupBox1 // + this.groupBox1.Controls.Add(this.groupBoxMaps); this.groupBox1.Controls.Add(this.ButtonAddShip); - this.groupBox1.Controls.Add(this.ComboBoxSelectorMap); this.groupBox1.Controls.Add(this.buttonUp); this.groupBox1.Controls.Add(this.buttonRight); this.groupBox1.Controls.Add(this.buttonDown); @@ -59,20 +65,61 @@ this.groupBox1.Dock = System.Windows.Forms.DockStyle.Right; this.groupBox1.Location = new System.Drawing.Point(553, 0); this.groupBox1.Name = "groupBox1"; - this.groupBox1.Size = new System.Drawing.Size(300, 508); + this.groupBox1.Size = new System.Drawing.Size(300, 687); this.groupBox1.TabIndex = 0; this.groupBox1.TabStop = false; this.groupBox1.Text = "Инструменты"; // - // ButtonAddShip + // groupBoxMaps // - this.ButtonAddShip.Location = new System.Drawing.Point(6, 74); - this.ButtonAddShip.Name = "ButtonAddShip"; - this.ButtonAddShip.Size = new System.Drawing.Size(288, 34); - this.ButtonAddShip.TabIndex = 11; - this.ButtonAddShip.Text = "Добавить корабль"; - this.ButtonAddShip.UseVisualStyleBackColor = true; - this.ButtonAddShip.Click += new System.EventHandler(this.ButtonAddShip_Click); + this.groupBoxMaps.Controls.Add(this.ButtonDeleteMap); + this.groupBoxMaps.Controls.Add(this.listBoxMaps); + this.groupBoxMaps.Controls.Add(this.textBoxNewMapName); + this.groupBoxMaps.Controls.Add(this.ButtonAddMap); + this.groupBoxMaps.Controls.Add(this.ComboBoxSelectorMap); + this.groupBoxMaps.Location = new System.Drawing.Point(10, 32); + this.groupBoxMaps.Name = "groupBoxMaps"; + this.groupBoxMaps.Size = new System.Drawing.Size(284, 327); + this.groupBoxMaps.TabIndex = 12; + this.groupBoxMaps.TabStop = false; + this.groupBoxMaps.Text = "Карты"; + // + // ButtonDeleteMap + // + this.ButtonDeleteMap.Location = new System.Drawing.Point(6, 277); + this.ButtonDeleteMap.Name = "ButtonDeleteMap"; + this.ButtonDeleteMap.Size = new System.Drawing.Size(272, 44); + this.ButtonDeleteMap.TabIndex = 15; + this.ButtonDeleteMap.Text = "Удалить карту"; + this.ButtonDeleteMap.UseVisualStyleBackColor = true; + this.ButtonDeleteMap.Click += new System.EventHandler(this.ButtonDeleteMap_Click); + // + // listBoxMaps + // + this.listBoxMaps.FormattingEnabled = true; + this.listBoxMaps.ItemHeight = 25; + this.listBoxMaps.Location = new System.Drawing.Point(6, 167); + this.listBoxMaps.Name = "listBoxMaps"; + this.listBoxMaps.Size = new System.Drawing.Size(272, 104); + this.listBoxMaps.TabIndex = 14; + this.listBoxMaps.SelectedIndexChanged += new System.EventHandler(this.ListBoxMaps_SelectedIndexChanged); + // + // textBoxNewMapName + // + this.textBoxNewMapName.Location = new System.Drawing.Point(6, 39); + this.textBoxNewMapName.Name = "textBoxNewMapName"; + this.textBoxNewMapName.Size = new System.Drawing.Size(272, 31); + this.textBoxNewMapName.TabIndex = 13; + // + // ButtonAddMap + // + this.ButtonAddMap.Location = new System.Drawing.Point(6, 115); + this.ButtonAddMap.Name = "ButtonAddMap"; + this.ButtonAddMap.Size = new System.Drawing.Size(272, 44); + this.ButtonAddMap.TabIndex = 12; + this.ButtonAddMap.Text = "Добавить карту"; + this.ButtonAddMap.UseVisualStyleBackColor = true; + this.ButtonAddMap.Click += new System.EventHandler(this.ButtonAddMap_Click); // // ComboBoxSelectorMap // @@ -80,18 +127,27 @@ this.ComboBoxSelectorMap.Items.AddRange(new object[] { "Простая карта", "Модифицированная карта"}); - this.ComboBoxSelectorMap.Location = new System.Drawing.Point(6, 35); + this.ComboBoxSelectorMap.Location = new System.Drawing.Point(6, 76); this.ComboBoxSelectorMap.Name = "ComboBoxSelectorMap"; - this.ComboBoxSelectorMap.Size = new System.Drawing.Size(288, 33); + this.ComboBoxSelectorMap.Size = new System.Drawing.Size(272, 33); this.ComboBoxSelectorMap.TabIndex = 10; - this.ComboBoxSelectorMap.SelectedIndexChanged += new System.EventHandler(this.ComboBoxSelectorMap_SelectedIndexChanged); + // + // ButtonAddShip + // + this.ButtonAddShip.Location = new System.Drawing.Point(6, 365); + this.ButtonAddShip.Name = "ButtonAddShip"; + this.ButtonAddShip.Size = new System.Drawing.Size(288, 34); + this.ButtonAddShip.TabIndex = 11; + this.ButtonAddShip.Text = "Добавить корабль"; + this.ButtonAddShip.UseVisualStyleBackColor = true; + this.ButtonAddShip.Click += new System.EventHandler(this.ButtonAddShip_Click); // // buttonUp // this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonUp.BackgroundImage = global::ContainerShip.Properties.Resources.upArrow; this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonUp.Location = new System.Drawing.Point(131, 390); + this.buttonUp.Location = new System.Drawing.Point(131, 569); this.buttonUp.Name = "buttonUp"; this.buttonUp.Size = new System.Drawing.Size(50, 50); this.buttonUp.TabIndex = 9; @@ -103,7 +159,7 @@ this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonRight.BackgroundImage = global::ContainerShip.Properties.Resources.LeftArrow; this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonRight.Location = new System.Drawing.Point(187, 446); + this.buttonRight.Location = new System.Drawing.Point(187, 625); this.buttonRight.Name = "buttonRight"; this.buttonRight.Size = new System.Drawing.Size(50, 50); this.buttonRight.TabIndex = 8; @@ -115,7 +171,7 @@ this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonDown.BackgroundImage = global::ContainerShip.Properties.Resources.DownArrow; this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonDown.Location = new System.Drawing.Point(131, 446); + this.buttonDown.Location = new System.Drawing.Point(131, 625); this.buttonDown.Name = "buttonDown"; this.buttonDown.Size = new System.Drawing.Size(50, 50); this.buttonDown.TabIndex = 7; @@ -127,7 +183,7 @@ this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonLeft.BackgroundImage = global::ContainerShip.Properties.Resources.RightArrow; this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonLeft.Location = new System.Drawing.Point(75, 446); + this.buttonLeft.Location = new System.Drawing.Point(75, 625); this.buttonLeft.Name = "buttonLeft"; this.buttonLeft.Size = new System.Drawing.Size(50, 50); this.buttonLeft.TabIndex = 6; @@ -136,9 +192,9 @@ // // button3 // - this.button3.Location = new System.Drawing.Point(8, 292); + this.button3.Location = new System.Drawing.Point(6, 529); this.button3.Name = "button3"; - this.button3.Size = new System.Drawing.Size(286, 34); + this.button3.Size = new System.Drawing.Size(288, 34); this.button3.TabIndex = 3; this.button3.Text = "Посмотреть карту"; this.button3.UseVisualStyleBackColor = true; @@ -146,7 +202,7 @@ // // ButtonShowStorage // - this.ButtonShowStorage.Location = new System.Drawing.Point(6, 243); + this.ButtonShowStorage.Location = new System.Drawing.Point(6, 489); this.ButtonShowStorage.Name = "ButtonShowStorage"; this.ButtonShowStorage.Size = new System.Drawing.Size(288, 34); this.ButtonShowStorage.TabIndex = 2; @@ -156,7 +212,7 @@ // // ButtonRemoveShip // - this.ButtonRemoveShip.Location = new System.Drawing.Point(6, 162); + this.ButtonRemoveShip.Location = new System.Drawing.Point(6, 442); this.ButtonRemoveShip.Name = "ButtonRemoveShip"; this.ButtonRemoveShip.Size = new System.Drawing.Size(288, 41); this.ButtonRemoveShip.TabIndex = 1; @@ -166,7 +222,7 @@ // // maskedTextBoxPosition // - this.maskedTextBoxPosition.Location = new System.Drawing.Point(6, 125); + this.maskedTextBoxPosition.Location = new System.Drawing.Point(6, 405); this.maskedTextBoxPosition.Mask = "00"; this.maskedTextBoxPosition.Name = "maskedTextBoxPosition"; this.maskedTextBoxPosition.Size = new System.Drawing.Size(288, 31); @@ -177,7 +233,7 @@ 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(553, 508); + this.pictureBox.Size = new System.Drawing.Size(553, 687); this.pictureBox.TabIndex = 1; this.pictureBox.TabStop = false; // @@ -185,13 +241,15 @@ // this.AutoScaleDimensions = new System.Drawing.SizeF(10F, 25F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(853, 508); + this.ClientSize = new System.Drawing.Size(853, 687); this.Controls.Add(this.pictureBox); this.Controls.Add(this.groupBox1); this.Name = "FormMapWithSetShips"; this.Text = "Карта с набором объектов"; this.groupBox1.ResumeLayout(false); this.groupBox1.PerformLayout(); + this.groupBoxMaps.ResumeLayout(false); + this.groupBoxMaps.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit(); this.ResumeLayout(false); @@ -211,5 +269,10 @@ private Button buttonRight; private Button ButtonAddShip; private ComboBox ComboBoxSelectorMap; + private ListBox listBoxMaps; + private TextBox textBoxNewMapName; + private Button ButtonAddMap; + private GroupBox groupBoxMaps; + private Button ButtonDeleteMap; } } \ No newline at end of file diff --git a/ContainerShip/ContainerShip/FormMapWithSetShips.cs b/ContainerShip/ContainerShip/FormMapWithSetShips.cs index faa900b..e04780f 100644 --- a/ContainerShip/ContainerShip/FormMapWithSetShips.cs +++ b/ContainerShip/ContainerShip/FormMapWithSetShips.cs @@ -13,46 +13,60 @@ namespace ContainerShip { public partial class FormMapWithSetShips : Form { + /// + /// Словарь для выпадающего списка + /// + private readonly Dictionary _mapsDict = new() + { + { "Простая карта", new SimpleMap() } + }; /// /// Объект от класса карты с набором объектов /// - private MapWithSetShipsGeneric _mapShipsCollectionGeneric; + private readonly MapsCollection _mapsCollection; /// /// Конструктор /// public FormMapWithSetShips() { InitializeComponent(); + _mapsCollection = new MapsCollection(pictureBox.Width, pictureBox.Height); + ComboBoxSelectorMap.Items.Clear(); + foreach (var elem in _mapsDict) + { + ComboBoxSelectorMap.Items.Add(elem.Key); + } + } + /// + /// Заполнение listBoxMaps + /// + 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]); + } + + 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 ComboBoxSelectorMap_SelectedIndexChanged(object sender, EventArgs e) - { - AbstractMap map = null; - switch (ComboBoxSelectorMap.Text) - { - case "Простая карта": - map = new SimpleMap(); - break; - case "Модифицированная карта": - map = new ModifyMap(); - break; - } - if (map != null) - { - _mapShipsCollectionGeneric = new MapWithSetShipsGeneric(pictureBox.Width, pictureBox.Height, map); - } - else -{ - _mapShipsCollectionGeneric = null; -} - } /// /// private void ButtonAddShip_Click(object sender, EventArgs e) { - if (_mapShipsCollectionGeneric == null) + if (listBoxMaps.SelectedIndex == -1) { return; } @@ -60,10 +74,10 @@ namespace ContainerShip if (form.ShowDialog() == DialogResult.OK) { DrawingObjectShip ship = new(form.SelectedShip); - if (_mapShipsCollectionGeneric + ship != -1) + if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + ship != -1) { MessageBox.Show("Объект добавлен"); - pictureBox.Image = _mapShipsCollectionGeneric.ShowSet(); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } else { @@ -78,6 +92,10 @@ namespace ContainerShip /// private void ButtonRemoveShip_Click(object sender, EventArgs e) { + if (listBoxMaps.SelectedIndex == -1) + { + return; + } if (string.IsNullOrEmpty(maskedTextBoxPosition.Text)) { return; @@ -87,10 +105,10 @@ namespace ContainerShip return; } int pos = Convert.ToInt32(maskedTextBoxPosition.Text); - if (_mapShipsCollectionGeneric - pos != null) + if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] - pos != null) { MessageBox.Show("Объект удален"); - pictureBox.Image = _mapShipsCollectionGeneric.ShowSet(); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } else { @@ -104,11 +122,11 @@ namespace ContainerShip /// private void ButtonShowStorage_Click(object sender, EventArgs e) { - if (_mapShipsCollectionGeneric == null) + if (listBoxMaps.SelectedIndex == -1) { return; } - pictureBox.Image = _mapShipsCollectionGeneric.ShowSet(); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } /// /// Вывод карты @@ -117,11 +135,11 @@ namespace ContainerShip /// private void ButtonShowOnMap_Click(object sender, EventArgs e) { - if (_mapShipsCollectionGeneric == null) + if (listBoxMaps.SelectedIndex == -1) { return; } - pictureBox.Image = _mapShipsCollectionGeneric.ShowOnMap(); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowOnMap(); } /// /// Перемещение @@ -130,7 +148,7 @@ namespace ContainerShip /// private void ButtonMove_Click(object sender, EventArgs e) { - if (_mapShipsCollectionGeneric == null) + if (listBoxMaps.SelectedIndex == -1) { return; } @@ -152,7 +170,50 @@ namespace ContainerShip dir = Direction.Right; break; } - pictureBox.Image = _mapShipsCollectionGeneric.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(); + } } } } diff --git a/ContainerShip/ContainerShip/MapsCollection.cs b/ContainerShip/ContainerShip/MapsCollection.cs index d1c9b62..aa31364 100644 --- a/ContainerShip/ContainerShip/MapsCollection.cs +++ b/ContainerShip/ContainerShip/MapsCollection.cs @@ -43,7 +43,7 @@ namespace ContainerShip public void AddMap(string name, AbstractMap map) { // TODO Прописать логику для добавления - if (_mapStorages.ContainsKey(name)) return; + if (_mapStorages.ContainsKey(name)) return; //уникальное имя else { _mapStorages.Add(name, new MapWithSetShipsGeneric(_pictureWidth, _pictureHeight, map)); diff --git a/ContainerShip/ContainerShip/SetShipsGeneric.cs b/ContainerShip/ContainerShip/SetShipsGeneric.cs index 04cbea5..cb778d4 100644 --- a/ContainerShip/ContainerShip/SetShipsGeneric.cs +++ b/ContainerShip/ContainerShip/SetShipsGeneric.cs @@ -56,12 +56,12 @@ namespace ContainerShip /// public T Remove(int position) { - if (position < 0 || position >= _places.Count) + if (position < 0 || position >= _maxCount) { return null; } T ship = _places[position]; - _places[position] = null; + _places.RemoveAt(position); return ship; } ///