This commit is contained in:
kirin 2023-11-28 12:26:51 +04:00
parent 1834031847
commit cb38f8505d
11 changed files with 268 additions and 19 deletions

View File

@ -4,16 +4,16 @@ namespace ElectricLocomotive;
public class DrawingElectricLocomotiv : DrawingLocomotiv
{
private bool isBattery;
private bool isRoga;
public DrawingElectricLocomotiv(bool isBattery, bool isRoga, int speed, double weight, int width, int height, Color mainColor, Color dopColor, Color batteryColor, Color rogaColor) : base(speed, weight, width,
public bool isBattery;
public bool isRoga;
public DrawingElectricLocomotiv(int speed, double weight, int width, int height, Color mainColor, Color dopColor, Color batteryColor, Color rogaColor,bool isBattery, bool isRoga) : base(speed, weight, width,
height, mainColor, dopColor)
{
this.isBattery = isBattery;
this.isRoga = isRoga;
if (EntityLocomotiv != null)
{
EntityLocomotiv = new EntityElectricLocomotiv(speed, weight, batteryColor, rogaColor, mainColor, dopColor);
EntityLocomotiv = new EntityElectricLocomotiv(speed, weight, batteryColor, rogaColor, mainColor, dopColor, isRoga, isBattery);
}
}

View File

@ -0,0 +1,53 @@
namespace ElectricLocomotive;
public static class ExtentionDrawingLocomotiv
{
public static DrawingLocomotiv? CreateDrawingLoco(this string info, char
separatorForObject, int width, int height)
{
string[] strs = info.Split(separatorForObject);
if (strs.Length == 4)
{
return new DrawingLocomotiv(Convert.ToInt32(strs[0]),
Convert.ToInt32(strs[1]), width, height,Color.FromName(strs[2]), Color.FromName(strs[3]));
}
if (strs.Length == 8)
{
return new DrawingElectricLocomotiv(
Convert.ToInt32(strs[0]),
Convert.ToInt32(strs[1]),
width, height,
Color.FromName(strs[2]),
Color.FromName(strs[3]),
Color.FromName(strs[4]),
Color.FromName(strs[5]),
Convert.ToBoolean(strs[6]),
Convert.ToBoolean(strs[7])
);
}
return null;
}
public static string GetDataForSave(this DrawingLocomotiv drawingLoco,
char separatorForObject)
{
var loco = drawingLoco.EntityLocomotiv;
if (loco == null)
{
return string.Empty;
}
var str =
$"{loco.Speed}{separatorForObject}{loco.Weight}" +
$"{separatorForObject}{loco.ColorBody.Name}" +
$"{separatorForObject}{loco.ColorWindow.Name}";
if (loco is not EntityElectricLocomotiv electricLocomotiv)
{
return str;
}
return
$"{str}{separatorForObject}{electricLocomotiv.BatteryColor.Name}{separatorForObject}" +
$"{electricLocomotiv.RogaColor.Name}{separatorForObject}" +
$"{electricLocomotiv.isBattery}{separatorForObject}{electricLocomotiv.isRoga}";
}
}

View File

@ -4,9 +4,13 @@ public class EntityElectricLocomotiv : EntityLocomotiv
{
public Color BatteryColor { get; set; }
public Color RogaColor { get; set; }
public EntityElectricLocomotiv(int speed,double weight, Color batteryColor, Color rogaColor, Color mainColor, Color dopColor) : base(speed, weight, mainColor, dopColor)
public bool isRoga;
public bool isBattery;
public EntityElectricLocomotiv(int speed,double weight, Color batteryColor, Color rogaColor, Color mainColor, Color dopColor, bool isRoga, bool isBattery) : base(speed, weight, mainColor, dopColor)
{
BatteryColor = batteryColor;
RogaColor = rogaColor;
this.isRoga = isRoga;
this.isBattery = isBattery;
}
}

View File

@ -189,7 +189,7 @@
//
// panelRed
//
panelRed.BackColor = Color.FromArgb(192, 0, 0);
panelRed.BackColor = Color.Red;
panelRed.Location = new Point(6, 38);
panelRed.Name = "panelRed";
panelRed.Size = new Size(60, 56);

View File

@ -73,9 +73,8 @@ DragDropEffects.Move | DragDropEffects.Copy);
pictureBox.Height, Color.Black, Color.Yellow);
break;
case "labelAdvanced":
_loco = new DrawingElectricLocomotiv(checkBoxRoga.Checked, checkBoxBattery.Checked,
(int)numericUpDownSpeed.Value, (int)numericUpDownWeight.Value, pictureBox.Width,
pictureBox.Height, Color.Black, Color.Yellow, Color.Aqua, Color.Black);
_loco = new DrawingElectricLocomotiv((int)numericUpDownSpeed.Value, (int)numericUpDownWeight.Value, pictureBox.Width,
pictureBox.Height, Color.Black, Color.Yellow, Color.Aqua, Color.Black,checkBoxRoga.Checked, checkBoxBattery.Checked);
break;
}
labelColor.BackColor = Color.Empty;

View File

@ -43,7 +43,7 @@ namespace ElectricLocomotive {
if (dialog.ShowDialog() == DialogResult.OK)
dopColor = dialog.Color;
_drawingLocomotiv = new DrawingElectricLocomotiv(true, true, random.Next(100, 300), random.Next(1000, 3000), locoBox.Width, locoBox.Height, color, dopColor, batteryColor, rogaColor);
_drawingLocomotiv = new DrawingElectricLocomotiv(random.Next(100, 300), random.Next(1000, 3000), locoBox.Width, locoBox.Height, color, dopColor, batteryColor, rogaColor,true, true);
_drawingLocomotiv.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw();
}

View File

@ -38,9 +38,16 @@ partial class FormLocomotivCollection {
locoIndexInput = new TextBox();
addLocomotiv = new Button();
collectionPictureBox = new PictureBox();
menuStrip1 = new MenuStrip();
toolStripMenuItem = new ToolStripMenuItem();
SaveToolStripMenuItem = new ToolStripMenuItem();
LoadToolStripMenuItem = new ToolStripMenuItem();
loadFileDialog = new OpenFileDialog();
saveFileDialog = new SaveFileDialog();
toolsBox.SuspendLayout();
collectionGroupBoxes.SuspendLayout();
((ISupportInitialize)collectionPictureBox).BeginInit();
menuStrip1.SuspendLayout();
SuspendLayout();
//
// toolsBox
@ -146,12 +153,52 @@ partial class FormLocomotivCollection {
//
// collectionPictureBox
//
collectionPictureBox.Location = new Point(12, 12);
collectionPictureBox.Location = new Point(12, 38);
collectionPictureBox.Name = "collectionPictureBox";
collectionPictureBox.Size = new Size(933, 707);
collectionPictureBox.Size = new Size(933, 681);
collectionPictureBox.TabIndex = 1;
collectionPictureBox.TabStop = false;
//
// menuStrip1
//
menuStrip1.ImageScalingSize = new Size(20, 20);
menuStrip1.Items.AddRange(new ToolStripItem[] { toolStripMenuItem });
menuStrip1.Location = new Point(0, 0);
menuStrip1.Name = "menuStrip1";
menuStrip1.Size = new Size(1265, 28);
menuStrip1.TabIndex = 3;
menuStrip1.Text = "menuStrip1";
//
// toolStripMenuItem
//
toolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { SaveToolStripMenuItem, LoadToolStripMenuItem });
toolStripMenuItem.Name = "toolStripMenuItem";
toolStripMenuItem.Size = new Size(59, 24);
toolStripMenuItem.Text = "Файл";
//
// SaveToolStripMenuItem
//
SaveToolStripMenuItem.Name = "SaveToolStripMenuItem";
SaveToolStripMenuItem.Size = new Size(166, 26);
SaveToolStripMenuItem.Text = "Сохранить";
SaveToolStripMenuItem.Click += SaveToolStripMenuItem_Click;
//
// LoadToolStripMenuItem
//
LoadToolStripMenuItem.Name = "LoadToolStripMenuItem";
LoadToolStripMenuItem.Size = new Size(166, 26);
LoadToolStripMenuItem.Text = "Загрузить";
LoadToolStripMenuItem.Click += LoadToolStripMenuItem_Click;
//
// loadFileDialog
//
loadFileDialog.FileName = "openFileDialog4";
loadFileDialog.Filter = "txt file | *.txt";
//
// saveFileDialog
//
saveFileDialog.Filter = "txt file | *.txt";
//
// FormLocomotivCollection
//
AutoScaleDimensions = new SizeF(8F, 20F);
@ -159,6 +206,8 @@ partial class FormLocomotivCollection {
ClientSize = new Size(1265, 731);
Controls.Add(collectionPictureBox);
Controls.Add(toolsBox);
Controls.Add(menuStrip1);
MainMenuStrip = menuStrip1;
Name = "FormLocomotivCollection";
Text = "Набор локомотивов";
toolsBox.ResumeLayout(false);
@ -166,7 +215,10 @@ partial class FormLocomotivCollection {
collectionGroupBoxes.ResumeLayout(false);
collectionGroupBoxes.PerformLayout();
((ISupportInitialize)collectionPictureBox).EndInit();
menuStrip1.ResumeLayout(false);
menuStrip1.PerformLayout();
ResumeLayout(false);
PerformLayout();
}
#endregion
@ -182,4 +234,10 @@ partial class FormLocomotivCollection {
private Button addStorageButton;
private TextBox storageIndexInput;
private ListBox storageListBox;
private MenuStrip menuStrip1;
private ToolStripMenuItem toolStripMenuItem;
private ToolStripMenuItem SaveToolStripMenuItem;
private ToolStripMenuItem LoadToolStripMenuItem;
private OpenFileDialog loadFileDialog;
private SaveFileDialog saveFileDialog;
}

View File

@ -1,4 +1,6 @@
namespace ElectricLocomotive;
using System.Windows.Forms;
namespace ElectricLocomotive;
public partial class FormLocomotivCollection : Form {
private readonly LocosGenericStorage _storage;
@ -34,14 +36,12 @@ public partial class FormLocomotivCollection : Form {
form.Show();
Action<DrawingLocomotiv>? monorailDelegate = new((m) => {
bool q = (obj + m);
if (q)
{
if (q) {
MessageBox.Show("Объект добавлен");
m.ChangePictureBoxSize(collectionPictureBox.Width, collectionPictureBox.Height);
collectionPictureBox.Image = obj.ShowLocos();
}
else
{
else {
MessageBox.Show("Не удалось добавить объект");
}
});
@ -108,4 +108,33 @@ public partial class FormLocomotivCollection : Form {
_storage.AddSet(storageIndexInput.Text);
ReloadObjects();
}
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 (loadFileDialog.ShowDialog() == DialogResult.OK) {
if (_storage.LoadData(loadFileDialog.FileName)) {
MessageBox.Show("Загрузка прошла успешно",
"Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
foreach (var collection in _storage.Keys) {
storageListBox.Items.Add(collection);
}
}
else {
MessageBox.Show("Не загрузилось", "Результат",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}

View File

@ -117,4 +117,13 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<metadata name="menuStrip1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
<metadata name="loadFileDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>323, 17</value>
</metadata>
<metadata name="saveFileDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>481, 17</value>
</metadata>
</root>

View File

@ -7,6 +7,7 @@ public class LocosGenericCollection <T, U> where T : DrawingLocomotiv where U :
private readonly int _placeSizeWidth = 170;
private readonly int _placeSizeHeight = 110;
private readonly SetGeneric<T> _collection;
public IEnumerable<T?> GetLocos => _collection.GetElectricLocos();
public LocosGenericCollection(int picWidth, int picHeight)
{
int width = picWidth / _placeSizeWidth;

View File

@ -1,6 +1,8 @@
namespace ElectricLocomotive;
using System.Text;
public class LocosGenericStorage
namespace ElectricLocomotive;
public class LocosGenericStorage
{
readonly Dictionary<string, LocosGenericCollection<DrawingLocomotiv, DrawingObjectLocomotiv>> _electricLocoStorages;
public List<string> Keys => _electricLocoStorages.Keys.ToList();
@ -8,6 +10,11 @@ public class LocosGenericStorage
private readonly int _pictureWidth;
private readonly int _pictureHeight;
private static readonly char _separatorForKeyValue = '|';
private readonly char _separatorRecords = ';';
private static readonly char _separatorForObject = ':';
public LocosGenericStorage(int pictureWidth, int pictureHeight)
{
@ -38,4 +45,93 @@ public class LocosGenericStorage
return null;
}
}
public bool SaveData(string filename)
{
if (File.Exists(filename))
{
File.Delete(filename);
}
StringBuilder data = new();
foreach (KeyValuePair<string,
LocosGenericCollection<DrawingLocomotiv, DrawingObjectLocomotiv>> record in _electricLocoStorages)
{
StringBuilder records = new();
foreach (DrawingLocomotiv? elem in record.Value.GetLocos)
{
records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}");
}
data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}");
}
if (data.Length == 0)
{
return false;
}
string toWrite = $"LocoStorage{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("LocoStorage"))
{
return false;
}
_electricLocoStorages.Clear();
do
{
string[] record = str.Split(_separatorForKeyValue,
StringSplitOptions.RemoveEmptyEntries);
if (record.Length != 2)
{
str = sr.ReadLine();
continue;
}
LocosGenericCollection<DrawingLocomotiv, DrawingObjectLocomotiv>
collection = new(_pictureWidth, _pictureHeight);
string[] set = record[1].Split(_separatorRecords,
StringSplitOptions.RemoveEmptyEntries);
foreach (string elem in set)
{
DrawingLocomotiv? monorail =
elem?.CreateDrawingLoco(_separatorForObject, _pictureWidth, _pictureHeight);
if (monorail != null)
{
if (!(collection + monorail))
{
return false;
}
}
}
_electricLocoStorages.Add(record[0], collection);
str = sr.ReadLine();
} while (str != null);
}
return true;
}
}