Pyatakov K.M. LabWork6 #11

Closed
ker73rus wants to merge 5 commits from LabWork6 into LabWork5
8 changed files with 247 additions and 16 deletions

View File

@ -22,6 +22,11 @@ namespace Stormtrooper
return _airplane?.GetCurrentPosition() ?? default;
}
public string GetInfo()
{
return _airplane?.GetDataForSave();
}
public void MoveObject(Direction direction)
{
_airplane?.MoveAirplane(direction);
@ -36,5 +41,8 @@ namespace Stormtrooper
{
_airplane.DrawAirplane(g);
}
public static IDrawningObject Create(string data) => new DrawningObject(data.CreateDrawningAirplane());
}
}

View File

@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Stormtrooper
{
internal static class ExtentionAirplane
{
private static readonly char _separatorForObject = ':';
public static string GetDataForSave(this DrawningMilitaryAirplane drawningMilitaryAirplane)
{
var air = drawningMilitaryAirplane.Airplane;
var str = $"{air.Speed}{_separatorForObject}{air.Weight}{_separatorForObject}{air.Color.ToArgb()}";
if (air is not EntityStormtrooper stormtrooper)
{
return str;
}
else return
$"{str}{_separatorForObject}{stormtrooper.AdvColor.ToArgb()}{_separatorForObject}{stormtrooper.Rockets}{_separatorForObject}{stormtrooper.Booster}{_separatorForObject}{stormtrooper.Radar}";
}
public static DrawningMilitaryAirplane CreateDrawningAirplane(this string info)
{
string[] strs = info.Split(_separatorForObject);
if (strs.Length == 3)
{
return new DrawningMilitaryAirplane(Convert.ToInt32(strs[0]),
Convert.ToInt32(strs[1]), Color.FromArgb(Convert.ToInt32(strs[2])));
}
if (strs.Length == 7)
{
return new DrawningStormtrooper(Convert.ToInt32(strs[0]),
Convert.ToInt32(strs[1]), Color.FromArgb(Convert.ToInt32(strs[2])),
Color.FromArgb(Convert.ToInt32(strs[3])), Convert.ToBoolean(strs[4]),
Convert.ToBoolean(strs[5]), Convert.ToBoolean(strs[6]));
}
return null;
}
}
}

View File

@ -45,9 +45,16 @@
this.maskedTextBoxPosition = new System.Windows.Forms.MaskedTextBox();
this.buttonCreate = 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.saveFileDialog = new System.Windows.Forms.SaveFileDialog();
this.openFileDialog = new System.Windows.Forms.OpenFileDialog();
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.maskedTextBoxPosition);
this.groupBoxTools.Controls.Add(this.buttonCreate);
this.groupBoxTools.Dock = System.Windows.Forms.DockStyle.Right;
this.groupBoxTools.Location = new System.Drawing.Point(600, 0);
this.groupBoxTools.Location = new System.Drawing.Point(1069, 24);
this.groupBoxTools.Name = "groupBoxTools";
this.groupBoxTools.Size = new System.Drawing.Size(200, 588);
this.groupBoxTools.Size = new System.Drawing.Size(200, 564);
this.groupBoxTools.TabIndex = 0;
this.groupBoxTools.TabStop = false;
this.groupBoxTools.Text = "Инструменты";
@ -137,7 +144,7 @@
// buttonRight
//
this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonRight.Location = new System.Drawing.Point(119, 540);
this.buttonRight.Location = new System.Drawing.Point(119, 516);
this.buttonRight.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3);
this.buttonRight.Name = "buttonRight";
this.buttonRight.Size = new System.Drawing.Size(35, 35);
@ -148,7 +155,7 @@
// buttonLeft
//
this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonLeft.Location = new System.Drawing.Point(35, 540);
this.buttonLeft.Location = new System.Drawing.Point(35, 516);
this.buttonLeft.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3);
this.buttonLeft.Name = "buttonLeft";
this.buttonLeft.Size = new System.Drawing.Size(35, 35);
@ -159,7 +166,7 @@
// buttonDown
//
this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonDown.Location = new System.Drawing.Point(77, 541);
this.buttonDown.Location = new System.Drawing.Point(77, 517);
this.buttonDown.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3);
this.buttonDown.Name = "buttonDown";
this.buttonDown.Size = new System.Drawing.Size(35, 35);
@ -170,7 +177,7 @@
// buttonUp
//
this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonUp.Location = new System.Drawing.Point(77, 499);
this.buttonUp.Location = new System.Drawing.Point(77, 475);
this.buttonUp.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3);
this.buttonUp.Name = "buttonUp";
this.buttonUp.Size = new System.Drawing.Size(35, 35);
@ -230,19 +237,58 @@
// 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(600, 588);
this.pictureBox.Size = new System.Drawing.Size(1069, 564);
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(1269, 24);
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(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.FileName = "openFileDialog1";
//
// FormMapWithSetAirplane
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 588);
this.ClientSize = new System.Drawing.Size(1269, 588);
this.Controls.Add(this.pictureBox);
this.Controls.Add(this.groupBoxTools);
this.Controls.Add(this.menuStrip);
this.MainMenuStrip = this.menuStrip;
this.Name = "FormMapWithSetAirplane";
this.Text = "FormMapWithSetAirplane";
this.groupBoxTools.ResumeLayout(false);
@ -250,7 +296,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();
}
@ -273,5 +322,11 @@
private Button buttonAddMap;
private Button buttonRemoveMap;
private ListBox listBoxMap;
private MenuStrip menuStrip;
private ToolStripMenuItem файлToolStripMenuItem;
private ToolStripMenuItem SaveToolStripMenuItem;
private ToolStripMenuItem LoadToolStripMenuItem;
private SaveFileDialog saveFileDialog;
private OpenFileDialog openFileDialog;
}
}

View File

@ -24,10 +24,11 @@ namespace Stormtrooper
/// Объект от коллекции карт
/// </summary>
private readonly MapCollection _mapCollection;
private MapWithSetAirplaneGeneric<DrawningObject, AbstractMap> _mapAirsCollectionGeneric;
public FormMapWithSetAirplane()
{
InitializeComponent();
openFileDialog.Filter = "Text files(*.txt)|*.txt";
saveFileDialog.Filter = "Text files(*.txt)|*.txt";
_mapCollection = new MapCollection(pictureBox.Width, pictureBox.Height);
comboBoxMapSelector.Items.Clear();
foreach(var map in _mapsDict)
@ -205,5 +206,36 @@ namespace Stormtrooper
ReloadMaps();
}
}
private void SaveToolStripMenuItem_Click(object sender, EventArgs e)
{
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
if (_mapCollection.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 (_mapCollection.LoadData(openFileDialog.FileName))
{
ReloadMaps();
MessageBox.Show("Загрузка прошла успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
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="menuStrip.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
<metadata name="saveFileDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>125, 17</value>
</metadata>
<metadata name="openFileDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>254, 17</value>
</metadata>
</root>

View File

@ -36,5 +36,7 @@ namespace Stormtrooper
/// </summary>
/// <returns></returns>
(float Left, float Top, float Right, float Bottom) GetCurrentPosition();
string GetInfo();
}
}

View File

@ -1,4 +1,5 @@
using System;
using Strormtrooper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@ -11,7 +12,9 @@ namespace Stormtrooper
/// <summary>
/// Словарь (хранилище) с картами
/// </summary>
readonly Dictionary<string, MapWithSetAirplaneGeneric<DrawningObject, AbstractMap>> _mapStorages;
readonly Dictionary<string, MapWithSetAirplaneGeneric<IDrawningObject, AbstractMap>> _mapStorages;
private readonly char separatorDict = '|';
private readonly char separatorData = ';';
/// <summary>
/// Возвращение списка названий карт
/// </summary>
@ -31,7 +34,7 @@ namespace Stormtrooper
/// <param name="pictureHeight"></param>
public MapCollection(int pictureWidth, int pictureHeight)
{
_mapStorages = new Dictionary<string, MapWithSetAirplaneGeneric<DrawningObject, AbstractMap>>();
_mapStorages = new Dictionary<string, MapWithSetAirplaneGeneric<IDrawningObject, AbstractMap>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
@ -43,7 +46,7 @@ namespace Stormtrooper
public void AddMap(string name, AbstractMap map)
{
// Добавление карты
MapWithSetAirplaneGeneric<DrawningObject, AbstractMap> newMap = new(_pictureWidth, _pictureHeight, map);
MapWithSetAirplaneGeneric<IDrawningObject, AbstractMap> newMap = new(_pictureWidth, _pictureHeight, map);
if (!_mapStorages.ContainsKey(name))
_mapStorages.Add(name, newMap);
}
@ -60,12 +63,74 @@ namespace Stormtrooper
}
_mapStorages.Remove(name);
}
public bool SaveData(string filename)
{
if (File.Exists(filename))
{
File.Delete(filename);
}
using (StreamWriter sw = new(filename))
{
sw.Write($"MapCollection{Environment.NewLine}");
foreach (var storage in _mapStorages)
{
sw.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 = sr.ReadLine();
if (!str.Contains("MapCollection"))
Review

Строка может быть пустой

Строка может быть пустой
{
//если нет такой записи, то это не те данные
return false;
}
_mapStorages.Clear();
while ((str = sr.ReadLine()) != null)
{
var elem = str.Split(separatorDict);
AbstractMap map = null;
switch (elem[1])
{
case "SimpleMap":
map = new SimpleMap();
break;
case "DangerMap":
map = new DangerMap();
break;
case "CloudMap":
map = new CloudMap();
break;
}
_mapStorages.Add(elem[0], new MapWithSetAirplaneGeneric<IDrawningObject, AbstractMap>(_pictureWidth, _pictureHeight, map));
_mapStorages[elem[0]].LoadData(elem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries));
}
}
return true;
}
/// <summary>
/// Доступ к гавани
/// </summary>
/// <param name="ind"></param>
/// <returns></returns>
public MapWithSetAirplaneGeneric<DrawningObject, AbstractMap> this[string index]
public MapWithSetAirplaneGeneric<IDrawningObject, AbstractMap> this[string index]
{
get
{

View File

@ -108,6 +108,25 @@ namespace Stormtrooper
}
return new(_pictureWidth, _pictureHeight);
}
public string GetData(char separatorType, char separatorData)
{
string data = $"{_map.GetType().Name}{separatorType}";
foreach (var air in _setAirs.GetAirs())
{
data += $"{air.GetInfo()}{separatorData}";
}
return data;
}
public void LoadData(string[] records)
{
foreach (var rec in records.Reverse())
{
_setAirs.Insert(DrawningObject.Create(rec) as T);
}
}
/// <summary>
/// "Взбалтываем" набор, чтобы все элементы оказались в начале
/// </summary>
@ -167,7 +186,6 @@ namespace Stormtrooper
int currentWidth = width - 1;
int currentHeight = 0;
int i = 0;
foreach (var air in _setAirs.GetAirs())
{