6 лабораторная работа
This commit is contained in:
parent
944a015cbc
commit
5d0339bfdc
@ -35,5 +35,9 @@ namespace Warship
|
||||
{
|
||||
_warship.SetPosition(x,y,width,height);
|
||||
}
|
||||
|
||||
public string GetInfo() => _warship?.GetDataForSave();
|
||||
|
||||
public static IDrawingObject Create(string data) => new DrawingObjectWarship(data.CreateDrawingWarship());
|
||||
}
|
||||
}
|
||||
|
39
Warship/Warship/ExtentionCar.cs
Normal file
39
Warship/Warship/ExtentionCar.cs
Normal 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}";
|
||||
}
|
||||
}
|
||||
}
|
86
Warship/Warship/FormMapWithSetWarships.Designer.cs
generated
86
Warship/Warship/FormMapWithSetWarships.Designer.cs
generated
@ -30,6 +30,7 @@
|
||||
{
|
||||
this.groupBoxTools = new System.Windows.Forms.GroupBox();
|
||||
this.groupBoxMaps = new System.Windows.Forms.GroupBox();
|
||||
this.textBoxMap = new System.Windows.Forms.TextBox();
|
||||
this.ButtonDeleteMap = new System.Windows.Forms.Button();
|
||||
this.listBoxMaps = new System.Windows.Forms.ListBox();
|
||||
this.ButtonAddMap = new System.Windows.Forms.Button();
|
||||
@ -44,10 +45,16 @@
|
||||
this.buttonShowOnMap = new System.Windows.Forms.Button();
|
||||
this.buttonAddWarship = new System.Windows.Forms.Button();
|
||||
this.pictureBox = new System.Windows.Forms.PictureBox();
|
||||
this.textBoxMap = new System.Windows.Forms.TextBox();
|
||||
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.groupBoxTools.SuspendLayout();
|
||||
this.groupBoxMaps.SuspendLayout();
|
||||
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit();
|
||||
this.MenuStrip.SuspendLayout();
|
||||
this.SuspendLayout();
|
||||
//
|
||||
// groupBoxTools
|
||||
@ -63,9 +70,9 @@
|
||||
this.groupBoxTools.Controls.Add(this.buttonShowOnMap);
|
||||
this.groupBoxTools.Controls.Add(this.buttonAddWarship);
|
||||
this.groupBoxTools.Dock = System.Windows.Forms.DockStyle.Right;
|
||||
this.groupBoxTools.Location = new System.Drawing.Point(859, 0);
|
||||
this.groupBoxTools.Location = new System.Drawing.Point(859, 24);
|
||||
this.groupBoxTools.Name = "groupBoxTools";
|
||||
this.groupBoxTools.Size = new System.Drawing.Size(204, 652);
|
||||
this.groupBoxTools.Size = new System.Drawing.Size(204, 628);
|
||||
this.groupBoxTools.TabIndex = 0;
|
||||
this.groupBoxTools.TabStop = false;
|
||||
this.groupBoxTools.Text = "Инструменты";
|
||||
@ -84,6 +91,13 @@
|
||||
this.groupBoxMaps.TabStop = false;
|
||||
this.groupBoxMaps.Text = "Карты";
|
||||
//
|
||||
// textBoxMap
|
||||
//
|
||||
this.textBoxMap.Location = new System.Drawing.Point(5, 21);
|
||||
this.textBoxMap.Name = "textBoxMap";
|
||||
this.textBoxMap.Size = new System.Drawing.Size(175, 23);
|
||||
this.textBoxMap.TabIndex = 12;
|
||||
//
|
||||
// ButtonDeleteMap
|
||||
//
|
||||
this.ButtonDeleteMap.Location = new System.Drawing.Point(12, 223);
|
||||
@ -160,7 +174,7 @@
|
||||
this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
|
||||
this.buttonDown.BackgroundImage = global::Warship.Properties.Resources.arrowDown;
|
||||
this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
|
||||
this.buttonDown.Location = new System.Drawing.Point(91, 602);
|
||||
this.buttonDown.Location = new System.Drawing.Point(91, 578);
|
||||
this.buttonDown.Name = "buttonDown";
|
||||
this.buttonDown.Size = new System.Drawing.Size(30, 30);
|
||||
this.buttonDown.TabIndex = 10;
|
||||
@ -172,7 +186,7 @@
|
||||
this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
|
||||
this.buttonRight.BackgroundImage = global::Warship.Properties.Resources.arrowRight;
|
||||
this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
|
||||
this.buttonRight.Location = new System.Drawing.Point(127, 602);
|
||||
this.buttonRight.Location = new System.Drawing.Point(127, 578);
|
||||
this.buttonRight.Name = "buttonRight";
|
||||
this.buttonRight.Size = new System.Drawing.Size(30, 30);
|
||||
this.buttonRight.TabIndex = 9;
|
||||
@ -184,7 +198,7 @@
|
||||
this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
|
||||
this.buttonLeft.BackgroundImage = global::Warship.Properties.Resources.arrowLeft;
|
||||
this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
|
||||
this.buttonLeft.Location = new System.Drawing.Point(55, 602);
|
||||
this.buttonLeft.Location = new System.Drawing.Point(55, 578);
|
||||
this.buttonLeft.Name = "buttonLeft";
|
||||
this.buttonLeft.Size = new System.Drawing.Size(30, 30);
|
||||
this.buttonLeft.TabIndex = 8;
|
||||
@ -196,7 +210,7 @@
|
||||
this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
|
||||
this.buttonUp.BackgroundImage = global::Warship.Properties.Resources.arrowUp;
|
||||
this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
|
||||
this.buttonUp.Location = new System.Drawing.Point(91, 566);
|
||||
this.buttonUp.Location = new System.Drawing.Point(91, 542);
|
||||
this.buttonUp.Name = "buttonUp";
|
||||
this.buttonUp.Size = new System.Drawing.Size(30, 30);
|
||||
this.buttonUp.TabIndex = 7;
|
||||
@ -226,18 +240,51 @@
|
||||
// pictureBox
|
||||
//
|
||||
this.pictureBox.Dock = System.Windows.Forms.DockStyle.Fill;
|
||||
this.pictureBox.Location = new System.Drawing.Point(0, 0);
|
||||
this.pictureBox.Location = new System.Drawing.Point(0, 24);
|
||||
this.pictureBox.Name = "pictureBox";
|
||||
this.pictureBox.Size = new System.Drawing.Size(859, 652);
|
||||
this.pictureBox.Size = new System.Drawing.Size(859, 628);
|
||||
this.pictureBox.TabIndex = 1;
|
||||
this.pictureBox.TabStop = false;
|
||||
//
|
||||
// textBoxMap
|
||||
// MenuStrip
|
||||
//
|
||||
this.textBoxMap.Location = new System.Drawing.Point(5, 21);
|
||||
this.textBoxMap.Name = "textBoxMap";
|
||||
this.textBoxMap.Size = new System.Drawing.Size(175, 23);
|
||||
this.textBoxMap.TabIndex = 12;
|
||||
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
|
||||
//
|
||||
@ -246,6 +293,8 @@
|
||||
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);
|
||||
@ -253,7 +302,10 @@
|
||||
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();
|
||||
|
||||
}
|
||||
|
||||
@ -276,5 +328,11 @@
|
||||
private ListBox listBoxMaps;
|
||||
private Button ButtonAddMap;
|
||||
private TextBox textBoxMap;
|
||||
private MenuStrip MenuStrip;
|
||||
private ToolStripMenuItem файлToolStripMenuItem;
|
||||
private ToolStripMenuItem SaveToolStripMenuItem;
|
||||
private ToolStripMenuItem LoadToolStripMenuItem;
|
||||
private OpenFileDialog OpenFileDialog;
|
||||
private SaveFileDialog SaveFileDialog;
|
||||
}
|
||||
}
|
@ -178,5 +178,36 @@ 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))
|
||||
{
|
||||
ReloadMaps();
|
||||
MessageBox.Show("Загрузка прошла успешно!", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||||
}
|
||||
else
|
||||
{
|
||||
MessageBox.Show("Не загрузилось", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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="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>125, 17</value>
|
||||
</metadata>
|
||||
<metadata name="SaveFileDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
|
||||
<value>265, 17</value>
|
||||
</metadata>
|
||||
</root>
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
@ -65,6 +65,24 @@ 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()
|
||||
{
|
||||
int j = _setWarship.Count - 1;
|
||||
|
@ -9,20 +9,86 @@ 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;
|
||||
}
|
||||
|
||||
private static void WriteToFile(string text,FileStream stream)
|
||||
{
|
||||
byte[] info = new UTF8Encoding(true).GetBytes(text);
|
||||
stream.Write(info, 0, info.Length);
|
||||
}
|
||||
|
||||
public bool SaveData(string filename)
|
||||
{
|
||||
if (File.Exists(filename))
|
||||
{
|
||||
File.Delete(filename);
|
||||
}
|
||||
using (FileStream fs = new(filename, FileMode.Create))
|
||||
{
|
||||
WriteToFile($"MapsCollection{Environment.NewLine}", fs);
|
||||
foreach (var storage in _mapStorages)
|
||||
{
|
||||
WriteToFile($"{storage.Key}{separatorDict}{storage.Value.GetData(separatorDict, separatorData)}{Environment.NewLine}", fs);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool LoadData(string filename)
|
||||
{
|
||||
if (!File.Exists(filename))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
string bufferTextFromFile = "";
|
||||
using (FileStream fs = new(filename, FileMode.Open))
|
||||
{
|
||||
byte[] b = new byte[fs.Length];
|
||||
UTF8Encoding temp = new(true);
|
||||
while (fs.Read(b, 0, b.Length) > 0)
|
||||
{
|
||||
bufferTextFromFile += temp.GetString(b);
|
||||
}
|
||||
}
|
||||
var strs = bufferTextFromFile.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
|
||||
if (!strs[0].Contains("MapCollection"))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
_mapStorages.Clear();
|
||||
for(int i = 1; i < strs.Length; ++i)
|
||||
{
|
||||
var elem = strs[i].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))
|
||||
@ -38,9 +104,7 @@ namespace Warship
|
||||
|
||||
public void DelMap(string name)
|
||||
{
|
||||
|
||||
_mapStorages.Remove(name);
|
||||
|
||||
}
|
||||
|
||||
public MapWithSetWarshipsGeneric<DrawingObjectWarship, AbstractMap> this[string ind]
|
||||
|
Loading…
Reference in New Issue
Block a user