diff --git a/WarmlyShip/WarmlyShip/DrawningObjectShip.cs b/WarmlyShip/WarmlyShip/DrawningObjectShip.cs index 22cba01..24e6ff5 100644 --- a/WarmlyShip/WarmlyShip/DrawningObjectShip.cs +++ b/WarmlyShip/WarmlyShip/DrawningObjectShip.cs @@ -36,5 +36,10 @@ namespace WarmlyShip { _ship.DrawTransport(g); } + + public string GetInfo() => _ship?.GetDataForSave(); + + public static IDrawningObject Create(string data) => new DrawningObjectShip(data.CreateDrawningShip()); + } } diff --git a/WarmlyShip/WarmlyShip/ExtentionShip.cs b/WarmlyShip/WarmlyShip/ExtentionShip.cs new file mode 100644 index 0000000..ebbe708 --- /dev/null +++ b/WarmlyShip/WarmlyShip/ExtentionShip.cs @@ -0,0 +1,54 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace WarmlyShip +{ + internal static class ExtentionShip + { + /// + /// Разделитель для записи информации по объекту в файл + /// + private static readonly char _separatorForObject = ':'; + /// + /// Создание объекта из строки + /// + /// + /// + public static DrawningShip CreateDrawningShip(this string info) + { + string[] strs = info.Split(_separatorForObject); + if (strs.Length == 3) + { + return new DrawningShip(Convert.ToInt32(strs[0]), + Convert.ToInt32(strs[1]), Color.FromName(strs[2])); + } + if (strs.Length == 6) + { + return new DrawningLiner(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 DrawningShip drawningShip) + { + var ship = drawningShip.Ship; + var str = + $"{ship.Speed}{_separatorForObject}{ship.Weight}{_separatorForObject}{ship.BodyColor.Name}"; + if (ship is not EntityLiner liner) + { + return str; + } + return $"{str}{_separatorForObject}{liner.DopColor.Name}{_separatorForObject}{liner.SwimmingPool}{_separatorForObject}{liner.DopDeck}"; + } + } +} diff --git a/WarmlyShip/WarmlyShip/FormMapWithSetWarmlyShip.Designer.cs b/WarmlyShip/WarmlyShip/FormMapWithSetWarmlyShip.Designer.cs index 8816de6..07966f8 100644 --- a/WarmlyShip/WarmlyShip/FormMapWithSetWarmlyShip.Designer.cs +++ b/WarmlyShip/WarmlyShip/FormMapWithSetWarmlyShip.Designer.cs @@ -45,9 +45,16 @@ this.maskedTextBoxPosition = new System.Windows.Forms.MaskedTextBox(); this.ButtonRemoveShip = new System.Windows.Forms.Button(); this.pictureBox1 = 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.pictureBox1)).BeginInit(); + this.menuStrip.SuspendLayout(); this.SuspendLayout(); // // groupBox1 @@ -63,9 +70,11 @@ this.groupBox1.Controls.Add(this.maskedTextBoxPosition); this.groupBox1.Controls.Add(this.ButtonRemoveShip); this.groupBox1.Dock = System.Windows.Forms.DockStyle.Right; - this.groupBox1.Location = new System.Drawing.Point(593, 0); + this.groupBox1.Location = new System.Drawing.Point(677, 30); + this.groupBox1.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.groupBox1.Name = "groupBox1"; - this.groupBox1.Size = new System.Drawing.Size(200, 582); + this.groupBox1.Padding = new System.Windows.Forms.Padding(3, 4, 3, 4); + this.groupBox1.Size = new System.Drawing.Size(229, 746); this.groupBox1.TabIndex = 1; this.groupBox1.TabStop = false; this.groupBox1.Text = "Инструменты"; @@ -77,9 +86,11 @@ 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, 22); + 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.Size = new System.Drawing.Size(188, 209); + this.groupBoxMaps.Padding = new System.Windows.Forms.Padding(3, 4, 3, 4); + this.groupBoxMaps.Size = new System.Drawing.Size(215, 279); this.groupBoxMaps.TabIndex = 12; this.groupBoxMaps.TabStop = false; this.groupBoxMaps.Text = "Карты"; @@ -87,17 +98,20 @@ // listBoxMaps // this.listBoxMaps.FormattingEnabled = true; - this.listBoxMaps.ItemHeight = 15; - this.listBoxMaps.Location = new System.Drawing.Point(6, 109); + this.listBoxMaps.ItemHeight = 20; + this.listBoxMaps.Location = new System.Drawing.Point(7, 145); + this.listBoxMaps.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.listBoxMaps.Name = "listBoxMaps"; - this.listBoxMaps.Size = new System.Drawing.Size(176, 64); + this.listBoxMaps.Size = new System.Drawing.Size(201, 84); this.listBoxMaps.TabIndex = 7; + this.listBoxMaps.SelectedIndexChanged += new System.EventHandler(this.ListBoxMaps_SelectedIndexChanged); // // buttonDeleteMap // - this.buttonDeleteMap.Location = new System.Drawing.Point(6, 180); + this.buttonDeleteMap.Location = new System.Drawing.Point(7, 240); + this.buttonDeleteMap.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.buttonDeleteMap.Name = "buttonDeleteMap"; - this.buttonDeleteMap.Size = new System.Drawing.Size(176, 23); + this.buttonDeleteMap.Size = new System.Drawing.Size(201, 31); this.buttonDeleteMap.TabIndex = 6; this.buttonDeleteMap.Text = "Удалить карту"; this.buttonDeleteMap.UseVisualStyleBackColor = true; @@ -105,9 +119,10 @@ // // buttonAddMap // - this.buttonAddMap.Location = new System.Drawing.Point(6, 80); + this.buttonAddMap.Location = new System.Drawing.Point(7, 107); + this.buttonAddMap.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.buttonAddMap.Name = "buttonAddMap"; - this.buttonAddMap.Size = new System.Drawing.Size(176, 23); + this.buttonAddMap.Size = new System.Drawing.Size(201, 31); this.buttonAddMap.TabIndex = 5; this.buttonAddMap.Text = "Добавить карту"; this.buttonAddMap.UseVisualStyleBackColor = true; @@ -115,9 +130,10 @@ // // 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(176, 23); + this.textBoxNewMapName.Size = new System.Drawing.Size(201, 27); this.textBoxNewMapName.TabIndex = 0; // // comboBoxSelectorMap @@ -126,9 +142,10 @@ this.comboBoxSelectorMap.Items.AddRange(new object[] { "Простая карта", "Море"}); - this.comboBoxSelectorMap.Location = new System.Drawing.Point(6, 51); + this.comboBoxSelectorMap.Location = new System.Drawing.Point(7, 68); + this.comboBoxSelectorMap.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.comboBoxSelectorMap.Name = "comboBoxSelectorMap"; - this.comboBoxSelectorMap.Size = new System.Drawing.Size(176, 23); + this.comboBoxSelectorMap.Size = new System.Drawing.Size(201, 28); this.comboBoxSelectorMap.TabIndex = 4; // // buttonDown @@ -136,18 +153,20 @@ this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonDown.BackgroundImage = global::WarmlyShip.Properties.Resources.Down; this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonDown.Location = new System.Drawing.Point(96, 536); + this.buttonDown.Location = new System.Drawing.Point(110, 685); + this.buttonDown.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.buttonDown.Name = "buttonDown"; - this.buttonDown.Size = new System.Drawing.Size(30, 30); + this.buttonDown.Size = new System.Drawing.Size(34, 40); this.buttonDown.TabIndex = 11; this.buttonDown.UseVisualStyleBackColor = true; this.buttonDown.Click += new System.EventHandler(this.ButtonMove_Click); // // ButtonAddShip // - this.ButtonAddShip.Location = new System.Drawing.Point(12, 298); + this.ButtonAddShip.Location = new System.Drawing.Point(14, 397); + this.ButtonAddShip.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.ButtonAddShip.Name = "ButtonAddShip"; - this.ButtonAddShip.Size = new System.Drawing.Size(176, 25); + this.ButtonAddShip.Size = new System.Drawing.Size(201, 33); this.ButtonAddShip.TabIndex = 2; this.ButtonAddShip.Text = "Добавить корабль"; this.ButtonAddShip.UseVisualStyleBackColor = true; @@ -158,9 +177,10 @@ this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonUp.BackgroundImage = global::WarmlyShip.Properties.Resources.Up; this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonUp.Location = new System.Drawing.Point(96, 500); + this.buttonUp.Location = new System.Drawing.Point(110, 637); + this.buttonUp.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.buttonUp.Name = "buttonUp"; - this.buttonUp.Size = new System.Drawing.Size(30, 30); + this.buttonUp.Size = new System.Drawing.Size(34, 40); this.buttonUp.TabIndex = 10; this.buttonUp.UseVisualStyleBackColor = true; this.buttonUp.Click += new System.EventHandler(this.ButtonMove_Click); @@ -170,9 +190,10 @@ this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonRight.BackgroundImage = global::WarmlyShip.Properties.Resources.Right; this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonRight.Location = new System.Drawing.Point(132, 536); + this.buttonRight.Location = new System.Drawing.Point(151, 685); + this.buttonRight.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.buttonRight.Name = "buttonRight"; - this.buttonRight.Size = new System.Drawing.Size(30, 30); + this.buttonRight.Size = new System.Drawing.Size(34, 40); this.buttonRight.TabIndex = 9; this.buttonRight.UseVisualStyleBackColor = true; this.buttonRight.Click += new System.EventHandler(this.ButtonMove_Click); @@ -182,18 +203,20 @@ this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonLeft.BackgroundImage = global::WarmlyShip.Properties.Resources.Left; this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonLeft.Location = new System.Drawing.Point(60, 536); + this.buttonLeft.Location = new System.Drawing.Point(69, 685); + this.buttonLeft.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.buttonLeft.Name = "buttonLeft"; - this.buttonLeft.Size = new System.Drawing.Size(30, 30); + this.buttonLeft.Size = new System.Drawing.Size(34, 40); this.buttonLeft.TabIndex = 8; this.buttonLeft.UseVisualStyleBackColor = true; this.buttonLeft.Click += new System.EventHandler(this.ButtonMove_Click); // // ButtonShowStorage // - this.ButtonShowStorage.Location = new System.Drawing.Point(12, 402); + this.ButtonShowStorage.Location = new System.Drawing.Point(14, 536); + this.ButtonShowStorage.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.ButtonShowStorage.Name = "ButtonShowStorage"; - this.ButtonShowStorage.Size = new System.Drawing.Size(176, 23); + this.ButtonShowStorage.Size = new System.Drawing.Size(201, 31); this.ButtonShowStorage.TabIndex = 7; this.ButtonShowStorage.Text = "Посмотреть хранилище"; this.ButtonShowStorage.UseVisualStyleBackColor = true; @@ -201,9 +224,10 @@ // // ButtonShowOnMap // - this.ButtonShowOnMap.Location = new System.Drawing.Point(12, 431); + this.ButtonShowOnMap.Location = new System.Drawing.Point(14, 575); + this.ButtonShowOnMap.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.ButtonShowOnMap.Name = "ButtonShowOnMap"; - this.ButtonShowOnMap.Size = new System.Drawing.Size(176, 23); + this.ButtonShowOnMap.Size = new System.Drawing.Size(201, 31); this.ButtonShowOnMap.TabIndex = 6; this.ButtonShowOnMap.Text = "Посмотреть карту"; this.ButtonShowOnMap.UseVisualStyleBackColor = true; @@ -211,17 +235,19 @@ // // maskedTextBoxPosition // - this.maskedTextBoxPosition.Location = new System.Drawing.Point(12, 329); + this.maskedTextBoxPosition.Location = new System.Drawing.Point(14, 439); + 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(176, 23); + this.maskedTextBoxPosition.Size = new System.Drawing.Size(201, 27); this.maskedTextBoxPosition.TabIndex = 5; // // ButtonRemoveShip // - this.ButtonRemoveShip.Location = new System.Drawing.Point(12, 358); + this.ButtonRemoveShip.Location = new System.Drawing.Point(14, 477); + this.ButtonRemoveShip.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.ButtonRemoveShip.Name = "ButtonRemoveShip"; - this.ButtonRemoveShip.Size = new System.Drawing.Size(176, 25); + this.ButtonRemoveShip.Size = new System.Drawing.Size(201, 33); this.ButtonRemoveShip.TabIndex = 3; this.ButtonRemoveShip.Text = "Удалить корабль"; this.ButtonRemoveShip.UseVisualStyleBackColor = true; @@ -230,19 +256,65 @@ // pictureBox1 // this.pictureBox1.Dock = System.Windows.Forms.DockStyle.Fill; - this.pictureBox1.Location = new System.Drawing.Point(0, 0); + this.pictureBox1.Location = new System.Drawing.Point(0, 30); + this.pictureBox1.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.pictureBox1.Name = "pictureBox1"; - this.pictureBox1.Size = new System.Drawing.Size(593, 582); + this.pictureBox1.Size = new System.Drawing.Size(677, 746); this.pictureBox1.TabIndex = 0; this.pictureBox1.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.Padding = new System.Windows.Forms.Padding(7, 3, 0, 3); + this.menuStrip.Size = new System.Drawing.Size(906, 30); + 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(177, 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(177, 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"; + // // FormMapWithSetWarmlyShip // - 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(793, 582); + this.ClientSize = new System.Drawing.Size(906, 776); this.Controls.Add(this.pictureBox1); this.Controls.Add(this.groupBox1); + this.Controls.Add(this.menuStrip); + this.MainMenuStrip = this.menuStrip; + this.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.Name = "FormMapWithSetWarmlyShip"; this.Text = "FormMapWithSetWarmlyShip"; this.groupBox1.ResumeLayout(false); @@ -250,7 +322,10 @@ this.groupBoxMaps.ResumeLayout(false); this.groupBoxMaps.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit(); + this.menuStrip.ResumeLayout(false); + this.menuStrip.PerformLayout(); this.ResumeLayout(false); + this.PerformLayout(); } @@ -273,5 +348,11 @@ private Button buttonDeleteMap; 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/WarmlyShip/WarmlyShip/FormMapWithSetWarmlyShip.cs b/WarmlyShip/WarmlyShip/FormMapWithSetWarmlyShip.cs index eb70ecf..f60cb5b 100644 --- a/WarmlyShip/WarmlyShip/FormMapWithSetWarmlyShip.cs +++ b/WarmlyShip/WarmlyShip/FormMapWithSetWarmlyShip.cs @@ -226,5 +226,47 @@ namespace WarmlyShip pictureBox1.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) + { + // TODO продумать логику + 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/WarmlyShip/WarmlyShip/FormMapWithSetWarmlyShip.resx b/WarmlyShip/WarmlyShip/FormMapWithSetWarmlyShip.resx index f298a7b..934ed35 100644 --- a/WarmlyShip/WarmlyShip/FormMapWithSetWarmlyShip.resx +++ b/WarmlyShip/WarmlyShip/FormMapWithSetWarmlyShip.resx @@ -57,4 +57,13 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + 17, 17 + + + 125, 17 + + + 265, 17 + \ No newline at end of file diff --git a/WarmlyShip/WarmlyShip/IDrawningObject.cs b/WarmlyShip/WarmlyShip/IDrawningObject.cs index 58a8bb7..cbbd689 100644 --- a/WarmlyShip/WarmlyShip/IDrawningObject.cs +++ b/WarmlyShip/WarmlyShip/IDrawningObject.cs @@ -39,5 +39,11 @@ namespace WarmlyShip /// /// (float Left, float Right, float Top, float Bottom) GetCurrentPosition(); + + /// + /// Получение информации по объекту + /// + /// + string GetInfo(); } } diff --git a/WarmlyShip/WarmlyShip/MapWithSetWarmlyShipGeneric.cs b/WarmlyShip/WarmlyShip/MapWithSetWarmlyShipGeneric.cs index a5b2393..9a6e6b3 100644 --- a/WarmlyShip/WarmlyShip/MapWithSetWarmlyShipGeneric.cs +++ b/WarmlyShip/WarmlyShip/MapWithSetWarmlyShipGeneric.cs @@ -58,7 +58,7 @@ namespace WarmlyShip /// Перегрузка оператора сложения /// /// - /// + /// /// public static int operator +(MapWithSetWarmlyShipGeneric map, T ship) { @@ -113,6 +113,33 @@ namespace WarmlyShip } return new(_pictureWidth, _pictureHeight); } + + /// + /// Получение данных в виде строки + /// + /// + /// + public string GetData(char separatorType, char separatorData) + { + string data = $"{_map.GetType().Name}{separatorType}"; + foreach (var ship in _setWarmlyShip.GetShip()) + { + data += $"{ship.GetInfo()}{separatorData}"; + } + return data; + } + /// + /// Загрузка списка из массива строк + /// + /// + public void LoadData(string[] records) + { + foreach (var rec in records) + { + _setWarmlyShip.Insert(DrawningObjectShip.Create(rec) as T); + } + } + /// /// "Взбалтываем" набор, чтобы все элементы оказались в начале /// @@ -174,5 +201,7 @@ namespace WarmlyShip } } + + } } diff --git a/WarmlyShip/WarmlyShip/MapsCollection.cs b/WarmlyShip/WarmlyShip/MapsCollection.cs index 361ef5e..b1e7e4c 100644 --- a/WarmlyShip/WarmlyShip/MapsCollection.cs +++ b/WarmlyShip/WarmlyShip/MapsCollection.cs @@ -14,7 +14,7 @@ namespace WarmlyShip /// /// Словарь (хранилище) с картами /// - readonly Dictionary> _mapStorages; + readonly Dictionary> _mapStorages; /// /// Возвращение списка названий карт /// @@ -27,6 +27,16 @@ namespace WarmlyShip /// Высота окна отрисовки /// private readonly int _pictureHeight; + + /// + /// Разделитель для записи информации по элементу словаря в файл + /// + private readonly char separatorDict = '|'; + /// + /// Разделитель для записей коллекции данных в файл + /// + private readonly char separatorData = ';'; + /// /// Конструктор /// @@ -34,7 +44,7 @@ namespace WarmlyShip /// public MapsCollection(int pictureWidth, int pictureHeight) { - _mapStorages = new Dictionary>(); + _mapStorages = new Dictionary>(); _pictureWidth = pictureWidth; _pictureHeight = pictureHeight; } @@ -46,7 +56,7 @@ namespace WarmlyShip public void AddMap(string name, AbstractMap map) { if (!Keys.Contains(name)) - _mapStorages.Add(name, new MapWithSetWarmlyShipGeneric(_pictureWidth, _pictureHeight, map)); + _mapStorages.Add(name, new MapWithSetWarmlyShipGeneric(_pictureWidth, _pictureHeight, map)); } /// /// Удаление карты @@ -62,7 +72,7 @@ namespace WarmlyShip /// /// /// - public MapWithSetWarmlyShipGeneric this[string + public MapWithSetWarmlyShipGeneric this[string ind] { get @@ -72,5 +82,77 @@ namespace WarmlyShip return null; } } + + /// + /// Метод записи информации в файл + /// + /// Строка, которую следует записать + /// Поток для записи + 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; + } + using (StreamReader sr = new(filename)) + { + if (!sr.ReadLine().Contains("MapsCollection")) + { + //если нет такой записи, то это не те данные + return false; + } + + //очищаем записи + _mapStorages.Clear(); + while (!sr.EndOfStream) + { + var elem = sr.ReadLine().Split(separatorDict); + AbstractMap map = null; + switch (elem[1]) + { + case "SimpleMap": + map = new SimpleMap(); + break; + case "MyMap": + map = new MyMap(); + break; + } + _mapStorages.Add(elem[0], new MapWithSetWarmlyShipGeneric(_pictureWidth, _pictureHeight, map)); + _mapStorages[elem[0]].LoadData(elem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries)); + } + } + return true; + } } }