diff --git a/Monorail/Monorail/DrawingObjectLocomotive.cs b/Monorail/Monorail/DrawingObjectLocomotive.cs index 9784293..8f82693 100644 --- a/Monorail/Monorail/DrawingObjectLocomotive.cs +++ b/Monorail/Monorail/DrawingObjectLocomotive.cs @@ -36,6 +36,8 @@ namespace Monorail { _locomotive.DrawTransport(g); } + public string GetInfo() => _locomotive?.GetDataForSave(); + public static IDrawingObject Create(string data) => new DrawingObjectLocomotive(data.CreateDrawingLocomotive()); } } diff --git a/Monorail/Monorail/ExtentionLocomotive.cs b/Monorail/Monorail/ExtentionLocomotive.cs new file mode 100644 index 0000000..e4663c8 --- /dev/null +++ b/Monorail/Monorail/ExtentionLocomotive.cs @@ -0,0 +1,38 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Monorail +{ + internal static class ExtentionLocomotive + { + private static readonly char _separatorForObject = ':'; + public static DrawingLocomotive CreateDrawingLocomotive(this string info) + { + string[] strs = info.Split(_separatorForObject); + if (strs.Length == 3) + { + return new DrawingLocomotive(Convert.ToInt32(strs[0]), + Convert.ToInt32(strs[1]), Color.FromName(strs[2])); + } + if (strs.Length == 6) + { + return new DrawingMonorailLocomotive(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 DrawingLocomotive DrawingCar) + { + var locomotive = DrawingCar.Locomotive; + var str = $"{locomotive.Speed}{_separatorForObject}{locomotive.Weight}{_separatorForObject}{locomotive.BodyColor.Name}"; + if (locomotive is not EntityMonorailLocomotive monorailLocomotive) + { + return str; + } + return + $"{str}{_separatorForObject}{monorailLocomotive.DopColor.Name}{_separatorForObject}{monorailLocomotive.DopCabin}{_separatorForObject}{monorailLocomotive.Monorail}"; + } + } +} diff --git a/Monorail/Monorail/FormMapWithSetLocomotive.Designer.cs b/Monorail/Monorail/FormMapWithSetLocomotive.Designer.cs index fc6abdf..57ab501 100644 --- a/Monorail/Monorail/FormMapWithSetLocomotive.Designer.cs +++ b/Monorail/Monorail/FormMapWithSetLocomotive.Designer.cs @@ -45,17 +45,24 @@ buttonDeleteMap = new Button(); buttonAddMap = new Button(); listBoxMaps = new ListBox(); + menuStrip = new MenuStrip(); + ToolStripMenuItem = new ToolStripMenuItem(); + SaveToolStripMenuItem = new ToolStripMenuItem(); + LoadToolStripMenuItem = new ToolStripMenuItem(); + openFileDialog = new OpenFileDialog(); + saveFileDialog = new SaveFileDialog(); ((System.ComponentModel.ISupportInitialize)pictureBoxLocomotive).BeginInit(); groupBoxTools.SuspendLayout(); groupBoxMaps.SuspendLayout(); + menuStrip.SuspendLayout(); SuspendLayout(); // // pictureBoxLocomotive // pictureBoxLocomotive.Dock = DockStyle.Left; - pictureBoxLocomotive.Location = new Point(0, 0); + pictureBoxLocomotive.Location = new Point(0, 24); pictureBoxLocomotive.Name = "pictureBoxLocomotive"; - pictureBoxLocomotive.Size = new Size(579, 561); + pictureBoxLocomotive.Size = new Size(579, 537); pictureBoxLocomotive.TabIndex = 0; pictureBoxLocomotive.TabStop = false; // @@ -231,6 +238,44 @@ listBoxMaps.TabIndex = 12; listBoxMaps.SelectedIndexChanged += listBoxMaps_SelectedIndexChanged; // + // menuStrip + // + menuStrip.Items.AddRange(new ToolStripItem[] { ToolStripMenuItem }); + menuStrip.Location = new Point(0, 0); + menuStrip.Name = "menuStrip"; + menuStrip.Size = new Size(800, 24); + menuStrip.TabIndex = 13; + menuStrip.Text = "menuStrip1"; + // + // ToolStripMenuItem + // + ToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { SaveToolStripMenuItem, LoadToolStripMenuItem }); + ToolStripMenuItem.Name = "ToolStripMenuItem"; + ToolStripMenuItem.Size = new Size(48, 20); + ToolStripMenuItem.Text = "Файл"; + // + // SaveToolStripMenuItem + // + SaveToolStripMenuItem.Name = "SaveToolStripMenuItem"; + SaveToolStripMenuItem.Size = new Size(180, 22); + SaveToolStripMenuItem.Text = "Сохранение"; + SaveToolStripMenuItem.Click += SaveToolStripMenuItem_Click; + // + // LoadToolStripMenuItem + // + LoadToolStripMenuItem.Name = "LoadToolStripMenuItem"; + LoadToolStripMenuItem.Size = new Size(180, 22); + LoadToolStripMenuItem.Text = "Загрузка"; + LoadToolStripMenuItem.Click += LoadToolStripMenuItem_Click; + // + // openFileDialog + // + openFileDialog.Filter = "txt file | *.txt"; + // + // saveFileDialog + // + saveFileDialog.Filter = "txt file | *.txt"; + // // FormMapWithSetLocomotive // AutoScaleDimensions = new SizeF(7F, 15F); @@ -238,6 +283,8 @@ ClientSize = new Size(800, 561); Controls.Add(groupBoxTools); Controls.Add(pictureBoxLocomotive); + Controls.Add(menuStrip); + MainMenuStrip = menuStrip; Name = "FormMapWithSetLocomotive"; Text = "Карта с набором объектов"; ((System.ComponentModel.ISupportInitialize)pictureBoxLocomotive).EndInit(); @@ -245,7 +292,10 @@ groupBoxTools.PerformLayout(); groupBoxMaps.ResumeLayout(false); groupBoxMaps.PerformLayout(); + menuStrip.ResumeLayout(false); + menuStrip.PerformLayout(); ResumeLayout(false); + PerformLayout(); } #endregion @@ -267,5 +317,11 @@ private ListBox listBoxMaps; private GroupBox groupBoxMaps; 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/Monorail/Monorail/FormMapWithSetLocomotive.cs b/Monorail/Monorail/FormMapWithSetLocomotive.cs index c8f49d0..a81148a 100644 --- a/Monorail/Monorail/FormMapWithSetLocomotive.cs +++ b/Monorail/Monorail/FormMapWithSetLocomotive.cs @@ -181,5 +181,40 @@ namespace Monorail } pictureBoxLocomotive.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)) + { + ReloadMaps(); + MessageBox.Show("Загрузка прошла успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); + } + else + { + MessageBox.Show("Не загрузилось", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } + } } diff --git a/Monorail/Monorail/FormMapWithSetLocomotive.resx b/Monorail/Monorail/FormMapWithSetLocomotive.resx index f298a7b..934ed35 100644 --- a/Monorail/Monorail/FormMapWithSetLocomotive.resx +++ b/Monorail/Monorail/FormMapWithSetLocomotive.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/Monorail/Monorail/IDrawingObject.cs b/Monorail/Monorail/IDrawingObject.cs index 2e7174f..a2c1fb2 100644 --- a/Monorail/Monorail/IDrawingObject.cs +++ b/Monorail/Monorail/IDrawingObject.cs @@ -36,6 +36,7 @@ namespace Monorail /// /// (float Left, float Right, float Top, float Bottom) GetCurrentPosition(); + string GetInfo(); } } diff --git a/Monorail/Monorail/MapWithSetLocomotiveGeneric.cs b/Monorail/Monorail/MapWithSetLocomotiveGeneric.cs index 611b9ee..d09ff7a 100644 --- a/Monorail/Monorail/MapWithSetLocomotiveGeneric.cs +++ b/Monorail/Monorail/MapWithSetLocomotiveGeneric.cs @@ -7,7 +7,7 @@ using System.Threading.Tasks; namespace Monorail { internal class MapWithSetLocomotiveGeneric -where T : class, IDrawingObject + where T : class, IDrawingObject where U : AbstractMap { // Ширина окна отрисовки @@ -131,5 +131,22 @@ where T : class, IDrawingObject } } } + + public string GetData(char separatorType, char separatorData) + { + string data = $"{_map.GetType().Name}{separatorType}"; + foreach (var boat in _setLocomotive.GetLocomotives()) + { + data += $"{boat.GetInfo()}{separatorData}"; + } + return data; + } + public void LoadData(string[] records) + { + foreach (var rec in records) + { + _setLocomotive.Insert(DrawingObjectLocomotive.Create(rec) as T); + } + } } } diff --git a/Monorail/Monorail/MapsCollection.cs b/Monorail/Monorail/MapsCollection.cs index 59e2ec8..f91f3d2 100644 --- a/Monorail/Monorail/MapsCollection.cs +++ b/Monorail/Monorail/MapsCollection.cs @@ -8,42 +8,102 @@ namespace Monorail { 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; } - public void AddMap(string name, AbstractMap map) { if (!_mapStorages.ContainsKey(name)) { - _mapStorages.Add(name, new MapWithSetLocomotiveGeneric - (_pictureWidth, _pictureHeight, map)); + _mapStorages.Add(name, new MapWithSetLocomotiveGeneric(_pictureWidth, _pictureHeight, map)); } } public void DelMap(string name) { - if (_mapStorages.ContainsKey(name)) - _mapStorages.Remove(name); + if (_mapStorages.ContainsKey(name)) _mapStorages.Remove(name); } - public MapWithSetLocomotiveGeneric this[string ind] + public MapWithSetLocomotiveGeneric this[string ind] { get { if (_mapStorages.ContainsKey(ind)) + { return _mapStorages[ind]; - else - return null; + } + 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 (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 "BushesMap": + map = new BushesMap(); + break; + case "FieldMap": + map = new FieldMap(); + break; + } + _mapStorages.Add(elem[0], new MapWithSetLocomotiveGeneric(_pictureWidth, _pictureHeight, map)); + _mapStorages[elem[0]].LoadData(elem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries)); + } + } + return true; + } } }