diff --git a/Catamaran/BoatDelegate.cs b/Catamaran/BoatDelegate.cs deleted file mode 100644 index 520aa98..0000000 --- a/Catamaran/BoatDelegate.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace Catamaran -{ - - /// - /// Делегат для передачи объекта-лодки - /// - /// - public delegate void BoatDelegate(DrawingBoat boat); -} diff --git a/Catamaran/Catamaran.csproj b/Catamaran/Catamaran.csproj index 3d367cc..c83a6c2 100644 --- a/Catamaran/Catamaran.csproj +++ b/Catamaran/Catamaran.csproj @@ -54,6 +54,7 @@ + Form diff --git a/Catamaran/DrawingBoat.cs b/Catamaran/DrawingBoat.cs index c437349..bea4a55 100644 --- a/Catamaran/DrawingBoat.cs +++ b/Catamaran/DrawingBoat.cs @@ -127,8 +127,8 @@ namespace Catamaran /// Скорость /// Вес автомобиля /// Цвет кузова - /// Ширина отрисовки автомобиля - /// Высота отрисовки автомобиля + /// Ширина отрисовки автомобиля + /// Высота отрисовки автомобиля protected DrawingBoat(int speed, float weight, Color bodyColor, int catamaranWidth, int catamaranHeight) : this(speed, weight, bodyColor) diff --git a/Catamaran/DrawingObjectBoat.cs b/Catamaran/DrawingObjectBoat.cs index 01c436c..6904f4c 100644 --- a/Catamaran/DrawingObjectBoat.cs +++ b/Catamaran/DrawingObjectBoat.cs @@ -26,13 +26,14 @@ namespace Catamaran } public void SetObject(int x, int y, int width, int height) { - _catamaran.SetPosition(x, y, width, height); + _catamaran?.SetPosition(x, y, width, height); } public void DrawingObject(Graphics g) { - - _catamaran.DrawTransport(g); + _catamaran?.DrawTransport(g); } + public string GetInfo() => _catamaran?.GetDataForSave(); + public static IDrawingObject Create(string data) => new DrawingObjectBoat(data.CreateDrawingBoat()); } } diff --git a/Catamaran/ExtentionBoat.cs b/Catamaran/ExtentionBoat.cs new file mode 100644 index 0000000..8759d8d --- /dev/null +++ b/Catamaran/ExtentionBoat.cs @@ -0,0 +1,57 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Catamaran +{ + /// + /// Расширение для класса DrawingBoat + /// + internal static class ExtentionBoat + { + /// + /// Разделитель для записи информации по объекту в файл + /// + private static readonly char _separatorForObject = ':'; + /// + /// Создание объекта из строки + /// + /// + /// + public static DrawingBoat CreateDrawingBoat(this string info) + { + string[] strs = info.Split(_separatorForObject); + if (strs.Length == 3) + { + return new DrawingBoat(Convert.ToInt32(strs[0]), + Convert.ToInt32(strs[1]), Color.FromName(strs[2])); + } + if (strs.Length == 6) + { + return new DrawingCatamaran(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 DrawingBoat DrawingBoat) + { + var Boat = DrawingBoat.Catamaran; + var str = $"{Boat.Speed}{_separatorForObject}{Boat.Weight}{_separatorForObject}{Boat.BodyColor.Name}"; + if (!(Boat is EntityCatamaran Catamaran)) + { + return str; + } + return $"{str}{_separatorForObject}{Catamaran.DopColor.Name}{_separatorForObject}{Catamaran.Floats}{_separatorForObject}{Catamaran.Sail}"; + } + } +} diff --git a/Catamaran/FormBoatConfig.cs b/Catamaran/FormBoatConfig.cs index 7f17d68..8c35bad 100644 --- a/Catamaran/FormBoatConfig.cs +++ b/Catamaran/FormBoatConfig.cs @@ -182,7 +182,7 @@ namespace Catamaran } DrawCatamaran(); - // TODO Call method from object _boat if _boat is DrawningSportCar and set dop color + // TODO Call method from object _boat if _boat is DrawingCatamaran and set dop color } /// /// Добавление машины diff --git a/Catamaran/FormMapWithSetBoats.Designer.cs b/Catamaran/FormMapWithSetBoats.Designer.cs index 2a45d76..78eac40 100644 --- a/Catamaran/FormMapWithSetBoats.Designer.cs +++ b/Catamaran/FormMapWithSetBoats.Designer.cs @@ -29,25 +29,32 @@ private void InitializeComponent() { this.groupBoxTools = new System.Windows.Forms.GroupBox(); + this.groupBoxMaps = new System.Windows.Forms.GroupBox(); + this.listBoxMaps = new System.Windows.Forms.ListBox(); + this.buttonDeleteMap = new System.Windows.Forms.Button(); + this.buttonAddMap = new System.Windows.Forms.Button(); + this.textBoxNewMapName = new System.Windows.Forms.TextBox(); + this.comboBoxSelectorMap = new System.Windows.Forms.ComboBox(); this.buttonShowOnMap = new System.Windows.Forms.Button(); this.buttonShowStorage = new System.Windows.Forms.Button(); this.buttonRemoveBoat = new System.Windows.Forms.Button(); this.maskedTextBoxPosition = new System.Windows.Forms.MaskedTextBox(); this.buttonAddBoat = new System.Windows.Forms.Button(); - this.comboBoxSelectorMap = new System.Windows.Forms.ComboBox(); this.buttonLeft = new System.Windows.Forms.Button(); this.buttonRight = new System.Windows.Forms.Button(); this.buttonUp = new System.Windows.Forms.Button(); this.buttonDown = new System.Windows.Forms.Button(); this.pictureBox = new System.Windows.Forms.PictureBox(); - this.groupBoxMaps = new System.Windows.Forms.GroupBox(); - this.textBoxNewMapName = new System.Windows.Forms.TextBox(); - this.buttonAddMap = new System.Windows.Forms.Button(); - this.buttonDeleteMap = new System.Windows.Forms.Button(); - this.listBoxMaps = new System.Windows.Forms.ListBox(); + this.menuStrip = 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.groupBoxTools.SuspendLayout(); - ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit(); this.groupBoxMaps.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit(); + this.menuStrip.SuspendLayout(); this.SuspendLayout(); // // groupBoxTools @@ -63,13 +70,75 @@ this.groupBoxTools.Controls.Add(this.buttonUp); this.groupBoxTools.Controls.Add(this.buttonDown); this.groupBoxTools.Dock = System.Windows.Forms.DockStyle.Right; - this.groupBoxTools.Location = new System.Drawing.Point(653, 0); + this.groupBoxTools.Location = new System.Drawing.Point(653, 33); this.groupBoxTools.Name = "groupBoxTools"; - this.groupBoxTools.Size = new System.Drawing.Size(303, 647); + this.groupBoxTools.Size = new System.Drawing.Size(303, 614); this.groupBoxTools.TabIndex = 0; this.groupBoxTools.TabStop = false; this.groupBoxTools.Text = "Tools"; // + // groupBoxMaps + // + this.groupBoxMaps.Controls.Add(this.listBoxMaps); + this.groupBoxMaps.Controls.Add(this.buttonDeleteMap); + 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(10, 25); + this.groupBoxMaps.Name = "groupBoxMaps"; + this.groupBoxMaps.Size = new System.Drawing.Size(213, 314); + this.groupBoxMaps.TabIndex = 2; + this.groupBoxMaps.TabStop = false; + this.groupBoxMaps.Text = "Maps"; + // + // listBoxMaps + // + this.listBoxMaps.FormattingEnabled = true; + this.listBoxMaps.ItemHeight = 20; + this.listBoxMaps.Location = new System.Drawing.Point(14, 146); + this.listBoxMaps.Name = "listBoxMaps"; + this.listBoxMaps.Size = new System.Drawing.Size(191, 84); + this.listBoxMaps.TabIndex = 3; + this.listBoxMaps.SelectedIndexChanged += new System.EventHandler(this.ListBoxMaps_SelectedIndexChanged); + // + // buttonDeleteMap + // + this.buttonDeleteMap.Location = new System.Drawing.Point(14, 233); + this.buttonDeleteMap.Name = "buttonDeleteMap"; + this.buttonDeleteMap.Size = new System.Drawing.Size(191, 25); + this.buttonDeleteMap.TabIndex = 2; + this.buttonDeleteMap.Text = "DeleteMap"; + this.buttonDeleteMap.UseVisualStyleBackColor = true; + this.buttonDeleteMap.Click += new System.EventHandler(this.ButtonDeleteMap_Click); + // + // buttonAddMap + // + this.buttonAddMap.Location = new System.Drawing.Point(14, 112); + this.buttonAddMap.Name = "buttonAddMap"; + this.buttonAddMap.Size = new System.Drawing.Size(191, 27); + this.buttonAddMap.TabIndex = 1; + this.buttonAddMap.Text = "AddMap"; + this.buttonAddMap.UseVisualStyleBackColor = true; + this.buttonAddMap.Click += new System.EventHandler(this.ButtonAddMap_Click); + // + // textBoxNewMapName + // + this.textBoxNewMapName.Location = new System.Drawing.Point(14, 45); + this.textBoxNewMapName.Name = "textBoxNewMapName"; + this.textBoxNewMapName.Size = new System.Drawing.Size(191, 26); + this.textBoxNewMapName.TabIndex = 0; + // + // comboBoxSelectorMap + // + this.comboBoxSelectorMap.FormattingEnabled = true; + this.comboBoxSelectorMap.Items.AddRange(new object[] { + "SimpleMap", + "SecondMap"}); + this.comboBoxSelectorMap.Location = new System.Drawing.Point(14, 77); + this.comboBoxSelectorMap.Name = "comboBoxSelectorMap"; + this.comboBoxSelectorMap.Size = new System.Drawing.Size(191, 28); + this.comboBoxSelectorMap.TabIndex = 0; + // // buttonShowOnMap // this.buttonShowOnMap.Location = new System.Drawing.Point(24, 490); @@ -118,24 +187,12 @@ this.buttonAddBoat.UseVisualStyleBackColor = true; this.buttonAddBoat.Click += new System.EventHandler(this.buttonAddBoat_Click_1); // - // comboBoxSelectorMap - // - this.comboBoxSelectorMap.FormattingEnabled = true; - this.comboBoxSelectorMap.Items.AddRange(new object[] { - "SimpleMap", - "SecondMap"}); - this.comboBoxSelectorMap.Location = new System.Drawing.Point(14, 77); - this.comboBoxSelectorMap.Name = "comboBoxSelectorMap"; - this.comboBoxSelectorMap.Size = new System.Drawing.Size(191, 28); - this.comboBoxSelectorMap.TabIndex = 0; - //this.comboBoxSelectorMap.SelectedIndexChanged += new System.EventHandler(this.ComboBoxSelectorMap_SelectedIndexChanged); - // // buttonLeft // this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonLeft.BackgroundImage = global::Catamaran.Properties.Resources.Left; this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; - this.buttonLeft.Location = new System.Drawing.Point(10, 562); + this.buttonLeft.Location = new System.Drawing.Point(10, 529); this.buttonLeft.Name = "buttonLeft"; this.buttonLeft.Size = new System.Drawing.Size(30, 30); this.buttonLeft.TabIndex = 4; @@ -147,7 +204,7 @@ this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonRight.BackgroundImage = global::Catamaran.Properties.Resources.Right; this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; - this.buttonRight.Location = new System.Drawing.Point(70, 562); + this.buttonRight.Location = new System.Drawing.Point(70, 529); this.buttonRight.Name = "buttonRight"; this.buttonRight.Size = new System.Drawing.Size(30, 30); this.buttonRight.TabIndex = 5; @@ -159,7 +216,7 @@ this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonUp.BackgroundImage = global::Catamaran.Properties.Resources.Up; this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; - this.buttonUp.Location = new System.Drawing.Point(40, 540); + this.buttonUp.Location = new System.Drawing.Point(40, 507); this.buttonUp.Name = "buttonUp"; this.buttonUp.Size = new System.Drawing.Size(30, 30); this.buttonUp.TabIndex = 2; @@ -171,7 +228,7 @@ this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonDown.BackgroundImage = global::Catamaran.Properties.Resources.Down; this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; - this.buttonDown.Location = new System.Drawing.Point(40, 570); + this.buttonDown.Location = new System.Drawing.Point(40, 537); this.buttonDown.Name = "buttonDown"; this.buttonDown.Size = new System.Drawing.Size(30, 30); this.buttonDown.TabIndex = 3; @@ -181,62 +238,50 @@ // 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, 33); this.pictureBox.Name = "pictureBox"; - this.pictureBox.Size = new System.Drawing.Size(653, 647); + this.pictureBox.Size = new System.Drawing.Size(653, 614); this.pictureBox.TabIndex = 1; this.pictureBox.TabStop = false; // - // groupBoxMaps + // menuStrip // - this.groupBoxMaps.Controls.Add(this.listBoxMaps); - this.groupBoxMaps.Controls.Add(this.buttonDeleteMap); - 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(10, 25); - this.groupBoxMaps.Name = "groupBoxMaps"; - this.groupBoxMaps.Size = new System.Drawing.Size(213, 314); - this.groupBoxMaps.TabIndex = 2; - this.groupBoxMaps.TabStop = false; - this.groupBoxMaps.Text = "Maps"; + this.menuStrip.GripMargin = new System.Windows.Forms.Padding(2, 2, 0, 2); + this.menuStrip.ImageScalingSize = new System.Drawing.Size(24, 24); + this.menuStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.fileToolStripMenuItem}); + this.menuStrip.Location = new System.Drawing.Point(0, 0); + this.menuStrip.Name = "menuStrip"; + this.menuStrip.Size = new System.Drawing.Size(956, 33); + this.menuStrip.TabIndex = 2; + this.menuStrip.Text = "menuStrip"; // - // textBoxNewMapName + // fileToolStripMenuItem // - this.textBoxNewMapName.Location = new System.Drawing.Point(14, 45); - this.textBoxNewMapName.Name = "textBoxNewMapName"; - this.textBoxNewMapName.Size = new System.Drawing.Size(191, 26); - this.textBoxNewMapName.TabIndex = 0; + this.fileToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.saveToolStripMenuItem, + this.loadToolStripMenuItem}); + this.fileToolStripMenuItem.Name = "fileToolStripMenuItem"; + this.fileToolStripMenuItem.Size = new System.Drawing.Size(54, 29); + this.fileToolStripMenuItem.Text = "File"; // - // buttonAddMap + // saveToolStripMenuItem // - this.buttonAddMap.Location = new System.Drawing.Point(14, 112); - this.buttonAddMap.Name = "buttonAddMap"; - this.buttonAddMap.Size = new System.Drawing.Size(191, 27); - this.buttonAddMap.TabIndex = 1; - this.buttonAddMap.Text = "AddMap"; - this.buttonAddMap.UseVisualStyleBackColor = true; - this.buttonAddMap.Click += new System.EventHandler(this.ButtonAddMap_Click); + this.saveToolStripMenuItem.Name = "saveToolStripMenuItem"; + this.saveToolStripMenuItem.Size = new System.Drawing.Size(270, 34); + this.saveToolStripMenuItem.Text = "Save"; + this.saveToolStripMenuItem.Click += new System.EventHandler(this.saveToolStripMenuItem_Click); // - // buttonDeleteMap + // loadToolStripMenuItem // - this.buttonDeleteMap.Location = new System.Drawing.Point(14, 233); - this.buttonDeleteMap.Name = "buttonDeleteMap"; - this.buttonDeleteMap.Size = new System.Drawing.Size(191, 25); - this.buttonDeleteMap.TabIndex = 2; - this.buttonDeleteMap.Text = "DeleteMap"; - this.buttonDeleteMap.UseVisualStyleBackColor = true; - this.buttonDeleteMap.Click += new System.EventHandler(this.ButtonDeleteMap_Click); + this.loadToolStripMenuItem.Name = "loadToolStripMenuItem"; + this.loadToolStripMenuItem.Size = new System.Drawing.Size(270, 34); + this.loadToolStripMenuItem.Text = "Load"; + this.loadToolStripMenuItem.Click += new System.EventHandler(this.loadToolStripMenuItem_Click); // - // listBoxMaps + // openFileDialog // - this.listBoxMaps.FormattingEnabled = true; - this.listBoxMaps.ItemHeight = 20; - this.listBoxMaps.Location = new System.Drawing.Point(14, 146); - this.listBoxMaps.Name = "listBoxMaps"; - this.listBoxMaps.Size = new System.Drawing.Size(191, 84); - this.listBoxMaps.TabIndex = 3; - this.listBoxMaps.SelectedIndexChanged += new System.EventHandler(this.ListBoxMaps_SelectedIndexChanged); + this.openFileDialog.Filter = "txt file | *.txt"; // // FormMapWithSetBoats // @@ -245,14 +290,19 @@ this.ClientSize = new System.Drawing.Size(956, 647); this.Controls.Add(this.pictureBox); this.Controls.Add(this.groupBoxTools); + this.Controls.Add(this.menuStrip); + this.MainMenuStrip = this.menuStrip; this.Name = "FormMapWithSetBoats"; this.Text = "FormMapWithSetBoats"; this.groupBoxTools.ResumeLayout(false); this.groupBoxTools.PerformLayout(); - ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit(); 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 +325,11 @@ private System.Windows.Forms.Button buttonDeleteMap; private System.Windows.Forms.Button buttonAddMap; private System.Windows.Forms.TextBox textBoxNewMapName; + private System.Windows.Forms.MenuStrip menuStrip; + private System.Windows.Forms.ToolStripMenuItem fileToolStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem saveToolStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem loadToolStripMenuItem; + private System.Windows.Forms.OpenFileDialog openFileDialog; + private System.Windows.Forms.SaveFileDialog saveFileDialog; } } \ No newline at end of file diff --git a/Catamaran/FormMapWithSetBoats.cs b/Catamaran/FormMapWithSetBoats.cs index 1f0c0ef..48c7d19 100644 --- a/Catamaran/FormMapWithSetBoats.cs +++ b/Catamaran/FormMapWithSetBoats.cs @@ -214,7 +214,7 @@ namespace Catamaran { var formBoatConfig = new FormBoatConfig(); formBoatConfig.AddEvent(new Action(AddBoat)); - // TODO Call method AddEvent from formCarConfig + // TODO Call method AddEvent from formBoatConfig formBoatConfig.Show(); } @@ -234,5 +234,40 @@ namespace Catamaran MessageBox.Show("Не удалось добавить объект"); } } + 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/Catamaran/FormMapWithSetBoats.resx b/Catamaran/FormMapWithSetBoats.resx index 174ebc7..2882e1c 100644 --- a/Catamaran/FormMapWithSetBoats.resx +++ b/Catamaran/FormMapWithSetBoats.resx @@ -117,7 +117,13 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - + 17, 17 + + 163, 17 + + + 357, 17 + \ No newline at end of file diff --git a/Catamaran/IDrawingObject.cs b/Catamaran/IDrawingObject.cs index ce8d31a..2c54719 100644 --- a/Catamaran/IDrawingObject.cs +++ b/Catamaran/IDrawingObject.cs @@ -38,5 +38,12 @@ namespace Catamaran (float Left, float Right, float Top, float Bottom) GetCurrentPosition(); + /// + /// Получение информации по объекту + /// + /// + string GetInfo(); + + } } diff --git a/Catamaran/MapWithSetBoatsGeneric.cs b/Catamaran/MapWithSetBoatsGeneric.cs index 267d621..c2887b5 100644 --- a/Catamaran/MapWithSetBoatsGeneric.cs +++ b/Catamaran/MapWithSetBoatsGeneric.cs @@ -182,5 +182,32 @@ namespace Catamaran _setBoats[i]?.DrawingObject(g); } } + + /// + /// Получение данных в виде строки + /// + /// + /// + public string GetData(char separatorType, char separatorData) + { + string data = $"{_map.GetType().Name}{separatorType}"; + foreach (var Boat in _setBoats.GetBoats()) + { + data += $"{Boat.GetInfo()}{separatorData}"; + } + return data; + } + /// + /// Загрузка списка из массива строк + /// + /// + public void LoadData(string[] records) + { + foreach (var rec in records) + { + _setBoats.Insert(DrawingObjectBoat.Create(rec) as T); + } + } + } } diff --git a/Catamaran/MapsCollection.cs b/Catamaran/MapsCollection.cs index 3b9c006..ee84381 100644 --- a/Catamaran/MapsCollection.cs +++ b/Catamaran/MapsCollection.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -11,7 +12,7 @@ namespace Catamaran /// /// Словарь (хранилище) с картами /// - readonly Dictionary> _mapStorages; + readonly Dictionary> _mapStorages; /// /// Возвращение списка названий карт @@ -30,9 +31,13 @@ namespace Catamaran /// /// /// + /// + private readonly char separatorDict = '|'; + + private readonly char separatorData = ';'; public MapsCollection(int pictureWidth, int pictureHeight) { - _mapStorages = new Dictionary>(); + _mapStorages = new Dictionary>(); _pictureWidth = pictureWidth; _pictureHeight = pictureHeight; } @@ -45,7 +50,7 @@ namespace Catamaran { if (!_mapStorages.ContainsKey(name)) { - _mapStorages.Add(name, new MapWithSetBoatsGeneric(_pictureWidth, _pictureHeight, map)); + _mapStorages.Add(name, new MapWithSetBoatsGeneric(_pictureWidth, _pictureHeight, map)); } } /// @@ -61,7 +66,7 @@ namespace Catamaran /// /// /// - public MapWithSetBoatsGeneric this[string ind] + public MapWithSetBoatsGeneric this[string ind] { get { @@ -70,5 +75,69 @@ namespace Catamaran } } + /// + /// Сохранение информации про лодки м в хранилище в файл + /// + /// Путь и имя файла + /// + public bool SaveData(string filename) + { + if (File.Exists(filename)) + { + File.Delete(filename); + } + using (StreamWriter sw = new StreamWriter(filename)) + { + sw.WriteLine("MapsCollection"); + foreach (var storage in _mapStorages) + { + + sw.WriteLine($"{storage.Key}{separatorDict}{storage.Value.GetData(separatorDict, separatorData)}"); + } + } + return true; + } + + /// + /// Загрузка нформации про лодки в гавани из файла + /// + /// + /// + + public bool LoadData(string filename) + { + if (!File.Exists(filename)) + { + return false; + } + using (StreamReader sr = new StreamReader(filename)) + { + string str = ""; + if ((str = sr.ReadLine()) == null || !str.Contains("MapsCollection")) + { + //если нет такой записи, то это не те данные + return false; + } + //очищаем записи + _mapStorages.Clear(); + while ((str = sr.ReadLine()) != null) + { + var elem = str.Split(separatorDict); + AbstractMap map = null; + switch (elem[1]) + { + case "SimpleMap": + map = new SimpleMap(); + break; + case "SecondMap": + map = new SecondMap(); + break; + } + _mapStorages.Add(elem[0], new MapWithSetBoatsGeneric(_pictureWidth, _pictureHeight, map)); + _mapStorages[elem[0]].LoadData(elem[2].Split(separatorData, (char)StringSplitOptions.RemoveEmptyEntries)); + } + } + return true; + } } }