diff --git a/AntiAircraftGun/AntiAircraftGun/DrawingObjectAntiAircraftGun.cs b/AntiAircraftGun/AntiAircraftGun/DrawingObjectAntiAircraftGun.cs
index 6f82639..bd7fc3c 100644
--- a/AntiAircraftGun/AntiAircraftGun/DrawingObjectAntiAircraftGun.cs
+++ b/AntiAircraftGun/AntiAircraftGun/DrawingObjectAntiAircraftGun.cs
@@ -18,6 +18,7 @@ namespace AntiAircraftGun
{
return _antiAircraftGun?.GetCurrentPosition() ?? default;
}
+
public void MoveObject(Direction direction)
{
_antiAircraftGun?.MoveTransport(direction);
@@ -31,5 +32,8 @@ namespace AntiAircraftGun
{
_antiAircraftGun?.DrawTransport(g);
}
+
+ public string GetInfo() => _antiAircraftGun?.GetDataForSave();
+ public static IDrawingObject Create(string data) => new DrawingObjectAntiAircraftGun(data.CreateDrawingAntiAircraftGun());
}
}
diff --git a/AntiAircraftGun/AntiAircraftGun/ExtentionAntiAircraftGun.cs b/AntiAircraftGun/AntiAircraftGun/ExtentionAntiAircraftGun.cs
new file mode 100644
index 0000000..9e21588
--- /dev/null
+++ b/AntiAircraftGun/AntiAircraftGun/ExtentionAntiAircraftGun.cs
@@ -0,0 +1,56 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace AntiAircraftGun
+{
+ ///
+ /// Расширение для класса DrawingAntiAircraftGun
+ ///
+ internal static class ExtentionAntiAircraftGun
+ {
+ ///
+ /// Разделитель для записи информации по объекту в файл
+ ///
+ private static readonly char _separatorForObject = ':';
+ ///
+ /// Создание объекта из строки
+ ///
+ ///
+ ///
+ public static DrawingAntiAircraftGun CreateDrawingAntiAircraftGun(this string info)
+ {
+ string[] strs = info.Split(_separatorForObject);
+ if (strs.Length == 3)
+ {
+ return new DrawingAntiAircraftGun(Convert.ToInt32(strs[0]),
+ Convert.ToInt32(strs[1]), Color.FromName(strs[2]));
+ }
+ if (strs.Length == 6)
+ {
+ return new DrawingUpdateAntiAircraftGun(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 DrawingAntiAircraftGun drawingAntiAircraftGun)
+ {
+ var antiAircraftGun = drawingAntiAircraftGun.AntiAircraftGun;
+ var str = $"{antiAircraftGun.Speed}{_separatorForObject}{antiAircraftGun.Weight}{_separatorForObject}{antiAircraftGun.BodyColor.Name}";
+ if (antiAircraftGun is not EntityUpdateAntiAircraftGun updateAntiAircraftGun)
+ {
+ return str;
+ }
+ return $"{str}{_separatorForObject}{updateAntiAircraftGun.DopColor.Name}{_separatorForObject}{updateAntiAircraftGun.Gun}{_separatorForObject}{updateAntiAircraftGun.Radar}";
+ }
+ }
+}
diff --git a/AntiAircraftGun/AntiAircraftGun/FormMapWithSetAntiAircraftGuns.Designer.cs b/AntiAircraftGun/AntiAircraftGun/FormMapWithSetAntiAircraftGuns.Designer.cs
index c515e95..9d7a137 100644
--- a/AntiAircraftGun/AntiAircraftGun/FormMapWithSetAntiAircraftGuns.Designer.cs
+++ b/AntiAircraftGun/AntiAircraftGun/FormMapWithSetAntiAircraftGuns.Designer.cs
@@ -45,9 +45,16 @@
this.buttonDeleteMap = new System.Windows.Forms.Button();
this.buttonAddMap = new System.Windows.Forms.Button();
this.textBoxNewMapName = new System.Windows.Forms.TextBox();
+ 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.groupBox.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit();
this.groupBoxMaps.SuspendLayout();
+ this.menuStrip.SuspendLayout();
this.SuspendLayout();
//
// groupBox
@@ -62,9 +69,9 @@
this.groupBox.Controls.Add(this.buttonLeft);
this.groupBox.Controls.Add(this.buttonUp);
this.groupBox.Dock = System.Windows.Forms.DockStyle.Right;
- this.groupBox.Location = new System.Drawing.Point(574, 0);
+ this.groupBox.Location = new System.Drawing.Point(574, 28);
this.groupBox.Name = "groupBox";
- this.groupBox.Size = new System.Drawing.Size(238, 589);
+ this.groupBox.Size = new System.Drawing.Size(238, 579);
this.groupBox.TabIndex = 0;
this.groupBox.TabStop = false;
this.groupBox.Text = "Инструменты";
@@ -122,7 +129,7 @@
this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonDown.BackgroundImage = global::AntiAircraftGun.Properties.Resources.arrowDown;
this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
- this.buttonDown.Location = new System.Drawing.Point(108, 547);
+ this.buttonDown.Location = new System.Drawing.Point(108, 537);
this.buttonDown.Name = "buttonDown";
this.buttonDown.Size = new System.Drawing.Size(30, 29);
this.buttonDown.TabIndex = 10;
@@ -134,7 +141,7 @@
this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonRight.BackgroundImage = global::AntiAircraftGun.Properties.Resources.arrowRight;
this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
- this.buttonRight.Location = new System.Drawing.Point(143, 547);
+ this.buttonRight.Location = new System.Drawing.Point(143, 537);
this.buttonRight.Name = "buttonRight";
this.buttonRight.Size = new System.Drawing.Size(30, 29);
this.buttonRight.TabIndex = 9;
@@ -146,7 +153,7 @@
this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonLeft.BackgroundImage = global::AntiAircraftGun.Properties.Resources.arrowLeft;
this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
- this.buttonLeft.Location = new System.Drawing.Point(71, 547);
+ this.buttonLeft.Location = new System.Drawing.Point(71, 537);
this.buttonLeft.Name = "buttonLeft";
this.buttonLeft.Size = new System.Drawing.Size(30, 29);
this.buttonLeft.TabIndex = 8;
@@ -158,7 +165,7 @@
this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonUp.BackgroundImage = global::AntiAircraftGun.Properties.Resources.arrowUp;
this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
- this.buttonUp.Location = new System.Drawing.Point(108, 509);
+ this.buttonUp.Location = new System.Drawing.Point(108, 499);
this.buttonUp.Name = "buttonUp";
this.buttonUp.Size = new System.Drawing.Size(30, 29);
this.buttonUp.TabIndex = 7;
@@ -180,9 +187,9 @@
// 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(574, 589);
+ this.pictureBox.Size = new System.Drawing.Size(574, 579);
this.pictureBox.TabIndex = 1;
this.pictureBox.TabStop = false;
//
@@ -238,14 +245,57 @@
this.textBoxNewMapName.Size = new System.Drawing.Size(194, 27);
this.textBoxNewMapName.TabIndex = 0;
//
+ // 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(812, 28);
+ this.menuStrip.TabIndex = 18;
+ //
+ // файл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";
+ //
// FormMapWithSetAntiAircraftGuns
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
- this.ClientSize = new System.Drawing.Size(812, 589);
+ this.ClientSize = new System.Drawing.Size(812, 607);
this.Controls.Add(this.groupBoxMaps);
this.Controls.Add(this.pictureBox);
this.Controls.Add(this.groupBox);
+ this.Controls.Add(this.menuStrip);
+ this.MainMenuStrip = this.menuStrip;
this.Name = "FormMapWithSetAntiAircraftGuns";
this.Text = "FormMapWithSetAntiAircraftGuns";
this.groupBox.ResumeLayout(false);
@@ -253,7 +303,10 @@
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit();
this.groupBoxMaps.ResumeLayout(false);
this.groupBoxMaps.PerformLayout();
+ this.menuStrip.ResumeLayout(false);
+ this.menuStrip.PerformLayout();
this.ResumeLayout(false);
+ this.PerformLayout();
}
@@ -276,5 +329,11 @@
private Button buttonDeleteMap;
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/AntiAircraftGun/AntiAircraftGun/FormMapWithSetAntiAircraftGuns.cs b/AntiAircraftGun/AntiAircraftGun/FormMapWithSetAntiAircraftGuns.cs
index e7c6c51..f8af888 100644
--- a/AntiAircraftGun/AntiAircraftGun/FormMapWithSetAntiAircraftGuns.cs
+++ b/AntiAircraftGun/AntiAircraftGun/FormMapWithSetAntiAircraftGuns.cs
@@ -217,5 +217,44 @@ namespace AntiAircraftGun
}
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);
+ ReloadMaps();
+ }
+ else
+ {
+ MessageBox.Show("Не загрузилось", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
+ }
+ }
+ }
}
}
diff --git a/AntiAircraftGun/AntiAircraftGun/FormMapWithSetAntiAircraftGuns.resx b/AntiAircraftGun/AntiAircraftGun/FormMapWithSetAntiAircraftGuns.resx
index f298a7b..77f1fe0 100644
--- a/AntiAircraftGun/AntiAircraftGun/FormMapWithSetAntiAircraftGuns.resx
+++ b/AntiAircraftGun/AntiAircraftGun/FormMapWithSetAntiAircraftGuns.resx
@@ -57,4 +57,16 @@
System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
+
+ 17, 17
+
+
+ 144, 17
+
+
+ 311, 17
+
+
+ 25
+
\ No newline at end of file
diff --git a/AntiAircraftGun/AntiAircraftGun/IDrawingObject.cs b/AntiAircraftGun/AntiAircraftGun/IDrawingObject.cs
index ee0ad14..6128b09 100644
--- a/AntiAircraftGun/AntiAircraftGun/IDrawingObject.cs
+++ b/AntiAircraftGun/AntiAircraftGun/IDrawingObject.cs
@@ -38,5 +38,10 @@ namespace AntiAircraftGun
///
///
(float Left, float Right, float Top, float Bottom) GetCurrentPosition();
+ ///
+ /// Получение информации по объекту
+ ///
+ ///
+ string GetInfo();
}
}
diff --git a/AntiAircraftGun/AntiAircraftGun/MapWithSetAntiAircraftGunsGeneric.cs b/AntiAircraftGun/AntiAircraftGun/MapWithSetAntiAircraftGunsGeneric.cs
index a4b8b30..aaaa625 100644
--- a/AntiAircraftGun/AntiAircraftGun/MapWithSetAntiAircraftGunsGeneric.cs
+++ b/AntiAircraftGun/AntiAircraftGun/MapWithSetAntiAircraftGunsGeneric.cs
@@ -113,6 +113,31 @@ namespace AntiAircraftGun
return new(_pictureWidth, _pictureHeight);
}
///
+ /// Получение данных в виде строки
+ ///
+ ///
+ ///
+ public string GetData(char separatorType, char separatorData)
+ {
+ string data = $"{_map.GetType().Name}{separatorType}";
+ foreach (var antiAircraftGun in _setAntiAircraftGuns.GetAntiAircraftGuns())
+ {
+ data += $"{antiAircraftGun.GetInfo()}{separatorData}";
+ }
+ return data;
+ }
+ ///
+ /// Загрузка списка из массива строк
+ ///
+ ///
+ public void LoadData(string[] records)
+ {
+ foreach (var rec in records)
+ {
+ _setAntiAircraftGuns.Insert(DrawingObjectAntiAircraftGun.Create(rec) as T);
+ }
+ }
+ ///
/// "Взбалтываем" набор, чтобы все элементы оказались в начале
///
private void Shaking()
diff --git a/AntiAircraftGun/AntiAircraftGun/MapsCollection.cs b/AntiAircraftGun/AntiAircraftGun/MapsCollection.cs
index 9fafc57..02de5e8 100644
--- a/AntiAircraftGun/AntiAircraftGun/MapsCollection.cs
+++ b/AntiAircraftGun/AntiAircraftGun/MapsCollection.cs
@@ -11,7 +11,7 @@ namespace AntiAircraftGun
///
/// Словарь (хранилище) с картами
///
- readonly Dictionary> _mapStorages;
+ readonly Dictionary> _mapStorages;
///
/// Возвращение списка названий карт
///
@@ -25,13 +25,21 @@ namespace AntiAircraftGun
///
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;
}
@@ -42,7 +50,7 @@ namespace AntiAircraftGun
/// Карта
public void AddMap(string name, AbstractMap map)
{
- if(!_mapStorages.ContainsKey(name)) _mapStorages.Add(name, new MapWithSetAntiAircraftGunsGeneric(_pictureWidth, _pictureHeight, map));
+ if(!_mapStorages.ContainsKey(name)) _mapStorages.Add(name, new MapWithSetAntiAircraftGunsGeneric(_pictureWidth, _pictureHeight, map));
}
///
/// Удаление карты
@@ -57,7 +65,7 @@ namespace AntiAircraftGun
///
///
///
- public MapWithSetAntiAircraftGunsGeneric this[string ind]
+ public MapWithSetAntiAircraftGunsGeneric this[string ind]
{
get
{
@@ -65,5 +73,65 @@ namespace AntiAircraftGun
return null;
}
}
+ ///
+ /// Сохранение информации по орудиям в хранилище в файл
+ ///
+ /// Путь и имя файла
+ ///
+ public bool SaveData(string filename)
+ {
+ if (File.Exists(filename))
+ {
+ File.Delete(filename);
+ }
+ using (StreamWriter sw = new StreamWriter(filename))
+ {
+ sw.WriteLine($"MapsCollection");
+ foreach (var storage in _mapStorages)
+ {
+ sw.WriteLine($"{storage.Key}{separatorDict}{storage.Value.GetData(separatorDict, separatorData)}");
+ }
+ }
+ return true;
+ }
+ ///
+ /// Загрузка нформации по орудиям на парковках из файла
+ ///
+ ///
+ ///
+ public bool LoadData(string filename)
+ {
+ if (!File.Exists(filename))
+ {
+ return false;
+ }
+ using (StreamReader sr = new StreamReader(filename))
+ {
+ string str;
+ _mapStorages.Clear();
+ str = sr.ReadLine();
+ if (!str.Contains("MapsCollection"))
+ {
+ return false;
+ }
+ while ((str = sr.ReadLine()) != null)
+ {
+ var elem = str.Split(separatorDict);
+ AbstractMap map = null;
+ switch (elem[1])
+ {
+ case "SimpleMap":
+ map = new SimpleMap();
+ break;
+ case "SimpleMap2":
+ map = new SimpleMap2();
+ break;
+ }
+ _mapStorages.Add(elem[0], new MapWithSetAntiAircraftGunsGeneric(_pictureWidth, _pictureHeight, map));
+ _mapStorages[elem[0]].LoadData(elem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries));
+ }
+ return true;
+ }
+ }
}
}