diff --git a/Stormtrooper/Stormtrooper/DrawningObject.cs b/Stormtrooper/Stormtrooper/DrawningObject.cs index 13841e1..bdf9a94 100644 --- a/Stormtrooper/Stormtrooper/DrawningObject.cs +++ b/Stormtrooper/Stormtrooper/DrawningObject.cs @@ -22,6 +22,11 @@ namespace Stormtrooper return _airplane?.GetCurrentPosition() ?? default; } + public string GetInfo() + { + return _airplane?.GetDataForSave(); + } + public void MoveObject(Direction direction) { _airplane?.MoveAirplane(direction); @@ -36,5 +41,8 @@ namespace Stormtrooper { _airplane.DrawAirplane(g); } + + public static IDrawningObject Create(string data) => new DrawningObject(data.CreateDrawningAirplane()); + } } diff --git a/Stormtrooper/Stormtrooper/ExtentionAirplane.cs b/Stormtrooper/Stormtrooper/ExtentionAirplane.cs new file mode 100644 index 0000000..d8d9e2f --- /dev/null +++ b/Stormtrooper/Stormtrooper/ExtentionAirplane.cs @@ -0,0 +1,42 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Stormtrooper +{ + internal static class ExtentionAirplane + { + private static readonly char _separatorForObject = ':'; + public static string GetDataForSave(this DrawningMilitaryAirplane drawningMilitaryAirplane) + { + var air = drawningMilitaryAirplane.Airplane; + var str = $"{air.Speed}{_separatorForObject}{air.Weight}{_separatorForObject}{air.Color.ToArgb()}"; + if (air is not EntityStormtrooper stormtrooper) + { + return str; + } + else return + $"{str}{_separatorForObject}{stormtrooper.AdvColor.ToArgb()}{_separatorForObject}{stormtrooper.Rockets}{_separatorForObject}{stormtrooper.Booster}{_separatorForObject}{stormtrooper.Radar}"; + } + public static DrawningMilitaryAirplane CreateDrawningAirplane(this string info) + { + string[] strs = info.Split(_separatorForObject); + if (strs.Length == 3) + { + return new DrawningMilitaryAirplane(Convert.ToInt32(strs[0]), + Convert.ToInt32(strs[1]), Color.FromArgb(Convert.ToInt32(strs[2]))); + } + if (strs.Length == 7) + { + return new DrawningStormtrooper(Convert.ToInt32(strs[0]), + Convert.ToInt32(strs[1]), Color.FromArgb(Convert.ToInt32(strs[2])), + Color.FromArgb(Convert.ToInt32(strs[3])), Convert.ToBoolean(strs[4]), + Convert.ToBoolean(strs[5]), Convert.ToBoolean(strs[6])); + + } + return null; + } + } +} diff --git a/Stormtrooper/Stormtrooper/FormMapWithSetAirplane.Designer.cs b/Stormtrooper/Stormtrooper/FormMapWithSetAirplane.Designer.cs index 67e78a5..fde0103 100644 --- a/Stormtrooper/Stormtrooper/FormMapWithSetAirplane.Designer.cs +++ b/Stormtrooper/Stormtrooper/FormMapWithSetAirplane.Designer.cs @@ -45,9 +45,16 @@ this.maskedTextBoxPosition = new System.Windows.Forms.MaskedTextBox(); this.buttonCreate = 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.saveFileDialog = new System.Windows.Forms.SaveFileDialog(); + this.openFileDialog = new System.Windows.Forms.OpenFileDialog(); this.groupBoxTools.SuspendLayout(); this.groupBoxMap.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.buttonCreate); this.groupBoxTools.Dock = System.Windows.Forms.DockStyle.Right; - this.groupBoxTools.Location = new System.Drawing.Point(600, 0); + this.groupBoxTools.Location = new System.Drawing.Point(1069, 24); this.groupBoxTools.Name = "groupBoxTools"; - this.groupBoxTools.Size = new System.Drawing.Size(200, 588); + this.groupBoxTools.Size = new System.Drawing.Size(200, 564); this.groupBoxTools.TabIndex = 0; this.groupBoxTools.TabStop = false; this.groupBoxTools.Text = "Инструменты"; @@ -137,7 +144,7 @@ // buttonRight // this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.buttonRight.Location = new System.Drawing.Point(119, 540); + this.buttonRight.Location = new System.Drawing.Point(119, 516); this.buttonRight.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3); this.buttonRight.Name = "buttonRight"; this.buttonRight.Size = new System.Drawing.Size(35, 35); @@ -148,7 +155,7 @@ // buttonLeft // this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.buttonLeft.Location = new System.Drawing.Point(35, 540); + this.buttonLeft.Location = new System.Drawing.Point(35, 516); this.buttonLeft.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3); this.buttonLeft.Name = "buttonLeft"; this.buttonLeft.Size = new System.Drawing.Size(35, 35); @@ -159,7 +166,7 @@ // buttonDown // this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.buttonDown.Location = new System.Drawing.Point(77, 541); + this.buttonDown.Location = new System.Drawing.Point(77, 517); this.buttonDown.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3); this.buttonDown.Name = "buttonDown"; this.buttonDown.Size = new System.Drawing.Size(35, 35); @@ -170,7 +177,7 @@ // buttonUp // this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.buttonUp.Location = new System.Drawing.Point(77, 499); + this.buttonUp.Location = new System.Drawing.Point(77, 475); this.buttonUp.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3); this.buttonUp.Name = "buttonUp"; this.buttonUp.Size = new System.Drawing.Size(35, 35); @@ -230,19 +237,58 @@ // 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(600, 588); + this.pictureBox.Size = new System.Drawing.Size(1069, 564); 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(1269, 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.FileName = "openFileDialog1"; + // // FormMapWithSetAirplane // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(800, 588); + this.ClientSize = new System.Drawing.Size(1269, 588); 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); @@ -250,7 +296,10 @@ this.groupBoxMap.ResumeLayout(false); this.groupBoxMap.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit(); + this.menuStrip.ResumeLayout(false); + this.menuStrip.PerformLayout(); this.ResumeLayout(false); + this.PerformLayout(); } @@ -273,5 +322,11 @@ private Button buttonAddMap; private Button buttonRemoveMap; private ListBox listBoxMap; + private MenuStrip menuStrip; + private ToolStripMenuItem файлToolStripMenuItem; + private ToolStripMenuItem SaveToolStripMenuItem; + private ToolStripMenuItem LoadToolStripMenuItem; + private SaveFileDialog saveFileDialog; + private OpenFileDialog openFileDialog; } } \ No newline at end of file diff --git a/Stormtrooper/Stormtrooper/FormMapWithSetAirplane.cs b/Stormtrooper/Stormtrooper/FormMapWithSetAirplane.cs index 3fdccf9..117782f 100644 --- a/Stormtrooper/Stormtrooper/FormMapWithSetAirplane.cs +++ b/Stormtrooper/Stormtrooper/FormMapWithSetAirplane.cs @@ -205,5 +205,36 @@ namespace Stormtrooper ReloadMaps(); } } + + private void SaveToolStripMenuItem_Click(object sender, EventArgs e) + { + if (saveFileDialog.ShowDialog() == DialogResult.OK) + { + if (_mapCollection.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 (_mapCollection.LoadData(openFileDialog.FileName)) + { + ReloadMaps(); + MessageBox.Show("Загрузка прошла успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); + } + else + { + MessageBox.Show("Не получилось загрузить файл", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } } } diff --git a/Stormtrooper/Stormtrooper/FormMapWithSetAirplane.resx b/Stormtrooper/Stormtrooper/FormMapWithSetAirplane.resx index f298a7b..1c5f0f8 100644 --- a/Stormtrooper/Stormtrooper/FormMapWithSetAirplane.resx +++ b/Stormtrooper/Stormtrooper/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 + + + 254, 17 + \ No newline at end of file diff --git a/Stormtrooper/Stormtrooper/IDrawningObject.cs b/Stormtrooper/Stormtrooper/IDrawningObject.cs index 08ef54e..082b8ec 100644 --- a/Stormtrooper/Stormtrooper/IDrawningObject.cs +++ b/Stormtrooper/Stormtrooper/IDrawningObject.cs @@ -36,5 +36,7 @@ namespace Stormtrooper /// /// (float Left, float Top, float Right, float Bottom) GetCurrentPosition(); + + string GetInfo(); } } diff --git a/Stormtrooper/Stormtrooper/MapCollection.cs b/Stormtrooper/Stormtrooper/MapCollection.cs index 07d9ffe..07d2181 100644 --- a/Stormtrooper/Stormtrooper/MapCollection.cs +++ b/Stormtrooper/Stormtrooper/MapCollection.cs @@ -1,4 +1,5 @@ -using System; +using Strormtrooper; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -11,7 +12,9 @@ namespace Stormtrooper /// /// Словарь (хранилище) с картами /// - readonly Dictionary> _mapStorages; + readonly Dictionary> _mapStorages; + private readonly char separatorDict = '|'; + private readonly char separatorData = ';'; /// /// Возвращение списка названий карт /// @@ -31,7 +34,7 @@ namespace Stormtrooper /// public MapCollection(int pictureWidth, int pictureHeight) { - _mapStorages = new Dictionary>(); + _mapStorages = new Dictionary>(); _pictureWidth = pictureWidth; _pictureHeight = pictureHeight; } @@ -43,7 +46,7 @@ namespace Stormtrooper public void AddMap(string name, AbstractMap map) { // Добавление карты - MapWithSetAirplaneGeneric newMap = new(_pictureWidth, _pictureHeight, map); + MapWithSetAirplaneGeneric newMap = new(_pictureWidth, _pictureHeight, map); if (!_mapStorages.ContainsKey(name)) _mapStorages.Add(name, newMap); } @@ -60,12 +63,74 @@ namespace Stormtrooper } _mapStorages.Remove(name); } + + public bool SaveData(string filename) + { + if (File.Exists(filename)) + { + File.Delete(filename); + } + using (StreamWriter sw = new(filename)) + { + sw.Write($"MapCollection{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)) + { + string str = sr.ReadLine(); + if (!str.Contains("MapCollection")) + { + //если нет такой записи, то это не те данные + return false; + } + _mapStorages.Clear(); + while ((str = sr.ReadLine()) != null) + { + var elem = str.Split(separatorDict); + AbstractMap map = null; + switch (elem[1]) + { + case "SimpleMap": + map = new SimpleMap(); + break; + case "DangerMap": + map = new DangerMap(); + break; + case "CloudMap": + map = new CloudMap(); + break; + } + _mapStorages.Add(elem[0], new MapWithSetAirplaneGeneric(_pictureWidth, _pictureHeight, map)); + _mapStorages[elem[0]].LoadData(elem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries)); + } + } + return true; + } + + + /// /// Доступ к гавани /// /// /// - public MapWithSetAirplaneGeneric this[string index] + public MapWithSetAirplaneGeneric this[string index] { get { diff --git a/Stormtrooper/Stormtrooper/MapWithSetAirplaneGeneric.cs b/Stormtrooper/Stormtrooper/MapWithSetAirplaneGeneric.cs index 12424a2..89a9b65 100644 --- a/Stormtrooper/Stormtrooper/MapWithSetAirplaneGeneric.cs +++ b/Stormtrooper/Stormtrooper/MapWithSetAirplaneGeneric.cs @@ -108,6 +108,25 @@ namespace Stormtrooper } return new(_pictureWidth, _pictureHeight); } + public string GetData(char separatorType, char separatorData) + { + string data = $"{_map.GetType().Name}{separatorType}"; + foreach (var air in _setAirs.GetAirs()) + { + data += $"{air.GetInfo()}{separatorData}"; + } + return data; + } + public void LoadData(string[] records) + { + foreach (var rec in records.Reverse()) + { + _setAirs.Insert(DrawningObject.Create(rec) as T); + } + } + + + /// /// "Взбалтываем" набор, чтобы все элементы оказались в начале ///