From f52af0e723c82caadc92cd38eecd94eae920b02d Mon Sep 17 00:00:00 2001 From: Efi Date: Tue, 26 Dec 2023 16:16:22 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9B=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D0=BD=D0=B0=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=B0=206?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AirBomber/ExtentionDrawingWarAirplane.cs | 46 ++++++++++ .../FormWarAirplaneCollection.Designer.cs | 66 +++++++++++++- .../AirBomber/FormWarAirplaneCollection.cs | 38 ++++++++ .../AirBomber/FormWarAirplaneCollection.resx | 9 ++ .../AirBomber/WarAirplaneGenericCollection.cs | 1 + .../AirBomber/WarAirplaneGenericStorage.cs | 91 ++++++++++++++++++- 6 files changed, 241 insertions(+), 10 deletions(-) create mode 100644 AirBomber/AirBomber/ExtentionDrawingWarAirplane.cs diff --git a/AirBomber/AirBomber/ExtentionDrawingWarAirplane.cs b/AirBomber/AirBomber/ExtentionDrawingWarAirplane.cs new file mode 100644 index 0000000..1991507 --- /dev/null +++ b/AirBomber/AirBomber/ExtentionDrawingWarAirplane.cs @@ -0,0 +1,46 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using AirBomber.Entities; + +namespace AirBomber.DrawingObjects +{ + public static class ExtentionDrawingWarAirplane + { + public static DrawingWarAirplane? CreateDrawingWarAirplane(this string info, char separatorForObject, + int width, int height) + { + string[] strs = info.Split(separatorForObject); + if(strs.Length == 3) + { + return new DrawingWarAirplane(Convert.ToInt32(strs[0]), Convert.ToInt32(strs[1]), + Color.FromName(strs[2]), width, height); + } + if(strs.Length == 6) + { + return new DrawingAirBomber(Convert.ToInt32(strs[0]), Convert.ToInt32(strs[1]), + Color.FromName(strs[2]), Color.FromName(strs[3]), Convert.ToBoolean(strs[4]), + Convert.ToBoolean(strs[5]), width, height); + } + return null; + } + public static string GetDataForSave(this DrawingWarAirplane drawingWarAirplane, char separatorForObject) + { + var warAirplane = drawingWarAirplane.EntityWarAirplane; + if(warAirplane == null) + { + return string.Empty; + } + var str = $"{warAirplane.Speed}{separatorForObject}{warAirplane.Weight}" + + $"{separatorForObject}{warAirplane.BodyColor.Name}"; + if(warAirplane is not EntityAirBomber airBomber) + { + return str; + } + return $"{str}{separatorForObject}{airBomber.AdditionalColor.Name}{separatorForObject}" + + $"{airBomber.FuelTank}{separatorForObject}{airBomber.Bombs}"; + } + } +} diff --git a/AirBomber/AirBomber/FormWarAirplaneCollection.Designer.cs b/AirBomber/AirBomber/FormWarAirplaneCollection.Designer.cs index 0ffa662..3870f89 100644 --- a/AirBomber/AirBomber/FormWarAirplaneCollection.Designer.cs +++ b/AirBomber/AirBomber/FormWarAirplaneCollection.Designer.cs @@ -40,9 +40,16 @@ buttonAdd = new Button(); maskedTextBoxNumber = new MaskedTextBox(); pictureBoxCollection = new PictureBox(); + menuStrip = new MenuStrip(); + файлToolStripMenuItem = new ToolStripMenuItem(); + SaveToolStripMenuItem = new ToolStripMenuItem(); + LoadToolStripMenuItem = new ToolStripMenuItem(); + openFileDialog = new OpenFileDialog(); + saveFileDialog = new SaveFileDialog(); panelCollection.SuspendLayout(); panelObject.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); + menuStrip.SuspendLayout(); SuspendLayout(); // // panelCollection @@ -54,9 +61,9 @@ panelCollection.Controls.Add(buttonAdd); panelCollection.Controls.Add(maskedTextBoxNumber); panelCollection.Dock = DockStyle.Right; - panelCollection.Location = new Point(617, 0); + panelCollection.Location = new Point(617, 24); panelCollection.Name = "panelCollection"; - panelCollection.Size = new Size(183, 450); + panelCollection.Size = new Size(183, 456); panelCollection.TabIndex = 0; // // panelObject @@ -156,19 +163,59 @@ // pictureBoxCollection // pictureBoxCollection.Dock = DockStyle.Left; - pictureBoxCollection.Location = new Point(0, 0); + pictureBoxCollection.Location = new Point(0, 24); pictureBoxCollection.Name = "pictureBoxCollection"; - pictureBoxCollection.Size = new Size(617, 450); + pictureBoxCollection.Size = new Size(617, 456); pictureBoxCollection.TabIndex = 1; pictureBoxCollection.TabStop = false; // + // menuStrip + // + menuStrip.Items.AddRange(new ToolStripItem[] { файлToolStripMenuItem }); + menuStrip.Location = new Point(0, 0); + menuStrip.Name = "menuStrip"; + menuStrip.Size = new Size(800, 24); + menuStrip.TabIndex = 2; + menuStrip.Text = "menuStrip1"; + // + // файлToolStripMenuItem + // + файлToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { SaveToolStripMenuItem, LoadToolStripMenuItem }); + файлToolStripMenuItem.Name = "файлToolStripMenuItem"; + файлToolStripMenuItem.Size = new Size(48, 20); + файлToolStripMenuItem.Text = "Файл"; + // + // SaveToolStripMenuItem + // + SaveToolStripMenuItem.Name = "SaveToolStripMenuItem"; + SaveToolStripMenuItem.Size = new Size(133, 22); + SaveToolStripMenuItem.Text = "Сохранить"; + SaveToolStripMenuItem.Click += SaveToolStripMenuItem_Click; + // + // LoadToolStripMenuItem + // + LoadToolStripMenuItem.Name = "LoadToolStripMenuItem"; + LoadToolStripMenuItem.Size = new Size(133, 22); + LoadToolStripMenuItem.Text = "Загрузить"; + LoadToolStripMenuItem.Click += LoadToolStripMenuItem_Click; + // + // openFileDialog + // + openFileDialog.Filter = "txt file | *.txt"; + // + // saveFileDialog + // + saveFileDialog.Filter = "txt file | *.txt"; + // // FormWarAirplaneCollection // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(800, 450); + ClientSize = new Size(800, 480); Controls.Add(pictureBoxCollection); Controls.Add(panelCollection); + Controls.Add(menuStrip); + MainMenuStrip = menuStrip; Name = "FormWarAirplaneCollection"; Text = "Набор военных самолётов"; panelCollection.ResumeLayout(false); @@ -176,7 +223,10 @@ panelObject.ResumeLayout(false); panelObject.PerformLayout(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit(); + menuStrip.ResumeLayout(false); + menuStrip.PerformLayout(); ResumeLayout(false); + PerformLayout(); } #endregion @@ -193,5 +243,11 @@ private Button buttonDelObject; private Panel panelObject; private TextBox textBoxStorageName; + 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/FormWarAirplaneCollection.cs b/AirBomber/AirBomber/FormWarAirplaneCollection.cs index 9cf3757..1d234e9 100644 --- a/AirBomber/AirBomber/FormWarAirplaneCollection.cs +++ b/AirBomber/AirBomber/FormWarAirplaneCollection.cs @@ -157,6 +157,44 @@ namespace AirBomber } pictureBoxCollection.Image = obj.ShowWarAirplane(); } + private void SaveToolStripMenuItem_Click(object sender, EventArgs e) + { + if (saveFileDialog.ShowDialog() == DialogResult.OK) + { + if (_storage.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 (_storage.LoadData(openFileDialog.FileName)) + { + MessageBox.Show("Загрузка прошла успешно", + "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); + foreach (var collection in _storage.Keys) + { + listBoxStorages.Items.Add(collection); + } + } + else + { + MessageBox.Show("Не загрузилось", "Результат", + MessageBoxButtons.OK, MessageBoxIcon.Error); + } + + } + } } } diff --git a/AirBomber/AirBomber/FormWarAirplaneCollection.resx b/AirBomber/AirBomber/FormWarAirplaneCollection.resx index af32865..f53194e 100644 --- a/AirBomber/AirBomber/FormWarAirplaneCollection.resx +++ b/AirBomber/AirBomber/FormWarAirplaneCollection.resx @@ -117,4 +117,13 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + 17, 17 + + + 132, 17 + + + 271, 17 + \ No newline at end of file diff --git a/AirBomber/AirBomber/WarAirplaneGenericCollection.cs b/AirBomber/AirBomber/WarAirplaneGenericCollection.cs index 56d034e..4e1df06 100644 --- a/AirBomber/AirBomber/WarAirplaneGenericCollection.cs +++ b/AirBomber/AirBomber/WarAirplaneGenericCollection.cs @@ -18,6 +18,7 @@ namespace AirBomber.Generics private readonly int _placeSizeWidth = 110; private readonly int _placeSizeHeight = 110; private readonly SetGeneric _collection; + public IEnumerable GetWarAirplanes => _collection.GetWarAirplane(); public WarAirplaneGenericCollection(int picWidth, int picHeight) { int width = picWidth / _placeSizeWidth; diff --git a/AirBomber/AirBomber/WarAirplaneGenericStorage.cs b/AirBomber/AirBomber/WarAirplaneGenericStorage.cs index 12c660c..bf74837 100644 --- a/AirBomber/AirBomber/WarAirplaneGenericStorage.cs +++ b/AirBomber/AirBomber/WarAirplaneGenericStorage.cs @@ -15,6 +15,9 @@ namespace AirBomber.Generics public List Keys => _warAirplaneStorages.Keys.ToList(); private readonly int _pictureWidth; private readonly int _pictureHeight; + private static readonly char _separatorForKeyValue = '|'; + private readonly char _separatorRecords = ';'; + private static readonly char _separatorForObject = ':'; public WarAirplaneGenericStorage(int pictureWidth, int pictureHeight) { _warAirplaneStorages = new Dictionary>(); @@ -39,11 +42,6 @@ namespace AirBomber.Generics _warAirplaneStorages.Remove(name); } - /// - /// Доступ к набору - /// - /// - /// public WarAirplaneGenericCollection? this[string ind] { get @@ -55,5 +53,88 @@ namespace AirBomber.Generics return null; } } + + public bool SaveData(string filename) + { + if (File.Exists(filename)) + { + File.Delete(filename); + } + StringBuilder data = new(); + foreach(KeyValuePair> record in _warAirplaneStorages) + { + StringBuilder records = new(); + foreach(DrawingWarAirplane? elem in record.Value.GetWarAirplanes) + { + records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}"); + } + data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}"); + } + if(data.Length == 0) + { + return false; + } + using FileStream fs = new(filename, FileMode.Create); + byte[] info = new UTF8Encoding(true).GetBytes($"WarAirplaneStorage" + + $"{Environment.NewLine}{data}"); + fs.Write(info, 0, info.Length); + return true; + } + public bool LoadData(string filename) + { + if (!File.Exists(filename)) + { + return false; + } + string bufferTextFromFile = ""; + using (FileStream fs = new(filename, FileMode.Open)) + { + byte[] b = new byte[fs.Length]; + UTF8Encoding temp = new(true); + while (fs.Read(b,0,b.Length) > 0) + { + bufferTextFromFile += temp.GetString(b); + } + } + var strs = bufferTextFromFile.Split(new char[] { '\n', '\r' }, + StringSplitOptions.RemoveEmptyEntries); + if (strs == null || strs.Length == 0) + { + return false; + } + if (!strs[0].StartsWith("WarAirplaneStorage")) + { + return false; + } + _warAirplaneStorages.Clear(); + foreach (string data in strs) + { + string[] record = data.Split(_separatorForKeyValue, + StringSplitOptions.RemoveEmptyEntries); + if (record.Length != 2) + { + continue; + } + WarAirplaneGenericCollection + collection = new(_pictureWidth, _pictureHeight); + string[] set = record[1].Split(_separatorRecords, StringSplitOptions.RemoveEmptyEntries); + foreach (string elem in set) + { + DrawingWarAirplane? warAirplane = elem?.CreateDrawingWarAirplane(_separatorForObject, _pictureWidth, _pictureHeight); + if(warAirplane != null) + { + if(!(collection + warAirplane)) + { + return false; + } + } + } + _warAirplaneStorages.Add(record[0], collection); + } + return true; + } + + } }