diff --git a/AirFighter/AirFighter/DrawingObjectAirFighter.cs b/AirFighter/AirFighter/DrawingObjectAirFighter.cs index 54a8912..9c69e80 100644 --- a/AirFighter/AirFighter/DrawingObjectAirFighter.cs +++ b/AirFighter/AirFighter/DrawingObjectAirFighter.cs @@ -32,5 +32,8 @@ namespace AirFighter // TODO _airFighter.DrawTransport(g); } + public string GetInfo() => _airFighter?.GetDataForSave(); + public static IDrawingObject Create(string data) => return new DrawingObjectAirFighter(data.CreateDrawingAirFighter()); + } } diff --git a/AirFighter/AirFighter/ExtensionAircraft.cs b/AirFighter/AirFighter/ExtensionAircraft.cs new file mode 100644 index 0000000..8ec9f31 --- /dev/null +++ b/AirFighter/AirFighter/ExtensionAircraft.cs @@ -0,0 +1,39 @@ +namespace AirFighter +{ + internal static class ExtentionAircraft + { + private static readonly char _separatorForObject = ':'; + public static DrawingAirFighter CreateDrawingAirFighter(this string info) + { + string[] strs = info.Split(_separatorForObject); + if (strs.Length == 3) + { + return new DrawingAirFighter(Convert.ToInt32(strs[0]), + Convert.ToInt32(strs[1]), Color.FromName(strs[2])); + } + if (strs.Length == 6) + { + return new DrawingModernAirFighter(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 DrawingAirFighter drawningAircraft) + { + var aircraft = drawningAircraft.AirFighter; + var str = + $"{aircraft.Speed}{_separatorForObject}{aircraft.Weight}{_separatorForObject}" + + $"{aircraft.BodyColor.Name}"; + + if (aircraft is not EntityModernAirFighter modernAircraft) + { + return str; + } + return + $"{str}{_separatorForObject}{modernAircraft.DopColor.Name}{_separatorForObject}" + + $"{modernAircraft.Rockets}{_separatorForObject}{modernAircraft.DopWings}"; + } + } +} \ No newline at end of file diff --git a/AirFighter/AirFighter/FormAircraftConfig.Designer.cs b/AirFighter/AirFighter/FormAircraftConfig.Designer.cs index bde4d19..bfca9be 100644 --- a/AirFighter/AirFighter/FormAircraftConfig.Designer.cs +++ b/AirFighter/AirFighter/FormAircraftConfig.Designer.cs @@ -159,7 +159,7 @@ // // panelGreen // - this.panelGreen.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(192)))), ((int)(((byte)(0))))); + this.panelGreen.BackColor = System.Drawing.Color.Lime; this.panelGreen.Location = new System.Drawing.Point(148, 31); this.panelGreen.Name = "panelGreen"; this.panelGreen.Size = new System.Drawing.Size(50, 50); diff --git a/AirFighter/AirFighter/FormMapWithSetCars.Designer.cs b/AirFighter/AirFighter/FormMapWithSetCars.Designer.cs index 895120c..e3b248d 100644 --- a/AirFighter/AirFighter/FormMapWithSetCars.Designer.cs +++ b/AirFighter/AirFighter/FormMapWithSetCars.Designer.cs @@ -45,9 +45,16 @@ this.ButtonShowStorage = new System.Windows.Forms.Button(); this.buttonAdd = new System.Windows.Forms.Button(); 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.tools.SuspendLayout(); this.groupBox1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit(); + this.menuStrip1.SuspendLayout(); this.SuspendLayout(); // // tools @@ -63,9 +70,9 @@ this.tools.Controls.Add(this.ButtonShowStorage); this.tools.Controls.Add(this.buttonAdd); this.tools.Dock = System.Windows.Forms.DockStyle.Right; - this.tools.Location = new System.Drawing.Point(940, 0); + this.tools.Location = new System.Drawing.Point(940, 28); this.tools.Name = "tools"; - this.tools.Size = new System.Drawing.Size(250, 725); + this.tools.Size = new System.Drawing.Size(250, 697); this.tools.TabIndex = 0; this.tools.TabStop = false; this.tools.Text = "Инструменты"; @@ -138,7 +145,7 @@ this.RightButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.RightButton.BackgroundImage = global::AirFighter.Properties.Resources.right; this.RightButton.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.RightButton.Location = new System.Drawing.Point(148, 683); + this.RightButton.Location = new System.Drawing.Point(148, 655); this.RightButton.Name = "RightButton"; this.RightButton.RightToLeft = System.Windows.Forms.RightToLeft.No; this.RightButton.Size = new System.Drawing.Size(30, 30); @@ -151,7 +158,7 @@ this.LeftButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.LeftButton.BackgroundImage = global::AirFighter.Properties.Resources.left; this.LeftButton.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.LeftButton.Location = new System.Drawing.Point(76, 683); + this.LeftButton.Location = new System.Drawing.Point(76, 655); this.LeftButton.Name = "LeftButton"; this.LeftButton.RightToLeft = System.Windows.Forms.RightToLeft.No; this.LeftButton.Size = new System.Drawing.Size(30, 30); @@ -164,7 +171,7 @@ this.UpButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.UpButton.BackgroundImage = global::AirFighter.Properties.Resources.up; this.UpButton.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.UpButton.Location = new System.Drawing.Point(112, 648); + this.UpButton.Location = new System.Drawing.Point(112, 620); this.UpButton.Name = "UpButton"; this.UpButton.RightToLeft = System.Windows.Forms.RightToLeft.No; this.UpButton.Size = new System.Drawing.Size(30, 30); @@ -177,7 +184,7 @@ this.DownButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.DownButton.BackgroundImage = global::AirFighter.Properties.Resources.down; this.DownButton.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.DownButton.Location = new System.Drawing.Point(112, 683); + this.DownButton.Location = new System.Drawing.Point(112, 655); this.DownButton.Name = "DownButton"; this.DownButton.RightToLeft = System.Windows.Forms.RightToLeft.No; this.DownButton.Size = new System.Drawing.Size(30, 30); @@ -236,12 +243,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, 28); this.pictureBox.Name = "pictureBox"; - this.pictureBox.Size = new System.Drawing.Size(940, 725); + this.pictureBox.Size = new System.Drawing.Size(940, 697); this.pictureBox.TabIndex = 1; this.pictureBox.TabStop = false; // + // menuStrip1 + // + this.menuStrip1.ImageScalingSize = new System.Drawing.Size(20, 20); + 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(1190, 28); + this.menuStrip1.TabIndex = 2; + 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(58, 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); + // + // openFileDialog + // + this.openFileDialog.Filter = "txt file | *.txt"; + // + // saveFileDialog + // + this.saveFileDialog.Filter = "txt file | *.txt"; + // // FormMapWithSetCars // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); @@ -249,6 +298,8 @@ this.ClientSize = new System.Drawing.Size(1190, 725); this.Controls.Add(this.pictureBox); this.Controls.Add(this.tools); + this.Controls.Add(this.menuStrip1); + this.MainMenuStrip = this.menuStrip1; this.Name = "FormMapWithSetCars"; this.Text = "FormMapWithSetCars"; this.tools.ResumeLayout(false); @@ -256,7 +307,10 @@ this.groupBox1.ResumeLayout(false); this.groupBox1.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit(); + this.menuStrip1.ResumeLayout(false); + this.menuStrip1.PerformLayout(); this.ResumeLayout(false); + this.PerformLayout(); } @@ -279,5 +333,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/AirFighter/AirFighter/FormMapWithSetCars.cs b/AirFighter/AirFighter/FormMapWithSetCars.cs index 99eb221..75e2d5b 100644 --- a/AirFighter/AirFighter/FormMapWithSetCars.cs +++ b/AirFighter/AirFighter/FormMapWithSetCars.cs @@ -223,5 +223,43 @@ namespace AirFighter 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) + { + // TODO продумать логику + + 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/AirFighter/AirFighter/FormMapWithSetCars.resx b/AirFighter/AirFighter/FormMapWithSetCars.resx index f298a7b..79fa9d7 100644 --- a/AirFighter/AirFighter/FormMapWithSetCars.resx +++ b/AirFighter/AirFighter/FormMapWithSetCars.resx @@ -57,4 +57,13 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + 17, 17 + + + 152, 17 + + + 319, 17 + \ No newline at end of file diff --git a/AirFighter/AirFighter/IDrawingObject.cs b/AirFighter/AirFighter/IDrawingObject.cs index ef278d8..e12e2b8 100644 --- a/AirFighter/AirFighter/IDrawingObject.cs +++ b/AirFighter/AirFighter/IDrawingObject.cs @@ -12,6 +12,7 @@ namespace AirFighter void SetObject(int x, int y, int width, int height); void MoveObject(Direction direction); void DrawningObject(Graphics g); + string GetInfo(); (float Left, float Right, float Top, float Bottom) GetCurrentPosition(); } diff --git a/AirFighter/AirFighter/MapWithSetCarsGeneric.cs b/AirFighter/AirFighter/MapWithSetCarsGeneric.cs index cf70b17..f66a707 100644 --- a/AirFighter/AirFighter/MapWithSetCarsGeneric.cs +++ b/AirFighter/AirFighter/MapWithSetCarsGeneric.cs @@ -109,9 +109,24 @@ namespace AirFighter } return new(_pictureWidth, _pictureHeight); } - /// - /// "Взбалтываем" набор, чтобы все элементы оказались в начале - /// + + public string GetData(char separatorType, char separatorData) + { + string data = $"{_map.GetType().Name}{separatorType}"; + foreach (var car in _setCars.GetCars()) + { + data += $"{car.GetInfo()}{separatorData}"; + } + return data; + } + public void LoadData(string[] records) + { + foreach (var rec in records) + { + _setCars.Insert(DrawingObjectAirFighter.Create(rec) as T); + } + } + private void Shaking() { int j = _setCars.Count - 1; diff --git a/AirFighter/AirFighter/MapsCollection.cs b/AirFighter/AirFighter/MapsCollection.cs index 5e541cf..48d1f5e 100644 --- a/AirFighter/AirFighter/MapsCollection.cs +++ b/AirFighter/AirFighter/MapsCollection.cs @@ -8,31 +8,19 @@ namespace AirFighter { internal class MapsCollection { - /// - /// Словарь (хранилище) с картами - /// - readonly Dictionary> _mapStorages; - /// - /// Возвращение списка названий карт - /// + readonly Dictionary> _mapStorages; + public List Keys => _mapStorages.Keys.ToList(); - /// - /// Ширина окна отрисовки - /// + private readonly int _pictureWidth; - /// - /// Высота окна отрисовки - /// private readonly int _pictureHeight; - /// - /// Конструктор - /// - /// - /// + + private char separatorDict = '|'; + private char separatorData = ';'; + public MapsCollection(int pictureWidth, int pictureHeight) { - _mapStorages = new Dictionary>(); + _mapStorages = new Dictionary>(); _pictureWidth = pictureWidth; _pictureHeight = pictureHeight; } @@ -49,21 +37,86 @@ namespace AirFighter _mapStorages[name] = new(_pictureWidth, _pictureHeight, map); } - /// - /// Удаление карты - /// - /// Название карты + public void DelMap(string name) { // TODO Прописать логику для удаления _mapStorages.Remove(name); } + + 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 (FileStream fs = new(filename, FileMode.Create)) + { + WriteToFile($"MapsCollection{Environment.NewLine}", fs); + foreach (var storage in _mapStorages) + { + + WriteToFile($"{storage.Key}{separatorDict}{storage.Value.GetData(separatorDict, separatorData)}{Environment.NewLine}", fs); + } + } + return true; + } /// - /// Доступ к парковке + /// Загрузка нформации по автомобилям на парковках из файла /// - /// + /// /// - public MapWithSetCarsGeneric this[string ind] + public bool LoadData(string filename) + { + if (!File.Exists(filename)) + { + return false; + } + string bufferTextFromFile = ""; + using (FileStream fs = new(filename, FileMode.Open)) + { + byte[] b = new byte[fs.Length]; + UTF8Encoding temp = new(true); + while (fs.Read(b, 0, b.Length) > 0) + { + bufferTextFromFile += temp.GetString(b); + } + } + var strs = bufferTextFromFile.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries); + if (!strs[0].Contains("MapsCollection")) + { + //если нет такой записи, то это не те данные + return false; + } + //очищаем записи + _mapStorages.Clear(); + for (int i = 1; i < strs.Length; ++i) + { + var elem = strs[i].Split(separatorDict); + AbstractMap map = null; + switch (elem[1]) + { + case "SimpleMap": + map = new SimpleMap(); + break; + case "MyMap": + map = new MyMap(); + break; + } + _mapStorages.Add(elem[0], new MapWithSetCarsGeneric(_pictureWidth, _pictureHeight, map)); + _mapStorages[elem[0]].LoadData(elem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries)); + } + return true; + } + + + public MapWithSetCarsGeneric this[string ind] { get {