From 0c5960afb2824f5709b597ae111de0d14d017ccb Mon Sep 17 00:00:00 2001 From: shadowik Date: Thu, 24 Nov 2022 21:54:29 +0400 Subject: [PATCH] Final --- .../DoubleDeckerBus/DrawingObjectBus.cs | 10 ++ DoubleDeckerBus/DoubleDeckerBus/EntityBus.cs | 6 + .../DoubleDeckerBus/ExtentionCar.cs | 35 ++++ .../FormMapWithSetBuses.Designer.cs | 155 +++++++++++++----- .../DoubleDeckerBus/FormMapWithSetBuses.cs | 31 +++- .../DoubleDeckerBus/FormMapWithSetBuses.resx | 9 + .../DoubleDeckerBus/IDrawingObject.cs | 1 + .../DoubleDeckerBus/MapWithSetBusesGeneric.cs | 18 ++ .../DoubleDeckerBus/MapsCollection.cs | 79 ++++++++- 9 files changed, 296 insertions(+), 48 deletions(-) create mode 100644 DoubleDeckerBus/DoubleDeckerBus/ExtentionCar.cs diff --git a/DoubleDeckerBus/DoubleDeckerBus/DrawingObjectBus.cs b/DoubleDeckerBus/DoubleDeckerBus/DrawingObjectBus.cs index 9ed77ea..bd5e511 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/DrawingObjectBus.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/DrawingObjectBus.cs @@ -22,6 +22,11 @@ namespace DoubleDeckerBus return _bus?.GetCurrentPosition() ?? default; } + public string GetInfo() + { + return _bus?.GetDataForSave(); + } + public void MoveObject(Direction direction) { _bus?.MoveTransport(direction); @@ -36,5 +41,10 @@ namespace DoubleDeckerBus { _bus.DrawTransport(g); } + + public static IDrawingObject Create(string data) + { + return new DrawingObjectBus(data.CreateDrawningCar()); + } } } diff --git a/DoubleDeckerBus/DoubleDeckerBus/EntityBus.cs b/DoubleDeckerBus/DoubleDeckerBus/EntityBus.cs index d112acf..40e6b9f 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/EntityBus.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/EntityBus.cs @@ -19,5 +19,11 @@ namespace DoubleDeckerBus Weight = (weight <= 0) ? rnd.Next(50, 70) : weight; BodyColor = bodyColor; } + + public static EntityBus Creator(string data) { + string[] strs = data.Split(':'); + return new EntityBus(Convert.ToInt32(strs[0]), + Convert.ToInt32(strs[1]), Color.FromName(strs[2])); + } } } diff --git a/DoubleDeckerBus/DoubleDeckerBus/ExtentionCar.cs b/DoubleDeckerBus/DoubleDeckerBus/ExtentionCar.cs new file mode 100644 index 0000000..c36ffea --- /dev/null +++ b/DoubleDeckerBus/DoubleDeckerBus/ExtentionCar.cs @@ -0,0 +1,35 @@ +namespace DoubleDeckerBus +{ + internal static class ExtentionBus + { + private static readonly char _separatorForObject = ':'; + public static DrawingBus CreateDrawningCar(this string info) + { + string[] strs = info.Split(_separatorForObject); + if (strs.Length == 3) + { + return new DrawingBus(Convert.ToInt32(strs[0]), + Convert.ToInt32(strs[1]), Color.FromName(strs[2])); + } + if (strs.Length == 6) + { + return new DrawingDDB(Convert.ToInt32(strs[0]), + Convert.ToInt32(strs[1]), Color.FromName(strs[2]), + Color.FromName(strs[3]), Convert.ToBoolean(strs[4]), + Convert.ToBoolean(strs[5])); + } + return null; + } + + public static string GetDataForSave(this DrawingBus drawingBus) + { + var bus = drawingBus.Bus; + var str = $"{bus.Speed}{_separatorForObject}{bus.Weight}{_separatorForObject}{bus.BodyColor.Name}"; + if (bus is not EntityDDB sportCar) + { + return str; + } + return $"{str}{_separatorForObject}{sportCar.ExtraColor.Name}{_separatorForObject}{sportCar.SecondStage}{_separatorForObject}{sportCar.Ledder}"; + } + } +} \ No newline at end of file diff --git a/DoubleDeckerBus/DoubleDeckerBus/FormMapWithSetBuses.Designer.cs b/DoubleDeckerBus/DoubleDeckerBus/FormMapWithSetBuses.Designer.cs index 6125aa2..aaaad2f 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/FormMapWithSetBuses.Designer.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/FormMapWithSetBuses.Designer.cs @@ -45,9 +45,16 @@ this.maskedTextBoxPosition = new System.Windows.Forms.MaskedTextBox(); this.buttonAddBus = new System.Windows.Forms.Button(); this.pictureBox = new System.Windows.Forms.PictureBox(); + this.menuStrip1 = new System.Windows.Forms.MenuStrip(); + this.FileToolStripMenuItem = 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.groupBoxSettings.SuspendLayout(); this.groupBox1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit(); + this.menuStrip1.SuspendLayout(); this.SuspendLayout(); // // groupBoxSettings @@ -63,9 +70,11 @@ this.groupBoxSettings.Controls.Add(this.maskedTextBoxPosition); this.groupBoxSettings.Controls.Add(this.buttonAddBus); this.groupBoxSettings.Dock = System.Windows.Forms.DockStyle.Right; - this.groupBoxSettings.Location = new System.Drawing.Point(645, 0); + this.groupBoxSettings.Location = new System.Drawing.Point(737, 28); + this.groupBoxSettings.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.groupBoxSettings.Name = "groupBoxSettings"; - this.groupBoxSettings.Size = new System.Drawing.Size(200, 643); + this.groupBoxSettings.Padding = new System.Windows.Forms.Padding(3, 4, 3, 4); + this.groupBoxSettings.Size = new System.Drawing.Size(229, 829); this.groupBoxSettings.TabIndex = 0; this.groupBoxSettings.TabStop = false; this.groupBoxSettings.Text = "Инструменты"; @@ -77,9 +86,11 @@ this.groupBox1.Controls.Add(this.ButtonAddMap); this.groupBox1.Controls.Add(this.textBoxNewMapName); this.groupBox1.Controls.Add(this.comboBoxSelectorMap); - this.groupBox1.Location = new System.Drawing.Point(0, 22); + this.groupBox1.Location = new System.Drawing.Point(0, 29); + this.groupBox1.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.groupBox1.Name = "groupBox1"; - this.groupBox1.Size = new System.Drawing.Size(200, 339); + this.groupBox1.Padding = new System.Windows.Forms.Padding(3, 4, 3, 4); + this.groupBox1.Size = new System.Drawing.Size(229, 452); this.groupBox1.TabIndex = 11; this.groupBox1.TabStop = false; this.groupBox1.Text = "Карты"; @@ -87,18 +98,20 @@ // listBoxMaps // this.listBoxMaps.FormattingEnabled = true; - this.listBoxMaps.ItemHeight = 15; - this.listBoxMaps.Location = new System.Drawing.Point(6, 150); + this.listBoxMaps.ItemHeight = 20; + this.listBoxMaps.Location = new System.Drawing.Point(7, 200); + this.listBoxMaps.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.listBoxMaps.Name = "listBoxMaps"; - this.listBoxMaps.Size = new System.Drawing.Size(188, 124); + this.listBoxMaps.Size = new System.Drawing.Size(214, 164); this.listBoxMaps.TabIndex = 4; this.listBoxMaps.SelectedIndexChanged += new System.EventHandler(this.listBoxMaps_SelectedIndexChanged); // // ButtonDeleteMap // - this.ButtonDeleteMap.Location = new System.Drawing.Point(6, 280); + this.ButtonDeleteMap.Location = new System.Drawing.Point(7, 373); + this.ButtonDeleteMap.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.ButtonDeleteMap.Name = "ButtonDeleteMap"; - this.ButtonDeleteMap.Size = new System.Drawing.Size(194, 53); + this.ButtonDeleteMap.Size = new System.Drawing.Size(222, 71); this.ButtonDeleteMap.TabIndex = 3; this.ButtonDeleteMap.Text = "Удалить карту"; this.ButtonDeleteMap.UseVisualStyleBackColor = true; @@ -106,9 +119,10 @@ // // ButtonAddMap // - this.ButtonAddMap.Location = new System.Drawing.Point(6, 84); + 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(194, 53); + this.ButtonAddMap.Size = new System.Drawing.Size(222, 71); this.ButtonAddMap.TabIndex = 2; this.ButtonAddMap.Text = "Добавить карту"; this.ButtonAddMap.UseVisualStyleBackColor = true; @@ -116,17 +130,19 @@ // // textBoxNewMapName // - this.textBoxNewMapName.Location = new System.Drawing.Point(6, 22); + this.textBoxNewMapName.Location = new System.Drawing.Point(7, 29); + this.textBoxNewMapName.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.textBoxNewMapName.Name = "textBoxNewMapName"; - this.textBoxNewMapName.Size = new System.Drawing.Size(188, 23); + this.textBoxNewMapName.Size = new System.Drawing.Size(214, 27); this.textBoxNewMapName.TabIndex = 1; // // comboBoxSelectorMap // this.comboBoxSelectorMap.FormattingEnabled = true; - this.comboBoxSelectorMap.Location = new System.Drawing.Point(6, 55); + this.comboBoxSelectorMap.Location = new System.Drawing.Point(7, 73); + this.comboBoxSelectorMap.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.comboBoxSelectorMap.Name = "comboBoxSelectorMap"; - this.comboBoxSelectorMap.Size = new System.Drawing.Size(188, 23); + this.comboBoxSelectorMap.Size = new System.Drawing.Size(214, 28); this.comboBoxSelectorMap.TabIndex = 0; // // buttonRight @@ -134,10 +150,9 @@ this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonRight.BackgroundImage = global::DoubleDeckerBus.Properties.Resources.RightArrow; this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonRight.Location = new System.Drawing.Point(109, 591); - this.buttonRight.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.buttonRight.Location = new System.Drawing.Point(125, 760); this.buttonRight.Name = "buttonRight"; - this.buttonRight.Size = new System.Drawing.Size(26, 22); + this.buttonRight.Size = new System.Drawing.Size(30, 29); this.buttonRight.TabIndex = 10; this.buttonRight.Text = " "; this.buttonRight.UseVisualStyleBackColor = true; @@ -148,10 +163,9 @@ this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonDown.BackgroundImage = global::DoubleDeckerBus.Properties.Resources.DownArrow; this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonDown.Location = new System.Drawing.Point(86, 610); - this.buttonDown.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.buttonDown.Location = new System.Drawing.Point(98, 785); this.buttonDown.Name = "buttonDown"; - this.buttonDown.Size = new System.Drawing.Size(26, 22); + this.buttonDown.Size = new System.Drawing.Size(30, 29); this.buttonDown.TabIndex = 9; this.buttonDown.Text = " "; this.buttonDown.UseVisualStyleBackColor = true; @@ -162,10 +176,9 @@ this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonLeft.BackgroundImage = global::DoubleDeckerBus.Properties.Resources.LeftArrow; this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonLeft.Location = new System.Drawing.Point(63, 591); - this.buttonLeft.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.buttonLeft.Location = new System.Drawing.Point(72, 760); this.buttonLeft.Name = "buttonLeft"; - this.buttonLeft.Size = new System.Drawing.Size(26, 22); + this.buttonLeft.Size = new System.Drawing.Size(30, 29); this.buttonLeft.TabIndex = 8; this.buttonLeft.Text = " "; this.buttonLeft.UseVisualStyleBackColor = true; @@ -176,10 +189,9 @@ this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonUp.BackgroundImage = global::DoubleDeckerBus.Properties.Resources.UpArrow; this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonUp.Location = new System.Drawing.Point(85, 572); - this.buttonUp.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.buttonUp.Location = new System.Drawing.Point(97, 735); this.buttonUp.Name = "buttonUp"; - this.buttonUp.Size = new System.Drawing.Size(26, 22); + this.buttonUp.Size = new System.Drawing.Size(30, 29); this.buttonUp.TabIndex = 7; this.buttonUp.Text = " "; this.buttonUp.UseVisualStyleBackColor = true; @@ -187,9 +199,10 @@ // // button1 // - this.button1.Location = new System.Drawing.Point(6, 521); + this.button1.Location = new System.Drawing.Point(7, 695); + this.button1.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.button1.Name = "button1"; - this.button1.Size = new System.Drawing.Size(182, 35); + this.button1.Size = new System.Drawing.Size(208, 47); this.button1.TabIndex = 5; this.button1.Text = "Посмотреть карту"; this.button1.UseVisualStyleBackColor = true; @@ -197,9 +210,10 @@ // // buttonShowStorage // - this.buttonShowStorage.Location = new System.Drawing.Point(6, 480); + this.buttonShowStorage.Location = new System.Drawing.Point(7, 640); + this.buttonShowStorage.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.buttonShowStorage.Name = "buttonShowStorage"; - this.buttonShowStorage.Size = new System.Drawing.Size(182, 35); + this.buttonShowStorage.Size = new System.Drawing.Size(208, 47); this.buttonShowStorage.TabIndex = 4; this.buttonShowStorage.Text = "Посмотреть хранилище"; this.buttonShowStorage.UseVisualStyleBackColor = true; @@ -207,9 +221,10 @@ // // buttonDeleteBus // - this.buttonDeleteBus.Location = new System.Drawing.Point(6, 438); + this.buttonDeleteBus.Location = new System.Drawing.Point(7, 584); + this.buttonDeleteBus.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.buttonDeleteBus.Name = "buttonDeleteBus"; - this.buttonDeleteBus.Size = new System.Drawing.Size(188, 36); + this.buttonDeleteBus.Size = new System.Drawing.Size(215, 48); this.buttonDeleteBus.TabIndex = 3; this.buttonDeleteBus.Text = "Удалить автобус"; this.buttonDeleteBus.UseVisualStyleBackColor = true; @@ -217,17 +232,19 @@ // // maskedTextBoxPosition // - this.maskedTextBoxPosition.Location = new System.Drawing.Point(6, 409); + this.maskedTextBoxPosition.Location = new System.Drawing.Point(7, 545); + 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(188, 23); + this.maskedTextBoxPosition.Size = new System.Drawing.Size(214, 27); this.maskedTextBoxPosition.TabIndex = 2; // // buttonAddBus // - this.buttonAddBus.Location = new System.Drawing.Point(6, 367); + this.buttonAddBus.Location = new System.Drawing.Point(7, 489); + this.buttonAddBus.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.buttonAddBus.Name = "buttonAddBus"; - this.buttonAddBus.Size = new System.Drawing.Size(188, 36); + this.buttonAddBus.Size = new System.Drawing.Size(215, 48); this.buttonAddBus.TabIndex = 1; this.buttonAddBus.Text = "Добавить автобус"; this.buttonAddBus.UseVisualStyleBackColor = true; @@ -236,19 +253,66 @@ // 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.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.pictureBox.Name = "pictureBox"; - this.pictureBox.Size = new System.Drawing.Size(645, 643); + this.pictureBox.Size = new System.Drawing.Size(737, 829); this.pictureBox.TabIndex = 1; this.pictureBox.TabStop = false; // + // menuStrip1 + // + this.menuStrip1.ImageScalingSize = new System.Drawing.Size(20, 20); + this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.FileToolStripMenuItem}); + this.menuStrip1.Location = new System.Drawing.Point(0, 0); + this.menuStrip1.Name = "menuStrip1"; + this.menuStrip1.Size = new System.Drawing.Size(966, 28); + this.menuStrip1.TabIndex = 2; + this.menuStrip1.Text = "menuStrip1"; + // + // FileToolStripMenuItem + // + this.FileToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.SaveToolStripMenuItem, + this.LoadToolStripMenuItem}); + this.FileToolStripMenuItem.Name = "FileToolStripMenuItem"; + this.FileToolStripMenuItem.Size = new System.Drawing.Size(59, 24); + this.FileToolStripMenuItem.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.FileName = "openFileDialog1"; + this.openFileDialog.Filter = "txt file | *.txt"; + // + // saveFileDialog + // + this.saveFileDialog.Filter = "txt file | *.txt"; + // // FormMapWithSetBuses // - 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(845, 643); + this.ClientSize = new System.Drawing.Size(966, 857); this.Controls.Add(this.pictureBox); this.Controls.Add(this.groupBoxSettings); + this.Controls.Add(this.menuStrip1); + this.MainMenuStrip = this.menuStrip1; + this.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.Name = "FormMapWithSetBuses"; this.Text = "Карты с набором объектов"; this.groupBoxSettings.ResumeLayout(false); @@ -256,7 +320,10 @@ this.groupBox1.ResumeLayout(false); this.groupBox1.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit(); + this.menuStrip1.ResumeLayout(false); + this.menuStrip1.PerformLayout(); this.ResumeLayout(false); + this.PerformLayout(); } @@ -279,5 +346,11 @@ private Button ButtonDeleteMap; private Button ButtonAddMap; private ListBox listBoxMaps; + private MenuStrip menuStrip1; + private ToolStripMenuItem FileToolStripMenuItem; + private ToolStripMenuItem SaveToolStripMenuItem; + private ToolStripMenuItem LoadToolStripMenuItem; + private OpenFileDialog openFileDialog; + private SaveFileDialog saveFileDialog; } } \ No newline at end of file diff --git a/DoubleDeckerBus/DoubleDeckerBus/FormMapWithSetBuses.cs b/DoubleDeckerBus/DoubleDeckerBus/FormMapWithSetBuses.cs index 0e92328..9b404cb 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/FormMapWithSetBuses.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/FormMapWithSetBuses.cs @@ -54,7 +54,6 @@ namespace DoubleDeckerBus private void ButtonAddBus_Click(object sender, EventArgs e) { var formBusConfig = new FormBusConfig(); - // TODO Call method AddEvent from fromBusConfig formBusConfig.AddEvent(AddBus); formBusConfig.Show(); @@ -179,6 +178,36 @@ namespace DoubleDeckerBus ReloadMaps(); } } + + 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/DoubleDeckerBus/DoubleDeckerBus/FormMapWithSetBuses.resx b/DoubleDeckerBus/DoubleDeckerBus/FormMapWithSetBuses.resx index f298a7b..79fa9d7 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/FormMapWithSetBuses.resx +++ b/DoubleDeckerBus/DoubleDeckerBus/FormMapWithSetBuses.resx @@ -57,4 +57,13 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + 17, 17 + + + 152, 17 + + + 319, 17 + \ No newline at end of file diff --git a/DoubleDeckerBus/DoubleDeckerBus/IDrawingObject.cs b/DoubleDeckerBus/DoubleDeckerBus/IDrawingObject.cs index c54ed57..93c20a0 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/IDrawingObject.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/IDrawingObject.cs @@ -18,5 +18,6 @@ namespace DoubleDeckerBus (float Left, float Right, float Top, float Bottom) GetCurrentPosition(); + string GetInfo(); } } diff --git a/DoubleDeckerBus/DoubleDeckerBus/MapWithSetBusesGeneric.cs b/DoubleDeckerBus/DoubleDeckerBus/MapWithSetBusesGeneric.cs index 5de41e8..d5513cf 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/MapWithSetBusesGeneric.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/MapWithSetBusesGeneric.cs @@ -140,5 +140,23 @@ namespace DoubleDeckerBus if (currentHeight > height) return; } } + + 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(DrawingObjectBus.Create(rec) as T); + } + } } } diff --git a/DoubleDeckerBus/DoubleDeckerBus/MapsCollection.cs b/DoubleDeckerBus/DoubleDeckerBus/MapsCollection.cs index bb943e4..5642947 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/MapsCollection.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/MapsCollection.cs @@ -1,21 +1,26 @@ using DoubleDeckerBus; +using System.Text; namespace DoubleDeckerBus { internal class MapsCollection { - readonly Dictionary> _mapStorages; + readonly Dictionary> _mapStorages; public List Keys => _mapStorages.Keys.ToList(); private readonly int _pictureWidth; 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; } @@ -23,7 +28,7 @@ namespace DoubleDeckerBus public void AddMap(string name, AbstractMap map) { if (Keys.Contains(name)) return; - _mapStorages.Add(name, new MapWithSetBusesGeneric(_pictureWidth, _pictureHeight, map)); + _mapStorages.Add(name, new MapWithSetBusesGeneric(_pictureWidth, _pictureHeight, map)); } public void DelMap(string name) @@ -31,7 +36,7 @@ namespace DoubleDeckerBus _mapStorages.Remove(name); } - public MapWithSetBusesGeneric this[string ind] + public MapWithSetBusesGeneric this[string ind] { get { @@ -39,5 +44,67 @@ namespace DoubleDeckerBus return result; } } + + private static void WriteToFile(string text, FileStream stream) + { + byte[] info = new UTF8Encoding(true).GetBytes(text); + stream.Write(info, 0, info.Length); + } + + public bool SaveData(string filename) + { + if (File.Exists(filename)) + { + File.Delete(filename); + } + using (StreamWriter sw = new(filename)) + { + sw.Write($"MapsCollection{Environment.NewLine}"); + foreach (var storage in _mapStorages) + { + sw.Write($"{storage.Key}{separatorDict}{storage.Value.GetData(separatorDict, separatorData)}{Environment.NewLine}"); + } + } + return true; + } + + public bool LoadData(string filename) + { + if (!File.Exists(filename)) + { + return false; + } + string line; + using (StreamReader sw = new(filename)) + { + line = sw.ReadLine(); + if (line == null || !line.Contains("MapsCollection")) + { + return false; + } + + _mapStorages.Clear(); + line = sw.ReadLine(); + while (line != null) + { + var elem = line.Split(separatorDict); + AbstractMap map = null; + switch (elem[1]) + { + case "SimpleMap": + map = new SimpleMap(); + break; + case "WaterMap": + map = new WaterMap(); + break; + } + _mapStorages.Add(elem[0], new MapWithSetBusesGeneric(_pictureWidth, _pictureHeight, map)); + _mapStorages[elem[0]].LoadData(elem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries)); + line = sw.ReadLine(); + } + + return true; + } + } } -} \ No newline at end of file +} -- 2.25.1