лаба 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); _loc.DrawTransport(g);
} }
public string getInfo() => _loc?.getDataForSave(); public string getInfo() => _loc?.getDataForSave();
public static IDrawningObject Create(string data) => new DrawningObject(data.createDrawningLocomotive()); public static IDrawningObject Create(string data) => new DrawningObject(data.createDrawningLocomotive());
} }
} }

View File

@ -44,9 +44,16 @@
this.maskedTextBoxPosition = new System.Windows.Forms.MaskedTextBox(); this.maskedTextBoxPosition = new System.Windows.Forms.MaskedTextBox();
this.buttonAddLocomotive = new System.Windows.Forms.Button(); this.buttonAddLocomotive = new System.Windows.Forms.Button();
this.pictureBox = new System.Windows.Forms.PictureBox(); 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.groupBoxTools.SuspendLayout();
this.groupBox1.SuspendLayout(); this.groupBox1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit();
this.menuStrip.SuspendLayout();
this.SuspendLayout(); this.SuspendLayout();
// //
// groupBoxTools // groupBoxTools
@ -62,11 +69,11 @@
this.groupBoxTools.Controls.Add(this.maskedTextBoxPosition); this.groupBoxTools.Controls.Add(this.maskedTextBoxPosition);
this.groupBoxTools.Controls.Add(this.buttonAddLocomotive); this.groupBoxTools.Controls.Add(this.buttonAddLocomotive);
this.groupBoxTools.Dock = System.Windows.Forms.DockStyle.Right; 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.Margin = new System.Windows.Forms.Padding(4);
this.groupBoxTools.Name = "groupBoxTools"; this.groupBoxTools.Name = "groupBoxTools";
this.groupBoxTools.Padding = new System.Windows.Forms.Padding(4); 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.TabIndex = 0;
this.groupBoxTools.TabStop = false; this.groupBoxTools.TabStop = false;
this.groupBoxTools.Text = "Инструменты"; this.groupBoxTools.Text = "Инструменты";
@ -238,20 +245,63 @@
// pictureBox // pictureBox
// //
this.pictureBox.Dock = System.Windows.Forms.DockStyle.Fill; 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.Margin = new System.Windows.Forms.Padding(4);
this.pictureBox.Name = "pictureBox"; 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.TabIndex = 1;
this.pictureBox.TabStop = false; 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 // FormMapWithSetLocomotives
// //
this.AutoScaleDimensions = new System.Drawing.SizeF(10F, 25F); this.AutoScaleDimensions = new System.Drawing.SizeF(10F, 25F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 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.pictureBox);
this.Controls.Add(this.groupBoxTools); this.Controls.Add(this.groupBoxTools);
this.Controls.Add(this.menuStrip);
this.MainMenuStrip = this.menuStrip;
this.Margin = new System.Windows.Forms.Padding(4); this.Margin = new System.Windows.Forms.Padding(4);
this.Name = "FormMapWithSetLocomotives"; this.Name = "FormMapWithSetLocomotives";
this.Text = "Карта с набором объектов"; this.Text = "Карта с набором объектов";
@ -260,7 +310,10 @@
this.groupBox1.ResumeLayout(false); this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout(); this.groupBox1.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit();
this.menuStrip.ResumeLayout(false);
this.menuStrip.PerformLayout();
this.ResumeLayout(false); this.ResumeLayout(false);
this.PerformLayout();
} }
#endregion #endregion
private GroupBox groupBoxTools; private GroupBox groupBoxTools;
@ -280,5 +333,11 @@
private Button buttonAddMap; private Button buttonAddMap;
private Button buttonDeleteMap; private Button buttonDeleteMap;
private ListBox listBoxMaps; 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; return;
} }
if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + new DrawningObject(locomotive) != -1) if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + new DrawningObject(locomotive) != -1)
{ {
MessageBox.Show("Объект добавлен"); MessageBox.Show("Объект добавлен");
@ -75,7 +74,6 @@ namespace ProjectLocomotive
{ {
MessageBox.Show("Ошибка при добавлении объекта"); MessageBox.Show("Ошибка при добавлении объекта");
} }
} }
/// Удаление объекта /// Удаление объекта
private void buttonRemoveLocomotive_Click(object sender, EventArgs e) private void buttonRemoveLocomotive_Click(object sender, EventArgs e)
@ -184,5 +182,36 @@ namespace ProjectLocomotive
_mapsCollection.AddMap(textBoxNewMapName.Text, _mapsDict[comboBoxSelectorMap.Text]); _mapsCollection.AddMap(textBoxNewMapName.Text, _mapsDict[comboBoxSelectorMap.Text]);
ReloadMaps(); 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; int curHeight = 0;
foreach (var locomotive in _setLocomotives.GetLocomotives()) 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); locomotive?.DrawningObject(g);
if (curWidth < width) curWidth++; if (curWidth < width) curWidth++;
else else

View File

@ -41,7 +41,7 @@ namespace Locomotive
// Логика для удаления // Логика для удаления
if (_mapStorages.ContainsKey(name)) _mapStorages.Remove(name); if (_mapStorages.ContainsKey(name)) _mapStorages.Remove(name);
} }
/// Доступ к парковке /// Доступ к депо
public MapWithSetLocomotivesGeneric<IDrawningObject, AbstractMap> this[string ind] public MapWithSetLocomotivesGeneric<IDrawningObject, AbstractMap> this[string ind]
{ {
get get
@ -51,12 +51,6 @@ namespace Locomotive
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) public bool SaveData(string filename)
{ {
@ -65,46 +59,42 @@ namespace Locomotive
File.Delete(filename); File.Delete(filename);
} }
using (FileStream fs = new(filename, FileMode.Create)) 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) 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; return true;
} }
/// Загрузка информации по локомотивам на парковках из файла /// Загрузка информации по локомотивам в депо из файла
public bool LoadData(string filename) public bool LoadData(string filename)
{ {
if (!File.Exists(filename)) if (!File.Exists(filename))
{ {
return false; return false;
} }
string bufferTextFromFile = "";
using (FileStream fs = new(filename, FileMode.Open)) using (FileStream fs = new(filename, FileMode.Open))
using (StreamReader sr = new StreamReader(fs, Encoding.UTF8))
{ {
byte[] b = new byte[fs.Length]; string curLine = sr.ReadLine();
UTF8Encoding temp = new(true);
while (fs.Read(b, 0, b.Length) > 0) 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; return false;
} }
//очищаем записи
_mapStorages.Clear(); _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; AbstractMap map = null;
switch (elem[1])
switch (elems[1])
{ {
case "Simple Map": case "Simple Map":
map = new SimpleMap(); map = new SimpleMap();
@ -116,10 +106,12 @@ namespace Locomotive
map = new RailroadMap(); map = new RailroadMap();
break; 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; return true;
} }
} }
}
} }