diff --git a/ProjStormtrooper/ProjStormtrooper/ExtensionDrawingPlane.cs b/ProjStormtrooper/ProjStormtrooper/ExtensionDrawingPlane.cs new file mode 100644 index 0000000..a82570b --- /dev/null +++ b/ProjStormtrooper/ProjStormtrooper/ExtensionDrawingPlane.cs @@ -0,0 +1,53 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjStormtrooper +{ + 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/ProjStormtrooper/ProjStormtrooper/PlaneCollection.Designer.cs b/ProjStormtrooper/ProjStormtrooper/PlaneCollection.Designer.cs index 5613067..d4a4d39 100644 --- a/ProjStormtrooper/ProjStormtrooper/PlaneCollection.Designer.cs +++ b/ProjStormtrooper/ProjStormtrooper/PlaneCollection.Designer.cs @@ -31,17 +31,24 @@ groupBoxTools = new GroupBox(); groupBoxStorages = new GroupBox(); buttonRemoveStorage = new Button(); + listBoxStorages = new ListBox(); buttonAddStorage = new Button(); textBoxStorageName = new TextBox(); - listBoxStorages = new ListBox(); maskedTextBoxNumber = new MaskedTextBox(); buttonRefreshCollection = new Button(); 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,11 +59,11 @@ groupBoxTools.Controls.Add(buttonRemovePlane); groupBoxTools.Controls.Add(buttonAddPlane); groupBoxTools.Dock = DockStyle.Right; - groupBoxTools.Location = new Point(689, 0); + groupBoxTools.Location = new Point(689, 24); groupBoxTools.Margin = new Padding(3, 2, 3, 2); groupBoxTools.Name = "groupBoxTools"; groupBoxTools.Padding = new Padding(3, 2, 3, 2); - groupBoxTools.Size = new Size(201, 404); + groupBoxTools.Size = new Size(201, 380); groupBoxTools.TabIndex = 0; groupBoxTools.TabStop = false; groupBoxTools.Text = "Инструменты"; @@ -64,57 +71,62 @@ // groupBoxStorages // groupBoxStorages.Controls.Add(buttonRemoveStorage); + groupBoxStorages.Controls.Add(listBoxStorages); groupBoxStorages.Controls.Add(buttonAddStorage); groupBoxStorages.Controls.Add(textBoxStorageName); - groupBoxStorages.Controls.Add(listBoxStorages); - groupBoxStorages.Location = new Point(6, 21); + groupBoxStorages.Location = new Point(5, 20); + groupBoxStorages.Margin = new Padding(3, 2, 3, 2); groupBoxStorages.Name = "groupBoxStorages"; - groupBoxStorages.Size = new Size(189, 204); - groupBoxStorages.TabIndex = 6; + groupBoxStorages.Padding = new Padding(3, 2, 3, 2); + groupBoxStorages.Size = new Size(191, 181); + groupBoxStorages.TabIndex = 5; groupBoxStorages.TabStop = false; groupBoxStorages.Text = "Наборы"; // // buttonRemoveStorage // - buttonRemoveStorage.Location = new Point(6, 175); + buttonRemoveStorage.Location = new Point(5, 153); + buttonRemoveStorage.Margin = new Padding(3, 2, 3, 2); buttonRemoveStorage.Name = "buttonRemoveStorage"; - buttonRemoveStorage.Size = new Size(177, 23); - buttonRemoveStorage.TabIndex = 8; + buttonRemoveStorage.Size = new Size(180, 22); + buttonRemoveStorage.TabIndex = 3; buttonRemoveStorage.Text = "Удалить набор"; buttonRemoveStorage.UseVisualStyleBackColor = true; - buttonRemoveStorage.Click += buttonRemoveStorage_Click; + buttonRemoveStorage.Click += buttonDelStorage_Click; + // + // listBoxStorages + // + listBoxStorages.FormattingEnabled = true; + listBoxStorages.ItemHeight = 15; + listBoxStorages.Location = new Point(5, 70); + listBoxStorages.Margin = new Padding(3, 2, 3, 2); + listBoxStorages.Name = "listBoxStorages"; + listBoxStorages.Size = new Size(181, 79); + listBoxStorages.TabIndex = 2; + listBoxStorages.SelectedIndexChanged += listBoxStorages_SelectedIndexChanged; // // buttonAddStorage // - buttonAddStorage.Location = new Point(6, 51); + buttonAddStorage.Location = new Point(5, 44); + buttonAddStorage.Margin = new Padding(3, 2, 3, 2); buttonAddStorage.Name = "buttonAddStorage"; - buttonAddStorage.Size = new Size(177, 23); - buttonAddStorage.TabIndex = 7; + buttonAddStorage.Size = new Size(180, 22); + buttonAddStorage.TabIndex = 1; buttonAddStorage.Text = "Добавить набор"; buttonAddStorage.UseVisualStyleBackColor = true; buttonAddStorage.Click += buttonAddStorage_Click; // // textBoxStorageName // - textBoxStorageName.Location = new Point(6, 22); + textBoxStorageName.Location = new Point(5, 20); + textBoxStorageName.Margin = new Padding(3, 2, 3, 2); textBoxStorageName.Name = "textBoxStorageName"; - textBoxStorageName.Size = new Size(177, 23); - textBoxStorageName.TabIndex = 6; - textBoxStorageName.TextChanged += textBoxStorageName_TextChanged; - // - // listBoxStorages - // - listBoxStorages.FormattingEnabled = true; - listBoxStorages.ItemHeight = 15; - listBoxStorages.Location = new Point(6, 79); - listBoxStorages.Name = "listBoxStorages"; - listBoxStorages.Size = new Size(177, 94); - listBoxStorages.TabIndex = 5; - listBoxStorages.SelectedIndexChanged += listBoxStorages_SelectedIndexChanged; + textBoxStorageName.Size = new Size(181, 23); + textBoxStorageName.TabIndex = 0; // // maskedTextBoxNumber // - maskedTextBoxNumber.Location = new Point(5, 318); + maskedTextBoxNumber.Location = new Point(5, 303); maskedTextBoxNumber.Margin = new Padding(3, 2, 3, 2); maskedTextBoxNumber.Mask = "00"; maskedTextBoxNumber.Name = "maskedTextBoxNumber"; @@ -125,7 +137,7 @@ // // buttonRefreshCollection // - buttonRefreshCollection.Location = new Point(5, 371); + buttonRefreshCollection.Location = new Point(5, 373); buttonRefreshCollection.Margin = new Padding(3, 2, 3, 2); buttonRefreshCollection.Name = "buttonRefreshCollection"; buttonRefreshCollection.Size = new Size(191, 22); @@ -136,7 +148,7 @@ // // buttonRemovePlane // - buttonRemovePlane.Location = new Point(5, 345); + buttonRemovePlane.Location = new Point(5, 328); buttonRemovePlane.Margin = new Padding(3, 2, 3, 2); buttonRemovePlane.Name = "buttonRemovePlane"; buttonRemovePlane.Size = new Size(191, 22); @@ -147,7 +159,7 @@ // // buttonAddPlane // - buttonAddPlane.Location = new Point(4, 292); + buttonAddPlane.Location = new Point(5, 262); buttonAddPlane.Margin = new Padding(3, 2, 3, 2); buttonAddPlane.Name = "buttonAddPlane"; buttonAddPlane.Size = new Size(191, 22); @@ -159,13 +171,54 @@ // pictureBoxCollection // pictureBoxCollection.Dock = DockStyle.Fill; - pictureBoxCollection.Location = new Point(0, 0); + pictureBoxCollection.Location = new Point(0, 24); pictureBoxCollection.Margin = new Padding(3, 2, 3, 2); pictureBoxCollection.Name = "pictureBoxCollection"; - pictureBoxCollection.Size = new Size(689, 404); + pictureBoxCollection.Size = new Size(689, 380); pictureBoxCollection.TabIndex = 1; pictureBoxCollection.TabStop = false; - pictureBoxCollection.Click += pictureBoxCollection_Click; + // + // menuStrip + // + menuStrip.ImageScalingSize = new Size(20, 20); + menuStrip.Items.AddRange(new ToolStripItem[] { файлToolStripMenuItem }); + menuStrip.Location = new Point(0, 0); + menuStrip.Name = "menuStrip"; + menuStrip.Padding = new Padding(5, 2, 0, 2); + menuStrip.Size = new Size(890, 24); + menuStrip.TabIndex = 2; + menuStrip.Text = "menuStrip1"; + // + // файлToolStripMenuItem + // + файлToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { сохранитьToolStripMenuItem, загрузитьToolStripMenuItem }); + файлToolStripMenuItem.Name = "файлToolStripMenuItem"; + файлToolStripMenuItem.Size = new Size(48, 20); + файлToolStripMenuItem.Text = "Файл"; + // + // сохранитьToolStripMenuItem + // + сохранитьToolStripMenuItem.Name = "сохранитьToolStripMenuItem"; + сохранитьToolStripMenuItem.Size = new Size(133, 22); + сохранитьToolStripMenuItem.Text = "Сохранить"; + сохранитьToolStripMenuItem.Click += saveToFileToolStripMenuItem_Click; + // + // загрузитьToolStripMenuItem + // + загрузитьToolStripMenuItem.Name = "загрузитьToolStripMenuItem"; + загрузитьToolStripMenuItem.Size = new Size(133, 22); + загрузитьToolStripMenuItem.Text = "Загрузить"; + загрузитьToolStripMenuItem.Click += loadFromFileToolStripMenuItem_Click; + // + // openFileDialog + // + openFileDialog.FileName = "openFileDialog1"; + openFileDialog.Filter = "txt file | *.txt"; + openFileDialog.FileOk += openFileDialog_FileOk; + // + // saveFileDialog + // + saveFileDialog.Filter = "txt file | *.txt"; // // PlaneCollection // @@ -174,6 +227,8 @@ ClientSize = new Size(890, 404); Controls.Add(pictureBoxCollection); Controls.Add(groupBoxTools); + Controls.Add(menuStrip); + MainMenuStrip = menuStrip; Margin = new Padding(3, 2, 3, 2); Name = "PlaneCollection"; Text = "Набор самолетов"; @@ -182,7 +237,10 @@ groupBoxStorages.ResumeLayout(false); groupBoxStorages.PerformLayout(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit(); + menuStrip.ResumeLayout(false); + menuStrip.PerformLayout(); ResumeLayout(false); + PerformLayout(); } #endregion @@ -194,9 +252,15 @@ private MaskedTextBox maskedTextBoxNumber; private PictureBox pictureBoxCollection; private GroupBox groupBoxStorages; - private TextBox textBoxStorageName; + private Button buttonRemoveStorage; private ListBox listBoxStorages; private Button buttonAddStorage; - private Button buttonRemoveStorage; + 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/ProjStormtrooper/ProjStormtrooper/PlaneCollection.cs b/ProjStormtrooper/ProjStormtrooper/PlaneCollection.cs index 2c66540..4d79d8d 100644 --- a/ProjStormtrooper/ProjStormtrooper/PlaneCollection.cs +++ b/ProjStormtrooper/ProjStormtrooper/PlaneCollection.cs @@ -27,7 +27,9 @@ namespace ProjStormtrooper InitializeComponent(); _storage = new PlanesGenericStorage(pictureBoxCollection.Width, pictureBoxCollection.Height); } - + /// + /// Заполнение listBoxObjects + /// private void ReloadObjects() { int index = listBoxStorages.SelectedIndex; @@ -46,11 +48,57 @@ namespace ProjStormtrooper } } /// + /// Добавление набора в коллекцию + /// + /// + /// + private void buttonAddStorage_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(textBoxStorageName.Text)) + { + MessageBox.Show("Не все данные заполнены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _storage.AddSet(textBoxStorageName.Text); + ReloadObjects(); + } + /// + /// Выбор набора + /// + /// + /// + private void listBoxStorages_SelectedIndexChanged(object sender, EventArgs e) + { + pictureBoxCollection.Image = _storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowPlanes(); + } + /// + /// Удаление набора + /// + /// + /// + private void buttonDelStorage_Click(object sender, EventArgs e) + { + if (listBoxStorages.SelectedIndex == -1) + { + return; + } + if (MessageBox.Show($"Удалить объект {listBoxStorages.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) + { + _storage.DelSet(listBoxStorages.SelectedItem.ToString() ?? string.Empty); + ReloadObjects(); + } + } + /// /// Добавление объекта в набор /// /// /// - /// + private void buttonAddPlane_Click(object sender, EventArgs e) + { + var formPlaneConfig = new PlaneConfig(); + formPlaneConfig.AddEvent(AddPlaneListener); + formPlaneConfig.Show(); + } public void AddPlaneListener(DrawingPlane plane) { plane._pictureWidth = pictureBoxCollection.Width; @@ -74,13 +122,6 @@ namespace ProjStormtrooper MessageBox.Show("Не удалось добавить объект"); } } - private void buttonAddPlane_Click(object sender, EventArgs e) - { - var formPlaneConfig = new FormPlaneConfig(); - formPlaneConfig.AddEvent(AddPlaneListener); - formPlaneConfig.Show(); - } - /// /// Удаление объекта из набора /// @@ -118,6 +159,10 @@ namespace ProjStormtrooper /// /// private void buttonRefreshCollection_Click(object sender, EventArgs e) + { + RefreshCollection(); + } + private void RefreshCollection() { if (listBoxStorages.SelectedIndex == -1) { @@ -131,32 +176,35 @@ namespace ProjStormtrooper pictureBoxCollection.Image = obj.ShowPlanes(); } - private void buttonAddStorage_Click(object sender, EventArgs e) + private void saveToFileToolStripMenuItem_Click(object sender, EventArgs e) { - if (string.IsNullOrEmpty(textBoxStorageName.Text)) + if (saveFileDialog.ShowDialog() == DialogResult.OK) { - MessageBox.Show("Не все данные заполнены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); - return; + if (_storage.SaveData(saveFileDialog.FileName)) + { + MessageBox.Show("Сохранение прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); + } + else + { + MessageBox.Show("Не сохранилось", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); + } } - _storage.AddSet(textBoxStorageName.Text); - ReloadObjects(); } - private void listBoxStorages_SelectedIndexChanged(object sender, EventArgs e) + private void loadFromFileToolStripMenuItem_Click(object sender, EventArgs e) { - pictureBoxCollection.Image = _storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowPlanes(); - } - - private void buttonRemoveStorage_Click(object sender, EventArgs e) - { - if (listBoxStorages.SelectedIndex == -1) + if (openFileDialog.ShowDialog() == DialogResult.OK) { - return; - } - if (MessageBox.Show($"Удалить объект {listBoxStorages.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) - { - _storage.DelSet(listBoxStorages.SelectedItem.ToString() ?? string.Empty); - ReloadObjects(); + if (_storage.LoadData(openFileDialog.FileName)) + { + ReloadObjects(); + RefreshCollection(); + MessageBox.Show("Загрузка прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); + } + else + { + MessageBox.Show("Не загрузилось", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); + } } } } diff --git a/ProjStormtrooper/ProjStormtrooper/PlaneCollection.resx b/ProjStormtrooper/ProjStormtrooper/PlaneCollection.resx index af32865..6df6d7f 100644 --- a/ProjStormtrooper/ProjStormtrooper/PlaneCollection.resx +++ b/ProjStormtrooper/ProjStormtrooper/PlaneCollection.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/ProjStormtrooper/ProjStormtrooper/FormPlaneConfig.Designer.cs b/ProjStormtrooper/ProjStormtrooper/PlaneConfig.Designer.cs similarity index 99% rename from ProjStormtrooper/ProjStormtrooper/FormPlaneConfig.Designer.cs rename to ProjStormtrooper/ProjStormtrooper/PlaneConfig.Designer.cs index 8215bda..6ecfe56 100644 --- a/ProjStormtrooper/ProjStormtrooper/FormPlaneConfig.Designer.cs +++ b/ProjStormtrooper/ProjStormtrooper/PlaneConfig.Designer.cs @@ -1,6 +1,6 @@ namespace ProjStormtrooper { - partial class FormPlaneConfig + partial class PlaneConfig { /// /// Required designer variable. diff --git a/ProjStormtrooper/ProjStormtrooper/FormPlaneConfig.cs b/ProjStormtrooper/ProjStormtrooper/PlaneConfig.cs similarity index 98% rename from ProjStormtrooper/ProjStormtrooper/FormPlaneConfig.cs rename to ProjStormtrooper/ProjStormtrooper/PlaneConfig.cs index 5b74d9d..f06554a 100644 --- a/ProjStormtrooper/ProjStormtrooper/FormPlaneConfig.cs +++ b/ProjStormtrooper/ProjStormtrooper/PlaneConfig.cs @@ -10,7 +10,7 @@ using System.Windows.Forms; namespace ProjStormtrooper { - public partial class FormPlaneConfig : Form + public partial class PlaneConfig : Form { /// /// Переменная-выбранный самолет @@ -20,7 +20,7 @@ namespace ProjStormtrooper /// Событие /// private event Action? EventAddPlane; - public FormPlaneConfig() + public PlaneConfig() { InitializeComponent(); diff --git a/ProjStormtrooper/ProjStormtrooper/FormPlaneConfig.resx b/ProjStormtrooper/ProjStormtrooper/PlaneConfig.resx similarity index 100% rename from ProjStormtrooper/ProjStormtrooper/FormPlaneConfig.resx rename to ProjStormtrooper/ProjStormtrooper/PlaneConfig.resx diff --git a/ProjStormtrooper/ProjStormtrooper/PlanesGenericCollection.cs b/ProjStormtrooper/ProjStormtrooper/PlanesGenericCollection.cs index a0db0af..aed534c 100644 --- a/ProjStormtrooper/ProjStormtrooper/PlanesGenericCollection.cs +++ b/ProjStormtrooper/ProjStormtrooper/PlanesGenericCollection.cs @@ -140,7 +140,7 @@ namespace ProjStormtrooper } } - + public IEnumerable GetPlanes => _collection.GetPlanes(); } } diff --git a/ProjStormtrooper/ProjStormtrooper/PlanesGenericStorage.cs b/ProjStormtrooper/ProjStormtrooper/PlanesGenericStorage.cs index 24643c2..d4a2f50 100644 --- a/ProjStormtrooper/ProjStormtrooper/PlanesGenericStorage.cs +++ b/ProjStormtrooper/ProjStormtrooper/PlanesGenericStorage.cs @@ -8,6 +8,18 @@ namespace ProjStormtrooper { internal class PlanesGenericStorage { + /// + /// Разделитель для записи ключа и значения элемента словаря + /// + private static readonly char _separatorForKeyValue = '|'; + /// + /// Разделитель для записей коллекции данных в файл + /// + private readonly char _separatorRecords = ';'; + /// + /// Разделитель для записи информации по объекту в файл + /// + private static readonly char _separatorForObject = ':'; /// /// Словарь (хранилище) /// @@ -66,5 +78,79 @@ namespace ProjStormtrooper 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; + } + + } } }