diff --git a/Airbus/Airbus/DrawningObjectAirplane.cs b/Airbus/Airbus/DrawningObjectAirplane.cs index 65bb174..5ec45c4 100644 --- a/Airbus/Airbus/DrawningObjectAirplane.cs +++ b/Airbus/Airbus/DrawningObjectAirplane.cs @@ -31,5 +31,9 @@ namespace Airbus { _airplane?.SetPosition(x, y, width, height); } + + public string GetInfo() => _airplane?.GetDataForSave(); + public static IDrawningObject Create(string data) => new DrawningObjectAirplane(data.CreateDrawningAirplane()); + } } diff --git a/Airbus/Airbus/ExtentionAirplane.cs b/Airbus/Airbus/ExtentionAirplane.cs new file mode 100644 index 0000000..b5ef0ab --- /dev/null +++ b/Airbus/Airbus/ExtentionAirplane.cs @@ -0,0 +1,53 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Airbus +{ + internal static class ExtentionAirplane + { + /// + /// Разделитель для записи информации по объекту в файл + /// + private static readonly char _separatorForObject = ':'; + /// + /// Создание объекта из строки + /// + /// + /// + public static DrawningAirplane CreateDrawningAirplane(this string info) + { + string[] strs = info.Split(_separatorForObject); + if (strs.Length == 3) + { + return new DrawningAirplane(Convert.ToInt32(strs[0]), + Convert.ToInt32(strs[1]), Color.FromName(strs[2])); + } + if (strs.Length == 6) + { + return new DrawingAirbus(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 DrawningAirplane drawningAirplane) + { + var airplane = drawningAirplane.airplane; + var str = $"{airplane.Speed}{_separatorForObject}{airplane.Weight}{_separatorForObject}{airplane.BodyColor.Name}"; + if (airplane is not EntityAirbus airbus) + { + return str; + } + return $"{str}{_separatorForObject}{airbus.DopColor.Name}{_separatorForObject}{airbus.Compartment}{_separatorForObject}{airbus.Engine}"; + } + } +} diff --git a/Airbus/Airbus/FormMapWithSetAirplane.Designer.cs b/Airbus/Airbus/FormMapWithSetAirplane.Designer.cs index 232f7db..4d1a28d 100644 --- a/Airbus/Airbus/FormMapWithSetAirplane.Designer.cs +++ b/Airbus/Airbus/FormMapWithSetAirplane.Designer.cs @@ -45,9 +45,16 @@ this.maskedTextBoxPosition = new System.Windows.Forms.MaskedTextBox(); this.buttonAddAirplane = 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.SaveFileDialog(); 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.buttonAddAirplane); this.groupBoxTools.Dock = System.Windows.Forms.DockStyle.Right; - this.groupBoxTools.Location = new System.Drawing.Point(631, 0); + this.groupBoxTools.Location = new System.Drawing.Point(631, 24); this.groupBoxTools.Name = "groupBoxTools"; - this.groupBoxTools.Size = new System.Drawing.Size(200, 593); + this.groupBoxTools.Size = new System.Drawing.Size(200, 569); this.groupBoxTools.TabIndex = 0; this.groupBoxTools.TabStop = false; this.groupBoxTools.Text = "Инструменты"; @@ -132,14 +139,13 @@ this.comboBoxSelectorMap.Name = "comboBoxSelectorMap"; this.comboBoxSelectorMap.Size = new System.Drawing.Size(170, 23); this.comboBoxSelectorMap.TabIndex = 0; - // // buttonDown // this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonDown.BackgroundImage = global::Airbus.Properties.Resources.v4; this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonDown.Location = new System.Drawing.Point(80, 523); + this.buttonDown.Location = new System.Drawing.Point(80, 499); this.buttonDown.Name = "buttonDown"; this.buttonDown.Size = new System.Drawing.Size(35, 35); this.buttonDown.TabIndex = 9; @@ -151,7 +157,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.v3; this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonRight.Location = new System.Drawing.Point(112, 491); + this.buttonRight.Location = new System.Drawing.Point(112, 467); this.buttonRight.Name = "buttonRight"; this.buttonRight.Size = new System.Drawing.Size(35, 35); this.buttonRight.TabIndex = 8; @@ -163,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.v1; this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonLeft.Location = new System.Drawing.Point(49, 491); + this.buttonLeft.Location = new System.Drawing.Point(49, 467); this.buttonLeft.Name = "buttonLeft"; this.buttonLeft.Size = new System.Drawing.Size(35, 35); this.buttonLeft.TabIndex = 7; @@ -175,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.v2; this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonUp.Location = new System.Drawing.Point(80, 459); + this.buttonUp.Location = new System.Drawing.Point(80, 435); this.buttonUp.Name = "buttonUp"; this.buttonUp.Size = new System.Drawing.Size(35, 35); this.buttonUp.TabIndex = 6; @@ -233,12 +239,53 @@ // 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(631, 593); + this.pictureBox.Size = new System.Drawing.Size(631, 569); 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(831, 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.Filter = "txt file | *.txt"; + // + // saveFileDialog + // + this.saveFileDialog.Filter = "txt file | *.txt"; + // // FormMapWithSetAirplane // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); @@ -246,6 +293,8 @@ this.ClientSize = new System.Drawing.Size(831, 593); 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); @@ -253,7 +302,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(); } @@ -276,5 +328,11 @@ private Button buttonDelMap; private Button buttonAddMap; private TextBox textBoxNewMapName; + private MenuStrip menuStrip; + private ToolStripMenuItem файлToolStripMenuItem; + private ToolStripMenuItem SaveToolStripMenuItem; + private ToolStripMenuItem LoadToolStripMenuItem; + private OpenFileDialog openFileDialog; + private SaveFileDialog saveFileDialog; } } \ No newline at end of file diff --git a/Airbus/Airbus/FormMapWithSetAirplane.cs b/Airbus/Airbus/FormMapWithSetAirplane.cs index 83be8ab..7a776c7 100644 --- a/Airbus/Airbus/FormMapWithSetAirplane.cs +++ b/Airbus/Airbus/FormMapWithSetAirplane.cs @@ -173,6 +173,44 @@ namespace Airbus pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowOnMap(); } + + + 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); + } + } + } /// /// Перемещение /// diff --git a/Airbus/Airbus/FormMapWithSetAirplane.resx b/Airbus/Airbus/FormMapWithSetAirplane.resx index f298a7b..934ed35 100644 --- a/Airbus/Airbus/FormMapWithSetAirplane.resx +++ b/Airbus/Airbus/FormMapWithSetAirplane.resx @@ -57,4 +57,13 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + 17, 17 + + + 125, 17 + + + 265, 17 + \ No newline at end of file diff --git a/Airbus/Airbus/IDrawningObject.cs b/Airbus/Airbus/IDrawningObject.cs index 0390d83..1195eac 100644 --- a/Airbus/Airbus/IDrawningObject.cs +++ b/Airbus/Airbus/IDrawningObject.cs @@ -11,6 +11,7 @@ namespace Airbus public float Step { get; } void SetObject(int x, int y, int width, int height); void MoveObject(Direction direction); + string GetInfo(); void DrawningObject(Graphics g); (float Left, float Right, float Top, float Bottom) GetCurrentPosition(); diff --git a/Airbus/Airbus/MapWithSetAirplaneGeneric.cs b/Airbus/Airbus/MapWithSetAirplaneGeneric.cs index 739d385..4209b36 100644 --- a/Airbus/Airbus/MapWithSetAirplaneGeneric.cs +++ b/Airbus/Airbus/MapWithSetAirplaneGeneric.cs @@ -177,9 +177,29 @@ namespace Airbus int maxLeft = (countInLine - 1) * _placeSizeWidth; for (int i = 0; i < _setAirplane.Count; i++) { - _setAirplane[i]?.SetObject(maxLeft - i % countInLine * _placeSizeWidth, (_pictureHeight - (i / 2 * _placeSizeHeight + _placeSizeHeight + 30)), _pictureWidth, _placeSizeHeight); + _setAirplane[i]?.SetObject(maxLeft - i % countInLine * _placeSizeWidth, (_pictureHeight - (i / 2 * _placeSizeHeight + _placeSizeHeight + 30)) + 25, _pictureWidth, _placeSizeHeight); _setAirplane[i]?.DrawningObject(g); } } + public string GetData(char separatorType, char separatorData) + { + string data = $"{_map.GetType().Name}{separatorType}"; + foreach (var airplane in _setAirplane.GetAirplanes()) + { + data += $"{airplane.GetInfo()}{separatorData}"; + } + return data; + } + /// + /// Загрузка списка из массива строк + /// + /// + public void LoadData(string[] records) + { + foreach (var rec in records) + { + _setAirplane.Insert(DrawningObjectAirplane.Create(rec) as T); + } + } } - } +} diff --git a/Airbus/Airbus/MapsCollection.cs b/Airbus/Airbus/MapsCollection.cs index 2e4c90c..6d8237b 100644 --- a/Airbus/Airbus/MapsCollection.cs +++ b/Airbus/Airbus/MapsCollection.cs @@ -8,7 +8,7 @@ namespace Airbus { internal class MapsCollection { - readonly Dictionary> _mapStorages; + readonly Dictionary> _mapStorages; /// /// Возвращение списка названий карт /// @@ -21,6 +21,8 @@ namespace Airbus /// Высота окна отрисовки /// private readonly int _pictureHeight; + private readonly char separatorDict = '|'; + private readonly char separatorData = ';'; /// /// Конструктор /// @@ -28,7 +30,7 @@ namespace Airbus /// public MapsCollection(int pictureWidth, int pictureHeight) { - _mapStorages = new Dictionary>(); + _mapStorages = new Dictionary>(); _pictureWidth = pictureWidth; _pictureHeight = pictureHeight; } @@ -41,7 +43,7 @@ namespace Airbus { if (!_mapStorages.ContainsKey(name)) { - MapWithSetAirplaneGeneric Map = new(_pictureWidth, _pictureHeight, map); + MapWithSetAirplaneGeneric Map = new(_pictureWidth, _pictureHeight, map); _mapStorages.Add(name, Map); } } @@ -58,14 +60,87 @@ namespace Airbus /// /// /// - public MapWithSetAirplaneGeneric this[string ind] + public MapWithSetAirplaneGeneric this[string ind] { get { if (_mapStorages.ContainsKey(ind)) - return _mapStorages[ind]; + return _mapStorages[ind]; return null; } } + /// + /// Сохранение информации по автомобилям в хранилище в файл + /// + /// Путь и имя файла + /// + 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 _mapStorages) + { + 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)) + { + bool isFirst = true; + string str; + while ((str = sr.ReadLine()) != null) + { + if (isFirst) + { + if (!str.Contains("MapsCollection")) + { + return false; + } + else + { + _mapStorages.Clear(); + } + isFirst = false; + } + else + { + var tempElem = str.Split(separatorDict); + AbstractMap map = null; + switch (tempElem[1]) + { + case "SimpleMap": + map = new SimpleMap(); + break; + case "SecondMap": + map = new SecondMap(); + break; + case "ThirdMap": + map = new ThirdMap(); + break; + } + _mapStorages.Add(tempElem[0], new MapWithSetAirplaneGeneric(_pictureWidth, _pictureHeight, map)); + _mapStorages[tempElem[0]].LoadData(tempElem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries)); + } + } + return true; + } + } } }