diff --git a/ElectricLocomotive/ElectricLocomotive/DrawningObjectLocomotive.cs b/ElectricLocomotive/ElectricLocomotive/DrawningObjectLocomotive.cs index 53378eb..ac8d69b 100644 --- a/ElectricLocomotive/ElectricLocomotive/DrawningObjectLocomotive.cs +++ b/ElectricLocomotive/ElectricLocomotive/DrawningObjectLocomotive.cs @@ -14,25 +14,23 @@ namespace ElectricLocomotive _locomotive = locomotive; } public float Step => _locomotive?.Locomotive?.Step ?? 0; - public (float Left, float Right, float Top, float Bottom) GetCurrentPosition() { return _locomotive?.GetCurrentPosition() ?? default; } - public void MoveObject(Direction direction) { _locomotive?.MoveTransport(direction); } - public void SetObject(int x, int y, int width, int height) { _locomotive.SetPosition(x, y, width, height); } - void IDrawningObject.DrawningObject(Graphics g) { _locomotive.DrawTransport(g); } + public string GetInfo() => _locomotive?.GetDataForSave(); + public static IDrawningObject Create(string data) => new DrawningObjectLocomotive(data.CreateDrawningCar()); } } diff --git a/ElectricLocomotive/ElectricLocomotive/ElectricLocomotive - Backup.csproj b/ElectricLocomotive/ElectricLocomotive/ElectricLocomotive - Backup.csproj new file mode 100644 index 0000000..13ee123 --- /dev/null +++ b/ElectricLocomotive/ElectricLocomotive/ElectricLocomotive - Backup.csproj @@ -0,0 +1,26 @@ + + + + WinExe + net6.0-windows + enable + true + enable + + + + + True + True + Resources.resx + + + + + + ResXFileCodeGenerator + Resources.Designer.cs + + + + \ No newline at end of file diff --git a/ElectricLocomotive/ElectricLocomotive/ElectricLocomotive.csproj b/ElectricLocomotive/ElectricLocomotive/ElectricLocomotive.csproj index 13ee123..7d7c710 100644 --- a/ElectricLocomotive/ElectricLocomotive/ElectricLocomotive.csproj +++ b/ElectricLocomotive/ElectricLocomotive/ElectricLocomotive.csproj @@ -8,6 +8,11 @@ enable + + + + + True diff --git a/ElectricLocomotive/ElectricLocomotive/ExtentionLocomotive.cs b/ElectricLocomotive/ElectricLocomotive/ExtentionLocomotive.cs new file mode 100644 index 0000000..499c531 --- /dev/null +++ b/ElectricLocomotive/ElectricLocomotive/ExtentionLocomotive.cs @@ -0,0 +1,38 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ElectricLocomotive +{ + internal static class ExtentionLocomotive + { + private static readonly char _separatorForObject = ':'; + public static DrawningLocomotive CreateDrawningCar(this string info) + { + string[] strs = info.Split(_separatorForObject); + if (strs.Length == 3) + { + return new DrawningLocomotive(Convert.ToInt32(strs[0]), + Convert.ToInt32(strs[1]), Color.FromName(strs[2])); + } + if (strs.Length == 6) + { + return new DrawningElectricLocomotive(Convert.ToInt32(strs[0]),Convert.ToInt32(strs[1]), Color.FromName(strs[2]),Color.FromName(strs[3]), Convert.ToBoolean(strs[4]), Convert.ToBoolean(strs[5])); + } + return null; + } + public static string GetDataForSave(this DrawningLocomotive drawningCar) + { + var car = drawningCar.Locomotive; + var str = $"{car.Speed}{_separatorForObject}{car.Weight}{_separatorForObject}{car.BodyColor.Name}"; + if (car is not EntityElectricLocomotive sportCar) + { + return str; + } + return + $"{str}{_separatorForObject}{sportCar.DopColor.Name}{_separatorForObject}{sportCar.BodyKit}{_separatorForObject}{sportCar.SportLine}"; + } + } +} diff --git a/ElectricLocomotive/ElectricLocomotive/FormLocomotive.Designer.cs b/ElectricLocomotive/ElectricLocomotive/FormLocomotive.Designer.cs index 4ac4aa2..5838d0d 100644 --- a/ElectricLocomotive/ElectricLocomotive/FormLocomotive.Designer.cs +++ b/ElectricLocomotive/ElectricLocomotive/FormLocomotive.Designer.cs @@ -29,10 +29,6 @@ private void InitializeComponent() { this.ButtonCreate = new System.Windows.Forms.Button(); - this.statusStrip1 = new System.Windows.Forms.StatusStrip(); - this.toolStripStatusLabelSpeed = new System.Windows.Forms.ToolStripStatusLabel(); - this.toolStripStatusLabelWeight = new System.Windows.Forms.ToolStripStatusLabel(); - this.toolStripStatusLabelColor = new System.Windows.Forms.ToolStripStatusLabel(); this.pictureBox1 = new System.Windows.Forms.PictureBox(); this.buttonLeft = new System.Windows.Forms.Button(); this.buttonDown = new System.Windows.Forms.Button(); @@ -40,8 +36,12 @@ this.buttonUp = new System.Windows.Forms.Button(); this.buttonCreateModif = new System.Windows.Forms.Button(); this.buttonselectlocomotive = new System.Windows.Forms.Button(); - this.statusStrip1.SuspendLayout(); + this.toolStripStatusLabelSpeed = new System.Windows.Forms.ToolStripStatusLabel(); + this.toolStripStatusLabelWeight = new System.Windows.Forms.ToolStripStatusLabel(); + this.toolStripStatusLabelColor = new System.Windows.Forms.ToolStripStatusLabel(); + this.statusStrip1 = new System.Windows.Forms.StatusStrip(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit(); + this.statusStrip1.SuspendLayout(); this.SuspendLayout(); // // ButtonCreate @@ -56,36 +56,6 @@ this.ButtonCreate.UseVisualStyleBackColor = true; this.ButtonCreate.Click += new System.EventHandler(this.ButtonCreate_Click); // - // statusStrip1 - // - this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { - this.toolStripStatusLabelSpeed, - this.toolStripStatusLabelWeight, - this.toolStripStatusLabelColor}); - this.statusStrip1.Location = new System.Drawing.Point(0, 428); - this.statusStrip1.Name = "statusStrip1"; - this.statusStrip1.Size = new System.Drawing.Size(800, 22); - this.statusStrip1.TabIndex = 1; - this.statusStrip1.Text = "statusStrip1"; - // - // toolStripStatusLabelSpeed - // - this.toolStripStatusLabelSpeed.Name = "toolStripStatusLabelSpeed"; - this.toolStripStatusLabelSpeed.Size = new System.Drawing.Size(59, 17); - this.toolStripStatusLabelSpeed.Text = "Скорость"; - // - // toolStripStatusLabelWeight - // - this.toolStripStatusLabelWeight.Name = "toolStripStatusLabelWeight"; - this.toolStripStatusLabelWeight.Size = new System.Drawing.Size(26, 17); - this.toolStripStatusLabelWeight.Text = "Вес"; - // - // toolStripStatusLabelColor - // - this.toolStripStatusLabelColor.Name = "toolStripStatusLabelColor"; - this.toolStripStatusLabelColor.Size = new System.Drawing.Size(33, 17); - this.toolStripStatusLabelColor.Text = "Цвет"; - // // pictureBox1 // this.pictureBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) @@ -170,6 +140,36 @@ this.buttonselectlocomotive.UseVisualStyleBackColor = true; this.buttonselectlocomotive.Click += new System.EventHandler(this.ButtonSelectLocomotive_Click); // + // toolStripStatusLabelSpeed + // + this.toolStripStatusLabelSpeed.Name = "toolStripStatusLabelSpeed"; + this.toolStripStatusLabelSpeed.Size = new System.Drawing.Size(59, 17); + this.toolStripStatusLabelSpeed.Text = "Скорость"; + // + // toolStripStatusLabelWeight + // + this.toolStripStatusLabelWeight.Name = "toolStripStatusLabelWeight"; + this.toolStripStatusLabelWeight.Size = new System.Drawing.Size(26, 17); + this.toolStripStatusLabelWeight.Text = "Вес"; + // + // toolStripStatusLabelColor + // + this.toolStripStatusLabelColor.Name = "toolStripStatusLabelColor"; + this.toolStripStatusLabelColor.Size = new System.Drawing.Size(33, 17); + this.toolStripStatusLabelColor.Text = "Цвет"; + // + // statusStrip1 + // + this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.toolStripStatusLabelSpeed, + this.toolStripStatusLabelWeight, + this.toolStripStatusLabelColor}); + this.statusStrip1.Location = new System.Drawing.Point(0, 428); + this.statusStrip1.Name = "statusStrip1"; + this.statusStrip1.Size = new System.Drawing.Size(800, 22); + this.statusStrip1.TabIndex = 1; + this.statusStrip1.Text = "statusStrip1"; + // // FormLocomotive // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); @@ -187,9 +187,9 @@ this.Name = "FormLocomotive"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "Локомотив"; + ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit(); this.statusStrip1.ResumeLayout(false); this.statusStrip1.PerformLayout(); - ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit(); this.ResumeLayout(false); this.PerformLayout(); @@ -198,10 +198,6 @@ #endregion private Button ButtonCreate; - private StatusStrip statusStrip1; - private ToolStripStatusLabel toolStripStatusLabelSpeed; - private ToolStripStatusLabel toolStripStatusLabelWeight; - private ToolStripStatusLabel toolStripStatusLabelColor; private PictureBox pictureBox1; private Button buttonLeft; private Button buttonDown; @@ -209,5 +205,9 @@ private Button buttonUp; private Button buttonCreateModif; private Button buttonselectlocomotive; + private ToolStripStatusLabel toolStripStatusLabelSpeed; + private ToolStripStatusLabel toolStripStatusLabelWeight; + private ToolStripStatusLabel toolStripStatusLabelColor; + private StatusStrip statusStrip1; } } \ No newline at end of file diff --git a/ElectricLocomotive/ElectricLocomotive/FormMapWithSetLocomotive.Designer.cs b/ElectricLocomotive/ElectricLocomotive/FormMapWithSetLocomotive.Designer.cs index ef3926a..65cbcb9 100644 --- a/ElectricLocomotive/ElectricLocomotive/FormMapWithSetLocomotive.Designer.cs +++ b/ElectricLocomotive/ElectricLocomotive/FormMapWithSetLocomotive.Designer.cs @@ -44,7 +44,14 @@ this.ListBoxMaps = new System.Windows.Forms.ListBox(); this.ButtonAddMap = new System.Windows.Forms.Button(); this.textBoxNewMapName = new System.Windows.Forms.TextBox(); + this.menuStrip1 = new System.Windows.Forms.MenuStrip(); + this.файлToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.SaveToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.LoadToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.openFileDialog = new System.Windows.Forms.OpenFileDialog(); + this.saveFileDialog = new System.Windows.Forms.SaveFileDialog(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit(); + this.menuStrip1.SuspendLayout(); this.SuspendLayout(); // // pictureBox1 @@ -113,7 +120,7 @@ // label1 // this.label1.AutoSize = true; - this.label1.Location = new System.Drawing.Point(837, 19); + this.label1.Location = new System.Drawing.Point(837, 29); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(83, 15); this.label1.TabIndex = 15; @@ -219,11 +226,53 @@ this.textBoxNewMapName.Size = new System.Drawing.Size(151, 23); this.textBoxNewMapName.TabIndex = 30; // + // menuStrip1 + // + this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.файлToolStripMenuItem}); + this.menuStrip1.Location = new System.Drawing.Point(0, 0); + this.menuStrip1.Name = "menuStrip1"; + this.menuStrip1.Size = new System.Drawing.Size(1000, 24); + this.menuStrip1.TabIndex = 31; + this.menuStrip1.Text = "menuStrip1"; + // + // файлToolStripMenuItem + // + this.файлToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.SaveToolStripMenuItem, + this.LoadToolStripMenuItem}); + this.файлToolStripMenuItem.Name = "файлToolStripMenuItem"; + this.файлToolStripMenuItem.Size = new System.Drawing.Size(48, 20); + this.файлToolStripMenuItem.Text = "Файл"; + // + // SaveToolStripMenuItem + // + this.SaveToolStripMenuItem.Name = "SaveToolStripMenuItem"; + this.SaveToolStripMenuItem.Size = new System.Drawing.Size(141, 22); + this.SaveToolStripMenuItem.Text = "Сохранение"; + this.SaveToolStripMenuItem.Click += new System.EventHandler(this.SaveToolStripMenuItem_Click); + // + // LoadToolStripMenuItem + // + this.LoadToolStripMenuItem.Name = "LoadToolStripMenuItem"; + this.LoadToolStripMenuItem.Size = new System.Drawing.Size(141, 22); + this.LoadToolStripMenuItem.Text = "Загрузка"; + this.LoadToolStripMenuItem.Click += new System.EventHandler(this.LoadToolStripMenuItem_Click); + // + // openFileDialog + // + this.openFileDialog.FileName = "openFileDialog1"; + this.openFileDialog.Filter = "txt file | *.txt"; + // + // saveFileDialog + this.saveFileDialog.Filter = "txt file | *.txt"; + // // FormMapWithSetLocomotive // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(1000, 722); + this.Controls.Add(this.menuStrip1); this.Controls.Add(this.textBoxNewMapName); this.Controls.Add(this.ButtonAddMap); this.Controls.Add(this.ListBoxMaps); @@ -240,9 +289,12 @@ this.Controls.Add(this.buttonDown); this.Controls.Add(this.buttonLeft); this.Controls.Add(this.pictureBox1); + this.MainMenuStrip = this.menuStrip1; this.Name = "FormMapWithSetLocomotive"; this.Text = "Карта с набором объектов"; ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit(); + this.menuStrip1.ResumeLayout(false); + this.menuStrip1.PerformLayout(); this.ResumeLayout(false); this.PerformLayout(); @@ -265,5 +317,11 @@ private ListBox ListBoxMaps; private Button ButtonAddMap; private TextBox textBoxNewMapName; + private MenuStrip menuStrip1; + private ToolStripMenuItem файлToolStripMenuItem; + private ToolStripMenuItem SaveToolStripMenuItem; + private ToolStripMenuItem LoadToolStripMenuItem; + private OpenFileDialog openFileDialog; + private SaveFileDialog saveFileDialog; } } \ No newline at end of file diff --git a/ElectricLocomotive/ElectricLocomotive/FormMapWithSetLocomotive.cs b/ElectricLocomotive/ElectricLocomotive/FormMapWithSetLocomotive.cs index 09199d9..8215527 100644 --- a/ElectricLocomotive/ElectricLocomotive/FormMapWithSetLocomotive.cs +++ b/ElectricLocomotive/ElectricLocomotive/FormMapWithSetLocomotive.cs @@ -197,6 +197,39 @@ namespace ElectricLocomotive ReloadMaps(); } } + + private void SaveToolStripMenuItem_Click(object sender, EventArgs e) + { + if (saveFileDialog.ShowDialog() == DialogResult.OK) + { + if (_mapsCollection.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 (_mapsCollection.LoadData(openFileDialog.FileName)) + { + ReloadMaps(); + MessageBox.Show("Загрузка прошла успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); + } + else + { + MessageBox.Show("Не загрузилось", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } } } diff --git a/ElectricLocomotive/ElectricLocomotive/FormMapWithSetLocomotive.resx b/ElectricLocomotive/ElectricLocomotive/FormMapWithSetLocomotive.resx index f298a7b..4e658d5 100644 --- a/ElectricLocomotive/ElectricLocomotive/FormMapWithSetLocomotive.resx +++ b/ElectricLocomotive/ElectricLocomotive/FormMapWithSetLocomotive.resx @@ -57,4 +57,13 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + 279, 17 + + + 146, 17 + + + 17, 17 + \ No newline at end of file diff --git a/ElectricLocomotive/ElectricLocomotive/IDrawningObject.cs b/ElectricLocomotive/ElectricLocomotive/IDrawningObject.cs index 651fedd..70ae7c7 100644 --- a/ElectricLocomotive/ElectricLocomotive/IDrawningObject.cs +++ b/ElectricLocomotive/ElectricLocomotive/IDrawningObject.cs @@ -13,5 +13,6 @@ namespace ElectricLocomotive void MoveObject(Direction direction); void DrawningObject(Graphics g); (float Left, float Right, float Top, float Bottom) GetCurrentPosition(); + string GetInfo(); } } diff --git a/ElectricLocomotive/ElectricLocomotive/MapWithSetLocomotivGeneric.cs b/ElectricLocomotive/ElectricLocomotive/MapWithSetLocomotivGeneric.cs index 70abd9c..0da7333 100644 --- a/ElectricLocomotive/ElectricLocomotive/MapWithSetLocomotivGeneric.cs +++ b/ElectricLocomotive/ElectricLocomotive/MapWithSetLocomotivGeneric.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace ElectricLocomotive +namespace ElectricLocomotive { internal class MapWithSetLocomotivGeneric where T : class, IDrawningObject @@ -138,5 +132,21 @@ namespace ElectricLocomotive } } + public string GetData(char separatorType, char separatorData) + { + string data = $"{_map.GetType().Name}{separatorType}"; + foreach (var boat in _setLocomotive.GetLocomotive()) + { + data += $"{boat.GetInfo()}{separatorData}"; + } + return data; + } + public void LoadData(string[] records) + { + foreach (var rec in records) + { + _setLocomotive.Insert(DrawningObjectLocomotive.Create(rec) as T); + } + } } } \ No newline at end of file diff --git a/ElectricLocomotive/ElectricLocomotive/MapsCollection.cs b/ElectricLocomotive/ElectricLocomotive/MapsCollection.cs index a0e9286..34b5b94 100644 --- a/ElectricLocomotive/ElectricLocomotive/MapsCollection.cs +++ b/ElectricLocomotive/ElectricLocomotive/MapsCollection.cs @@ -8,13 +8,16 @@ namespace ElectricLocomotive { internal class MapsCollection { - readonly Dictionary> _mapStorages; + readonly Dictionary> _mapStorages; public List Keys => _mapStorages.Keys.ToList(); private readonly int _pictureWidth; private readonly int _pictureHeight; + + private readonly char separatorDict = '|'; + private readonly char separatorData = ';'; public MapsCollection(int pictureWidth, int pictureHeight) { - _mapStorages = new Dictionary>(); + _mapStorages = new Dictionary>(); _pictureWidth = pictureWidth; _pictureHeight = pictureHeight; } @@ -22,14 +25,14 @@ namespace ElectricLocomotive { if (!_mapStorages.ContainsKey(name)) { - _mapStorages.Add(name, new MapWithSetLocomotivGeneric(_pictureWidth, _pictureHeight, map)); + _mapStorages.Add(name, new MapWithSetLocomotivGeneric(_pictureWidth, _pictureHeight, map)); } } public void DelMap(string name) { if (_mapStorages.ContainsKey(name)) _mapStorages.Remove(name); } - public MapWithSetLocomotivGeneric this[string ind] + public MapWithSetLocomotivGeneric this[string ind] { get { @@ -40,5 +43,66 @@ namespace ElectricLocomotive return null; } } + + private static void WriteToFile(string text, FileStream stream) + { + byte[] info = new UTF8Encoding(true).GetBytes(text); + stream.Write(info, 0, info.Length); + } + public bool SaveData(string filename) + { + if (File.Exists(filename)) + { + File.Delete(filename); + } + using (StreamWriter fs = new StreamWriter(filename)) + { + fs.Write($"MapsCollection{Environment.NewLine}"); + foreach (var storage in _mapStorages) + { + + fs.Write($"{storage.Key}{separatorDict}{storage.Value.GetData(separatorDict, separatorData)}{Environment.NewLine}"); + } + } + return true; + } + public bool LoadData(string filename) + { + if (!File.Exists(filename)) + { + return false; + } + string str = ""; + using (StreamReader fs = new StreamReader(filename)) + { + str = fs.ReadLine(); + if (!str.Contains("MapsCollection")) + { + //если нет такой записи, то это не те данные + return false; + } + _mapStorages.Clear(); + while ((str = fs.ReadLine()) != null) + { + var elem = str.Split(separatorDict); + AbstractMap map = null; + switch (elem[1]) + { + case "SimpleMap": + map = new SimpleMap(); + break; + case "BushesMap": + map = new BushesMap(); + break; + case "FieldMap": + map = new FieldMap(); + break; + } + _mapStorages.Add(elem[0], new MapWithSetLocomotivGeneric(_pictureWidth, _pictureHeight, map)); + _mapStorages[elem[0]].LoadData(elem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries)); + } + } + return true; + } } }