From 9e1d8bf1ac09d65869204cdc9d67c7aee161d64c Mon Sep 17 00:00:00 2001 From: Danil Malin Date: Tue, 11 Oct 2022 12:29:06 +0400 Subject: [PATCH 1/5] =?UTF-8?q?=D0=A7=D0=B5=D1=82=D0=B2=D0=B5=D1=80=D1=82?= =?UTF-8?q?=D0=B0=D1=8F=20=D0=BB=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82=D0=BE?= =?UTF-8?q?=D1=80=D0=BD=D0=B0=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../FormMapWithSetLocomotives.Designer.cs | 123 ++++++++++++----- .../FormMapWithSetLocomotives.cs | 126 +++++++++++++----- .../MapWithSetLocomotivesGeneric.cs | 17 +-- .../WarmlyLocomotive/MapsCollection.cs | 87 ++++++++++++ .../WarmlyLocomotive/SetLocomotivesGeneric.cs | 123 ++++++++--------- 5 files changed, 335 insertions(+), 141 deletions(-) create mode 100644 WarmlyLocomotive/WarmlyLocomotive/MapsCollection.cs diff --git a/WarmlyLocomotive/WarmlyLocomotive/FormMapWithSetLocomotives.Designer.cs b/WarmlyLocomotive/WarmlyLocomotive/FormMapWithSetLocomotives.Designer.cs index f17a7ae..29195a6 100644 --- a/WarmlyLocomotive/WarmlyLocomotive/FormMapWithSetLocomotives.Designer.cs +++ b/WarmlyLocomotive/WarmlyLocomotive/FormMapWithSetLocomotives.Designer.cs @@ -29,6 +29,12 @@ private void InitializeComponent() { this.groupBox = 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.buttonDown = new System.Windows.Forms.Button(); this.buttonRight = 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.buttonAddLocomotive = new System.Windows.Forms.Button(); - this.comboBoxSelectorMap = new System.Windows.Forms.ComboBox(); this.pictureBox = new System.Windows.Forms.PictureBox(); this.groupBox.SuspendLayout(); + this.groupBoxMaps.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit(); this.SuspendLayout(); // // groupBox // + this.groupBox.Controls.Add(this.groupBoxMaps); this.groupBox.Controls.Add(this.buttonDown); this.groupBox.Controls.Add(this.buttonRight); this.groupBox.Controls.Add(this.buttonLeft); @@ -55,21 +62,85 @@ this.groupBox.Controls.Add(this.buttonRemoveLocomotive); this.groupBox.Controls.Add(this.maskedTextBoxPosition); this.groupBox.Controls.Add(this.buttonAddLocomotive); - this.groupBox.Controls.Add(this.comboBoxSelectorMap); this.groupBox.Dock = System.Windows.Forms.DockStyle.Right; - this.groupBox.Location = new System.Drawing.Point(600, 0); + this.groupBox.Location = new System.Drawing.Point(668, 0); this.groupBox.Name = "groupBox"; - this.groupBox.Size = new System.Drawing.Size(200, 450); + this.groupBox.Size = new System.Drawing.Size(200, 629); this.groupBox.TabIndex = 0; this.groupBox.TabStop = false; this.groupBox.Text = "Инструменты"; // + // 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(6, 22); + this.groupBoxMaps.Name = "groupBoxMaps"; + this.groupBoxMaps.Size = new System.Drawing.Size(188, 261); + this.groupBoxMaps.TabIndex = 10; + this.groupBoxMaps.TabStop = false; + this.groupBoxMaps.Text = "Карты"; + // + // buttonDeleteMap + // + this.buttonDeleteMap.Location = new System.Drawing.Point(13, 220); + this.buttonDeleteMap.Name = "buttonDeleteMap"; + this.buttonDeleteMap.Size = new System.Drawing.Size(169, 34); + this.buttonDeleteMap.TabIndex = 3; + 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(13, 120); + this.listBoxMaps.Name = "listBoxMaps"; + this.listBoxMaps.Size = new System.Drawing.Size(169, 94); + this.listBoxMaps.TabIndex = 2; + this.listBoxMaps.SelectedIndexChanged += new System.EventHandler(this.ListBoxMaps_SelectedIndexChanged); + // + // buttonAddMap + // + this.buttonAddMap.Location = new System.Drawing.Point(13, 80); + this.buttonAddMap.Name = "buttonAddMap"; + this.buttonAddMap.Size = new System.Drawing.Size(169, 34); + this.buttonAddMap.TabIndex = 1; + this.buttonAddMap.Text = "Добавить карту"; + this.buttonAddMap.UseVisualStyleBackColor = true; + this.buttonAddMap.Click += new System.EventHandler(this.ButtonAddMap_Click); + // + // textBoxNewMapName + // + this.textBoxNewMapName.Location = new System.Drawing.Point(13, 22); + this.textBoxNewMapName.Name = "textBoxNewMapName"; + this.textBoxNewMapName.Size = new System.Drawing.Size(169, 23); + this.textBoxNewMapName.TabIndex = 0; + // + // comboBoxSelectorMap + // + this.comboBoxSelectorMap.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + 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(13, 46); + this.comboBoxSelectorMap.Name = "comboBoxSelectorMap"; + this.comboBoxSelectorMap.Size = new System.Drawing.Size(169, 23); + this.comboBoxSelectorMap.TabIndex = 0; + // // buttonDown // this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonDown.BackgroundImage = global::WarmlyLocomotive.Properties.Resources.arrowDown; this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonDown.Location = new System.Drawing.Point(87, 403); + this.buttonDown.Location = new System.Drawing.Point(87, 582); this.buttonDown.Name = "buttonDown"; this.buttonDown.Size = new System.Drawing.Size(30, 30); this.buttonDown.TabIndex = 9; @@ -81,7 +152,7 @@ this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonRight.BackgroundImage = global::WarmlyLocomotive.Properties.Resources.arrowRight; this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonRight.Location = new System.Drawing.Point(123, 403); + this.buttonRight.Location = new System.Drawing.Point(123, 582); this.buttonRight.Name = "buttonRight"; this.buttonRight.Size = new System.Drawing.Size(30, 30); this.buttonRight.TabIndex = 8; @@ -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::WarmlyLocomotive.Properties.Resources.arrowLeft; this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonLeft.Location = new System.Drawing.Point(51, 403); + this.buttonLeft.Location = new System.Drawing.Point(51, 582); this.buttonLeft.Name = "buttonLeft"; this.buttonLeft.Size = new System.Drawing.Size(30, 30); this.buttonLeft.TabIndex = 7; @@ -105,7 +176,7 @@ this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonUp.BackgroundImage = global::WarmlyLocomotive.Properties.Resources.arrowUp; this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonUp.Location = new System.Drawing.Point(87, 367); + this.buttonUp.Location = new System.Drawing.Point(87, 546); this.buttonUp.Name = "buttonUp"; this.buttonUp.Size = new System.Drawing.Size(30, 30); this.buttonUp.TabIndex = 6; @@ -115,7 +186,7 @@ // buttonShowOnMap // this.buttonShowOnMap.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.buttonShowOnMap.Location = new System.Drawing.Point(19, 312); + this.buttonShowOnMap.Location = new System.Drawing.Point(19, 491); this.buttonShowOnMap.Name = "buttonShowOnMap"; this.buttonShowOnMap.Size = new System.Drawing.Size(169, 30); this.buttonShowOnMap.TabIndex = 5; @@ -126,7 +197,7 @@ // buttonShowStorage // this.buttonShowStorage.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.buttonShowStorage.Location = new System.Drawing.Point(19, 233); + this.buttonShowStorage.Location = new System.Drawing.Point(19, 443); this.buttonShowStorage.Name = "buttonShowStorage"; this.buttonShowStorage.Size = new System.Drawing.Size(169, 30); this.buttonShowStorage.TabIndex = 4; @@ -137,7 +208,7 @@ // buttonRemoveLocomotive // this.buttonRemoveLocomotive.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.buttonRemoveLocomotive.Location = new System.Drawing.Point(19, 171); + this.buttonRemoveLocomotive.Location = new System.Drawing.Point(19, 397); this.buttonRemoveLocomotive.Name = "buttonRemoveLocomotive"; this.buttonRemoveLocomotive.Size = new System.Drawing.Size(169, 30); this.buttonRemoveLocomotive.TabIndex = 3; @@ -148,7 +219,7 @@ // maskedTextBoxPosition // this.maskedTextBoxPosition.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.maskedTextBoxPosition.Location = new System.Drawing.Point(19, 142); + this.maskedTextBoxPosition.Location = new System.Drawing.Point(19, 368); this.maskedTextBoxPosition.Mask = "00"; this.maskedTextBoxPosition.Name = "maskedTextBoxPosition"; this.maskedTextBoxPosition.Size = new System.Drawing.Size(169, 23); @@ -157,7 +228,7 @@ // buttonAddLocomotive // this.buttonAddLocomotive.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.buttonAddLocomotive.Location = new System.Drawing.Point(19, 90); + this.buttonAddLocomotive.Location = new System.Drawing.Point(19, 332); this.buttonAddLocomotive.Name = "buttonAddLocomotive"; this.buttonAddLocomotive.Size = new System.Drawing.Size(169, 30); this.buttonAddLocomotive.TabIndex = 1; @@ -165,27 +236,12 @@ this.buttonAddLocomotive.UseVisualStyleBackColor = true; this.buttonAddLocomotive.Click += new System.EventHandler(this.ButtonAddLocomotive_Click); // - // comboBoxSelectorMap - // - this.comboBoxSelectorMap.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - 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(19, 33); - this.comboBoxSelectorMap.Name = "comboBoxSelectorMap"; - this.comboBoxSelectorMap.Size = new System.Drawing.Size(169, 23); - 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(600, 450); + this.pictureBox.Size = new System.Drawing.Size(668, 629); this.pictureBox.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize; this.pictureBox.TabIndex = 1; this.pictureBox.TabStop = false; @@ -194,13 +250,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(868, 629); this.Controls.Add(this.pictureBox); this.Controls.Add(this.groupBox); this.Name = "FormMapWithSetLocomotives"; this.Text = "FormMapWithSetLocomotives"; this.groupBox.ResumeLayout(false); this.groupBox.PerformLayout(); + this.groupBoxMaps.ResumeLayout(false); + this.groupBoxMaps.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit(); this.ResumeLayout(false); this.PerformLayout(); @@ -221,5 +279,10 @@ private Button buttonAddLocomotive; private ComboBox comboBoxSelectorMap; private PictureBox pictureBox; + private GroupBox groupBoxMaps; + private Button buttonDeleteMap; + private ListBox listBoxMaps; + private Button buttonAddMap; + private TextBox textBoxNewMapName; } } \ No newline at end of file diff --git a/WarmlyLocomotive/WarmlyLocomotive/FormMapWithSetLocomotives.cs b/WarmlyLocomotive/WarmlyLocomotive/FormMapWithSetLocomotives.cs index f512cb6..5e890ab 100644 --- a/WarmlyLocomotive/WarmlyLocomotive/FormMapWithSetLocomotives.cs +++ b/WarmlyLocomotive/WarmlyLocomotive/FormMapWithSetLocomotives.cs @@ -14,41 +14,50 @@ namespace WarmlyLocomotive public partial class FormMapWithSetLocomotives : Form { /// - /// Объект от класса карты с набором объектов + /// Словарь для выпадающего списка + /// + private readonly Dictionary _mapsDict = new() + { + { "Простая карта", new SimpleMap() }, + {"Карта с большими барьерами", new BigBarriersMap() }, + {"Карта с круглыми барьерами", new CirclesMap() } + }; + /// + /// Объект от коллекции карт + /// + private readonly MapsCollection _mapsCollection; + /// + /// Конструктор /// - private MapWithSetLocomotivesGeneric _mapLocomotivesCollectionGeneric; public FormMapWithSetLocomotives() { 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 BigBarriersMap(); - break; - case "Карта с круглыми барьерами": - map = new CirclesMap(); - break; + listBoxMaps.Items.Add(_mapsCollection.Keys[i]); } - if (map != null) + if (listBoxMaps.Items.Count > 0 && (index == -1 || index >= listBoxMaps.Items.Count)) { - _mapLocomotivesCollectionGeneric = new MapWithSetLocomotivesGeneric( - pictureBox.Width, pictureBox.Height, map); + listBoxMaps.SelectedIndex = 0; } - else + else if (listBoxMaps.Items.Count > 0 && index > -1 && index < listBoxMaps.Items.Count) { - _mapLocomotivesCollectionGeneric = null; + listBoxMaps.SelectedIndex = index; } } /// @@ -58,7 +67,7 @@ namespace WarmlyLocomotive /// private void ButtonAddLocomotive_Click(object sender, EventArgs e) { - if (_mapLocomotivesCollectionGeneric == null) + if (listBoxMaps.SelectedIndex == -1) { return; } @@ -66,10 +75,10 @@ namespace WarmlyLocomotive if (form.ShowDialog() == DialogResult.OK) { DrawningObjectLocomotive car = new(form.SelectedLocomotive); - if (_mapLocomotivesCollectionGeneric + car != -1) + if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + car != -1) { MessageBox.Show("Объект добавлен"); - pictureBox.Image = _mapLocomotivesCollectionGeneric.ShowSet(); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } else { @@ -84,6 +93,10 @@ namespace WarmlyLocomotive /// private void ButtonRemoveLocomotive_Click(object sender, EventArgs e) { + if (listBoxMaps.SelectedIndex == -1) + { + return; + } if (string.IsNullOrEmpty(maskedTextBoxPosition.Text)) { return; @@ -93,10 +106,10 @@ namespace WarmlyLocomotive return; } int pos = Convert.ToInt32(maskedTextBoxPosition.Text); - if (_mapLocomotivesCollectionGeneric - pos != null) + if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] - pos != null) { MessageBox.Show("Объект удален"); - pictureBox.Image = _mapLocomotivesCollectionGeneric.ShowSet(); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } else { @@ -110,11 +123,11 @@ namespace WarmlyLocomotive /// private void ButtonShowStorage_Click(object sender, EventArgs e) { - if (_mapLocomotivesCollectionGeneric == null) + if (listBoxMaps.SelectedIndex == -1) { return; } - pictureBox.Image = _mapLocomotivesCollectionGeneric.ShowSet(); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } /// /// Вывод карты @@ -123,11 +136,11 @@ namespace WarmlyLocomotive /// private void ButtonShowOnMap_Click(object sender, EventArgs e) { - if (_mapLocomotivesCollectionGeneric == null) + if (listBoxMaps.SelectedIndex == -1) { return; } - pictureBox.Image = _mapLocomotivesCollectionGeneric.ShowOnMap(); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowOnMap(); } /// /// Перемещение @@ -136,7 +149,7 @@ namespace WarmlyLocomotive /// private void ButtonMove_Click(object sender, EventArgs e) { - if (_mapLocomotivesCollectionGeneric == null) + if (listBoxMaps.SelectedIndex == -1) { return; } @@ -158,7 +171,54 @@ namespace WarmlyLocomotive dir = Direction.Right; break; } - pictureBox.Image = _mapLocomotivesCollectionGeneric.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(); + } } } } diff --git a/WarmlyLocomotive/WarmlyLocomotive/MapWithSetLocomotivesGeneric.cs b/WarmlyLocomotive/WarmlyLocomotive/MapWithSetLocomotivesGeneric.cs index 297daad..ab5b5d2 100644 --- a/WarmlyLocomotive/WarmlyLocomotive/MapWithSetLocomotivesGeneric.cs +++ b/WarmlyLocomotive/WarmlyLocomotive/MapWithSetLocomotivesGeneric.cs @@ -88,13 +88,9 @@ namespace WarmlyLocomotive 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); } @@ -119,11 +115,11 @@ namespace WarmlyLocomotive 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); @@ -179,7 +175,7 @@ namespace WarmlyLocomotive int xForLocomotive = 2; int yForLocomotive = 10; int countInRow = 0; - for (int i = 0; i < _setLocomotives.Count; i++) + foreach (var locomotive in _setLocomotives.GetLocomotives()) { if (countInRow >= _pictureWidth / (_placeSizeWidth + 30)) { @@ -187,9 +183,8 @@ namespace WarmlyLocomotive yForLocomotive += _placeSizeHeight; countInRow = 0; } - if (_setLocomotives.Get(i) != null) + if (locomotive != null) { - T locomotive = _setLocomotives.Get(i); locomotive.SetObject(xForLocomotive, yForLocomotive, _pictureWidth, _pictureHeight); locomotive.DrawningObject(g); } diff --git a/WarmlyLocomotive/WarmlyLocomotive/MapsCollection.cs b/WarmlyLocomotive/WarmlyLocomotive/MapsCollection.cs new file mode 100644 index 0000000..e172901 --- /dev/null +++ b/WarmlyLocomotive/WarmlyLocomotive/MapsCollection.cs @@ -0,0 +1,87 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace WarmlyLocomotive +{ + /// + /// Класс для хранения коллекции карт + /// + 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 MapWithSetLocomotivesGeneric(_pictureWidth, _pictureHeight, map)); + } + } + /// + /// Удаление карты + /// + /// Название карты + public void DelMap(string name) + { + // TODO Прописать логику для удаления + if (_mapStorages.ContainsKey(name)) + { + _mapStorages.Remove(name); + } + } + /// + /// Доступ к парковке + /// + /// + /// + public MapWithSetLocomotivesGeneric this[string ind] + { + get + { + // TODO Продумать логику получения объекта + if (ind != String.Empty) + { + MapWithSetLocomotivesGeneric value; + if (_mapStorages.TryGetValue(ind, out value)) + { + return value; + } + } + return null; + } + } + } +} diff --git a/WarmlyLocomotive/WarmlyLocomotive/SetLocomotivesGeneric.cs b/WarmlyLocomotive/WarmlyLocomotive/SetLocomotivesGeneric.cs index eec3dba..edcd3c2 100644 --- a/WarmlyLocomotive/WarmlyLocomotive/SetLocomotivesGeneric.cs +++ b/WarmlyLocomotive/WarmlyLocomotive/SetLocomotivesGeneric.cs @@ -10,20 +10,22 @@ namespace WarmlyLocomotive 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(); } /// /// Добавление объекта в набор @@ -32,30 +34,14 @@ namespace WarmlyLocomotive /// public int Insert(T locomotive) { - bool hasEmptySpace = false; - int indexOfEmptyPlace = 0; - for (int i = 0; i < Count; ++i) - { - if (_places[i] == null || _places[i] == default(T)) - { - hasEmptySpace = true; - indexOfEmptyPlace = i; - break; - } - } - if (hasEmptySpace) - { - for (int i = indexOfEmptyPlace; i > 0; --i) - { - _places[i] = _places[i - 1]; - } - _places[0] = locomotive; - return 0; - } - else + // TODO вставка в начало набора + // TODO проверка на _maxCount + if (_maxCount == Count) { return -1; } + _places.Insert(0, locomotive); + return 0; } /// /// Добавление объекта в набор на конкретную позицию @@ -65,42 +51,14 @@ namespace WarmlyLocomotive /// public int Insert(T locomotive, int position) { - if (position < 0 || position >= Count) + // TODO проверка позиции + // TODO вставка по позиции + if (position < 0 || position >= Count || _maxCount == Count) { return -1; } - if (_places[position] == null || _places[position] == default(T)) - { - _places[position] = locomotive; - return position; - } - else - { - bool hasEmptySpace = false; - int indexOfEmptyPlace = 0; - for (int i = position + 1; i < Count; ++i) - { - if (_places[i] == null || _places[i] == default(T)) - { - hasEmptySpace = true; - indexOfEmptyPlace = i; - break; - } - } - if (hasEmptySpace) - { - for (int i = indexOfEmptyPlace; i > position; --i) - { - _places[i] = _places[i - 1]; - } - _places[position] = locomotive; - return position; - } - else - { - return -1; - } - } + _places.Insert(position, locomotive); + return position; } /// /// Удаление объекта из набора с конкретной позиции @@ -109,26 +67,57 @@ namespace WarmlyLocomotive /// public T Remove(int position) { + // TODO проверка позиции if (position < 0 || position >= Count) { return null; } T memoryObj = _places[position]; - _places[position] = null; + _places.RemoveAt(position); return memoryObj; } /// - /// Получение объекта из набора по позиции - /// - /// - /// - public T Get(int position) + /// Получение объекта из набора по позиции + /// + /// + /// + public T this[int position] { - if (position < 0 || position >= Count) + get { + if (position >= 0 && position < Count) + { + return _places[position]; + } return null; } - return _places[position]; + set + { + // TODO проверка позиции + // TODO вставка в список по позиции + if (position >= 0 && position < Count) + { + _places[position] = value; + } + } + } + /// + /// Проход по набору до первого пустого + /// + /// + public IEnumerable GetLocomotives() + { + foreach (var locomotive in _places) + { + if (locomotive != null) + { + yield return locomotive; + } + else + { + yield break; + } + } } } } From 7082b3197b9ea2d7becb9b7a443c24b4c756ca51 Mon Sep 17 00:00:00 2001 From: Danil Malin Date: Tue, 18 Oct 2022 17:42:24 +0400 Subject: [PATCH 2/5] =?UTF-8?q?=D0=A4=D0=B8=D0=BA=D1=81=20=D0=B1=D0=B0?= =?UTF-8?q?=D0=B3=D0=B0=20=D0=B7=D0=B0=D0=B4=D0=B0=D0=BD=D0=B8=D1=8F=20?= =?UTF-8?q?=D1=80=D0=B0=D0=B7=D0=BC=D0=B5=D1=80=D0=B0=20=D1=81=D0=BF=D0=B8?= =?UTF-8?q?=D1=81=D0=BA=D0=B0,=20=D1=83=D0=B4=D0=B0=D0=BB=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5=20=D0=BB=D0=B8=D1=88=D0=BD=D0=B8=D1=85=20=D1=81?= =?UTF-8?q?=D1=82=D1=80=D0=BE=D0=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../WarmlyLocomotive/FormMapWithSetLocomotives.cs | 4 ++-- .../WarmlyLocomotive/MapWithSetLocomotivesGeneric.cs | 2 +- WarmlyLocomotive/WarmlyLocomotive/MapsCollection.cs | 2 -- WarmlyLocomotive/WarmlyLocomotive/SetLocomotivesGeneric.cs | 7 ------- 4 files changed, 3 insertions(+), 12 deletions(-) diff --git a/WarmlyLocomotive/WarmlyLocomotive/FormMapWithSetLocomotives.cs b/WarmlyLocomotive/WarmlyLocomotive/FormMapWithSetLocomotives.cs index 5e890ab..44686b3 100644 --- a/WarmlyLocomotive/WarmlyLocomotive/FormMapWithSetLocomotives.cs +++ b/WarmlyLocomotive/WarmlyLocomotive/FormMapWithSetLocomotives.cs @@ -74,8 +74,8 @@ namespace WarmlyLocomotive FormLocomotive form = new(); if (form.ShowDialog() == DialogResult.OK) { - DrawningObjectLocomotive car = new(form.SelectedLocomotive); - if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + car != -1) + DrawningObjectLocomotive locomotive = new(form.SelectedLocomotive); + if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + locomotive != -1) { MessageBox.Show("Объект добавлен"); pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); diff --git a/WarmlyLocomotive/WarmlyLocomotive/MapWithSetLocomotivesGeneric.cs b/WarmlyLocomotive/WarmlyLocomotive/MapWithSetLocomotivesGeneric.cs index ab5b5d2..c5cf58b 100644 --- a/WarmlyLocomotive/WarmlyLocomotive/MapWithSetLocomotivesGeneric.cs +++ b/WarmlyLocomotive/WarmlyLocomotive/MapWithSetLocomotivesGeneric.cs @@ -42,7 +42,7 @@ namespace WarmlyLocomotive /// public MapWithSetLocomotivesGeneric(int picWidth, int picHeight, U map) { - int width = picWidth / _placeSizeWidth; + int width = picWidth / (_placeSizeWidth + 30); int height = picHeight / _placeSizeHeight; _setLocomotives = new SetLocomotivesGeneric(width * height); _pictureWidth = picWidth; diff --git a/WarmlyLocomotive/WarmlyLocomotive/MapsCollection.cs b/WarmlyLocomotive/WarmlyLocomotive/MapsCollection.cs index e172901..6abc264 100644 --- a/WarmlyLocomotive/WarmlyLocomotive/MapsCollection.cs +++ b/WarmlyLocomotive/WarmlyLocomotive/MapsCollection.cs @@ -56,7 +56,6 @@ namespace WarmlyLocomotive /// Название карты public void DelMap(string name) { - // TODO Прописать логику для удаления if (_mapStorages.ContainsKey(name)) { _mapStorages.Remove(name); @@ -71,7 +70,6 @@ namespace WarmlyLocomotive { get { - // TODO Продумать логику получения объекта if (ind != String.Empty) { MapWithSetLocomotivesGeneric value; diff --git a/WarmlyLocomotive/WarmlyLocomotive/SetLocomotivesGeneric.cs b/WarmlyLocomotive/WarmlyLocomotive/SetLocomotivesGeneric.cs index edcd3c2..b8c7371 100644 --- a/WarmlyLocomotive/WarmlyLocomotive/SetLocomotivesGeneric.cs +++ b/WarmlyLocomotive/WarmlyLocomotive/SetLocomotivesGeneric.cs @@ -34,8 +34,6 @@ namespace WarmlyLocomotive /// public int Insert(T locomotive) { - // TODO вставка в начало набора - // TODO проверка на _maxCount if (_maxCount == Count) { return -1; @@ -51,8 +49,6 @@ namespace WarmlyLocomotive /// public int Insert(T locomotive, int position) { - // TODO проверка позиции - // TODO вставка по позиции if (position < 0 || position >= Count || _maxCount == Count) { return -1; @@ -67,7 +63,6 @@ namespace WarmlyLocomotive /// public T Remove(int position) { - // TODO проверка позиции if (position < 0 || position >= Count) { return null; @@ -93,8 +88,6 @@ namespace WarmlyLocomotive } set { - // TODO проверка позиции - // TODO вставка в список по позиции if (position >= 0 && position < Count) { _places[position] = value; From 56b0d0de6fa2fa79237e158a3115abe6e7f9d748 Mon Sep 17 00:00:00 2001 From: Danil Malin Date: Tue, 18 Oct 2022 18:55:59 +0400 Subject: [PATCH 3/5] =?UTF-8?q?=D0=A3=D0=B4=D0=B0=D0=BB=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D0=B5=20=D0=BF=D1=83=D1=81=D1=82=D0=BE=D0=B9=20=D1=81=D1=82?= =?UTF-8?q?=D1=80=D0=BE=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../WarmlyLocomotive/FormMapWithSetLocomotives.cs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/WarmlyLocomotive/WarmlyLocomotive/FormMapWithSetLocomotives.cs b/WarmlyLocomotive/WarmlyLocomotive/FormMapWithSetLocomotives.cs index 44686b3..de2e866 100644 --- a/WarmlyLocomotive/WarmlyLocomotive/FormMapWithSetLocomotives.cs +++ b/WarmlyLocomotive/WarmlyLocomotive/FormMapWithSetLocomotives.cs @@ -18,9 +18,9 @@ namespace WarmlyLocomotive /// private readonly Dictionary _mapsDict = new() { - { "Простая карта", new SimpleMap() }, - {"Карта с большими барьерами", new BigBarriersMap() }, - {"Карта с круглыми барьерами", new CirclesMap() } + {"Простая карта", new SimpleMap()}, + {"Карта с большими барьерами", new BigBarriersMap()}, + {"Карта с круглыми барьерами", new CirclesMap()} }; /// /// Объект от коллекции карт @@ -45,7 +45,6 @@ namespace WarmlyLocomotive private void ReloadMaps() { int index = listBoxMaps.SelectedIndex; - listBoxMaps.Items.Clear(); for (int i = 0; i < _mapsCollection.Keys.Count; i++) { From 80c18de5dbfd5712faafcfe04518220996aef748 Mon Sep 17 00:00:00 2001 From: Danil Malin Date: Tue, 18 Oct 2022 20:30:54 +0400 Subject: [PATCH 4/5] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=20insert?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../WarmlyLocomotive/SetLocomotivesGeneric.cs | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/WarmlyLocomotive/WarmlyLocomotive/SetLocomotivesGeneric.cs b/WarmlyLocomotive/WarmlyLocomotive/SetLocomotivesGeneric.cs index b8c7371..28765de 100644 --- a/WarmlyLocomotive/WarmlyLocomotive/SetLocomotivesGeneric.cs +++ b/WarmlyLocomotive/WarmlyLocomotive/SetLocomotivesGeneric.cs @@ -34,12 +34,7 @@ namespace WarmlyLocomotive /// public int Insert(T locomotive) { - if (_maxCount == Count) - { - return -1; - } - _places.Insert(0, locomotive); - return 0; + return Insert(locomotive, 0); } /// /// Добавление объекта в набор на конкретную позицию @@ -49,7 +44,7 @@ namespace WarmlyLocomotive /// public int Insert(T locomotive, int position) { - if (position < 0 || position >= Count || _maxCount == Count) + if (position < 0 || position > Count || _maxCount == Count) { return -1; } From c9dd2ad4722f7ddc2ac97a4e46be129ce0424d89 Mon Sep 17 00:00:00 2001 From: Danil Malin Date: Wed, 19 Oct 2022 22:09:21 +0400 Subject: [PATCH 5/5] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=BE=20=D0=B8=D0=BC=D1=8F=20=D1=84=D0=BE=D1=80?= =?UTF-8?q?=D0=BC=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../WarmlyLocomotive/FormMapWithSetLocomotives.Designer.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/WarmlyLocomotive/WarmlyLocomotive/FormMapWithSetLocomotives.Designer.cs b/WarmlyLocomotive/WarmlyLocomotive/FormMapWithSetLocomotives.Designer.cs index 29195a6..e0342dd 100644 --- a/WarmlyLocomotive/WarmlyLocomotive/FormMapWithSetLocomotives.Designer.cs +++ b/WarmlyLocomotive/WarmlyLocomotive/FormMapWithSetLocomotives.Designer.cs @@ -254,7 +254,7 @@ this.Controls.Add(this.pictureBox); this.Controls.Add(this.groupBox); this.Name = "FormMapWithSetLocomotives"; - this.Text = "FormMapWithSetLocomotives"; + this.Text = "Карта с набором объектов"; this.groupBox.ResumeLayout(false); this.groupBox.PerformLayout(); this.groupBoxMaps.ResumeLayout(false);