diff --git a/AirBomber/AirBomber/DrawningObjectJet.cs b/AirBomber/AirBomber/DrawningObjectJet.cs index 273fd65..d9c0702 100644 --- a/AirBomber/AirBomber/DrawningObjectJet.cs +++ b/AirBomber/AirBomber/DrawningObjectJet.cs @@ -31,5 +31,7 @@ namespace AirBomber { _jet.DrawTransport(g); } + public string GetInfo() => _jet?.GetDataForSave(); + public static IDrawningObject Create(string data) => new DrawningObjectJet(data.CreateDrawningJet()); } } diff --git a/AirBomber/AirBomber/ExtentionJet.cs b/AirBomber/AirBomber/ExtentionJet.cs new file mode 100644 index 0000000..e0e739c --- /dev/null +++ b/AirBomber/AirBomber/ExtentionJet.cs @@ -0,0 +1,56 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AirBomber +{ + /// + /// Расширение для класса DrawningJet + /// + internal static class ExtentionJet + { + /// + /// Разделитель для записи информации по объекту в файл + /// + private static readonly char _separatorForObject = ':'; + /// + /// Создание объекта из строки + /// + /// + /// + public static DrawningJet CreateDrawningJet(this string info) + { + string[] strs = info.Split(_separatorForObject); + if (strs.Length == 3) + { + return new DrawningJet(Convert.ToInt32(strs[0]), + Convert.ToInt32(strs[1]), Color.FromName(strs[2])); + } + if (strs.Length == 7) + { + return new DrawningSportJet(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 DrawningJet drawningJet) + { + var jet = drawningJet.Jet; + var str = $"{jet.Speed}{_separatorForObject}{jet.Weight}{_separatorForObject}{jet.BodyColor.Name}"; + if (jet is not EntitySportJet sportCar) + { + return str; + } + return $"{str}{_separatorForObject}{sportCar.DopColor.Name}{_separatorForObject}{sportCar.BodyKit}{_separatorForObject}{sportCar.Wing}"; + } + } +} diff --git a/AirBomber/AirBomber/FormMapWithSetJets.Designer.cs b/AirBomber/AirBomber/FormMapWithSetJets.Designer.cs index 216095e..6ae1899 100644 --- a/AirBomber/AirBomber/FormMapWithSetJets.Designer.cs +++ b/AirBomber/AirBomber/FormMapWithSetJets.Designer.cs @@ -49,9 +49,16 @@ this.ButtonAddJet = new System.Windows.Forms.Button(); this.ButtonRemoveJet = new System.Windows.Forms.Button(); this.pictureBoxJet = new System.Windows.Forms.PictureBox(); + this.menuStrip1 = 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.groupBox2.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxJet)).BeginInit(); + this.menuStrip1.SuspendLayout(); this.SuspendLayout(); // // buttonDown1 @@ -59,7 +66,7 @@ this.buttonDown1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonDown1.BackgroundImage = global::AirBomber.Properties.Resources.arrowDown; this.buttonDown1.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; - this.buttonDown1.Location = new System.Drawing.Point(68, 899); + this.buttonDown1.Location = new System.Drawing.Point(68, 884); this.buttonDown1.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); this.buttonDown1.Name = "buttonDown1"; this.buttonDown1.Size = new System.Drawing.Size(35, 30); @@ -68,7 +75,7 @@ // // groupBox1 // - this.groupBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) + this.groupBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Right))); this.groupBox1.Controls.Add(this.groupBox2); this.groupBox1.Controls.Add(this.buttonRight); @@ -84,9 +91,9 @@ this.groupBox1.Controls.Add(this.maskedTextBoxPosition); this.groupBox1.Controls.Add(this.ButtonAddJet); this.groupBox1.Controls.Add(this.ButtonRemoveJet); - this.groupBox1.Location = new System.Drawing.Point(893, 12); + this.groupBox1.Location = new System.Drawing.Point(893, 27); this.groupBox1.Name = "groupBox1"; - this.groupBox1.Size = new System.Drawing.Size(191, 561); + this.groupBox1.Size = new System.Drawing.Size(191, 546); this.groupBox1.TabIndex = 7; this.groupBox1.TabStop = false; this.groupBox1.Text = "Инструменты"; @@ -156,7 +163,7 @@ this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonRight.BackgroundImage = global::AirBomber.Properties.Resources.arrowRight; this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; - this.buttonRight.Location = new System.Drawing.Point(116, 509); + this.buttonRight.Location = new System.Drawing.Point(116, 494); this.buttonRight.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); this.buttonRight.Name = "buttonRight"; this.buttonRight.Size = new System.Drawing.Size(35, 30); @@ -169,7 +176,7 @@ this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonLeft.BackgroundImage = global::AirBomber.Properties.Resources.arrowLeft; this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; - this.buttonLeft.Location = new System.Drawing.Point(34, 509); + this.buttonLeft.Location = new System.Drawing.Point(34, 494); this.buttonLeft.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); this.buttonLeft.Name = "buttonLeft"; this.buttonLeft.Size = new System.Drawing.Size(35, 30); @@ -182,7 +189,7 @@ this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonUp.BackgroundImage = global::AirBomber.Properties.Resources.arrowUp; this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; - this.buttonUp.Location = new System.Drawing.Point(75, 475); + this.buttonUp.Location = new System.Drawing.Point(75, 460); this.buttonUp.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); this.buttonUp.Name = "buttonUp"; this.buttonUp.Size = new System.Drawing.Size(35, 30); @@ -195,7 +202,7 @@ this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonDown.BackgroundImage = global::AirBomber.Properties.Resources.arrowDown; this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; - this.buttonDown.Location = new System.Drawing.Point(75, 509); + this.buttonDown.Location = new System.Drawing.Point(75, 494); this.buttonDown.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); this.buttonDown.Name = "buttonDown"; this.buttonDown.Size = new System.Drawing.Size(35, 30); @@ -208,7 +215,7 @@ this.buttonRight1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonRight1.BackgroundImage = global::AirBomber.Properties.Resources.arrowRight; this.buttonRight1.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; - this.buttonRight1.Location = new System.Drawing.Point(109, 899); + this.buttonRight1.Location = new System.Drawing.Point(109, 884); this.buttonRight1.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); this.buttonRight1.Name = "buttonRight1"; this.buttonRight1.Size = new System.Drawing.Size(35, 30); @@ -220,7 +227,7 @@ this.buttonLeft1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonLeft1.BackgroundImage = global::AirBomber.Properties.Resources.arrowLeft; this.buttonLeft1.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; - this.buttonLeft1.Location = new System.Drawing.Point(27, 899); + this.buttonLeft1.Location = new System.Drawing.Point(27, 884); this.buttonLeft1.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); this.buttonLeft1.Name = "buttonLeft1"; this.buttonLeft1.Size = new System.Drawing.Size(35, 30); @@ -232,7 +239,7 @@ this.buttonUp1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonUp1.BackgroundImage = global::AirBomber.Properties.Resources.arrowUp; this.buttonUp1.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; - this.buttonUp1.Location = new System.Drawing.Point(68, 865); + this.buttonUp1.Location = new System.Drawing.Point(68, 850); this.buttonUp1.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); this.buttonUp1.Name = "buttonUp1"; this.buttonUp1.Size = new System.Drawing.Size(35, 30); @@ -288,15 +295,56 @@ // // pictureBoxJet // - this.pictureBoxJet.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) - | System.Windows.Forms.AnchorStyles.Left) + this.pictureBoxJet.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) + | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); - this.pictureBoxJet.Location = new System.Drawing.Point(0, 0); + this.pictureBoxJet.Location = new System.Drawing.Point(0, 27); this.pictureBoxJet.Name = "pictureBoxJet"; - this.pictureBoxJet.Size = new System.Drawing.Size(887, 573); + this.pictureBoxJet.Size = new System.Drawing.Size(887, 546); this.pictureBoxJet.TabIndex = 6; this.pictureBoxJet.TabStop = false; // + // menuStrip1 + // + this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.файлToolStripMenuItem}); + this.menuStrip1.Location = new System.Drawing.Point(0, 0); + this.menuStrip1.Name = "menuStrip1"; + this.menuStrip1.Size = new System.Drawing.Size(1096, 24); + this.menuStrip1.TabIndex = 8; + this.menuStrip1.Text = "menuStrip1"; + // + // файл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(48, 20); + this.файлToolStripMenuItem.Text = "Файл"; + // + // SaveToolStripMenuItem + // + this.SaveToolStripMenuItem.Name = "SaveToolStripMenuItem"; + this.SaveToolStripMenuItem.Size = new System.Drawing.Size(180, 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(180, 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"; + // // FormMapWithSetJets // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); @@ -304,6 +352,8 @@ this.ClientSize = new System.Drawing.Size(1096, 573); this.Controls.Add(this.groupBox1); this.Controls.Add(this.pictureBoxJet); + this.Controls.Add(this.menuStrip1); + this.MainMenuStrip = this.menuStrip1; this.Name = "FormMapWithSetJets"; this.Text = "Карта с набором объектов"; this.groupBox1.ResumeLayout(false); @@ -311,7 +361,10 @@ this.groupBox2.ResumeLayout(false); this.groupBox2.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxJet)).EndInit(); + this.menuStrip1.ResumeLayout(false); + this.menuStrip1.PerformLayout(); this.ResumeLayout(false); + this.PerformLayout(); } @@ -338,5 +391,11 @@ private ListBox listBoxMaps; private Button ButtonDeleteMap; private TextBox textBoxNewMapName; + private MenuStrip menuStrip1; + private ToolStripMenuItem файлToolStripMenuItem; + private ToolStripMenuItem SaveToolStripMenuItem; + private ToolStripMenuItem LoadToolStripMenuItem; + private OpenFileDialog openFileDialog; + private SaveFileDialog saveFileDialog; } } \ No newline at end of file diff --git a/AirBomber/AirBomber/FormMapWithSetJets.cs b/AirBomber/AirBomber/FormMapWithSetJets.cs index d195f1f..51322a9 100644 --- a/AirBomber/AirBomber/FormMapWithSetJets.cs +++ b/AirBomber/AirBomber/FormMapWithSetJets.cs @@ -234,5 +234,36 @@ namespace AirBomber } pictureBoxJet.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); + } + } + } } } diff --git a/AirBomber/AirBomber/FormMapWithSetJets.resx b/AirBomber/AirBomber/FormMapWithSetJets.resx index f298a7b..b43eb12 100644 --- a/AirBomber/AirBomber/FormMapWithSetJets.resx +++ b/AirBomber/AirBomber/FormMapWithSetJets.resx @@ -57,4 +57,13 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + 417, 19 + + + 132, 17 + + + 272, 17 + \ No newline at end of file diff --git a/AirBomber/AirBomber/IDrawningObject.cs b/AirBomber/AirBomber/IDrawningObject.cs index acaf82d..5457609 100644 --- a/AirBomber/AirBomber/IDrawningObject.cs +++ b/AirBomber/AirBomber/IDrawningObject.cs @@ -35,5 +35,10 @@ namespace AirBomber /// /// (float Left, float Right, float Top, float Bottom) GetCurrentPosition(); + /// + /// Получение информации по объекту + /// + /// + string GetInfo(); } } diff --git a/AirBomber/AirBomber/MapWithSetJetsGeneric.cs b/AirBomber/AirBomber/MapWithSetJetsGeneric.cs index ee86681..49dd752 100644 --- a/AirBomber/AirBomber/MapWithSetJetsGeneric.cs +++ b/AirBomber/AirBomber/MapWithSetJetsGeneric.cs @@ -197,5 +197,31 @@ namespace AirBomber } } } + /// + /// Получение данных в виде строки + /// + /// Разделитель карт + /// Разделитель самолетов + /// + public string GetData(char separatorType, char separatorData) + { + string data = $"{_map.GetType().Name}{separatorType}"; + foreach (var car in _setJets.GetJets()) + { + data += $"{car.GetInfo()}{separatorData}"; + } + return data; + } + /// + /// Загрузка списка из массива строк + /// + /// + public void LoadData(string[] records) + { + foreach (var rec in records) + { + _setJets.Insert(DrawningObjectJet.Create(rec) as T); + } + } } } diff --git a/AirBomber/AirBomber/MapsCollection.cs b/AirBomber/AirBomber/MapsCollection.cs index 20b280e..d34c6f9 100644 --- a/AirBomber/AirBomber/MapsCollection.cs +++ b/AirBomber/AirBomber/MapsCollection.cs @@ -14,7 +14,7 @@ namespace AirBomber /// /// Словарь (хранилище) с картами /// - readonly Dictionary> _mapStorages; + readonly Dictionary> _mapStorages; /// /// Возвращение списка названий карт /// @@ -28,13 +28,21 @@ namespace AirBomber /// private readonly int _pictureHeight; /// + /// Разделитель карт + /// + private static readonly char separatorDict = '|'; + /// + /// Разделитель самолетов + /// + private static readonly char separatorData = ','; + /// /// Конструктор /// /// /// public MapsCollection(int pictureWidth, int pictureHeight) { - _mapStorages = new Dictionary>(); + _mapStorages = new Dictionary>(); _pictureWidth = pictureWidth; _pictureHeight = pictureHeight; } @@ -45,14 +53,14 @@ namespace AirBomber /// Карта public void AddMap(string name, AbstractMap map) { - MapWithSetJetsGeneric mapJetsCollectionGeneric; + MapWithSetJetsGeneric mapJetsCollectionGeneric; // Если карта или её имя не задано - выходим if (map == null || string.IsNullOrEmpty(name)) return; // Если совершается попытка добавить карту с уже существующим именем, то выходим if (_mapStorages.ContainsKey(name)) return; - mapJetsCollectionGeneric = new MapWithSetJetsGeneric(_pictureWidth, _pictureHeight, map); + mapJetsCollectionGeneric = new MapWithSetJetsGeneric(_pictureWidth, _pictureHeight, map); _mapStorages.Add(name, mapJetsCollectionGeneric); } @@ -72,14 +80,85 @@ namespace AirBomber /// /// /// - public MapWithSetJetsGeneric this[string ind] + public MapWithSetJetsGeneric this[string ind] { get { - if (string.IsNullOrEmpty(ind)) return null; - return _mapStorages[ind]; + _mapStorages.TryGetValue(ind, out var result); + 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 fs = new(filename)) + { + fs.Write($"MapsCollection{Environment.NewLine}"); + foreach (var storage in _mapStorages) + { + fs.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)) + { + 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 "Простая карта": + map = new SimpleMap(); + break; + case "Небо": + map = new SkyMap(); + break; + } + _mapStorages.Add(elem[0], new MapWithSetJetsGeneric(_pictureWidth, _pictureHeight, map)); + _mapStorages[elem[0]].LoadData(elem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries)); + } + } + return true; + } } }