diff --git a/AccordionBus/AccordionBus/FormMapWithSetBuses.Designer.cs b/AccordionBus/AccordionBus/FormMapWithSetBuses.Designer.cs
index d4e9a77..aa1f31a 100644
--- a/AccordionBus/AccordionBus/FormMapWithSetBuses.Designer.cs
+++ b/AccordionBus/AccordionBus/FormMapWithSetBuses.Designer.cs
@@ -29,6 +29,12 @@
private void InitializeComponent()
{
this.groupBox1 = 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.buttonRight = new System.Windows.Forms.Button();
this.buttonDown = new System.Windows.Forms.Button();
this.buttonLeft = new System.Windows.Forms.Button();
@@ -38,14 +44,15 @@
this.buttonShowStorage = new System.Windows.Forms.Button();
this.buttonRemoveBus = new System.Windows.Forms.Button();
this.buttonAddBus = new System.Windows.Forms.Button();
- this.comboBoxSelectorMap = new System.Windows.Forms.ComboBox();
this.pictureBox = new System.Windows.Forms.PictureBox();
this.groupBox1.SuspendLayout();
+ this.groupBoxMaps.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit();
this.SuspendLayout();
//
// groupBox1
//
+ this.groupBox1.Controls.Add(this.groupBoxMaps);
this.groupBox1.Controls.Add(this.buttonRight);
this.groupBox1.Controls.Add(this.buttonDown);
this.groupBox1.Controls.Add(this.buttonLeft);
@@ -55,21 +62,83 @@
this.groupBox1.Controls.Add(this.buttonShowStorage);
this.groupBox1.Controls.Add(this.buttonRemoveBus);
this.groupBox1.Controls.Add(this.buttonAddBus);
- this.groupBox1.Controls.Add(this.comboBoxSelectorMap);
this.groupBox1.Dock = System.Windows.Forms.DockStyle.Right;
- this.groupBox1.Location = new System.Drawing.Point(832, 0);
+ this.groupBox1.Location = new System.Drawing.Point(719, 0);
this.groupBox1.Name = "groupBox1";
- this.groupBox1.Size = new System.Drawing.Size(250, 553);
+ this.groupBox1.Size = new System.Drawing.Size(250, 621);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false;
this.groupBox1.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, 26);
+ this.groupBoxMaps.Name = "groupBoxMaps";
+ this.groupBoxMaps.Size = new System.Drawing.Size(238, 251);
+ this.groupBoxMaps.TabIndex = 11;
+ this.groupBoxMaps.TabStop = false;
+ this.groupBoxMaps.Text = "Карты";
+ //
+ // buttonDeleteMap
+ //
+ this.buttonDeleteMap.Location = new System.Drawing.Point(6, 218);
+ this.buttonDeleteMap.Name = "buttonDeleteMap";
+ this.buttonDeleteMap.Size = new System.Drawing.Size(226, 29);
+ this.buttonDeleteMap.TabIndex = 4;
+ this.buttonDeleteMap.Text = "Удалить карту";
+ 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(6, 128);
+ this.listBoxMaps.Name = "listBoxMaps";
+ this.listBoxMaps.Size = new System.Drawing.Size(226, 84);
+ this.listBoxMaps.TabIndex = 3;
+ this.listBoxMaps.SelectedIndexChanged += new System.EventHandler(this.ListBoxMaps_SelectedIndexChanged);
+ //
+ // buttonAddMap
+ //
+ this.buttonAddMap.Location = new System.Drawing.Point(6, 93);
+ this.buttonAddMap.Name = "buttonAddMap";
+ this.buttonAddMap.Size = new System.Drawing.Size(226, 29);
+ this.buttonAddMap.TabIndex = 2;
+ this.buttonAddMap.Text = "Добавить карту";
+ this.buttonAddMap.UseVisualStyleBackColor = true;
+ this.buttonAddMap.Click += new System.EventHandler(this.ButtonAddMap_Click);
+ //
+ // textBoxNewMapName
+ //
+ this.textBoxNewMapName.Location = new System.Drawing.Point(6, 26);
+ this.textBoxNewMapName.Name = "textBoxNewMapName";
+ this.textBoxNewMapName.Size = new System.Drawing.Size(226, 27);
+ this.textBoxNewMapName.TabIndex = 0;
+ //
+ // comboBoxSelectorMap
+ //
+ this.comboBoxSelectorMap.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
+ this.comboBoxSelectorMap.FormattingEnabled = true;
+ this.comboBoxSelectorMap.Items.AddRange(new object[] {
+ "Простая карта",
+ "Карта подземного туннеля"});
+ this.comboBoxSelectorMap.Location = new System.Drawing.Point(6, 59);
+ this.comboBoxSelectorMap.Name = "comboBoxSelectorMap";
+ this.comboBoxSelectorMap.Size = new System.Drawing.Size(226, 28);
+ this.comboBoxSelectorMap.TabIndex = 0;
+ //
// buttonRight
//
this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonRight.BackgroundImage = global::AccordionBus.Properties.Resources.arrowRight;
this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
- this.buttonRight.Location = new System.Drawing.Point(151, 480);
+ this.buttonRight.Location = new System.Drawing.Point(155, 558);
this.buttonRight.Name = "buttonRight";
this.buttonRight.Size = new System.Drawing.Size(30, 30);
this.buttonRight.TabIndex = 10;
@@ -81,7 +150,7 @@
this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonDown.BackgroundImage = global::AccordionBus.Properties.Resources.arrowDown;
this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
- this.buttonDown.Location = new System.Drawing.Point(115, 480);
+ this.buttonDown.Location = new System.Drawing.Point(119, 558);
this.buttonDown.Name = "buttonDown";
this.buttonDown.Size = new System.Drawing.Size(30, 30);
this.buttonDown.TabIndex = 9;
@@ -93,7 +162,7 @@
this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonLeft.BackgroundImage = global::AccordionBus.Properties.Resources.arrowLeft;
this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
- this.buttonLeft.Location = new System.Drawing.Point(79, 480);
+ this.buttonLeft.Location = new System.Drawing.Point(83, 558);
this.buttonLeft.Name = "buttonLeft";
this.buttonLeft.Size = new System.Drawing.Size(30, 30);
this.buttonLeft.TabIndex = 8;
@@ -105,7 +174,7 @@
this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonUp.BackgroundImage = global::AccordionBus.Properties.Resources.arrowUp;
this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
- this.buttonUp.Location = new System.Drawing.Point(115, 444);
+ this.buttonUp.Location = new System.Drawing.Point(119, 522);
this.buttonUp.Name = "buttonUp";
this.buttonUp.Size = new System.Drawing.Size(30, 30);
this.buttonUp.TabIndex = 7;
@@ -114,17 +183,17 @@
//
// maskedTextBoxPosition
//
- this.maskedTextBoxPosition.Location = new System.Drawing.Point(22, 157);
+ this.maskedTextBoxPosition.Location = new System.Drawing.Point(12, 338);
this.maskedTextBoxPosition.Mask = "00";
this.maskedTextBoxPosition.Name = "maskedTextBoxPosition";
- this.maskedTextBoxPosition.Size = new System.Drawing.Size(207, 27);
+ this.maskedTextBoxPosition.Size = new System.Drawing.Size(226, 27);
this.maskedTextBoxPosition.TabIndex = 6;
//
// buttonShowOnMap
//
- this.buttonShowOnMap.Location = new System.Drawing.Point(22, 353);
+ this.buttonShowOnMap.Location = new System.Drawing.Point(12, 461);
this.buttonShowOnMap.Name = "buttonShowOnMap";
- this.buttonShowOnMap.Size = new System.Drawing.Size(207, 29);
+ this.buttonShowOnMap.Size = new System.Drawing.Size(226, 29);
this.buttonShowOnMap.TabIndex = 5;
this.buttonShowOnMap.Text = "Посмотреть карту";
this.buttonShowOnMap.UseVisualStyleBackColor = true;
@@ -132,9 +201,9 @@
//
// buttonShowStorage
//
- this.buttonShowStorage.Location = new System.Drawing.Point(22, 272);
+ this.buttonShowStorage.Location = new System.Drawing.Point(12, 416);
this.buttonShowStorage.Name = "buttonShowStorage";
- this.buttonShowStorage.Size = new System.Drawing.Size(207, 29);
+ this.buttonShowStorage.Size = new System.Drawing.Size(226, 29);
this.buttonShowStorage.TabIndex = 4;
this.buttonShowStorage.Text = "Посмотреть хранилище";
this.buttonShowStorage.UseVisualStyleBackColor = true;
@@ -142,9 +211,9 @@
//
// buttonRemoveBus
//
- this.buttonRemoveBus.Location = new System.Drawing.Point(22, 190);
+ this.buttonRemoveBus.Location = new System.Drawing.Point(12, 371);
this.buttonRemoveBus.Name = "buttonRemoveBus";
- this.buttonRemoveBus.Size = new System.Drawing.Size(207, 29);
+ this.buttonRemoveBus.Size = new System.Drawing.Size(226, 29);
this.buttonRemoveBus.TabIndex = 3;
this.buttonRemoveBus.Text = "Удалить автомобиль";
this.buttonRemoveBus.UseVisualStyleBackColor = true;
@@ -152,33 +221,20 @@
//
// buttonAddBus
//
- this.buttonAddBus.Location = new System.Drawing.Point(22, 106);
+ this.buttonAddBus.Location = new System.Drawing.Point(12, 303);
this.buttonAddBus.Name = "buttonAddBus";
- this.buttonAddBus.Size = new System.Drawing.Size(207, 29);
+ this.buttonAddBus.Size = new System.Drawing.Size(226, 29);
this.buttonAddBus.TabIndex = 1;
this.buttonAddBus.Text = "Добавить автомобиль";
this.buttonAddBus.UseVisualStyleBackColor = true;
this.buttonAddBus.Click += new System.EventHandler(this.ButtonAddBus_Click);
//
- // comboBoxSelectorMap
- //
- this.comboBoxSelectorMap.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
- this.comboBoxSelectorMap.FormattingEnabled = true;
- this.comboBoxSelectorMap.Items.AddRange(new object[] {
- "Простая карта",
- "Карта подземного туннеля"});
- this.comboBoxSelectorMap.Location = new System.Drawing.Point(22, 26);
- this.comboBoxSelectorMap.Name = "comboBoxSelectorMap";
- this.comboBoxSelectorMap.Size = new System.Drawing.Size(207, 28);
- 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(832, 553);
+ this.pictureBox.Size = new System.Drawing.Size(719, 621);
this.pictureBox.TabIndex = 1;
this.pictureBox.TabStop = false;
//
@@ -186,13 +242,15 @@
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
- this.ClientSize = new System.Drawing.Size(1082, 553);
+ this.ClientSize = new System.Drawing.Size(969, 621);
this.Controls.Add(this.pictureBox);
this.Controls.Add(this.groupBox1);
this.Name = "FormMapWithSetBuses";
this.Text = "Карта с набором объектов";
this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout();
+ this.groupBoxMaps.ResumeLayout(false);
+ this.groupBoxMaps.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit();
this.ResumeLayout(false);
@@ -212,5 +270,10 @@
private Button buttonDown;
private Button buttonLeft;
private Button buttonUp;
+ private GroupBox groupBoxMaps;
+ private Button buttonDeleteMap;
+ private ListBox listBoxMaps;
+ private Button buttonAddMap;
+ private TextBox textBoxNewMapName;
}
}
\ No newline at end of file
diff --git a/AccordionBus/AccordionBus/FormMapWithSetBuses.cs b/AccordionBus/AccordionBus/FormMapWithSetBuses.cs
index cdba42a..13d1fc3 100644
--- a/AccordionBus/AccordionBus/FormMapWithSetBuses.cs
+++ b/AccordionBus/AccordionBus/FormMapWithSetBuses.cs
@@ -7,47 +7,102 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
+using static System.Windows.Forms.DataFormats;
namespace AccordionBus
{
public partial class FormMapWithSetBuses : Form
{
///
- /// Объект от класса карты с набором объектов
+ /// Словарь для выпадающего списка
///
- private MapWithSetBusesGeneric _mapBusesCollectionGeneric;
+ private readonly Dictionary _mapsDict = new()
+ {
+ { "Простая карта", new SimpleMap() },
+ { "Карта подземного туннеля", new UndergroundTunnelMap() },
+ };
+ ///
+ /// Объект от коллекции карт
+ ///
+ private readonly MapsCollection _mapsCollection;
///
/// Конструктор
///
public FormMapWithSetBuses()
{
InitializeComponent();
+ _mapsCollection = new MapsCollection(pictureBox.Width, pictureBox.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)
+ pictureBox.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 UndergroundTunnelMap();
- break;
+ return;
}
- if (map != null)
+
+ if (MessageBox.Show($"Удалить карту {listBoxMaps.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
- _mapBusesCollectionGeneric = new MapWithSetBusesGeneric(
- pictureBox.Width, pictureBox.Height, map);
- }
- else
- {
- _mapBusesCollectionGeneric = null;
+ _mapsCollection.DelMap(listBoxMaps.SelectedItem?.ToString() ?? string.Empty);
+ ReloadMaps();
}
}
///
@@ -57,7 +112,7 @@ namespace AccordionBus
///
private void ButtonAddBus_Click(object sender, EventArgs e)
{
- if (_mapBusesCollectionGeneric == null)
+ if (listBoxMaps.SelectedIndex == -1)
{
return;
}
@@ -65,14 +120,14 @@ namespace AccordionBus
if (form.ShowDialog() == DialogResult.OK)
{
DrawningObjectBus bus = new(form.SelectedBus);
- if (form.SelectedBus == null || (_mapBusesCollectionGeneric + bus == -1))
+ if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + bus != -1)
{
- MessageBox.Show("Не удалось добавить объект");
+ MessageBox.Show("Объект добавлен");
+ pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
else
{
- MessageBox.Show("Объект добавлен");
- pictureBox.Image = _mapBusesCollectionGeneric.ShowSet();
+ MessageBox.Show("Не удалось добавить объект");
}
}
}
@@ -83,6 +138,10 @@ namespace AccordionBus
///
private void ButtonRemoveBus_Click(object sender, EventArgs e)
{
+ if (listBoxMaps.SelectedIndex == -1)
+ {
+ return;
+ }
if (string.IsNullOrEmpty(maskedTextBoxPosition.Text))
{
return;
@@ -92,10 +151,10 @@ namespace AccordionBus
return;
}
int pos = Convert.ToInt32(maskedTextBoxPosition.Text);
- if (_mapBusesCollectionGeneric - pos != -1)
+ if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] - pos != null)
{
MessageBox.Show("Объект удален");
- pictureBox.Image = _mapBusesCollectionGeneric.ShowSet();
+ pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
else
{
@@ -109,11 +168,11 @@ namespace AccordionBus
///
private void ButtonShowStorage_Click(object sender, EventArgs e)
{
- if (_mapBusesCollectionGeneric == null)
+ if (listBoxMaps.SelectedIndex == -1)
{
return;
}
- pictureBox.Image = _mapBusesCollectionGeneric.ShowSet();
+ pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
///
/// Вывод карты
@@ -122,11 +181,11 @@ namespace AccordionBus
///
private void ButtonShowOnMap_Click(object sender, EventArgs e)
{
- if (_mapBusesCollectionGeneric == null)
+ if (listBoxMaps.SelectedIndex == -1)
{
return;
}
- pictureBox.Image = _mapBusesCollectionGeneric.ShowOnMap();
+ pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowOnMap();
}
///
/// Перемещение
@@ -135,7 +194,7 @@ namespace AccordionBus
///
private void ButtonMove_Click(object sender, EventArgs e)
{
- if (_mapBusesCollectionGeneric == null)
+ if (listBoxMaps.SelectedIndex == -1)
{
return;
}
@@ -157,7 +216,7 @@ namespace AccordionBus
dir = Direction.Right;
break;
}
- pictureBox.Image = _mapBusesCollectionGeneric.MoveObject(dir);
+ pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].MoveObject(dir);
}
}
}
diff --git a/AccordionBus/AccordionBus/MapWithSetBusesGeneric.cs b/AccordionBus/AccordionBus/MapWithSetBusesGeneric.cs
index 751f7b2..ac5ee7f 100644
--- a/AccordionBus/AccordionBus/MapWithSetBusesGeneric.cs
+++ b/AccordionBus/AccordionBus/MapWithSetBusesGeneric.cs
@@ -60,7 +60,7 @@ namespace AccordionBus
///
///
///
- ///
+ /// Возвращает позицию вставленого объекта либо -1, если не получилось его добавить
public static int operator +(MapWithSetBusesGeneric map, T bus)
{
return map._setBuses.Insert(bus);
@@ -70,8 +70,8 @@ namespace AccordionBus
///
///
///
- ///
- public static int operator -(MapWithSetBusesGeneric map, int position)
+ /// Возвращает удаленный объект, либо null если его не удалось удалить
+ public static T operator -(MapWithSetBusesGeneric map, int position)
{
return map._setBuses.Remove(position);
}
@@ -94,13 +94,9 @@ namespace AccordionBus
public Bitmap ShowOnMap()
{
Shaking();
- for (int i = 0; i < _setBuses.Count; i++)
+ foreach (var bus in _setBuses.GetBuses())
{
- var bus = _setBuses.Get(i);
- if (bus != null)
- {
- return _map.CreateMap(_pictureWidth, _pictureHeight, bus);
- }
+ return _map.CreateMap(_pictureWidth, _pictureHeight, bus);
}
return new(_pictureWidth, _pictureHeight);
}
@@ -125,11 +121,11 @@ namespace AccordionBus
int j = _setBuses.Count - 1;
for (int i = 0; i < _setBuses.Count; i++)
{
- if (_setBuses.Get(i) == null)
+ if (_setBuses[i] == null)
{
for (; j > i; j--)
{
- var bus = _setBuses.Get(j);
+ var bus = _setBuses[j];
if (bus != null)
{
_setBuses.Insert(bus, i);
@@ -172,10 +168,12 @@ namespace AccordionBus
int numberOfSeatsInHeight = _pictureHeight / _placeSizeHeight;
int rightLine = (numberOfSeatsInWidth - 1) * _placeSizeWidth;
int bottomLine = (numberOfSeatsInHeight - 1) * _placeSizeHeight;
- for (int i = 0; i < _setBuses.Count; i++)
+ int count = 0;
+ foreach (var bus in _setBuses.GetBuses())
{
- _setBuses.Get(i)?.SetObject(rightLine - i % numberOfSeatsInWidth * _placeSizeWidth, bottomLine - i / numberOfSeatsInWidth * _placeSizeHeight, _pictureWidth, _pictureHeight);
- _setBuses.Get(i)?.DrawningObject(g);
+ bus.SetObject(rightLine - count % numberOfSeatsInWidth * _placeSizeWidth, bottomLine - count / numberOfSeatsInWidth * _placeSizeHeight, _pictureWidth, _pictureHeight);
+ bus.DrawningObject(g);
+ count++;
}
}
}
diff --git a/AccordionBus/AccordionBus/MapsCollection.cs b/AccordionBus/AccordionBus/MapsCollection.cs
new file mode 100644
index 0000000..a2df57d
--- /dev/null
+++ b/AccordionBus/AccordionBus/MapsCollection.cs
@@ -0,0 +1,75 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace AccordionBus
+{
+ ///
+ /// Класс для хранения коллекции карт
+ ///
+ 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(_pictureWidth, _pictureHeight, map));
+ }
+ }
+ ///
+ /// Удаление карты
+ ///
+ /// Название карты
+ public void DelMap(string name)
+ {
+ _mapStorages.Remove(name);
+ }
+ ///
+ /// Доступ к парковке
+ ///
+ ///
+ ///
+ public MapWithSetBusesGeneric this[string ind]
+ {
+ get
+ {
+ _mapStorages.TryGetValue(ind, out var mapWithSetBusesGeneric);
+ return mapWithSetBusesGeneric;
+ }
+ }
+ }
+}
diff --git a/AccordionBus/AccordionBus/SetBusesGeneric.cs b/AccordionBus/AccordionBus/SetBusesGeneric.cs
index 02c3c15..ce09271 100644
--- a/AccordionBus/AccordionBus/SetBusesGeneric.cs
+++ b/AccordionBus/AccordionBus/SetBusesGeneric.cs
@@ -14,26 +14,29 @@ namespace AccordionBus
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 SetBusesGeneric(int count)
{
- _places = new T[count];
+ _maxCount = count;
+ _places = new List();
}
///
/// Добавление объекта в набор
///
/// Добавляемый автобус
- ///
+ /// Возвращает позицию вставленного объекта, либо -1 если его не удалось вставить
public int Insert(T bus)
{
return Insert(bus, 0);
@@ -43,61 +46,72 @@ namespace AccordionBus
///
/// Добавляемый автобус
/// Позиция
- ///
+ /// Возвращает позицию вставленного объекта, либо -1 если его не удалось вставить
public int Insert(T bus, int position)
{
//проверка позиции
- if (position < 0 || position >= Count)
+ if (position < 0 || position >= _maxCount)
{
return -1;
}
- //поиск пустой позиции
- int positionEmpty = position;
- for (; positionEmpty < Count; positionEmpty++)
- {
- if (_places[positionEmpty] == null)
- {
- break;
- }
- }
- if (positionEmpty == Count)
- {
- return -1;
- }
- //сдвиг вправо
- for (; positionEmpty > position; positionEmpty--)
- {
- _places[positionEmpty] = _places[positionEmpty - 1];
- }
- _places[position] = bus;
+ _places.Insert(position, bus);
return position;
}
///
/// Удаление объекта из набора с конкретной позиции
///
///
- ///
- public int Remove(int position)
+ /// Возвращает удаленный объект, либо null если его не удалось удалить
+ public T Remove(int position)
{
- if (position < 0 || position >= Count)
+ if (position < 0 || position >= _maxCount)
{
- return -1;
+ return null;
}
- _places[position] = null;
- return position;
+ var result = _places[position];
+ _places.RemoveAt(position);
+ return result;
}
///
/// Получение объекта из набора по позиции
///
///
///
- public T Get(int position)
+ public T this[int position]
{
- if (position < 0 || position >= Count)
+ get
{
- return null;
+ if (position >= 0 && position < _maxCount && position < Count)
+ {
+ return _places[position];
+ }
+ else
+ {
+ return null;
+ }
+ }
+ set
+ {
+ Insert(value, position);
+ }
+ }
+ ///
+ /// Проход по набору до первого пустого
+ ///
+ ///
+ public IEnumerable GetBuses()
+ {
+ foreach (var bus in _places)
+ {
+ if (bus != null)
+ {
+ yield return bus;
+ }
+ else
+ {
+ yield break;
+ }
}
- return _places[position];
}
}
}
\ No newline at end of file