diff --git a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/Extention.cs b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/Extention.cs new file mode 100644 index 0000000..148030c --- /dev/null +++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/Extention.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SelfPropelledArtilleryUnit +{ + internal class Extention + { + } +} diff --git a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/ExtentionDrawningSPAU.cs b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/ExtentionDrawningSPAU.cs new file mode 100644 index 0000000..03bf9c1 --- /dev/null +++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/ExtentionDrawningSPAU.cs @@ -0,0 +1,54 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using SelfPropelledArtilleryUnit.Entities; + +namespace SelfPropelledArtilleryUnit.DrawningObjects +{ + public static class ExtentionDrawningSPAU + { + /// + /// Создание объекта из строки + /// + /// Строка с данными для создания объекта + /// Разделитель даннных + /// Ширина + /// Высота + /// Объект + public static DrawningSPAU? CreateDrawningSPAU(this string info, char separatorForObject, int width, int height) + { + string[] strs = info.Split(separatorForObject); + if (strs.Length == 3) + { + return new DrawningSPAU(Convert.ToInt32(strs[0]), Convert.ToInt32(strs[1]), Color.FromName(strs[2]), width, height); + } + if (strs.Length == 6) + { + return new DrawningSPAUchild(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 DrawningSPAU drawningSPAU, char separatorForObject) + { + var sPAU = drawningSPAU.EntitySPAU; + if (sPAU == null) + { + return string.Empty; + } + var str = $"{sPAU.Speed}{separatorForObject}{sPAU.Weight}{separatorForObject}{sPAU.BodyColor.Name}"; + if (sPAU is not EntitySPAUchild sPAUchild) + { + return str; + } + return $"{str}{separatorForObject}{sPAUchild.AdditionalColor.Name}{separatorForObject}{sPAUchild.BodyKit}{separatorForObject}{sPAUchild.Ballon}"; + } + } +} diff --git a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/FormSPAUCollection.Designer.cs b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/FormSPAUCollection.Designer.cs index f2322bf..79a993d 100644 --- a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/FormSPAUCollection.Designer.cs +++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/FormSPAUCollection.Designer.cs @@ -39,14 +39,21 @@ ButtonDelObject = new Button(); ButtonAddObject = new Button(); textBoxStorageName = new TextBox(); + menuStrip = new MenuStrip(); + toolStripMenuItem1 = new ToolStripMenuItem(); + openFileDialog = new ToolStripMenuItem(); + saveFileDialog = new ToolStripMenuItem(); + openFileDialog_ = new OpenFileDialog(); + saveFileDialog_ = new OpenFileDialog(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); panel1.SuspendLayout(); panel2.SuspendLayout(); + menuStrip.SuspendLayout(); SuspendLayout(); // // pictureBoxCollection // - pictureBoxCollection.Location = new Point(12, 12); + pictureBoxCollection.Location = new Point(12, 40); pictureBoxCollection.Name = "pictureBoxCollection"; pictureBoxCollection.Size = new Size(680, 432); pictureBoxCollection.TabIndex = 0; @@ -95,7 +102,7 @@ panel1.Controls.Add(ButtonRemoveSPAU); panel1.Controls.Add(ButtonAddSPAU); panel1.Controls.Add(maskedTextBoxNumber); - panel1.Location = new Point(711, 217); + panel1.Location = new Point(711, 245); panel1.Name = "panel1"; panel1.Size = new Size(176, 227); panel1.TabIndex = 5; @@ -106,9 +113,9 @@ panel2.Controls.Add(ButtonDelObject); panel2.Controls.Add(ButtonAddObject); panel2.Controls.Add(textBoxStorageName); - panel2.Location = new Point(711, 12); + panel2.Location = new Point(711, 40); panel2.Name = "panel2"; - panel2.Size = new Size(174, 198); + panel2.Size = new Size(174, 199); panel2.TabIndex = 6; // // listBoxStorages @@ -148,14 +155,57 @@ textBoxStorageName.Size = new Size(143, 27); textBoxStorageName.TabIndex = 0; // + // menuStrip + // + menuStrip.ImageScalingSize = new Size(20, 20); + menuStrip.Items.AddRange(new ToolStripItem[] { toolStripMenuItem1 }); + menuStrip.Location = new Point(0, 0); + menuStrip.Name = "menuStrip"; + menuStrip.Size = new Size(899, 28); + menuStrip.TabIndex = 7; + menuStrip.Text = "menuStrip1"; + // + // toolStripMenuItem1 + // + toolStripMenuItem1.DropDownItems.AddRange(new ToolStripItem[] { openFileDialog, saveFileDialog }); + toolStripMenuItem1.Name = "toolStripMenuItem1"; + toolStripMenuItem1.Size = new Size(59, 24); + toolStripMenuItem1.Text = "Файл"; + // + // openFileDialog + // + openFileDialog.Name = "openFileDialog"; + openFileDialog.Size = new Size(224, 26); + openFileDialog.Text = "Открыть файл"; + openFileDialog.Click += LoadToolStripMenuItem_Click; + // + // saveFileDialog + // + saveFileDialog.Name = "saveFileDialog"; + saveFileDialog.Size = new Size(224, 26); + saveFileDialog.Text = "Сохранить файл"; + saveFileDialog.Click += SaveToolStripMenuItem_Click; + // + // openFileDialog_ + // + openFileDialog_.FileName = "openFileDialog"; + openFileDialog_.Filter = "txt file | *.txt"; + // + // saveFileDialog_ + // + saveFileDialog_.FileName = "saveFileDialog"; + saveFileDialog_.Filter = "txt file | *.txt"; + // // FormSPAUCollection // AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(899, 456); + ClientSize = new Size(899, 484); Controls.Add(panel2); Controls.Add(panel1); Controls.Add(pictureBoxCollection); + Controls.Add(menuStrip); + MainMenuStrip = menuStrip; Name = "FormSPAUCollection"; Text = "FormSPAUCollection"; ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit(); @@ -163,7 +213,10 @@ panel1.PerformLayout(); panel2.ResumeLayout(false); panel2.PerformLayout(); + menuStrip.ResumeLayout(false); + menuStrip.PerformLayout(); ResumeLayout(false); + PerformLayout(); } #endregion @@ -179,5 +232,11 @@ private TextBox textBoxStorageName; private Button ButtonDelObject; private ListBox listBoxStorages; + private MenuStrip menuStrip; + private ToolStripMenuItem toolStripMenuItem1; + private ToolStripMenuItem openFileDialog; + private ToolStripMenuItem saveFileDialog; + private OpenFileDialog openFileDialog_; + private OpenFileDialog saveFileDialog_; } } \ No newline at end of file diff --git a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/FormSPAUCollection.cs b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/FormSPAUCollection.cs index d5753a7..c983750 100644 --- a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/FormSPAUCollection.cs +++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/FormSPAUCollection.cs @@ -207,7 +207,47 @@ namespace SelfPropelledArtilleryUnit } pictureBoxCollection.Image = obj.ShowSPAUs(); } - - + /// + /// Обработка нажатия "Сохранение" + /// + /// + /// + 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("Загрузка прошлa успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); + ReloadObjects(); + } + else + { + MessageBox.Show("Не загрузилось", "Результат", + MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } } } diff --git a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/FormSPAUCollection.resx b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/FormSPAUCollection.resx index af32865..375a32c 100644 --- a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/FormSPAUCollection.resx +++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/FormSPAUCollection.resx @@ -117,4 +117,13 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + 17, 17 + + + 283, 23 + + + 130, 23 + \ No newline at end of file diff --git a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/FormSPAUConfig.Designer.cs b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/FormSPAUConfig.Designer.cs index 2dfe2ff..1200f32 100644 --- a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/FormSPAUConfig.Designer.cs +++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/FormSPAUConfig.Designer.cs @@ -142,7 +142,7 @@ // // panelPurple // - panelPurple.BackColor = Color.FromArgb(192, 0, 192); + panelPurple.BackColor = Color.Magenta; panelPurple.Location = new Point(91, 123); panelPurple.Name = "panelPurple"; panelPurple.Size = new Size(60, 25); diff --git a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/SPAUGenericCollection.cs b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/SPAUGenericCollection.cs index 0a4a07f..749640c 100644 --- a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/SPAUGenericCollection.cs +++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/SPAUGenericCollection.cs @@ -51,6 +51,10 @@ namespace SelfPropelledArtilleryUnit.Generics _collection = new SetGeneric(width * height); } /// + /// Получение объектов коллекции + /// + public IEnumerable GetCars => _collection.GetSPAUs(); + /// /// Перегрузка оператора сложения /// /// diff --git a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/SPAUGenericStorage.cs b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/SPAUGenericStorage.cs index fb2db48..fb2ea2a 100644 --- a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/SPAUGenericStorage.cs +++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/SPAUGenericStorage.cs @@ -79,6 +79,109 @@ namespace SelfPropelledArtilleryUnit.Generics } } } - } + /// + /// Разделитель для записи ключа и значения элемента словаря + /// + private static readonly char _separatorForKeyValue = '|'; + /// + /// Разделитель для записей коллекции данных в файл + /// + private readonly char _separatorRecords = ';'; + /// + /// Разделитель для записи информации по объекту в файл + /// + private static readonly char _separatorForObject = ':'; + + /// + /// Сохранение информации по автомобилям в хранилище в файл + /// + /// Путь и имя файла + /// true - сохранение прошло успешно, false - ошибка при сохранении данных + public bool SaveData(string filename) + { + if (File.Exists(filename)) + { + File.Delete(filename); + } + StringBuilder data = new(); + foreach (KeyValuePair> record in _SPAUStorages) + { + StringBuilder records = new(); + foreach (DrawningSPAU? elem in record.Value.GetCars.Reverse()) + { + records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}"); + } + data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}"); + } + if (data.Length == 0) + { + return false; + } + using (StreamWriter writer = new StreamWriter(filename)) + { + writer.WriteLine("SPAUStorage"); + writer.Write(data.ToString()); + } + return true; + } + /// + /// Загрузка информации по автомобилям в хранилище из файла + /// + /// Путь и имя файла + /// true - загрузка прошла успешно, false - ошибка при загрузке данных + public bool LoadData(string filename) + { + if (!File.Exists(filename)) + { + return false; + } + + using (StreamReader fs = File.OpenText(filename)) + { + string str = fs.ReadLine(); + if (str == null || str.Length == 0) + { + return false; + } + if (!str.StartsWith("SPAUStorage")) + { + return false; + } + + _SPAUStorages.Clear(); + string strs = ""; + + while ((strs = fs.ReadLine()) != null) + { + if (strs == null) + { + return false; + } + + string[] record = strs.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries); + if (record.Length != 2) + { + continue; + } + SPAUGenericCollection collection = new(_pictureWidth, _pictureHeight); + string[] set = record[1].Split(_separatorRecords, StringSplitOptions.RemoveEmptyEntries); + foreach (string elem in set) + { + DrawningSPAU? sPAU = elem?.CreateDrawningSPAU(_separatorForObject, _pictureWidth, _pictureHeight); + if (sPAU != null) + { + if ((collection + sPAU) == -1) + { + return false; + } + } + } + _SPAUStorages.Add(record[0], collection); + } + return true; + } + } + + } }