diff --git a/MotorBoat/MotorBoat/DrawningObjectBoat.cs b/MotorBoat/MotorBoat/DrawningObjectBoat.cs index f551b37..c6685be 100644 --- a/MotorBoat/MotorBoat/DrawningObjectBoat.cs +++ b/MotorBoat/MotorBoat/DrawningObjectBoat.cs @@ -30,5 +30,7 @@ namespace MotorBoat { _boat.DrawTransport(g); } + public string GetInfo() => _boat?.GetDataForSave(); + public static IDrawningObject Create(string data) => new DrawningObjectBoat(data.CreateDrawningBoat()); } } diff --git a/MotorBoat/MotorBoat/ExtentionBoat.cs b/MotorBoat/MotorBoat/ExtentionBoat.cs new file mode 100644 index 0000000..5b19b6a --- /dev/null +++ b/MotorBoat/MotorBoat/ExtentionBoat.cs @@ -0,0 +1,56 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MotorBoat +{ + internal static class ExtentionCar + { + /// + /// Разделитель для записи информации по объекту в файл + /// + private static readonly char _separatorForObject = ':'; + /// + /// Создание объекта из строки + /// + /// + /// + public static DrawningBoat CreateDrawningBoat(this string info) + { + string[] strs = info.Split(_separatorForObject); + if (strs.Length == 3) + { + return new DrawningBoat(Convert.ToInt32(strs[0]), + Convert.ToInt32(strs[1]), Color.FromName(strs[2])); + } + if (strs.Length == 7) + { + return new DrawningMotorBoat(Convert.ToInt32(strs[0]), + Convert.ToInt32(strs[1]), Color.FromName(strs[2]), + Color.FromName(strs[3]), Convert.ToBoolean(strs[4]), + Convert.ToBoolean(strs[5]), Convert.ToBoolean(strs[6])); + } + return null; + } + /// + /// Получение данных для сохранения в файл + /// + /// + /// + public static string GetDataForSave(this DrawningBoat drawningBoat) + { + var boat = drawningBoat.Boat; + var str = + $"{boat.Speed}{_separatorForObject}{boat.Weight}{_separatorForObject}{boat.BodyColor.Name}"; + if (boat is not EntityMotorBoat motorBoat) + { + return str; + } + return + $"{str}{_separatorForObject}{motorBoat.DopColor.Name}{_separatorForObject}{motorBoat.BodyKit}{_separatorForObject}{motorBoat.Wing}{_separatorForObject}{motorBoat.SportLine}"; + } + + } +} diff --git a/MotorBoat/MotorBoat/FormMapWithSetBoats.Designer.cs b/MotorBoat/MotorBoat/FormMapWithSetBoats.Designer.cs index 44e533b..1894cbd 100644 --- a/MotorBoat/MotorBoat/FormMapWithSetBoats.Designer.cs +++ b/MotorBoat/MotorBoat/FormMapWithSetBoats.Designer.cs @@ -44,8 +44,15 @@ this.buttonAddBoat = new System.Windows.Forms.Button(); this.comboBoxSelectorMap = new System.Windows.Forms.ComboBox(); this.pictureBox = new System.Windows.Forms.PictureBox(); + this.menuStrip1 = 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.groupBoxTools.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit(); + this.menuStrip1.SuspendLayout(); this.SuspendLayout(); // // groupBoxTools @@ -65,9 +72,9 @@ this.groupBoxTools.Controls.Add(this.buttonAddBoat); this.groupBoxTools.Controls.Add(this.comboBoxSelectorMap); this.groupBoxTools.Dock = System.Windows.Forms.DockStyle.Right; - this.groupBoxTools.Location = new System.Drawing.Point(809, 0); + this.groupBoxTools.Location = new System.Drawing.Point(809, 24); this.groupBoxTools.Name = "groupBoxTools"; - this.groupBoxTools.Size = new System.Drawing.Size(251, 645); + this.groupBoxTools.Size = new System.Drawing.Size(251, 621); this.groupBoxTools.TabIndex = 0; this.groupBoxTools.TabStop = false; this.groupBoxTools.Text = "v"; @@ -143,7 +150,7 @@ this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonDown.BackgroundImage = global::MotorBoat.Properties.Resources.d; this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonDown.Location = new System.Drawing.Point(112, 553); + this.buttonDown.Location = new System.Drawing.Point(112, 529); this.buttonDown.Name = "buttonDown"; this.buttonDown.Size = new System.Drawing.Size(30, 30); this.buttonDown.TabIndex = 20; @@ -155,7 +162,7 @@ this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonRight.BackgroundImage = global::MotorBoat.Properties.Resources.up; this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonRight.Location = new System.Drawing.Point(148, 553); + this.buttonRight.Location = new System.Drawing.Point(148, 529); this.buttonRight.Name = "buttonRight"; this.buttonRight.Size = new System.Drawing.Size(30, 30); this.buttonRight.TabIndex = 19; @@ -167,7 +174,7 @@ this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonLeft.BackgroundImage = global::MotorBoat.Properties.Resources.left; this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonLeft.Location = new System.Drawing.Point(76, 553); + this.buttonLeft.Location = new System.Drawing.Point(76, 529); this.buttonLeft.Name = "buttonLeft"; this.buttonLeft.Size = new System.Drawing.Size(30, 30); this.buttonLeft.TabIndex = 18; @@ -179,7 +186,7 @@ this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonUp.BackgroundImage = global::MotorBoat.Properties.Resources.r; this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonUp.Location = new System.Drawing.Point(112, 517); + this.buttonUp.Location = new System.Drawing.Point(112, 493); this.buttonUp.Name = "buttonUp"; this.buttonUp.Size = new System.Drawing.Size(30, 30); this.buttonUp.TabIndex = 17; @@ -223,12 +230,54 @@ // 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(809, 645); + this.pictureBox.Size = new System.Drawing.Size(809, 621); this.pictureBox.TabIndex = 0; this.pictureBox.TabStop = false; // + // menuStrip1 + // + this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.файлToolStripMenuItem}); + this.menuStrip1.Location = new System.Drawing.Point(0, 0); + this.menuStrip1.Name = "menuStrip1"; + this.menuStrip1.Size = new System.Drawing.Size(1060, 24); + this.menuStrip1.TabIndex = 1; + this.menuStrip1.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"; + this.openFileDialog.Filter = "txt file | *.txt"; + // + // saveFileDialog + // + this.saveFileDialog.Filter = "txt file | *.txt"; + // // FormMapWithSetBoats // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); @@ -236,12 +285,16 @@ this.ClientSize = new System.Drawing.Size(1060, 645); this.Controls.Add(this.pictureBox); this.Controls.Add(this.groupBoxTools); + this.Controls.Add(this.menuStrip1); this.Name = "FormMapWithSetBoats"; - this.Text = "Лодка"; + this.Text = "FormMapWithSetBoats"; this.groupBoxTools.ResumeLayout(false); this.groupBoxTools.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit(); + this.menuStrip1.ResumeLayout(false); + this.menuStrip1.PerformLayout(); this.ResumeLayout(false); + this.PerformLayout(); } @@ -263,5 +316,11 @@ private ListBox ListBoxMaps; private Button ButtonAddMap; private TextBox textBoxNewMapName; + private MenuStrip menuStrip1; + private ToolStripMenuItem файлToolStripMenuItem; + private ToolStripMenuItem SaveToolStripMenuItem; + private ToolStripMenuItem LoadToolStripMenuItem; + private OpenFileDialog openFileDialog; + private SaveFileDialog saveFileDialog; } } \ No newline at end of file diff --git a/MotorBoat/MotorBoat/FormMapWithSetBoats.cs b/MotorBoat/MotorBoat/FormMapWithSetBoats.cs index cbc794e..45c643d 100644 --- a/MotorBoat/MotorBoat/FormMapWithSetBoats.cs +++ b/MotorBoat/MotorBoat/FormMapWithSetBoats.cs @@ -111,12 +111,12 @@ namespace MotorBoat if (_mapsCollection[ListBoxMaps.SelectedItem?.ToString() ?? string.Empty] + new DrawningObjectBoat(boat) != -1) { - MessageBox.Show("Объект добавлен"); + MessageBox.Show("Object is added"); pictureBox.Image = _mapsCollection[ListBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } else { - MessageBox.Show("Не удалось добавить"); + MessageBox.Show("Unable to add object"); } } @@ -240,6 +240,39 @@ namespace MotorBoat 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/MotorBoat/MotorBoat/FormMapWithSetBoats.resx b/MotorBoat/MotorBoat/FormMapWithSetBoats.resx index f298a7b..7df72eb 100644 --- a/MotorBoat/MotorBoat/FormMapWithSetBoats.resx +++ b/MotorBoat/MotorBoat/FormMapWithSetBoats.resx @@ -57,4 +57,13 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + 14, 13 + + + 129, 13 + + + 269, 13 + \ No newline at end of file diff --git a/MotorBoat/MotorBoat/IDrawningObject.cs b/MotorBoat/MotorBoat/IDrawningObject.cs index 68c9958..dea6b07 100644 --- a/MotorBoat/MotorBoat/IDrawningObject.cs +++ b/MotorBoat/MotorBoat/IDrawningObject.cs @@ -36,5 +36,9 @@ namespace MotorBoat /// /// (float Left, float Right, float Top, float Bottom) GetCurrentPosition(); + /// Получение информации по объекту + /// + /// + string GetInfo(); } } \ No newline at end of file diff --git a/MotorBoat/MotorBoat/MapWithSetBoatsGeneric.cs b/MotorBoat/MotorBoat/MapWithSetBoatsGeneric.cs index 27da92e..82eff4c 100644 --- a/MotorBoat/MotorBoat/MapWithSetBoatsGeneric.cs +++ b/MotorBoat/MotorBoat/MapWithSetBoatsGeneric.cs @@ -163,5 +163,22 @@ } } + public string GetData(char separatorType, char separatorData) + { + string data = $"{_map.GetType().Name}{separatorType}"; + foreach (var boat in _setBoats.GetBoat()) + { + data += $"{boat.GetInfo()}{separatorData}"; + } + return data; + } + public void LoadData(string[] records) + { + foreach (var rec in records) + { + _setBoats.Insert(DrawningObjectBoat.Create(rec) as T); + } + } + } } \ No newline at end of file diff --git a/MotorBoat/MotorBoat/MapsCollection.cs b/MotorBoat/MotorBoat/MapsCollection.cs index 06b1dd9..e873d83 100644 --- a/MotorBoat/MotorBoat/MapsCollection.cs +++ b/MotorBoat/MotorBoat/MapsCollection.cs @@ -8,13 +8,16 @@ namespace MotorBoat { 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; } @@ -22,14 +25,14 @@ namespace MotorBoat { if (!_mapStorages.ContainsKey(name)) { - _mapStorages.Add(name, new MapWithSetBoatsGeneric(_pictureWidth, _pictureHeight, map)); + _mapStorages.Add(name, new MapWithSetBoatsGeneric(_pictureWidth, _pictureHeight, map)); } } public void DelMap(string name) { if (_mapStorages.ContainsKey(name)) _mapStorages.Remove(name); } - public MapWithSetBoatsGeneric this[string ind] + public MapWithSetBoatsGeneric this[string ind] { get { @@ -40,5 +43,62 @@ namespace MotorBoat return null; } } + + public bool SaveData(string filename) + { + if (File.Exists(filename)) + { + File.Delete(filename); + } + using (StreamWriter fs = new StreamWriter(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 str = ""; + using (StreamReader fs = new StreamReader(filename)) + { + str = fs.ReadLine(); + if (!str.Contains("MapsCollection")) + { + //если нет такой записи, то это не те данные + return false; + } + _mapStorages.Clear(); + while ((str = fs.ReadLine()) != null) + { + var elem = str.Split(separatorDict); + AbstractMap map = null; + switch (elem[1]) + { + case "SimpleMap": + map = new SimpleMap(); + break; + case "PinkMap": + map = new PinkMap(); + break; + case "SeaMap": + map = new SeaMap(); + break; + } + _mapStorages.Add(elem[0], new MapWithSetBoatsGeneric(_pictureWidth, _pictureHeight, map)); + _mapStorages[elem[0]].LoadData(elem[2].Split(separatorData,StringSplitOptions.RemoveEmptyEntries)); + } + } + return true; + } + } }