From a03fc8bb8aaad6757792ee35d95435800824de40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B5=D0=BB=D1=8C=D0=BD=D0=B8=D0=BA=D0=BE=D0=B2=20?= =?UTF-8?q?=D0=98=D0=B3=D0=BE=D1=80=D1=8C?= Date: Wed, 26 Oct 2022 04:58:32 +0400 Subject: [PATCH 1/4] =?UTF-8?q?=D0=BC=D0=B0=D1=81=D1=81=D0=B8=D0=B2=20->?= =?UTF-8?q?=20=D1=81=D0=BF=D0=B8=D1=81=D0=BE=D0=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../MapWithSetLocomotivesGeneric.cs | 20 ++-- .../Locomotives/SetLocomotivesGeneric.cs | 98 +++++++++++-------- 2 files changed, 67 insertions(+), 51 deletions(-) diff --git a/Locomotives/Locomotives/MapWithSetLocomotivesGeneric.cs b/Locomotives/Locomotives/MapWithSetLocomotivesGeneric.cs index e26b025..08f71f9 100644 --- a/Locomotives/Locomotives/MapWithSetLocomotivesGeneric.cs +++ b/Locomotives/Locomotives/MapWithSetLocomotivesGeneric.cs @@ -82,13 +82,9 @@ public Bitmap ShowOnMap() { Shaking(); - for (int i = 0; i < _setLocomotives.Count; i++) + foreach (var locomotive in _setLocomotives.GetLocomotives()) { - var locomotive = _setLocomotives.Get(i); - if (locomotive != null) - { - return _map.CreateMap(_pictureWidth, _pictureHeight, locomotive); - } + return _map.CreateMap(_pictureWidth, _pictureHeight, locomotive); } return new(_pictureWidth, _pictureHeight); } @@ -113,11 +109,11 @@ int j = _setLocomotives.Count - 1; for (int i = 0; i < _setLocomotives.Count; i++) { - if (_setLocomotives.Get(i) == null) + if (_setLocomotives[i] == null) { for (; j > i; j--) { - var locomotive = _setLocomotives.Get(j); + var locomotive = _setLocomotives[j]; if (locomotive != null) { _setLocomotives.Insert(locomotive, i); @@ -161,10 +157,11 @@ int LocomotivesInLine = _pictureWidth / _placeSizeWidth; int CurrentVertPos = 20; int CurrentHorPos = 0; - for (int i = 0; i < _setLocomotives.Count; i++) + int CurrentLocomotiveNumber = 0; + foreach (var locomotive in _setLocomotives.GetLocomotives()) { - _setLocomotives.Get(i)?.SetObject(CurrentHorPos, CurrentVertPos, _pictureWidth, _pictureHeight); - _setLocomotives.Get(i)?.DrawningObject(g); + _setLocomotives[CurrentLocomotiveNumber]?.SetObject(CurrentHorPos, CurrentVertPos, _pictureWidth, _pictureHeight); + _setLocomotives[CurrentLocomotiveNumber]?.DrawningObject(g); if (CurrentHorPos < LocomotivesInLine) { CurrentHorPos += _placeSizeWidth; @@ -174,6 +171,7 @@ CurrentHorPos = 0; CurrentVertPos += _placeSizeHeight; } + CurrentLocomotiveNumber++; } } } diff --git a/Locomotives/Locomotives/SetLocomotivesGeneric.cs b/Locomotives/Locomotives/SetLocomotivesGeneric.cs index 88cee91..96c7182 100644 --- a/Locomotives/Locomotives/SetLocomotivesGeneric.cs +++ b/Locomotives/Locomotives/SetLocomotivesGeneric.cs @@ -4,20 +4,22 @@ where T : class { /// - /// Массив объектов, которые храним + /// Список объектов, которые храним /// - private readonly T[] _places; + private readonly List _places; /// - /// Количество объектов в массиве + /// Количество объектов в списке /// - public int Count => _places.Length; + public int Count => _places.Count; /// /// Конструктор /// /// + private readonly int _maxCount; public SetLocomotivesGeneric(int count) { - _places = new T[count]; + _maxCount = count; + _places = new List(); } /// /// Добавление объекта в набор @@ -26,24 +28,22 @@ /// public int Insert(T locomotive) { - T testLocomotive = _places[0]; - for (int i = 0; i < Count; i++) + if (_places.Count == 0) { - if (_places[i] == null) + _places.Add(locomotive); + return 0; + } + if (Count < _maxCount) + { + for (int i = Count; i >= 1; i--) { - testLocomotive = _places[i]; + _places.Insert(i, _places[i - 1]); + _places.RemoveAt(i - 1); } + _places.Insert(0, locomotive); + return 0; } - if (testLocomotive != null) - { - return -1; - } - for (int i = Count - 2; i >= 0; i--) - { - _places[i + 1] = _places[i]; - } - _places[0] = locomotive; - return 0; + return -1; } /// /// Добавление объекта в набор на конкретную позицию @@ -53,26 +53,16 @@ /// public int Insert(T locomotive, int position) { - if (position < 0 || position >= Count) + if (position < 0 || position > Count + 1) { return position; } - int ClosestNullElementIndex = position; - while (_places[ClosestNullElementIndex] != null) + if (_places[position] == null) { - if (ClosestNullElementIndex == Count) - { - return position; - } - ClosestNullElementIndex++; + _places.Insert(position, locomotive); + return position; } - while (ClosestNullElementIndex != position) - { - _places[ClosestNullElementIndex] = _places[ClosestNullElementIndex - 1]; - ClosestNullElementIndex--; - } - _places[position] = locomotive; - return position; + return -1; } /// /// Удаление объекта из набора с конкретной позиции @@ -81,11 +71,11 @@ /// public int Remove(int position) { - if (_places[position] == null) + if (position > Count || _places[position] == null) { return -1; } - _places[position] = null; + _places.RemoveAt(position); return position; } /// @@ -93,13 +83,41 @@ /// /// /// - public T Get(int position) + public T this[int position] { - if (_places[position] != null) + get { - return _places[position]; + if (_places[position] != null) + { + return _places[position]; + } + else + { + return null; + } + } + set + { + Insert(value, position); + } + } + /// + /// Проход по набору до первого пустого + /// + /// + public IEnumerable GetLocomotives() + { + foreach (var locomotive in _places) + { + if (locomotive != null) + { + yield return locomotive; + } + else + { + yield break; + } } - return null; } } } -- 2.25.1 From 08af1d11adff7cb3d5c4d2ba5bfb4cc98834f13b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B5=D0=BB=D1=8C=D0=BD=D0=B8=D0=BA=D0=BE=D0=B2=20?= =?UTF-8?q?=D0=98=D0=B3=D0=BE=D1=80=D1=8C?= Date: Wed, 26 Oct 2022 06:41:37 +0400 Subject: [PATCH 2/4] =?UTF-8?q?2-3=20=D1=8D=D1=82=D0=B0=D0=BF=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../FormMapWithSetLocomotives.Designer.cs | 122 +++++++++++++---- .../Locomotives/FormMapWithSetLocomotives.cs | 126 +++++++++++++----- Locomotives/Locomotives/MapsCollection.cs | 66 +++++++++ 3 files changed, 251 insertions(+), 63 deletions(-) create mode 100644 Locomotives/Locomotives/MapsCollection.cs diff --git a/Locomotives/Locomotives/FormMapWithSetLocomotives.Designer.cs b/Locomotives/Locomotives/FormMapWithSetLocomotives.Designer.cs index 74cc9e2..bc78814 100644 --- a/Locomotives/Locomotives/FormMapWithSetLocomotives.Designer.cs +++ b/Locomotives/Locomotives/FormMapWithSetLocomotives.Designer.cs @@ -29,6 +29,12 @@ private void InitializeComponent() { this.groupBoxTools = new System.Windows.Forms.GroupBox(); + 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.comboBoxSelectorMap = new System.Windows.Forms.ComboBox(); this.buttonUp = new System.Windows.Forms.Button(); this.buttonDown = new System.Windows.Forms.Button(); this.buttonLeft = new System.Windows.Forms.Button(); @@ -38,14 +44,15 @@ this.buttonRemoveLocomotive = new System.Windows.Forms.Button(); this.maskedTextBoxPosition = new System.Windows.Forms.MaskedTextBox(); this.buttonAddCar = new System.Windows.Forms.Button(); - this.comboBoxSelectorMap = new System.Windows.Forms.ComboBox(); this.pictureBoxLocomotives = new System.Windows.Forms.PictureBox(); this.groupBoxTools.SuspendLayout(); + this.groupBoxMaps.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxLocomotives)).BeginInit(); this.SuspendLayout(); // // groupBoxTools // + this.groupBoxTools.Controls.Add(this.groupBoxMaps); this.groupBoxTools.Controls.Add(this.buttonUp); this.groupBoxTools.Controls.Add(this.buttonDown); this.groupBoxTools.Controls.Add(this.buttonLeft); @@ -55,21 +62,85 @@ this.groupBoxTools.Controls.Add(this.buttonRemoveLocomotive); this.groupBoxTools.Controls.Add(this.maskedTextBoxPosition); this.groupBoxTools.Controls.Add(this.buttonAddCar); - this.groupBoxTools.Controls.Add(this.comboBoxSelectorMap); this.groupBoxTools.Dock = System.Windows.Forms.DockStyle.Right; - this.groupBoxTools.Location = new System.Drawing.Point(577, 0); + this.groupBoxTools.Location = new System.Drawing.Point(950, 0); this.groupBoxTools.Name = "groupBoxTools"; - this.groupBoxTools.Size = new System.Drawing.Size(223, 450); + this.groupBoxTools.Size = new System.Drawing.Size(223, 676); this.groupBoxTools.TabIndex = 0; this.groupBoxTools.TabStop = false; this.groupBoxTools.Text = "Инструменты"; // + // groupBoxMaps + // + this.groupBoxMaps.Anchor = System.Windows.Forms.AnchorStyles.Right; + 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(6, 18); + this.groupBoxMaps.Name = "groupBoxMaps"; + this.groupBoxMaps.Size = new System.Drawing.Size(217, 267); + this.groupBoxMaps.TabIndex = 11; + this.groupBoxMaps.TabStop = false; + this.groupBoxMaps.Text = "Карты"; + // + // buttonDeleteMap + // + this.buttonDeleteMap.Location = new System.Drawing.Point(25, 235); + this.buttonDeleteMap.Name = "buttonDeleteMap"; + this.buttonDeleteMap.Size = new System.Drawing.Size(164, 26); + this.buttonDeleteMap.TabIndex = 14; + 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(25, 112); + this.listBoxMaps.Name = "listBoxMaps"; + this.listBoxMaps.Size = new System.Drawing.Size(164, 109); + this.listBoxMaps.TabIndex = 13; + this.listBoxMaps.SelectedIndexChanged += new System.EventHandler(this.ListBoxMaps_SelectedIndexChanged); + // + // buttonAddMap + // + this.buttonAddMap.Location = new System.Drawing.Point(25, 80); + this.buttonAddMap.Name = "buttonAddMap"; + this.buttonAddMap.Size = new System.Drawing.Size(164, 26); + this.buttonAddMap.TabIndex = 12; + this.buttonAddMap.Text = "Добавить карту"; + this.buttonAddMap.UseVisualStyleBackColor = true; + this.buttonAddMap.Click += new System.EventHandler(this.ButtonAddMap_Click); + // + // textBoxNewMapName + // + this.textBoxNewMapName.Location = new System.Drawing.Point(25, 22); + this.textBoxNewMapName.Name = "textBoxNewMapName"; + this.textBoxNewMapName.Size = new System.Drawing.Size(164, 23); + this.textBoxNewMapName.TabIndex = 0; + // + // 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(25, 51); + this.comboBoxSelectorMap.Name = "comboBoxSelectorMap"; + this.comboBoxSelectorMap.Size = new System.Drawing.Size(164, 23); + this.comboBoxSelectorMap.TabIndex = 0; + // // buttonUp // this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonUp.BackgroundImage = global::Locomotives.Properties.Resources.ArrowUp; this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonUp.Location = new System.Drawing.Point(107, 378); + this.buttonUp.Location = new System.Drawing.Point(107, 604); this.buttonUp.Name = "buttonUp"; this.buttonUp.Size = new System.Drawing.Size(30, 30); this.buttonUp.TabIndex = 10; @@ -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::Locomotives.Properties.Resources.ArrowDown; this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonDown.Location = new System.Drawing.Point(107, 414); + this.buttonDown.Location = new System.Drawing.Point(107, 640); this.buttonDown.Name = "buttonDown"; this.buttonDown.Size = new System.Drawing.Size(30, 30); this.buttonDown.TabIndex = 9; @@ -93,7 +164,7 @@ this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonLeft.BackgroundImage = global::Locomotives.Properties.Resources.ArrowLeft; this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonLeft.Location = new System.Drawing.Point(71, 414); + this.buttonLeft.Location = new System.Drawing.Point(71, 640); this.buttonLeft.Name = "buttonLeft"; this.buttonLeft.Size = new System.Drawing.Size(30, 30); this.buttonLeft.TabIndex = 8; @@ -105,7 +176,7 @@ this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonRight.BackgroundImage = global::Locomotives.Properties.Resources.ArrowRight; this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonRight.Location = new System.Drawing.Point(143, 414); + this.buttonRight.Location = new System.Drawing.Point(143, 640); this.buttonRight.Name = "buttonRight"; this.buttonRight.Size = new System.Drawing.Size(30, 30); this.buttonRight.TabIndex = 7; @@ -114,7 +185,7 @@ // // buttonShowOnMap // - this.buttonShowOnMap.Location = new System.Drawing.Point(31, 314); + this.buttonShowOnMap.Location = new System.Drawing.Point(31, 426); this.buttonShowOnMap.Name = "buttonShowOnMap"; this.buttonShowOnMap.Size = new System.Drawing.Size(164, 26); this.buttonShowOnMap.TabIndex = 5; @@ -124,7 +195,7 @@ // // buttonShowStorage // - this.buttonShowStorage.Location = new System.Drawing.Point(31, 217); + this.buttonShowStorage.Location = new System.Drawing.Point(31, 394); this.buttonShowStorage.Name = "buttonShowStorage"; this.buttonShowStorage.Size = new System.Drawing.Size(164, 26); this.buttonShowStorage.TabIndex = 4; @@ -134,7 +205,7 @@ // // buttonRemoveLocomotive // - this.buttonRemoveLocomotive.Location = new System.Drawing.Point(31, 156); + this.buttonRemoveLocomotive.Location = new System.Drawing.Point(31, 362); this.buttonRemoveLocomotive.Name = "buttonRemoveLocomotive"; this.buttonRemoveLocomotive.Size = new System.Drawing.Size(164, 26); this.buttonRemoveLocomotive.TabIndex = 3; @@ -144,7 +215,7 @@ // // maskedTextBoxPosition // - this.maskedTextBoxPosition.Location = new System.Drawing.Point(31, 127); + this.maskedTextBoxPosition.Location = new System.Drawing.Point(31, 333); this.maskedTextBoxPosition.Mask = "00"; this.maskedTextBoxPosition.Name = "maskedTextBoxPosition"; this.maskedTextBoxPosition.Size = new System.Drawing.Size(164, 23); @@ -152,7 +223,7 @@ // // buttonAddCar // - this.buttonAddCar.Location = new System.Drawing.Point(31, 72); + this.buttonAddCar.Location = new System.Drawing.Point(31, 291); this.buttonAddCar.Name = "buttonAddCar"; this.buttonAddCar.Size = new System.Drawing.Size(164, 26); this.buttonAddCar.TabIndex = 1; @@ -160,26 +231,12 @@ this.buttonAddCar.UseVisualStyleBackColor = true; this.buttonAddCar.Click += new System.EventHandler(this.ButtonAddLocomotive_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(31, 22); - this.comboBoxSelectorMap.Name = "comboBoxSelectorMap"; - this.comboBoxSelectorMap.Size = new System.Drawing.Size(164, 23); - this.comboBoxSelectorMap.TabIndex = 0; - this.comboBoxSelectorMap.SelectedIndexChanged += new System.EventHandler(this.ComboBoxSelectorMap_SelectedIndexChanged); - // // pictureBoxLocomotives // this.pictureBoxLocomotives.Dock = System.Windows.Forms.DockStyle.Fill; this.pictureBoxLocomotives.Location = new System.Drawing.Point(0, 0); this.pictureBoxLocomotives.Name = "pictureBoxLocomotives"; - this.pictureBoxLocomotives.Size = new System.Drawing.Size(577, 450); + this.pictureBoxLocomotives.Size = new System.Drawing.Size(950, 676); this.pictureBoxLocomotives.TabIndex = 1; this.pictureBoxLocomotives.TabStop = false; // @@ -187,13 +244,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(1173, 676); this.Controls.Add(this.pictureBoxLocomotives); this.Controls.Add(this.groupBoxTools); this.Name = "FormMapWithSetLocomotives"; this.Text = "Карта с набором объектов"; this.groupBoxTools.ResumeLayout(false); this.groupBoxTools.PerformLayout(); + this.groupBoxMaps.ResumeLayout(false); + this.groupBoxMaps.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxLocomotives)).EndInit(); this.ResumeLayout(false); @@ -213,5 +272,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/Locomotives/Locomotives/FormMapWithSetLocomotives.cs b/Locomotives/Locomotives/FormMapWithSetLocomotives.cs index 9b1e87c..eb7fe48 100644 --- a/Locomotives/Locomotives/FormMapWithSetLocomotives.cs +++ b/Locomotives/Locomotives/FormMapWithSetLocomotives.cs @@ -6,48 +6,101 @@ public partial class FormMapWithSetLocomotives : Form { /// - /// Объект от класса карты с набором объектов + /// Словарь для выпадающего списка /// - private MapWithSetLocomotivesGeneric _mapCarsCollectionGeneric; + private readonly Dictionary _mapsDict = new() + { + {"Простая карта", new SimpleMap()}, + {"Карта с крестом", new CrossMap()}, + {"Карта с дорожками", new RoadsMap()}, + }; + /// + /// Объект от коллекции карт + /// + private readonly MapsCollection _mapsCollection; /// /// Конструктор /// public FormMapWithSetLocomotives() { InitializeComponent(); + _mapsCollection = new MapsCollection(pictureBoxLocomotives.Width, pictureBoxLocomotives.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 ComboBoxSelectorMap_SelectedIndexChanged(object sender, EventArgs e) + private void ListBoxMaps_SelectedIndexChanged(object sender, EventArgs e) { - AbstractMap map = null; - switch (comboBoxSelectorMap.Text) + pictureBoxLocomotives.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); + } + /// + /// Удаление карты + /// + /// + /// + private void ButtonDeleteMap_Click(object sender, EventArgs e) + { + if (listBoxMaps.SelectedIndex == -1) { - case "Простая карта": - map = new SimpleMap(); - break; - case "Карта с крестом": - map = new CrossMap(); - break; - case "Карта с дорожками": - map = new RoadsMap(); - break; - default: - break; + return; } - if (map != null) + if (MessageBox.Show($"Удалить карту {listBoxMaps.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { - _mapCarsCollectionGeneric = new - MapWithSetLocomotivesGeneric(pictureBoxLocomotives.Width, pictureBoxLocomotives.Height, map); - } - else - { - _mapCarsCollectionGeneric = null; + _mapsCollection.DelMap(listBoxMaps.SelectedItem?.ToString() ?? string.Empty); + ReloadMaps(); } } + /// /// Добавление объекта /// @@ -55,7 +108,7 @@ /// private void ButtonAddLocomotive_Click(object sender, EventArgs e) { - if (_mapCarsCollectionGeneric == null) + if (listBoxMaps.SelectedIndex == -1) { return; } @@ -63,10 +116,10 @@ if (form.ShowDialog() == DialogResult.OK) { DrawningObjectLocomotive locomotive = new(form.SelectedLocomotive); - if ((_mapCarsCollectionGeneric + locomotive) > -1) + if ((_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + locomotive) > -1) { MessageBox.Show("Объект добавлен"); - pictureBoxLocomotives.Image = _mapCarsCollectionGeneric.ShowSet(); + pictureBoxLocomotives.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } else { @@ -81,6 +134,10 @@ /// private void ButtonRemoveLocomotive_Click(object sender, EventArgs e) { + if (listBoxMaps.SelectedIndex == -1) + { + return; + } if (string.IsNullOrEmpty(maskedTextBoxPosition.Text)) { return; @@ -90,15 +147,16 @@ return; } int pos = Convert.ToInt32(maskedTextBoxPosition.Text); - if ((_mapCarsCollectionGeneric - pos) > -1) + if ((_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] - pos) > -1) { MessageBox.Show("Объект удален"); - pictureBoxLocomotives.Image = _mapCarsCollectionGeneric.ShowSet(); + pictureBoxLocomotives.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } else { MessageBox.Show("Не удалось удалить объект"); } + } /// /// Вывод набора @@ -107,11 +165,11 @@ /// private void ButtonShowStorage_Click(object sender, EventArgs e) { - if (_mapCarsCollectionGeneric == null) + if (listBoxMaps.SelectedIndex == -1) { return; } - pictureBoxLocomotives.Image = _mapCarsCollectionGeneric.ShowSet(); + pictureBoxLocomotives.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } /// /// Вывод карты @@ -120,11 +178,11 @@ /// private void ButtonShowOnMap_Click(object sender, EventArgs e) { - if (_mapCarsCollectionGeneric == null) + if (listBoxMaps.SelectedIndex == -1) { return; } - pictureBoxLocomotives.Image = _mapCarsCollectionGeneric.ShowOnMap(); + pictureBoxLocomotives.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowOnMap(); } /// /// Перемещение @@ -133,7 +191,7 @@ /// private void ButtonMove_Click(object sender, EventArgs e) { - if (_mapCarsCollectionGeneric == null) + if (listBoxMaps.SelectedIndex == -1) { return; } @@ -155,7 +213,7 @@ dir = Direction.Right; break; } - pictureBoxLocomotives.Image = _mapCarsCollectionGeneric.MoveObject(dir); + pictureBoxLocomotives.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].MoveObject(dir); } } } diff --git a/Locomotives/Locomotives/MapsCollection.cs b/Locomotives/Locomotives/MapsCollection.cs new file mode 100644 index 0000000..700e25c --- /dev/null +++ b/Locomotives/Locomotives/MapsCollection.cs @@ -0,0 +1,66 @@ +namespace Locomotives +{ + /// + /// Класс для хранения коллекции карт + /// + 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) + { + _mapStorages.Add(name, new MapWithSetLocomotivesGeneric(_pictureWidth, _pictureHeight, map)); + } + /// + /// Удаление карты + /// + /// Название карты + public void DelMap(string name) + { + _mapStorages.Remove(name); + } + /// + /// Доступ к парковке + /// + /// + /// + public MapWithSetLocomotivesGeneric this[string ind] + { + get + { + return _mapStorages[ind]; + } + } + } + +} -- 2.25.1 From dba9d94527559cbb1291f36f2defe9f4dfe2645e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B5=D0=BB=D1=8C=D0=BD=D0=B8=D0=BA=D0=BE=D0=B2=20?= =?UTF-8?q?=D0=98=D0=B3=D0=BE=D1=80=D1=8C?= Date: Wed, 26 Oct 2022 20:47:14 +0400 Subject: [PATCH 3/4] =?UTF-8?q?=D0=9A=D0=BE=D1=80=D1=80=D0=B5=D0=BA=D1=82?= =?UTF-8?q?=D0=B8=D1=80=D0=BE=D0=B2=D0=BA=D0=B0=20=D1=84=D0=BE=D1=80=D0=BC?= =?UTF-8?q?=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Locomotives/Locomotives/FormMapWithSetLocomotives.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Locomotives/Locomotives/FormMapWithSetLocomotives.cs b/Locomotives/Locomotives/FormMapWithSetLocomotives.cs index eb7fe48..bfca24b 100644 --- a/Locomotives/Locomotives/FormMapWithSetLocomotives.cs +++ b/Locomotives/Locomotives/FormMapWithSetLocomotives.cs @@ -72,6 +72,7 @@ return; } _mapsCollection.AddMap(textBoxNewMapName.Text, _mapsDict[comboBoxSelectorMap.Text]); + textBoxNewMapName.Text = ""; ReloadMaps(); } /// @@ -81,7 +82,7 @@ /// private void ListBoxMaps_SelectedIndexChanged(object sender, EventArgs e) { - pictureBoxLocomotives.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); + pictureBoxLocomotives.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowOnMap(); ; } /// /// Удаление карты @@ -99,6 +100,7 @@ _mapsCollection.DelMap(listBoxMaps.SelectedItem?.ToString() ?? string.Empty); ReloadMaps(); } + MessageBox.Show("Карта удалена"); } /// -- 2.25.1 From aa8035f87e74ad199aa5de78635e7103b342ce7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B5=D0=BB=D1=8C=D0=BD=D0=B8=D0=BA=D0=BE=D0=B2=20?= =?UTF-8?q?=D0=98=D0=B3=D0=BE=D1=80=D1=8C?= Date: Thu, 27 Oct 2022 15:27:30 +0400 Subject: [PATCH 4/4] =?UTF-8?q?=D0=A3=D1=81=D1=82=D1=80=D0=B0=D0=BD=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D0=B7=D0=B0=D0=BC=D0=B5=D1=87=D0=B0=D0=BD?= =?UTF-8?q?=D0=B8=D0=B9=20=D0=BF=D1=80=D0=B5=D0=BF=D0=BE=D0=B4=D0=B0=D0=B2?= =?UTF-8?q?=D0=B0=D1=82=D0=B5=D0=BB=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Locomotives/Locomotives/FormMapWithSetLocomotives.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Locomotives/Locomotives/FormMapWithSetLocomotives.cs b/Locomotives/Locomotives/FormMapWithSetLocomotives.cs index bfca24b..db2e33c 100644 --- a/Locomotives/Locomotives/FormMapWithSetLocomotives.cs +++ b/Locomotives/Locomotives/FormMapWithSetLocomotives.cs @@ -82,7 +82,7 @@ /// private void ListBoxMaps_SelectedIndexChanged(object sender, EventArgs e) { - pictureBoxLocomotives.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowOnMap(); ; + pictureBoxLocomotives.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } /// /// Удаление карты -- 2.25.1