diff --git a/Stormtrooper/Stormtrooper/DrawningObjectStorm.cs b/Stormtrooper/Stormtrooper/DrawningObjectStorm.cs index 6dac87e..16e65bd 100644 --- a/Stormtrooper/Stormtrooper/DrawningObjectStorm.cs +++ b/Stormtrooper/Stormtrooper/DrawningObjectStorm.cs @@ -32,6 +32,8 @@ namespace Stormtrooper ((DrawningMilitary)_storm).DrawTransport(g); _storm.DrawTransport(g); } + public string GetInfo() => _storm?.GetDataForSave(); + public static IDrawningObject Create(string data) => new DrawningObjectStorm(data.CreateDrawningStormtrooper()); } } diff --git a/Stormtrooper/Stormtrooper/ExtentionStormtrooper.cs b/Stormtrooper/Stormtrooper/ExtentionStormtrooper.cs new file mode 100644 index 0000000..1b1a21f --- /dev/null +++ b/Stormtrooper/Stormtrooper/ExtentionStormtrooper.cs @@ -0,0 +1,58 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Stormtrooper +{ + /// + /// Расширение для класса DrawningStormtrooper + /// + internal static class ExtentionStormtrooper + { + /// + /// Разделитель для записи информации по объекту в файл + /// + private static readonly char _separatorForObject = ':'; + /// + /// Создание объекта из строки + /// + /// + /// + public static Drawning CreateDrawningStormtrooper(this string info) + { + string[] strs = info.Split(_separatorForObject); + if (strs.Length == 3) + { + return new Drawning(Convert.ToInt32(strs[0]), + Convert.ToInt32(strs[1]), Color.FromName(strs[2])); + } + if (strs.Length == 7) + { + return new DrawningMilitary(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 Drawning drawningStormtrooper) + { + var stormtrooper = drawningStormtrooper.Storm; + var str = $"{stormtrooper.Speed}{_separatorForObject}{stormtrooper.Weight}{_separatorForObject}{stormtrooper.BodyColor.Name}"; + if (stormtrooper is not EntityMilitaryStormtrooper militaryStormtrooper) + { + return str; + } + return + $"{str}{_separatorForObject}{militaryStormtrooper.DopColor.Name}{_separatorForObject}{militaryStormtrooper .BodyKit}{_separatorForObject}{militaryStormtrooper.Rocket}{_separatorForObject}{militaryStormtrooper.SportLine}"; + } + } + +} diff --git a/Stormtrooper/Stormtrooper/FormMapWithSetStormtroopers.Designer.cs b/Stormtrooper/Stormtrooper/FormMapWithSetStormtroopers.Designer.cs index 5728514..28736b6 100644 --- a/Stormtrooper/Stormtrooper/FormMapWithSetStormtroopers.Designer.cs +++ b/Stormtrooper/Stormtrooper/FormMapWithSetStormtroopers.Designer.cs @@ -45,9 +45,16 @@ this.buttonShowOnMap = new System.Windows.Forms.Button(); this.buttonAddCar = new System.Windows.Forms.Button(); this.pictureBox = new System.Windows.Forms.PictureBox(); + 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(); this.groupBoxMaps.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit(); + this.menuStrip.SuspendLayout(); this.SuspendLayout(); // // groupBoxTools @@ -63,11 +70,11 @@ 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(927, 0); + this.groupBoxTools.Location = new System.Drawing.Point(927, 28); this.groupBoxTools.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.groupBoxTools.Name = "groupBoxTools"; this.groupBoxTools.Padding = new System.Windows.Forms.Padding(3, 4, 3, 4); - this.groupBoxTools.Size = new System.Drawing.Size(233, 884); + this.groupBoxTools.Size = new System.Drawing.Size(233, 856); this.groupBoxTools.TabIndex = 0; this.groupBoxTools.TabStop = false; this.groupBoxTools.Text = "Инструменты"; @@ -176,7 +183,7 @@ this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonDown.BackgroundImage = global::Stormtrooper.Properties.Resources.arrowDown; this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonDown.Location = new System.Drawing.Point(104, 817); + this.buttonDown.Location = new System.Drawing.Point(104, 789); this.buttonDown.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.buttonDown.Name = "buttonDown"; this.buttonDown.Size = new System.Drawing.Size(34, 40); @@ -189,7 +196,7 @@ this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonRight.BackgroundImage = global::Stormtrooper.Properties.Resources.arrowRight; this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonRight.Location = new System.Drawing.Point(145, 817); + this.buttonRight.Location = new System.Drawing.Point(145, 789); this.buttonRight.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.buttonRight.Name = "buttonRight"; this.buttonRight.Size = new System.Drawing.Size(34, 40); @@ -202,7 +209,7 @@ this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonLeft.BackgroundImage = global::Stormtrooper.Properties.Resources.arrowLeft; this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonLeft.Location = new System.Drawing.Point(63, 817); + this.buttonLeft.Location = new System.Drawing.Point(63, 789); this.buttonLeft.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.buttonLeft.Name = "buttonLeft"; this.buttonLeft.Size = new System.Drawing.Size(34, 40); @@ -215,7 +222,7 @@ this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonUp.BackgroundImage = global::Stormtrooper.Properties.Resources.arrowUp; this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonUp.Location = new System.Drawing.Point(104, 769); + this.buttonUp.Location = new System.Drawing.Point(104, 741); this.buttonUp.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.buttonUp.Name = "buttonUp"; this.buttonUp.Size = new System.Drawing.Size(34, 40); @@ -248,13 +255,54 @@ // 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(927, 884); + this.pictureBox.Size = new System.Drawing.Size(927, 856); 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.FileToolStripMenuItem}); + this.menuStrip.Location = new System.Drawing.Point(0, 0); + this.menuStrip.Name = "menuStrip"; + this.menuStrip.Size = new System.Drawing.Size(1160, 28); + this.menuStrip.TabIndex = 2; + // + // 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.Filter = "txt file | *.txt"; + // + // saveFileDialog + // + this.saveFileDialog.Filter = "txt file | *.txt"; + // // FormMapWithSetStormtroopers // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); @@ -262,6 +310,8 @@ this.ClientSize = new System.Drawing.Size(1160, 884); this.Controls.Add(this.pictureBox); this.Controls.Add(this.groupBoxTools); + this.Controls.Add(this.menuStrip); + this.MainMenuStrip = this.menuStrip; this.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.Name = "FormMapWithSetStormtroopers"; this.Text = "Карта с набором объектов"; @@ -270,7 +320,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(); } @@ -292,5 +345,11 @@ private Button buttonDeleteMap; private Button buttonAddMap; private TextBox textBoxNewMapName; + private MenuStrip menuStrip; + private ToolStripMenuItem FileToolStripMenuItem; + private ToolStripMenuItem SaveToolStripMenuItem; + private ToolStripMenuItem LoadToolStripMenuItem; + private OpenFileDialog openFileDialog; + private SaveFileDialog saveFileDialog; } } \ No newline at end of file diff --git a/Stormtrooper/Stormtrooper/FormMapWithSetStormtroopers.cs b/Stormtrooper/Stormtrooper/FormMapWithSetStormtroopers.cs index 37260c9..6f89c82 100644 --- a/Stormtrooper/Stormtrooper/FormMapWithSetStormtroopers.cs +++ b/Stormtrooper/Stormtrooper/FormMapWithSetStormtroopers.cs @@ -207,5 +207,44 @@ namespace Stormtrooper 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/Stormtrooper/Stormtrooper/FormMapWithSetStormtroopers.resx b/Stormtrooper/Stormtrooper/FormMapWithSetStormtroopers.resx index f298a7b..985a3e4 100644 --- a/Stormtrooper/Stormtrooper/FormMapWithSetStormtroopers.resx +++ b/Stormtrooper/Stormtrooper/FormMapWithSetStormtroopers.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/Stormtrooper/Stormtrooper/IDrawningObject.cs b/Stormtrooper/Stormtrooper/IDrawningObject.cs index 82df051..539dbd0 100644 --- a/Stormtrooper/Stormtrooper/IDrawningObject.cs +++ b/Stormtrooper/Stormtrooper/IDrawningObject.cs @@ -38,6 +38,10 @@ namespace Stormtrooper /// /// (float Left, float Right, float Top, float Bottom) GetCurrentPosition(); - + /// + /// Получение информации по объекту + /// + /// + string GetInfo(); } } diff --git a/Stormtrooper/Stormtrooper/MapWithSetStormtroopersGeneric.cs b/Stormtrooper/Stormtrooper/MapWithSetStormtroopersGeneric.cs index 9d33343..75454c3 100644 --- a/Stormtrooper/Stormtrooper/MapWithSetStormtroopersGeneric.cs +++ b/Stormtrooper/Stormtrooper/MapWithSetStormtroopersGeneric.cs @@ -113,6 +113,33 @@ namespace Stormtrooper } return new(_pictureWidth, _pictureHeight); } + + /// + /// Получение данных в виде строки + /// + /// + /// + public string GetData(char separatorType, char separatorData) + { + string data = $"{_map.GetType().Name}{separatorType}"; + foreach (var stormtrooper in _setStormtroopers.GetStormtroopers()) + { + data += $"{stormtrooper.GetInfo()}{separatorData}"; + } + return data; + } + /// + /// Загрузка списка из массива строк + /// + /// + public void LoadData(string[] records) + { + foreach (var rec in records) + { + _setStormtroopers.Insert(DrawningObjectStorm.Create(rec) as T); + } + } + /// /// "Взбалтываем" набор, чтобы все элементы оказались в начале /// diff --git a/Stormtrooper/Stormtrooper/MapsCollection.cs b/Stormtrooper/Stormtrooper/MapsCollection.cs index ee68f69..6a9cc63 100644 --- a/Stormtrooper/Stormtrooper/MapsCollection.cs +++ b/Stormtrooper/Stormtrooper/MapsCollection.cs @@ -11,7 +11,7 @@ namespace Stormtrooper /// /// Словарь (хранилище) с картами /// - readonly Dictionary> _mapStorages; + readonly Dictionary> _mapStorages; /// /// Возвращение списка названий карт /// @@ -25,13 +25,21 @@ namespace Stormtrooper /// 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; } @@ -47,7 +55,7 @@ namespace Stormtrooper MessageBox.Show("Карта с таким названием уже существует!"); return; } - _mapStorages.Add(name, new MapWithSetStormtroopersGeneric(_pictureWidth, _pictureHeight, map)); + _mapStorages.Add(name, new MapWithSetStormtroopersGeneric(_pictureWidth, _pictureHeight, map)); } /// /// Удаление карты @@ -62,15 +70,82 @@ namespace Stormtrooper /// /// /// - public MapWithSetStormtroopersGeneric this[string ind] + public MapWithSetStormtroopersGeneric this[string ind] { get { if (_mapStorages.ContainsKey(ind)) return _mapStorages[ind]; - return null; } } + /// + /// Сохранение информации по самолётам в хранилище в файл + /// + /// Путь и имя файла + /// + public bool SaveData(string filename) + { + if (File.Exists(filename)) + { + File.Delete(filename); + } + using (StreamWriter sw = new(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; + } + 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 "Простая карта": + map = new SimpleMap(); + break; + case "Ясное небо": + map = new SecondMap(); + break; + case "Пасмурное небо": + map = new ThirdMap(); + break; + } + _mapStorages.Add(elem[0], new MapWithSetStormtroopersGeneric(_pictureWidth, _pictureHeight, map)); + _mapStorages[elem[0]].LoadData(elem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries)); + line = sw.ReadLine(); + } + + return true; + } + } } }