further improvement for settings input validation

This commit is contained in:
2025-12-28 23:42:45 +01:00
parent 08162ecccb
commit d00e82a96f

View File

@@ -17,6 +17,9 @@ public class SettingsWindow : Window, IDisposable
private readonly Configuration configuration;
private Settings settings;
private bool maxBetFormatValid = true;
private bool stepFormatValid = true;
public SettingsWindow(Plugin plugin) : base("Settings###HRC Settings")
{
Flags = ImGuiWindowFlags.NoResize | ImGuiWindowFlags.NoCollapse | ImGuiWindowFlags.NoScrollbar |
@@ -41,14 +44,21 @@ public class SettingsWindow : Window, IDisposable
if (ImGui.Button("Add roll")) { }
foreach (var roll in settings.rolls)
{
// ImGui.BeginTable();
ImGui.EndTable();
}
var maxbetValid = NewInput("max_bet_label", "Max bet", "max_bet_text", ref settings.maxBet);
if (maxbetValid.HasValue) maxBetFormatValid = maxbetValid.Value;
ImGui.Spacing();
var stepValid = NewInput("step_label", "Step", "step_input", ref settings.step);
if (stepValid.HasValue) stepFormatValid = stepValid.Value;
ImGui.Spacing();
bool? valid = maxbetValid.HasValue && stepValid.HasValue ? maxbetValid.Value && stepValid.Value : null;
var inputValidation = ValidateInput(valid);
var inputValidation = ValidateInput();
ImGui.BeginDisabled(!inputValidation.valid);
@@ -62,13 +72,16 @@ public class SettingsWindow : Window, IDisposable
}
ImGui.EndDisabled();
ImGui.SameLine();
if (!inputValidation.valid && ImGui.IsItemHovered(ImGuiHoveredFlags.AllowWhenDisabled)) ImGui.SetTooltip(inputValidation.message);
if (ImGui.Button("Reset")) settings = configuration.Settings;
if (ImGui.IsItemHovered(ImGuiHoveredFlags.AllowWhenDisabled)) ImGui.SetTooltip(inputValidation.message);
}
private bool? NewInput(string labelId, string labelText, string inputId, ref uint? result)
{
var valid = false;
bool? valid = null;
ImGui.LabelText($"###{labelId}", $"{labelText}: ");
ImGui.SameLine(XOffset, Spacing);
@@ -97,36 +110,30 @@ public class SettingsWindow : Window, IDisposable
/// Validates inputs for Max bet and Step
/// </summary>
/// <exception cref="NotImplementedException"></exception>
private (bool valid, string message) ValidateInput(bool? validInputFormat)
private (bool valid, string message) ValidateInput()
{
var valid = true;
var message = string.Empty;
if (validInputFormat.HasValue && !validInputFormat.Value)
if (!maxBetFormatValid || !stepFormatValid)
{
message += "Input fields have invalid data";
valid = false;
SetError("Input fields have invalid data", ref message, ref valid);
}
if (settings.maxBet > MaxAllowedGil)
{
message += "Entered bet amount exceeds maximum possible bet";
valid = false;
SetError("Entered bet amount exceeds maximum possible bet", ref message, ref valid);
}
if (settings.step > configuration.Settings.maxBet || settings.maxBet < configuration.Settings.maxBet)
{
if (!valid) message += "\n";
message += "Step change must not exceed current maximum bet";
valid = false;
// SetError("Step change must not exceed current maximum bet", message, valid);
SetError("Step change must not exceed current maximum bet", ref message, ref valid);
}
return (valid, message);
}
private void SetError(string errMsg, ref string message, ref bool valid)
private static void SetError(string errMsg, ref string message, ref bool valid)
{
if (!valid) message += "\n";
message += errMsg;