diff --git a/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/DrawningObjectBoat.cs b/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/DrawningObjectBoat.cs
index ac982cb..6dc06c4 100644
--- a/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/DrawningObjectBoat.cs
+++ b/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/DrawningObjectBoat.cs
@@ -22,6 +22,7 @@ namespace PIbd_22_Kalyshev_Y_V_MotorBoat_Base
{
return _boat?.GetCurrentPosition() ?? default;
}
+ public string GetInfo() => _boat?.GetDataForSave();
public void MoveObject(Direction direction)
{
_boat.MoveTransport(direction);
@@ -30,5 +31,6 @@ namespace PIbd_22_Kalyshev_Y_V_MotorBoat_Base
{
_boat.SetPosition(x, y, width, height);
}
+ public static IDrawningObject Create(string data) => new DrawningObjectBoat(data.CreateDrawningBoat());
}
}
diff --git a/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/ExtensionBoat.cs b/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/ExtensionBoat.cs
new file mode 100644
index 0000000..5002401
--- /dev/null
+++ b/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/ExtensionBoat.cs
@@ -0,0 +1,57 @@
+using System;
+using System.Collections.Generic;
+using System.Drawing.Text;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace PIbd_22_Kalyshev_Y_V_MotorBoat_Base
+{
+ ///
+ /// Расширение для класса DrawningBoat
+ ///
+ internal static class ExtentionBoat
+ {
+ ///
+ /// Разделитель для записи информации по объекту в файл
+ ///
+ private static readonly char _separatorForObject = ':';
+ ///
+ /// Создание объекта из строки
+ ///
+ ///
+ ///
+ public static DrawningBoat CreateDrawningBoat(this string info)
+ {
+ string[] strs = info.Split(_separatorForObject);
+ if (strs.Length == 3)
+ {
+ return new DrawningBoat(Convert.ToInt32(strs[0]),
+ Convert.ToInt32(strs[1]), Color.FromName(strs[2]));
+ }
+ if (strs.Length == 7)
+ {
+ return new DrawningSpeedboat(Convert.ToInt32(strs[0]),
+ Convert.ToInt32(strs[1]), Color.FromName(strs[2]),
+ Color.FromName(strs[3]), Convert.ToBoolean(strs[4]),
+ Convert.ToBoolean(strs[5]), Convert.ToBoolean(strs[6]));
+ }
+ return null;
+ }
+ ///
+ /// Получение данных для сохранения в файл
+ ///
+ ///
+ ///
+ public static string GetDataForSave (this DrawningBoat drawningBoat)
+ {
+ var boat = drawningBoat.Boat;
+ var str = $"{boat.Speed}{_separatorForObject}{boat.Weight}{_separatorForObject}{boat.BodyColor.Name}";
+ if (boat is not EntitySpeedboat speedboat)
+ {
+ return str;
+ }
+ return $"{str}{_separatorForObject}{speedboat.DopColor.Name}{_separatorForObject}{speedboat.BodyKit}{_separatorForObject}{speedboat.Wing}{_separatorForObject}{speedboat.SportLine}";
+ }
+ }
+}
diff --git a/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/FormMapWithSetBoats.Designer.cs b/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/FormMapWithSetBoats.Designer.cs
index 837846c..531b27e 100644
--- a/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/FormMapWithSetBoats.Designer.cs
+++ b/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/FormMapWithSetBoats.Designer.cs
@@ -45,9 +45,16 @@
this.buttonShowOnMap = new System.Windows.Forms.Button();
this.buttonAddBoat = 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.SaveFileDialog();
this.groupBoxTools.SuspendLayout();
this.groupBoxMap.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit();
+ this.menuStrip.SuspendLayout();
this.SuspendLayout();
//
// groupBoxTools
@@ -63,9 +70,9 @@
this.groupBoxTools.Controls.Add(this.buttonShowOnMap);
this.groupBoxTools.Controls.Add(this.buttonAddBoat);
this.groupBoxTools.Dock = System.Windows.Forms.DockStyle.Right;
- this.groupBoxTools.Location = new System.Drawing.Point(690, 0);
+ this.groupBoxTools.Location = new System.Drawing.Point(690, 24);
this.groupBoxTools.Name = "groupBoxTools";
- this.groupBoxTools.Size = new System.Drawing.Size(204, 587);
+ this.groupBoxTools.Size = new System.Drawing.Size(204, 563);
this.groupBoxTools.TabIndex = 0;
this.groupBoxTools.TabStop = false;
this.groupBoxTools.Text = "Инструменты";
@@ -167,7 +174,7 @@
this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonDown.BackgroundImage = global::PIbd_22_Kalyshev_Y_V_MotorBoat_Base.Properties.Resources.down;
this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
- this.buttonDown.Location = new System.Drawing.Point(91, 537);
+ this.buttonDown.Location = new System.Drawing.Point(91, 513);
this.buttonDown.Name = "buttonDown";
this.buttonDown.Size = new System.Drawing.Size(30, 30);
this.buttonDown.TabIndex = 10;
@@ -179,7 +186,7 @@
this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonRight.BackgroundImage = global::PIbd_22_Kalyshev_Y_V_MotorBoat_Base.Properties.Resources.right;
this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
- this.buttonRight.Location = new System.Drawing.Point(127, 537);
+ this.buttonRight.Location = new System.Drawing.Point(127, 513);
this.buttonRight.Name = "buttonRight";
this.buttonRight.Size = new System.Drawing.Size(30, 30);
this.buttonRight.TabIndex = 9;
@@ -191,7 +198,7 @@
this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonLeft.BackgroundImage = global::PIbd_22_Kalyshev_Y_V_MotorBoat_Base.Properties.Resources.left;
this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
- this.buttonLeft.Location = new System.Drawing.Point(55, 537);
+ this.buttonLeft.Location = new System.Drawing.Point(55, 513);
this.buttonLeft.Name = "buttonLeft";
this.buttonLeft.Size = new System.Drawing.Size(30, 30);
this.buttonLeft.TabIndex = 8;
@@ -203,7 +210,7 @@
this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonUp.BackgroundImage = global::PIbd_22_Kalyshev_Y_V_MotorBoat_Base.Properties.Resources.up;
this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
- this.buttonUp.Location = new System.Drawing.Point(91, 501);
+ this.buttonUp.Location = new System.Drawing.Point(91, 477);
this.buttonUp.Name = "buttonUp";
this.buttonUp.Size = new System.Drawing.Size(30, 30);
this.buttonUp.TabIndex = 7;
@@ -233,12 +240,50 @@
// 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, 24);
this.pictureBox.Name = "pictureBox";
- this.pictureBox.Size = new System.Drawing.Size(690, 587);
+ this.pictureBox.Size = new System.Drawing.Size(690, 563);
this.pictureBox.TabIndex = 1;
this.pictureBox.TabStop = false;
//
+ // menuStrip
+ //
+ 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(894, 24);
+ this.menuStrip.TabIndex = 2;
+ //
+ // файл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(180, 22);
+ this.SaveToolStripMenuItem.Text = "Сохранение";
+ //
+ // LoadToolStripMenuItem
+ //
+ this.LoadToolStripMenuItem.Name = "LoadToolStripMenuItem";
+ this.LoadToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
+ this.LoadToolStripMenuItem.Text = "Загрузка";
+ //
+ // openFileDialog
+ //
+ this.openFileDialog.Filter = "txt file | *.txt";
+ //
+ // saveFileDialog
+ //
+ this.saveFileDialog.Filter = "txt file | *.txt";
+ //
// FormMapWithSetBoats
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
@@ -246,6 +291,8 @@
this.ClientSize = new System.Drawing.Size(894, 587);
this.Controls.Add(this.pictureBox);
this.Controls.Add(this.groupBoxTools);
+ this.Controls.Add(this.menuStrip);
+ this.MainMenuStrip = this.menuStrip;
this.Name = "FormMapWithSetBoats";
this.Text = "Карта с набором объектов";
this.groupBoxTools.ResumeLayout(false);
@@ -253,7 +300,10 @@
this.groupBoxMap.ResumeLayout(false);
this.groupBoxMap.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit();
+ this.menuStrip.ResumeLayout(false);
+ this.menuStrip.PerformLayout();
this.ResumeLayout(false);
+ this.PerformLayout();
}
@@ -276,5 +326,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 SaveFileDialog saveFileDialog;
}
}
\ No newline at end of file
diff --git a/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/FormMapWithSetBoats.cs b/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/FormMapWithSetBoats.cs
index 721c00c..497cf9b 100644
--- a/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/FormMapWithSetBoats.cs
+++ b/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/FormMapWithSetBoats.cs
@@ -1,4 +1,5 @@
-using static System.Windows.Forms.DataFormats;
+using System.Windows.Forms;
+using static System.Windows.Forms.DataFormats;
namespace PIbd_22_Kalyshev_Y_V_MotorBoat_Base
{
@@ -211,5 +212,43 @@ namespace PIbd_22_Kalyshev_Y_V_MotorBoat_Base
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)
+ {
+ if (openFileDialog.ShowDialog() == DialogResult.OK)
+ {
+ if (_mapsCollection.LoadData(openFileDialog.FileName))
+ {
+ MessageBox.Show("Загрузка прошла успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
+ }
+ else
+ {
+ MessageBox.Show("Не загрузилось", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
+ }
+ }
+ }
}
}
\ No newline at end of file
diff --git a/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/FormMapWithSetBoats.resx b/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/FormMapWithSetBoats.resx
index f298a7b..934ed35 100644
--- a/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/FormMapWithSetBoats.resx
+++ b/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/FormMapWithSetBoats.resx
@@ -57,4 +57,13 @@
System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
+
+ 17, 17
+
+
+ 125, 17
+
+
+ 265, 17
+
\ No newline at end of file
diff --git a/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/IDrawningObject.cs b/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/IDrawningObject.cs
index 282547e..e2ce4e7 100644
--- a/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/IDrawningObject.cs
+++ b/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/IDrawningObject.cs
@@ -36,6 +36,11 @@ namespace PIbd_22_Kalyshev_Y_V_MotorBoat_Base
///
(float Left, float Right, float Top, float Bottom)
GetCurrentPosition();
+ ///
+ /// Получение информации по объекту
+ ///
+ ///
+ string GetInfo();
}
}
\ No newline at end of file
diff --git a/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/MapWithSetBoatsGeneric.cs b/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/MapWithSetBoatsGeneric.cs
index 467690d..0f6e139 100644
--- a/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/MapWithSetBoatsGeneric.cs
+++ b/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/MapWithSetBoatsGeneric.cs
@@ -109,6 +109,31 @@ namespace PIbd_22_Kalyshev_Y_V_MotorBoat_Base
return new(_pictureWidth, _pictureHeight);
}
///
+ /// Получение данных в виде строки
+ ///
+ ///
+ ///
+ public string GetData(char separatorType, char separatorData)
+ {
+ string data = $"{_map.GetType().Name}{separatorType}";
+ foreach (var boat in _setBoats.GetBoats())
+ {
+ data += $"{boat.GetInfo()}{separatorData}";
+ }
+ return data;
+ }
+ ///
+ /// Загрузка списка из массива строк
+ ///
+ ///
+ public void LoadData(string[] records)
+ {
+ foreach (var rec in records)
+ {
+ _setBoats.Insert(DrawningObjectBoat.Create(rec) as T);
+ }
+ }
+ ///
/// "Взбалтываем" набор, чтобы все элементы оказались в начале
///
private void Shaking()
diff --git a/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/MapsCollection.cs b/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/MapsCollection.cs
index c94f3cc..0a95df3 100644
--- a/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/MapsCollection.cs
+++ b/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/MapsCollection.cs
@@ -14,7 +14,7 @@ namespace PIbd_22_Kalyshev_Y_V_MotorBoat_Base
///
/// Словарь (хранилище) с картами
///
- readonly Dictionary> _mapStorages;
+ readonly Dictionary> _mapStorages;
///
/// Возвращение списка названий карт
///
@@ -28,13 +28,21 @@ namespace PIbd_22_Kalyshev_Y_V_MotorBoat_Base
///
private readonly int _pictureHeight;
///
+ /// Разделитель для записи ифнормации по элементу словаря в файл
+ ///
+ private readonly char separatorDict = '|';
+ ///
+ /// Разделитель для записей коллекции данных файл
+ ///
+ private readonly char separatorData = ';';
+ ///
/// Конструктор
///
///
///
public MapsCollection(int pictureWidth, int pictureHeight)
{
- _mapStorages = new Dictionary>();
+ _mapStorages = new Dictionary>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
@@ -45,7 +53,7 @@ namespace PIbd_22_Kalyshev_Y_V_MotorBoat_Base
/// Карта
public void AddMap(string name, AbstractMap map)
{
- _mapStorages.Add(name, new MapWithSetBoatsGeneric(_pictureWidth, _pictureHeight, map));
+ _mapStorages.Add(name, new MapWithSetBoatsGeneric(_pictureWidth, _pictureHeight, map));
}
///
/// Удаление карты
@@ -60,7 +68,7 @@ namespace PIbd_22_Kalyshev_Y_V_MotorBoat_Base
///
///
///
- public MapWithSetBoatsGeneric this[string ind]
+ public MapWithSetBoatsGeneric this[string ind]
{
get
{
@@ -71,5 +79,83 @@ namespace PIbd_22_Kalyshev_Y_V_MotorBoat_Base
return null;
}
}
+ ///
+ /// Метод записи информации в файл
+ ///
+ /// Строка, которую следует записать
+ /// Поток для записи
+ 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 _mapStorages)
+ {
+ 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;
+ }
+ //очищаем записи
+ _mapStorages.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 "MySecondMap":
+ map = new MySecondMap();
+ break;
+ }
+ _mapStorages.Add(elem[0], new MapWithSetBoatsGeneric(_pictureWidth, _pictureHeight, map));
+ _mapStorages[elem[0]].LoadData(elem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries));
+ }
+ return true;
+ }
}
}