Лабораторная работа 6
This commit is contained in:
parent
d41a44ec2c
commit
214931344f
@ -36,6 +36,8 @@ namespace Monorail
|
||||
{
|
||||
_locomotive.DrawTransport(g);
|
||||
}
|
||||
public string GetInfo() => _locomotive?.GetDataForSave();
|
||||
public static IDrawingObject Create(string data) => new DrawingObjectLocomotive(data.CreateDrawingLocomotive());
|
||||
|
||||
}
|
||||
}
|
||||
|
38
Monorail/Monorail/ExtentionLocomotive.cs
Normal file
38
Monorail/Monorail/ExtentionLocomotive.cs
Normal file
@ -0,0 +1,38 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Monorail
|
||||
{
|
||||
internal static class ExtentionLocomotive
|
||||
{
|
||||
private static readonly char _separatorForObject = ':';
|
||||
public static DrawingLocomotive CreateDrawingLocomotive(this string info)
|
||||
{
|
||||
string[] strs = info.Split(_separatorForObject);
|
||||
if (strs.Length == 3)
|
||||
{
|
||||
return new DrawingLocomotive(Convert.ToInt32(strs[0]),
|
||||
Convert.ToInt32(strs[1]), Color.FromName(strs[2]));
|
||||
}
|
||||
if (strs.Length == 6)
|
||||
{
|
||||
return new DrawingMonorailLocomotive(Convert.ToInt32(strs[0]), Convert.ToInt32(strs[1]), Color.FromName(strs[2]), Color.FromName(strs[3]), Convert.ToBoolean(strs[4]), Convert.ToBoolean(strs[5]));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
public static string GetDataForSave(this DrawingLocomotive DrawingCar)
|
||||
{
|
||||
var locomotive = DrawingCar.Locomotive;
|
||||
var str = $"{locomotive.Speed}{_separatorForObject}{locomotive.Weight}{_separatorForObject}{locomotive.BodyColor.Name}";
|
||||
if (locomotive is not EntityMonorailLocomotive monorailLocomotive)
|
||||
{
|
||||
return str;
|
||||
}
|
||||
return
|
||||
$"{str}{_separatorForObject}{monorailLocomotive.DopColor.Name}{_separatorForObject}{monorailLocomotive.DopCabin}{_separatorForObject}{monorailLocomotive.Monorail}";
|
||||
}
|
||||
}
|
||||
}
|
@ -45,17 +45,24 @@
|
||||
buttonDeleteMap = new Button();
|
||||
buttonAddMap = new Button();
|
||||
listBoxMaps = new ListBox();
|
||||
menuStrip = new MenuStrip();
|
||||
ToolStripMenuItem = new ToolStripMenuItem();
|
||||
SaveToolStripMenuItem = new ToolStripMenuItem();
|
||||
LoadToolStripMenuItem = new ToolStripMenuItem();
|
||||
openFileDialog = new OpenFileDialog();
|
||||
saveFileDialog = new SaveFileDialog();
|
||||
((System.ComponentModel.ISupportInitialize)pictureBoxLocomotive).BeginInit();
|
||||
groupBoxTools.SuspendLayout();
|
||||
groupBoxMaps.SuspendLayout();
|
||||
menuStrip.SuspendLayout();
|
||||
SuspendLayout();
|
||||
//
|
||||
// pictureBoxLocomotive
|
||||
//
|
||||
pictureBoxLocomotive.Dock = DockStyle.Left;
|
||||
pictureBoxLocomotive.Location = new Point(0, 0);
|
||||
pictureBoxLocomotive.Location = new Point(0, 24);
|
||||
pictureBoxLocomotive.Name = "pictureBoxLocomotive";
|
||||
pictureBoxLocomotive.Size = new Size(579, 561);
|
||||
pictureBoxLocomotive.Size = new Size(579, 537);
|
||||
pictureBoxLocomotive.TabIndex = 0;
|
||||
pictureBoxLocomotive.TabStop = false;
|
||||
//
|
||||
@ -231,6 +238,44 @@
|
||||
listBoxMaps.TabIndex = 12;
|
||||
listBoxMaps.SelectedIndexChanged += listBoxMaps_SelectedIndexChanged;
|
||||
//
|
||||
// menuStrip
|
||||
//
|
||||
menuStrip.Items.AddRange(new ToolStripItem[] { ToolStripMenuItem });
|
||||
menuStrip.Location = new Point(0, 0);
|
||||
menuStrip.Name = "menuStrip";
|
||||
menuStrip.Size = new Size(800, 24);
|
||||
menuStrip.TabIndex = 13;
|
||||
menuStrip.Text = "menuStrip1";
|
||||
//
|
||||
// ToolStripMenuItem
|
||||
//
|
||||
ToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { SaveToolStripMenuItem, LoadToolStripMenuItem });
|
||||
ToolStripMenuItem.Name = "ToolStripMenuItem";
|
||||
ToolStripMenuItem.Size = new Size(48, 20);
|
||||
ToolStripMenuItem.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;
|
||||
//
|
||||
// openFileDialog
|
||||
//
|
||||
openFileDialog.Filter = "txt file | *.txt";
|
||||
//
|
||||
// saveFileDialog
|
||||
//
|
||||
saveFileDialog.Filter = "txt file | *.txt";
|
||||
//
|
||||
// FormMapWithSetLocomotive
|
||||
//
|
||||
AutoScaleDimensions = new SizeF(7F, 15F);
|
||||
@ -238,6 +283,8 @@
|
||||
ClientSize = new Size(800, 561);
|
||||
Controls.Add(groupBoxTools);
|
||||
Controls.Add(pictureBoxLocomotive);
|
||||
Controls.Add(menuStrip);
|
||||
MainMenuStrip = menuStrip;
|
||||
Name = "FormMapWithSetLocomotive";
|
||||
Text = "Карта с набором объектов";
|
||||
((System.ComponentModel.ISupportInitialize)pictureBoxLocomotive).EndInit();
|
||||
@ -245,7 +292,10 @@
|
||||
groupBoxTools.PerformLayout();
|
||||
groupBoxMaps.ResumeLayout(false);
|
||||
groupBoxMaps.PerformLayout();
|
||||
menuStrip.ResumeLayout(false);
|
||||
menuStrip.PerformLayout();
|
||||
ResumeLayout(false);
|
||||
PerformLayout();
|
||||
}
|
||||
|
||||
#endregion
|
||||
@ -267,5 +317,11 @@
|
||||
private ListBox listBoxMaps;
|
||||
private GroupBox groupBoxMaps;
|
||||
private TextBox textBoxNewMapName;
|
||||
private MenuStrip menuStrip;
|
||||
private ToolStripMenuItem ToolStripMenuItem;
|
||||
private ToolStripMenuItem SaveToolStripMenuItem;
|
||||
private ToolStripMenuItem LoadToolStripMenuItem;
|
||||
private OpenFileDialog openFileDialog;
|
||||
private SaveFileDialog saveFileDialog;
|
||||
}
|
||||
}
|
@ -181,5 +181,40 @@ namespace Monorail
|
||||
}
|
||||
pictureBoxLocomotive.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].MoveObject(dir);
|
||||
}
|
||||
|
||||
|
||||
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>
|
@ -36,6 +36,7 @@ namespace Monorail
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
(float Left, float Right, float Top, float Bottom) GetCurrentPosition();
|
||||
string GetInfo();
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -7,7 +7,7 @@ using System.Threading.Tasks;
|
||||
namespace Monorail
|
||||
{
|
||||
internal class MapWithSetLocomotiveGeneric<T, U>
|
||||
where T : class, IDrawingObject
|
||||
where T : class, IDrawingObject
|
||||
where U : AbstractMap
|
||||
{
|
||||
// Ширина окна отрисовки
|
||||
@ -131,5 +131,22 @@ where T : class, IDrawingObject
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public string GetData(char separatorType, char separatorData)
|
||||
{
|
||||
string data = $"{_map.GetType().Name}{separatorType}";
|
||||
foreach (var boat in _setLocomotive.GetLocomotives())
|
||||
{
|
||||
data += $"{boat.GetInfo()}{separatorData}";
|
||||
}
|
||||
return data;
|
||||
}
|
||||
public void LoadData(string[] records)
|
||||
{
|
||||
foreach (var rec in records)
|
||||
{
|
||||
_setLocomotive.Insert(DrawingObjectLocomotive.Create(rec) as T);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -8,42 +8,102 @@ namespace Monorail
|
||||
{
|
||||
internal class MapsCollection
|
||||
{
|
||||
readonly Dictionary<string, MapWithSetLocomotiveGeneric<DrawingObjectLocomotive, AbstractMap>> _mapStorages;
|
||||
readonly Dictionary<string, MapWithSetLocomotiveGeneric<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, MapWithSetLocomotiveGeneric<DrawingObjectLocomotive, AbstractMap>>();
|
||||
_mapStorages = new Dictionary<string, MapWithSetLocomotiveGeneric<IDrawingObject, AbstractMap>>();
|
||||
_pictureWidth = pictureWidth;
|
||||
_pictureHeight = pictureHeight;
|
||||
}
|
||||
|
||||
public void AddMap(string name, AbstractMap map)
|
||||
{
|
||||
if (!_mapStorages.ContainsKey(name))
|
||||
{
|
||||
_mapStorages.Add(name, new MapWithSetLocomotiveGeneric<DrawingObjectLocomotive, AbstractMap>
|
||||
(_pictureWidth, _pictureHeight, map));
|
||||
_mapStorages.Add(name, new MapWithSetLocomotiveGeneric<IDrawingObject, AbstractMap>(_pictureWidth, _pictureHeight, map));
|
||||
}
|
||||
}
|
||||
public void DelMap(string name)
|
||||
{
|
||||
if (_mapStorages.ContainsKey(name))
|
||||
_mapStorages.Remove(name);
|
||||
if (_mapStorages.ContainsKey(name)) _mapStorages.Remove(name);
|
||||
}
|
||||
public MapWithSetLocomotiveGeneric<DrawingObjectLocomotive, AbstractMap> this[string ind]
|
||||
public MapWithSetLocomotiveGeneric<IDrawingObject, AbstractMap> this[string ind]
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_mapStorages.ContainsKey(ind))
|
||||
{
|
||||
return _mapStorages[ind];
|
||||
else
|
||||
return null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
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 (StreamWriter fs = new StreamWriter(filename))
|
||||
{
|
||||
fs.Write($"MapsCollection{Environment.NewLine}");
|
||||
foreach (var storage in _mapStorages)
|
||||
{
|
||||
|
||||
fs.Write($"{storage.Key}{separatorDict}{storage.Value.GetData(separatorDict, separatorData)}{Environment.NewLine}");
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
public bool LoadData(string filename)
|
||||
{
|
||||
if (!File.Exists(filename))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
string str = "";
|
||||
using (StreamReader fs = new StreamReader(filename))
|
||||
{
|
||||
str = fs.ReadLine();
|
||||
if (!str.Contains("MapsCollection"))
|
||||
{
|
||||
//если нет такой записи, то это не те данные
|
||||
return false;
|
||||
}
|
||||
_mapStorages.Clear();
|
||||
while ((str = fs.ReadLine()) != null)
|
||||
{
|
||||
var elem = str.Split(separatorDict);
|
||||
AbstractMap map = null;
|
||||
switch (elem[1])
|
||||
{
|
||||
case "SimpleMap":
|
||||
map = new SimpleMap();
|
||||
break;
|
||||
case "BushesMap":
|
||||
map = new BushesMap();
|
||||
break;
|
||||
case "FieldMap":
|
||||
map = new FieldMap();
|
||||
break;
|
||||
}
|
||||
_mapStorages.Add(elem[0], new MapWithSetLocomotiveGeneric<IDrawingObject, AbstractMap>(_pictureWidth, _pictureHeight, map));
|
||||
_mapStorages[elem[0]].LoadData(elem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries));
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user