SHA256
Add UI Value Validators
This commit is contained in:
5 files changed
+359
-2
No files matched your search
@@ -0,0 +1,98 @@
|
||||
using System.Collections.Generic;
|
||||
using MelonLoader;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
namespace ADOFAIRenderer.Data
|
||||
{
|
||||
public class DoubleInputValidator
|
||||
{
|
||||
private struct FieldEntry
|
||||
{
|
||||
public List<DoubleLimit> Limits;
|
||||
public bool Optional;
|
||||
}
|
||||
|
||||
private readonly VisualElement _rootVisualElement;
|
||||
private readonly Dictionary<string, FieldEntry> _fields = new();
|
||||
|
||||
public DoubleInputValidator(VisualElement rootVisualElement)
|
||||
{
|
||||
_rootVisualElement = rootVisualElement;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Registers one or more <see cref="DoubleLimit"/>s for the field with the given name.
|
||||
/// Multiple limits can be added to the same field — they are all checked in order.
|
||||
/// </summary>
|
||||
/// <param name="fieldName">The name of the DoubleField in the UXML.</param>
|
||||
/// <param name="optional">
|
||||
/// If true, the field is allowed to be empty. Limits are only enforced when a value is present.
|
||||
/// </param>
|
||||
/// <param name="limits">The limits to apply.</param>
|
||||
public DoubleInputValidator AddLimits(string fieldName, bool optional, params DoubleLimit[] limits)
|
||||
{
|
||||
if (!_fields.TryGetValue(fieldName, out var entry))
|
||||
{
|
||||
entry = new FieldEntry
|
||||
{
|
||||
Limits = new List<DoubleLimit>(),
|
||||
Optional = optional
|
||||
};
|
||||
}
|
||||
|
||||
entry.Optional = optional;
|
||||
entry.Limits.AddRange(limits);
|
||||
_fields[fieldName] = entry;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="AddLimits(string, bool, DoubleLimit[])"/>
|
||||
/// <remarks>Shorthand for required (non-optional) fields.</remarks>
|
||||
public DoubleInputValidator AddLimits(string fieldName, params DoubleLimit[] limits)
|
||||
{
|
||||
return AddLimits(fieldName, false, limits);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Queries all registered fields from the root VisualElement and
|
||||
/// registers value-changed callbacks that rollback invalid values.
|
||||
/// </summary>
|
||||
public void SetupValidation()
|
||||
{
|
||||
foreach (var kvp in _fields)
|
||||
{
|
||||
var fieldName = kvp.Key;
|
||||
var entry = kvp.Value;
|
||||
|
||||
var field = _rootVisualElement.Q<DoubleField>(fieldName);
|
||||
if (field == null)
|
||||
{
|
||||
Melon<RenderMod>.Logger.Warning(
|
||||
$"DoubleField '{fieldName}'을(를) 찾을 수 없습니다. 유효성 검사를 건너뜁니다.");
|
||||
continue;
|
||||
}
|
||||
|
||||
var limits = entry.Limits;
|
||||
var optional = entry.Optional;
|
||||
|
||||
field.RegisterValueChangedCallback(evt =>
|
||||
{
|
||||
// Optional fields allow empty input — skip validation when cleared
|
||||
if (optional && string.IsNullOrEmpty(field.text))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var limit in limits)
|
||||
{
|
||||
if (!limit.Validate(evt.newValue, out _))
|
||||
{
|
||||
field.SetValueWithoutNotify(evt.previousValue);
|
||||
return;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
namespace ADOFAIRenderer.Data
|
||||
{
|
||||
/// <summary>
|
||||
/// Base class for double input validation limits.
|
||||
/// Multiple limits can be composed on a single input field — all are checked in order.
|
||||
/// </summary>
|
||||
public abstract class DoubleLimit
|
||||
{
|
||||
/// <summary>
|
||||
/// Validates the given double value against this limit.
|
||||
/// </summary>
|
||||
/// <param name="value">The parsed double value to validate.</param>
|
||||
/// <param name="errorMessage">
|
||||
/// When validation fails, set to a human-readable error message.
|
||||
/// When validation succeeds, set to null.
|
||||
/// </param>
|
||||
/// <returns>true if the value satisfies this limit; false otherwise.</returns>
|
||||
public abstract bool Validate(double value, out string errorMessage);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Requires the double value to be strictly positive (> 0).
|
||||
/// </summary>
|
||||
public class PositiveOnlyDoubleLimit : DoubleLimit
|
||||
{
|
||||
public override bool Validate(double value, out string errorMessage)
|
||||
{
|
||||
if (value > 0)
|
||||
{
|
||||
errorMessage = null!;
|
||||
return true;
|
||||
}
|
||||
|
||||
errorMessage = "값은 0보다 큰 양수여야 합니다.";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Requires the double value to be within an inclusive [Min, Max] range.
|
||||
/// Either bound can be null to represent an unbounded side.
|
||||
/// </summary>
|
||||
public class RangeDoubleLimit : DoubleLimit
|
||||
{
|
||||
public double? Min { get; }
|
||||
public double? Max { get; }
|
||||
|
||||
/// <param name="min">Inclusive lower bound, or null for no lower bound.</param>
|
||||
/// <param name="max">Inclusive upper bound, or null for no upper bound.</param>
|
||||
public RangeDoubleLimit(double? min, double? max)
|
||||
{
|
||||
Min = min;
|
||||
Max = max;
|
||||
}
|
||||
|
||||
public override bool Validate(double value, out string errorMessage)
|
||||
{
|
||||
if (Min.HasValue && value < Min.Value)
|
||||
{
|
||||
errorMessage = Max.HasValue
|
||||
? $"값은 {Min.Value}에서 {Max.Value} 사이의 숫자여야 합니다."
|
||||
: $"값은 {Min.Value} 이상의 숫자여야 합니다.";
|
||||
return false;
|
||||
}
|
||||
|
||||
if (Max.HasValue && value > Max.Value)
|
||||
{
|
||||
errorMessage = Min.HasValue
|
||||
? $"값은 {Min.Value}에서 {Max.Value} 사이의 숫자여야 합니다."
|
||||
: $"값은 {Max.Value} 이하의 숫자여야 합니다.";
|
||||
return false;
|
||||
}
|
||||
|
||||
errorMessage = null!;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,98 @@
|
||||
using System.Collections.Generic;
|
||||
using MelonLoader;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
namespace ADOFAIRenderer.Data
|
||||
{
|
||||
public class PositiveIntegerValidator
|
||||
public class IntegerInputValidator
|
||||
{
|
||||
|
||||
private struct FieldEntry
|
||||
{
|
||||
public List<IntegerLimit> Limits;
|
||||
public bool Optional;
|
||||
}
|
||||
|
||||
private readonly VisualElement _rootVisualElement;
|
||||
private readonly Dictionary<string, FieldEntry> _fields = new();
|
||||
|
||||
public IntegerInputValidator(VisualElement rootVisualElement)
|
||||
{
|
||||
_rootVisualElement = rootVisualElement;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Registers one or more <see cref="IntegerLimit"/>s for the field with the given name.
|
||||
/// Multiple limits can be added to the same field — they are all checked in order.
|
||||
/// </summary>
|
||||
/// <param name="fieldName">The name of the IntegerField in the UXML.</param>
|
||||
/// <param name="optional">
|
||||
/// If true, the field is allowed to be empty. Limits are only enforced when a value is present.
|
||||
/// </param>
|
||||
/// <param name="limits">The limits to apply.</param>
|
||||
public IntegerInputValidator AddLimits(string fieldName, bool optional, params IntegerLimit[] limits)
|
||||
{
|
||||
if (!_fields.TryGetValue(fieldName, out var entry))
|
||||
{
|
||||
entry = new FieldEntry
|
||||
{
|
||||
Limits = new List<IntegerLimit>(),
|
||||
Optional = optional
|
||||
};
|
||||
}
|
||||
|
||||
entry.Optional = optional;
|
||||
entry.Limits.AddRange(limits);
|
||||
_fields[fieldName] = entry;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="AddLimits(string, bool, IntegerLimit[])"/>
|
||||
/// <remarks>Shorthand for required (non-optional) fields.</remarks>
|
||||
public IntegerInputValidator AddLimits(string fieldName, params IntegerLimit[] limits)
|
||||
{
|
||||
return AddLimits(fieldName, false, limits);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Queries all registered fields from the root VisualElement and
|
||||
/// registers value-changed callbacks that rollback invalid values.
|
||||
/// </summary>
|
||||
public void SetupValidation()
|
||||
{
|
||||
foreach (var kvp in _fields)
|
||||
{
|
||||
var fieldName = kvp.Key;
|
||||
var entry = kvp.Value;
|
||||
|
||||
var field = _rootVisualElement.Q<IntegerField>(fieldName);
|
||||
if (field == null)
|
||||
{
|
||||
Melon<RenderMod>.Logger.Warning(
|
||||
$"IntegerField '{fieldName}'을(를) 찾을 수 없습니다. 유효성 검사를 건너뜁니다.");
|
||||
continue;
|
||||
}
|
||||
|
||||
var limits = entry.Limits;
|
||||
var optional = entry.Optional;
|
||||
|
||||
field.RegisterValueChangedCallback(evt =>
|
||||
{
|
||||
// Optional fields allow empty input — skip validation when cleared
|
||||
if (optional && string.IsNullOrEmpty(field.text))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var limit in limits)
|
||||
{
|
||||
if (!limit.Validate(evt.newValue, out _))
|
||||
{
|
||||
field.SetValueWithoutNotify(evt.previousValue);
|
||||
return;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
namespace ADOFAIRenderer.Data
|
||||
{
|
||||
/// <summary>
|
||||
/// Base class for integer input validation limits.
|
||||
/// Multiple limits can be composed on a single input field — all are checked in order.
|
||||
/// </summary>
|
||||
public abstract class IntegerLimit
|
||||
{
|
||||
/// <summary>
|
||||
/// Validates the given integer value against this limit.
|
||||
/// </summary>
|
||||
/// <param name="value">The parsed integer value to validate.</param>
|
||||
/// <param name="errorMessage">
|
||||
/// When validation fails, set to a human-readable error message.
|
||||
/// When validation succeeds, set to null.
|
||||
/// </param>
|
||||
/// <returns>true if the value satisfies this limit; false otherwise.</returns>
|
||||
public abstract bool Validate(int value, out string errorMessage);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Requires the integer value to be strictly positive (> 0).
|
||||
/// </summary>
|
||||
public class PositiveOnlyIntegerLimit : IntegerLimit
|
||||
{
|
||||
public override bool Validate(int value, out string errorMessage)
|
||||
{
|
||||
if (value > 0)
|
||||
{
|
||||
errorMessage = null!;
|
||||
return true;
|
||||
}
|
||||
|
||||
errorMessage = "값은 0보다 큰 양의 정수여야 합니다.";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Requires the integer value to be within an inclusive [Min, Max] range.
|
||||
/// Either bound can be null to represent an unbounded side.
|
||||
/// </summary>
|
||||
public class RangeIntegerLimit : IntegerLimit
|
||||
{
|
||||
public int? Min { get; }
|
||||
public int? Max { get; }
|
||||
|
||||
/// <param name="min">Inclusive lower bound, or null for no lower bound.</param>
|
||||
/// <param name="max">Inclusive upper bound, or null for no upper bound.</param>
|
||||
public RangeIntegerLimit(int? min, int? max)
|
||||
{
|
||||
Min = min;
|
||||
Max = max;
|
||||
}
|
||||
|
||||
public override bool Validate(int value, out string errorMessage)
|
||||
{
|
||||
if (Min.HasValue && value < Min.Value)
|
||||
{
|
||||
errorMessage = Max.HasValue
|
||||
? $"값은 {Min.Value}에서 {Max.Value} 사이의 정수여야 합니다."
|
||||
: $"값은 {Min.Value} 이상의 정수여야 합니다.";
|
||||
return false;
|
||||
}
|
||||
|
||||
if (Max.HasValue && value > Max.Value)
|
||||
{
|
||||
errorMessage = Min.HasValue
|
||||
? $"값은 {Min.Value}에서 {Max.Value} 사이의 정수여야 합니다."
|
||||
: $"값은 {Max.Value} 이하의 정수여야 합니다.";
|
||||
return false;
|
||||
}
|
||||
|
||||
errorMessage = null!;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace ADOFAIRenderer.Core
|
||||
{
|
||||
public class RenderConfigUIManager : MonoBehaviour
|
||||
{
|
||||
void Awake()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user