Добавления методов сохранения и загрузки объектов.
This commit is contained in:
parent
8f4f105b75
commit
5b00b1a238
@ -25,6 +25,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="метк\" />
|
||||
<Folder Include="делите\" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
@ -36,5 +36,14 @@ namespace Airbus
|
||||
{
|
||||
_airbus?.SetPosition(x, y, width, height);
|
||||
}
|
||||
|
||||
public void DrawningObject()
|
||||
{
|
||||
//TODO
|
||||
}
|
||||
|
||||
public string GetInfo() => _airbus?.GetDataForSave();
|
||||
|
||||
public static IDrawningObject Create(string data) => new IDrawningObject(data.CreateDrawningPlane());
|
||||
}
|
||||
}
|
||||
|
54
Airbus/Airbus/ExtentionPlane.cs
Normal file
54
Airbus/Airbus/ExtentionPlane.cs
Normal file
@ -0,0 +1,54 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Forms.VisualStyles;
|
||||
|
||||
namespace Airbus
|
||||
{
|
||||
//расширение для класса DrawingPlane (класс для сохранения в файл колекций объектов)
|
||||
internal static class ExtentionPlane
|
||||
{
|
||||
//разделитель для записи информации по объекту в файл
|
||||
private static readonly char _separatorForObject = ':';
|
||||
|
||||
//создание объекта из строки
|
||||
public static DrawningAirbus CreateDrawningPlane(this string info)
|
||||
{
|
||||
string[] strs = info.Split(_separatorForObject);
|
||||
|
||||
//если простой самолёт
|
||||
if(strs.Length == 3)
|
||||
{
|
||||
return new DrawningAirbus(Convert.ToInt32(strs[0]), Convert.ToInt32(strs[1]), Color.FromName(strs[2]));
|
||||
}
|
||||
|
||||
//если аэробус
|
||||
if(strs.Length == 5)
|
||||
{
|
||||
return new DrawningSuperAirbus(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 DrawningAirbus drawingPlane)
|
||||
{
|
||||
var plane = drawingPlane.Airbus;
|
||||
var str = $"{plane.Speed}{_separatorForObject}{plane.Weight}{_separatorForObject}{plane.CorpusColor.Name}";
|
||||
|
||||
//если объект не расширеный
|
||||
if(plane is not EntitySuperAirbus airbus)
|
||||
{
|
||||
return str;
|
||||
}
|
||||
|
||||
return $"{str}{_separatorForObject}{airbus.AddColor.Name}{_separatorForObject}" +
|
||||
$"{airbus.AddEngine}{_separatorForObject}{airbus.AddСompartment}";
|
||||
|
||||
}
|
||||
}
|
||||
}
|
76
Airbus/Airbus/FormMapWithSetPlanes.Designer.cs
generated
76
Airbus/Airbus/FormMapWithSetPlanes.Designer.cs
generated
@ -45,9 +45,16 @@
|
||||
this.maskedTextBoxPosition = new System.Windows.Forms.MaskedTextBox();
|
||||
this.buttonAddPlane = new System.Windows.Forms.Button();
|
||||
this.pictureBox = new System.Windows.Forms.PictureBox();
|
||||
this.menuStrip = 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.OpenFileDialog();
|
||||
this.groupBoxTools.SuspendLayout();
|
||||
this.groupBoxMaps.SuspendLayout();
|
||||
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit();
|
||||
this.menuStrip.SuspendLayout();
|
||||
this.SuspendLayout();
|
||||
//
|
||||
// groupBoxTools
|
||||
@ -63,9 +70,9 @@
|
||||
this.groupBoxTools.Controls.Add(this.maskedTextBoxPosition);
|
||||
this.groupBoxTools.Controls.Add(this.buttonAddPlane);
|
||||
this.groupBoxTools.Dock = System.Windows.Forms.DockStyle.Right;
|
||||
this.groupBoxTools.Location = new System.Drawing.Point(843, 0);
|
||||
this.groupBoxTools.Location = new System.Drawing.Point(843, 28);
|
||||
this.groupBoxTools.Name = "groupBoxTools";
|
||||
this.groupBoxTools.Size = new System.Drawing.Size(250, 722);
|
||||
this.groupBoxTools.Size = new System.Drawing.Size(250, 694);
|
||||
this.groupBoxTools.TabIndex = 0;
|
||||
this.groupBoxTools.TabStop = false;
|
||||
this.groupBoxTools.Text = "Инструменты";
|
||||
@ -138,7 +145,7 @@
|
||||
this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
|
||||
this.buttonRight.BackgroundImage = global::Airbus.Properties.Resources.Right;
|
||||
this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
|
||||
this.buttonRight.Location = new System.Drawing.Point(158, 657);
|
||||
this.buttonRight.Location = new System.Drawing.Point(158, 629);
|
||||
this.buttonRight.Name = "buttonRight";
|
||||
this.buttonRight.Size = new System.Drawing.Size(45, 45);
|
||||
this.buttonRight.TabIndex = 9;
|
||||
@ -150,7 +157,7 @@
|
||||
this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
|
||||
this.buttonDown.BackgroundImage = global::Airbus.Properties.Resources.Down;
|
||||
this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
|
||||
this.buttonDown.Location = new System.Drawing.Point(107, 657);
|
||||
this.buttonDown.Location = new System.Drawing.Point(107, 629);
|
||||
this.buttonDown.Name = "buttonDown";
|
||||
this.buttonDown.Size = new System.Drawing.Size(45, 45);
|
||||
this.buttonDown.TabIndex = 8;
|
||||
@ -162,7 +169,7 @@
|
||||
this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
|
||||
this.buttonLeft.BackgroundImage = global::Airbus.Properties.Resources.Left;
|
||||
this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
|
||||
this.buttonLeft.Location = new System.Drawing.Point(56, 657);
|
||||
this.buttonLeft.Location = new System.Drawing.Point(56, 629);
|
||||
this.buttonLeft.Name = "buttonLeft";
|
||||
this.buttonLeft.Size = new System.Drawing.Size(45, 45);
|
||||
this.buttonLeft.TabIndex = 7;
|
||||
@ -174,7 +181,7 @@
|
||||
this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
|
||||
this.buttonUp.BackgroundImage = global::Airbus.Properties.Resources.Up;
|
||||
this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
|
||||
this.buttonUp.Location = new System.Drawing.Point(107, 606);
|
||||
this.buttonUp.Location = new System.Drawing.Point(107, 578);
|
||||
this.buttonUp.Name = "buttonUp";
|
||||
this.buttonUp.Size = new System.Drawing.Size(45, 45);
|
||||
this.buttonUp.TabIndex = 6;
|
||||
@ -232,12 +239,54 @@
|
||||
// pictureBox
|
||||
//
|
||||
this.pictureBox.Dock = System.Windows.Forms.DockStyle.Fill;
|
||||
this.pictureBox.Location = new System.Drawing.Point(0, 0);
|
||||
this.pictureBox.Location = new System.Drawing.Point(0, 28);
|
||||
this.pictureBox.Name = "pictureBox";
|
||||
this.pictureBox.Size = new System.Drawing.Size(843, 722);
|
||||
this.pictureBox.Size = new System.Drawing.Size(843, 694);
|
||||
this.pictureBox.TabIndex = 1;
|
||||
this.pictureBox.TabStop = false;
|
||||
//
|
||||
// menuStrip
|
||||
//
|
||||
this.menuStrip.ImageScalingSize = new System.Drawing.Size(20, 20);
|
||||
this.menuStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
|
||||
this.ToolStripMenuItem});
|
||||
this.menuStrip.Location = new System.Drawing.Point(0, 0);
|
||||
this.menuStrip.Name = "menuStrip";
|
||||
this.menuStrip.Size = new System.Drawing.Size(1093, 28);
|
||||
this.menuStrip.TabIndex = 2;
|
||||
this.menuStrip.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(59, 24);
|
||||
this.ToolStripMenuItem.Text = "Файл";
|
||||
//
|
||||
// SaveToolStripMenuItem
|
||||
//
|
||||
this.SaveToolStripMenuItem.Name = "SaveToolStripMenuItem";
|
||||
this.SaveToolStripMenuItem.Size = new System.Drawing.Size(224, 26);
|
||||
this.SaveToolStripMenuItem.Text = "Сохранение";
|
||||
this.SaveToolStripMenuItem.Click += new System.EventHandler(this.SaveToolStripMenuItem_Click);
|
||||
//
|
||||
// LoadToolStripMenuItem
|
||||
//
|
||||
this.LoadToolStripMenuItem.Name = "LoadToolStripMenuItem";
|
||||
this.LoadToolStripMenuItem.Size = new System.Drawing.Size(224, 26);
|
||||
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";
|
||||
//
|
||||
// FormMapWithSetPlanes
|
||||
//
|
||||
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F);
|
||||
@ -245,6 +294,8 @@
|
||||
this.ClientSize = new System.Drawing.Size(1093, 722);
|
||||
this.Controls.Add(this.pictureBox);
|
||||
this.Controls.Add(this.groupBoxTools);
|
||||
this.Controls.Add(this.menuStrip);
|
||||
this.MainMenuStrip = this.menuStrip;
|
||||
this.Name = "FormMapWithSetPlanes";
|
||||
this.Text = "FormMapWithSetPlanes";
|
||||
this.groupBoxTools.ResumeLayout(false);
|
||||
@ -252,7 +303,10 @@
|
||||
this.groupBoxMaps.ResumeLayout(false);
|
||||
this.groupBoxMaps.PerformLayout();
|
||||
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit();
|
||||
this.menuStrip.ResumeLayout(false);
|
||||
this.menuStrip.PerformLayout();
|
||||
this.ResumeLayout(false);
|
||||
this.PerformLayout();
|
||||
|
||||
}
|
||||
|
||||
@ -275,5 +329,11 @@
|
||||
private ListBox listBoxMaps;
|
||||
private Button buttonAddMap;
|
||||
private TextBox textBoxNewMapName;
|
||||
private MenuStrip menuStrip;
|
||||
private ToolStripMenuItem ToolStripMenuItem;
|
||||
private ToolStripMenuItem SaveToolStripMenuItem;
|
||||
private ToolStripMenuItem LoadToolStripMenuItem;
|
||||
private OpenFileDialog openFileDialog;
|
||||
private OpenFileDialog saveFileDialog;
|
||||
}
|
||||
}
|
@ -208,5 +208,29 @@ namespace Airbus
|
||||
|
||||
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? String.Empty].MoveObject(dir);
|
||||
}
|
||||
|
||||
//обработка нажатия сохранения
|
||||
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)
|
||||
{
|
||||
// TODO продумать логику по аналогии
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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="menuStrip.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>144, 17</value>
|
||||
</metadata>
|
||||
<metadata name="saveFileDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
|
||||
<value>311, 17</value>
|
||||
</metadata>
|
||||
</root>
|
@ -23,5 +23,8 @@ namespace Airbus
|
||||
|
||||
//получение текущей позиции объекта
|
||||
(float Left, float Right, float Top, float Bottom) GetCurrentPosition();
|
||||
|
||||
//получение информации по объекту
|
||||
string GetInfo();
|
||||
}
|
||||
}
|
||||
|
@ -88,6 +88,28 @@ namespace Airbus
|
||||
return new(_pictureWidth, _pictureHeight);
|
||||
}
|
||||
|
||||
//получение данных в виде строки
|
||||
public string GetData(char separatorType, char separatorData)
|
||||
{
|
||||
string data = $"{_map.GetType().Name}{separatorType}";
|
||||
|
||||
foreach (var plane in _setPlanes.GetAirbus())
|
||||
{
|
||||
data += $"{plane.GetInfo()}{separatorData}";
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
//Загрузка списка из массива строк
|
||||
public void LoadData(string[] records)
|
||||
{
|
||||
foreach (var rec in records)
|
||||
{
|
||||
_setPlanes.Insert(DrawningObjectPlane.Create(rec) as T);
|
||||
}
|
||||
}
|
||||
|
||||
//"взламываем" набор, чтобы все элементы оказались в начале
|
||||
private void Shaking()
|
||||
{
|
||||
|
@ -10,11 +10,17 @@ namespace Airbus
|
||||
internal class MapsCollection
|
||||
{
|
||||
//словарь (хранилище) с картами
|
||||
readonly Dictionary<string, MapWithSetPlanesGeneric<DrawningObjectPlane, AbstractMap>> _mapStorage;
|
||||
readonly Dictionary<string, MapWithSetPlanesGeneric<IDrawningObject, AbstractMap>> _mapStorage;
|
||||
|
||||
//возвращение списка названий карт
|
||||
public List<string> Keys => _mapStorage.Keys.ToList();
|
||||
|
||||
//разделитель для записи информации по элементу словаря в файл
|
||||
private readonly char separatorDict = '|';
|
||||
|
||||
//разделитель для записи коллекции данных в файл
|
||||
private readonly char separatorData = ';';
|
||||
|
||||
//ширина окна отрисовки
|
||||
private readonly int _pictureWidth;
|
||||
|
||||
@ -24,7 +30,7 @@ namespace Airbus
|
||||
//конструктор
|
||||
public MapsCollection(int pictureWidth, int pictureHeight)
|
||||
{
|
||||
_mapStorage = new Dictionary<string, MapWithSetPlanesGeneric<DrawningObjectPlane, AbstractMap>>();
|
||||
_mapStorage = new Dictionary<string, MapWithSetPlanesGeneric<IDrawningObject, AbstractMap>>();
|
||||
_pictureWidth = pictureWidth;
|
||||
_pictureHeight = pictureHeight;
|
||||
}
|
||||
@ -32,7 +38,7 @@ namespace Airbus
|
||||
//добавление карты
|
||||
public void AddMap(string name, AbstractMap map)
|
||||
{
|
||||
var NewElem = new MapWithSetPlanesGeneric<DrawningObjectPlane, AbstractMap>(
|
||||
var NewElem = new MapWithSetPlanesGeneric<IDrawningObject, AbstractMap>(
|
||||
_pictureWidth, _pictureHeight, map);
|
||||
_mapStorage.Add(name, NewElem);
|
||||
}
|
||||
@ -43,8 +49,97 @@ namespace Airbus
|
||||
_mapStorage.Remove(name);
|
||||
}
|
||||
|
||||
// Метод записи информации в файл
|
||||
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 (FileStream fs = new(filename, FileMode.Create))
|
||||
{
|
||||
WriteToFile($"MapsCollection{Environment.NewLine}", fs);
|
||||
foreach (var storage in _mapStorage)
|
||||
{
|
||||
|
||||
WriteToFile($"{storage.Key}{separatorDict}{storage.Value.GetData(separatorDict, separatorData)}" +
|
||||
$"{Environment.NewLine}", fs);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Загрузка нформации по автомобилям на парковках из файла
|
||||
public bool LoadData(string filename)
|
||||
{
|
||||
if (!File.Exists(filename))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
string bufferTextFromFile = "";
|
||||
|
||||
using (FileStream fs = new(filename, FileMode.Open))
|
||||
{
|
||||
byte[] b = new byte[fs.Length];
|
||||
UTF8Encoding temp = new(true);
|
||||
|
||||
while (fs.Read(b, 0, b.Length) > 0)
|
||||
{
|
||||
bufferTextFromFile += temp.GetString(b);
|
||||
}
|
||||
}
|
||||
|
||||
var strs = bufferTextFromFile.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
|
||||
|
||||
if (!strs[0].Contains("MapsCollection"))
|
||||
{
|
||||
//если нет такой записи, то это не те данные
|
||||
return false;
|
||||
}
|
||||
|
||||
//очищаем записи
|
||||
_mapStorage.Clear();
|
||||
|
||||
for (int i = 1; i < strs.Length; ++i)
|
||||
{
|
||||
var elem = strs[i].Split(separatorDict);
|
||||
AbstractMap map = null;
|
||||
|
||||
switch (elem[1])
|
||||
{
|
||||
case "SimpleMap":
|
||||
map = new SimpleMap();
|
||||
break;
|
||||
case "DesertStormMap":
|
||||
map = new DesertStormMap();
|
||||
break;
|
||||
case "StarWarsMap":
|
||||
map = new StarWarsMap();
|
||||
break;
|
||||
}
|
||||
|
||||
_mapStorage.Add(elem[0], new MapWithSetPlanesGeneric<IDrawningObject, AbstractMap>(_pictureWidth, _pictureHeight,
|
||||
map));
|
||||
|
||||
_mapStorage[elem[0]].LoadData(elem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
//Доступ к аэродрому
|
||||
public MapWithSetPlanesGeneric<DrawningObjectPlane, AbstractMap> this[string ind]
|
||||
public MapWithSetPlanesGeneric<IDrawningObject, AbstractMap> this[string ind]
|
||||
{
|
||||
get
|
||||
{
|
||||
|
Loading…
x
Reference in New Issue
Block a user