PIbd-22. Shabunov O.A. Lab work 06 #6

Closed
olshab wants to merge 5 commits from Lab6 into Lab5
6 changed files with 221 additions and 5 deletions

View File

@ -39,17 +39,24 @@
RemoveBomberButton = new Button();
NumberMaskedTextBox = new MaskedTextBox();
AddBomberButton = new Button();
FormMenuStrip = new MenuStrip();
FileMenuStripItem = new ToolStripMenuItem();
SaveToolStripMenuItem = new ToolStripMenuItem();
LoadToolStripMenuItem = new ToolStripMenuItem();
OpenFileDialog = new OpenFileDialog();
SaveFileDialog = new SaveFileDialog();
((System.ComponentModel.ISupportInitialize)CollectionPictureBox).BeginInit();
ToolGroupBox.SuspendLayout();
SetsGroupBox.SuspendLayout();
FormMenuStrip.SuspendLayout();
SuspendLayout();
//
// CollectionPictureBox
//
CollectionPictureBox.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
CollectionPictureBox.Location = new Point(0, 0);
CollectionPictureBox.Location = new Point(0, 27);
CollectionPictureBox.Name = "CollectionPictureBox";
CollectionPictureBox.Size = new Size(812, 603);
CollectionPictureBox.Size = new Size(812, 576);
CollectionPictureBox.TabIndex = 0;
CollectionPictureBox.TabStop = false;
//
@ -61,9 +68,9 @@
ToolGroupBox.Controls.Add(RemoveBomberButton);
ToolGroupBox.Controls.Add(NumberMaskedTextBox);
ToolGroupBox.Controls.Add(AddBomberButton);
ToolGroupBox.Location = new Point(818, 12);
ToolGroupBox.Location = new Point(818, 27);
ToolGroupBox.Name = "ToolGroupBox";
ToolGroupBox.Size = new Size(176, 579);
ToolGroupBox.Size = new Size(176, 564);
ToolGroupBox.TabIndex = 1;
ToolGroupBox.TabStop = false;
ToolGroupBox.Text = "Инструменты";
@ -121,7 +128,7 @@
//
// RefreshCollectionButton
//
RefreshCollectionButton.Location = new Point(6, 543);
RefreshCollectionButton.Location = new Point(6, 528);
RefreshCollectionButton.Name = "RefreshCollectionButton";
RefreshCollectionButton.Size = new Size(164, 30);
RefreshCollectionButton.TabIndex = 3;
@ -158,6 +165,45 @@
AddBomberButton.UseVisualStyleBackColor = true;
AddBomberButton.Click += ButtonAddEntity_Click;
//
// FormMenuStrip
//
FormMenuStrip.Items.AddRange(new ToolStripItem[] { FileMenuStripItem });
FormMenuStrip.Location = new Point(0, 0);
FormMenuStrip.Name = "FormMenuStrip";
FormMenuStrip.Size = new Size(1006, 24);
FormMenuStrip.TabIndex = 2;
FormMenuStrip.Text = "menuStrip1";
//
// FileMenuStripItem
//
FileMenuStripItem.DropDownItems.AddRange(new ToolStripItem[] { SaveToolStripMenuItem, LoadToolStripMenuItem });
FileMenuStripItem.Name = "FileMenuStripItem";
FileMenuStripItem.Size = new Size(48, 20);
FileMenuStripItem.Text = "Файл";
//
// SaveToolStripMenuItem
//
SaveToolStripMenuItem.Name = "SaveToolStripMenuItem";
SaveToolStripMenuItem.Size = new Size(141, 22);
SaveToolStripMenuItem.Text = "Сохранение";
SaveToolStripMenuItem.Click += SaveToolStripMenuItem_Click;
//
// LoadToolStripMenuItem
//
LoadToolStripMenuItem.Name = "LoadToolStripMenuItem";
LoadToolStripMenuItem.Size = new Size(141, 22);
LoadToolStripMenuItem.Text = "Загрузка";
LoadToolStripMenuItem.Click += LoadToolStripMenuItem_Click;
//
// OpenFileDialog
//
OpenFileDialog.FileName = "openFileDialog1";
OpenFileDialog.Filter = "txt file | *.txt";
//
// SaveFileDialog
//
SaveFileDialog.Filter = "txt file | *.txt";
//
// FormEntityCollection
//
AutoScaleDimensions = new SizeF(7F, 15F);
@ -165,6 +211,7 @@
ClientSize = new Size(1006, 603);
Controls.Add(ToolGroupBox);
Controls.Add(CollectionPictureBox);
Controls.Add(FormMenuStrip);
Name = "FormEntityCollection";
Text = "Набор бомбардировщиков";
((System.ComponentModel.ISupportInitialize)CollectionPictureBox).EndInit();
@ -172,7 +219,10 @@
ToolGroupBox.PerformLayout();
SetsGroupBox.ResumeLayout(false);
SetsGroupBox.PerformLayout();
FormMenuStrip.ResumeLayout(false);
FormMenuStrip.PerformLayout();
ResumeLayout(false);
PerformLayout();
}
#endregion
@ -188,5 +238,11 @@
private ListBox StorageListBox;
private Button AddSetButton;
private Button RemoveSetButton;
private MenuStrip FormMenuStrip;
private ToolStripMenuItem FileMenuStripItem;
private ToolStripMenuItem SaveToolStripMenuItem;
private ToolStripMenuItem LoadToolStripMenuItem;
private OpenFileDialog OpenFileDialog;
private SaveFileDialog SaveFileDialog;
}
}

View File

@ -127,5 +127,29 @@ namespace AirBomber
{
CollectionPictureBox.Image = _storage[StorageListBox.SelectedItem?.ToString() ?? string.Empty]?.ShowEntities();
}
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))
MessageBox.Show("Загрузка прошла успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
else
MessageBox.Show("Не загрузилось", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
ReloadObjects();
}
}
}

View File

@ -117,4 +117,13 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<metadata name="FormMenuStrip.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
<metadata name="OpenFileDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>153, 17</value>
</metadata>
<metadata name="SaveFileDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>288, 17</value>
</metadata>
</root>

View File

@ -15,6 +15,8 @@ namespace AirBomber.Generics
private readonly SetGeneric<T> _collection;
public IEnumerable<T?> Entities => _collection.GetEntities();
public EntitiesGenericCollection(int PictureWidth, int PictureHeight)
{
int width = PictureWidth / _placeSizeWidth;

View File

@ -1,5 +1,6 @@
using AirBomber.MovementStrategy;
using AirBomber.Rendering;
using System.Text;
namespace AirBomber.Generics
{
@ -12,6 +13,10 @@ namespace AirBomber.Generics
private readonly int _pictureWidth;
private readonly int _pictureHeight;
private readonly char _keyValueDelimiter = '|';
private readonly char _recordsDelimiter = ';';
private readonly char _entityDelimiter = ':';
public EntitiesGenericStorage(int PictureWidth, int PictureHeight)
{
_entityStorages = new Dictionary<string, EntitiesGenericCollection<BomberRendererBase, ObjectEntityRenderer>>();
@ -49,5 +54,69 @@ namespace AirBomber.Generics
return _entityStorages[Index];
}
}
public bool SaveData(string FileName)
{
if (_entityStorages.Count == 0)
return false;
using (StreamWriter writer = new StreamWriter(FileName, false))
{
writer.WriteLine("BomberStorage");
foreach (KeyValuePair<string, EntitiesGenericCollection<BomberRendererBase, ObjectEntityRenderer>> Record in _entityStorages)
{
StringBuilder Records = new StringBuilder();
foreach (BomberRendererBase? Element in Record.Value.Entities)
Records.Append($"{Element?.SerializeRenderer(_entityDelimiter)}{_recordsDelimiter}");
writer.WriteLine($"{Record.Key}{_keyValueDelimiter}{Records}");
}
}
return true;
}
public bool LoadData(string FileName)
{
if (!File.Exists(FileName))
return false;
using (StreamReader reader = new StreamReader(FileName))
{
if (reader.ReadLine() != "BomberStorage")
return false;
_entityStorages.Clear();
string? Data;
while ((Data = reader.ReadLine()) != null)
{
string[] Record = Data.Split(_keyValueDelimiter, StringSplitOptions.RemoveEmptyEntries);
if (Record.Length != 2)
continue;
EntitiesGenericCollection<BomberRendererBase, ObjectEntityRenderer> Collection = new(_pictureWidth, _pictureHeight);
string[] Set = Record[1].Split(_recordsDelimiter, StringSplitOptions.RemoveEmptyEntries);
foreach (string Element in Set)
{
BomberRendererBase? Renderer = Element?.DeserializeRenderer(_entityDelimiter, _pictureWidth, _pictureHeight);
if (Renderer != null)
{
if ((Collection + Renderer) == -1)
return false;
}
}
_entityStorages.Add(Record[0], Collection);
}
}
return true;
}
}
}

View File

@ -0,0 +1,56 @@
using AirBomber.Entities;
namespace AirBomber.Rendering
{
public static class ExtensionBomberRenderer
{
public static BomberRendererBase? DeserializeRenderer(this string Info, char Delimiter, int Width, int Height)
{
string[] Properties = Info.Split(Delimiter);
if (Properties.Length == 3)
{
return new BomberRendererBase(
Convert.ToInt32(Properties[0]),
Convert.ToInt32(Properties[1]),
Color.FromName(Properties[2]),
Width,
Height
);
}
if (Properties.Length == 6)
{
return new BomberRenderer(
Convert.ToInt32(Properties[0]),
Convert.ToInt32(Properties[1]),
Color.FromName(Properties[2]),
Color.FromName(Properties[3]),
Convert.ToBoolean(Properties[4]),
Convert.ToBoolean(Properties[5]),
Width,
Height
);
}
return null;
}
public static string SerializeRenderer(this BomberRendererBase Renderer, char Delimiter)
{
var Bomber = Renderer.EntityBomber;
if (Bomber is null)
return string.Empty;
string ResultString = string.Empty;
ResultString += $"{Bomber.Speed}{Delimiter}{Bomber.Weight}{Delimiter}{Bomber.BodyColor.Name}";
if (Bomber is BomberEntity AdvancedBomber)
ResultString += $"{Delimiter}{AdvancedBomber.AdditionalColor.Name}" +
$"{Delimiter}{AdvancedBomber.Bombs}{Delimiter}{AdvancedBomber.FuelTanks}";
return ResultString;
}
}
}