CatStation/Content.Client/Options/UI/OptionIntInput.cs
2026-08-22 17:36:38 +10:00

82 lines
1.8 KiB
C#

using Robust.Client.AutoGenerated;
using Robust.Client.UserInterface;
using Robust.Client.UserInterface.Controls;
using Robust.Client.UserInterface.XAML;
using Robust.Shared.Maths;
namespace Content.Client.Options.UI;
[GenerateTypedNameReferences]
public sealed partial class OptionIntInput : Control
{
private int _value;
private bool _disabled;
public int MinValue
{
get => (int) Slider.MinValue;
set => Slider.MinValue = value;
}
public int MaxValue
{
get => (int) Slider.MaxValue;
set => Slider.MaxValue = value;
}
public int Value
{
get => _value;
set
{
if (_value == value)
return;
_value = value;
Slider.Value = value;
SpinBox.Value = value;
OnValueChanged?.Invoke(_value);
}
}
public bool Disabled
{
get => _disabled;
set
{
_disabled = value;
Slider.Disabled = value;
SpinBox.LineEditDisabled = value;
SpinBox.SetButtonDisabled(value);
Modulate = value ? Color.FromHex("#FFFFFF80") : Color.White;
}
}
public event Action<int>? OnValueChanged;
public OptionIntInput()
{
RobustXamlLoader.Load(this);
Slider.OnValueChanged += OnSliderValueChanged;
SpinBox.IsValid = ValidateSpinBox;
SpinBox.ValueChanged += OnSpinBoxChanged;
}
private void OnSliderValueChanged(Robust.Client.UserInterface.Controls.Range slider)
{
Value = (int) slider.Value;
}
private void OnSpinBoxChanged(ValueChangedEventArgs e)
{
if (!_disabled)
Value = e.Value;
}
private bool ValidateSpinBox(int value)
{
return !_disabled && value >= MinValue && value <= MaxValue;
}
}