Merge pull request 'Eliseev E.E. LabWork06' (#7) from LabWork06 into LabWork05

Reviewed-on: http://student.git.athene.tech/ElEgEv/PIbd-21_Eliseev_E.E._Airbus_Base/pulls/7
This commit is contained in:
Евгений Эгов 2022-11-11 09:05:01 +04:00
commit b0ea1ca4c1
12 changed files with 316 additions and 26 deletions

View File

@ -25,6 +25,7 @@
<ItemGroup>
<Folder Include="метк\" />
<Folder Include="делите\" />
</ItemGroup>
</Project>

View File

@ -36,5 +36,15 @@ namespace Airbus
{
_airbus?.SetPosition(x, y, width, height);
}
public void DrawningObject()
{
//TODO
}
public string GetInfo() => _airbus?.GetDataForSave();
public static IDrawningObject Create(string data) => new DrawningObjectPlane(data.CreateDrawningPlane());
}
}

View File

@ -45,7 +45,5 @@ namespace Airbus
g.DrawEllipse(pen, _startPosX + 24, _startPosY + 22, 10, 5);
}
}
}
}

View File

@ -0,0 +1,54 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms.VisualStyles;
namespace Airbus
{
//расширение для класса DrawingPlane (класс для сохранения в файл колекций объектов)
internal static class ExtentionPlane
{
//разделитель для записи информации по объекту в файл
private static readonly char _separatorForObject = ':';
//создание объекта из строки
public static DrawningAirbus CreateDrawningPlane(this string info)
{
string[] strs = info.Split(_separatorForObject);
//если простой самолёт
if(strs.Length == 3)
{
return new DrawningAirbus(Convert.ToInt32(strs[0]), Convert.ToInt32(strs[1]), Color.FromName(strs[2]));
}
//если аэробус
if(strs.Length == 6)
{
return new DrawningSuperAirbus(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 DrawningAirbus drawingPlane)
{
var plane = drawingPlane.Airbus;
var str = $"{plane.Speed}{_separatorForObject}{plane.Weight}{_separatorForObject}{plane.CorpusColor.Name}";
//если объект не расширеный
if(plane is not EntitySuperAirbus airbus)
{
return str;
}
return $"{str}{_separatorForObject}{airbus.AddColor.Name}{_separatorForObject}" +
$"{airbus.AddEngine}{_separatorForObject}{airbus.AddСompartment}";
}
}
}

View File

@ -45,9 +45,16 @@
this.maskedTextBoxPosition = new System.Windows.Forms.MaskedTextBox();
this.buttonAddPlane = 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.OpenFileDialog();
this.groupBoxTools.SuspendLayout();
this.groupBoxMaps.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.buttonAddPlane);
this.groupBoxTools.Dock = System.Windows.Forms.DockStyle.Right;
this.groupBoxTools.Location = new System.Drawing.Point(843, 0);
this.groupBoxTools.Location = new System.Drawing.Point(843, 28);
this.groupBoxTools.Name = "groupBoxTools";
this.groupBoxTools.Size = new System.Drawing.Size(250, 722);
this.groupBoxTools.Size = new System.Drawing.Size(250, 769);
this.groupBoxTools.TabIndex = 0;
this.groupBoxTools.TabStop = false;
this.groupBoxTools.Text = "Инструменты";
@ -138,7 +145,7 @@
this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonRight.BackgroundImage = global::Airbus.Properties.Resources.Right;
this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.buttonRight.Location = new System.Drawing.Point(158, 657);
this.buttonRight.Location = new System.Drawing.Point(158, 704);
this.buttonRight.Name = "buttonRight";
this.buttonRight.Size = new System.Drawing.Size(45, 45);
this.buttonRight.TabIndex = 9;
@ -150,7 +157,7 @@
this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonDown.BackgroundImage = global::Airbus.Properties.Resources.Down;
this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.buttonDown.Location = new System.Drawing.Point(107, 657);
this.buttonDown.Location = new System.Drawing.Point(107, 704);
this.buttonDown.Name = "buttonDown";
this.buttonDown.Size = new System.Drawing.Size(45, 45);
this.buttonDown.TabIndex = 8;
@ -162,7 +169,7 @@
this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonLeft.BackgroundImage = global::Airbus.Properties.Resources.Left;
this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.buttonLeft.Location = new System.Drawing.Point(56, 657);
this.buttonLeft.Location = new System.Drawing.Point(56, 704);
this.buttonLeft.Name = "buttonLeft";
this.buttonLeft.Size = new System.Drawing.Size(45, 45);
this.buttonLeft.TabIndex = 7;
@ -174,7 +181,7 @@
this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonUp.BackgroundImage = global::Airbus.Properties.Resources.Up;
this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.buttonUp.Location = new System.Drawing.Point(107, 606);
this.buttonUp.Location = new System.Drawing.Point(107, 653);
this.buttonUp.Name = "buttonUp";
this.buttonUp.Size = new System.Drawing.Size(45, 45);
this.buttonUp.TabIndex = 6;
@ -232,19 +239,63 @@
// 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(843, 722);
this.pictureBox.Size = new System.Drawing.Size(843, 769);
this.pictureBox.TabIndex = 1;
this.pictureBox.TabStop = false;
//
// 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(1093, 28);
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(59, 24);
this.ToolStripMenuItem.Text = "Файл";
//
// SaveToolStripMenuItem
//
this.SaveToolStripMenuItem.Name = "SaveToolStripMenuItem";
this.SaveToolStripMenuItem.Size = new System.Drawing.Size(177, 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(177, 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";
//
// FormMapWithSetPlanes
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1093, 722);
this.ClientSize = new System.Drawing.Size(1093, 797);
this.Controls.Add(this.pictureBox);
this.Controls.Add(this.groupBoxTools);
this.Controls.Add(this.menuStrip);
this.MainMenuStrip = this.menuStrip;
this.Name = "FormMapWithSetPlanes";
this.Text = "FormMapWithSetPlanes";
this.groupBoxTools.ResumeLayout(false);
@ -252,7 +303,10 @@
this.groupBoxMaps.ResumeLayout(false);
this.groupBoxMaps.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit();
this.menuStrip.ResumeLayout(false);
this.menuStrip.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
@ -275,5 +329,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 OpenFileDialog saveFileDialog;
}
}

View File

@ -208,5 +208,42 @@ namespace Airbus
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))
{
ReloadMaps();
MessageBox.Show("Загрузка данных прошла успешно", "Результат",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("Ошибка загрузки данных", "Результат",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
}

View File

@ -57,4 +57,16 @@
<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="openFileDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>144, 17</value>
</metadata>
<metadata name="saveFileDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>311, 17</value>
</metadata>
<metadata name="$this.TrayHeight" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>25</value>
</metadata>
</root>

View File

@ -146,7 +146,7 @@
//
// panelPurple
//
this.panelPurple.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(0)))), ((int)(((byte)(192)))));
this.panelPurple.BackColor = System.Drawing.Color.DeepPink;
this.panelPurple.Location = new System.Drawing.Point(192, 79);
this.panelPurple.Name = "panelPurple";
this.panelPurple.Size = new System.Drawing.Size(40, 40);

View File

@ -23,5 +23,8 @@ namespace Airbus
//получение текущей позиции объекта
(float Left, float Right, float Top, float Bottom) GetCurrentPosition();
//получение информации по объекту
string GetInfo();
}
}

View File

@ -88,6 +88,28 @@ namespace Airbus
return new(_pictureWidth, _pictureHeight);
}
//получение данных в виде строки
public string GetData(char separatorType, char separatorData)
{
string data = $"{_map.GetType().Name}{separatorType}";
foreach (var plane in _setPlanes.GetAirbus())
{
data += $"{plane.GetInfo()}{separatorData}";
}
return data;
}
//Загрузка списка из массива строк
public void LoadData(string[] records)
{
foreach (var rec in records)
{
_setPlanes.Insert(DrawningObjectPlane.Create(rec) as T);
}
}
//"взламываем" набор, чтобы все элементы оказались в начале
private void Shaking()
{

View File

@ -10,11 +10,17 @@ namespace Airbus
internal class MapsCollection
{
//словарь (хранилище) с картами
readonly Dictionary<string, MapWithSetPlanesGeneric<DrawningObjectPlane, AbstractMap>> _mapStorage;
readonly Dictionary<string, MapWithSetPlanesGeneric<IDrawningObject, AbstractMap>> _mapStorage;
//возвращение списка названий карт
public List<string> Keys => _mapStorage.Keys.ToList();
//разделитель для записи информации по элементу словаря в файл
private readonly char separatorDict = '|';
//разделитель для записи коллекции данных в файл
private readonly char separatorData = ';';
//ширина окна отрисовки
private readonly int _pictureWidth;
@ -24,7 +30,7 @@ namespace Airbus
//конструктор
public MapsCollection(int pictureWidth, int pictureHeight)
{
_mapStorage = new Dictionary<string, MapWithSetPlanesGeneric<DrawningObjectPlane, AbstractMap>>();
_mapStorage = new Dictionary<string, MapWithSetPlanesGeneric<IDrawningObject, AbstractMap>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
@ -32,23 +38,109 @@ namespace Airbus
//добавление карты
public void AddMap(string name, AbstractMap map)
{
var NewElem = new MapWithSetPlanesGeneric<DrawningObjectPlane, AbstractMap>(
_pictureWidth, _pictureHeight, map);
_mapStorage.Add(name, NewElem);
if (!_mapStorage.ContainsKey(name))
{
_mapStorage.Add(name, new MapWithSetPlanesGeneric<IDrawningObject, AbstractMap>(_pictureWidth, _pictureHeight, map));
}
}
//удаление карты
public void DelMap(string name)
{
if (_mapStorage.ContainsKey(name))
{
_mapStorage.Remove(name);
}
}
//Доступ к аэродрому
public MapWithSetPlanesGeneric<DrawningObjectPlane, AbstractMap> this[string ind]
//сохранение информации по самолётам в ангарах в файл
public bool SaveData(string filename)
{
if (File.Exists(filename))
{
File.Delete(filename);
}
using (StreamWriter sw = new(filename))
{
sw.Write($"MapsCollection{Environment.NewLine}");
foreach (var storage in _mapStorage)
{
sw.Write($"{storage.Key}{separatorDict}{storage.Value.GetData(separatorDict, separatorData)}" +
$"{Environment.NewLine}");
}
}
return true;
}
//загрузка нформации по по самолётам в ангарах из файла
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;
}
_mapStorage.Clear();
while ((str = sr.ReadLine()) != null)
{
var element = str.Split(separatorDict);
AbstractMap map = null;
switch (element[1])
{
case "SimpleMap":
map = new SimpleMap();
break;
case "DesertStormMap":
map = new DesertStormMap();
break;
case "StarWarsMap":
map = new StarWarsMap();
break;
}
_mapStorage.Add(element[0], new MapWithSetPlanesGeneric<IDrawningObject, AbstractMap>(_pictureWidth, _pictureHeight,
map));
_mapStorage[element[0]].LoadData(element[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries));
}
}
return true;
}
//доступ к аэродрому
public MapWithSetPlanesGeneric<IDrawningObject, AbstractMap> this[string ind]
{
get
{
return _mapStorage[ind];
if(ind != string.Empty)
{
MapWithSetPlanesGeneric<IDrawningObject, AbstractMap> value;
if(_mapStorage.TryGetValue(ind, out value))
{
return value;
}
}
return null;
}
}
}

View File

@ -0,0 +1 @@