Files
tbd-station-14/Content.Client/UserInterface/Controls/ListContainer.cs
Brandon Li 545cacbcae StyleNano removal: Palette system and Sheetlets (#29903)
* Apply patch 1777eea9a4..6b32bb2b14

Signed-off-by: Brandon Li <sirbrandonthenerd@gmail.com>

* make red squiggly line go away

Signed-off-by: Brandon Li <sirbrandonthenerd@gmail.com>

* Add todo list

Signed-off-by: Brandon Li <sirbrandonthenerd@gmail.com>

* Add palette to `TextureButton`

Signed-off-by: Brandon Li <sirbrandonthenerd@gmail.com>

* Rename `PalettedButtonSheetlet` to `NTButtonSheetlet` and move useful methods to `ButtonSheetlet`

Signed-off-by: Brandon Li <sirbrandonthenerd@gmail.com>

* migrate `ContextMenu` styles

Signed-off-by: Brandon Li <sirbrandonthenerd@gmail.com>

* Update todo

Signed-off-by: Brandon Li <sirbrandonthenerd@gmail.com>

* tweak NT colors

* New stylesheet: `InterfaceStylesheet` & `InterfaceTooltipSheetlet`

* Move inheritance of `IButtonConfig` to `NanotransenStylesheet.Buttons`

* move `MenuButtonSheetlet` & actually implement `InterfaceStylesheet` correctly

Signed-off-by: Brandon Li <sirbrandonthenerd@gmail.com>

* tweak color & update todo

Signed-off-by: Brandon Li <sirbrandonthenerd@gmail.com>

* chat is this real (update chat palette)

Signed-off-by: Brandon Li <sirbrandonthenerd@gmail.com>

* Update todo

Signed-off-by: Brandon Li <sirbrandonthenerd@gmail.com>

* `SmallButton` and remove some obsolete things from `StyleNano`

Signed-off-by: Brandon Li <sirbrandonthenerd@gmail.com>

* rename `StyleClasses` to `StyleClass` so `Stylesheets.Redux.StyleClasses` syntax is dead

Signed-off-by: Brandon Li <sirbrandonthenerd@gmail.com>

* replace `ButtonColorGreen` with `Positive`

Signed-off-by: Brandon Li <sirbrandonthenerd@gmail.com>

* `Placeholder`

Signed-off-by: Brandon Li <sirbrandonthenerd@gmail.com>

* Examine popup buttons

Signed-off-by: Brandon Li <sirbrandonthenerd@gmail.com>

* move over more things & cleanup `StyleNano` more (under 1000 lines!!!!)

Signed-off-by: Brandon Li <sirbrandonthenerd@gmail.com>

* Remove some more redundant stuff

Signed-off-by: Brandon Li <sirbrandonthenerd@gmail.com>

* Undo style change for chat window

Signed-off-by: Brandon Li <sirbrandonthenerd@gmail.com>

* paper editing works now

Signed-off-by: Brandon Li <sirbrandonthenerd@gmail.com>

* `OptionButton` styles

Signed-off-by: Brandon Li <sirbrandonthenerd@gmail.com>

* `ListContainer`, move `DefaultWindow` styles (for now) & more cleanup

Signed-off-by: Brandon Li <sirbrandonthenerd@gmail.com>

* fix `ActionButton` not having highlighting

Signed-off-by: Brandon Li <sirbrandonthenerd@gmail.com>

* remove imports of `Robust.Client.UserInterface.StylesheetHelpers` & format

Signed-off-by: Brandon Li <sirbrandonthenerd@gmail.com>

* `ButtonBig` and more cleanup

Signed-off-by: Brandon Li <sirbrandonthenerd@gmail.com>

* Move items inheriting from `ISheetletConfig` into their own directory

Signed-off-by: Brandon Li <sirbrandonthenerd@gmail.com>

* Cleanup & move `Label` styles

Signed-off-by: Brandon Li <sirbrandonthenerd@gmail.com>

* Action search box styles

Signed-off-by: Brandon Li <sirbrandonthenerd@gmail.com>

* Moved, stuff is

Signed-off-by: Brandon Li <sirbrandonthenerd@gmail.com>

* rename `LabelSubtext` to `LabelSubText` & move more stuff (were almost there!!)

Signed-off-by: Brandon Li <sirbrandonthenerd@gmail.com>

* yap & move over MORE stuff (just like one thing left!!!)

Signed-off-by: Brandon Li <sirbrandonthenerd@gmail.com>

* Change status classes to appropriate existing classes

Signed-off-by: Brandon Li <sirbrandonthenerd@gmail.com>

* remove remaining references to `StyleNano`

Signed-off-by: Brandon Li <sirbrandonthenerd@gmail.com>

* Fix some hardcoding & broken code, `GetFromControl`

Signed-off-by: Brandon Li <sirbrandonthenerd@gmail.com>

* Scrollbars!

Signed-off-by: Brandon Li <sirbrandonthenerd@gmail.com>

* chores

Signed-off-by: Brandon Li <sirbrandonthenerd@gmail.com>

* clean up `StyleClass.cs`

Signed-off-by: Brandon Li <sirbrandonthenerd@gmail.com>

* `ItemListSheetlet` refactor

Signed-off-by: Brandon Li <sirbrandonthenerd@gmail.com>

* more chores!

Signed-off-by: Brandon Li <sirbrandonthenerd@gmail.com>

* Consistency w/ directory structure

Signed-off-by: Brandon Li <sirbrandonthenerd@gmail.com>

* Move `MainMenuSheetlet`

Signed-off-by: Brandon Li <sirbrandonthenerd@gmail.com>

* `ColorPalette`

Signed-off-by: Brandon Li <sirbrandonthenerd@gmail.com>

* whoopsie

Signed-off-by: Brandon Li <sirbrandonthenerd@gmail.com>

* Remove most sheet-specific sheetlets

Signed-off-by: Brandon Li <sirbrandonthenerd@gmail.com>

* fix warnings, cleanup, & fix scrollbar (this is why we fix warnings boys)

Signed-off-by: Brandon Li <sirbrandonthenerd@gmail.com>

* yap

Signed-off-by: Brandon Li <sirbrandonthenerd@gmail.com>

* MASSIVE resharper skill issue

Signed-off-by: Brandon Li <sirbrandonthenerd@gmail.com>

* actually use `ISheetletConfig`

Signed-off-by: Brandon Li <sirbrandonthenerd@gmail.com>

* have specific sheetlet be specific

Signed-off-by: Brandon Li <sirbrandonthenerd@gmail.com>

* `GetResourceOr`

Signed-off-by: Brandon Li <sirbrandonthenerd@gmail.com>

* cleanup & move / remove `IPalette`s

Signed-off-by: Brandon Li <sirbrandonthenerd@gmail.com>

* actually do specific stylesheets correctly & fix tooltips

Signed-off-by: Brandon Li <sirbrandonthenerd@gmail.com>

* cleanup & logging

Signed-off-by: Brandon Li <sirbrandonthenerd@gmail.com>

* Move `FontKind` and `FontKindExtensions` to their own files

Signed-off-by: Brandon Li <sirbrandonthenerd@gmail.com>

* rename `InterfaceStylesheet` to `SystemStylesheet`

Signed-off-by: Brandon Li <sirbrandonthenerd@gmail.com>

* change `ButtonHovered` etc to `PseudoHovered` etc

Signed-off-by: Brandon Li <sirbrandonthenerd@gmail.com>

* give the palettes fun names

Signed-off-by: Brandon Li <sirbrandonthenerd@gmail.com>

* `StyleSpace` is no more

Signed-off-by: Brandon Li <sirbrandonthenerd@gmail.com>

* It should compile now! I am now going to bed (fr) if it fails it fails

Signed-off-by: Brandon Li <sirbrandonthenerd@gmail.com>

* make squiggly red line go away

Signed-off-by: Brandon Li <sirbrandonthenerd@gmail.com>

* add additional type restrictions to sheetlets

* `CommonStylesheet`

* minor cleanup

* Make `GetSheetletRules` not horrible

* wait this was duplicating style rules. oops!

* move some sheetlets to their associated xamls

* oh wait apparently that was important

* review pass 1

* review pass 2 (font & color stuff)

* review pass 3: remove unused stuff / filename fix

* fix warnings & "replace cast with explicit variable type"

* move `Palette` stuff to its own directory

* tweak colors (they're different now that I actually fixed the OKlab thing)

* review pass 4: little things

* make window close button grey before hovering

* refactor `HLine` to make it less terrible and allow it to be styled

* fix `NanoHeading` (it's been broken for a while whoops) and cleanup hardcoding

* band-aid missing references in `StyleNano`

* move `StyleBox` generating functions out of `IButtonSheetlet` into `StyleBoxHelper`

* remove dictionary field from `IStylesheetManager`

* Add check for unloaded sheetlets

* style tweaks to satisfy OCD

* I somehow missed this: `Caution` styleclass replaced with `negative`, refactor `PowerChargeWindow`

* tweak palettes for like the fourth time

* construct `StyleNano` / `StyleSpace` in `StylesheetManager` and mark them as obsolete

* rename `BackgroundPanel` classes for consistency

* tweak window / `ListContainer`

* oh right you use `///` not `/**`

* font system is bad, make it temporary

* acknowledge Divider funkyness

* remove use of class `Disabled`

* `ColorPalette` allow overriding colors with brace initialization

* review pass again

* tweak disabled button colors

* `StatusPalette` tweaks

* typo

* Make squiggly red line go away

* Delete `Redux`

* Remove all references to `Redux`

* make red less radioactive

* Store stylesheet name inside stylesheet class

* fix merge errors

* use RT's Oklab support instead

* shuffle around `StylesheetManager` fields

* apply stylesheets based off `StylesheetComponent`

* simplify `ColorPalette` construction

* add todo for `SheetletConfigType`

* `OptionButton` has a background color now

* fix disabled buttons

* sigh (red color palette fixed)

* make `ItemList` use primary palette

* Revert "apply stylesheets based off `StylesheetComponent`"

This reverts commit c05b147da845f6e04ff33d1cbd91a18a92c676d7.

* dead code removal

* buttons are green when pressed (we need togglebuttons)

---------

Signed-off-by: Brandon Li <sirbrandonthenerd@gmail.com>
Co-authored-by: Janet Blackquill <uhhadd@gmail.com>
2025-10-19 21:10:44 +00:00

406 lines
12 KiB
C#

using System.Linq;
using System.Numerics;
using JetBrains.Annotations;
using Robust.Client.Graphics;
using Robust.Client.UserInterface;
using Robust.Client.UserInterface.Controls;
using Robust.Shared.Input;
using Robust.Shared.Map;
namespace Content.Client.UserInterface.Controls;
[Virtual]
public class ListContainer : Control
{
public const string StylePropertySeparation = "separation";
public const string StyleClassListContainerButton = "list-container-button";
public int? SeparationOverride { get; set; }
public bool Group
{
get => _buttonGroup != null;
set => _buttonGroup = value ? new ButtonGroup() : null;
}
public bool Toggle { get; set; }
/// <summary>
/// Called when creating a button on the UI.
/// The provided <see cref="ListContainerButton"/> is the generated button that Controls should be parented to.
/// </summary>
public Action<ListData, ListContainerButton>? GenerateItem;
/// <inheritdoc cref="BaseButton.OnPressed"/>
public Action<BaseButton.ButtonEventArgs, ListData>? ItemPressed;
/// <summary>
/// Invoked when a KeyBind is pressed on a ListContainerButton.
/// </summary>
public Action<GUIBoundKeyEventArgs, ListData>? ItemKeyBindDown;
/// <summary>
/// Invoked when the selected item does not exist in the new data when PopulateList is called.
/// </summary>
public Action? NoItemSelected;
public IReadOnlyList<ListData> Data => _data;
private const int DefaultSeparation = 3;
private readonly VScrollBar _vScrollBar;
private readonly Dictionary<ListData, ListContainerButton> _buttons = new();
private List<ListData> _data = new();
private ListData? _selected;
private float _itemHeight = 0;
private float _totalHeight = 0;
private int _topIndex = 0;
private int _bottomIndex = 0;
private bool _updateChildren = false;
private bool _suppressScrollValueChanged;
private ButtonGroup? _buttonGroup;
public int ScrollSpeedY { get; set; } = 50;
private int ActualSeparation
{
get
{
if (TryGetStyleProperty(StylePropertySeparation, out int separation))
{
return separation;
}
return SeparationOverride ?? DefaultSeparation;
}
}
public ListContainer()
{
HorizontalExpand = true;
VerticalExpand = true;
RectClipContent = true;
MouseFilter = MouseFilterMode.Pass;
_vScrollBar = new VScrollBar
{
HorizontalExpand = false,
HorizontalAlignment = HAlignment.Right
};
AddChild(_vScrollBar);
_vScrollBar.OnValueChanged += ScrollValueChanged;
}
public virtual void PopulateList(IReadOnlyList<ListData> data)
{
if ((_itemHeight == 0 || _data is {Count: 0}) && data.Count > 0)
{
ListContainerButton control = new(data[0], 0);
GenerateItem?.Invoke(data[0], control);
// Yes this AddChild is necessary for reasons (get proper style or whatever?)
// without it the DesiredSize may be different to the final DesiredSize.
AddChild(control);
control.Measure(Vector2Helpers.Infinity);
_itemHeight = control.DesiredSize.Y;
control.Orphan();
}
// Ensure buttons are re-generated.
foreach (var button in _buttons.Values)
{
button.Dispose();
}
_buttons.Clear();
_data = data.ToList();
_updateChildren = true;
InvalidateArrange();
if (_selected != null && !data.Contains(_selected))
{
_selected = null;
NoItemSelected?.Invoke();
}
}
public void DirtyList()
{
_updateChildren = true;
InvalidateArrange();
}
#region Selection
public void Select(ListData data)
{
if (!_data.Contains(data))
return;
if (_buttons.TryGetValue(data, out var button) && Toggle)
button.Pressed = true;
_selected = data;
button ??= new ListContainerButton(data, _data.IndexOf(data));
OnItemPressed(new BaseButton.ButtonEventArgs(button,
new GUIBoundKeyEventArgs(EngineKeyFunctions.UIClick, BoundKeyState.Up,
new ScreenCoordinates(0, 0, WindowId.Main), true, Vector2.Zero, Vector2.Zero)));
}
/*
* Need to implement selecting the first item in code.
* Need to implement updating one entry without having to repopulate
*/
#endregion
private void OnItemPressed(BaseButton.ButtonEventArgs args)
{
if (args.Button is not ListContainerButton button)
return;
_selected = button.Data;
ItemPressed?.Invoke(args, button.Data);
}
private void OnItemKeyBindDown(ListContainerButton button, GUIBoundKeyEventArgs args)
{
ItemKeyBindDown?.Invoke(args, button.Data);
}
[Pure]
private Vector2 GetScrollValue()
{
var v = _vScrollBar.Value;
if (!_vScrollBar.Visible)
{
v = 0;
}
return new Vector2(0, v);
}
protected override Vector2 ArrangeOverride(Vector2 finalSize)
{
#region Scroll
var cHeight = _totalHeight;
var vBarSize = _vScrollBar.DesiredSize.X;
var (finalWidth, finalHeight) = finalSize;
try
{
// Suppress events to avoid weird recursion.
_suppressScrollValueChanged = true;
if (finalHeight < cHeight)
finalWidth -= vBarSize;
if (finalHeight < cHeight)
{
_vScrollBar.Visible = true;
_vScrollBar.Page = finalHeight;
_vScrollBar.MaxValue = cHeight;
}
else
_vScrollBar.Visible = false;
}
finally
{
_suppressScrollValueChanged = false;
}
if (_vScrollBar.Visible)
{
_vScrollBar.Arrange(UIBox2.FromDimensions(Vector2.Zero, finalSize));
}
#endregion
#region Rebuild Children
/*
* Example:
*
* var _itemHeight = 32;
* var separation = 3;
* 32 | 32 | Control.Size.Y 0
* 35 | 3 | Padding
* 67 | 32 | Control.Size.Y 1
* 70 | 3 | Padding
* 102 | 32 | Control.Size.Y 2
* 105 | 3 | Padding
* 137 | 32 | Control.Size.Y 3
*
* If viewport height is 60
* visible should be 2 items (start = 0, end = 1)
*
* scroll.Y = 11
* visible should be 3 items (start = 0, end = 2)
*
* start expected: 11 (item: 0)
* var start = (int) (scroll.Y
*
* if (scroll == 32) then { start = 1 }
* var start = (int) (scroll.Y + separation / (_itemHeight + separation));
* var start = (int) (32 + 3 / (32 + 3));
* var start = (int) (35 / 35);
* var start = (int) (1);
*
* scroll = 0, height = 36
* if (scroll + height == 36) then { end = 2 }
* var end = (int) Math.Ceiling(scroll.Y + height / (_itemHeight + separation));
* var end = (int) Math.Ceiling(0 + 36 / (32 + 3));
* var end = (int) Math.Ceiling(36 / 35);
* var end = (int) Math.Ceiling(1.02857);
* var end = (int) 2;
*
*/
var scroll = GetScrollValue();
var oldTopIndex = _topIndex;
_topIndex = (int) ((scroll.Y + ActualSeparation) / (_itemHeight + ActualSeparation));
if (_topIndex != oldTopIndex)
_updateChildren = true;
var oldBottomIndex = _bottomIndex;
_bottomIndex = (int) Math.Ceiling((scroll.Y + finalHeight) / (_itemHeight + ActualSeparation));
_bottomIndex = Math.Min(_bottomIndex, _data.Count);
if (_bottomIndex != oldBottomIndex)
_updateChildren = true;
// When scrolling only rebuild visible list when a new item should be visible
if (_updateChildren)
{
_updateChildren = false;
var toRemove = new Dictionary<ListData, ListContainerButton>(_buttons);
if (_data.Count > 0)
{
for (var i = _topIndex; i < _bottomIndex; i++)
{
var data = _data[i];
if (_buttons.TryGetValue(data, out var button))
toRemove.Remove(data);
else
{
button = new ListContainerButton(data, i);
button.OnPressed += OnItemPressed;
button.OnKeyBindDown += args => OnItemKeyBindDown(button, args);
button.ToggleMode = Toggle;
button.Group = _buttonGroup;
GenerateItem?.Invoke(data, button);
_buttons.Add(data, button);
if (Toggle && data == _selected)
button.Pressed = true;
AddChild(button);
}
button.SetPositionInParent(i - _topIndex);
button.Measure(finalSize);
}
}
foreach (var (data, button) in toRemove)
{
_buttons.Remove(data);
button.Dispose();
}
_vScrollBar.SetPositionLast();
}
#endregion
#region Layout Children
// Use pixel position
var pixelWidth = (int)(finalWidth * UIScale);
var pixelSeparation = (int) (ActualSeparation * UIScale);
var pixelOffset = (int) -((scroll.Y - _topIndex * (_itemHeight + ActualSeparation)) * UIScale);
var first = true;
foreach (var child in Children)
{
if (child == _vScrollBar)
continue;
if (!first)
pixelOffset += pixelSeparation;
first = false;
var pixelSize = child.DesiredPixelSize.Y;
var targetBox = new UIBox2i(0, pixelOffset, pixelWidth, pixelOffset + pixelSize);
child.ArrangePixel(targetBox);
pixelOffset += pixelSize;
}
#endregion
return finalSize;
}
protected override Vector2 MeasureOverride(Vector2 availableSize)
{
_vScrollBar.Measure(availableSize);
availableSize.X -= _vScrollBar.DesiredSize.X;
var constraint = new Vector2(availableSize.X, float.PositiveInfinity);
var childSize = Vector2.Zero;
foreach (var child in Children)
{
child.Measure(constraint);
if (child == _vScrollBar)
continue;
childSize = Vector2.Max(childSize, child.DesiredSize);
}
if (_itemHeight == 0 && childSize.Y != 0)
_itemHeight = childSize.Y;
_totalHeight = _itemHeight * _data.Count + ActualSeparation * (_data.Count - 1);
return new Vector2(childSize.X, 0);
}
private void ScrollValueChanged(Robust.Client.UserInterface.Controls.Range _)
{
if (_suppressScrollValueChanged)
{
return;
}
InvalidateArrange();
}
protected override void MouseWheel(GUIMouseWheelEventArgs args)
{
base.MouseWheel(args);
_vScrollBar.ValueTarget -= args.Delta.Y * ScrollSpeedY;
args.Handle();
}
}
public sealed class ListContainerButton : ContainerButton, IEntityControl
{
public readonly ListData Data;
public readonly int Index;
// public PanelContainer Background;
public ListContainerButton(ListData data, int index)
{
AddStyleClass(StyleClassButton);
Data = data;
Index = index;
StyleBoxOverride = new StyleBoxFlat(Color.White);
// AddChild(Background = new PanelContainer
// {
// HorizontalExpand = true,
// VerticalExpand = true,
// PanelOverride = new StyleBoxFlat {BackgroundColor = new Color(55, 55, 68)}
// });
}
public EntityUid? UiEntity => (Data as EntityListData)?.Uid;
}
#region Data
public abstract record ListData;
public record EntityListData(EntityUid Uid) : ListData;
#endregion