From 5b00b1a2387ce7e21f86af128813dc17b242561d Mon Sep 17 00:00:00 2001 From: Programmist73 Date: Tue, 8 Nov 2022 01:21:27 +0400 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D1=8F=20=D0=BC=D0=B5=D1=82=D0=BE=D0=B4=D0=BE=D0=B2?= =?UTF-8?q?=20=D1=81=D0=BE=D1=85=D1=80=D0=B0=D0=BD=D0=B5=D0=BD=D0=B8=D1=8F?= =?UTF-8?q?=20=D0=B8=20=D0=B7=D0=B0=D0=B3=D1=80=D1=83=D0=B7=D0=BA=D0=B8=20?= =?UTF-8?q?=D0=BE=D0=B1=D1=8A=D0=B5=D0=BA=D1=82=D0=BE=D0=B2.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Airbus/Airbus/Airbus.csproj | 1 + Airbus/Airbus/DrawningObjectPlane.cs | 9 ++ Airbus/Airbus/ExtentionPlane.cs | 54 +++++++++ .../Airbus/FormMapWithSetPlanes.Designer.cs | 76 +++++++++++-- Airbus/Airbus/FormMapWithSetPlanes.cs | 24 ++++ Airbus/Airbus/FormMapWithSetPlanes.resx | 9 ++ Airbus/Airbus/IDrawningObject.cs | 3 + Airbus/Airbus/MapWithSetPlanesGeneric.cs | 22 ++++ Airbus/Airbus/MapsCollection.cs | 103 +++++++++++++++++- 9 files changed, 289 insertions(+), 12 deletions(-) create mode 100644 Airbus/Airbus/ExtentionPlane.cs diff --git a/Airbus/Airbus/Airbus.csproj b/Airbus/Airbus/Airbus.csproj index f2a984c..3ec74d3 100644 --- a/Airbus/Airbus/Airbus.csproj +++ b/Airbus/Airbus/Airbus.csproj @@ -25,6 +25,7 @@ + \ No newline at end of file diff --git a/Airbus/Airbus/DrawningObjectPlane.cs b/Airbus/Airbus/DrawningObjectPlane.cs index 0af9cab..ded8421 100644 --- a/Airbus/Airbus/DrawningObjectPlane.cs +++ b/Airbus/Airbus/DrawningObjectPlane.cs @@ -36,5 +36,14 @@ namespace Airbus { _airbus?.SetPosition(x, y, width, height); } + + public void DrawningObject() + { + //TODO + } + + public string GetInfo() => _airbus?.GetDataForSave(); + + public static IDrawningObject Create(string data) => new IDrawningObject(data.CreateDrawningPlane()); } } diff --git a/Airbus/Airbus/ExtentionPlane.cs b/Airbus/Airbus/ExtentionPlane.cs new file mode 100644 index 0000000..7723c9e --- /dev/null +++ b/Airbus/Airbus/ExtentionPlane.cs @@ -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 == 5) + { + 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}"; + + } + } +} diff --git a/Airbus/Airbus/FormMapWithSetPlanes.Designer.cs b/Airbus/Airbus/FormMapWithSetPlanes.Designer.cs index ac26e14..cd170f5 100644 --- a/Airbus/Airbus/FormMapWithSetPlanes.Designer.cs +++ b/Airbus/Airbus/FormMapWithSetPlanes.Designer.cs @@ -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, 694); 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, 629); 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, 629); 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, 629); 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, 578); this.buttonUp.Name = "buttonUp"; this.buttonUp.Size = new System.Drawing.Size(45, 45); this.buttonUp.TabIndex = 6; @@ -232,12 +239,54 @@ // 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, 694); 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(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"; + // // FormMapWithSetPlanes // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); @@ -245,6 +294,8 @@ this.ClientSize = new System.Drawing.Size(1093, 722); 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; } } \ No newline at end of file diff --git a/Airbus/Airbus/FormMapWithSetPlanes.cs b/Airbus/Airbus/FormMapWithSetPlanes.cs index e097b0f..cea722b 100644 --- a/Airbus/Airbus/FormMapWithSetPlanes.cs +++ b/Airbus/Airbus/FormMapWithSetPlanes.cs @@ -208,5 +208,29 @@ 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) + { + // TODO продумать логику по аналогии + } } } diff --git a/Airbus/Airbus/FormMapWithSetPlanes.resx b/Airbus/Airbus/FormMapWithSetPlanes.resx index f298a7b..985a3e4 100644 --- a/Airbus/Airbus/FormMapWithSetPlanes.resx +++ b/Airbus/Airbus/FormMapWithSetPlanes.resx @@ -57,4 +57,13 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + 17, 17 + + + 144, 17 + + + 311, 17 + \ No newline at end of file diff --git a/Airbus/Airbus/IDrawningObject.cs b/Airbus/Airbus/IDrawningObject.cs index 5da91a0..ae2dff5 100644 --- a/Airbus/Airbus/IDrawningObject.cs +++ b/Airbus/Airbus/IDrawningObject.cs @@ -23,5 +23,8 @@ namespace Airbus //получение текущей позиции объекта (float Left, float Right, float Top, float Bottom) GetCurrentPosition(); + + //получение информации по объекту + string GetInfo(); } } diff --git a/Airbus/Airbus/MapWithSetPlanesGeneric.cs b/Airbus/Airbus/MapWithSetPlanesGeneric.cs index 4681c30..485747c 100644 --- a/Airbus/Airbus/MapWithSetPlanesGeneric.cs +++ b/Airbus/Airbus/MapWithSetPlanesGeneric.cs @@ -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() { diff --git a/Airbus/Airbus/MapsCollection.cs b/Airbus/Airbus/MapsCollection.cs index b432cea..900b48e 100644 --- a/Airbus/Airbus/MapsCollection.cs +++ b/Airbus/Airbus/MapsCollection.cs @@ -10,11 +10,17 @@ namespace Airbus internal class MapsCollection { //словарь (хранилище) с картами - readonly Dictionary> _mapStorage; + readonly Dictionary> _mapStorage; //возвращение списка названий карт public List 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>(); + _mapStorage = new Dictionary>(); _pictureWidth = pictureWidth; _pictureHeight = pictureHeight; } @@ -32,7 +38,7 @@ namespace Airbus //добавление карты public void AddMap(string name, AbstractMap map) { - var NewElem = new MapWithSetPlanesGeneric( + var NewElem = new MapWithSetPlanesGeneric( _pictureWidth, _pictureHeight, map); _mapStorage.Add(name, NewElem); } @@ -43,8 +49,97 @@ namespace Airbus _mapStorage.Remove(name); } + // Метод записи информации в файл + private static void WriteToFile(string text, FileStream stream) + { + byte[] info = new UTF8Encoding(true).GetBytes(text); + stream.Write(info, 0, info.Length); + } + + // Сохранение информации по автомобилям в хранилище в файл + public bool SaveData(string filename) + { + if (File.Exists(filename)) + { + File.Delete(filename); + } + + using (FileStream fs = new(filename, FileMode.Create)) + { + WriteToFile($"MapsCollection{Environment.NewLine}", fs); + foreach (var storage in _mapStorage) + { + + WriteToFile($"{storage.Key}{separatorDict}{storage.Value.GetData(separatorDict, separatorData)}" + + $"{Environment.NewLine}", fs); + } + } + + return true; + } + + // Загрузка нформации по автомобилям на парковках из файла + public bool LoadData(string filename) + { + if (!File.Exists(filename)) + { + return false; + } + + string bufferTextFromFile = ""; + + using (FileStream fs = new(filename, FileMode.Open)) + { + byte[] b = new byte[fs.Length]; + UTF8Encoding temp = new(true); + + while (fs.Read(b, 0, b.Length) > 0) + { + bufferTextFromFile += temp.GetString(b); + } + } + + var strs = bufferTextFromFile.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries); + + if (!strs[0].Contains("MapsCollection")) + { + //если нет такой записи, то это не те данные + return false; + } + + //очищаем записи + _mapStorage.Clear(); + + for (int i = 1; i < strs.Length; ++i) + { + var elem = strs[i].Split(separatorDict); + AbstractMap map = null; + + switch (elem[1]) + { + case "SimpleMap": + map = new SimpleMap(); + break; + case "DesertStormMap": + map = new DesertStormMap(); + break; + case "StarWarsMap": + map = new StarWarsMap(); + break; + } + + _mapStorage.Add(elem[0], new MapWithSetPlanesGeneric(_pictureWidth, _pictureHeight, + map)); + + _mapStorage[elem[0]].LoadData(elem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries)); + } + + return true; + } + + //Доступ к аэродрому - public MapWithSetPlanesGeneric this[string ind] + public MapWithSetPlanesGeneric this[string ind] { get {