база
This commit is contained in:
parent
1b8ab2bb3b
commit
903f8f6e46
@ -30,6 +30,7 @@
|
|||||||
{
|
{
|
||||||
_locomotive.DrawTransport(g);
|
_locomotive.DrawTransport(g);
|
||||||
}
|
}
|
||||||
|
public string GetInfo() => _locomotive?.GetDataForSave();
|
||||||
|
public static IDrawningObject Create(string data) => new DrawningObjectLocomotive(data.CreateDrawningLocomotive());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,50 @@
|
|||||||
|
namespace WarmlyLocomotove
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Расширение для класса DrawningLocomotive
|
||||||
|
/// </summary>
|
||||||
|
internal static class ExtentionLocomotive
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Разделитель для записи информации
|
||||||
|
/// </summary>
|
||||||
|
private static readonly char _separatorForObject = ':';
|
||||||
|
/// <summary>
|
||||||
|
/// Получаем данные для сохранения в файл
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="drawningLocomotive"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static string GetDataForSave(this DrawningLocomotive drawningLocomotive)
|
||||||
|
{
|
||||||
|
var locomotive = drawningLocomotive.Locomotive;
|
||||||
|
var str = $"{locomotive.Speed}{_separatorForObject}{locomotive.Weight}{_separatorForObject}{locomotive.BodyColor.Name}";
|
||||||
|
if (locomotive is not EntityWarmlyLocomotive warmlyLocomotive)
|
||||||
|
{
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
return $"{str}{_separatorForObject}{warmlyLocomotive.AdditionalColor.Name}{_separatorForObject}{warmlyLocomotive.HasPipe}{_separatorForObject}{warmlyLocomotive.HasFuelTank}";
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// Восстанавливаем объект по полученной из файла информации
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="info"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static DrawningLocomotive CreateDrawningLocomotive(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 DrawningWarmlyLocomotive
|
||||||
|
(
|
||||||
|
Convert.ToInt32(strs[0]), Convert.ToInt32(strs[1]), Color.FromName(strs[2]), 160, 85,
|
||||||
|
Color.FromName(strs[3]), Convert.ToBoolean(strs[4]), Convert.ToBoolean(strs[5])
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -29,6 +29,7 @@
|
|||||||
private void InitializeComponent()
|
private void InitializeComponent()
|
||||||
{
|
{
|
||||||
this.groupBoxTools = new System.Windows.Forms.GroupBox();
|
this.groupBoxTools = new System.Windows.Forms.GroupBox();
|
||||||
|
this.buttonShowLastRemovedObject = new System.Windows.Forms.Button();
|
||||||
this.groupBoxMaps = new System.Windows.Forms.GroupBox();
|
this.groupBoxMaps = new System.Windows.Forms.GroupBox();
|
||||||
this.buttonDeleteMap = new System.Windows.Forms.Button();
|
this.buttonDeleteMap = new System.Windows.Forms.Button();
|
||||||
this.listBoxMaps = new System.Windows.Forms.ListBox();
|
this.listBoxMaps = new System.Windows.Forms.ListBox();
|
||||||
@ -45,10 +46,16 @@
|
|||||||
this.maskedTextBoxPosition = new System.Windows.Forms.MaskedTextBox();
|
this.maskedTextBoxPosition = new System.Windows.Forms.MaskedTextBox();
|
||||||
this.buttonAddCar = new System.Windows.Forms.Button();
|
this.buttonAddCar = new System.Windows.Forms.Button();
|
||||||
this.pictureBoxLocomotives = new System.Windows.Forms.PictureBox();
|
this.pictureBoxLocomotives = new System.Windows.Forms.PictureBox();
|
||||||
this.buttonShowLastRemovedObject = new System.Windows.Forms.Button();
|
this.menuStrip1 = new System.Windows.Forms.MenuStrip();
|
||||||
|
this.FileToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
|
||||||
|
this.SaveToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
|
||||||
|
this.LoadToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
|
||||||
|
this.saveFileDialog = new System.Windows.Forms.SaveFileDialog();
|
||||||
|
this.openFileDialog = new System.Windows.Forms.OpenFileDialog();
|
||||||
this.groupBoxTools.SuspendLayout();
|
this.groupBoxTools.SuspendLayout();
|
||||||
this.groupBoxMaps.SuspendLayout();
|
this.groupBoxMaps.SuspendLayout();
|
||||||
((System.ComponentModel.ISupportInitialize)(this.pictureBoxLocomotives)).BeginInit();
|
((System.ComponentModel.ISupportInitialize)(this.pictureBoxLocomotives)).BeginInit();
|
||||||
|
this.menuStrip1.SuspendLayout();
|
||||||
this.SuspendLayout();
|
this.SuspendLayout();
|
||||||
//
|
//
|
||||||
// groupBoxTools
|
// groupBoxTools
|
||||||
@ -72,6 +79,16 @@
|
|||||||
this.groupBoxTools.TabStop = false;
|
this.groupBoxTools.TabStop = false;
|
||||||
this.groupBoxTools.Text = "Инструменты";
|
this.groupBoxTools.Text = "Инструменты";
|
||||||
//
|
//
|
||||||
|
// buttonShowLastRemovedObject
|
||||||
|
//
|
||||||
|
this.buttonShowLastRemovedObject.Location = new System.Drawing.Point(31, 517);
|
||||||
|
this.buttonShowLastRemovedObject.Name = "buttonShowLastRemovedObject";
|
||||||
|
this.buttonShowLastRemovedObject.Size = new System.Drawing.Size(164, 40);
|
||||||
|
this.buttonShowLastRemovedObject.TabIndex = 2;
|
||||||
|
this.buttonShowLastRemovedObject.Text = "Показать последний удалённый элемент";
|
||||||
|
this.buttonShowLastRemovedObject.UseVisualStyleBackColor = true;
|
||||||
|
this.buttonShowLastRemovedObject.Click += new System.EventHandler(this.ButtonShowLastRemovedObject_Click);
|
||||||
|
//
|
||||||
// groupBoxMaps
|
// groupBoxMaps
|
||||||
//
|
//
|
||||||
this.groupBoxMaps.Anchor = System.Windows.Forms.AnchorStyles.Right;
|
this.groupBoxMaps.Anchor = System.Windows.Forms.AnchorStyles.Right;
|
||||||
@ -242,21 +259,53 @@
|
|||||||
this.pictureBoxLocomotives.TabIndex = 1;
|
this.pictureBoxLocomotives.TabIndex = 1;
|
||||||
this.pictureBoxLocomotives.TabStop = false;
|
this.pictureBoxLocomotives.TabStop = false;
|
||||||
//
|
//
|
||||||
// buttonShowLastRemovedObject
|
// menuStrip1
|
||||||
//
|
//
|
||||||
this.buttonShowLastRemovedObject.Location = new System.Drawing.Point(31, 517);
|
this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
|
||||||
this.buttonShowLastRemovedObject.Name = "buttonShowLastRemovedObject";
|
this.FileToolStripMenuItem});
|
||||||
this.buttonShowLastRemovedObject.Size = new System.Drawing.Size(164, 40);
|
this.menuStrip1.Location = new System.Drawing.Point(0, 0);
|
||||||
this.buttonShowLastRemovedObject.TabIndex = 2;
|
this.menuStrip1.Name = "menuStrip1";
|
||||||
this.buttonShowLastRemovedObject.Text = "Показать последний удалённый элемент";
|
this.menuStrip1.Size = new System.Drawing.Size(464, 24);
|
||||||
this.buttonShowLastRemovedObject.UseVisualStyleBackColor = true;
|
this.menuStrip1.TabIndex = 3;
|
||||||
this.buttonShowLastRemovedObject.Click += new System.EventHandler(this.ButtonShowLastRemovedObject_Click);
|
this.menuStrip1.Text = "menuStrip";
|
||||||
|
//
|
||||||
|
// FileToolStripMenuItem
|
||||||
|
//
|
||||||
|
this.FileToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
|
||||||
|
this.SaveToolStripMenuItem,
|
||||||
|
this.LoadToolStripMenuItem});
|
||||||
|
this.FileToolStripMenuItem.Name = "FileToolStripMenuItem";
|
||||||
|
this.FileToolStripMenuItem.Size = new System.Drawing.Size(48, 20);
|
||||||
|
this.FileToolStripMenuItem.Text = "Файл";
|
||||||
|
//
|
||||||
|
// SaveToolStripMenuItem
|
||||||
|
//
|
||||||
|
this.SaveToolStripMenuItem.Name = "SaveToolStripMenuItem";
|
||||||
|
this.SaveToolStripMenuItem.Size = new System.Drawing.Size(180, 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.Text = "Загрузка";
|
||||||
|
this.LoadToolStripMenuItem.Click += new System.EventHandler(this.LoadToolStripMenuItem_Click);
|
||||||
|
//
|
||||||
|
// saveFileDialog
|
||||||
|
//
|
||||||
|
this.saveFileDialog.Filter = "txt file|*.txt";
|
||||||
|
//
|
||||||
|
// openFileDialog
|
||||||
|
//
|
||||||
|
this.openFileDialog.Filter = "txt file|*.txt";
|
||||||
//
|
//
|
||||||
// FormMapWithSetLocomotives
|
// FormMapWithSetLocomotives
|
||||||
//
|
//
|
||||||
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
|
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
|
||||||
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
|
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
|
||||||
this.ClientSize = new System.Drawing.Size(687, 760);
|
this.ClientSize = new System.Drawing.Size(687, 760);
|
||||||
|
this.Controls.Add(this.menuStrip1);
|
||||||
this.Controls.Add(this.pictureBoxLocomotives);
|
this.Controls.Add(this.pictureBoxLocomotives);
|
||||||
this.Controls.Add(this.groupBoxTools);
|
this.Controls.Add(this.groupBoxTools);
|
||||||
this.Name = "FormMapWithSetLocomotives";
|
this.Name = "FormMapWithSetLocomotives";
|
||||||
@ -266,7 +315,10 @@
|
|||||||
this.groupBoxMaps.ResumeLayout(false);
|
this.groupBoxMaps.ResumeLayout(false);
|
||||||
this.groupBoxMaps.PerformLayout();
|
this.groupBoxMaps.PerformLayout();
|
||||||
((System.ComponentModel.ISupportInitialize)(this.pictureBoxLocomotives)).EndInit();
|
((System.ComponentModel.ISupportInitialize)(this.pictureBoxLocomotives)).EndInit();
|
||||||
|
this.menuStrip1.ResumeLayout(false);
|
||||||
|
this.menuStrip1.PerformLayout();
|
||||||
this.ResumeLayout(false);
|
this.ResumeLayout(false);
|
||||||
|
this.PerformLayout();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -290,5 +342,11 @@
|
|||||||
private Button buttonAddMap;
|
private Button buttonAddMap;
|
||||||
private TextBox textBoxNewMapName;
|
private TextBox textBoxNewMapName;
|
||||||
private Button buttonShowLastRemovedObject;
|
private Button buttonShowLastRemovedObject;
|
||||||
|
private MenuStrip menuStrip1;
|
||||||
|
private ToolStripMenuItem FileToolStripMenuItem;
|
||||||
|
private ToolStripMenuItem SaveToolStripMenuItem;
|
||||||
|
private ToolStripMenuItem LoadToolStripMenuItem;
|
||||||
|
private SaveFileDialog saveFileDialog;
|
||||||
|
private OpenFileDialog openFileDialog;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -235,5 +235,48 @@
|
|||||||
formWithLastRemovedObject.Draw();
|
formWithLastRemovedObject.Draw();
|
||||||
_mapsCollection.RemoveLastObject();
|
_mapsCollection.RemoveLastObject();
|
||||||
}
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// Обработка нажатия "Сохранение"
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="sender"></param>
|
||||||
|
/// <param name="e"></param>
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// Обработка нажатия "Загрузка"
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="sender"></param>
|
||||||
|
/// <param name="e"></param>
|
||||||
|
private void LoadToolStripMenuItem_Click(object sender, EventArgs e)
|
||||||
|
{
|
||||||
|
if (openFileDialog.ShowDialog() == DialogResult.OK)
|
||||||
|
{
|
||||||
|
if (_mapsCollection.LoadData(openFileDialog.FileName))
|
||||||
|
{
|
||||||
|
MessageBox.Show("Загрузка прошла успешно", "Результат",
|
||||||
|
MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||||||
|
ReloadMaps();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
MessageBox.Show("Не удалось загрузить файл", "Результат",
|
||||||
|
MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -57,4 +57,13 @@
|
|||||||
<resheader name="writer">
|
<resheader name="writer">
|
||||||
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
|
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
|
||||||
</resheader>
|
</resheader>
|
||||||
|
<metadata name="menuStrip1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
|
||||||
|
<value>17, 17</value>
|
||||||
|
</metadata>
|
||||||
|
<metadata name="saveFileDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
|
||||||
|
<value>265, 17</value>
|
||||||
|
</metadata>
|
||||||
|
<metadata name="openFileDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
|
||||||
|
<value>132, 17</value>
|
||||||
|
</metadata>
|
||||||
</root>
|
</root>
|
@ -32,5 +32,10 @@
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
(float Top, float Bottom, float Left, float Right) GetCurrentPosition();
|
(float Top, float Bottom, float Left, float Right) GetCurrentPosition();
|
||||||
|
/// <summary>
|
||||||
|
/// Получение информации по объекту
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
string GetInfo();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -178,5 +178,32 @@
|
|||||||
CurrentLocomotiveNumber++;
|
CurrentLocomotiveNumber++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// Получение данных в виде строки
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="separatorType"></param>
|
||||||
|
/// <param name="separatorData"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public string GetData(char separatorType, char separatorData)
|
||||||
|
{
|
||||||
|
//Получаем название карты
|
||||||
|
string data = $"{_map.GetType().Name}{separatorType}";
|
||||||
|
foreach (var locomotive in _setLocomotives.GetLocomotives())
|
||||||
|
{
|
||||||
|
data += $"{locomotive.GetInfo()}{separatorData}";
|
||||||
|
}
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// Загрузка списка из массива строк
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="records"></param>
|
||||||
|
public void LoadData(string[] records)
|
||||||
|
{
|
||||||
|
foreach (var record in records)
|
||||||
|
{
|
||||||
|
_setLocomotives.Insert(DrawningObjectLocomotive.Create(record) as T, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -22,6 +22,14 @@
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
private readonly int _pictureHeight;
|
private readonly int _pictureHeight;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
/// Разделитель для записи информации по элементу словаря в файл
|
||||||
|
/// </summary>
|
||||||
|
private readonly char separatorDict = '|';
|
||||||
|
/// <summary>
|
||||||
|
/// Разделитель для записи коллекции данных в файл
|
||||||
|
/// </summary>
|
||||||
|
private readonly char separatorData = ';';
|
||||||
|
/// <summary>
|
||||||
/// Конструктор
|
/// Конструктор
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="pictureWidth"></param>
|
/// <param name="pictureWidth"></param>
|
||||||
@ -115,6 +123,61 @@
|
|||||||
{
|
{
|
||||||
_removedObjects.RemoveLast();
|
_removedObjects.RemoveLast();
|
||||||
}
|
}
|
||||||
|
public bool SaveData(string filename)
|
||||||
|
{
|
||||||
|
if (File.Exists(filename))
|
||||||
|
{
|
||||||
|
File.Delete(filename);
|
||||||
|
}
|
||||||
|
using (StreamWriter sw = new(filename))
|
||||||
|
{
|
||||||
|
sw.Write("MapsCollection\n");
|
||||||
|
foreach (var storage in _mapStorages)
|
||||||
|
{
|
||||||
|
sw.Write($"{storage.Key}{separatorDict}{storage.Value.GetData(separatorDict, separatorData)}\n");
|
||||||
|
}
|
||||||
|
sw.Close();
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
public bool LoadData(string filename)
|
||||||
|
{
|
||||||
|
if (!File.Exists(filename))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
using (StreamReader sr = new(filename))
|
||||||
|
{
|
||||||
|
string firstStr = sr.ReadLine();
|
||||||
|
if (firstStr == null || !firstStr.Contains("MapsCollection"))
|
||||||
|
{
|
||||||
|
//если нет такой записи, то это не те данные
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
string? currentString;
|
||||||
|
while ((currentString = sr.ReadLine()) != null)
|
||||||
|
{
|
||||||
|
var elem = currentString.Split(separatorDict);
|
||||||
|
AbstractMap map = null;
|
||||||
|
switch (elem[1])
|
||||||
|
{
|
||||||
|
case "SimpleMap":
|
||||||
|
map = new SimpleMap();
|
||||||
|
break;
|
||||||
|
case "CrossMap":
|
||||||
|
map = new CrossMap();
|
||||||
|
break;
|
||||||
|
case "RoadsMap":
|
||||||
|
map = new RoadsMap();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
_mapStorages.Add(elem[0], new MapWithSetLocomotivesGeneric<DrawningObjectLocomotive, AbstractMap>(_pictureWidth, _pictureHeight, map));
|
||||||
|
_mapStorages[elem[0]].LoadData(elem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries));
|
||||||
|
}
|
||||||
|
sr.Close();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user