diff --git a/Monorail/Monorail/DrawningObjects/ExtentionDrawningMonorail.cs b/Monorail/Monorail/DrawningObjects/ExtentionDrawningMonorail.cs new file mode 100644 index 0000000..5a969f0 --- /dev/null +++ b/Monorail/Monorail/DrawningObjects/ExtentionDrawningMonorail.cs @@ -0,0 +1,52 @@ +using Monorail.Entities; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Monorail.DrawningObjects +{ + public static class ExtentionDrawningMonorail + { + public static DrawningMonorail? CreateDrawningMonorail(this string info, + char separatorForObject, int width, int height) + { + string[] strs = info.Split(separatorForObject); + if(strs.Length == 3) + { + return new DrawningMonorail(Convert.ToInt32(strs[0]), Convert.ToInt32(strs[1]), + Color.FromName(strs[2]), width, height); + } + if (strs.Length == 6) + { + return new DrawningSecondMonorail(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 DrawningMonorail drawningMonorail, + char separatorForObject) + { + var monorail = drawningMonorail.EntityMonorail; + if (monorail == null) + { + return string.Empty; + } + var str = + $"{monorail.Speed}{separatorForObject}{monorail.Weight}{separatorForObject}{monorail.BodyColor.Name}"; + if (monorail is not EntitySecondMonorail secondMonorail) + { + return str; + } + return + $"{str}{separatorForObject}{secondMonorail.AdditionalColor.Name}{separatorForObject}" + + $"{secondMonorail.Monorails}{separatorForObject}{secondMonorail.SecondCabin}{separatorForObject}"; + } + } +} diff --git a/Monorail/Monorail/FormMonorailCollection.Designer.cs b/Monorail/Monorail/FormMonorailCollection.Designer.cs index a1b15a0..b5201ce 100644 --- a/Monorail/Monorail/FormMonorailCollection.Designer.cs +++ b/Monorail/Monorail/FormMonorailCollection.Designer.cs @@ -41,16 +41,23 @@ buttonDelMonorail = new Button(); buttonAddMonorail = new Button(); label1 = new Label(); + menuStrip1 = new MenuStrip(); + файлToolStripMenuItem = new ToolStripMenuItem(); + сохранитьToolStripMenuItem = new ToolStripMenuItem(); + загрузитьToolStripMenuItem = new ToolStripMenuItem(); + openFileDialog = new OpenFileDialog(); + saveFileDialog = new SaveFileDialog(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); panel1.SuspendLayout(); panel2.SuspendLayout(); + menuStrip1.SuspendLayout(); SuspendLayout(); // // pictureBoxCollection // - pictureBoxCollection.Location = new Point(5, -1); + pictureBoxCollection.Location = new Point(5, 31); pictureBoxCollection.Name = "pictureBoxCollection"; - pictureBoxCollection.Size = new Size(1017, 517); + pictureBoxCollection.Size = new Size(1017, 485); pictureBoxCollection.TabIndex = 5; pictureBoxCollection.TabStop = false; // @@ -97,6 +104,7 @@ listBoxStorages.Name = "listBoxStorages"; listBoxStorages.Size = new Size(144, 104); listBoxStorages.TabIndex = 9; + listBoxStorages.Click += ListBoxObjects_SelectedIndexChanged; // // textBoxStorageName // @@ -176,6 +184,47 @@ label1.TabIndex = 0; label1.Text = "Инструменты"; // + // 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(1228, 28); + menuStrip1.TabIndex = 6; + menuStrip1.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 += SaveToolStripMenuItem_Click; + // + // загрузитьToolStripMenuItem + // + загрузитьToolStripMenuItem.Name = "загрузитьToolStripMenuItem"; + загрузитьToolStripMenuItem.Size = new Size(166, 26); + загрузитьToolStripMenuItem.Text = "Загрузить"; + загрузитьToolStripMenuItem.Click += LoadToolStripMenuItem_Click; + // + // openFileDialog + // + openFileDialog.FileName = "openFileDialog"; + openFileDialog.Filter = "txt file | *.txt"; + // + // saveFileDialog + // + saveFileDialog.FileName = "saveFileDialog"; + saveFileDialog.Filter = "txt file | *.txt"; + // // FormMonorailCollection // AutoScaleDimensions = new SizeF(8F, 20F); @@ -184,6 +233,8 @@ ClientSize = new Size(1228, 518); Controls.Add(pictureBoxCollection); Controls.Add(panel1); + Controls.Add(menuStrip1); + MainMenuStrip = menuStrip1; Name = "FormMonorailCollection"; Text = "Набор Монорельсов"; ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit(); @@ -191,7 +242,10 @@ panel1.PerformLayout(); panel2.ResumeLayout(false); panel2.PerformLayout(); + menuStrip1.ResumeLayout(false); + menuStrip1.PerformLayout(); ResumeLayout(false); + PerformLayout(); } #endregion @@ -209,5 +263,11 @@ private ListBox listBoxStorages; private TextBox textBoxStorageName; private Button ButtonAddObject; + private MenuStrip menuStrip1; + private ToolStripMenuItem файлToolStripMenuItem; + private ToolStripMenuItem сохранитьToolStripMenuItem; + private ToolStripMenuItem загрузитьToolStripMenuItem; + private OpenFileDialog openFileDialog; + private SaveFileDialog saveFileDialog; } } \ No newline at end of file diff --git a/Monorail/Monorail/FormMonorailCollection.cs b/Monorail/Monorail/FormMonorailCollection.cs index 2d2a16d..e8f0349 100644 --- a/Monorail/Monorail/FormMonorailCollection.cs +++ b/Monorail/Monorail/FormMonorailCollection.cs @@ -105,7 +105,7 @@ EventArgs e) } int addedIndex = obj + monorail; - if (addedIndex!= -1 && addedIndex < countPlace) + if (addedIndex != -1 && addedIndex < countPlace) { MessageBox.Show("Объект добавлен"); pictureBoxCollection.Image = obj.ShowMonorails(); @@ -168,5 +168,37 @@ EventArgs e) } pictureBoxCollection.Image = obj.ShowMonorails(); } + 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("Загрузка прошло успешно", + "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); + } + else + { + MessageBox.Show("Не загрузилось", "Результат", + MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } } } diff --git a/Monorail/Monorail/FormMonorailCollection.resx b/Monorail/Monorail/FormMonorailCollection.resx index 0f362c9..88a7555 100644 --- a/Monorail/Monorail/FormMonorailCollection.resx +++ b/Monorail/Monorail/FormMonorailCollection.resx @@ -117,4 +117,13 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + 330, 17 + + + 17, 17 + + + 176, 17 + \ No newline at end of file diff --git a/Monorail/Monorail/Generics/MonorailsGenericCollection.cs b/Monorail/Monorail/Generics/MonorailsGenericCollection.cs index 6ad6ab4..43b29fe 100644 --- a/Monorail/Monorail/Generics/MonorailsGenericCollection.cs +++ b/Monorail/Monorail/Generics/MonorailsGenericCollection.cs @@ -12,6 +12,7 @@ namespace Monorail.Generics where T: DrawningMonorail where U : IMoveableObject { + public IEnumerable GetMonorails => _collection.GetMonorails(); private readonly int _pictureWidth; private readonly int _pictureHeight; private readonly int _placeSizeWidth = 295; diff --git a/Monorail/Monorail/Generics/MonorailsGenericStorage.cs b/Monorail/Monorail/Generics/MonorailsGenericStorage.cs index bb19d61..d4a8118 100644 --- a/Monorail/Monorail/Generics/MonorailsGenericStorage.cs +++ b/Monorail/Monorail/Generics/MonorailsGenericStorage.cs @@ -10,6 +10,10 @@ namespace Monorail.Generics { internal class MonorailsGenericStorage { + private static readonly char _separatorForKeyValue = '|'; + private readonly char _separatorRecords = ';'; + private static readonly char _separatorForObject = ':'; + readonly Dictionary> _monorailsStorages; public List Keys => _monorailsStorages.Keys.ToList(); @@ -48,5 +52,89 @@ namespace Monorail.Generics return _monorailsStorages[ind]; } } + public bool SaveData(string filename) + { + if (File.Exists(filename)) + { + File.Delete(filename); + } + StringBuilder data = new(); + foreach (KeyValuePair> + record in _monorailsStorages) + { + StringBuilder records = new(); + foreach (DrawningMonorail? elem in record.Value.GetMonorails) + { + records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}"); + } + data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}"); + } + if (data.Length == 0) + { + return false; + } + using FileStream fs = new(filename, FileMode.Create); + byte[] info = new + UTF8Encoding(true).GetBytes($"MonorailStorage{Environment.NewLine}{data}"); + fs.Write(info, 0, info.Length); + return true; + } + public bool LoadData(string filename) + { + if (!File.Exists(filename)) + { + return false; + } + string bufferTextFromFile = ""; + using (FileStream fs = new(filename, FileMode.Open)) + { + byte[] b = new byte[fs.Length]; + UTF8Encoding temp = new(true); + while (fs.Read(b, 0, b.Length) > 0) + { + bufferTextFromFile += temp.GetString(b); + } + } + var strs = bufferTextFromFile.Split(new char[] { '\n', '\r' }, + StringSplitOptions.RemoveEmptyEntries); + if (strs == null || strs.Length == 0) + { + return false; + } + if (!strs[0].StartsWith("MonorailStorage")) + { + //если нет такой записи, то это не те данные + return false; + } + _monorailsStorages.Clear(); + foreach (string data in strs) + { + string[] record = data.Split(_separatorForKeyValue, + StringSplitOptions.RemoveEmptyEntries); + if (record.Length != 2) + { + continue; + } + MonorailsGenericCollection + collection = new(_pictureWidth, _pictureHeight); + string[] set = record[1].Split(_separatorRecords, + StringSplitOptions.RemoveEmptyEntries); + foreach (string elem in set) + { + DrawningMonorail? monorail = + elem?.CreateDrawningMonorail(_separatorForObject, _pictureWidth, _pictureHeight); + if (monorail != null) + { + if ((collection + monorail)==-1) + { + return false; + } + } + } + _monorailsStorages.Add(record[0], collection); + } + return true; + } + } }