лаб6
This commit is contained in:
parent
6fdbfe8a89
commit
224eaff7b8
@ -30,6 +30,7 @@
|
||||
{
|
||||
_locomotive.DrawTransport(g);
|
||||
}
|
||||
|
||||
public string GetInfo() => _locomotive?.GetDataForSave();
|
||||
public static IDrawningObject Create(string data) => new DrawningObjectLocomotive(data.CreateDrawningLocomotive());
|
||||
}
|
||||
}
|
||||
|
50
Locomotives/Locomotives/ExtentionLocomotive.cs
Normal file
50
Locomotives/Locomotives/ExtentionLocomotive.cs
Normal file
@ -0,0 +1,50 @@
|
||||
namespace Locomotives
|
||||
{
|
||||
/// <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;
|
||||
}
|
||||
}
|
||||
}
|
@ -45,9 +45,16 @@
|
||||
this.maskedTextBoxPosition = new System.Windows.Forms.MaskedTextBox();
|
||||
this.buttonAddCar = new System.Windows.Forms.Button();
|
||||
this.pictureBoxLocomotives = new System.Windows.Forms.PictureBox();
|
||||
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();
|
||||
this.groupBoxTools.SuspendLayout();
|
||||
this.groupBoxMaps.SuspendLayout();
|
||||
((System.ComponentModel.ISupportInitialize)(this.pictureBoxLocomotives)).BeginInit();
|
||||
this.menuStrip1.SuspendLayout();
|
||||
this.SuspendLayout();
|
||||
//
|
||||
// groupBoxTools
|
||||
@ -63,9 +70,9 @@
|
||||
this.groupBoxTools.Controls.Add(this.maskedTextBoxPosition);
|
||||
this.groupBoxTools.Controls.Add(this.buttonAddCar);
|
||||
this.groupBoxTools.Dock = System.Windows.Forms.DockStyle.Right;
|
||||
this.groupBoxTools.Location = new System.Drawing.Point(950, 0);
|
||||
this.groupBoxTools.Location = new System.Drawing.Point(950, 24);
|
||||
this.groupBoxTools.Name = "groupBoxTools";
|
||||
this.groupBoxTools.Size = new System.Drawing.Size(223, 676);
|
||||
this.groupBoxTools.Size = new System.Drawing.Size(223, 652);
|
||||
this.groupBoxTools.TabIndex = 0;
|
||||
this.groupBoxTools.TabStop = false;
|
||||
this.groupBoxTools.Text = "Инструменты";
|
||||
@ -140,7 +147,7 @@
|
||||
this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
|
||||
this.buttonUp.BackgroundImage = global::Locomotives.Properties.Resources.ArrowUp;
|
||||
this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
|
||||
this.buttonUp.Location = new System.Drawing.Point(107, 604);
|
||||
this.buttonUp.Location = new System.Drawing.Point(107, 580);
|
||||
this.buttonUp.Name = "buttonUp";
|
||||
this.buttonUp.Size = new System.Drawing.Size(30, 30);
|
||||
this.buttonUp.TabIndex = 10;
|
||||
@ -152,7 +159,7 @@
|
||||
this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
|
||||
this.buttonDown.BackgroundImage = global::Locomotives.Properties.Resources.ArrowDown;
|
||||
this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
|
||||
this.buttonDown.Location = new System.Drawing.Point(107, 640);
|
||||
this.buttonDown.Location = new System.Drawing.Point(107, 616);
|
||||
this.buttonDown.Name = "buttonDown";
|
||||
this.buttonDown.Size = new System.Drawing.Size(30, 30);
|
||||
this.buttonDown.TabIndex = 9;
|
||||
@ -164,7 +171,7 @@
|
||||
this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
|
||||
this.buttonLeft.BackgroundImage = global::Locomotives.Properties.Resources.ArrowLeft;
|
||||
this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
|
||||
this.buttonLeft.Location = new System.Drawing.Point(71, 640);
|
||||
this.buttonLeft.Location = new System.Drawing.Point(71, 616);
|
||||
this.buttonLeft.Name = "buttonLeft";
|
||||
this.buttonLeft.Size = new System.Drawing.Size(30, 30);
|
||||
this.buttonLeft.TabIndex = 8;
|
||||
@ -176,7 +183,7 @@
|
||||
this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
|
||||
this.buttonRight.BackgroundImage = global::Locomotives.Properties.Resources.ArrowRight;
|
||||
this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
|
||||
this.buttonRight.Location = new System.Drawing.Point(143, 640);
|
||||
this.buttonRight.Location = new System.Drawing.Point(143, 616);
|
||||
this.buttonRight.Name = "buttonRight";
|
||||
this.buttonRight.Size = new System.Drawing.Size(30, 30);
|
||||
this.buttonRight.TabIndex = 7;
|
||||
@ -234,12 +241,53 @@
|
||||
// pictureBoxLocomotives
|
||||
//
|
||||
this.pictureBoxLocomotives.Dock = System.Windows.Forms.DockStyle.Fill;
|
||||
this.pictureBoxLocomotives.Location = new System.Drawing.Point(0, 0);
|
||||
this.pictureBoxLocomotives.Location = new System.Drawing.Point(0, 24);
|
||||
this.pictureBoxLocomotives.Name = "pictureBoxLocomotives";
|
||||
this.pictureBoxLocomotives.Size = new System.Drawing.Size(950, 676);
|
||||
this.pictureBoxLocomotives.Size = new System.Drawing.Size(950, 652);
|
||||
this.pictureBoxLocomotives.TabIndex = 1;
|
||||
this.pictureBoxLocomotives.TabStop = false;
|
||||
//
|
||||
// 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(1173, 24);
|
||||
this.menuStrip1.TabIndex = 2;
|
||||
this.menuStrip1.Text = "menuStrip";
|
||||
//
|
||||
// файл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.Filter = "txt file|*.txt";
|
||||
//
|
||||
// saveFileDialog
|
||||
//
|
||||
this.saveFileDialog.Filter = "txt file|*.txt";
|
||||
//
|
||||
// FormMapWithSetLocomotives
|
||||
//
|
||||
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
|
||||
@ -247,6 +295,8 @@
|
||||
this.ClientSize = new System.Drawing.Size(1173, 676);
|
||||
this.Controls.Add(this.pictureBoxLocomotives);
|
||||
this.Controls.Add(this.groupBoxTools);
|
||||
this.Controls.Add(this.menuStrip1);
|
||||
this.MainMenuStrip = this.menuStrip1;
|
||||
this.Name = "FormMapWithSetLocomotives";
|
||||
this.Text = "Карта с набором объектов";
|
||||
this.groupBoxTools.ResumeLayout(false);
|
||||
@ -254,7 +304,10 @@
|
||||
this.groupBoxMaps.ResumeLayout(false);
|
||||
this.groupBoxMaps.PerformLayout();
|
||||
((System.ComponentModel.ISupportInitialize)(this.pictureBoxLocomotives)).EndInit();
|
||||
this.menuStrip1.ResumeLayout(false);
|
||||
this.menuStrip1.PerformLayout();
|
||||
this.ResumeLayout(false);
|
||||
this.PerformLayout();
|
||||
|
||||
}
|
||||
|
||||
@ -277,5 +330,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;
|
||||
}
|
||||
}
|
@ -214,5 +214,48 @@
|
||||
}
|
||||
pictureBoxLocomotives.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].MoveObject(dir);
|
||||
}
|
||||
/// <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">
|
||||
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
|
||||
</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="openFileDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
|
||||
<value>132, 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>
|
||||
</root>
|
@ -32,5 +32,10 @@
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
(float Top, float Bottom, float Left, float Right) GetCurrentPosition();
|
||||
/// <summary>
|
||||
/// Получение информации по объекту
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
string GetInfo();
|
||||
}
|
||||
}
|
||||
|
@ -174,5 +174,32 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,6 @@
|
||||
namespace Locomotives
|
||||
using System.Text;
|
||||
using System.IO;
|
||||
namespace Locomotives
|
||||
{
|
||||
/// <summary>
|
||||
/// Класс для хранения коллекции карт
|
||||
@ -8,7 +10,7 @@
|
||||
/// <summary>
|
||||
/// Словарь (хранилище) с картами
|
||||
/// </summary>
|
||||
readonly Dictionary<string, MapWithSetLocomotivesGeneric<DrawningObjectLocomotive, AbstractMap>> _mapStorages;
|
||||
readonly Dictionary<string, MapWithSetLocomotivesGeneric<IDrawningObject, AbstractMap>> _mapStorages;
|
||||
/// <summary>
|
||||
/// Возвращение списка названий карт
|
||||
/// </summary>
|
||||
@ -22,13 +24,21 @@
|
||||
/// </summary>
|
||||
private readonly int _pictureHeight;
|
||||
/// <summary>
|
||||
/// Разделитель для записи информации по элементу словаря в файл
|
||||
/// </summary>
|
||||
private readonly char separatorDict = '|';
|
||||
/// <summary>
|
||||
/// Разделитель для записи коллекции данных в файл
|
||||
/// </summary>
|
||||
private readonly char separatorData = ';';
|
||||
/// <summary>
|
||||
/// Конструктор
|
||||
/// </summary>
|
||||
/// <param name="pictureWidth"></param>
|
||||
/// <param name="pictureHeight"></param>
|
||||
public MapsCollection(int pictureWidth, int pictureHeight)
|
||||
{
|
||||
_mapStorages = new Dictionary<string, MapWithSetLocomotivesGeneric<DrawningObjectLocomotive, AbstractMap>>();
|
||||
_mapStorages = new Dictionary<string, MapWithSetLocomotivesGeneric<IDrawningObject, AbstractMap>>();
|
||||
_pictureWidth = pictureWidth;
|
||||
_pictureHeight = pictureHeight;
|
||||
}
|
||||
@ -39,7 +49,7 @@
|
||||
/// <param name="map">Карта</param>
|
||||
public void AddMap(string name, AbstractMap map)
|
||||
{
|
||||
_mapStorages.Add(name, new MapWithSetLocomotivesGeneric<DrawningObjectLocomotive, AbstractMap>(_pictureWidth, _pictureHeight, map));
|
||||
_mapStorages.Add(name, new MapWithSetLocomotivesGeneric<IDrawningObject, AbstractMap>(_pictureWidth, _pictureHeight, map));
|
||||
}
|
||||
/// <summary>
|
||||
/// Удаление карты
|
||||
@ -54,13 +64,66 @@
|
||||
/// </summary>
|
||||
/// <param name="ind"></param>
|
||||
/// <returns></returns>
|
||||
public MapWithSetLocomotivesGeneric<DrawningObjectLocomotive, AbstractMap> this[string ind]
|
||||
public MapWithSetLocomotivesGeneric<IDrawningObject, AbstractMap> this[string ind]
|
||||
{
|
||||
get
|
||||
{
|
||||
return _mapStorages[ind];
|
||||
}
|
||||
}
|
||||
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");
|
||||
}
|
||||
}
|
||||
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<IDrawningObject, 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