From ed234124660579d77f4f6544e57ad9d38f8f2e03 Mon Sep 17 00:00:00 2001 From: abazov73 <92822431+abazov73@users.noreply.github.com> Date: Tue, 15 Nov 2022 12:29:58 +0400 Subject: [PATCH] =?UTF-8?q?=D0=A8=D0=B5=D1=81=D1=82=D0=B0=D1=8F=20=D0=BB?= =?UTF-8?q?=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82=D0=BE=D1=80=D0=BD=D0=B0?= =?UTF-8?q?=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AirBomber/AirBomber/DrawingObjectAirBomber.cs | 4 + AirBomber/AirBomber/ExtentionAirBomber.cs | 53 ++++++++++++ .../FormMapWithSetAirBombers.Designer.cs | 77 +++++++++++++++-- .../AirBomber/FormMapWithSetAirBombers.cs | 31 +++++++ .../AirBomber/FormMapWithSetAirBombers.resx | 9 ++ AirBomber/AirBomber/IDrawingObject.cs | 2 + .../AirBomber/MapWithSetAirBombersGeneric.cs | 25 ++++++ AirBomber/AirBomber/MapsCollection.cs | 82 ++++++++++++++++++- 8 files changed, 271 insertions(+), 12 deletions(-) create mode 100644 AirBomber/AirBomber/ExtentionAirBomber.cs diff --git a/AirBomber/AirBomber/DrawingObjectAirBomber.cs b/AirBomber/AirBomber/DrawingObjectAirBomber.cs index 44d3809..4099fe1 100644 --- a/AirBomber/AirBomber/DrawingObjectAirBomber.cs +++ b/AirBomber/AirBomber/DrawingObjectAirBomber.cs @@ -44,5 +44,9 @@ namespace AirBomber _airBomber.DrawTransport(g); } } + + public string GetInfo() => _airBomber?.GetDataForSave(); + + public static IDrawingObject Create(string data) => new DrawingObjectAirBomber(data.CreateDrawningAirBomber()); } } diff --git a/AirBomber/AirBomber/ExtentionAirBomber.cs b/AirBomber/AirBomber/ExtentionAirBomber.cs new file mode 100644 index 0000000..a225cd3 --- /dev/null +++ b/AirBomber/AirBomber/ExtentionAirBomber.cs @@ -0,0 +1,53 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AirBomber +{ + internal static class ExtentionAirBomber + { + /// + /// Разделитель для записи информации по объекту в файл + /// + private static readonly char _separatorForObject = ':'; + /// + /// Создание объекта из строки + /// + /// + /// + public static DrawingAirBomber CreateDrawningAirBomber(this string info) + { + string[] strs = info.Split(_separatorForObject); + if (strs.Length == 3) + { + return new DrawingAirBomber(Convert.ToInt32(strs[0]), + Convert.ToInt32(strs[1]), Color.FromName(strs[2])); + } + if (strs.Length == 7) + { + return new DrawingHeavyAirBomber(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 DrawingAirBomber drawningAirBomber) + { + var airBomber = drawningAirBomber.AirBomber; + var str = $"{airBomber.Speed}{_separatorForObject}{airBomber.Weight}{_separatorForObject}{airBomber.BodyColor.Name}"; + if (airBomber is not EntityHeavyAirBomber heavyAirBomber) + { + return str; + } + return $"{str}{_separatorForObject}{heavyAirBomber.DopColor.Name}{_separatorForObject}{heavyAirBomber.Bombs}{_separatorForObject}{heavyAirBomber.FuelTank}{_separatorForObject}{heavyAirBomber.TailLine}"; + } + } +} diff --git a/AirBomber/AirBomber/FormMapWithSetAirBombers.Designer.cs b/AirBomber/AirBomber/FormMapWithSetAirBombers.Designer.cs index 5483272..e08ad46 100644 --- a/AirBomber/AirBomber/FormMapWithSetAirBombers.Designer.cs +++ b/AirBomber/AirBomber/FormMapWithSetAirBombers.Designer.cs @@ -45,9 +45,16 @@ this.maskedTextBoxPosition = new System.Windows.Forms.MaskedTextBox(); this.buttonAddAirBomber = 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 @@ -63,9 +70,9 @@ this.groupBoxTools.Controls.Add(this.maskedTextBoxPosition); this.groupBoxTools.Controls.Add(this.buttonAddAirBomber); this.groupBoxTools.Dock = System.Windows.Forms.DockStyle.Right; - this.groupBoxTools.Location = new System.Drawing.Point(621, 0); + this.groupBoxTools.Location = new System.Drawing.Point(621, 28); this.groupBoxTools.Name = "groupBoxTools"; - this.groupBoxTools.Size = new System.Drawing.Size(261, 671); + this.groupBoxTools.Size = new System.Drawing.Size(261, 643); this.groupBoxTools.TabIndex = 0; this.groupBoxTools.TabStop = false; this.groupBoxTools.Text = "Инструменты"; @@ -154,7 +161,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.Stretch; - this.buttonRight.Location = new System.Drawing.Point(148, 629); + this.buttonRight.Location = new System.Drawing.Point(148, 601); this.buttonRight.Name = "buttonRight"; this.buttonRight.Size = new System.Drawing.Size(30, 30); this.buttonRight.TabIndex = 18; @@ -166,7 +173,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.Stretch; - this.buttonDown.Location = new System.Drawing.Point(112, 629); + this.buttonDown.Location = new System.Drawing.Point(112, 601); this.buttonDown.Name = "buttonDown"; this.buttonDown.Size = new System.Drawing.Size(30, 30); this.buttonDown.TabIndex = 17; @@ -178,7 +185,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.Stretch; - this.buttonLeft.Location = new System.Drawing.Point(76, 629); + this.buttonLeft.Location = new System.Drawing.Point(76, 601); this.buttonLeft.Name = "buttonLeft"; this.buttonLeft.Size = new System.Drawing.Size(30, 30); this.buttonLeft.TabIndex = 16; @@ -190,7 +197,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.Stretch; - this.buttonUp.Location = new System.Drawing.Point(112, 593); + this.buttonUp.Location = new System.Drawing.Point(112, 565); this.buttonUp.Name = "buttonUp"; this.buttonUp.Size = new System.Drawing.Size(30, 30); this.buttonUp.TabIndex = 15; @@ -239,12 +246,55 @@ // 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.Name = "pictureBox"; - this.pictureBox.Size = new System.Drawing.Size(621, 671); + this.pictureBox.Size = new System.Drawing.Size(621, 643); this.pictureBox.TabIndex = 1; this.pictureBox.TabStop = false; // + // menuStrip1 + // + this.menuStrip1.ImageScalingSize = new System.Drawing.Size(20, 20); + 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(882, 28); + 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(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.FileName = "openFileDialog1"; + this.openFileDialog.Filter = "txt file | *.txt"; + // + // saveFileDialog + // + this.saveFileDialog.Filter = "txt file | *.txt"; + // // FormMapWithSetAirBombers // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); @@ -252,6 +302,8 @@ this.ClientSize = new System.Drawing.Size(882, 671); this.Controls.Add(this.pictureBox); this.Controls.Add(this.groupBoxTools); + this.Controls.Add(this.menuStrip1); + this.MainMenuStrip = this.menuStrip1; this.Name = "FormMapWithSetAirBombers"; this.Text = "Карта с набором объектов"; this.groupBoxTools.ResumeLayout(false); @@ -259,7 +311,10 @@ 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(); } @@ -282,5 +337,11 @@ private ListBox listBoxMaps; private TextBox textBoxNewMapName; private ComboBox comboBoxSelectorMap; + 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/AirBomber/AirBomber/FormMapWithSetAirBombers.cs b/AirBomber/AirBomber/FormMapWithSetAirBombers.cs index 64f7b32..e9d7648 100644 --- a/AirBomber/AirBomber/FormMapWithSetAirBombers.cs +++ b/AirBomber/AirBomber/FormMapWithSetAirBombers.cs @@ -178,5 +178,36 @@ namespace AirBomber 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/AirBomber/AirBomber/FormMapWithSetAirBombers.resx b/AirBomber/AirBomber/FormMapWithSetAirBombers.resx index f298a7b..988cd24 100644 --- a/AirBomber/AirBomber/FormMapWithSetAirBombers.resx +++ b/AirBomber/AirBomber/FormMapWithSetAirBombers.resx @@ -57,4 +57,13 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + 17, 17 + + + 152, 17 + + + 311, 17 + \ No newline at end of file diff --git a/AirBomber/AirBomber/IDrawingObject.cs b/AirBomber/AirBomber/IDrawingObject.cs index 6f8a68a..7bdaa10 100644 --- a/AirBomber/AirBomber/IDrawingObject.cs +++ b/AirBomber/AirBomber/IDrawingObject.cs @@ -39,5 +39,7 @@ namespace AirBomber /// /// (float Left, float Top, float Right, float Bottom) GetCurrentPosition(); + + string GetInfo(); } } diff --git a/AirBomber/AirBomber/MapWithSetAirBombersGeneric.cs b/AirBomber/AirBomber/MapWithSetAirBombersGeneric.cs index a9704db..5c2ba88 100644 --- a/AirBomber/AirBomber/MapWithSetAirBombersGeneric.cs +++ b/AirBomber/AirBomber/MapWithSetAirBombersGeneric.cs @@ -113,6 +113,31 @@ namespace AirBomber return new(_pictureWidth, _pictureHeight); } /// + /// Получение данных в виде строки + /// + /// + /// + public string GetData(char separatorType, char separatorData) + { + string data = $"{_map.GetType().Name}{separatorType}"; + foreach (var airBomber in _setAirBombers.GetAirBombers()) + { + data += $"{airBomber.GetInfo()}{separatorData}"; + } + return data; + } + /// + /// Загрузка списка из массива строк + /// + /// + public void LoadData(string[] records) + { + foreach (var rec in records) + { + _setAirBombers.Insert(DrawingObjectAirBomber.Create(rec) as T); + } + } + /// /// "Взбалтываем" набор, чтобы все элементы оказались в начале /// private void Shaking() diff --git a/AirBomber/AirBomber/MapsCollection.cs b/AirBomber/AirBomber/MapsCollection.cs index 5911fc4..b33ff80 100644 --- a/AirBomber/AirBomber/MapsCollection.cs +++ b/AirBomber/AirBomber/MapsCollection.cs @@ -11,7 +11,7 @@ namespace AirBomber /// /// Словарь (хранилище) с картами /// - readonly Dictionary> _mapStorages; + readonly Dictionary> _mapStorages; /// /// Возвращение списка названий карт /// @@ -25,13 +25,21 @@ namespace AirBomber /// 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 AirBomber MessageBox.Show("Карта с таким названием уже существует!"); return; } - _mapStorages.Add(name, new MapWithSetAirBombersGeneric(_pictureWidth, _pictureHeight, map)); + _mapStorages.Add(name, new MapWithSetAirBombersGeneric(_pictureWidth, _pictureHeight, map)); } /// /// Удаление карты @@ -65,7 +73,7 @@ namespace AirBomber /// /// /// - public MapWithSetAirBombersGeneric this[string ind] + public MapWithSetAirBombersGeneric this[string ind] { get { @@ -73,5 +81,71 @@ namespace AirBomber return null; } } + + /// + /// Сохранение информации по автомобилям в хранилище в файл + /// + /// Путь и имя файла + /// + public bool SaveData(string filename) + { + if (File.Exists(filename)) + { + File.Delete(filename); + } + using (FileStream fs = new(filename, FileMode.Create)) + using (StreamWriter sw = new StreamWriter(fs)) + { + sw.Write($"MapsCollection{Environment.NewLine}", sw); + foreach (var storage in _mapStorages) + { + sw.Write($"{storage.Key}{separatorDict}{storage.Value.GetData(separatorDict, separatorData)}{Environment.NewLine}", sw); + } + } + return true; + } + + /// + /// Загрузка нформации по автомобилям на парковках из файла + /// + /// + /// + public bool LoadData(string filename) + { + if (!File.Exists(filename)) + { + return false; + } + using (FileStream fs = new(filename, FileMode.Open)) + using (StreamReader sr = new StreamReader(fs)) + { + string line; + line = sr.ReadLine(); + line.Trim(); + if (line != "MapsCollection") return false; + _mapStorages.Clear(); + while ((line = sr.ReadLine()) != null) + { + line = line.Trim(); + var elem = line.Split(separatorDict); + AbstractMap map = null; + switch (elem[1]) + { + case "SimpleMap": + map = new SimpleMap(); + break; + case "CityMap": + map = new CityMap(); + break; + case "LineMap": + map = new LineMap(); + break; + } + _mapStorages.Add(elem[0], new MapWithSetAirBombersGeneric(_pictureWidth, _pictureHeight, map)); + _mapStorages[elem[0]].LoadData(elem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries)); + } + } + return true; + } } } -- 2.25.1