diff --git a/Tank/Tank/ExtentionDrawingTank.cs b/Tank/Tank/ExtentionDrawingTank.cs new file mode 100644 index 0000000..a3df396 --- /dev/null +++ b/Tank/Tank/ExtentionDrawingTank.cs @@ -0,0 +1,49 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Tank.DrawningObjects; +using Tank.Entities; + +namespace Tank +{ + public static class ExtentionDrawingTank + { + public static DrawArmoVehicle? CreateDrawTank(this string info, char separatorForObject, int width, int height) + { + string[] strs = info.Split(separatorForObject); + if (strs.Length == 3) + { + return new DrawArmoVehicle(Convert.ToInt32(strs[0]), + Convert.ToInt32(strs[1]), Color.FromName(strs[2]), width, height); + } + if (strs.Length == 7) + { + return new DrawTank(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]), width, height); + } + return null; + } + + public static string GetDataForSave(this DrawArmoVehicle vehicle, char separatorForObject) + { + var Vehicle = vehicle.Tank; + if (Vehicle == null) + { + return string.Empty; + } + var str = $"{Vehicle.Speed}{separatorForObject}{Vehicle.Weight}{separatorForObject}{Vehicle.BodyColor.Name}"; + if (Vehicle is not EntityTank tank) + { + return str; + } + return $"{str}{separatorForObject}{tank.AdditionalColor.Name}{separatorForObject}{tank.BodyKit}{separatorForObject}{tank.Caterpillar}{separatorForObject}{tank.Tower}"; + } + } +} \ No newline at end of file diff --git a/Tank/Tank/FormTanksCollections.Designer.cs b/Tank/Tank/FormTanksCollections.Designer.cs index 3b605e4..4f047a9 100644 --- a/Tank/Tank/FormTanksCollections.Designer.cs +++ b/Tank/Tank/FormTanksCollections.Designer.cs @@ -29,6 +29,10 @@ private void InitializeComponent() { panel1 = new Panel(); + menuStrip1 = new MenuStrip(); + StripMenu = new ToolStripMenuItem(); + SaveItem = new ToolStripMenuItem(); + LoadItem = new ToolStripMenuItem(); panel2 = new Panel(); DeleteCollectButton = new Button(); CollectionListBox = new ListBox(); @@ -41,13 +45,17 @@ InputNum = new TextBox(); label1 = new Label(); DrawTank = new PictureBox(); + openFileDialog = new OpenFileDialog(); + saveFileDialog = new SaveFileDialog(); panel1.SuspendLayout(); + menuStrip1.SuspendLayout(); panel2.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)DrawTank).BeginInit(); SuspendLayout(); // // panel1 // + panel1.Controls.Add(menuStrip1); panel1.Controls.Add(panel2); panel1.Controls.Add(UpdateButton); panel1.Controls.Add(DeleteButton); @@ -55,12 +63,42 @@ panel1.Controls.Add(InputNum); panel1.Controls.Add(label1); panel1.Dock = DockStyle.Right; - panel1.Location = new Point(568, 0); - panel1.Margin = new Padding(3, 2, 3, 2); + panel1.Location = new Point(694, 0); panel1.Name = "panel1"; - panel1.Size = new Size(207, 429); + panel1.Size = new Size(237, 479); panel1.TabIndex = 0; // + // menuStrip1 + // + menuStrip1.ImageScalingSize = new Size(20, 20); + menuStrip1.Items.AddRange(new ToolStripItem[] { StripMenu }); + menuStrip1.Location = new Point(0, 0); + menuStrip1.Name = "menuStrip1"; + menuStrip1.Size = new Size(237, 28); + menuStrip1.TabIndex = 7; + menuStrip1.Text = "menuStrip1"; + // + // StripMenu + // + StripMenu.DropDownItems.AddRange(new ToolStripItem[] { SaveItem, LoadItem }); + StripMenu.Name = "StripMenu"; + StripMenu.Size = new Size(59, 24); + StripMenu.Text = "Файл"; + // + // SaveItem + // + SaveItem.Name = "SaveItem"; + SaveItem.Size = new Size(166, 26); + SaveItem.Text = "Сохранить"; + SaveItem.Click += SaveToolStripMenuItem_Click; + // + // LoadItem + // + LoadItem.Name = "LoadItem"; + LoadItem.Size = new Size(166, 26); + LoadItem.Text = "Загрузить"; + LoadItem.Click += LoadToolStripMenuItem_Click; + // // panel2 // panel2.Controls.Add(DeleteCollectButton); @@ -68,18 +106,16 @@ panel2.Controls.Add(AddCollectButton); panel2.Controls.Add(SetTextBox); panel2.Controls.Add(label2); - panel2.Location = new Point(14, 19); - panel2.Margin = new Padding(3, 2, 3, 2); + panel2.Location = new Point(16, 62); panel2.Name = "panel2"; - panel2.Size = new Size(187, 165); + panel2.Size = new Size(214, 220); panel2.TabIndex = 6; // // DeleteCollectButton // - DeleteCollectButton.Location = new Point(3, 136); - DeleteCollectButton.Margin = new Padding(3, 2, 3, 2); + DeleteCollectButton.Location = new Point(3, 181); DeleteCollectButton.Name = "DeleteCollectButton"; - DeleteCollectButton.Size = new Size(182, 22); + DeleteCollectButton.Size = new Size(208, 29); DeleteCollectButton.TabIndex = 4; DeleteCollectButton.Text = "Удалить набор"; DeleteCollectButton.UseVisualStyleBackColor = true; @@ -88,20 +124,18 @@ // CollectionListBox // CollectionListBox.FormattingEnabled = true; - CollectionListBox.ItemHeight = 15; - CollectionListBox.Location = new Point(3, 68); - CollectionListBox.Margin = new Padding(3, 2, 3, 2); + CollectionListBox.ItemHeight = 20; + CollectionListBox.Location = new Point(3, 91); CollectionListBox.Name = "CollectionListBox"; - CollectionListBox.Size = new Size(182, 64); + CollectionListBox.Size = new Size(207, 84); CollectionListBox.TabIndex = 3; CollectionListBox.SelectedIndexChanged += ListBoxObjects_SelectedIndexChanged; // // AddCollectButton // - AddCollectButton.Location = new Point(3, 42); - AddCollectButton.Margin = new Padding(3, 2, 3, 2); + AddCollectButton.Location = new Point(3, 56); AddCollectButton.Name = "AddCollectButton"; - AddCollectButton.Size = new Size(182, 22); + AddCollectButton.Size = new Size(208, 29); AddCollectButton.TabIndex = 2; AddCollectButton.Text = "Добавить набор"; AddCollectButton.UseVisualStyleBackColor = true; @@ -109,10 +143,9 @@ // // SetTextBox // - SetTextBox.Location = new Point(2, 17); - SetTextBox.Margin = new Padding(3, 2, 3, 2); + SetTextBox.Location = new Point(2, 23); SetTextBox.Name = "SetTextBox"; - SetTextBox.Size = new Size(183, 23); + SetTextBox.Size = new Size(209, 27); SetTextBox.TabIndex = 1; // // label2 @@ -120,16 +153,15 @@ label2.AutoSize = true; label2.Location = new Point(3, 0); label2.Name = "label2"; - label2.Size = new Size(52, 15); + label2.Size = new Size(66, 20); label2.TabIndex = 0; label2.Text = "Наборы"; // // UpdateButton // - UpdateButton.Location = new Point(9, 283); - UpdateButton.Margin = new Padding(3, 2, 3, 2); + UpdateButton.Location = new Point(10, 414); UpdateButton.Name = "UpdateButton"; - UpdateButton.Size = new Size(192, 28); + UpdateButton.Size = new Size(219, 37); UpdateButton.TabIndex = 4; UpdateButton.Text = "Обновить коллекцию"; UpdateButton.UseVisualStyleBackColor = true; @@ -137,10 +169,9 @@ // // DeleteButton // - DeleteButton.Location = new Point(9, 253); - DeleteButton.Margin = new Padding(3, 2, 3, 2); + DeleteButton.Location = new Point(10, 374); DeleteButton.Name = "DeleteButton"; - DeleteButton.Size = new Size(192, 28); + DeleteButton.Size = new Size(219, 37); DeleteButton.TabIndex = 3; DeleteButton.Text = "Удалить технику"; DeleteButton.UseVisualStyleBackColor = true; @@ -148,10 +179,9 @@ // // AddButton // - AddButton.Location = new Point(9, 194); - AddButton.Margin = new Padding(3, 2, 3, 2); + AddButton.Location = new Point(10, 296); AddButton.Name = "AddButton"; - AddButton.Size = new Size(192, 28); + AddButton.Size = new Size(219, 37); AddButton.TabIndex = 2; AddButton.Text = "Добавить технику"; AddButton.UseVisualStyleBackColor = true; @@ -159,18 +189,17 @@ // // InputNum // - InputNum.Location = new Point(9, 225); - InputNum.Margin = new Padding(3, 2, 3, 2); + InputNum.Location = new Point(10, 337); InputNum.Name = "InputNum"; - InputNum.Size = new Size(192, 23); + InputNum.Size = new Size(219, 27); InputNum.TabIndex = 1; // // label1 // label1.AutoSize = true; - label1.Location = new Point(2, 2); + label1.Location = new Point(122, 39); label1.Name = "label1"; - label1.Size = new Size(83, 15); + label1.Size = new Size(103, 20); label1.TabIndex = 0; label1.Text = "Инструменты"; // @@ -178,24 +207,28 @@ // DrawTank.Dock = DockStyle.Fill; DrawTank.Location = new Point(0, 0); - DrawTank.Margin = new Padding(3, 2, 3, 2); DrawTank.Name = "DrawTank"; - DrawTank.Size = new Size(568, 429); + DrawTank.Size = new Size(694, 479); DrawTank.TabIndex = 1; DrawTank.TabStop = false; // - // CollectionsFrame + // openFileDialog // - AutoScaleDimensions = new SizeF(7F, 15F); + openFileDialog.FileName = "openFileDialog1"; + // + // FormTanksCollections + // + AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(775, 429); + ClientSize = new Size(931, 479); Controls.Add(DrawTank); Controls.Add(panel1); - Margin = new Padding(3, 2, 3, 2); - Name = "CollectionsFrame"; + Name = "FormTanksCollections"; Text = "CollectionsFrame"; panel1.ResumeLayout(false); panel1.PerformLayout(); + menuStrip1.ResumeLayout(false); + menuStrip1.PerformLayout(); panel2.ResumeLayout(false); panel2.PerformLayout(); ((System.ComponentModel.ISupportInitialize)DrawTank).EndInit(); @@ -217,5 +250,11 @@ private Button AddCollectButton; private TextBox SetTextBox; private Label label2; + private MenuStrip menuStrip1; + private ToolStripMenuItem StripMenu; + private ToolStripMenuItem SaveItem; + private ToolStripMenuItem LoadItem; + private OpenFileDialog openFileDialog; + private SaveFileDialog saveFileDialog; } } \ No newline at end of file diff --git a/Tank/Tank/FormTanksCollections.cs b/Tank/Tank/FormTanksCollections.cs index f35d91d..95b7bad 100644 --- a/Tank/Tank/FormTanksCollections.cs +++ b/Tank/Tank/FormTanksCollections.cs @@ -160,5 +160,37 @@ namespace Tank } DrawTank.Image = obj.ShowTanks(); } + + private void SaveToolStripMenuItem_Click(object sender, EventArgs e) + { + if (saveFileDialog.ShowDialog() == DialogResult.OK) + { + if (_storage.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 (_storage.LoadData(openFileDialog.FileName)) + { + ReloadObjects(); + MessageBox.Show("Загрузка прошла успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); + } + else + { + MessageBox.Show("Не загрузилось", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } } } diff --git a/Tank/Tank/FormTanksCollections.resx b/Tank/Tank/FormTanksCollections.resx index af32865..797c78c 100644 --- a/Tank/Tank/FormTanksCollections.resx +++ b/Tank/Tank/FormTanksCollections.resx @@ -117,4 +117,13 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + 344, 17 + + + 17, 17 + + + 187, 17 + \ No newline at end of file diff --git a/Tank/Tank/TanksGenericCollection.cs b/Tank/Tank/TanksGenericCollection.cs index 70c5411..dc13839 100644 --- a/Tank/Tank/TanksGenericCollection.cs +++ b/Tank/Tank/TanksGenericCollection.cs @@ -24,6 +24,8 @@ namespace Tank.Generics // Набор объектов private readonly SetGeneric _collection; + public IEnumerable GetTanks => _collection.GetTanks(); + // Конструктор public TanksGenericCollection(int pictureWidth, int pictureHeight) { diff --git a/Tank/Tank/TanksGenericStorage.cs b/Tank/Tank/TanksGenericStorage.cs index f3e4320..3a5d10f 100644 --- a/Tank/Tank/TanksGenericStorage.cs +++ b/Tank/Tank/TanksGenericStorage.cs @@ -19,6 +19,10 @@ namespace Tank private readonly int _pictureWidth; private readonly int _pictureHeight; + private static readonly char _separatorForKeyValue = '|'; + private readonly char _separatorRecords = ';'; + private static readonly char _separatorForObject = ':'; + // Конструктор public TanksGenericStorage(int pictureWidth, int pictureHeight) { @@ -51,5 +55,77 @@ namespace Tank return null; } } + + public bool SaveData(string filename) + { + if (File.Exists(filename)) + { + File.Delete(filename); + } + + StringBuilder data = new(); + foreach (KeyValuePair> record in _tankStorages) + { + StringBuilder records = new(); + foreach (DrawArmoVehicle? elem in record.Value.GetTanks) + { + records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}"); + } + data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}"); + + } + if (data.Length == 0) + { + return false; + } + using (StreamWriter writer = new StreamWriter(filename)) + { + writer.WriteLine("TankStorage"); + writer.Write(data.ToString()); + return true; + } + } + + public bool LoadData(string filename) + { + if (!File.Exists(filename)) + { + return false; + } + using (StreamReader reader = new StreamReader(filename)) + { + string checker = reader.ReadLine(); + if (checker == null) + return false; + if (!checker.StartsWith("TankStorage")) + return false; + _tankStorages.Clear(); + string strs; + bool firstinit = true; + while ((strs = reader.ReadLine()) != null) + { + if (strs == null && firstinit) + return false; + if (strs == null) + break; + firstinit = false; + string name = strs.Split('|')[0]; + TanksGenericCollection collection = new(_pictureWidth, _pictureHeight); + foreach (string data in strs.Split('|')[1].Split(';')) + { + DrawArmoVehicle? vehicle = data?.CreateDrawTank(_separatorForObject, _pictureWidth, _pictureHeight); + if (vehicle != null) + { + if (!(collection + vehicle)) + { + return false; + } + } + } + _tankStorages.Add(name, collection); + } + return true; + } + } } } \ No newline at end of file