diff --git a/ProjectElectricLocomotive/ProjectElectricLocomotive/DrawingElectricLocomotive.cs b/ProjectElectricLocomotive/ProjectElectricLocomotive/DrawingElectricLocomotive.cs index f944efc..f882f92 100644 --- a/ProjectElectricLocomotive/ProjectElectricLocomotive/DrawingElectricLocomotive.cs +++ b/ProjectElectricLocomotive/ProjectElectricLocomotive/DrawingElectricLocomotive.cs @@ -14,7 +14,7 @@ namespace ProjectElectricLocomotive.DrawingObjects { if (EntityLocomotive != null) { - EntityLocomotive = new EntityElectricLocomotive(speed, width, bodyColor, additionalColor, horns, seifBatteries); + EntityLocomotive = new EntityElectricLocomotive(speed, weight, bodyColor, additionalColor, horns, seifBatteries); } } public override void DrawTransport(Graphics g) diff --git a/ProjectElectricLocomotive/ProjectElectricLocomotive/ExtentionDrawingLocomotive.cs b/ProjectElectricLocomotive/ProjectElectricLocomotive/ExtentionDrawingLocomotive.cs new file mode 100644 index 0000000..a2f4878 --- /dev/null +++ b/ProjectElectricLocomotive/ProjectElectricLocomotive/ExtentionDrawingLocomotive.cs @@ -0,0 +1,81 @@ +using ProjectElectricLocomotive.DrawingObjects; +using ProjectElectricLocomotive.Entities; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectElectricLocomotive +{ + /// + /// Расширение для класса EntityLocomotive + /// + public static class ExtentionDrawingLocomotive + { + /// + /// Создание объекта из строки + /// + /// Строка с данными для создания объекта + /// Разделитель даннных + /// Ширина + /// Высота + /// Объект + public static DrawingLocomotive? CreateDrawingLocomotive(this string info, char + separatorForObject, int width, int height) + { + string[] strs = info.Split(separatorForObject); + if (strs.Length == 3) + { + return new DrawingLocomotive( + Convert.ToInt32(strs[0]), + Convert.ToInt32(strs[1]), + Color.FromName(strs[2]), + width, + height + ); + } + if (strs.Length == 6) + { + return new DrawingElectricLocomotive( + 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 DrawingLocomotive drawningLoco, char separatorForObject) + { + var loco = drawningLoco.EntityLocomotive; + if (loco == null) + { + return string.Empty; + } + var str = + $"{loco.Speed}{separatorForObject}{loco.Weight}{separatorForObject}{loco.BodyColor.Name}"; + if (loco is not EntityElectricLocomotive electroLoco) + { + return str; + } + else + { + return + $"{str}{separatorForObject}{electroLoco.AdditionalColor.Name}{separatorForObject}" + + $"{electroLoco.Horns}{separatorForObject}{electroLoco.SeifBatteries}"; + } + } + + } +} diff --git a/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveCollections.Designer.cs b/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveCollections.Designer.cs index fe4e41c..ae11658 100644 --- a/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveCollections.Designer.cs +++ b/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveCollections.Designer.cs @@ -28,175 +28,224 @@ /// private void InitializeComponent() { - this.components = new System.ComponentModel.Container(); - this.maskedTextBoxNumber = new System.Windows.Forms.MaskedTextBox(); - this.ButtonRefreshCollection = new System.Windows.Forms.Button(); - this.ButtonRemoveLocomotive = new System.Windows.Forms.Button(); - this.ButtonAddLocomotive = new System.Windows.Forms.Button(); - this.pictureBoxCollections = new System.Windows.Forms.PictureBox(); - this.textBoxStorageName = new System.Windows.Forms.TextBox(); - this.bindingSource1 = new System.Windows.Forms.BindingSource(this.components); - this.bindingSource2 = new System.Windows.Forms.BindingSource(this.components); - this.groupBox1 = new System.Windows.Forms.GroupBox(); - this.listBoxStorage = new System.Windows.Forms.ListBox(); - this.ButtonAddObject = new System.Windows.Forms.Button(); - this.ButtonRemoveObject = new System.Windows.Forms.Button(); - this.Instruments = new System.Windows.Forms.GroupBox(); - ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollections)).BeginInit(); - ((System.ComponentModel.ISupportInitialize)(this.bindingSource1)).BeginInit(); - ((System.ComponentModel.ISupportInitialize)(this.bindingSource2)).BeginInit(); - this.groupBox1.SuspendLayout(); - this.Instruments.SuspendLayout(); - this.SuspendLayout(); + components = new System.ComponentModel.Container(); + maskedTextBoxNumber = new MaskedTextBox(); + ButtonRefreshCollection = new Button(); + ButtonRemoveLocomotive = new Button(); + ButtonAddLocomotive = new Button(); + pictureBoxCollections = new PictureBox(); + textBoxStorageName = new TextBox(); + bindingSource1 = new BindingSource(components); + bindingSource2 = new BindingSource(components); + groupBox1 = new GroupBox(); + listBoxStorage = new ListBox(); + ButtonAddObject = new Button(); + ButtonRemoveObject = new Button(); + Instruments = new GroupBox(); + menuStrip1 = new MenuStrip(); + файлToolStripMenuItem = new ToolStripMenuItem(); + SaveToolStripMenuItem = new ToolStripMenuItem(); + LoadToolStripMenuItem = new ToolStripMenuItem(); + saveFileDialog = new SaveFileDialog(); + openFileDialog = new OpenFileDialog(); + ((System.ComponentModel.ISupportInitialize)pictureBoxCollections).BeginInit(); + ((System.ComponentModel.ISupportInitialize)bindingSource1).BeginInit(); + ((System.ComponentModel.ISupportInitialize)bindingSource2).BeginInit(); + groupBox1.SuspendLayout(); + Instruments.SuspendLayout(); + menuStrip1.SuspendLayout(); + SuspendLayout(); // // maskedTextBoxNumber // - this.maskedTextBoxNumber.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) - | System.Windows.Forms.AnchorStyles.Right))); - this.maskedTextBoxNumber.Location = new System.Drawing.Point(38, 532); - this.maskedTextBoxNumber.Mask = "0"; - this.maskedTextBoxNumber.Name = "maskedTextBoxNumber"; - this.maskedTextBoxNumber.Size = new System.Drawing.Size(149, 27); - this.maskedTextBoxNumber.TabIndex = 4; + maskedTextBoxNumber.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + maskedTextBoxNumber.Location = new Point(38, 532); + maskedTextBoxNumber.Mask = "0"; + maskedTextBoxNumber.Name = "maskedTextBoxNumber"; + maskedTextBoxNumber.Size = new Size(156, 27); + maskedTextBoxNumber.TabIndex = 4; // // ButtonRefreshCollection // - this.ButtonRefreshCollection.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.ButtonRefreshCollection.Font = new System.Drawing.Font("Candara Light", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); - this.ButtonRefreshCollection.Location = new System.Drawing.Point(38, 665); - this.ButtonRefreshCollection.Name = "ButtonRefreshCollection"; - this.ButtonRefreshCollection.Size = new System.Drawing.Size(150, 41); - this.ButtonRefreshCollection.TabIndex = 2; - this.ButtonRefreshCollection.Text = "Обновить все"; - this.ButtonRefreshCollection.UseVisualStyleBackColor = true; + ButtonRefreshCollection.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; + ButtonRefreshCollection.Font = new Font("Candara Light", 9F, FontStyle.Regular, GraphicsUnit.Point); + ButtonRefreshCollection.Location = new Point(45, 663); + ButtonRefreshCollection.Name = "ButtonRefreshCollection"; + ButtonRefreshCollection.Size = new Size(150, 41); + ButtonRefreshCollection.TabIndex = 2; + ButtonRefreshCollection.Text = "Обновить все"; + ButtonRefreshCollection.UseVisualStyleBackColor = true; // // ButtonRemoveLocomotive // - this.ButtonRemoveLocomotive.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.ButtonRemoveLocomotive.Font = new System.Drawing.Font("Candara Light", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); - this.ButtonRemoveLocomotive.Location = new System.Drawing.Point(38, 592); - this.ButtonRemoveLocomotive.Name = "ButtonRemoveLocomotive"; - this.ButtonRemoveLocomotive.Size = new System.Drawing.Size(150, 44); - this.ButtonRemoveLocomotive.TabIndex = 1; - this.ButtonRemoveLocomotive.Text = "Удалить локо"; - this.ButtonRemoveLocomotive.UseVisualStyleBackColor = true; - this.ButtonRemoveLocomotive.Click += new System.EventHandler(this.ButtonRemoveLocomotive_Click); + ButtonRemoveLocomotive.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; + ButtonRemoveLocomotive.Font = new Font("Candara Light", 9F, FontStyle.Regular, GraphicsUnit.Point); + ButtonRemoveLocomotive.Location = new Point(45, 590); + ButtonRemoveLocomotive.Name = "ButtonRemoveLocomotive"; + ButtonRemoveLocomotive.Size = new Size(150, 44); + ButtonRemoveLocomotive.TabIndex = 1; + ButtonRemoveLocomotive.Text = "Удалить локо"; + ButtonRemoveLocomotive.UseVisualStyleBackColor = true; + ButtonRemoveLocomotive.Click += ButtonRemoveLocomotive_Click; // // ButtonAddLocomotive // - this.ButtonAddLocomotive.Anchor = System.Windows.Forms.AnchorStyles.Top; - this.ButtonAddLocomotive.Font = new System.Drawing.Font("Candara Light", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); - this.ButtonAddLocomotive.Location = new System.Drawing.Point(38, 461); - this.ButtonAddLocomotive.Name = "ButtonAddLocomotive"; - this.ButtonAddLocomotive.Size = new System.Drawing.Size(150, 39); - this.ButtonAddLocomotive.TabIndex = 0; - this.ButtonAddLocomotive.Text = "Добавить локо"; - this.ButtonAddLocomotive.UseVisualStyleBackColor = true; - this.ButtonAddLocomotive.Click += new System.EventHandler(this.ButtonAddLocomotive_Click); + ButtonAddLocomotive.Anchor = AnchorStyles.Top; + ButtonAddLocomotive.Font = new Font("Candara Light", 9F, FontStyle.Regular, GraphicsUnit.Point); + ButtonAddLocomotive.Location = new Point(42, 461); + ButtonAddLocomotive.Name = "ButtonAddLocomotive"; + ButtonAddLocomotive.Size = new Size(150, 39); + ButtonAddLocomotive.TabIndex = 0; + ButtonAddLocomotive.Text = "Добавить локо"; + ButtonAddLocomotive.UseVisualStyleBackColor = true; + ButtonAddLocomotive.Click += ButtonAddLocomotive_Click; // // pictureBoxCollections // - this.pictureBoxCollections.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) - | System.Windows.Forms.AnchorStyles.Left) - | System.Windows.Forms.AnchorStyles.Right))); - this.pictureBoxCollections.Location = new System.Drawing.Point(-1, 5); - this.pictureBoxCollections.Name = "pictureBoxCollections"; - this.pictureBoxCollections.Size = new System.Drawing.Size(798, 721); - this.pictureBoxCollections.TabIndex = 1; - this.pictureBoxCollections.TabStop = false; + pictureBoxCollections.Anchor = AnchorStyles.Left; + pictureBoxCollections.Location = new Point(-1, 27); + pictureBoxCollections.Name = "pictureBoxCollections"; + pictureBoxCollections.Size = new Size(1104, 692); + pictureBoxCollections.TabIndex = 1; + pictureBoxCollections.TabStop = false; // // textBoxStorageName // - this.textBoxStorageName.Location = new System.Drawing.Point(31, 43); - this.textBoxStorageName.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); - this.textBoxStorageName.Name = "textBoxStorageName"; - this.textBoxStorageName.Size = new System.Drawing.Size(149, 27); - this.textBoxStorageName.TabIndex = 5; + textBoxStorageName.Location = new Point(31, 43); + textBoxStorageName.Margin = new Padding(3, 4, 3, 4); + textBoxStorageName.Name = "textBoxStorageName"; + textBoxStorageName.Size = new Size(149, 27); + textBoxStorageName.TabIndex = 5; // // groupBox1 // - this.groupBox1.Controls.Add(this.listBoxStorage); - this.groupBox1.Controls.Add(this.ButtonAddObject); - this.groupBox1.Controls.Add(this.ButtonRemoveObject); - this.groupBox1.Controls.Add(this.textBoxStorageName); - this.groupBox1.Location = new System.Drawing.Point(7, 29); - this.groupBox1.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); - this.groupBox1.Name = "groupBox1"; - this.groupBox1.Padding = new System.Windows.Forms.Padding(3, 4, 3, 4); - this.groupBox1.Size = new System.Drawing.Size(216, 395); - this.groupBox1.TabIndex = 5; - this.groupBox1.TabStop = false; - this.groupBox1.Text = "Наборы"; + groupBox1.Controls.Add(listBoxStorage); + groupBox1.Controls.Add(ButtonAddObject); + groupBox1.Controls.Add(ButtonRemoveObject); + groupBox1.Controls.Add(textBoxStorageName); + groupBox1.Location = new Point(7, 29); + groupBox1.Margin = new Padding(3, 4, 3, 4); + groupBox1.Name = "groupBox1"; + groupBox1.Padding = new Padding(3, 4, 3, 4); + groupBox1.Size = new Size(216, 395); + groupBox1.TabIndex = 5; + groupBox1.TabStop = false; + groupBox1.Text = "Наборы"; // // listBoxStorage // - this.listBoxStorage.FormattingEnabled = true; - this.listBoxStorage.ItemHeight = 20; - this.listBoxStorage.Location = new System.Drawing.Point(31, 163); - this.listBoxStorage.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); - this.listBoxStorage.Name = "listBoxStorage"; - this.listBoxStorage.Size = new System.Drawing.Size(149, 124); - this.listBoxStorage.TabIndex = 9; - this.listBoxStorage.SelectedIndexChanged += new System.EventHandler(this.listBoxStorage_SelectedIndexChanged); + listBoxStorage.FormattingEnabled = true; + listBoxStorage.ItemHeight = 20; + listBoxStorage.Location = new Point(31, 163); + listBoxStorage.Margin = new Padding(3, 4, 3, 4); + listBoxStorage.Name = "listBoxStorage"; + listBoxStorage.Size = new Size(149, 124); + listBoxStorage.TabIndex = 9; + listBoxStorage.SelectedIndexChanged += listBoxStorage_SelectedIndexChanged; // // ButtonAddObject // - this.ButtonAddObject.Anchor = System.Windows.Forms.AnchorStyles.Top; - this.ButtonAddObject.Font = new System.Drawing.Font("Candara Light", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); - this.ButtonAddObject.Location = new System.Drawing.Point(31, 96); - this.ButtonAddObject.Name = "ButtonAddObject"; - this.ButtonAddObject.Size = new System.Drawing.Size(150, 39); - this.ButtonAddObject.TabIndex = 7; - this.ButtonAddObject.Text = "Добавить набор"; - this.ButtonAddObject.UseVisualStyleBackColor = true; - this.ButtonAddObject.Click += new System.EventHandler(this.ButtonAddObject_Click); + ButtonAddObject.Anchor = AnchorStyles.Top; + ButtonAddObject.Font = new Font("Candara Light", 9F, FontStyle.Regular, GraphicsUnit.Point); + ButtonAddObject.Location = new Point(31, 96); + ButtonAddObject.Name = "ButtonAddObject"; + ButtonAddObject.Size = new Size(150, 39); + ButtonAddObject.TabIndex = 7; + ButtonAddObject.Text = "Добавить набор"; + ButtonAddObject.UseVisualStyleBackColor = true; + ButtonAddObject.Click += ButtonAddObject_Click; // // ButtonRemoveObject // - this.ButtonRemoveObject.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.ButtonRemoveObject.Font = new System.Drawing.Font("Candara Light", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); - this.ButtonRemoveObject.Location = new System.Drawing.Point(31, 325); - this.ButtonRemoveObject.Name = "ButtonRemoveObject"; - this.ButtonRemoveObject.Size = new System.Drawing.Size(150, 44); - this.ButtonRemoveObject.TabIndex = 8; - this.ButtonRemoveObject.Text = "Удалить набор"; - this.ButtonRemoveObject.UseVisualStyleBackColor = true; - this.ButtonRemoveObject.Click += new System.EventHandler(this.ButtonRemoveObject_Click); + ButtonRemoveObject.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; + ButtonRemoveObject.Font = new Font("Candara Light", 9F, FontStyle.Regular, GraphicsUnit.Point); + ButtonRemoveObject.Location = new Point(31, 325); + ButtonRemoveObject.Name = "ButtonRemoveObject"; + ButtonRemoveObject.Size = new Size(150, 44); + ButtonRemoveObject.TabIndex = 8; + ButtonRemoveObject.Text = "Удалить набор"; + ButtonRemoveObject.UseVisualStyleBackColor = true; + ButtonRemoveObject.Click += ButtonRemoveObject_Click; // // Instruments // - this.Instruments.Controls.Add(this.ButtonRefreshCollection); - this.Instruments.Controls.Add(this.groupBox1); - this.Instruments.Controls.Add(this.maskedTextBoxNumber); - this.Instruments.Controls.Add(this.ButtonAddLocomotive); - this.Instruments.Controls.Add(this.ButtonRemoveLocomotive); - this.Instruments.Location = new System.Drawing.Point(797, 5); - this.Instruments.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); - this.Instruments.Name = "Instruments"; - this.Instruments.Padding = new System.Windows.Forms.Padding(3, 4, 3, 4); - this.Instruments.Size = new System.Drawing.Size(229, 721); - this.Instruments.TabIndex = 6; - this.Instruments.TabStop = false; - this.Instruments.Text = "Инструменты"; + Instruments.Anchor = AnchorStyles.Right; + Instruments.Controls.Add(ButtonRefreshCollection); + Instruments.Controls.Add(groupBox1); + Instruments.Controls.Add(maskedTextBoxNumber); + Instruments.Controls.Add(ButtonAddLocomotive); + Instruments.Controls.Add(ButtonRemoveLocomotive); + Instruments.Location = new Point(1102, 0); + Instruments.Margin = new Padding(3, 4, 3, 4); + Instruments.Name = "Instruments"; + Instruments.Padding = new Padding(3, 4, 3, 4); + Instruments.Size = new Size(236, 719); + Instruments.TabIndex = 6; + Instruments.TabStop = false; + Instruments.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(1338, 28); + menuStrip1.TabIndex = 7; + menuStrip1.Text = "menuStrip1"; + // + // файлToolStripMenuItem + // + файлToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { SaveToolStripMenuItem, LoadToolStripMenuItem }); + файлToolStripMenuItem.Name = "файлToolStripMenuItem"; + файлToolStripMenuItem.Size = new Size(59, 24); + файлToolStripMenuItem.Text = "Файл"; + // + // SaveToolStripMenuItem + // + SaveToolStripMenuItem.Name = "SaveToolStripMenuItem"; + SaveToolStripMenuItem.Size = new Size(166, 26); + SaveToolStripMenuItem.Text = "Сохранить"; + SaveToolStripMenuItem.Click += SaveToolStripMenuItem_Click; + // + // LoadToolStripMenuItem + // + LoadToolStripMenuItem.Name = "LoadToolStripMenuItem"; + LoadToolStripMenuItem.Size = new Size(166, 26); + LoadToolStripMenuItem.Text = "Загрузить"; + LoadToolStripMenuItem.Click += LoadToolStripMenuItem_Click; + // + // saveFileDialog + // + saveFileDialog.Filter = "txt file | *.txt"; + // + // openFileDialog + // + openFileDialog.FileName = "openFileDialog1"; + openFileDialog.Filter = "txt file | *.txt"; // // FormLocomotiveCollections // - this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); - this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(1033, 724); - this.Controls.Add(this.Instruments); - this.Controls.Add(this.pictureBoxCollections); - this.Name = "FormLocomotiveCollections"; - this.Text = "Набор локомотивов"; - ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollections)).EndInit(); - ((System.ComponentModel.ISupportInitialize)(this.bindingSource1)).EndInit(); - ((System.ComponentModel.ISupportInitialize)(this.bindingSource2)).EndInit(); - this.groupBox1.ResumeLayout(false); - this.groupBox1.PerformLayout(); - this.Instruments.ResumeLayout(false); - this.Instruments.PerformLayout(); - this.ResumeLayout(false); - + AutoScaleDimensions = new SizeF(8F, 20F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(1338, 724); + Controls.Add(Instruments); + Controls.Add(pictureBoxCollections); + Controls.Add(menuStrip1); + MainMenuStrip = menuStrip1; + Name = "FormLocomotiveCollections"; + Text = "Набор локомотивов"; + ((System.ComponentModel.ISupportInitialize)pictureBoxCollections).EndInit(); + ((System.ComponentModel.ISupportInitialize)bindingSource1).EndInit(); + ((System.ComponentModel.ISupportInitialize)bindingSource2).EndInit(); + groupBox1.ResumeLayout(false); + groupBox1.PerformLayout(); + Instruments.ResumeLayout(false); + Instruments.PerformLayout(); + menuStrip1.ResumeLayout(false); + menuStrip1.PerformLayout(); + ResumeLayout(false); + PerformLayout(); } #endregion @@ -213,5 +262,11 @@ private Button ButtonAddObject; private Button ButtonRemoveObject; private GroupBox Instruments; + private MenuStrip menuStrip1; + private ToolStripMenuItem файлToolStripMenuItem; + private ToolStripMenuItem SaveToolStripMenuItem; + private ToolStripMenuItem LoadToolStripMenuItem; + private SaveFileDialog saveFileDialog; + private OpenFileDialog openFileDialog; } } \ No newline at end of file diff --git a/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveCollections.cs b/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveCollections.cs index 7d4cae9..d2a4f71 100644 --- a/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveCollections.cs +++ b/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveCollections.cs @@ -143,5 +143,48 @@ namespace ProjectElectricLocomotive } pictureBoxCollections.Image = obj.ShowLocomotives(); } + /// + /// Обработка нажатия "Сохранение" + /// + /// + /// + 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) + { + // TODO продумать логику + if(openFileDialog.ShowDialog() == DialogResult.OK) + { + if (_storage.LoadData(openFileDialog.FileName)) + { + MessageBox.Show("Load is done!", "Result", MessageBoxButtons.OK, MessageBoxIcon.Information); + ReloadObjects(); + } + else + { + MessageBox.Show("Load is fail!", "Result", MessageBoxButtons.OK, MessageBoxIcon.Error); + + } + } + } } } diff --git a/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveCollections.resx b/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveCollections.resx index 4d89b60..f2c8442 100644 --- a/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveCollections.resx +++ b/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveCollections.resx @@ -1,4 +1,64 @@ - + + + @@ -63,7 +123,16 @@ 154, 17 + + 318, 17 + + + 453, 17 + + + 615, 17 + - 25 + 84 \ No newline at end of file diff --git a/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveConfig.Designer.cs b/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveConfig.Designer.cs index b305860..9f8b1fa 100644 --- a/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveConfig.Designer.cs +++ b/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveConfig.Designer.cs @@ -205,7 +205,7 @@ // panelPastelViolet // this.panelPastelViolet.AllowDrop = true; - this.panelPastelViolet.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(192)))), ((int)(((byte)(255))))); + this.panelPastelViolet.BackColor = System.Drawing.Color.Plum; this.panelPastelViolet.Location = new System.Drawing.Point(281, 110); this.panelPastelViolet.Name = "panelPastelViolet"; this.panelPastelViolet.Size = new System.Drawing.Size(40, 40); @@ -214,7 +214,7 @@ // panelPastelLilac // this.panelPastelLilac.AllowDrop = true; - this.panelPastelLilac.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(192)))), ((int)(((byte)(255))))); + this.panelPastelLilac.BackColor = System.Drawing.Color.RoyalBlue; this.panelPastelLilac.Location = new System.Drawing.Point(207, 110); this.panelPastelLilac.Name = "panelPastelLilac"; this.panelPastelLilac.Size = new System.Drawing.Size(40, 40); @@ -223,7 +223,7 @@ // panelSkyBlue // this.panelSkyBlue.AllowDrop = true; - this.panelSkyBlue.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(255)))), ((int)(((byte)(255))))); + this.panelSkyBlue.BackColor = System.Drawing.Color.Turquoise; this.panelSkyBlue.Location = new System.Drawing.Point(132, 110); this.panelSkyBlue.Name = "panelSkyBlue"; this.panelSkyBlue.Size = new System.Drawing.Size(40, 40); @@ -232,7 +232,7 @@ // panelPastelGreen // this.panelPastelGreen.AllowDrop = true; - this.panelPastelGreen.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(255)))), ((int)(((byte)(192))))); + this.panelPastelGreen.BackColor = System.Drawing.Color.PaleGreen; this.panelPastelGreen.Location = new System.Drawing.Point(55, 110); this.panelPastelGreen.Name = "panelPastelGreen"; this.panelPastelGreen.Size = new System.Drawing.Size(40, 40); @@ -241,7 +241,7 @@ // panelPastelYellow // this.panelPastelYellow.AllowDrop = true; - this.panelPastelYellow.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(255)))), ((int)(((byte)(192))))); + this.panelPastelYellow.BackColor = System.Drawing.Color.LightYellow; this.panelPastelYellow.Location = new System.Drawing.Point(281, 35); this.panelPastelYellow.Name = "panelPastelYellow"; this.panelPastelYellow.Size = new System.Drawing.Size(40, 40); @@ -250,7 +250,7 @@ // panelPastelOrange // this.panelPastelOrange.AllowDrop = true; - this.panelPastelOrange.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(224)))), ((int)(((byte)(192))))); + this.panelPastelOrange.BackColor = System.Drawing.Color.PeachPuff; this.panelPastelOrange.Location = new System.Drawing.Point(207, 35); this.panelPastelOrange.Name = "panelPastelOrange"; this.panelPastelOrange.Size = new System.Drawing.Size(40, 40); @@ -259,7 +259,7 @@ // panelPastelPink // this.panelPastelPink.AllowDrop = true; - this.panelPastelPink.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(192)))), ((int)(((byte)(192))))); + this.panelPastelPink.BackColor = System.Drawing.Color.LightSalmon; this.panelPastelPink.Location = new System.Drawing.Point(132, 35); this.panelPastelPink.Name = "panelPastelPink"; this.panelPastelPink.Size = new System.Drawing.Size(40, 40); @@ -268,7 +268,7 @@ // panelPastelRed // this.panelPastelRed.AllowDrop = true; - this.panelPastelRed.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(128)))), ((int)(((byte)(128))))); + this.panelPastelRed.BackColor = System.Drawing.Color.Red; this.panelPastelRed.Location = new System.Drawing.Point(55, 35); this.panelPastelRed.Name = "panelPastelRed"; this.panelPastelRed.Size = new System.Drawing.Size(40, 40); @@ -330,7 +330,7 @@ this.ButtonOk.TabIndex = 2; this.ButtonOk.Text = "Добавить"; this.ButtonOk.UseVisualStyleBackColor = true; - // this.ButtonOk.Click += new System.EventHandler(this.ButtonOk_Click); + this.ButtonOk.Click += new System.EventHandler(this.ButtonOk_Click); // // buttonCancel // diff --git a/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveConfig.cs b/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveConfig.cs index ac78182..d1bd132 100644 --- a/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveConfig.cs +++ b/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveConfig.cs @@ -170,8 +170,6 @@ namespace ProjectElectricLocomotive } } - // TODO Реализовать логику смены цветов: основного и дополнительного (для продвинутого объекта) - /// /// Добавление loco /// diff --git a/ProjectElectricLocomotive/ProjectElectricLocomotive/LocomotiveGenericCollection.cs b/ProjectElectricLocomotive/ProjectElectricLocomotive/LocomotiveGenericCollection.cs index ecb9eaf..fcbb498 100644 --- a/ProjectElectricLocomotive/ProjectElectricLocomotive/LocomotiveGenericCollection.cs +++ b/ProjectElectricLocomotive/ProjectElectricLocomotive/LocomotiveGenericCollection.cs @@ -10,6 +10,12 @@ namespace ProjectElectricLocomotive.Generics { internal class LocomotiveGenericCollection where T : DrawingLocomotive where U : IMoveableObject { + /// + /// Получение объектов коллекции + /// + public IEnumerable GetLocomotives => _collection.GetLocomotives(); + + //ширина/высота окна private readonly int _pictureWidth; private readonly int _pictureHeight; diff --git a/ProjectElectricLocomotive/ProjectElectricLocomotive/LocomotiveGenericStorage.cs b/ProjectElectricLocomotive/ProjectElectricLocomotive/LocomotiveGenericStorage.cs index 63704a9..100df98 100644 --- a/ProjectElectricLocomotive/ProjectElectricLocomotive/LocomotiveGenericStorage.cs +++ b/ProjectElectricLocomotive/ProjectElectricLocomotive/LocomotiveGenericStorage.cs @@ -20,9 +20,9 @@ namespace ProjectElectricLocomotive.Generics /// public List Keys => _locomotivesStorage.Keys.ToList(); - + private readonly int _pictureWidth; - + private readonly int _pictureHeight; /// @@ -78,5 +78,114 @@ namespace ProjectElectricLocomotive.Generics return null; } } + + /// + /// Разделитель для записи ключа и значения элемента словаря + /// + private static readonly char _separatorForKeyValue = '|'; + /// + /// Разделитель для записей коллекции данных в файл + /// + private readonly char _separatorRecords = ';'; + /// + /// Разделитель для записи информации по объекту в файл + /// + private static readonly char _separatorForObject = ':'; + + /// + /// Сохранение информации по автомобилям в хранилище в файл + /// + /// Путь и имя файла + /// true - сохранение прошло успешно, false - ошибка при сохранении данных + public bool SaveData(string filename) + { + if (File.Exists(filename)) + { + File.Delete(filename); + } + StringBuilder data = new(); + foreach (KeyValuePair> record in _locomotivesStorage) + { + StringBuilder records = new(); + foreach (DrawingLocomotive? elem in record.Value.GetLocomotives) + { + records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}"); + } + data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}"); + } + if (data.Length == 0) + { + return false; + } + using StreamWriter fs = new StreamWriter(filename); + { + fs.WriteLine($"LocomotiveStorage{Environment.NewLine}"); + fs.WriteLine(data); + } + return true; + } + /// + /// Загрузка информации по автомобилям в хранилище из файла + /// + /// Путь и имя файла + /// true - загрузка прошла успешно, false - ошибка при загрузке данных + public bool LoadData(string filename) + { + if (!File.Exists(filename)) + { + return false; + } + + using (StreamReader fs = File.OpenText(filename)) + { + + string str = fs.ReadLine(); + + if (str == null || str.Length == 0) + { + return false; + } + + if (!str.StartsWith("LocomotiveStorage")) + { + //если нет такой записи, то это не те данные + return false; + } + + _locomotivesStorage.Clear(); + string strs = ""; + + + while ((strs = fs.ReadLine()) != null) + { + + if (strs == null) + { + return false; + } + + string[] record = strs.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries); + if (record.Length != 2) + { + continue; + } + LocomotiveGenericCollection collection = new(_pictureWidth, _pictureHeight); + string[] set = record[1].Split(_separatorRecords, StringSplitOptions.RemoveEmptyEntries); + foreach (string elem in set) + { + DrawingLocomotive? loco = elem?.CreateDrawingLocomotive(_separatorForObject, _pictureWidth, _pictureHeight); + if (loco != null) + { + if ((collection + loco) == -1) // for my realization it's -1, for eegov's realization it's boolean + { + return false; + } + } + } + _locomotivesStorage.Add(record[0], collection); + } + return true; + } + } } }