GtaVUsersInfo/Sources/FontSettings.cs

143 lines
4.7 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using GtaVUsersInfo.Helpers;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace GtaVUsersInfo.Sources
{
[Serializable]
public class FontSettings
{
public string FontFamily { get; set; }
public float FontSize { get; set; }
public FontStyle FontStyle { get; set; }
public Color FontColor { get; set; }
private static List<Control> DTControls { get; set; }
public FontSettings() { }
public FontSettings(Font font, Color color)
{
FontFamily = font.FontFamily.Name;
FontSize = font.Size;
FontStyle = font.Style;
FontColor = color;
}
public FontSettings(Font font, Color color, List<Control> controls)
{
FontFamily = font.FontFamily.Name;
FontSize = font.Size;
FontStyle = font.Style;
FontColor = color;
DTControls = controls;
}
public void FontSettingsEdit(Font font)
{
FontFamily = font.FontFamily.Name;
FontSize = font.Size;
FontStyle = font.Style;
}
public void FontSettingsEdit(Color color)
{
FontColor = color;
}
public void FontSettingsEdit(List<Control> controls)
{
DTControls = controls;
}
public static void ChangeFontInControls(Control parent, FontSettings fontSettings)
{
Font font = CreateFontFromSettings(fontSettings);
Color color = fontSettings.FontColor;
foreach (Control control in parent.Controls)
{
if (DTControls.Contains(control))
{
continue;
}
if (control.Parent is FlowLayoutPanel)
{
int newWidth = (int)(int.Parse(Resources.CarControlWidth) * (font.Size / 11));
int newHeight = (int)(int.Parse(Resources.CarControlHeight) * (font.Size / 11));
control.Size = new Size(newWidth, newHeight);
}
if (control is TextBox || control is Button || control is ComboBox)
{
control.Font = font;
control.ForeColor = color;
}
else if (control is Label)
{
control.Font = font.Size >= 8 ? new Font(font.FontFamily, font.Size - 2) : new Font(font.FontFamily, 8);
control.ForeColor = color;
}
else if (control is MenuStrip)
{
control.Font = font.Size >= 8 ? new Font(font.FontFamily, font.Size - 1) : new Font(font.FontFamily, 8);
control.ForeColor = color;
}
if (control.HasChildren)
{
ChangeFontInControls(control, fontSettings);
}
}
}
public static Font CreateFontFromSettings(FontSettings settings)
{
return new Font(settings.FontFamily, settings.FontSize, settings.FontStyle);
}
public static void SaveFontSettings(FontSettings fontSettings)
{
string json = JsonConvert.SerializeObject(fontSettings, Formatting.Indented);
if (!Directory.Exists(Path.GetDirectoryName(Resources.fontSettingsJsonPath)))
{
Directory.CreateDirectory(Path.GetDirectoryName(Resources.fontSettingsJsonPath));
}
File.WriteAllText(Resources.fontSettingsJsonPath, json);
}
public static FontSettings LoadFontSettings()
{
try
{
string json = File.ReadAllText(Resources.fontSettingsJsonPath);
return JsonConvert.DeserializeObject<FontSettings>(json);
}
catch (FileNotFoundException)
{
Console.WriteLine($"Файл со стилями не найден: {Resources.fontSettingsJsonPath}");
return null;
}
catch (JsonException ex)
{
Console.WriteLine($"Ошибка при десериализации JSON стилей: {ex.Message}");
return null;
}
catch (Exception ex)
{
Console.WriteLine($"Произошла неожиданная ошибка при открытии файла со стилями: {ex.Message}");
return null;
}
}
}
}