From 0db883d7f88cc592a6e7cec5891ee8ea50c38a3c Mon Sep 17 00:00:00 2001 From: "ityurner02@mail.ru" Date: Sat, 5 Nov 2022 20:34:10 +0400 Subject: [PATCH] =?UTF-8?q?=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=20=D1=81?= =?UTF-8?q?=20=D1=84=D0=B0=D0=B9=D0=BB=D0=B0=D0=BC=D0=B8=20(=D1=81=D0=BE?= =?UTF-8?q?=D1=85=D1=80=D0=B0=D0=BD=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=B8=20?= =?UTF-8?q?=D0=B7=D0=B0=D0=B3=D1=80=D1=83=D0=B7=D0=BA=D0=B0)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DrawingObjectAntiAircraftGun.cs | 4 + .../ExtentionAntiAircraftGun.cs | 56 +++++++++++ ...FormMapWithSetAntiAircraftGuns.Designer.cs | 75 ++++++++++++-- .../FormMapWithSetAntiAircraftGuns.cs | 28 ++++++ .../FormMapWithSetAntiAircraftGuns.resx | 9 ++ .../AntiAircraftGun/IDrawingObject.cs | 5 + .../MapWithSetAntiAircraftGunsGeneric.cs | 25 +++++ .../AntiAircraftGun/MapsCollection.cs | 97 ++++++++++++++++++- 8 files changed, 287 insertions(+), 12 deletions(-) create mode 100644 AntiAircraftGun/AntiAircraftGun/ExtentionAntiAircraftGun.cs diff --git a/AntiAircraftGun/AntiAircraftGun/DrawingObjectAntiAircraftGun.cs b/AntiAircraftGun/AntiAircraftGun/DrawingObjectAntiAircraftGun.cs index 6f82639..bd7fc3c 100644 --- a/AntiAircraftGun/AntiAircraftGun/DrawingObjectAntiAircraftGun.cs +++ b/AntiAircraftGun/AntiAircraftGun/DrawingObjectAntiAircraftGun.cs @@ -18,6 +18,7 @@ namespace AntiAircraftGun { return _antiAircraftGun?.GetCurrentPosition() ?? default; } + public void MoveObject(Direction direction) { _antiAircraftGun?.MoveTransport(direction); @@ -31,5 +32,8 @@ namespace AntiAircraftGun { _antiAircraftGun?.DrawTransport(g); } + + public string GetInfo() => _antiAircraftGun?.GetDataForSave(); + public static IDrawingObject Create(string data) => new DrawingObjectAntiAircraftGun(data.CreateDrawingAntiAircraftGun()); } } diff --git a/AntiAircraftGun/AntiAircraftGun/ExtentionAntiAircraftGun.cs b/AntiAircraftGun/AntiAircraftGun/ExtentionAntiAircraftGun.cs new file mode 100644 index 0000000..bd42abe --- /dev/null +++ b/AntiAircraftGun/AntiAircraftGun/ExtentionAntiAircraftGun.cs @@ -0,0 +1,56 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AntiAircraftGun +{ + /// + /// Расширение для класса DrawningCar + /// + internal static class ExtentionAntiAircraftGun + { + /// + /// Разделитель для записи информации по объекту в файл + /// + private static readonly char _separatorForObject = ':'; + /// + /// Создание объекта из строки + /// + /// + /// + public static DrawingAntiAircraftGun CreateDrawingAntiAircraftGun(this string info) + { + string[] strs = info.Split(_separatorForObject); + if (strs.Length == 3) + { + return new DrawingAntiAircraftGun(Convert.ToInt32(strs[0]), + Convert.ToInt32(strs[1]), Color.FromName(strs[2])); + } + if (strs.Length == 6) + { + return new DrawingUpdateAntiAircraftGun(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 DrawingAntiAircraftGun drawingAntiAircraftGun) + { + var antiAircraftGun = drawingAntiAircraftGun.AntiAircraftGun; + var str = $"{antiAircraftGun.Speed}{_separatorForObject}{antiAircraftGun.Weight}{_separatorForObject}{antiAircraftGun.BodyColor.Name}"; + if (antiAircraftGun is not EntityUpdateAntiAircraftGun updateAntiAircraftGun) + { + return str; + } + return $"{str}{_separatorForObject}{updateAntiAircraftGun.DopColor.Name}{_separatorForObject}{updateAntiAircraftGun.Gun}{_separatorForObject}{updateAntiAircraftGun.Radar}"; + } + } +} diff --git a/AntiAircraftGun/AntiAircraftGun/FormMapWithSetAntiAircraftGuns.Designer.cs b/AntiAircraftGun/AntiAircraftGun/FormMapWithSetAntiAircraftGuns.Designer.cs index c515e95..15a3733 100644 --- a/AntiAircraftGun/AntiAircraftGun/FormMapWithSetAntiAircraftGuns.Designer.cs +++ b/AntiAircraftGun/AntiAircraftGun/FormMapWithSetAntiAircraftGuns.Designer.cs @@ -45,9 +45,16 @@ this.buttonDeleteMap = new System.Windows.Forms.Button(); this.buttonAddMap = new System.Windows.Forms.Button(); this.textBoxNewMapName = new System.Windows.Forms.TextBox(); + 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.groupBox.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit(); this.groupBoxMaps.SuspendLayout(); + this.menuStrip.SuspendLayout(); this.SuspendLayout(); // // groupBox @@ -62,9 +69,9 @@ this.groupBox.Controls.Add(this.buttonLeft); this.groupBox.Controls.Add(this.buttonUp); this.groupBox.Dock = System.Windows.Forms.DockStyle.Right; - this.groupBox.Location = new System.Drawing.Point(574, 0); + this.groupBox.Location = new System.Drawing.Point(574, 28); this.groupBox.Name = "groupBox"; - this.groupBox.Size = new System.Drawing.Size(238, 589); + this.groupBox.Size = new System.Drawing.Size(238, 561); this.groupBox.TabIndex = 0; this.groupBox.TabStop = false; this.groupBox.Text = "Инструменты"; @@ -122,7 +129,7 @@ this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonDown.BackgroundImage = global::AntiAircraftGun.Properties.Resources.arrowDown; this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonDown.Location = new System.Drawing.Point(108, 547); + this.buttonDown.Location = new System.Drawing.Point(108, 519); this.buttonDown.Name = "buttonDown"; this.buttonDown.Size = new System.Drawing.Size(30, 29); this.buttonDown.TabIndex = 10; @@ -134,7 +141,7 @@ this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonRight.BackgroundImage = global::AntiAircraftGun.Properties.Resources.arrowRight; this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonRight.Location = new System.Drawing.Point(143, 547); + this.buttonRight.Location = new System.Drawing.Point(143, 519); this.buttonRight.Name = "buttonRight"; this.buttonRight.Size = new System.Drawing.Size(30, 29); this.buttonRight.TabIndex = 9; @@ -146,7 +153,7 @@ this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonLeft.BackgroundImage = global::AntiAircraftGun.Properties.Resources.arrowLeft; this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonLeft.Location = new System.Drawing.Point(71, 547); + this.buttonLeft.Location = new System.Drawing.Point(71, 519); this.buttonLeft.Name = "buttonLeft"; this.buttonLeft.Size = new System.Drawing.Size(30, 29); this.buttonLeft.TabIndex = 8; @@ -158,7 +165,7 @@ this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonUp.BackgroundImage = global::AntiAircraftGun.Properties.Resources.arrowUp; this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonUp.Location = new System.Drawing.Point(108, 509); + this.buttonUp.Location = new System.Drawing.Point(108, 481); this.buttonUp.Name = "buttonUp"; this.buttonUp.Size = new System.Drawing.Size(30, 29); this.buttonUp.TabIndex = 7; @@ -180,9 +187,9 @@ // 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(574, 589); + this.pictureBox.Size = new System.Drawing.Size(574, 561); this.pictureBox.TabIndex = 1; this.pictureBox.TabStop = false; // @@ -238,6 +245,47 @@ this.textBoxNewMapName.Size = new System.Drawing.Size(194, 27); this.textBoxNewMapName.TabIndex = 0; // + // 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(812, 28); + this.menuStrip.TabIndex = 18; + // + // файл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"; + // // FormMapWithSetAntiAircraftGuns // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); @@ -246,6 +294,8 @@ this.Controls.Add(this.groupBoxMaps); this.Controls.Add(this.pictureBox); this.Controls.Add(this.groupBox); + this.Controls.Add(this.menuStrip); + this.MainMenuStrip = this.menuStrip; this.Name = "FormMapWithSetAntiAircraftGuns"; this.Text = "FormMapWithSetAntiAircraftGuns"; this.groupBox.ResumeLayout(false); @@ -253,7 +303,10 @@ ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit(); this.groupBoxMaps.ResumeLayout(false); this.groupBoxMaps.PerformLayout(); + this.menuStrip.ResumeLayout(false); + this.menuStrip.PerformLayout(); this.ResumeLayout(false); + this.PerformLayout(); } @@ -276,5 +329,11 @@ private Button buttonDeleteMap; 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/AntiAircraftGun/AntiAircraftGun/FormMapWithSetAntiAircraftGuns.cs b/AntiAircraftGun/AntiAircraftGun/FormMapWithSetAntiAircraftGuns.cs index e7c6c51..93e4bba 100644 --- a/AntiAircraftGun/AntiAircraftGun/FormMapWithSetAntiAircraftGuns.cs +++ b/AntiAircraftGun/AntiAircraftGun/FormMapWithSetAntiAircraftGuns.cs @@ -217,5 +217,33 @@ namespace AntiAircraftGun } 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/AntiAircraftGun/AntiAircraftGun/FormMapWithSetAntiAircraftGuns.resx b/AntiAircraftGun/AntiAircraftGun/FormMapWithSetAntiAircraftGuns.resx index f298a7b..985a3e4 100644 --- a/AntiAircraftGun/AntiAircraftGun/FormMapWithSetAntiAircraftGuns.resx +++ b/AntiAircraftGun/AntiAircraftGun/FormMapWithSetAntiAircraftGuns.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/AntiAircraftGun/AntiAircraftGun/IDrawingObject.cs b/AntiAircraftGun/AntiAircraftGun/IDrawingObject.cs index ee0ad14..6128b09 100644 --- a/AntiAircraftGun/AntiAircraftGun/IDrawingObject.cs +++ b/AntiAircraftGun/AntiAircraftGun/IDrawingObject.cs @@ -38,5 +38,10 @@ namespace AntiAircraftGun /// /// (float Left, float Right, float Top, float Bottom) GetCurrentPosition(); + /// + /// Получение информации по объекту + /// + /// + string GetInfo(); } } diff --git a/AntiAircraftGun/AntiAircraftGun/MapWithSetAntiAircraftGunsGeneric.cs b/AntiAircraftGun/AntiAircraftGun/MapWithSetAntiAircraftGunsGeneric.cs index a4b8b30..aaaa625 100644 --- a/AntiAircraftGun/AntiAircraftGun/MapWithSetAntiAircraftGunsGeneric.cs +++ b/AntiAircraftGun/AntiAircraftGun/MapWithSetAntiAircraftGunsGeneric.cs @@ -113,6 +113,31 @@ namespace AntiAircraftGun return new(_pictureWidth, _pictureHeight); } /// + /// Получение данных в виде строки + /// + /// + /// + public string GetData(char separatorType, char separatorData) + { + string data = $"{_map.GetType().Name}{separatorType}"; + foreach (var antiAircraftGun in _setAntiAircraftGuns.GetAntiAircraftGuns()) + { + data += $"{antiAircraftGun.GetInfo()}{separatorData}"; + } + return data; + } + /// + /// Загрузка списка из массива строк + /// + /// + public void LoadData(string[] records) + { + foreach (var rec in records) + { + _setAntiAircraftGuns.Insert(DrawingObjectAntiAircraftGun.Create(rec) as T); + } + } + /// /// "Взбалтываем" набор, чтобы все элементы оказались в начале /// private void Shaking() diff --git a/AntiAircraftGun/AntiAircraftGun/MapsCollection.cs b/AntiAircraftGun/AntiAircraftGun/MapsCollection.cs index 9fafc57..2ef493f 100644 --- a/AntiAircraftGun/AntiAircraftGun/MapsCollection.cs +++ b/AntiAircraftGun/AntiAircraftGun/MapsCollection.cs @@ -11,7 +11,7 @@ namespace AntiAircraftGun /// /// Словарь (хранилище) с картами /// - readonly Dictionary> _mapStorages; + readonly Dictionary> _mapStorages; /// /// Возвращение списка названий карт /// @@ -25,13 +25,21 @@ namespace AntiAircraftGun /// private readonly int _pictureHeight; /// + /// Разделитель для записи по элементу словаря в файл + /// + private readonly char separatorDict = '|'; + /// + /// Разделитель для записи колекции данных в файл + /// + private readonly char separatorData = ';'; + /// /// Конструктор /// /// /// public MapsCollection(int pictureWidth, int pictureHeight) { - _mapStorages = new Dictionary>(); + _mapStorages = new Dictionary>(); _pictureWidth = pictureWidth; _pictureHeight = pictureHeight; } @@ -42,7 +50,7 @@ namespace AntiAircraftGun /// Карта public void AddMap(string name, AbstractMap map) { - if(!_mapStorages.ContainsKey(name)) _mapStorages.Add(name, new MapWithSetAntiAircraftGunsGeneric(_pictureWidth, _pictureHeight, map)); + if(!_mapStorages.ContainsKey(name)) _mapStorages.Add(name, new MapWithSetAntiAircraftGunsGeneric(_pictureWidth, _pictureHeight, map)); } /// /// Удаление карты @@ -57,7 +65,7 @@ namespace AntiAircraftGun /// /// /// - public MapWithSetAntiAircraftGunsGeneric this[string ind] + public MapWithSetAntiAircraftGunsGeneric this[string ind] { get { @@ -65,5 +73,86 @@ namespace AntiAircraftGun 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 (FileStream fs = new(filename, FileMode.Create)) + { + WriteToFile($"MapsCollection{Environment.NewLine}", fs); + foreach (var storage in _mapStorages) + { + 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; + } + //очищаем записи + _mapStorages.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; + } + switch (elem[2]) + { + case "SimpleMap2": + map = new SimpleMap2(); + break; + } + _mapStorages.Add(elem[0], new MapWithSetAntiAircraftGunsGeneric(_pictureWidth, _pictureHeight, map)); + _mapStorages[elem[0]].LoadData(elem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries)); + } + return true; + } } }