diff --git a/Liner/Liner/FormMapWithSetShips.Designer.cs b/Liner/Liner/FormMapWithSetShips.Designer.cs index b2e1d3e..40d145a 100644 --- a/Liner/Liner/FormMapWithSetShips.Designer.cs +++ b/Liner/Liner/FormMapWithSetShips.Designer.cs @@ -29,6 +29,12 @@ private void InitializeComponent() { this.groupBox = new System.Windows.Forms.GroupBox(); + this.groupBox1 = 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.ComboBoxSelectorMap = new System.Windows.Forms.ComboBox(); this.buttonRight = new System.Windows.Forms.Button(); this.buttonDown = new System.Windows.Forms.Button(); this.buttonUp = new System.Windows.Forms.Button(); @@ -38,14 +44,15 @@ this.ButtonRemoveShip = new System.Windows.Forms.Button(); this.maskedTextBoxPosition = new System.Windows.Forms.MaskedTextBox(); this.ButtonAddShip = new System.Windows.Forms.Button(); - this.ComboBoxSelectorMap = new System.Windows.Forms.ComboBox(); this.pictureBox = new System.Windows.Forms.PictureBox(); this.groupBox.SuspendLayout(); + this.groupBox1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit(); this.SuspendLayout(); // // groupBox // + this.groupBox.Controls.Add(this.groupBox1); this.groupBox.Controls.Add(this.buttonRight); this.groupBox.Controls.Add(this.buttonDown); this.groupBox.Controls.Add(this.buttonUp); @@ -55,21 +62,85 @@ this.groupBox.Controls.Add(this.ButtonRemoveShip); this.groupBox.Controls.Add(this.maskedTextBoxPosition); this.groupBox.Controls.Add(this.ButtonAddShip); - this.groupBox.Controls.Add(this.ComboBoxSelectorMap); this.groupBox.Dock = System.Windows.Forms.DockStyle.Right; - this.groupBox.Location = new System.Drawing.Point(627, 0); + this.groupBox.Location = new System.Drawing.Point(696, 0); this.groupBox.Name = "groupBox"; - this.groupBox.Size = new System.Drawing.Size(250, 450); + this.groupBox.Size = new System.Drawing.Size(279, 668); this.groupBox.TabIndex = 0; this.groupBox.TabStop = false; this.groupBox.Text = "Инструменты"; // + // groupBox1 + // + this.groupBox1.Controls.Add(this.ButtonDeleteMap); + this.groupBox1.Controls.Add(this.ListBoxMaps); + this.groupBox1.Controls.Add(this.ButtonAddMap); + this.groupBox1.Controls.Add(this.textBoxNewMapName); + this.groupBox1.Controls.Add(this.ComboBoxSelectorMap); + this.groupBox1.Location = new System.Drawing.Point(6, 26); + this.groupBox1.Name = "groupBox1"; + this.groupBox1.Size = new System.Drawing.Size(250, 300); + this.groupBox1.TabIndex = 18; + this.groupBox1.TabStop = false; + this.groupBox1.Text = "Карты"; + // + // ButtonDeleteMap + // + this.ButtonDeleteMap.Location = new System.Drawing.Point(10, 255); + this.ButtonDeleteMap.Name = "ButtonDeleteMap"; + this.ButtonDeleteMap.Size = new System.Drawing.Size(222, 29); + this.ButtonDeleteMap.TabIndex = 13; + 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(10, 136); + this.ListBoxMaps.Name = "ListBoxMaps"; + this.ListBoxMaps.Size = new System.Drawing.Size(222, 104); + this.ListBoxMaps.TabIndex = 12; + this.ListBoxMaps.SelectedIndexChanged += new System.EventHandler(this.ListBoxMaps_SelectedIndexChanged); + // + // ButtonAddMap + // + this.ButtonAddMap.Location = new System.Drawing.Point(10, 101); + this.ButtonAddMap.Name = "ButtonAddMap"; + this.ButtonAddMap.Size = new System.Drawing.Size(222, 29); + this.ButtonAddMap.TabIndex = 11; + this.ButtonAddMap.Text = "Добавить карту"; + this.ButtonAddMap.UseVisualStyleBackColor = true; + this.ButtonAddMap.Click += new System.EventHandler(this.ButtonAddMap_Click); + // + // textBoxNewMapName + // + this.textBoxNewMapName.Location = new System.Drawing.Point(10, 34); + this.textBoxNewMapName.Name = "textBoxNewMapName"; + this.textBoxNewMapName.Size = new System.Drawing.Size(222, 27); + this.textBoxNewMapName.TabIndex = 10; + // + // ComboBoxSelectorMap + // + this.ComboBoxSelectorMap.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; + this.ComboBoxSelectorMap.FormattingEnabled = true; + this.ComboBoxSelectorMap.Items.AddRange(new object[] { + "Простая карта", + "Море и айсберги", + "Болото"}); + this.ComboBoxSelectorMap.Location = new System.Drawing.Point(10, 67); + this.ComboBoxSelectorMap.Name = "ComboBoxSelectorMap"; + this.ComboBoxSelectorMap.Size = new System.Drawing.Size(222, 28); + this.ComboBoxSelectorMap.TabIndex = 9; + this.ComboBoxSelectorMap.SelectedIndexChanged += new System.EventHandler(this.ComboBoxSelectorMap_SelectedIndexChanged); + // // buttonRight // this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonRight.BackgroundImage = global::Liner.Properties.Resources._3; this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonRight.Location = new System.Drawing.Point(204, 408); + this.buttonRight.Location = new System.Drawing.Point(233, 626); this.buttonRight.Name = "buttonRight"; this.buttonRight.Size = new System.Drawing.Size(30, 30); this.buttonRight.TabIndex = 17; @@ -81,7 +152,7 @@ this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonDown.BackgroundImage = global::Liner.Properties.Resources._2; this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonDown.Location = new System.Drawing.Point(168, 408); + this.buttonDown.Location = new System.Drawing.Point(197, 626); this.buttonDown.Name = "buttonDown"; this.buttonDown.Size = new System.Drawing.Size(30, 30); this.buttonDown.TabIndex = 16; @@ -93,7 +164,7 @@ this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonUp.BackgroundImage = global::Liner.Properties.Resources._4; this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonUp.Location = new System.Drawing.Point(168, 372); + this.buttonUp.Location = new System.Drawing.Point(197, 590); this.buttonUp.Name = "buttonUp"; this.buttonUp.Size = new System.Drawing.Size(30, 30); this.buttonUp.TabIndex = 15; @@ -105,7 +176,7 @@ this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonLeft.BackgroundImage = global::Liner.Properties.Resources._1; this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonLeft.Location = new System.Drawing.Point(132, 408); + this.buttonLeft.Location = new System.Drawing.Point(161, 626); this.buttonLeft.Name = "buttonLeft"; this.buttonLeft.Size = new System.Drawing.Size(30, 30); this.buttonLeft.TabIndex = 14; @@ -114,9 +185,9 @@ // // ButtonShowOnMap // - this.ButtonShowOnMap.Location = new System.Drawing.Point(28, 323); + this.ButtonShowOnMap.Location = new System.Drawing.Point(16, 558); this.ButtonShowOnMap.Name = "ButtonShowOnMap"; - this.ButtonShowOnMap.Size = new System.Drawing.Size(200, 29); + this.ButtonShowOnMap.Size = new System.Drawing.Size(222, 29); this.ButtonShowOnMap.TabIndex = 13; this.ButtonShowOnMap.Text = "Посмотреть карту"; this.ButtonShowOnMap.UseVisualStyleBackColor = true; @@ -124,9 +195,9 @@ // // ButtonShowStorage // - this.ButtonShowStorage.Location = new System.Drawing.Point(28, 258); + this.ButtonShowStorage.Location = new System.Drawing.Point(16, 505); this.ButtonShowStorage.Name = "ButtonShowStorage"; - this.ButtonShowStorage.Size = new System.Drawing.Size(200, 29); + this.ButtonShowStorage.Size = new System.Drawing.Size(222, 29); this.ButtonShowStorage.TabIndex = 12; this.ButtonShowStorage.Text = "Посмотреть хранилище"; this.ButtonShowStorage.UseVisualStyleBackColor = true; @@ -134,9 +205,9 @@ // // ButtonRemoveShip // - this.ButtonRemoveShip.Location = new System.Drawing.Point(28, 191); + this.ButtonRemoveShip.Location = new System.Drawing.Point(16, 452); this.ButtonRemoveShip.Name = "ButtonRemoveShip"; - this.ButtonRemoveShip.Size = new System.Drawing.Size(200, 29); + this.ButtonRemoveShip.Size = new System.Drawing.Size(222, 29); this.ButtonRemoveShip.TabIndex = 11; this.ButtonRemoveShip.Text = "Удалить корабль"; this.ButtonRemoveShip.UseVisualStyleBackColor = true; @@ -144,42 +215,28 @@ // // maskedTextBoxPosition // - this.maskedTextBoxPosition.Location = new System.Drawing.Point(28, 158); + this.maskedTextBoxPosition.Location = new System.Drawing.Point(16, 419); this.maskedTextBoxPosition.Mask = "00"; this.maskedTextBoxPosition.Name = "maskedTextBoxPosition"; - this.maskedTextBoxPosition.Size = new System.Drawing.Size(200, 27); + this.maskedTextBoxPosition.Size = new System.Drawing.Size(222, 27); this.maskedTextBoxPosition.TabIndex = 10; // // ButtonAddShip // - this.ButtonAddShip.Location = new System.Drawing.Point(28, 110); + this.ButtonAddShip.Location = new System.Drawing.Point(16, 368); this.ButtonAddShip.Name = "ButtonAddShip"; - this.ButtonAddShip.Size = new System.Drawing.Size(200, 29); + this.ButtonAddShip.Size = new System.Drawing.Size(222, 29); this.ButtonAddShip.TabIndex = 2; this.ButtonAddShip.Text = "Добавить корабль"; this.ButtonAddShip.UseVisualStyleBackColor = true; this.ButtonAddShip.Click += new System.EventHandler(this.ButtonAddShip_Click); // - // ComboBoxSelectorMap - // - this.ComboBoxSelectorMap.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; - this.ComboBoxSelectorMap.FormattingEnabled = true; - this.ComboBoxSelectorMap.Items.AddRange(new object[] { - "Простая карта", - "Море и айсберги", - "Болото"}); - this.ComboBoxSelectorMap.Location = new System.Drawing.Point(28, 38); - this.ComboBoxSelectorMap.Name = "ComboBoxSelectorMap"; - this.ComboBoxSelectorMap.Size = new System.Drawing.Size(200, 28); - this.ComboBoxSelectorMap.TabIndex = 9; - 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(627, 450); + this.pictureBox.Size = new System.Drawing.Size(696, 668); this.pictureBox.TabIndex = 1; this.pictureBox.TabStop = false; // @@ -187,13 +244,15 @@ // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(877, 450); + this.ClientSize = new System.Drawing.Size(975, 668); this.Controls.Add(this.pictureBox); this.Controls.Add(this.groupBox); this.Name = "FormMapWithSetShips"; this.Text = "FormMapWithSetShips"; this.groupBox.ResumeLayout(false); this.groupBox.PerformLayout(); + this.groupBox1.ResumeLayout(false); + this.groupBox1.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit(); this.ResumeLayout(false); @@ -213,5 +272,10 @@ private Button buttonDown; private Button buttonUp; private Button buttonLeft; + private GroupBox groupBox1; + private Button ButtonDeleteMap; + private ListBox ListBoxMaps; + private Button ButtonAddMap; + private TextBox textBoxNewMapName; } } \ No newline at end of file diff --git a/Liner/Liner/FormMapWithSetShips.cs b/Liner/Liner/FormMapWithSetShips.cs index 500b974..8e10dc0 100644 --- a/Liner/Liner/FormMapWithSetShips.cs +++ b/Liner/Liner/FormMapWithSetShips.cs @@ -14,9 +14,41 @@ namespace Liner public partial class FormMapWithSetShips : Form { private MapWithSetShipsGeneric _mapShipsCollectionGeneric; + private readonly Dictionary _mapDict = new() + { + {"Простая карта", new SimpleMap() }, + {"Море и айсберги", new SeaMap() }, + {"Болото",new SwampMap() } + }; + private readonly MapsCollection _mapsCollection; public FormMapWithSetShips() { InitializeComponent(); + _mapsCollection = new MapsCollection(pictureBox.Width, pictureBox.Height); + ComboBoxSelectorMap.Items.Clear(); + foreach (var elem in _mapDict) + { + 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]); + } + + 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) { @@ -45,7 +77,7 @@ namespace Liner } private void ButtonAddShip_Click(object sender, EventArgs e) { - if (_mapShipsCollectionGeneric == null) + if (ListBoxMaps.SelectedIndex == -1) { return; } @@ -53,10 +85,10 @@ namespace Liner 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 { @@ -66,6 +98,10 @@ namespace Liner } private void ButtonRemoveShip_Click(object sender, EventArgs e) { + if (ListBoxMaps.SelectedIndex == -1) + { + return; + } if (string.IsNullOrEmpty(maskedTextBoxPosition.Text)) { return; @@ -75,10 +111,10 @@ namespace Liner 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 { @@ -87,23 +123,23 @@ namespace Liner } 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(); } 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(); } private void ButtonMove_Click(object sender, EventArgs e) { - if (_mapShipsCollectionGeneric == null) + if (ListBoxMaps.SelectedIndex == -1) { return; } @@ -124,7 +160,41 @@ namespace Liner 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 (!_mapDict.ContainsKey(ComboBoxSelectorMap.Text)) + { + MessageBox.Show("Нет такой карты", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _mapsCollection.AddMap(textBoxNewMapName.Text, _mapDict[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(); + } } } }