усложненная часть

This commit is contained in:
Мельников Игорь 2022-12-20 20:34:52 +04:00
parent 065dd3cc40
commit 23a736f24f
3 changed files with 124 additions and 2 deletions

View File

@ -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;
}
}

View File

@ -278,5 +278,48 @@
}
}
}
/// <summary>
/// Обработка нажатия "Сохранить выбранную карту"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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);
}
}
}
/// <summary>
/// Обработка нажатия "Загрузить карту"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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);
}
}
}
}
}

View File

@ -53,6 +53,59 @@
_mapStorages.Add(name, new MapWithSetLocomotivesGeneric<DrawningObjectLocomotive, AbstractMap>(_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<DrawningObjectLocomotive, AbstractMap>(_pictureWidth, _pictureHeight, newMap));
_mapStorages[mapName].LoadData(info[1].Split(separatorData, StringSplitOptions.RemoveEmptyEntries));
}
return true;
}
/// <summary>
/// Удаление карты
/// </summary>