using System.Collections.Generic; using System.Linq; using MelonLoader; using UnityEngine.UIElements; namespace ADOFAIRenderer.Data { public class IntegerInputValidator { private struct FieldEntry { public List Limits; } private readonly VisualElement _rootVisualElement; private readonly Dictionary _fields = new(); public IntegerInputValidator(VisualElement rootVisualElement) { _rootVisualElement = rootVisualElement; } /// /// Registers one or more s for the field with the given name. /// Multiple limits can be added to the same field — they are all checked in order. /// /// The name of the IntegerField in the UXML. /// The limits to apply. public IntegerInputValidator AddLimits(string fieldName, params IntegerLimit[] limits) { if (!_fields.TryGetValue(fieldName, out var entry)) { entry = new FieldEntry { Limits = new List() }; } entry.Limits.AddRange(limits); _fields[fieldName] = entry; return this; } /// /// Queries all registered fields from the root VisualElement and /// registers value-changed callbacks that rollback invalid values. /// public void SetupValidation() { foreach (var (fieldName, entry) in _fields) { var field = _rootVisualElement.Q(fieldName); if (field == null) { Melon.Logger.Warning( $"IntegerField '{fieldName}'을(를) 찾을 수 없습니다. 유효성 검사를 건너뜁니다."); continue; } var limits = entry.Limits; field.RegisterValueChangedCallback(evt => { if (string.IsNullOrEmpty(field.text)) { return; } if (limits.Any(limit => !limit.Validate(evt.newValue, out _))) { field.SetValueWithoutNotify(evt.previousValue); } }); } } } }