This commit is contained in:
goblinrf 2023-11-24 21:30:21 +03:00
parent 5558f6569c
commit 442f917927
6 changed files with 210 additions and 12 deletions

View File

@ -8,6 +8,7 @@ namespace ProjectAirFighter.Generics
where T :DrawningAirplane
where U: IMoveableObject
{
public IEnumerable<T?> GetAirplanes => _collection.GetAirplanes();
private readonly int _pictureWidth;
private readonly int _pictureHeight;

View File

@ -6,6 +6,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace ProjectAirFighter.Generics
{
@ -18,13 +19,105 @@ namespace ProjectAirFighter.Generics
private readonly int _pictureHeight;
private static readonly char _separatorForKeyValue = '|';
private readonly char _separatorRecords = ';';
private static readonly char _separatorForObject = ':';
public AirplanesGenericStorage(int pictureWidth, int pictureHeight)
{
_airplaneStorages = new Dictionary<string, AirplaneslGenericCollection<DrawningAirplane, DrawningObjectAirplane>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
public bool SaveData(string filename)
{
if (File.Exists(filename))
{
File.Delete(filename);
}
StringBuilder data = new();
foreach (KeyValuePair<string,
AirplaneslGenericCollection<DrawningAirplane, DrawningObjectAirplane>> record in _airplaneStorages)
{
StringBuilder records = new();
foreach (DrawningAirplane? elem in record.Value.GetAirplanes)
{
records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}");
}
data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}");
}
if (data.Length == 0)
{
return false;
}
string toWrite = $"AirplaneStorage{Environment.NewLine}{data}";
var strs = toWrite.Split(new char[] { '\n', '\r' },
StringSplitOptions.RemoveEmptyEntries);
using (StreamWriter sw = new(filename))
{
foreach (var str in strs)
{
sw.WriteLine(str);
}
}
return true;
}
public bool LoadData(string filename)
{
if (!File.Exists(filename))
{
return false;
}
using (StreamReader sr = new(filename))
{
string str = sr.ReadLine();
var strs = str.Split(new char[] { '\n', '\r' },
StringSplitOptions.RemoveEmptyEntries);
if (strs == null || strs.Length == 0)
{
return false;
}
if (!strs[0].StartsWith("AirplaneStorage"))
{
return false;
}
_airplaneStorages.Clear();
do
{
string[] record = str.Split(_separatorForKeyValue,
StringSplitOptions.RemoveEmptyEntries);
if (record.Length != 2)
{
str = sr.ReadLine();
continue;
}
AirplaneslGenericCollection<DrawningAirplane, DrawningObjectAirplane>
collection = new(_pictureWidth, _pictureHeight);
string[] set = record[1].Split(_separatorRecords,
StringSplitOptions.RemoveEmptyEntries);
foreach (string elem in set)
{
DrawningAirplane? airplane =
elem?.CreateDrawningAirplane(_separatorForObject, _pictureWidth, _pictureHeight);
if (airplane != null)
{
if (!(collection + airplane))
{
return false;
}
}
}
_airplaneStorages.Add(record[0], collection);
str = sr.ReadLine();
} while (str != null);
}
return true;
}
public void AddSet(string name)
{
_airplaneStorages.Add(name, new AirplaneslGenericCollection<DrawningAirplane, DrawningObjectAirplane>(_pictureWidth, _pictureHeight));

View File

@ -32,9 +32,7 @@ namespace ProjectAirFighter.DrawningObjects
if (width <= _airplaneWidth || height <= _airplanewingHeight)
return;
_pictureWidth = width;
_pictureHeight = height;
EntityAirplane = new EntityAirplane(speed, weight, bodyColor);
}
@ -47,7 +45,6 @@ width, int height, int airplaneWidth, int airplaneHeight)
_pictureHeight = height;
_airplaneWidth = airplaneWidth;
_airplanewingHeight = airplaneHeight;
EntityAirplane = new EntityAirplane(speed, weight, bodyColor);
}
public void ChangeColor(Color col)

View File

@ -38,9 +38,16 @@
this.updateCollectionButton = new System.Windows.Forms.Button();
this.deleteAirplaneButton = new System.Windows.Forms.Button();
this.addAirplaneButton = new System.Windows.Forms.Button();
this.menuStrip1 = new System.Windows.Forms.MenuStrip();
this.файлToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.SaveToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.LoadToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.pictureBoxCollection = new System.Windows.Forms.PictureBox();
this.openFileDialog = new System.Windows.Forms.OpenFileDialog();
this.saveFileDialog = new System.Windows.Forms.SaveFileDialog();
this.toolGroupBox.SuspendLayout();
this.kitGroupbox.SuspendLayout();
this.menuStrip1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).BeginInit();
this.SuspendLayout();
//
@ -51,6 +58,7 @@
this.toolGroupBox.Controls.Add(this.updateCollectionButton);
this.toolGroupBox.Controls.Add(this.deleteAirplaneButton);
this.toolGroupBox.Controls.Add(this.addAirplaneButton);
this.toolGroupBox.Controls.Add(this.menuStrip1);
this.toolGroupBox.Location = new System.Drawing.Point(769, 12);
this.toolGroupBox.Name = "toolGroupBox";
this.toolGroupBox.Size = new System.Drawing.Size(232, 632);
@ -60,7 +68,7 @@
//
// maskedTextBoxNumber
//
this.maskedTextBoxNumber.Location = new System.Drawing.Point(7, 387);
this.maskedTextBoxNumber.Location = new System.Drawing.Point(7, 406);
this.maskedTextBoxNumber.Name = "maskedTextBoxNumber";
this.maskedTextBoxNumber.Size = new System.Drawing.Size(215, 27);
this.maskedTextBoxNumber.TabIndex = 7;
@ -71,7 +79,7 @@
this.kitGroupbox.Controls.Add(this.ButtonDelObject);
this.kitGroupbox.Controls.Add(this.ButtonAddObject);
this.kitGroupbox.Controls.Add(this.listBoxStorages);
this.kitGroupbox.Location = new System.Drawing.Point(8, 39);
this.kitGroupbox.Location = new System.Drawing.Point(7, 74);
this.kitGroupbox.Name = "kitGroupbox";
this.kitGroupbox.Size = new System.Drawing.Size(217, 280);
this.kitGroupbox.TabIndex = 5;
@ -80,7 +88,7 @@
//
// textBoxStorageName
//
this.textBoxStorageName.Location = new System.Drawing.Point(23, 27);
this.textBoxStorageName.Location = new System.Drawing.Point(17, 26);
this.textBoxStorageName.Name = "textBoxStorageName";
this.textBoxStorageName.Size = new System.Drawing.Size(185, 27);
this.textBoxStorageName.TabIndex = 9;
@ -113,10 +121,11 @@
this.listBoxStorages.Name = "listBoxStorages";
this.listBoxStorages.Size = new System.Drawing.Size(185, 104);
this.listBoxStorages.TabIndex = 6;
this.listBoxStorages.SelectedIndexChanged += new System.EventHandler(this.listBoxStorages_SelectedIndexChanged);
//
// updateCollectionButton
//
this.updateCollectionButton.Location = new System.Drawing.Point(6, 484);
this.updateCollectionButton.Location = new System.Drawing.Point(6, 505);
this.updateCollectionButton.Name = "updateCollectionButton";
this.updateCollectionButton.Size = new System.Drawing.Size(215, 40);
this.updateCollectionButton.TabIndex = 3;
@ -126,7 +135,7 @@
//
// deleteAirplaneButton
//
this.deleteAirplaneButton.Location = new System.Drawing.Point(7, 439);
this.deleteAirplaneButton.Location = new System.Drawing.Point(6, 449);
this.deleteAirplaneButton.Name = "deleteAirplaneButton";
this.deleteAirplaneButton.Size = new System.Drawing.Size(215, 40);
this.deleteAirplaneButton.TabIndex = 2;
@ -136,7 +145,7 @@
//
// addAirplaneButton
//
this.addAirplaneButton.Location = new System.Drawing.Point(8, 324);
this.addAirplaneButton.Location = new System.Drawing.Point(8, 360);
this.addAirplaneButton.Name = "addAirplaneButton";
this.addAirplaneButton.Size = new System.Drawing.Size(215, 40);
this.addAirplaneButton.TabIndex = 0;
@ -144,6 +153,40 @@
this.addAirplaneButton.UseVisualStyleBackColor = true;
this.addAirplaneButton.Click += new System.EventHandler(this.addAirplaneButton_Click);
//
// menuStrip1
//
this.menuStrip1.ImageScalingSize = new System.Drawing.Size(20, 20);
this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.файлToolStripMenuItem});
this.menuStrip1.Location = new System.Drawing.Point(3, 23);
this.menuStrip1.Name = "menuStrip1";
this.menuStrip1.Size = new System.Drawing.Size(226, 28);
this.menuStrip1.TabIndex = 8;
this.menuStrip1.Text = "menuStrip1";
//
// файлToolStripMenuItem
//
this.файлToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.SaveToolStripMenuItem,
this.LoadToolStripMenuItem});
this.файлToolStripMenuItem.Name = айлToolStripMenuItem";
this.файлToolStripMenuItem.Size = new System.Drawing.Size(59, 24);
this.файлToolStripMenuItem.Text = "Файл";
//
// SaveToolStripMenuItem
//
this.SaveToolStripMenuItem.Name = "SaveToolStripMenuItem";
this.SaveToolStripMenuItem.Size = new System.Drawing.Size(177, 26);
this.SaveToolStripMenuItem.Text = "Сохранение";
this.SaveToolStripMenuItem.Click += new System.EventHandler(this.SaveToolStripMenuItem_Click);
//
// LoadToolStripMenuItem
//
this.LoadToolStripMenuItem.Name = "LoadToolStripMenuItem";
this.LoadToolStripMenuItem.Size = new System.Drawing.Size(177, 26);
this.LoadToolStripMenuItem.Text = "Загрузка";
this.LoadToolStripMenuItem.Click += new System.EventHandler(this.LoadToolStripMenuItem_Click);
//
// pictureBoxCollection
//
this.pictureBoxCollection.Location = new System.Drawing.Point(12, 12);
@ -152,6 +195,15 @@
this.pictureBoxCollection.TabIndex = 1;
this.pictureBoxCollection.TabStop = false;
//
// openFileDialog
//
this.openFileDialog.FileName = "openFileDialog1";
this.openFileDialog.Filter = "txt file | *.txt";
//
// saveFileDialog
//
this.saveFileDialog.Filter = "txt file | *.txt";
//
// FormAirplaneCollection
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F);
@ -159,12 +211,15 @@
this.ClientSize = new System.Drawing.Size(1017, 654);
this.Controls.Add(this.pictureBoxCollection);
this.Controls.Add(this.toolGroupBox);
this.MainMenuStrip = this.menuStrip1;
this.Name = "FormAirplaneCollection";
this.Text = "FormMonorailCollection";
this.toolGroupBox.ResumeLayout(false);
this.toolGroupBox.PerformLayout();
this.kitGroupbox.ResumeLayout(false);
this.kitGroupbox.PerformLayout();
this.menuStrip1.ResumeLayout(false);
this.menuStrip1.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).EndInit();
this.ResumeLayout(false);
@ -183,5 +238,11 @@
private Button ButtonDelObject;
private TextBox textBoxStorageName;
private MaskedTextBox maskedTextBoxNumber;
private MenuStrip menuStrip1;
private ToolStripMenuItem файлToolStripMenuItem;
private ToolStripMenuItem SaveToolStripMenuItem;
private ToolStripMenuItem LoadToolStripMenuItem;
private OpenFileDialog openFileDialog;
private SaveFileDialog saveFileDialog;
}
}

View File

@ -23,6 +23,44 @@ namespace ProjectAirFighter
_storage = new AirplanesGenericStorage(pictureBoxCollection.Width,
pictureBoxCollection.Height);
}
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);
foreach (var collection in _storage.Keys)
{
listBoxStorages.Items.Add(collection);
}
}
else
{
MessageBox.Show("Не загрузилось", "Результат",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void ReloadObjects()
{
int index = listBoxStorages.SelectedIndex;
@ -42,6 +80,7 @@ namespace ProjectAirFighter
{
listBoxStorages.SelectedIndex = index;
}
if (listBoxStorages.SelectedIndex == -1)
{
return;
@ -53,7 +92,6 @@ namespace ProjectAirFighter
return;
}
pictureBoxCollection.Image = obj.ShowAirplanes();
}
private void ButtonAddObject_Click(object sender, EventArgs e)
{
@ -111,7 +149,6 @@ namespace ProjectAirFighter
pictureBoxCollection.Image = obj.ShowAirplanes();
}
private void addAirplaneButton_Click(object sender, EventArgs e)
{
if (listBoxStorages.SelectedIndex == -1)
@ -145,6 +182,7 @@ namespace ProjectAirFighter
{
pictureBoxCollection.Image =
_storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowAirplanes();
}
private void ButtonDelObject_Click(object sender, EventArgs e)
@ -162,6 +200,5 @@ MessageBoxIcon.Question) == DialogResult.Yes)
}
}
}
}

View File

@ -57,4 +57,13 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<metadata name="menuStrip1.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>311, 17</value>
</metadata>
</root>