143 lines
4.3 KiB
C#
143 lines
4.3 KiB
C#
using System;
|
|
using System.Numerics;
|
|
using Dalamud.Bindings.ImGui;
|
|
using Dalamud.Interface.Windowing;
|
|
using HighRollerClassic.DataStructures;
|
|
|
|
namespace HighRollerClassic.Windows;
|
|
|
|
public class SettingsWindow : Window, IDisposable
|
|
{
|
|
private const int XOffset = 80;
|
|
private const int Spacing = 5;
|
|
private const int InputWidth = 105;
|
|
private const int InputMaxLen = 11;
|
|
|
|
private const uint MaxAllowedGil = 999_999_999;
|
|
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 |
|
|
ImGuiWindowFlags.NoScrollWithMouse;
|
|
|
|
Size = new Vector2(500, 300);
|
|
SizeCondition = ImGuiCond.Always;
|
|
|
|
configuration = plugin.Configuration;
|
|
// this creates a copy, not a reference
|
|
settings = configuration.Settings;
|
|
}
|
|
|
|
public void Dispose() { }
|
|
|
|
public override void PreDraw() { }
|
|
|
|
public override void Draw()
|
|
{
|
|
// todo set up multiplier, roll, color, etc
|
|
// todo add button for rolls
|
|
|
|
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();
|
|
|
|
var inputValidation = ValidateInput();
|
|
|
|
ImGui.BeginDisabled(!inputValidation.valid);
|
|
|
|
if (ImGui.Button("Save"))
|
|
{
|
|
configuration.Settings = settings;
|
|
configuration.Save();
|
|
Plugin.Log.Debug($"Configuration data: \n\n " +
|
|
$"Max bet: {configuration.Settings.maxBet}\n" +
|
|
$"Step change: {configuration.Settings.step}");
|
|
}
|
|
|
|
ImGui.EndDisabled();
|
|
ImGui.SameLine();
|
|
if (!inputValidation.valid && ImGui.IsItemHovered(ImGuiHoveredFlags.AllowWhenDisabled)) ImGui.SetTooltip(inputValidation.message);
|
|
|
|
if (ImGui.Button("Reset")) settings = configuration.Settings;
|
|
|
|
}
|
|
|
|
private bool? NewInput(string labelId, string labelText, string inputId, ref uint? result)
|
|
{
|
|
bool? valid = null;
|
|
|
|
ImGui.LabelText($"###{labelId}", $"{labelText}: ");
|
|
ImGui.SameLine(XOffset, Spacing);
|
|
ImGui.SetNextItemWidth(InputWidth);
|
|
|
|
var buf = result?.ToString("N0") ?? "";
|
|
|
|
if (ImGui.InputText($"###{inputId}", ref buf, InputMaxLen))
|
|
{
|
|
var num = buf.Replace(",", string.Empty).Replace(".", string.Empty);
|
|
if (uint.TryParse(num, out var parsedValue))
|
|
{
|
|
result = parsedValue;
|
|
valid = true;
|
|
}
|
|
else
|
|
{
|
|
valid = false;
|
|
}
|
|
}
|
|
|
|
return valid;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Validates inputs for Max bet and Step
|
|
/// </summary>
|
|
/// <exception cref="NotImplementedException"></exception>
|
|
private (bool valid, string message) ValidateInput()
|
|
{
|
|
var valid = true;
|
|
var message = string.Empty;
|
|
|
|
if (!maxBetFormatValid || !stepFormatValid)
|
|
{
|
|
SetError("Input fields have invalid data", ref message, ref valid);
|
|
}
|
|
|
|
if (settings.maxBet > MaxAllowedGil)
|
|
{
|
|
SetError("Entered bet amount exceeds maximum possible bet", ref message, ref valid);
|
|
}
|
|
|
|
if (settings.step > configuration.Settings.maxBet || settings.maxBet < configuration.Settings.maxBet)
|
|
{
|
|
SetError("Step change must not exceed current maximum bet", ref message, ref valid);
|
|
}
|
|
|
|
return (valid, message);
|
|
}
|
|
|
|
private static void SetError(string errMsg, ref string message, ref bool valid)
|
|
{
|
|
if (!valid) message += "\n";
|
|
message += errMsg;
|
|
valid = false;
|
|
}
|
|
}
|