Novopolcev A.A. LabWork 06 #6

Closed
A.Novopoltsev wants to merge 1 commits from LabWork06 into LabWork05
7 changed files with 227 additions and 12 deletions
Showing only changes of commit 0f6942ad6e - Show all commits

View File

@ -35,5 +35,8 @@ namespace Warship
{
_warship.SetPosition(x, y, width, height);
}
public string GetInfo() => _warship?.GetDataForSave();
public static IDrawingObject Create(string data) => new DrawingObjectWarship(data.CreateDrawingWarship());
}
}

View File

@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Warship
{
internal static class ExtentionCar
{
private static readonly char _separatorForObject = ':';
public static DrawingWarship CreateDrawingWarship(this string info)
{
string[] strs = info.Split(_separatorForObject);
if (strs.Length == 3)
{
return new DrawingWarship(Convert.ToInt32(strs[0]), Convert.ToInt32(strs[1]), Color.FromName(strs[2]));
}
if (strs.Length == 7)
{
return new DrawingAdvancedWarship(Convert.ToInt32(strs[0]), Convert.ToInt32(strs[1]), Color.FromName(strs[2]), Color.FromName(strs[3]), Convert.ToBoolean(strs[4]), Convert.ToBoolean(strs[5]), Convert.ToBoolean(strs[6]));
}
return null;
}
public static string GetDataForSave(this DrawingWarship drawingWarship)
{
var warship = drawingWarship.Warship;
var str = $"{warship.Speed}{_separatorForObject}{warship.Weight}{_separatorForObject}{warship.BodyColor.Name}";
if (warship is not EntityAdvancedWarship advancedWarship)
{
return str;
}
return $"{str}{_separatorForObject}{advancedWarship.DopColor.Name}{_separatorForObject}{advancedWarship.Helipad}{_separatorForObject}{advancedWarship.Antenna}{_separatorForObject}{advancedWarship.Missile}";
}
}
}

View File

@ -41,11 +41,18 @@
this.buttonShowStorage = new System.Windows.Forms.Button();
this.maskedTextBoxPosition = new System.Windows.Forms.MaskedTextBox();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.MenuStrip = 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.OpenFileDialog = new System.Windows.Forms.OpenFileDialog();
this.SaveFileDialog = new System.Windows.Forms.SaveFileDialog();
this.pictureBox = new System.Windows.Forms.PictureBox();
this.groupBoxTools.SuspendLayout();
this.groupBoxMaps.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit();
this.MenuStrip.SuspendLayout();
this.SuspendLayout();
//
// groupBoxTools
@ -58,9 +65,9 @@
this.groupBoxTools.Controls.Add(this.maskedTextBoxPosition);
this.groupBoxTools.Controls.Add(this.pictureBox1);
this.groupBoxTools.Dock = System.Windows.Forms.DockStyle.Right;
this.groupBoxTools.Location = new System.Drawing.Point(911, 0);
this.groupBoxTools.Location = new System.Drawing.Point(863, 0);
this.groupBoxTools.Name = "groupBoxTools";
this.groupBoxTools.Size = new System.Drawing.Size(200, 597);
this.groupBoxTools.Size = new System.Drawing.Size(200, 652);
this.groupBoxTools.TabIndex = 0;
this.groupBoxTools.TabStop = false;
this.groupBoxTools.Text = "Инструменты";
@ -189,7 +196,7 @@
this.pictureBox.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBox.Location = new System.Drawing.Point(0, 0);
this.pictureBox.Name = "pictureBox";
this.pictureBox.Size = new System.Drawing.Size(911, 597);
this.pictureBox.Size = new System.Drawing.Size(863, 652);
this.pictureBox.TabIndex = 1;
this.pictureBox.TabStop = false;
//
@ -197,11 +204,11 @@
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1111, 597);
this.ClientSize = new System.Drawing.Size(1063, 652);
this.Controls.Add(this.pictureBox);
this.Controls.Add(this.groupBoxTools);
this.Name = "FormMapWithSetWarships";
this.Text = "FormMapWithSetWarships";
this.Text = "Карта с набором объектов";
this.groupBoxTools.ResumeLayout(false);
this.groupBoxTools.PerformLayout();
this.groupBoxMaps.ResumeLayout(false);
@ -209,6 +216,67 @@
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit();
this.ResumeLayout(false);
//
// MenuStrip
//
this.MenuStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.файлToolStripMenuItem});
this.MenuStrip.Location = new System.Drawing.Point(0, 0);
this.MenuStrip.Name = "MenuStrip";
this.MenuStrip.Size = new System.Drawing.Size(1063, 24);
this.MenuStrip.TabIndex = 2;
//
// файл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(48, 20);
this.файлToolStripMenuItem.Text = "Файл";
//
// SaveToolStripMenuItem
//
this.SaveToolStripMenuItem.Name = "SaveToolStripMenuItem";
this.SaveToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
this.SaveToolStripMenuItem.Text = "Сохранение";
this.SaveToolStripMenuItem.Click += new System.EventHandler(this.SaveToolStripMenuItem_Click);
//
// LoadToolStripMenuItem
//
this.LoadToolStripMenuItem.Name = "LoadToolStripMenuItem";
this.LoadToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
this.LoadToolStripMenuItem.Text = "Загрузка";
this.LoadToolStripMenuItem.Click += new System.EventHandler(this.LoadToolStripMenuItem_Click);
//
// OpenFileDialog
//
this.OpenFileDialog.Filter = "txt file | *.txt";
//
// SaveFileDialog
//
this.SaveFileDialog.Filter = "txt file | *.txt";
//
// FormMapWithSetWarships
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1063, 652);
this.Controls.Add(this.pictureBox);
this.Controls.Add(this.groupBoxTools);
this.Controls.Add(this.MenuStrip);
this.MainMenuStrip = this.MenuStrip;
this.Name = "FormMapWithSetWarships";
this.Text = "Карта с набором объектов";
this.groupBoxTools.ResumeLayout(false);
this.groupBoxTools.PerformLayout();
this.groupBoxMaps.ResumeLayout(false);
this.groupBoxMaps.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit();
this.MenuStrip.ResumeLayout(false);
this.MenuStrip.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
@ -227,6 +295,12 @@
private TextBox textBoxMap;
private Button buttonAddMap;
private ListBox listBoxMaps;
private Button buttonDeleteMap;
private Button buttonDeleteMap;
private MenuStrip MenuStrip;
private ToolStripMenuItem файлToolStripMenuItem;
private ToolStripMenuItem SaveToolStripMenuItem;
private ToolStripMenuItem LoadToolStripMenuItem;
private OpenFileDialog OpenFileDialog;
private SaveFileDialog SaveFileDialog;
}
}

View File

@ -178,5 +178,35 @@ namespace Warship
ReloadMaps();
}
}
private void SaveToolStripMenuItem_Click(object sender, EventArgs e)
{
if (SaveFileDialog.ShowDialog() == DialogResult.OK)
{
if (_mapsCollection.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 (_mapsCollection.LoadData(OpenFileDialog.FileName))
{
MessageBox.Show("Загрузка прошла успешно!", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
ReloadMaps();
}
else
{
MessageBox.Show("Не загрузилось", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
}

View File

@ -13,5 +13,6 @@ namespace Warship
void MoveObject(Direction direction);
void DrawingObject(Graphics g);
(float Left, float Right, float Top, float Bottom) GetCurrentPosition();
string GetInfo();
}
}

View File

@ -64,6 +64,23 @@ namespace Warship
}
return new(_pictureWidth, _pictureHeight);
}
public string GetData(char separatorType, char separatorData)
{
string data = $"{_map.GetType().Name}{separatorType}";
foreach (var warship in _setWarship.GetWarships())
{
data += $"{warship.GetInfo()}{separatorData}";
}
return data;
}
public void LoadData(string[] records)
{
foreach (var rec in records)
{
_setWarship.Insert(DrawingObjectWarship.Create(rec) as T);
}
}
public void Shaking()
{

View File

@ -8,20 +8,73 @@ namespace Warship
{
internal class MapsCollection
{
readonly Dictionary<string, MapWithSetWarshipsGeneric<DrawingObjectWarship, AbstractMap>> _mapStorages;
readonly Dictionary<string, MapWithSetWarshipsGeneric<IDrawingObject, AbstractMap>> _mapStorages;
public List<string> Keys => _mapStorages.Keys.ToList();
private readonly int _pictureWidth;
private readonly int _pictureHeight;
private readonly char separatorDict = '|';
private readonly char separatorData = ';';
public MapsCollection(int pictureWidth, int pictureHeight)
{
_mapStorages = new Dictionary<string, MapWithSetWarshipsGeneric<DrawingObjectWarship, AbstractMap>>();
_mapStorages = new Dictionary<string, MapWithSetWarshipsGeneric<IDrawingObject, AbstractMap>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
public bool SaveData(string filename)
{
if (File.Exists(filename))
{
File.Delete(filename);
}
using (StreamWriter sw = new(filename))
{
sw.Write($"MapsCollection{Environment.NewLine}");
foreach (var storage in _mapStorages)
{
sw.Write($"{storage.Key}{separatorDict}{storage.Value.GetData(separatorDict, separatorData)}{Environment.NewLine}");
}
}
return true;
}
public bool LoadData(string filename)
{
if (!File.Exists(filename))
{
return false;
}
using (StreamReader sr = new(filename))
{
string str = "";
if ((str = sr.ReadLine()) == null || !str.Contains("MapsCollection"))
{
return false;
}
_mapStorages.Clear();
while ((str = sr.ReadLine()) != null)
{
var elem = str.Split(separatorDict);
AbstractMap map = null;
switch (elem[1])
{
case "SimpleMap":
map = new SimpleMap();
break;
case "SecondMap":
map = new SecondMap();
break;
}
_mapStorages.Add(elem[0], new MapWithSetWarshipsGeneric<IDrawingObject, AbstractMap>(_pictureWidth, _pictureHeight, map));
_mapStorages[elem[0]].LoadData(elem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries));
}
}
return true;
}
public void AddMap(string name, AbstractMap map)
{
if (_mapStorages.ContainsKey(name))
@ -31,18 +84,16 @@ namespace Warship
}
else
{
_mapStorages.Add(name, new MapWithSetWarshipsGeneric<DrawingObjectWarship, AbstractMap>(_pictureWidth, _pictureHeight, map));
_mapStorages.Add(name, new MapWithSetWarshipsGeneric<IDrawingObject, AbstractMap>(_pictureWidth, _pictureHeight, map));
}
}
public void DelMap(string name)
{
_mapStorages.Remove(name);
}
public MapWithSetWarshipsGeneric<DrawingObjectWarship, AbstractMap> this[string ind]
public MapWithSetWarshipsGeneric<IDrawingObject, AbstractMap> this[string ind]
{
get
{