From ec836fb5268a00eee2d2869269e35b3ca8794d90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=90=D0=BB=D0=B5=D0=B9?= =?UTF-8?q?=D0=BA=D0=B8=D0=BD?= Date: Sun, 13 Nov 2022 18:35:50 +0400 Subject: [PATCH] =?UTF-8?q?LabWork06=20=D1=81=D0=B4=D0=B5=D0=BB=D0=B0?= =?UTF-8?q?=D0=BD=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AirBomber/AirBomber/AirBomber.csproj | 8 ++ AirBomber/AirBomber/DrawningObjectBomber.cs | 4 + AirBomber/AirBomber/EntityAirBomber.cs | 1 + AirBomber/AirBomber/ExtentionAirBomber.cs | 40 +++++++++ .../FormMapWithSetAirBomber.Designer.cs | 76 +++++++++++++++-- .../AirBomber/FormMapWithSetAirBomber.cs | 33 ++++++++ .../AirBomber/FormMapWithSetAirBomber.resx | 9 +++ AirBomber/AirBomber/IDrawningObject.cs | 2 + .../AirBomber/MapWithSetAirBomberGeneric.cs | 18 +++++ AirBomber/AirBomber/MapsCollection.cs | 81 +++++++++++++++++-- 10 files changed, 259 insertions(+), 13 deletions(-) create mode 100644 AirBomber/AirBomber/ExtentionAirBomber.cs diff --git a/AirBomber/AirBomber/AirBomber.csproj b/AirBomber/AirBomber/AirBomber.csproj index 13ee123..ad1bb7a 100644 --- a/AirBomber/AirBomber/AirBomber.csproj +++ b/AirBomber/AirBomber/AirBomber.csproj @@ -8,6 +8,14 @@ enable + + preview + + + + preview + + True diff --git a/AirBomber/AirBomber/DrawningObjectBomber.cs b/AirBomber/AirBomber/DrawningObjectBomber.cs index 532fdf2..b49ee71 100644 --- a/AirBomber/AirBomber/DrawningObjectBomber.cs +++ b/AirBomber/AirBomber/DrawningObjectBomber.cs @@ -36,5 +36,9 @@ namespace AirBomber { _airBomber.DrawTransport(g); } + + public string GetInfo() => _airBomber?.GetDataForSave(); + + public static IDrawningObject Create(string data) => new DrawningObjectBomber(data.CreateDrawningAirBomber()); } } diff --git a/AirBomber/AirBomber/EntityAirBomber.cs b/AirBomber/AirBomber/EntityAirBomber.cs index 0ff99f2..0d1f06d 100644 --- a/AirBomber/AirBomber/EntityAirBomber.cs +++ b/AirBomber/AirBomber/EntityAirBomber.cs @@ -20,5 +20,6 @@ namespace AirBomber Weight = weight <= 0 ? rnd.Next(40, 70) : weight; BodyColor = bodyColor; } + } } diff --git a/AirBomber/AirBomber/ExtentionAirBomber.cs b/AirBomber/AirBomber/ExtentionAirBomber.cs new file mode 100644 index 0000000..84a18a4 --- /dev/null +++ b/AirBomber/AirBomber/ExtentionAirBomber.cs @@ -0,0 +1,40 @@ +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 DrawningBomber CreateDrawningAirBomber(this string info) + { + string[] strs = info.Split(_separatorForObject); + if (strs.Length == 3) + { + return new DrawningBomber(Convert.ToInt32(strs[0]), Convert.ToInt32(strs[1]), Color.FromName(strs[2])); + } + if (strs.Length == 6) + { + return new DrawningWarplane(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 DrawningBomber drawningAirBomber) + { + var airBomber = drawningAirBomber.AirBomber; + var str = $"{airBomber.Speed}{_separatorForObject}{airBomber.Weight}{_separatorForObject}{airBomber.BodyColor.Name}"; + if (airBomber is not EntityWarplane warplane) + { + return str; + } + return $"{str}{_separatorForObject}{warplane.DopColor.Name}{_separatorForObject}{warplane.Engines}{_separatorForObject}{warplane.Weapons}"; + } + } +} diff --git a/AirBomber/AirBomber/FormMapWithSetAirBomber.Designer.cs b/AirBomber/AirBomber/FormMapWithSetAirBomber.Designer.cs index f6617f0..27df940 100644 --- a/AirBomber/AirBomber/FormMapWithSetAirBomber.Designer.cs +++ b/AirBomber/AirBomber/FormMapWithSetAirBomber.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.menuStrip = 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.groupBoxMaps.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit(); + this.menuStrip.SuspendLayout(); this.SuspendLayout(); // // groupBox1 @@ -63,9 +70,9 @@ this.groupBox1.Controls.Add(this.maskedTextBoxPosition); this.groupBox1.Controls.Add(this.buttonAddAirBomber); this.groupBox1.Dock = System.Windows.Forms.DockStyle.Right; - this.groupBox1.Location = new System.Drawing.Point(1034, 0); + this.groupBox1.Location = new System.Drawing.Point(1034, 24); this.groupBox1.Name = "groupBox1"; - this.groupBox1.Size = new System.Drawing.Size(200, 764); + this.groupBox1.Size = new System.Drawing.Size(200, 740); this.groupBox1.TabIndex = 0; this.groupBox1.TabStop = false; this.groupBox1.Text = "Инструменты"; @@ -138,7 +145,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(116, 718); + this.buttonRight.Location = new System.Drawing.Point(116, 694); this.buttonRight.Name = "buttonRight"; this.buttonRight.Size = new System.Drawing.Size(30, 30); this.buttonRight.TabIndex = 10; @@ -150,7 +157,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(80, 718); + this.buttonDown.Location = new System.Drawing.Point(80, 694); this.buttonDown.Name = "buttonDown"; this.buttonDown.Size = new System.Drawing.Size(30, 30); this.buttonDown.TabIndex = 9; @@ -162,7 +169,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(44, 718); + this.buttonLeft.Location = new System.Drawing.Point(44, 694); this.buttonLeft.Name = "buttonLeft"; this.buttonLeft.Size = new System.Drawing.Size(30, 30); this.buttonLeft.TabIndex = 8; @@ -174,7 +181,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(80, 682); + this.buttonUp.Location = new System.Drawing.Point(80, 658); this.buttonUp.Name = "buttonUp"; this.buttonUp.Size = new System.Drawing.Size(30, 30); this.buttonUp.TabIndex = 7; @@ -232,12 +239,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, 24); this.pictureBox.Name = "pictureBox"; - this.pictureBox.Size = new System.Drawing.Size(1034, 764); + this.pictureBox.Size = new System.Drawing.Size(1034, 740); this.pictureBox.TabIndex = 1; this.pictureBox.TabStop = false; // + // menuStrip + // + this.menuStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.файлToolStripMenuItem}); + this.menuStrip.Location = new System.Drawing.Point(0, 0); + this.menuStrip.Name = "menuStrip"; + this.menuStrip.Size = new System.Drawing.Size(1234, 24); + this.menuStrip.TabIndex = 2; + this.menuStrip.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.FileName = "openFileDialog1"; + this.openFileDialog.Filter = "txt file | *.txt"; + // + // saveFileDialog + // + this.saveFileDialog.Filter = "txt file | *.txt"; + // // FormMapWithSetAirBomber // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); @@ -245,6 +294,8 @@ this.ClientSize = new System.Drawing.Size(1234, 764); this.Controls.Add(this.pictureBox); this.Controls.Add(this.groupBox1); + this.Controls.Add(this.menuStrip); + this.MainMenuStrip = this.menuStrip; this.Name = "FormMapWithSetAirBomber"; this.Text = "Карта с набором объектов"; this.groupBox1.ResumeLayout(false); @@ -252,7 +303,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(); } @@ -275,5 +329,11 @@ private ListBox listBoxMaps; private Button buttonDeleteMap; private Button buttonAddMap; + private MenuStrip menuStrip; + 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/FormMapWithSetAirBomber.cs b/AirBomber/AirBomber/FormMapWithSetAirBomber.cs index 9549b8f..2aed3ef 100644 --- a/AirBomber/AirBomber/FormMapWithSetAirBomber.cs +++ b/AirBomber/AirBomber/FormMapWithSetAirBomber.cs @@ -182,5 +182,38 @@ namespace AirBomber } } + + 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/FormMapWithSetAirBomber.resx b/AirBomber/AirBomber/FormMapWithSetAirBomber.resx index f298a7b..934ed35 100644 --- a/AirBomber/AirBomber/FormMapWithSetAirBomber.resx +++ b/AirBomber/AirBomber/FormMapWithSetAirBomber.resx @@ -57,4 +57,13 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + 17, 17 + + + 125, 17 + + + 265, 17 + \ No newline at end of file diff --git a/AirBomber/AirBomber/IDrawningObject.cs b/AirBomber/AirBomber/IDrawningObject.cs index 22f6bc9..a32cb67 100644 --- a/AirBomber/AirBomber/IDrawningObject.cs +++ b/AirBomber/AirBomber/IDrawningObject.cs @@ -17,5 +17,7 @@ namespace AirBomber void DrawningObject(Graphics g); (float Left, float Right, float Top, float Bottom) GetCurrentPosition(); + + string GetInfo(); } } diff --git a/AirBomber/AirBomber/MapWithSetAirBomberGeneric.cs b/AirBomber/AirBomber/MapWithSetAirBomberGeneric.cs index 70295c7..8e9e2ef 100644 --- a/AirBomber/AirBomber/MapWithSetAirBomberGeneric.cs +++ b/AirBomber/AirBomber/MapWithSetAirBomberGeneric.cs @@ -65,6 +65,24 @@ namespace AirBomber return new(_pictureWidth, _pictureHeight); } + public string GetData(char separatorType, char separatorData) + { + string data = $"{_map.GetType().Name}{separatorType}"; + foreach (var airBomber in _setAirBomber.GetAirBomber()) + { + data += $"{airBomber.GetInfo()}{separatorData}"; + } + return data; + } + + public void LoadData(string[] records) + { + foreach (var rec in records) + { + _setAirBomber.Insert(DrawningObjectBomber.Create(rec) as T); + } + } + private void Shaking() { int j = _setAirBomber.Count - 1; diff --git a/AirBomber/AirBomber/MapsCollection.cs b/AirBomber/AirBomber/MapsCollection.cs index cb7ca87..f5868cd 100644 --- a/AirBomber/AirBomber/MapsCollection.cs +++ b/AirBomber/AirBomber/MapsCollection.cs @@ -1,4 +1,4 @@ -using System; + using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -8,14 +8,16 @@ namespace AirBomber { internal class MapsCollection { - readonly Dictionary> _mapStorages; + readonly Dictionary> _mapStorages; public List Keys => _mapStorages.Keys.ToList(); private readonly int _pictureWidth; 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; } @@ -24,7 +26,7 @@ namespace AirBomber { if (_mapStorages.ContainsKey(name)) return; - _mapStorages.Add(name, new MapWithSetAirBomberGeneric(_pictureWidth, _pictureHeight, map)); + _mapStorages.Add(name, new MapWithSetAirBomberGeneric(_pictureWidth, _pictureHeight, map)); } public void DelMap(string name) @@ -34,7 +36,7 @@ namespace AirBomber _mapStorages.Remove(name); } - public MapWithSetAirBomberGeneric this[string ind] + public MapWithSetAirBomberGeneric this[string ind] { get { @@ -42,5 +44,74 @@ namespace AirBomber return null; } } + + public bool SaveData(string filename) + { + if (File.Exists(filename)) + { + File.Delete(filename); + } + using (StreamWriter sw = new(filename)) + { + sw.Write($"MapsCollection{Environment.NewLine}"); + foreach (var storage in _mapStorages) + { + sw.Write($"{storage.Key}{separatorDict}{storage.Value.GetData(separatorDict, + separatorData)}{Environment.NewLine}"); + } + } + return true; + } + + public bool LoadData(string filename) + { + if (!File.Exists(filename)) + { + return false; + } + string bufferTextFromFile = ""; + using (StreamReader sr = new(filename)) + { + string str; + bool first = true; + while ((str = sr.ReadLine()) != null) + { + if (first) + { + if (!str.Contains("MapsCollection")) + { + return false; + } + else + { + _mapStorages.Clear(); + } + first = false; + } + else + { + var elem = str.Split(separatorDict); + AbstractMap map = null; + switch (elem[1]) + { + case "SimpleMap": + map = new SimpleMap(); + break; + case "SecondMap": + map = new SecondMap(); + break; + case "ThirdMap": + map = new ThirdMap(); + break; + } + _mapStorages.Add(elem[0], + new MapWithSetAirBomberGeneric(_pictureWidth, _pictureHeight, map)); + _mapStorages[elem[0]].LoadData(elem[2].Split(separatorData, + StringSplitOptions.RemoveEmptyEntries)); + } + } + } + return true; + } } } -- 2.25.1