diff --git a/ArmoredCar/ArmoredCar/DrawningObjectArmCar.cs b/ArmoredCar/ArmoredCar/DrawningObjectArmCar.cs index 09aebde..22effca 100644 --- a/ArmoredCar/ArmoredCar/DrawningObjectArmCar.cs +++ b/ArmoredCar/ArmoredCar/DrawningObjectArmCar.cs @@ -33,5 +33,8 @@ namespace ArmoredCar if (_armCar != null) _armCar.DrawTransport(g); } + public string GetInfo() => _armCar?.GetDataForSave(); + public static IDrawningObject Create(string data) => new + DrawningObjectArmCar(data.CreateDrawningArmoredCar()); } } diff --git a/ArmoredCar/ArmoredCar/ExtensionArmoredCar.cs b/ArmoredCar/ArmoredCar/ExtensionArmoredCar.cs new file mode 100644 index 0000000..ea0969a --- /dev/null +++ b/ArmoredCar/ArmoredCar/ExtensionArmoredCar.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 ArmoredCar +{ + internal static class ExtensionArmoredCar + { + /// + /// Разделитель для записи информации по объекту в файл + /// + private static readonly char _separatorForObject = ':'; + /// + /// Создание объекта из строки + /// + /// + /// + public static DrawningArmoredCar CreateDrawningArmoredCar(this string info) + { + string[] strs = info.Split(_separatorForObject); + if (strs.Length == 3) + { + return new DrawningArmoredCar(Convert.ToInt32(strs[0]), + Convert.ToInt32(strs[1]), Color.FromName(strs[2])); + } + if (strs.Length == 6) + { + return new DrawningTank(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 DrawningArmoredCar drawningArmoredCar) + { + var armoredCar = drawningArmoredCar.ArmoredCar; + var str = + $"{armoredCar.Speed}{_separatorForObject}{armoredCar.Weight}{_separatorForObject}{armoredCar.BodyColor.Name}"; + if (armoredCar is not EntityTank tank) + { + return str; + } + return + $"{str}{_separatorForObject}{tank.DopColor.Name}{_separatorForObject}{tank.TowerWeapon}{_separatorForObject}{tank.AMachineGun}"; + } + } +} + diff --git a/ArmoredCar/ArmoredCar/FormMapWithSetArmoredCars.Designer.cs b/ArmoredCar/ArmoredCar/FormMapWithSetArmoredCars.Designer.cs index ba49d7a..0cd41b5 100644 --- a/ArmoredCar/ArmoredCar/FormMapWithSetArmoredCars.Designer.cs +++ b/ArmoredCar/ArmoredCar/FormMapWithSetArmoredCars.Designer.cs @@ -47,9 +47,16 @@ namespace ArmoredCar this.buttonShowOnMap = new System.Windows.Forms.Button(); this.buttonAddCar = 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.groupBoxTools.SuspendLayout(); this.groupBoxMaps.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit(); + this.menuStrip1.SuspendLayout(); this.SuspendLayout(); // // groupBoxTools @@ -65,9 +72,9 @@ namespace ArmoredCar this.groupBoxTools.Controls.Add(this.buttonShowOnMap); this.groupBoxTools.Controls.Add(this.buttonAddCar); this.groupBoxTools.Dock = System.Windows.Forms.DockStyle.Right; - this.groupBoxTools.Location = new System.Drawing.Point(811, 0); + this.groupBoxTools.Location = new System.Drawing.Point(802, 24); this.groupBoxTools.Name = "groupBoxTools"; - this.groupBoxTools.Size = new System.Drawing.Size(204, 632); + this.groupBoxTools.Size = new System.Drawing.Size(204, 637); this.groupBoxTools.TabIndex = 0; this.groupBoxTools.TabStop = false; this.groupBoxTools.Text = "Инструменты"; @@ -168,7 +175,7 @@ namespace ArmoredCar this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonDown.BackgroundImage = global::ArmoredCar.Properties.Resources.Down; this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonDown.Location = new System.Drawing.Point(91, 582); + this.buttonDown.Location = new System.Drawing.Point(91, 587); this.buttonDown.Name = "buttonDown"; this.buttonDown.Size = new System.Drawing.Size(30, 30); this.buttonDown.TabIndex = 10; @@ -180,7 +187,7 @@ namespace ArmoredCar this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonRight.BackgroundImage = global::ArmoredCar.Properties.Resources.Right; this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonRight.Location = new System.Drawing.Point(127, 582); + this.buttonRight.Location = new System.Drawing.Point(127, 587); this.buttonRight.Name = "buttonRight"; this.buttonRight.Size = new System.Drawing.Size(30, 30); this.buttonRight.TabIndex = 9; @@ -192,7 +199,7 @@ namespace ArmoredCar this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonLeft.BackgroundImage = global::ArmoredCar.Properties.Resources.Left; this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonLeft.Location = new System.Drawing.Point(55, 582); + this.buttonLeft.Location = new System.Drawing.Point(55, 587); this.buttonLeft.Name = "buttonLeft"; this.buttonLeft.Size = new System.Drawing.Size(30, 30); this.buttonLeft.TabIndex = 8; @@ -204,7 +211,7 @@ namespace ArmoredCar this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonUp.BackgroundImage = global::ArmoredCar.Properties.Resources.Up; this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonUp.Location = new System.Drawing.Point(91, 546); + this.buttonUp.Location = new System.Drawing.Point(91, 551); this.buttonUp.Name = "buttonUp"; this.buttonUp.Size = new System.Drawing.Size(30, 30); this.buttonUp.TabIndex = 7; @@ -234,19 +241,62 @@ namespace ArmoredCar // 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, 24); this.pictureBox.Name = "pictureBox"; - this.pictureBox.Size = new System.Drawing.Size(811, 632); + this.pictureBox.Size = new System.Drawing.Size(802, 637); this.pictureBox.TabIndex = 1; this.pictureBox.TabStop = false; // + // menuStrip1 + // + 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(1006, 24); + 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(48, 20); + this.FileToolStripMenuItem.Text = "Файл"; + // + // SaveToolStripMenuItem + // + this.SaveToolStripMenuItem.Name = "SaveToolStripMenuItem"; + this.SaveToolStripMenuItem.Size = new System.Drawing.Size(141, 22); + this.SaveToolStripMenuItem.Text = "Сохранение"; + this.SaveToolStripMenuItem.Click += new System.EventHandler(this.SaveToolStripMenuItem_Click); + // + // LoadToolStripMenuItem + // + this.LoadToolStripMenuItem.Name = "LoadToolStripMenuItem"; + this.LoadToolStripMenuItem.Size = new System.Drawing.Size(141, 22); + 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"; + // // FormMapWithSetArmoredCars // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(1015, 632); + this.ClientSize = new System.Drawing.Size(1006, 661); this.Controls.Add(this.pictureBox); this.Controls.Add(this.groupBoxTools); + this.Controls.Add(this.menuStrip1); + this.MainMenuStrip = this.menuStrip1; this.Name = "FormMapWithSetArmoredCars"; this.Text = "Карта с набором объектов"; this.groupBoxTools.ResumeLayout(false); @@ -254,7 +304,10 @@ namespace ArmoredCar this.groupBoxMaps.ResumeLayout(false); this.groupBoxMaps.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit(); + this.menuStrip1.ResumeLayout(false); + this.menuStrip1.PerformLayout(); this.ResumeLayout(false); + this.PerformLayout(); } @@ -277,5 +330,11 @@ namespace ArmoredCar private ListBox listBoxMaps; private TextBox textBoxNewMapName; private Button buttonAddMap; + 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/ArmoredCar/ArmoredCar/FormMapWithSetArmoredCars.cs b/ArmoredCar/ArmoredCar/FormMapWithSetArmoredCars.cs index 89f0d00..c99cb31 100644 --- a/ArmoredCar/ArmoredCar/FormMapWithSetArmoredCars.cs +++ b/ArmoredCar/ArmoredCar/FormMapWithSetArmoredCars.cs @@ -220,5 +220,49 @@ namespace ArmoredCar } 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); + } + } + } + } } \ No newline at end of file diff --git a/ArmoredCar/ArmoredCar/FormMapWithSetArmoredCars.resx b/ArmoredCar/ArmoredCar/FormMapWithSetArmoredCars.resx index f298a7b..a990664 100644 --- a/ArmoredCar/ArmoredCar/FormMapWithSetArmoredCars.resx +++ b/ArmoredCar/ArmoredCar/FormMapWithSetArmoredCars.resx @@ -57,4 +57,16 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + 279, 17 + + + 17, 17 + + + 150, 17 + + + 25 + \ No newline at end of file diff --git a/ArmoredCar/ArmoredCar/IDrawningObject.cs b/ArmoredCar/ArmoredCar/IDrawningObject.cs index 64307a1..4e97bfa 100644 --- a/ArmoredCar/ArmoredCar/IDrawningObject.cs +++ b/ArmoredCar/ArmoredCar/IDrawningObject.cs @@ -36,5 +36,10 @@ namespace ArmoredCar /// /// (float Left, float Right, float Top, float Bottom) GetCurrentPosition(); + /// + /// Получение информации по объекту + /// + /// + string GetInfo(); } } diff --git a/ArmoredCar/ArmoredCar/MapWithSetArmoredCarsGeneric.cs b/ArmoredCar/ArmoredCar/MapWithSetArmoredCarsGeneric.cs index ce1d67a..e4a7e64 100644 --- a/ArmoredCar/ArmoredCar/MapWithSetArmoredCarsGeneric.cs +++ b/ArmoredCar/ArmoredCar/MapWithSetArmoredCarsGeneric.cs @@ -181,5 +181,30 @@ namespace ArmoredCar k++; } } + /// + /// Получение данных в виде строки + /// + /// + /// + public string GetData(char separatorType, char separatorData) + { + string data = $"{_map.GetType().Name}{separatorType}"; + foreach (var armoredCar in _setCars.GetCars()) + { + data += $"{armoredCar.GetInfo()}{separatorData}"; + } + return data; + } + /// + /// Загрузка списка из массива строк + /// + /// + public void LoadData(string[] records) + { + foreach (var rec in records) + { + _setCars.Insert(DrawningObjectArmCar.Create(rec) as T); + } + } } } diff --git a/ArmoredCar/ArmoredCar/MapsCollection.cs b/ArmoredCar/ArmoredCar/MapsCollection.cs index 6d7db7a..19195c8 100644 --- a/ArmoredCar/ArmoredCar/MapsCollection.cs +++ b/ArmoredCar/ArmoredCar/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; @@ -10,7 +11,7 @@ namespace ArmoredCar { /// Словарь (хранилище) с картами /// - readonly Dictionary> _mapStorages; + readonly Dictionary> _mapStorages; /// /// Возвращение списка названий карт /// @@ -24,13 +25,21 @@ namespace ArmoredCar /// 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; } @@ -42,7 +51,7 @@ namespace ArmoredCar public void AddMap(string name, AbstractMap map) { if (!Keys.Contains(name)) - _mapStorages.Add(name, new MapWithSetArmoredCarsGeneric(_pictureWidth, _pictureHeight, map)); + _mapStorages.Add(name, new MapWithSetArmoredCarsGeneric(_pictureWidth, _pictureHeight, map)); } /// /// Удаление карты @@ -58,7 +67,7 @@ namespace ArmoredCar /// /// /// - public MapWithSetArmoredCarsGeneric this[string + public MapWithSetArmoredCarsGeneric this[string ind] { get @@ -68,5 +77,70 @@ namespace ArmoredCar return null; } } + + /// + /// Сохранение информации по автомобилям в хранилище в файл + /// + /// Путь и имя файла + /// + public bool SaveData(string filename) + { + if (File.Exists(filename)) + { + File.Delete(filename); + } + using (FileStream f = new FileStream(filename, FileMode.Create)) + using (StreamWriter sw = new StreamWriter(f, Encoding.UTF8)) + { + 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 (FileStream f = new(filename, FileMode.Open)) + using (StreamReader sw = new(f, Encoding.UTF8)) + { + + if (!sw.ReadLine().Contains("MapsCollection")) + return false; + string? line; + _mapStorages.Clear(); + while ((line = sw.ReadLine()) != null) + { + var elem = line.Split(separatorDict); + AbstractMap map = null; + switch (elem[1]) + { + case "SimpleMap": + map = new SimpleMap(); + break; + case "MyMapLabirinth": + map = new MyMapLabirinth(); + break; + case "MyMapWooden": + map = new MyMapWooden(); + break; + } + _mapStorages.Add(elem[0], new MapWithSetArmoredCarsGeneric(_pictureWidth, _pictureHeight, map)); + _mapStorages[elem[0]].LoadData(elem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries)); + } + return true; + } + } } }