commit 6 full

This commit is contained in:
Дарья Антонова 2022-12-17 02:44:26 +04:00
parent e4a261f7fd
commit b0ae098ee0
8 changed files with 289 additions and 22 deletions

View File

@ -31,5 +31,7 @@ namespace AirBomber
{
_jet.DrawTransport(g);
}
public string GetInfo() => _jet?.GetDataForSave();
public static IDrawningObject Create(string data) => new DrawningObjectJet(data.CreateDrawningJet());
}
}

View File

@ -0,0 +1,56 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AirBomber
{
/// <summary>
/// Расширение для класса DrawningJet
/// </summary>
internal static class ExtentionJet
{
/// <summary>
/// Разделитель для записи информации по объекту в файл
/// </summary>
private static readonly char _separatorForObject = ':';
/// <summary>
/// Создание объекта из строки
/// </summary>
/// <param name="info"></param>
/// <returns></returns>
public static DrawningJet CreateDrawningJet(this string info)
{
string[] strs = info.Split(_separatorForObject);
if (strs.Length == 3)
{
return new DrawningJet(Convert.ToInt32(strs[0]),
Convert.ToInt32(strs[1]), Color.FromName(strs[2]));
}
if (strs.Length == 7)
{
return new DrawningSportJet(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;
}
/// <summary>
/// Получение данных для сохранения в файл
/// </summary>
/// <param name="drawningJet"></param>
/// <returns></returns>
public static string GetDataForSave(this DrawningJet drawningJet)
{
var jet = drawningJet.Jet;
var str = $"{jet.Speed}{_separatorForObject}{jet.Weight}{_separatorForObject}{jet.BodyColor.Name}";
if (jet is not EntitySportJet sportCar)
{
return str;
}
return $"{str}{_separatorForObject}{sportCar.DopColor.Name}{_separatorForObject}{sportCar.BodyKit}{_separatorForObject}{sportCar.Wing}";
}
}
}

View File

@ -49,9 +49,16 @@
this.ButtonAddJet = new System.Windows.Forms.Button();
this.ButtonRemoveJet = new System.Windows.Forms.Button();
this.pictureBoxJet = 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.groupBox1.SuspendLayout();
this.groupBox2.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxJet)).BeginInit();
this.menuStrip1.SuspendLayout();
this.SuspendLayout();
//
// buttonDown1
@ -59,7 +66,7 @@
this.buttonDown1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonDown1.BackgroundImage = global::AirBomber.Properties.Resources.arrowDown;
this.buttonDown1.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.buttonDown1.Location = new System.Drawing.Point(68, 899);
this.buttonDown1.Location = new System.Drawing.Point(68, 884);
this.buttonDown1.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2);
this.buttonDown1.Name = "buttonDown1";
this.buttonDown1.Size = new System.Drawing.Size(35, 30);
@ -68,7 +75,7 @@
//
// groupBox1
//
this.groupBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
this.groupBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Right)));
this.groupBox1.Controls.Add(this.groupBox2);
this.groupBox1.Controls.Add(this.buttonRight);
@ -84,9 +91,9 @@
this.groupBox1.Controls.Add(this.maskedTextBoxPosition);
this.groupBox1.Controls.Add(this.ButtonAddJet);
this.groupBox1.Controls.Add(this.ButtonRemoveJet);
this.groupBox1.Location = new System.Drawing.Point(893, 12);
this.groupBox1.Location = new System.Drawing.Point(893, 27);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(191, 561);
this.groupBox1.Size = new System.Drawing.Size(191, 546);
this.groupBox1.TabIndex = 7;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "Инструменты";
@ -156,7 +163,7 @@
this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonRight.BackgroundImage = global::AirBomber.Properties.Resources.arrowRight;
this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.buttonRight.Location = new System.Drawing.Point(116, 509);
this.buttonRight.Location = new System.Drawing.Point(116, 494);
this.buttonRight.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2);
this.buttonRight.Name = "buttonRight";
this.buttonRight.Size = new System.Drawing.Size(35, 30);
@ -169,7 +176,7 @@
this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonLeft.BackgroundImage = global::AirBomber.Properties.Resources.arrowLeft;
this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.buttonLeft.Location = new System.Drawing.Point(34, 509);
this.buttonLeft.Location = new System.Drawing.Point(34, 494);
this.buttonLeft.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2);
this.buttonLeft.Name = "buttonLeft";
this.buttonLeft.Size = new System.Drawing.Size(35, 30);
@ -182,7 +189,7 @@
this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonUp.BackgroundImage = global::AirBomber.Properties.Resources.arrowUp;
this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.buttonUp.Location = new System.Drawing.Point(75, 475);
this.buttonUp.Location = new System.Drawing.Point(75, 460);
this.buttonUp.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2);
this.buttonUp.Name = "buttonUp";
this.buttonUp.Size = new System.Drawing.Size(35, 30);
@ -195,7 +202,7 @@
this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonDown.BackgroundImage = global::AirBomber.Properties.Resources.arrowDown;
this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.buttonDown.Location = new System.Drawing.Point(75, 509);
this.buttonDown.Location = new System.Drawing.Point(75, 494);
this.buttonDown.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2);
this.buttonDown.Name = "buttonDown";
this.buttonDown.Size = new System.Drawing.Size(35, 30);
@ -208,7 +215,7 @@
this.buttonRight1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonRight1.BackgroundImage = global::AirBomber.Properties.Resources.arrowRight;
this.buttonRight1.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.buttonRight1.Location = new System.Drawing.Point(109, 899);
this.buttonRight1.Location = new System.Drawing.Point(109, 884);
this.buttonRight1.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2);
this.buttonRight1.Name = "buttonRight1";
this.buttonRight1.Size = new System.Drawing.Size(35, 30);
@ -220,7 +227,7 @@
this.buttonLeft1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonLeft1.BackgroundImage = global::AirBomber.Properties.Resources.arrowLeft;
this.buttonLeft1.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.buttonLeft1.Location = new System.Drawing.Point(27, 899);
this.buttonLeft1.Location = new System.Drawing.Point(27, 884);
this.buttonLeft1.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2);
this.buttonLeft1.Name = "buttonLeft1";
this.buttonLeft1.Size = new System.Drawing.Size(35, 30);
@ -232,7 +239,7 @@
this.buttonUp1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonUp1.BackgroundImage = global::AirBomber.Properties.Resources.arrowUp;
this.buttonUp1.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.buttonUp1.Location = new System.Drawing.Point(68, 865);
this.buttonUp1.Location = new System.Drawing.Point(68, 850);
this.buttonUp1.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2);
this.buttonUp1.Name = "buttonUp1";
this.buttonUp1.Size = new System.Drawing.Size(35, 30);
@ -288,15 +295,56 @@
//
// pictureBoxJet
//
this.pictureBoxJet.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
this.pictureBoxJet.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.pictureBoxJet.Location = new System.Drawing.Point(0, 0);
this.pictureBoxJet.Location = new System.Drawing.Point(0, 27);
this.pictureBoxJet.Name = "pictureBoxJet";
this.pictureBoxJet.Size = new System.Drawing.Size(887, 573);
this.pictureBoxJet.Size = new System.Drawing.Size(887, 546);
this.pictureBoxJet.TabIndex = 6;
this.pictureBoxJet.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(1096, 24);
this.menuStrip1.TabIndex = 8;
this.menuStrip1.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(48, 20);
this.файлToolStripMenuItem.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);
//
// openFileDialog
//
this.openFileDialog.Filter = "txt file | *.txt";
//
// saveFileDialog
//
this.saveFileDialog.Filter = "txt file | *.txt";
//
// FormMapWithSetJets
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
@ -304,6 +352,8 @@
this.ClientSize = new System.Drawing.Size(1096, 573);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.pictureBoxJet);
this.Controls.Add(this.menuStrip1);
this.MainMenuStrip = this.menuStrip1;
this.Name = "FormMapWithSetJets";
this.Text = "Карта с набором объектов";
this.groupBox1.ResumeLayout(false);
@ -311,7 +361,10 @@
this.groupBox2.ResumeLayout(false);
this.groupBox2.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxJet)).EndInit();
this.menuStrip1.ResumeLayout(false);
this.menuStrip1.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
@ -338,5 +391,11 @@
private ListBox listBoxMaps;
private Button ButtonDeleteMap;
private TextBox textBoxNewMapName;
private MenuStrip menuStrip1;
private ToolStripMenuItem файлToolStripMenuItem;
private ToolStripMenuItem SaveToolStripMenuItem;
private ToolStripMenuItem LoadToolStripMenuItem;
private OpenFileDialog openFileDialog;
private SaveFileDialog saveFileDialog;
}
}

View File

@ -234,5 +234,36 @@ namespace AirBomber
}
pictureBoxJet.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);
}
}
}
}
}

View File

@ -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>417, 19</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>272, 17</value>
</metadata>
</root>

View File

@ -35,5 +35,10 @@ namespace AirBomber
/// </summary>
/// <returns></returns>
(float Left, float Right, float Top, float Bottom) GetCurrentPosition();
/// <summary>
/// Получение информации по объекту
/// </summary>
/// <returns></returns>
string GetInfo();
}
}

View File

@ -197,5 +197,31 @@ namespace AirBomber
}
}
}
/// <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 car in _setJets.GetJets())
{
data += $"{car.GetInfo()}{separatorData}";
}
return data;
}
/// <summary>
/// Загрузка списка из массива строк
/// </summary>
/// <param name="records"></param>
public void LoadData(string[] records)
{
foreach (var rec in records)
{
_setJets.Insert(DrawningObjectJet.Create(rec) as T);
}
}
}
}

View File

@ -14,7 +14,7 @@ namespace AirBomber
/// <summary>
/// Словарь (хранилище) с картами
/// </summary>
readonly Dictionary<string, MapWithSetJetsGeneric<DrawningObjectJet, AbstractMap>> _mapStorages;
readonly Dictionary<string, MapWithSetJetsGeneric<IDrawningObject, AbstractMap>> _mapStorages;
/// <summary>
/// Возвращение списка названий карт
/// </summary>
@ -28,13 +28,21 @@ namespace AirBomber
/// </summary>
private readonly int _pictureHeight;
/// <summary>
/// Разделитель карт
/// </summary>
private static readonly char separatorDict = '|';
/// <summary>
/// Разделитель самолетов
/// </summary>
private static readonly char separatorData = ',';
/// <summary>
/// Конструктор
/// </summary>
/// <param name="pictureWidth"></param>
/// <param name="pictureHeight"></param>
public MapsCollection(int pictureWidth, int pictureHeight)
{
_mapStorages = new Dictionary<string, MapWithSetJetsGeneric<DrawningObjectJet, AbstractMap>>();
_mapStorages = new Dictionary<string, MapWithSetJetsGeneric<IDrawningObject, AbstractMap>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
@ -45,14 +53,14 @@ namespace AirBomber
/// <param name="map">Карта</param>
public void AddMap(string name, AbstractMap map)
{
MapWithSetJetsGeneric<DrawningObjectJet, AbstractMap> mapJetsCollectionGeneric;
MapWithSetJetsGeneric<IDrawningObject, AbstractMap> mapJetsCollectionGeneric;
// Если карта или её имя не задано - выходим
if (map == null || string.IsNullOrEmpty(name)) return;
// Если совершается попытка добавить карту с уже существующим именем, то выходим
if (_mapStorages.ContainsKey(name)) return;
mapJetsCollectionGeneric = new MapWithSetJetsGeneric<DrawningObjectJet, AbstractMap>(_pictureWidth, _pictureHeight, map);
mapJetsCollectionGeneric = new MapWithSetJetsGeneric<IDrawningObject, AbstractMap>(_pictureWidth, _pictureHeight, map);
_mapStorages.Add(name, mapJetsCollectionGeneric);
}
@ -72,14 +80,85 @@ namespace AirBomber
/// </summary>
/// <param name="ind"></param>
/// <returns></returns>
public MapWithSetJetsGeneric<DrawningObjectJet, AbstractMap> this[string ind]
public MapWithSetJetsGeneric<IDrawningObject, AbstractMap> this[string ind]
{
get
{
if (string.IsNullOrEmpty(ind)) return null;
return _mapStorages[ind];
_mapStorages.TryGetValue(ind, out var result);
return result;
}
}
/// <summary>
/// Метод записи информации в файл
/// </summary>
/// <param name="text">Строка, которую следует записать</param>
/// <param name="stream">Поток для записи</param>
private static void WriteToFile(string text, FileStream stream)
{
byte[] info = new UTF8Encoding(true).GetBytes(text);
stream.Write(info, 0, info.Length);
}
/// <summary>
/// Сохранение информации по самолетам в хранилище в файл
/// </summary>
/// <param name="filename">Путь и имя файла</param>
/// <returns></returns>
public bool SaveData(string filename)
{
if (File.Exists(filename))
{
File.Delete(filename);
}
using (StreamWriter fs = new(filename))
{
fs.Write($"MapsCollection{Environment.NewLine}");
foreach (var storage in _mapStorages)
{
fs.Write($"{storage.Key}{separatorDict}{storage.Value.GetData(separatorDict, separatorData)}{Environment.NewLine}");
}
}
return true;
}
/// <summary>
/// Загрузка информации по самолетам из файла
/// </summary>
/// <param name="filename"></param>
/// <returns></returns>
public bool LoadData(string filename)
{
if (!File.Exists(filename))
{
return false;
}
using (StreamReader sr = new(filename))
{
string str = "";
if ((str = sr.ReadLine()) == null || !str.Contains("MapsCollection"))
{
//если нет такой записи, то это не те данные
return false;
}
//очищаем записи
_mapStorages.Clear();
while ((str = sr.ReadLine()) != null)
{
var elem = str.Split(separatorDict);
AbstractMap map = null;
switch (elem[1])
{
case "Простая карта":
map = new SimpleMap();
break;
case "Небо":
map = new SkyMap();
break;
}
_mapStorages.Add(elem[0], new MapWithSetJetsGeneric<IDrawningObject, AbstractMap>(_pictureWidth, _pictureHeight, map));
_mapStorages[elem[0]].LoadData(elem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries));
}
}
return true;
}
}
}