This commit is contained in:
Sharosh322 2023-12-23 10:57:14 +04:00
parent 6f933314ab
commit 484d9e6cba
9 changed files with 329 additions and 69 deletions

View File

@ -0,0 +1,53 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjStormtrooper
{
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}";
}
}
}

View File

@ -31,17 +31,24 @@
groupBoxTools = new GroupBox();
groupBoxStorages = new GroupBox();
buttonRemoveStorage = new Button();
listBoxStorages = new ListBox();
buttonAddStorage = new Button();
textBoxStorageName = new TextBox();
listBoxStorages = new ListBox();
maskedTextBoxNumber = new MaskedTextBox();
buttonRefreshCollection = new Button();
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,11 +59,11 @@
groupBoxTools.Controls.Add(buttonRemovePlane);
groupBoxTools.Controls.Add(buttonAddPlane);
groupBoxTools.Dock = DockStyle.Right;
groupBoxTools.Location = new Point(689, 0);
groupBoxTools.Location = new Point(689, 24);
groupBoxTools.Margin = new Padding(3, 2, 3, 2);
groupBoxTools.Name = "groupBoxTools";
groupBoxTools.Padding = new Padding(3, 2, 3, 2);
groupBoxTools.Size = new Size(201, 404);
groupBoxTools.Size = new Size(201, 380);
groupBoxTools.TabIndex = 0;
groupBoxTools.TabStop = false;
groupBoxTools.Text = "Инструменты";
@ -64,57 +71,62 @@
// groupBoxStorages
//
groupBoxStorages.Controls.Add(buttonRemoveStorage);
groupBoxStorages.Controls.Add(listBoxStorages);
groupBoxStorages.Controls.Add(buttonAddStorage);
groupBoxStorages.Controls.Add(textBoxStorageName);
groupBoxStorages.Controls.Add(listBoxStorages);
groupBoxStorages.Location = new Point(6, 21);
groupBoxStorages.Location = new Point(5, 20);
groupBoxStorages.Margin = new Padding(3, 2, 3, 2);
groupBoxStorages.Name = "groupBoxStorages";
groupBoxStorages.Size = new Size(189, 204);
groupBoxStorages.TabIndex = 6;
groupBoxStorages.Padding = new Padding(3, 2, 3, 2);
groupBoxStorages.Size = new Size(191, 181);
groupBoxStorages.TabIndex = 5;
groupBoxStorages.TabStop = false;
groupBoxStorages.Text = "Наборы";
//
// buttonRemoveStorage
//
buttonRemoveStorage.Location = new Point(6, 175);
buttonRemoveStorage.Location = new Point(5, 153);
buttonRemoveStorage.Margin = new Padding(3, 2, 3, 2);
buttonRemoveStorage.Name = "buttonRemoveStorage";
buttonRemoveStorage.Size = new Size(177, 23);
buttonRemoveStorage.TabIndex = 8;
buttonRemoveStorage.Size = new Size(180, 22);
buttonRemoveStorage.TabIndex = 3;
buttonRemoveStorage.Text = "Удалить набор";
buttonRemoveStorage.UseVisualStyleBackColor = true;
buttonRemoveStorage.Click += buttonRemoveStorage_Click;
buttonRemoveStorage.Click += buttonDelStorage_Click;
//
// listBoxStorages
//
listBoxStorages.FormattingEnabled = true;
listBoxStorages.ItemHeight = 15;
listBoxStorages.Location = new Point(5, 70);
listBoxStorages.Margin = new Padding(3, 2, 3, 2);
listBoxStorages.Name = "listBoxStorages";
listBoxStorages.Size = new Size(181, 79);
listBoxStorages.TabIndex = 2;
listBoxStorages.SelectedIndexChanged += listBoxStorages_SelectedIndexChanged;
//
// buttonAddStorage
//
buttonAddStorage.Location = new Point(6, 51);
buttonAddStorage.Location = new Point(5, 44);
buttonAddStorage.Margin = new Padding(3, 2, 3, 2);
buttonAddStorage.Name = "buttonAddStorage";
buttonAddStorage.Size = new Size(177, 23);
buttonAddStorage.TabIndex = 7;
buttonAddStorage.Size = new Size(180, 22);
buttonAddStorage.TabIndex = 1;
buttonAddStorage.Text = "Добавить набор";
buttonAddStorage.UseVisualStyleBackColor = true;
buttonAddStorage.Click += buttonAddStorage_Click;
//
// textBoxStorageName
//
textBoxStorageName.Location = new Point(6, 22);
textBoxStorageName.Location = new Point(5, 20);
textBoxStorageName.Margin = new Padding(3, 2, 3, 2);
textBoxStorageName.Name = "textBoxStorageName";
textBoxStorageName.Size = new Size(177, 23);
textBoxStorageName.TabIndex = 6;
textBoxStorageName.TextChanged += textBoxStorageName_TextChanged;
//
// listBoxStorages
//
listBoxStorages.FormattingEnabled = true;
listBoxStorages.ItemHeight = 15;
listBoxStorages.Location = new Point(6, 79);
listBoxStorages.Name = "listBoxStorages";
listBoxStorages.Size = new Size(177, 94);
listBoxStorages.TabIndex = 5;
listBoxStorages.SelectedIndexChanged += listBoxStorages_SelectedIndexChanged;
textBoxStorageName.Size = new Size(181, 23);
textBoxStorageName.TabIndex = 0;
//
// maskedTextBoxNumber
//
maskedTextBoxNumber.Location = new Point(5, 318);
maskedTextBoxNumber.Location = new Point(5, 303);
maskedTextBoxNumber.Margin = new Padding(3, 2, 3, 2);
maskedTextBoxNumber.Mask = "00";
maskedTextBoxNumber.Name = "maskedTextBoxNumber";
@ -125,7 +137,7 @@
//
// buttonRefreshCollection
//
buttonRefreshCollection.Location = new Point(5, 371);
buttonRefreshCollection.Location = new Point(5, 373);
buttonRefreshCollection.Margin = new Padding(3, 2, 3, 2);
buttonRefreshCollection.Name = "buttonRefreshCollection";
buttonRefreshCollection.Size = new Size(191, 22);
@ -136,7 +148,7 @@
//
// buttonRemovePlane
//
buttonRemovePlane.Location = new Point(5, 345);
buttonRemovePlane.Location = new Point(5, 328);
buttonRemovePlane.Margin = new Padding(3, 2, 3, 2);
buttonRemovePlane.Name = "buttonRemovePlane";
buttonRemovePlane.Size = new Size(191, 22);
@ -147,7 +159,7 @@
//
// buttonAddPlane
//
buttonAddPlane.Location = new Point(4, 292);
buttonAddPlane.Location = new Point(5, 262);
buttonAddPlane.Margin = new Padding(3, 2, 3, 2);
buttonAddPlane.Name = "buttonAddPlane";
buttonAddPlane.Size = new Size(191, 22);
@ -159,13 +171,54 @@
// pictureBoxCollection
//
pictureBoxCollection.Dock = DockStyle.Fill;
pictureBoxCollection.Location = new Point(0, 0);
pictureBoxCollection.Location = new Point(0, 24);
pictureBoxCollection.Margin = new Padding(3, 2, 3, 2);
pictureBoxCollection.Name = "pictureBoxCollection";
pictureBoxCollection.Size = new Size(689, 404);
pictureBoxCollection.Size = new Size(689, 380);
pictureBoxCollection.TabIndex = 1;
pictureBoxCollection.TabStop = false;
pictureBoxCollection.Click += pictureBoxCollection_Click;
//
// menuStrip
//
menuStrip.ImageScalingSize = new Size(20, 20);
menuStrip.Items.AddRange(new ToolStripItem[] { файлToolStripMenuItem });
menuStrip.Location = new Point(0, 0);
menuStrip.Name = "menuStrip";
menuStrip.Padding = new Padding(5, 2, 0, 2);
menuStrip.Size = new Size(890, 24);
menuStrip.TabIndex = 2;
menuStrip.Text = "menuStrip1";
//
// файлToolStripMenuItem
//
файлToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { сохранитьToolStripMenuItem, загрузитьToolStripMenuItem });
файлToolStripMenuItem.Name = айлToolStripMenuItem";
файлToolStripMenuItem.Size = new Size(48, 20);
файлToolStripMenuItem.Text = "Файл";
//
// сохранитьToolStripMenuItem
//
сохранитьToolStripMenuItem.Name = "сохранитьToolStripMenuItem";
сохранитьToolStripMenuItem.Size = new Size(133, 22);
сохранитьToolStripMenuItem.Text = "Сохранить";
сохранитьToolStripMenuItem.Click += saveToFileToolStripMenuItem_Click;
//
// загрузитьToolStripMenuItem
//
загрузитьToolStripMenuItem.Name = агрузитьToolStripMenuItem";
загрузитьToolStripMenuItem.Size = new Size(133, 22);
загрузитьToolStripMenuItem.Text = "Загрузить";
загрузитьToolStripMenuItem.Click += loadFromFileToolStripMenuItem_Click;
//
// openFileDialog
//
openFileDialog.FileName = "openFileDialog1";
openFileDialog.Filter = "txt file | *.txt";
openFileDialog.FileOk += openFileDialog_FileOk;
//
// saveFileDialog
//
saveFileDialog.Filter = "txt file | *.txt";
//
// PlaneCollection
//
@ -174,6 +227,8 @@
ClientSize = new Size(890, 404);
Controls.Add(pictureBoxCollection);
Controls.Add(groupBoxTools);
Controls.Add(menuStrip);
MainMenuStrip = menuStrip;
Margin = new Padding(3, 2, 3, 2);
Name = "PlaneCollection";
Text = "Набор самолетов";
@ -182,7 +237,10 @@
groupBoxStorages.ResumeLayout(false);
groupBoxStorages.PerformLayout();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit();
menuStrip.ResumeLayout(false);
menuStrip.PerformLayout();
ResumeLayout(false);
PerformLayout();
}
#endregion
@ -194,9 +252,15 @@
private MaskedTextBox maskedTextBoxNumber;
private PictureBox pictureBoxCollection;
private GroupBox groupBoxStorages;
private TextBox textBoxStorageName;
private Button buttonRemoveStorage;
private ListBox listBoxStorages;
private Button buttonAddStorage;
private Button buttonRemoveStorage;
private TextBox textBoxStorageName;
private MenuStrip menuStrip;
private ToolStripMenuItem файлToolStripMenuItem;
private ToolStripMenuItem сохранитьToolStripMenuItem;
private ToolStripMenuItem загрузитьToolStripMenuItem;
private OpenFileDialog openFileDialog;
private SaveFileDialog saveFileDialog;
}
}

View File

@ -27,7 +27,9 @@ namespace ProjStormtrooper
InitializeComponent();
_storage = new PlanesGenericStorage(pictureBoxCollection.Width, pictureBoxCollection.Height);
}
/// <summary>
/// Заполнение listBoxObjects
/// </summary>
private void ReloadObjects()
{
int index = listBoxStorages.SelectedIndex;
@ -46,11 +48,57 @@ namespace ProjStormtrooper
}
}
/// <summary>
/// Добавление набора в коллекцию
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonAddStorage_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBoxStorageName.Text))
{
MessageBox.Show("Не все данные заполнены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
_storage.AddSet(textBoxStorageName.Text);
ReloadObjects();
}
/// <summary>
/// Выбор набора
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void listBoxStorages_SelectedIndexChanged(object sender, EventArgs e)
{
pictureBoxCollection.Image = _storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowPlanes();
}
/// <summary>
/// Удаление набора
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonDelStorage_Click(object sender, EventArgs e)
{
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
if (MessageBox.Show($"Удалить объект {listBoxStorages.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
_storage.DelSet(listBoxStorages.SelectedItem.ToString() ?? string.Empty);
ReloadObjects();
}
}
/// <summary>
/// Добавление объекта в набор
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
///
private void buttonAddPlane_Click(object sender, EventArgs e)
{
var formPlaneConfig = new PlaneConfig();
formPlaneConfig.AddEvent(AddPlaneListener);
formPlaneConfig.Show();
}
public void AddPlaneListener(DrawingPlane plane)
{
plane._pictureWidth = pictureBoxCollection.Width;
@ -74,13 +122,6 @@ namespace ProjStormtrooper
MessageBox.Show("Не удалось добавить объект");
}
}
private void buttonAddPlane_Click(object sender, EventArgs e)
{
var formPlaneConfig = new FormPlaneConfig();
formPlaneConfig.AddEvent(AddPlaneListener);
formPlaneConfig.Show();
}
/// <summary>
/// Удаление объекта из набора
/// </summary>
@ -118,6 +159,10 @@ namespace ProjStormtrooper
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonRefreshCollection_Click(object sender, EventArgs e)
{
RefreshCollection();
}
private void RefreshCollection()
{
if (listBoxStorages.SelectedIndex == -1)
{
@ -131,32 +176,35 @@ namespace ProjStormtrooper
pictureBoxCollection.Image = obj.ShowPlanes();
}
private void buttonAddStorage_Click(object sender, EventArgs e)
private void saveToFileToolStripMenuItem_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBoxStorageName.Text))
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
MessageBox.Show("Не все данные заполнены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
if (_storage.SaveData(saveFileDialog.FileName))
{
MessageBox.Show("Сохранение прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("Не сохранилось", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
_storage.AddSet(textBoxStorageName.Text);
ReloadObjects();
}
private void listBoxStorages_SelectedIndexChanged(object sender, EventArgs e)
private void loadFromFileToolStripMenuItem_Click(object sender, EventArgs e)
{
pictureBoxCollection.Image = _storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowPlanes();
}
private void buttonRemoveStorage_Click(object sender, EventArgs e)
{
if (listBoxStorages.SelectedIndex == -1)
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
return;
}
if (MessageBox.Show($"Удалить объект {listBoxStorages.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
_storage.DelSet(listBoxStorages.SelectedItem.ToString() ?? string.Empty);
ReloadObjects();
if (_storage.LoadData(openFileDialog.FileName))
{
ReloadObjects();
RefreshCollection();
MessageBox.Show("Загрузка прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("Не загрузилось", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}

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="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>

View File

@ -1,6 +1,6 @@
namespace ProjStormtrooper
{
partial class FormPlaneConfig
partial class PlaneConfig
{
/// <summary>
/// Required designer variable.

View File

@ -10,7 +10,7 @@ using System.Windows.Forms;
namespace ProjStormtrooper
{
public partial class FormPlaneConfig : Form
public partial class PlaneConfig : Form
{
/// <summary>
/// Переменная-выбранный самолет
@ -20,7 +20,7 @@ namespace ProjStormtrooper
/// Событие
/// </summary>
private event Action<DrawingPlane>? EventAddPlane;
public FormPlaneConfig()
public PlaneConfig()
{
InitializeComponent();

View File

@ -140,7 +140,7 @@ namespace ProjStormtrooper
}
}
public IEnumerable<T?> GetPlanes => _collection.GetPlanes();
}
}

View File

@ -8,6 +8,18 @@ namespace ProjStormtrooper
{
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 ProjStormtrooper
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;
}
}
}
}