From f17a1c473577880ec58885e59639970449554efb Mon Sep 17 00:00:00 2001 From: Stranni15k Date: Sat, 19 Nov 2022 22:38:17 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9A=D0=BE=D0=BC=D0=BC=D0=B8=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../FormMapWithSetLocomotive.Designer.cs | 75 ++++++++++--- .../FormMapWithSetLocomotive.cs | 106 ++++++++++++++---- .../ElectricLocomotive/MapsCollection.cs | 44 ++++++++ 3 files changed, 193 insertions(+), 32 deletions(-) create mode 100644 ElectricLocomotive/ElectricLocomotive/MapsCollection.cs diff --git a/ElectricLocomotive/ElectricLocomotive/FormMapWithSetLocomotive.Designer.cs b/ElectricLocomotive/ElectricLocomotive/FormMapWithSetLocomotive.Designer.cs index 9f7b19b..ef3926a 100644 --- a/ElectricLocomotive/ElectricLocomotive/FormMapWithSetLocomotive.Designer.cs +++ b/ElectricLocomotive/ElectricLocomotive/FormMapWithSetLocomotive.Designer.cs @@ -40,6 +40,10 @@ this.buttonRemoveCar = new System.Windows.Forms.Button(); this.buttonShowStorage = new System.Windows.Forms.Button(); this.buttonShowOnMap = new System.Windows.Forms.Button(); + 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(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit(); this.SuspendLayout(); // @@ -50,7 +54,7 @@ | System.Windows.Forms.AnchorStyles.Right))); this.pictureBox1.Location = new System.Drawing.Point(0, 0); this.pictureBox1.Name = "pictureBox1"; - this.pictureBox1.Size = new System.Drawing.Size(631, 449); + this.pictureBox1.Size = new System.Drawing.Size(808, 721); this.pictureBox1.TabIndex = 10; this.pictureBox1.TabStop = false; // @@ -60,7 +64,7 @@ this.buttonUp.AutoSize = true; this.buttonUp.BackgroundImage = global::ElectricLocomotive.Properties.Resources.arrowup; this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonUp.Location = new System.Drawing.Point(699, 372); + this.buttonUp.Location = new System.Drawing.Point(892, 624); this.buttonUp.Name = "buttonUp"; this.buttonUp.Size = new System.Drawing.Size(30, 30); this.buttonUp.TabIndex = 14; @@ -73,7 +77,7 @@ this.buttonRight.AutoSize = true; this.buttonRight.BackgroundImage = global::ElectricLocomotive.Properties.Resources.arrowright; this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonRight.Location = new System.Drawing.Point(735, 408); + this.buttonRight.Location = new System.Drawing.Point(928, 660); this.buttonRight.Name = "buttonRight"; this.buttonRight.Size = new System.Drawing.Size(30, 30); this.buttonRight.TabIndex = 13; @@ -86,7 +90,7 @@ this.buttonDown.AutoSize = true; this.buttonDown.BackgroundImage = global::ElectricLocomotive.Properties.Resources.arrowdown; this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonDown.Location = new System.Drawing.Point(699, 408); + this.buttonDown.Location = new System.Drawing.Point(892, 660); this.buttonDown.Name = "buttonDown"; this.buttonDown.Size = new System.Drawing.Size(30, 30); this.buttonDown.TabIndex = 12; @@ -99,7 +103,7 @@ this.buttonLeft.AutoSize = true; this.buttonLeft.BackgroundImage = global::ElectricLocomotive.Properties.Resources.arrowleft; this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonLeft.Location = new System.Drawing.Point(663, 408); + this.buttonLeft.Location = new System.Drawing.Point(856, 660); this.buttonLeft.Name = "buttonLeft"; this.buttonLeft.Size = new System.Drawing.Size(30, 30); this.buttonLeft.TabIndex = 11; @@ -109,7 +113,7 @@ // label1 // this.label1.AutoSize = true; - this.label1.Location = new System.Drawing.Point(637, 9); + this.label1.Location = new System.Drawing.Point(837, 19); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(83, 15); this.label1.TabIndex = 15; @@ -117,7 +121,7 @@ // // buttonAddLocomotive // - this.buttonAddLocomotive.Location = new System.Drawing.Point(637, 98); + this.buttonAddLocomotive.Location = new System.Drawing.Point(837, 386); this.buttonAddLocomotive.Name = "buttonAddLocomotive"; this.buttonAddLocomotive.Size = new System.Drawing.Size(151, 28); this.buttonAddLocomotive.TabIndex = 17; @@ -133,7 +137,7 @@ "Простая карта", "Карта с грязью", "Карта с кустами"}); - this.comboBoxSelectorMap.Location = new System.Drawing.Point(637, 43); + this.comboBoxSelectorMap.Location = new System.Drawing.Point(837, 90); this.comboBoxSelectorMap.Name = "comboBoxSelectorMap"; this.comboBoxSelectorMap.Size = new System.Drawing.Size(151, 23); this.comboBoxSelectorMap.TabIndex = 22; @@ -141,7 +145,7 @@ // // maskedTextBoxPosition // - this.maskedTextBoxPosition.Location = new System.Drawing.Point(637, 151); + this.maskedTextBoxPosition.Location = new System.Drawing.Point(837, 420); this.maskedTextBoxPosition.Mask = "00"; this.maskedTextBoxPosition.Name = "maskedTextBoxPosition"; this.maskedTextBoxPosition.Size = new System.Drawing.Size(151, 23); @@ -150,7 +154,7 @@ // // buttonRemoveCar // - this.buttonRemoveCar.Location = new System.Drawing.Point(637, 178); + this.buttonRemoveCar.Location = new System.Drawing.Point(837, 449); this.buttonRemoveCar.Name = "buttonRemoveCar"; this.buttonRemoveCar.Size = new System.Drawing.Size(151, 28); this.buttonRemoveCar.TabIndex = 24; @@ -160,7 +164,7 @@ // // buttonShowStorage // - this.buttonShowStorage.Location = new System.Drawing.Point(637, 239); + this.buttonShowStorage.Location = new System.Drawing.Point(837, 483); this.buttonShowStorage.Name = "buttonShowStorage"; this.buttonShowStorage.Size = new System.Drawing.Size(151, 28); this.buttonShowStorage.TabIndex = 25; @@ -170,7 +174,7 @@ // // buttonShowOnMap // - this.buttonShowOnMap.Location = new System.Drawing.Point(637, 328); + this.buttonShowOnMap.Location = new System.Drawing.Point(837, 546); this.buttonShowOnMap.Name = "buttonShowOnMap"; this.buttonShowOnMap.Size = new System.Drawing.Size(151, 28); this.buttonShowOnMap.TabIndex = 26; @@ -178,11 +182,52 @@ this.buttonShowOnMap.UseVisualStyleBackColor = true; this.buttonShowOnMap.Click += new System.EventHandler(this.ButtonShowOnMap_Click); // + // ButtonDeleteMap + // + this.ButtonDeleteMap.Location = new System.Drawing.Point(837, 239); + this.ButtonDeleteMap.Name = "ButtonDeleteMap"; + this.ButtonDeleteMap.Size = new System.Drawing.Size(151, 33); + this.ButtonDeleteMap.TabIndex = 27; + this.ButtonDeleteMap.Text = "Удалить карту"; + this.ButtonDeleteMap.UseVisualStyleBackColor = true; + this.ButtonDeleteMap.Click += new System.EventHandler(this.ButtonDeleteMap_Click); + // + // ListBoxMaps + // + this.ListBoxMaps.FormattingEnabled = true; + this.ListBoxMaps.ItemHeight = 15; + this.ListBoxMaps.Location = new System.Drawing.Point(837, 154); + this.ListBoxMaps.Name = "ListBoxMaps"; + this.ListBoxMaps.Size = new System.Drawing.Size(151, 79); + this.ListBoxMaps.TabIndex = 28; + this.ListBoxMaps.SelectedIndexChanged += new System.EventHandler(this.ListBoxMaps_SelectedIndexChanged); + // + // ButtonAddMap + // + this.ButtonAddMap.Location = new System.Drawing.Point(837, 119); + this.ButtonAddMap.Name = "ButtonAddMap"; + this.ButtonAddMap.Size = new System.Drawing.Size(151, 29); + this.ButtonAddMap.TabIndex = 29; + this.ButtonAddMap.Text = "Добавить карту"; + this.ButtonAddMap.UseVisualStyleBackColor = true; + this.ButtonAddMap.Click += new System.EventHandler(this.ButtonAddMap_Click); + // + // textBoxNewMapName + // + this.textBoxNewMapName.Location = new System.Drawing.Point(837, 61); + this.textBoxNewMapName.Name = "textBoxNewMapName"; + this.textBoxNewMapName.Size = new System.Drawing.Size(151, 23); + this.textBoxNewMapName.TabIndex = 30; + // // FormMapWithSetLocomotive // 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(1000, 722); + this.Controls.Add(this.textBoxNewMapName); + this.Controls.Add(this.ButtonAddMap); + this.Controls.Add(this.ListBoxMaps); + this.Controls.Add(this.ButtonDeleteMap); this.Controls.Add(this.buttonRemoveCar); this.Controls.Add(this.buttonShowStorage); this.Controls.Add(this.buttonShowOnMap); @@ -216,5 +261,9 @@ private Button buttonRemoveCar; private Button buttonShowStorage; private Button buttonShowOnMap; + private Button ButtonDeleteMap; + private ListBox ListBoxMaps; + private Button ButtonAddMap; + private TextBox textBoxNewMapName; } } \ No newline at end of file diff --git a/ElectricLocomotive/ElectricLocomotive/FormMapWithSetLocomotive.cs b/ElectricLocomotive/ElectricLocomotive/FormMapWithSetLocomotive.cs index 1bddbe1..903fcc8 100644 --- a/ElectricLocomotive/ElectricLocomotive/FormMapWithSetLocomotive.cs +++ b/ElectricLocomotive/ElectricLocomotive/FormMapWithSetLocomotive.cs @@ -12,10 +12,42 @@ namespace ElectricLocomotive { public partial class FormMapWithSetLocomotive : Form { - private MapWithSetLocomotivGeneric _mapLocomotivesCollectionGeneric; + private MapWithSetLocomotivGeneric _mapLocomotiveCollectionGeneric; + private readonly Dictionary _mapDict = new() + { + {"Простая карта", new SimpleMap() }, + {"Карта с грязью", new FieldMap() }, + {"Карта с кустами",new BushesMap() } + }; + private readonly MapsCollection _mapsCollection; public FormMapWithSetLocomotive() { InitializeComponent(); + _mapsCollection = new MapsCollection(pictureBox1.Width, pictureBox1.Height); + comboBoxSelectorMap.Items.Clear(); + foreach (var elem in _mapDict) + { + comboBoxSelectorMap.Items.Add(elem.Key); + } + } + private void ReloadMaps() + { + int index = ListBoxMaps.SelectedIndex; + + ListBoxMaps.Items.Clear(); + foreach (var list in _mapsCollection.Keys) + { + ListBoxMaps.Items.Add(list); + } + + 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) @@ -35,16 +67,17 @@ namespace ElectricLocomotive } if (map != null) { - _mapLocomotivesCollectionGeneric = new MapWithSetLocomotivGeneric(pictureBox1.Width, pictureBox1.Height, map); + _mapLocomotiveCollectionGeneric = new MapWithSetLocomotivGeneric( + pictureBox1.Width, pictureBox1.Height, map); } else { - _mapLocomotivesCollectionGeneric = null; + _mapLocomotiveCollectionGeneric = null; } } private void ButtonAddLocomotive_Click(object sender, EventArgs e) { - if (_mapLocomotivesCollectionGeneric == null) + if (ListBoxMaps.SelectedIndex == -1) { return; } @@ -52,10 +85,10 @@ namespace ElectricLocomotive if (form.ShowDialog() == DialogResult.OK) { DrawningObjectLocomotive Locomotive = new(form.SelectedLocomotive); - if (_mapLocomotivesCollectionGeneric + Locomotive != -1) + if (_mapsCollection[ListBoxMaps.SelectedItem?.ToString() ?? string.Empty] + Locomotive != -1) { MessageBox.Show("Объект добавлен"); - pictureBox1.Image = _mapLocomotivesCollectionGeneric.ShowSet(); + pictureBox1.Image = _mapsCollection[ListBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } else { @@ -65,20 +98,23 @@ namespace ElectricLocomotive } private void ButtonRemoveLocomotive_Click(object sender, EventArgs e) { + if (ListBoxMaps.SelectedIndex == -1) + { + return; + } if (string.IsNullOrEmpty(maskedTextBoxPosition.Text)) { return; } - if (MessageBox.Show("Удалить объект?", "Удаление", - MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) + if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) { return; } int pos = Convert.ToInt32(maskedTextBoxPosition.Text); - if (_mapLocomotivesCollectionGeneric - pos != null) + if (_mapsCollection[ListBoxMaps.SelectedItem?.ToString() ?? string.Empty] - pos != null) { MessageBox.Show("Объект удален"); - pictureBox1.Image = _mapLocomotivesCollectionGeneric.ShowSet(); + pictureBox1.Image = _mapsCollection[ListBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } else { @@ -87,23 +123,23 @@ namespace ElectricLocomotive } private void ButtonShowStorage_Click(object sender, EventArgs e) { - if (_mapLocomotivesCollectionGeneric == null) + if (ListBoxMaps.SelectedIndex == -1) { return; } - pictureBox1.Image = _mapLocomotivesCollectionGeneric.ShowSet(); + pictureBox1.Image = _mapsCollection[ListBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } private void ButtonShowOnMap_Click(object sender, EventArgs e) { - if (_mapLocomotivesCollectionGeneric == null) - { - return; - } - pictureBox1.Image = _mapLocomotivesCollectionGeneric.ShowOnMap(); + if (ListBoxMaps.SelectedIndex == -1) + { + return; + } + pictureBox1.Image = _mapsCollection[ListBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowOnMap(); } private void ButtonMove_Click(object sender, EventArgs e) { - if (_mapLocomotivesCollectionGeneric == null) + if (ListBoxMaps.SelectedIndex == -1) { return; } @@ -125,7 +161,39 @@ namespace ElectricLocomotive dir = Direction.Right; break; } - pictureBox1.Image = _mapLocomotivesCollectionGeneric.MoveObject(dir); + pictureBox1.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) + { + pictureBox1.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/ElectricLocomotive/ElectricLocomotive/MapsCollection.cs b/ElectricLocomotive/ElectricLocomotive/MapsCollection.cs new file mode 100644 index 0000000..a0e9286 --- /dev/null +++ b/ElectricLocomotive/ElectricLocomotive/MapsCollection.cs @@ -0,0 +1,44 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ElectricLocomotive +{ + internal class MapsCollection + { + readonly Dictionary> _mapStorages; + public List Keys => _mapStorages.Keys.ToList(); + private readonly int _pictureWidth; + private readonly int _pictureHeight; + public MapsCollection(int pictureWidth, int pictureHeight) + { + _mapStorages = new Dictionary>(); + _pictureWidth = pictureWidth; + _pictureHeight = pictureHeight; + } + public void AddMap(string name, AbstractMap map) + { + if (!_mapStorages.ContainsKey(name)) + { + _mapStorages.Add(name, new MapWithSetLocomotivGeneric(_pictureWidth, _pictureHeight, map)); + } + } + public void DelMap(string name) + { + if (_mapStorages.ContainsKey(name)) _mapStorages.Remove(name); + } + public MapWithSetLocomotivGeneric this[string ind] + { + get + { + if (_mapStorages.ContainsKey(ind)) + { + return _mapStorages[ind]; + } + return null; + } + } + } +}