diff --git a/ProjectStormtrooper/ProjectStormtrooper/ExtensionDrawingPlane.cs b/ProjectStormtrooper/ProjectStormtrooper/ExtensionDrawingPlane.cs new file mode 100644 index 0000000..c51a72a --- /dev/null +++ b/ProjectStormtrooper/ProjectStormtrooper/ExtensionDrawingPlane.cs @@ -0,0 +1,53 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectStormtrooper +{ + public static class ExtensionDrawingPlane + { + /// + /// Создание объекта из строки + /// + public static DrawingPlane? CreateDrawingPlane(this string info, char separatorForObject, int width, int height) + { + string[] strs = info.Split(separatorForObject); + if (strs.Length == 3) + { + return new DrawingPlane(Convert.ToInt32(strs[0]), Convert.ToInt32(strs[1]), Color.FromName(strs[2]), width, height); + } + if (strs.Length == 6) + { + return new DrawingStormtrooper( + 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 DrawingPlane drawningPlane, char separatorForObject) + { + var plane = drawningPlane.EntityPlane; + if (plane == null) + { + return string.Empty; + } + var str = $"{plane.Speed}{separatorForObject}{plane.Weight}{separatorForObject}{plane.BodyColor.Name}"; + if (plane is not EntityStormtrooper stormtrooper) + { + return str; + } + return $"{str}{separatorForObject}{stormtrooper.AdditionalColor.Name}{separatorForObject}{stormtrooper.Rockets}{separatorForObject}{stormtrooper.Bombs}"; + } + } +} diff --git a/ProjectStormtrooper/ProjectStormtrooper/FormPlaneCollection.Designer.cs b/ProjectStormtrooper/ProjectStormtrooper/FormPlaneCollection.Designer.cs index ef86dd2..1a511d4 100644 --- a/ProjectStormtrooper/ProjectStormtrooper/FormPlaneCollection.Designer.cs +++ b/ProjectStormtrooper/ProjectStormtrooper/FormPlaneCollection.Designer.cs @@ -39,9 +39,16 @@ buttonRemovePlane = new Button(); buttonAddPlane = new Button(); pictureBoxCollection = new PictureBox(); + menuStrip = new MenuStrip(); + файлToolStripMenuItem = new ToolStripMenuItem(); + сохранитьToolStripMenuItem = new ToolStripMenuItem(); + загрузитьToolStripMenuItem = new ToolStripMenuItem(); + openFileDialog = new OpenFileDialog(); + saveFileDialog = new SaveFileDialog(); groupBoxTools.SuspendLayout(); groupBoxStorages.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); + menuStrip.SuspendLayout(); SuspendLayout(); // // groupBoxTools @@ -52,9 +59,9 @@ groupBoxTools.Controls.Add(buttonRemovePlane); groupBoxTools.Controls.Add(buttonAddPlane); groupBoxTools.Dock = DockStyle.Right; - groupBoxTools.Location = new Point(787, 0); + groupBoxTools.Location = new Point(787, 28); groupBoxTools.Name = "groupBoxTools"; - groupBoxTools.Size = new Size(230, 538); + groupBoxTools.Size = new Size(230, 510); groupBoxTools.TabIndex = 0; groupBoxTools.TabStop = false; groupBoxTools.Text = "Инструменты"; @@ -152,12 +159,52 @@ // pictureBoxCollection // pictureBoxCollection.Dock = DockStyle.Fill; - pictureBoxCollection.Location = new Point(0, 0); + pictureBoxCollection.Location = new Point(0, 28); pictureBoxCollection.Name = "pictureBoxCollection"; - pictureBoxCollection.Size = new Size(787, 538); + pictureBoxCollection.Size = new Size(787, 510); pictureBoxCollection.TabIndex = 1; pictureBoxCollection.TabStop = false; // + // menuStrip + // + menuStrip.ImageScalingSize = new Size(20, 20); + menuStrip.Items.AddRange(new ToolStripItem[] { файлToolStripMenuItem }); + menuStrip.Location = new Point(0, 0); + menuStrip.Name = "menuStrip"; + menuStrip.Size = new Size(1017, 28); + menuStrip.TabIndex = 2; + menuStrip.Text = "menuStrip1"; + // + // файлToolStripMenuItem + // + файлToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { сохранитьToolStripMenuItem, загрузитьToolStripMenuItem }); + файлToolStripMenuItem.Name = "файлToolStripMenuItem"; + файлToolStripMenuItem.Size = new Size(59, 24); + файлToolStripMenuItem.Text = "Файл"; + // + // сохранитьToolStripMenuItem + // + сохранитьToolStripMenuItem.Name = "сохранитьToolStripMenuItem"; + сохранитьToolStripMenuItem.Size = new Size(166, 26); + сохранитьToolStripMenuItem.Text = "Сохранить"; + сохранитьToolStripMenuItem.Click += saveToFileToolStripMenuItem_Click; + // + // загрузитьToolStripMenuItem + // + загрузитьToolStripMenuItem.Name = "загрузитьToolStripMenuItem"; + загрузитьToolStripMenuItem.Size = new Size(166, 26); + загрузитьToolStripMenuItem.Text = "Загрузить"; + загрузитьToolStripMenuItem.Click += loadFromFileToolStripMenuItem_Click; + // + // openFileDialog + // + openFileDialog.FileName = "openFileDialog1"; + openFileDialog.Filter = "txt file | *.txt"; + // + // saveFileDialog + // + saveFileDialog.Filter = "txt file | *.txt"; + // // FormPlaneCollection // AutoScaleDimensions = new SizeF(8F, 20F); @@ -165,6 +212,8 @@ ClientSize = new Size(1017, 538); Controls.Add(pictureBoxCollection); Controls.Add(groupBoxTools); + Controls.Add(menuStrip); + MainMenuStrip = menuStrip; Name = "FormPlaneCollection"; Text = "Набор самолетов"; groupBoxTools.ResumeLayout(false); @@ -172,7 +221,10 @@ groupBoxStorages.ResumeLayout(false); groupBoxStorages.PerformLayout(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit(); + menuStrip.ResumeLayout(false); + menuStrip.PerformLayout(); ResumeLayout(false); + PerformLayout(); } #endregion @@ -188,5 +240,11 @@ private ListBox listBoxStorages; private Button buttonAddStorage; private TextBox textBoxStorageName; + private MenuStrip menuStrip; + private ToolStripMenuItem файлToolStripMenuItem; + private ToolStripMenuItem сохранитьToolStripMenuItem; + private ToolStripMenuItem загрузитьToolStripMenuItem; + private OpenFileDialog openFileDialog; + private SaveFileDialog saveFileDialog; } } \ No newline at end of file diff --git a/ProjectStormtrooper/ProjectStormtrooper/FormPlaneCollection.cs b/ProjectStormtrooper/ProjectStormtrooper/FormPlaneCollection.cs index c2fc639..9abec9a 100644 --- a/ProjectStormtrooper/ProjectStormtrooper/FormPlaneCollection.cs +++ b/ProjectStormtrooper/ProjectStormtrooper/FormPlaneCollection.cs @@ -159,6 +159,10 @@ namespace ProjectStormtrooper /// /// private void buttonRefreshCollection_Click(object sender, EventArgs e) + { + RefreshCollection(); + } + private void RefreshCollection() { if (listBoxStorages.SelectedIndex == -1) { @@ -171,5 +175,37 @@ namespace ProjectStormtrooper } pictureBoxCollection.Image = obj.ShowPlanes(); } + + private void saveToFileToolStripMenuItem_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 loadFromFileToolStripMenuItem_Click(object sender, EventArgs e) + { + if (openFileDialog.ShowDialog() == DialogResult.OK) + { + if (_storage.LoadData(openFileDialog.FileName)) + { + ReloadObjects(); + RefreshCollection(); + MessageBox.Show("Загрузка прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); + } + else + { + MessageBox.Show("Не загрузилось", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } } } diff --git a/ProjectStormtrooper/ProjectStormtrooper/FormPlaneCollection.resx b/ProjectStormtrooper/ProjectStormtrooper/FormPlaneCollection.resx index a395bff..ad03251 100644 --- a/ProjectStormtrooper/ProjectStormtrooper/FormPlaneCollection.resx +++ b/ProjectStormtrooper/ProjectStormtrooper/FormPlaneCollection.resx @@ -117,4 +117,13 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + 17, 17 + + + 152, 17 + + + 319, 17 + \ No newline at end of file diff --git a/ProjectStormtrooper/ProjectStormtrooper/FormPlaneConfig.Designer.cs b/ProjectStormtrooper/ProjectStormtrooper/FormPlaneConfig.Designer.cs index 9a1f06d..21c56ed 100644 --- a/ProjectStormtrooper/ProjectStormtrooper/FormPlaneConfig.Designer.cs +++ b/ProjectStormtrooper/ProjectStormtrooper/FormPlaneConfig.Designer.cs @@ -97,7 +97,7 @@ // // panelColorPink // - panelColorPink.BackColor = Color.FromArgb(255, 192, 192); + panelColorPink.BackColor = Color.PeachPuff; panelColorPink.Location = new Point(241, 89); panelColorPink.Name = "panelColorPink"; panelColorPink.Size = new Size(50, 50); @@ -121,7 +121,7 @@ // // panelColorBrown // - panelColorBrown.BackColor = Color.FromArgb(192, 64, 0); + panelColorBrown.BackColor = Color.DarkRed; panelColorBrown.Location = new Point(17, 89); panelColorBrown.Name = "panelColorBrown"; panelColorBrown.Size = new Size(50, 50); diff --git a/ProjectStormtrooper/ProjectStormtrooper/PlanesGenericCollection.cs b/ProjectStormtrooper/ProjectStormtrooper/PlanesGenericCollection.cs index 3a7f871..a2afcc5 100644 --- a/ProjectStormtrooper/ProjectStormtrooper/PlanesGenericCollection.cs +++ b/ProjectStormtrooper/ProjectStormtrooper/PlanesGenericCollection.cs @@ -138,5 +138,9 @@ namespace ProjectStormtrooper i++; } } + /// + /// Получение объектов коллекции + /// + public IEnumerable GetPlanes => _collection.GetPlanes(); } } diff --git a/ProjectStormtrooper/ProjectStormtrooper/PlanesGenericStorage.cs b/ProjectStormtrooper/ProjectStormtrooper/PlanesGenericStorage.cs index 078087f..5e87d18 100644 --- a/ProjectStormtrooper/ProjectStormtrooper/PlanesGenericStorage.cs +++ b/ProjectStormtrooper/ProjectStormtrooper/PlanesGenericStorage.cs @@ -8,6 +8,18 @@ namespace ProjectStormtrooper { internal class PlanesGenericStorage { + /// + /// Разделитель для записи ключа и значения элемента словаря + /// + private static readonly char _separatorForKeyValue = '|'; + /// + /// Разделитель для записей коллекции данных в файл + /// + private readonly char _separatorRecords = ';'; + /// + /// Разделитель для записи информации по объекту в файл + /// + private static readonly char _separatorForObject = ':'; /// /// Словарь (хранилище) /// @@ -66,5 +78,79 @@ namespace ProjectStormtrooper return null; } } + /// + /// Сохранение информации в файл + /// + /// Путь и имя файла + /// true - сохранение прошло успешно, false - ошибка при сохранении данных + public bool SaveData(string filename) + { + if (File.Exists(filename)) + { + File.Delete(filename); + } + using (StreamWriter sw = File.CreateText(filename)) + { + sw.WriteLine($"PlaneStorage"); + string storageString; + foreach (var storage in _planeStorages) + { + storageString = ""; + foreach (var plane in storage.Value.GetPlanes) + { + storageString += $"{plane?.GetDataForSave(_separatorForObject)}{_separatorRecords}"; + } + sw.WriteLine($"{storage.Key}{_separatorForKeyValue}{storageString}"); + } + } + return true; + } + /// + /// Загрузка информации по автомобилям в хранилище из файла + /// + /// Путь и имя файла + /// true - загрузка прошла успешно, false - ошибка при загрузке данных + public bool LoadData(string filename) + { + if (!File.Exists(filename)) + { + return false; + } + using (StreamReader sr = new StreamReader(filename)) + { + string? currentLine = sr.ReadLine(); + if (currentLine == null || !currentLine.Contains("PlaneStorage")) + { + return false; + } + _planeStorages.Clear(); + currentLine = sr.ReadLine(); + while (currentLine != null) + { + string[] record = currentLine.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries); + if (record.Length != 2) + { + continue; + } + PlanesGenericCollection collection = new(_pictureWidth, _pictureHeight); + string[] set = record[1].Split(_separatorRecords, StringSplitOptions.RemoveEmptyEntries); + foreach (string elem in set) + { + DrawingPlane? plane = elem?.CreateDrawingPlane(_separatorForObject, _pictureWidth, _pictureHeight); + if (plane != null) + { + if (collection + plane == -1) + { + return false; + } + } + } + _planeStorages.Add(record[0], collection); + currentLine = sr.ReadLine(); + } + return true; + } + + } } }