From 0a889412fc7c88eefab6ddd05428170a13bfb9f7 Mon Sep 17 00:00:00 2001 From: "DozorovaA.A" Date: Sun, 13 Nov 2022 13:41:25 +0400 Subject: [PATCH] fuull lab --- 1.txt | 2 + 2.txt | 3 + ArmoredVehicle/DrawningObject.cs | 2 + ArmoredVehicle/ExtentionMachine.cs | 53 +++++++++++ .../FormMapWithSetMachine.Designer.cs | 76 +++++++++++++-- ArmoredVehicle/FormMapWithSetMachine.cs | 41 ++++++++ ArmoredVehicle/FormMapWithSetMachine.resx | 9 ++ ArmoredVehicle/IDrawningObject.cs | 5 + ArmoredVehicle/MapWithSetMachineGeneric.cs | 27 ++++++ ArmoredVehicle/MapsCollection.cs | 94 ++++++++++++++++++- 10 files changed, 299 insertions(+), 13 deletions(-) create mode 100644 1.txt create mode 100644 2.txt create mode 100644 ArmoredVehicle/ExtentionMachine.cs diff --git a/1.txt b/1.txt new file mode 100644 index 0000000..c8ac687 --- /dev/null +++ b/1.txt @@ -0,0 +1,2 @@ +MapsCollection +1|SimpleMap|100:100:Indigo:PaleTurquoise:False:True;100:100:Orange; diff --git a/2.txt b/2.txt new file mode 100644 index 0000000..da4eb7b --- /dev/null +++ b/2.txt @@ -0,0 +1,3 @@ +MapsCollection +1|SimpleMap|100:100:Indigo:PaleTurquoise:False:True;100:100:Orange; +16|VerticalMap|100:100:Blue:Indigo:True:True;100:100:Blue; diff --git a/ArmoredVehicle/DrawningObject.cs b/ArmoredVehicle/DrawningObject.cs index 8792a37..ed7906e 100644 --- a/ArmoredVehicle/DrawningObject.cs +++ b/ArmoredVehicle/DrawningObject.cs @@ -31,5 +31,7 @@ _machine.DrawTransport(g); } + public string GetInfo() => _machine?.GetDataForSave(); + public static IDrawningObject Create(string data) => new DrawningObject(data.CreateDrawningCar()); } } \ No newline at end of file diff --git a/ArmoredVehicle/ExtentionMachine.cs b/ArmoredVehicle/ExtentionMachine.cs new file mode 100644 index 0000000..e0d5cf1 --- /dev/null +++ b/ArmoredVehicle/ExtentionMachine.cs @@ -0,0 +1,53 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ArmoredVehicle +{ + internal static class ExtentionMachine + { + /// + /// Разделитель для записи информации по объекту в файл + /// + private static readonly char _separatorForObject = ':'; + /// + /// Создание объекта из строки + /// + /// + /// + public static DrawingArmoredVehicle CreateDrawningCar(this string info) + { + string[] strs = info.Split(_separatorForObject); + if (strs.Length == 3) + { + return new DrawingArmoredVehicle(Convert.ToInt32(strs[0]), + Convert.ToInt32(strs[1]), Color.FromName(strs[2])); + } + if (strs.Length == 6) + { + return new DrawingTank(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 DrawingArmoredVehicle drawningMachine) + { + var car = drawningMachine.ArmoredVehicle; + var str = $"{car.Speed}{_separatorForObject}{car.Weight}{_separatorForObject}{car.BodyColor.Name}"; + if (car is not TankEnity tank) + { + return str; + } + return $"{str}{_separatorForObject}{tank.DopColor.Name}{_separatorForObject}{tank.MachineGun}{_separatorForObject}{tank.Tower}"; + } + } +} diff --git a/ArmoredVehicle/FormMapWithSetMachine.Designer.cs b/ArmoredVehicle/FormMapWithSetMachine.Designer.cs index 590b2b9..1112daa 100644 --- a/ArmoredVehicle/FormMapWithSetMachine.Designer.cs +++ b/ArmoredVehicle/FormMapWithSetMachine.Designer.cs @@ -46,9 +46,16 @@ this.buttonDelete = new System.Windows.Forms.Button(); this.buttonAdd = new System.Windows.Forms.Button(); this.pictureBoxImage = 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.groupBoxInstruments.SuspendLayout(); this.groupBoxMap.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxImage)).BeginInit(); + this.menuStrip.SuspendLayout(); this.SuspendLayout(); // // groupBoxInstruments @@ -64,9 +71,9 @@ this.groupBoxInstruments.Controls.Add(this.buttonDelete); this.groupBoxInstruments.Controls.Add(this.buttonAdd); this.groupBoxInstruments.Dock = System.Windows.Forms.DockStyle.Right; - this.groupBoxInstruments.Location = new System.Drawing.Point(964, 0); + this.groupBoxInstruments.Location = new System.Drawing.Point(964, 33); this.groupBoxInstruments.Name = "groupBoxInstruments"; - this.groupBoxInstruments.Size = new System.Drawing.Size(300, 910); + this.groupBoxInstruments.Size = new System.Drawing.Size(300, 877); this.groupBoxInstruments.TabIndex = 0; this.groupBoxInstruments.TabStop = false; this.groupBoxInstruments.Text = "Инструменты"; @@ -151,7 +158,7 @@ this.ButtonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.ButtonDown.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("ButtonDown.BackgroundImage"))); this.ButtonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.ButtonDown.Location = new System.Drawing.Point(129, 823); + this.ButtonDown.Location = new System.Drawing.Point(129, 790); this.ButtonDown.Name = "ButtonDown"; this.ButtonDown.Size = new System.Drawing.Size(40, 36); this.ButtonDown.TabIndex = 17; @@ -163,7 +170,7 @@ this.ButtonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.ButtonRight.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("ButtonRight.BackgroundImage"))); this.ButtonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.ButtonRight.Location = new System.Drawing.Point(172, 776); + this.ButtonRight.Location = new System.Drawing.Point(172, 743); this.ButtonRight.Name = "ButtonRight"; this.ButtonRight.Size = new System.Drawing.Size(40, 36); this.ButtonRight.TabIndex = 16; @@ -175,7 +182,7 @@ this.ButtonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.ButtonLeft.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("ButtonLeft.BackgroundImage"))); this.ButtonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.ButtonLeft.Location = new System.Drawing.Point(89, 778); + this.ButtonLeft.Location = new System.Drawing.Point(89, 745); this.ButtonLeft.Name = "ButtonLeft"; this.ButtonLeft.Size = new System.Drawing.Size(40, 36); this.ButtonLeft.TabIndex = 15; @@ -187,7 +194,7 @@ this.ButtonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.ButtonUp.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("ButtonUp.BackgroundImage"))); this.ButtonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.ButtonUp.Location = new System.Drawing.Point(129, 728); + this.ButtonUp.Location = new System.Drawing.Point(129, 695); this.ButtonUp.Name = "ButtonUp"; this.ButtonUp.Size = new System.Drawing.Size(40, 36); this.ButtonUp.TabIndex = 14; @@ -237,12 +244,54 @@ // pictureBoxImage // this.pictureBoxImage.Dock = System.Windows.Forms.DockStyle.Fill; - this.pictureBoxImage.Location = new System.Drawing.Point(0, 0); + this.pictureBoxImage.Location = new System.Drawing.Point(0, 33); this.pictureBoxImage.Name = "pictureBoxImage"; - this.pictureBoxImage.Size = new System.Drawing.Size(964, 910); + this.pictureBoxImage.Size = new System.Drawing.Size(964, 877); this.pictureBoxImage.TabIndex = 1; this.pictureBoxImage.TabStop = false; // + // menuStrip + // + this.menuStrip.ImageScalingSize = new System.Drawing.Size(24, 24); + 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(1264, 33); + 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(69, 29); + this.файлToolStripMenuItem.Text = "Файл"; + // + // SaveToolStripMenuItem + // + this.SaveToolStripMenuItem.Name = "SaveToolStripMenuItem"; + this.SaveToolStripMenuItem.Size = new System.Drawing.Size(270, 34); + this.SaveToolStripMenuItem.Text = "Сохранение "; + this.SaveToolStripMenuItem.Click += new System.EventHandler(this.SaveToolStripMenuItem_Click); + // + // LoadToolStripMenuItem + // + this.LoadToolStripMenuItem.Name = "LoadToolStripMenuItem"; + this.LoadToolStripMenuItem.Size = new System.Drawing.Size(270, 34); + this.LoadToolStripMenuItem.Text = "Загрузка"; + this.LoadToolStripMenuItem.Click += new System.EventHandler(this.LoadToolStripMenuItem_Click); + // + // openFileDialog + // + this.openFileDialog.Filter = "text file | *.txt"; + // + // saveFileDialog + // + this.saveFileDialog.Filter = "text file | *.txt"; + // // FormMapWithSetMachine // this.AutoScaleDimensions = new System.Drawing.SizeF(10F, 25F); @@ -250,6 +299,8 @@ this.ClientSize = new System.Drawing.Size(1264, 910); this.Controls.Add(this.pictureBoxImage); this.Controls.Add(this.groupBoxInstruments); + this.Controls.Add(this.menuStrip); + this.MainMenuStrip = this.menuStrip; this.Name = "FormMapWithSetMachine"; this.Text = "Карта с набором объектов"; this.groupBoxInstruments.ResumeLayout(false); @@ -257,7 +308,10 @@ this.groupBoxMap.ResumeLayout(false); this.groupBoxMap.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxImage)).EndInit(); + this.menuStrip.ResumeLayout(false); + this.menuStrip.PerformLayout(); this.ResumeLayout(false); + this.PerformLayout(); } @@ -280,5 +334,11 @@ private Button buttonAddMap; private ComboBox comboBoxSelectorMap; 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/ArmoredVehicle/FormMapWithSetMachine.cs b/ArmoredVehicle/FormMapWithSetMachine.cs index d651c32..72a02c4 100644 --- a/ArmoredVehicle/FormMapWithSetMachine.cs +++ b/ArmoredVehicle/FormMapWithSetMachine.cs @@ -235,5 +235,46 @@ namespace ArmoredVehicle } pictureBoxImage.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)) + { + MessageBox.Show("Загрузка прошла успешно", "Результат", + MessageBoxButtons.OK, MessageBoxIcon.Information); + ReloadMaps(); + } + else + { + MessageBox.Show("Загрузка не удалась", "Результат", + MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } } } diff --git a/ArmoredVehicle/FormMapWithSetMachine.resx b/ArmoredVehicle/FormMapWithSetMachine.resx index 75a564b..f5abc8c 100644 --- a/ArmoredVehicle/FormMapWithSetMachine.resx +++ b/ArmoredVehicle/FormMapWithSetMachine.resx @@ -3379,4 +3379,13 @@ KgIWwAgCFgD9CVgAVBGwAEYQsADoT8ACoIqABTCCgAVAd9/85v8DkZmkbaZCAjsAAAAASUVORK5CYII= + + 17, 17 + + + 163, 17 + + + 347, 17 + \ No newline at end of file diff --git a/ArmoredVehicle/IDrawningObject.cs b/ArmoredVehicle/IDrawningObject.cs index c0dd705..38cdcd2 100644 --- a/ArmoredVehicle/IDrawningObject.cs +++ b/ArmoredVehicle/IDrawningObject.cs @@ -33,6 +33,11 @@ /// /// (float Left, float Right, float Top, float Bottom) GetCurrentPosition(); + /// + /// Получение информации по объекту + /// + /// + string GetInfo(); } } \ No newline at end of file diff --git a/ArmoredVehicle/MapWithSetMachineGeneric.cs b/ArmoredVehicle/MapWithSetMachineGeneric.cs index 8af5bfe..30fcace 100644 --- a/ArmoredVehicle/MapWithSetMachineGeneric.cs +++ b/ArmoredVehicle/MapWithSetMachineGeneric.cs @@ -107,6 +107,33 @@ } return new(_pictureWidth, _pictureHeight); } + + /// + /// Получение данных в виде строки + /// + /// + /// + public string GetData(char separatorType, char separatorData) + { + string data = $"{_map.GetType().Name}{separatorType}"; + foreach (var machine in _setMachines.GetMachine()) + { + data += $"{machine.GetInfo()}{separatorData}"; + } + return data; + } + /// + /// Загрузка списка из массива строк + /// + /// + public void LoadData(string[] records) + { + foreach (var rec in records) + { + _setMachines.Insert(DrawningObject.Create(rec) as T); + } + } + /// /// "Взбалтываем" набор, чтобы все элементы оказались в начале /// diff --git a/ArmoredVehicle/MapsCollection.cs b/ArmoredVehicle/MapsCollection.cs index caa0b3b..c9b254b 100644 --- a/ArmoredVehicle/MapsCollection.cs +++ b/ArmoredVehicle/MapsCollection.cs @@ -14,7 +14,7 @@ namespace ArmoredVehicle /// /// Словарь (хранилище) с картами /// - readonly Dictionary> _mapStorages; /// /// Возвращение списка названий карт @@ -29,6 +29,14 @@ namespace ArmoredVehicle /// private readonly int _pictureHeight; /// + /// Разделитель для записи информации по элементу словаря в файл + /// + private readonly char separatorDict = '|'; + /// + /// Разделитель для записей коллекции данных в файл + /// + private readonly char separatorData = ';'; + /// /// Конструктор /// /// @@ -36,7 +44,7 @@ namespace ArmoredVehicle public MapsCollection(int pictureWidth, int pictureHeight) { _mapStorages = new Dictionary>(); + MapWithSetMachineGeneric>(); _pictureWidth = pictureWidth; _pictureHeight = pictureHeight; } @@ -54,7 +62,7 @@ namespace ArmoredVehicle } else { - var NewElem = new MapWithSetMachineGeneric( + var NewElem = new MapWithSetMachineGeneric( _pictureWidth, _pictureHeight, map); _mapStorages.Add(name, NewElem); } @@ -80,7 +88,7 @@ namespace ArmoredVehicle /// /// /// - public MapWithSetMachineGeneric this[string ind] + public MapWithSetMachineGeneric this[string ind] { get { @@ -92,6 +100,82 @@ namespace ArmoredVehicle return null; } } - + /// + /// Метод записи информации в файл + /// + /// Строка, которую следует записать + /// Поток для записи + 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 (StreamWriter fs = new(filename)) + { + fs.Write($"MapsCollection{Environment.NewLine}"); + foreach (var storage in _mapStorages) + { + fs.Write($"{storage.Key}{separatorDict}{storage.Value.GetData(separatorDict, separatorData)}{Environment.NewLine}"); + } + } + return true; + } + + /// + /// Загрузка нформации по автомобилям на парковках из файла + /// + /// + /// + public bool LoadData(string filename) + { + if (!File.Exists(filename)) + { + return false; + } + string bufferTextFromFile = ""; + using (StreamReader sr = new(filename)) + { + string checkMap = sr.ReadLine(); + if (!checkMap.Contains("MapsCollection")) + { + return false; + } + bufferTextFromFile = sr.ReadLine(); + _mapStorages.Clear(); + while (bufferTextFromFile != null) + { + var strs = bufferTextFromFile.Split(separatorDict); + AbstractMap map = null; + + switch (strs[1]) + { + case "SimpleMap": + map = new SimpleMap(); + break; + case "VertivalMap": + map = new VerticalMap(); + break; + case "HorizontalMap": + map = new HorizontalMap(); + break; + } + _mapStorages.Add(strs[0], new MapWithSetMachineGeneric(_pictureWidth, _pictureHeight, map)); + _mapStorages[strs[0]].LoadData(strs[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries)); + bufferTextFromFile = sr.ReadLine(); + } + } + return true; + } } }