From 640160b60c13e1c1f33435756d274a24c3378cfb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=B0=D0=BB=D0=B5=D1=80=D0=B8=D1=8F=20=D0=9D=D0=B8?= =?UTF-8?q?=D0=BA=D0=B8=D1=84=D0=BE=D1=80=D0=BE=D0=B2=D0=B0?= Date: Fri, 11 Nov 2022 16:49:59 +0400 Subject: [PATCH] commit 06 full --- .../AccordionBus/DrawningObjectBus.cs | 4 + AccordionBus/AccordionBus/ExtentionBus.cs | 56 +++++++++++++ .../FormMapWithSetBuses.Designer.cs | 75 ++++++++++++++++-- .../AccordionBus/FormMapWithSetBuses.cs | 39 +++++++++ .../AccordionBus/FormMapWithSetBuses.resx | 9 +++ AccordionBus/AccordionBus/IDrawningObject.cs | 5 ++ .../AccordionBus/MapWithSetBusesGeneric.cs | 25 ++++++ AccordionBus/AccordionBus/MapsCollection.cs | 79 ++++++++++++++++++- 8 files changed, 281 insertions(+), 11 deletions(-) create mode 100644 AccordionBus/AccordionBus/ExtentionBus.cs diff --git a/AccordionBus/AccordionBus/DrawningObjectBus.cs b/AccordionBus/AccordionBus/DrawningObjectBus.cs index 41b7d73..82be0ff 100644 --- a/AccordionBus/AccordionBus/DrawningObjectBus.cs +++ b/AccordionBus/AccordionBus/DrawningObjectBus.cs @@ -35,5 +35,9 @@ namespace AccordionBus { _bus?.DrawTransport(g); } + + public string GetInfo() => _bus?.GetDataForSave(); + + public static IDrawningObject Create(string data) => new DrawningObjectBus(data.CreateDrawningBus()); } } diff --git a/AccordionBus/AccordionBus/ExtentionBus.cs b/AccordionBus/AccordionBus/ExtentionBus.cs new file mode 100644 index 0000000..07de306 --- /dev/null +++ b/AccordionBus/AccordionBus/ExtentionBus.cs @@ -0,0 +1,56 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AccordionBus +{ + /// + /// Расширение для класса DrawningBus + /// + internal static class ExtentionBus + { + /// + /// Разделитель для записи информации по объекту в файл + /// + private static readonly char _separatorForObject = ':'; + /// + /// Создание объекта из строки + /// + /// + /// + public static DrawningBus CreateDrawningBus(this string info) + { + string[] strs = info.Split(_separatorForObject); + if (strs.Length == 3) + { + return new DrawningBus(Convert.ToInt32(strs[0]), + Convert.ToInt32(strs[1]), Color.FromName(strs[2])); + } + if (strs.Length == 7) + { + return new DrawningAccordionBus(Convert.ToInt32(strs[0]), + Convert.ToInt32(strs[1]), Color.FromName(strs[2]), + Color.FromName(strs[3]), Convert.ToBoolean(strs[4]), + Convert.ToBoolean(strs[5]), Convert.ToBoolean(strs[6])); + } + return null; + } + /// + /// Получение данных для сохранения в файл + /// + /// + /// + public static string GetDataForSave(this DrawningBus drawningBus) + { + var bus = drawningBus.Bus; + var str = $"{bus.Speed}{_separatorForObject}{bus.Weight}{_separatorForObject}{bus.BodyColor.Name}"; + if (bus is not EntityAccordionBus accordionBus) + { + return str; + } + return $"{str}{_separatorForObject}{accordionBus.DopColor.Name}{_separatorForObject}{accordionBus.Compartment}{_separatorForObject}{accordionBus.RearviewMirror}{_separatorForObject}{accordionBus.BusNumber}"; + } + } +} diff --git a/AccordionBus/AccordionBus/FormMapWithSetBuses.Designer.cs b/AccordionBus/AccordionBus/FormMapWithSetBuses.Designer.cs index aa1f31a..3c20478 100644 --- a/AccordionBus/AccordionBus/FormMapWithSetBuses.Designer.cs +++ b/AccordionBus/AccordionBus/FormMapWithSetBuses.Designer.cs @@ -45,9 +45,16 @@ this.buttonRemoveBus = new System.Windows.Forms.Button(); this.buttonAddBus = new System.Windows.Forms.Button(); this.pictureBox = new System.Windows.Forms.PictureBox(); + this.menuStrip = new System.Windows.Forms.MenuStrip(); + this.файлToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.SaveToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.LoadToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.openFileDialog = new System.Windows.Forms.OpenFileDialog(); + this.saveFileDialog = new System.Windows.Forms.SaveFileDialog(); this.groupBox1.SuspendLayout(); this.groupBoxMaps.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit(); + this.menuStrip.SuspendLayout(); this.SuspendLayout(); // // groupBox1 @@ -63,9 +70,9 @@ this.groupBox1.Controls.Add(this.buttonRemoveBus); this.groupBox1.Controls.Add(this.buttonAddBus); this.groupBox1.Dock = System.Windows.Forms.DockStyle.Right; - this.groupBox1.Location = new System.Drawing.Point(719, 0); + this.groupBox1.Location = new System.Drawing.Point(719, 28); this.groupBox1.Name = "groupBox1"; - this.groupBox1.Size = new System.Drawing.Size(250, 621); + this.groupBox1.Size = new System.Drawing.Size(250, 593); this.groupBox1.TabIndex = 0; this.groupBox1.TabStop = false; this.groupBox1.Text = "Инструменты"; @@ -138,7 +145,7 @@ 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(155, 558); + this.buttonRight.Location = new System.Drawing.Point(155, 530); this.buttonRight.Name = "buttonRight"; this.buttonRight.Size = new System.Drawing.Size(30, 30); this.buttonRight.TabIndex = 10; @@ -150,7 +157,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(119, 558); + this.buttonDown.Location = new System.Drawing.Point(119, 530); this.buttonDown.Name = "buttonDown"; this.buttonDown.Size = new System.Drawing.Size(30, 30); this.buttonDown.TabIndex = 9; @@ -162,7 +169,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(83, 558); + this.buttonLeft.Location = new System.Drawing.Point(83, 530); this.buttonLeft.Name = "buttonLeft"; this.buttonLeft.Size = new System.Drawing.Size(30, 30); this.buttonLeft.TabIndex = 8; @@ -174,7 +181,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(119, 522); + this.buttonUp.Location = new System.Drawing.Point(119, 494); this.buttonUp.Name = "buttonUp"; this.buttonUp.Size = new System.Drawing.Size(30, 30); this.buttonUp.TabIndex = 7; @@ -232,12 +239,53 @@ // pictureBox // this.pictureBox.Dock = System.Windows.Forms.DockStyle.Fill; - this.pictureBox.Location = new System.Drawing.Point(0, 0); + this.pictureBox.Location = new System.Drawing.Point(0, 28); this.pictureBox.Name = "pictureBox"; - this.pictureBox.Size = new System.Drawing.Size(719, 621); + this.pictureBox.Size = new System.Drawing.Size(719, 593); this.pictureBox.TabIndex = 1; this.pictureBox.TabStop = false; // + // menuStrip + // + this.menuStrip.ImageScalingSize = new System.Drawing.Size(20, 20); + this.menuStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.файлToolStripMenuItem}); + this.menuStrip.Location = new System.Drawing.Point(0, 0); + this.menuStrip.Name = "menuStrip"; + this.menuStrip.Size = new System.Drawing.Size(969, 28); + this.menuStrip.TabIndex = 2; + // + // файлToolStripMenuItem + // + this.файлToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.SaveToolStripMenuItem, + this.LoadToolStripMenuItem}); + this.файлToolStripMenuItem.Name = "файлToolStripMenuItem"; + this.файлToolStripMenuItem.Size = new System.Drawing.Size(59, 24); + this.файлToolStripMenuItem.Text = "Файл"; + // + // SaveToolStripMenuItem + // + this.SaveToolStripMenuItem.Name = "SaveToolStripMenuItem"; + this.SaveToolStripMenuItem.Size = new System.Drawing.Size(224, 26); + this.SaveToolStripMenuItem.Text = "Сохранение"; + this.SaveToolStripMenuItem.Click += new System.EventHandler(this.SaveToolStripMenuItem_Click); + // + // LoadToolStripMenuItem + // + this.LoadToolStripMenuItem.Name = "LoadToolStripMenuItem"; + this.LoadToolStripMenuItem.Size = new System.Drawing.Size(224, 26); + this.LoadToolStripMenuItem.Text = "Загрузка"; + this.LoadToolStripMenuItem.Click += new System.EventHandler(this.LoadToolStripMenuItem_Click); + // + // openFileDialog + // + this.openFileDialog.Filter = "txt file | *.txt"; + // + // saveFileDialog + // + this.saveFileDialog.Filter = "txt file | *.txt"; + // // FormMapWithSetBuses // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); @@ -245,6 +293,8 @@ this.ClientSize = new System.Drawing.Size(969, 621); this.Controls.Add(this.pictureBox); this.Controls.Add(this.groupBox1); + this.Controls.Add(this.menuStrip); + this.MainMenuStrip = this.menuStrip; this.Name = "FormMapWithSetBuses"; this.Text = "Карта с набором объектов"; this.groupBox1.ResumeLayout(false); @@ -252,7 +302,10 @@ this.groupBoxMaps.ResumeLayout(false); this.groupBoxMaps.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit(); + this.menuStrip.ResumeLayout(false); + this.menuStrip.PerformLayout(); this.ResumeLayout(false); + this.PerformLayout(); } @@ -275,5 +328,11 @@ private ListBox listBoxMaps; private Button buttonAddMap; private TextBox textBoxNewMapName; + private MenuStrip menuStrip; + private ToolStripMenuItem файлToolStripMenuItem; + private ToolStripMenuItem SaveToolStripMenuItem; + private ToolStripMenuItem LoadToolStripMenuItem; + private OpenFileDialog openFileDialog; + private SaveFileDialog saveFileDialog; } } \ No newline at end of file diff --git a/AccordionBus/AccordionBus/FormMapWithSetBuses.cs b/AccordionBus/AccordionBus/FormMapWithSetBuses.cs index 6812f8f..cb8bc03 100644 --- a/AccordionBus/AccordionBus/FormMapWithSetBuses.cs +++ b/AccordionBus/AccordionBus/FormMapWithSetBuses.cs @@ -223,5 +223,44 @@ namespace AccordionBus } pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].MoveObject(dir); } + /// + /// Обработка нажатия "Сохранение" + /// + /// + /// + private void SaveToolStripMenuItem_Click(object sender, EventArgs e) + { + if (saveFileDialog.ShowDialog() == DialogResult.OK) + { + if (_mapsCollection.SaveData(saveFileDialog.FileName)) + { + MessageBox.Show("Сохранение прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); + } + else + { + MessageBox.Show("Не сохранилось", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } + /// + /// Обработка нажатия "Загрузка" + /// + /// + /// + private void LoadToolStripMenuItem_Click(object sender, EventArgs e) + { + if (openFileDialog.ShowDialog() == DialogResult.OK) + { + if (_mapsCollection.LoadData(openFileDialog.FileName)) + { + MessageBox.Show("Открытие прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); + ReloadMaps(); + } + else + { + MessageBox.Show("Не удалось открыть", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } } } diff --git a/AccordionBus/AccordionBus/FormMapWithSetBuses.resx b/AccordionBus/AccordionBus/FormMapWithSetBuses.resx index f298a7b..985a3e4 100644 --- a/AccordionBus/AccordionBus/FormMapWithSetBuses.resx +++ b/AccordionBus/AccordionBus/FormMapWithSetBuses.resx @@ -57,4 +57,13 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + 17, 17 + + + 144, 17 + + + 311, 17 + \ No newline at end of file diff --git a/AccordionBus/AccordionBus/IDrawningObject.cs b/AccordionBus/AccordionBus/IDrawningObject.cs index a9136d8..d5a475e 100644 --- a/AccordionBus/AccordionBus/IDrawningObject.cs +++ b/AccordionBus/AccordionBus/IDrawningObject.cs @@ -40,5 +40,10 @@ namespace AccordionBus /// /// (float Left, float Right, float Top, float Bottom) GetCurrentPosition(); + /// + /// Получение информации по объекту + /// + /// + string GetInfo(); } } diff --git a/AccordionBus/AccordionBus/MapWithSetBusesGeneric.cs b/AccordionBus/AccordionBus/MapWithSetBusesGeneric.cs index ac5ee7f..7318086 100644 --- a/AccordionBus/AccordionBus/MapWithSetBusesGeneric.cs +++ b/AccordionBus/AccordionBus/MapWithSetBusesGeneric.cs @@ -114,6 +114,31 @@ namespace AccordionBus return new(_pictureWidth, _pictureHeight); } /// + /// Получение данных в виде строки + /// + /// + /// + public string GetData(char separatorType, char separatorData) + { + string data = $"{_map.GetType().Name}{separatorType}"; + foreach (var bus in _setBuses.GetBuses()) + { + data += $"{bus.GetInfo()}{separatorData}"; + } + return data; + } + /// + /// Загрузка списка из массива строк + /// + /// + public void LoadData(string[] records) + { + foreach (var rec in records) + { + _setBuses.Insert(DrawningObjectBus.Create(rec) as T); + } + } + /// /// "Взбалтываем" набор, чтобы все элементы оказались в начале /// private void Shaking() diff --git a/AccordionBus/AccordionBus/MapsCollection.cs b/AccordionBus/AccordionBus/MapsCollection.cs index a2df57d..5215574 100644 --- a/AccordionBus/AccordionBus/MapsCollection.cs +++ b/AccordionBus/AccordionBus/MapsCollection.cs @@ -14,7 +14,7 @@ namespace AccordionBus /// /// Словарь (хранилище) с картами /// - readonly Dictionary> _mapStorages; + readonly Dictionary> _mapStorages; /// /// Возвращение списка названий карт /// @@ -28,13 +28,21 @@ namespace AccordionBus /// private readonly int _pictureHeight; /// + /// Разделитель для записи информации по элементу словаря в файл + /// + private readonly char separatorDict = '|'; + /// + /// Разделитель для записей коллекции данных в файл + /// + private readonly char separatorData = ';'; + /// /// Конструктор /// /// /// public MapsCollection(int pictureWidth, int pictureHeight) { - _mapStorages = new Dictionary>(); + _mapStorages = new Dictionary>(); _pictureWidth = pictureWidth; _pictureHeight = pictureHeight; } @@ -63,7 +71,7 @@ namespace AccordionBus /// /// /// - public MapWithSetBusesGeneric this[string ind] + public MapWithSetBusesGeneric this[string ind] { get { @@ -71,5 +79,70 @@ namespace AccordionBus return mapWithSetBusesGeneric; } } + /// + /// Сохранение информации по автобусам в хранилище в файл + /// + /// Путь и имя файла + /// + public bool SaveData(string filename) + { + if (File.Exists(filename)) + { + File.Delete(filename); + } + using (StreamWriter fs = new(filename)) + { + fs.Write($"MapsCollection{Environment.NewLine}"); + foreach (var storage in _mapStorages) + { + fs.Write($"{storage.Key}{separatorDict}{storage.Value.GetData(separatorDict, separatorData)}{Environment.NewLine}"); + } + } + return true; + } + /// + /// Загрузка информации по автобусам на парковках из файла + /// + /// + /// + public bool LoadData(string filename) + { + if (!File.Exists(filename)) + { + return false; + } + List strs = new List(); + using (StreamReader fs = new(filename)) + { + while (!fs.EndOfStream) + { + strs.Add(fs.ReadLine()); + } + } + if (!strs[0].Contains("MapsCollection")) + { + //если нет такой записи, то это не те данные + return false; + } + //очищаем записи + _mapStorages.Clear(); + for (int i = 1; i < strs.Count; ++i) + { + var elem = strs[i].Split(separatorDict); + AbstractMap map = null; + switch (elem[1]) + { + case "SimpleMap": + map = new SimpleMap(); + break; + case "UndergroundTunnelMap": + map = new UndergroundTunnelMap(); + break; + } + _mapStorages.Add(elem[0], new MapWithSetBusesGeneric(_pictureWidth, _pictureHeight, map)); + _mapStorages[elem[0]].LoadData(elem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries)); + } + return true; + } } }