From cc983c32720f96506ae536c6b5848ef728f3cd96 Mon Sep 17 00:00:00 2001 From: Daniel Date: Tue, 13 Dec 2022 01:49:12 +0400 Subject: [PATCH] =?UTF-8?q?6=20=D0=BB=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D0=BD=D0=B0=D1=8F=20=D0=BF=D0=BE=20=D1=81=D0=BE?= =?UTF-8?q?=D1=85=D1=80=D0=B0=D0=BD=D0=B5=D0=BD=D0=B8=D1=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- WinFormsApp1/DrawningObjectTraktor.cs | 5 ++ WinFormsApp1/EntityTractor.cs | 7 ++ WinFormsApp1/ExtentionTraktor.cs | 45 +++++++++++ .../FormMapWithSetTraktor.Designer.cs | 80 ++++++++++++++++--- WinFormsApp1/FormMapWithSetTraktor.cs | 30 +++++++ WinFormsApp1/IDrawningObject.cs | 2 + WinFormsApp1/MapWithSetTraktorGeneric.cs | 28 +++++-- WinFormsApp1/MapsCollection.cs | 75 +++++++++++++++-- 8 files changed, 252 insertions(+), 20 deletions(-) create mode 100644 WinFormsApp1/ExtentionTraktor.cs diff --git a/WinFormsApp1/DrawningObjectTraktor.cs b/WinFormsApp1/DrawningObjectTraktor.cs index a7973ec..66c5490 100644 --- a/WinFormsApp1/DrawningObjectTraktor.cs +++ b/WinFormsApp1/DrawningObjectTraktor.cs @@ -4,6 +4,7 @@ using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; +using Tractors; namespace WinFormsApp1 { @@ -23,6 +24,10 @@ namespace WinFormsApp1 return _tractor?.GetCurrentPosition() ?? default; } + public string GetInfo() => _tractor?.GetDataForSave(); + + public static IDrawningObject Create(string data) => new DrawningObjectTractor(data.CreateDrawingTraktor()); + public void MoveObject(Direction direction) { _tractor?.MoveTransport(direction); diff --git a/WinFormsApp1/EntityTractor.cs b/WinFormsApp1/EntityTractor.cs index c055e9b..a4bb75d 100644 --- a/WinFormsApp1/EntityTractor.cs +++ b/WinFormsApp1/EntityTractor.cs @@ -34,5 +34,12 @@ namespace WinFormsApp1 Weight = weight <= 0 ? rnd.Next(40, 70) : weight; BodyColor = bodyColor; } + + public static EntityTractor Creator(string data) + { + string[] strs = data.Split(':'); + return new EntityTractor(Convert.ToInt32(strs[0]), + Convert.ToInt32(strs[1]), Color.FromName(strs[2])); + } } } diff --git a/WinFormsApp1/ExtentionTraktor.cs b/WinFormsApp1/ExtentionTraktor.cs new file mode 100644 index 0000000..bdd693a --- /dev/null +++ b/WinFormsApp1/ExtentionTraktor.cs @@ -0,0 +1,45 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using WinFormsApp1; + +namespace WinFormsApp1 +{ + internal static class ExtentionTraktor + { + private static readonly char _separatorForObject = ':'; + + public static TractorDraw CreateDrawingTraktor(this string info) + { + string[] strs = info.Split(_separatorForObject); + if (strs.Length == 3) + { + return new TractorDraw(Convert.ToInt32(strs[0]), + Convert.ToInt32(strs[1]), Color.FromName(strs[2])); + } + if (strs.Length == 6) + { + return new MultiTraktorDraw( + 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 TractorDraw drawingTraktor) + { + var traktor = drawingTraktor.Tractor; + var str = $"{traktor.Speed}{_separatorForObject}{traktor.Weight}{_separatorForObject}{traktor.BodyColor.Name}"; + if (traktor is not MultiTraktor multiTraktor) + { + return str; + } + return $"{str}{_separatorForObject}{multiTraktor.DopColor.Name}{_separatorForObject}{multiTraktor.dopAhead}{_separatorForObject}{multiTraktor.dopBehind}"; + } + } +} diff --git a/WinFormsApp1/FormMapWithSetTraktor.Designer.cs b/WinFormsApp1/FormMapWithSetTraktor.Designer.cs index 01ce979..2906c11 100644 --- a/WinFormsApp1/FormMapWithSetTraktor.Designer.cs +++ b/WinFormsApp1/FormMapWithSetTraktor.Designer.cs @@ -47,9 +47,16 @@ namespace WinFormsApp1 this.buttonRemoveTraktor = new System.Windows.Forms.Button(); this.buttonAddTraktor = 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.openFileDialog = new System.Windows.Forms.OpenFileDialog(); + this.saveFileDialog = new System.Windows.Forms.SaveFileDialog(); this.groupBox1.SuspendLayout(); this.groupBoxMaps.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit(); + this.menuStrip.SuspendLayout(); this.SuspendLayout(); // // groupBox1 @@ -65,11 +72,11 @@ namespace WinFormsApp1 this.groupBox1.Controls.Add(this.buttonRemoveTraktor); this.groupBox1.Controls.Add(this.buttonAddTraktor); this.groupBox1.Dock = System.Windows.Forms.DockStyle.Right; - this.groupBox1.Location = new System.Drawing.Point(596, 0); + this.groupBox1.Location = new System.Drawing.Point(596, 24); this.groupBox1.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); this.groupBox1.Name = "groupBox1"; this.groupBox1.Padding = new System.Windows.Forms.Padding(3, 2, 3, 2); - this.groupBox1.Size = new System.Drawing.Size(219, 476); + this.groupBox1.Size = new System.Drawing.Size(219, 515); this.groupBox1.TabIndex = 0; this.groupBox1.TabStop = false; this.groupBox1.Text = "Инструменты"; @@ -143,7 +150,7 @@ namespace WinFormsApp1 this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonRight.BackgroundImage = global::Tractors.Properties.Resources.RkYIe2_6DuQ; this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonRight.Location = new System.Drawing.Point(126, 435); + this.buttonRight.Location = new System.Drawing.Point(126, 442); this.buttonRight.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); this.buttonRight.Name = "buttonRight"; this.buttonRight.Size = new System.Drawing.Size(35, 30); @@ -156,7 +163,7 @@ namespace WinFormsApp1 this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonDown.BackgroundImage = global::Tractors.Properties.Resources.MbV2DYU_nPM; this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonDown.Location = new System.Drawing.Point(91, 435); + this.buttonDown.Location = new System.Drawing.Point(91, 442); this.buttonDown.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); this.buttonDown.Name = "buttonDown"; this.buttonDown.Size = new System.Drawing.Size(35, 30); @@ -169,7 +176,7 @@ namespace WinFormsApp1 this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonLeft.BackgroundImage = global::Tractors.Properties.Resources.Hhxt4dLqV5g; this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonLeft.Location = new System.Drawing.Point(56, 435); + this.buttonLeft.Location = new System.Drawing.Point(56, 442); this.buttonLeft.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); this.buttonLeft.Name = "buttonLeft"; this.buttonLeft.Size = new System.Drawing.Size(35, 30); @@ -182,7 +189,7 @@ namespace WinFormsApp1 this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonUp.BackgroundImage = global::Tractors.Properties.Resources._2EdzyM4iEKw; this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonUp.Location = new System.Drawing.Point(91, 405); + this.buttonUp.Location = new System.Drawing.Point(91, 412); this.buttonUp.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); this.buttonUp.Name = "buttonUp"; this.buttonUp.Size = new System.Drawing.Size(35, 30); @@ -245,20 +252,66 @@ namespace WinFormsApp1 // 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.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); this.pictureBox.Name = "pictureBox"; - this.pictureBox.Size = new System.Drawing.Size(596, 476); + this.pictureBox.Size = new System.Drawing.Size(596, 515); 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(815, 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"; + this.openFileDialog.Filter = "txt file | *.txt"; + // + // saveFileDialog + // + this.saveFileDialog.Filter = "txt file | *.txt"; + // // // FormMapWithSetTraktor // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(815, 476); + this.ClientSize = new System.Drawing.Size(815, 538); this.Controls.Add(this.pictureBox); this.Controls.Add(this.groupBox1); + this.Controls.Add(this.menuStrip); + this.MainMenuStrip = this.menuStrip; this.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); this.Name = "FormMapWithSetTraktor"; this.Text = "Автобус"; @@ -267,7 +320,10 @@ namespace WinFormsApp1 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(); } @@ -290,5 +346,11 @@ namespace WinFormsApp1 private Button buttonAddMap; private TextBox textBoxNewMapName; private ListBox listBoxMaps; + 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/WinFormsApp1/FormMapWithSetTraktor.cs b/WinFormsApp1/FormMapWithSetTraktor.cs index dbf51b8..a76aaf7 100644 --- a/WinFormsApp1/FormMapWithSetTraktor.cs +++ b/WinFormsApp1/FormMapWithSetTraktor.cs @@ -215,5 +215,35 @@ namespace WinFormsApp1 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)) + { + MessageBox.Show("Загрузка прошла успешно!", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); + ReloadMaps(); + } + else + { + MessageBox.Show("Не загрузилось", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } } } diff --git a/WinFormsApp1/IDrawningObject.cs b/WinFormsApp1/IDrawningObject.cs index e257f3a..06e1b86 100644 --- a/WinFormsApp1/IDrawningObject.cs +++ b/WinFormsApp1/IDrawningObject.cs @@ -25,5 +25,7 @@ namespace WinFormsApp1 void DrawningObject(Graphics g); /// Получение текущей позиции объекта (float Left, float Right, float Top, float Bottom) GetCurrentPosition(); + + string GetInfo(); } } diff --git a/WinFormsApp1/MapWithSetTraktorGeneric.cs b/WinFormsApp1/MapWithSetTraktorGeneric.cs index a1658f0..e7ebc34 100644 --- a/WinFormsApp1/MapWithSetTraktorGeneric.cs +++ b/WinFormsApp1/MapWithSetTraktorGeneric.cs @@ -28,9 +28,9 @@ namespace WinFormsApp1 _map = map; } - public static int operator +(MapWithSetTraktorGeneric map, T bus) + public static int operator +(MapWithSetTraktorGeneric map, T traktor) { - return map._setTraktors.Insert(bus); + return map._setTraktors.Insert(traktor); } public static T operator -(MapWithSetTraktorGeneric map, int position) @@ -75,10 +75,10 @@ namespace WinFormsApp1 { for (; j > i; j--) { - var bus = _setTraktors[j]; - if (bus != null) + var traktor = _setTraktors[j]; + if (traktor != null) { - _setTraktors.Insert(bus, i); + _setTraktors.Insert(traktor, i); _setTraktors.Remove(j); break; } @@ -135,5 +135,23 @@ namespace WinFormsApp1 } } } + + public string GetData(char separatorType, char separatorData) + { + string data = $"{_map.GetType().Name}{separatorType}"; + foreach (var traktor in _setTraktors.GetTraktor()) + { + data += $"{traktor.GetInfo()}{separatorData}"; + } + return data; + } + + public void LoadData(string[] records) + { + foreach (var rec in records) + { + _setTraktors.Insert(DrawningObjectTractor.Create(rec) as T); + } + } } } diff --git a/WinFormsApp1/MapsCollection.cs b/WinFormsApp1/MapsCollection.cs index 498b24f..900299e 100644 --- a/WinFormsApp1/MapsCollection.cs +++ b/WinFormsApp1/MapsCollection.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -8,7 +9,7 @@ namespace WinFormsApp1 { internal class MapsCollection { - readonly Dictionary> _mapStorages; + readonly Dictionary> _mapStorages; public List Keys => _mapStorages.Keys.ToList(); @@ -16,9 +17,13 @@ namespace WinFormsApp1 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 +33,7 @@ namespace WinFormsApp1 if (_mapStorages.ContainsKey(name)) return; //уникальное имя else { - _mapStorages.Add(name, new MapWithSetTraktorGeneric(_pictureWidth, _pictureHeight, map)); + _mapStorages.Add(name, new MapWithSetTraktorGeneric(_pictureWidth, _pictureHeight, map)); } } @@ -40,14 +45,72 @@ namespace WinFormsApp1 } } - public MapWithSetTraktorGeneric this[string ind] + public MapWithSetTraktorGeneric this[string ind] { get { - _mapStorages.TryGetValue(ind, out var MapWithSetDoubleDeckerBusGeneric); - return MapWithSetDoubleDeckerBusGeneric; + _mapStorages.TryGetValue(ind, out var result); + return result; } } + 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(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 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 "Простая карта": + map = new SimpleMap(); + break; + case "Поле": + map = new FieldMap(); + break; + } + _mapStorages.Add(elem[0], new MapWithSetTraktorGeneric(_pictureWidth, _pictureHeight, map)); + _mapStorages[elem[0]].LoadData(elem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries)); + } + } + return true; + } } }