diff --git a/WarmlyShip/WarmlyShip/FormMapWithSetWarmlyShip.Designer.cs b/WarmlyShip/WarmlyShip/FormMapWithSetWarmlyShip.Designer.cs index d059d9d..8816de6 100644 --- a/WarmlyShip/WarmlyShip/FormMapWithSetWarmlyShip.Designer.cs +++ b/WarmlyShip/WarmlyShip/FormMapWithSetWarmlyShip.Designer.cs @@ -29,56 +29,136 @@ private void InitializeComponent() { this.groupBox1 = new System.Windows.Forms.GroupBox(); + this.groupBoxMaps = new System.Windows.Forms.GroupBox(); + this.listBoxMaps = new System.Windows.Forms.ListBox(); + this.buttonDeleteMap = new System.Windows.Forms.Button(); + this.buttonAddMap = new System.Windows.Forms.Button(); + this.textBoxNewMapName = new System.Windows.Forms.TextBox(); + this.comboBoxSelectorMap = new System.Windows.Forms.ComboBox(); this.buttonDown = new System.Windows.Forms.Button(); + this.ButtonAddShip = new System.Windows.Forms.Button(); this.buttonUp = new System.Windows.Forms.Button(); this.buttonRight = new System.Windows.Forms.Button(); this.buttonLeft = new System.Windows.Forms.Button(); this.ButtonShowStorage = new System.Windows.Forms.Button(); this.ButtonShowOnMap = new System.Windows.Forms.Button(); this.maskedTextBoxPosition = new System.Windows.Forms.MaskedTextBox(); - this.comboBoxSelectorMap = new System.Windows.Forms.ComboBox(); this.ButtonRemoveShip = new System.Windows.Forms.Button(); - this.ButtonAddShip = new System.Windows.Forms.Button(); this.pictureBox1 = new System.Windows.Forms.PictureBox(); this.groupBox1.SuspendLayout(); + this.groupBoxMaps.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit(); this.SuspendLayout(); // // groupBox1 // + this.groupBox1.Controls.Add(this.groupBoxMaps); this.groupBox1.Controls.Add(this.buttonDown); + this.groupBox1.Controls.Add(this.ButtonAddShip); this.groupBox1.Controls.Add(this.buttonUp); this.groupBox1.Controls.Add(this.buttonRight); this.groupBox1.Controls.Add(this.buttonLeft); this.groupBox1.Controls.Add(this.ButtonShowStorage); this.groupBox1.Controls.Add(this.ButtonShowOnMap); this.groupBox1.Controls.Add(this.maskedTextBoxPosition); - this.groupBox1.Controls.Add(this.comboBoxSelectorMap); this.groupBox1.Controls.Add(this.ButtonRemoveShip); - this.groupBox1.Controls.Add(this.ButtonAddShip); this.groupBox1.Dock = System.Windows.Forms.DockStyle.Right; - this.groupBox1.Location = new System.Drawing.Point(600, 0); + this.groupBox1.Location = new System.Drawing.Point(593, 0); this.groupBox1.Name = "groupBox1"; - this.groupBox1.Size = new System.Drawing.Size(200, 450); + this.groupBox1.Size = new System.Drawing.Size(200, 582); this.groupBox1.TabIndex = 1; this.groupBox1.TabStop = false; + this.groupBox1.Text = "Инструменты"; + // + // groupBoxMaps + // + this.groupBoxMaps.Controls.Add(this.listBoxMaps); + this.groupBoxMaps.Controls.Add(this.buttonDeleteMap); + 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(6, 22); + this.groupBoxMaps.Name = "groupBoxMaps"; + this.groupBoxMaps.Size = new System.Drawing.Size(188, 209); + this.groupBoxMaps.TabIndex = 12; + this.groupBoxMaps.TabStop = false; + this.groupBoxMaps.Text = "Карты"; + // + // listBoxMaps + // + this.listBoxMaps.FormattingEnabled = true; + this.listBoxMaps.ItemHeight = 15; + this.listBoxMaps.Location = new System.Drawing.Point(6, 109); + this.listBoxMaps.Name = "listBoxMaps"; + this.listBoxMaps.Size = new System.Drawing.Size(176, 64); + this.listBoxMaps.TabIndex = 7; + // + // buttonDeleteMap + // + this.buttonDeleteMap.Location = new System.Drawing.Point(6, 180); + this.buttonDeleteMap.Name = "buttonDeleteMap"; + this.buttonDeleteMap.Size = new System.Drawing.Size(176, 23); + this.buttonDeleteMap.TabIndex = 6; + this.buttonDeleteMap.Text = "Удалить карту"; + this.buttonDeleteMap.UseVisualStyleBackColor = true; + this.buttonDeleteMap.Click += new System.EventHandler(this.ButtonDeleteMap_Click); + // + // buttonAddMap + // + this.buttonAddMap.Location = new System.Drawing.Point(6, 80); + this.buttonAddMap.Name = "buttonAddMap"; + this.buttonAddMap.Size = new System.Drawing.Size(176, 23); + this.buttonAddMap.TabIndex = 5; + this.buttonAddMap.Text = "Добавить карту"; + this.buttonAddMap.UseVisualStyleBackColor = true; + this.buttonAddMap.Click += new System.EventHandler(this.ButtonAddMap_Click); + // + // textBoxNewMapName + // + this.textBoxNewMapName.Location = new System.Drawing.Point(6, 22); + this.textBoxNewMapName.Name = "textBoxNewMapName"; + this.textBoxNewMapName.Size = new System.Drawing.Size(176, 23); + this.textBoxNewMapName.TabIndex = 0; + // + // comboBoxSelectorMap + // + this.comboBoxSelectorMap.FormattingEnabled = true; + this.comboBoxSelectorMap.Items.AddRange(new object[] { + "Простая карта", + "Море"}); + this.comboBoxSelectorMap.Location = new System.Drawing.Point(6, 51); + this.comboBoxSelectorMap.Name = "comboBoxSelectorMap"; + this.comboBoxSelectorMap.Size = new System.Drawing.Size(176, 23); + this.comboBoxSelectorMap.TabIndex = 4; // // buttonDown // + this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonDown.BackgroundImage = global::WarmlyShip.Properties.Resources.Down; this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonDown.Location = new System.Drawing.Point(96, 388); + this.buttonDown.Location = new System.Drawing.Point(96, 536); this.buttonDown.Name = "buttonDown"; this.buttonDown.Size = new System.Drawing.Size(30, 30); this.buttonDown.TabIndex = 11; this.buttonDown.UseVisualStyleBackColor = true; this.buttonDown.Click += new System.EventHandler(this.ButtonMove_Click); // + // ButtonAddShip + // + this.ButtonAddShip.Location = new System.Drawing.Point(12, 298); + this.ButtonAddShip.Name = "ButtonAddShip"; + this.ButtonAddShip.Size = new System.Drawing.Size(176, 25); + this.ButtonAddShip.TabIndex = 2; + 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::WarmlyShip.Properties.Resources.Up; this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonUp.Location = new System.Drawing.Point(96, 352); + this.buttonUp.Location = new System.Drawing.Point(96, 500); this.buttonUp.Name = "buttonUp"; this.buttonUp.Size = new System.Drawing.Size(30, 30); this.buttonUp.TabIndex = 10; @@ -87,9 +167,10 @@ // // buttonRight // + this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonRight.BackgroundImage = global::WarmlyShip.Properties.Resources.Right; this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonRight.Location = new System.Drawing.Point(132, 388); + this.buttonRight.Location = new System.Drawing.Point(132, 536); this.buttonRight.Name = "buttonRight"; this.buttonRight.Size = new System.Drawing.Size(30, 30); this.buttonRight.TabIndex = 9; @@ -98,9 +179,10 @@ // // buttonLeft // + this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonLeft.BackgroundImage = global::WarmlyShip.Properties.Resources.Left; this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonLeft.Location = new System.Drawing.Point(60, 388); + this.buttonLeft.Location = new System.Drawing.Point(60, 536); this.buttonLeft.Name = "buttonLeft"; this.buttonLeft.Size = new System.Drawing.Size(30, 30); this.buttonLeft.TabIndex = 8; @@ -109,9 +191,9 @@ // // ButtonShowStorage // - this.ButtonShowStorage.Location = new System.Drawing.Point(6, 193); + this.ButtonShowStorage.Location = new System.Drawing.Point(12, 402); this.ButtonShowStorage.Name = "ButtonShowStorage"; - this.ButtonShowStorage.Size = new System.Drawing.Size(182, 23); + this.ButtonShowStorage.Size = new System.Drawing.Size(176, 23); this.ButtonShowStorage.TabIndex = 7; this.ButtonShowStorage.Text = "Посмотреть хранилище"; this.ButtonShowStorage.UseVisualStyleBackColor = true; @@ -119,9 +201,9 @@ // // ButtonShowOnMap // - this.ButtonShowOnMap.Location = new System.Drawing.Point(6, 243); + this.ButtonShowOnMap.Location = new System.Drawing.Point(12, 431); this.ButtonShowOnMap.Name = "ButtonShowOnMap"; - this.ButtonShowOnMap.Size = new System.Drawing.Size(188, 23); + this.ButtonShowOnMap.Size = new System.Drawing.Size(176, 23); this.ButtonShowOnMap.TabIndex = 6; this.ButtonShowOnMap.Text = "Посмотреть карту"; this.ButtonShowOnMap.UseVisualStyleBackColor = true; @@ -129,50 +211,28 @@ // // maskedTextBoxPosition // - this.maskedTextBoxPosition.Location = new System.Drawing.Point(6, 108); + this.maskedTextBoxPosition.Location = new System.Drawing.Point(12, 329); this.maskedTextBoxPosition.Mask = "00"; this.maskedTextBoxPosition.Name = "maskedTextBoxPosition"; - this.maskedTextBoxPosition.Size = new System.Drawing.Size(188, 23); + this.maskedTextBoxPosition.Size = new System.Drawing.Size(176, 23); this.maskedTextBoxPosition.TabIndex = 5; // - // comboBoxSelectorMap - // - this.comboBoxSelectorMap.FormattingEnabled = true; - this.comboBoxSelectorMap.Items.AddRange(new object[] { - "Простая карта", - "Море"}); - this.comboBoxSelectorMap.Location = new System.Drawing.Point(6, 32); - this.comboBoxSelectorMap.Name = "comboBoxSelectorMap"; - this.comboBoxSelectorMap.Size = new System.Drawing.Size(188, 23); - this.comboBoxSelectorMap.TabIndex = 4; - this.comboBoxSelectorMap.SelectedIndexChanged += new System.EventHandler(this.ComboBoxSelectorShip_SelectedIndexChanged); - // // ButtonRemoveShip // - this.ButtonRemoveShip.Location = new System.Drawing.Point(6, 137); + this.ButtonRemoveShip.Location = new System.Drawing.Point(12, 358); this.ButtonRemoveShip.Name = "ButtonRemoveShip"; - this.ButtonRemoveShip.Size = new System.Drawing.Size(188, 25); + this.ButtonRemoveShip.Size = new System.Drawing.Size(176, 25); this.ButtonRemoveShip.TabIndex = 3; this.ButtonRemoveShip.Text = "Удалить корабль"; this.ButtonRemoveShip.UseVisualStyleBackColor = true; this.ButtonRemoveShip.Click += new System.EventHandler(this.ButtonRemoveShip_Click); // - // ButtonAddShip - // - this.ButtonAddShip.Location = new System.Drawing.Point(6, 61); - this.ButtonAddShip.Name = "ButtonAddShip"; - this.ButtonAddShip.Size = new System.Drawing.Size(188, 25); - this.ButtonAddShip.TabIndex = 2; - this.ButtonAddShip.Text = "Добавить корабль"; - this.ButtonAddShip.UseVisualStyleBackColor = true; - this.ButtonAddShip.Click += new System.EventHandler(this.ButtonAddShip_Click); - // // pictureBox1 // this.pictureBox1.Dock = System.Windows.Forms.DockStyle.Fill; this.pictureBox1.Location = new System.Drawing.Point(0, 0); this.pictureBox1.Name = "pictureBox1"; - this.pictureBox1.Size = new System.Drawing.Size(600, 450); + this.pictureBox1.Size = new System.Drawing.Size(593, 582); this.pictureBox1.TabIndex = 0; this.pictureBox1.TabStop = false; // @@ -180,13 +240,15 @@ // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(800, 450); + this.ClientSize = new System.Drawing.Size(793, 582); this.Controls.Add(this.pictureBox1); this.Controls.Add(this.groupBox1); this.Name = "FormMapWithSetWarmlyShip"; this.Text = "FormMapWithSetWarmlyShip"; this.groupBox1.ResumeLayout(false); this.groupBox1.PerformLayout(); + this.groupBoxMaps.ResumeLayout(false); + this.groupBoxMaps.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit(); this.ResumeLayout(false); @@ -206,5 +268,10 @@ private Button buttonUp; private Button buttonRight; private Button buttonLeft; + private GroupBox groupBoxMaps; + private ListBox listBoxMaps; + private Button buttonDeleteMap; + private Button buttonAddMap; + private TextBox textBoxNewMapName; } } \ No newline at end of file diff --git a/WarmlyShip/WarmlyShip/FormMapWithSetWarmlyShip.cs b/WarmlyShip/WarmlyShip/FormMapWithSetWarmlyShip.cs index fbaf5a2..8d0efef 100644 --- a/WarmlyShip/WarmlyShip/FormMapWithSetWarmlyShip.cs +++ b/WarmlyShip/WarmlyShip/FormMapWithSetWarmlyShip.cs @@ -14,45 +14,134 @@ namespace WarmlyShip public partial class FormMapWithSetWarmlyShip : Form { /// - /// Объект от класса карты с набором объектов + /// Словарь для выпадающего списка /// - private MapWithSetWarmlyShipGeneric _mapWarmlyShipCollectionGeneric; + private readonly Dictionary _mapsDict = new() + { + { "Простая карта", new SimpleMap() }, + { "Море", new MyMap() } + }; + /// + /// Объект от коллекции карт + /// + private readonly MapsCollection _mapsCollection; + + + ///// + ///// Объект от класса карты с набором объектов + ///// + //private MapWithSetWarmlyShipGeneric _mapWarmlyShipCollectionGeneric; + + /// /// Конструктор /// public FormMapWithSetWarmlyShip() { InitializeComponent(); + _mapsCollection = new MapsCollection(pictureBox1.Width, pictureBox1.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 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 ComboBoxSelectorShip_SelectedIndexChanged(object sender, EventArgs e) + private void ListBoxMaps_SelectedIndexChanged(object sender, EventArgs e) { - AbstractMap map = null; - switch (comboBoxSelectorMap.Text) - { - case "Простая карта": - map = new SimpleMap(); - break; - case "Море": - map = new MyMap(); - break; - } - if (map != null) - { - _mapWarmlyShipCollectionGeneric = new MapWithSetWarmlyShipGeneric( - pictureBox1.Width, pictureBox1.Height, map); - } - else - { - _mapWarmlyShipCollectionGeneric = null; - } - + pictureBox1.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } + ///// + ///// Выбор карты + ///// + ///// + ///// + //private void ComboBoxSelectorShip_SelectedIndexChanged(object sender, EventArgs e) + //{ + // AbstractMap map = null; + // switch (comboBoxSelectorMap.Text) + // { + // case "Простая карта": + // map = new SimpleMap(); + // break; + // case "Море": + // map = new MyMap(); + // break; + // } + // if (map != null) + // { + // _mapWarmlyShipCollectionGeneric = new MapWithSetWarmlyShipGeneric( + // pictureBox1.Width, pictureBox1.Height, map); + // } + // else + // { + // _mapWarmlyShipCollectionGeneric = null; + // } + + //} + + /// + /// Удаление карты + /// + /// + /// + 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(); + } + } /// /// Добавление объекта /// @@ -60,18 +149,18 @@ namespace WarmlyShip /// private void ButtonAddShip_Click(object sender, EventArgs e) { - if (_mapWarmlyShipCollectionGeneric == null) + if (listBoxMaps.SelectedIndex == -1) { return; } FormShip form = new(); if (form.ShowDialog() == DialogResult.OK) { - DrawningObjectShip car = new(form.SelectedShip); - if ((_mapWarmlyShipCollectionGeneric + car) > -1) + DrawningObjectShip ship = new(form.SelectedShip); + if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + ship!=null) { MessageBox.Show("Объект добавлен"); - pictureBox1.Image = _mapWarmlyShipCollectionGeneric.ShowSet(); + pictureBox1.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } else { @@ -88,6 +177,10 @@ namespace WarmlyShip /// private void ButtonRemoveShip_Click(object sender, EventArgs e) { + if (listBoxMaps.SelectedIndex == -1) + { + return; + } if (string.IsNullOrEmpty(maskedTextBoxPosition.Text)) { return; @@ -98,10 +191,10 @@ namespace WarmlyShip return; } int pos = Convert.ToInt32(maskedTextBoxPosition.Text); - if ((_mapWarmlyShipCollectionGeneric - pos) != null) + if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] - pos != null) { MessageBox.Show("Объект удален"); - pictureBox1.Image = _mapWarmlyShipCollectionGeneric.ShowSet(); + pictureBox1.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); ; } else { @@ -116,11 +209,11 @@ namespace WarmlyShip /// private void ButtonShowStorage_Click(object sender, EventArgs e) { - if (_mapWarmlyShipCollectionGeneric == null) + if (listBoxMaps.SelectedIndex == -1) { return; } - pictureBox1.Image = _mapWarmlyShipCollectionGeneric.ShowSet(); + pictureBox1.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } /// @@ -130,11 +223,11 @@ namespace WarmlyShip /// private void ButtonShowOnMap_Click(object sender, EventArgs e) { - if (_mapWarmlyShipCollectionGeneric == null) + if (listBoxMaps.SelectedIndex == -1) { return; } - pictureBox1.Image = _mapWarmlyShipCollectionGeneric.ShowOnMap(); + pictureBox1.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowOnMap(); } /// @@ -144,7 +237,7 @@ namespace WarmlyShip /// private void ButtonMove_Click(object sender, EventArgs e) { - if (_mapWarmlyShipCollectionGeneric == null) + if (listBoxMaps.SelectedIndex == -1) { return; } @@ -167,9 +260,7 @@ namespace WarmlyShip break; } - pictureBox1.Image = _mapWarmlyShipCollectionGeneric.MoveObject(dir); + pictureBox1.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].MoveObject(dir); } - - } } diff --git a/WarmlyShip/WarmlyShip/MapsCollection.cs b/WarmlyShip/WarmlyShip/MapsCollection.cs index 8cbdd75..add470b 100644 --- a/WarmlyShip/WarmlyShip/MapsCollection.cs +++ b/WarmlyShip/WarmlyShip/MapsCollection.cs @@ -42,7 +42,7 @@ namespace WarmlyShip /// Карта public void AddMap(string name, AbstractMap map) { - // TODO Прописать логику для добавления + } /// /// Удаление карты