diff --git a/Monorail/Monorail/DrawningObjects/ExtentionDrawningMonorail.cs b/Monorail/Monorail/DrawningObjects/ExtentionDrawningMonorail.cs new file mode 100644 index 0000000..8eed1e4 --- /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 == 7) + { + 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..4a8a3cf 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,38 @@ 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); + } + ReloadObjects(); + } + } } } 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/FormMonorailConfig.Designer.cs b/Monorail/Monorail/FormMonorailConfig.Designer.cs index 3013e16..ef2309c 100644 --- a/Monorail/Monorail/FormMonorailConfig.Designer.cs +++ b/Monorail/Monorail/FormMonorailConfig.Designer.cs @@ -29,6 +29,10 @@ private void InitializeComponent() { groupBox1 = new GroupBox(); + ButtonOk = new Button(); + ButtonCancel = new Button(); + label_body_color = new Label(); + label_additional_color = new Label(); labelModifiedObject = new Label(); labelSimpleObject = new Label(); GroupColor = new GroupBox(); @@ -48,11 +52,7 @@ numericUpDownSpeed = new NumericUpDown(); pictureBoxObject = new PictureBox(); label4 = new Label(); - ButtonOk = new Button(); - ButtonCancel = new Button(); PanelObject = new Panel(); - label_body_color = new Label(); - label_additional_color = new Label(); groupBox1.SuspendLayout(); GroupColor.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)numericUpDownWeight).BeginInit(); @@ -84,6 +84,59 @@ groupBox1.TabStop = false; groupBox1.Text = "Параметры"; // + // ButtonOk + // + ButtonOk.BackColor = SystemColors.Info; + ButtonOk.ForeColor = SystemColors.ControlText; + ButtonOk.Location = new Point(313, 480); + ButtonOk.Name = "ButtonOk"; + ButtonOk.Size = new Size(101, 46); + ButtonOk.TabIndex = 12; + ButtonOk.Text = "Добавить"; + ButtonOk.UseVisualStyleBackColor = false; + ButtonOk.Click += ButtonOk_Click; + // + // ButtonCancel + // + ButtonCancel.BackColor = SystemColors.Info; + ButtonCancel.ForeColor = SystemColors.ControlText; + ButtonCancel.Location = new Point(6, 480); + ButtonCancel.Name = "ButtonCancel"; + ButtonCancel.Size = new Size(106, 46); + ButtonCancel.TabIndex = 13; + ButtonCancel.Text = "Отмена"; + ButtonCancel.UseVisualStyleBackColor = false; + // + // label_body_color + // + label_body_color.AllowDrop = true; + label_body_color.BackColor = SystemColors.Info; + label_body_color.BorderStyle = BorderStyle.FixedSingle; + label_body_color.ForeColor = SystemColors.ControlText; + label_body_color.Location = new Point(6, 382); + label_body_color.Name = "label_body_color"; + label_body_color.Size = new Size(106, 56); + label_body_color.TabIndex = 10; + label_body_color.Text = "Осн. цвет"; + label_body_color.TextAlign = ContentAlignment.MiddleCenter; + label_body_color.DragDrop += PanelColor_DragDrop; + label_body_color.DragEnter += PanelColor_DragEnter; + // + // label_additional_color + // + label_additional_color.AllowDrop = true; + label_additional_color.BackColor = SystemColors.Info; + label_additional_color.BorderStyle = BorderStyle.FixedSingle; + label_additional_color.ForeColor = SystemColors.ControlText; + label_additional_color.Location = new Point(250, 382); + label_additional_color.Name = "label_additional_color"; + label_additional_color.Size = new Size(101, 56); + label_additional_color.TabIndex = 10; + label_additional_color.Text = "Доп. цвет"; + label_additional_color.TextAlign = ContentAlignment.MiddleCenter; + label_additional_color.DragDrop += PanelColor_DragDrop; + label_additional_color.DragEnter += PanelColor_DragEnter; + // // labelModifiedObject // labelModifiedObject.AllowDrop = true; @@ -159,7 +212,7 @@ // // panelBlue // - panelBlue.BackColor = Color.FromArgb(0, 0, 192); + panelBlue.BackColor = Color.Blue; panelBlue.Location = new Point(174, 32); panelBlue.Name = "panelBlue"; panelBlue.Size = new Size(78, 55); @@ -195,7 +248,7 @@ // // panelRed // - panelRed.BackColor = Color.FromArgb(192, 0, 0); + panelRed.BackColor = Color.Red; panelRed.Location = new Point(6, 32); panelRed.Name = "panelRed"; panelRed.Size = new Size(78, 55); @@ -282,29 +335,6 @@ label4.Size = new Size(0, 20); label4.TabIndex = 2; // - // ButtonOk - // - ButtonOk.BackColor = SystemColors.Info; - ButtonOk.ForeColor = SystemColors.ControlText; - ButtonOk.Location = new Point(313, 480); - ButtonOk.Name = "ButtonOk"; - ButtonOk.Size = new Size(101, 46); - ButtonOk.TabIndex = 12; - ButtonOk.Text = "Добавить"; - ButtonOk.UseVisualStyleBackColor = false; - ButtonOk.Click += ButtonOk_Click; - // - // ButtonCancel - // - ButtonCancel.BackColor = SystemColors.Info; - ButtonCancel.ForeColor = SystemColors.ControlText; - ButtonCancel.Location = new Point(6, 480); - ButtonCancel.Name = "ButtonCancel"; - ButtonCancel.Size = new Size(106, 46); - ButtonCancel.TabIndex = 13; - ButtonCancel.Text = "Отмена"; - ButtonCancel.UseVisualStyleBackColor = false; - // // PanelObject // PanelObject.AllowDrop = true; @@ -316,36 +346,6 @@ PanelObject.DragDrop += PanelObject_DragDrop; PanelObject.DragEnter += PanelObject_DragEnter; // - // label_body_color - // - label_body_color.AllowDrop = true; - label_body_color.BackColor = SystemColors.Info; - label_body_color.BorderStyle = BorderStyle.FixedSingle; - label_body_color.ForeColor = SystemColors.ControlText; - label_body_color.Location = new Point(6, 382); - label_body_color.Name = "label_body_color"; - label_body_color.Size = new Size(106, 56); - label_body_color.TabIndex = 10; - label_body_color.Text = "Осн. цвет"; - label_body_color.TextAlign = ContentAlignment.MiddleCenter; - label_body_color.DragDrop += PanelColor_DragDrop; - label_body_color.DragEnter += PanelColor_DragEnter; - // - // label_additional_color - // - label_additional_color.AllowDrop = true; - label_additional_color.BackColor = SystemColors.Info; - label_additional_color.BorderStyle = BorderStyle.FixedSingle; - label_additional_color.ForeColor = SystemColors.ControlText; - label_additional_color.Location = new Point(250, 382); - label_additional_color.Name = "label_additional_color"; - label_additional_color.Size = new Size(101, 56); - label_additional_color.TabIndex = 10; - label_additional_color.Text = "Доп. цвет"; - label_additional_color.TextAlign = ContentAlignment.MiddleCenter; - label_additional_color.DragDrop += PanelColor_DragDrop; - label_additional_color.DragEnter += PanelColor_DragEnter; - // // FormMonorailConfig // AutoScaleDimensions = new SizeF(8F, 20F); 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..5fa5bca 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,85 @@ 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 StreamWriter sw = new(filename); + sw.Write($"MonorailStorage{Environment.NewLine}{data}"); + return true; + } + public bool LoadData(string filename) + { + if (!File.Exists(filename)) + { + return false; + } + using (StreamReader sr = File.OpenText(filename)) + { + string str = sr.ReadLine(); + if (str == null || str.Length == 0) + { + return false; + } + if (!str.StartsWith("MonorailStorage")) + { + return false; + } + + _monorailsStorages.Clear(); + string strs = ""; + + while ((strs = sr.ReadLine()) != null) + { + if (strs == null) + { + return false; + } + + string[] record = strs.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; + } + } + } }