From 32033b87a5439ce59c793ff7ee563117ab174749 Mon Sep 17 00:00:00 2001 From: Danil Markov Date: Fri, 25 Nov 2022 16:31:33 +0400 Subject: [PATCH] done --- .../ContainerShip/DrawingObjectShip.cs | 4 + ContainerShip/ContainerShip/ExtentionShip.cs | 45 +++++++++++ .../FormMapWithSetShip.Designer.cs | 80 ++++++++++++++++--- .../ContainerShip/FormMapWithSetShip.cs | 32 +++++++- .../ContainerShip/FormMapWithSetShip.resx | 9 +++ .../ContainerShip/FormShipConfig.Designer.cs | 2 +- ContainerShip/ContainerShip/IDrawingObject.cs | 2 + .../ContainerShip/MapWithSetShipGeneric.cs | 17 ++++ ContainerShip/ContainerShip/MapsCollection.cs | 67 ++++++++++++++-- 9 files changed, 241 insertions(+), 17 deletions(-) create mode 100644 ContainerShip/ContainerShip/ExtentionShip.cs diff --git a/ContainerShip/ContainerShip/DrawingObjectShip.cs b/ContainerShip/ContainerShip/DrawingObjectShip.cs index 1290d72..c2a9e09 100644 --- a/ContainerShip/ContainerShip/DrawingObjectShip.cs +++ b/ContainerShip/ContainerShip/DrawingObjectShip.cs @@ -32,5 +32,9 @@ namespace ContainerShip { _ship?.DrawTransport(g); } + + public string GetInfo() => _ship?.GetDataForSave(); + + public static IDrawingObject Create(string data) => new DrawingObjectShip(data.CreateDrawingShip()); } } diff --git a/ContainerShip/ContainerShip/ExtentionShip.cs b/ContainerShip/ContainerShip/ExtentionShip.cs new file mode 100644 index 0000000..5bd7bfc --- /dev/null +++ b/ContainerShip/ContainerShip/ExtentionShip.cs @@ -0,0 +1,45 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ContainerShip +{ + internal static class ExtentionShip + { + private static readonly char _separatorForObject = ':'; + + public static DrawingShip CreateDrawingShip(this string info) + { + string[] strs = info.Split(_separatorForObject); + if (strs.Length == 3) + { + return new DrawingShip(Convert.ToInt32(strs[0]), + Convert.ToInt32(strs[1]), Color.FromName(strs[2])); + } + if (strs.Length == 6) + { + return new DrawingContainerShip(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 DrawingShip drawningShip) + { + var ship = drawningShip.Ship; + var str = + $"{ship.Speed}{_separatorForObject}{ship.Weight}{_separatorForObject}{ship.BodyColor.Name}"; + if (ship is not EntityContainerShip containerShip) + { + return str; + } + return + $"{str}{_separatorForObject}{containerShip.DopColor.Name}{_separatorForObject}{containerShip.Crane}{_separatorForObject}{containerShip.Containers}"; + } + + } +} diff --git a/ContainerShip/ContainerShip/FormMapWithSetShip.Designer.cs b/ContainerShip/ContainerShip/FormMapWithSetShip.Designer.cs index 315a49c..83fed4a 100644 --- a/ContainerShip/ContainerShip/FormMapWithSetShip.Designer.cs +++ b/ContainerShip/ContainerShip/FormMapWithSetShip.Designer.cs @@ -45,9 +45,16 @@ this.buttonRight = new System.Windows.Forms.Button(); this.buttonUp = new System.Windows.Forms.Button(); this.pictureBox = new System.Windows.Forms.PictureBox(); + this.menuStrip = new System.Windows.Forms.MenuStrip(); + this.fileToolStripMenuItem = 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.groupBox1.SuspendLayout(); this.groupBox2.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit(); + this.menuStrip.SuspendLayout(); this.SuspendLayout(); // // groupBox1 @@ -63,9 +70,9 @@ this.groupBox1.Controls.Add(this.buttonRight); this.groupBox1.Controls.Add(this.buttonUp); this.groupBox1.Dock = System.Windows.Forms.DockStyle.Right; - this.groupBox1.Location = new System.Drawing.Point(600, 0); + this.groupBox1.Location = new System.Drawing.Point(600, 24); this.groupBox1.Name = "groupBox1"; - this.groupBox1.Size = new System.Drawing.Size(200, 525); + this.groupBox1.Size = new System.Drawing.Size(200, 512); this.groupBox1.TabIndex = 0; this.groupBox1.TabStop = false; this.groupBox1.Text = "Инструменты"; @@ -132,7 +139,7 @@ this.comboBoxSelectorMap.Location = new System.Drawing.Point(6, 51); this.comboBoxSelectorMap.Name = "comboBoxSelectorMap"; this.comboBoxSelectorMap.Size = new System.Drawing.Size(176, 23); - this.comboBoxSelectorMap.TabIndex = 18; + this.comboBoxSelectorMap.TabIndex = 18; // // maskedTextBoxPosition // @@ -187,7 +194,7 @@ this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonDown.BackgroundImage = global::ContainerShip.Properties.Resources.ArrowDown; this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonDown.Location = new System.Drawing.Point(89, 483); + this.buttonDown.Location = new System.Drawing.Point(89, 470); this.buttonDown.Name = "buttonDown"; this.buttonDown.Size = new System.Drawing.Size(30, 30); this.buttonDown.TabIndex = 12; @@ -199,7 +206,7 @@ this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonLeft.BackgroundImage = global::ContainerShip.Properties.Resources.ArrowLeft; this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonLeft.Location = new System.Drawing.Point(53, 483); + this.buttonLeft.Location = new System.Drawing.Point(53, 470); this.buttonLeft.Name = "buttonLeft"; this.buttonLeft.Size = new System.Drawing.Size(30, 30); this.buttonLeft.TabIndex = 11; @@ -211,7 +218,7 @@ this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonRight.BackgroundImage = global::ContainerShip.Properties.Resources.ArrowRight; this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonRight.Location = new System.Drawing.Point(125, 483); + this.buttonRight.Location = new System.Drawing.Point(125, 470); this.buttonRight.Name = "buttonRight"; this.buttonRight.Size = new System.Drawing.Size(30, 30); this.buttonRight.TabIndex = 10; @@ -223,7 +230,7 @@ this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonUp.BackgroundImage = global::ContainerShip.Properties.Resources.ArrowUp; this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonUp.Location = new System.Drawing.Point(89, 447); + this.buttonUp.Location = new System.Drawing.Point(89, 434); this.buttonUp.Name = "buttonUp"; this.buttonUp.Size = new System.Drawing.Size(30, 30); this.buttonUp.TabIndex = 9; @@ -233,19 +240,63 @@ // 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, 525); + this.pictureBox.Size = new System.Drawing.Size(600, 512); this.pictureBox.TabIndex = 1; this.pictureBox.TabStop = false; // + // menuStrip + // + this.menuStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.fileToolStripMenuItem}); + this.menuStrip.Location = new System.Drawing.Point(0, 0); + this.menuStrip.Name = "menuStrip"; + this.menuStrip.Size = new System.Drawing.Size(800, 24); + this.menuStrip.TabIndex = 2; + this.menuStrip.Text = "menuStrip1"; + // + // fileToolStripMenuItem + // + this.fileToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.saveToolStripMenuItem, + this.loadToolStripMenuItem}); + this.fileToolStripMenuItem.Name = "fileToolStripMenuItem"; + this.fileToolStripMenuItem.Size = new System.Drawing.Size(48, 20); + this.fileToolStripMenuItem.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"; + this.openFileDialog.Filter = "txt file | *.txt"; + // + // saveFileDialog + // + this.saveFileDialog.Filter = "txt file | *.txt"; + // // FormMapWithSetShip // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(800, 525); + this.ClientSize = new System.Drawing.Size(800, 536); this.Controls.Add(this.pictureBox); this.Controls.Add(this.groupBox1); + this.Controls.Add(this.menuStrip); + this.MainMenuStrip = this.menuStrip; this.Name = "FormMapWithSetShip"; this.Text = "FormMapWithSetShip"; this.groupBox1.ResumeLayout(false); @@ -253,7 +304,10 @@ this.groupBox2.ResumeLayout(false); this.groupBox2.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit(); + this.menuStrip.ResumeLayout(false); + this.menuStrip.PerformLayout(); this.ResumeLayout(false); + this.PerformLayout(); } @@ -276,5 +330,11 @@ private ListBox listBoxMaps; private TextBox textBoxNewMapName; private Button buttonAddMap; + private MenuStrip menuStrip; + private ToolStripMenuItem fileToolStripMenuItem; + private ToolStripMenuItem saveToolStripMenuItem; + private ToolStripMenuItem loadToolStripMenuItem; + private OpenFileDialog openFileDialog; + private SaveFileDialog saveFileDialog; } } \ No newline at end of file diff --git a/ContainerShip/ContainerShip/FormMapWithSetShip.cs b/ContainerShip/ContainerShip/FormMapWithSetShip.cs index a5af32e..b7427d9 100644 --- a/ContainerShip/ContainerShip/FormMapWithSetShip.cs +++ b/ContainerShip/ContainerShip/FormMapWithSetShip.cs @@ -168,7 +168,6 @@ namespace ContainerShip } pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].MoveObject(dir); } - private void ListBoxMaps_SelectedIndexChanged(object sender, EventArgs e) { pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); @@ -187,5 +186,36 @@ namespace ContainerShip ReloadMaps(); } } + 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/ContainerShip/ContainerShip/FormMapWithSetShip.resx b/ContainerShip/ContainerShip/FormMapWithSetShip.resx index f298a7b..934ed35 100644 --- a/ContainerShip/ContainerShip/FormMapWithSetShip.resx +++ b/ContainerShip/ContainerShip/FormMapWithSetShip.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/ContainerShip/ContainerShip/FormShipConfig.Designer.cs b/ContainerShip/ContainerShip/FormShipConfig.Designer.cs index 29b8d51..a55457b 100644 --- a/ContainerShip/ContainerShip/FormShipConfig.Designer.cs +++ b/ContainerShip/ContainerShip/FormShipConfig.Designer.cs @@ -146,7 +146,7 @@ // // panelPurple // - this.panelPurple.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(0)))), ((int)(((byte)(192))))); + this.panelPurple.BackColor = System.Drawing.Color.Purple; this.panelPurple.Location = new System.Drawing.Point(144, 68); this.panelPurple.Name = "panelPurple"; this.panelPurple.Size = new System.Drawing.Size(40, 40); diff --git a/ContainerShip/ContainerShip/IDrawingObject.cs b/ContainerShip/ContainerShip/IDrawingObject.cs index e283741..79d3c43 100644 --- a/ContainerShip/ContainerShip/IDrawingObject.cs +++ b/ContainerShip/ContainerShip/IDrawingObject.cs @@ -17,5 +17,7 @@ namespace ContainerShip void DrawingObject(Graphics g); (float Left, float Right, float Top, float Bottom) GetCurrentPosition(); + + string GetInfo(); } } diff --git a/ContainerShip/ContainerShip/MapWithSetShipGeneric.cs b/ContainerShip/ContainerShip/MapWithSetShipGeneric.cs index afbc8f6..ecd61f2 100644 --- a/ContainerShip/ContainerShip/MapWithSetShipGeneric.cs +++ b/ContainerShip/ContainerShip/MapWithSetShipGeneric.cs @@ -158,5 +158,22 @@ namespace ContainerShip } } } + public string GetData(char separatorType, char separatorData) + { + string data = $"{_map.GetType().Name}{separatorType}"; + foreach (var ship in _setShip.GetShip().Reverse()) + { + data += $"{ship.GetInfo()}{separatorData}"; + } + return data; + } + + public void LoadData(string[] records) + { + foreach (var rec in records) + { + _setShip.Insert(DrawingObjectShip.Create(rec) as T); + } + } } } diff --git a/ContainerShip/ContainerShip/MapsCollection.cs b/ContainerShip/ContainerShip/MapsCollection.cs index a321740..1d9163a 100644 --- a/ContainerShip/ContainerShip/MapsCollection.cs +++ b/ContainerShip/ContainerShip/MapsCollection.cs @@ -8,16 +8,20 @@ namespace ContainerShip { internal class MapsCollection { - readonly Dictionary> _mapStorages; + readonly Dictionary> _mapStorages; public List Keys => _mapStorages.Keys.ToList(); private readonly int _pictureWidth; 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; } @@ -28,7 +32,7 @@ namespace ContainerShip { return; } - MapWithSetShipGeneric newMap = new(_pictureWidth, _pictureHeight, map); + MapWithSetShipGeneric newMap = new(_pictureWidth, _pictureHeight, map); _mapStorages.Add(name, newMap); } @@ -40,7 +44,7 @@ namespace ContainerShip } } - public MapWithSetShipGeneric this[string + public MapWithSetShipGeneric this[string ind] { get @@ -52,5 +56,58 @@ namespace ContainerShip 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)) + { + string str; + if ((str = sr.ReadLine()) == null || !str.Contains("MapsCollection")) + { + 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 "IslandsMap": + map = new IslandsMap(); + break; + case "RocksMap": + map = new RocksMap(); + break; + } + _mapStorages.Add(elem[0], new MapWithSetShipGeneric(_pictureWidth, _pictureHeight, map)); + _mapStorages[elem[0]].LoadData(elem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries)); + } + } + return true; + } } } -- 2.25.1