Compare commits

...

5 Commits

8 changed files with 241 additions and 8 deletions

View File

@ -36,5 +36,9 @@ namespace Artilleries
{
_artillery.DrawTransport(g);
}
public string GetInfo() => _artillery?.GetDateForSave();
public static IDrawingObject Create(string data) => new DrawingObjectArtillery(data.CreateDrawingArtillery());
}
}

View File

@ -0,0 +1,49 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Artilleries {
internal static class ExtensionArtillery
{
private static readonly char _separatorForObject = ':';
public static DrawingArtillery CreateDrawingArtillery(this string info)
{
string[] strs = info.Split(_separatorForObject);
if (strs.Length == 3)
{
return new DrawingArtillery(Convert.ToInt32(strs[0]), Convert.ToInt32(strs[1]), Color.FromName(strs[2]));
}
if (strs.Length == 6)
{
return new DrawingAdvancedArtillery(
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 GetDateForSave(this DrawingArtillery drawingArtillery)
{
var artillery = drawingArtillery.Artillery;
var str = $"{artillery.Speed}{_separatorForObject}{artillery.Weight}{_separatorForObject}{artillery.BodyColor.Name}";
if (artillery is not EntityAdvancedArtillery advanced)
{
return str;
}
return $"{str}{_separatorForObject}{advanced.DopColor.Name}{_separatorForObject}{advanced.Weapon}{_separatorForObject}{advanced.SalvoBattery}";
}
}
}

View File

@ -45,9 +45,16 @@
this.buttonUp = new System.Windows.Forms.Button();
this.buttonLeft = new System.Windows.Forms.Button();
this.pictureBoxArtilleries = new System.Windows.Forms.PictureBox();
this.menuStrip1 = 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.toolsGroupBox.SuspendLayout();
this.mapsGroupBox.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxArtilleries)).BeginInit();
this.menuStrip1.SuspendLayout();
this.SuspendLayout();
//
// toolsGroupBox
@ -63,9 +70,9 @@
this.toolsGroupBox.Controls.Add(this.buttonUp);
this.toolsGroupBox.Controls.Add(this.buttonLeft);
this.toolsGroupBox.Dock = System.Windows.Forms.DockStyle.Right;
this.toolsGroupBox.Location = new System.Drawing.Point(600, 0);
this.toolsGroupBox.Location = new System.Drawing.Point(600, 24);
this.toolsGroupBox.Name = "toolsGroupBox";
this.toolsGroupBox.Size = new System.Drawing.Size(200, 596);
this.toolsGroupBox.Size = new System.Drawing.Size(200, 572);
this.toolsGroupBox.TabIndex = 0;
this.toolsGroupBox.TabStop = false;
this.toolsGroupBox.Text = "Инструменты";
@ -186,7 +193,7 @@
this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonRight.BackgroundImage = global::SelfPropelledArtilleryUnit.Properties.Resources.ArrowRight;
this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.buttonRight.Location = new System.Drawing.Point(124, 545);
this.buttonRight.Location = new System.Drawing.Point(124, 521);
this.buttonRight.Name = "buttonRight";
this.buttonRight.Size = new System.Drawing.Size(30, 30);
this.buttonRight.TabIndex = 18;
@ -198,7 +205,7 @@
this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonDown.BackgroundImage = global::SelfPropelledArtilleryUnit.Properties.Resources.ArrowDown;
this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.buttonDown.Location = new System.Drawing.Point(88, 545);
this.buttonDown.Location = new System.Drawing.Point(88, 521);
this.buttonDown.Name = "buttonDown";
this.buttonDown.Size = new System.Drawing.Size(30, 30);
this.buttonDown.TabIndex = 17;
@ -210,7 +217,7 @@
this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonUp.BackgroundImage = global::SelfPropelledArtilleryUnit.Properties.Resources.ArrowUp;
this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.buttonUp.Location = new System.Drawing.Point(88, 512);
this.buttonUp.Location = new System.Drawing.Point(88, 488);
this.buttonUp.Name = "buttonUp";
this.buttonUp.Size = new System.Drawing.Size(30, 30);
this.buttonUp.TabIndex = 16;
@ -222,7 +229,7 @@
this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonLeft.BackgroundImage = global::SelfPropelledArtilleryUnit.Properties.Resources.ArrowLeft;
this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.buttonLeft.Location = new System.Drawing.Point(54, 545);
this.buttonLeft.Location = new System.Drawing.Point(54, 521);
this.buttonLeft.Name = "buttonLeft";
this.buttonLeft.Size = new System.Drawing.Size(30, 30);
this.buttonLeft.TabIndex = 15;
@ -232,12 +239,49 @@
// pictureBoxArtilleries
//
this.pictureBoxArtilleries.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBoxArtilleries.Location = new System.Drawing.Point(0, 0);
this.pictureBoxArtilleries.Location = new System.Drawing.Point(0, 24);
this.pictureBoxArtilleries.Name = "pictureBoxArtilleries";
this.pictureBoxArtilleries.Size = new System.Drawing.Size(600, 596);
this.pictureBoxArtilleries.Size = new System.Drawing.Size(600, 572);
this.pictureBoxArtilleries.TabIndex = 1;
this.pictureBoxArtilleries.TabStop = false;
//
// menuStrip1
//
this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.fileToolStripMenuItem});
this.menuStrip1.Location = new System.Drawing.Point(0, 0);
this.menuStrip1.Name = "menuStrip1";
this.menuStrip1.Size = new System.Drawing.Size(800, 24);
this.menuStrip1.TabIndex = 2;
this.menuStrip1.Text = "menuStrip1";
//
// 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(48, 20);
this.fileToolStripMenuItem.Text = "Файл";
//
// saveToolStripMenuItem
//
this.saveToolStripMenuItem.Name = "saveToolStripMenuItem";
this.saveToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
this.saveToolStripMenuItem.Text = "Сохранить";
this.saveToolStripMenuItem.Click += new System.EventHandler(this.saveToolStripMenuItem_Click);
//
// loadToolStripMenuItem
//
this.loadToolStripMenuItem.Name = "loadToolStripMenuItem";
this.loadToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
this.loadToolStripMenuItem.Text = "Загрузить";
this.loadToolStripMenuItem.Click += new System.EventHandler(this.loadToolStripMenuItem_Click);
//
// loadFileDialog
//
this.loadFileDialog.FileName = "openFileDialog1";
//
// FormMapWithSetArtilleries
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
@ -245,6 +289,8 @@
this.ClientSize = new System.Drawing.Size(800, 596);
this.Controls.Add(this.pictureBoxArtilleries);
this.Controls.Add(this.toolsGroupBox);
this.Controls.Add(this.menuStrip1);
this.MainMenuStrip = this.menuStrip1;
this.Name = "FormMapWithSetArtilleries";
this.Text = "Artillery";
this.toolsGroupBox.ResumeLayout(false);
@ -252,7 +298,10 @@
this.mapsGroupBox.ResumeLayout(false);
this.mapsGroupBox.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxArtilleries)).EndInit();
this.menuStrip1.ResumeLayout(false);
this.menuStrip1.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
@ -275,5 +324,11 @@
private ListBox listBoxMaps;
private Button buttonAddMap;
private TextBox textBoxNewMapName;
private MenuStrip menuStrip1;
private ToolStripMenuItem fileToolStripMenuItem;
private ToolStripMenuItem saveToolStripMenuItem;
private ToolStripMenuItem loadToolStripMenuItem;
private OpenFileDialog loadFileDialog;
private SaveFileDialog saveFileDialog;
}
}

View File

@ -177,5 +177,37 @@ namespace Artilleries
}
pictureBoxArtilleries.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 (loadFileDialog.ShowDialog() == DialogResult.OK)
{
if (_mapsCollection.LoadData(loadFileDialog.FileName))
{
MessageBox.Show("Загрузка прошла успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("Не загрузилось", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
ReloadMaps();
pictureBoxArtilleries.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
}
}
}

View File

@ -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="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>132, 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>

View File

@ -13,5 +13,6 @@ namespace Artilleries
void MoveObject(Direction direction);
void DrawingObject(Graphics g);
(float Left, float Right, float Top, float Bottom) GetCurrentPosition();
string GetInfo();
}
}

View File

@ -124,5 +124,23 @@ namespace Artilleries
index++;
}
}
public string GetData(char separatorType, char separatorData)
{
string data = $"{_map.GetType().Name}{separatorType}";
foreach (var artillery in _setArtilleries.GetArtilleries())
{
data += $"{artillery.GetInfo()}{separatorData}";
}
return data;
}
public void LoadData(string[] records)
{
foreach (var record in records)
{
_setArtilleries.Insert(DrawingObjectArtillery.Create(record) as T);
}
}
}
}

View File

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.Tracing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
@ -14,6 +15,9 @@ namespace Artilleries
private readonly int _pictureWidth;
private readonly int _pictureHeight;
private readonly char separatorDict = '|';
private readonly char separatorData = ';';
public MapsCollection(int pictureWidth, int pictureHeight)
{
_mapsStorage = new Dictionary<string, MapWithSetArtilleriesGeneric<DrawingObjectArtillery, AbstractMap>>();
@ -44,5 +48,66 @@ namespace Artilleries
return _mapsStorage.ContainsKey(index) ? _mapsStorage[index] : null;
}
}
public bool SaveData(string filename)
{
if (File.Exists(filename))
{
File.Delete(filename);
}
using (FileStream fs = new FileStream(filename, FileMode.Create))
using (StreamWriter sw = new StreamWriter(fs, Encoding.UTF8))
{
sw.WriteLine("MapsCollection");
foreach (var storage in _mapsStorage)
{
sw.WriteLine($"{storage.Key}{separatorDict}{storage.Value.GetData(separatorDict, separatorData)}");
}
}
return true;
}
public bool LoadData(string filename)
{
if (!File.Exists(filename))
{
return false;
}
using (FileStream fs = new FileStream(filename, FileMode.Open))
using (StreamReader sr = new StreamReader(fs, Encoding.UTF8))
{
string current_line = sr.ReadLine();
if (current_line == null || !current_line.Contains("MapsCollection"))
{
return false;
}
_mapsStorage.Clear();
while ((current_line = sr.ReadLine()) != null)
{
var elements = current_line.Split(separatorDict);
AbstractMap map = null;
switch (elements[1])
{
case "SimpleMap":
map = new SimpleMap();
break;
case "ForestMap":
map = new ForestMap();
break;
}
_mapsStorage.Add(elements[0], new MapWithSetArtilleriesGeneric<DrawingObjectArtillery, AbstractMap>(_pictureWidth, _pictureHeight, map));
_mapsStorage[elements[0]].LoadData(elements[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries));
}
return true;
}
}
}
}