This commit is contained in:
Whoisthatjulia 2023-12-13 03:15:53 +04:00
parent 34e4d490c0
commit f33c17eade
9 changed files with 281 additions and 39 deletions

View File

@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AirFighter.DrawningObjects;
namespace AirFighter
{
public delegate void AirFighterDelegate(DrawningAirFighter fighter);
}

View File

@ -10,6 +10,8 @@ namespace AirFighter.Generics
where T : DrawningAirFighter
where U : IMoveableObject
{
public IEnumerable<T?> GetAirFighter => _collection.GetAirFighter();
private readonly int _pictureWidth;
private readonly int _pictureHeight;

View File

@ -3,53 +3,138 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AirFighter.Drawnings;
using AirFighter.MovementStrategy;
using AirFighter.DrawningObjects;
using AirFighter.MovementStrategy;
using AirFighter.Generics;
using System.IO;
namespace AirFighter.Generics
{
internal class AirFighterGenericStorage
{
readonly Dictionary<string, AirFighterGenericCollection<DrawningAirFighter, DrawningObjectAirFighter>> _AirFighterStorages;
public List<string> Keys => _AirFighterStorages.Keys.ToList();
readonly Dictionary<string, AirFighterGenericCollection<DrawningAirFighter, DrawningObjectAirFighter>> _fighterStorages;
public List<string> Keys => _fighterStorages.Keys.ToList();
private readonly int _pictureWidth;
private readonly int _pictureHeight;
public AirFighterGenericStorage(int pictureWidth, int pictureHeight)
{
_AirFighterStorages = new Dictionary<string, AirFighterGenericCollection<DrawningAirFighter, DrawningObjectAirFighter>>();
_fighterStorages = new Dictionary<string, AirFighterGenericCollection<DrawningAirFighter, DrawningObjectAirFighter>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
public void AddSet(string name)
{
if (!_AirFighterStorages.ContainsKey(name))
{
var fighterCollection = new AirFighterGenericCollection<DrawningAirFighter, DrawningObjectAirFighter>(_pictureWidth, _pictureHeight);
_AirFighterStorages.Add(name, fighterCollection);
}
if (_fighterStorages.ContainsKey(name)) return;
_fighterStorages[name] = new AirFighterGenericCollection<DrawningAirFighter, DrawningObjectAirFighter>(_pictureWidth, _pictureHeight);
}
public void DelSet(string name)
{
if (_AirFighterStorages.ContainsKey(name))
{
_AirFighterStorages.Remove(name);
}
if (!_fighterStorages.ContainsKey(name)) return;
_fighterStorages.Remove(name);
}
public AirFighterGenericCollection<DrawningAirFighter, DrawningObjectAirFighter>? this[string ind]
public AirFighterGenericCollection<DrawningAirFighter, DrawningObjectAirFighter>?
this[string ind]
{
get
{
if (_AirFighterStorages.ContainsKey(ind))
{
return _AirFighterStorages[ind];
}
if (_fighterStorages.ContainsKey(ind)) return _fighterStorages[ind];
return null;
}
}
private static readonly char _separatorForKeyValue = '|';
private readonly char _separatorRecords = ';';
private static readonly char _separatorForObject = ':';
public bool SaveData(string filename)
{
if (File.Exists(filename))
{
File.Delete(filename);
}
StringBuilder data = new();
foreach (KeyValuePair<string, AirFighterGenericCollection<DrawningAirFighter, DrawningObjectAirFighter>> record in _fighterStorages)
{
StringBuilder records = new();
foreach (DrawningAirFighter? elem in record.Value.GetAirFighter)
{
records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}");
}
data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}");
}
if (data.Length == 0)
{
return false;
}
using StreamWriter fs = new StreamWriter(filename);
{
fs.WriteLine($"AirFighterStorages{Environment.NewLine}");
fs.WriteLine(data);
}
return true;
}
public bool LoadData(string filename)
{
if (!File.Exists(filename))
{
return false;
}
using (StreamReader fs = File.OpenText(filename))
{
string str = fs.ReadLine();
if (str == null || str.Length == 0)
{
return false;
}
if (!str.StartsWith("AirFighterStorages"))
{
//если нет такой записи, то это не те данные
return false;
}
_fighterStorages.Clear();
string strs = "";
while ((strs = fs.ReadLine()) != null)
{
if (strs == null)
{
return false;
}
string[] record = strs.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries);
if (record.Length != 2)
{
continue;
}
AirFighterGenericCollection<DrawningAirFighter, DrawningObjectAirFighter> collection = new(_pictureWidth, _pictureHeight);
string[] set = record[1].Split(_separatorRecords, StringSplitOptions.RemoveEmptyEntries);
foreach (string elem in set)
{
DrawningAirFighter? fighter = elem?.CreateDrawningAirFighter(_separatorForObject, _pictureWidth, _pictureHeight);
if (fighter != null)
{
if ((collection + fighter) == -1)
{
return false;
}
}
}
_fighterStorages.Add(record[0], collection);
}
return true;
}
}
}
}

View File

@ -0,0 +1,49 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AirFighter.Entities;
namespace AirFighter.DrawningObjects
{
public static class ExtentionDrawningAirFighter
{
public static DrawningAirFighter? CreateDrawningAirFighter(this string info, char separatorForObject, int width, int height)
{
string[] strs = info.Split(separatorForObject);
if (strs.Length == 3)
{
return new DrawningAirFighter(Convert.ToInt32(strs[0]),
Convert.ToInt32(strs[1]), Color.FromName(strs[2]), width, height);
}
if (strs.Length == 6)
{
return new DrawningAirFighterMilitary(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;
}
public static string GetDataForSave(this DrawningAirFighter drawningAirFighter,
char separatorForObject)
{
var fighter = drawningAirFighter.EntityAirFighter;
if (fighter == null)
{
return string.Empty;
}
var str =
$"{fighter.Speed}{separatorForObject}{fighter.Weight}{separatorForObject}{fighter.BodyColor.Name}";
if (fighter is not EntityAirFighterMilitary fighterMilitary)
{
return str;
}
return
$"{str}{separatorForObject}{fighterMilitary.AdditionalColor.Name}{separatorForObject}{fighterMilitary.Wing}{separatorForObject}{fighterMilitary.Rocket}";
}
}
}

View File

@ -39,9 +39,16 @@
buttonRefreshCollection = new Button();
buttonAddFighter = new Button();
pictureBoxCollection = new PictureBox();
menuStrip1 = new MenuStrip();
fileToolStripMenuItem = new ToolStripMenuItem();
SaveToolStripMenuItem = new ToolStripMenuItem();
LoadToolStripMenuItem = new ToolStripMenuItem();
openFileDialog1 = new OpenFileDialog();
saveFileDialog1 = new SaveFileDialog();
groupBox1.SuspendLayout();
groupBox2.SuspendLayout();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit();
menuStrip1.SuspendLayout();
SuspendLayout();
//
// groupBox1
@ -149,29 +156,73 @@
//
// pictureBoxCollection
//
pictureBoxCollection.Location = new Point(0, 0);
pictureBoxCollection.Location = new Point(0, 27);
pictureBoxCollection.Name = "pictureBoxCollection";
pictureBoxCollection.Size = new Size(734, 448);
pictureBoxCollection.Size = new Size(734, 421);
pictureBoxCollection.SizeMode = PictureBoxSizeMode.Zoom;
pictureBoxCollection.TabIndex = 1;
pictureBoxCollection.TabStop = false;
pictureBoxCollection.Click += ButtonAddAirFighter_Click;
//
// FormFighterCollection
// menuStrip1
//
menuStrip1.Items.AddRange(new ToolStripItem[] { fileToolStripMenuItem });
menuStrip1.Location = new Point(0, 0);
menuStrip1.Name = "menuStrip1";
menuStrip1.Size = new Size(953, 24);
menuStrip1.TabIndex = 2;
menuStrip1.Text = "menuStrip1";
//
// fileToolStripMenuItem
//
fileToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { SaveToolStripMenuItem, LoadToolStripMenuItem });
fileToolStripMenuItem.Name = "fileToolStripMenuItem";
fileToolStripMenuItem.Size = new Size(48, 20);
fileToolStripMenuItem.Text = "Файл";
//
// SaveToolStripMenuItem
//
SaveToolStripMenuItem.Name = "SaveToolStripMenuItem";
SaveToolStripMenuItem.Size = new Size(180, 22);
SaveToolStripMenuItem.Text = "Сохранить";
SaveToolStripMenuItem.Click += SaveToolStripMenuItem_Click;
//
// LoadToolStripMenuItem
//
LoadToolStripMenuItem.Name = "LoadToolStripMenuItem";
LoadToolStripMenuItem.Size = new Size(180, 22);
LoadToolStripMenuItem.Text = "Загрузить";
LoadToolStripMenuItem.Click += LoadToolStripMenuItem_Click;
//
// openFileDialog1
//
openFileDialog1.FileName = "openFileDialog1";
openFileDialog1.Filter = "txt file | *.txt";
//
// saveFileDialog1
//
saveFileDialog1.Filter = "txt file | *.txt";
//
// FormAirFighterCollection
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(953, 448);
Controls.Add(pictureBoxCollection);
Controls.Add(groupBox1);
Name = "FormFighterCollection";
Controls.Add(menuStrip1);
MainMenuStrip = menuStrip1;
Name = "FormAirFighterCollection";
Text = "Набор истребителя";
groupBox1.ResumeLayout(false);
groupBox1.PerformLayout();
groupBox2.ResumeLayout(false);
groupBox2.PerformLayout();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit();
menuStrip1.ResumeLayout(false);
menuStrip1.PerformLayout();
ResumeLayout(false);
PerformLayout();
}
#endregion
@ -187,5 +238,11 @@
private Button buttonDelObject;
private ListBox listBoxStorage;
private Button buttonAddObject;
private MenuStrip menuStrip1;
private ToolStripMenuItem fileToolStripMenuItem;
private ToolStripMenuItem SaveToolStripMenuItem;
private ToolStripMenuItem LoadToolStripMenuItem;
private OpenFileDialog openFileDialog1;
private SaveFileDialog saveFileDialog1;
}
}

View File

@ -7,7 +7,7 @@ using AirFighter.MovementStrategy;
namespace AirFighter
{
{
/// <summary>
/// Форма для работы с набором объектов класса DrawningAirFighter
/// </summary>
@ -126,10 +126,6 @@ namespace AirFighter
}
private void ButtonAddAirFighter_Click(object sender, EventArgs e)
{
if (listBoxStorage.SelectedIndex == -1)
{
return;
}
var formAirFighterConfig = new FormAirFighterConfig();
formAirFighterConfig.AddEvent(addAirFighter);
formAirFighterConfig.Show();
@ -190,7 +186,40 @@ namespace AirFighter
pictureBoxCollection.Image = obj.ShowAirFighter();
}
private void SaveToolStripMenuItem_Click(object sender, EventArgs e)
{
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
if (_storage.SaveData(saveFileDialog1.FileName))
{
MessageBox.Show("Сохранение прошло успешно",
"Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("Не сохранилось", "Результат",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void LoadToolStripMenuItem_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
if (_storage.LoadData(openFileDialog1.FileName))
{
ReloadObjects();
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="menuStrip1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
<metadata name="openFileDialog1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>132, 17</value>
</metadata>
<metadata name="saveFileDialog1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>272, 17</value>
</metadata>
</root>

View File

@ -119,7 +119,7 @@
//
// panelPurple
//
panelPurple.BackColor = Color.FromArgb(192, 0, 192);
panelPurple.BackColor = Color.Purple;
panelPurple.Location = new Point(139, 63);
panelPurple.Name = "panelPurple";
panelPurple.Size = new Size(35, 30);
@ -164,7 +164,7 @@
//
// panelGreen
//
panelGreen.BackColor = Color.FromArgb(0, 192, 0);
panelGreen.BackColor = Color.Green;
panelGreen.Location = new Point(57, 22);
panelGreen.Name = "panelGreen";
panelGreen.Size = new Size(35, 30);
@ -311,7 +311,7 @@
buttonCancel.Text = "Отмена";
buttonCancel.UseVisualStyleBackColor = true;
//
// FormFighterConfig
// FormAirFighterConfig
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
@ -320,9 +320,8 @@
Controls.Add(ButtonOk);
Controls.Add(panelColor);
Controls.Add(groupBox1);
Name = "FormFighterConfig";
Name = "FormAirFighterConfig";
Text = "FormFighterConfig";
groupBox1.ResumeLayout(false);
groupBox1.PerformLayout();
groupBox2.ResumeLayout(false);

View File

@ -1,4 +1,6 @@
namespace AirFighter.Generics
using System.Numerics;
namespace AirFighter.Generics
{
internal class SetGeneric<T>
where T : class
@ -18,8 +20,7 @@
}
public int Insert(T fighter, int position)
{
if (position < 0 || position > Count || Count >= _maxCount)
return -1;
if (position < 0 || position >= _maxCount) return -1;
_places.Insert(position, fighter);
return position;
}