This commit is contained in:
nikbel2004@outlook.com 2023-11-29 01:08:18 +04:00
parent 9f84d8ff33
commit 1d003ace98
6 changed files with 248 additions and 41 deletions

View File

@ -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}";
}
}
}

View File

@ -29,6 +29,10 @@
private void InitializeComponent() private void InitializeComponent()
{ {
panel1 = new Panel(); panel1 = new Panel();
menuStrip1 = new MenuStrip();
StripMenu = new ToolStripMenuItem();
SaveItem = new ToolStripMenuItem();
LoadItem = new ToolStripMenuItem();
panel2 = new Panel(); panel2 = new Panel();
DeleteCollectButton = new Button(); DeleteCollectButton = new Button();
CollectionListBox = new ListBox(); CollectionListBox = new ListBox();
@ -41,13 +45,17 @@
InputNum = new TextBox(); InputNum = new TextBox();
label1 = new Label(); label1 = new Label();
DrawTank = new PictureBox(); DrawTank = new PictureBox();
openFileDialog = new OpenFileDialog();
saveFileDialog = new SaveFileDialog();
panel1.SuspendLayout(); panel1.SuspendLayout();
menuStrip1.SuspendLayout();
panel2.SuspendLayout(); panel2.SuspendLayout();
((System.ComponentModel.ISupportInitialize)DrawTank).BeginInit(); ((System.ComponentModel.ISupportInitialize)DrawTank).BeginInit();
SuspendLayout(); SuspendLayout();
// //
// panel1 // panel1
// //
panel1.Controls.Add(menuStrip1);
panel1.Controls.Add(panel2); panel1.Controls.Add(panel2);
panel1.Controls.Add(UpdateButton); panel1.Controls.Add(UpdateButton);
panel1.Controls.Add(DeleteButton); panel1.Controls.Add(DeleteButton);
@ -55,12 +63,42 @@
panel1.Controls.Add(InputNum); panel1.Controls.Add(InputNum);
panel1.Controls.Add(label1); panel1.Controls.Add(label1);
panel1.Dock = DockStyle.Right; panel1.Dock = DockStyle.Right;
panel1.Location = new Point(568, 0); panel1.Location = new Point(694, 0);
panel1.Margin = new Padding(3, 2, 3, 2);
panel1.Name = "panel1"; panel1.Name = "panel1";
panel1.Size = new Size(207, 429); panel1.Size = new Size(237, 479);
panel1.TabIndex = 0; 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
// //
panel2.Controls.Add(DeleteCollectButton); panel2.Controls.Add(DeleteCollectButton);
@ -68,18 +106,16 @@
panel2.Controls.Add(AddCollectButton); panel2.Controls.Add(AddCollectButton);
panel2.Controls.Add(SetTextBox); panel2.Controls.Add(SetTextBox);
panel2.Controls.Add(label2); panel2.Controls.Add(label2);
panel2.Location = new Point(14, 19); panel2.Location = new Point(16, 62);
panel2.Margin = new Padding(3, 2, 3, 2);
panel2.Name = "panel2"; panel2.Name = "panel2";
panel2.Size = new Size(187, 165); panel2.Size = new Size(214, 220);
panel2.TabIndex = 6; panel2.TabIndex = 6;
// //
// DeleteCollectButton // DeleteCollectButton
// //
DeleteCollectButton.Location = new Point(3, 136); DeleteCollectButton.Location = new Point(3, 181);
DeleteCollectButton.Margin = new Padding(3, 2, 3, 2);
DeleteCollectButton.Name = "DeleteCollectButton"; DeleteCollectButton.Name = "DeleteCollectButton";
DeleteCollectButton.Size = new Size(182, 22); DeleteCollectButton.Size = new Size(208, 29);
DeleteCollectButton.TabIndex = 4; DeleteCollectButton.TabIndex = 4;
DeleteCollectButton.Text = "Удалить набор"; DeleteCollectButton.Text = "Удалить набор";
DeleteCollectButton.UseVisualStyleBackColor = true; DeleteCollectButton.UseVisualStyleBackColor = true;
@ -88,20 +124,18 @@
// CollectionListBox // CollectionListBox
// //
CollectionListBox.FormattingEnabled = true; CollectionListBox.FormattingEnabled = true;
CollectionListBox.ItemHeight = 15; CollectionListBox.ItemHeight = 20;
CollectionListBox.Location = new Point(3, 68); CollectionListBox.Location = new Point(3, 91);
CollectionListBox.Margin = new Padding(3, 2, 3, 2);
CollectionListBox.Name = "CollectionListBox"; CollectionListBox.Name = "CollectionListBox";
CollectionListBox.Size = new Size(182, 64); CollectionListBox.Size = new Size(207, 84);
CollectionListBox.TabIndex = 3; CollectionListBox.TabIndex = 3;
CollectionListBox.SelectedIndexChanged += ListBoxObjects_SelectedIndexChanged; CollectionListBox.SelectedIndexChanged += ListBoxObjects_SelectedIndexChanged;
// //
// AddCollectButton // AddCollectButton
// //
AddCollectButton.Location = new Point(3, 42); AddCollectButton.Location = new Point(3, 56);
AddCollectButton.Margin = new Padding(3, 2, 3, 2);
AddCollectButton.Name = "AddCollectButton"; AddCollectButton.Name = "AddCollectButton";
AddCollectButton.Size = new Size(182, 22); AddCollectButton.Size = new Size(208, 29);
AddCollectButton.TabIndex = 2; AddCollectButton.TabIndex = 2;
AddCollectButton.Text = "Добавить набор"; AddCollectButton.Text = "Добавить набор";
AddCollectButton.UseVisualStyleBackColor = true; AddCollectButton.UseVisualStyleBackColor = true;
@ -109,10 +143,9 @@
// //
// SetTextBox // SetTextBox
// //
SetTextBox.Location = new Point(2, 17); SetTextBox.Location = new Point(2, 23);
SetTextBox.Margin = new Padding(3, 2, 3, 2);
SetTextBox.Name = "SetTextBox"; SetTextBox.Name = "SetTextBox";
SetTextBox.Size = new Size(183, 23); SetTextBox.Size = new Size(209, 27);
SetTextBox.TabIndex = 1; SetTextBox.TabIndex = 1;
// //
// label2 // label2
@ -120,16 +153,15 @@
label2.AutoSize = true; label2.AutoSize = true;
label2.Location = new Point(3, 0); label2.Location = new Point(3, 0);
label2.Name = "label2"; label2.Name = "label2";
label2.Size = new Size(52, 15); label2.Size = new Size(66, 20);
label2.TabIndex = 0; label2.TabIndex = 0;
label2.Text = "Наборы"; label2.Text = "Наборы";
// //
// UpdateButton // UpdateButton
// //
UpdateButton.Location = new Point(9, 283); UpdateButton.Location = new Point(10, 414);
UpdateButton.Margin = new Padding(3, 2, 3, 2);
UpdateButton.Name = "UpdateButton"; UpdateButton.Name = "UpdateButton";
UpdateButton.Size = new Size(192, 28); UpdateButton.Size = new Size(219, 37);
UpdateButton.TabIndex = 4; UpdateButton.TabIndex = 4;
UpdateButton.Text = "Обновить коллекцию"; UpdateButton.Text = "Обновить коллекцию";
UpdateButton.UseVisualStyleBackColor = true; UpdateButton.UseVisualStyleBackColor = true;
@ -137,10 +169,9 @@
// //
// DeleteButton // DeleteButton
// //
DeleteButton.Location = new Point(9, 253); DeleteButton.Location = new Point(10, 374);
DeleteButton.Margin = new Padding(3, 2, 3, 2);
DeleteButton.Name = "DeleteButton"; DeleteButton.Name = "DeleteButton";
DeleteButton.Size = new Size(192, 28); DeleteButton.Size = new Size(219, 37);
DeleteButton.TabIndex = 3; DeleteButton.TabIndex = 3;
DeleteButton.Text = "Удалить технику"; DeleteButton.Text = "Удалить технику";
DeleteButton.UseVisualStyleBackColor = true; DeleteButton.UseVisualStyleBackColor = true;
@ -148,10 +179,9 @@
// //
// AddButton // AddButton
// //
AddButton.Location = new Point(9, 194); AddButton.Location = new Point(10, 296);
AddButton.Margin = new Padding(3, 2, 3, 2);
AddButton.Name = "AddButton"; AddButton.Name = "AddButton";
AddButton.Size = new Size(192, 28); AddButton.Size = new Size(219, 37);
AddButton.TabIndex = 2; AddButton.TabIndex = 2;
AddButton.Text = "Добавить технику"; AddButton.Text = "Добавить технику";
AddButton.UseVisualStyleBackColor = true; AddButton.UseVisualStyleBackColor = true;
@ -159,18 +189,17 @@
// //
// InputNum // InputNum
// //
InputNum.Location = new Point(9, 225); InputNum.Location = new Point(10, 337);
InputNum.Margin = new Padding(3, 2, 3, 2);
InputNum.Name = "InputNum"; InputNum.Name = "InputNum";
InputNum.Size = new Size(192, 23); InputNum.Size = new Size(219, 27);
InputNum.TabIndex = 1; InputNum.TabIndex = 1;
// //
// label1 // label1
// //
label1.AutoSize = true; label1.AutoSize = true;
label1.Location = new Point(2, 2); label1.Location = new Point(122, 39);
label1.Name = "label1"; label1.Name = "label1";
label1.Size = new Size(83, 15); label1.Size = new Size(103, 20);
label1.TabIndex = 0; label1.TabIndex = 0;
label1.Text = "Инструменты"; label1.Text = "Инструменты";
// //
@ -178,24 +207,28 @@
// //
DrawTank.Dock = DockStyle.Fill; DrawTank.Dock = DockStyle.Fill;
DrawTank.Location = new Point(0, 0); DrawTank.Location = new Point(0, 0);
DrawTank.Margin = new Padding(3, 2, 3, 2);
DrawTank.Name = "DrawTank"; DrawTank.Name = "DrawTank";
DrawTank.Size = new Size(568, 429); DrawTank.Size = new Size(694, 479);
DrawTank.TabIndex = 1; DrawTank.TabIndex = 1;
DrawTank.TabStop = false; DrawTank.TabStop = false;
// //
// CollectionsFrame // openFileDialog
// //
AutoScaleDimensions = new SizeF(7F, 15F); openFileDialog.FileName = "openFileDialog1";
//
// FormTanksCollections
//
AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font; AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(775, 429); ClientSize = new Size(931, 479);
Controls.Add(DrawTank); Controls.Add(DrawTank);
Controls.Add(panel1); Controls.Add(panel1);
Margin = new Padding(3, 2, 3, 2); Name = "FormTanksCollections";
Name = "CollectionsFrame";
Text = "CollectionsFrame"; Text = "CollectionsFrame";
panel1.ResumeLayout(false); panel1.ResumeLayout(false);
panel1.PerformLayout(); panel1.PerformLayout();
menuStrip1.ResumeLayout(false);
menuStrip1.PerformLayout();
panel2.ResumeLayout(false); panel2.ResumeLayout(false);
panel2.PerformLayout(); panel2.PerformLayout();
((System.ComponentModel.ISupportInitialize)DrawTank).EndInit(); ((System.ComponentModel.ISupportInitialize)DrawTank).EndInit();
@ -217,5 +250,11 @@
private Button AddCollectButton; private Button AddCollectButton;
private TextBox SetTextBox; private TextBox SetTextBox;
private Label label2; private Label label2;
private MenuStrip menuStrip1;
private ToolStripMenuItem StripMenu;
private ToolStripMenuItem SaveItem;
private ToolStripMenuItem LoadItem;
private OpenFileDialog openFileDialog;
private SaveFileDialog saveFileDialog;
} }
} }

View File

@ -160,5 +160,37 @@ namespace Tank
} }
DrawTank.Image = obj.ShowTanks(); 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);
}
}
}
} }
} }

View File

@ -117,4 +117,13 @@
<resheader name="writer"> <resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader> </resheader>
<metadata name="menuStrip1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>344, 17</value>
</metadata>
<metadata name="openFileDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
<metadata name="saveFileDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>187, 17</value>
</metadata>
</root> </root>

View File

@ -24,6 +24,8 @@ namespace Tank.Generics
// Набор объектов // Набор объектов
private readonly SetGeneric<T> _collection; private readonly SetGeneric<T> _collection;
public IEnumerable<T?> GetTanks => _collection.GetTanks();
// Конструктор // Конструктор
public TanksGenericCollection(int pictureWidth, int pictureHeight) public TanksGenericCollection(int pictureWidth, int pictureHeight)
{ {

View File

@ -19,6 +19,10 @@ namespace Tank
private readonly int _pictureWidth; private readonly int _pictureWidth;
private readonly int _pictureHeight; private readonly int _pictureHeight;
private static readonly char _separatorForKeyValue = '|';
private readonly char _separatorRecords = ';';
private static readonly char _separatorForObject = ':';
// Конструктор // Конструктор
public TanksGenericStorage(int pictureWidth, int pictureHeight) public TanksGenericStorage(int pictureWidth, int pictureHeight)
{ {
@ -51,5 +55,77 @@ namespace Tank
return null; return null;
} }
} }
public bool SaveData(string filename)
{
if (File.Exists(filename))
{
File.Delete(filename);
}
StringBuilder data = new();
foreach (KeyValuePair<string, TanksGenericCollection<DrawArmoVehicle, DrawingObjectTank>> 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<DrawArmoVehicle, DrawingObjectTank> 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;
}
}
} }
} }