From 50baf3b05b3c63e70ca72336a2b565375d171e41 Mon Sep 17 00:00:00 2001 From: Danil Kargin Date: Wed, 2 Nov 2022 22:20:29 +0400 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 --- .../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(); + } } } }