This commit is contained in:
ValAnn 2023-11-23 17:35:58 +04:00
parent dbace1908e
commit 80d8e17d1e
12 changed files with 278 additions and 22 deletions

View File

@ -13,6 +13,7 @@ namespace DumpTruck.Generics
where T : DrawningCar where T : DrawningCar
where U : IMoveableObject where U : IMoveableObject
{ {
public IEnumerable<T?> GetCars => _collection.GetCars();
/// <summary> /// <summary>
/// Ширина окна прорисовки /// Ширина окна прорисовки
/// </summary> /// </summary>

View File

@ -10,6 +10,18 @@ namespace DumpTruck.Generics
{ {
internal class CarsGenericStorage internal class CarsGenericStorage
{ {
/// <summary>
/// Разделитель для записи ключа и значения элемента словаря
/// </summary>
private static readonly char _separatorForKeyValue = '|';
/// <summary>
/// Разделитель для записей коллекции данных в файл
/// </summary>
private readonly char _separatorRecords = ';';
/// <summary>
/// Разделитель для записи информации по объекту в файл
/// </summary>
private static readonly char _separatorForObject = ':';
/// <summary> /// <summary>
/// Словарь (хранилище) /// Словарь (хранилище)
/// </summary> /// </summary>
@ -78,7 +90,98 @@ namespace DumpTruck.Generics
} }
} }
public bool SaveData(string filename)
{
if (File.Exists(filename))
{
File.Delete(filename);
}
StringBuilder data = new();
foreach (KeyValuePair<string,
CarsGenericCollection<DrawningCar, DrawningObjectCar>> record in _carStorages)
{
StringBuilder records = new();
foreach (DrawningCar? elem in record.Value.GetCars)
{
records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}");
}
data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}");
}
if (data.Length == 0)
{
return false;
}
string toWrite = $"CarStorage{Environment.NewLine}{data}";
var strs = toWrite.Split(new char[] { '\n', '\r' },
StringSplitOptions.RemoveEmptyEntries);
using (StreamWriter sw = new(filename))
{
foreach (var str in strs)
{
sw.WriteLine(str);
}
}
return true;
}
public bool LoadData(string filename)
{
{
if (!File.Exists(filename))
{
return false;
}
using (StreamReader sr = new(filename))
{
string str = sr.ReadLine();
var strs = str.Split(new char[] { '\n', '\r' },
StringSplitOptions.RemoveEmptyEntries);
if (strs == null || strs.Length == 0)
{
return false;
}
if (!strs[0].StartsWith("CarStorage"))
{
return false;
}
_carStorages.Clear();
do
{
string[] record = str.Split(_separatorForKeyValue,
StringSplitOptions.RemoveEmptyEntries);
if (record.Length != 2)
{
str = sr.ReadLine();
continue;
}
CarsGenericCollection<DrawningCar, DrawningObjectCar>
collection = new(_pictureWidth, _pictureHeight);
string[] set = record[1].Split(_separatorRecords,
StringSplitOptions.RemoveEmptyEntries);
foreach (string elem in set)
{
DrawningCar? car = elem?.CreateDrawningCar(_separatorForObject, _pictureWidth, _pictureHeight);
if (car != null)
{
if (collection + car == -1)
{
return false;
}
}
}
_carStorages.Add(record[0], collection);
str = sr.ReadLine();
} while (str != null);
}
return true;
}
}
} }

View File

@ -72,13 +72,10 @@ namespace DumpTruck.DrawningObjects
public void setAddColor(Color color) public void setAddColor(Color color)
{ {
if (EntityCar is EntityDumpTruck dumpTruck) ((EntityDumpTruck)EntityCar).AdditionalColor = color;
{
dumpTruck.setAddColor(color);
} }
} }
}
} }

View File

@ -154,11 +154,12 @@ width, int height, int carWidth, int carHeight)
} }
public void setColor(Color color) public void setColor(Color color)
{ {
EntityCar.setColor(color); if (EntityCar== null)
return;
EntityCar.BodyColor = color;
} }
} }
} }

View File

@ -19,7 +19,7 @@ namespace DumpTruck.Entities
/// <summary> /// <summary>
/// Основной цвет /// Основной цвет
/// </summary> /// </summary>
public Color BodyColor { get; private set; } public Color BodyColor { get; set; }
/// <summary> /// <summary>
/// Шаг перемещения автомобиля /// Шаг перемещения автомобиля
/// </summary> /// </summary>

View File

@ -15,7 +15,7 @@ namespace DumpTruck.Entities
/// <summary> /// <summary>
/// Дополнительный цвет (для опциональных элементов) /// Дополнительный цвет (для опциональных элементов)
/// </summary> /// </summary>
public Color AdditionalColor { get; private set; } public Color AdditionalColor { get; set; }
/// <summary> /// <summary>
/// Признак (опция) наличия кузова /// Признак (опция) наличия кузова

View File

@ -0,0 +1,55 @@
using DumpTruck.DrawningObjects;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DumpTruck.Entities;
namespace DumpTruck
{
public static class ExtentionDrawningCar
{
public static DrawningCar? CreateDrawningCar(this string info, char
separatorForObject, int width, int height)
{
string[] strs = info.Split(separatorForObject);
if (strs.Length == 3)
{
return new DrawningCar(Convert.ToInt32(strs[0]),
Convert.ToInt32(strs[1]), Color.FromName(strs[2]), width, height);
}
if (strs.Length == 6)
{
return new DrawningDumpTruck(Convert.ToInt32(strs[0]),
Convert.ToInt32(strs[1]),
Color.FromName(strs[2]),
Color.FromName(strs[3]),
Convert.ToBoolean(strs[4]),
Convert.ToBoolean(strs[5]), width, height);
}
return null;
}
public static string GetDataForSave(this DrawningCar drawningCar,
char separatorForObject)
{
var car = drawningCar.EntityCar;
if (car == null)
{
return string.Empty;
}
var str = $"{car.Speed}{separatorForObject}{car.Weight}{separatorForObject}{car.BodyColor.Name}";
if (car is not EntityDumpTruck dumpTruck)
{
return str;
}
return
$"{str}{separatorForObject}{dumpTruck.AdditionalColor.Name}" +
$"{separatorForObject}{dumpTruck.BodyKit}{separatorForObject}{dumpTruck.Tent}";
}
}
}

View File

@ -40,17 +40,23 @@
this.textBoxStorageName = new System.Windows.Forms.TextBox(); this.textBoxStorageName = new System.Windows.Forms.TextBox();
this.listBoxStorages = new System.Windows.Forms.ListBox(); this.listBoxStorages = new System.Windows.Forms.ListBox();
this.ButtonAddObject = new System.Windows.Forms.Button(); this.ButtonAddObject = new System.Windows.Forms.Button();
this.menuStrip = new System.Windows.Forms.MenuStrip();
this.LoadToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.SaveToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.openFileDialog = new System.Windows.Forms.OpenFileDialog();
this.saveFileDialog = new System.Windows.Forms.SaveFileDialog();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).BeginInit();
this.panelCollection.SuspendLayout(); this.panelCollection.SuspendLayout();
this.panel1.SuspendLayout(); this.panel1.SuspendLayout();
this.panelObjects.SuspendLayout(); this.panelObjects.SuspendLayout();
this.menuStrip.SuspendLayout();
this.SuspendLayout(); this.SuspendLayout();
// //
// pictureBoxCollection // pictureBoxCollection
// //
this.pictureBoxCollection.Location = new System.Drawing.Point(0, 0); this.pictureBoxCollection.Location = new System.Drawing.Point(0, 27);
this.pictureBoxCollection.Name = "pictureBoxCollection"; this.pictureBoxCollection.Name = "pictureBoxCollection";
this.pictureBoxCollection.Size = new System.Drawing.Size(630, 482); this.pictureBoxCollection.Size = new System.Drawing.Size(630, 455);
this.pictureBoxCollection.TabIndex = 0; this.pictureBoxCollection.TabIndex = 0;
this.pictureBoxCollection.TabStop = false; this.pictureBoxCollection.TabStop = false;
// //
@ -161,6 +167,40 @@
this.ButtonAddObject.UseVisualStyleBackColor = true; this.ButtonAddObject.UseVisualStyleBackColor = true;
this.ButtonAddObject.Click += new System.EventHandler(this.ButtonAddObject_Click); this.ButtonAddObject.Click += new System.EventHandler(this.ButtonAddObject_Click);
// //
// menuStrip
//
this.menuStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.LoadToolStripMenuItem,
this.SaveToolStripMenuItem});
this.menuStrip.Location = new System.Drawing.Point(0, 0);
this.menuStrip.Name = "menuStrip";
this.menuStrip.Size = new System.Drawing.Size(848, 24);
this.menuStrip.TabIndex = 2;
this.menuStrip.Text = "Файл";
//
// LoadToolStripMenuItem
//
this.LoadToolStripMenuItem.Name = "LoadToolStripMenuItem";
this.LoadToolStripMenuItem.Size = new System.Drawing.Size(67, 20);
this.LoadToolStripMenuItem.Text = "Загрузка";
this.LoadToolStripMenuItem.Click += new System.EventHandler(this.LoadToolStripMenuItem_Click);
//
// SaveToolStripMenuItem
//
this.SaveToolStripMenuItem.Name = "SaveToolStripMenuItem";
this.SaveToolStripMenuItem.Size = new System.Drawing.Size(86, 20);
this.SaveToolStripMenuItem.Text = "Сохранение";
this.SaveToolStripMenuItem.Click += new System.EventHandler(this.SaveToolStripMenuItem_Click);
//
// openFileDialog
//
this.openFileDialog.FileName = "openFileDialog1";
this.openFileDialog.Filter = "txt file | *.txt";
//
// saveFileDialog
//
this.saveFileDialog.Filter = "txt file | *.txt";
//
// FormCarCollection // FormCarCollection
// //
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
@ -168,6 +208,8 @@
this.ClientSize = new System.Drawing.Size(848, 482); this.ClientSize = new System.Drawing.Size(848, 482);
this.Controls.Add(this.panelCollection); this.Controls.Add(this.panelCollection);
this.Controls.Add(this.pictureBoxCollection); this.Controls.Add(this.pictureBoxCollection);
this.Controls.Add(this.menuStrip);
this.MainMenuStrip = this.menuStrip;
this.Name = "FormCarCollection"; this.Name = "FormCarCollection";
this.Text = "Набор грузовиков"; this.Text = "Набор грузовиков";
((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).EndInit();
@ -176,7 +218,10 @@
this.panel1.PerformLayout(); this.panel1.PerformLayout();
this.panelObjects.ResumeLayout(false); this.panelObjects.ResumeLayout(false);
this.panelObjects.PerformLayout(); this.panelObjects.PerformLayout();
this.menuStrip.ResumeLayout(false);
this.menuStrip.PerformLayout();
this.ResumeLayout(false); this.ResumeLayout(false);
this.PerformLayout();
} }
@ -194,5 +239,10 @@
private Panel panel1; private Panel panel1;
private Button ButtonDelObject_; private Button ButtonDelObject_;
private TextBox textBoxStorageName; private TextBox textBoxStorageName;
private MenuStrip menuStrip;
private ToolStripMenuItem LoadToolStripMenuItem;
private ToolStripMenuItem SaveToolStripMenuItem;
private OpenFileDialog openFileDialog;
private SaveFileDialog saveFileDialog;
} }
} }

View File

@ -181,6 +181,46 @@ MessageBoxIcon.Question) == DialogResult.Yes)
{ {
} }
private void SaveToolStripMenuItem_Click(object sender, EventArgs e)
{
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
if (_storage.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 (_storage.LoadData(openFileDialog.FileName))
{
MessageBox.Show("Загрузка прошла успешно",
"Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
foreach (var collection in _storage.Keys)
{
listBoxStorages.Items.Add(collection);
}
}
else
{
MessageBox.Show("Не загрузилось", "Результат",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
} }
} }

View File

@ -57,4 +57,13 @@
<resheader name="writer"> <resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader> </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> </root>

View File

@ -130,7 +130,7 @@
// panelPurple // panelPurple
// //
this.panelPurple.AllowDrop = true; this.panelPurple.AllowDrop = true;
this.panelPurple.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(128)))), ((int)(((byte)(255))))); this.panelPurple.BackColor = System.Drawing.Color.Fuchsia;
this.panelPurple.Location = new System.Drawing.Point(156, 106); this.panelPurple.Location = new System.Drawing.Point(156, 106);
this.panelPurple.Name = "panelPurple"; this.panelPurple.Name = "panelPurple";
this.panelPurple.Size = new System.Drawing.Size(57, 53); this.panelPurple.Size = new System.Drawing.Size(57, 53);
@ -139,7 +139,7 @@
// panelLightBlue // panelLightBlue
// //
this.panelLightBlue.AllowDrop = true; this.panelLightBlue.AllowDrop = true;
this.panelLightBlue.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(128)))), ((int)(((byte)(255)))), ((int)(((byte)(255))))); this.panelLightBlue.BackColor = System.Drawing.Color.Cyan;
this.panelLightBlue.Location = new System.Drawing.Point(13, 106); this.panelLightBlue.Location = new System.Drawing.Point(13, 106);
this.panelLightBlue.Name = "panelLightBlue"; this.panelLightBlue.Name = "panelLightBlue";
this.panelLightBlue.Size = new System.Drawing.Size(57, 53); this.panelLightBlue.Size = new System.Drawing.Size(57, 53);
@ -148,7 +148,7 @@
// panelYellow // panelYellow
// //
this.panelYellow.AllowDrop = true; this.panelYellow.AllowDrop = true;
this.panelYellow.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(255)))), ((int)(((byte)(128))))); this.panelYellow.BackColor = System.Drawing.Color.Yellow;
this.panelYellow.Location = new System.Drawing.Point(156, 35); this.panelYellow.Location = new System.Drawing.Point(156, 35);
this.panelYellow.Name = "panelYellow"; this.panelYellow.Name = "panelYellow";
this.panelYellow.Size = new System.Drawing.Size(57, 53); this.panelYellow.Size = new System.Drawing.Size(57, 53);
@ -157,7 +157,7 @@
// panelBlue // panelBlue
// //
this.panelBlue.AllowDrop = true; this.panelBlue.AllowDrop = true;
this.panelBlue.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(128)))), ((int)(((byte)(128)))), ((int)(((byte)(255))))); this.panelBlue.BackColor = System.Drawing.Color.Blue;
this.panelBlue.Location = new System.Drawing.Point(86, 106); this.panelBlue.Location = new System.Drawing.Point(86, 106);
this.panelBlue.Name = "panelBlue"; this.panelBlue.Name = "panelBlue";
this.panelBlue.Size = new System.Drawing.Size(57, 53); this.panelBlue.Size = new System.Drawing.Size(57, 53);
@ -175,7 +175,7 @@
// panelOrange // panelOrange
// //
this.panelOrange.AllowDrop = true; this.panelOrange.AllowDrop = true;
this.panelOrange.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(192)))), ((int)(((byte)(128))))); this.panelOrange.BackColor = System.Drawing.Color.Orange;
this.panelOrange.Location = new System.Drawing.Point(86, 35); this.panelOrange.Location = new System.Drawing.Point(86, 35);
this.panelOrange.Name = "panelOrange"; this.panelOrange.Name = "panelOrange";
this.panelOrange.Size = new System.Drawing.Size(57, 53); this.panelOrange.Size = new System.Drawing.Size(57, 53);
@ -184,7 +184,7 @@
// panelRed // panelRed
// //
this.panelRed.AllowDrop = true; this.panelRed.AllowDrop = true;
this.panelRed.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(128)))), ((int)(((byte)(128))))); this.panelRed.BackColor = System.Drawing.Color.Red;
this.panelRed.Location = new System.Drawing.Point(13, 35); this.panelRed.Location = new System.Drawing.Point(13, 35);
this.panelRed.Name = "panelRed"; this.panelRed.Name = "panelRed";
this.panelRed.Size = new System.Drawing.Size(57, 53); this.panelRed.Size = new System.Drawing.Size(57, 53);

View File

@ -62,7 +62,7 @@ DragDropEffects.Move | DragDropEffects.Copy);
if (_car is DrawningCar car) if (_car is DrawningCar car)
{ {
labelColor.BackColor = (Color)e.Data.GetData(typeof(Color)); labelColor.BackColor = (Color)e.Data.GetData(typeof(Color));
_car.setColor((Color)e.Data.GetData(typeof(Color))); _car.setColor(labelColor.BackColor);
} }
DrawCar(); DrawCar();
} }
@ -73,7 +73,7 @@ DragDropEffects.Move | DragDropEffects.Copy);
if (_car is DrawningDumpTruck car) if (_car is DrawningDumpTruck car)
{ {
labelAddColor.BackColor = (Color)e.Data.GetData(typeof(Color)); labelAddColor.BackColor = (Color)e.Data.GetData(typeof(Color));
((DrawningDumpTruck)_car).setAddColor((Color)e.Data.GetData(typeof(Color))); ((DrawningDumpTruck)_car).setAddColor(labelAddColor.BackColor);
} }
DrawCar(); DrawCar();
} }