console: add "toggle" command to flip boolean convars

This commit is contained in:
goat 2024-06-07 21:53:29 +02:00
parent b3727a0f2a
commit 0f2aaa4241

View file

@ -27,6 +27,7 @@ internal partial class ConsoleManager : IServiceType
[ServiceManager.ServiceConstructor] [ServiceManager.ServiceConstructor]
public ConsoleManager() public ConsoleManager()
{ {
this.AddCommand("toggle", "Toggle a boolean variable.", this.OnToggleVariable);
} }
/// <summary> /// <summary>
@ -317,6 +318,19 @@ internal partial class ConsoleManager : IServiceType
{ {
return this.entries.TryGetValue(name, out var entry) ? entry as ConsoleEntry : null; return this.entries.TryGetValue(name, out var entry) ? entry as ConsoleEntry : null;
} }
private bool OnToggleVariable(string name)
{
if (this.FindEntry(name) is not IConsoleVariable<bool> variable)
{
Log.Error("Variable {VariableName} not found or not a boolean", name);
return false;
}
variable.Value = !variable.Value;
return true;
}
private static class Traits private static class Traits
{ {