From e0092edb54c4300f339318acc3bd426e0315ca46 Mon Sep 17 00:00:00 2001 From: Ivan_Starostin Date: Tue, 19 Dec 2023 20:12:51 +0400 Subject: [PATCH] =?UTF-8?q?=D1=84=D0=B0=D0=B9=D0=BB=D1=8B=20=D0=BB=D0=B0?= =?UTF-8?q?=D0=B1=D0=BE=D1=80=D0=B0=D1=82=D0=BE=D1=80=D0=BD=D0=BE=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lainer/Lainer1/ExtensionDrawingLainer.cs | 51 +++++++++ .../Lainer1/FormLainerCollection.Designer.cs | 75 +++++++++++-- lainer/Lainer1/FormLainerCollection.cs | 34 ++++++ lainer/Lainer1/LainersGenericStorage.cs | 101 ++++++++++++++++-- 4 files changed, 239 insertions(+), 22 deletions(-) create mode 100644 lainer/Lainer1/ExtensionDrawingLainer.cs diff --git a/lainer/Lainer1/ExtensionDrawingLainer.cs b/lainer/Lainer1/ExtensionDrawingLainer.cs new file mode 100644 index 0000000..6990110 --- /dev/null +++ b/lainer/Lainer1/ExtensionDrawingLainer.cs @@ -0,0 +1,51 @@ +using System; +using System.Collections.Generic; +using ProjectLainer.Entities; + + +namespace ProjectLainer.DrawningObjects +{ + public static class ExtensionDrawningLainer + { + public static DrawingLainer? CreateDrawningLainer(this string info, char + separatorForObject, int width, int height) + { + string[] strs = info.Split(separatorForObject); + if (strs.Length == 3) + { + + return new DrawingLainer(Convert.ToInt32(strs[0]), + Convert.ToInt32(strs[1]), Color.FromName(strs[2]), width, height); + + } + if (strs.Length == 7) + { + return new DrawningSuperLainer(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 DrawingLainer drawningLainer, char separatorForObject) + { + var lainer = drawningLainer.EntityLainer; + if (lainer == null) + { + return string.Empty; + } + + var str = $"{lainer.Speed}{separatorForObject}{lainer.Weight}{separatorForObject}{lainer.BodyColor.Name}"; + if (lainer is not EntitySuperLainer superLainer) + { + return str; + } + return $"{str}{separatorForObject}{superLainer.AdditionalColor.Name}{separatorForObject}{superLainer.Pools}{separatorForObject}{superLainer.Decks}{separatorForObject}"; + } + } +} + + diff --git a/lainer/Lainer1/FormLainerCollection.Designer.cs b/lainer/Lainer1/FormLainerCollection.Designer.cs index e4f5a11..29e7cb9 100644 --- a/lainer/Lainer1/FormLainerCollection.Designer.cs +++ b/lainer/Lainer1/FormLainerCollection.Designer.cs @@ -37,20 +37,27 @@ textBoxStorageName = new TextBox(); listBoxStorages = new ListBox(); ButtonDelObject = new Button(); + saveFileDialog = new SaveFileDialog(); + openFileDialog = new OpenFileDialog(); + menuStrip1 = new MenuStrip(); + файлToolStripMenuItem = new ToolStripMenuItem(); + SaveToolStripMenuItem = new ToolStripMenuItem(); + LoadToolStripItem = new ToolStripMenuItem(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); + menuStrip1.SuspendLayout(); SuspendLayout(); // // pictureBoxCollection // - pictureBoxCollection.Location = new Point(16, 14); + pictureBoxCollection.Location = new Point(12, 56); pictureBoxCollection.Name = "pictureBoxCollection"; - pictureBoxCollection.Size = new Size(815, 434); + pictureBoxCollection.Size = new Size(706, 434); pictureBoxCollection.TabIndex = 0; pictureBoxCollection.TabStop = false; // // ButtonAddLainer // - ButtonAddLainer.Location = new Point(872, 282); + ButtonAddLainer.Location = new Point(804, 374); ButtonAddLainer.Name = "ButtonAddLainer"; ButtonAddLainer.Size = new Size(154, 32); ButtonAddLainer.TabIndex = 1; @@ -60,7 +67,7 @@ // // ButtonRemove // - ButtonRemove.Location = new Point(887, 384); + ButtonRemove.Location = new Point(807, 453); ButtonRemove.Name = "ButtonRemove"; ButtonRemove.Size = new Size(139, 37); ButtonRemove.TabIndex = 2; @@ -70,7 +77,7 @@ // // ButtonRefresh // - ButtonRefresh.Location = new Point(849, 449); + ButtonRefresh.Location = new Point(808, 510); ButtonRefresh.Name = "ButtonRefresh"; ButtonRefresh.Size = new Size(164, 29); ButtonRefresh.TabIndex = 3; @@ -80,14 +87,14 @@ // // maskedTextBoxNumber // - maskedTextBoxNumber.Location = new Point(888, 351); + maskedTextBoxNumber.Location = new Point(812, 412); maskedTextBoxNumber.Name = "maskedTextBoxNumber"; maskedTextBoxNumber.Size = new Size(125, 27); maskedTextBoxNumber.TabIndex = 4; // // ButtonAddObject // - ButtonAddObject.Location = new Point(856, 55); + ButtonAddObject.Location = new Point(790, 133); ButtonAddObject.Name = "ButtonAddObject"; ButtonAddObject.Size = new Size(170, 29); ButtonAddObject.TabIndex = 5; @@ -97,16 +104,17 @@ // // textBoxStorageName // - textBoxStorageName.Location = new Point(892, 22); + textBoxStorageName.Location = new Point(808, 69); textBoxStorageName.Name = "textBoxStorageName"; textBoxStorageName.Size = new Size(125, 27); textBoxStorageName.TabIndex = 6; + //textBoxStorageName.TextChanged += textBoxStorageName_TextChanged; // // listBoxStorages // listBoxStorages.FormattingEnabled = true; listBoxStorages.ItemHeight = 20; - listBoxStorages.Location = new Point(876, 90); + listBoxStorages.Location = new Point(808, 168); listBoxStorages.Name = "listBoxStorages"; listBoxStorages.Size = new Size(150, 104); listBoxStorages.TabIndex = 7; @@ -114,7 +122,7 @@ // // ButtonDelObject // - ButtonDelObject.Location = new Point(888, 210); + ButtonDelObject.Location = new Point(808, 293); ButtonDelObject.Name = "ButtonDelObject"; ButtonDelObject.Size = new Size(138, 29); ButtonDelObject.TabIndex = 8; @@ -122,11 +130,47 @@ ButtonDelObject.UseVisualStyleBackColor = true; ButtonDelObject.Click += ButtonDelObject_Click; // + // openFileDialog + // + openFileDialog.FileName = "openFileDialog1"; + // + // menuStrip1 + // + menuStrip1.ImageScalingSize = new Size(20, 20); + menuStrip1.Items.AddRange(new ToolStripItem[] { файлToolStripMenuItem }); + menuStrip1.Location = new Point(0, 0); + menuStrip1.Name = "menuStrip1"; + menuStrip1.Size = new Size(1027, 28); + menuStrip1.TabIndex = 12; + menuStrip1.Text = "menuStrip1"; + // + // файлToolStripMenuItem + // + файлToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { SaveToolStripMenuItem, LoadToolStripItem }); + файлToolStripMenuItem.Name = "файлToolStripMenuItem"; + файлToolStripMenuItem.Size = new Size(58, 24); + файлToolStripMenuItem.Text = "файл"; + // + // SaveToolStripMenuItem + // + SaveToolStripMenuItem.Name = "SaveToolStripMenuItem"; + SaveToolStripMenuItem.Size = new Size(175, 26); + SaveToolStripMenuItem.Text = "сохранение"; + SaveToolStripMenuItem.Click += SaveToolStripMenuItem_Click; + // + // LoadToolStripItem + // + LoadToolStripItem.Name = "LoadToolStripItem"; + LoadToolStripItem.Size = new Size(175, 26); + LoadToolStripItem.Text = "загрузка"; + LoadToolStripItem.Click += LoadToolStripItem_Click; + // // FormLainerCollection // AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(1038, 490); + ClientSize = new Size(1027, 551); + Controls.Add(menuStrip1); Controls.Add(ButtonDelObject); Controls.Add(listBoxStorages); Controls.Add(textBoxStorageName); @@ -136,9 +180,12 @@ Controls.Add(ButtonRemove); Controls.Add(ButtonAddLainer); Controls.Add(pictureBoxCollection); + MainMenuStrip = menuStrip1; Name = "FormLainerCollection"; Text = "FormLainerCollection"; ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit(); + menuStrip1.ResumeLayout(false); + menuStrip1.PerformLayout(); ResumeLayout(false); PerformLayout(); } @@ -154,5 +201,11 @@ private TextBox textBoxStorageName; private ListBox listBoxStorages; private Button ButtonDelObject; + private SaveFileDialog saveFileDialog; + private OpenFileDialog openFileDialog; + private MenuStrip menuStrip1; + private ToolStripMenuItem файлToolStripMenuItem; + private ToolStripMenuItem SaveToolStripMenuItem; + private ToolStripMenuItem LoadToolStripItem; } } \ No newline at end of file diff --git a/lainer/Lainer1/FormLainerCollection.cs b/lainer/Lainer1/FormLainerCollection.cs index 5ad33d9..61d511f 100644 --- a/lainer/Lainer1/FormLainerCollection.cs +++ b/lainer/Lainer1/FormLainerCollection.cs @@ -137,5 +137,39 @@ namespace ProjectLainer MessageBox.Show("Не удалось добавить объект"); } } + 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 LoadToolStripItem_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/lainer/Lainer1/LainersGenericStorage.cs b/lainer/Lainer1/LainersGenericStorage.cs index f5cbae6..1cdb237 100644 --- a/lainer/Lainer1/LainersGenericStorage.cs +++ b/lainer/Lainer1/LainersGenericStorage.cs @@ -2,14 +2,15 @@ using ProjectLainer.MovementStrategy; namespace ProjectLainer.Generics { - /// - /// Класс для хранения коллекции - /// internal class LainersGenericStorage { + readonly Dictionary> _lainerStorages; + public List Keys => _lainerStorages.Keys.ToList(); + private readonly int _pictureWidth; + private readonly int _pictureHeight; public LainersGenericStorage(int pictureWidth, int pictureHeight) { @@ -17,20 +18,22 @@ namespace ProjectLainer.Generics _pictureWidth = pictureWidth; _pictureHeight = pictureHeight; } + public void AddSet(string name) + { + _lainerStorages.Add(name, new LainersGenericCollection(_pictureWidth, _pictureHeight)); + } + + public void DelSet(string name) { if (!_lainerStorages.ContainsKey(name)) { - _lainerStorages.Add(name, new LainersGenericCollection(_pictureWidth, _pictureHeight)); - } - } - public void DelSet(string name) - { - if (_lainerStorages.ContainsKey(name)) - { - _lainerStorages.Remove(name); + return; } + + _lainerStorages.Remove(name); } + public LainersGenericCollection? this[string ind] { get @@ -42,6 +45,82 @@ namespace ProjectLainer.Generics return null; } } + private static readonly char _separatorForKeyValue = '|'; + private readonly char _separatorRecords = ';'; + private static readonly char _separatorForObject = ':'; + public bool SaveData(string filename) + { + + if (File.Exists(filename)) + { + File.Delete(filename); + } + StringBuilder data = new(); + foreach (KeyValuePair> record in _lainerStorages) + { + StringBuilder records = new(); + foreach (DrawingLainer? elem in record.Value.GetLainers) + { + records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}"); + } + data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}"); + } + if (data.Length == 0) + { + return false; + } + using (StreamWriter sw = new(filename)) + { + sw.WriteLine($"LainerStorage{Environment.NewLine}{data}"); + } + return true; + } + public bool LoadData(string filename) + { + if (!File.Exists(filename)) + { + return false; + } + using (StreamReader sr = new(filename)) + { + string str = sr.ReadLine(); + var strs = str.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries); + if (strs == null || strs.Length == 0) + { + return false; + } + if (!strs[0].StartsWith("LainerStorage")) + { + return false; + } + _lainerStorages.Clear(); + do + { + string[] record = str.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries); + if (record.Length != 1) + { + str = sr.ReadLine(); + continue; + } + LainersGenericCollection collection = new(_pictureWidth, _pictureHeight); + string[] set = record[1].Split(_separatorRecords, StringSplitOptions.RemoveEmptyEntries); + foreach (string elem in set) + { + DrawingLainer? lainer = elem?.CreateDrawningLainer(_separatorForObject, _pictureWidth, _pictureHeight); + if (lainer != null) + { + if (!(collection + lainer)) + { + return false; + } + } + } + _lainerStorages.Add(record[0], collection); + str = sr.ReadLine(); + } while (str != null); + } + return true; + } } }