diff --git a/WarmlyShip/WarmlyShip/DrawningObjectShip.cs b/WarmlyShip/WarmlyShip/DrawningObjectShip.cs index 22cba01..2c002e9 100644 --- a/WarmlyShip/WarmlyShip/DrawningObjectShip.cs +++ b/WarmlyShip/WarmlyShip/DrawningObjectShip.cs @@ -36,5 +36,8 @@ namespace WarmlyShip { _ship.DrawTransport(g); } + public string GetInfo() => _ship?.GetDataForSave(); + + public static IDrawningObject Create(string data) => new DrawningObjectShip(data.CreateDrawningShip()); } } diff --git a/WarmlyShip/WarmlyShip/ExtentionShip.cs b/WarmlyShip/WarmlyShip/ExtentionShip.cs new file mode 100644 index 0000000..dcddc0c --- /dev/null +++ b/WarmlyShip/WarmlyShip/ExtentionShip.cs @@ -0,0 +1,53 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace WarmlyShip +{ + internal static class ExtentionShip + { + /// + /// Разделитель для записи информации по объекту в файл + /// + private static readonly char _separatorForObject = ':'; + /// + /// Создание объекта из строки + /// + /// + /// + public static DrawningShip CreateDrawningShip(this string info) + { + string[] strs = info.Split(_separatorForObject); + if (strs.Length == 3) + { + return new DrawningShip(Convert.ToInt32(strs[0]), + Convert.ToInt32(strs[1]), Color.FromName(strs[2])); + } + if (strs.Length == 6) + { + return new DrawningWarmlyShip(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 DrawningShip drawningShip) + { + var Ship = drawningShip.Ship; + var str = $"{Ship.Speed}{_separatorForObject}{Ship.Weight}{_separatorForObject}{Ship.BodyColor.Name}"; + if (Ship is not EntityWarmlyShip WarmlyShip) + { + return str; + } + return $"{str}{_separatorForObject}{WarmlyShip.DopColor.Name}{_separatorForObject}{WarmlyShip.Pipes}{_separatorForObject}{WarmlyShip.FuelCompartment}"; + } + } +} diff --git a/WarmlyShip/WarmlyShip/FormMapWithSetShips.Designer.cs b/WarmlyShip/WarmlyShip/FormMapWithSetShips.Designer.cs index 3e5154a..96b7cd7 100644 --- a/WarmlyShip/WarmlyShip/FormMapWithSetShips.Designer.cs +++ b/WarmlyShip/WarmlyShip/FormMapWithSetShips.Designer.cs @@ -45,9 +45,16 @@ this.maskedTextBoxPosition = new System.Windows.Forms.MaskedTextBox(); this.buttonAddShip = 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.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.buttonAddShip); this.groupBoxTools.Dock = System.Windows.Forms.DockStyle.Right; - this.groupBoxTools.Location = new System.Drawing.Point(616, 0); + this.groupBoxTools.Location = new System.Drawing.Point(616, 28); this.groupBoxTools.Name = "groupBoxTools"; - this.groupBoxTools.Size = new System.Drawing.Size(250, 594); + this.groupBoxTools.Size = new System.Drawing.Size(250, 566); this.groupBoxTools.TabIndex = 0; this.groupBoxTools.TabStop = false; this.groupBoxTools.Text = "Инструменты"; @@ -137,7 +144,7 @@ this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonLeft.BackgroundImage = global::WarmlyShip.Properties.Resources.arrowLeft; this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonLeft.Location = new System.Drawing.Point(73, 544); + this.buttonLeft.Location = new System.Drawing.Point(73, 516); this.buttonLeft.Name = "buttonLeft"; this.buttonLeft.Size = new System.Drawing.Size(30, 30); this.buttonLeft.TabIndex = 10; @@ -150,7 +157,7 @@ this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonRight.BackgroundImage = global::WarmlyShip.Properties.Resources.arrowRight; this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonRight.Location = new System.Drawing.Point(145, 543); + this.buttonRight.Location = new System.Drawing.Point(145, 515); this.buttonRight.Name = "buttonRight"; this.buttonRight.Size = new System.Drawing.Size(30, 30); this.buttonRight.TabIndex = 9; @@ -163,7 +170,7 @@ this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonUp.BackgroundImage = global::WarmlyShip.Properties.Resources.arrowUp; this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonUp.Location = new System.Drawing.Point(109, 507); + this.buttonUp.Location = new System.Drawing.Point(109, 479); this.buttonUp.Name = "buttonUp"; this.buttonUp.Size = new System.Drawing.Size(30, 30); this.buttonUp.TabIndex = 8; @@ -176,7 +183,7 @@ this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonDown.BackgroundImage = global::WarmlyShip.Properties.Resources.arrowDown; this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonDown.Location = new System.Drawing.Point(109, 543); + this.buttonDown.Location = new System.Drawing.Point(109, 515); this.buttonDown.Name = "buttonDown"; this.buttonDown.Size = new System.Drawing.Size(30, 30); this.buttonDown.TabIndex = 7; @@ -235,12 +242,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, 28); this.pictureBox.Name = "pictureBox"; - this.pictureBox.Size = new System.Drawing.Size(616, 594); + this.pictureBox.Size = new System.Drawing.Size(616, 566); 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(866, 28); + this.menuStrip.TabIndex = 2; + // + // файл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); + // + // saveFileDialog + // + this.saveFileDialog.Filter = "txt file | *.txt"; + // + // openFileDialog + // + this.openFileDialog.Filter = "txt file | *.txt"; + // // FormMapWithSetShips // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); @@ -248,6 +296,8 @@ this.ClientSize = new System.Drawing.Size(866, 594); this.Controls.Add(this.pictureBox); this.Controls.Add(this.groupBoxTools); + this.Controls.Add(this.menuStrip); + this.MainMenuStrip = this.menuStrip; this.Name = "FormMapWithSetShips"; this.Text = "Карта с набором объектов"; this.groupBoxTools.ResumeLayout(false); @@ -255,7 +305,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(); } @@ -278,5 +331,11 @@ private Button buttonAddMap; private TextBox textBoxNewMapName; private ComboBox comboBoxSelectorMap; + 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/WarmlyShip/WarmlyShip/FormMapWithSetShips.cs b/WarmlyShip/WarmlyShip/FormMapWithSetShips.cs index c632610..b64f039 100644 --- a/WarmlyShip/WarmlyShip/FormMapWithSetShips.cs +++ b/WarmlyShip/WarmlyShip/FormMapWithSetShips.cs @@ -218,5 +218,36 @@ namespace WarmlyShip } 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) + { + 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/WarmlyShip/WarmlyShip/FormMapWithSetShips.resx b/WarmlyShip/WarmlyShip/FormMapWithSetShips.resx index f298a7b..4d7ccd6 100644 --- a/WarmlyShip/WarmlyShip/FormMapWithSetShips.resx +++ b/WarmlyShip/WarmlyShip/FormMapWithSetShips.resx @@ -57,4 +57,13 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + 17, 17 + + + 144, 17 + + + 306, 17 + \ No newline at end of file diff --git a/WarmlyShip/WarmlyShip/FormShipConfig.Designer.cs b/WarmlyShip/WarmlyShip/FormShipConfig.Designer.cs index 91e35e3..4133361 100644 --- a/WarmlyShip/WarmlyShip/FormShipConfig.Designer.cs +++ b/WarmlyShip/WarmlyShip/FormShipConfig.Designer.cs @@ -119,7 +119,7 @@ // // panelPurple // - this.panelPurple.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(0)))), ((int)(((byte)(64))))); + this.panelPurple.BackColor = System.Drawing.Color.Purple; this.panelPurple.Location = new System.Drawing.Point(230, 96); this.panelPurple.Name = "panelPurple"; this.panelPurple.Size = new System.Drawing.Size(50, 50); @@ -164,7 +164,7 @@ // // panelGreen // - this.panelGreen.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(192)))), ((int)(((byte)(0))))); + this.panelGreen.BackColor = System.Drawing.Color.Green; this.panelGreen.Location = new System.Drawing.Point(89, 26); this.panelGreen.Name = "panelGreen"; this.panelGreen.Size = new System.Drawing.Size(50, 50); diff --git a/WarmlyShip/WarmlyShip/IDrawningObject.cs b/WarmlyShip/WarmlyShip/IDrawningObject.cs index 8637b93..22295e9 100644 --- a/WarmlyShip/WarmlyShip/IDrawningObject.cs +++ b/WarmlyShip/WarmlyShip/IDrawningObject.cs @@ -36,5 +36,10 @@ namespace WarmlyShip /// /// (float Left, float Right, float Top, float Bottom) GetCurrentPosition(); + /// + /// Получение информации по объекту + /// + /// + string GetInfo(); } } diff --git a/WarmlyShip/WarmlyShip/MapWithSetShipsGeneric.cs b/WarmlyShip/WarmlyShip/MapWithSetShipsGeneric.cs index 43ed7f8..8101eec 100644 --- a/WarmlyShip/WarmlyShip/MapWithSetShipsGeneric.cs +++ b/WarmlyShip/WarmlyShip/MapWithSetShipsGeneric.cs @@ -173,5 +173,30 @@ namespace WarmlyShip ship?.DrawningObject(g); } } + /// + /// Получение данных в виде строки + /// + /// + /// + public string GetData(char separatorType, char separatorData) + { + string data = $"{_map.GetType().Name}{separatorType}"; + foreach (var ship in _setShips.GetShips()) + { + data += $"{ship.GetInfo()}{separatorData}"; + } + return data; + } + /// + /// Загрузка списка из массива строк + /// + /// + public void LoadData(string[] records) + { + foreach (var rec in records) + { + _setShips.Insert(DrawningObjectShip.Create(rec) as T); + } + } } } diff --git a/WarmlyShip/WarmlyShip/MapsCollection.cs b/WarmlyShip/WarmlyShip/MapsCollection.cs index 742b90f..3f650a4 100644 --- a/WarmlyShip/WarmlyShip/MapsCollection.cs +++ b/WarmlyShip/WarmlyShip/MapsCollection.cs @@ -11,7 +11,7 @@ namespace WarmlyShip /// /// Словарь (хранилище) с картами /// - readonly Dictionary> _mapStorages; + readonly Dictionary> _mapStorages; /// /// Возвращение списка названий карт /// @@ -24,6 +24,13 @@ namespace WarmlyShip /// Высота окна отрисовки /// private readonly int _pictureHeight; + /// Разделитель для записи информации по элементу словаря в файл + /// + private readonly char separatorDict = '|'; + /// + /// Разделитель для записей коллекции данных в файл + /// + private readonly char separatorData = ';'; /// /// Конструктор /// @@ -31,7 +38,7 @@ namespace WarmlyShip /// public MapsCollection(int pictureWidth, int pictureHeight) { - _mapStorages = new Dictionary>(); + _mapStorages = new Dictionary>(); _pictureWidth = pictureWidth; _pictureHeight = pictureHeight; } @@ -57,7 +64,7 @@ namespace WarmlyShip /// /// /// - public MapWithSetShipsGeneric this[string ind] + public MapWithSetShipsGeneric this[string ind] { get { @@ -65,5 +72,67 @@ namespace WarmlyShip return mapWithSetShipsGeneric; } } + /// + /// Сохранение информации по кораблям в хранилище в файл + /// + /// Путь и имя файла + /// + 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; + } + using (StreamReader fs = new(filename)) + { + if (!fs.ReadLine().Contains("MapsCollection")) + { + //если нет такой записи, то это не те данные + return false; + } + //очищаем записи + _mapStorages.Clear(); + while (!fs.EndOfStream) + { + var elem = fs.ReadLine().Split(separatorDict); + AbstractMap map = null; + switch (elem[1]) + { + case "SimpleMap": + map = new SimpleMap(); + break; + case "WallMap": + map = new OceanMap(); + break; + } + _mapStorages.Add(elem[0], new + MapWithSetShipsGeneric(_pictureWidth, _pictureHeight, map)); + _mapStorages[elem[0]].LoadData(elem[2].Split(separatorData, + StringSplitOptions.RemoveEmptyEntries)); + } + } + return true; + } } }