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