From 0f6942ad6e383b379cb06d97fdbcea4c5e897aa6 Mon Sep 17 00:00:00 2001 From: "A.Novopoltsev" Date: Sat, 17 Dec 2022 01:26:37 +0300 Subject: [PATCH] commit 06 full --- Warship/Warship/DrawingObjectWarship.cs | 3 + Warship/Warship/ExtentionWarship.cs | 39 +++++++++ .../FormMapWithSetWarships.Designer.cs | 86 +++++++++++++++++-- Warship/Warship/FormMapWithSetWarships.cs | 30 +++++++ Warship/Warship/IDrawingObject.cs | 1 + Warship/Warship/MapWithSetWarshipsGeneric.cs | 17 ++++ Warship/Warship/MapsCollection.cs | 63 ++++++++++++-- 7 files changed, 227 insertions(+), 12 deletions(-) create mode 100644 Warship/Warship/ExtentionWarship.cs diff --git a/Warship/Warship/DrawingObjectWarship.cs b/Warship/Warship/DrawingObjectWarship.cs index 65c2705..15fc564 100644 --- a/Warship/Warship/DrawingObjectWarship.cs +++ b/Warship/Warship/DrawingObjectWarship.cs @@ -35,5 +35,8 @@ namespace Warship { _warship.SetPosition(x, y, width, height); } + public string GetInfo() => _warship?.GetDataForSave(); + + public static IDrawingObject Create(string data) => new DrawingObjectWarship(data.CreateDrawingWarship()); } } diff --git a/Warship/Warship/ExtentionWarship.cs b/Warship/Warship/ExtentionWarship.cs new file mode 100644 index 0000000..b3ccd95 --- /dev/null +++ b/Warship/Warship/ExtentionWarship.cs @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Warship +{ + internal static class ExtentionCar + { + private static readonly char _separatorForObject = ':'; + + public static DrawingWarship CreateDrawingWarship(this string info) + { + string[] strs = info.Split(_separatorForObject); + + if (strs.Length == 3) + { + return new DrawingWarship(Convert.ToInt32(strs[0]), Convert.ToInt32(strs[1]), Color.FromName(strs[2])); + } + if (strs.Length == 7) + { + return new DrawingAdvancedWarship(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 DrawingWarship drawingWarship) + { + var warship = drawingWarship.Warship; + var str = $"{warship.Speed}{_separatorForObject}{warship.Weight}{_separatorForObject}{warship.BodyColor.Name}"; + if (warship is not EntityAdvancedWarship advancedWarship) + { + return str; + } + return $"{str}{_separatorForObject}{advancedWarship.DopColor.Name}{_separatorForObject}{advancedWarship.Helipad}{_separatorForObject}{advancedWarship.Antenna}{_separatorForObject}{advancedWarship.Missile}"; + } + } +} diff --git a/Warship/Warship/FormMapWithSetWarships.Designer.cs b/Warship/Warship/FormMapWithSetWarships.Designer.cs index cbab596..c66689a 100644 --- a/Warship/Warship/FormMapWithSetWarships.Designer.cs +++ b/Warship/Warship/FormMapWithSetWarships.Designer.cs @@ -41,11 +41,18 @@ this.buttonShowStorage = new System.Windows.Forms.Button(); this.maskedTextBoxPosition = new System.Windows.Forms.MaskedTextBox(); this.pictureBox1 = 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.pictureBox = new System.Windows.Forms.PictureBox(); this.groupBoxTools.SuspendLayout(); this.groupBoxMaps.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit(); + this.MenuStrip.SuspendLayout(); this.SuspendLayout(); // // groupBoxTools @@ -58,9 +65,9 @@ this.groupBoxTools.Controls.Add(this.maskedTextBoxPosition); this.groupBoxTools.Controls.Add(this.pictureBox1); this.groupBoxTools.Dock = System.Windows.Forms.DockStyle.Right; - this.groupBoxTools.Location = new System.Drawing.Point(911, 0); + this.groupBoxTools.Location = new System.Drawing.Point(863, 0); this.groupBoxTools.Name = "groupBoxTools"; - this.groupBoxTools.Size = new System.Drawing.Size(200, 597); + this.groupBoxTools.Size = new System.Drawing.Size(200, 652); this.groupBoxTools.TabIndex = 0; this.groupBoxTools.TabStop = false; this.groupBoxTools.Text = "Инструменты"; @@ -189,7 +196,7 @@ this.pictureBox.Dock = System.Windows.Forms.DockStyle.Fill; this.pictureBox.Location = new System.Drawing.Point(0, 0); this.pictureBox.Name = "pictureBox"; - this.pictureBox.Size = new System.Drawing.Size(911, 597); + this.pictureBox.Size = new System.Drawing.Size(863, 652); this.pictureBox.TabIndex = 1; this.pictureBox.TabStop = false; // @@ -197,11 +204,11 @@ // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(1111, 597); + this.ClientSize = new System.Drawing.Size(1063, 652); this.Controls.Add(this.pictureBox); this.Controls.Add(this.groupBoxTools); this.Name = "FormMapWithSetWarships"; - this.Text = "FormMapWithSetWarships"; + this.Text = "Карта с набором объектов"; this.groupBoxTools.ResumeLayout(false); this.groupBoxTools.PerformLayout(); this.groupBoxMaps.ResumeLayout(false); @@ -209,6 +216,67 @@ ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit(); this.ResumeLayout(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(1063, 24); + this.MenuStrip.TabIndex = 2; + // + // файл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"; + // + // FormMapWithSetWarships + // + this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(1063, 652); + this.Controls.Add(this.pictureBox); + this.Controls.Add(this.groupBoxTools); + this.Controls.Add(this.MenuStrip); + this.MainMenuStrip = this.MenuStrip; + this.Name = "FormMapWithSetWarships"; + this.Text = "Карта с набором объектов"; + this.groupBoxTools.ResumeLayout(false); + this.groupBoxTools.PerformLayout(); + 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(); } @@ -227,6 +295,12 @@ private TextBox textBoxMap; private Button buttonAddMap; private ListBox listBoxMaps; - private Button buttonDeleteMap; + private Button buttonDeleteMap; + 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/Warship/Warship/FormMapWithSetWarships.cs b/Warship/Warship/FormMapWithSetWarships.cs index 996337f..bc25ad9 100644 --- a/Warship/Warship/FormMapWithSetWarships.cs +++ b/Warship/Warship/FormMapWithSetWarships.cs @@ -178,5 +178,35 @@ namespace Warship 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/Warship/Warship/IDrawingObject.cs b/Warship/Warship/IDrawingObject.cs index 3aa306a..00feb14 100644 --- a/Warship/Warship/IDrawingObject.cs +++ b/Warship/Warship/IDrawingObject.cs @@ -13,5 +13,6 @@ namespace Warship void MoveObject(Direction direction); void DrawingObject(Graphics g); (float Left, float Right, float Top, float Bottom) GetCurrentPosition(); + string GetInfo(); } } diff --git a/Warship/Warship/MapWithSetWarshipsGeneric.cs b/Warship/Warship/MapWithSetWarshipsGeneric.cs index 5dc0551..029fdc6 100644 --- a/Warship/Warship/MapWithSetWarshipsGeneric.cs +++ b/Warship/Warship/MapWithSetWarshipsGeneric.cs @@ -64,6 +64,23 @@ namespace Warship } return new(_pictureWidth, _pictureHeight); } + public string GetData(char separatorType, char separatorData) + { + string data = $"{_map.GetType().Name}{separatorType}"; + foreach (var warship in _setWarship.GetWarships()) + { + data += $"{warship.GetInfo()}{separatorData}"; + } + return data; + } + + public void LoadData(string[] records) + { + foreach (var rec in records) + { + _setWarship.Insert(DrawingObjectWarship.Create(rec) as T); + } + } public void Shaking() { diff --git a/Warship/Warship/MapsCollection.cs b/Warship/Warship/MapsCollection.cs index 7eae226..1016e71 100644 --- a/Warship/Warship/MapsCollection.cs +++ b/Warship/Warship/MapsCollection.cs @@ -8,20 +8,73 @@ namespace Warship { 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; } + 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; + } + 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 "SimpleMap": + map = new SimpleMap(); + break; + case "SecondMap": + map = new SecondMap(); + break; + } + _mapStorages.Add(elem[0], new MapWithSetWarshipsGeneric(_pictureWidth, _pictureHeight, map)); + _mapStorages[elem[0]].LoadData(elem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries)); + } + } + return true; + } + public void AddMap(string name, AbstractMap map) { if (_mapStorages.ContainsKey(name)) @@ -31,18 +84,16 @@ namespace Warship } else { - _mapStorages.Add(name, new MapWithSetWarshipsGeneric(_pictureWidth, _pictureHeight, map)); + _mapStorages.Add(name, new MapWithSetWarshipsGeneric(_pictureWidth, _pictureHeight, map)); } } public void DelMap(string name) { - _mapStorages.Remove(name); - } - public MapWithSetWarshipsGeneric this[string ind] + public MapWithSetWarshipsGeneric this[string ind] { get { -- 2.25.1