Added Godot settings

This commit is contained in:
Thorsten Sommer 2022-10-29 18:48:38 +02:00
parent 42589c89d8
commit 58f30240dc
Signed by: tsommer
GPG Key ID: 371BBA77A02C0108
3 changed files with 239 additions and 0 deletions

View File

@ -10,4 +10,6 @@ public static class SettingNames
public static readonly string GENERATOR_MODE = "Generator Mode";
public static readonly string GENERATOR_DOTNET_ENABLED = "Generator .NET Enabled";
public static readonly string GENERATOR_DOTNET_DESTINATION_PATH = "Generator .NET Destination Path";
public static readonly string GENERATOR_GODOT_ENABLED = "Generator Godot Enabled";
public static readonly string GENERATOR_GODOT_DESTINATION_PATH = "Generator Godot Destination Path";
}

View File

@ -685,5 +685,153 @@ public static class AppSettings
#endregion
#region Godot Generator Enabled/Disabled
private static bool CACHE_GENERATOR_GODOT_ENABLED = true;
private static bool CACHE_GENERATOR_GODOT_ENABLED_IS_LOADED = false;
public static async Task<bool> GetGeneratorGodotEnabled()
{
// When possible, use the cache:
if (CACHE_GENERATOR_GODOT_ENABLED_IS_LOADED)
return CACHE_GENERATOR_GODOT_ENABLED;
var generatorGodotEnabled = false;
try
{
// Get the database:
await using var db = ProcessorMeta.ServiceProvider.GetRequiredService<DataContext>();
// Check, if the setting is already set:
if (await db.Settings.FirstOrDefaultAsync(n => n.Code == SettingNames.GENERATOR_GODOT_ENABLED) is { } existingSetting)
{
generatorGodotEnabled = existingSetting.BoolValue;
return generatorGodotEnabled;
}
// Does not exist, so create it:
var setting = new Setting
{
Code = SettingNames.GENERATOR_GODOT_ENABLED,
BoolValue = generatorGodotEnabled,
};
await db.Settings.AddAsync(setting);
await db.SaveChangesAsync();
return generatorGodotEnabled;
}
finally
{
CACHE_GENERATOR_GODOT_ENABLED_IS_LOADED = true;
CACHE_GENERATOR_GODOT_ENABLED = generatorGodotEnabled;
}
}
public static async Task SetGeneratorGodotEnabled(bool enabled)
{
// Update the cache:
CACHE_GENERATOR_GODOT_ENABLED = enabled;
CACHE_GENERATOR_GODOT_ENABLED_IS_LOADED = true;
// Get the database:
await using var db = ProcessorMeta.ServiceProvider.GetRequiredService<DataContext>();
// Check, if the setting is already set:
if (await db.Settings.FirstOrDefaultAsync(n => n.Code == SettingNames.GENERATOR_GODOT_ENABLED) is { } existingSetting)
{
existingSetting.BoolValue = enabled;
await db.SaveChangesAsync();
}
// Does not exist, so create it:
else
{
var setting = new Setting
{
Code = SettingNames.GENERATOR_GODOT_ENABLED,
BoolValue = enabled,
};
await db.Settings.AddAsync(setting);
await db.SaveChangesAsync();
}
}
#endregion
#region Godot Generator Destination Path
private static string CACHE_GENERATOR_GODOT_DESTINATION_PATH = string.Empty;
private static bool CACHE_GENERATOR_GODOT_DESTINATION_PATH_IS_LOADED = false;
public static async Task<string> GetGeneratorGodotDestinationPath()
{
// When possible, use the cache:
if (CACHE_GENERATOR_GODOT_DESTINATION_PATH_IS_LOADED)
return CACHE_GENERATOR_GODOT_DESTINATION_PATH;
var generatorGodotDestinationPath = string.Empty;
try
{
// Get the database:
await using var db = ProcessorMeta.ServiceProvider.GetRequiredService<DataContext>();
// Check, if the setting is already set:
if (await db.Settings.FirstOrDefaultAsync(n => n.Code == SettingNames.GENERATOR_GODOT_DESTINATION_PATH) is { } existingSetting)
{
generatorGodotDestinationPath = existingSetting.TextValue;
return generatorGodotDestinationPath;
}
// Does not exist, so create it:
var setting = new Setting
{
Code = SettingNames.GENERATOR_GODOT_DESTINATION_PATH,
TextValue = generatorGodotDestinationPath,
};
await db.Settings.AddAsync(setting);
await db.SaveChangesAsync();
return generatorGodotDestinationPath;
}
finally
{
CACHE_GENERATOR_GODOT_DESTINATION_PATH_IS_LOADED = true;
CACHE_GENERATOR_GODOT_DESTINATION_PATH = generatorGodotDestinationPath;
}
}
public static async Task SetGeneratorGodotDestinationPath(string path)
{
// Update the cache:
CACHE_GENERATOR_GODOT_DESTINATION_PATH = path;
CACHE_GENERATOR_GODOT_DESTINATION_PATH_IS_LOADED = true;
// Get the database:
await using var db = ProcessorMeta.ServiceProvider.GetRequiredService<DataContext>();
// Check, if the setting is already set:
if (await db.Settings.FirstOrDefaultAsync(n => n.Code == SettingNames.GENERATOR_GODOT_DESTINATION_PATH) is { } existingSetting)
{
existingSetting.TextValue = path;
await db.SaveChangesAsync();
}
// Does not exist, so create it:
else
{
var setting = new Setting
{
Code = SettingNames.GENERATOR_GODOT_DESTINATION_PATH,
TextValue = path,
};
await db.Settings.AddAsync(setting);
await db.SaveChangesAsync();
}
}
#endregion
#endregion
}

View File

@ -520,9 +520,98 @@ public sealed partial class Setting : UserControl
return new Setting(settingData);
}
private static async Task<Setting> ShowGeneratorGodotEnabledSettingAsync()
{
var currentSetting = await AppSettings.GetGeneratorGodotEnabled();
var settingData = new SettingUIData(
Icon: Icons.icons8_code_512,
SettingName: () => "Generator: Godot",
ChangeNeedsRestart: false,
SettingExplanation: () => "When enabled, Godot translation files are generated. Requires a Godot 3.5 or newer project.",
SettingExplanationLink: () => (string.Empty, string.Empty),
SetupDataControl: (changeTrigger) =>
{
// Set up an checkbox:
var checkbox = new CheckBox();
checkbox.Checked = currentSetting;
checkbox.CheckedChanged += (sender, args) => changeTrigger();
checkbox.CheckedChanged += async (sender, args) => await AppSettings.SetGeneratorGodotEnabled(checkbox.Checked);
checkbox.Text = "Enable Godot Generator";
// Apply the desired layout:
checkbox.Dock = DockStyle.Fill;
return checkbox;
}
);
return new Setting(settingData);
}
private static async Task<Setting> ShowGeneratorGodotDestinationPathSettingAsync()
{
var currentSetting = await AppSettings.GetGeneratorGodotDestinationPath();
var settingData = new SettingUIData(
Icon: Icons.icons8_code_512,
SettingName: () => "Generator: Godot Destination Path",
ChangeNeedsRestart: false,
SettingExplanation: () => "The destination path for the Godot translation files. You might use environment variables like %USERPROFILE%.",
SettingExplanationLink: () => (string.Empty, string.Empty),
SetupDataControl: (changeTrigger) =>
{
// Set up a horizontal layout:
var layout = new TableLayoutPanel();
layout.ColumnCount = 2;
layout.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100F));
layout.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 66F));
layout.RowCount = 1;
layout.RowStyles.Add(new RowStyle(SizeType.Percent, 100F));
layout.Dock = DockStyle.Fill;
// Set up a textbox:
var textbox = new TextBox();
textbox.Text = currentSetting;
textbox.TextChanged += (sender, args) => changeTrigger();
textbox.TextChanged += async (sender, args) => await AppSettings.SetGeneratorGodotDestinationPath(textbox.Text);
textbox.Dock = DockStyle.Fill;
textbox.Margin = new Padding(0, 13, 0, 13);
layout.Controls.Add(textbox, 0, 0);
// Set up a button:
var button = new Button();
button.Text = string.Empty;
button.Image = Icons.icons8_folder_tree_512;
button.FlatStyle = FlatStyle.Flat;
button.FlatAppearance.BorderSize = 0;
button.BackColor = Color.Empty;
button.UseVisualStyleBackColor = true;
button.Size = new Size(60, 60);
button.Click += (sender, args) =>
{
var dialog = new FolderBrowserDialog();
dialog.SelectedPath = textbox.Text;
dialog.InitialDirectory = textbox.Text;
dialog.Description = "Select the destination path for the Godot translation files.";
dialog.ShowNewFolderButton = true;
if (dialog.ShowDialog() == DialogResult.OK)
textbox.Text = dialog.SelectedPath;
};
button.Dock = DockStyle.Fill;
layout.Controls.Add(button, 1, 0);
return layout;
}
);
return new Setting(settingData);
}
public static IEnumerable<Task<Setting>> GetAllSettings()
{
yield return ShowGeneratorGodotDestinationPathSettingAsync();
yield return ShowGeneratorGodotEnabledSettingAsync();
yield return ShowGeneratorDotnetDestinationPathSettingAsync();
yield return ShowGeneratorDotnetEnabledSettingAsync();
yield return ShowGeneratorModeSettingAsync();