From 70a62e00856e29c46839344c0d01b0a709ba045f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D0=BB=D0=B0?= Date: Sun, 4 Dec 2022 14:27:23 +0400 Subject: [PATCH 1/4] =?UTF-8?q?=D0=9A=D0=B0=D1=80=D1=82=D1=8B=20+=20=D1=80?= =?UTF-8?q?=D0=B0=D1=81=D0=BF=D0=BE=D0=BB=D0=BE=D0=B6=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D0=B5=20=D0=BE=D0=B1=D1=8C=D0=B5=D0=BA=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Locomative/MapWithSetLocoGeneric.cs | 59 ++++------ Locomative/Locomative/MapsCollection.cs | 51 +++++++++ Locomative/Locomative/SetLocoGeneric.cs | 102 +++++++++--------- 3 files changed, 126 insertions(+), 86 deletions(-) create mode 100644 Locomative/Locomative/MapsCollection.cs diff --git a/Locomative/Locomative/MapWithSetLocoGeneric.cs b/Locomative/Locomative/MapWithSetLocoGeneric.cs index 896bce3..d3b2b7a 100644 --- a/Locomative/Locomative/MapWithSetLocoGeneric.cs +++ b/Locomative/Locomative/MapWithSetLocoGeneric.cs @@ -46,13 +46,9 @@ namespace Locomative public Bitmap ShowOnMap() { Shaking(); - for(int i =0; i < _setLoco.Count; i++) + foreach (var loco in _setLoco.GetLoco()) { - var loco = _setLoco.Get(i); - if(loco != null) - { - return _map.CreateMap(_pictureWidth, _pictureHeight, loco); - } + return _map.CreateMap(_pictureWidth, _pictureHeight, loco); } return new(_pictureWidth, _pictureHeight); } @@ -69,11 +65,11 @@ namespace Locomative int j = _setLoco.Count - 1; for(int i = 0; i < _setLoco.Count; i++) { - if(_setLoco.Get(i) == null) + if (_setLoco[j] == null) { for(; j>1; j--) { - var car = _setLoco.Get(j); + var car = _setLoco[j]; if (car != null) { _setLoco.Insert(car, i); @@ -107,40 +103,31 @@ namespace Locomative int CountWidth = _pictureWidth / _placeSizeWidth; int x = 10; - int y = _pictureHeight - _placeSizeHeight + 5; - - for (int k = 0; k < _setLoco.Count; k++) + int y = _pictureHeight - _placeSizeHeight; + int k = 0; + foreach (var plain in _setLoco.GetLoco()) { - if (_setLoco.Get(k) != null) - { - if ((k - 1) % CountWidth != 0 || k == 0) - { - _setLoco.Get(k)?.SetObject(x, y, _pictureWidth, _pictureHeight); - _setLoco.Get(k)?.DrawningObject(g); - x += _placeSizeWidth; - } - else - { - _setLoco.Get(k)?.SetObject(x, y, _pictureWidth, _pictureHeight); - _setLoco.Get(k)?.DrawningObject(g); - x = 10; - y -= _placeSizeHeight; - - } - } - if (_setLoco.Get(k) == null) + if ((k + 1) % CountWidth != 0 || k == 0) { - if ((k + 1) % CountWidth != 0 || k == 0) - x += _placeSizeWidth; - else - { - x = 10; - y -= _placeSizeHeight; - } + plain?.SetObject(x, y, _pictureWidth, _pictureHeight); + plain?.DrawningObject(g); + x += _placeSizeWidth; } + else + { + + plain?.SetObject(x, y, _pictureWidth, _pictureHeight); + plain?.DrawningObject(g); + x = 10; + y -= _placeSizeHeight; + + } + + k++; } } + } } diff --git a/Locomative/Locomative/MapsCollection.cs b/Locomative/Locomative/MapsCollection.cs new file mode 100644 index 0000000..714dc48 --- /dev/null +++ b/Locomative/Locomative/MapsCollection.cs @@ -0,0 +1,51 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Locomative +{ + 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)) + return; + Keys.Add(name); + _mapStorages.Add(name, new MapWithSetLocoGeneric(_pictureWidth, _pictureHeight, map)); + + } + + public void DelMap(string name) + { + if (!_mapStorages.ContainsKey(name)) + return; + Keys.Remove(name); + _mapStorages.Remove(name); + } + + public MapWithSetLocoGeneric this[string ind] + { + get + { + + if (!_mapStorages.ContainsKey(ind)) + return null; + else + return _mapStorages[ind]; + } + } + } +} + diff --git a/Locomative/Locomative/SetLocoGeneric.cs b/Locomative/Locomative/SetLocoGeneric.cs index d24f61d..dd515db 100644 --- a/Locomative/Locomative/SetLocoGeneric.cs +++ b/Locomative/Locomative/SetLocoGeneric.cs @@ -9,73 +9,75 @@ namespace Locomative internal class SetLocoGeneric where T : class { - private readonly T[] _places; - public int Count => _places.Length; + private readonly List _places; + public int Count => _places.Count; + private readonly int _maxCount; public SetLocoGeneric(int count) { - _places = new T[count]; + _maxCount = count; + _places = new List(); } - public int Insert(T locomative) + public int Insert(T plain) { - int i; - for (i = 0; i < Count; i++) - { - if (_places[i] == null) - { - for (int j = i; j >= 1; j--) - { - _places[j] = _places[j - 1]; - } - _places[0] = locomative; - return i; - } - } - return -1; + Insert(plain, 0); + return _places.Count; } - public int Insert(T locomative, int position) + public int Insert(T loco, int position) { - if (position > Count || position < 0) + if (position < 0 || _places.Count < position || position > _maxCount || _places.Count == _maxCount) return -1; - if (_places[position] == null) - { - _places[position] = locomative; - return position; - } - else - { - for (int i = position; i < Count; i++) - { - if (_places[i] == null) - { - for (int j = i; j >= position + 1; j--) - { - _places[j] = _places[j - 1]; - } - _places[position] = locomative; - return position; - } - } - } - return -1; + else if (loco == null) + return -1; + _places.Insert(position, loco); + return position; } public T Remove(int position) { T mid; - if (_places[position] != null && position < _places.Length) + if (position < 0 || _places.Count < position || position > _maxCount) + return null; + else if (_places[position] == null) + return null; + else { mid = _places[position]; - _places[position] = null; - return mid; - + _places.RemoveAt(position); } - else - return null; + return mid; } - public T Get(int position) + public T this[int position] { - return _places[position]; + get + { + if (position < 0 || _places.Count < position || position > _maxCount) + return null; + else if (_places[position] == null) + return null; + else + return _places[position]; + } + set + { + if (position < 0 || _places.Count < position || position > _maxCount) + return; + else if (_places.Count == _maxCount) + return; + else + _places[position] = value; + } } + public IEnumerable GetLoco() + { + foreach (var loco in _places) + { + if (loco != null) + yield return loco; + else + yield break; + } + } + } -} +} \ No newline at end of file -- 2.25.1 From b777ea482770a2866cba061ca077d79e6e6fefbe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D0=BB=D0=B0?= Date: Sun, 4 Dec 2022 14:34:23 +0400 Subject: [PATCH 2/4] / --- Locomative/Locomative/MapWithSetLocoGeneric.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Locomative/Locomative/MapWithSetLocoGeneric.cs b/Locomative/Locomative/MapWithSetLocoGeneric.cs index d3b2b7a..f0757a8 100644 --- a/Locomative/Locomative/MapWithSetLocoGeneric.cs +++ b/Locomative/Locomative/MapWithSetLocoGeneric.cs @@ -105,20 +105,20 @@ namespace Locomative int x = 10; int y = _pictureHeight - _placeSizeHeight; int k = 0; - foreach (var plain in _setLoco.GetLoco()) + foreach (var loco in _setLoco.GetLoco()) { if ((k + 1) % CountWidth != 0 || k == 0) { - plain?.SetObject(x, y, _pictureWidth, _pictureHeight); - plain?.DrawningObject(g); + loco?.SetObject(x, y, _pictureWidth, _pictureHeight); + loco?.DrawningObject(g); x += _placeSizeWidth; } else { - plain?.SetObject(x, y, _pictureWidth, _pictureHeight); - plain?.DrawningObject(g); + loco?.SetObject(x, y, _pictureWidth, _pictureHeight); + loco?.DrawningObject(g); x = 10; y -= _placeSizeHeight; -- 2.25.1 From 50da9d82269aa0374f2ef8d03e4b86ab420f3398 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D0=BB=D0=B0?= Date: Sun, 4 Dec 2022 14:37:41 +0400 Subject: [PATCH 3/4] =?UTF-8?q?=D0=9D=D0=B0=D0=B7=D0=B2=D0=B0=D0=BD=D0=B8?= =?UTF-8?q?=D1=8F=20=D0=BA=D0=B0=D1=80=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Locomative/Locomative/FormMapWithSetLocomatives.Designer.cs | 6 +++--- Locomative/Locomative/FormMapWithSetLocomatives.cs | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Locomative/Locomative/FormMapWithSetLocomatives.Designer.cs b/Locomative/Locomative/FormMapWithSetLocomatives.Designer.cs index 4b93e2f..0f33973 100644 --- a/Locomative/Locomative/FormMapWithSetLocomatives.Designer.cs +++ b/Locomative/Locomative/FormMapWithSetLocomatives.Designer.cs @@ -77,9 +77,9 @@ this.comboBoxSelectorMap.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.comboBoxSelectorMap.FormattingEnabled = true; this.comboBoxSelectorMap.Items.AddRange(new object[] { - "SimpleMap", - "UserMap #1", - "UserMap #2"}); + "Простая карта", + "Пользовательская карта #1", + "Пользовательская карта #2"}); this.comboBoxSelectorMap.Location = new System.Drawing.Point(7, 22); this.comboBoxSelectorMap.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3); this.comboBoxSelectorMap.Name = "comboBoxSelectorMap"; diff --git a/Locomative/Locomative/FormMapWithSetLocomatives.cs b/Locomative/Locomative/FormMapWithSetLocomatives.cs index 384c17a..3ded5a8 100644 --- a/Locomative/Locomative/FormMapWithSetLocomatives.cs +++ b/Locomative/Locomative/FormMapWithSetLocomatives.cs @@ -23,13 +23,13 @@ namespace Locomative AbstractMap map = null; switch (comboBoxSelectorMap.Text) { - case "SimpleMap": + case "Простая карта": map = new SimpleMap(); break; - case "UserMap #1": + case "Пользовательская карта #1": map = new UserMap_BigBox(); break; - case "UserMap #2": + case "Пользовательская карта #2": map = new UserMap_Rally(); break; } -- 2.25.1 From cea4b0acb6c3933e2423fe0a67317570469ca2db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D0=BB=D0=B0?= Date: Sun, 4 Dec 2022 16:21:41 +0400 Subject: [PATCH 4/4] =?UTF-8?q?=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D1=8F=20=D0=B2=20=D1=84=D0=BE=D1=80=D0=BC=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../FormMapWithSetLocomatives.Designer.cs | 89 +++++++++++---- .../Locomative/FormMapWithSetLocomatives.cs | 106 +++++++++++++----- Locomative/Locomative/LocomativeForm.cs | 4 +- 3 files changed, 147 insertions(+), 52 deletions(-) diff --git a/Locomative/Locomative/FormMapWithSetLocomatives.Designer.cs b/Locomative/Locomative/FormMapWithSetLocomatives.Designer.cs index 0f33973..9ad591f 100644 --- a/Locomative/Locomative/FormMapWithSetLocomatives.Designer.cs +++ b/Locomative/Locomative/FormMapWithSetLocomatives.Designer.cs @@ -29,6 +29,10 @@ private void InitializeComponent() { this.groupBox1 = new System.Windows.Forms.GroupBox(); + this.MapsTextBox = new System.Windows.Forms.MaskedTextBox(); + this.buttonMapsAdd = new System.Windows.Forms.Button(); + this.ButtonDeleteMap = new System.Windows.Forms.Button(); + this.listBoxMaps = new System.Windows.Forms.ListBox(); this.maskedTextBox1 = new System.Windows.Forms.MaskedTextBox(); this.comboBoxSelectorMap = new System.Windows.Forms.ComboBox(); this.ButtonShowOnMap = new System.Windows.Forms.Button(); @@ -46,6 +50,10 @@ // // groupBox1 // + this.groupBox1.Controls.Add(this.MapsTextBox); + this.groupBox1.Controls.Add(this.buttonMapsAdd); + this.groupBox1.Controls.Add(this.ButtonDeleteMap); + this.groupBox1.Controls.Add(this.listBoxMaps); this.groupBox1.Controls.Add(this.maskedTextBox1); this.groupBox1.Controls.Add(this.comboBoxSelectorMap); this.groupBox1.Controls.Add(this.ButtonShowOnMap); @@ -57,19 +65,57 @@ this.groupBox1.Controls.Add(this.buttonRight); this.groupBox1.Controls.Add(this.buttonLeft); this.groupBox1.Dock = System.Windows.Forms.DockStyle.Right; - this.groupBox1.Location = new System.Drawing.Point(600, 0); + this.groupBox1.Location = new System.Drawing.Point(607, 0); this.groupBox1.Name = "groupBox1"; - this.groupBox1.Size = new System.Drawing.Size(200, 450); + this.groupBox1.Size = new System.Drawing.Size(200, 539); this.groupBox1.TabIndex = 0; this.groupBox1.TabStop = false; this.groupBox1.Text = "Инструменты"; // + // MapsTextBox + // + this.MapsTextBox.Location = new System.Drawing.Point(7, 22); + this.MapsTextBox.Mask = "00"; + this.MapsTextBox.Name = "MapsTextBox"; + this.MapsTextBox.Size = new System.Drawing.Size(187, 23); + this.MapsTextBox.TabIndex = 19; + // + // buttonMapsAdd + // + this.buttonMapsAdd.Location = new System.Drawing.Point(7, 80); + this.buttonMapsAdd.Name = "buttonMapsAdd"; + this.buttonMapsAdd.Size = new System.Drawing.Size(187, 27); + this.buttonMapsAdd.TabIndex = 18; + this.buttonMapsAdd.Text = "Добавить карту"; + this.buttonMapsAdd.UseVisualStyleBackColor = true; + this.buttonMapsAdd.Click += new System.EventHandler(this.ButtonAddMap_Click); + // + // ButtonDeleteMap + // + this.ButtonDeleteMap.Location = new System.Drawing.Point(7, 213); + this.ButtonDeleteMap.Name = "ButtonDeleteMap"; + this.ButtonDeleteMap.Size = new System.Drawing.Size(187, 27); + this.ButtonDeleteMap.TabIndex = 17; + 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(7, 113); + this.listBoxMaps.Name = "listBoxMaps"; + this.listBoxMaps.Size = new System.Drawing.Size(187, 94); + this.listBoxMaps.TabIndex = 16; + this.listBoxMaps.SelectedIndexChanged += new System.EventHandler(this.ListBoxMaps_SelectedIndexChanged); + // // maskedTextBox1 // - this.maskedTextBox1.Location = new System.Drawing.Point(7, 159); + this.maskedTextBox1.Location = new System.Drawing.Point(7, 311); this.maskedTextBox1.Mask = "00"; this.maskedTextBox1.Name = "maskedTextBox1"; - this.maskedTextBox1.Size = new System.Drawing.Size(181, 23); + this.maskedTextBox1.Size = new System.Drawing.Size(187, 23); this.maskedTextBox1.TabIndex = 15; // // comboBoxSelectorMap @@ -80,18 +126,17 @@ "Простая карта", "Пользовательская карта #1", "Пользовательская карта #2"}); - this.comboBoxSelectorMap.Location = new System.Drawing.Point(7, 22); + this.comboBoxSelectorMap.Location = new System.Drawing.Point(7, 51); this.comboBoxSelectorMap.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3); this.comboBoxSelectorMap.Name = "comboBoxSelectorMap"; this.comboBoxSelectorMap.Size = new System.Drawing.Size(187, 23); this.comboBoxSelectorMap.TabIndex = 14; - this.comboBoxSelectorMap.SelectedIndexChanged += new System.EventHandler(this.ComboBoxSelectorMap_SelectedIndexChanged); // // ButtonShowOnMap // - this.ButtonShowOnMap.Location = new System.Drawing.Point(6, 279); + this.ButtonShowOnMap.Location = new System.Drawing.Point(7, 406); this.ButtonShowOnMap.Name = "ButtonShowOnMap"; - this.ButtonShowOnMap.Size = new System.Drawing.Size(188, 27); + this.ButtonShowOnMap.Size = new System.Drawing.Size(187, 27); this.ButtonShowOnMap.TabIndex = 13; this.ButtonShowOnMap.Text = "Посмотреть карту"; this.ButtonShowOnMap.UseVisualStyleBackColor = true; @@ -99,9 +144,9 @@ // // ButtonShowStorage // - this.ButtonShowStorage.Location = new System.Drawing.Point(6, 246); + this.ButtonShowStorage.Location = new System.Drawing.Point(7, 373); this.ButtonShowStorage.Name = "ButtonShowStorage"; - this.ButtonShowStorage.Size = new System.Drawing.Size(188, 27); + this.ButtonShowStorage.Size = new System.Drawing.Size(187, 27); this.ButtonShowStorage.TabIndex = 12; this.ButtonShowStorage.Text = "Посмотреть хранилище"; this.ButtonShowStorage.UseVisualStyleBackColor = true; @@ -109,9 +154,9 @@ // // ButtonRemoveCar // - this.ButtonRemoveCar.Location = new System.Drawing.Point(6, 213); + this.ButtonRemoveCar.Location = new System.Drawing.Point(7, 340); this.ButtonRemoveCar.Name = "ButtonRemoveCar"; - this.ButtonRemoveCar.Size = new System.Drawing.Size(188, 27); + this.ButtonRemoveCar.Size = new System.Drawing.Size(187, 27); this.ButtonRemoveCar.TabIndex = 11; this.ButtonRemoveCar.Text = "Удалить поезд"; this.ButtonRemoveCar.UseVisualStyleBackColor = true; @@ -119,9 +164,9 @@ // // ButtonAddLoco // - this.ButtonAddLoco.Location = new System.Drawing.Point(6, 101); + this.ButtonAddLoco.Location = new System.Drawing.Point(7, 278); this.ButtonAddLoco.Name = "ButtonAddLoco"; - this.ButtonAddLoco.Size = new System.Drawing.Size(188, 27); + this.ButtonAddLoco.Size = new System.Drawing.Size(187, 27); this.ButtonAddLoco.TabIndex = 9; this.ButtonAddLoco.Text = "Добавить поезд"; this.ButtonAddLoco.UseVisualStyleBackColor = true; @@ -132,7 +177,7 @@ this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonUp.BackgroundImage = global::Locomative.Properties.Resources.arrow_up; this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonUp.Location = new System.Drawing.Point(80, 350); + this.buttonUp.Location = new System.Drawing.Point(80, 439); this.buttonUp.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3); this.buttonUp.Name = "buttonUp"; this.buttonUp.Size = new System.Drawing.Size(41, 40); @@ -145,7 +190,7 @@ this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonDown.BackgroundImage = global::Locomative.Properties.Resources.arrow_down; this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonDown.Location = new System.Drawing.Point(80, 398); + this.buttonDown.Location = new System.Drawing.Point(80, 487); this.buttonDown.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3); this.buttonDown.Name = "buttonDown"; this.buttonDown.Size = new System.Drawing.Size(41, 40); @@ -158,7 +203,7 @@ this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonRight.BackgroundImage = global::Locomative.Properties.Resources.arrow_right; this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonRight.Location = new System.Drawing.Point(127, 398); + this.buttonRight.Location = new System.Drawing.Point(127, 487); this.buttonRight.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3); this.buttonRight.Name = "buttonRight"; this.buttonRight.Size = new System.Drawing.Size(41, 40); @@ -171,7 +216,7 @@ this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonLeft.BackgroundImage = global::Locomative.Properties.Resources.arrow_left; this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonLeft.Location = new System.Drawing.Point(32, 398); + this.buttonLeft.Location = new System.Drawing.Point(32, 487); this.buttonLeft.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3); this.buttonLeft.Name = "buttonLeft"; this.buttonLeft.Size = new System.Drawing.Size(41, 40); @@ -184,7 +229,7 @@ this.pictureBox.Dock = System.Windows.Forms.DockStyle.Fill; this.pictureBox.Location = new System.Drawing.Point(0, 0); this.pictureBox.Name = "pictureBox"; - this.pictureBox.Size = new System.Drawing.Size(600, 450); + this.pictureBox.Size = new System.Drawing.Size(607, 539); this.pictureBox.TabIndex = 0; this.pictureBox.TabStop = false; // @@ -192,7 +237,7 @@ // 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(807, 539); this.Controls.Add(this.pictureBox); this.Controls.Add(this.groupBox1); this.Name = "FormMapWithSetLocomatives"; @@ -218,5 +263,9 @@ private Button ButtonAddLoco; private MaskedTextBox maskedTextBox1; private ComboBox comboBoxSelectorMap; + private MaskedTextBox MapsTextBox; + private Button buttonMapsAdd; + private Button ButtonDeleteMap; + private ListBox listBoxMaps; } } \ No newline at end of file diff --git a/Locomative/Locomative/FormMapWithSetLocomatives.cs b/Locomative/Locomative/FormMapWithSetLocomatives.cs index 3ded5a8..b9023f9 100644 --- a/Locomative/Locomative/FormMapWithSetLocomatives.cs +++ b/Locomative/Locomative/FormMapWithSetLocomatives.cs @@ -14,37 +14,82 @@ namespace Locomative public partial class FormMapWithSetLocomatives : Form { private MapWithSetLocoGeneric _mapLocoCollectionGeneric; + private readonly Dictionary _mapsDict = new() + { + { "Простая карта", new SimpleMap() }, + { "Пользовательская карта #1", new UserMap_BigBox() }, + { "Пользовательская карта #2", new UserMap_Rally() } + }; + private readonly MapsCollection _mapsCollection; public FormMapWithSetLocomatives() { InitializeComponent(); + _mapsCollection = new MapsCollection(pictureBox.Width, pictureBox.Height); + comboBoxSelectorMap.Items.Clear(); + foreach (var elem in _mapsDict) + { + comboBoxSelectorMap.Items.Add(elem.Key); + } } - private void ComboBoxSelectorMap_SelectedIndexChanged(object sender, EventArgs e) + private void ReloadMaps() { - AbstractMap map = null; - switch (comboBoxSelectorMap.Text) + int index = listBoxMaps.SelectedIndex; + listBoxMaps.Items.Clear(); + for (int i = 0; i < _mapsCollection.Keys.Count; i++) { - case "Простая карта": - map = new SimpleMap(); - break; - case "Пользовательская карта #1": - map = new UserMap_BigBox(); - break; - case "Пользовательская карта #2": - map = new UserMap_Rally(); - break; + listBoxMaps.Items.Add(_mapsCollection.Keys[i]); } - if (map != null) + if (listBoxMaps.Items.Count > 0 && (index == -1 || index >= + listBoxMaps.Items.Count)) { - _mapLocoCollectionGeneric = new MapWithSetLocoGeneric(pictureBox.Width, pictureBox.Height, map); + listBoxMaps.SelectedIndex = 0; } - else + else if (listBoxMaps.Items.Count > 0 && index > -1 && index < + listBoxMaps.Items.Count) { - _mapLocoCollectionGeneric = null; + listBoxMaps.SelectedIndex = index; } } + private void ButtonAddMap_Click(object sender, EventArgs e) + { + if (comboBoxSelectorMap.SelectedIndex == -1 || string.IsNullOrEmpty(MapsTextBox.Text)) + { + MessageBox.Show("Не все данные заполнены", "Ошибка", + MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (!_mapsDict.ContainsKey(comboBoxSelectorMap.Text)) + { + MessageBox.Show("Нет такой карты", "Ошибка", MessageBoxButtons.OK, + MessageBoxIcon.Error); + return; + } + _mapsCollection.AddMap(MapsTextBox.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(); + } + } + private void ButtonAddLoco_Click(object sender, EventArgs e) { - if (_mapLocoCollectionGeneric == null) + if (listBoxMaps.SelectedIndex == -1) { return; } @@ -52,10 +97,10 @@ namespace Locomative if (form.ShowDialog() == DialogResult.OK) { DrawningObjectLoco loco = new(form.SelectedLoco); - if ((_mapLocoCollectionGeneric + loco)>=0) + if ((_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + loco) >= 0) { MessageBox.Show("Объект добавлен"); - pictureBox.Image = _mapLocoCollectionGeneric.ShowSet(); + pictureBox.Image =_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } else { @@ -65,20 +110,21 @@ namespace Locomative } private void ButtonRemoveLoco_Click(object sender, EventArgs e) { - if (string.IsNullOrEmpty(maskedTextBox1.Text)) + if (listBoxMaps.SelectedIndex == -1) { return; } - if (MessageBox.Show("Удалить объект?", "Удаление", - MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) + if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) { return; } + if (maskedTextBox1.Text == null) + return; int pos = Convert.ToInt32(maskedTextBox1.Text); - if (( _mapLocoCollectionGeneric - (pos - 1)) != null) + if ((_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] - (pos - 1)) != null) { MessageBox.Show("Объект удален"); - pictureBox.Image = _mapLocoCollectionGeneric.ShowSet(); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } else { @@ -87,23 +133,23 @@ namespace Locomative } private void ButtonShowStorage_Click(object sender, EventArgs e) { - if (_mapLocoCollectionGeneric == null) + if (listBoxMaps.SelectedIndex == -1) { return; } - pictureBox.Image = _mapLocoCollectionGeneric.ShowSet(); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } private void ButtonShowOnMap_Click(object sender, EventArgs e) { - if (_mapLocoCollectionGeneric == null) + if (listBoxMaps.SelectedIndex == -1) { return; } - pictureBox.Image = _mapLocoCollectionGeneric.ShowOnMap(); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowOnMap(); } private void ButtonMove_Click(object sender, EventArgs e) { - if (_mapLocoCollectionGeneric == null) + if (listBoxMaps.SelectedIndex == -1) { return; } @@ -124,7 +170,7 @@ namespace Locomative dir = Direction.Right; break; } - pictureBox.Image = _mapLocoCollectionGeneric.MoveObject(dir); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].MoveObject(dir); } } diff --git a/Locomative/Locomative/LocomativeForm.cs b/Locomative/Locomative/LocomativeForm.cs index f4c09d3..5159282 100644 --- a/Locomative/Locomative/LocomativeForm.cs +++ b/Locomative/Locomative/LocomativeForm.cs @@ -33,7 +33,7 @@ namespace Locomative { color = dialog.Color; } - _loco = new DrawningLocomative(rnd.Next(1, 200), rnd.Next(10, 300), color); + _loco = new DrawningLocomative(rnd.Next(20, 70), rnd.Next(10, 300), color); SetData(); Draw(); } @@ -85,7 +85,7 @@ namespace Locomative { dopColor = dialogDop.Color; } - _loco = new DrawningFastLoco(rnd.Next(1, 200), rnd.Next(100, 300), color, dopColor, Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0, 2))); + _loco = new DrawningFastLoco(rnd.Next(20, 70), rnd.Next(100, 300), color, dopColor, Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0, 2))); SetData(); Draw(); } -- 2.25.1