diff --git a/AntiAircraftGun/AntiAircraftGun/DrawningObjects/ExtentionDrawningAircraftGun.cs b/AntiAircraftGun/AntiAircraftGun/DrawningObjects/ExtentionDrawningAircraftGun.cs new file mode 100644 index 0000000..814aa89 --- /dev/null +++ b/AntiAircraftGun/AntiAircraftGun/DrawningObjects/ExtentionDrawningAircraftGun.cs @@ -0,0 +1,43 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using AntiAircraftGun.Entities; + +namespace AntiAircraftGun.DrawningObjects +{ + public static class ExtentionDrawningAircraftGun + { + // Создание объекта из строки + public static DrawningAircraftGun? CreateDrawningAircraftGun(this string info, char separatorForObject, int width, int height) + { + string[] strs = info.Split(separatorForObject); + if (strs.Length == 3) + { + return new DrawningAircraftGun(Convert.ToInt32(strs[0]), Convert.ToInt32(strs[1]), Color.FromName(strs[2]), width, height); + } + if (strs.Length == 7) + { + return new DrawingAntiAircraftGun(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]), width, height); + } + return null; + } + + // Получение данных для сохранения в файл + public static string GetDataForSave(this DrawningAircraftGun drawningAircraftGun, char separatorForObject) + { + var aircraftgun = drawningAircraftGun.EntityAircraftGun; + if (aircraftgun == null) + { + return string.Empty; + } + var str = $"{aircraftgun.Speed}{separatorForObject}{aircraftgun.Weight}{separatorForObject}{aircraftgun.BodyColor.Name}"; + if (aircraftgun is not EntityAntiAircraftGun antiAircraftGun) + { + return str; + } + return $"{str}{separatorForObject}{antiAircraftGun.AdditionalColor.Name}{separatorForObject}{antiAircraftGun.Radar}{separatorForObject}{antiAircraftGun.Luke}{separatorForObject}{antiAircraftGun.ZenitkaGun}"; + } + } +} diff --git a/AntiAircraftGun/AntiAircraftGun/FormAircraftGunCollection.Designer.cs b/AntiAircraftGun/AntiAircraftGun/FormAircraftGunCollection.Designer.cs index 2db2e84..8987274 100644 --- a/AntiAircraftGun/AntiAircraftGun/FormAircraftGunCollection.Designer.cs +++ b/AntiAircraftGun/AntiAircraftGun/FormAircraftGunCollection.Designer.cs @@ -30,25 +30,32 @@ { this.pictureBoxCollection = new System.Windows.Forms.PictureBox(); this.groupBox1 = new System.Windows.Forms.GroupBox(); - this.maskedTextBoxNumber = new System.Windows.Forms.MaskedTextBox(); + this.groupBox2 = new System.Windows.Forms.GroupBox(); this.listBoxStorages = new System.Windows.Forms.ListBox(); this.textBoxStorageName = new System.Windows.Forms.TextBox(); this.ButtonAddObject = new System.Windows.Forms.Button(); this.ButtonDelObject = new System.Windows.Forms.Button(); + this.maskedTextBoxNumber = new System.Windows.Forms.MaskedTextBox(); this.ButtonAddAircraftGun = new System.Windows.Forms.Button(); this.ButtonRemoveAircraftGun = new System.Windows.Forms.Button(); this.ButtonRefreshCollection = new System.Windows.Forms.Button(); - this.groupBox2 = new System.Windows.Forms.GroupBox(); + 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(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).BeginInit(); this.groupBox1.SuspendLayout(); this.groupBox2.SuspendLayout(); + this.menuStrip.SuspendLayout(); this.SuspendLayout(); // // pictureBoxCollection // - this.pictureBoxCollection.Location = new System.Drawing.Point(7, 6); + this.pictureBoxCollection.Location = new System.Drawing.Point(12, 89); this.pictureBoxCollection.Name = "pictureBoxCollection"; - this.pictureBoxCollection.Size = new System.Drawing.Size(634, 439); + this.pictureBoxCollection.Size = new System.Drawing.Size(634, 354); this.pictureBoxCollection.TabIndex = 0; this.pictureBoxCollection.TabStop = false; // @@ -66,12 +73,18 @@ this.groupBox1.TabStop = false; this.groupBox1.Text = "Инструменты"; // - // maskedTextBoxNumber + // groupBox2 // - this.maskedTextBoxNumber.Location = new System.Drawing.Point(49, 318); - this.maskedTextBoxNumber.Name = "maskedTextBoxNumber"; - this.maskedTextBoxNumber.Size = new System.Drawing.Size(156, 23); - this.maskedTextBoxNumber.TabIndex = 5; + this.groupBox2.Controls.Add(this.listBoxStorages); + this.groupBox2.Controls.Add(this.textBoxStorageName); + this.groupBox2.Controls.Add(this.ButtonAddObject); + this.groupBox2.Controls.Add(this.ButtonDelObject); + this.groupBox2.Location = new System.Drawing.Point(19, 28); + this.groupBox2.Name = "groupBox2"; + this.groupBox2.Size = new System.Drawing.Size(209, 222); + this.groupBox2.TabIndex = 6; + this.groupBox2.TabStop = false; + this.groupBox2.Text = "Наборы"; // // listBoxStorages // @@ -110,6 +123,13 @@ this.ButtonDelObject.UseVisualStyleBackColor = true; this.ButtonDelObject.Click += new System.EventHandler(this.ButtonDelObject_Click); // + // maskedTextBoxNumber + // + this.maskedTextBoxNumber.Location = new System.Drawing.Point(49, 318); + this.maskedTextBoxNumber.Name = "maskedTextBoxNumber"; + this.maskedTextBoxNumber.Size = new System.Drawing.Size(156, 23); + this.maskedTextBoxNumber.TabIndex = 5; + // // ButtonAddAircraftGun // this.ButtonAddAircraftGun.Location = new System.Drawing.Point(49, 285); @@ -140,18 +160,47 @@ this.ButtonRefreshCollection.UseVisualStyleBackColor = true; this.ButtonRefreshCollection.Click += new System.EventHandler(this.ButtonRefreshCollection_Click); // - // groupBox2 + // menuStrip // - this.groupBox2.Controls.Add(this.listBoxStorages); - this.groupBox2.Controls.Add(this.textBoxStorageName); - this.groupBox2.Controls.Add(this.ButtonAddObject); - this.groupBox2.Controls.Add(this.ButtonDelObject); - this.groupBox2.Location = new System.Drawing.Point(19, 28); - this.groupBox2.Name = "groupBox2"; - this.groupBox2.Size = new System.Drawing.Size(209, 222); - this.groupBox2.TabIndex = 6; - this.groupBox2.TabStop = false; - this.groupBox2.Text = "Наборы"; + 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(906, 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"; // // FormAircraftGunCollection // @@ -160,6 +209,8 @@ this.ClientSize = new System.Drawing.Size(906, 450); this.Controls.Add(this.groupBox1); this.Controls.Add(this.pictureBoxCollection); + this.Controls.Add(this.menuStrip); + this.MainMenuStrip = this.menuStrip; this.Name = "FormAircraftGunCollection"; this.Text = "Набор зениток"; ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).EndInit(); @@ -167,7 +218,10 @@ this.groupBox1.PerformLayout(); this.groupBox2.ResumeLayout(false); this.groupBox2.PerformLayout(); + this.menuStrip.ResumeLayout(false); + this.menuStrip.PerformLayout(); this.ResumeLayout(false); + this.PerformLayout(); } @@ -184,5 +238,11 @@ private ListBox listBoxStorages; private MaskedTextBox maskedTextBoxNumber; private GroupBox groupBox2; + 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/AntiAircraftGun/AntiAircraftGun/FormAircraftGunCollection.cs b/AntiAircraftGun/AntiAircraftGun/FormAircraftGunCollection.cs index 17a0694..dbd8cc8 100644 --- a/AntiAircraftGun/AntiAircraftGun/FormAircraftGunCollection.cs +++ b/AntiAircraftGun/AntiAircraftGun/FormAircraftGunCollection.cs @@ -156,5 +156,38 @@ namespace AntiAircraftGun { pictureBoxCollection.Image = _storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowAntiAircraftGuns(); } + + // Обработка нажатия "Сохранение" + 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)) + { + ReloadObjects(); + MessageBox.Show("Загрузка прошла успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); + } + else + { + MessageBox.Show("Не загрузилось", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } } } diff --git a/AntiAircraftGun/AntiAircraftGun/FormAircraftGunCollection.resx b/AntiAircraftGun/AntiAircraftGun/FormAircraftGunCollection.resx index f298a7b..afb1440 100644 --- a/AntiAircraftGun/AntiAircraftGun/FormAircraftGunCollection.resx +++ b/AntiAircraftGun/AntiAircraftGun/FormAircraftGunCollection.resx @@ -57,4 +57,13 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + 17, 17 + + + 132, 18 + + + 272, 18 + \ No newline at end of file diff --git a/AntiAircraftGun/AntiAircraftGun/Generics/AntiAircraftGunsGenericCollection.cs b/AntiAircraftGun/AntiAircraftGun/Generics/AntiAircraftGunsGenericCollection.cs index e09ccae..564e6f4 100644 --- a/AntiAircraftGun/AntiAircraftGun/Generics/AntiAircraftGunsGenericCollection.cs +++ b/AntiAircraftGun/AntiAircraftGun/Generics/AntiAircraftGunsGenericCollection.cs @@ -101,5 +101,8 @@ namespace AntiAircraftGun.Generics i++; } } + + // Получение объектов коллекции + public IEnumerable GetAntiAircraftGuns => _collection.GetAntiAircraftGuns(); } } diff --git a/AntiAircraftGun/AntiAircraftGun/Generics/AntiAircraftGunsGenericStorage.cs b/AntiAircraftGun/AntiAircraftGun/Generics/AntiAircraftGunsGenericStorage.cs index 074664c..54318a6 100644 --- a/AntiAircraftGun/AntiAircraftGun/Generics/AntiAircraftGunsGenericStorage.cs +++ b/AntiAircraftGun/AntiAircraftGun/Generics/AntiAircraftGunsGenericStorage.cs @@ -19,6 +19,12 @@ namespace AntiAircraftGun.Generics private readonly int _pictureWidth; // Высота окна отрисовки private readonly int _pictureHeight; + // Разделитель для записи ключа и значения элемента словаря + private static readonly char _separatorForKeyValue = '|'; + // Разделитель для записей коллекции данных в файл + private readonly char _separatorRecords = ';'; + // Разделитель для записи информации по объекту в файл + private static readonly char _separatorForObject = ':'; // Конструктор public AntiAircraftGunsGenericStorage(int pictureWidth, int pictureHeight) @@ -60,5 +66,81 @@ namespace AntiAircraftGun.Generics return null; } } + + // Сохранение информации по зениткам в хранилище в файл + public bool SaveData(string filename) + { + if (File.Exists(filename)) + { + File.Delete(filename); + } + StringBuilder data = new(); + foreach (KeyValuePair> record in _aircraftgunStorages) + { + StringBuilder records = new(); + foreach (DrawningAircraftGun? elem in record.Value.GetAntiAircraftGuns) + { + records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}"); + } + data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}"); + } + if (data.Length == 0) + { + return false; + } + + using StreamWriter fs = new StreamWriter(filename); + string info = $"AircraftStorage{Environment.NewLine}{data}"; + fs.Write(info, 0, info.Length); + return true; + } + + // Загрузка информации по зениткам в хранилище из файла + public bool LoadData(string filename) + { + if (!File.Exists(filename)) + { + return false; + } + + using (StreamReader fs = new StreamReader(filename)) + { + string str = fs.ReadLine(); + if (!str.StartsWith("AircraftStorage")) + { + return false; + } + _aircraftgunStorages.Clear(); + while ((str = fs.ReadLine()) != null) + { + if (str.Length == 0) + { + return false; + } + + string[] record = str.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries); + if (record.Length != 2) + { + continue; + } + + AntiAircraftGunsGenericCollection collection = new(_pictureWidth, _pictureHeight); + string[] set = record[1].Split(_separatorRecords, StringSplitOptions.RemoveEmptyEntries); + foreach (string elem in set) + { + DrawningAircraftGun? aircraftgun = elem?.CreateDrawningAircraftGun(_separatorForObject, _pictureWidth, _pictureHeight); + if (aircraftgun != null) + { + if (!(collection + aircraftgun)) + { + return false; + } + } + } + _aircraftgunStorages.Add(record[0], collection); + } + return true; + } + } } }