From 23a736f24fc51e19474e484f7280312647f84ec0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B5=D0=BB=D1=8C=D0=BD=D0=B8=D0=BA=D0=BE=D0=B2=20?= =?UTF-8?q?=D0=98=D0=B3=D0=BE=D1=80=D1=8C?= Date: Tue, 20 Dec 2022 20:34:52 +0400 Subject: [PATCH] =?UTF-8?q?=D1=83=D1=81=D0=BB=D0=BE=D0=B6=D0=BD=D0=B5?= =?UTF-8?q?=D0=BD=D0=BD=D0=B0=D1=8F=20=D1=87=D0=B0=D1=81=D1=82=D1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../FormMapWithSetLocomotives.Designer.cs | 30 ++++++++++- .../FormMapWithSetLocomotives.cs | 43 +++++++++++++++ .../LocomotivesAdvanced/MapsCollection.cs | 53 +++++++++++++++++++ 3 files changed, 124 insertions(+), 2 deletions(-) diff --git a/LocomotivesAdvanced/LocomotivesAdvanced/FormMapWithSetLocomotives.Designer.cs b/LocomotivesAdvanced/LocomotivesAdvanced/FormMapWithSetLocomotives.Designer.cs index 33c445f..78f7a2d 100644 --- a/LocomotivesAdvanced/LocomotivesAdvanced/FormMapWithSetLocomotives.Designer.cs +++ b/LocomotivesAdvanced/LocomotivesAdvanced/FormMapWithSetLocomotives.Designer.cs @@ -29,6 +29,7 @@ private void InitializeComponent() { this.groupBoxTools = new System.Windows.Forms.GroupBox(); + this.buttonSaveSelectedMap = new System.Windows.Forms.Button(); this.buttonShowLastRemovedObject = new System.Windows.Forms.Button(); this.groupBoxMaps = new System.Windows.Forms.GroupBox(); this.buttonDeleteMap = new System.Windows.Forms.Button(); @@ -52,6 +53,7 @@ this.LoadToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.saveFileDialog = new System.Windows.Forms.SaveFileDialog(); this.openFileDialog = new System.Windows.Forms.OpenFileDialog(); + this.buttonLoadMap = new System.Windows.Forms.Button(); this.groupBoxTools.SuspendLayout(); this.groupBoxMaps.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxLocomotives)).BeginInit(); @@ -60,6 +62,8 @@ // // groupBoxTools // + this.groupBoxTools.Controls.Add(this.buttonLoadMap); + this.groupBoxTools.Controls.Add(this.buttonSaveSelectedMap); this.groupBoxTools.Controls.Add(this.buttonShowLastRemovedObject); this.groupBoxTools.Controls.Add(this.groupBoxMaps); this.groupBoxTools.Controls.Add(this.buttonUp); @@ -79,6 +83,16 @@ this.groupBoxTools.TabStop = false; this.groupBoxTools.Text = "Инструменты"; // + // buttonSaveSelectedMap + // + this.buttonSaveSelectedMap.Location = new System.Drawing.Point(31, 588); + this.buttonSaveSelectedMap.Name = "buttonSaveSelectedMap"; + this.buttonSaveSelectedMap.Size = new System.Drawing.Size(164, 38); + this.buttonSaveSelectedMap.TabIndex = 13; + this.buttonSaveSelectedMap.Text = "Сохранить выбранную карту"; + this.buttonSaveSelectedMap.UseVisualStyleBackColor = true; + this.buttonSaveSelectedMap.Click += new System.EventHandler(this.ButtonSaveSelectedMap_Click); + // // buttonShowLastRemovedObject // this.buttonShowLastRemovedObject.Location = new System.Drawing.Point(31, 517); @@ -281,14 +295,14 @@ // SaveToolStripMenuItem // this.SaveToolStripMenuItem.Name = "SaveToolStripMenuItem"; - this.SaveToolStripMenuItem.Size = new System.Drawing.Size(180, 22); + 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(180, 22); + this.LoadToolStripMenuItem.Size = new System.Drawing.Size(141, 22); this.LoadToolStripMenuItem.Text = "Загрузка"; this.LoadToolStripMenuItem.Click += new System.EventHandler(this.LoadToolStripMenuItem_Click); // @@ -300,6 +314,16 @@ // this.openFileDialog.Filter = "txt file|*.txt"; // + // buttonLoadMap + // + this.buttonLoadMap.Location = new System.Drawing.Point(31, 632); + this.buttonLoadMap.Name = "buttonLoadMap"; + this.buttonLoadMap.Size = new System.Drawing.Size(164, 38); + this.buttonLoadMap.TabIndex = 14; + this.buttonLoadMap.Text = "Загрузить карту"; + this.buttonLoadMap.UseVisualStyleBackColor = true; + this.buttonLoadMap.Click += new System.EventHandler(this.ButtonLoadMap_Click); + // // FormMapWithSetLocomotives // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); @@ -348,5 +372,7 @@ private ToolStripMenuItem LoadToolStripMenuItem; private SaveFileDialog saveFileDialog; private OpenFileDialog openFileDialog; + private Button buttonSaveSelectedMap; + private Button buttonLoadMap; } } \ No newline at end of file diff --git a/LocomotivesAdvanced/LocomotivesAdvanced/FormMapWithSetLocomotives.cs b/LocomotivesAdvanced/LocomotivesAdvanced/FormMapWithSetLocomotives.cs index 56f0013..82d5da6 100644 --- a/LocomotivesAdvanced/LocomotivesAdvanced/FormMapWithSetLocomotives.cs +++ b/LocomotivesAdvanced/LocomotivesAdvanced/FormMapWithSetLocomotives.cs @@ -278,5 +278,48 @@ } } } + /// + /// Обработка нажатия "Сохранить выбранную карту" + /// + /// + /// + private void ButtonSaveSelectedMap_Click(object sender, EventArgs e) + { + if (saveFileDialog.ShowDialog() == DialogResult.OK) + { + if (_mapsCollection.SaveMap(saveFileDialog.FileName, listBoxMaps.SelectedItem?.ToString() ?? string.Empty)) + { + MessageBox.Show("Сохранение карты прошло успешно", "Результат", + MessageBoxButtons.OK, MessageBoxIcon.Information); + } + else + { + MessageBox.Show("Карта не сохранилась", "Результат", + MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } + /// + /// Обработка нажатия "Загрузить карту" + /// + /// + /// + private void ButtonLoadMap_Click(object sender, EventArgs e) + { + if (openFileDialog.ShowDialog() == DialogResult.OK) + { + if (_mapsCollection.LoadMap(openFileDialog.FileName)) + { + MessageBox.Show("Загрузка прошла успешно", "Результат", + MessageBoxButtons.OK, MessageBoxIcon.Information); + ReloadMaps(); + } + else + { + MessageBox.Show("Не удалось загрузить файл", "Результат", + MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } } } diff --git a/LocomotivesAdvanced/LocomotivesAdvanced/MapsCollection.cs b/LocomotivesAdvanced/LocomotivesAdvanced/MapsCollection.cs index e55e569..6ea6147 100644 --- a/LocomotivesAdvanced/LocomotivesAdvanced/MapsCollection.cs +++ b/LocomotivesAdvanced/LocomotivesAdvanced/MapsCollection.cs @@ -53,6 +53,59 @@ _mapStorages.Add(name, new MapWithSetLocomotivesGeneric(_pictureWidth, _pictureHeight, map)); } } + public bool SaveMap(string filename, string maptosave) + { + if (File.Exists(filename)) + { + File.Delete(filename); + } + using (StreamWriter sw = new(filename)) + { + sw.Write("Map\n"); + sw.Write(maptosave + "\n"); + sw.Write($"{_mapStorages[maptosave].GetData(separatorDict, separatorData)}"); + sw.Close(); + } + return true; + } + public bool LoadMap(string filename) + { + if (!File.Exists(filename)) + { + return false; + } + using (StreamReader sr = new(filename)) + { + string firstStr = sr.ReadLine(); + if (firstStr == null || !firstStr.Contains("Map")) + { + return false; + } + string mapName = sr.ReadLine(); + string mapInfo = sr.ReadLine(); + AbstractMap newMap = null; + var info = mapInfo.Split(separatorDict); + switch (info[0]) + { + case "SimpleMap": + newMap = new SimpleMap(); + break; + case "CrossMap": + newMap = new CrossMap(); + break; + case "RoadsMap": + newMap = new RoadsMap(); + break; + } + if (_mapStorages.ContainsKey(mapName)) + { + _mapStorages.Remove(mapName); + } + _mapStorages.Add(mapName, new MapWithSetLocomotivesGeneric(_pictureWidth, _pictureHeight, newMap)); + _mapStorages[mapName].LoadData(info[1].Split(separatorData, StringSplitOptions.RemoveEmptyEntries)); + } + return true; + } /// /// Удаление карты ///