PIbd-21 Potapov N.S. LabWork06 #6
@ -0,0 +1,53 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ProjectStormtrooper
|
||||
{
|
||||
public static class ExtensionDrawingPlane
|
||||
{
|
||||
/// <summary>
|
||||
/// Создание объекта из строки
|
||||
/// </summary>
|
||||
public static DrawingPlane? CreateDrawingPlane(this string info, char separatorForObject, int width, int height)
|
||||
{
|
||||
string[] strs = info.Split(separatorForObject);
|
||||
if (strs.Length == 3)
|
||||
{
|
||||
return new DrawingPlane(Convert.ToInt32(strs[0]), Convert.ToInt32(strs[1]), Color.FromName(strs[2]), width, height);
|
||||
}
|
||||
if (strs.Length == 6)
|
||||
{
|
||||
return new DrawingStormtrooper(
|
||||
Convert.ToInt32(strs[0]),
|
||||
Convert.ToInt32(strs[1]),
|
||||
Color.FromName(strs[2]),
|
||||
Color.FromName(strs[3]),
|
||||
Convert.ToBoolean(strs[4]),
|
||||
Convert.ToBoolean(strs[5]),
|
||||
width,
|
||||
height);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
/// <summary>
|
||||
/// Получение данных для сохранения в файл
|
||||
/// </summary>
|
||||
public static string GetDataForSave(this DrawingPlane drawningPlane, char separatorForObject)
|
||||
{
|
||||
var plane = drawningPlane.EntityPlane;
|
||||
if (plane == null)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
var str = $"{plane.Speed}{separatorForObject}{plane.Weight}{separatorForObject}{plane.BodyColor.Name}";
|
||||
if (plane is not EntityStormtrooper stormtrooper)
|
||||
{
|
||||
return str;
|
||||
}
|
||||
return $"{str}{separatorForObject}{stormtrooper.AdditionalColor.Name}{separatorForObject}{stormtrooper.Rockets}{separatorForObject}{stormtrooper.Bombs}";
|
||||
}
|
||||
}
|
||||
}
|
@ -39,9 +39,16 @@
|
||||
buttonRemovePlane = new Button();
|
||||
buttonAddPlane = new Button();
|
||||
pictureBoxCollection = new PictureBox();
|
||||
menuStrip = new MenuStrip();
|
||||
файлToolStripMenuItem = new ToolStripMenuItem();
|
||||
сохранитьToolStripMenuItem = new ToolStripMenuItem();
|
||||
загрузитьToolStripMenuItem = new ToolStripMenuItem();
|
||||
openFileDialog = new OpenFileDialog();
|
||||
saveFileDialog = new SaveFileDialog();
|
||||
groupBoxTools.SuspendLayout();
|
||||
groupBoxStorages.SuspendLayout();
|
||||
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit();
|
||||
menuStrip.SuspendLayout();
|
||||
SuspendLayout();
|
||||
//
|
||||
// groupBoxTools
|
||||
@ -52,9 +59,9 @@
|
||||
groupBoxTools.Controls.Add(buttonRemovePlane);
|
||||
groupBoxTools.Controls.Add(buttonAddPlane);
|
||||
groupBoxTools.Dock = DockStyle.Right;
|
||||
groupBoxTools.Location = new Point(787, 0);
|
||||
groupBoxTools.Location = new Point(787, 28);
|
||||
groupBoxTools.Name = "groupBoxTools";
|
||||
groupBoxTools.Size = new Size(230, 538);
|
||||
groupBoxTools.Size = new Size(230, 510);
|
||||
groupBoxTools.TabIndex = 0;
|
||||
groupBoxTools.TabStop = false;
|
||||
groupBoxTools.Text = "Инструменты";
|
||||
@ -152,12 +159,52 @@
|
||||
// pictureBoxCollection
|
||||
//
|
||||
pictureBoxCollection.Dock = DockStyle.Fill;
|
||||
pictureBoxCollection.Location = new Point(0, 0);
|
||||
pictureBoxCollection.Location = new Point(0, 28);
|
||||
pictureBoxCollection.Name = "pictureBoxCollection";
|
||||
pictureBoxCollection.Size = new Size(787, 538);
|
||||
pictureBoxCollection.Size = new Size(787, 510);
|
||||
pictureBoxCollection.TabIndex = 1;
|
||||
pictureBoxCollection.TabStop = false;
|
||||
//
|
||||
// menuStrip
|
||||
//
|
||||
menuStrip.ImageScalingSize = new Size(20, 20);
|
||||
menuStrip.Items.AddRange(new ToolStripItem[] { файлToolStripMenuItem });
|
||||
menuStrip.Location = new Point(0, 0);
|
||||
menuStrip.Name = "menuStrip";
|
||||
menuStrip.Size = new Size(1017, 28);
|
||||
menuStrip.TabIndex = 2;
|
||||
menuStrip.Text = "menuStrip1";
|
||||
//
|
||||
// файлToolStripMenuItem
|
||||
//
|
||||
файлToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { сохранитьToolStripMenuItem, загрузитьToolStripMenuItem });
|
||||
файлToolStripMenuItem.Name = "файлToolStripMenuItem";
|
||||
файлToolStripMenuItem.Size = new Size(59, 24);
|
||||
файлToolStripMenuItem.Text = "Файл";
|
||||
//
|
||||
// сохранитьToolStripMenuItem
|
||||
//
|
||||
сохранитьToolStripMenuItem.Name = "сохранитьToolStripMenuItem";
|
||||
сохранитьToolStripMenuItem.Size = new Size(166, 26);
|
||||
сохранитьToolStripMenuItem.Text = "Сохранить";
|
||||
сохранитьToolStripMenuItem.Click += saveToFileToolStripMenuItem_Click;
|
||||
//
|
||||
// загрузитьToolStripMenuItem
|
||||
//
|
||||
загрузитьToolStripMenuItem.Name = "загрузитьToolStripMenuItem";
|
||||
загрузитьToolStripMenuItem.Size = new Size(166, 26);
|
||||
загрузитьToolStripMenuItem.Text = "Загрузить";
|
||||
загрузитьToolStripMenuItem.Click += loadFromFileToolStripMenuItem_Click;
|
||||
//
|
||||
// openFileDialog
|
||||
//
|
||||
openFileDialog.FileName = "openFileDialog1";
|
||||
openFileDialog.Filter = "txt file | *.txt";
|
||||
//
|
||||
// saveFileDialog
|
||||
//
|
||||
saveFileDialog.Filter = "txt file | *.txt";
|
||||
//
|
||||
// FormPlaneCollection
|
||||
//
|
||||
AutoScaleDimensions = new SizeF(8F, 20F);
|
||||
@ -165,6 +212,8 @@
|
||||
ClientSize = new Size(1017, 538);
|
||||
Controls.Add(pictureBoxCollection);
|
||||
Controls.Add(groupBoxTools);
|
||||
Controls.Add(menuStrip);
|
||||
MainMenuStrip = menuStrip;
|
||||
Name = "FormPlaneCollection";
|
||||
Text = "Набор самолетов";
|
||||
groupBoxTools.ResumeLayout(false);
|
||||
@ -172,7 +221,10 @@
|
||||
groupBoxStorages.ResumeLayout(false);
|
||||
groupBoxStorages.PerformLayout();
|
||||
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit();
|
||||
menuStrip.ResumeLayout(false);
|
||||
menuStrip.PerformLayout();
|
||||
ResumeLayout(false);
|
||||
PerformLayout();
|
||||
}
|
||||
|
||||
#endregion
|
||||
@ -188,5 +240,11 @@
|
||||
private ListBox listBoxStorages;
|
||||
private Button buttonAddStorage;
|
||||
private TextBox textBoxStorageName;
|
||||
private MenuStrip menuStrip;
|
||||
private ToolStripMenuItem файлToolStripMenuItem;
|
||||
private ToolStripMenuItem сохранитьToolStripMenuItem;
|
||||
private ToolStripMenuItem загрузитьToolStripMenuItem;
|
||||
private OpenFileDialog openFileDialog;
|
||||
private SaveFileDialog saveFileDialog;
|
||||
}
|
||||
}
|
@ -159,6 +159,10 @@ namespace ProjectStormtrooper
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private void buttonRefreshCollection_Click(object sender, EventArgs e)
|
||||
{
|
||||
RefreshCollection();
|
||||
}
|
||||
private void RefreshCollection()
|
||||
{
|
||||
if (listBoxStorages.SelectedIndex == -1)
|
||||
{
|
||||
@ -171,5 +175,37 @@ namespace ProjectStormtrooper
|
||||
}
|
||||
pictureBoxCollection.Image = obj.ShowPlanes();
|
||||
}
|
||||
|
||||
private void saveToFileToolStripMenuItem_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 loadFromFileToolStripMenuItem_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (openFileDialog.ShowDialog() == DialogResult.OK)
|
||||
{
|
||||
if (_storage.LoadData(openFileDialog.FileName))
|
||||
{
|
||||
ReloadObjects();
|
||||
RefreshCollection();
|
||||
MessageBox.Show("Загрузка прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||||
}
|
||||
else
|
||||
{
|
||||
MessageBox.Show("Не загрузилось", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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="menuStrip.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>152, 17</value>
|
||||
</metadata>
|
||||
<metadata name="saveFileDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
|
||||
<value>319, 17</value>
|
||||
</metadata>
|
||||
</root>
|
@ -97,7 +97,7 @@
|
||||
//
|
||||
// panelColorPink
|
||||
//
|
||||
panelColorPink.BackColor = Color.FromArgb(255, 192, 192);
|
||||
panelColorPink.BackColor = Color.PeachPuff;
|
||||
panelColorPink.Location = new Point(241, 89);
|
||||
panelColorPink.Name = "panelColorPink";
|
||||
panelColorPink.Size = new Size(50, 50);
|
||||
@ -121,7 +121,7 @@
|
||||
//
|
||||
// panelColorBrown
|
||||
//
|
||||
panelColorBrown.BackColor = Color.FromArgb(192, 64, 0);
|
||||
panelColorBrown.BackColor = Color.DarkRed;
|
||||
panelColorBrown.Location = new Point(17, 89);
|
||||
panelColorBrown.Name = "panelColorBrown";
|
||||
panelColorBrown.Size = new Size(50, 50);
|
||||
|
@ -138,5 +138,9 @@ namespace ProjectStormtrooper
|
||||
i++;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Получение объектов коллекции
|
||||
/// </summary>
|
||||
public IEnumerable<T?> GetPlanes => _collection.GetPlanes();
|
||||
}
|
||||
}
|
||||
|
@ -8,6 +8,18 @@ namespace ProjectStormtrooper
|
||||
{
|
||||
internal class PlanesGenericStorage
|
||||
{
|
||||
/// <summary>
|
||||
/// Разделитель для записи ключа и значения элемента словаря
|
||||
/// </summary>
|
||||
private static readonly char _separatorForKeyValue = '|';
|
||||
/// <summary>
|
||||
/// Разделитель для записей коллекции данных в файл
|
||||
/// </summary>
|
||||
private readonly char _separatorRecords = ';';
|
||||
/// <summary>
|
||||
/// Разделитель для записи информации по объекту в файл
|
||||
/// </summary>
|
||||
private static readonly char _separatorForObject = ':';
|
||||
/// <summary>
|
||||
/// Словарь (хранилище)
|
||||
/// </summary>
|
||||
@ -66,5 +78,79 @@ namespace ProjectStormtrooper
|
||||
return null;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Сохранение информации в файл
|
||||
/// </summary>
|
||||
/// <param name="filename">Путь и имя файла</param>
|
||||
/// <returns>true - сохранение прошло успешно, false - ошибка при сохранении данных</returns>
|
||||
public bool SaveData(string filename)
|
||||
{
|
||||
if (File.Exists(filename))
|
||||
{
|
||||
File.Delete(filename);
|
||||
}
|
||||
using (StreamWriter sw = File.CreateText(filename))
|
||||
{
|
||||
sw.WriteLine($"PlaneStorage");
|
||||
string storageString;
|
||||
foreach (var storage in _planeStorages)
|
||||
{
|
||||
storageString = "";
|
||||
foreach (var plane in storage.Value.GetPlanes)
|
||||
{
|
||||
storageString += $"{plane?.GetDataForSave(_separatorForObject)}{_separatorRecords}";
|
||||
}
|
||||
sw.WriteLine($"{storage.Key}{_separatorForKeyValue}{storageString}");
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
/// <summary>
|
||||
/// Загрузка информации по автомобилям в хранилище из файла
|
||||
/// </summary>
|
||||
/// <param name="filename">Путь и имя файла</param>
|
||||
/// <returns>true - загрузка прошла успешно, false - ошибка при загрузке данных</returns>
|
||||
public bool LoadData(string filename)
|
||||
{
|
||||
if (!File.Exists(filename))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
using (StreamReader sr = new StreamReader(filename))
|
||||
{
|
||||
string? currentLine = sr.ReadLine();
|
||||
if (currentLine == null || !currentLine.Contains("PlaneStorage"))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
_planeStorages.Clear();
|
||||
currentLine = sr.ReadLine();
|
||||
while (currentLine != null)
|
||||
{
|
||||
string[] record = currentLine.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries);
|
||||
if (record.Length != 2)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
PlanesGenericCollection<DrawingPlane, DrawingObjectPlane> collection = new(_pictureWidth, _pictureHeight);
|
||||
string[] set = record[1].Split(_separatorRecords, StringSplitOptions.RemoveEmptyEntries);
|
||||
foreach (string elem in set)
|
||||
{
|
||||
DrawingPlane? plane = elem?.CreateDrawingPlane(_separatorForObject, _pictureWidth, _pictureHeight);
|
||||
if (plane != null)
|
||||
{
|
||||
if (collection + plane == -1)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
_planeStorages.Add(record[0], collection);
|
||||
currentLine = sr.ReadLine();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user