лаба 6 готовая

This commit is contained in:
Sem730 2022-11-20 09:54:54 +03:00
parent ae6e1074b8
commit b4e17fa1f7
5 changed files with 129 additions and 50 deletions

View File

@ -32,7 +32,6 @@ namespace ProjectLocomotive
_loc.DrawTransport(g);
}
public string getInfo() => _loc?.getDataForSave();
public static IDrawningObject Create(string data) => new DrawningObject(data.createDrawningLocomotive());
}
}

View File

@ -44,9 +44,16 @@
this.maskedTextBoxPosition = new System.Windows.Forms.MaskedTextBox();
this.buttonAddLocomotive = new System.Windows.Forms.Button();
this.pictureBox = new System.Windows.Forms.PictureBox();
this.menuStrip = new System.Windows.Forms.MenuStrip();
this.fileToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.saveToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.loadToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.loadFileDialog = new System.Windows.Forms.OpenFileDialog();
this.saveFileDialog = new System.Windows.Forms.SaveFileDialog();
this.groupBoxTools.SuspendLayout();
this.groupBox1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit();
this.menuStrip.SuspendLayout();
this.SuspendLayout();
//
// groupBoxTools
@ -62,11 +69,11 @@
this.groupBoxTools.Controls.Add(this.maskedTextBoxPosition);
this.groupBoxTools.Controls.Add(this.buttonAddLocomotive);
this.groupBoxTools.Dock = System.Windows.Forms.DockStyle.Right;
this.groupBoxTools.Location = new System.Drawing.Point(794, 0);
this.groupBoxTools.Location = new System.Drawing.Point(580, 28);
this.groupBoxTools.Margin = new System.Windows.Forms.Padding(4);
this.groupBoxTools.Name = "groupBoxTools";
this.groupBoxTools.Padding = new System.Windows.Forms.Padding(4);
this.groupBoxTools.Size = new System.Drawing.Size(220, 654);
this.groupBoxTools.Size = new System.Drawing.Size(220, 540);
this.groupBoxTools.TabIndex = 0;
this.groupBoxTools.TabStop = false;
this.groupBoxTools.Text = "Инструменты";
@ -238,20 +245,63 @@
// 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, 28);
this.pictureBox.Margin = new System.Windows.Forms.Padding(4);
this.pictureBox.Name = "pictureBox";
this.pictureBox.Size = new System.Drawing.Size(794, 654);
this.pictureBox.Size = new System.Drawing.Size(580, 540);
this.pictureBox.TabIndex = 1;
this.pictureBox.TabStop = false;
// menuStrip
//
this.menuStrip.ImageScalingSize = new System.Drawing.Size(20, 20);
this.menuStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.fileToolStripMenuItem});
this.menuStrip.Location = new System.Drawing.Point(0, 0);
this.menuStrip.Name = "menuStrip";
this.menuStrip.Size = new System.Drawing.Size(800, 28);
this.menuStrip.TabIndex = 2;
//
// fileToolStripMenuItem
//
this.fileToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.saveToolStripMenuItem,
this.loadToolStripMenuItem});
this.fileToolStripMenuItem.Name = "fileToolStripMenuItem";
this.fileToolStripMenuItem.Size = new System.Drawing.Size(46, 24);
this.fileToolStripMenuItem.Text = "File";
//
// saveToolStripMenuItem
//
this.saveToolStripMenuItem.Name = "saveToolStripMenuItem";
this.saveToolStripMenuItem.Size = new System.Drawing.Size(224, 26);
this.saveToolStripMenuItem.Text = "Save";
this.saveToolStripMenuItem.Click += new System.EventHandler(this.saveToolStripMenuItem_Click);
//
// loadToolStripMenuItem
//
this.loadToolStripMenuItem.Name = "loadToolStripMenuItem";
this.loadToolStripMenuItem.Size = new System.Drawing.Size(224, 26);
this.loadToolStripMenuItem.Text = "Load";
this.loadToolStripMenuItem.Click += new System.EventHandler(this.loadToolStripMenuItem_Click);
//
// loadFileDialog
//
this.loadFileDialog.Filter = "txt file | *.txt";
//
// saveFileDialog
//
this.saveFileDialog.Filter = "txt file | *.txt";
//
//
// FormMapWithSetLocomotives
//
this.AutoScaleDimensions = new System.Drawing.SizeF(10F, 25F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1014, 654);
this.ClientSize = new System.Drawing.Size(800, 568);
this.Controls.Add(this.pictureBox);
this.Controls.Add(this.groupBoxTools);
this.Controls.Add(this.menuStrip);
this.MainMenuStrip = this.menuStrip;
this.Margin = new System.Windows.Forms.Padding(4);
this.Name = "FormMapWithSetLocomotives";
this.Text = "Карта с набором объектов";
@ -260,7 +310,10 @@
this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit();
this.menuStrip.ResumeLayout(false);
this.menuStrip.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private GroupBox groupBoxTools;
@ -280,5 +333,11 @@
private Button buttonAddMap;
private Button buttonDeleteMap;
private ListBox listBoxMaps;
private MenuStrip menuStrip;
private ToolStripMenuItem fileToolStripMenuItem;
private ToolStripMenuItem saveToolStripMenuItem;
private ToolStripMenuItem loadToolStripMenuItem;
private OpenFileDialog loadFileDialog;
private SaveFileDialog saveFileDialog;
}
}

View File

@ -65,7 +65,6 @@ namespace ProjectLocomotive
{
return;
}
if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + new DrawningObject(locomotive) != -1)
{
MessageBox.Show("Объект добавлен");
@ -75,7 +74,6 @@ namespace ProjectLocomotive
{
MessageBox.Show("Ошибка при добавлении объекта");
}
}
/// Удаление объекта
private void buttonRemoveLocomotive_Click(object sender, EventArgs e)
@ -184,5 +182,36 @@ namespace ProjectLocomotive
_mapsCollection.AddMap(textBoxNewMapName.Text, _mapsDict[comboBoxSelectorMap.Text]);
ReloadMaps();
}
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
if (_mapsCollection.SaveData(saveFileDialog.FileName))
{
MessageBox.Show("Saved successfully", "Result", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("Saving failed", "Result", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void loadToolStripMenuItem_Click(object sender, EventArgs e)
{
if (loadFileDialog.ShowDialog() == DialogResult.OK)
{
if (_mapsCollection.LoadData(loadFileDialog.FileName))
{
MessageBox.Show("Loaded successfully", "Result", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("Loading failed", "Result", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
ReloadMaps();
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
}
}
}

View File

@ -137,7 +137,7 @@ namespace ProjectLocomotive
int curHeight = 0;
foreach (var locomotive in _setLocomotives.GetLocomotives())
{
locomotive?.SetObject(curWidth * _placeSizeWidth + 10, curHeight * _placeSizeHeight + 560, _pictureWidth, _pictureHeight);
locomotive?.SetObject(curWidth * _placeSizeWidth + 10, curHeight * _placeSizeHeight + 380, _pictureWidth, _pictureHeight);
locomotive?.DrawningObject(g);
if (curWidth < width) curWidth++;
else

View File

@ -41,7 +41,7 @@ namespace Locomotive
// Логика для удаления
if (_mapStorages.ContainsKey(name)) _mapStorages.Remove(name);
}
/// Доступ к парковке
/// Доступ к депо
public MapWithSetLocomotivesGeneric<IDrawningObject, AbstractMap> this[string ind]
{
get
@ -51,12 +51,6 @@ namespace Locomotive
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)
{
@ -65,46 +59,42 @@ namespace Locomotive
File.Delete(filename);
}
using (FileStream fs = new(filename, FileMode.Create))
using (StreamWriter sw = new StreamWriter(fs, Encoding.UTF8))
{
WriteToFile($"MapsCollection{Environment.NewLine}", fs);
sw.WriteLine("MapsCollection");
foreach (var storage in _mapStorages)
{
WriteToFile($"{storage.Key}{separatorDict}{storage.Value.GetData(separatorDict, separatorData)}{Environment.NewLine}", fs);
sw.WriteLine(
$"{storage.Key}{separatorDict}{storage.Value.GetData(separatorDict, separatorData)}"
);
}
}
return true;
}
/// Загрузка информации по локомотивам на парковках из файла
/// Загрузка информации по локомотивам в депо из файла
public bool LoadData(string filename)
{
if (!File.Exists(filename))
{
return false;
}
string bufferTextFromFile = "";
using (FileStream fs = new(filename, FileMode.Open))
using (StreamReader sr = new StreamReader(fs, Encoding.UTF8))
{
byte[] b = new byte[fs.Length];
UTF8Encoding temp = new(true);
while (fs.Read(b, 0, b.Length) > 0)
string curLine = sr.ReadLine();
if (!curLine.Contains("MapsCollection"))
{
bufferTextFromFile += temp.GetString(b);
}
}
var strs = bufferTextFromFile.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
if (!strs[0].Contains("MapsCollection"))
{
//если нет такой записи, то это не те данные
return false;
}
//очищаем записи
_mapStorages.Clear();
for (int i = 1; i < strs.Length; ++i)
while ((curLine = sr.ReadLine()) != null)
{
var elem = strs[i].Split(separatorDict);
var elems = curLine.Split(separatorDict);
AbstractMap map = null;
switch (elem[1])
switch (elems[1])
{
case "Simple Map":
map = new SimpleMap();
@ -116,10 +106,12 @@ namespace Locomotive
map = new RailroadMap();
break;
}
_mapStorages.Add(elem[0], new MapWithSetLocomotivesGeneric<IDrawningObject, AbstractMap>(_pictureWidth, _pictureHeight, map));
_mapStorages[elem[0]].LoadData(elem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries));
_mapStorages.Add(elems[0], new MapWithSetLocomotivesGeneric<IDrawningObject, AbstractMap>(_pictureWidth, _pictureHeight, map));
_mapStorages[elems[0]].LoadData(elems[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries));
}
return true;
}
}
}
}