diff --git a/GasolineTanker/GasolineTanker/FormMapWithSetGasolineTanker.Designer.cs b/GasolineTanker/GasolineTanker/FormMapWithSetGasolineTanker.Designer.cs index 311cc56..ac96937 100644 --- a/GasolineTanker/GasolineTanker/FormMapWithSetGasolineTanker.Designer.cs +++ b/GasolineTanker/GasolineTanker/FormMapWithSetGasolineTanker.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.maskedTextBoxPosition = new System.Windows.Forms.MaskedTextBox(); this.keyRight = new System.Windows.Forms.Button(); this.keyLeft = new System.Windows.Forms.Button(); @@ -38,14 +44,15 @@ this.buttonShowStorage = new System.Windows.Forms.Button(); this.buttonRemoveGasolineTanker = new System.Windows.Forms.Button(); this.buttonAddGasolineTanker = new System.Windows.Forms.Button(); - this.comboBoxSelectorMap = new System.Windows.Forms.ComboBox(); this.pictureBox = new System.Windows.Forms.PictureBox(); this.groupBoxTools.SuspendLayout(); + this.groupBoxMaps.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit(); this.SuspendLayout(); // // groupBoxTools // + this.groupBoxTools.Controls.Add(this.groupBoxMaps); this.groupBoxTools.Controls.Add(this.maskedTextBoxPosition); this.groupBoxTools.Controls.Add(this.keyRight); this.groupBoxTools.Controls.Add(this.keyLeft); @@ -55,21 +62,94 @@ this.groupBoxTools.Controls.Add(this.buttonShowStorage); this.groupBoxTools.Controls.Add(this.buttonRemoveGasolineTanker); this.groupBoxTools.Controls.Add(this.buttonAddGasolineTanker); - this.groupBoxTools.Controls.Add(this.comboBoxSelectorMap); this.groupBoxTools.Dock = System.Windows.Forms.DockStyle.Right; - this.groupBoxTools.Location = new System.Drawing.Point(600, 0); + this.groupBoxTools.Location = new System.Drawing.Point(793, 0); + this.groupBoxTools.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.groupBoxTools.Name = "groupBoxTools"; - this.groupBoxTools.Size = new System.Drawing.Size(200, 450); + this.groupBoxTools.Padding = new System.Windows.Forms.Padding(3, 4, 3, 4); + this.groupBoxTools.Size = new System.Drawing.Size(229, 839); this.groupBoxTools.TabIndex = 0; this.groupBoxTools.TabStop = false; this.groupBoxTools.Text = "Tools"; // + // 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(7, 29); + this.groupBoxMaps.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); + this.groupBoxMaps.Name = "groupBoxMaps"; + this.groupBoxMaps.Padding = new System.Windows.Forms.Padding(3, 4, 3, 4); + this.groupBoxMaps.Size = new System.Drawing.Size(217, 407); + this.groupBoxMaps.TabIndex = 13; + this.groupBoxMaps.TabStop = false; + this.groupBoxMaps.Text = "Maps"; + // + // buttonDeleteMap + // + this.buttonDeleteMap.Location = new System.Drawing.Point(7, 321); + this.buttonDeleteMap.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); + this.buttonDeleteMap.Name = "buttonDeleteMap"; + this.buttonDeleteMap.Size = new System.Drawing.Size(200, 65); + this.buttonDeleteMap.TabIndex = 16; + this.buttonDeleteMap.Text = "Delete Map"; + this.buttonDeleteMap.UseVisualStyleBackColor = true; + this.buttonDeleteMap.Click += new System.EventHandler(this.buttonDeleteMap_Click); + // + // listBoxMaps + // + this.listBoxMaps.FormattingEnabled = true; + this.listBoxMaps.ItemHeight = 20; + this.listBoxMaps.Location = new System.Drawing.Point(7, 188); + this.listBoxMaps.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); + this.listBoxMaps.Name = "listBoxMaps"; + this.listBoxMaps.Size = new System.Drawing.Size(201, 124); + this.listBoxMaps.TabIndex = 15; + this.listBoxMaps.SelectedIndexChanged += new System.EventHandler(this.listBoxMaps_SelectedIndexChanged); + // + // buttonAddMap + // + this.buttonAddMap.Location = new System.Drawing.Point(7, 112); + this.buttonAddMap.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); + this.buttonAddMap.Name = "buttonAddMap"; + this.buttonAddMap.Size = new System.Drawing.Size(201, 68); + this.buttonAddMap.TabIndex = 14; + this.buttonAddMap.Text = "Add Map"; + this.buttonAddMap.UseVisualStyleBackColor = true; + this.buttonAddMap.Click += new System.EventHandler(this.buttonAddMap_Click); + // + // textBoxNewMapName + // + this.textBoxNewMapName.Location = new System.Drawing.Point(7, 32); + this.textBoxNewMapName.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); + this.textBoxNewMapName.Name = "textBoxNewMapName"; + this.textBoxNewMapName.Size = new System.Drawing.Size(203, 27); + this.textBoxNewMapName.TabIndex = 13; + // + // comboBoxSelectorMap + // + this.comboBoxSelectorMap.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; + this.comboBoxSelectorMap.FormattingEnabled = true; + this.comboBoxSelectorMap.Items.AddRange(new object[] { + "Simple map", + "Long map"}); + this.comboBoxSelectorMap.Location = new System.Drawing.Point(7, 71); + this.comboBoxSelectorMap.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); + this.comboBoxSelectorMap.Name = "comboBoxSelectorMap"; + this.comboBoxSelectorMap.Size = new System.Drawing.Size(203, 28); + this.comboBoxSelectorMap.TabIndex = 12; + this.comboBoxSelectorMap.SelectedIndexChanged += new System.EventHandler(this.ComboBoxSelectorMap_SelectedIndexChanged); + // // maskedTextBoxPosition // - this.maskedTextBoxPosition.Location = new System.Drawing.Point(10, 113); + this.maskedTextBoxPosition.Location = new System.Drawing.Point(11, 521); + this.maskedTextBoxPosition.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.maskedTextBoxPosition.Mask = "00"; this.maskedTextBoxPosition.Name = "maskedTextBoxPosition"; - this.maskedTextBoxPosition.Size = new System.Drawing.Size(184, 23); + this.maskedTextBoxPosition.Size = new System.Drawing.Size(210, 27); this.maskedTextBoxPosition.TabIndex = 11; // // keyRight @@ -77,9 +157,10 @@ this.keyRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.keyRight.BackgroundImage = global::GasolineTanker.Properties.Resources.KeyRight; this.keyRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.keyRight.Location = new System.Drawing.Point(122, 408); + this.keyRight.Location = new System.Drawing.Point(139, 783); + this.keyRight.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.keyRight.Name = "keyRight"; - this.keyRight.Size = new System.Drawing.Size(30, 30); + this.keyRight.Size = new System.Drawing.Size(34, 40); this.keyRight.TabIndex = 10; this.keyRight.UseVisualStyleBackColor = true; this.keyRight.Click += new System.EventHandler(this.ButtonMove_Click); @@ -89,9 +170,10 @@ this.keyLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.keyLeft.BackgroundImage = global::GasolineTanker.Properties.Resources.KeyLeft; this.keyLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.keyLeft.Location = new System.Drawing.Point(50, 408); + this.keyLeft.Location = new System.Drawing.Point(57, 783); + this.keyLeft.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.keyLeft.Name = "keyLeft"; - this.keyLeft.Size = new System.Drawing.Size(30, 30); + this.keyLeft.Size = new System.Drawing.Size(34, 40); this.keyLeft.TabIndex = 9; this.keyLeft.UseVisualStyleBackColor = true; this.keyLeft.Click += new System.EventHandler(this.ButtonMove_Click); @@ -101,9 +183,10 @@ this.keyUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.keyUp.BackgroundImage = global::GasolineTanker.Properties.Resources.KeyUp; this.keyUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.keyUp.Location = new System.Drawing.Point(86, 372); + this.keyUp.Location = new System.Drawing.Point(98, 735); + this.keyUp.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.keyUp.Name = "keyUp"; - this.keyUp.Size = new System.Drawing.Size(30, 30); + this.keyUp.Size = new System.Drawing.Size(34, 40); this.keyUp.TabIndex = 8; this.keyUp.UseVisualStyleBackColor = true; this.keyUp.Click += new System.EventHandler(this.ButtonMove_Click); @@ -113,18 +196,20 @@ this.keyDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.keyDown.BackgroundImage = global::GasolineTanker.Properties.Resources.KeyDown; this.keyDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.keyDown.Location = new System.Drawing.Point(86, 408); + this.keyDown.Location = new System.Drawing.Point(98, 783); + this.keyDown.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.keyDown.Name = "keyDown"; - this.keyDown.Size = new System.Drawing.Size(30, 30); + this.keyDown.Size = new System.Drawing.Size(34, 40); this.keyDown.TabIndex = 7; this.keyDown.UseVisualStyleBackColor = true; this.keyDown.Click += new System.EventHandler(this.ButtonMove_Click); // // buttonShowOnMap // - this.buttonShowOnMap.Location = new System.Drawing.Point(8, 326); + this.buttonShowOnMap.Location = new System.Drawing.Point(9, 667); + this.buttonShowOnMap.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.buttonShowOnMap.Name = "buttonShowOnMap"; - this.buttonShowOnMap.Size = new System.Drawing.Size(186, 34); + this.buttonShowOnMap.Size = new System.Drawing.Size(213, 45); this.buttonShowOnMap.TabIndex = 4; this.buttonShowOnMap.Text = "Show on map"; this.buttonShowOnMap.UseVisualStyleBackColor = true; @@ -132,9 +217,10 @@ // // buttonShowStorage // - this.buttonShowStorage.Location = new System.Drawing.Point(8, 241); + this.buttonShowStorage.Location = new System.Drawing.Point(9, 613); + this.buttonShowStorage.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.buttonShowStorage.Name = "buttonShowStorage"; - this.buttonShowStorage.Size = new System.Drawing.Size(186, 34); + this.buttonShowStorage.Size = new System.Drawing.Size(213, 45); this.buttonShowStorage.TabIndex = 3; this.buttonShowStorage.Text = "Show storage"; this.buttonShowStorage.UseVisualStyleBackColor = true; @@ -142,55 +228,50 @@ // // buttonRemoveGasolineTanker // - this.buttonRemoveGasolineTanker.Location = new System.Drawing.Point(10, 142); + this.buttonRemoveGasolineTanker.Location = new System.Drawing.Point(9, 560); + this.buttonRemoveGasolineTanker.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.buttonRemoveGasolineTanker.Name = "buttonRemoveGasolineTanker"; - this.buttonRemoveGasolineTanker.Size = new System.Drawing.Size(186, 34); + this.buttonRemoveGasolineTanker.Size = new System.Drawing.Size(213, 45); this.buttonRemoveGasolineTanker.TabIndex = 2; this.buttonRemoveGasolineTanker.Text = "Remove gasoline tanker"; this.buttonRemoveGasolineTanker.UseVisualStyleBackColor = true; - this.buttonRemoveGasolineTanker.Click += new System.EventHandler(this.ButtonRemoveGasolineTanker_Click); + this.buttonRemoveGasolineTanker.Click += new System.EventHandler(this.buttonRemoveGasolineTanker_Click); // // buttonAddGasolineTanker // - this.buttonAddGasolineTanker.Location = new System.Drawing.Point(8, 73); + this.buttonAddGasolineTanker.Location = new System.Drawing.Point(11, 468); + this.buttonAddGasolineTanker.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.buttonAddGasolineTanker.Name = "buttonAddGasolineTanker"; - this.buttonAddGasolineTanker.Size = new System.Drawing.Size(186, 34); + this.buttonAddGasolineTanker.Size = new System.Drawing.Size(213, 45); this.buttonAddGasolineTanker.TabIndex = 1; this.buttonAddGasolineTanker.Text = "Add gasoline tanker"; this.buttonAddGasolineTanker.UseVisualStyleBackColor = true; this.buttonAddGasolineTanker.Click += new System.EventHandler(this.ButtonAddGasolineTanker_Click); // - // comboBoxSelectorMap - // - this.comboBoxSelectorMap.Items.AddRange(new object[] { - "Simple map", - "Long map"}); - this.comboBoxSelectorMap.Location = new System.Drawing.Point(6, 22); - this.comboBoxSelectorMap.Name = "comboBoxSelectorMap"; - this.comboBoxSelectorMap.Size = new System.Drawing.Size(188, 23); - this.comboBoxSelectorMap.TabIndex = 12; - 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.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.pictureBox.Name = "pictureBox"; - this.pictureBox.Size = new System.Drawing.Size(600, 450); + this.pictureBox.Size = new System.Drawing.Size(793, 839); this.pictureBox.TabIndex = 1; this.pictureBox.TabStop = false; // // FormMapWithSetGasolineTanker // - this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); + this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(800, 450); + this.ClientSize = new System.Drawing.Size(1022, 839); this.Controls.Add(this.pictureBox); this.Controls.Add(this.groupBoxTools); + this.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.Name = "FormMapWithSetGasolineTanker"; this.Text = "FormMapWithSetGasolineTanker"; this.groupBoxTools.ResumeLayout(false); this.groupBoxTools.PerformLayout(); + this.groupBoxMaps.ResumeLayout(false); + this.groupBoxMaps.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit(); this.ResumeLayout(false); @@ -210,5 +291,10 @@ private Button keyUp; private Button keyDown; private MaskedTextBox maskedTextBoxPosition; + private GroupBox groupBoxMaps; + private Button buttonAddMap; + private TextBox textBoxNewMapName; + private Button buttonDeleteMap; + private ListBox listBoxMaps; } } \ No newline at end of file diff --git a/GasolineTanker/GasolineTanker/FormMapWithSetGasolineTanker.cs b/GasolineTanker/GasolineTanker/FormMapWithSetGasolineTanker.cs index e7b9072..6df5df9 100644 --- a/GasolineTanker/GasolineTanker/FormMapWithSetGasolineTanker.cs +++ b/GasolineTanker/GasolineTanker/FormMapWithSetGasolineTanker.cs @@ -12,25 +12,69 @@ namespace GasolineTanker { public partial class FormMapWithSetGasolineTanker : Form { + private readonly Dictionary _mapsDict = new() + { + { "Simple map", new SimpleMap() }, + { "Long map", new LongMap() } + }; private MapWithSetGasolienTankerGeneric _mapGasolineTankerCollectionGeneric; + private readonly MapsCollection _mapsCollection; public FormMapWithSetGasolineTanker() { InitializeComponent(); - } + _mapsCollection = new MapsCollection(pictureBox.Width, pictureBox.Height); + comboBoxSelectorMap.Items.Clear(); + foreach (var elem in _mapsDict) + { + comboBoxSelectorMap.Items.Add(elem.Key); + } + } + 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 ButtonDeleteMap_Click(object sender, EventArgs e) + { + if (listBoxMaps.SelectedIndex == -1) + { + return; + } + if (MessageBox.Show($"Delete map {listBoxMaps.SelectedItem}?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) + { + _mapsCollection.DelMap(listBoxMaps.SelectedItem?.ToString() ?? string.Empty); + ReloadMaps(); + } + } private void ButtonAddGasolineTanker_Click(object sender, EventArgs e) { - if (_mapGasolineTankerCollectionGeneric == null) + if (listBoxMaps.SelectedIndex == -1) { return; } FormGasolineTanker form = new(); if (form.ShowDialog() == DialogResult.OK) { - DrawingObjectGasolineTanker GasolineTanker = new(form.SelectedGasolineTanker); - if (_mapGasolineTankerCollectionGeneric + GasolineTanker >= 0) + DrawingObjectGasolineTanker antiAircraftGun = new(form.SelectedGasolineTanker); + if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + antiAircraftGun >= 0) { MessageBox.Show("Object added"); - pictureBox.Image = _mapGasolineTankerCollectionGeneric.ShowSet(); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } else { @@ -39,31 +83,9 @@ namespace GasolineTanker } } - private void ButtonRemoveGasolineTanker_Click(object sender, EventArgs e) - { - if (string.IsNullOrEmpty(maskedTextBoxPosition.Text)) - { - return; - } - if (MessageBox.Show("Delete object?", "Removal", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) - { - return; - } - int pos = Convert.ToInt32(maskedTextBoxPosition.Text); - if (_mapGasolineTankerCollectionGeneric - pos != null) - { - MessageBox.Show("Object deleted"); - pictureBox.Image = _mapGasolineTankerCollectionGeneric.ShowSet(); - } - else - { - MessageBox.Show("Failed to delete object"); - } - } - private void ButtonShowStorage_Click(object sender, EventArgs e) { - if (_mapGasolineTankerCollectionGeneric == null) + if (listBoxMaps.SelectedIndex == -1) { return; } @@ -72,15 +94,15 @@ namespace GasolineTanker private void ButtonShowOnMap_Click(object sender, EventArgs e) { - if (_mapGasolineTankerCollectionGeneric == null) + if(listBoxMaps.SelectedIndex == -1) { return; } - pictureBox.Image = _mapGasolineTankerCollectionGeneric.ShowOnMap(); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowOnMap(); } private void ButtonMove_Click(object sender, EventArgs e) { - if (_mapGasolineTankerCollectionGeneric == null) + if (listBoxMaps.SelectedIndex == -1) { return; } @@ -127,5 +149,65 @@ namespace GasolineTanker _mapGasolineTankerCollectionGeneric = null; } } + + private void buttonAddMap_Click(object sender, EventArgs e) + { + if (comboBoxSelectorMap.SelectedIndex == -1 || string.IsNullOrEmpty(textBoxNewMapName.Text)) + { + MessageBox.Show("Not all data saved", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (!_mapsDict.ContainsKey(comboBoxSelectorMap.Text)) + { + MessageBox.Show("No such card", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _mapsCollection.AddMap(textBoxNewMapName.Text, _mapsDict[comboBoxSelectorMap.Text]); + ReloadMaps(); + } + + private void buttonDeleteMap_Click(object sender, EventArgs e) + { + if (listBoxMaps.SelectedIndex == -1) + { + return; + } + if (MessageBox.Show($"Delete map {listBoxMaps.SelectedItem}?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) + { + _mapsCollection.DelMap(listBoxMaps.SelectedItem?.ToString() ?? string.Empty); + ReloadMaps(); + } + } + + private void listBoxMaps_SelectedIndexChanged(object sender, EventArgs e) + { + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); + } + + private void buttonRemoveGasolineTanker_Click(object sender, EventArgs e) + { + if (listBoxMaps.SelectedIndex == -1) + { + return; + } + if (string.IsNullOrEmpty(maskedTextBoxPosition.Text)) + { + return; + } + if (MessageBox.Show("Delete object?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) + { + return; + } + int pos = Convert.ToInt32(maskedTextBoxPosition.Text); + if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] - pos != null) + { + MessageBox.Show("Object delete"); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); + } + else + { + MessageBox.Show("Errore delete object"); + } + } } } diff --git a/GasolineTanker/GasolineTanker/MapWithSetGasolienTankerGeneric.cs b/GasolineTanker/GasolineTanker/MapWithSetGasolienTankerGeneric.cs index f25a19d..cd44080 100644 --- a/GasolineTanker/GasolineTanker/MapWithSetGasolienTankerGeneric.cs +++ b/GasolineTanker/GasolineTanker/MapWithSetGasolienTankerGeneric.cs @@ -49,13 +49,11 @@ namespace GasolineTanker public Bitmap ShowOnMap() { Shaking(); - for (int i = 0; i < _setGasolineTanker.Count; i++) + foreach (var gasolineTanker in _setGasolineTanker.GetGasolineTanker()) { - var gasolineTanker = _setGasolineTanker.Get(i); - if (gasolineTanker != null) - { - return _map.CreateMap(_pictureWidth, _pictureHeight, gasolineTanker); - } + + return _map.CreateMap(_pictureWidth, _pictureHeight, gasolineTanker); + } return new(_pictureWidth, _pictureHeight); } @@ -74,11 +72,11 @@ namespace GasolineTanker int j = _setGasolineTanker.Count - 1; for (int i = 0; i < _setGasolineTanker.Count; i++) { - if (_setGasolineTanker.Get(i) == null) + if (_setGasolineTanker[i] == null) { for (; j > i; j--) { - var gasolineTanker = _setGasolineTanker.Get(j); + var gasolineTanker = _setGasolineTanker[j]; if (gasolineTanker != null) { _setGasolineTanker.Insert(gasolineTanker, i); @@ -110,16 +108,15 @@ namespace GasolineTanker private void DrawGasolineTanker(Graphics g) { - int width = _pictureWidth / _placeSizeWidth; - int height = _pictureHeight / _placeSizeHeight; + int width = _pictureWidth / _placeSizeWidth; + int height = _pictureHeight / _placeSizeHeight; + int i = 0; - for (int i = 0; i < _setGasolineTanker.Count; i++) + foreach (var gasolineTanker in _setGasolineTanker.GetGasolineTanker()) { - if (_setGasolineTanker.Get(i) != null) - { - _setGasolineTanker.Get(i).SetObject(i % width * _placeSizeWidth + 10, (height - 1 - i / width) * _placeSizeHeight + 10, _pictureWidth, _pictureHeight); - _setGasolineTanker.Get(i)?.DrawningObject(g); - } + gasolineTanker.SetObject(i % width * _placeSizeWidth+10, (height - 1 - i / width) * _placeSizeHeight + 10, _pictureWidth, _pictureHeight); + gasolineTanker.DrawningObject(g); + i++; } } } diff --git a/GasolineTanker/GasolineTanker/MapsCollection.cs b/GasolineTanker/GasolineTanker/MapsCollection.cs new file mode 100644 index 0000000..473b2bc --- /dev/null +++ b/GasolineTanker/GasolineTanker/MapsCollection.cs @@ -0,0 +1,45 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace GasolineTanker +{ + 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 MapWithSetGasolienTankerGeneric(_pictureWidth, _pictureHeight, map)); + } + + public void DelMap(string name) + { + _mapStorages.Remove(name); + } + + public MapWithSetGasolienTankerGeneric this[string ind] + { + get + { + if (_mapStorages.TryGetValue(ind, out var map)) return map; + return null; + } + } + } +} diff --git a/GasolineTanker/GasolineTanker/SetGasolineTankerGeneric.cs b/GasolineTanker/GasolineTanker/SetGasolineTankerGeneric.cs index c546fa5..e6a5ae0 100644 --- a/GasolineTanker/GasolineTanker/SetGasolineTankerGeneric.cs +++ b/GasolineTanker/GasolineTanker/SetGasolineTankerGeneric.cs @@ -9,72 +9,66 @@ namespace GasolineTanker internal class SetGasolineTankerGeneric 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 SetGasolineTankerGeneric(int count) { - _places = new T[count]; + _maxCount = count; + _places = new List(); } public int Insert(T gasolineTanker) { - if (_places[Count - 1] == null) - { - for (int i = Count - 1; i > 0; i--) - { - _places[i] = _places[i - 1]; - } - _places[0] = gasolineTanker; - return 0; - } - return -1; - } + if (_places.Count < _maxCount) + { + _places.Add(gasolineTanker); + for (int i = 0; i < _places.Count; i++) + { + if (_places[i] == gasolineTanker) return i; + } + } + return -1; + } public int Insert(T gasolineTanker, int position) { - if (position < 0 || position >= Count) return -1; - if (_places[position] == null) - { - _places[position] = gasolineTanker; - return position; - } - else - { - if (_places[Count - 1] == null) - { - for (int i = Count - 1; i > position; i--) - { - _places[i] = _places[i - 1]; - } - _places[position] = gasolineTanker; - return position; - } - return -1; - } - } + if (position < 0 || position >= _places.Count) return -1; + _places.Insert(position, gasolineTanker); + return position; + } public T Remove(int position) { - if (position < 0 || position >= Count) - return null; - if (_places[position] != null) - { - T removed = _places[position]; - _places[position] = null; - if (position < Count - 1) - { - for (int k = position; k < Count - 1; k++) - { - _places[k] = _places[k + 1]; - } - } - return removed; - } - return null; - } - public T Get(int position) - { - if (position >= Count || position < 0) - return null; - - return _places[position]; - } - } + if (position < 0 || position >= _places.Count) return null; + if (_places[position] == null) return null; + T removed = _places[position]; + _places.RemoveAt(position); + return removed; + } + public T this[int position] + { + get + { + if (position < 0 || position >= _places.Count) return null; + return _places[position]; + } + set + { + if (position < 0 || position >= _places.Count) return; + _places.Insert(position, value); + } + } + public IEnumerable GetGasolineTanker() + { + foreach (var gasolineTanker in _places) + { + if (gasolineTanker != null) + { + yield return gasolineTanker; + } + else + { + yield break; + } + } + } + } }