Добавлены сохранение и загрузка персонажей

This commit is contained in:
MisterMLiL 2024-10-08 18:33:19 +03:00
parent ed5ef49236
commit 916b9dc85c
11 changed files with 208 additions and 51 deletions

23
Car.cs
View File

@ -1,4 +1,8 @@
namespace GtaVUsersInfo using GtaVUsersInfo.Controls;
using System.Collections.Generic;
using System.Linq;
namespace GtaVUsersInfo
{ {
public class Car public class Car
{ {
@ -8,5 +12,22 @@
public string Manufacturer { get; set; } public string Manufacturer { get; set; }
public string Class { get; set; } public string Class { get; set; }
public string Model { get; set; } public string Model { get; set; }
public Car() { }
public Car(Car car)
{
Name = car.Name;
Price = car.Price;
Photo = car.Photo;
Manufacturer = car.Manufacturer;
Class = car.Class;
Model = car.Model;
}
public static List<Car> ConvertToCarList(List<CarControl> cars)
{
return cars.Select(car => new Car(car.Car)).ToList();
}
} }
} }

View File

@ -1,20 +0,0 @@
using System.Collections.Generic;
using System.Linq;
namespace GtaVUsersInfo
{
public class CarPrice
{
public string Name { get; set; }
public int Price { get; set; }
public static List<CarPrice> ConvertToCarPriceList(List<Car> cars)
{
return cars.Select(car => new CarPrice
{
Name = car.Name,
Price = car.Price
}).ToList();
}
}
}

View File

@ -235,14 +235,14 @@
this.contextMenuStrip1.Name = "contextMenuStrip1"; this.contextMenuStrip1.Name = "contextMenuStrip1";
this.contextMenuStrip1.Size = new System.Drawing.Size(61, 4); this.contextMenuStrip1.Size = new System.Drawing.Size(61, 4);
// //
// CarItem // CarControl
// //
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.Color.White; this.BackColor = System.Drawing.Color.White;
this.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.Controls.Add(this.MainPanel); this.Controls.Add(this.MainPanel);
this.Name = "CarItem"; this.Name = "CarControl";
this.Size = new System.Drawing.Size(210, 204); this.Size = new System.Drawing.Size(210, 204);
this.MainPanel.ResumeLayout(false); this.MainPanel.ResumeLayout(false);
this.MainPanel.PerformLayout(); this.MainPanel.PerformLayout();

View File

@ -50,11 +50,12 @@
// Textbox // Textbox
// //
this.Textbox.Dock = System.Windows.Forms.DockStyle.Top; this.Textbox.Dock = System.Windows.Forms.DockStyle.Top;
this.Textbox.ImeMode = System.Windows.Forms.ImeMode.NoControl;
this.Textbox.Location = new System.Drawing.Point(2, 15); this.Textbox.Location = new System.Drawing.Point(2, 15);
this.Textbox.Multiline = true;
this.Textbox.Name = "Textbox"; this.Textbox.Name = "Textbox";
this.Textbox.Size = new System.Drawing.Size(146, 20); this.Textbox.Size = new System.Drawing.Size(146, 20);
this.Textbox.TabIndex = 1; this.Textbox.TabIndex = 1;
this.Textbox.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox_KeyPress);
// //
// Label // Label
// //

View File

@ -18,10 +18,10 @@ namespace GtaVUsersInfo.Controls
set { Label.Text = value + ':'; } set { Label.Text = value + ':'; }
} }
public int ParamText public string ParamText
{ {
get { return int.Parse(Textbox.Text); } get { return Textbox.Text; }
set { Textbox.Text = value.ToString(); } set { Textbox.Text = value; }
} }
public ContextMenuStrip MenuStrip public ContextMenuStrip MenuStrip
@ -30,6 +30,14 @@ namespace GtaVUsersInfo.Controls
set { ContextMenuStrip = value; } set { ContextMenuStrip = value; }
} }
public ParameterControl()
{
InitializeComponent();
Label.Text = "";
Textbox.Text = "";
}
public ParameterControl(string name, string text = "") public ParameterControl(string name, string text = "")
{ {
InitializeComponent(); InitializeComponent();
@ -38,12 +46,21 @@ namespace GtaVUsersInfo.Controls
Textbox.Text = text; Textbox.Text = text;
} }
public ParameterControl() public ParameterControl(Parameter parameter)
{ {
InitializeComponent(); InitializeComponent();
Label.Text = ""; Label.Text = parameter.Name + ':';
Textbox.Text = ""; Textbox.Text = parameter.Text;
}
private void textBox_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Enter)
{
Label.Focus();
e.Handled = true;
}
} }
} }
} }

View File

@ -14,10 +14,10 @@ namespace GtaVUsersInfo.Forms
{ {
public partial class AddParameter : Form public partial class AddParameter : Form
{ {
List<Control> listControls; List<ParameterControl> listControls;
ParameterControl parameter; ParameterControl parameter;
public AddParameter(List<Control> listControls, ParameterControl parameter) public AddParameter(List<ParameterControl> listControls, ParameterControl parameter)
{ {
InitializeComponent(); InitializeComponent();

View File

@ -116,7 +116,7 @@
<DependentUpon>AddCars.cs</DependentUpon> <DependentUpon>AddCars.cs</DependentUpon>
</Compile> </Compile>
<Compile Include="Car.cs" /> <Compile Include="Car.cs" />
<Compile Include="CarPrice.cs" /> <Compile Include="Parameter.cs" />
<Compile Include="Controls\CarControl.cs"> <Compile Include="Controls\CarControl.cs">
<SubType>UserControl</SubType> <SubType>UserControl</SubType>
</Compile> </Compile>
@ -129,6 +129,7 @@
<Compile Include="Forms\AddParameter.Designer.cs"> <Compile Include="Forms\AddParameter.Designer.cs">
<DependentUpon>AddParameter.cs</DependentUpon> <DependentUpon>AddParameter.cs</DependentUpon>
</Compile> </Compile>
<Compile Include="Helpers\Cars_Parameters.cs" />
<Compile Include="Helpers\ErrorsShow.cs" /> <Compile Include="Helpers\ErrorsShow.cs" />
<Compile Include="MainForm.cs"> <Compile Include="MainForm.cs">
<SubType>Form</SubType> <SubType>Form</SubType>

View File

@ -0,0 +1,23 @@
using GtaVUsersInfo.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace GtaVUsersInfo.Helpers
{
[Serializable]
public class Cars_Parameters
{
public List<Car> cars { get; set; }
public List<Parameter> parameters { get; set; }
public Cars_Parameters(List<Car> cars, List<Parameter> parameters)
{
this.cars = cars;
this.parameters = parameters;
}
}
}

19
MainForm.Designer.cs generated
View File

@ -225,42 +225,46 @@
// openFileToolStripMenuItem // openFileToolStripMenuItem
// //
this.openFileToolStripMenuItem.Name = "openFileToolStripMenuItem"; this.openFileToolStripMenuItem.Name = "openFileToolStripMenuItem";
this.openFileToolStripMenuItem.Size = new System.Drawing.Size(170, 22); this.openFileToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
this.openFileToolStripMenuItem.Text = "Открыть"; this.openFileToolStripMenuItem.Text = "Открыть";
this.openFileToolStripMenuItem.Click += new System.EventHandler(this.openFileToolStripMenuItem_Click);
// //
// saveToolStripMenuItem // saveToolStripMenuItem
// //
this.saveToolStripMenuItem.Name = "saveToolStripMenuItem"; this.saveToolStripMenuItem.Name = "saveToolStripMenuItem";
this.saveToolStripMenuItem.Size = new System.Drawing.Size(170, 22); this.saveToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
this.saveToolStripMenuItem.Text = "Сохранить"; this.saveToolStripMenuItem.Text = "Сохранить";
this.saveToolStripMenuItem.Click += new System.EventHandler(this.saveToolStripMenuItem_Click);
// //
// saveAsКакToolStripMenuItem // saveAsКакToolStripMenuItem
// //
this.saveAsКакToolStripMenuItem.Name = "saveAsКакToolStripMenuItem"; this.saveAsКакToolStripMenuItem.Name = "saveAsКакToolStripMenuItem";
this.saveAsКакToolStripMenuItem.Size = new System.Drawing.Size(170, 22); this.saveAsКакToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
this.saveAsКакToolStripMenuItem.Text = "Сохранить как"; this.saveAsКакToolStripMenuItem.Text = "Сохранить как";
// //
// toolStripSeparator2 // toolStripSeparator2
// //
this.toolStripSeparator2.Name = "toolStripSeparator2"; this.toolStripSeparator2.Name = "toolStripSeparator2";
this.toolStripSeparator2.Size = new System.Drawing.Size(167, 6); this.toolStripSeparator2.Size = new System.Drawing.Size(177, 6);
// //
// newToolStripMenuItem // newToolStripMenuItem
// //
this.newToolStripMenuItem.Name = "newToolStripMenuItem"; this.newToolStripMenuItem.Name = "newToolStripMenuItem";
this.newToolStripMenuItem.Size = new System.Drawing.Size(170, 22); this.newToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
this.newToolStripMenuItem.Text = "Новый персонаж"; this.newToolStripMenuItem.Text = "Новый персонаж";
this.newToolStripMenuItem.Click += new System.EventHandler(this.newToolStripMenuItem_Click);
// //
// toolStripSeparator1 // toolStripSeparator1
// //
this.toolStripSeparator1.Name = "toolStripSeparator1"; this.toolStripSeparator1.Name = "toolStripSeparator1";
this.toolStripSeparator1.Size = new System.Drawing.Size(167, 6); this.toolStripSeparator1.Size = new System.Drawing.Size(177, 6);
// //
// exitToolStripMenuItem // exitToolStripMenuItem
// //
this.exitToolStripMenuItem.Name = "exitToolStripMenuItem"; this.exitToolStripMenuItem.Name = "exitToolStripMenuItem";
this.exitToolStripMenuItem.Size = new System.Drawing.Size(170, 22); this.exitToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
this.exitToolStripMenuItem.Text = "Выйти"; this.exitToolStripMenuItem.Text = "Выйти";
this.exitToolStripMenuItem.Click += new System.EventHandler(this.exitToolStripMenuItem_Click);
// //
// стилиToolStripMenuItem // стилиToolStripMenuItem
// //
@ -390,6 +394,7 @@
this.Name = "MainForm"; this.Name = "MainForm";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "null"; this.Text = "null";
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.MainForm_FormClosing);
this.Load += new System.EventHandler(this.MainForm_Load); this.Load += new System.EventHandler(this.MainForm_Load);
this.panel1.ResumeLayout(false); this.panel1.ResumeLayout(false);
this.panel1.PerformLayout(); this.panel1.PerformLayout();

View File

@ -15,6 +15,8 @@ using Newtonsoft.Json;
using System.IO; using System.IO;
using System.Data.Common; using System.Data.Common;
using GtaVUsersInfo.Forms; using GtaVUsersInfo.Forms;
using System.Collections;
using System.Security.Cryptography;
namespace GtaVUsersInfo namespace GtaVUsersInfo
{ {
@ -24,7 +26,7 @@ namespace GtaVUsersInfo
private List<Car> carList; private List<Car> carList;
private List<Control> parameters; private List<ParameterControl> parameters;
private List<CarControl> cars; private List<CarControl> cars;
public MainForm() public MainForm()
@ -37,8 +39,8 @@ namespace GtaVUsersInfo
carList = LoadCarsFromJson(Resources.carJsonPath); carList = LoadCarsFromJson(Resources.carJsonPath);
parameters = new List<Control> { new ParameterControl("Имя"), new ParameterControl("Деньги") };
cars = new List<CarControl>(); cars = new List<CarControl>();
parameters = new List<ParameterControl> { new ParameterControl("Имя"), new ParameterControl("Деньги") };
carsItems.ContextMenuStrip = new CarPanelMenuStrip(addNewCar).Menu; carsItems.ContextMenuStrip = new CarPanelMenuStrip(addNewCar).Menu;
parametersPanel.ContextMenuStrip = new ParameterPanelMenuStrip(addNewParameter).Menu; parametersPanel.ContextMenuStrip = new ParameterPanelMenuStrip(addNewParameter).Menu;
@ -113,6 +115,16 @@ namespace GtaVUsersInfo
controls.ForEach(control => { control.Visible = isVisible; control.Enabled = isVisible; }); controls.ForEach(control => { control.Visible = isVisible; control.Enabled = isVisible; });
} }
private void readJsonButton_Click(object sender, EventArgs e)
{
carList = LoadCarsFromJson(Resources.carJsonPath);
}
private void debugCheck_CheckedChanged(object sender, EventArgs e)
{
this.visible(debugControlsList, debugCheck.Checked);
}
#endregion #endregion
async private void parserButton_Click(object sender, EventArgs e) async private void parserButton_Click(object sender, EventArgs e)
@ -127,15 +139,7 @@ namespace GtaVUsersInfo
carList = LoadCarsFromJson(Resources.carJsonPath); carList = LoadCarsFromJson(Resources.carJsonPath);
} }
private void readJsonButton_Click(object sender, EventArgs e) #region Методы автомобильной панели
{
carList = LoadCarsFromJson(Resources.carJsonPath);
}
private void debugCheck_CheckedChanged(object sender, EventArgs e)
{
this.visible(debugControlsList, debugCheck.Checked);
}
private void addCar_Click(object sender, EventArgs e) private void addCar_Click(object sender, EventArgs e)
{ {
@ -232,6 +236,10 @@ namespace GtaVUsersInfo
} }
} }
#endregion
#region Методы панели параметров
private void addParameter_Click(object sender, EventArgs e) private void addParameter_Click(object sender, EventArgs e)
{ {
ParameterControl parameter = new ParameterControl(); ParameterControl parameter = new ParameterControl();
@ -286,5 +294,83 @@ namespace GtaVUsersInfo
} }
} }
} }
#endregion
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveDataToFile();
AllPanelCreate();
}
private void openFileToolStripMenuItem_Click(object sender, EventArgs e)
{
Cars_Parameters lists = LoadDataFromFile();
if (lists != null)
{
cars = lists.cars.Select(car => new CarControl(car)).ToList();
parameters = lists.parameters.Select(parameter => new ParameterControl(parameter)).ToList();
}
AllPanelCreate();
}
public void SaveDataToFile()
{
Cars_Parameters data = new Cars_Parameters(Car.ConvertToCarList(cars), Parameter.ConvertToParameterList(parameters));
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "JSON files (*.json)|*.json|All files (*.*)|*.*";
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
// Сериализуем данные в JSON
string jsonData = JsonConvert.SerializeObject(data, Formatting.Indented);
// Записываем данные в файл
File.WriteAllText(saveFileDialog.FileName, jsonData);
}
}
public Cars_Parameters LoadDataFromFile()
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "JSON files (*.json)|*.json|All files (*.*)|*.*";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
// Читаем данные из файла
string jsonData = File.ReadAllText(openFileDialog.FileName);
// Десериализуем данные в объект ListData
Cars_Parameters data = JsonConvert.DeserializeObject<Cars_Parameters>(jsonData);
return data;
}
return null; // Если пользователь отменил выбор файла
}
private void newToolStripMenuItem_Click(object sender, EventArgs e)
{
if (ErrorsShow.ShowQuestMessage("Хотите сохранить текущего персонажа?") == DialogResult.Yes)
{
SaveDataToFile();
}
cars = new List<CarControl>();
parameters = new List<ParameterControl> { new ParameterControl("Имя"), new ParameterControl("Деньги") };
AllPanelCreate();
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (ErrorsShow.ShowQuestMessage("Хотите сохранить текущего персонажа?") == DialogResult.Yes)
{
SaveDataToFile();
}
}
} }
} }

23
Parameter.cs Normal file
View File

@ -0,0 +1,23 @@
using GtaVUsersInfo.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace GtaVUsersInfo
{
public class Parameter
{
public string Name { get; set; }
public string Text { get; set; }
public static List<Parameter> ConvertToParameterList(List<ParameterControl> parameter)
{
return parameter.Select(param => new Parameter
{
Name = param.ParamName,
Text = param.ParamText
}).ToList();
}
}
}