Skip to content

Commit

Permalink
spin box changes
Browse files Browse the repository at this point in the history
  • Loading branch information
EmoGarbage404 committed Jun 8, 2024
1 parent fcd507d commit a8a3026
Showing 1 changed file with 38 additions and 12 deletions.
50 changes: 38 additions & 12 deletions Robust.Client/UserInterface/Controls/SpinBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ public class SpinBox : BoxContainer
public const string RightButtonStyle = "spinbox-right";
public const string MiddleButtonStyle = "spinbox-middle";
public LineEdit LineEditControl { get; }
private List<Button> _leftButtons = new();
private List<Button> _rightButtons = new();
private List<SpinBoxButton> _leftButtons = new();
private List<SpinBoxButton> _rightButtons = new();
private int _stepSize = 1;
private bool _buttonsDisabled;

/// <summary>
/// Determines whether the SpinBox value gets changed by the input text.
Expand All @@ -30,12 +31,7 @@ public int Value
get => _value;
set
{
if (IsValid != null && !IsValid(value))
{
return;
}
_value = value;
LineEditControl.Text = value.ToString();
OverrideValue(value);
ValueChanged?.Invoke(new ValueChangedEventArgs(value));
}
}
Expand All @@ -52,6 +48,7 @@ public void OverrideValue(int value)
return;
}
_value = value;
UpdateButtonCanPress();
LineEditControl.Text = value.ToString();
}

Expand Down Expand Up @@ -87,15 +84,16 @@ public void InitDefaultButtons()
ClearButtons();
AddLeftButton(-1, "-");
AddRightButton(1, "+");
UpdateButtonCanPress();
}

/// <summary>
/// Adds a button to the right of the SpinBox LineEdit.
/// </summary>
public void AddRightButton(int num, string text)
{
var button = new Button { Text = text };
button.OnPressed += (args) => Value += num;
var button = new SpinBoxButton(num) { Text = text };
button.OnPressed += _ => Value += num;
AddChild(button);
button.AddStyleClass(RightButtonStyle);
if (_rightButtons.Count > 0)
Expand All @@ -111,8 +109,8 @@ public void AddRightButton(int num, string text)
/// </summary>
public void AddLeftButton(int num, string text)
{
var button = new Button { Text = text };
button.OnPressed += (args) => Value += num;
var button = new SpinBoxButton(num) { Text = text };
button.OnPressed += _ => Value += num;
AddChild(button);
button.SetPositionInParent(_leftButtons.Count);
button.AddStyleClass(_leftButtons.Count == 0 ? LeftButtonStyle : MiddleButtonStyle);
Expand Down Expand Up @@ -162,6 +160,24 @@ public void SetButtonDisabled(bool disabled)
{
rightButton.Disabled = disabled;
}

_buttonsDisabled = disabled;
}

private void UpdateButtonCanPress()
{
if (IsValid == null)
return;

foreach (var button in _leftButtons)
{
button.Disabled = !IsValid(_value + button.Value) || _buttonsDisabled;
}

foreach (var button in _rightButtons)
{
button.Disabled = !IsValid(_value + button.Value) || _buttonsDisabled;
}
}

/// <summary>
Expand Down Expand Up @@ -206,4 +222,14 @@ public ValueChangedEventArgs(int value)
Value = value;
}
}

public sealed class SpinBoxButton : Button
{
public readonly int Value;

public SpinBoxButton(int value)
{
Value = value;
}
}
}

0 comments on commit a8a3026

Please sign in to comment.