diff --git a/Car.cs b/Car.cs index f1a478c..08643b7 100644 --- a/Car.cs +++ b/Car.cs @@ -1,4 +1,8 @@ -namespace GtaVUsersInfo +using GtaVUsersInfo.Controls; +using System.Collections.Generic; +using System.Linq; + +namespace GtaVUsersInfo { public class Car { @@ -8,5 +12,22 @@ public string Manufacturer { get; set; } public string Class { 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 ConvertToCarList(List cars) + { + return cars.Select(car => new Car(car.Car)).ToList(); + } } } diff --git a/CarPrice.cs b/CarPrice.cs deleted file mode 100644 index 5b5981b..0000000 --- a/CarPrice.cs +++ /dev/null @@ -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 ConvertToCarPriceList(List cars) - { - return cars.Select(car => new CarPrice - { - Name = car.Name, - Price = car.Price - }).ToList(); - } - } -} \ No newline at end of file diff --git a/Controls/CarControl.Designer.cs b/Controls/CarControl.Designer.cs index 7eb43ce..bac275e 100644 --- a/Controls/CarControl.Designer.cs +++ b/Controls/CarControl.Designer.cs @@ -235,14 +235,14 @@ this.contextMenuStrip1.Name = "contextMenuStrip1"; this.contextMenuStrip1.Size = new System.Drawing.Size(61, 4); // - // CarItem + // CarControl // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.BackColor = System.Drawing.Color.White; this.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.Controls.Add(this.MainPanel); - this.Name = "CarItem"; + this.Name = "CarControl"; this.Size = new System.Drawing.Size(210, 204); this.MainPanel.ResumeLayout(false); this.MainPanel.PerformLayout(); diff --git a/Controls/ParameterControl.Designer.cs b/Controls/ParameterControl.Designer.cs index 98a5fb8..d8a81be 100644 --- a/Controls/ParameterControl.Designer.cs +++ b/Controls/ParameterControl.Designer.cs @@ -50,11 +50,12 @@ // Textbox // 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.Multiline = true; this.Textbox.Name = "Textbox"; this.Textbox.Size = new System.Drawing.Size(146, 20); this.Textbox.TabIndex = 1; + this.Textbox.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox_KeyPress); // // Label // diff --git a/Controls/ParameterControl.cs b/Controls/ParameterControl.cs index b5f2f8c..bcdf776 100644 --- a/Controls/ParameterControl.cs +++ b/Controls/ParameterControl.cs @@ -18,10 +18,10 @@ namespace GtaVUsersInfo.Controls set { Label.Text = value + ':'; } } - public int ParamText + public string ParamText { - get { return int.Parse(Textbox.Text); } - set { Textbox.Text = value.ToString(); } + get { return Textbox.Text; } + set { Textbox.Text = value; } } public ContextMenuStrip MenuStrip @@ -30,6 +30,14 @@ namespace GtaVUsersInfo.Controls set { ContextMenuStrip = value; } } + public ParameterControl() + { + InitializeComponent(); + + Label.Text = ""; + Textbox.Text = ""; + } + public ParameterControl(string name, string text = "") { InitializeComponent(); @@ -38,12 +46,21 @@ namespace GtaVUsersInfo.Controls Textbox.Text = text; } - public ParameterControl() + public ParameterControl(Parameter parameter) { InitializeComponent(); - Label.Text = ""; - Textbox.Text = ""; + Label.Text = parameter.Name + ':'; + Textbox.Text = parameter.Text; + } + + private void textBox_KeyPress(object sender, KeyPressEventArgs e) + { + if (e.KeyChar == (char)Keys.Enter) + { + Label.Focus(); + e.Handled = true; + } } } } diff --git a/Forms/AddParameter.cs b/Forms/AddParameter.cs index 00e7297..e3858a7 100644 --- a/Forms/AddParameter.cs +++ b/Forms/AddParameter.cs @@ -14,10 +14,10 @@ namespace GtaVUsersInfo.Forms { public partial class AddParameter : Form { - List listControls; + List listControls; ParameterControl parameter; - public AddParameter(List listControls, ParameterControl parameter) + public AddParameter(List listControls, ParameterControl parameter) { InitializeComponent(); diff --git a/GtaVUsersInfo.csproj b/GtaVUsersInfo.csproj index b2d27be..dab492c 100644 --- a/GtaVUsersInfo.csproj +++ b/GtaVUsersInfo.csproj @@ -116,7 +116,7 @@ AddCars.cs - + UserControl @@ -129,6 +129,7 @@ AddParameter.cs + Form diff --git a/Helpers/Cars_Parameters.cs b/Helpers/Cars_Parameters.cs new file mode 100644 index 0000000..e96e949 --- /dev/null +++ b/Helpers/Cars_Parameters.cs @@ -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 cars { get; set; } + public List parameters { get; set; } + + public Cars_Parameters(List cars, List parameters) + { + this.cars = cars; + this.parameters = parameters; + } + } +} diff --git a/MainForm.Designer.cs b/MainForm.Designer.cs index 0d593eb..f8a742d 100644 --- a/MainForm.Designer.cs +++ b/MainForm.Designer.cs @@ -225,42 +225,46 @@ // 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.Click += new System.EventHandler(this.openFileToolStripMenuItem_Click); // // 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.Click += new System.EventHandler(this.saveToolStripMenuItem_Click); // // 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 = "Сохранить как"; // // toolStripSeparator2 // this.toolStripSeparator2.Name = "toolStripSeparator2"; - this.toolStripSeparator2.Size = new System.Drawing.Size(167, 6); + this.toolStripSeparator2.Size = new System.Drawing.Size(177, 6); // // 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.Click += new System.EventHandler(this.newToolStripMenuItem_Click); // // toolStripSeparator1 // this.toolStripSeparator1.Name = "toolStripSeparator1"; - this.toolStripSeparator1.Size = new System.Drawing.Size(167, 6); + this.toolStripSeparator1.Size = new System.Drawing.Size(177, 6); // // 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.Click += new System.EventHandler(this.exitToolStripMenuItem_Click); // // стилиToolStripMenuItem // @@ -390,6 +394,7 @@ this.Name = "MainForm"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "null"; + this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.MainForm_FormClosing); this.Load += new System.EventHandler(this.MainForm_Load); this.panel1.ResumeLayout(false); this.panel1.PerformLayout(); diff --git a/MainForm.cs b/MainForm.cs index 69234ff..3ad2714 100644 --- a/MainForm.cs +++ b/MainForm.cs @@ -15,6 +15,8 @@ using Newtonsoft.Json; using System.IO; using System.Data.Common; using GtaVUsersInfo.Forms; +using System.Collections; +using System.Security.Cryptography; namespace GtaVUsersInfo { @@ -24,7 +26,7 @@ namespace GtaVUsersInfo private List carList; - private List parameters; + private List parameters; private List cars; public MainForm() @@ -37,8 +39,8 @@ namespace GtaVUsersInfo carList = LoadCarsFromJson(Resources.carJsonPath); - parameters = new List { new ParameterControl("Имя"), new ParameterControl("Деньги") }; cars = new List(); + parameters = new List { new ParameterControl("Имя"), new ParameterControl("Деньги") }; carsItems.ContextMenuStrip = new CarPanelMenuStrip(addNewCar).Menu; parametersPanel.ContextMenuStrip = new ParameterPanelMenuStrip(addNewParameter).Menu; @@ -113,6 +115,16 @@ namespace GtaVUsersInfo 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 async private void parserButton_Click(object sender, EventArgs e) @@ -127,15 +139,7 @@ namespace GtaVUsersInfo carList = LoadCarsFromJson(Resources.carJsonPath); } - 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); - } + #region Методы автомобильной панели private void addCar_Click(object sender, EventArgs e) { @@ -232,6 +236,10 @@ namespace GtaVUsersInfo } } + #endregion + + #region Методы панели параметров + private void addParameter_Click(object sender, EventArgs e) { 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(jsonData); + return data; + } + return null; // Если пользователь отменил выбор файла + } + + private void newToolStripMenuItem_Click(object sender, EventArgs e) + { + if (ErrorsShow.ShowQuestMessage("Хотите сохранить текущего персонажа?") == DialogResult.Yes) + { + SaveDataToFile(); + } + + cars = new List(); + parameters = new List { 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(); + } + } } } diff --git a/Parameter.cs b/Parameter.cs new file mode 100644 index 0000000..3f234ec --- /dev/null +++ b/Parameter.cs @@ -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 ConvertToParameterList(List parameter) + { + return parameter.Select(param => new Parameter + { + Name = param.ParamName, + Text = param.ParamText + }).ToList(); + } + } +} \ No newline at end of file