From 8b6f9e2177ed3068e99391c90fa90373f6dced6e Mon Sep 17 00:00:00 2001 From: "A.Novopoltsev" Date: Fri, 16 Dec 2022 23:55:09 +0300 Subject: [PATCH] =?UTF-8?q?=D0=AD=D1=82=D0=B0=D0=BF=203.=20=D0=A4=D0=BE?= =?UTF-8?q?=D1=80=D0=BC=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../FormMapWithSetWarships.Designer.cs | 176 ++++++++++++------ Warship/Warship/FormMapWithSetWarships.cs | 100 +++++++--- Warship/Warship/MapWithSetWarshipsGeneric.cs | 23 +-- 3 files changed, 202 insertions(+), 97 deletions(-) diff --git a/Warship/Warship/FormMapWithSetWarships.Designer.cs b/Warship/Warship/FormMapWithSetWarships.Designer.cs index 9a24629..cbab596 100644 --- a/Warship/Warship/FormMapWithSetWarships.Designer.cs +++ b/Warship/Warship/FormMapWithSetWarships.Designer.cs @@ -29,77 +29,108 @@ private void InitializeComponent() { this.groupBoxTools = new System.Windows.Forms.GroupBox(); - this.buttonShowOnMap = new System.Windows.Forms.Button(); - this.buttonShowStorage = new System.Windows.Forms.Button(); - this.buttonRemoveWarship = new System.Windows.Forms.Button(); - this.maskedTextBoxPosition = new System.Windows.Forms.MaskedTextBox(); - this.buttonAddWarship = new System.Windows.Forms.Button(); + this.groupBoxMaps = new System.Windows.Forms.GroupBox(); + this.buttonDeleteMap = new System.Windows.Forms.Button(); + this.buttonAddMap = new System.Windows.Forms.Button(); + this.listBoxMaps = new System.Windows.Forms.ListBox(); + this.textBoxMap = new System.Windows.Forms.TextBox(); this.comboBoxSelectorMap = new System.Windows.Forms.ComboBox(); + this.buttonAddWarship = new System.Windows.Forms.Button(); + this.buttonShowOnMap = new System.Windows.Forms.Button(); + this.buttonRemoveWarship = new System.Windows.Forms.Button(); + this.buttonShowStorage = new System.Windows.Forms.Button(); + this.maskedTextBoxPosition = new System.Windows.Forms.MaskedTextBox(); this.pictureBox1 = new System.Windows.Forms.PictureBox(); this.pictureBox = new System.Windows.Forms.PictureBox(); this.groupBoxTools.SuspendLayout(); + this.groupBoxMaps.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit(); this.SuspendLayout(); // // groupBoxTools // - this.groupBoxTools.Controls.Add(this.buttonShowOnMap); - this.groupBoxTools.Controls.Add(this.buttonShowStorage); - this.groupBoxTools.Controls.Add(this.buttonRemoveWarship); - this.groupBoxTools.Controls.Add(this.maskedTextBoxPosition); + this.groupBoxTools.Controls.Add(this.groupBoxMaps); this.groupBoxTools.Controls.Add(this.buttonAddWarship); - this.groupBoxTools.Controls.Add(this.comboBoxSelectorMap); + this.groupBoxTools.Controls.Add(this.buttonShowOnMap); + this.groupBoxTools.Controls.Add(this.buttonRemoveWarship); + this.groupBoxTools.Controls.Add(this.buttonShowStorage); + this.groupBoxTools.Controls.Add(this.maskedTextBoxPosition); this.groupBoxTools.Controls.Add(this.pictureBox1); this.groupBoxTools.Dock = System.Windows.Forms.DockStyle.Right; - this.groupBoxTools.Location = new System.Drawing.Point(600, 0); + this.groupBoxTools.Location = new System.Drawing.Point(911, 0); this.groupBoxTools.Name = "groupBoxTools"; - this.groupBoxTools.Size = new System.Drawing.Size(200, 450); + this.groupBoxTools.Size = new System.Drawing.Size(200, 597); this.groupBoxTools.TabIndex = 0; this.groupBoxTools.TabStop = false; this.groupBoxTools.Text = "Инструменты"; // - // buttonShowOnMap + // groupBoxMaps // - this.buttonShowOnMap.Location = new System.Drawing.Point(25, 317); - this.buttonShowOnMap.Name = "buttonShowOnMap"; - this.buttonShowOnMap.Size = new System.Drawing.Size(163, 23); - this.buttonShowOnMap.TabIndex = 7; - this.buttonShowOnMap.Text = "Посмотреть карту"; - this.buttonShowOnMap.UseVisualStyleBackColor = true; - this.buttonShowOnMap.Click += new System.EventHandler(this.ButtonShowOnMap_Click); + this.groupBoxMaps.Controls.Add(this.buttonDeleteMap); + this.groupBoxMaps.Controls.Add(this.buttonAddMap); + this.groupBoxMaps.Controls.Add(this.listBoxMaps); + this.groupBoxMaps.Controls.Add(this.textBoxMap); + this.groupBoxMaps.Controls.Add(this.comboBoxSelectorMap); + this.groupBoxMaps.Location = new System.Drawing.Point(15, 32); + this.groupBoxMaps.Name = "groupBoxMaps"; + this.groupBoxMaps.Size = new System.Drawing.Size(173, 250); + this.groupBoxMaps.TabIndex = 2; + this.groupBoxMaps.TabStop = false; + this.groupBoxMaps.Text = "Карты"; // - // buttonShowStorage + // buttonDeleteMap // - this.buttonShowStorage.Location = new System.Drawing.Point(25, 253); - this.buttonShowStorage.Name = "buttonShowStorage"; - this.buttonShowStorage.Size = new System.Drawing.Size(163, 23); - this.buttonShowStorage.TabIndex = 6; - this.buttonShowStorage.Text = "Посмотреть хранилище"; - this.buttonShowStorage.UseVisualStyleBackColor = true; - this.buttonShowStorage.Click += new System.EventHandler(this.ButtonShowStorage_Click); + this.buttonDeleteMap.Location = new System.Drawing.Point(6, 214); + this.buttonDeleteMap.Name = "buttonDeleteMap"; + this.buttonDeleteMap.Size = new System.Drawing.Size(152, 23); + this.buttonDeleteMap.TabIndex = 9; + this.buttonDeleteMap.Text = "Удалить карту"; + this.buttonDeleteMap.UseVisualStyleBackColor = true; + this.buttonDeleteMap.Click += new System.EventHandler(this.ButtonDeleteMap_Click); // - // buttonRemoveWarship + // buttonAddMap // - this.buttonRemoveWarship.Location = new System.Drawing.Point(25, 188); - this.buttonRemoveWarship.Name = "buttonRemoveWarship"; - this.buttonRemoveWarship.Size = new System.Drawing.Size(163, 23); - this.buttonRemoveWarship.TabIndex = 5; - this.buttonRemoveWarship.Text = "Удалить корабль"; - this.buttonRemoveWarship.UseVisualStyleBackColor = true; - this.buttonRemoveWarship.Click += new System.EventHandler(this.ButtonRemoveWarship_Click); + this.buttonAddMap.Location = new System.Drawing.Point(3, 77); + this.buttonAddMap.Name = "buttonAddMap"; + this.buttonAddMap.Size = new System.Drawing.Size(155, 31); + this.buttonAddMap.TabIndex = 7; + this.buttonAddMap.Text = "Добавить карту"; + this.buttonAddMap.UseVisualStyleBackColor = true; + this.buttonAddMap.Click += new System.EventHandler(this.ButtonAddMap_Click); // - // maskedTextBoxPosition + // listBoxMaps // - this.maskedTextBoxPosition.Location = new System.Drawing.Point(25, 123); - this.maskedTextBoxPosition.Mask = "00"; - this.maskedTextBoxPosition.Name = "maskedTextBoxPosition"; - this.maskedTextBoxPosition.Size = new System.Drawing.Size(163, 23); - this.maskedTextBoxPosition.TabIndex = 4; + this.listBoxMaps.FormattingEnabled = true; + this.listBoxMaps.ItemHeight = 15; + this.listBoxMaps.Location = new System.Drawing.Point(3, 114); + this.listBoxMaps.Name = "listBoxMaps"; + this.listBoxMaps.Size = new System.Drawing.Size(158, 94); + this.listBoxMaps.TabIndex = 8; + this.listBoxMaps.SelectedIndexChanged += new System.EventHandler(this.ListBoxMaps_SelectedIndexChanged); + // + // textBoxMap + // + this.textBoxMap.Location = new System.Drawing.Point(3, 19); + this.textBoxMap.Name = "textBoxMap"; + this.textBoxMap.Size = new System.Drawing.Size(164, 23); + this.textBoxMap.TabIndex = 6; + // + // 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(4, 48); + this.comboBoxSelectorMap.Name = "comboBoxSelectorMap"; + this.comboBoxSelectorMap.Size = new System.Drawing.Size(157, 23); + this.comboBoxSelectorMap.TabIndex = 0; // // buttonAddWarship // - this.buttonAddWarship.Location = new System.Drawing.Point(25, 62); + this.buttonAddWarship.Location = new System.Drawing.Point(18, 344); this.buttonAddWarship.Name = "buttonAddWarship"; this.buttonAddWarship.Size = new System.Drawing.Size(163, 23); this.buttonAddWarship.TabIndex = 3; @@ -107,23 +138,49 @@ this.buttonAddWarship.UseVisualStyleBackColor = true; this.buttonAddWarship.Click += new System.EventHandler(this.ButtonAddWarship_Click); // - // comboBoxSelectorMap + // buttonShowOnMap // - this.comboBoxSelectorMap.FormattingEnabled = true; - this.comboBoxSelectorMap.Items.AddRange(new object[] { - "Первая карта", - "Вторая карта"}); - this.comboBoxSelectorMap.Location = new System.Drawing.Point(25, 22); - this.comboBoxSelectorMap.Name = "comboBoxSelectorMap"; - this.comboBoxSelectorMap.Size = new System.Drawing.Size(163, 23); - this.comboBoxSelectorMap.TabIndex = 2; - this.comboBoxSelectorMap.Click += new System.EventHandler(this.ComboBoxSelectorMap_SelectedIndexChanged); + this.buttonShowOnMap.Location = new System.Drawing.Point(15, 509); + this.buttonShowOnMap.Name = "buttonShowOnMap"; + this.buttonShowOnMap.Size = new System.Drawing.Size(163, 23); + this.buttonShowOnMap.TabIndex = 7; + this.buttonShowOnMap.Text = "Посмотреть карту"; + this.buttonShowOnMap.UseVisualStyleBackColor = true; + this.buttonShowOnMap.Click += new System.EventHandler(this.ButtonShowOnMap_Click); + // + // buttonRemoveWarship + // + this.buttonRemoveWarship.Location = new System.Drawing.Point(15, 402); + this.buttonRemoveWarship.Name = "buttonRemoveWarship"; + this.buttonRemoveWarship.Size = new System.Drawing.Size(163, 23); + this.buttonRemoveWarship.TabIndex = 5; + this.buttonRemoveWarship.Text = "Удалить корабль"; + this.buttonRemoveWarship.UseVisualStyleBackColor = true; + this.buttonRemoveWarship.Click += new System.EventHandler(this.ButtonRemoveWarship_Click); + // + // buttonShowStorage + // + this.buttonShowStorage.Location = new System.Drawing.Point(18, 480); + this.buttonShowStorage.Name = "buttonShowStorage"; + this.buttonShowStorage.Size = new System.Drawing.Size(163, 23); + this.buttonShowStorage.TabIndex = 6; + this.buttonShowStorage.Text = "Посмотреть хранилище"; + this.buttonShowStorage.UseVisualStyleBackColor = true; + this.buttonShowStorage.Click += new System.EventHandler(this.ButtonShowStorage_Click); + // + // maskedTextBoxPosition + // + this.maskedTextBoxPosition.Location = new System.Drawing.Point(19, 373); + this.maskedTextBoxPosition.Mask = "00"; + this.maskedTextBoxPosition.Name = "maskedTextBoxPosition"; + this.maskedTextBoxPosition.Size = new System.Drawing.Size(163, 23); + this.maskedTextBoxPosition.TabIndex = 4; // // pictureBox1 // this.pictureBox1.Location = new System.Drawing.Point(3, 12); this.pictureBox1.Name = "pictureBox1"; - this.pictureBox1.Size = new System.Drawing.Size(197, 438); + this.pictureBox1.Size = new System.Drawing.Size(197, 546); this.pictureBox1.TabIndex = 0; this.pictureBox1.TabStop = false; // @@ -132,7 +189,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(600, 450); + this.pictureBox.Size = new System.Drawing.Size(911, 597); this.pictureBox.TabIndex = 1; this.pictureBox.TabStop = false; // @@ -140,13 +197,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(1111, 597); this.Controls.Add(this.pictureBox); this.Controls.Add(this.groupBoxTools); this.Name = "FormMapWithSetWarships"; this.Text = "FormMapWithSetWarships"; this.groupBoxTools.ResumeLayout(false); this.groupBoxTools.PerformLayout(); + this.groupBoxMaps.ResumeLayout(false); + this.groupBoxMaps.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit(); this.ResumeLayout(false); @@ -164,5 +223,10 @@ private Button buttonShowStorage; private Button buttonRemoveWarship; private MaskedTextBox maskedTextBoxPosition; + private GroupBox groupBoxMaps; + private TextBox textBoxMap; + private Button buttonAddMap; + private ListBox listBoxMaps; + private Button buttonDeleteMap; } } \ No newline at end of file diff --git a/Warship/Warship/FormMapWithSetWarships.cs b/Warship/Warship/FormMapWithSetWarships.cs index 375904b..7c946ba 100644 --- a/Warship/Warship/FormMapWithSetWarships.cs +++ b/Warship/Warship/FormMapWithSetWarships.cs @@ -12,40 +12,46 @@ namespace Warship { public partial class FormMapWithSetWarships : Form { - private MapWithSetWarshipsGeneric _mapWarshipsCollectionGeneric; + + private readonly Dictionary _mapDict = new() + { + {"Первая карта",new SimpleMap() }, + {"Вторая карта",new SecondMap() } + }; + + private readonly MapsCollection _mapsCollection; public FormMapWithSetWarships() { InitializeComponent(); + _mapsCollection = new MapsCollection(pictureBox.Width, pictureBox.Height); + comboBoxSelectorMap.Items.Clear(); + foreach (var elem in _mapDict) + { + comboBoxSelectorMap.Items.Add(elem.Key); + } } - private void ComboBoxSelectorMap_SelectedIndexChanged(object sender, EventArgs e) + private void ReloadMaps() { - AbstractMap map = null; - - switch (comboBoxSelectorMap.Text) + int index = listBoxMaps.SelectedIndex; + for (int i = 0; i < _mapsCollection.Keys.Count; i++) { - case "Первая карта": - map = new SimpleMap(); - break; - case "Вторая карта": - map = new SecondMap(); - break; + listBoxMaps.Items.Add(_mapsCollection.Keys[i]); } - if (map != null) + if (listBoxMaps.Items.Count > 0 && (index == -1 || index >= listBoxMaps.Items.Count)) { - _mapWarshipsCollectionGeneric = new MapWithSetWarshipsGeneric( - pictureBox.Width, pictureBox.Height, map); + listBoxMaps.SelectedIndex = 0; } - else + else if (listBoxMaps.Items.Count > 0 && index > -1 && index < listBoxMaps.Items.Count) { - _mapWarshipsCollectionGeneric = null; + listBoxMaps.SelectedIndex = index; } } private void ButtonAddWarship_Click(object sender, EventArgs e) { - if (_mapWarshipsCollectionGeneric == null) + if (listBoxMaps.SelectedIndex == -1) { return; } @@ -53,10 +59,10 @@ namespace Warship if (form.ShowDialog() == DialogResult.OK) { DrawingObjectWarship warship = new(form.SelectedWarship); - if (_mapWarshipsCollectionGeneric + warship >= 0) + if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + warship != -1) { MessageBox.Show("Объект добавлен"); - pictureBox.Image = _mapWarshipsCollectionGeneric.ShowSet(); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } else { @@ -67,6 +73,10 @@ namespace Warship private void ButtonRemoveWarship_Click(object sender, EventArgs e) { + if (listBoxMaps.SelectedIndex == -1) + { + return; + } if (string.IsNullOrEmpty(maskedTextBoxPosition.Text)) { return; @@ -76,10 +86,10 @@ namespace Warship return; } int pos = Convert.ToInt32(maskedTextBoxPosition.Text); - if (_mapWarshipsCollectionGeneric - pos != null) + if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] - pos != null) { MessageBox.Show("Объект удален"); - pictureBox.Image = _mapWarshipsCollectionGeneric.ShowSet(); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } else { @@ -89,25 +99,25 @@ namespace Warship private void ButtonShowStorage_Click(object sender, EventArgs e) { - if (_mapWarshipsCollectionGeneric == null) + if (listBoxMaps.SelectedIndex == -1) { return; } - pictureBox.Image = _mapWarshipsCollectionGeneric.ShowSet(); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } private void ButtonShowOnMap_Click(object sender, EventArgs e) { - if (_mapWarshipsCollectionGeneric == null) + if (listBoxMaps.SelectedIndex == -1) { return; } - pictureBox.Image = _mapWarshipsCollectionGeneric.ShowOnMap(); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowOnMap(); } private void ButtonMove_Click(object sender, EventArgs e) { - if (_mapWarshipsCollectionGeneric == null) + if (listBoxMaps.SelectedIndex == -1) { return; } @@ -128,7 +138,43 @@ namespace Warship dir = Direction.Right; break; } - pictureBox.Image = _mapWarshipsCollectionGeneric.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(textBoxMap.Text)) + { + MessageBox.Show("Не все данные заполнены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (!_mapDict.ContainsKey(comboBoxSelectorMap.Text)) + { + MessageBox.Show("Нет такой карты", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + listBoxMaps.Items.Clear(); + _mapsCollection.AddMap(textBoxMap.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); + listBoxMaps.Items.Clear(); + ReloadMaps(); + } } } } diff --git a/Warship/Warship/MapWithSetWarshipsGeneric.cs b/Warship/Warship/MapWithSetWarshipsGeneric.cs index 0c4208d..5dc0551 100644 --- a/Warship/Warship/MapWithSetWarshipsGeneric.cs +++ b/Warship/Warship/MapWithSetWarshipsGeneric.cs @@ -49,13 +49,9 @@ namespace Warship public Bitmap ShowOnMap() { Shaking(); - for (int i = 0; i < _setWarship.Count; i++) + foreach (var warship in _setWarship.GetWarships()) { - var warship = _setWarship.Get(i); - if (warship != null) - { - return _map.CreateMap(_pictureWidth, _pictureHeight, warship); - } + return _map.CreateMap(_pictureWidth, _pictureHeight, warship); } return new(_pictureWidth, _pictureHeight); } @@ -74,11 +70,11 @@ namespace Warship int j = _setWarship.Count - 1; for (int i = 0; i < _setWarship.Count; i++) { - if (_setWarship.Get(i) == null) + if (_setWarship[i] == null) { for (; j > i; j--) { - var warship = _setWarship.Get(j); + var warship = _setWarship[j]; if (warship != null) { _setWarship.Insert(warship, i); @@ -113,14 +109,13 @@ namespace Warship { int width = _pictureWidth / _placeSizeWidth; int height = _pictureHeight / _placeSizeHeight; + int i = 0; - for (int i = 0; i < _setWarship.Count; i++) + foreach (var warship in _setWarship.GetWarships()) { - if (_setWarship.Get(i) != null) - { - _setWarship.Get(i).SetObject(i % width * _placeSizeWidth, (height - 1 - i / width) * _placeSizeHeight, _pictureWidth, _pictureHeight); - _setWarship.Get(i)?.DrawingObject(gr); - } + warship.SetObject(i % width * _placeSizeWidth, (height - 1 - i / width) * _placeSizeHeight, _pictureWidth, _pictureHeight); + warship.DrawingObject(gr); + i++; } } }