всё готово

This commit is contained in:
foxkerik6 2022-11-29 04:35:36 +04:00
parent 577de84cd5
commit 4bfd8eb75c
8 changed files with 245 additions and 14 deletions

View File

@ -22,6 +22,11 @@ namespace Stormtrooper
return _airplane?.GetCurrentPosition() ?? default; return _airplane?.GetCurrentPosition() ?? default;
} }
public string GetInfo()
{
return _airplane?.GetDataForSave();
}
public void MoveObject(Direction direction) public void MoveObject(Direction direction)
{ {
_airplane?.MoveAirplane(direction); _airplane?.MoveAirplane(direction);
@ -36,5 +41,8 @@ namespace Stormtrooper
{ {
_airplane.DrawAirplane(g); _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.maskedTextBoxPosition = new System.Windows.Forms.MaskedTextBox();
this.buttonCreate = new System.Windows.Forms.Button(); this.buttonCreate = new System.Windows.Forms.Button();
this.pictureBox = new System.Windows.Forms.PictureBox(); 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.groupBoxTools.SuspendLayout();
this.groupBoxMap.SuspendLayout(); this.groupBoxMap.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit();
this.menuStrip.SuspendLayout();
this.SuspendLayout(); this.SuspendLayout();
// //
// groupBoxTools // groupBoxTools
@ -63,9 +70,9 @@
this.groupBoxTools.Controls.Add(this.maskedTextBoxPosition); this.groupBoxTools.Controls.Add(this.maskedTextBoxPosition);
this.groupBoxTools.Controls.Add(this.buttonCreate); this.groupBoxTools.Controls.Add(this.buttonCreate);
this.groupBoxTools.Dock = System.Windows.Forms.DockStyle.Right; 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.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.TabIndex = 0;
this.groupBoxTools.TabStop = false; this.groupBoxTools.TabStop = false;
this.groupBoxTools.Text = "Инструменты"; this.groupBoxTools.Text = "Инструменты";
@ -137,7 +144,7 @@
// buttonRight // buttonRight
// //
this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); 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.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3);
this.buttonRight.Name = "buttonRight"; this.buttonRight.Name = "buttonRight";
this.buttonRight.Size = new System.Drawing.Size(35, 35); this.buttonRight.Size = new System.Drawing.Size(35, 35);
@ -148,7 +155,7 @@
// buttonLeft // buttonLeft
// //
this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); 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.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3);
this.buttonLeft.Name = "buttonLeft"; this.buttonLeft.Name = "buttonLeft";
this.buttonLeft.Size = new System.Drawing.Size(35, 35); this.buttonLeft.Size = new System.Drawing.Size(35, 35);
@ -159,7 +166,7 @@
// buttonDown // buttonDown
// //
this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); 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.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3);
this.buttonDown.Name = "buttonDown"; this.buttonDown.Name = "buttonDown";
this.buttonDown.Size = new System.Drawing.Size(35, 35); this.buttonDown.Size = new System.Drawing.Size(35, 35);
@ -170,7 +177,7 @@
// buttonUp // buttonUp
// //
this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); 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.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3);
this.buttonUp.Name = "buttonUp"; this.buttonUp.Name = "buttonUp";
this.buttonUp.Size = new System.Drawing.Size(35, 35); this.buttonUp.Size = new System.Drawing.Size(35, 35);
@ -230,19 +237,58 @@
// pictureBox // pictureBox
// //
this.pictureBox.Dock = System.Windows.Forms.DockStyle.Fill; 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.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.TabIndex = 1;
this.pictureBox.TabStop = false; 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 // FormMapWithSetAirplane
// //
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 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.pictureBox);
this.Controls.Add(this.groupBoxTools); this.Controls.Add(this.groupBoxTools);
this.Controls.Add(this.menuStrip);
this.MainMenuStrip = this.menuStrip;
this.Name = "FormMapWithSetAirplane"; this.Name = "FormMapWithSetAirplane";
this.Text = "FormMapWithSetAirplane"; this.Text = "FormMapWithSetAirplane";
this.groupBoxTools.ResumeLayout(false); this.groupBoxTools.ResumeLayout(false);
@ -250,7 +296,10 @@
this.groupBoxMap.ResumeLayout(false); this.groupBoxMap.ResumeLayout(false);
this.groupBoxMap.PerformLayout(); this.groupBoxMap.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit();
this.menuStrip.ResumeLayout(false);
this.menuStrip.PerformLayout();
this.ResumeLayout(false); this.ResumeLayout(false);
this.PerformLayout();
} }
@ -273,5 +322,11 @@
private Button buttonAddMap; private Button buttonAddMap;
private Button buttonRemoveMap; private Button buttonRemoveMap;
private ListBox listBoxMap; private ListBox listBoxMap;
private MenuStrip menuStrip;
private ToolStripMenuItem файлToolStripMenuItem;
private ToolStripMenuItem SaveToolStripMenuItem;
private ToolStripMenuItem LoadToolStripMenuItem;
private SaveFileDialog saveFileDialog;
private OpenFileDialog openFileDialog;
} }
} }

View File

@ -205,5 +205,36 @@ namespace Stormtrooper
ReloadMaps(); 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"> <resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader> </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> </root>

View File

@ -36,5 +36,7 @@ namespace Stormtrooper
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
(float Left, float Top, float Right, float Bottom) GetCurrentPosition(); (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.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
@ -11,7 +12,9 @@ namespace Stormtrooper
/// <summary> /// <summary>
/// Словарь (хранилище) с картами /// Словарь (хранилище) с картами
/// </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>
/// Возвращение списка названий карт /// Возвращение списка названий карт
/// </summary> /// </summary>
@ -31,7 +34,7 @@ namespace Stormtrooper
/// <param name="pictureHeight"></param> /// <param name="pictureHeight"></param>
public MapCollection(int pictureWidth, int pictureHeight) public MapCollection(int pictureWidth, int pictureHeight)
{ {
_mapStorages = new Dictionary<string, MapWithSetAirplaneGeneric<DrawningObject, AbstractMap>>(); _mapStorages = new Dictionary<string, MapWithSetAirplaneGeneric<IDrawningObject, AbstractMap>>();
_pictureWidth = pictureWidth; _pictureWidth = pictureWidth;
_pictureHeight = pictureHeight; _pictureHeight = pictureHeight;
} }
@ -43,7 +46,7 @@ namespace Stormtrooper
public void AddMap(string name, AbstractMap map) 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)) if (!_mapStorages.ContainsKey(name))
_mapStorages.Add(name, newMap); _mapStorages.Add(name, newMap);
} }
@ -60,12 +63,74 @@ namespace Stormtrooper
} }
_mapStorages.Remove(name); _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"))
{
//если нет такой записи, то это не те данные
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>
/// Доступ к гавани /// Доступ к гавани
/// </summary> /// </summary>
/// <param name="ind"></param> /// <param name="ind"></param>
/// <returns></returns> /// <returns></returns>
public MapWithSetAirplaneGeneric<DrawningObject, AbstractMap> this[string index] public MapWithSetAirplaneGeneric<IDrawningObject, AbstractMap> this[string index]
{ {
get get
{ {

View File

@ -108,6 +108,25 @@ namespace Stormtrooper
} }
return new(_pictureWidth, _pictureHeight); 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>
/// "Взбалтываем" набор, чтобы все элементы оказались в начале /// "Взбалтываем" набор, чтобы все элементы оказались в начале
/// </summary> /// </summary>